blob: 1f45f45a8387a6549fed65807fc72dd4393c5de6 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020018 */
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pkcs12.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000034#include "mbedtls/error.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/arc4.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020044#endif
45
Hanno Becker8a89f9f2018-10-12 10:46:32 +010046#if defined(MBEDTLS_ASN1_PARSE_C)
47
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
49 mbedtls_asn1_buf *salt, int *iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020050{
Janos Follath24eed8d2019-11-22 13:21:35 +000051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020052 unsigned char **p = &params->p;
53 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020054
55 /*
56 * pkcs-12PbeParams ::= SEQUENCE {
57 * salt OCTET STRING,
58 * iterations INTEGER
59 * }
60 *
61 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
63 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
64 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
65 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
68 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
69 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070
71 salt->p = *p;
72 *p += salt->len;
73
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
75 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
76 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 if (*p != end) {
79 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
80 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
81 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020084}
85
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020086#define PKCS12_MAX_PWDLEN 128
87
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
89 const unsigned char *pwd, size_t pwdlen,
90 unsigned char *key, size_t keylen,
91 unsigned char *iv, size_t ivlen)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020092{
Nicholas Wilson409401c2016-04-13 11:48:25 +010093 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020095 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020096 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if (pwdlen > PKCS12_MAX_PWDLEN) {
99 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
100 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 memset(&salt, 0, sizeof(mbedtls_asn1_buf));
103 memset(&unipwd, 0, sizeof(unipwd));
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt,
106 &iterations)) != 0) {
107 return ret;
108 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 for (i = 0; i < pwdlen; i++) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200111 unipwd[i * 2 + 1] = pwd[i];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2,
115 salt.p, salt.len, md_type,
116 MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
117 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200118 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119
120 if (iv == NULL || ivlen == 0) {
121 return 0;
122 }
123
124 if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2,
125 salt.p, salt.len, md_type,
126 MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
127 return ret;
128 }
129 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200130}
131
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200132#undef PKCS12_MAX_PWDLEN
133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
135 const unsigned char *pwd, size_t pwdlen,
136 const unsigned char *data, size_t len,
137 unsigned char *output)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200140 ((void) pbe_params);
141 ((void) mode);
142 ((void) pwd);
143 ((void) pwdlen);
144 ((void) data);
145 ((void) len);
146 ((void) output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200148#else
Janos Follath24eed8d2019-11-22 13:21:35 +0000149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200150 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200152 ((void) mode);
153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 mbedtls_arc4_init(&ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, MBEDTLS_MD_SHA1,
157 pwd, pwdlen,
158 key, 16, NULL, 0)) != 0) {
159 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200160 }
161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 mbedtls_arc4_setup(&ctx, key, 16);
163 if ((ret = mbedtls_arc4_crypt(&ctx, len, data, output)) != 0) {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200164 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200166
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200167exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 mbedtls_platform_zeroize(key, sizeof(key));
169 mbedtls_arc4_free(&ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200173}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
176 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
177 const unsigned char *pwd, size_t pwdlen,
178 const unsigned char *data, size_t len,
179 unsigned char *output)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200180{
Paul Bakker38b50d72013-06-24 19:33:27 +0200181 int ret, keylen = 0;
182 unsigned char key[32];
183 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 const mbedtls_cipher_info_t *cipher_info;
185 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200186 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 if (pwd == NULL && pwdlen != 0) {
189 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
190 }
Paul Elliottfe724fe2021-11-11 19:00:38 +0000191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
193 if (cipher_info == NULL) {
194 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
195 }
Paul Bakker38b50d72013-06-24 19:33:27 +0200196
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200197 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
200 key, keylen,
201 iv, cipher_info->iv_size)) != 0) {
202 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200203 }
204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200208 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200209 }
210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 if ((ret =
212 mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
213 (mbedtls_operation_t) mode)) != 0) {
214 goto exit;
215 }
216
Waleed Elmelegy38a89ad2023-09-04 15:11:22 +0100217#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
218 /* PKCS12 uses CBC with PKCS7 padding */
219
220 mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
221#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
222 /* For historical reasons, when decrypting, this function works when
223 * decrypting even when support for PKCS7 padding is disabled. In this
224 * case, it ignores the padding, and so will never report a
225 * password mismatch.
226 */
227 if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
228 padding = MBEDTLS_PADDING_NONE;
229 }
230#endif
231 if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
232 goto exit;
233 }
234#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) {
237 goto exit;
238 }
239
240 if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
241 goto exit;
242 }
243
244 if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
245 output, &olen)) != 0) {
246 goto exit;
247 }
248
249 if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + olen, &olen)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200252
Paul Bakkerbd552442013-07-03 14:44:40 +0200253exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_platform_zeroize(key, sizeof(key));
255 mbedtls_platform_zeroize(iv, sizeof(iv));
256 mbedtls_cipher_free(&cipher_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return ret;
Paul Bakker531e2942013-06-24 19:23:12 +0200259}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200260
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100261#endif /* MBEDTLS_ASN1_PARSE_C */
262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
264 const unsigned char *filler, size_t fill_len)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200265{
266 unsigned char *p = data;
267 size_t use_len;
268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (filler != NULL && fill_len != 0) {
270 while (data_len > 0) {
271 use_len = (data_len > fill_len) ? fill_len : data_len;
272 memcpy(p, filler, use_len);
Paul Elliottfe724fe2021-11-11 19:00:38 +0000273 p += use_len;
274 data_len -= use_len;
275 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 } else {
Paul Elliott8d7eef42021-12-02 17:51:34 +0000277 /* If either of the above are not true then clearly there is nothing
278 * that this function can do. The function should *not* be called
279 * under either of those circumstances, as you could end up with an
280 * incorrect output but for safety's sake, leaving the check in as
281 * otherwise we could end up with memory corruption.*/
282 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200283}
284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
286 const unsigned char *pwd, size_t pwdlen,
287 const unsigned char *salt, size_t saltlen,
288 mbedtls_md_type_t md_type, int id, int iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200289{
Janos Follath24eed8d2019-11-22 13:21:35 +0000290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200291 unsigned int j;
292
293 unsigned char diversifier[128];
294 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200296 unsigned char *p;
297 unsigned char c;
Paul Elliott7412eb42021-11-18 14:02:21 +0000298 int use_password = 0;
299 int use_salt = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200301 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 const mbedtls_md_info_t *md_info;
304 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200305
306 // This version only allows max of 64 bytes of password or salt
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
308 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
Paul Elliott7412eb42021-11-18 14:02:21 +0000309 }
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (pwd == NULL && pwdlen != 0) {
312 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
313 }
314
315 if (salt == NULL && saltlen != 0) {
316 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
317 }
318
319 use_password = (pwd && pwdlen != 0);
320 use_salt = (salt && saltlen != 0);
321
322 md_info = mbedtls_md_info_from_type(md_type);
323 if (md_info == NULL) {
324 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
325 }
326
327 mbedtls_md_init(&md_ctx);
328
329 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
330 return ret;
331 }
332 hlen = mbedtls_md_get_size(md_info);
333
334 if (hlen <= 32) {
335 v = 64;
336 } else {
337 v = 128;
338 }
339
340 memset(diversifier, (unsigned char) id, v);
341
342 if (use_salt != 0) {
343 pkcs12_fill_buffer(salt_block, v, salt, saltlen);
344 }
345
346 if (use_password != 0) {
347 pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
Paul Elliott7412eb42021-11-18 14:02:21 +0000348 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200349
350 p = data;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 while (datalen > 0) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200352 // Calculate hash( diversifier || salt_block || pwd_block )
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200354 goto exit;
Paul Elliott7412eb42021-11-18 14:02:21 +0000355 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200358 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200359 }
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 if (use_salt != 0) {
362 if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
363 goto exit;
Paul Elliott7412eb42021-11-18 14:02:21 +0000364 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200365 }
366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 if (use_password != 0) {
368 if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
369 goto exit;
370 }
371 }
372
373 if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
374 goto exit;
375 }
376
377 // Perform remaining ( iterations - 1 ) recursive hash calculations
378 for (i = 1; i < (size_t) iterations; i++) {
379 if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) != 0) {
380 goto exit;
381 }
382 }
383
384 use_len = (datalen > hlen) ? hlen : datalen;
385 memcpy(p, hash_output, use_len);
386 datalen -= use_len;
387 p += use_len;
388
389 if (datalen == 0) {
390 break;
391 }
392
393 // Concatenating copies of hash_output into hash_block (B)
394 pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
395
396 // B += 1
397 for (i = v; i > 0; i--) {
398 if (++hash_block[i - 1] != 0) {
399 break;
400 }
401 }
402
403 if (use_salt != 0) {
404 // salt_block += B
405 c = 0;
406 for (i = v; i > 0; i--) {
407 j = salt_block[i - 1] + hash_block[i - 1] + c;
408 c = MBEDTLS_BYTE_1(j);
409 salt_block[i - 1] = MBEDTLS_BYTE_0(j);
410 }
411 }
412
413 if (use_password != 0) {
Paul Elliott7412eb42021-11-18 14:02:21 +0000414 // pwd_block += B
415 c = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 for (i = v; i > 0; i--) {
Paul Elliott7412eb42021-11-18 14:02:21 +0000417 j = pwd_block[i - 1] + hash_block[i - 1] + c;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 c = MBEDTLS_BYTE_1(j);
419 pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
Paul Elliott7412eb42021-11-18 14:02:21 +0000420 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200421 }
422 }
423
Paul Bakkerbd552442013-07-03 14:44:40 +0200424 ret = 0;
425
426exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
428 mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
429 mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
430 mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
Paul Bakker91c301a2014-06-18 13:59:38 +0200431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_md_free(&md_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200435}
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#endif /* MBEDTLS_PKCS12_C */