blob: 76aad9e96c59eb77a07f56022a00b64c09128057 [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;
Waleed Elmelegyf9193932023-09-12 14:05:10 +010052 unsigned char **p = &params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020053 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
Waleed Elmelegyf9193932023-09-12 14:05:10 +010067 if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
69 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070
Waleed Elmelegyf9193932023-09-12 14:05:10 +010071 salt->p = *p;
72 *p += salt->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020073
Waleed Elmelegyf9193932023-09-12 14:05:10 +010074 if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
76 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020077
Waleed Elmelegyf9193932023-09-12 14:05:10 +010078 if (*p != end) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 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
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100175#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
176int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
177 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
178 const unsigned char *pwd, size_t pwdlen,
179 const unsigned char *data, size_t len,
180 unsigned char *output, size_t output_size,
181 size_t *output_len);
182#endif
183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
185 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
186 const unsigned char *pwd, size_t pwdlen,
187 const unsigned char *data, size_t len,
188 unsigned char *output)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200189{
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100190 size_t output_len = 0;
191
192 /* We assume caller of the function is providing a big enough output buffer
193 * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
194 * for the output size actually being correct.
195 */
196 return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type,
197 pwd, pwdlen, data, len, output, SIZE_MAX,
198 &output_len);
199}
200
201int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
202 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
203 const unsigned char *pwd, size_t pwdlen,
204 const unsigned char *data, size_t len,
205 unsigned char *output, size_t output_size,
206 size_t *output_len)
207{
Paul Bakker38b50d72013-06-24 19:33:27 +0200208 int ret, keylen = 0;
209 unsigned char key[32];
210 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 const mbedtls_cipher_info_t *cipher_info;
212 mbedtls_cipher_context_t cipher_ctx;
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100213 size_t finish_olen = 0;
214 unsigned int padlen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (pwd == NULL && pwdlen != 0) {
217 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
218 }
Paul Elliottfe724fe2021-11-11 19:00:38 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
221 if (cipher_info == NULL) {
222 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
223 }
Paul Bakker38b50d72013-06-24 19:33:27 +0200224
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200225 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200226
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100227 if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
228 if (output_size < len) {
229 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
230 }
231 }
232
233 if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) {
234 padlen = cipher_info->block_size - (len % cipher_info->block_size);
235 if (output_size < (len + padlen)) {
236 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
237 }
238 }
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
241 key, keylen,
242 iv, cipher_info->iv_size)) != 0) {
243 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200244 }
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200249 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200250 }
251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if ((ret =
253 mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
254 (mbedtls_operation_t) mode)) != 0) {
255 goto exit;
256 }
257
Waleed Elmelegy38a89ad2023-09-04 15:11:22 +0100258#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Dave Rodgman2af05c82023-10-13 14:40:14 +0100259 {
260 /* PKCS12 uses CBC with PKCS7 padding */
261 mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
Waleed Elmelegy38a89ad2023-09-04 15:11:22 +0100262#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Dave Rodgman2af05c82023-10-13 14:40:14 +0100263 /* For historical reasons, when decrypting, this function works when
264 * decrypting even when support for PKCS7 padding is disabled. In this
265 * case, it ignores the padding, and so will never report a
266 * password mismatch.
267 */
268 if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
269 padding = MBEDTLS_PADDING_NONE;
270 }
Waleed Elmelegy38a89ad2023-09-04 15:11:22 +0100271#endif
Dave Rodgman2af05c82023-10-13 14:40:14 +0100272 if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
273 goto exit;
274 }
Waleed Elmelegy38a89ad2023-09-04 15:11:22 +0100275 }
276#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) {
279 goto exit;
280 }
281
282 if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
283 goto exit;
284 }
285
286 if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100287 output, output_len)) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 goto exit;
289 }
290
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100291 if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + (*output_len), &finish_olen)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200294
Waleed Elmelegy6060cf12023-09-06 15:48:08 +0100295 *output_len += finish_olen;
296
Paul Bakkerbd552442013-07-03 14:44:40 +0200297exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 mbedtls_platform_zeroize(key, sizeof(key));
299 mbedtls_platform_zeroize(iv, sizeof(iv));
300 mbedtls_cipher_free(&cipher_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return ret;
Paul Bakker531e2942013-06-24 19:23:12 +0200303}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200304
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100305#endif /* MBEDTLS_ASN1_PARSE_C */
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
308 const unsigned char *filler, size_t fill_len)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200309{
310 unsigned char *p = data;
311 size_t use_len;
312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (filler != NULL && fill_len != 0) {
314 while (data_len > 0) {
315 use_len = (data_len > fill_len) ? fill_len : data_len;
316 memcpy(p, filler, use_len);
Paul Elliottfe724fe2021-11-11 19:00:38 +0000317 p += use_len;
318 data_len -= use_len;
319 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 } else {
Paul Elliott8d7eef42021-12-02 17:51:34 +0000321 /* If either of the above are not true then clearly there is nothing
322 * that this function can do. The function should *not* be called
323 * under either of those circumstances, as you could end up with an
324 * incorrect output but for safety's sake, leaving the check in as
325 * otherwise we could end up with memory corruption.*/
326 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200327}
328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
330 const unsigned char *pwd, size_t pwdlen,
331 const unsigned char *salt, size_t saltlen,
332 mbedtls_md_type_t md_type, int id, int iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200333{
Janos Follath24eed8d2019-11-22 13:21:35 +0000334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200335 unsigned int j;
336
337 unsigned char diversifier[128];
338 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200340 unsigned char *p;
341 unsigned char c;
Paul Elliott7412eb42021-11-18 14:02:21 +0000342 int use_password = 0;
343 int use_salt = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200344
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200345 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 const mbedtls_md_info_t *md_info;
348 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200349
350 // This version only allows max of 64 bytes of password or salt
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
352 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
Paul Elliott7412eb42021-11-18 14:02:21 +0000353 }
354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 if (pwd == NULL && pwdlen != 0) {
356 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
357 }
358
359 if (salt == NULL && saltlen != 0) {
360 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
361 }
362
363 use_password = (pwd && pwdlen != 0);
364 use_salt = (salt && saltlen != 0);
365
366 md_info = mbedtls_md_info_from_type(md_type);
367 if (md_info == NULL) {
368 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
369 }
370
371 mbedtls_md_init(&md_ctx);
372
373 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
374 return ret;
375 }
376 hlen = mbedtls_md_get_size(md_info);
377
378 if (hlen <= 32) {
379 v = 64;
380 } else {
381 v = 128;
382 }
383
384 memset(diversifier, (unsigned char) id, v);
385
386 if (use_salt != 0) {
387 pkcs12_fill_buffer(salt_block, v, salt, saltlen);
388 }
389
390 if (use_password != 0) {
391 pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
Paul Elliott7412eb42021-11-18 14:02:21 +0000392 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200393
394 p = data;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 while (datalen > 0) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200396 // Calculate hash( diversifier || salt_block || pwd_block )
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200398 goto exit;
Paul Elliott7412eb42021-11-18 14:02:21 +0000399 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200402 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200403 }
404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 if (use_salt != 0) {
406 if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
407 goto exit;
Paul Elliott7412eb42021-11-18 14:02:21 +0000408 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200409 }
410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 if (use_password != 0) {
412 if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
413 goto exit;
414 }
415 }
416
417 if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
418 goto exit;
419 }
420
421 // Perform remaining ( iterations - 1 ) recursive hash calculations
422 for (i = 1; i < (size_t) iterations; i++) {
423 if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) != 0) {
424 goto exit;
425 }
426 }
427
428 use_len = (datalen > hlen) ? hlen : datalen;
429 memcpy(p, hash_output, use_len);
430 datalen -= use_len;
431 p += use_len;
432
433 if (datalen == 0) {
434 break;
435 }
436
437 // Concatenating copies of hash_output into hash_block (B)
438 pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
439
440 // B += 1
441 for (i = v; i > 0; i--) {
442 if (++hash_block[i - 1] != 0) {
443 break;
444 }
445 }
446
447 if (use_salt != 0) {
448 // salt_block += B
449 c = 0;
450 for (i = v; i > 0; i--) {
451 j = salt_block[i - 1] + hash_block[i - 1] + c;
452 c = MBEDTLS_BYTE_1(j);
453 salt_block[i - 1] = MBEDTLS_BYTE_0(j);
454 }
455 }
456
457 if (use_password != 0) {
Paul Elliott7412eb42021-11-18 14:02:21 +0000458 // pwd_block += B
459 c = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 for (i = v; i > 0; i--) {
Paul Elliott7412eb42021-11-18 14:02:21 +0000461 j = pwd_block[i - 1] + hash_block[i - 1] + c;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 c = MBEDTLS_BYTE_1(j);
463 pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
Paul Elliott7412eb42021-11-18 14:02:21 +0000464 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200465 }
466 }
467
Paul Bakkerbd552442013-07-03 14:44:40 +0200468 ret = 0;
469
470exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
472 mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
473 mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
474 mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
Paul Bakker91c301a2014-06-18 13:59:38 +0200475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 mbedtls_md_free(&md_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_PKCS12_C */