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