blob: dd3a24037a01039207f78705b445197c279f782b [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_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/des.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020040#endif
41
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020042#include "psa_util_internal.h"
Andrzej Kurek7bd12c52022-08-24 10:47:10 -040043
Hanno Becker8a89f9f2018-10-12 10:46:32 +010044#if defined(MBEDTLS_ASN1_PARSE_C)
45
Gilles Peskine449bd832023-01-11 14:50:10 +010046static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
47 mbedtls_asn1_buf *salt, int *iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020048{
Janos Follath24eed8d2019-11-22 13:21:35 +000049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Waleed Elmelegy57d09b72023-09-12 14:05:10 +010050 unsigned char **p = &params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020051 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020052
53 /*
54 * pkcs-12PbeParams ::= SEQUENCE {
55 * salt OCTET STRING,
56 * iterations INTEGER
57 * }
58 *
59 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
61 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
62 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
63 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020064
Waleed Elmelegy57d09b72023-09-12 14:05:10 +010065 if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010066 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
67 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020068
Waleed Elmelegy57d09b72023-09-12 14:05:10 +010069 salt->p = *p;
70 *p += salt->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020071
Waleed Elmelegy57d09b72023-09-12 14:05:10 +010072 if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
74 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020075
Waleed Elmelegy57d09b72023-09-12 14:05:10 +010076 if (*p != end) {
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
78 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
79 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020082}
83
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020084#define PKCS12_MAX_PWDLEN 128
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
87 const unsigned char *pwd, size_t pwdlen,
88 unsigned char *key, size_t keylen,
89 unsigned char *iv, size_t ivlen)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020090{
Nicholas Wilson409401c2016-04-13 11:48:25 +010091 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020093 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020094 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (pwdlen > PKCS12_MAX_PWDLEN) {
97 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
98 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 memset(&salt, 0, sizeof(mbedtls_asn1_buf));
101 memset(&unipwd, 0, sizeof(unipwd));
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt,
104 &iterations)) != 0) {
105 return ret;
106 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 for (i = 0; i < pwdlen; i++) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200109 unipwd[i * 2 + 1] = pwd[i];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2,
113 salt.p, salt.len, md_type,
114 MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
115 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200116 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100117
118 if (iv == NULL || ivlen == 0) {
119 return 0;
120 }
121
122 if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2,
123 salt.p, salt.len, md_type,
124 MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
125 return ret;
126 }
127 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200128}
129
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200130#undef PKCS12_MAX_PWDLEN
131
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100132#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
133int mbedtls_pkcs12_pbe_ext(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, size_t output_size,
138 size_t *output_len);
139#endif
140
Waleed Elmelegyd5278962023-09-12 14:42:49 +0100141#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
143 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
144 const unsigned char *pwd, size_t pwdlen,
145 const unsigned char *data, size_t len,
146 unsigned char *output)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200147{
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100148 size_t output_len = 0;
149
150 /* We assume caller of the function is providing a big enough output buffer
151 * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
152 * for the output size actually being correct.
153 */
154 return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type,
155 pwd, pwdlen, data, len, output, SIZE_MAX,
156 &output_len);
157}
Waleed Elmelegyd5278962023-09-12 14:42:49 +0100158#endif
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100159
160int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
161 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
162 const unsigned char *pwd, size_t pwdlen,
163 const unsigned char *data, size_t len,
164 unsigned char *output, size_t output_size,
165 size_t *output_len)
166{
Paul Bakker38b50d72013-06-24 19:33:27 +0200167 int ret, keylen = 0;
168 unsigned char key[32];
169 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 const mbedtls_cipher_info_t *cipher_info;
171 mbedtls_cipher_context_t cipher_ctx;
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100172 size_t finish_olen = 0;
173 unsigned int padlen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (pwd == NULL && pwdlen != 0) {
176 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
177 }
Paul Elliott853c0da2021-11-11 19:00:38 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
180 if (cipher_info == NULL) {
181 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
182 }
Paul Bakker38b50d72013-06-24 19:33:27 +0200183
Dave Rodgmane59b9d42023-06-24 16:53:13 +0100184 keylen = (int) mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200185
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100186 if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
187 if (output_size < len) {
188 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
189 }
190 }
191
192 if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) {
193 padlen = cipher_info->block_size - (len % cipher_info->block_size);
194 if (output_size < (len + padlen)) {
195 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
196 }
197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
200 key, keylen,
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100201 iv, mbedtls_cipher_info_get_iv_size(cipher_info))) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100211 if ((ret =
212 mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
213 (mbedtls_operation_t) mode)) != 0) {
214 goto exit;
215 }
216
Waleed Elmelegy255db802023-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
Dave Rodgman3b46b772023-06-24 13:25:06 +0100236 if ((ret =
237 mbedtls_cipher_set_iv(&cipher_ctx, iv,
238 mbedtls_cipher_info_get_iv_size(cipher_info))) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 goto exit;
240 }
241
242 if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
243 goto exit;
244 }
245
246 if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100247 output, output_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 goto exit;
249 }
250
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100251 if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + (*output_len), &finish_olen)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200254
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +0100255 *output_len += finish_olen;
256
Paul Bakkerbd552442013-07-03 14:44:40 +0200257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_platform_zeroize(key, sizeof(key));
259 mbedtls_platform_zeroize(iv, sizeof(iv));
260 mbedtls_cipher_free(&cipher_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return ret;
Paul Bakker531e2942013-06-24 19:23:12 +0200263}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200264
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100265#endif /* MBEDTLS_ASN1_PARSE_C */
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
268 const unsigned char *filler, size_t fill_len)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200269{
270 unsigned char *p = data;
271 size_t use_len;
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (filler != NULL && fill_len != 0) {
274 while (data_len > 0) {
275 use_len = (data_len > fill_len) ? fill_len : data_len;
276 memcpy(p, filler, use_len);
Paul Elliott853c0da2021-11-11 19:00:38 +0000277 p += use_len;
278 data_len -= use_len;
279 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 } else {
Paul Elliott7298bef2021-12-02 17:51:34 +0000281 /* If either of the above are not true then clearly there is nothing
282 * that this function can do. The function should *not* be called
283 * under either of those circumstances, as you could end up with an
284 * incorrect output but for safety's sake, leaving the check in as
285 * otherwise we could end up with memory corruption.*/
286 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200287}
288
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290static int calculate_hashes(mbedtls_md_type_t md_type, int iterations,
291 unsigned char *diversifier, unsigned char *salt_block,
292 unsigned char *pwd_block, unsigned char *hash_output, int use_salt,
293 int use_password, size_t hlen, size_t v)
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400294{
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400295 int ret = -1;
296 size_t i;
297 const mbedtls_md_info_t *md_info;
298 mbedtls_md_context_t md_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 md_info = mbedtls_md_info_from_type(md_type);
300 if (md_info == NULL) {
301 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
302 }
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_md_init(&md_ctx);
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
307 return ret;
308 }
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400309 // Calculate hash( diversifier || salt_block || pwd_block )
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400311 goto exit;
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400312 }
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
315 goto exit;
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (use_salt != 0) {
319 if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
320 goto exit;
321 }
322 }
323
324 if (use_password != 0) {
325 if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
326 goto exit;
327 }
328 }
329
330 if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400331 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400333
334 // Perform remaining ( iterations - 1 ) recursive hash calculations
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 for (i = 1; i < (size_t) iterations; i++) {
336 if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output))
337 != 0) {
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400340 }
341
342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_md_free(&md_ctx);
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400344 return ret;
Andrzej Kurek7bd12c52022-08-24 10:47:10 -0400345}
346
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
349 const unsigned char *pwd, size_t pwdlen,
350 const unsigned char *salt, size_t saltlen,
351 mbedtls_md_type_t md_type, int id, int iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200352{
Janos Follath24eed8d2019-11-22 13:21:35 +0000353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200354 unsigned int j;
355
356 unsigned char diversifier[128];
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 unsigned char salt_block[128], pwd_block[128], hash_block[128] = { 0 };
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +0200358 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200359 unsigned char *p;
360 unsigned char c;
Paul Elliott4086bdb2021-11-18 14:02:21 +0000361 int use_password = 0;
362 int use_salt = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200363
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200364 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200365
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200366 // This version only allows max of 64 bytes of password or salt
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
368 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
Paul Elliott4086bdb2021-11-18 14:02:21 +0000369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (pwd == NULL && pwdlen != 0) {
372 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
373 }
374
375 if (salt == NULL && saltlen != 0) {
376 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
377 }
378
379 use_password = (pwd && pwdlen != 0);
380 use_salt = (salt && saltlen != 0);
381
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200382 hlen = mbedtls_md_get_size_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100383
384 if (hlen <= 32) {
385 v = 64;
386 } else {
387 v = 128;
388 }
389
390 memset(diversifier, (unsigned char) id, v);
391
392 if (use_salt != 0) {
393 pkcs12_fill_buffer(salt_block, v, salt, saltlen);
394 }
395
396 if (use_password != 0) {
397 pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
Paul Elliott4086bdb2021-11-18 14:02:21 +0000398 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200399
400 p = data;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 while (datalen > 0) {
402 if (calculate_hashes(md_type, iterations, diversifier, salt_block,
403 pwd_block, hash_output, use_salt, use_password, hlen,
404 v) != 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200405 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 use_len = (datalen > hlen) ? hlen : datalen;
409 memcpy(p, hash_output, use_len);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200410 datalen -= use_len;
411 p += use_len;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (datalen == 0) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200414 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200416
417 // Concatenating copies of hash_output into hash_block (B)
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200419
420 // B += 1
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 for (i = v; i > 0; i--) {
422 if (++hash_block[i - 1] != 0) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200423 break;
Paul Elliott4086bdb2021-11-18 14:02:21 +0000424 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200425 }
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (use_salt != 0) {
428 // salt_block += B
429 c = 0;
430 for (i = v; i > 0; i--) {
431 j = salt_block[i - 1] + hash_block[i - 1] + c;
432 c = MBEDTLS_BYTE_1(j);
433 salt_block[i - 1] = MBEDTLS_BYTE_0(j);
434 }
435 }
436
437 if (use_password != 0) {
Paul Elliott4086bdb2021-11-18 14:02:21 +0000438 // pwd_block += B
439 c = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 for (i = v; i > 0; i--) {
Paul Elliott4086bdb2021-11-18 14:02:21 +0000441 j = pwd_block[i - 1] + hash_block[i - 1] + c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 c = MBEDTLS_BYTE_1(j);
443 pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
Paul Elliott4086bdb2021-11-18 14:02:21 +0000444 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200445 }
446 }
447
Paul Bakkerbd552442013-07-03 14:44:40 +0200448 ret = 0;
449
450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
452 mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
453 mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
454 mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
Paul Bakker91c301a2014-06-18 13:59:38 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200457}
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* MBEDTLS_PKCS12_C */