]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/x509.py
7e3938591e4e0c70d7e8c5396358333c47c470c5
[pygost.git] / pygost / asn1schemas / x509.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 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, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 """:rfc:`5280` related structures (**NOT COMPLETE**)
18
19 They are taken from `PyDERASN <http://pyderasn.cypherpunks.ru/`__ tests.
20 """
21
22 from pyderasn import Any
23 from pyderasn import BitString
24 from pyderasn import Boolean
25 from pyderasn import Choice
26 from pyderasn import GeneralizedTime
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
40 class Version(Integer):
41     schema = (
42         ("v1", 0),
43         ("v2", 1),
44         ("v3", 2),
45     )
46
47
48 class CertificateSerialNumber(Integer):
49     pass
50
51
52 class AlgorithmIdentifier(Sequence):
53     schema = (
54         ("algorithm", ObjectIdentifier()),
55         ("parameters", Any(optional=True)),
56     )
57
58
59 class AttributeType(ObjectIdentifier):
60     pass
61
62
63 class AttributeValue(Any):
64     pass
65
66
67 class OrganizationName(Choice):
68     schema = (
69         ("printableString", PrintableString()),
70         ("teletexString", TeletexString()),
71     )
72
73
74 class AttributeTypeAndValue(Sequence):
75     schema = (
76         ("type", AttributeType(defines=(((".", "value"), {
77             ObjectIdentifier("2.5.4.6"): PrintableString(),
78             ObjectIdentifier("2.5.4.8"): PrintableString(),
79             ObjectIdentifier("2.5.4.7"): PrintableString(),
80             ObjectIdentifier("2.5.4.10"): OrganizationName(),
81             ObjectIdentifier("2.5.4.3"): PrintableString(),
82         }),))),
83         ("value", AttributeValue()),
84     )
85
86
87 class RelativeDistinguishedName(SetOf):
88     schema = AttributeTypeAndValue()
89     bounds = (1, float("+inf"))
90
91
92 class RDNSequence(SequenceOf):
93     schema = RelativeDistinguishedName()
94
95
96 class Name(Choice):
97     schema = (
98         ("rdnSequence", RDNSequence()),
99     )
100
101
102 class Time(Choice):
103     schema = (
104         ("utcTime", UTCTime()),
105         ("generalTime", GeneralizedTime()),
106     )
107
108
109 class Validity(Sequence):
110     schema = (
111         ("notBefore", Time()),
112         ("notAfter", Time()),
113     )
114
115
116 class SubjectPublicKeyInfo(Sequence):
117     schema = (
118         ("algorithm", AlgorithmIdentifier()),
119         ("subjectPublicKey", BitString()),
120     )
121
122
123 class UniqueIdentifier(BitString):
124     pass
125
126
127 class Extension(Sequence):
128     schema = (
129         ("extnID", ObjectIdentifier()),
130         ("critical", Boolean(default=False)),
131         ("extnValue", OctetString()),
132     )
133
134
135 class Extensions(SequenceOf):
136     schema = Extension()
137     bounds = (1, float("+inf"))
138
139
140 class TBSCertificate(Sequence):
141     schema = (
142         ("version", Version(expl=tag_ctxc(0), default="v1")),
143         ("serialNumber", CertificateSerialNumber()),
144         ("signature", AlgorithmIdentifier()),
145         ("issuer", Name()),
146         ("validity", Validity()),
147         ("subject", Name()),
148         ("subjectPublicKeyInfo", SubjectPublicKeyInfo()),
149         ("issuerUniqueID", UniqueIdentifier(impl=tag_ctxp(1), optional=True)),
150         ("subjectUniqueID", UniqueIdentifier(impl=tag_ctxp(2), optional=True)),
151         ("extensions", Extensions(expl=tag_ctxc(3), optional=True)),
152     )
153
154
155 class Certificate(Sequence):
156     schema = (
157         ("tbsCertificate", TBSCertificate()),
158         ("signatureAlgorithm", AlgorithmIdentifier()),
159         ("signatureValue", BitString()),
160     )