blob: e4a812a6340c72d76890d380f16815b5249524ac [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1a7550a2013-09-15 13:01:22 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pk.h"
13#include "mbedtls/asn1.h"
14#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050015#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020016#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Tomi Fontanilles851d8df2023-12-19 15:44:52 +020018#include "mbedtls/ecp.h"
Valerio Setti3cc486a2023-11-30 08:09:47 +010019#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <string.h>
22
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020023#if defined(MBEDTLS_USE_PSA_CRYPTO)
24#include "mbedtls/psa_util.h"
25#include "psa/crypto.h"
26#endif
27
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020028/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Valerio Settib328c442024-01-23 10:48:45 +010031#include "rsa_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020032#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020033
34/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
44
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020045#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
46
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020047/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020048 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020049 * 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é-Gonnardd1aa6422023-07-26 22:24:23 +020059 *
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 Bakker1a7550a2013-09-15 13:01:22 +020063 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +010064int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020065{
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 Setti3bfad3a2024-02-01 11:28:27 +010098 * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group().
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020099 * out: will have the private key set.
100 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200101 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100102int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200103{
104#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Settifbbafa02023-12-06 10:07:34 +0100106 psa_key_usage_t flags;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200107 psa_status_t status;
108
109 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Settifbbafa02023-12-06 10:07:34 +0100110 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é-Gonnard54708982023-07-26 15:38:36 +0200121 }
122 psa_set_key_usage_flags(&attributes, flags);
123
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200124 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200125 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é-Gonnardd1aa6422023-07-26 22:24:23 +0200130 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200131 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é-Gonnardde251942023-07-26 22:33:58 +0200139 * Derive a public key from its private counterpart.
140 * Computationally intensive, only use when public key is not available.
141 *
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100142 * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key().
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200143 * 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é-Gonnard54708982023-07-26 15:38:36 +0200146 *
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é-Gonnard94cf1f82023-08-02 12:09:24 +0200150 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200151 *
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é-Gonnard54708982023-07-26 15:38:36 +0200156 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100157int 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é-Gonnard54708982023-07-26 15:38:36 +0200160{
161#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200162
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200163 (void) f_rng;
164 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200165 (void) prv;
166 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200167 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200168
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é-Gonnard0b8e4562023-07-26 22:43:25 +0200172
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200173#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200174
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200175 (void) f_rng;
176 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200177 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200178
179 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200180 size_t curve_bits;
181 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200182
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200183 /* 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é-Gonnard54708982023-07-26 15:38:36 +0200186 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é-Gonnardde251942023-07-26 22:33:58 +0200188 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200189 if (status != PSA_SUCCESS) {
190 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200191 }
192
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200193 /* 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é-Gonnard54708982023-07-26 15:38:36 +0200200 } else if (destruction_status != PSA_SUCCESS) {
201 return psa_pk_status_to_mbedtls(destruction_status);
202 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200203
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é-Gonnard54708982023-07-26 15:38:36 +0200207#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200208
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200209 (void) prv;
210 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200211
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200212 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200213 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200214
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200215#endif /* MBEDTLS_USE_PSA_CRYPTO */
216}
217
218#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
219/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200220 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200221 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200222 * 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 Setti3bfad3a2024-02-01 11:28:27 +0100228 * [in/out] pk: in: must have the group set, see mbedtls_pk_ecc_set_group().
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200229 * 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é-Gonnardfac98192023-07-27 09:19:42 +0200232 *
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é-Gonnard54708982023-07-26 15:38:36 +0200238 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200239static 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é-Gonnard54708982023-07-26 15:38:36 +0200242{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200243#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200244 (void) pk;
245 (void) pub;
246 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200247 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200248#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200249 mbedtls_ecp_keypair ecp_key;
250 mbedtls_ecp_group_id ecp_group_id;
251 int ret;
252
Valerio Settid36c3132023-12-21 14:03:51 +0100253 ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200254
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é-Gonnard842ffc52023-08-02 12:10:51 +0200258 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200259 }
260 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200261 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200262 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é-Gonnard681e30b2023-07-26 23:03:35 +0200267 &pk->pub_raw_len, pk->pub_raw,
268 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200269
270exit:
271 mbedtls_ecp_keypair_free(&ecp_key);
272 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200273#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
274}
275#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
276
277/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200278 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200279 *
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100280 * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group().
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200281 * out: will have the public key set.
282 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200283 *
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é-Gonnard54708982023-07-26 15:38:36 +0200289 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100290int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200291{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200292#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200293
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200294 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200295 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é-Gonnardff72ea92023-07-26 23:56:05 +0200299 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200300 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
301 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200302 memcpy(pk->pub_raw, pub, pub_len);
303 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200304 } 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é-Gonnardeab20d22014-03-14 17:58:42 +0100310 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200311
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200312 /* 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é-Gonnard54708982023-07-26 15:38:36 +0200316 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200317 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é-Gonnardff72ea92023-07-26 23:56:05 +0200321 &key_id) != PSA_SUCCESS) ||
322 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
323 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200325
326 return 0;
327
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200328#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200329
330 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200331 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200332 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
333 if (ret != 0) {
334 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200336 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
337
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200338#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200339}
340
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200341/***********************************************************************
342 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200343 * 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é-Gonnardf1b76332023-08-02 12:14:19 +0200346 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200347 * - 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é-Gonnard54708982023-07-26 15:38:36 +0200355 *
356 **********************************************************************/
357
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200358#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
359/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200360static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200361{
362 (void) tag;
363 return 0;
364}
365
366/* See the "real" version for documentation */
367static 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é-Gonnardf1b76332023-08-02 12:14:19 +0200379static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200380{
381 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
382}
383
Paul Bakker1a7550a2013-09-15 13:01:22 +0200384/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385 * 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é-Gonnard12ea63a2023-07-27 12:20:16 +0200387 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388 * 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 Peskine449bd832023-01-11 14:50:10 +0100403static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404{
Janos Follath24eed8d2019-11-22 13:21:35 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100406 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200407 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 const unsigned char *end_field, *end_curve;
409 size_t len;
410 int ver;
411
412 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 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é-Gonnardeab20d22014-03-14 17:58:42 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (ver < 1 || ver > 3) {
418 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
419 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100420
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 Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
428 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
429 return ret;
430 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
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 Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
443 return ret;
444 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 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é-Gonnardeab20d22014-03-14 17:58:42 +0100449 }
450
451 p += len;
452
453 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 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é-Gonnardeab20d22014-03-14 17:58:42 +0100457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 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é-Gonnardeab20d22014-03-14 17:58:42 +0100464
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 Peskine449bd832023-01-11 14:50:10 +0100474 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
475 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
476 return ret;
477 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100478
479 end_curve = p + len;
480
481 /*
482 * FieldElement ::= OCTET STRING
483 * containing an integer in the case of a prime field
484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 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é-Gonnardeab20d22014-03-14 17:58:42 +0100488 }
489
490 p += len;
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 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é-Gonnardeab20d22014-03-14 17:58:42 +0100495 }
496
497 p += len;
498
499 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100501 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 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é-Gonnardeab20d22014-03-14 17:58:42 +0100508
509 /*
510 * ECPoint ::= OCTET STRING
511 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 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é-Gonnardeab20d22014-03-14 17:58:42 +0100515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
517 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100518 /*
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 Peskine449bd832023-01-11 14:50:10 +0100522 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é-Gonnard5246ee52014-03-19 16:18:38 +0100529 }
530 }
531
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100532 p += len;
533
534 /*
535 * order INTEGER
536 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 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é-Gonnardeab20d22014-03-14 17:58:42 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100542
543 /*
544 * Allow optional elements by purposefully not enforcing p == end here.
545 */
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548}
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 Peskine449bd832023-01-11 14:50:10 +0100554static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100555{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100556 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_ecp_group ref;
558 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100563 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 mbedtls_ecp_group_free(&ref);
565 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100566
567 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 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é-Gonnardeab20d22014-03-14 17:58:42 +0100575 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100577 break;
578 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100579 }
580
581cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100583
584 *grp_id = *id;
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100591}
592
593/*
594 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
595 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200596static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
597 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100598{
Janos Follath24eed8d2019-11-22 13:21:35 +0000599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100605 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100609
610cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000611 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200612 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000613 * 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 Galanakisc8e381a2023-01-19 16:08:34 +0000618 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é-Gonnardeab20d22014-03-14 17:58:42 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100627
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200628/***********************************************************************
629 *
630 * Unsorted (yet!) from this point on until the next section header
631 *
632 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200633
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200634/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200635 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200636 * ECParameters ::= CHOICE {
637 * namedCurve OBJECT IDENTIFIER
638 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
639 * -- implicitCurve NULL
640 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200641 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200642static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
643 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200644{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200646
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200647 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é-Gonnarddcd98ff2023-07-25 11:58:31 +0200650 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200651
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200652 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200653 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200654 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200655 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
657 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
658 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200660 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
662 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200663
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é-Gonnarddcd98ff2023-07-25 11:58:31 +0200672 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200673}
674
675/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100677 *
678 * ECParameters ::= CHOICE {
679 * namedCurve OBJECT IDENTIFIER
680 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
681 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200683static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200684{
Janos Follath24eed8d2019-11-22 13:21:35 +0000685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 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é-Gonnard12ea63a2023-07-27 12:20:16 +0200693 ret = pk_ecc_group_id_from_specified(params, &grp_id);
694 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return ret;
696 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100697 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100699 return mbedtls_pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700}
701
Jethro Beekman01672442023-04-19 14:08:14 +0200702#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
703
704/*
705 * Load an RFC8410 EC key, which doesn't have any parameters
706 */
707static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
708 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200709 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200710{
711 if (params->tag != 0 || params->len != 0) {
712 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
713 }
714
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100715 return mbedtls_pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200716}
717
718/*
719 * Parse an RFC 8410 encoded private EC key
720 *
721 * CurvePrivateKey ::= OCTET STRING
722 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200723static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200724 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é-Gonnarddcd98ff2023-07-25 11:58:31 +0200738 /*
739 * Load the private key
740 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100741 ret = mbedtls_pk_ecc_set_key(pk, key, len);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200742 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200743 return ret;
744 }
Jethro Beekman01672442023-04-19 14:08:14 +0200745
Valerio Setti4064dbb2023-05-17 15:33:07 +0200746 /* 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 Setti3bfad3a2024-02-01 11:28:27 +0100749 if ((ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200750 return ret;
751 }
752
Jethro Beekman01672442023-04-19 14:08:14 +0200753 return 0;
754}
755#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200756
Valerio Setti81d75122023-06-14 14:49:33 +0200757#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759/* Get a PK algorithm identifier
760 *
761 * AlgorithmIdentifier ::= SEQUENCE {
762 * algorithm OBJECT IDENTIFIER,
763 * parameters ANY DEFINED BY algorithm OPTIONAL }
764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765static int pk_get_pk_alg(unsigned char **p,
766 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200767 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
768 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769{
Janos Follath24eed8d2019-11-22 13:21:35 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 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 Bakker1a7550a2013-09-15 13:01:22 +0200778
Jethro Beekman01672442023-04-19 14:08:14 +0200779 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200780#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200781 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 Peskine449bd832023-01-11 14:50:10 +0100791 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
792 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793
794 /*
795 * No parameters with RSA (only for EC)
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 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 Bakker1a7550a2013-09-15 13:01:22 +0200801 }
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804}
805
806/*
807 * SubjectPublicKeyInfo ::= SEQUENCE {
808 * algorithm AlgorithmIdentifier,
809 * subjectPublicKey BIT STRING }
810 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
812 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813{
Janos Follath24eed8d2019-11-22 13:21:35 +0000814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_asn1_buf alg_params;
817 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200818 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 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 Bakker1a7550a2013-09-15 13:01:22 +0200824 }
825
826 end = *p + len;
827
Jethro Beekman01672442023-04-19 14:08:14 +0200828 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return ret;
830 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
834 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (*p + len != end) {
837 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
838 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
839 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
842 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
843 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
846 return ret;
847 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti201e6432024-02-01 17:19:37 +0100851 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), *p, (size_t) (end - *p));
Valerio Setti5922cb92024-02-02 09:21:25 +0100852 if (ret == 0) {
853 /* On success all the input has been consumed by the parsing function. */
854 *p += end - *p;
Valerio Setti5a198922024-02-02 13:59:51 +0100855 } else if ((ret <= MBEDTLS_ERR_ASN1_OUT_OF_DATA) &&
856 (ret >= MBEDTLS_ERR_ASN1_BUF_TOO_SMALL)) {
Valerio Setti5922cb92024-02-02 09:21:25 +0100857 /* 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 Bakker1a7550a2013-09-15 13:01:22 +0200862 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200864#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200866#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200867 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200868 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200869 } else
870#endif
871 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200872 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200873 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (ret == 0) {
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100875 ret = mbedtls_pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200876 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200879#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (ret == 0 && *p != end) {
883 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
884 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
885 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (ret != 0) {
888 mbedtls_pk_free(pk);
889 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892}
893
Valerio Setti81d75122023-06-14 14:49:33 +0200894#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895/*
896 * Parse a SEC1 encoded private EC key
897 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200898static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 const unsigned char *key, size_t keylen,
900 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901{
Janos Follath24eed8d2019-11-22 13:21:35 +0000902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100903 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +0200904 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700905 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +0200907 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 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 Peskine449bd832023-01-11 14:50:10 +0100921 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 Bakker1a7550a2013-09-15 13:01:22 +0200924 }
925
926 end = p + len;
927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
929 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (version != 1) {
933 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
934 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 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 Bakker1a7550a2013-09-15 13:01:22 +0200939
Valerio Setti6b062ee2023-06-30 17:32:57 +0200940 /* Keep a reference to the position fo the private key. It will be used
941 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +0200942 d = p;
943 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200944
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945 p += len;
946
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200947 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949 /*
950 * Is 'parameters' present?
951 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 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, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +0200956 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200958 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100961 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800962 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200963
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200964 /*
965 * Load the private key
966 */
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100967 ret = mbedtls_pk_ecc_set_key(pk, d, d_len);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200968 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +0200969 return ret;
970 }
Valerio Setti6b062ee2023-06-30 17:32:57 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200973 /*
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 Peskine449bd832023-01-11 14:50:10 +0100977 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
978 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
979 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200980 end2 = p + len;
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 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é-Gonnard924cd102015-04-14 11:18:04 +0200985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 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é-Gonnard924cd102015-04-14 11:18:04 +0200990
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100991 if ((ret = mbedtls_pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200992 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200994 /*
Valerio Setti3bfad3a2024-02-01 11:28:27 +0100995 * The only acceptable failure mode of mbedtls_pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200996 * is if the point format is not recognized.
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
999 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1000 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001001 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001004 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001006
Valerio Setti34f67552023-04-03 15:19:18 +02001007 if (!pubkey_done) {
Valerio Setti3bfad3a2024-02-01 11:28:27 +01001008 if ((ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001009 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001010 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001011 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014}
Valerio Setti81d75122023-06-14 14:49:33 +02001015#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001017/***********************************************************************
1018 *
1019 * PKCS#8 parsing functions
1020 *
1021 **********************************************************************/
1022
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023/*
1024 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001025 *
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 Bakker1a7550a2013-09-15 13:01:22 +02001035 */
1036static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 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 Bakker1a7550a2013-09-15 13:01:22 +02001040{
1041 int ret, version;
1042 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 unsigned char *p = (unsigned char *) key;
1045 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001047 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
Valerio Setti81d75122023-06-14 14:49:33 +02001050#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001051 (void) f_rng;
1052 (void) p_rng;
1053#endif
1054
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001056 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 *
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 Peskine449bd832023-01-11 14:50:10 +01001071 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 Bakker1a7550a2013-09-15 13:01:22 +02001074 }
1075
1076 end = p + len;
1077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1079 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001080 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (version != 0) {
1083 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1084 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085
Jethro Beekman01672442023-04-19 14:08:14 +02001086 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return ret;
1088 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 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 Bakker1a7550a2013-09-15 13:01:22 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 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 Bakker1a7550a2013-09-15 13:01:22 +02001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001109 if ((ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), p, len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_pk_free(pk);
1111 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 }
1113 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001115#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001117#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001118 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001119 if ((ret =
1120 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001121 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001122 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001123 p_rng)) != 0) {
1124 mbedtls_pk_free(pk);
1125 return ret;
1126 }
1127 } else
1128#endif
1129 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001130 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1131 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001132 mbedtls_pk_free(pk);
1133 return ret;
1134 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 }
1136 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001137#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001140 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 Elmelegyd5278962023-09-12 14:42:49 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147}
1148
1149/*
1150 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001151 *
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 Bakker1a7550a2013-09-15 13:01:22 +02001157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001159MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 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 Bakker1a7550a2013-09-15 13:01:22 +02001164{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001165 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001167 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001170#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_cipher_type_t cipher_alg;
1172 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001174 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Hanno Becker2aa80a72017-09-07 15:28:45 +01001176 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 end = p + keylen;
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (pwdlen == 0) {
1180 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1181 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
1183 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001184 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185 *
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 Beckerb8d16572017-09-07 15:29:01 +01001196 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 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 Bakker1a7550a2013-09-15 13:01:22 +02001201 }
1202
1203 end = p + len;
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 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 Bakker1a7550a2013-09-15 13:01:22 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 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 Bakker1a7550a2013-09-15 13:01:22 +02001212
Hanno Beckerfab35692017-08-25 13:38:26 +01001213 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214
1215 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001216 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 */
Valerio Settie581e142023-12-29 16:35:07 +01001218#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001220 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001221 cipher_alg, md_alg,
1222 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1224 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1225 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001229
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001230 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 } else
Valerio Settie581e142023-12-29 16:35:07 +01001232#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 Peskine449bd832023-01-11 14:50:10 +01001234 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001235 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1236 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1238 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1239 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001243
1244 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 } else
Valerio Settie581e142023-12-29 16:35:07 +01001246#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001247 {
1248 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001249 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (decrypted == 0) {
1252 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1253 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001254 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001258/***********************************************************************
1259 *
1260 * Top-level functions, with format auto-discovery
1261 *
1262 **********************************************************************/
1263
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264/*
1265 * Parse a private key
1266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267int 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 Bakker1a7550a2013-09-15 13:01:22 +02001271{
Janos Follath24eed8d2019-11-22 13:21:35 +00001272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001277#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (keylen == 0) {
1280 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1281 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001282
1283#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001287 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001289 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 } else {
1291 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001292 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 key, pwd, pwdlen, &len);
1294 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 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 Setti135ebde2024-02-01 17:00:29 +01001299 (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk),
Valerio Settifd49a462024-01-23 08:35:11 +01001300 pem.buf, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 }
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 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 Bakker1a7550a2013-09-15 13:01:22 +02001312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314
Valerio Setti81d75122023-06-14 14:49:33 +02001315#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001316 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001318 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 } else {
1320 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001321 PEM_BEGIN_PRIVATE_KEY_EC,
1322 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 key, pwd, pwdlen, &len);
1324 }
1325 if (ret == 0) {
1326 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001329 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 pem.buf, pem.buflen,
1331 f_rng, p_rng)) != 0) {
1332 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 }
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 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 Bakker1a7550a2013-09-15 13:01:22 +02001343 }
Valerio Setti81d75122023-06-14 14:49:33 +02001344#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001346 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001348 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 } else {
1350 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001351 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 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 Bakker1a7550a2013-09-15 13:01:22 +02001358 }
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 mbedtls_pem_free(&pem);
1361 return ret;
1362 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1363 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001367 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001369 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 } else {
1371 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001372 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1373 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 key, NULL, 0, &len);
1375 }
1376 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001377 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1378 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 }
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 mbedtls_pem_free(&pem);
1383 return ret;
1384 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1385 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388#else
1389 ((void) pwd);
1390 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
1393 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001394 * 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001402 unsigned char *key_copy;
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1405 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1406 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001409
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001410 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1411 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001412
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001413 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 }
1415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (ret == 0) {
1417 return 0;
1418 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 mbedtls_pk_free(pk);
1421 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1424 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1429 if (ret == 0) {
1430 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001431 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 mbedtls_pk_free(pk);
1434 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1439 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti135ebde2024-02-01 17:00:29 +01001440 mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 }
1443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 mbedtls_pk_free(pk);
1445 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Valerio Setti81d75122023-06-14 14:49:33 +02001448#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1450 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001451 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 key, keylen, f_rng, p_rng) == 0) {
1453 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001456#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
Valerio Setti81d75122023-06-14 14:49:33 +02001458 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001459 * 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 Setti81d75122023-06-14 14:49:33 +02001462 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001463 * 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 Peskine449bd832023-01-11 14:50:10 +01001468 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469}
1470
1471/*
1472 * Parse a public key
1473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1475 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476{
Janos Follath24eed8d2019-11-22 13:21:35 +00001477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001479#if defined(MBEDTLS_RSA_C)
1480 const mbedtls_pk_info_t *pk_info;
1481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001485#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (keylen == 0) {
1488 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1489 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001490
1491#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001493#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001494 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001496 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 } else {
1498 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001499 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001501 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001502
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 Setti201e6432024-02-01 17:19:37 +01001515 if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 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 Eldord0c56de2017-10-10 17:03:08 +03001524 }
1525#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001526
1527 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001529 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 } else {
1531 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001532 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 key, NULL, 0, &len);
1534 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537 /*
1538 * Was PEM encoded
1539 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001540 p = pem.buf;
1541
Jethro Beekman01672442023-04-19 14:08:14 +02001542 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 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 Bakker1a7550a2013-09-15 13:01:22 +02001548 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001551
1552#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1554 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001556
1557 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1558 return ret;
1559 }
1560
1561 p = (unsigned char *) key;
Valerio Setti201e6432024-02-01 17:19:37 +01001562 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, keylen);
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (ret == 0) {
1564 return ret;
1565 }
1566 mbedtls_pk_free(ctx);
Valerio Settidccfd362024-01-23 17:07:59 +01001567 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001569 }
1570#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571 p = (unsigned char *) key;
1572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576}
1577
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001578/***********************************************************************
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 */
1592int 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 */
1641int 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 */
1668int 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_PK_PARSE_C */