]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/pfx.py
More TC26 ASN.1 test vectors
[pygost.git] / pygost / asn1schemas / pfx.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 """PKCS #12 related structures (**NOT COMPLETE**)
18 """
19
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
30
31 from pygost.asn1schemas.cms import CMSVersion
32 from pygost.asn1schemas.cms import ContentType
33 from pygost.asn1schemas.x509 import AlgorithmIdentifier
34
35
36 class EncryptionAlgorithmIdentifier(AlgorithmIdentifier):
37     schema = (
38         ("algorithm", ObjectIdentifier()),
39         ("parameters", Any(optional=True)),
40     )
41
42
43 class ContentEncryptionAlgorithmIdentifier(EncryptionAlgorithmIdentifier):
44     pass
45
46
47 class PBES2KDFs(AlgorithmIdentifier):
48     schema = (
49         ("algorithm", ObjectIdentifier()),
50         ("parameters", Any(optional=True)),
51     )
52
53
54 class PBES2Encs(AlgorithmIdentifier):
55     schema = (
56         ("algorithm", ObjectIdentifier()),
57         ("parameters", Any(optional=True)),
58     )
59
60
61 class PBES2Params(Sequence):
62     schema = (
63         ("keyDerivationFunc", PBES2KDFs()),
64         ("encryptionScheme", PBES2Encs()),
65     )
66
67
68 class EncryptedContent(OctetString):
69     pass
70
71
72 class EncryptedContentInfo(Sequence):
73     schema = (
74         ("contentType", ContentType()),
75         ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
76         ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
77     )
78
79
80 class EncryptedData(Sequence):
81     schema = (
82         ("version", CMSVersion()),
83         ("encryptedContentInfo", EncryptedContentInfo()),
84         # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
85     )
86
87
88 class PKCS12BagSet(Any):
89     pass
90
91
92 class AttrValue(SetOf):
93     schema = Any()
94
95
96 class PKCS12Attribute(Sequence):
97     schema = (
98         ("attrId", ObjectIdentifier()),
99         ("attrValue", AttrValue()),
100     )
101
102
103 class PKCS12Attributes(SetOf):
104     schema = PKCS12Attribute()
105
106
107 class SafeBag(Sequence):
108     schema = (
109         ("bagId", ObjectIdentifier()),
110         ("bagValue", PKCS12BagSet(expl=tag_ctxc(0))),
111         ("bagAttributes", PKCS12Attributes(optional=True)),
112     )
113
114
115 class SafeContents(SequenceOf):
116     schema = SafeBag()
117
118
119 class OctetStringSafeContents(Sequence):
120     tag_default = OctetString.tag_default
121     schema = (("safeContents", SafeContents()),)
122
123
124 class AuthSafe(Sequence):
125     schema = (
126         ("contentType", ContentType()),
127         ("content", Any(expl=tag_ctxc(0))),
128     )
129
130
131 class DigestInfo(Sequence):
132     schema = (
133         ("digestAlgorithm", AlgorithmIdentifier()),
134         ("digest", OctetString()),
135     )
136
137
138 class MacData(Sequence):
139     schema = (
140         ("mac", DigestInfo()),
141         ("macSalt", OctetString()),
142         ("iterations", Integer(default=1)),
143     )
144
145
146 class PFX(Sequence):
147     schema = (
148         ("version", Integer(default=1)),
149         ("authSafe", AuthSafe()),
150         ("macData", MacData(optional=True)),
151     )
152
153
154 class EncryptedPrivateKeyInfo(Sequence):
155     schema = (
156         ("encryptionAlgorithm", EncryptionAlgorithmIdentifier()),
157         ("encryptedData", OctetString()),
158     )
159
160
161 class PKCS8ShroudedKeyBag(EncryptedPrivateKeyInfo):
162     pass
163
164
165 class PBKDF2Salt(Choice):
166     schema = (
167         ("specified", OctetString()),
168         # ("otherSource", PBKDF2SaltSources()),
169     )
170
171
172 id_hmacWithSHA1 = ObjectIdentifier("1.2.840.113549.2.7")
173
174
175 class PBKDF2PRFs(AlgorithmIdentifier):
176     schema = (
177         ("algorithm", ObjectIdentifier(default=id_hmacWithSHA1)),
178         ("parameters", Any(optional=True)),
179     )
180
181
182 class IterationCount(Integer):
183     bounds = (1, float("+inf"))
184
185
186 class KeyLength(Integer):
187     bounds = (1, float("+inf"))
188
189
190 class PBKDF2Params(Sequence):
191     schema = (
192         ("salt", PBKDF2Salt()),
193         ("iterationCount", IterationCount(optional=True)),
194         ("keyLength", KeyLength(optional=True)),
195         ("prf", PBKDF2PRFs()),
196     )