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