]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/cms.py
More TC26 ASN.1 test vectors
[pygost.git] / pygost / asn1schemas / cms.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2018 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 """CMS related structures (**NOT COMPLETE**)
18 """
19
20 from pyderasn import Any
21 from pyderasn import BitString
22 from pyderasn import Choice
23 from pyderasn import Integer
24 from pyderasn import ObjectIdentifier
25 from pyderasn import OctetString
26 from pyderasn import Sequence
27 from pyderasn import SequenceOf
28 from pyderasn import SetOf
29 from pyderasn import tag_ctxc
30 from pyderasn import tag_ctxp
31
32 from pygost.asn1schemas.x509 import AlgorithmIdentifier
33 from pygost.asn1schemas.x509 import SubjectPublicKeyInfo
34
35
36 class CMSVersion(Integer):
37     pass
38
39
40 class ContentType(ObjectIdentifier):
41     pass
42
43
44 class RecipientIdentifier(Choice):
45     schema = (
46         ("issuerAndSerialNumber", Any()),
47         # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
48     )
49
50
51 class KeyEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
52     pass
53
54
55 class EncryptedKey(OctetString):
56     pass
57
58
59 class KeyTransRecipientInfo(Sequence):
60     schema = (
61         ("version", CMSVersion()),
62         ("rid", RecipientIdentifier()),
63         ("keyEncryptionAlgorithm", KeyEncryptionAlgorithmIdentifier()),
64         ("encryptedKey", EncryptedKey()),
65     )
66
67
68 class OriginatorPublicKey(Sequence):
69     schema = (
70         ("algorithm", AlgorithmIdentifier()),
71         ("publicKey", BitString()),
72     )
73
74
75 class OriginatorIdentifierOrKey(Choice):
76     schema = (
77         # ("issuerAndSerialNumber", IssuerAndSerialNumber()),
78         # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
79         ("originatorKey", OriginatorPublicKey(impl=tag_ctxc(1))),
80     )
81
82
83 class UserKeyingMaterial(OctetString):
84     pass
85
86
87 class KeyAgreeRecipientIdentifier(Choice):
88     schema = (
89         ("issuerAndSerialNumber", Any()),
90         # ("rKeyId", RecipientKeyIdentifier(impl=tag_ctxc(0))),
91     )
92
93
94 class RecipientEncryptedKey(Sequence):
95     schema = (
96         ("rid", KeyAgreeRecipientIdentifier()),
97         ("encryptedKey", EncryptedKey()),
98     )
99
100
101 class RecipientEncryptedKeys(SequenceOf):
102     schema = RecipientEncryptedKey()
103
104
105 class KeyAgreeRecipientInfo(Sequence):
106     schema = (
107         ("version", CMSVersion(3)),
108         ("originator", OriginatorIdentifierOrKey(expl=tag_ctxc(0))),
109         ("ukm", UserKeyingMaterial(expl=tag_ctxc(1), optional=True)),
110         ("keyEncryptionAlgorithm", KeyEncryptionAlgorithmIdentifier()),
111         ("recipientEncryptedKeys", RecipientEncryptedKeys()),
112     )
113
114
115 class RecipientInfo(Choice):
116     schema = (
117         ("ktri", KeyTransRecipientInfo()),
118         ("kari", KeyAgreeRecipientInfo(impl=tag_ctxc(1))),
119         # ("kekri", KEKRecipientInfo(impl=tag_ctxc(2))),
120         # ("pwri", PasswordRecipientInfo(impl=tag_ctxc(3))),
121         # ("ori", OtherRecipientInfo(impl=tag_ctxc(4))),
122     )
123
124
125 class RecipientInfos(SetOf):
126     schema = RecipientInfo()
127     bounds = (1, float("+inf"))
128
129
130 class ContentEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
131     pass
132
133
134 class EncryptedContent(OctetString):
135     pass
136
137
138 class EncryptedContentInfo(Sequence):
139     schema = (
140         ("contentType", ContentType()),
141         ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
142         ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
143     )
144
145
146 class EnvelopedData(Sequence):
147     schema = (
148         ("version", CMSVersion()),
149         # ("originatorInfo", OriginatorInfo(impl=tag_ctxc(0), optional=True)),
150         ("recipientInfos", RecipientInfos()),
151         ("encryptedContentInfo", EncryptedContentInfo()),
152         # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
153     )
154
155
156 class ContentInfo(Sequence):
157     schema = (
158         ("contentType", ContentType()),
159         ("content", Any(expl=tag_ctxc(0))),
160     )
161
162
163 class Gost2814789IV(OctetString):
164     bounds = (8, 8)
165
166
167 class Gost2814789Parameters(Sequence):
168     schema = (
169         ("iv", Gost2814789IV()),
170         ("encryptionParamSet", ObjectIdentifier()),
171     )
172
173
174 class Gost2814789Key(OctetString):
175     bounds = (32, 32)
176
177
178 class Gost2814789MAC(OctetString):
179     bounds = (4, 4)
180
181
182 class Gost2814789EncryptedKey(Sequence):
183     schema = (
184         ("encryptedKey", Gost2814789Key()),
185         ("maskKey", Gost2814789Key(impl=tag_ctxp(0), optional=True)),
186         ("macKey", Gost2814789MAC()),
187     )
188
189
190 class GostR34102001TransportParameters(Sequence):
191     schema = (
192         ("encryptionParamSet", ObjectIdentifier()),
193         ("ephemeralPublicKey", SubjectPublicKeyInfo(
194             impl=tag_ctxc(0),
195             optional=True,
196         )),
197         ("ukm", OctetString()),
198     )
199
200
201 class GostR3410KeyTransport(Sequence):
202     schema = (
203         ("sessionEncryptedKey", Gost2814789EncryptedKey()),
204         ("transportParameters", GostR34102001TransportParameters(
205             impl=tag_ctxc(0),
206             optional=True,
207         )),
208     )
209
210
211 class EncapsulatedContentInfo(Sequence):
212     schema = (
213         ("eContentType", ContentType()),
214         ("eContent", OctetString(expl=tag_ctxc(0), optional=True)),
215     )
216
217
218 class SignerIdentifier(Choice):
219     schema = (
220         ("issuerAndSerialNumber", Any()),
221         # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
222     )
223
224
225 class DigestAlgorithmIdentifiers(SetOf):
226     schema = AlgorithmIdentifier()
227
228
229 class DigestAlgorithmIdentifier(AlgorithmIdentifier):
230     pass
231
232
233 class SignatureAlgorithmIdentifier(AlgorithmIdentifier):
234     pass
235
236
237 class SignatureValue(OctetString):
238     pass
239
240
241 class SignerInfo(Sequence):
242     schema = (
243         ("version", CMSVersion()),
244         ("sid", SignerIdentifier()),
245         ("digestAlgorithm", DigestAlgorithmIdentifier()),
246         # ("signedAttrs", SignedAttributes(impl=tag_ctxc(0), optional=True)),
247         ("signatureAlgorithm", SignatureAlgorithmIdentifier()),
248         ("signature", SignatureValue()),
249         # ("unsignedAttrs", UnsignedAttributes(impl=tag_ctxc(1), optional=True)),
250     )
251
252
253 class SignerInfos(SetOf):
254     schema = SignerInfo()
255
256
257 class SignedData(Sequence):
258     schema = (
259         ("version", CMSVersion()),
260         ("digestAlgorithms", DigestAlgorithmIdentifiers()),
261         ("encapContentInfo", EncapsulatedContentInfo()),
262         # ("certificates", CertificateSet(impl=tag_ctxc(0), optional=True)),
263         # ("crls", RevocationInfoChoices(impl=tag_ctxc(1), optional=True)),
264         ("signerInfos", SignerInfos()),
265     )
266
267
268 class Digest(OctetString):
269     pass
270
271
272 class DigestedData(Sequence):
273     schema = (
274         ("version", CMSVersion()),
275         ("digestAlgorithm", DigestAlgorithmIdentifier()),
276         ("encapContentInfo", EncapsulatedContentInfo()),
277         ("digest", Digest()),
278     )