]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/x509.py
188ef3a6126ee3a1d02f5ede5e97ea18bc6572ad
[pygost.git] / pygost / asn1schemas / x509.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2020 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
39 class Version(Integer):
40     schema = (
41         ("v1", 0),
42         ("v2", 1),
43         ("v3", 2),
44     )
45
46
47 class CertificateSerialNumber(Integer):
48     pass
49
50
51 class AlgorithmIdentifier(Sequence):
52     schema = (
53         ("algorithm", ObjectIdentifier()),
54         ("parameters", Any(optional=True)),
55     )
56
57
58 class AttributeType(ObjectIdentifier):
59     pass
60
61
62 class AttributeValue(Any):
63     pass
64
65
66 class OrganizationName(Choice):
67     schema = (
68         ("printableString", PrintableString()),
69         ("teletexString", TeletexString()),
70     )
71
72
73 class AttributeTypeAndValue(Sequence):
74     schema = (
75         ("type", AttributeType(defines=(((".", "value"), {
76             ObjectIdentifier("2.5.4.6"): PrintableString(),
77             ObjectIdentifier("2.5.4.8"): PrintableString(),
78             ObjectIdentifier("2.5.4.7"): PrintableString(),
79             ObjectIdentifier("2.5.4.10"): OrganizationName(),
80             ObjectIdentifier("2.5.4.3"): PrintableString(),
81         }),))),
82         ("value", AttributeValue()),
83     )
84
85
86 class RelativeDistinguishedName(SetOf):
87     schema = AttributeTypeAndValue()
88     bounds = (1, float("+inf"))
89
90
91 class RDNSequence(SequenceOf):
92     schema = RelativeDistinguishedName()
93
94
95 class Name(Choice):
96     schema = (
97         ("rdnSequence", RDNSequence()),
98     )
99
100
101 class Time(Choice):
102     schema = (
103         ("utcTime", UTCTime()),
104         ("generalTime", GeneralizedTime()),
105     )
106
107
108 class Validity(Sequence):
109     schema = (
110         ("notBefore", Time()),
111         ("notAfter", Time()),
112     )
113
114
115 class GostR34102012PublicKeyParameters(Sequence):
116     schema = (
117         ("publicKeyParamSet", ObjectIdentifier()),
118         ("digestParamSet", ObjectIdentifier(optional=True)),
119     )
120
121
122 class SubjectPublicKeyInfo(Sequence):
123     schema = (
124         ("algorithm", AlgorithmIdentifier()),
125         ("subjectPublicKey", BitString()),
126     )
127
128
129 class UniqueIdentifier(BitString):
130     pass
131
132
133 class KeyIdentifier(OctetString):
134     pass
135
136
137 class SubjectKeyIdentifier(KeyIdentifier):
138     pass
139
140
141 class BasicConstraints(Sequence):
142     schema = (
143         ("cA", Boolean(default=False)),
144         # ("pathLenConstraint", PathLenConstraint(optional=True)),
145     )
146
147
148 class Extension(Sequence):
149     schema = (
150         ("extnID", ObjectIdentifier()),
151         ("critical", Boolean(default=False)),
152         ("extnValue", OctetString()),
153     )
154
155
156 class Extensions(SequenceOf):
157     schema = Extension()
158     bounds = (1, float("+inf"))
159
160
161 class TBSCertificate(Sequence):
162     schema = (
163         ("version", Version(expl=tag_ctxc(0), default="v1")),
164         ("serialNumber", CertificateSerialNumber()),
165         ("signature", AlgorithmIdentifier()),
166         ("issuer", Name()),
167         ("validity", Validity()),
168         ("subject", Name()),
169         ("subjectPublicKeyInfo", SubjectPublicKeyInfo()),
170         ("issuerUniqueID", UniqueIdentifier(impl=tag_ctxp(1), optional=True)),
171         ("subjectUniqueID", UniqueIdentifier(impl=tag_ctxp(2), optional=True)),
172         ("extensions", Extensions(expl=tag_ctxc(3), optional=True)),
173     )
174
175
176 class Certificate(Sequence):
177     schema = (
178         ("tbsCertificate", TBSCertificate()),
179         ("signatureAlgorithm", AlgorithmIdentifier()),
180         ("signatureValue", BitString()),
181     )
182
183
184 class RevokedCertificates(SequenceOf):
185     # schema = RevokedCertificate()
186     schema = OctetString()  # dummy
187
188
189 class TBSCertList(Sequence):
190     schema = (
191         ("version", Version(optional=True)),
192         ("signature", AlgorithmIdentifier()),
193         ("issuer", Name()),
194         ("thisUpdate", Time()),
195         ("nextUpdate", Time(optional=True)),
196         ("revokedCertificates", RevokedCertificates(optional=True)),
197         ("crlExtensions", Extensions(expl=tag_ctxc(0), optional=True)),
198     )
199
200
201 class CertificateList(Sequence):
202     schema = (
203         ("tbsCertList", TBSCertList()),
204         ("signatureAlgorithm", AlgorithmIdentifier()),
205         ("signatureValue", BitString()),
206     )