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 | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 853 | ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), p, end); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 854 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 856 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 858 | #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 859 | if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 860 | ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk); |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 861 | } else |
| 862 | #endif |
| 863 | { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 864 | ret = pk_use_ecparams(&alg_params, pk); |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 865 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | if (ret == 0) { |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 867 | 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] | 868 | *p += end - *p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 870 | } else |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 871 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 872 | ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | if (ret == 0 && *p != end) { |
| 875 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 876 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 877 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | if (ret != 0) { |
| 880 | mbedtls_pk_free(pk); |
| 881 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 886 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 887 | /* |
| 888 | * Parse a SEC1 encoded private EC key |
| 889 | */ |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 890 | static int pk_parse_key_sec1_der(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | const unsigned char *key, size_t keylen, |
| 892 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 893 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 894 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 895 | int version, pubkey_done; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 896 | size_t len, d_len; |
Leonid Rozenboim | a3008e7 | 2022-04-21 17:28:18 -0700 | [diff] [blame] | 897 | mbedtls_asn1_buf params = { 0, 0, NULL }; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 898 | unsigned char *p = (unsigned char *) key; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 899 | unsigned char *d; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 900 | unsigned char *end = p + keylen; |
| 901 | unsigned char *end2; |
| 902 | |
| 903 | /* |
| 904 | * RFC 5915, or SEC1 Appendix C.4 |
| 905 | * |
| 906 | * ECPrivateKey ::= SEQUENCE { |
| 907 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 908 | * privateKey OCTET STRING, |
| 909 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 910 | * publicKey [1] BIT STRING OPTIONAL |
| 911 | * } |
| 912 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 914 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 915 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | end = p + len; |
| 919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 920 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 921 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 922 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 924 | if (version != 1) { |
| 925 | return MBEDTLS_ERR_PK_KEY_INVALID_VERSION; |
| 926 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 929 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 930 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 931 | |
Valerio Setti | 6b062ee | 2023-06-30 17:32:57 +0200 | [diff] [blame] | 932 | /* Keep a reference to the position fo the private key. It will be used |
| 933 | * later in this function. */ |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 934 | d = p; |
| 935 | d_len = len; |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 936 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 937 | p += len; |
| 938 | |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 939 | pubkey_done = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | if (p != end) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 941 | /* |
| 942 | * Is 'parameters' present? |
| 943 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 944 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 945 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 946 | 0)) == 0) { |
| 947 | if ((ret = pk_get_ecparams(&p, p + len, ¶ms)) != 0 || |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 948 | (ret = pk_use_ecparams(¶ms, pk)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | return ret; |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 950 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | 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] | 953 | } |
Jethro Beekman | d2df936 | 2018-02-16 13:11:04 -0800 | [diff] [blame] | 954 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 955 | |
Manuel Pégourié-Gonnard | dcd98ff | 2023-07-25 11:58:31 +0200 | [diff] [blame] | 956 | /* |
| 957 | * Load the private key |
| 958 | */ |
| 959 | ret = pk_ecc_set_key(pk, d, d_len); |
| 960 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 6db11d5 | 2023-07-25 11:20:48 +0200 | [diff] [blame] | 961 | return ret; |
| 962 | } |
Valerio Setti | 6b062ee | 2023-06-30 17:32:57 +0200 | [diff] [blame] | 963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | if (p != end) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 965 | /* |
| 966 | * Is 'publickey' present? If not, or if we can't read it (eg because it |
| 967 | * is compressed), create it from the private key. |
| 968 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 970 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 971 | 1)) == 0) { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 972 | end2 = p + len; |
| 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) { |
| 975 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 976 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | if (p + len != end2) { |
| 979 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 980 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 981 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 982 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 983 | 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] | 984 | pubkey_done = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } else { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 986 | /* |
Manuel Pégourié-Gonnard | e4c883b | 2023-07-26 23:31:01 +0200 | [diff] [blame] | 987 | * 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] | 988 | * is if the point format is not recognized. |
| 989 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) { |
| 991 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 992 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 993 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | 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] | 996 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 997 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 998 | |
Valerio Setti | 34f6755 | 2023-04-03 15:19:18 +0200 | [diff] [blame] | 999 | if (!pubkey_done) { |
Manuel Pégourié-Gonnard | de25194 | 2023-07-26 22:33:58 +0200 | [diff] [blame] | 1000 | 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] | 1001 | return ret; |
Valerio Setti | 34f6755 | 2023-04-03 15:19:18 +0200 | [diff] [blame] | 1002 | } |
Manuel Pégourié-Gonnard | ff29f9c | 2013-09-18 16:13:02 +0200 | [diff] [blame] | 1003 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1006 | } |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1007 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1008 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1009 | /*********************************************************************** |
| 1010 | * |
| 1011 | * PKCS#8 parsing functions |
| 1012 | * |
| 1013 | **********************************************************************/ |
| 1014 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1015 | /* |
| 1016 | * Parse an unencrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1017 | * |
| 1018 | * Notes: |
| 1019 | * |
| 1020 | * - This function does not own the key buffer. It is the |
| 1021 | * responsibility of the caller to take care of zeroizing |
| 1022 | * and freeing it after use. |
| 1023 | * |
| 1024 | * - The function is responsible for freeing the provided |
| 1025 | * PK context on failure. |
| 1026 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1027 | */ |
| 1028 | static int pk_parse_key_pkcs8_unencrypted_der( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | mbedtls_pk_context *pk, |
| 1030 | const unsigned char *key, size_t keylen, |
| 1031 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1032 | { |
| 1033 | int ret, version; |
| 1034 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1035 | mbedtls_asn1_buf params; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1036 | unsigned char *p = (unsigned char *) key; |
| 1037 | unsigned char *end = p + keylen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1039 | 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] | 1040 | const mbedtls_pk_info_t *pk_info; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1041 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1042 | #if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 609ab64 | 2021-06-16 14:29:11 +0200 | [diff] [blame] | 1043 | (void) f_rng; |
| 1044 | (void) p_rng; |
| 1045 | #endif |
| 1046 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1047 | /* |
Hanno Becker | 9c6cb38 | 2017-09-05 10:08:01 +0100 | [diff] [blame] | 1048 | * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1049 | * |
| 1050 | * PrivateKeyInfo ::= SEQUENCE { |
| 1051 | * version Version, |
| 1052 | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
| 1053 | * privateKey PrivateKey, |
| 1054 | * attributes [0] IMPLICIT Attributes OPTIONAL } |
| 1055 | * |
| 1056 | * Version ::= INTEGER |
| 1057 | * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1058 | * PrivateKey ::= OCTET STRING |
| 1059 | * |
| 1060 | * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey |
| 1061 | */ |
| 1062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1064 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1065 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | end = p + len; |
| 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 1071 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Chris Jones | fdb588b | 2021-04-14 18:15:24 +0100 | [diff] [blame] | 1072 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | if (version != 0) { |
| 1075 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret); |
| 1076 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1077 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1078 | 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] | 1079 | return ret; |
| 1080 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1083 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1084 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | if (len < 1) { |
| 1087 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 1088 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 1089 | } |
| 1090 | |
| 1091 | if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) { |
| 1092 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1093 | } |
| 1094 | |
| 1095 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) { |
| 1096 | return ret; |
| 1097 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | if (pk_alg == MBEDTLS_PK_RSA) { |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 1101 | 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] | 1102 | mbedtls_pk_free(pk); |
| 1103 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1104 | } |
| 1105 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1106 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1107 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) { |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1109 | #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) |
Valerio Setti | 00e8dd1 | 2023-05-18 18:56:59 +0200 | [diff] [blame] | 1110 | if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1111 | if ((ret = |
| 1112 | pk_use_ecparams_rfc8410(¶ms, ec_grp_id, pk)) != 0 || |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1113 | (ret = |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1114 | pk_parse_key_rfc8410_der(pk, p, len, end, f_rng, |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1115 | p_rng)) != 0) { |
| 1116 | mbedtls_pk_free(pk); |
| 1117 | return ret; |
| 1118 | } |
| 1119 | } else |
| 1120 | #endif |
| 1121 | { |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1122 | if ((ret = pk_use_ecparams(¶ms, pk)) != 0 || |
| 1123 | (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] | 1124 | mbedtls_pk_free(pk); |
| 1125 | return ret; |
| 1126 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1127 | } |
| 1128 | } else |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1129 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1131 | |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1132 | end = p + len; |
| 1133 | if (end != (key + keylen)) { |
| 1134 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 1135 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1136 | } |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 1137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Parse an encrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1143 | * |
| 1144 | * To save space, the decryption happens in-place on the given key buffer. |
| 1145 | * Also, while this function may modify the keybuffer, it doesn't own it, |
| 1146 | * and instead it is the responsibility of the caller to zeroize and properly |
| 1147 | * free it after use. |
| 1148 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1151 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | mbedtls_pk_context *pk, |
| 1153 | unsigned char *key, size_t keylen, |
| 1154 | const unsigned char *pwd, size_t pwdlen, |
| 1155 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1156 | { |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1157 | int ret, decrypted = 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1158 | size_t len; |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1159 | unsigned char *buf; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1160 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1161 | mbedtls_asn1_buf pbe_alg_oid, pbe_params; |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1162 | #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] | 1163 | mbedtls_cipher_type_t cipher_alg; |
| 1164 | mbedtls_md_type_t md_alg; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1165 | #endif |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1166 | size_t outlen = 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1167 | |
Hanno Becker | 2aa80a7 | 2017-09-07 15:28:45 +0100 | [diff] [blame] | 1168 | p = key; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1169 | end = p + keylen; |
| 1170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1171 | if (pwdlen == 0) { |
| 1172 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1173 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1174 | |
| 1175 | /* |
Hanno Becker | f04111f | 2017-09-29 19:18:42 +0100 | [diff] [blame] | 1176 | * This function parses the EncryptedPrivateKeyInfo object (PKCS#8) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1177 | * |
| 1178 | * EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 1179 | * encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 1180 | * encryptedData EncryptedData |
| 1181 | * } |
| 1182 | * |
| 1183 | * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1184 | * |
| 1185 | * EncryptedData ::= OCTET STRING |
| 1186 | * |
| 1187 | * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1188 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1189 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1191 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1192 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | end = p + len; |
| 1196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) { |
| 1198 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1199 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1201 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1202 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1203 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1204 | |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1205 | buf = p; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1206 | |
| 1207 | /* |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1208 | * Decrypt EncryptedData with appropriate PBE |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1209 | */ |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1210 | #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] | 1211 | 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] | 1212 | if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT, |
Waleed Elmelegy | 5e48cad | 2023-09-12 14:52:48 +0100 | [diff] [blame] | 1213 | cipher_alg, md_alg, |
| 1214 | pwd, pwdlen, p, len, buf, len, &outlen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1215 | if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) { |
| 1216 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1217 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1219 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1220 | } |
Waleed Elmelegy | d527896 | 2023-09-12 14:42:49 +0100 | [diff] [blame] | 1221 | |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1222 | decrypted = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1223 | } else |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1224 | #endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */ |
| 1225 | #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] | 1226 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) { |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1227 | if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen, |
| 1228 | p, len, buf, len, &outlen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) { |
| 1230 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1231 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1234 | } |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1235 | |
| 1236 | decrypted = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | } else |
Valerio Setti | e581e14 | 2023-12-29 16:35:07 +0100 | [diff] [blame] | 1238 | #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] | 1239 | { |
| 1240 | ((void) pwd); |
Manuel Pégourié-Gonnard | 1032c1d | 2013-09-18 17:18:34 +0200 | [diff] [blame] | 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 | if (decrypted == 0) { |
| 1244 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1245 | } |
Waleed Elmelegy | c9f4040 | 2023-08-08 15:28:15 +0100 | [diff] [blame] | 1246 | 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] | 1247 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1248 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1249 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1250 | /*********************************************************************** |
| 1251 | * |
| 1252 | * Top-level functions, with format auto-discovery |
| 1253 | * |
| 1254 | **********************************************************************/ |
| 1255 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1256 | /* |
| 1257 | * Parse a private key |
| 1258 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | int mbedtls_pk_parse_key(mbedtls_pk_context *pk, |
| 1260 | const unsigned char *key, size_t keylen, |
| 1261 | const unsigned char *pwd, size_t pwdlen, |
| 1262 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1263 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1264 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | const mbedtls_pk_info_t *pk_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1267 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1268 | mbedtls_pem_context pem; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1269 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1271 | if (keylen == 0) { |
| 1272 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1273 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1274 | |
| 1275 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | mbedtls_pem_init(&pem); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1278 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1279 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1280 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1281 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1282 | } else { |
| 1283 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1284 | PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | key, pwd, pwdlen, &len); |
| 1286 | } |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | if (ret == 0) { |
| 1289 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1290 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 1291 | (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), |
Valerio Setti | fd49a46 | 2024-01-23 08:35:11 +0100 | [diff] [blame] | 1292 | pem.buf, pem.buflen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1293 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | mbedtls_pem_free(&pem); |
| 1297 | return ret; |
| 1298 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1299 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1300 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1301 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1302 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1303 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1304 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1306 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1307 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1308 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1309 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1310 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | } else { |
| 1312 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1313 | PEM_BEGIN_PRIVATE_KEY_EC, |
| 1314 | PEM_END_PRIVATE_KEY_EC, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | key, pwd, pwdlen, &len); |
| 1316 | } |
| 1317 | if (ret == 0) { |
| 1318 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1320 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1321 | (ret = pk_parse_key_sec1_der(pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1322 | pem.buf, pem.buflen, |
| 1323 | f_rng, p_rng)) != 0) { |
| 1324 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1327 | mbedtls_pem_free(&pem); |
| 1328 | return ret; |
| 1329 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1330 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1331 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1332 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1333 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1334 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1335 | } |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1336 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1337 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1338 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1339 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1340 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | } else { |
| 1342 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1343 | PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | key, NULL, 0, &len); |
| 1345 | } |
| 1346 | if (ret == 0) { |
| 1347 | if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk, |
| 1348 | pem.buf, pem.buflen, f_rng, p_rng)) != 0) { |
| 1349 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | mbedtls_pem_free(&pem); |
| 1353 | return ret; |
| 1354 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1355 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1356 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1359 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1361 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1362 | } else { |
| 1363 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1364 | PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8, |
| 1365 | PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1366 | key, NULL, 0, &len); |
| 1367 | } |
| 1368 | if (ret == 0) { |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1369 | if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen, |
| 1370 | pwd, pwdlen, f_rng, p_rng)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1371 | mbedtls_pk_free(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | mbedtls_pem_free(&pem); |
| 1375 | return ret; |
| 1376 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1377 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1378 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1379 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1380 | #else |
| 1381 | ((void) pwd); |
| 1382 | ((void) pwdlen); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1384 | |
| 1385 | /* |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 1386 | * At this point we only know it's not a PEM formatted key. Could be any |
| 1387 | * of the known DER encoded private key formats |
| 1388 | * |
| 1389 | * We try the different DER format parsers to see if one passes without |
| 1390 | * error |
| 1391 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | if (pwdlen != 0) { |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1394 | unsigned char *key_copy; |
| 1395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1396 | if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) { |
| 1397 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 1398 | } |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1400 | memcpy(key_copy, key, keylen); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1401 | |
Waleed Elmelegy | 1db5cda | 2023-09-20 18:00:48 +0100 | [diff] [blame] | 1402 | ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen, |
| 1403 | pwd, pwdlen, f_rng, p_rng); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1404 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 1405 | mbedtls_zeroize_and_free(key_copy, keylen); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1408 | if (ret == 0) { |
| 1409 | return 0; |
| 1410 | } |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | mbedtls_pk_free(pk); |
| 1413 | mbedtls_pk_init(pk); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1415 | if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) { |
| 1416 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1417 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1418 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng); |
| 1421 | if (ret == 0) { |
| 1422 | return 0; |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1423 | } |
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 | mbedtls_pk_free(pk); |
| 1426 | mbedtls_pk_init(pk); |
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 | #if defined(MBEDTLS_RSA_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 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1431 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 1432 | mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1433 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | mbedtls_pk_free(pk); |
| 1437 | mbedtls_pk_init(pk); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1439 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1440 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1441 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
| 1442 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 1443 | pk_parse_key_sec1_der(pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | key, keylen, f_rng, p_rng) == 0) { |
| 1445 | return 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1446 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1447 | mbedtls_pk_free(pk); |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1448 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
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 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] | 1451 | * it is ok to leave the PK context initialized but not |
| 1452 | * freed: It is the caller's responsibility to call pk_init() |
| 1453 | * before calling this function, and to call pk_free() |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 1454 | * 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] | 1455 | * isn't, this leads to mbedtls_pk_free() being called |
| 1456 | * twice, once here and once by the caller, but this is |
| 1457 | * also ok and in line with the mbedtls_pk_free() calls |
| 1458 | * on failed PEM parsing attempts. */ |
| 1459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Parse a public key |
| 1465 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, |
| 1467 | const unsigned char *key, size_t keylen) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1468 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1469 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1470 | unsigned char *p; |
Ron Eldor | 5472d43 | 2017-10-17 09:49:00 +0300 | [diff] [blame] | 1471 | #if defined(MBEDTLS_RSA_C) |
| 1472 | const mbedtls_pk_info_t *pk_info; |
| 1473 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1474 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1475 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | mbedtls_pem_context pem; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1477 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | if (keylen == 0) { |
| 1480 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1481 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1482 | |
| 1483 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1484 | mbedtls_pem_init(&pem); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1485 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1486 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1488 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | } else { |
| 1490 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1491 | PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1492 | key, NULL, 0, &len); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1493 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | |
| 1495 | if (ret == 0) { |
| 1496 | p = pem.buf; |
| 1497 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1498 | mbedtls_pem_free(&pem); |
| 1499 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1500 | } |
| 1501 | |
| 1502 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1503 | mbedtls_pem_free(&pem); |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 1507 | if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), &p, p + pem.buflen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | mbedtls_pk_free(ctx); |
| 1509 | } |
| 1510 | |
| 1511 | mbedtls_pem_free(&pem); |
| 1512 | return ret; |
| 1513 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1514 | mbedtls_pem_free(&pem); |
| 1515 | return ret; |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1516 | } |
| 1517 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1518 | |
| 1519 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1520 | if (key[keylen - 1] != '\0') { |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1521 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | } else { |
| 1523 | ret = mbedtls_pem_read_buffer(&pem, |
Valerio Setti | 854c737 | 2023-11-28 08:37:57 +0100 | [diff] [blame] | 1524 | PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | key, NULL, 0, &len); |
| 1526 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | if (ret == 0) { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1529 | /* |
| 1530 | * Was PEM encoded |
| 1531 | */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1532 | p = pem.buf; |
| 1533 | |
Jethro Beekman | 0167244 | 2023-04-19 14:08:14 +0200 | [diff] [blame] | 1534 | ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | mbedtls_pem_free(&pem); |
| 1536 | return ret; |
| 1537 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1538 | mbedtls_pem_free(&pem); |
| 1539 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1540 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | mbedtls_pem_free(&pem); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1543 | |
| 1544 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1546 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1547 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | |
| 1549 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1550 | return ret; |
| 1551 | } |
| 1552 | |
| 1553 | p = (unsigned char *) key; |
Valerio Setti | 135ebde | 2024-02-01 17:00:29 +0100 | [diff] [blame^] | 1554 | ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), &p, p + keylen); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | if (ret == 0) { |
| 1556 | return ret; |
| 1557 | } |
| 1558 | mbedtls_pk_free(ctx); |
Valerio Setti | dccfd36 | 2024-01-23 17:07:59 +0100 | [diff] [blame] | 1559 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1560 | return ret; |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1561 | } |
| 1562 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1563 | p = (unsigned char *) key; |
| 1564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | return ret; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
Manuel Pégourié-Gonnard | 212517b | 2023-07-26 12:05:38 +0200 | [diff] [blame] | 1570 | /*********************************************************************** |
| 1571 | * |
| 1572 | * Top-level functions, with filesystem support |
| 1573 | * |
| 1574 | **********************************************************************/ |
| 1575 | |
| 1576 | #if defined(MBEDTLS_FS_IO) |
| 1577 | /* |
| 1578 | * Load all data from a file into a given buffer. |
| 1579 | * |
| 1580 | * The file is expected to contain either PEM or DER encoded data. |
| 1581 | * A terminating null byte is always appended. It is included in the announced |
| 1582 | * length only if the data looks like it is PEM encoded. |
| 1583 | */ |
| 1584 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n) |
| 1585 | { |
| 1586 | FILE *f; |
| 1587 | long size; |
| 1588 | |
| 1589 | if ((f = fopen(path, "rb")) == NULL) { |
| 1590 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1591 | } |
| 1592 | |
| 1593 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
| 1594 | mbedtls_setbuf(f, NULL); |
| 1595 | |
| 1596 | fseek(f, 0, SEEK_END); |
| 1597 | if ((size = ftell(f)) == -1) { |
| 1598 | fclose(f); |
| 1599 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1600 | } |
| 1601 | fseek(f, 0, SEEK_SET); |
| 1602 | |
| 1603 | *n = (size_t) size; |
| 1604 | |
| 1605 | if (*n + 1 == 0 || |
| 1606 | (*buf = mbedtls_calloc(1, *n + 1)) == NULL) { |
| 1607 | fclose(f); |
| 1608 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 1609 | } |
| 1610 | |
| 1611 | if (fread(*buf, 1, *n, f) != *n) { |
| 1612 | fclose(f); |
| 1613 | |
| 1614 | mbedtls_zeroize_and_free(*buf, *n); |
| 1615 | |
| 1616 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 1617 | } |
| 1618 | |
| 1619 | fclose(f); |
| 1620 | |
| 1621 | (*buf)[*n] = '\0'; |
| 1622 | |
| 1623 | if (strstr((const char *) *buf, "-----BEGIN ") != NULL) { |
| 1624 | ++*n; |
| 1625 | } |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Load and parse a private key |
| 1632 | */ |
| 1633 | int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, |
| 1634 | const char *path, const char *pwd, |
| 1635 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| 1636 | { |
| 1637 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1638 | size_t n; |
| 1639 | unsigned char *buf; |
| 1640 | |
| 1641 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | if (pwd == NULL) { |
| 1646 | ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng); |
| 1647 | } else { |
| 1648 | ret = mbedtls_pk_parse_key(ctx, buf, n, |
| 1649 | (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng); |
| 1650 | } |
| 1651 | |
| 1652 | mbedtls_zeroize_and_free(buf, n); |
| 1653 | |
| 1654 | return ret; |
| 1655 | } |
| 1656 | |
| 1657 | /* |
| 1658 | * Load and parse a public key |
| 1659 | */ |
| 1660 | int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path) |
| 1661 | { |
| 1662 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1663 | size_t n; |
| 1664 | unsigned char *buf; |
| 1665 | |
| 1666 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 1667 | return ret; |
| 1668 | } |
| 1669 | |
| 1670 | ret = mbedtls_pk_parse_public_key(ctx, buf, n); |
| 1671 | |
| 1672 | mbedtls_zeroize_and_free(buf, n); |
| 1673 | |
| 1674 | return ret; |
| 1675 | } |
| 1676 | #endif /* MBEDTLS_FS_IO */ |
| 1677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1678 | #endif /* MBEDTLS_PK_PARSE_C */ |