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