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