/src/gnutls/lib/x509/pkcs7-crypt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2003-2016 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014-2016 Red Hat |
4 | | * Copyright (C) 2014-2016 Nikos Mavrogiannopoulos |
5 | | * |
6 | | * Author: Nikos Mavrogiannopoulos |
7 | | * |
8 | | * This file is part of GnuTLS. |
9 | | * |
10 | | * The GnuTLS is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public License |
12 | | * as published by the Free Software Foundation; either version 2.1 of |
13 | | * the License, or (at your option) any later version. |
14 | | * |
15 | | * This library is distributed in the hope that it will be useful, but |
16 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public License |
21 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
22 | | * |
23 | | */ |
24 | | |
25 | | #include "gnutls_int.h" |
26 | | |
27 | | #include "datum.h" |
28 | | #include "global.h" |
29 | | #include "errors.h" |
30 | | #include "common.h" |
31 | | #include "x509.h" |
32 | | #include "x509_b64.h" |
33 | | #include "x509_int.h" |
34 | | #include "pkcs7_int.h" |
35 | | #include "algorithms.h" |
36 | | #include "num.h" |
37 | | #include "random.h" |
38 | | #include "pk.h" |
39 | | |
40 | | #define PBES1_DES_MD5_OID "1.2.840.113549.1.5.3" |
41 | | #define PBES1_DES_SHA1_OID "1.2.840.113549.1.5.10" |
42 | | |
43 | 0 | #define PBES2_OID "1.2.840.113549.1.5.13" |
44 | 0 | #define PBKDF2_OID "1.2.840.113549.1.5.12" |
45 | | #define DES_EDE3_CBC_OID "1.2.840.113549.3.7" |
46 | | #define AES_128_CBC_OID "2.16.840.1.101.3.4.1.2" |
47 | | #define AES_192_CBC_OID "2.16.840.1.101.3.4.1.22" |
48 | | #define AES_256_CBC_OID "2.16.840.1.101.3.4.1.42" |
49 | | #define DES_CBC_OID "1.3.14.3.2.7" |
50 | | |
51 | | /* oid_pbeWithSHAAnd3_KeyTripleDES_CBC */ |
52 | | #define PKCS12_PBE_3DES_SHA1_OID "1.2.840.113549.1.12.1.3" |
53 | | #define PKCS12_PBE_ARCFOUR_SHA1_OID "1.2.840.113549.1.12.1.1" |
54 | | #define PKCS12_PBE_RC2_40_SHA1_OID "1.2.840.113549.1.12.1.6" |
55 | | |
56 | | static const struct pkcs_cipher_schema_st avail_pkcs_cipher_schemas[] = { |
57 | | { .schema = PBES1_DES_MD5, |
58 | | .name = "PBES1-DES-CBC-MD5", |
59 | | .flag = GNUTLS_PKCS_PBES1_DES_MD5, |
60 | | .cipher = GNUTLS_CIPHER_DES_CBC, |
61 | | .pbes2 = 0, |
62 | | .cipher_oid = PBES1_DES_MD5_OID, |
63 | | .write_oid = PBES1_DES_MD5_OID, |
64 | | .desc = NULL, |
65 | | .iv_name = NULL, |
66 | | .decrypt_only = 1 }, |
67 | | { .schema = PBES1_DES_SHA1, |
68 | | .name = "PBES1-DES-CBC-SHA1", |
69 | | .flag = GNUTLS_PKCS_PBES1_DES_SHA1, |
70 | | .cipher = GNUTLS_CIPHER_DES_CBC, |
71 | | .pbes2 = 0, |
72 | | .cipher_oid = PBES1_DES_SHA1_OID, |
73 | | .write_oid = PBES1_DES_SHA1_OID, |
74 | | .desc = NULL, |
75 | | .iv_name = NULL, |
76 | | .decrypt_only = 1 }, |
77 | | { .schema = PBES2_3DES, |
78 | | .name = "PBES2-3DES-CBC", |
79 | | .flag = GNUTLS_PKCS_PBES2_3DES, |
80 | | .cipher = GNUTLS_CIPHER_3DES_CBC, |
81 | | .pbes2 = 1, |
82 | | .cipher_oid = DES_EDE3_CBC_OID, |
83 | | .write_oid = PBES2_OID, |
84 | | .desc = "PKIX1.pkcs-5-des-EDE3-CBC-params", |
85 | | .iv_name = "", |
86 | | .decrypt_only = 0 }, |
87 | | { .schema = PBES2_DES, |
88 | | .name = "PBES2-DES-CBC", |
89 | | .flag = GNUTLS_PKCS_PBES2_DES, |
90 | | .cipher = GNUTLS_CIPHER_DES_CBC, |
91 | | .pbes2 = 1, |
92 | | .cipher_oid = DES_CBC_OID, |
93 | | .write_oid = PBES2_OID, |
94 | | .desc = "PKIX1.pkcs-5-des-CBC-params", |
95 | | .iv_name = "", |
96 | | .decrypt_only = 0 }, |
97 | | { .schema = PBES2_AES_128, |
98 | | .name = "PBES2-AES128-CBC", |
99 | | .flag = GNUTLS_PKCS_PBES2_AES_128, |
100 | | .cipher = GNUTLS_CIPHER_AES_128_CBC, |
101 | | .pbes2 = 1, |
102 | | .cipher_oid = AES_128_CBC_OID, |
103 | | .write_oid = PBES2_OID, |
104 | | .desc = "PKIX1.pkcs-5-aes128-CBC-params", |
105 | | .iv_name = "", |
106 | | .decrypt_only = 0 }, |
107 | | { .schema = PBES2_AES_192, |
108 | | .name = "PBES2-AES192-CBC", |
109 | | .flag = GNUTLS_PKCS_PBES2_AES_192, |
110 | | .cipher = GNUTLS_CIPHER_AES_192_CBC, |
111 | | .pbes2 = 1, |
112 | | .cipher_oid = AES_192_CBC_OID, |
113 | | .write_oid = PBES2_OID, |
114 | | .desc = "PKIX1.pkcs-5-aes192-CBC-params", |
115 | | .iv_name = "", |
116 | | .decrypt_only = 0 }, |
117 | | { .schema = PBES2_AES_256, |
118 | | .name = "PBES2-AES256-CBC", |
119 | | .flag = GNUTLS_PKCS_PBES2_AES_256, |
120 | | .cipher = GNUTLS_CIPHER_AES_256_CBC, |
121 | | .pbes2 = 1, |
122 | | .cipher_oid = AES_256_CBC_OID, |
123 | | .write_oid = PBES2_OID, |
124 | | .desc = "PKIX1.pkcs-5-aes256-CBC-params", |
125 | | .iv_name = "", |
126 | | .decrypt_only = 0 }, |
127 | | { .schema = PBES2_GOST28147_89_TC26Z, |
128 | | .name = "PBES2-GOST28147-89-TC26Z", |
129 | | .flag = GNUTLS_PKCS_PBES2_GOST_TC26Z, |
130 | | .cipher = GNUTLS_CIPHER_GOST28147_TC26Z_CFB, |
131 | | .pbes2 = 1, |
132 | | .cipher_oid = GOST28147_89_TC26Z_OID, |
133 | | .write_oid = PBES2_OID, |
134 | | .desc = "PKIX1.Gost28147-89-Parameters", |
135 | | .iv_name = "iv", |
136 | | .decrypt_only = 0 }, |
137 | | { .schema = PBES2_GOST28147_89_CPA, |
138 | | .name = "PBES2-GOST28147-89-CPA", |
139 | | .flag = GNUTLS_PKCS_PBES2_GOST_CPA, |
140 | | .cipher = GNUTLS_CIPHER_GOST28147_CPA_CFB, |
141 | | .pbes2 = 1, |
142 | | .cipher_oid = GOST28147_89_CPA_OID, |
143 | | .write_oid = PBES2_OID, |
144 | | .desc = "PKIX1.Gost28147-89-Parameters", |
145 | | .iv_name = "iv", |
146 | | .decrypt_only = 0 }, |
147 | | { .schema = PBES2_GOST28147_89_CPB, |
148 | | .name = "PBES2-GOST28147-89-CPB", |
149 | | .flag = GNUTLS_PKCS_PBES2_GOST_CPB, |
150 | | .cipher = GNUTLS_CIPHER_GOST28147_CPB_CFB, |
151 | | .pbes2 = 1, |
152 | | .cipher_oid = GOST28147_89_CPB_OID, |
153 | | .write_oid = PBES2_OID, |
154 | | .desc = "PKIX1.Gost28147-89-Parameters", |
155 | | .iv_name = "iv", |
156 | | .decrypt_only = 0 }, |
157 | | { .schema = PBES2_GOST28147_89_CPC, |
158 | | .name = "PBES2-GOST28147-89-CPC", |
159 | | .flag = GNUTLS_PKCS_PBES2_GOST_CPC, |
160 | | .cipher = GNUTLS_CIPHER_GOST28147_CPC_CFB, |
161 | | .pbes2 = 1, |
162 | | .cipher_oid = GOST28147_89_CPC_OID, |
163 | | .write_oid = PBES2_OID, |
164 | | .desc = "PKIX1.Gost28147-89-Parameters", |
165 | | .iv_name = "iv", |
166 | | .decrypt_only = 0 }, |
167 | | { .schema = PBES2_GOST28147_89_CPD, |
168 | | .name = "PBES2-GOST28147-89-CPD", |
169 | | .flag = GNUTLS_PKCS_PBES2_GOST_CPD, |
170 | | .cipher = GNUTLS_CIPHER_GOST28147_CPD_CFB, |
171 | | .pbes2 = 1, |
172 | | .cipher_oid = GOST28147_89_CPD_OID, |
173 | | .write_oid = PBES2_OID, |
174 | | .desc = "PKIX1.Gost28147-89-Parameters", |
175 | | .iv_name = "iv", |
176 | | .decrypt_only = 0 }, |
177 | | { .schema = PKCS12_ARCFOUR_SHA1, |
178 | | .name = "PKCS12-ARCFOUR-SHA1", |
179 | | .flag = GNUTLS_PKCS_PKCS12_ARCFOUR, |
180 | | .cipher = GNUTLS_CIPHER_ARCFOUR, |
181 | | .pbes2 = 0, |
182 | | .cipher_oid = PKCS12_PBE_ARCFOUR_SHA1_OID, |
183 | | .write_oid = PKCS12_PBE_ARCFOUR_SHA1_OID, |
184 | | .desc = NULL, |
185 | | .iv_name = NULL, |
186 | | .decrypt_only = 0 }, |
187 | | { .schema = PKCS12_RC2_40_SHA1, |
188 | | .name = "PKCS12-RC2-40-SHA1", |
189 | | .flag = GNUTLS_PKCS_PKCS12_RC2_40, |
190 | | .cipher = GNUTLS_CIPHER_RC2_40_CBC, |
191 | | .pbes2 = 0, |
192 | | .cipher_oid = PKCS12_PBE_RC2_40_SHA1_OID, |
193 | | .write_oid = PKCS12_PBE_RC2_40_SHA1_OID, |
194 | | .desc = NULL, |
195 | | .iv_name = NULL, |
196 | | .decrypt_only = 0 }, |
197 | | { .schema = PKCS12_3DES_SHA1, |
198 | | .name = "PKCS12-3DES-SHA1", |
199 | | .flag = GNUTLS_PKCS_PKCS12_3DES, |
200 | | .cipher = GNUTLS_CIPHER_3DES_CBC, |
201 | | .pbes2 = 0, |
202 | | .cipher_oid = PKCS12_PBE_3DES_SHA1_OID, |
203 | | .write_oid = PKCS12_PBE_3DES_SHA1_OID, |
204 | | .desc = NULL, |
205 | | .iv_name = NULL, |
206 | | .decrypt_only = 0 }, |
207 | | { 0, NULL, 0, 0, 0 } |
208 | | }; |
209 | | |
210 | | #define PBES2_SCHEMA_LOOP(b) \ |
211 | 0 | { \ |
212 | 0 | const struct pkcs_cipher_schema_st *_p; \ |
213 | 0 | for (_p = avail_pkcs_cipher_schemas; _p->schema != 0; _p++) { \ |
214 | 0 | b; \ |
215 | 0 | } \ |
216 | 0 | } |
217 | | |
218 | | #define PBES2_SCHEMA_FIND_FROM_FLAGS(fl, what) \ |
219 | 0 | PBES2_SCHEMA_LOOP( \ |
220 | 0 | if (_p->flag == GNUTLS_PKCS_CIPHER_MASK(fl)) { what; }) |
221 | | |
222 | | int _gnutls_pkcs_flags_to_schema(unsigned int flags) |
223 | 0 | { |
224 | 0 | PBES2_SCHEMA_FIND_FROM_FLAGS(flags, return _p->schema;); |
225 | |
|
226 | 0 | gnutls_assert(); |
227 | 0 | _gnutls_debug_log( |
228 | 0 | "Selecting default encryption PBES2_AES_256 (flags: %u).\n", |
229 | 0 | flags); |
230 | 0 | return PBES2_AES_256; |
231 | 0 | } |
232 | | |
233 | | /** |
234 | | * gnutls_pkcs_schema_get_name: |
235 | | * @schema: Holds the PKCS #12 or PBES2 schema (%gnutls_pkcs_encrypt_flags_t) |
236 | | * |
237 | | * This function will return a human readable description of the |
238 | | * PKCS12 or PBES2 schema. |
239 | | * |
240 | | * Returns: a constraint string or %NULL on error. |
241 | | * |
242 | | * Since: 3.4.0 |
243 | | */ |
244 | | const char *gnutls_pkcs_schema_get_name(unsigned int schema) |
245 | 0 | { |
246 | 0 | PBES2_SCHEMA_FIND_FROM_FLAGS(schema, return _p->name;); |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | | /** |
251 | | * gnutls_pkcs_schema_get_oid: |
252 | | * @schema: Holds the PKCS #12 or PBES2 schema (%gnutls_pkcs_encrypt_flags_t) |
253 | | * |
254 | | * This function will return the object identifier of the |
255 | | * PKCS12 or PBES2 schema. |
256 | | * |
257 | | * Returns: a constraint string or %NULL on error. |
258 | | * |
259 | | * Since: 3.4.0 |
260 | | */ |
261 | | const char *gnutls_pkcs_schema_get_oid(unsigned int schema) |
262 | 0 | { |
263 | 0 | PBES2_SCHEMA_FIND_FROM_FLAGS(schema, return _p->cipher_oid;); |
264 | 0 | return NULL; |
265 | 0 | } |
266 | | |
267 | | static const struct pkcs_cipher_schema_st * |
268 | | algo_to_pbes2_cipher_schema(unsigned cipher) |
269 | 0 | { |
270 | 0 | PBES2_SCHEMA_LOOP( |
271 | 0 | if (_p->cipher == cipher && _p->pbes2 != 0) { return _p; }); |
272 | |
|
273 | 0 | gnutls_assert(); |
274 | 0 | return NULL; |
275 | 0 | } |
276 | | |
277 | | /* Converts a PKCS#7 encryption schema OID to an internal |
278 | | * schema_id or returns a negative value */ |
279 | | int _gnutls_check_pkcs_cipher_schema(const char *oid) |
280 | 0 | { |
281 | 0 | if (streq(oid, PBES2_OID)) |
282 | 0 | return PBES2_GENERIC; /* PBES2 ciphers are under an umbrella OID */ |
283 | | |
284 | 0 | PBES2_SCHEMA_LOOP(if (_p->pbes2 == 0 && streq(oid, _p->write_oid)) { |
285 | 0 | return _p->schema; |
286 | 0 | }); |
287 | 0 | _gnutls_debug_log( |
288 | 0 | "PKCS #12 encryption schema OID '%s' is unsupported.\n", oid); |
289 | |
|
290 | 0 | return GNUTLS_E_UNKNOWN_CIPHER_TYPE; |
291 | 0 | } |
292 | | |
293 | | const struct pkcs_cipher_schema_st *_gnutls_pkcs_schema_get(schema_id schema) |
294 | 0 | { |
295 | 0 | PBES2_SCHEMA_LOOP(if (schema == _p->schema) return _p;); |
296 | |
|
297 | 0 | gnutls_assert(); |
298 | 0 | return NULL; |
299 | 0 | } |
300 | | |
301 | | /* Converts an OID to a gnutls cipher type. |
302 | | */ |
303 | | static int pbes2_cipher_oid_to_algo(const char *oid, |
304 | | gnutls_cipher_algorithm_t *algo) |
305 | 0 | { |
306 | 0 | *algo = 0; |
307 | 0 | PBES2_SCHEMA_LOOP(if (_p->pbes2 != 0 && streq(_p->cipher_oid, oid)) { |
308 | 0 | *algo = _p->cipher; |
309 | 0 | return 0; |
310 | 0 | }); |
311 | |
|
312 | 0 | _gnutls_debug_log("PKCS #8 encryption OID '%s' is unsupported.\n", oid); |
313 | 0 | return GNUTLS_E_UNKNOWN_CIPHER_TYPE; |
314 | 0 | } |
315 | | |
316 | | /* Decrypts a PKCS #7 encryptedData. The output is allocated |
317 | | * and stored in dec. |
318 | | */ |
319 | | int _gnutls_pkcs7_decrypt_data(const gnutls_datum_t *data, const char *password, |
320 | | gnutls_datum_t *dec) |
321 | 0 | { |
322 | 0 | int result, len; |
323 | 0 | char enc_oid[MAX_OID_SIZE]; |
324 | 0 | gnutls_datum_t tmp; |
325 | 0 | asn1_node pasn = NULL, pkcs7_asn = NULL; |
326 | 0 | int params_start, params_end, params_len; |
327 | 0 | struct pbkdf2_params kdf_params; |
328 | 0 | struct pbe_enc_params enc_params; |
329 | 0 | schema_id schema; |
330 | |
|
331 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
332 | 0 | "PKIX1.pkcs-7-EncryptedData", |
333 | 0 | &pkcs7_asn)) != ASN1_SUCCESS) { |
334 | 0 | gnutls_assert(); |
335 | 0 | result = _gnutls_asn2err(result); |
336 | 0 | goto error; |
337 | 0 | } |
338 | | |
339 | 0 | result = asn1_der_decoding(&pkcs7_asn, data->data, data->size, NULL); |
340 | 0 | if (result != ASN1_SUCCESS) { |
341 | 0 | gnutls_assert(); |
342 | 0 | result = _gnutls_asn2err(result); |
343 | 0 | goto error; |
344 | 0 | } |
345 | | |
346 | | /* Check the encryption schema OID |
347 | | */ |
348 | 0 | len = sizeof(enc_oid); |
349 | 0 | result = asn1_read_value( |
350 | 0 | pkcs7_asn, |
351 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.algorithm", |
352 | 0 | enc_oid, &len); |
353 | 0 | if (result != ASN1_SUCCESS) { |
354 | 0 | gnutls_assert(); |
355 | 0 | result = _gnutls_asn2err(result); |
356 | 0 | goto error; |
357 | 0 | } |
358 | | |
359 | 0 | if ((result = _gnutls_check_pkcs_cipher_schema(enc_oid)) < 0) { |
360 | 0 | gnutls_assert(); |
361 | 0 | goto error; |
362 | 0 | } |
363 | 0 | schema = result; |
364 | | |
365 | | /* Get the DER encoding of the parameters. |
366 | | */ |
367 | 0 | result = asn1_der_decoding_startEnd( |
368 | 0 | pkcs7_asn, data->data, data->size, |
369 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.parameters", |
370 | 0 | ¶ms_start, ¶ms_end); |
371 | 0 | if (result != ASN1_SUCCESS) { |
372 | 0 | gnutls_assert(); |
373 | 0 | result = _gnutls_asn2err(result); |
374 | 0 | goto error; |
375 | 0 | } |
376 | 0 | params_len = params_end - params_start + 1; |
377 | |
|
378 | 0 | result = _gnutls_read_pkcs_schema_params(&schema, password, |
379 | 0 | &data->data[params_start], |
380 | 0 | params_len, &kdf_params, |
381 | 0 | &enc_params); |
382 | 0 | if (result < 0) { |
383 | 0 | gnutls_assert(); |
384 | 0 | goto error; |
385 | 0 | } |
386 | | |
387 | | /* Parameters have been decoded. Now |
388 | | * decrypt the EncryptedData. |
389 | | */ |
390 | | |
391 | 0 | result = _gnutls_pkcs_raw_decrypt_data( |
392 | 0 | schema, pkcs7_asn, "encryptedContentInfo.encryptedContent", |
393 | 0 | password, &kdf_params, &enc_params, &tmp); |
394 | 0 | if (result < 0) { |
395 | 0 | gnutls_assert(); |
396 | 0 | goto error; |
397 | 0 | } |
398 | | |
399 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
400 | |
|
401 | 0 | *dec = tmp; |
402 | |
|
403 | 0 | return 0; |
404 | | |
405 | 0 | error: |
406 | 0 | asn1_delete_structure(&pasn); |
407 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
408 | 0 | return result; |
409 | 0 | } |
410 | | |
411 | | int _gnutls_pkcs7_data_enc_info(const gnutls_datum_t *data, |
412 | | const struct pkcs_cipher_schema_st **p, |
413 | | struct pbkdf2_params *kdf_params, char **oid) |
414 | 0 | { |
415 | 0 | int result, len; |
416 | 0 | char enc_oid[MAX_OID_SIZE]; |
417 | 0 | asn1_node pasn = NULL, pkcs7_asn = NULL; |
418 | 0 | int params_start, params_end, params_len; |
419 | 0 | struct pbe_enc_params enc_params; |
420 | 0 | schema_id schema; |
421 | |
|
422 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
423 | 0 | "PKIX1.pkcs-7-EncryptedData", |
424 | 0 | &pkcs7_asn)) != ASN1_SUCCESS) { |
425 | 0 | gnutls_assert(); |
426 | 0 | result = _gnutls_asn2err(result); |
427 | 0 | goto error; |
428 | 0 | } |
429 | | |
430 | 0 | result = asn1_der_decoding(&pkcs7_asn, data->data, data->size, NULL); |
431 | 0 | if (result != ASN1_SUCCESS) { |
432 | 0 | gnutls_assert(); |
433 | 0 | result = _gnutls_asn2err(result); |
434 | 0 | goto error; |
435 | 0 | } |
436 | | |
437 | | /* Check the encryption schema OID |
438 | | */ |
439 | 0 | len = sizeof(enc_oid); |
440 | 0 | result = asn1_read_value( |
441 | 0 | pkcs7_asn, |
442 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.algorithm", |
443 | 0 | enc_oid, &len); |
444 | 0 | if (result != ASN1_SUCCESS) { |
445 | 0 | gnutls_assert(); |
446 | 0 | result = _gnutls_asn2err(result); |
447 | 0 | goto error; |
448 | 0 | } |
449 | | |
450 | 0 | if (oid) { |
451 | 0 | *oid = gnutls_strdup(enc_oid); |
452 | 0 | } |
453 | |
|
454 | 0 | if ((result = _gnutls_check_pkcs_cipher_schema(enc_oid)) < 0) { |
455 | 0 | gnutls_assert(); |
456 | 0 | goto error; |
457 | 0 | } |
458 | 0 | schema = result; |
459 | | |
460 | | /* Get the DER encoding of the parameters. |
461 | | */ |
462 | 0 | result = asn1_der_decoding_startEnd( |
463 | 0 | pkcs7_asn, data->data, data->size, |
464 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.parameters", |
465 | 0 | ¶ms_start, ¶ms_end); |
466 | 0 | if (result != ASN1_SUCCESS) { |
467 | 0 | gnutls_assert(); |
468 | 0 | result = _gnutls_asn2err(result); |
469 | 0 | goto error; |
470 | 0 | } |
471 | 0 | params_len = params_end - params_start + 1; |
472 | |
|
473 | 0 | result = _gnutls_read_pkcs_schema_params(&schema, NULL, |
474 | 0 | &data->data[params_start], |
475 | 0 | params_len, kdf_params, |
476 | 0 | &enc_params); |
477 | 0 | if (result < 0) { |
478 | 0 | gnutls_assert(); |
479 | 0 | goto error; |
480 | 0 | } |
481 | | |
482 | 0 | *p = _gnutls_pkcs_schema_get(schema); |
483 | 0 | if (*p == NULL) { |
484 | 0 | gnutls_assert(); |
485 | 0 | result = GNUTLS_E_UNKNOWN_CIPHER_TYPE; |
486 | 0 | goto error; |
487 | 0 | } |
488 | | |
489 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
490 | |
|
491 | 0 | return 0; |
492 | | |
493 | 0 | error: |
494 | 0 | asn1_delete_structure(&pasn); |
495 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
496 | 0 | return result; |
497 | 0 | } |
498 | | |
499 | | /* Encrypts to a PKCS #7 encryptedData. The output is allocated |
500 | | * and stored in enc. |
501 | | */ |
502 | | int _gnutls_pkcs7_encrypt_data(schema_id schema, const gnutls_datum_t *data, |
503 | | const char *password, gnutls_datum_t *enc) |
504 | 0 | { |
505 | 0 | int result; |
506 | 0 | gnutls_datum_t key = { NULL, 0 }; |
507 | 0 | gnutls_datum_t tmp = { NULL, 0 }; |
508 | 0 | asn1_node pkcs7_asn = NULL; |
509 | 0 | struct pbkdf2_params kdf_params; |
510 | 0 | struct pbe_enc_params enc_params; |
511 | 0 | const struct pkcs_cipher_schema_st *s; |
512 | |
|
513 | 0 | s = _gnutls_pkcs_schema_get(schema); |
514 | 0 | if (s == NULL || s->decrypt_only) { |
515 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
516 | 0 | } |
517 | | |
518 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
519 | 0 | "PKIX1.pkcs-7-EncryptedData", |
520 | 0 | &pkcs7_asn)) != ASN1_SUCCESS) { |
521 | 0 | gnutls_assert(); |
522 | 0 | result = _gnutls_asn2err(result); |
523 | 0 | goto error; |
524 | 0 | } |
525 | | |
526 | 0 | result = asn1_write_value( |
527 | 0 | pkcs7_asn, |
528 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.algorithm", |
529 | 0 | s->write_oid, 1); |
530 | |
|
531 | 0 | if (result != ASN1_SUCCESS) { |
532 | 0 | gnutls_assert(); |
533 | 0 | result = _gnutls_asn2err(result); |
534 | 0 | goto error; |
535 | 0 | } |
536 | | |
537 | | /* Generate a symmetric key. |
538 | | */ |
539 | | |
540 | 0 | result = _gnutls_pkcs_generate_key(schema, password, &kdf_params, |
541 | 0 | &enc_params, &key); |
542 | 0 | if (result < 0) { |
543 | 0 | gnutls_assert(); |
544 | 0 | goto error; |
545 | 0 | } |
546 | | |
547 | 0 | result = _gnutls_pkcs_write_schema_params( |
548 | 0 | schema, pkcs7_asn, |
549 | 0 | "encryptedContentInfo.contentEncryptionAlgorithm.parameters", |
550 | 0 | &kdf_params, &enc_params); |
551 | 0 | if (result < 0) { |
552 | 0 | gnutls_assert(); |
553 | 0 | goto error; |
554 | 0 | } |
555 | | |
556 | | /* Parameters have been encoded. Now |
557 | | * encrypt the Data. |
558 | | */ |
559 | 0 | result = _gnutls_pkcs_raw_encrypt_data(data, &enc_params, &key, &tmp); |
560 | 0 | if (result < 0) { |
561 | 0 | gnutls_assert(); |
562 | 0 | goto error; |
563 | 0 | } |
564 | | |
565 | | /* write the encrypted data. |
566 | | */ |
567 | 0 | result = asn1_write_value(pkcs7_asn, |
568 | 0 | "encryptedContentInfo.encryptedContent", |
569 | 0 | tmp.data, tmp.size); |
570 | 0 | if (result != ASN1_SUCCESS) { |
571 | 0 | gnutls_assert(); |
572 | 0 | result = _gnutls_asn2err(result); |
573 | 0 | goto error; |
574 | 0 | } |
575 | | |
576 | 0 | _gnutls_free_datum(&tmp); |
577 | 0 | _gnutls_free_key_datum(&key); |
578 | | |
579 | | /* Now write the rest of the pkcs-7 stuff. |
580 | | */ |
581 | |
|
582 | 0 | result = _gnutls_x509_write_uint32(pkcs7_asn, "version", 0); |
583 | 0 | if (result < 0) { |
584 | 0 | gnutls_assert(); |
585 | 0 | goto error; |
586 | 0 | } |
587 | | |
588 | 0 | result = asn1_write_value(pkcs7_asn, "encryptedContentInfo.contentType", |
589 | 0 | DATA_OID, 1); |
590 | 0 | if (result != ASN1_SUCCESS) { |
591 | 0 | gnutls_assert(); |
592 | 0 | result = _gnutls_asn2err(result); |
593 | 0 | goto error; |
594 | 0 | } |
595 | | |
596 | 0 | result = asn1_write_value(pkcs7_asn, "unprotectedAttrs", NULL, 0); |
597 | 0 | if (result != ASN1_SUCCESS) { |
598 | 0 | gnutls_assert(); |
599 | 0 | result = _gnutls_asn2err(result); |
600 | 0 | goto error; |
601 | 0 | } |
602 | | |
603 | | /* Now encode and copy the DER stuff. |
604 | | */ |
605 | 0 | result = _gnutls_x509_der_encode(pkcs7_asn, "", enc, 0); |
606 | |
|
607 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
608 | |
|
609 | 0 | if (result < 0) { |
610 | 0 | gnutls_assert(); |
611 | 0 | goto error; |
612 | 0 | } |
613 | | |
614 | 0 | error: |
615 | 0 | _gnutls_free_key_datum(&key); |
616 | 0 | _gnutls_free_datum(&tmp); |
617 | 0 | asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE); |
618 | 0 | return result; |
619 | 0 | } |
620 | | |
621 | | /* Reads the PBKDF2 parameters. |
622 | | */ |
623 | | int _gnutls_read_pbkdf2_params(asn1_node pasn, const gnutls_datum_t *der, |
624 | | struct pbkdf2_params *params) |
625 | 0 | { |
626 | 0 | int params_start, params_end; |
627 | 0 | int params_len, len, result; |
628 | 0 | asn1_node pbkdf2_asn = NULL; |
629 | 0 | char oid[MAX_OID_SIZE]; |
630 | |
|
631 | 0 | memset(params, 0, sizeof(*params)); |
632 | |
|
633 | 0 | params->mac = GNUTLS_MAC_SHA1; |
634 | | |
635 | | /* Check the key derivation algorithm |
636 | | */ |
637 | 0 | len = sizeof(oid); |
638 | 0 | result = |
639 | 0 | asn1_read_value(pasn, "keyDerivationFunc.algorithm", oid, &len); |
640 | 0 | if (result != ASN1_SUCCESS) { |
641 | 0 | gnutls_assert(); |
642 | 0 | return _gnutls_asn2err(result); |
643 | 0 | } |
644 | 0 | _gnutls_hard_log("keyDerivationFunc.algorithm: %s\n", oid); |
645 | |
|
646 | 0 | if (!streq(oid, PBKDF2_OID)) { |
647 | 0 | gnutls_assert(); |
648 | 0 | _gnutls_debug_log( |
649 | 0 | "PKCS #8 key derivation OID '%s' is unsupported.\n", |
650 | 0 | oid); |
651 | 0 | return _gnutls_asn2err(result); |
652 | 0 | } |
653 | | |
654 | 0 | result = asn1_der_decoding_startEnd(pasn, der->data, der->size, |
655 | 0 | "keyDerivationFunc.parameters", |
656 | 0 | ¶ms_start, ¶ms_end); |
657 | 0 | if (result != ASN1_SUCCESS) { |
658 | 0 | gnutls_assert(); |
659 | 0 | return _gnutls_asn2err(result); |
660 | 0 | } |
661 | 0 | params_len = params_end - params_start + 1; |
662 | | |
663 | | /* Now check the key derivation and the encryption |
664 | | * functions. |
665 | | */ |
666 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
667 | 0 | "PKIX1.pkcs-5-PBKDF2-params", |
668 | 0 | &pbkdf2_asn)) != ASN1_SUCCESS) { |
669 | 0 | gnutls_assert(); |
670 | 0 | return _gnutls_asn2err(result); |
671 | 0 | } |
672 | | |
673 | 0 | result = _asn1_strict_der_decode(&pbkdf2_asn, &der->data[params_start], |
674 | 0 | params_len, NULL); |
675 | 0 | if (result != ASN1_SUCCESS) { |
676 | 0 | gnutls_assert(); |
677 | 0 | result = _gnutls_asn2err(result); |
678 | 0 | goto error; |
679 | 0 | } |
680 | | |
681 | | /* Read the salt. |
682 | | */ |
683 | 0 | params->salt_size = sizeof(params->salt); |
684 | 0 | result = asn1_read_value(pbkdf2_asn, "salt.specified", params->salt, |
685 | 0 | ¶ms->salt_size); |
686 | 0 | if (result != ASN1_SUCCESS) { |
687 | 0 | gnutls_assert(); |
688 | 0 | result = _gnutls_asn2err(result); |
689 | 0 | goto error; |
690 | 0 | } |
691 | 0 | _gnutls_hard_log("salt.specified.size: %d\n", params->salt_size); |
692 | |
|
693 | 0 | if (params->salt_size < 0) { |
694 | 0 | result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
695 | 0 | goto error; |
696 | 0 | } |
697 | | |
698 | | /* Read the iteration count. |
699 | | */ |
700 | 0 | result = _gnutls_x509_read_uint(pbkdf2_asn, "iterationCount", |
701 | 0 | ¶ms->iter_count); |
702 | 0 | if (result < 0) { |
703 | 0 | gnutls_assert(); |
704 | 0 | goto error; |
705 | 0 | } |
706 | | |
707 | 0 | if (params->iter_count >= MAX_ITER_COUNT || params->iter_count == 0) { |
708 | 0 | result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
709 | 0 | goto error; |
710 | 0 | } |
711 | | |
712 | 0 | _gnutls_hard_log("iterationCount: %d\n", params->iter_count); |
713 | | |
714 | | /* Read the keyLength, if it is present. |
715 | | */ |
716 | 0 | result = _gnutls_x509_read_uint(pbkdf2_asn, "keyLength", |
717 | 0 | ¶ms->key_size); |
718 | 0 | if (result < 0) { |
719 | 0 | params->key_size = 0; |
720 | 0 | } |
721 | |
|
722 | 0 | if (params->key_size > MAX_MAC_KEY_SIZE) { |
723 | 0 | result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
724 | 0 | goto error; |
725 | 0 | } |
726 | | |
727 | 0 | _gnutls_hard_log("keyLength: %d\n", params->key_size); |
728 | |
|
729 | 0 | len = sizeof(oid); |
730 | 0 | result = asn1_read_value(pbkdf2_asn, "prf.algorithm", oid, &len); |
731 | 0 | if (result != ASN1_SUCCESS) { |
732 | | /* use the default MAC */ |
733 | 0 | result = 0; |
734 | 0 | goto error; |
735 | 0 | } |
736 | | |
737 | 0 | params->mac = gnutls_oid_to_mac(oid); |
738 | 0 | if (params->mac == GNUTLS_MAC_UNKNOWN) { |
739 | 0 | gnutls_assert(); |
740 | 0 | _gnutls_debug_log("Unsupported hash algorithm: %s\n", oid); |
741 | 0 | result = GNUTLS_E_UNKNOWN_HASH_ALGORITHM; |
742 | 0 | goto error; |
743 | 0 | } |
744 | | |
745 | 0 | result = 0; |
746 | |
|
747 | 0 | error: |
748 | 0 | asn1_delete_structure(&pbkdf2_asn); |
749 | 0 | return result; |
750 | 0 | } |
751 | | |
752 | | /* Reads the PBE parameters from PKCS-12 schemas (*&#%*&#% RSA). |
753 | | */ |
754 | | static int read_pkcs12_kdf_params(asn1_node pasn, struct pbkdf2_params *params) |
755 | 0 | { |
756 | 0 | int result; |
757 | |
|
758 | 0 | memset(params, 0, sizeof(*params)); |
759 | | |
760 | | /* read the salt */ |
761 | 0 | params->salt_size = sizeof(params->salt); |
762 | 0 | result = |
763 | 0 | asn1_read_value(pasn, "salt", params->salt, ¶ms->salt_size); |
764 | 0 | if (result != ASN1_SUCCESS) { |
765 | 0 | gnutls_assert(); |
766 | 0 | return _gnutls_asn2err(result); |
767 | 0 | } |
768 | | |
769 | 0 | if (params->salt_size < 0) |
770 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
771 | | |
772 | 0 | _gnutls_hard_log("salt.size: %d\n", params->salt_size); |
773 | | |
774 | | /* read the iteration count |
775 | | */ |
776 | 0 | result = |
777 | 0 | _gnutls_x509_read_uint(pasn, "iterations", ¶ms->iter_count); |
778 | 0 | if (result < 0) |
779 | 0 | return gnutls_assert_val(result); |
780 | | |
781 | 0 | if (params->iter_count >= MAX_ITER_COUNT || params->iter_count == 0) |
782 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
783 | | |
784 | 0 | _gnutls_hard_log("iterationCount: %d\n", params->iter_count); |
785 | |
|
786 | 0 | params->key_size = 0; |
787 | |
|
788 | 0 | return 0; |
789 | 0 | } |
790 | | |
791 | | /* Writes the PBE parameters for PKCS-12 schemas. |
792 | | */ |
793 | | static int write_pkcs12_kdf_params(asn1_node pasn, |
794 | | const struct pbkdf2_params *kdf_params) |
795 | 0 | { |
796 | 0 | int result; |
797 | | |
798 | | /* write the salt |
799 | | */ |
800 | 0 | result = asn1_write_value(pasn, "salt", kdf_params->salt, |
801 | 0 | kdf_params->salt_size); |
802 | 0 | if (result != ASN1_SUCCESS) { |
803 | 0 | gnutls_assert(); |
804 | 0 | result = _gnutls_asn2err(result); |
805 | 0 | goto error; |
806 | 0 | } |
807 | 0 | _gnutls_hard_log("salt.size: %d\n", kdf_params->salt_size); |
808 | | |
809 | | /* write the iteration count |
810 | | */ |
811 | 0 | result = _gnutls_x509_write_uint32(pasn, "iterations", |
812 | 0 | kdf_params->iter_count); |
813 | 0 | if (result < 0) { |
814 | 0 | gnutls_assert(); |
815 | 0 | goto error; |
816 | 0 | } |
817 | 0 | _gnutls_hard_log("iterationCount: %d\n", kdf_params->iter_count); |
818 | |
|
819 | 0 | return 0; |
820 | | |
821 | 0 | error: |
822 | 0 | return result; |
823 | 0 | } |
824 | | |
825 | | static int read_pbes2_gost_oid(uint8_t *der, size_t len, char *oid, |
826 | | int oid_size) |
827 | 0 | { |
828 | 0 | int result; |
829 | 0 | asn1_node pbe_asn = NULL; |
830 | |
|
831 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
832 | 0 | "PKIX1.Gost28147-89-Parameters", |
833 | 0 | &pbe_asn)) != ASN1_SUCCESS) { |
834 | 0 | gnutls_assert(); |
835 | 0 | return _gnutls_asn2err(result); |
836 | 0 | } |
837 | | |
838 | 0 | result = _asn1_strict_der_decode(&pbe_asn, der, len, NULL); |
839 | 0 | if (result != ASN1_SUCCESS) { |
840 | 0 | gnutls_assert(); |
841 | 0 | result = _gnutls_asn2err(result); |
842 | 0 | goto error; |
843 | 0 | } |
844 | | |
845 | 0 | result = asn1_read_value(pbe_asn, "encryptionParamSet", oid, &oid_size); |
846 | 0 | if (result != ASN1_SUCCESS) { |
847 | 0 | gnutls_assert(); |
848 | 0 | result = _gnutls_asn2err(result); |
849 | 0 | goto error; |
850 | 0 | } |
851 | | |
852 | 0 | result = 0; |
853 | |
|
854 | 0 | error: |
855 | 0 | asn1_delete_structure(&pbe_asn); |
856 | 0 | return result; |
857 | 0 | } |
858 | | |
859 | | static int read_pbes2_enc_params(asn1_node pasn, const gnutls_datum_t *der, |
860 | | struct pbe_enc_params *params) |
861 | 0 | { |
862 | 0 | int params_start, params_end; |
863 | 0 | int params_len, len, result; |
864 | 0 | asn1_node pbe_asn = NULL; |
865 | 0 | const struct pkcs_cipher_schema_st *p; |
866 | |
|
867 | 0 | memset(params, 0, sizeof(*params)); |
868 | | |
869 | | /* Check the encryption algorithm |
870 | | */ |
871 | 0 | len = sizeof(params->pbes2_oid); |
872 | 0 | result = asn1_read_value(pasn, "encryptionScheme.algorithm", |
873 | 0 | params->pbes2_oid, &len); |
874 | 0 | if (result != ASN1_SUCCESS) { |
875 | 0 | gnutls_assert(); |
876 | 0 | return _gnutls_asn2err(result); |
877 | 0 | } |
878 | 0 | _gnutls_hard_log("encryptionScheme.algorithm: %s\n", params->pbes2_oid); |
879 | |
|
880 | 0 | result = asn1_der_decoding_startEnd(pasn, der->data, der->size, |
881 | 0 | "encryptionScheme.parameters", |
882 | 0 | ¶ms_start, ¶ms_end); |
883 | 0 | if (result != ASN1_SUCCESS) { |
884 | 0 | gnutls_assert(); |
885 | 0 | return _gnutls_asn2err(result); |
886 | 0 | } |
887 | 0 | params_len = params_end - params_start + 1; |
888 | | |
889 | | /* For GOST we have to read params to determine actual cipher */ |
890 | 0 | if (streq(params->pbes2_oid, GOST28147_89_OID)) { |
891 | 0 | len = sizeof(params->pbes2_oid); |
892 | 0 | result = read_pbes2_gost_oid(&der->data[params_start], |
893 | 0 | params_len, params->pbes2_oid, |
894 | 0 | len); |
895 | 0 | if (result < 0) { |
896 | 0 | gnutls_assert(); |
897 | 0 | return result; |
898 | 0 | } |
899 | 0 | } |
900 | | |
901 | 0 | if ((result = pbes2_cipher_oid_to_algo(params->pbes2_oid, |
902 | 0 | ¶ms->cipher)) < 0) { |
903 | 0 | gnutls_assert(); |
904 | 0 | return result; |
905 | 0 | } |
906 | | |
907 | | /* Now check the encryption parameters. |
908 | | */ |
909 | 0 | p = algo_to_pbes2_cipher_schema(params->cipher); |
910 | 0 | if (p == NULL) { |
911 | 0 | gnutls_assert(); |
912 | 0 | return GNUTLS_E_INVALID_REQUEST; |
913 | 0 | } |
914 | | |
915 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), p->desc, |
916 | 0 | &pbe_asn)) != ASN1_SUCCESS) { |
917 | 0 | gnutls_assert(); |
918 | 0 | return _gnutls_asn2err(result); |
919 | 0 | } |
920 | | |
921 | 0 | result = _asn1_strict_der_decode(&pbe_asn, &der->data[params_start], |
922 | 0 | params_len, NULL); |
923 | 0 | if (result != ASN1_SUCCESS) { |
924 | 0 | gnutls_assert(); |
925 | 0 | result = _gnutls_asn2err(result); |
926 | 0 | goto error; |
927 | 0 | } |
928 | | |
929 | | /* read the IV */ |
930 | 0 | params->iv_size = sizeof(params->iv); |
931 | 0 | result = asn1_read_value(pbe_asn, p->iv_name, params->iv, |
932 | 0 | ¶ms->iv_size); |
933 | 0 | if (result != ASN1_SUCCESS) { |
934 | 0 | gnutls_assert(); |
935 | 0 | result = _gnutls_asn2err(result); |
936 | 0 | goto error; |
937 | 0 | } |
938 | 0 | _gnutls_hard_log("IV.size: %d\n", params->iv_size); |
939 | |
|
940 | 0 | result = 0; |
941 | |
|
942 | 0 | error: |
943 | 0 | asn1_delete_structure(&pbe_asn); |
944 | 0 | return result; |
945 | 0 | } |
946 | | |
947 | | /* Read the parameters cipher, IV, salt etc using the given |
948 | | * schema ID. Initially the schema ID should have PBES2_GENERIC, for |
949 | | * PBES2 schemas, and will be updated by this function for details. |
950 | | */ |
951 | | int _gnutls_read_pkcs_schema_params(schema_id *schema, const char *password, |
952 | | const uint8_t *data, int data_size, |
953 | | struct pbkdf2_params *kdf_params, |
954 | | struct pbe_enc_params *enc_params) |
955 | 0 | { |
956 | 0 | asn1_node pasn = NULL; |
957 | 0 | int result; |
958 | 0 | gnutls_datum_t tmp; |
959 | 0 | const struct pkcs_cipher_schema_st *p; |
960 | |
|
961 | 0 | if (*schema == PBES2_GENERIC) { |
962 | | /* Now check the key derivation and the encryption |
963 | | * functions. |
964 | | */ |
965 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
966 | 0 | "PKIX1.pkcs-5-PBES2-params", |
967 | 0 | &pasn)) != ASN1_SUCCESS) { |
968 | 0 | gnutls_assert(); |
969 | 0 | result = _gnutls_asn2err(result); |
970 | 0 | goto error; |
971 | 0 | } |
972 | | |
973 | | /* Decode the parameters. |
974 | | */ |
975 | 0 | result = _asn1_strict_der_decode(&pasn, data, data_size, NULL); |
976 | 0 | if (result != ASN1_SUCCESS) { |
977 | 0 | gnutls_assert(); |
978 | 0 | result = _gnutls_asn2err(result); |
979 | 0 | goto error; |
980 | 0 | } |
981 | | |
982 | 0 | tmp.data = (uint8_t *)data; |
983 | 0 | tmp.size = data_size; |
984 | |
|
985 | 0 | result = _gnutls_read_pbkdf2_params(pasn, &tmp, kdf_params); |
986 | 0 | if (result < 0) { |
987 | 0 | gnutls_assert(); |
988 | 0 | goto error; |
989 | 0 | } |
990 | | |
991 | 0 | result = read_pbes2_enc_params(pasn, &tmp, enc_params); |
992 | 0 | if (result < 0) { |
993 | 0 | gnutls_assert(); |
994 | 0 | goto error; |
995 | 0 | } |
996 | | |
997 | 0 | asn1_delete_structure2(&pasn, ASN1_DELETE_FLAG_ZEROIZE); |
998 | |
|
999 | 0 | p = algo_to_pbes2_cipher_schema(enc_params->cipher); |
1000 | 0 | if (p == NULL) { |
1001 | 0 | result = GNUTLS_E_INVALID_REQUEST; |
1002 | 0 | gnutls_assert(); |
1003 | 0 | goto error; |
1004 | 0 | } |
1005 | | |
1006 | 0 | *schema = p->schema; |
1007 | 0 | return 0; |
1008 | 0 | } else if (*schema == PBES1_DES_MD5 || *schema == PBES1_DES_SHA1) { |
1009 | 0 | return _gnutls_read_pbkdf1_params(data, data_size, kdf_params, |
1010 | 0 | enc_params); |
1011 | 0 | } else { /* PKCS #12 schema */ |
1012 | 0 | memset(enc_params, 0, sizeof(*enc_params)); |
1013 | |
|
1014 | 0 | p = _gnutls_pkcs_schema_get(*schema); |
1015 | 0 | if (p == NULL) { |
1016 | 0 | gnutls_assert(); |
1017 | 0 | result = GNUTLS_E_UNKNOWN_CIPHER_TYPE; |
1018 | 0 | goto error; |
1019 | 0 | } |
1020 | 0 | enc_params->cipher = p->cipher; |
1021 | 0 | enc_params->iv_size = gnutls_cipher_get_iv_size(p->cipher); |
1022 | |
|
1023 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1024 | 0 | "PKIX1.pkcs-12-PbeParams", |
1025 | 0 | &pasn)) != ASN1_SUCCESS) { |
1026 | 0 | gnutls_assert(); |
1027 | 0 | result = _gnutls_asn2err(result); |
1028 | 0 | goto error; |
1029 | 0 | } |
1030 | | |
1031 | | /* Decode the parameters. |
1032 | | */ |
1033 | 0 | result = _asn1_strict_der_decode(&pasn, data, data_size, NULL); |
1034 | 0 | if (result != ASN1_SUCCESS) { |
1035 | 0 | gnutls_assert(); |
1036 | 0 | result = _gnutls_asn2err(result); |
1037 | 0 | goto error; |
1038 | 0 | } |
1039 | | |
1040 | 0 | result = read_pkcs12_kdf_params(pasn, kdf_params); |
1041 | 0 | if (result < 0) { |
1042 | 0 | gnutls_assert(); |
1043 | 0 | goto error; |
1044 | 0 | } |
1045 | | |
1046 | 0 | if (enc_params->iv_size) { |
1047 | 0 | result = _gnutls_pkcs12_string_to_key( |
1048 | 0 | mac_to_entry(GNUTLS_MAC_SHA1), 2 /*IV*/, |
1049 | 0 | kdf_params->salt, kdf_params->salt_size, |
1050 | 0 | kdf_params->iter_count, password, |
1051 | 0 | enc_params->iv_size, enc_params->iv); |
1052 | 0 | if (result < 0) { |
1053 | 0 | gnutls_assert(); |
1054 | 0 | goto error; |
1055 | 0 | } |
1056 | 0 | } |
1057 | | |
1058 | 0 | asn1_delete_structure(&pasn); |
1059 | |
|
1060 | 0 | return 0; |
1061 | 0 | } /* switch */ |
1062 | | |
1063 | 0 | error: |
1064 | 0 | asn1_delete_structure(&pasn); |
1065 | 0 | return result; |
1066 | 0 | } |
1067 | | |
1068 | | int _gnutls_pbes2_string_to_key(unsigned int pass_len, const char *password, |
1069 | | const struct pbkdf2_params *kdf_params, |
1070 | | int key_size, uint8_t *key) |
1071 | 0 | { |
1072 | 0 | gnutls_datum_t _key; |
1073 | 0 | gnutls_datum_t salt; |
1074 | |
|
1075 | 0 | _key.data = (void *)password; |
1076 | 0 | _key.size = pass_len; |
1077 | 0 | salt.data = (void *)kdf_params->salt; |
1078 | 0 | salt.size = kdf_params->salt_size; |
1079 | |
|
1080 | 0 | return gnutls_pbkdf2(kdf_params->mac, &_key, &salt, |
1081 | 0 | kdf_params->iter_count, key, key_size); |
1082 | 0 | } |
1083 | | |
1084 | | int _gnutls_pkcs_raw_decrypt_data(schema_id schema, asn1_node pkcs8_asn, |
1085 | | const char *root, const char *_password, |
1086 | | const struct pbkdf2_params *kdf_params, |
1087 | | const struct pbe_enc_params *enc_params, |
1088 | | gnutls_datum_t *decrypted_data) |
1089 | 0 | { |
1090 | 0 | gnutls_datum_t enc = { NULL, 0 }; |
1091 | 0 | uint8_t *key = NULL; |
1092 | 0 | gnutls_datum_t dkey, d_iv; |
1093 | 0 | gnutls_cipher_hd_t ch = NULL; |
1094 | 0 | int key_size, ret; |
1095 | 0 | unsigned int pass_len = 0; |
1096 | 0 | const struct pkcs_cipher_schema_st *p; |
1097 | 0 | unsigned block_size; |
1098 | 0 | const cipher_entry_st *ce; |
1099 | 0 | char *password; |
1100 | |
|
1101 | 0 | if (_password) { |
1102 | 0 | gnutls_datum_t pout; |
1103 | 0 | ret = _gnutls_utf8_password_normalize( |
1104 | 0 | _password, strlen(_password), &pout, 1); |
1105 | 0 | if (ret < 0) |
1106 | 0 | return gnutls_assert_val(ret); |
1107 | | |
1108 | 0 | password = (char *)pout.data; |
1109 | 0 | pass_len = pout.size; |
1110 | 0 | } else { |
1111 | 0 | password = NULL; |
1112 | 0 | pass_len = 0; |
1113 | 0 | } |
1114 | | |
1115 | 0 | ret = _gnutls_x509_read_value(pkcs8_asn, root, &enc); |
1116 | 0 | if (ret < 0) { |
1117 | 0 | gnutls_assert(); |
1118 | 0 | enc.data = NULL; |
1119 | 0 | goto cleanup; |
1120 | 0 | } |
1121 | | |
1122 | 0 | if (schema == PBES1_DES_MD5) { |
1123 | 0 | ret = _gnutls_decrypt_pbes1_des_md5_data(password, pass_len, |
1124 | 0 | kdf_params, enc_params, |
1125 | 0 | &enc, decrypted_data); |
1126 | 0 | if (ret < 0) |
1127 | 0 | goto error; |
1128 | 0 | goto cleanup; |
1129 | 0 | } else if (schema == PBES1_DES_SHA1) { |
1130 | 0 | ret = _gnutls_decrypt_pbes1_des_sha1_data(password, pass_len, |
1131 | 0 | kdf_params, |
1132 | 0 | enc_params, &enc, |
1133 | 0 | decrypted_data); |
1134 | 0 | if (ret < 0) |
1135 | 0 | goto error; |
1136 | 0 | goto cleanup; |
1137 | 0 | } |
1138 | | |
1139 | 0 | if (kdf_params->key_size == 0) { |
1140 | 0 | key_size = gnutls_cipher_get_key_size(enc_params->cipher); |
1141 | 0 | } else |
1142 | 0 | key_size = kdf_params->key_size; |
1143 | |
|
1144 | 0 | key = gnutls_malloc(key_size); |
1145 | 0 | if (key == NULL) { |
1146 | 0 | gnutls_assert(); |
1147 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
1148 | 0 | goto error; |
1149 | 0 | } |
1150 | | |
1151 | | /* generate the key |
1152 | | */ |
1153 | 0 | p = _gnutls_pkcs_schema_get(schema); |
1154 | 0 | if (p != NULL && p->pbes2 != 0) { /* PBES2 */ |
1155 | 0 | ret = _gnutls_pbes2_string_to_key(pass_len, password, |
1156 | 0 | kdf_params, key_size, key); |
1157 | 0 | if (ret < 0) { |
1158 | 0 | gnutls_assert(); |
1159 | 0 | goto error; |
1160 | 0 | } |
1161 | 0 | } else if (p != NULL) { /* PKCS 12 schema */ |
1162 | 0 | ret = _gnutls_pkcs12_string_to_key( |
1163 | 0 | mac_to_entry(GNUTLS_MAC_SHA1), 1 /*KEY*/, |
1164 | 0 | kdf_params->salt, kdf_params->salt_size, |
1165 | 0 | kdf_params->iter_count, password, key_size, key); |
1166 | |
|
1167 | 0 | if (ret < 0) { |
1168 | 0 | gnutls_assert(); |
1169 | 0 | goto error; |
1170 | 0 | } |
1171 | 0 | } else { |
1172 | 0 | gnutls_assert(); |
1173 | 0 | ret = GNUTLS_E_UNKNOWN_CIPHER_TYPE; |
1174 | 0 | goto error; |
1175 | 0 | } |
1176 | | |
1177 | 0 | ce = cipher_to_entry(enc_params->cipher); |
1178 | 0 | if (unlikely(ce == NULL)) { |
1179 | 0 | ret = gnutls_assert_val(GNUTLS_E_UNKNOWN_CIPHER_TYPE); |
1180 | 0 | goto error; |
1181 | 0 | } |
1182 | 0 | block_size = _gnutls_cipher_get_block_size(ce); |
1183 | |
|
1184 | 0 | if (ce->type == CIPHER_BLOCK) { |
1185 | 0 | if (enc.size % block_size != 0 || |
1186 | 0 | (unsigned)enc_params->iv_size != block_size) { |
1187 | 0 | gnutls_assert(); |
1188 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1189 | 0 | goto error; |
1190 | 0 | } |
1191 | 0 | } else { |
1192 | 0 | unsigned iv_size = _gnutls_cipher_get_iv_size(ce); |
1193 | 0 | if (iv_size > (unsigned)enc_params->iv_size) { |
1194 | 0 | gnutls_assert(); |
1195 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1196 | 0 | goto error; |
1197 | 0 | } |
1198 | 0 | } |
1199 | | |
1200 | | /* do the decryption. |
1201 | | */ |
1202 | 0 | dkey.data = key; |
1203 | 0 | dkey.size = key_size; |
1204 | |
|
1205 | 0 | d_iv.data = (uint8_t *)enc_params->iv; |
1206 | 0 | d_iv.size = enc_params->iv_size; |
1207 | |
|
1208 | 0 | ret = gnutls_cipher_init(&ch, ce->id, &dkey, &d_iv); |
1209 | |
|
1210 | 0 | zeroize_key(key, key_size); |
1211 | 0 | gnutls_free(key); |
1212 | |
|
1213 | 0 | if (ret < 0) { |
1214 | 0 | gnutls_assert(); |
1215 | 0 | goto error; |
1216 | 0 | } |
1217 | | |
1218 | 0 | ret = gnutls_cipher_decrypt(ch, enc.data, enc.size); |
1219 | 0 | if (ret < 0) { |
1220 | 0 | gnutls_assert(); |
1221 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1222 | 0 | goto error; |
1223 | 0 | } |
1224 | | |
1225 | 0 | decrypted_data->data = enc.data; |
1226 | |
|
1227 | 0 | if (ce->type == CIPHER_BLOCK && block_size != 1) { |
1228 | 0 | unsigned pslen = (uint8_t)enc.data[enc.size - 1]; |
1229 | 0 | unsigned i; |
1230 | |
|
1231 | 0 | if (pslen > block_size || pslen >= enc.size || pslen == 0) { |
1232 | 0 | gnutls_assert(); |
1233 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1234 | 0 | goto error; |
1235 | 0 | } |
1236 | | |
1237 | | /* verify padding according to rfc2898 */ |
1238 | 0 | decrypted_data->size = enc.size - pslen; |
1239 | 0 | for (i = 0; i < pslen; i++) { |
1240 | 0 | if (enc.data[enc.size - 1 - i] != pslen) { |
1241 | 0 | gnutls_assert(); |
1242 | 0 | ret = GNUTLS_E_DECRYPTION_FAILED; |
1243 | 0 | goto error; |
1244 | 0 | } |
1245 | 0 | } |
1246 | 0 | } else { |
1247 | 0 | decrypted_data->size = enc.size; |
1248 | 0 | } |
1249 | | |
1250 | 0 | gnutls_cipher_deinit(ch); |
1251 | |
|
1252 | 0 | ret = 0; |
1253 | |
|
1254 | 0 | cleanup: |
1255 | 0 | if (password) { |
1256 | 0 | zeroize_key(password, pass_len); |
1257 | 0 | gnutls_free(password); |
1258 | 0 | } |
1259 | |
|
1260 | 0 | return ret; |
1261 | | |
1262 | 0 | error: |
1263 | 0 | if (password) { |
1264 | 0 | zeroize_key(password, pass_len); |
1265 | 0 | gnutls_free(password); |
1266 | 0 | } |
1267 | 0 | if (enc.data) { |
1268 | 0 | zeroize_key(enc.data, enc.size); |
1269 | 0 | gnutls_free(enc.data); |
1270 | 0 | } |
1271 | 0 | if (key) { |
1272 | 0 | zeroize_key(key, key_size); |
1273 | 0 | gnutls_free(key); |
1274 | 0 | } |
1275 | 0 | if (ch) { |
1276 | 0 | gnutls_cipher_deinit(ch); |
1277 | 0 | } |
1278 | 0 | return ret; |
1279 | 0 | } |
1280 | | |
1281 | | /* Writes the PBKDF2 parameters. |
1282 | | */ |
1283 | | int _gnutls_write_pbkdf2_params(asn1_node pasn, |
1284 | | const struct pbkdf2_params *kdf_params) |
1285 | 0 | { |
1286 | 0 | int result; |
1287 | 0 | asn1_node pbkdf2_asn = NULL; |
1288 | 0 | const mac_entry_st *me; |
1289 | | |
1290 | | /* Write the key derivation algorithm. |
1291 | | */ |
1292 | 0 | result = asn1_write_value(pasn, "keyDerivationFunc.algorithm", |
1293 | 0 | PBKDF2_OID, 1); |
1294 | 0 | if (result != ASN1_SUCCESS) { |
1295 | 0 | gnutls_assert(); |
1296 | 0 | return _gnutls_asn2err(result); |
1297 | 0 | } |
1298 | | |
1299 | | /* Now write the key derivation and the encryption |
1300 | | * functions. |
1301 | | */ |
1302 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1303 | 0 | "PKIX1.pkcs-5-PBKDF2-params", |
1304 | 0 | &pbkdf2_asn)) != ASN1_SUCCESS) { |
1305 | 0 | gnutls_assert(); |
1306 | 0 | return _gnutls_asn2err(result); |
1307 | 0 | } |
1308 | | |
1309 | 0 | result = asn1_write_value(pbkdf2_asn, "salt", "specified", 1); |
1310 | 0 | if (result != ASN1_SUCCESS) { |
1311 | 0 | gnutls_assert(); |
1312 | 0 | result = _gnutls_asn2err(result); |
1313 | 0 | goto error; |
1314 | 0 | } |
1315 | | |
1316 | | /* Write the salt. |
1317 | | */ |
1318 | 0 | result = asn1_write_value(pbkdf2_asn, "salt.specified", |
1319 | 0 | kdf_params->salt, kdf_params->salt_size); |
1320 | 0 | if (result != ASN1_SUCCESS) { |
1321 | 0 | gnutls_assert(); |
1322 | 0 | result = _gnutls_asn2err(result); |
1323 | 0 | goto error; |
1324 | 0 | } |
1325 | 0 | _gnutls_hard_log("salt.specified.size: %d\n", kdf_params->salt_size); |
1326 | | |
1327 | | /* Write the iteration count. |
1328 | | */ |
1329 | 0 | result = _gnutls_x509_write_uint32(pbkdf2_asn, "iterationCount", |
1330 | 0 | kdf_params->iter_count); |
1331 | 0 | if (result < 0) { |
1332 | 0 | gnutls_assert(); |
1333 | 0 | goto error; |
1334 | 0 | } |
1335 | 0 | _gnutls_hard_log("iterationCount: %d\n", kdf_params->iter_count); |
1336 | | |
1337 | | /* Write the keyLength, if it is set. |
1338 | | */ |
1339 | 0 | if (kdf_params->key_size > 0) { |
1340 | 0 | result = _gnutls_x509_write_uint32(pbkdf2_asn, "keyLength", |
1341 | 0 | kdf_params->key_size); |
1342 | 0 | if (result < 0) { |
1343 | 0 | gnutls_assert(); |
1344 | 0 | goto error; |
1345 | 0 | } |
1346 | 0 | } |
1347 | | |
1348 | 0 | me = _gnutls_mac_to_entry(kdf_params->mac); |
1349 | 0 | if (!me || !me->mac_oid) { |
1350 | 0 | gnutls_assert(); |
1351 | 0 | result = GNUTLS_E_INTERNAL_ERROR; |
1352 | 0 | goto error; |
1353 | 0 | } |
1354 | | |
1355 | 0 | result = asn1_write_value(pbkdf2_asn, "prf.algorithm", me->mac_oid, |
1356 | 0 | strlen(me->mac_oid)); |
1357 | 0 | if (result != ASN1_SUCCESS) { |
1358 | 0 | gnutls_assert(); |
1359 | 0 | result = _gnutls_asn2err(result); |
1360 | 0 | goto error; |
1361 | 0 | } |
1362 | | |
1363 | 0 | result = asn1_write_value(pbkdf2_asn, "prf.parameters", NULL, 0); |
1364 | 0 | if (result != ASN1_SUCCESS) { |
1365 | 0 | gnutls_assert(); |
1366 | 0 | result = _gnutls_asn2err(result); |
1367 | 0 | goto error; |
1368 | 0 | } |
1369 | | |
1370 | | /* Now encode them an put the DER output in the |
1371 | | * keyDerivationFunc.parameters. |
1372 | | */ |
1373 | 0 | result = _gnutls_x509_der_encode_and_copy( |
1374 | 0 | pbkdf2_asn, "", pasn, "keyDerivationFunc.parameters", 0); |
1375 | 0 | if (result < 0) { |
1376 | 0 | gnutls_assert(); |
1377 | 0 | goto error; |
1378 | 0 | } |
1379 | | |
1380 | 0 | result = 0; |
1381 | |
|
1382 | 0 | error: |
1383 | 0 | asn1_delete_structure(&pbkdf2_asn); |
1384 | 0 | return result; |
1385 | 0 | } |
1386 | | |
1387 | | static int write_pbes2_enc_params(asn1_node pasn, |
1388 | | const struct pbe_enc_params *params) |
1389 | 0 | { |
1390 | 0 | int result; |
1391 | 0 | asn1_node pbe_asn = NULL; |
1392 | 0 | const struct pkcs_cipher_schema_st *p; |
1393 | 0 | const char *cipher_oid; |
1394 | | |
1395 | | /* Write the encryption algorithm |
1396 | | */ |
1397 | 0 | p = algo_to_pbes2_cipher_schema(params->cipher); |
1398 | 0 | if (p == NULL || p->pbes2 == 0) { |
1399 | 0 | gnutls_assert(); |
1400 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1401 | 0 | } |
1402 | | |
1403 | | /* Now check the encryption parameters. |
1404 | | */ |
1405 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), p->desc, |
1406 | 0 | &pbe_asn)) != ASN1_SUCCESS) { |
1407 | 0 | gnutls_assert(); |
1408 | 0 | return _gnutls_asn2err(result); |
1409 | 0 | } |
1410 | | |
1411 | 0 | if (p->schema == PBES2_GOST28147_89_TC26Z || |
1412 | 0 | p->schema == PBES2_GOST28147_89_CPA || |
1413 | 0 | p->schema == PBES2_GOST28147_89_CPB || |
1414 | 0 | p->schema == PBES2_GOST28147_89_CPC || |
1415 | 0 | p->schema == PBES2_GOST28147_89_CPD) { |
1416 | 0 | cipher_oid = GOST28147_89_OID; |
1417 | 0 | result = asn1_write_value(pbe_asn, "encryptionParamSet", |
1418 | 0 | p->cipher_oid, 1); |
1419 | 0 | if (result != ASN1_SUCCESS) { |
1420 | 0 | gnutls_assert(); |
1421 | 0 | result = _gnutls_asn2err(result); |
1422 | 0 | goto error; |
1423 | 0 | } |
1424 | 0 | } else { |
1425 | 0 | cipher_oid = p->cipher_oid; |
1426 | 0 | } |
1427 | | |
1428 | 0 | result = asn1_write_value(pasn, "encryptionScheme.algorithm", |
1429 | 0 | cipher_oid, 1); |
1430 | 0 | if (result != ASN1_SUCCESS) { |
1431 | 0 | gnutls_assert(); |
1432 | 0 | goto error; |
1433 | 0 | } |
1434 | 0 | _gnutls_hard_log("encryptionScheme.algorithm: %s\n", cipher_oid); |
1435 | | |
1436 | | /* read the salt */ |
1437 | 0 | result = asn1_write_value(pbe_asn, p->iv_name, params->iv, |
1438 | 0 | params->iv_size); |
1439 | 0 | if (result != ASN1_SUCCESS) { |
1440 | 0 | gnutls_assert(); |
1441 | 0 | result = _gnutls_asn2err(result); |
1442 | 0 | goto error; |
1443 | 0 | } |
1444 | 0 | _gnutls_hard_log("IV.size: %d\n", params->iv_size); |
1445 | | |
1446 | | /* now encode them an put the DER output |
1447 | | * in the encryptionScheme.parameters |
1448 | | */ |
1449 | 0 | result = _gnutls_x509_der_encode_and_copy( |
1450 | 0 | pbe_asn, "", pasn, "encryptionScheme.parameters", 0); |
1451 | 0 | if (result < 0) { |
1452 | 0 | gnutls_assert(); |
1453 | 0 | goto error; |
1454 | 0 | } |
1455 | | |
1456 | 0 | result = 0; |
1457 | |
|
1458 | 0 | error: |
1459 | 0 | asn1_delete_structure(&pbe_asn); |
1460 | 0 | return result; |
1461 | 0 | } |
1462 | | |
1463 | | /* Generates a key and also stores the key parameters. |
1464 | | */ |
1465 | | int _gnutls_pkcs_generate_key(schema_id schema, const char *_password, |
1466 | | struct pbkdf2_params *kdf_params, |
1467 | | struct pbe_enc_params *enc_params, |
1468 | | gnutls_datum_t *key) |
1469 | 0 | { |
1470 | 0 | unsigned char rnd[2]; |
1471 | 0 | unsigned int pass_len = 0; |
1472 | 0 | int ret; |
1473 | 0 | const struct pkcs_cipher_schema_st *p; |
1474 | 0 | char *password = NULL; |
1475 | |
|
1476 | 0 | if (_password) { |
1477 | 0 | gnutls_datum_t pout; |
1478 | 0 | ret = _gnutls_utf8_password_normalize( |
1479 | 0 | _password, strlen(_password), &pout, 0); |
1480 | 0 | if (ret < 0) |
1481 | 0 | return gnutls_assert_val(ret); |
1482 | | |
1483 | 0 | password = (char *)pout.data; |
1484 | 0 | pass_len = pout.size; |
1485 | 0 | } else { |
1486 | 0 | password = NULL; |
1487 | 0 | pass_len = 0; |
1488 | 0 | } |
1489 | | |
1490 | 0 | ret = gnutls_rnd(GNUTLS_RND_RANDOM, rnd, 2); |
1491 | 0 | if (ret < 0) { |
1492 | 0 | gnutls_assert(); |
1493 | 0 | goto cleanup; |
1494 | 0 | } |
1495 | | |
1496 | | /* generate salt */ |
1497 | 0 | kdf_params->salt_size = |
1498 | 0 | MIN(sizeof(kdf_params->salt), (unsigned)(12 + (rnd[1] % 10))); |
1499 | |
|
1500 | 0 | p = _gnutls_pkcs_schema_get(schema); |
1501 | 0 | if (p != NULL && p->pbes2 != 0) { /* PBES2 */ |
1502 | 0 | enc_params->cipher = p->cipher; |
1503 | 0 | } else if (p != NULL) { |
1504 | | /* non PBES2 algorithms */ |
1505 | 0 | enc_params->cipher = p->cipher; |
1506 | 0 | kdf_params->salt_size = 8; |
1507 | 0 | } else { |
1508 | 0 | gnutls_assert(); |
1509 | 0 | ret = GNUTLS_E_INVALID_REQUEST; |
1510 | 0 | goto cleanup; |
1511 | 0 | } |
1512 | | |
1513 | 0 | ret = gnutls_rnd(GNUTLS_RND_RANDOM, kdf_params->salt, |
1514 | 0 | kdf_params->salt_size); |
1515 | 0 | if (ret < 0) { |
1516 | 0 | gnutls_assert(); |
1517 | 0 | goto cleanup; |
1518 | 0 | } |
1519 | | |
1520 | 0 | kdf_params->iter_count = PKCS12_ITER_COUNT; |
1521 | 0 | key->size = kdf_params->key_size = |
1522 | 0 | gnutls_cipher_get_key_size(enc_params->cipher); |
1523 | |
|
1524 | 0 | enc_params->iv_size = gnutls_cipher_get_iv_size(enc_params->cipher); |
1525 | 0 | key->data = gnutls_malloc(key->size); |
1526 | 0 | if (key->data == NULL) { |
1527 | 0 | gnutls_assert(); |
1528 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
1529 | 0 | goto cleanup; |
1530 | 0 | } |
1531 | | |
1532 | | /* now generate the key. |
1533 | | */ |
1534 | | |
1535 | 0 | if (p->pbes2 != 0) { |
1536 | 0 | if (p->schema == PBES2_GOST28147_89_TC26Z) |
1537 | 0 | kdf_params->mac = GNUTLS_MAC_STREEBOG_512; |
1538 | 0 | else if (p->schema == PBES2_GOST28147_89_CPA || |
1539 | 0 | p->schema == PBES2_GOST28147_89_CPB || |
1540 | 0 | p->schema == PBES2_GOST28147_89_CPC || |
1541 | 0 | p->schema == PBES2_GOST28147_89_CPD) |
1542 | 0 | kdf_params->mac = GNUTLS_MAC_GOSTR_94; |
1543 | 0 | else |
1544 | 0 | kdf_params->mac = GNUTLS_MAC_SHA256; |
1545 | 0 | ret = _gnutls_pbes2_string_to_key(pass_len, password, |
1546 | 0 | kdf_params, |
1547 | 0 | kdf_params->key_size, |
1548 | 0 | key->data); |
1549 | 0 | if (ret < 0) { |
1550 | 0 | gnutls_assert(); |
1551 | 0 | return ret; |
1552 | 0 | } |
1553 | | |
1554 | 0 | if (enc_params->iv_size) { |
1555 | 0 | ret = gnutls_rnd(GNUTLS_RND_NONCE, enc_params->iv, |
1556 | 0 | enc_params->iv_size); |
1557 | 0 | if (ret < 0) { |
1558 | 0 | gnutls_assert(); |
1559 | 0 | goto cleanup; |
1560 | 0 | } |
1561 | 0 | } |
1562 | 0 | } else { /* PKCS 12 schema */ |
1563 | 0 | ret = _gnutls_pkcs12_string_to_key( |
1564 | 0 | mac_to_entry(GNUTLS_MAC_SHA1), 1 /*KEY*/, |
1565 | 0 | kdf_params->salt, kdf_params->salt_size, |
1566 | 0 | kdf_params->iter_count, password, kdf_params->key_size, |
1567 | 0 | key->data); |
1568 | 0 | if (ret < 0) { |
1569 | 0 | gnutls_assert(); |
1570 | 0 | goto cleanup; |
1571 | 0 | } |
1572 | | |
1573 | | /* Now generate the IV |
1574 | | */ |
1575 | 0 | if (enc_params->iv_size) { |
1576 | 0 | ret = _gnutls_pkcs12_string_to_key( |
1577 | 0 | mac_to_entry(GNUTLS_MAC_SHA1), 2 /*IV*/, |
1578 | 0 | kdf_params->salt, kdf_params->salt_size, |
1579 | 0 | kdf_params->iter_count, password, |
1580 | 0 | enc_params->iv_size, enc_params->iv); |
1581 | 0 | if (ret < 0) { |
1582 | 0 | gnutls_assert(); |
1583 | 0 | goto cleanup; |
1584 | 0 | } |
1585 | 0 | } |
1586 | 0 | } |
1587 | | |
1588 | 0 | ret = 0; |
1589 | |
|
1590 | 0 | cleanup: |
1591 | 0 | gnutls_free(password); |
1592 | 0 | return ret; |
1593 | 0 | } |
1594 | | |
1595 | | /* Encodes the parameters to be written in the encryptionAlgorithm.parameters |
1596 | | * part. |
1597 | | */ |
1598 | | int _gnutls_pkcs_write_schema_params(schema_id schema, asn1_node pkcs8_asn, |
1599 | | const char *where, |
1600 | | const struct pbkdf2_params *kdf_params, |
1601 | | const struct pbe_enc_params *enc_params) |
1602 | 0 | { |
1603 | 0 | int result; |
1604 | 0 | asn1_node pasn = NULL; |
1605 | 0 | const struct pkcs_cipher_schema_st *p; |
1606 | |
|
1607 | 0 | p = _gnutls_pkcs_schema_get(schema); |
1608 | |
|
1609 | 0 | if (p != NULL && p->pbes2 != 0) { /* PBES2 */ |
1610 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1611 | 0 | "PKIX1.pkcs-5-PBES2-params", |
1612 | 0 | &pasn)) != ASN1_SUCCESS) { |
1613 | 0 | gnutls_assert(); |
1614 | 0 | return _gnutls_asn2err(result); |
1615 | 0 | } |
1616 | | |
1617 | 0 | result = _gnutls_write_pbkdf2_params(pasn, kdf_params); |
1618 | 0 | if (result < 0) { |
1619 | 0 | gnutls_assert(); |
1620 | 0 | goto error; |
1621 | 0 | } |
1622 | | |
1623 | 0 | result = write_pbes2_enc_params(pasn, enc_params); |
1624 | 0 | if (result < 0) { |
1625 | 0 | gnutls_assert(); |
1626 | 0 | goto error; |
1627 | 0 | } |
1628 | | |
1629 | 0 | result = _gnutls_x509_der_encode_and_copy(pasn, "", pkcs8_asn, |
1630 | 0 | where, 0); |
1631 | 0 | if (result < 0) { |
1632 | 0 | gnutls_assert(); |
1633 | 0 | goto error; |
1634 | 0 | } |
1635 | | |
1636 | 0 | asn1_delete_structure(&pasn); |
1637 | |
|
1638 | 0 | } else if (p != NULL) { /* PKCS #12 */ |
1639 | |
|
1640 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1641 | 0 | "PKIX1.pkcs-12-PbeParams", |
1642 | 0 | &pasn)) != ASN1_SUCCESS) { |
1643 | 0 | gnutls_assert(); |
1644 | 0 | result = _gnutls_asn2err(result); |
1645 | 0 | goto error; |
1646 | 0 | } |
1647 | | |
1648 | 0 | result = write_pkcs12_kdf_params(pasn, kdf_params); |
1649 | 0 | if (result < 0) { |
1650 | 0 | gnutls_assert(); |
1651 | 0 | goto error; |
1652 | 0 | } |
1653 | | |
1654 | 0 | result = _gnutls_x509_der_encode_and_copy(pasn, "", pkcs8_asn, |
1655 | 0 | where, 0); |
1656 | 0 | if (result < 0) { |
1657 | 0 | gnutls_assert(); |
1658 | 0 | goto error; |
1659 | 0 | } |
1660 | | |
1661 | 0 | asn1_delete_structure(&pasn); |
1662 | 0 | } |
1663 | | |
1664 | 0 | return 0; |
1665 | | |
1666 | 0 | error: |
1667 | 0 | asn1_delete_structure(&pasn); |
1668 | 0 | return result; |
1669 | 0 | } |
1670 | | |
1671 | | int _gnutls_pkcs_raw_encrypt_data(const gnutls_datum_t *plain, |
1672 | | const struct pbe_enc_params *enc_params, |
1673 | | const gnutls_datum_t *key, |
1674 | | gnutls_datum_t *encrypted) |
1675 | 0 | { |
1676 | 0 | int result; |
1677 | 0 | int data_size; |
1678 | 0 | uint8_t *data = NULL; |
1679 | 0 | gnutls_datum_t d_iv; |
1680 | 0 | gnutls_cipher_hd_t ch = NULL; |
1681 | 0 | uint8_t pad, pad_size; |
1682 | 0 | const cipher_entry_st *ce; |
1683 | |
|
1684 | 0 | ce = cipher_to_entry(enc_params->cipher); |
1685 | 0 | pad_size = _gnutls_cipher_get_block_size(ce); |
1686 | |
|
1687 | 0 | if (pad_size == 1 || ce->type == CIPHER_STREAM) /* stream */ |
1688 | 0 | pad_size = 0; |
1689 | |
|
1690 | 0 | data = gnutls_malloc(plain->size + pad_size); |
1691 | 0 | if (data == NULL) { |
1692 | 0 | gnutls_assert(); |
1693 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1694 | 0 | } |
1695 | | |
1696 | 0 | memcpy(data, plain->data, plain->size); |
1697 | |
|
1698 | 0 | if (pad_size > 0) { |
1699 | 0 | pad = pad_size - (plain->size % pad_size); |
1700 | 0 | if (pad == 0) |
1701 | 0 | pad = pad_size; |
1702 | 0 | memset(&data[plain->size], pad, pad); |
1703 | 0 | } else |
1704 | 0 | pad = 0; |
1705 | |
|
1706 | 0 | data_size = plain->size + pad; |
1707 | |
|
1708 | 0 | d_iv.data = (uint8_t *)enc_params->iv; |
1709 | 0 | d_iv.size = enc_params->iv_size; |
1710 | 0 | result = gnutls_cipher_init(&ch, enc_params->cipher, key, &d_iv); |
1711 | 0 | if (result < 0) { |
1712 | 0 | gnutls_assert(); |
1713 | 0 | goto error; |
1714 | 0 | } |
1715 | | |
1716 | 0 | result = gnutls_cipher_encrypt(ch, data, data_size); |
1717 | 0 | if (result < 0) { |
1718 | 0 | gnutls_assert(); |
1719 | 0 | goto error; |
1720 | 0 | } |
1721 | | |
1722 | 0 | encrypted->data = data; |
1723 | 0 | encrypted->size = data_size; |
1724 | |
|
1725 | 0 | gnutls_cipher_deinit(ch); |
1726 | |
|
1727 | 0 | return 0; |
1728 | | |
1729 | 0 | error: |
1730 | 0 | gnutls_free(data); |
1731 | 0 | if (ch) { |
1732 | 0 | gnutls_cipher_deinit(ch); |
1733 | 0 | } |
1734 | 0 | return result; |
1735 | 0 | } |
1736 | | |
1737 | | int _gnutls_pbmac1(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key, |
1738 | | const struct pbkdf2_params *params, |
1739 | | const gnutls_datum_t *data, uint8_t *output) |
1740 | 0 | { |
1741 | 0 | int result; |
1742 | 0 | gnutls_datum_t salt; |
1743 | 0 | uint8_t mac_key[MAX_HASH_SIZE]; |
1744 | | |
1745 | | /* Derive the MAC key */ |
1746 | 0 | salt.data = (void *)params->salt; |
1747 | 0 | salt.size = params->salt_size; |
1748 | 0 | result = gnutls_pbkdf2(params->mac, key, &salt, params->iter_count, |
1749 | 0 | mac_key, params->key_size); |
1750 | 0 | if (result < 0) |
1751 | 0 | return gnutls_assert_val(result); |
1752 | | |
1753 | | /* Calculate the MAC */ |
1754 | 0 | result = gnutls_hmac_fast(mac, mac_key, params->key_size, data->data, |
1755 | 0 | data->size, output); |
1756 | 0 | if (result < 0) |
1757 | 0 | return gnutls_assert_val(result); |
1758 | | |
1759 | 0 | return result; |
1760 | 0 | } |
1761 | | |
1762 | | static int read_pbmac1_auth(asn1_node pasn, const gnutls_datum_t *der) |
1763 | 0 | { |
1764 | 0 | char oid[MAX_OID_SIZE]; |
1765 | 0 | int len; |
1766 | 0 | int result; |
1767 | |
|
1768 | 0 | len = sizeof(oid); |
1769 | 0 | result = |
1770 | 0 | asn1_read_value(pasn, "messageAuthScheme.algorithm", oid, &len); |
1771 | 0 | if (result != ASN1_SUCCESS) { |
1772 | 0 | gnutls_assert(); |
1773 | 0 | return _gnutls_asn2err(result); |
1774 | 0 | } |
1775 | 0 | _gnutls_hard_log("messageAuthScheme.algorithm: %s\n", oid); |
1776 | |
|
1777 | 0 | return gnutls_oid_to_mac(oid); |
1778 | 0 | } |
1779 | | |
1780 | | int _gnutls_read_pbmac1_params(const uint8_t *data, int data_size, |
1781 | | struct pbkdf2_params *kdf_params, |
1782 | | gnutls_mac_algorithm_t *mac) |
1783 | 0 | { |
1784 | 0 | asn1_node pasn = NULL; |
1785 | 0 | int result; |
1786 | 0 | gnutls_datum_t tmp; |
1787 | |
|
1788 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1789 | 0 | "PKIX1.pkcs-5-PBMAC1-params", |
1790 | 0 | &pasn)) != ASN1_SUCCESS) { |
1791 | 0 | gnutls_assert(); |
1792 | 0 | result = _gnutls_asn2err(result); |
1793 | 0 | goto error; |
1794 | 0 | } |
1795 | | |
1796 | 0 | result = _asn1_strict_der_decode(&pasn, data, data_size, NULL); |
1797 | 0 | if (result != ASN1_SUCCESS) { |
1798 | 0 | gnutls_assert(); |
1799 | 0 | result = _gnutls_asn2err(result); |
1800 | 0 | goto error; |
1801 | 0 | } |
1802 | | |
1803 | 0 | tmp.data = (uint8_t *)data; |
1804 | 0 | tmp.size = data_size; |
1805 | |
|
1806 | 0 | result = _gnutls_read_pbkdf2_params(pasn, &tmp, kdf_params); |
1807 | 0 | if (result < 0) { |
1808 | 0 | gnutls_assert(); |
1809 | 0 | goto error; |
1810 | 0 | } |
1811 | | |
1812 | 0 | result = read_pbmac1_auth(pasn, &tmp); |
1813 | 0 | if (result < 0) { |
1814 | 0 | gnutls_assert(); |
1815 | 0 | goto error; |
1816 | 0 | } |
1817 | 0 | *mac = result; |
1818 | | |
1819 | | /* The keyLength field must present and the minimum is 20 bytes. |
1820 | | */ |
1821 | 0 | if (kdf_params->key_size < 20) { |
1822 | 0 | gnutls_assert(); |
1823 | 0 | result = GNUTLS_E_INSUFFICIENT_SECURITY; |
1824 | 0 | goto error; |
1825 | 0 | } |
1826 | | |
1827 | 0 | result = 0; |
1828 | |
|
1829 | 0 | error: |
1830 | 0 | asn1_delete_structure2(&pasn, ASN1_DELETE_FLAG_ZEROIZE); |
1831 | 0 | return result; |
1832 | 0 | } |
1833 | | |
1834 | | static int write_pbmac1_auth(asn1_node pasn, gnutls_mac_algorithm_t algo) |
1835 | 0 | { |
1836 | 0 | int result; |
1837 | 0 | const mac_entry_st *me = mac_to_entry(algo); |
1838 | |
|
1839 | 0 | if (unlikely(me == NULL)) |
1840 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1841 | | |
1842 | 0 | result = asn1_write_value(pasn, "messageAuthScheme.algorithm", |
1843 | 0 | me->mac_oid, 1); |
1844 | 0 | if (result != ASN1_SUCCESS) { |
1845 | 0 | gnutls_assert(); |
1846 | 0 | return _gnutls_asn2err(result); |
1847 | 0 | } |
1848 | 0 | _gnutls_hard_log("messageAuthScheme.algorithm: %s\n", me->oid); |
1849 | |
|
1850 | 0 | result = |
1851 | 0 | asn1_write_value(pasn, "messageAuthScheme.parameters", NULL, 0); |
1852 | 0 | if (result != ASN1_SUCCESS) { |
1853 | 0 | gnutls_assert(); |
1854 | 0 | return _gnutls_asn2err(result); |
1855 | 0 | } |
1856 | | |
1857 | 0 | return 0; |
1858 | 0 | } |
1859 | | |
1860 | | int _gnutls_write_pbmac1_params(asn1_node pkcs12, |
1861 | | const struct pbkdf2_params *kdf_params, |
1862 | | gnutls_mac_algorithm_t algo, const char *where) |
1863 | 0 | { |
1864 | 0 | int result; |
1865 | 0 | asn1_node pasn = NULL; |
1866 | |
|
1867 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
1868 | 0 | "PKIX1.pkcs-5-PBMAC1-params", |
1869 | 0 | &pasn)) != ASN1_SUCCESS) { |
1870 | 0 | gnutls_assert(); |
1871 | 0 | result = _gnutls_asn2err(result); |
1872 | 0 | goto error; |
1873 | 0 | } |
1874 | | |
1875 | 0 | result = _gnutls_write_pbkdf2_params(pasn, kdf_params); |
1876 | 0 | if (result < 0) { |
1877 | 0 | gnutls_assert(); |
1878 | 0 | goto error; |
1879 | 0 | } |
1880 | | |
1881 | 0 | result = write_pbmac1_auth(pasn, algo); |
1882 | 0 | if (result < 0) { |
1883 | 0 | gnutls_assert(); |
1884 | 0 | goto error; |
1885 | 0 | } |
1886 | | |
1887 | 0 | result = _gnutls_x509_der_encode_and_copy(pasn, "", pkcs12, where, 0); |
1888 | 0 | if (result < 0) { |
1889 | 0 | gnutls_assert(); |
1890 | 0 | goto error; |
1891 | 0 | } |
1892 | 0 | error: |
1893 | 0 | asn1_delete_structure2(&pasn, ASN1_DELETE_FLAG_ZEROIZE); |
1894 | 0 | return result; |
1895 | 0 | } |