Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key layer for parsing key files and structures |
| 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 | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_PK_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/pk.h" |
| 13 | #include "mbedtls/asn1.h" |
| 14 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 15 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 5fcbe4c | 2023-07-06 13:02:51 +0200 | [diff] [blame] | 16 | #include "mbedtls/platform.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 17 | #include "mbedtls/error.h" |
Tomi Fontanilles | 851d8df | 2023-12-19 15:44:52 +0200 | [diff] [blame] | 18 | #include "mbedtls/ecp.h" |
Valerio Setti | 3cc486a | 2023-11-30 08:09:47 +0100 | [diff] [blame] | 19 | #include "pk_internal.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 20 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
Manuel Pégourié-Gonnard | 5fcbe4c | 2023-07-06 13:02:51 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 24 | #include "mbedtls/psa_util.h" |
| 25 | #include "psa/crypto.h" |
| 26 | #endif |
| 27 | |
Manuel Pégourié-Gonnard | da88c38 | 2023-07-06 12:31:43 +0200 | [diff] [blame] | 28 | /* Key types */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/rsa.h" |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 31 | #include "rsa_internal.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 32 | #endif |
Manuel Pégourié-Gonnard | da88c38 | 2023-07-06 12:31:43 +0200 | [diff] [blame] | 33 | |
| 34 | /* Extended formats */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/pem.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 37 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_PKCS5_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/pkcs5.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 40 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PKCS12_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/pkcs12.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
Manuel Pégourié-Gonnard | 997a95e | 2023-07-26 15:18:30 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 46 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 47 | /*********************************************************************** |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 48 | * |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 49 | * ECC setters |
| 50 | * |
| 51 | * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA: |
| 52 | * this macro will not appear outside this section. |
| 53 | * 2. All inputs are raw: no metadata, no ASN.1 until the next section. |
| 54 | * |
| 55 | **********************************************************************/ |
| 56 | |
| 57 | /* |
| 58 | * Set the group used by this key. |
Manuel Pégourié-Gonnard | d1aa642 | 2023-07-26 22:24:23 +0200 | [diff] [blame] | 59 | * |
| 60 | * [in/out] pk: in: must have been pk_setup() to an ECC type |
| 61 | * out: will have group (curve) information set |
| 62 | * [in] grp_in: a supported group ID (not NONE) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 63 | */ |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 64 | static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id) |
| 65 | { |
| 66 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 67 | size_t ec_bits; |
| 68 | psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits); |
| 69 | |
| 70 | /* group may already be initialized; if so, make sure IDs match */ |
| 71 | if ((pk->ec_family != 0 && pk->ec_family != ec_family) || |
| 72 | (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) { |
| 73 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 74 | } |
| 75 | |
| 76 | /* set group */ |
| 77 | pk->ec_family = ec_family; |
| 78 | pk->ec_bits = ec_bits; |
| 79 | |
| 80 | return 0; |
| 81 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 82 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk); |
| 83 | |
| 84 | /* grp may already be initialized; if so, make sure IDs match */ |
| 85 | if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE && |
| 86 | mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) { |
| 87 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 88 | } |
| 89 | |
| 90 | /* set group */ |
| 91 | return mbedtls_ecp_group_load(&(ecp->grp), grp_id); |
| 92 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Set the private key material |
| 97 | * |
Manuel Pégourié-Gonnard | d1aa642 | 2023-07-26 22:24:23 +0200 | [diff] [blame] | 98 | * [in/out] pk: in: must have the group set already, see pk_ecc_set_group(). |
| 99 | * out: will have the private key set. |
| 100 | * [in] key, key_len: the raw private key (no ASN.1 wrapping). |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 101 | */ |
| 102 | static int pk_ecc_set_key(mbedtls_pk_context *pk, |
Manuel Pégourié-Gonnard | d1aa642 | 2023-07-26 22:24:23 +0200 | [diff] [blame] | 103 | unsigned char *key, size_t key_len) |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 104 | { |
| 105 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 106 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | fbbafa0 | 2023-12-06 10:07:34 +0100 | [diff] [blame] | 107 | psa_key_usage_t flags; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 108 | psa_status_t status; |
| 109 | |
| 110 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family)); |
Valerio Setti | fbbafa0 | 2023-12-06 10:07:34 +0100 | [diff] [blame] | 111 | if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) { |
| 112 | /* Do not set algorithm here because Montgomery keys cannot do ECDSA and |
| 113 | * the PK module cannot do ECDH. When the key will be used in TLS for |
| 114 | * ECDH, it will be exported and then re-imported with proper flags |
| 115 | * and algorithm. */ |
| 116 | flags = PSA_KEY_USAGE_EXPORT; |
| 117 | } else { |
| 118 | psa_set_key_algorithm(&attributes, |
| 119 | MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH)); |
| 120 | flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 121 | PSA_KEY_USAGE_EXPORT; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 122 | } |
| 123 | psa_set_key_usage_flags(&attributes, flags); |
| 124 | |
Manuel Pégourié-Gonnard | d1aa642 | 2023-07-26 22:24:23 +0200 | [diff] [blame] | 125 | status = psa_import_key(&attributes, key, key_len, &pk->priv_id); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 126 | return psa_pk_status_to_mbedtls(status); |
| 127 | |
| 128 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 129 | |
| 130 | mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk); |
Manuel Pégourié-Gonnard | d1aa642 | 2023-07-26 22:24:23 +0200 | [diff] [blame] | 131 | int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 132 | if (ret != 0) { |
| 133 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 134 | } |
| 135 | return 0; |
| 136 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 137 | } |
| 138 | |
| 139 | /* |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 140 | * Derive a public key from its private counterpart. |
| 141 | * Computationally intensive, only use when public key is not available. |
| 142 | * |
| 143 | * [in/out] pk: in: must have the private key set, see pk_ecc_set_key(). |
| 144 | * out: will have the public key set. |
| 145 | * [in] prv, prv_len: the raw private key (see note below). |
| 146 | * [in] f_rng, p_rng: RNG function and context. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 147 | * |
| 148 | * Note: the private key information is always available from pk, |
| 149 | * however for convenience the serialized version is also passed, |
| 150 | * as it's available at each calling site, and useful in some configs |
Manuel Pégourié-Gonnard | 94cf1f8 | 2023-08-02 12:09:24 +0200 | [diff] [blame] | 151 | * (as otherwise we would have to re-serialize it from the pk context). |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 152 | * |
| 153 | * There are three implementations of this function: |
| 154 | * 1. MBEDTLS_PK_USE_PSA_EC_DATA, |
| 155 | * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA, |
| 156 | * 3. not MBEDTLS_USE_PSA_CRYPTO. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 157 | */ |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 158 | static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk, |
| 159 | const unsigned char *prv, size_t prv_len, |
| 160 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 161 | { |
| 162 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 164 | (void) f_rng; |
| 165 | (void) p_rng; |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 166 | (void) prv; |
| 167 | (void) prv_len; |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 168 | psa_status_t status; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 169 | |
| 170 | status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw), |
| 171 | &pk->pub_raw_len); |
| 172 | return psa_pk_status_to_mbedtls(status); |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 174 | #elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */ |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 175 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 176 | (void) f_rng; |
| 177 | (void) p_rng; |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 178 | psa_status_t status; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 179 | |
| 180 | mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 181 | size_t curve_bits; |
| 182 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 183 | |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 184 | /* Import private key into PSA, from serialized input */ |
| 185 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 186 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 187 | psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 188 | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT); |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 189 | status = psa_import_key(&key_attr, prv, prv_len, &key_id); |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 190 | if (status != PSA_SUCCESS) { |
| 191 | return psa_pk_status_to_mbedtls(status); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 194 | /* Export public key from PSA */ |
| 195 | unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 196 | size_t pub_len; |
| 197 | status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len); |
| 198 | psa_status_t destruction_status = psa_destroy_key(key_id); |
| 199 | if (status != PSA_SUCCESS) { |
| 200 | return psa_pk_status_to_mbedtls(status); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 201 | } else if (destruction_status != PSA_SUCCESS) { |
| 202 | return psa_pk_status_to_mbedtls(destruction_status); |
| 203 | } |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 204 | |
| 205 | /* Load serialized public key into ecp_keypair structure */ |
| 206 | return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len); |
| 207 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 208 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 210 | (void) prv; |
| 211 | (void) prv_len; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 212 | |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 213 | mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 214 | return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng); |
Manuel Pégourié-Gonnard | 0b8e456 | 2023-07-26 22:43:25 +0200 | [diff] [blame] | 215 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 216 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 217 | } |
| 218 | |
| 219 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 220 | /* |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 221 | * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 222 | * |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 223 | * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA |
| 224 | * functions to handle keys. However, currently psa_import_key() does not |
| 225 | * support compressed points. In case that support was explicitly requested, |
| 226 | * this fallback uses ECP functions to get the job done. This is the reason |
| 227 | * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT. |
| 228 | * |
| 229 | * [in/out] pk: in: must have the group set, see pk_ecc_set_group(). |
| 230 | * out: will have the public key set. |
| 231 | * [in] pub, pub_len: the public key as an ECPoint, |
| 232 | * in any format supported by ECP. |
Manuel Pégourié-Gonnard | fac9819 | 2023-07-27 09:19:42 +0200 | [diff] [blame] | 233 | * |
| 234 | * Return: |
| 235 | * - 0 on success; |
| 236 | * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid |
| 237 | * but not supported; |
| 238 | * - another error code otherwise. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 239 | */ |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 240 | static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk, |
| 241 | const unsigned char *pub, |
| 242 | size_t pub_len) |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 243 | { |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 244 | #if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED) |
Manuel Pégourié-Gonnard | 53d3e40 | 2023-08-01 11:19:24 +0200 | [diff] [blame] | 245 | (void) pk; |
| 246 | (void) pub; |
| 247 | (void) pub_len; |
Manuel Pégourié-Gonnard | fac9819 | 2023-07-27 09:19:42 +0200 | [diff] [blame] | 248 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 249 | #else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */ |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 250 | mbedtls_ecp_keypair ecp_key; |
| 251 | mbedtls_ecp_group_id ecp_group_id; |
| 252 | int ret; |
| 253 | |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 254 | ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 255 | |
| 256 | mbedtls_ecp_keypair_init(&ecp_key); |
| 257 | ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id); |
| 258 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 842ffc5 | 2023-08-02 12:10:51 +0200 | [diff] [blame] | 259 | goto exit; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 260 | } |
| 261 | ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q, |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 262 | pub, pub_len); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 263 | if (ret != 0) { |
| 264 | goto exit; |
| 265 | } |
| 266 | ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q, |
| 267 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
Manuel Pégourié-Gonnard | 681e30b | 2023-07-26 23:03:35 +0200 | [diff] [blame] | 268 | &pk->pub_raw_len, pk->pub_raw, |
| 269 | sizeof(pk->pub_raw)); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 270 | |
| 271 | exit: |
| 272 | mbedtls_ecp_keypair_free(&ecp_key); |
| 273 | return ret; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 274 | #endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */ |
| 275 | } |
| 276 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 277 | |
| 278 | /* |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 279 | * Set the public key. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 280 | * |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 281 | * [in/out] pk: in: must have its group set, see pk_ecc_set_group(). |
| 282 | * out: will have the public key set. |
| 283 | * [in] pub, pub_len: the raw public key (an ECPoint). |
Manuel Pégourié-Gonnard | fac9819 | 2023-07-27 09:19:42 +0200 | [diff] [blame] | 284 | * |
| 285 | * Return: |
| 286 | * - 0 on success; |
| 287 | * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid |
| 288 | * but not supported; |
| 289 | * - another error code otherwise. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 290 | */ |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 291 | static int pk_ecc_set_pubkey(mbedtls_pk_context *pk, |
| 292 | const unsigned char *pub, size_t pub_len) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 293 | { |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 295 | |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 296 | /* Load the key */ |
Manuel Pégourié-Gonnard | 52e9548 | 2023-08-03 10:22:41 +0200 | [diff] [blame] | 297 | if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) { |
| 298 | /* Format directly supported by PSA: |
| 299 | * - non-Weierstrass curves that only have one format; |
| 300 | * - uncompressed format for Weierstrass curves. */ |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 301 | if (pub_len > sizeof(pk->pub_raw)) { |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 302 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 303 | } |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 304 | memcpy(pk->pub_raw, pub, pub_len); |
| 305 | pk->pub_raw_len = pub_len; |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 306 | } else { |
| 307 | /* Other format, try the fallback */ |
| 308 | int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len); |
| 309 | if (ret != 0) { |
| 310 | return ret; |
| 311 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 312 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 313 | |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 314 | /* Validate the key by trying to import it */ |
| 315 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 316 | psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT; |
| 317 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 318 | psa_set_key_usage_flags(&key_attrs, 0); |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 319 | psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)); |
| 320 | psa_set_key_bits(&key_attrs, pk->ec_bits); |
| 321 | |
| 322 | if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len, |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 323 | &key_id) != PSA_SUCCESS) || |
| 324 | (psa_destroy_key(key_id) != PSA_SUCCESS)) { |
| 325 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 326 | } |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 327 | |
| 328 | return 0; |
| 329 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 330 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 331 | |
| 332 | int ret; |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 333 | mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx; |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 334 | ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len); |
| 335 | if (ret != 0) { |
| 336 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | } |
Manuel Pégourié-Gonnard | ff72ea9 | 2023-07-26 23:56:05 +0200 | [diff] [blame] | 338 | return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q); |
| 339 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 340 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 343 | /*********************************************************************** |
| 344 | * |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 345 | * Low-level ECC parsing: optional support for SpecifiedECDomain |
| 346 | * |
| 347 | * There are two functions here that are used by the rest of the code: |
Manuel Pégourié-Gonnard | f1b7633 | 2023-08-02 12:14:19 +0200 | [diff] [blame] | 348 | * - pk_ecc_tag_is_speficied_ec_domain() |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 349 | * - pk_ecc_group_id_from_specified() |
| 350 | * |
| 351 | * All the other functions are internal to this section. |
| 352 | * |
| 353 | * The two "public" functions have a dummy variant provided |
| 354 | * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an |
| 355 | * abstraction layer for this macro, which should not appear outside |
| 356 | * this section. |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 357 | * |
| 358 | **********************************************************************/ |
| 359 | |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 360 | #if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
| 361 | /* See the "real" version for documentation */ |
Manuel Pégourié-Gonnard | f1b7633 | 2023-08-02 12:14:19 +0200 | [diff] [blame] | 362 | static int pk_ecc_tag_is_specified_ec_domain(int tag) |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 363 | { |
| 364 | (void) tag; |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* See the "real" version for documentation */ |
| 369 | static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params, |
| 370 | mbedtls_ecp_group_id *grp_id) |
| 371 | { |
| 372 | (void) params; |
| 373 | (void) grp_id; |
| 374 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 375 | } |
| 376 | #else /* MBEDTLS_PK_PARSE_EC_EXTENDED */ |
| 377 | /* |
| 378 | * Tell if the passed tag might be the start of SpecifiedECDomain |
| 379 | * (that is, a sequence). |
| 380 | */ |
Manuel Pégourié-Gonnard | f1b7633 | 2023-08-02 12:14:19 +0200 | [diff] [blame] | 381 | static int pk_ecc_tag_is_specified_ec_domain(int tag) |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 382 | { |
| 383 | return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 384 | } |
| 385 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 386 | /* |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 387 | * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it. |
| 388 | * WARNING: the resulting group should only be used with |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 389 | * pk_ecc_group_id_from_specified(), since its base point may not be set correctly |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 390 | * if it was encoded compressed. |
| 391 | * |
| 392 | * SpecifiedECDomain ::= SEQUENCE { |
| 393 | * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...), |
| 394 | * fieldID FieldID {{FieldTypes}}, |
| 395 | * curve Curve, |
| 396 | * base ECPoint, |
| 397 | * order INTEGER, |
| 398 | * cofactor INTEGER OPTIONAL, |
| 399 | * hash HashAlgorithm OPTIONAL, |
| 400 | * ... |
| 401 | * } |
| 402 | * |
| 403 | * We only support prime-field as field type, and ignore hash and cofactor. |
| 404 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 406 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 407 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 408 | unsigned char *p = params->p; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 409 | const unsigned char *const end = params->p + params->len; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 410 | const unsigned char *end_field, *end_curve; |
| 411 | size_t len; |
| 412 | int ver; |
| 413 | |
| 414 | /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) { |
| 416 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 417 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | if (ver < 1 || ver > 3) { |
| 420 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 421 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field |
| 425 | * fieldType FIELD-ID.&id({IOSet}), |
| 426 | * parameters FIELD-ID.&Type({IOSet}{@fieldType}) |
| 427 | * } |
| 428 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 430 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 431 | return ret; |
| 432 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 433 | |
| 434 | end_field = p + len; |
| 435 | |
| 436 | /* |
| 437 | * FIELD-ID ::= TYPE-IDENTIFIER |
| 438 | * FieldTypes FIELD-ID ::= { |
| 439 | * { Prime-p IDENTIFIED BY prime-field } | |
| 440 | * { Characteristic-two IDENTIFIED BY characteristic-two-field } |
| 441 | * } |
| 442 | * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 } |
| 443 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) { |
| 445 | return ret; |
| 446 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) || |
| 449 | memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) { |
| 450 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | p += len; |
| 454 | |
| 455 | /* Prime-p ::= INTEGER -- Field of size p. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) { |
| 457 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 458 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | grp->pbits = mbedtls_mpi_bitlen(&grp->P); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | if (p != end_field) { |
| 463 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 464 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 465 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Curve ::= SEQUENCE { |
| 469 | * a FieldElement, |
| 470 | * b FieldElement, |
| 471 | * seed BIT STRING OPTIONAL |
| 472 | * -- Shall be present if used in SpecifiedECDomain |
| 473 | * -- with version equal to ecdpVer2 or ecdpVer3 |
| 474 | * } |
| 475 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 477 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 478 | return ret; |
| 479 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 480 | |
| 481 | end_curve = p + len; |
| 482 | |
| 483 | /* |
| 484 | * FieldElement ::= OCTET STRING |
| 485 | * containing an integer in the case of a prime field |
| 486 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 || |
| 488 | (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) { |
| 489 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | p += len; |
| 493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 || |
| 495 | (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) { |
| 496 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | p += len; |
| 500 | |
| 501 | /* Ignore seed BIT STRING OPTIONAL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) { |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 503 | p += len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | if (p != end_curve) { |
| 507 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 508 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 509 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * ECPoint ::= OCTET STRING |
| 513 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 515 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 516 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G, |
| 519 | (const unsigned char *) p, len)) != 0) { |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 520 | /* |
| 521 | * If we can't read the point because it's compressed, cheat by |
| 522 | * reading only the X coordinate and the parity bit of Y. |
| 523 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE || |
| 525 | (p[0] != 0x02 && p[0] != 0x03) || |
| 526 | len != mbedtls_mpi_size(&grp->P) + 1 || |
| 527 | mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 || |
| 528 | mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 || |
| 529 | mbedtls_mpi_lset(&grp->G.Z, 1) != 0) { |
| 530 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 534 | p += len; |
| 535 | |
| 536 | /* |
| 537 | * order INTEGER |
| 538 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) { |
| 540 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 541 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | grp->nbits = mbedtls_mpi_bitlen(&grp->N); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Allow optional elements by purposefully not enforcing p == end here. |
| 547 | */ |
| 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return 0; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Find the group id associated with an (almost filled) group as generated by |
| 554 | * pk_group_from_specified(), or return an error if unknown. |
| 555 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 557 | { |
Manuel Pégourié-Gonnard | 5b8c409 | 2014-03-27 14:59:42 +0100 | [diff] [blame] | 558 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | mbedtls_ecp_group ref; |
| 560 | const mbedtls_ecp_group_id *id; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | mbedtls_ecp_group_init(&ref); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) { |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 565 | /* Load the group associated to that id */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | mbedtls_ecp_group_free(&ref); |
| 567 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id)); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 568 | |
| 569 | /* Compare to the group we were given, starting with easy tests */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if (grp->pbits == ref.pbits && grp->nbits == ref.nbits && |
| 571 | mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 && |
| 572 | mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 && |
| 573 | mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 && |
| 574 | mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 && |
| 575 | mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 && |
| 576 | mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 && |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 577 | /* For Y we may only know the parity bit, so compare only that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) { |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 579 | break; |
| 580 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | mbedtls_ecp_group_free(&ref); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 585 | |
| 586 | *grp_id = *id; |
| 587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | return ret; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID |
| 597 | */ |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 598 | static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params, |
| 599 | mbedtls_ecp_group_id *grp_id) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 600 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | mbedtls_ecp_group grp; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | mbedtls_ecp_group_init(&grp); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | if ((ret = pk_group_from_specified(params, &grp)) != 0) { |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 607 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | ret = pk_group_id_from_group(&grp, grp_id); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 611 | |
| 612 | cleanup: |
Minos Galanakis | 8692ec8 | 2023-01-20 15:27:32 +0000 | [diff] [blame] | 613 | /* The API respecting lifecycle for mbedtls_ecp_group struct is |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 614 | * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the |
Minos Galanakis | 8692ec8 | 2023-01-20 15:27:32 +0000 | [diff] [blame] | 615 | * temporary grp breaks that flow and it's members are populated |
| 616 | * by pk_group_id_from_group(). As such mbedtls_ecp_group_free() |
| 617 | * which is assuming a group populated by _setup() may not clean-up |
| 618 | * properly -> Manually free it's members. |
| 619 | */ |
Minos Galanakis | c8e381a | 2023-01-19 16:08:34 +0000 | [diff] [blame] | 620 | mbedtls_mpi_free(&grp.N); |
| 621 | mbedtls_mpi_free(&grp.P); |
| 622 | mbedtls_mpi_free(&grp.A); |
| 623 | mbedtls_mpi_free(&grp.B); |
| 624 | mbedtls_ecp_point_free(&grp.G); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | return ret; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 627 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */ |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 630 | /*********************************************************************** |
| 631 | * |
| 632 | * Unsorted (yet!) from this point on until the next section header |
| 633 | * |
| 634 | **********************************************************************/ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 635 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 636 | /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 637 | * |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 638 | * ECParameters ::= CHOICE { |
| 639 | * namedCurve OBJECT IDENTIFIER |
| 640 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
| 641 | * -- implicitCurve NULL |
| 642 | * } |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 643 | */ |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 644 | static int pk_get_ecparams(unsigned char **p, const unsigned char *end, |
| 645 | mbedtls_asn1_buf *params) |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 646 | { |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 647 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 648 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 649 | if (end - *p < 1) { |
| 650 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 651 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 652 | } |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 653 | |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 654 | /* Acceptable tags: OID for namedCurve, or specifiedECDomain */ |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 655 | params->tag = **p; |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 656 | if (params->tag != MBEDTLS_ASN1_OID && |
Manuel Pégourié-Gonnard | f1b7633 | 2023-08-02 12:14:19 +0200 | [diff] [blame] | 657 | !pk_ecc_tag_is_specified_ec_domain(params->tag)) { |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 658 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 659 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 660 | } |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 661 | |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 662 | if ((ret = mbedtls_asn1_get_tag(p, end, ¶ms->len, params->tag)) != 0) { |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 663 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 664 | } |
Manuel Pégourié-Gonnard | 5470898 | 2023-07-26 15:38:36 +0200 | [diff] [blame] | 665 | |
| 666 | params->p = *p; |
| 667 | *p += params->len; |
| 668 | |
| 669 | if (*p != end) { |
| 670 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 671 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 672 | } |
| 673 | |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 674 | return 0; |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 678 | * Use EC parameters to initialise an EC group |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 679 | * |
| 680 | * ECParameters ::= CHOICE { |
| 681 | * namedCurve OBJECT IDENTIFIER |
| 682 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
| 683 | * -- implicitCurve NULL |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 684 | */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 685 | static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 686 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 687 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_ecp_group_id grp_id; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | if (params->tag == MBEDTLS_ASN1_OID) { |
| 691 | if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) { |
| 692 | return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE; |
| 693 | } |
| 694 | } else { |
Manuel Pégourié-Gonnard | 12ea63a | 2023-07-27 12:20:16 +0200 | [diff] [blame] | 695 | ret = pk_ecc_group_id_from_specified(params, &grp_id); |
| 696 | if (ret != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | return ret; |
| 698 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 699 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 700 | |
Manuel Pégourié-Gonnard | 2585852 | 2023-07-24 11:44:55 +0200 | [diff] [blame] | 701 | return pk_ecc_set_group(pk, grp_id); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 704 | #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) |
| 705 | |
| 706 | /* |
| 707 | * Load an RFC8410 EC key, which doesn't have any parameters |
| 708 | */ |
| 709 | static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params, |
| 710 | mbedtls_ecp_group_id grp_id, |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 711 | mbedtls_pk_context *pk) |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 712 | { |
| 713 | if (params->tag != 0 || params->len != 0) { |
| 714 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 715 | } |
| 716 | |
Manuel Pégourié-Gonnard | 2585852 | 2023-07-24 11:44:55 +0200 | [diff] [blame] | 717 | return pk_ecc_set_group(pk, grp_id); |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Parse an RFC 8410 encoded private EC key |
| 722 | * |
| 723 | * CurvePrivateKey ::= OCTET STRING |
| 724 | */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 725 | static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk, |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 726 | unsigned char *key, size_t keylen, const unsigned char *end, |
| 727 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 728 | { |
| 729 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 730 | size_t len; |
| 731 | |
| 732 | if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 733 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 734 | } |
| 735 | |
| 736 | if (key + len != end) { |
| 737 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 738 | } |
| 739 | |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 740 | /* |
| 741 | * Load the private key |
| 742 | */ |
| 743 | ret = pk_ecc_set_key(pk, key, len); |
| 744 | if (ret != 0) { |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 745 | return ret; |
| 746 | } |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 747 | |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 748 | /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys, |
| 749 | * which never contain a public key. As such, derive the public key |
| 750 | * unconditionally. */ |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 751 | if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 752 | return ret; |
| 753 | } |
| 754 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | #endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 758 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 759 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 760 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 761 | /* Get a PK algorithm identifier |
| 762 | * |
| 763 | * AlgorithmIdentifier ::= SEQUENCE { |
| 764 | * algorithm OBJECT IDENTIFIER, |
| 765 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 766 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | static int pk_get_pk_alg(unsigned char **p, |
| 768 | const unsigned char *end, |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 769 | mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params, |
| 770 | mbedtls_ecp_group_id *ec_grp_id) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 771 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 772 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | mbedtls_asn1_buf alg_oid; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | memset(params, 0, sizeof(mbedtls_asn1_buf)); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) { |
| 778 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret); |
| 779 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 780 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 781 | ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg); |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 782 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 783 | if (ret == MBEDTLS_ERR_OID_NOT_FOUND) { |
| 784 | ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id); |
| 785 | if (ret == 0) { |
| 786 | *pk_alg = MBEDTLS_PK_ECKEY; |
| 787 | } |
| 788 | } |
| 789 | #else |
| 790 | (void) ec_grp_id; |
| 791 | #endif |
| 792 | if (ret != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 794 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 795 | |
| 796 | /* |
| 797 | * No parameters with RSA (only for EC) |
| 798 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | if (*pk_alg == MBEDTLS_PK_RSA && |
| 800 | ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) || |
| 801 | params->len != 0)) { |
| 802 | return MBEDTLS_ERR_PK_INVALID_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* |
| 809 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 810 | * algorithm AlgorithmIdentifier, |
| 811 | * subjectPublicKey BIT STRING } |
| 812 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 813 | int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, |
| 814 | mbedtls_pk_context *pk) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 815 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 816 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 817 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | mbedtls_asn1_buf alg_params; |
| 819 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 820 | mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | const mbedtls_pk_info_t *pk_info; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 824 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 825 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | end = *p + len; |
| 829 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 830 | if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | return ret; |
| 832 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 834 | if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) { |
| 835 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 836 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | if (*p + len != end) { |
| 839 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 840 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 841 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) { |
| 844 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 845 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) { |
| 848 | return ret; |
| 849 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 852 | if (pk_alg == MBEDTLS_PK_RSA) { |
Valerio Setti | 201e643 | 2024-02-01 17:19:37 +0100 | [diff] [blame] | 853 | ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), *p, (size_t) (end - *p)); |
Valerio Setti | 5922cb9 | 2024-02-02 09:21:25 +0100 | [diff] [blame] | 854 | if (ret == 0) { |
| 855 | /* On success all the input has been consumed by the parsing function. */ |
| 856 | *p += end - *p; |
Valerio Setti | 5a19892 | 2024-02-02 13:59:51 +0100 | [diff] [blame] | 857 | } else if ((ret <= MBEDTLS_ERR_ASN1_OUT_OF_DATA) && |
| 858 | (ret >= MBEDTLS_ERR_ASN1_BUF_TOO_SMALL)) { |
Valerio Setti | 5922cb9 | 2024-02-02 09:21:25 +0100 | [diff] [blame] | 859 | /* In case of ASN1 error codes add MBEDTLS_ERR_PK_INVALID_PUBKEY. */ |
| 860 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 861 | } else { |
| 862 | ret = MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 863 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 864 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 865 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 866 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 868 | #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 869 | if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 870 | ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk); |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 871 | } else |
| 872 | #endif |
| 873 | { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 874 | ret = pk_use_ecparams(&alg_params, pk); |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 875 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | if (ret == 0) { |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 877 | ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p)); |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 878 | *p += end - *p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 880 | } else |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 881 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | if (ret == 0 && *p != end) { |
| 885 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 886 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 887 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | if (ret != 0) { |
| 890 | mbedtls_pk_free(pk); |
| 891 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 893 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 894 | } |
| 895 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 896 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 897 | /* |
| 898 | * Parse a SEC1 encoded private EC key |
| 899 | */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 900 | static int pk_parse_key_sec1_der(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | const unsigned char *key, size_t keylen, |
| 902 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 903 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 904 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 905 | int version, pubkey_done; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 906 | size_t len, d_len; |
Leonid Rozenboim | a3008e7 | 2022-04-21 17:28:18 -0700 | [diff] [blame] | 907 | mbedtls_asn1_buf params = { 0, 0, NULL }; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 908 | unsigned char *p = (unsigned char *) key; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 909 | unsigned char *d; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 910 | unsigned char *end = p + keylen; |
| 911 | unsigned char *end2; |
| 912 | |
| 913 | /* |
| 914 | * RFC 5915, or SEC1 Appendix C.4 |
| 915 | * |
| 916 | * ECPrivateKey ::= SEQUENCE { |
| 917 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 918 | * privateKey OCTET STRING, |
| 919 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 920 | * publicKey [1] BIT STRING OPTIONAL |
| 921 | * } |
| 922 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 924 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 925 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | end = p + len; |
| 929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 931 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 932 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | if (version != 1) { |
| 935 | return MBEDTLS_ERR_PK_KEY_INVALID_VERSION; |
| 936 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 939 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 940 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 941 | |
Valerio Setti | 6b062ee | 2023-06-30 17:32:57 +0200 | [diff] [blame] | 942 | /* Keep a reference to the position fo the private key. It will be used |
| 943 | * later in this function. */ |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 944 | d = p; |
| 945 | d_len = len; |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 946 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 947 | p += len; |
| 948 | |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 949 | pubkey_done = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | if (p != end) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 951 | /* |
| 952 | * Is 'parameters' present? |
| 953 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 955 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 956 | 0)) == 0) { |
| 957 | if ((ret = pk_get_ecparams(&p, p + len, ¶ms)) != 0 || |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 958 | (ret = pk_use_ecparams(¶ms, pk)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 959 | return ret; |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 960 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 963 | } |
Jethro Beekman | d2df936 | 2018-02-16 13:11:04 -0800 | [diff] [blame] | 964 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 965 | |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 966 | /* |
| 967 | * Load the private key |
| 968 | */ |
| 969 | ret = pk_ecc_set_key(pk, d, d_len); |
| 970 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 6db11d5 | 2023-07-25 11:20:48 +0200 | [diff] [blame] | 971 | return ret; |
| 972 | } |
Valerio Setti | 6b062ee | 2023-06-30 17:32:57 +0200 | [diff] [blame] | 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | if (p != end) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 975 | /* |
| 976 | * Is 'publickey' present? If not, or if we can't read it (eg because it |
| 977 | * is compressed), create it from the private key. |
| 978 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 980 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 981 | 1)) == 0) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 982 | end2 = p + len; |
| 983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) { |
| 985 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 986 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | if (p + len != end2) { |
| 989 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 990 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 991 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 992 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 993 | if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 994 | pubkey_done = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | } else { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 996 | /* |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 997 | * The only acceptable failure mode of pk_ecc_set_pubkey() above |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 998 | * is if the point format is not recognized. |
| 999 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) { |
| 1001 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1002 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1003 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1006 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1007 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 1008 | |
Valerio Setti | 34f6755 | 2023-04-03 15:19:18 +0200 | [diff] [blame] | 1009 | if (!pubkey_done) { |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 1010 | if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) { |
Valerio Setti | 520c038 | 2023-04-07 11:38:09 +0200 | [diff] [blame] | 1011 | return ret; |
Valerio Setti | 34f6755 | 2023-04-03 15:19:18 +0200 | [diff] [blame] | 1012 | } |
Manuel Pégourié-Gonnard | ff29f9c | 2013-09-18 16:13:02 +0200 | [diff] [blame] | 1013 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1016 | } |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1017 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1018 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1019 | /*********************************************************************** |
| 1020 | * |
| 1021 | * PKCS#8 parsing functions |
| 1022 | * |
| 1023 | **********************************************************************/ |
| 1024 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1025 | /* |
| 1026 | * Parse an unencrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1027 | * |
| 1028 | * Notes: |
| 1029 | * |
| 1030 | * - This function does not own the key buffer. It is the |
| 1031 | * responsibility of the caller to take care of zeroizing |
| 1032 | * and freeing it after use. |
| 1033 | * |
| 1034 | * - The function is responsible for freeing the provided |
| 1035 | * PK context on failure. |
| 1036 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1037 | */ |
| 1038 | static int pk_parse_key_pkcs8_unencrypted_der( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1039 | mbedtls_pk_context *pk, |
| 1040 | const unsigned char *key, size_t keylen, |
| 1041 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1042 | { |
| 1043 | int ret, version; |
| 1044 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1045 | mbedtls_asn1_buf params; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1046 | unsigned char *p = (unsigned char *) key; |
| 1047 | unsigned char *end = p + keylen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1049 | mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1050 | const mbedtls_pk_info_t *pk_info; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1051 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1052 | #if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 609ab64 | 2021-06-16 14:29:11 +0200 | [diff] [blame] | 1053 | (void) f_rng; |
| 1054 | (void) p_rng; |
| 1055 | #endif |
| 1056 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1057 | /* |
Hanno Becker | 9c6cb38 | 2017-09-05 10:08:01 +0100 | [diff] [blame] | 1058 | * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1059 | * |
| 1060 | * PrivateKeyInfo ::= SEQUENCE { |
| 1061 | * version Version, |
| 1062 | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
| 1063 | * privateKey PrivateKey, |
| 1064 | * attributes [0] IMPLICIT Attributes OPTIONAL } |
| 1065 | * |
| 1066 | * Version ::= INTEGER |
| 1067 | * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1068 | * PrivateKey ::= OCTET STRING |
| 1069 | * |
| 1070 | * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey |
| 1071 | */ |
| 1072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1073 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1074 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1075 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | end = p + len; |
| 1079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 1081 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Chris Jones | fdb588b | 2021-04-14 18:15:24 +0100 | [diff] [blame] | 1082 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1084 | if (version != 0) { |
| 1085 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret); |
| 1086 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1087 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1088 | if ((ret = pk_get_pk_alg(&p, end, &pk_alg, ¶ms, &ec_grp_id)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1089 | return ret; |
| 1090 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1093 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1094 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | if (len < 1) { |
| 1097 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 1098 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 1099 | } |
| 1100 | |
| 1101 | if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) { |
| 1102 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1103 | } |
| 1104 | |
| 1105 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) { |
| 1106 | return ret; |
| 1107 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1109 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | if (pk_alg == MBEDTLS_PK_RSA) { |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame] | 1111 | if ((ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), p, len)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | mbedtls_pk_free(pk); |
| 1113 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1114 | } |
| 1115 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1119 | #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 1120 | if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1121 | if ((ret = |
| 1122 | pk_use_ecparams_rfc8410(¶ms, ec_grp_id, pk)) != 0 || |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1123 | (ret = |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1124 | pk_parse_key_rfc8410_der(pk, p, len, end, f_rng, |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1125 | p_rng)) != 0) { |
| 1126 | mbedtls_pk_free(pk); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | } else |
| 1130 | #endif |
| 1131 | { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1132 | if ((ret = pk_use_ecparams(¶ms, pk)) != 0 || |
| 1133 | (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1134 | mbedtls_pk_free(pk); |
| 1135 | return ret; |
| 1136 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1137 | } |
| 1138 | } else |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1139 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1141 | |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1142 | end = p + len; |
| 1143 | if (end != (key + keylen)) { |
| 1144 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 1145 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1146 | } |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 1147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * Parse an encrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1153 | * |
| 1154 | * To save space, the decryption happens in-place on the given key buffer. |
| 1155 | * Also, while this function may modify the keybuffer, it doesn't own it, |
| 1156 | * and instead it is the responsibility of the caller to zeroize and properly |
| 1157 | * free it after use. |
| 1158 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1159 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1161 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | mbedtls_pk_context *pk, |
| 1163 | unsigned char *key, size_t keylen, |
| 1164 | const unsigned char *pwd, size_t pwdlen, |
| 1165 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1166 | { |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1167 | int ret, decrypted = 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1168 | size_t len; |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1169 | unsigned char *buf; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1170 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1171 | mbedtls_asn1_buf pbe_alg_oid, pbe_params; |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1172 | #if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1173 | mbedtls_cipher_type_t cipher_alg; |
| 1174 | mbedtls_md_type_t md_alg; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1175 | #endif |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1176 | size_t outlen = 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1177 | |
Hanno Becker | 2aa80a7 | 2017-09-07 15:28:45 +0100 | [diff] [blame] | 1178 | p = key; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1179 | end = p + keylen; |
| 1180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | if (pwdlen == 0) { |
| 1182 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1183 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1184 | |
| 1185 | /* |
Hanno Becker | f04111f | 2017-09-29 19:18:42 +0100 | [diff] [blame] | 1186 | * This function parses the EncryptedPrivateKeyInfo object (PKCS#8) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1187 | * |
| 1188 | * EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 1189 | * encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 1190 | * encryptedData EncryptedData |
| 1191 | * } |
| 1192 | * |
| 1193 | * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1194 | * |
| 1195 | * EncryptedData ::= OCTET STRING |
| 1196 | * |
| 1197 | * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1198 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1199 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1201 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1202 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | end = p + len; |
| 1206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) { |
| 1208 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1209 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1211 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1212 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1213 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1214 | |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1215 | buf = p; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1216 | |
| 1217 | /* |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1218 | * Decrypt EncryptedData with appropriate PBE |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1219 | */ |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1220 | #if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) { |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 1222 | if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT, |
Waleed Elmelegy | 5e48cad | 2023-09-12 14:52:48 +0100 | [diff] [blame] | 1223 | cipher_alg, md_alg, |
| 1224 | pwd, pwdlen, p, len, buf, len, &outlen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) { |
| 1226 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1227 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1230 | } |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 1231 | |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1232 | decrypted = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | } else |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1234 | #endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */ |
| 1235 | #if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1236 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) { |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1237 | if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen, |
| 1238 | p, len, buf, len, &outlen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) { |
| 1240 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1241 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1244 | } |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1245 | |
| 1246 | decrypted = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | } else |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1248 | #endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */ |
Manuel Pégourié-Gonnard | 1032c1d | 2013-09-18 17:18:34 +0200 | [diff] [blame] | 1249 | { |
| 1250 | ((void) pwd); |
Manuel Pégourié-Gonnard | 1032c1d | 2013-09-18 17:18:34 +0200 | [diff] [blame] | 1251 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1253 | if (decrypted == 0) { |
| 1254 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1255 | } |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1256 | return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1257 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1259 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1260 | /*********************************************************************** |
| 1261 | * |
| 1262 | * Top-level functions, with format auto-discovery |
| 1263 | * |
| 1264 | **********************************************************************/ |
| 1265 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1266 | /* |
| 1267 | * Parse a private key |
| 1268 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | int mbedtls_pk_parse_key(mbedtls_pk_context *pk, |
| 1270 | const unsigned char *key, size_t keylen, |
| 1271 | const unsigned char *pwd, size_t pwdlen, |
| 1272 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1273 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1274 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | const mbedtls_pk_info_t *pk_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1277 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1278 | mbedtls_pem_context pem; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1279 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | if (keylen == 0) { |
| 1282 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1283 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1284 | |
| 1285 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | mbedtls_pem_init(&pem); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1289 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1291 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | } else { |
| 1293 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1294 | PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | key, pwd, pwdlen, &len); |
| 1296 | } |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | if (ret == 0) { |
| 1299 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1300 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame] | 1301 | (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), |
Valerio Setti | fd49a46 | 2024-01-23 08:35:11 +0100 | [diff] [blame] | 1302 | pem.buf, pem.buflen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1304 | } |
| 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | mbedtls_pem_free(&pem); |
| 1307 | return ret; |
| 1308 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1309 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1310 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1311 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1312 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1313 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1314 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1316 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1317 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1318 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1320 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | } else { |
| 1322 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1323 | PEM_BEGIN_PRIVATE_KEY_EC, |
| 1324 | PEM_END_PRIVATE_KEY_EC, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | key, pwd, pwdlen, &len); |
| 1326 | } |
| 1327 | if (ret == 0) { |
| 1328 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1330 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1331 | (ret = pk_parse_key_sec1_der(pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | pem.buf, pem.buflen, |
| 1333 | f_rng, p_rng)) != 0) { |
| 1334 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | mbedtls_pem_free(&pem); |
| 1338 | return ret; |
| 1339 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1340 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1341 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1342 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1343 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1344 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1345 | } |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1346 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1347 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1348 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1350 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | } else { |
| 1352 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1353 | PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | key, NULL, 0, &len); |
| 1355 | } |
| 1356 | if (ret == 0) { |
| 1357 | if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk, |
| 1358 | pem.buf, pem.buflen, f_rng, p_rng)) != 0) { |
| 1359 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1362 | mbedtls_pem_free(&pem); |
| 1363 | return ret; |
| 1364 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1365 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1366 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1368 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1369 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1371 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | } else { |
| 1373 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1374 | PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8, |
| 1375 | PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | key, NULL, 0, &len); |
| 1377 | } |
| 1378 | if (ret == 0) { |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1379 | if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen, |
| 1380 | pwd, pwdlen, f_rng, p_rng)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1384 | mbedtls_pem_free(&pem); |
| 1385 | return ret; |
| 1386 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1387 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1388 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1389 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1390 | #else |
| 1391 | ((void) pwd); |
| 1392 | ((void) pwdlen); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1394 | |
| 1395 | /* |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 1396 | * At this point we only know it's not a PEM formatted key. Could be any |
| 1397 | * of the known DER encoded private key formats |
| 1398 | * |
| 1399 | * We try the different DER format parsers to see if one passes without |
| 1400 | * error |
| 1401 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1403 | if (pwdlen != 0) { |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1404 | unsigned char *key_copy; |
| 1405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1406 | if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) { |
| 1407 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 1408 | } |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1410 | memcpy(key_copy, key, keylen); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1411 | |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1412 | ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen, |
| 1413 | pwd, pwdlen, f_rng, p_rng); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1414 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 1415 | mbedtls_zeroize_and_free(key_copy, keylen); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | if (ret == 0) { |
| 1419 | return 0; |
| 1420 | } |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | mbedtls_pk_free(pk); |
| 1423 | mbedtls_pk_init(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) { |
| 1426 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1427 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1428 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng); |
| 1431 | if (ret == 0) { |
| 1432 | return 0; |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1433 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1435 | mbedtls_pk_free(pk); |
| 1436 | mbedtls_pk_init(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1441 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame] | 1442 | mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1443 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1446 | mbedtls_pk_free(pk); |
| 1447 | mbedtls_pk_init(pk); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1448 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1449 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1450 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
| 1452 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1453 | pk_parse_key_sec1_der(pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | key, keylen, f_rng, p_rng) == 0) { |
| 1455 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1456 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1457 | mbedtls_pk_free(pk); |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1458 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1459 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1460 | /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't, |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1461 | * it is ok to leave the PK context initialized but not |
| 1462 | * freed: It is the caller's responsibility to call pk_init() |
| 1463 | * before calling this function, and to call pk_free() |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1464 | * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1465 | * isn't, this leads to mbedtls_pk_free() being called |
| 1466 | * twice, once here and once by the caller, but this is |
| 1467 | * also ok and in line with the mbedtls_pk_free() calls |
| 1468 | * on failed PEM parsing attempts. */ |
| 1469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * Parse a public key |
| 1475 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1476 | int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, |
| 1477 | const unsigned char *key, size_t keylen) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1478 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1479 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1480 | unsigned char *p; |
Ron Eldor | 5472d43 | 2017-10-17 09:49:00 +0300 | [diff] [blame] | 1481 | #if defined(MBEDTLS_RSA_C) |
| 1482 | const mbedtls_pk_info_t *pk_info; |
| 1483 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1485 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | mbedtls_pem_context pem; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1487 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | if (keylen == 0) { |
| 1490 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1491 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1492 | |
| 1493 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | mbedtls_pem_init(&pem); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1495 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1496 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1498 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | } else { |
| 1500 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1501 | PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | key, NULL, 0, &len); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1503 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | |
| 1505 | if (ret == 0) { |
| 1506 | p = pem.buf; |
| 1507 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1508 | mbedtls_pem_free(&pem); |
| 1509 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1510 | } |
| 1511 | |
| 1512 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1513 | mbedtls_pem_free(&pem); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
Valerio Setti | 201e643 | 2024-02-01 17:19:37 +0100 | [diff] [blame] | 1517 | if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, pem.buflen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | mbedtls_pk_free(ctx); |
| 1519 | } |
| 1520 | |
| 1521 | mbedtls_pem_free(&pem); |
| 1522 | return ret; |
| 1523 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1524 | mbedtls_pem_free(&pem); |
| 1525 | return ret; |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1526 | } |
| 1527 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1528 | |
| 1529 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1531 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | } else { |
| 1533 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1534 | PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | key, NULL, 0, &len); |
| 1536 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | if (ret == 0) { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1539 | /* |
| 1540 | * Was PEM encoded |
| 1541 | */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1542 | p = pem.buf; |
| 1543 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1544 | ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | mbedtls_pem_free(&pem); |
| 1546 | return ret; |
| 1547 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1548 | mbedtls_pem_free(&pem); |
| 1549 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1550 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1551 | mbedtls_pem_free(&pem); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1552 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1553 | |
| 1554 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1556 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1557 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | |
| 1559 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1560 | return ret; |
| 1561 | } |
| 1562 | |
| 1563 | p = (unsigned char *) key; |
Valerio Setti | 201e643 | 2024-02-01 17:19:37 +0100 | [diff] [blame] | 1564 | ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, keylen); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | if (ret == 0) { |
| 1566 | return ret; |
| 1567 | } |
| 1568 | mbedtls_pk_free(ctx); |
Valerio Setti | dccfd36 | 2024-01-23 17:07:59 +0100 | [diff] [blame] | 1569 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | return ret; |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1571 | } |
| 1572 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1573 | p = (unsigned char *) key; |
| 1574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1580 | /*********************************************************************** |
| 1581 | * |
| 1582 | * Top-level functions, with filesystem support |
| 1583 | * |
| 1584 | **********************************************************************/ |
| 1585 | |
| 1586 | #if defined(MBEDTLS_FS_IO) |
| 1587 | /* |
| 1588 | * Load all data from a file into a given buffer. |
| 1589 | * |
| 1590 | * The file is expected to contain either PEM or DER encoded data. |
| 1591 | * A terminating null byte is always appended. It is included in the announced |
| 1592 | * length only if the data looks like it is PEM encoded. |
| 1593 | */ |
| 1594 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n) |
| 1595 | { |
| 1596 | FILE *f; |
| 1597 | long size; |
| 1598 | |
| 1599 | if ((f = fopen(path, "rb")) == NULL) { |
| 1600 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1601 | } |
| 1602 | |
| 1603 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
| 1604 | mbedtls_setbuf(f, NULL); |
| 1605 | |
| 1606 | fseek(f, 0, SEEK_END); |
| 1607 | if ((size = ftell(f)) == -1) { |
| 1608 | fclose(f); |
| 1609 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1610 | } |
| 1611 | fseek(f, 0, SEEK_SET); |
| 1612 | |
| 1613 | *n = (size_t) size; |
| 1614 | |
| 1615 | if (*n + 1 == 0 || |
| 1616 | (*buf = mbedtls_calloc(1, *n + 1)) == NULL) { |
| 1617 | fclose(f); |
| 1618 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 1619 | } |
| 1620 | |
| 1621 | if (fread(*buf, 1, *n, f) != *n) { |
| 1622 | fclose(f); |
| 1623 | |
| 1624 | mbedtls_zeroize_and_free(*buf, *n); |
| 1625 | |
| 1626 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1627 | } |
| 1628 | |
| 1629 | fclose(f); |
| 1630 | |
| 1631 | (*buf)[*n] = '\0'; |
| 1632 | |
| 1633 | if (strstr((const char *) *buf, "-----BEGIN ") != NULL) { |
| 1634 | ++*n; |
| 1635 | } |
| 1636 | |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Load and parse a private key |
| 1642 | */ |
| 1643 | int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, |
| 1644 | const char *path, const char *pwd, |
| 1645 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 1646 | { |
| 1647 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1648 | size_t n; |
| 1649 | unsigned char *buf; |
| 1650 | |
| 1651 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 1652 | return ret; |
| 1653 | } |
| 1654 | |
| 1655 | if (pwd == NULL) { |
| 1656 | ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng); |
| 1657 | } else { |
| 1658 | ret = mbedtls_pk_parse_key(ctx, buf, n, |
| 1659 | (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng); |
| 1660 | } |
| 1661 | |
| 1662 | mbedtls_zeroize_and_free(buf, n); |
| 1663 | |
| 1664 | return ret; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * Load and parse a public key |
| 1669 | */ |
| 1670 | int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path) |
| 1671 | { |
| 1672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1673 | size_t n; |
| 1674 | unsigned char *buf; |
| 1675 | |
| 1676 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | ret = mbedtls_pk_parse_public_key(ctx, buf, n); |
| 1681 | |
| 1682 | mbedtls_zeroize_and_free(buf, n); |
| 1683 | |
| 1684 | return ret; |
| 1685 | } |
| 1686 | #endif /* MBEDTLS_FS_IO */ |
| 1687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1688 | #endif /* MBEDTLS_PK_PARSE_C */ |