2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 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 """PKCS #12 related structures (**NOT COMPLETE**)
20 from pyderasn import Any
21 from pyderasn import Choice
22 from pyderasn import Integer
23 from pyderasn import ObjectIdentifier
24 from pyderasn import OctetString
25 from pyderasn import Sequence
26 from pyderasn import SequenceOf
27 from pyderasn import SetOf
28 from pyderasn import tag_ctxc
29 from pyderasn import tag_ctxp
31 from pygost.asn1schemas.cms import CMSVersion
32 from pygost.asn1schemas.cms import ContentType
33 from pygost.asn1schemas.cms import Gost2814789Parameters
34 from pygost.asn1schemas.oids import id_data
35 from pygost.asn1schemas.oids import id_encryptedData
36 from pygost.asn1schemas.oids import id_Gost28147_89
37 from pygost.asn1schemas.oids import id_pbes2
38 from pygost.asn1schemas.oids import id_pbkdf2
39 from pygost.asn1schemas.x509 import AlgorithmIdentifier
42 class PBKDF2Salt(Choice):
44 ("specified", OctetString()),
45 # ("otherSource", PBKDF2SaltSources()),
49 id_hmacWithSHA1 = ObjectIdentifier("1.2.840.113549.2.7")
52 class PBKDF2PRFs(AlgorithmIdentifier):
54 ("algorithm", ObjectIdentifier(default=id_hmacWithSHA1)),
55 ("parameters", Any(optional=True)),
59 class IterationCount(Integer):
60 bounds = (1, float("+inf"))
63 class KeyLength(Integer):
64 bounds = (1, float("+inf"))
67 class PBKDF2Params(Sequence):
69 ("salt", PBKDF2Salt()),
70 ("iterationCount", IterationCount(optional=True)),
71 ("keyLength", KeyLength(optional=True)),
72 ("prf", PBKDF2PRFs()),
76 class PBES2KDFs(AlgorithmIdentifier):
78 ("algorithm", ObjectIdentifier(defines=(
79 (("parameters",), {id_pbkdf2: PBKDF2Params()}),
81 ("parameters", Any(optional=True)),
85 class PBES2Encs(AlgorithmIdentifier):
87 ("algorithm", ObjectIdentifier(defines=(
88 (("parameters",), {id_Gost28147_89: Gost2814789Parameters()}),
90 ("parameters", Any(optional=True)),
94 class PBES2Params(Sequence):
96 ("keyDerivationFunc", PBES2KDFs()),
97 ("encryptionScheme", PBES2Encs()),
101 class EncryptionAlgorithmIdentifier(AlgorithmIdentifier):
103 ("algorithm", ObjectIdentifier(defines=(
104 (("parameters",), {id_pbes2: PBES2Params()}),
106 ("parameters", Any(optional=True)),
110 class ContentEncryptionAlgorithmIdentifier(EncryptionAlgorithmIdentifier):
112 ("algorithm", ObjectIdentifier(defines=(
113 (("parameters",), {id_pbes2: PBES2Params()}),
115 ("parameters", Any(optional=True)),
119 class EncryptedContent(OctetString):
123 class EncryptedContentInfo(Sequence):
125 ("contentType", ContentType()),
126 ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
127 ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
131 class EncryptedData(Sequence):
133 ("version", CMSVersion()),
134 ("encryptedContentInfo", EncryptedContentInfo()),
135 # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
139 class PKCS12BagSet(Any):
143 class AttrValue(SetOf):
147 class PKCS12Attribute(Sequence):
149 ("attrId", ObjectIdentifier()),
150 ("attrValue", AttrValue()),
154 class PKCS12Attributes(SetOf):
155 schema = PKCS12Attribute()
158 class SafeBag(Sequence):
160 ("bagId", ObjectIdentifier(defines=(
161 (("bagValue",), {id_encryptedData: EncryptedData()}),
163 ("bagValue", PKCS12BagSet(expl=tag_ctxc(0))),
164 ("bagAttributes", PKCS12Attributes(optional=True)),
168 class SafeContents(SequenceOf):
172 OctetStringSafeContents = SafeContents(expl=OctetString.tag_default)
175 class AuthSafe(Sequence):
177 ("contentType", ContentType(defines=(
178 (("content",), {id_data: OctetStringSafeContents()}),
180 ("content", Any(expl=tag_ctxc(0))),
184 class DigestInfo(Sequence):
186 ("digestAlgorithm", AlgorithmIdentifier()),
187 ("digest", OctetString()),
191 class MacData(Sequence):
193 ("mac", DigestInfo()),
194 ("macSalt", OctetString()),
195 ("iterations", Integer(default=1)),
201 ("version", Integer(default=1)),
202 ("authSafe", AuthSafe()),
203 ("macData", MacData(optional=True)),
207 class EncryptedPrivateKeyInfo(Sequence):
209 ("encryptionAlgorithm", EncryptionAlgorithmIdentifier()),
210 ("encryptedData", OctetString()),
214 class PKCS8ShroudedKeyBag(EncryptedPrivateKeyInfo):