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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * The PKCS #12 Personal Information Exchange Syntax Standard v1.1 |
| 9 | * |
| 10 | * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf |
| 11 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn |
| 12 | */ |
| 13 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 14 | #include "common.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | #if defined(MBEDTLS_PKCS12_C) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/pkcs12.h" |
| 19 | #include "mbedtls/asn1.h" |
| 20 | #include "mbedtls/cipher.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 21 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 22 | #include "mbedtls/error.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 23 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_DES_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/des.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 30 | #include "psa_util_internal.h" |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 31 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 33 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params, |
| 35 | mbedtls_asn1_buf *salt, int *iterations) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 36 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 37 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Waleed Elmelegy | 57d09b7 | 2023-09-12 14:05:10 +0100 | [diff] [blame] | 38 | unsigned char **p = ¶ms->p; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 39 | const unsigned char *end = params->p + params->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * pkcs-12PbeParams ::= SEQUENCE { |
| 43 | * salt OCTET STRING, |
| 44 | * iterations INTEGER |
| 45 | * } |
| 46 | * |
| 47 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 49 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 50 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 51 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 52 | |
Waleed Elmelegy | 57d09b7 | 2023-09-12 14:05:10 +0100 | [diff] [blame] | 53 | if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret); |
| 55 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 56 | |
Waleed Elmelegy | 57d09b7 | 2023-09-12 14:05:10 +0100 | [diff] [blame] | 57 | salt->p = *p; |
| 58 | *p += salt->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 59 | |
Waleed Elmelegy | 57d09b7 | 2023-09-12 14:05:10 +0100 | [diff] [blame] | 60 | if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret); |
| 62 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 63 | |
Waleed Elmelegy | 57d09b7 | 2023-09-12 14:05:10 +0100 | [diff] [blame] | 64 | if (*p != end) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, |
| 66 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 67 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | return 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 72 | #define PKCS12_MAX_PWDLEN 128 |
| 73 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type, |
| 75 | const unsigned char *pwd, size_t pwdlen, |
| 76 | unsigned char *key, size_t keylen, |
| 77 | unsigned char *iv, size_t ivlen) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 78 | { |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 79 | int ret, iterations = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | mbedtls_asn1_buf salt; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 81 | size_t i; |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 82 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
| 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | if (pwdlen > PKCS12_MAX_PWDLEN) { |
| 85 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
| 86 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 87 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | memset(&salt, 0, sizeof(mbedtls_asn1_buf)); |
| 89 | memset(&unipwd, 0, sizeof(unipwd)); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 90 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt, |
| 92 | &iterations)) != 0) { |
| 93 | return ret; |
| 94 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 95 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | for (i = 0; i < pwdlen; i++) { |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 97 | unipwd[i * 2 + 1] = pwd[i]; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2, |
| 101 | salt.p, salt.len, md_type, |
| 102 | MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) { |
| 103 | return ret; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 104 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | |
| 106 | if (iv == NULL || ivlen == 0) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2, |
| 111 | salt.p, salt.len, md_type, |
| 112 | MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) { |
| 113 | return ret; |
| 114 | } |
| 115 | return 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 118 | #undef PKCS12_MAX_PWDLEN |
| 119 | |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 120 | #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7) |
| 121 | int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode, |
| 122 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
| 123 | const unsigned char *pwd, size_t pwdlen, |
| 124 | const unsigned char *data, size_t len, |
| 125 | unsigned char *output, size_t output_size, |
| 126 | size_t *output_len); |
| 127 | #endif |
| 128 | |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 129 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode, |
| 131 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
| 132 | const unsigned char *pwd, size_t pwdlen, |
| 133 | const unsigned char *data, size_t len, |
| 134 | unsigned char *output) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 135 | { |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 136 | size_t output_len = 0; |
| 137 | |
| 138 | /* We assume caller of the function is providing a big enough output buffer |
| 139 | * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees |
| 140 | * for the output size actually being correct. |
| 141 | */ |
| 142 | return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type, |
| 143 | pwd, pwdlen, data, len, output, SIZE_MAX, |
| 144 | &output_len); |
| 145 | } |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 146 | #endif |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 147 | |
| 148 | int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode, |
| 149 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
| 150 | const unsigned char *pwd, size_t pwdlen, |
| 151 | const unsigned char *data, size_t len, |
| 152 | unsigned char *output, size_t output_size, |
| 153 | size_t *output_len) |
| 154 | { |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 155 | int ret, keylen = 0; |
| 156 | unsigned char key[32]; |
| 157 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | const mbedtls_cipher_info_t *cipher_info; |
| 159 | mbedtls_cipher_context_t cipher_ctx; |
Valerio Setti | f484884 | 2023-10-05 06:24:06 +0200 | [diff] [blame] | 160 | size_t iv_len = 0; |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 161 | size_t finish_olen = 0; |
| 162 | unsigned int padlen = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | if (pwd == NULL && pwdlen != 0) { |
| 165 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
| 166 | } |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 169 | if (cipher_info == NULL) { |
| 170 | return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE; |
| 171 | } |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 172 | |
Dave Rodgman | e59b9d4 | 2023-06-24 16:53:13 +0100 | [diff] [blame] | 173 | keylen = (int) mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 174 | |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 175 | if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) { |
| 176 | if (output_size < len) { |
| 177 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) { |
| 182 | padlen = cipher_info->block_size - (len % cipher_info->block_size); |
| 183 | if (output_size < (len + padlen)) { |
| 184 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 185 | } |
| 186 | } |
| 187 | |
Valerio Setti | f484884 | 2023-10-05 06:24:06 +0200 | [diff] [blame] | 188 | iv_len = mbedtls_cipher_info_get_iv_size(cipher_info); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen, |
| 190 | key, keylen, |
Valerio Setti | f484884 | 2023-10-05 06:24:06 +0200 | [diff] [blame] | 191 | iv, iv_len)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | return ret; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | mbedtls_cipher_init(&cipher_ctx); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 198 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Valerio Setti | f484884 | 2023-10-05 06:24:06 +0200 | [diff] [blame] | 201 | if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen, |
| 202 | (mbedtls_operation_t) mode)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | goto exit; |
| 204 | } |
| 205 | |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 206 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 207 | { |
| 208 | /* PKCS12 uses CBC with PKCS7 padding */ |
| 209 | mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 210 | #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7) |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 211 | /* For historical reasons, when decrypting, this function works when |
| 212 | * decrypting even when support for PKCS7 padding is disabled. In this |
| 213 | * case, it ignores the padding, and so will never report a |
| 214 | * password mismatch. |
| 215 | */ |
| 216 | if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) { |
| 217 | padding = MBEDTLS_PADDING_NONE; |
| 218 | } |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 219 | #endif |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 220 | if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) { |
| 221 | goto exit; |
| 222 | } |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 223 | } |
| 224 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 225 | |
Valerio Setti | f484884 | 2023-10-05 06:24:06 +0200 | [diff] [blame] | 226 | ret = mbedtls_cipher_crypt(&cipher_ctx, iv, iv_len, data, len, output, &finish_olen); |
| 227 | if (ret == MBEDTLS_ERR_CIPHER_INVALID_PADDING) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 230 | |
Waleed Elmelegy | e1cb35b | 2023-09-06 15:48:08 +0100 | [diff] [blame] | 231 | *output_len += finish_olen; |
| 232 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 233 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | mbedtls_platform_zeroize(key, sizeof(key)); |
| 235 | mbedtls_platform_zeroize(iv, sizeof(iv)); |
| 236 | mbedtls_cipher_free(&cipher_ctx); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | return ret; |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 239 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 240 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 241 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
| 242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | static void pkcs12_fill_buffer(unsigned char *data, size_t data_len, |
| 244 | const unsigned char *filler, size_t fill_len) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 245 | { |
| 246 | unsigned char *p = data; |
| 247 | size_t use_len; |
| 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | if (filler != NULL && fill_len != 0) { |
| 250 | while (data_len > 0) { |
| 251 | use_len = (data_len > fill_len) ? fill_len : data_len; |
| 252 | memcpy(p, filler, use_len); |
Paul Elliott | 853c0da | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 253 | p += use_len; |
| 254 | data_len -= use_len; |
| 255 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | } else { |
Paul Elliott | 7298bef | 2021-12-02 17:51:34 +0000 | [diff] [blame] | 257 | /* If either of the above are not true then clearly there is nothing |
| 258 | * that this function can do. The function should *not* be called |
| 259 | * under either of those circumstances, as you could end up with an |
| 260 | * incorrect output but for safety's sake, leaving the check in as |
| 261 | * otherwise we could end up with memory corruption.*/ |
| 262 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | static int calculate_hashes(mbedtls_md_type_t md_type, int iterations, |
| 267 | unsigned char *diversifier, unsigned char *salt_block, |
| 268 | unsigned char *pwd_block, unsigned char *hash_output, int use_salt, |
| 269 | int use_password, size_t hlen, size_t v) |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 270 | { |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 271 | int ret = -1; |
| 272 | size_t i; |
| 273 | const mbedtls_md_info_t *md_info; |
| 274 | mbedtls_md_context_t md_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | md_info = mbedtls_md_info_from_type(md_type); |
| 276 | if (md_info == NULL) { |
| 277 | return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE; |
| 278 | } |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | mbedtls_md_init(&md_ctx); |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) { |
| 283 | return ret; |
| 284 | } |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 285 | // Calculate hash( diversifier || salt_block || pwd_block ) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | if ((ret = mbedtls_md_starts(&md_ctx)) != 0) { |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 287 | goto exit; |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) { |
| 291 | goto exit; |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | if (use_salt != 0) { |
| 295 | if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) { |
| 296 | goto exit; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (use_password != 0) { |
| 301 | if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) { |
| 302 | goto exit; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) { |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 307 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | } |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 309 | |
| 310 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | for (i = 1; i < (size_t) iterations; i++) { |
| 312 | if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) |
| 313 | != 0) { |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 314 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | } |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | mbedtls_md_free(&md_ctx); |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 320 | return ret; |
Andrzej Kurek | 7bd12c5 | 2022-08-24 10:47:10 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen, |
| 325 | const unsigned char *pwd, size_t pwdlen, |
| 326 | const unsigned char *salt, size_t saltlen, |
| 327 | mbedtls_md_type_t md_type, int id, int iterations) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 328 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 329 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 330 | unsigned int j; |
| 331 | |
| 332 | unsigned char diversifier[128]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | unsigned char salt_block[128], pwd_block[128], hash_block[128] = { 0 }; |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 334 | unsigned char hash_output[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 335 | unsigned char *p; |
| 336 | unsigned char c; |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 337 | int use_password = 0; |
| 338 | int use_salt = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 339 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 340 | size_t hlen, use_len, v, i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 341 | |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 342 | // This version only allows max of 64 bytes of password or salt |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | if (datalen > 128 || pwdlen > 64 || saltlen > 64) { |
| 344 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | if (pwd == NULL && pwdlen != 0) { |
| 348 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
| 349 | } |
| 350 | |
| 351 | if (salt == NULL && saltlen != 0) { |
| 352 | return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA; |
| 353 | } |
| 354 | |
| 355 | use_password = (pwd && pwdlen != 0); |
| 356 | use_salt = (salt && saltlen != 0); |
| 357 | |
Manuel Pégourié-Gonnard | 9b41eb8 | 2023-03-28 11:14:24 +0200 | [diff] [blame] | 358 | hlen = mbedtls_md_get_size_from_type(md_type); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | |
| 360 | if (hlen <= 32) { |
| 361 | v = 64; |
| 362 | } else { |
| 363 | v = 128; |
| 364 | } |
| 365 | |
| 366 | memset(diversifier, (unsigned char) id, v); |
| 367 | |
| 368 | if (use_salt != 0) { |
| 369 | pkcs12_fill_buffer(salt_block, v, salt, saltlen); |
| 370 | } |
| 371 | |
| 372 | if (use_password != 0) { |
| 373 | pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen); |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 374 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 375 | |
| 376 | p = data; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | while (datalen > 0) { |
| 378 | if (calculate_hashes(md_type, iterations, diversifier, salt_block, |
| 379 | pwd_block, hash_output, use_salt, use_password, hlen, |
| 380 | v) != 0) { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 381 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | use_len = (datalen > hlen) ? hlen : datalen; |
| 385 | memcpy(p, hash_output, use_len); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 386 | datalen -= use_len; |
| 387 | p += use_len; |
| 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | if (datalen == 0) { |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 390 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 392 | |
| 393 | // Concatenating copies of hash_output into hash_block (B) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | pkcs12_fill_buffer(hash_block, v, hash_output, hlen); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 395 | |
| 396 | // B += 1 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | for (i = v; i > 0; i--) { |
| 398 | if (++hash_block[i - 1] != 0) { |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 399 | break; |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 400 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 414 | // pwd_block += B |
| 415 | c = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | for (i = v; i > 0; i--) { |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 417 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | c = MBEDTLS_BYTE_1(j); |
| 419 | pwd_block[i - 1] = MBEDTLS_BYTE_0(j); |
Paul Elliott | 4086bdb | 2021-11-18 14:02:21 +0000 | [diff] [blame] | 420 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 424 | ret = 0; |
| 425 | |
| 426 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | 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 Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | return ret; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | #endif /* MBEDTLS_PKCS12_C */ |