]> Cypherpunks.ru repositories - pygost.git/blob - pygost/asn1schemas/pfx.py
Simplify OctetString wrapping
[pygost.git] / pygost / asn1schemas / pfx.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 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.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
40
41
42 class PBKDF2Salt(Choice):
43     schema = (
44         ("specified", OctetString()),
45         # ("otherSource", PBKDF2SaltSources()),
46     )
47
48
49 id_hmacWithSHA1 = ObjectIdentifier("1.2.840.113549.2.7")
50
51
52 class PBKDF2PRFs(AlgorithmIdentifier):
53     schema = (
54         ("algorithm", ObjectIdentifier(default=id_hmacWithSHA1)),
55         ("parameters", Any(optional=True)),
56     )
57
58
59 class IterationCount(Integer):
60     bounds = (1, float("+inf"))
61
62
63 class KeyLength(Integer):
64     bounds = (1, float("+inf"))
65
66
67 class PBKDF2Params(Sequence):
68     schema = (
69         ("salt", PBKDF2Salt()),
70         ("iterationCount", IterationCount(optional=True)),
71         ("keyLength", KeyLength(optional=True)),
72         ("prf", PBKDF2PRFs()),
73     )
74
75
76 class PBES2KDFs(AlgorithmIdentifier):
77     schema = (
78         ("algorithm", ObjectIdentifier(defines=(
79             (("parameters",), {id_pbkdf2: PBKDF2Params()}),
80         ))),
81         ("parameters", Any(optional=True)),
82     )
83
84
85 class PBES2Encs(AlgorithmIdentifier):
86     schema = (
87         ("algorithm", ObjectIdentifier(defines=(
88             (("parameters",), {id_Gost28147_89: Gost2814789Parameters()}),
89         ))),
90         ("parameters", Any(optional=True)),
91     )
92
93
94 class PBES2Params(Sequence):
95     schema = (
96         ("keyDerivationFunc", PBES2KDFs()),
97         ("encryptionScheme", PBES2Encs()),
98     )
99
100
101 class EncryptionAlgorithmIdentifier(AlgorithmIdentifier):
102     schema = (
103         ("algorithm", ObjectIdentifier(defines=(
104             (("parameters",), {id_pbes2: PBES2Params()}),
105         ))),
106         ("parameters", Any(optional=True)),
107     )
108
109
110 class ContentEncryptionAlgorithmIdentifier(EncryptionAlgorithmIdentifier):
111     schema = (
112         ("algorithm", ObjectIdentifier(defines=(
113             (("parameters",), {id_pbes2: PBES2Params()}),
114         ))),
115         ("parameters", Any(optional=True)),
116     )
117
118
119 class EncryptedContent(OctetString):
120     pass
121
122
123 class EncryptedContentInfo(Sequence):
124     schema = (
125         ("contentType", ContentType()),
126         ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
127         ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
128     )
129
130
131 class EncryptedData(Sequence):
132     schema = (
133         ("version", CMSVersion()),
134         ("encryptedContentInfo", EncryptedContentInfo()),
135         # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
136     )
137
138
139 class PKCS12BagSet(Any):
140     pass
141
142
143 class AttrValue(SetOf):
144     schema = Any()
145
146
147 class PKCS12Attribute(Sequence):
148     schema = (
149         ("attrId", ObjectIdentifier()),
150         ("attrValue", AttrValue()),
151     )
152
153
154 class PKCS12Attributes(SetOf):
155     schema = PKCS12Attribute()
156
157
158 class SafeBag(Sequence):
159     schema = (
160         ("bagId", ObjectIdentifier(defines=(
161             (("bagValue",), {id_encryptedData: EncryptedData()}),
162         ))),
163         ("bagValue", PKCS12BagSet(expl=tag_ctxc(0))),
164         ("bagAttributes", PKCS12Attributes(optional=True)),
165     )
166
167
168 class SafeContents(SequenceOf):
169     schema = SafeBag()
170
171
172 OctetStringSafeContents = SafeContents(expl=OctetString.tag_default)
173
174
175 class AuthSafe(Sequence):
176     schema = (
177         ("contentType", ContentType(defines=(
178             (("content",), {id_data: OctetStringSafeContents()}),
179         ))),
180         ("content", Any(expl=tag_ctxc(0))),
181     )
182
183
184 class DigestInfo(Sequence):
185     schema = (
186         ("digestAlgorithm", AlgorithmIdentifier()),
187         ("digest", OctetString()),
188     )
189
190
191 class MacData(Sequence):
192     schema = (
193         ("mac", DigestInfo()),
194         ("macSalt", OctetString()),
195         ("iterations", Integer(default=1)),
196     )
197
198
199 class PFX(Sequence):
200     schema = (
201         ("version", Integer(default=1)),
202         ("authSafe", AuthSafe()),
203         ("macData", MacData(optional=True)),
204     )
205
206
207 class EncryptedPrivateKeyInfo(Sequence):
208     schema = (
209         ("encryptionAlgorithm", EncryptionAlgorithmIdentifier()),
210         ("encryptedData", OctetString()),
211     )
212
213
214 class PKCS8ShroudedKeyBag(EncryptedPrivateKeyInfo):
215     pass