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