blob: 310381306217992e6011f5c56f25fe84462bb6ca [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048static 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +010063 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
64 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010067 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020068
69 salt->p = *p;
70 *p += salt->len;
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010073 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074
75 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +010076 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
77 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020078
79 return( 0 );
80}
81
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020082#define PKCS12_MAX_PWDLEN 128
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020085 const unsigned char *pwd, size_t pwdlen,
86 unsigned char *key, size_t keylen,
87 unsigned char *iv, size_t ivlen )
88{
Nicholas Wilson409401c2016-04-13 11:48:25 +010089 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020091 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020092 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
93
94 if( pwdlen > PKCS12_MAX_PWDLEN )
95 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker66d5d072014-06-17 16:39:18 +020098 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200100 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
101 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200102 return( ret );
103
Paul Bakker66d5d072014-06-17 16:39:18 +0200104 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200105 unipwd[i * 2 + 1] = pwd[i];
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200108 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200110 {
111 return( ret );
112 }
113
114 if( iv == NULL || ivlen == 0 )
115 return( 0 );
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200118 salt.p, salt.len, md_type,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200120 {
121 return( ret );
122 }
123 return( 0 );
124}
125
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200126#undef PKCS12_MAX_PWDLEN
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200129 const unsigned char *pwd, size_t pwdlen,
130 const unsigned char *data, size_t len,
131 unsigned char *output )
132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if !defined(MBEDTLS_ARC4_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200134 ((void) pbe_params);
135 ((void) mode);
136 ((void) pwd);
137 ((void) pwdlen);
138 ((void) data);
139 ((void) len);
140 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200142#else
Janos Follath24eed8d2019-11-22 13:21:35 +0000143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200144 unsigned char key[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_arc4_context ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200146 ((void) mode);
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_arc4_init( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
Paul Bakker38b50d72013-06-24 19:33:27 +0200151 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200152 key, 16, NULL, 0 ) ) != 0 )
153 {
154 return( ret );
155 }
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_arc4_setup( &ctx, key, 16 );
158 if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200159 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200160
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200161exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500162 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_arc4_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200164
165 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200167}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
170 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200171 const unsigned char *pwd, size_t pwdlen,
172 const unsigned char *data, size_t len,
173 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200174{
Paul Bakker38b50d72013-06-24 19:33:27 +0200175 int ret, keylen = 0;
176 unsigned char key[32];
177 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 const mbedtls_cipher_info_t *cipher_info;
179 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200180 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200181
Paul Elliottfe724fe2021-11-11 19:00:38 +0000182 if( pwd == NULL && pwdlen != 0 )
183 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
Paul Bakker38b50d72013-06-24 19:33:27 +0200186 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakker38b50d72013-06-24 19:33:27 +0200188
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200189 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200190
191 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
192 key, keylen,
193 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200194 {
195 return( ret );
196 }
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200199
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200200 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200201 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200204 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200207 goto exit;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200210 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
Paul Bakker38b50d72013-06-24 19:33:27 +0200213 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200214 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200215 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
219 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200220
Paul Bakkerbd552442013-07-03 14:44:40 +0200221exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500222 mbedtls_platform_zeroize( key, sizeof( key ) );
223 mbedtls_platform_zeroize( iv, sizeof( iv ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200225
226 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200227}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200228
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100229#endif /* MBEDTLS_ASN1_PARSE_C */
230
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200231static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
232 const unsigned char *filler, size_t fill_len )
233{
234 unsigned char *p = data;
235 size_t use_len;
236
Paul Elliottfe724fe2021-11-11 19:00:38 +0000237 if( filler != NULL && fill_len != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200238 {
Paul Elliottfe724fe2021-11-11 19:00:38 +0000239 while( data_len > 0 )
240 {
241 use_len = ( data_len > fill_len ) ? fill_len : data_len;
242 memcpy( p, filler, use_len );
243 p += use_len;
244 data_len -= use_len;
245 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200246 }
247}
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200250 const unsigned char *pwd, size_t pwdlen,
251 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_md_type_t md_type, int id, int iterations )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200253{
Janos Follath24eed8d2019-11-22 13:21:35 +0000254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200255 unsigned int j;
256
257 unsigned char diversifier[128];
258 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Paul Elliott7412eb42021-11-18 14:02:21 +0000259 unsigned char empty_string[2] = { 0, 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200261 unsigned char *p;
262 unsigned char c;
Paul Elliott7412eb42021-11-18 14:02:21 +0000263 int use_password = 0;
264 int use_salt = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200265
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200266 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 const mbedtls_md_info_t *md_info;
269 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200270
271 // This version only allows max of 64 bytes of password or salt
272 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200274
Paul Elliottfe724fe2021-11-11 19:00:38 +0000275 if( pwd == NULL && pwdlen != 0 )
276 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
277
Paul Elliott7412eb42021-11-18 14:02:21 +0000278 if( salt == NULL && saltlen != 0 )
279 return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
280
281 use_password = ( pwd && pwdlen != 0 );
282 use_salt = ( salt && saltlen != 0 );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200285 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_md_init( &md_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200291 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200293
294 if( hlen <= 32 )
295 v = 64;
296 else
297 v = 128;
298
299 memset( diversifier, (unsigned char) id, v );
300
Paul Elliott7412eb42021-11-18 14:02:21 +0000301 if( use_salt != 0 )
302 {
303 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
304 }
305
306 if( use_password != 0 )
307 {
308 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
309 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200310
311 p = data;
312 while( datalen > 0 )
313 {
314 // Calculate hash( diversifier || salt_block || pwd_block )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200316 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200319 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200320
Paul Elliott7412eb42021-11-18 14:02:21 +0000321 if( use_salt != 0 )
322 {
323 if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v )) != 0 )
324 goto exit;
325 }
326 else
327 {
328 if( ( ret = mbedtls_md_update( &md_ctx, empty_string,
329 sizeof( empty_string ) )) != 0 )
330 goto exit;
331 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200332
Paul Elliott7412eb42021-11-18 14:02:21 +0000333 if( use_password != 0)
334 {
335 if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v )) != 0 )
336 goto exit;
337 }
338 else
339 {
340 if( ( ret = mbedtls_md_update( &md_ctx, empty_string,
341 sizeof( empty_string ) )) != 0 )
342 goto exit;
343 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200346 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200347
348 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200349 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200352 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200353 }
354
355 use_len = ( datalen > hlen ) ? hlen : datalen;
356 memcpy( p, hash_output, use_len );
357 datalen -= use_len;
358 p += use_len;
359
360 if( datalen == 0 )
361 break;
362
363 // Concatenating copies of hash_output into hash_block (B)
364 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
365
366 // B += 1
367 for( i = v; i > 0; i-- )
368 if( ++hash_block[i - 1] != 0 )
369 break;
370
Paul Elliott7412eb42021-11-18 14:02:21 +0000371 if( use_salt != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200372 {
Paul Elliott7412eb42021-11-18 14:02:21 +0000373 // salt_block += B
374 c = 0;
375 for( i = v; i > 0; i-- )
376 {
377 j = salt_block[i - 1] + hash_block[i - 1] + c;
378 c = MBEDTLS_BYTE_1( j );
379 salt_block[i - 1] = MBEDTLS_BYTE_0( j );
380 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200381 }
382
Paul Elliott7412eb42021-11-18 14:02:21 +0000383 if( use_password != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200384 {
Paul Elliott7412eb42021-11-18 14:02:21 +0000385 // pwd_block += B
386 c = 0;
387 for( i = v; i > 0; i-- )
388 {
389 j = pwd_block[i - 1] + hash_block[i - 1] + c;
390 c = MBEDTLS_BYTE_1( j );
391 pwd_block[i - 1] = MBEDTLS_BYTE_0( j );
392 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200393 }
394 }
395
Paul Bakkerbd552442013-07-03 14:44:40 +0200396 ret = 0;
397
398exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500399 mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
400 mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
401 mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
402 mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
Paul Bakker91c301a2014-06-18 13:59:38 +0200403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200405
406 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200407}
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_PKCS12_C */