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