Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PKCS#12 Personal Information Exchange Syntax |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 26 | #include "common.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PKCS12_C) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/pkcs12.h" |
| 31 | #include "mbedtls/asn1.h" |
| 32 | #include "mbedtls/cipher.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 34 | #include "mbedtls/error.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 35 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_DES_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/des.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 42 | #include "hash_info.h" |
| 43 | #include "mbedtls/psa_util.h" |
| 44 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 45 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params, |
| 48 | mbedtls_asn1_buf *salt, int *iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 49 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 50 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 51 | unsigned char **p = ¶ms->p; |
| 52 | const unsigned char *end = params->p + params->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * pkcs-12PbeParams ::= SEQUENCE { |
| 56 | * salt OCTET STRING, |
| 57 | * iterations INTEGER |
| 58 | * } |
| 59 | * |
| 60 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 62 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 63 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 66 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 67 | |
| 68 | salt->p = *p; |
| 69 | *p += salt->len; |
| 70 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 72 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 73 | |
| 74 | if( *p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 75 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 76 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 77 | |
| 78 | return( 0 ); |
| 79 | } |
| 80 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 81 | #define PKCS12_MAX_PWDLEN 128 |
| 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 84 | const unsigned char *pwd, size_t pwdlen, |
| 85 | unsigned char *key, size_t keylen, |
| 86 | unsigned char *iv, size_t ivlen ) |
| 87 | { |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 88 | int ret, iterations = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | mbedtls_asn1_buf salt; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 90 | size_t i; |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 91 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
| 92 | |
| 93 | if( pwdlen > PKCS12_MAX_PWDLEN ) |
| 94 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | memset( &salt, 0, sizeof(mbedtls_asn1_buf) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 97 | memset( &unipwd, 0, sizeof(unipwd) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 98 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 99 | if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, |
| 100 | &iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 101 | return( ret ); |
| 102 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 103 | for( i = 0; i < pwdlen; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 104 | unipwd[i * 2 + 1] = pwd[i]; |
| 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 107 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 109 | { |
| 110 | return( ret ); |
| 111 | } |
| 112 | |
| 113 | if( iv == NULL || ivlen == 0 ) |
| 114 | return( 0 ); |
| 115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 117 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 119 | { |
| 120 | return( ret ); |
| 121 | } |
| 122 | return( 0 ); |
| 123 | } |
| 124 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 125 | #undef PKCS12_MAX_PWDLEN |
| 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode, |
| 128 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 129 | const unsigned char *pwd, size_t pwdlen, |
| 130 | const unsigned char *data, size_t len, |
| 131 | unsigned char *output ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 132 | { |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 133 | int ret, keylen = 0; |
| 134 | unsigned char key[32]; |
| 135 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | const mbedtls_cipher_info_t *cipher_info; |
| 137 | mbedtls_cipher_context_t cipher_ctx; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 138 | size_t olen = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 139 | |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 140 | if( pwd == NULL && pwdlen != 0 ) |
| 141 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 144 | if( cipher_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 147 | keylen = cipher_info->key_bitlen / 8; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 148 | |
| 149 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen, |
| 150 | key, keylen, |
| 151 | iv, cipher_info->iv_size ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 152 | { |
| 153 | return( ret ); |
| 154 | } |
| 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | mbedtls_cipher_init( &cipher_ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 158 | if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 159 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 162 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 165 | goto exit; |
| 166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 168 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 171 | output, &olen ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 172 | { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 173 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 ) |
| 177 | ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 178 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 179 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 180 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
| 181 | mbedtls_platform_zeroize( iv, sizeof( iv ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | mbedtls_cipher_free( &cipher_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 183 | |
| 184 | return( ret ); |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 185 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 186 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 187 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
| 188 | |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 189 | static void pkcs12_fill_buffer( unsigned char *data, size_t data_len, |
| 190 | const unsigned char *filler, size_t fill_len ) |
| 191 | { |
| 192 | unsigned char *p = data; |
| 193 | size_t use_len; |
| 194 | |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 195 | if( filler != NULL && fill_len != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 196 | { |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 197 | while( data_len > 0 ) |
| 198 | { |
| 199 | use_len = ( data_len > fill_len ) ? fill_len : data_len; |
| 200 | memcpy( p, filler, use_len ); |
| 201 | p += use_len; |
| 202 | data_len -= use_len; |
| 203 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 204 | } |
Paul Elliott | 7298bef | 2021-12-02 17:51:34 +0000 | [diff] [blame] | 205 | else |
| 206 | { |
| 207 | /* If either of the above are not true then clearly there is nothing |
| 208 | * that this function can do. The function should *not* be called |
| 209 | * under either of those circumstances, as you could end up with an |
| 210 | * incorrect output but for safety's sake, leaving the check in as |
| 211 | * otherwise we could end up with memory corruption.*/ |
| 212 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 215 | |
| 216 | static int calculate_hashes( mbedtls_md_type_t md_type, int iterations, |
| 217 | unsigned char *diversifier, unsigned char *salt_block, |
| 218 | unsigned char *pwd_block, unsigned char *hash_output, int use_salt, |
| 219 | int use_password, size_t hlen, size_t v ) |
| 220 | { |
| 221 | #if defined(MBEDTLS_MD_C) |
| 222 | int ret = -1; |
| 223 | size_t i; |
| 224 | const mbedtls_md_info_t *md_info; |
| 225 | mbedtls_md_context_t md_ctx; |
| 226 | md_info = mbedtls_md_info_from_type( md_type ); |
| 227 | if( md_info == NULL ) |
| 228 | return ( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 229 | |
| 230 | mbedtls_md_init( &md_ctx ); |
| 231 | |
| 232 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 233 | return ( ret ); |
| 234 | // Calculate hash( diversifier || salt_block || pwd_block ) |
| 235 | if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 ) |
| 236 | goto exit; |
| 237 | |
| 238 | if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 ) |
| 239 | goto exit; |
| 240 | |
| 241 | if( use_salt != 0 ) |
| 242 | { |
| 243 | if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 ) |
| 244 | goto exit; |
| 245 | } |
| 246 | |
| 247 | if( use_password != 0 ) |
| 248 | { |
| 249 | if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 ) |
| 250 | goto exit; |
| 251 | } |
| 252 | |
| 253 | if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 ) |
| 254 | goto exit; |
| 255 | |
| 256 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
| 257 | for( i = 1; i < (size_t) iterations; i++ ) |
| 258 | { |
| 259 | if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) |
| 260 | != 0 ) |
| 261 | goto exit; |
| 262 | } |
| 263 | |
| 264 | exit: |
| 265 | mbedtls_md_free( &md_ctx ); |
| 266 | return ret; |
| 267 | #else |
| 268 | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
| 269 | psa_algorithm_t alg = mbedtls_psa_translate_md( md_type ); |
| 270 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 271 | psa_status_t status_abort = PSA_ERROR_CORRUPTION_DETECTED; |
| 272 | size_t i, out_len, out_size = PSA_HASH_LENGTH( alg ); |
| 273 | |
| 274 | if( alg == PSA_ALG_NONE ) |
| 275 | return ( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 276 | |
| 277 | if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS ) |
| 278 | goto exit; |
| 279 | |
| 280 | // Calculate hash( diversifier || salt_block || pwd_block ) |
| 281 | if( ( status = psa_hash_update( &op, diversifier, v ) ) != PSA_SUCCESS ) |
| 282 | goto exit; |
| 283 | |
| 284 | if( use_salt != 0 ) |
| 285 | { |
| 286 | if( ( status = psa_hash_update( &op, salt_block, v ) ) != PSA_SUCCESS ) |
| 287 | goto exit; |
| 288 | } |
| 289 | |
| 290 | if( use_password != 0 ) |
| 291 | { |
| 292 | if( ( status = psa_hash_update( &op, pwd_block, v ) ) != PSA_SUCCESS ) |
| 293 | goto exit; |
| 294 | } |
| 295 | |
| 296 | if( ( status = psa_hash_finish( &op, hash_output, out_size, &out_len ) ) |
| 297 | != PSA_SUCCESS ) |
| 298 | goto exit; |
| 299 | |
| 300 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
| 301 | for( i = 1; i < (size_t) iterations; i++ ) |
| 302 | { |
| 303 | if( ( status = psa_hash_compute( alg, hash_output, hlen, hash_output, |
| 304 | out_size, &out_len ) ) != PSA_SUCCESS ) |
| 305 | goto exit; |
| 306 | } |
| 307 | |
| 308 | exit: |
| 309 | status_abort = psa_hash_abort( &op ); |
| 310 | if( status == PSA_SUCCESS ) |
| 311 | status = status_abort; |
| 312 | return ( mbedtls_md_error_from_psa( status ) ); |
| 313 | #endif /* !MBEDTLS_MD_C */ |
| 314 | } |
| 315 | |
| 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 318 | const unsigned char *pwd, size_t pwdlen, |
| 319 | const unsigned char *salt, size_t saltlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | mbedtls_md_type_t md_type, int id, int iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 321 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 322 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 323 | unsigned int j; |
| 324 | |
| 325 | unsigned char diversifier[128]; |
Leonid Rozenboim | a3008e7 | 2022-04-21 17:28:18 -0700 | [diff] [blame] | 326 | unsigned char salt_block[128], pwd_block[128], hash_block[128] = {0}; |
Przemek Stekiel | 40afdd2 | 2022-09-06 13:08:28 +0200 | [diff] [blame] | 327 | unsigned char hash_output[MBEDTLS_HASH_MAX_SIZE]; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 328 | unsigned char *p; |
| 329 | unsigned char c; |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 330 | int use_password = 0; |
| 331 | int use_salt = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 332 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 333 | size_t hlen, use_len, v, i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 334 | |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 335 | // This version only allows max of 64 bytes of password or salt |
| 336 | if( datalen > 128 || pwdlen > 64 || saltlen > 64 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 338 | |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 339 | if( pwd == NULL && pwdlen != 0 ) |
| 340 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 341 | |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 342 | if( salt == NULL && saltlen != 0 ) |
| 343 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 344 | |
| 345 | use_password = ( pwd && pwdlen != 0 ); |
| 346 | use_salt = ( salt && saltlen != 0 ); |
| 347 | |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 348 | hlen = mbedtls_hash_info_get_size( md_type ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 349 | |
| 350 | if( hlen <= 32 ) |
| 351 | v = 64; |
| 352 | else |
| 353 | v = 128; |
| 354 | |
| 355 | memset( diversifier, (unsigned char) id, v ); |
| 356 | |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 357 | if( use_salt != 0 ) |
| 358 | { |
| 359 | pkcs12_fill_buffer( salt_block, v, salt, saltlen ); |
| 360 | } |
| 361 | |
| 362 | if( use_password != 0 ) |
| 363 | { |
| 364 | pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen ); |
| 365 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 366 | |
| 367 | p = data; |
| 368 | while( datalen > 0 ) |
| 369 | { |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 370 | if( calculate_hashes( md_type, iterations, diversifier, salt_block, |
| 371 | pwd_block, hash_output, use_salt, use_password, hlen, |
| 372 | v ) != 0 ) |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 373 | { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 374 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | use_len = ( datalen > hlen ) ? hlen : datalen; |
| 378 | memcpy( p, hash_output, use_len ); |
| 379 | datalen -= use_len; |
| 380 | p += use_len; |
| 381 | |
| 382 | if( datalen == 0 ) |
| 383 | break; |
| 384 | |
| 385 | // Concatenating copies of hash_output into hash_block (B) |
| 386 | pkcs12_fill_buffer( hash_block, v, hash_output, hlen ); |
| 387 | |
| 388 | // B += 1 |
| 389 | for( i = v; i > 0; i-- ) |
| 390 | if( ++hash_block[i - 1] != 0 ) |
| 391 | break; |
| 392 | |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 393 | if( use_salt != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 394 | { |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 395 | // salt_block += B |
| 396 | c = 0; |
| 397 | for( i = v; i > 0; i-- ) |
| 398 | { |
| 399 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
| 400 | c = MBEDTLS_BYTE_1( j ); |
| 401 | salt_block[i - 1] = MBEDTLS_BYTE_0( j ); |
| 402 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 405 | if( use_password != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 406 | { |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 407 | // pwd_block += B |
| 408 | c = 0; |
| 409 | for( i = v; i > 0; i-- ) |
| 410 | { |
| 411 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
| 412 | c = MBEDTLS_BYTE_1( j ); |
| 413 | pwd_block[i - 1] = MBEDTLS_BYTE_0( j ); |
| 414 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 418 | ret = 0; |
| 419 | |
| 420 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 421 | mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) ); |
| 422 | mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) ); |
| 423 | mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) ); |
| 424 | mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) ); |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 425 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 426 | return( ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | #endif /* MBEDTLS_PKCS12_C */ |