2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2020 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, version 3 of the License.
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.
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 """PKCS #12 related structures (**NOT COMPLETE**)
19 from pyderasn import Any
20 from pyderasn import Choice
21 from pyderasn import Integer
22 from pyderasn import ObjectIdentifier
23 from pyderasn import OctetString
24 from pyderasn import Sequence
25 from pyderasn import SequenceOf
26 from pyderasn import SetOf
27 from pyderasn import tag_ctxc
28 from pyderasn import tag_ctxp
30 from pygost.asn1schemas.cms import CMSVersion
31 from pygost.asn1schemas.cms import ContentType
32 from pygost.asn1schemas.cms import Gost2814789Parameters
33 from pygost.asn1schemas.oids import id_data
34 from pygost.asn1schemas.oids import id_encryptedData
35 from pygost.asn1schemas.oids import id_Gost28147_89
36 from pygost.asn1schemas.oids import id_pbes2
37 from pygost.asn1schemas.oids import id_pbkdf2
38 from pygost.asn1schemas.x509 import AlgorithmIdentifier
41 class PBKDF2Salt(Choice):
43 ("specified", OctetString()),
44 # ("otherSource", PBKDF2SaltSources()),
48 id_hmacWithSHA1 = ObjectIdentifier("1.2.840.113549.2.7")
51 class PBKDF2PRFs(AlgorithmIdentifier):
53 ("algorithm", ObjectIdentifier(default=id_hmacWithSHA1)),
54 ("parameters", Any(optional=True)),
58 class IterationCount(Integer):
59 bounds = (1, float("+inf"))
62 class KeyLength(Integer):
63 bounds = (1, float("+inf"))
66 class PBKDF2Params(Sequence):
68 ("salt", PBKDF2Salt()),
69 ("iterationCount", IterationCount(optional=True)),
70 ("keyLength", KeyLength(optional=True)),
71 ("prf", PBKDF2PRFs()),
75 class PBES2KDFs(AlgorithmIdentifier):
77 ("algorithm", ObjectIdentifier(defines=(
78 (("parameters",), {id_pbkdf2: PBKDF2Params()}),
80 ("parameters", Any(optional=True)),
84 class PBES2Encs(AlgorithmIdentifier):
86 ("algorithm", ObjectIdentifier(defines=(
87 (("parameters",), {id_Gost28147_89: Gost2814789Parameters()}),
89 ("parameters", Any(optional=True)),
93 class PBES2Params(Sequence):
95 ("keyDerivationFunc", PBES2KDFs()),
96 ("encryptionScheme", PBES2Encs()),
100 class EncryptionAlgorithmIdentifier(AlgorithmIdentifier):
102 ("algorithm", ObjectIdentifier(defines=(
103 (("parameters",), {id_pbes2: PBES2Params()}),
105 ("parameters", Any(optional=True)),
109 class ContentEncryptionAlgorithmIdentifier(EncryptionAlgorithmIdentifier):
111 ("algorithm", ObjectIdentifier(defines=(
112 (("parameters",), {id_pbes2: PBES2Params()}),
114 ("parameters", Any(optional=True)),
118 class EncryptedContent(OctetString):
122 class EncryptedContentInfo(Sequence):
124 ("contentType", ContentType()),
125 ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
126 ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
130 class EncryptedData(Sequence):
132 ("version", CMSVersion()),
133 ("encryptedContentInfo", EncryptedContentInfo()),
134 # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
138 class PKCS12BagSet(Any):
142 class AttrValue(SetOf):
146 class PKCS12Attribute(Sequence):
148 ("attrId", ObjectIdentifier()),
149 ("attrValue", AttrValue()),
153 class PKCS12Attributes(SetOf):
154 schema = PKCS12Attribute()
157 class SafeBag(Sequence):
159 ("bagId", ObjectIdentifier(defines=(
160 (("bagValue",), {id_encryptedData: EncryptedData()}),
162 ("bagValue", PKCS12BagSet(expl=tag_ctxc(0))),
163 ("bagAttributes", PKCS12Attributes(optional=True)),
167 class SafeContents(SequenceOf):
171 OctetStringSafeContents = SafeContents(expl=OctetString.tag_default)
174 class AuthSafe(Sequence):
176 ("contentType", ContentType(defines=(
177 (("content",), {id_data: OctetStringSafeContents()}),
179 ("content", Any(expl=tag_ctxc(0))),
183 class DigestInfo(Sequence):
185 ("digestAlgorithm", AlgorithmIdentifier()),
186 ("digest", OctetString()),
190 class MacData(Sequence):
192 ("mac", DigestInfo()),
193 ("macSalt", OctetString()),
194 ("iterations", Integer(default=1)),
200 ("version", Integer(default=1)),
201 ("authSafe", AuthSafe()),
202 ("macData", MacData(optional=True)),
206 class EncryptedPrivateKeyInfo(Sequence):
208 ("encryptionAlgorithm", EncryptionAlgorithmIdentifier()),
209 ("encryptedData", OctetString()),
213 class PKCS8ShroudedKeyBag(EncryptedPrivateKeyInfo):