]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/x509.py
cert-selfsigned-example.py creates more correct CA
[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 IA5String
27 from pyderasn import Integer
28 from pyderasn import ObjectIdentifier
29 from pyderasn import OctetString
30 from pyderasn import PrintableString
31 from pyderasn import Sequence
32 from pyderasn import SequenceOf
33 from pyderasn import SetOf
34 from pyderasn import tag_ctxc
35 from pyderasn import tag_ctxp
36 from pyderasn import TeletexString
37 from pyderasn import UTCTime
38
39 from pygost.asn1schemas.oids import id_at_commonName
40 from pygost.asn1schemas.oids import id_at_countryName
41 from pygost.asn1schemas.oids import id_at_localityName
42 from pygost.asn1schemas.oids import id_at_organizationName
43 from pygost.asn1schemas.oids import id_at_stateOrProvinceName
44
45
46 class Version(Integer):
47     schema = (
48         ("v1", 0),
49         ("v2", 1),
50         ("v3", 2),
51     )
52
53
54 class CertificateSerialNumber(Integer):
55     pass
56
57
58 class AlgorithmIdentifier(Sequence):
59     schema = (
60         ("algorithm", ObjectIdentifier()),
61         ("parameters", Any(optional=True)),
62     )
63
64
65 class AttributeType(ObjectIdentifier):
66     pass
67
68
69 class AttributeValue(Any):
70     pass
71
72
73 class OrganizationName(Choice):
74     schema = (
75         ("printableString", PrintableString()),
76         ("teletexString", TeletexString()),
77     )
78
79
80 class AttributeTypeAndValue(Sequence):
81     schema = (
82         ("type", AttributeType(defines=(((".", "value"), {
83             id_at_countryName: PrintableString(),
84             id_at_stateOrProvinceName: PrintableString(),
85             id_at_localityName: PrintableString(),
86             id_at_organizationName: OrganizationName(),
87             id_at_commonName: PrintableString(),
88         }),))),
89         ("value", AttributeValue()),
90     )
91
92
93 class RelativeDistinguishedName(SetOf):
94     schema = AttributeTypeAndValue()
95     bounds = (1, float("+inf"))
96
97
98 class RDNSequence(SequenceOf):
99     schema = RelativeDistinguishedName()
100
101
102 class Name(Choice):
103     schema = (
104         ("rdnSequence", RDNSequence()),
105     )
106
107
108 class Time(Choice):
109     schema = (
110         ("utcTime", UTCTime()),
111         ("generalTime", GeneralizedTime()),
112     )
113
114
115 class Validity(Sequence):
116     schema = (
117         ("notBefore", Time()),
118         ("notAfter", Time()),
119     )
120
121
122 class GostR34102012PublicKeyParameters(Sequence):
123     schema = (
124         ("publicKeyParamSet", ObjectIdentifier()),
125         ("digestParamSet", ObjectIdentifier(optional=True)),
126     )
127
128
129 class SubjectPublicKeyInfo(Sequence):
130     schema = (
131         ("algorithm", AlgorithmIdentifier()),
132         ("subjectPublicKey", BitString()),
133     )
134
135
136 class UniqueIdentifier(BitString):
137     pass
138
139
140 class KeyIdentifier(OctetString):
141     pass
142
143
144 class SubjectKeyIdentifier(KeyIdentifier):
145     pass
146
147
148 class BasicConstraints(Sequence):
149     schema = (
150         ("cA", Boolean(default=False)),
151         # ("pathLenConstraint", PathLenConstraint(optional=True)),
152     )
153
154
155 class Extension(Sequence):
156     schema = (
157         ("extnID", ObjectIdentifier()),
158         ("critical", Boolean(default=False)),
159         ("extnValue", OctetString()),
160     )
161
162
163 class Extensions(SequenceOf):
164     schema = Extension()
165     bounds = (1, float("+inf"))
166
167
168 class TBSCertificate(Sequence):
169     schema = (
170         ("version", Version(expl=tag_ctxc(0), default="v1")),
171         ("serialNumber", CertificateSerialNumber()),
172         ("signature", AlgorithmIdentifier()),
173         ("issuer", Name()),
174         ("validity", Validity()),
175         ("subject", Name()),
176         ("subjectPublicKeyInfo", SubjectPublicKeyInfo()),
177         ("issuerUniqueID", UniqueIdentifier(impl=tag_ctxp(1), optional=True)),
178         ("subjectUniqueID", UniqueIdentifier(impl=tag_ctxp(2), optional=True)),
179         ("extensions", Extensions(expl=tag_ctxc(3), optional=True)),
180     )
181
182
183 class Certificate(Sequence):
184     schema = (
185         ("tbsCertificate", TBSCertificate()),
186         ("signatureAlgorithm", AlgorithmIdentifier()),
187         ("signatureValue", BitString()),
188     )
189
190
191 class RevokedCertificates(SequenceOf):
192     # schema = RevokedCertificate()
193     schema = OctetString()  # dummy
194
195
196 class TBSCertList(Sequence):
197     schema = (
198         ("version", Version(optional=True)),
199         ("signature", AlgorithmIdentifier()),
200         ("issuer", Name()),
201         ("thisUpdate", Time()),
202         ("nextUpdate", Time(optional=True)),
203         ("revokedCertificates", RevokedCertificates(optional=True)),
204         ("crlExtensions", Extensions(expl=tag_ctxc(0), optional=True)),
205     )
206
207
208 class CertificateList(Sequence):
209     schema = (
210         ("tbsCertList", TBSCertList()),
211         ("signatureAlgorithm", AlgorithmIdentifier()),
212         ("signatureValue", BitString()),
213     )
214
215
216 class GeneralName(Choice):
217     schema = (
218         # ("otherName", AnotherName(impl=tag_ctxc(0))),
219         # ("rfc822Name", IA5String(impl=tag_ctxp(1))),
220         ("dNSName", IA5String(impl=tag_ctxp(2))),
221         # ("x400Address", ORAddress(impl=tag_ctxp(3))),
222         # ("x400Address", OctetString(impl=tag_ctxp(3))),
223         # ("directoryName", Name(expl=tag_ctxc(4))),
224         # ("ediPartyName", EDIPartyName(impl=tag_ctxc(5))),
225         # ("uniformResourceIdentifier", IA5String(impl=tag_ctxp(6))),
226         # ("iPAddress", OctetString(impl=tag_ctxp(7))),
227         # ("registeredID", ObjectIdentifier(impl=tag_ctxp(8))),
228     )
229
230
231 class GeneralNames(SequenceOf):
232     schema = GeneralName()
233     bounds = (1, float("+inf"))
234
235
236 class SubjectAltName(GeneralNames):
237     pass
238
239
240 class AuthorityKeyIdentifier(Sequence):
241     schema = (
242         ("keyIdentifier", KeyIdentifier(impl=tag_ctxp(0), optional=True)),
243         # ("authorityCertIssuer", GeneralNames(impl=tag_ctxc(1), optional=True)),
244         # (
245         #     "authorityCertSerialNumber",
246         #     CertificateSerialNumber(impl=tag_ctxp(2), optional=True),
247         # ),
248     )
249
250
251 class KeyUsage(BitString):
252     schema = (
253         ("digitalSignature", 0),
254         ("nonRepudiation", 1),
255         ("keyEncipherment", 2),
256         ("dataEncipherment", 3),
257         ("keyAgreement", 4),
258         ("keyCertSign", 5),
259         ("cRLSign", 6),
260         ("encipherOnly", 7),
261         ("decipherOnly", 8),
262     )