]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/x509.py
Move x509's OIDs to oid module
[pygost.git] / pygost / asn1schemas / x509.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2021 Sergey Matveev <stargrave@stargrave.org>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 """:rfc:`5280` related structures (**NOT COMPLETE**)
17
18 They are taken from `PyDERASN <http://www.pyderasn.cypherpunks.ru/`__ tests.
19 """
20
21 from pyderasn import Any
22 from pyderasn import BitString
23 from pyderasn import Boolean
24 from pyderasn import Choice
25 from pyderasn import GeneralizedTime
26 from pyderasn import Integer
27 from pyderasn import ObjectIdentifier
28 from pyderasn import OctetString
29 from pyderasn import PrintableString
30 from pyderasn import Sequence
31 from pyderasn import SequenceOf
32 from pyderasn import SetOf
33 from pyderasn import tag_ctxc
34 from pyderasn import tag_ctxp
35 from pyderasn import TeletexString
36 from pyderasn import UTCTime
37
38 from pygost.asn1schemas.oids import id_at_commonName
39 from pygost.asn1schemas.oids import id_at_countryName
40 from pygost.asn1schemas.oids import id_at_localityName
41 from pygost.asn1schemas.oids import id_at_organizationName
42 from pygost.asn1schemas.oids import id_at_stateOrProvinceName
43
44
45 class Version(Integer):
46     schema = (
47         ("v1", 0),
48         ("v2", 1),
49         ("v3", 2),
50     )
51
52
53 class CertificateSerialNumber(Integer):
54     pass
55
56
57 class AlgorithmIdentifier(Sequence):
58     schema = (
59         ("algorithm", ObjectIdentifier()),
60         ("parameters", Any(optional=True)),
61     )
62
63
64 class AttributeType(ObjectIdentifier):
65     pass
66
67
68 class AttributeValue(Any):
69     pass
70
71
72 class OrganizationName(Choice):
73     schema = (
74         ("printableString", PrintableString()),
75         ("teletexString", TeletexString()),
76     )
77
78
79 class AttributeTypeAndValue(Sequence):
80     schema = (
81         ("type", AttributeType(defines=(((".", "value"), {
82             id_at_countryName: PrintableString(),
83             id_at_stateOrProvinceName: PrintableString(),
84             id_at_localityName: PrintableString(),
85             id_at_organizationName: OrganizationName(),
86             id_at_commonName: PrintableString(),
87         }),))),
88         ("value", AttributeValue()),
89     )
90
91
92 class RelativeDistinguishedName(SetOf):
93     schema = AttributeTypeAndValue()
94     bounds = (1, float("+inf"))
95
96
97 class RDNSequence(SequenceOf):
98     schema = RelativeDistinguishedName()
99
100
101 class Name(Choice):
102     schema = (
103         ("rdnSequence", RDNSequence()),
104     )
105
106
107 class Time(Choice):
108     schema = (
109         ("utcTime", UTCTime()),
110         ("generalTime", GeneralizedTime()),
111     )
112
113
114 class Validity(Sequence):
115     schema = (
116         ("notBefore", Time()),
117         ("notAfter", Time()),
118     )
119
120
121 class GostR34102012PublicKeyParameters(Sequence):
122     schema = (
123         ("publicKeyParamSet", ObjectIdentifier()),
124         ("digestParamSet", ObjectIdentifier(optional=True)),
125     )
126
127
128 class SubjectPublicKeyInfo(Sequence):
129     schema = (
130         ("algorithm", AlgorithmIdentifier()),
131         ("subjectPublicKey", BitString()),
132     )
133
134
135 class UniqueIdentifier(BitString):
136     pass
137
138
139 class KeyIdentifier(OctetString):
140     pass
141
142
143 class SubjectKeyIdentifier(KeyIdentifier):
144     pass
145
146
147 class BasicConstraints(Sequence):
148     schema = (
149         ("cA", Boolean(default=False)),
150         # ("pathLenConstraint", PathLenConstraint(optional=True)),
151     )
152
153
154 class Extension(Sequence):
155     schema = (
156         ("extnID", ObjectIdentifier()),
157         ("critical", Boolean(default=False)),
158         ("extnValue", OctetString()),
159     )
160
161
162 class Extensions(SequenceOf):
163     schema = Extension()
164     bounds = (1, float("+inf"))
165
166
167 class TBSCertificate(Sequence):
168     schema = (
169         ("version", Version(expl=tag_ctxc(0), default="v1")),
170         ("serialNumber", CertificateSerialNumber()),
171         ("signature", AlgorithmIdentifier()),
172         ("issuer", Name()),
173         ("validity", Validity()),
174         ("subject", Name()),
175         ("subjectPublicKeyInfo", SubjectPublicKeyInfo()),
176         ("issuerUniqueID", UniqueIdentifier(impl=tag_ctxp(1), optional=True)),
177         ("subjectUniqueID", UniqueIdentifier(impl=tag_ctxp(2), optional=True)),
178         ("extensions", Extensions(expl=tag_ctxc(3), optional=True)),
179     )
180
181
182 class Certificate(Sequence):
183     schema = (
184         ("tbsCertificate", TBSCertificate()),
185         ("signatureAlgorithm", AlgorithmIdentifier()),
186         ("signatureValue", BitString()),
187     )
188
189
190 class RevokedCertificates(SequenceOf):
191     # schema = RevokedCertificate()
192     schema = OctetString()  # dummy
193
194
195 class TBSCertList(Sequence):
196     schema = (
197         ("version", Version(optional=True)),
198         ("signature", AlgorithmIdentifier()),
199         ("issuer", Name()),
200         ("thisUpdate", Time()),
201         ("nextUpdate", Time(optional=True)),
202         ("revokedCertificates", RevokedCertificates(optional=True)),
203         ("crlExtensions", Extensions(expl=tag_ctxc(0), optional=True)),
204     )
205
206
207 class CertificateList(Sequence):
208     schema = (
209         ("tbsCertList", TBSCertList()),
210         ("signatureAlgorithm", AlgorithmIdentifier()),
211         ("signatureValue", BitString()),
212     )