2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2018 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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**)
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
32 from pygost.asn1schemas.x509 import AlgorithmIdentifier
33 from pygost.asn1schemas.x509 import SubjectPublicKeyInfo
36 class CMSVersion(Integer):
40 class ContentType(ObjectIdentifier):
44 class RecipientIdentifier(Choice):
46 ("issuerAndSerialNumber", Any()),
47 # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
51 class KeyEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
55 class EncryptedKey(OctetString):
59 class KeyTransRecipientInfo(Sequence):
61 ("version", CMSVersion()),
62 ("rid", RecipientIdentifier()),
63 ("keyEncryptionAlgorithm", KeyEncryptionAlgorithmIdentifier()),
64 ("encryptedKey", EncryptedKey()),
68 class OriginatorPublicKey(Sequence):
70 ("algorithm", AlgorithmIdentifier()),
71 ("publicKey", BitString()),
75 class OriginatorIdentifierOrKey(Choice):
77 # ("issuerAndSerialNumber", IssuerAndSerialNumber()),
78 # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
79 ("originatorKey", OriginatorPublicKey(impl=tag_ctxc(1))),
83 class UserKeyingMaterial(OctetString):
87 class KeyAgreeRecipientIdentifier(Choice):
89 ("issuerAndSerialNumber", Any()),
90 # ("rKeyId", RecipientKeyIdentifier(impl=tag_ctxc(0))),
94 class RecipientEncryptedKey(Sequence):
96 ("rid", KeyAgreeRecipientIdentifier()),
97 ("encryptedKey", EncryptedKey()),
101 class RecipientEncryptedKeys(SequenceOf):
102 schema = RecipientEncryptedKey()
105 class KeyAgreeRecipientInfo(Sequence):
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()),
115 class RecipientInfo(Choice):
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))),
125 class RecipientInfos(SetOf):
126 schema = RecipientInfo()
127 bounds = (1, float("+inf"))
130 class ContentEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
134 class EncryptedContent(OctetString):
138 class EncryptedContentInfo(Sequence):
140 ("contentType", ContentType()),
141 ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
142 ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
146 class EnvelopedData(Sequence):
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)),
156 class ContentInfo(Sequence):
158 ("contentType", ContentType()),
159 ("content", Any(expl=tag_ctxc(0))),
163 class Gost2814789IV(OctetString):
167 class Gost2814789Parameters(Sequence):
169 ("iv", Gost2814789IV()),
170 ("encryptionParamSet", ObjectIdentifier()),
174 class Gost2814789Key(OctetString):
178 class Gost2814789MAC(OctetString):
182 class Gost2814789EncryptedKey(Sequence):
184 ("encryptedKey", Gost2814789Key()),
185 ("maskKey", Gost2814789Key(impl=tag_ctxp(0), optional=True)),
186 ("macKey", Gost2814789MAC()),
190 class GostR34102001TransportParameters(Sequence):
192 ("encryptionParamSet", ObjectIdentifier()),
193 ("ephemeralPublicKey", SubjectPublicKeyInfo(
197 ("ukm", OctetString()),
201 class GostR3410KeyTransport(Sequence):
203 ("sessionEncryptedKey", Gost2814789EncryptedKey()),
204 ("transportParameters", GostR34102001TransportParameters(
211 class EncapsulatedContentInfo(Sequence):
213 ("eContentType", ContentType()),
214 ("eContent", OctetString(expl=tag_ctxc(0), optional=True)),
218 class SignerIdentifier(Choice):
220 ("issuerAndSerialNumber", Any()),
221 # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
225 class DigestAlgorithmIdentifiers(SetOf):
226 schema = AlgorithmIdentifier()
229 class DigestAlgorithmIdentifier(AlgorithmIdentifier):
233 class SignatureAlgorithmIdentifier(AlgorithmIdentifier):
237 class SignatureValue(OctetString):
241 class SignerInfo(Sequence):
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)),
253 class SignerInfos(SetOf):
254 schema = SignerInfo()
257 class SignedData(Sequence):
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()),
268 class Digest(OctetString):
272 class DigestedData(Sequence):
274 ("version", CMSVersion()),
275 ("digestAlgorithm", DigestAlgorithmIdentifier()),
276 ("encapContentInfo", EncapsulatedContentInfo()),
277 ("digest", Digest()),