/src/mbedtls/library/pkcs12.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * PKCS#12 Personal Information Exchange Syntax |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
8 | | * not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
15 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | /* |
20 | | * The PKCS #12 Personal Information Exchange Syntax Standard v1.1 |
21 | | * |
22 | | * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf |
23 | | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn |
24 | | */ |
25 | | |
26 | | #include "common.h" |
27 | | |
28 | | #if defined(MBEDTLS_PKCS12_C) |
29 | | |
30 | | #include "mbedtls/pkcs12.h" |
31 | | #include "mbedtls/asn1.h" |
32 | | #include "mbedtls/cipher.h" |
33 | | #include "mbedtls/platform_util.h" |
34 | | #include "mbedtls/error.h" |
35 | | |
36 | | #include <string.h> |
37 | | |
38 | | #if defined(MBEDTLS_DES_C) |
39 | | #include "mbedtls/des.h" |
40 | | #endif |
41 | | |
42 | | #include "hash_info.h" |
43 | | #include "mbedtls/psa_util.h" |
44 | | |
45 | | #if defined(MBEDTLS_ASN1_PARSE_C) |
46 | | |
47 | | static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params, |
48 | | mbedtls_asn1_buf *salt, int *iterations) |
49 | 0 | { |
50 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
51 | 0 | unsigned char **p = ¶ms->p; |
52 | 0 | const unsigned char *end = params->p + params->len; |
53 | | |
54 | | /* |
55 | | * pkcs-12PbeParams ::= SEQUENCE { |
56 | | * salt OCTET STRING, |
57 | | * iterations INTEGER |
58 | | * } |
59 | | * |
60 | | */ |
61 | 0 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
62 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
63 | 0 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
64 | 0 | } |
65 | | |
66 | 0 | if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
67 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret); |
68 | 0 | } |
69 | | |
70 | 0 | salt->p = *p; |
71 | 0 | *p += salt->len; |
72 | |
|
73 | 0 | if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) { |
74 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret); |
75 | 0 | } |
76 | | |
77 | 0 | if (*p != end) { |
78 | 0 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
79 | 0 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
80 | 0 | } |
81 | | |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | 0 | #define PKCS12_MAX_PWDLEN 128 |
86 | | |
87 | | static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type, |
88 | | const unsigned char *pwd, size_t pwdlen, |
89 | | unsigned char *key, size_t keylen, |
90 | | unsigned char *iv, size_t ivlen) |
91 | 0 | { |
92 | 0 | int ret, iterations = 0; |
93 | 0 | mbedtls_asn1_buf salt; |
94 | 0 | size_t i; |
95 | 0 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
96 | |
|
97 | 0 | if (pwdlen > PKCS12_MAX_PWDLEN) { |
98 | 0 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
99 | 0 | } |
100 | | |
101 | 0 | memset(&salt, 0, sizeof(mbedtls_asn1_buf)); |
102 | 0 | memset(&unipwd, 0, sizeof(unipwd)); |
103 | |
|
104 | 0 | if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt, |
105 | 0 | &iterations)) != 0) { |
106 | 0 | return ret; |
107 | 0 | } |
108 | | |
109 | 0 | for (i = 0; i < pwdlen; i++) { |
110 | 0 | unipwd[i * 2 + 1] = pwd[i]; |
111 | 0 | } |
112 | |
|
113 | 0 | if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2, |
114 | 0 | salt.p, salt.len, md_type, |
115 | 0 | MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) { |
116 | 0 | return ret; |
117 | 0 | } |
118 | | |
119 | 0 | if (iv == NULL || ivlen == 0) { |
120 | 0 | return 0; |
121 | 0 | } |
122 | | |
123 | 0 | if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2, |
124 | 0 | salt.p, salt.len, md_type, |
125 | 0 | MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) { |
126 | 0 | return ret; |
127 | 0 | } |
128 | 0 | return 0; |
129 | 0 | } |
130 | | |
131 | | #undef PKCS12_MAX_PWDLEN |
132 | | |
133 | | int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode, |
134 | | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
135 | | const unsigned char *pwd, size_t pwdlen, |
136 | | const unsigned char *data, size_t len, |
137 | | unsigned char *output) |
138 | 0 | { |
139 | 0 | int ret, keylen = 0; |
140 | 0 | unsigned char key[32]; |
141 | 0 | unsigned char iv[16]; |
142 | 0 | const mbedtls_cipher_info_t *cipher_info; |
143 | 0 | mbedtls_cipher_context_t cipher_ctx; |
144 | 0 | size_t olen = 0; |
145 | |
|
146 | 0 | if (pwd == NULL && pwdlen != 0) { |
147 | 0 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
148 | 0 | } |
149 | | |
150 | 0 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
151 | 0 | if (cipher_info == NULL) { |
152 | 0 | return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE; |
153 | 0 | } |
154 | | |
155 | 0 | keylen = cipher_info->key_bitlen / 8; |
156 | |
|
157 | 0 | if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen, |
158 | 0 | key, keylen, |
159 | 0 | iv, cipher_info->iv_size)) != 0) { |
160 | 0 | return ret; |
161 | 0 | } |
162 | | |
163 | 0 | mbedtls_cipher_init(&cipher_ctx); |
164 | |
|
165 | 0 | if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) { |
166 | 0 | goto exit; |
167 | 0 | } |
168 | | |
169 | 0 | if ((ret = |
170 | 0 | mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen, |
171 | 0 | (mbedtls_operation_t) mode)) != 0) { |
172 | 0 | goto exit; |
173 | 0 | } |
174 | | |
175 | 0 | if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) { |
176 | 0 | goto exit; |
177 | 0 | } |
178 | | |
179 | 0 | if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) { |
180 | 0 | goto exit; |
181 | 0 | } |
182 | | |
183 | 0 | if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len, |
184 | 0 | output, &olen)) != 0) { |
185 | 0 | goto exit; |
186 | 0 | } |
187 | | |
188 | 0 | if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + olen, &olen)) != 0) { |
189 | 0 | ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH; |
190 | 0 | } |
191 | |
|
192 | 0 | exit: |
193 | 0 | mbedtls_platform_zeroize(key, sizeof(key)); |
194 | 0 | mbedtls_platform_zeroize(iv, sizeof(iv)); |
195 | 0 | mbedtls_cipher_free(&cipher_ctx); |
196 | |
|
197 | 0 | return ret; |
198 | 0 | } |
199 | | |
200 | | #endif /* MBEDTLS_ASN1_PARSE_C */ |
201 | | |
202 | | static void pkcs12_fill_buffer(unsigned char *data, size_t data_len, |
203 | | const unsigned char *filler, size_t fill_len) |
204 | 0 | { |
205 | 0 | unsigned char *p = data; |
206 | 0 | size_t use_len; |
207 | |
|
208 | 0 | if (filler != NULL && fill_len != 0) { |
209 | 0 | while (data_len > 0) { |
210 | 0 | use_len = (data_len > fill_len) ? fill_len : data_len; |
211 | 0 | memcpy(p, filler, use_len); |
212 | 0 | p += use_len; |
213 | 0 | data_len -= use_len; |
214 | 0 | } |
215 | 0 | } else { |
216 | | /* If either of the above are not true then clearly there is nothing |
217 | | * that this function can do. The function should *not* be called |
218 | | * under either of those circumstances, as you could end up with an |
219 | | * incorrect output but for safety's sake, leaving the check in as |
220 | | * otherwise we could end up with memory corruption.*/ |
221 | 0 | } |
222 | 0 | } |
223 | | |
224 | | |
225 | | static int calculate_hashes(mbedtls_md_type_t md_type, int iterations, |
226 | | unsigned char *diversifier, unsigned char *salt_block, |
227 | | unsigned char *pwd_block, unsigned char *hash_output, int use_salt, |
228 | | int use_password, size_t hlen, size_t v) |
229 | 0 | { |
230 | 0 | #if defined(MBEDTLS_MD_C) |
231 | 0 | int ret = -1; |
232 | 0 | size_t i; |
233 | 0 | const mbedtls_md_info_t *md_info; |
234 | 0 | mbedtls_md_context_t md_ctx; |
235 | 0 | md_info = mbedtls_md_info_from_type(md_type); |
236 | 0 | if (md_info == NULL) { |
237 | 0 | return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE; |
238 | 0 | } |
239 | | |
240 | 0 | mbedtls_md_init(&md_ctx); |
241 | |
|
242 | 0 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) { |
243 | 0 | return ret; |
244 | 0 | } |
245 | | // Calculate hash( diversifier || salt_block || pwd_block ) |
246 | 0 | if ((ret = mbedtls_md_starts(&md_ctx)) != 0) { |
247 | 0 | goto exit; |
248 | 0 | } |
249 | | |
250 | 0 | if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) { |
251 | 0 | goto exit; |
252 | 0 | } |
253 | | |
254 | 0 | if (use_salt != 0) { |
255 | 0 | if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) { |
256 | 0 | goto exit; |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | 0 | if (use_password != 0) { |
261 | 0 | if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) { |
262 | 0 | goto exit; |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | 0 | if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) { |
267 | 0 | goto exit; |
268 | 0 | } |
269 | | |
270 | | // Perform remaining ( iterations - 1 ) recursive hash calculations |
271 | 0 | for (i = 1; i < (size_t) iterations; i++) { |
272 | 0 | if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) |
273 | 0 | != 0) { |
274 | 0 | goto exit; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | 0 | exit: |
279 | 0 | mbedtls_md_free(&md_ctx); |
280 | 0 | return ret; |
281 | | #else |
282 | | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
283 | | psa_algorithm_t alg = mbedtls_psa_translate_md(md_type); |
284 | | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
285 | | psa_status_t status_abort = PSA_ERROR_CORRUPTION_DETECTED; |
286 | | size_t i, out_len, out_size = PSA_HASH_LENGTH(alg); |
287 | | |
288 | | if (alg == PSA_ALG_NONE) { |
289 | | return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE; |
290 | | } |
291 | | |
292 | | if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) { |
293 | | goto exit; |
294 | | } |
295 | | |
296 | | // Calculate hash( diversifier || salt_block || pwd_block ) |
297 | | if ((status = psa_hash_update(&op, diversifier, v)) != PSA_SUCCESS) { |
298 | | goto exit; |
299 | | } |
300 | | |
301 | | if (use_salt != 0) { |
302 | | if ((status = psa_hash_update(&op, salt_block, v)) != PSA_SUCCESS) { |
303 | | goto exit; |
304 | | } |
305 | | } |
306 | | |
307 | | if (use_password != 0) { |
308 | | if ((status = psa_hash_update(&op, pwd_block, v)) != PSA_SUCCESS) { |
309 | | goto exit; |
310 | | } |
311 | | } |
312 | | |
313 | | if ((status = psa_hash_finish(&op, hash_output, out_size, &out_len)) |
314 | | != PSA_SUCCESS) { |
315 | | goto exit; |
316 | | } |
317 | | |
318 | | // Perform remaining ( iterations - 1 ) recursive hash calculations |
319 | | for (i = 1; i < (size_t) iterations; i++) { |
320 | | if ((status = psa_hash_compute(alg, hash_output, hlen, hash_output, |
321 | | out_size, &out_len)) != PSA_SUCCESS) { |
322 | | goto exit; |
323 | | } |
324 | | } |
325 | | |
326 | | exit: |
327 | | status_abort = psa_hash_abort(&op); |
328 | | if (status == PSA_SUCCESS) { |
329 | | status = status_abort; |
330 | | } |
331 | | return mbedtls_md_error_from_psa(status); |
332 | | #endif /* !MBEDTLS_MD_C */ |
333 | 0 | } |
334 | | |
335 | | |
336 | | int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen, |
337 | | const unsigned char *pwd, size_t pwdlen, |
338 | | const unsigned char *salt, size_t saltlen, |
339 | | mbedtls_md_type_t md_type, int id, int iterations) |
340 | 0 | { |
341 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
342 | 0 | unsigned int j; |
343 | |
|
344 | 0 | unsigned char diversifier[128]; |
345 | 0 | unsigned char salt_block[128], pwd_block[128], hash_block[128] = { 0 }; |
346 | 0 | unsigned char hash_output[MBEDTLS_HASH_MAX_SIZE]; |
347 | 0 | unsigned char *p; |
348 | 0 | unsigned char c; |
349 | 0 | int use_password = 0; |
350 | 0 | int use_salt = 0; |
351 | |
|
352 | 0 | size_t hlen, use_len, v, i; |
353 | | |
354 | | // This version only allows max of 64 bytes of password or salt |
355 | 0 | if (datalen > 128 || pwdlen > 64 || saltlen > 64) { |
356 | 0 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
357 | 0 | } |
358 | | |
359 | 0 | if (pwd == NULL && pwdlen != 0) { |
360 | 0 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
361 | 0 | } |
362 | | |
363 | 0 | if (salt == NULL && saltlen != 0) { |
364 | 0 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
365 | 0 | } |
366 | | |
367 | 0 | use_password = (pwd && pwdlen != 0); |
368 | 0 | use_salt = (salt && saltlen != 0); |
369 | |
|
370 | 0 | hlen = mbedtls_hash_info_get_size(md_type); |
371 | |
|
372 | 0 | if (hlen <= 32) { |
373 | 0 | v = 64; |
374 | 0 | } else { |
375 | 0 | v = 128; |
376 | 0 | } |
377 | |
|
378 | 0 | memset(diversifier, (unsigned char) id, v); |
379 | |
|
380 | 0 | if (use_salt != 0) { |
381 | 0 | pkcs12_fill_buffer(salt_block, v, salt, saltlen); |
382 | 0 | } |
383 | |
|
384 | 0 | if (use_password != 0) { |
385 | 0 | pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen); |
386 | 0 | } |
387 | |
|
388 | 0 | p = data; |
389 | 0 | while (datalen > 0) { |
390 | 0 | if (calculate_hashes(md_type, iterations, diversifier, salt_block, |
391 | 0 | pwd_block, hash_output, use_salt, use_password, hlen, |
392 | 0 | v) != 0) { |
393 | 0 | goto exit; |
394 | 0 | } |
395 | | |
396 | 0 | use_len = (datalen > hlen) ? hlen : datalen; |
397 | 0 | memcpy(p, hash_output, use_len); |
398 | 0 | datalen -= use_len; |
399 | 0 | p += use_len; |
400 | |
|
401 | 0 | if (datalen == 0) { |
402 | 0 | break; |
403 | 0 | } |
404 | | |
405 | | // Concatenating copies of hash_output into hash_block (B) |
406 | 0 | pkcs12_fill_buffer(hash_block, v, hash_output, hlen); |
407 | | |
408 | | // B += 1 |
409 | 0 | for (i = v; i > 0; i--) { |
410 | 0 | if (++hash_block[i - 1] != 0) { |
411 | 0 | break; |
412 | 0 | } |
413 | 0 | } |
414 | |
|
415 | 0 | if (use_salt != 0) { |
416 | | // salt_block += B |
417 | 0 | c = 0; |
418 | 0 | for (i = v; i > 0; i--) { |
419 | 0 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
420 | 0 | c = MBEDTLS_BYTE_1(j); |
421 | 0 | salt_block[i - 1] = MBEDTLS_BYTE_0(j); |
422 | 0 | } |
423 | 0 | } |
424 | |
|
425 | 0 | if (use_password != 0) { |
426 | | // pwd_block += B |
427 | 0 | c = 0; |
428 | 0 | for (i = v; i > 0; i--) { |
429 | 0 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
430 | 0 | c = MBEDTLS_BYTE_1(j); |
431 | 0 | pwd_block[i - 1] = MBEDTLS_BYTE_0(j); |
432 | 0 | } |
433 | 0 | } |
434 | 0 | } |
435 | | |
436 | 0 | ret = 0; |
437 | |
|
438 | 0 | exit: |
439 | 0 | mbedtls_platform_zeroize(salt_block, sizeof(salt_block)); |
440 | 0 | mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block)); |
441 | 0 | mbedtls_platform_zeroize(hash_block, sizeof(hash_block)); |
442 | 0 | mbedtls_platform_zeroize(hash_output, sizeof(hash_output)); |
443 | |
|
444 | 0 | return ret; |
445 | 0 | } |
446 | | |
447 | | #endif /* MBEDTLS_PKCS12_C */ |