blob: 5a3d3b25908453a14ce342583d45d4d35c619159 [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 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020064static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
65{
66#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
67 size_t ec_bits;
68 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
69
70 /* group may already be initialized; if so, make sure IDs match */
71 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
72 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
73 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
74 }
75
76 /* set group */
77 pk->ec_family = ec_family;
78 pk->ec_bits = ec_bits;
79
80 return 0;
81#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
82 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
83
84 /* grp may already be initialized; if so, make sure IDs match */
85 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
86 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
87 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
88 }
89
90 /* set group */
91 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
92#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
93}
94
95/*
96 * Set the private key material
97 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020098 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
99 * out: will have the private key set.
100 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200101 */
102static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200103 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200104{
105#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Settifbbafa02023-12-06 10:07:34 +0100107 psa_key_usage_t flags;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200108 psa_status_t status;
109
110 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Settifbbafa02023-12-06 10:07:34 +0100111 if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
112 /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
113 * the PK module cannot do ECDH. When the key will be used in TLS for
114 * ECDH, it will be exported and then re-imported with proper flags
115 * and algorithm. */
116 flags = PSA_KEY_USAGE_EXPORT;
117 } else {
118 psa_set_key_algorithm(&attributes,
119 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
120 flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
121 PSA_KEY_USAGE_EXPORT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200122 }
123 psa_set_key_usage_flags(&attributes, flags);
124
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200125 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200126 return psa_pk_status_to_mbedtls(status);
127
128#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
129
130 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200131 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200132 if (ret != 0) {
133 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
134 }
135 return 0;
136#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
137}
138
139/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200140 * Derive a public key from its private counterpart.
141 * Computationally intensive, only use when public key is not available.
142 *
143 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
144 * out: will have the public key set.
145 * [in] prv, prv_len: the raw private key (see note below).
146 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200147 *
148 * Note: the private key information is always available from pk,
149 * however for convenience the serialized version is also passed,
150 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200151 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200152 *
153 * There are three implementations of this function:
154 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
155 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
156 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200157 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200158static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
159 const unsigned char *prv, size_t prv_len,
160 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200161{
162#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200163
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200164 (void) f_rng;
165 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200166 (void) prv;
167 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200168 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200169
170 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
171 &pk->pub_raw_len);
172 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200173
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200174#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200175
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200176 (void) f_rng;
177 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200178 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200179
180 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200181 size_t curve_bits;
182 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200183
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200184 /* Import private key into PSA, from serialized input */
185 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
186 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200187 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
188 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200189 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200190 if (status != PSA_SUCCESS) {
191 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200192 }
193
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200194 /* Export public key from PSA */
195 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
196 size_t pub_len;
197 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
198 psa_status_t destruction_status = psa_destroy_key(key_id);
199 if (status != PSA_SUCCESS) {
200 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200201 } else if (destruction_status != PSA_SUCCESS) {
202 return psa_pk_status_to_mbedtls(destruction_status);
203 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200204
205 /* Load serialized public key into ecp_keypair structure */
206 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
207
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200208#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200209
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200210 (void) prv;
211 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200212
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200213 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200214 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 +0200215
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200216#endif /* MBEDTLS_USE_PSA_CRYPTO */
217}
218
219#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
220/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200221 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200222 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200223 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
224 * functions to handle keys. However, currently psa_import_key() does not
225 * support compressed points. In case that support was explicitly requested,
226 * this fallback uses ECP functions to get the job done. This is the reason
227 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
228 *
229 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
230 * out: will have the public key set.
231 * [in] pub, pub_len: the public key as an ECPoint,
232 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200233 *
234 * Return:
235 * - 0 on success;
236 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
237 * but not supported;
238 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200239 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200240static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
241 const unsigned char *pub,
242 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200243{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200244#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200245 (void) pk;
246 (void) pub;
247 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200248 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200249#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200250 mbedtls_ecp_keypair ecp_key;
251 mbedtls_ecp_group_id ecp_group_id;
252 int ret;
253
Valerio Settid36c3132023-12-21 14:03:51 +0100254 ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200255
256 mbedtls_ecp_keypair_init(&ecp_key);
257 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
258 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200259 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200260 }
261 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200262 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200263 if (ret != 0) {
264 goto exit;
265 }
266 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
267 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200268 &pk->pub_raw_len, pk->pub_raw,
269 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200270
271exit:
272 mbedtls_ecp_keypair_free(&ecp_key);
273 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200274#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
275}
276#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
277
278/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200279 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200280 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200281 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
282 * out: will have the public key set.
283 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200284 *
285 * Return:
286 * - 0 on success;
287 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
288 * but not supported;
289 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200290 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200291static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
292 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200293{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200294#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200295
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200296 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200297 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
298 /* Format directly supported by PSA:
299 * - non-Weierstrass curves that only have one format;
300 * - uncompressed format for Weierstrass curves. */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200301 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200302 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
303 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200304 memcpy(pk->pub_raw, pub, pub_len);
305 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200306 } else {
307 /* Other format, try the fallback */
308 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
309 if (ret != 0) {
310 return ret;
311 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200313
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200314 /* Validate the key by trying to import it */
315 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
316 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
317
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200318 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200319 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
320 psa_set_key_bits(&key_attrs, pk->ec_bits);
321
322 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200323 &key_id) != PSA_SUCCESS) ||
324 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
325 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200327
328 return 0;
329
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200330#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200331
332 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200333 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200334 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
335 if (ret != 0) {
336 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200338 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
339
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200340#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200341}
342
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200343/***********************************************************************
344 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200345 * Low-level ECC parsing: optional support for SpecifiedECDomain
346 *
347 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200348 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200349 * - pk_ecc_group_id_from_specified()
350 *
351 * All the other functions are internal to this section.
352 *
353 * The two "public" functions have a dummy variant provided
354 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
355 * abstraction layer for this macro, which should not appear outside
356 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200357 *
358 **********************************************************************/
359
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200360#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
361/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200362static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200363{
364 (void) tag;
365 return 0;
366}
367
368/* See the "real" version for documentation */
369static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
370 mbedtls_ecp_group_id *grp_id)
371{
372 (void) params;
373 (void) grp_id;
374 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
375}
376#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
377/*
378 * Tell if the passed tag might be the start of SpecifiedECDomain
379 * (that is, a sequence).
380 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200381static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200382{
383 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
384}
385
Paul Bakker1a7550a2013-09-15 13:01:22 +0200386/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
388 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200389 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390 * if it was encoded compressed.
391 *
392 * SpecifiedECDomain ::= SEQUENCE {
393 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
394 * fieldID FieldID {{FieldTypes}},
395 * curve Curve,
396 * base ECPoint,
397 * order INTEGER,
398 * cofactor INTEGER OPTIONAL,
399 * hash HashAlgorithm OPTIONAL,
400 * ...
401 * }
402 *
403 * We only support prime-field as field type, and ignore hash and cofactor.
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100406{
Janos Follath24eed8d2019-11-22 13:21:35 +0000407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200409 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410 const unsigned char *end_field, *end_curve;
411 size_t len;
412 int ver;
413
414 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (ver < 1 || ver > 3) {
420 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
421 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 /*
424 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
425 * fieldType FIELD-ID.&id({IOSet}),
426 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
427 * }
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
430 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
431 return ret;
432 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
434 end_field = p + len;
435
436 /*
437 * FIELD-ID ::= TYPE-IDENTIFIER
438 * FieldTypes FIELD-ID ::= {
439 * { Prime-p IDENTIFIED BY prime-field } |
440 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
441 * }
442 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
445 return ret;
446 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
449 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
450 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451 }
452
453 p += len;
454
455 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
457 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
458 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (p != end_field) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
465 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
467 /*
468 * Curve ::= SEQUENCE {
469 * a FieldElement,
470 * b FieldElement,
471 * seed BIT STRING OPTIONAL
472 * -- Shall be present if used in SpecifiedECDomain
473 * -- with version equal to ecdpVer2 or ecdpVer3
474 * }
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
477 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
478 return ret;
479 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480
481 end_curve = p + len;
482
483 /*
484 * FieldElement ::= OCTET STRING
485 * containing an integer in the case of a prime field
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
488 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
489 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490 }
491
492 p += len;
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
495 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
496 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497 }
498
499 p += len;
500
501 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100503 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (p != end_curve) {
507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
508 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
509 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100510
511 /*
512 * ECPoint ::= OCTET STRING
513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
515 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
516 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
519 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100520 /*
521 * If we can't read the point because it's compressed, cheat by
522 * reading only the X coordinate and the parity bit of Y.
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
525 (p[0] != 0x02 && p[0] != 0x03) ||
526 len != mbedtls_mpi_size(&grp->P) + 1 ||
527 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
528 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
529 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
530 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100531 }
532 }
533
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100534 p += len;
535
536 /*
537 * order INTEGER
538 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
540 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
541 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100544
545 /*
546 * Allow optional elements by purposefully not enforcing p == end here.
547 */
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100550}
551
552/*
553 * Find the group id associated with an (almost filled) group as generated by
554 * pk_group_from_specified(), or return an error if unknown.
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556static 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 +0100557{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100558 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_ecp_group ref;
560 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100565 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mbedtls_ecp_group_free(&ref);
567 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100568
569 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
571 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
572 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
573 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
574 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
575 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
576 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100577 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 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 +0100579 break;
580 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100581 }
582
583cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100585
586 *grp_id = *id;
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100593}
594
595/*
596 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
597 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200598static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
599 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100600{
Janos Follath24eed8d2019-11-22 13:21:35 +0000601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100607 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100611
612cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000613 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200614 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000615 * temporary grp breaks that flow and it's members are populated
616 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
617 * which is assuming a group populated by _setup() may not clean-up
618 * properly -> Manually free it's members.
619 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000620 mbedtls_mpi_free(&grp.N);
621 mbedtls_mpi_free(&grp.P);
622 mbedtls_mpi_free(&grp.A);
623 mbedtls_mpi_free(&grp.B);
624 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100627}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100629
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200630/***********************************************************************
631 *
632 * Unsorted (yet!) from this point on until the next section header
633 *
634 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200635
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200636/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200637 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200638 * ECParameters ::= CHOICE {
639 * namedCurve OBJECT IDENTIFIER
640 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
641 * -- implicitCurve NULL
642 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200643 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200644static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
645 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200646{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200648
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200649 if (end - *p < 1) {
650 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
651 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200652 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200653
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200654 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200655 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200656 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200657 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
659 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
660 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200661
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200662 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
664 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200665
666 params->p = *p;
667 *p += params->len;
668
669 if (*p != end) {
670 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
671 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
672 }
673
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200674 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200675}
676
677/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200678 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100679 *
680 * ECParameters ::= CHOICE {
681 * namedCurve OBJECT IDENTIFIER
682 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
683 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200684 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200685static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686{
Janos Follath24eed8d2019-11-22 13:21:35 +0000687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (params->tag == MBEDTLS_ASN1_OID) {
691 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
692 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
693 }
694 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200695 ret = pk_ecc_group_id_from_specified(params, &grp_id);
696 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return ret;
698 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100699 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200701 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702}
703
Jethro Beekman01672442023-04-19 14:08:14 +0200704#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
705
706/*
707 * Load an RFC8410 EC key, which doesn't have any parameters
708 */
709static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
710 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200711 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200712{
713 if (params->tag != 0 || params->len != 0) {
714 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
715 }
716
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200717 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200718}
719
720/*
721 * Parse an RFC 8410 encoded private EC key
722 *
723 * CurvePrivateKey ::= OCTET STRING
724 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200725static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200726 unsigned char *key, size_t keylen, const unsigned char *end,
727 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
728{
729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
730 size_t len;
731
732 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
733 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
734 }
735
736 if (key + len != end) {
737 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
738 }
739
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200740 /*
741 * Load the private key
742 */
743 ret = pk_ecc_set_key(pk, key, len);
744 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200745 return ret;
746 }
Jethro Beekman01672442023-04-19 14:08:14 +0200747
Valerio Setti4064dbb2023-05-17 15:33:07 +0200748 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
749 * which never contain a public key. As such, derive the public key
750 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200751 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200752 return ret;
753 }
754
Jethro Beekman01672442023-04-19 14:08:14 +0200755 return 0;
756}
757#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200758
Valerio Setti81d75122023-06-14 14:49:33 +0200759#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761/* Get a PK algorithm identifier
762 *
763 * AlgorithmIdentifier ::= SEQUENCE {
764 * algorithm OBJECT IDENTIFIER,
765 * parameters ANY DEFINED BY algorithm OPTIONAL }
766 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767static int pk_get_pk_alg(unsigned char **p,
768 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200769 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
770 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200771{
Janos Follath24eed8d2019-11-22 13:21:35 +0000772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
779 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
Jethro Beekman01672442023-04-19 14:08:14 +0200781 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200782#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200783 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
784 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
785 if (ret == 0) {
786 *pk_alg = MBEDTLS_PK_ECKEY;
787 }
788 }
789#else
790 (void) ec_grp_id;
791#endif
792 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
794 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795
796 /*
797 * No parameters with RSA (only for EC)
798 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (*pk_alg == MBEDTLS_PK_RSA &&
800 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
801 params->len != 0)) {
802 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803 }
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806}
807
808/*
809 * SubjectPublicKeyInfo ::= SEQUENCE {
810 * algorithm AlgorithmIdentifier,
811 * subjectPublicKey BIT STRING }
812 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
814 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815{
Janos Follath24eed8d2019-11-22 13:21:35 +0000816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_asn1_buf alg_params;
819 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200820 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
824 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
825 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826 }
827
828 end = *p + len;
829
Jethro Beekman01672442023-04-19 14:08:14 +0200830 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return ret;
832 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
835 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
836 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (*p + len != end) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
840 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
841 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
844 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
845 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
848 return ret;
849 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti201e6432024-02-01 17:19:37 +0100853 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), *p, (size_t) (end - *p));
Valerio Setti5922cb92024-02-02 09:21:25 +0100854 if (ret == 0) {
855 /* On success all the input has been consumed by the parsing function. */
856 *p += end - *p;
Valerio Setti5a198922024-02-02 13:59:51 +0100857 } else if ((ret <= MBEDTLS_ERR_ASN1_OUT_OF_DATA) &&
858 (ret >= MBEDTLS_ERR_ASN1_BUF_TOO_SMALL)) {
Valerio Setti5922cb92024-02-02 09:21:25 +0100859 /* In case of ASN1 error codes add MBEDTLS_ERR_PK_INVALID_PUBKEY. */
860 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
861 } else {
862 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY;
863 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200866#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200868#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200869 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200870 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200871 } else
872#endif
873 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200874 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200875 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000877 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200878 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200881#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (ret == 0 && *p != end) {
885 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
886 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
887 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (ret != 0) {
890 mbedtls_pk_free(pk);
891 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894}
895
Valerio Setti81d75122023-06-14 14:49:33 +0200896#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897/*
898 * Parse a SEC1 encoded private EC key
899 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200900static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 const unsigned char *key, size_t keylen,
902 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903{
Janos Follath24eed8d2019-11-22 13:21:35 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100905 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +0200906 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700907 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +0200909 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910 unsigned char *end = p + keylen;
911 unsigned char *end2;
912
913 /*
914 * RFC 5915, or SEC1 Appendix C.4
915 *
916 * ECPrivateKey ::= SEQUENCE {
917 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
918 * privateKey OCTET STRING,
919 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
920 * publicKey [1] BIT STRING OPTIONAL
921 * }
922 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
924 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926 }
927
928 end = p + len;
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
931 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
932 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (version != 1) {
935 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
939 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Valerio Setti6b062ee2023-06-30 17:32:57 +0200942 /* Keep a reference to the position fo the private key. It will be used
943 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +0200944 d = p;
945 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200946
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947 p += len;
948
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200951 /*
952 * Is 'parameters' present?
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
955 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
956 0)) == 0) {
957 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +0200958 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100963 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800964 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200965
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200966 /*
967 * Load the private key
968 */
969 ret = pk_ecc_set_key(pk, d, d_len);
970 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +0200971 return ret;
972 }
Valerio Setti6b062ee2023-06-30 17:32:57 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200975 /*
976 * Is 'publickey' present? If not, or if we can't read it (eg because it
977 * is compressed), create it from the private key.
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
980 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
981 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200982 end2 = p + len;
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
985 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
986 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (p + len != end2) {
989 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
990 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
991 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200992
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000993 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200994 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200996 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200997 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200998 * is if the point format is not recognized.
999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1001 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1002 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001006 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001008
Valerio Setti34f67552023-04-03 15:19:18 +02001009 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001010 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001011 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001012 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001013 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016}
Valerio Setti81d75122023-06-14 14:49:33 +02001017#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001019/***********************************************************************
1020 *
1021 * PKCS#8 parsing functions
1022 *
1023 **********************************************************************/
1024
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025/*
1026 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001027 *
1028 * Notes:
1029 *
1030 * - This function does not own the key buffer. It is the
1031 * responsibility of the caller to take care of zeroizing
1032 * and freeing it after use.
1033 *
1034 * - The function is responsible for freeing the provided
1035 * PK context on failure.
1036 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 */
1038static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_pk_context *pk,
1040 const unsigned char *key, size_t keylen,
1041 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001042{
1043 int ret, version;
1044 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 unsigned char *p = (unsigned char *) key;
1047 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001049 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051
Valerio Setti81d75122023-06-14 14:49:33 +02001052#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001053 (void) f_rng;
1054 (void) p_rng;
1055#endif
1056
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001058 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 *
1060 * PrivateKeyInfo ::= SEQUENCE {
1061 * version Version,
1062 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1063 * privateKey PrivateKey,
1064 * attributes [0] IMPLICIT Attributes OPTIONAL }
1065 *
1066 * Version ::= INTEGER
1067 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1068 * PrivateKey ::= OCTET STRING
1069 *
1070 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1071 */
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1074 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1075 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076 }
1077
1078 end = p + len;
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1081 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001082 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (version != 0) {
1085 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1086 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087
Jethro Beekman01672442023-04-19 14:08:14 +02001088 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return ret;
1090 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1093 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1094 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (len < 1) {
1097 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1098 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1099 }
1100
1101 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1102 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1103 }
1104
1105 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1106 return ret;
1107 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001111 if ((ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), p, len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_pk_free(pk);
1113 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 }
1115 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001117#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001119#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001120 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001121 if ((ret =
1122 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001123 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001124 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001125 p_rng)) != 0) {
1126 mbedtls_pk_free(pk);
1127 return ret;
1128 }
1129 } else
1130#endif
1131 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001132 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1133 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001134 mbedtls_pk_free(pk);
1135 return ret;
1136 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 }
1138 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001139#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001142 end = p + len;
1143 if (end != (key + keylen)) {
1144 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1145 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1146 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149}
1150
1151/*
1152 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001153 *
1154 * To save space, the decryption happens in-place on the given key buffer.
1155 * Also, while this function may modify the keybuffer, it doesn't own it,
1156 * and instead it is the responsibility of the caller to zeroize and properly
1157 * free it after use.
1158 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001161MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_pk_context *pk,
1163 unsigned char *key, size_t keylen,
1164 const unsigned char *pwd, size_t pwdlen,
1165 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001167 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001169 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001172#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_cipher_type_t cipher_alg;
1174 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001176 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
Hanno Becker2aa80a72017-09-07 15:28:45 +01001178 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 end = p + keylen;
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (pwdlen == 0) {
1182 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1183 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184
1185 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001186 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187 *
1188 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1189 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1190 * encryptedData EncryptedData
1191 * }
1192 *
1193 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1194 *
1195 * EncryptedData ::= OCTET STRING
1196 *
1197 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001198 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1201 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 }
1204
1205 end = p + len;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1213 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214
Hanno Beckerfab35692017-08-25 13:38:26 +01001215 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216
1217 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001218 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 */
Valerio Settie581e142023-12-29 16:35:07 +01001220#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001222 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001223 cipher_alg, md_alg,
1224 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1226 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1227 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001231
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001232 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 } else
Valerio Settie581e142023-12-29 16:35:07 +01001234#endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
1235#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001237 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1238 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1240 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1241 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001245
1246 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 } else
Valerio Settie581e142023-12-29 16:35:07 +01001248#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001249 {
1250 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001251 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (decrypted == 0) {
1254 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1255 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001256 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001260/***********************************************************************
1261 *
1262 * Top-level functions, with format auto-discovery
1263 *
1264 **********************************************************************/
1265
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266/*
1267 * Parse a private key
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1270 const unsigned char *key, size_t keylen,
1271 const unsigned char *pwd, size_t pwdlen,
1272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273{
Janos Follath24eed8d2019-11-22 13:21:35 +00001274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001279#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (keylen == 0) {
1282 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1283 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001284
1285#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001289 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 } else {
1293 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001294 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 key, pwd, pwdlen, &len);
1296 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (ret == 0) {
1299 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1300 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti135ebde2024-02-01 17:00:29 +01001301 (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk),
Valerio Settifd49a462024-01-23 08:35:11 +01001302 pem.buf, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_pem_free(&pem);
1307 return ret;
1308 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1309 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1310 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1311 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1312 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1313 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316
Valerio Setti81d75122023-06-14 14:49:33 +02001317#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001318 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001320 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 } else {
1322 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001323 PEM_BEGIN_PRIVATE_KEY_EC,
1324 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 key, pwd, pwdlen, &len);
1326 }
1327 if (ret == 0) {
1328 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001331 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 pem.buf, pem.buflen,
1333 f_rng, p_rng)) != 0) {
1334 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 }
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 mbedtls_pem_free(&pem);
1338 return ret;
1339 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1340 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1341 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1342 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1343 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1344 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 }
Valerio Setti81d75122023-06-14 14:49:33 +02001346#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001348 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001350 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 } else {
1352 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001353 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 key, NULL, 0, &len);
1355 }
1356 if (ret == 0) {
1357 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1358 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1359 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 }
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 mbedtls_pem_free(&pem);
1363 return ret;
1364 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1365 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001369 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001371 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 } else {
1373 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001374 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1375 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 key, NULL, 0, &len);
1377 }
1378 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001379 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1380 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 mbedtls_pem_free(&pem);
1385 return ret;
1386 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1387 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390#else
1391 ((void) pwd);
1392 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
1395 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001396 * At this point we only know it's not a PEM formatted key. Could be any
1397 * of the known DER encoded private key formats
1398 *
1399 * We try the different DER format parsers to see if one passes without
1400 * error
1401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001404 unsigned char *key_copy;
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1407 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1408 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001411
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001412 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1413 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001414
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001415 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (ret == 0) {
1419 return 0;
1420 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_pk_free(pk);
1423 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1426 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1431 if (ret == 0) {
1432 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001433 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_pk_free(pk);
1436 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1441 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti135ebde2024-02-01 17:00:29 +01001442 mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444 }
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 mbedtls_pk_free(pk);
1447 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
Valerio Setti81d75122023-06-14 14:49:33 +02001450#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1452 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001453 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 key, keylen, f_rng, p_rng) == 0) {
1455 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001458#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459
Valerio Setti81d75122023-06-14 14:49:33 +02001460 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001461 * it is ok to leave the PK context initialized but not
1462 * freed: It is the caller's responsibility to call pk_init()
1463 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001464 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001465 * isn't, this leads to mbedtls_pk_free() being called
1466 * twice, once here and once by the caller, but this is
1467 * also ok and in line with the mbedtls_pk_free() calls
1468 * on failed PEM parsing attempts. */
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471}
1472
1473/*
1474 * Parse a public key
1475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1477 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478{
Janos Follath24eed8d2019-11-22 13:21:35 +00001479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001481#if defined(MBEDTLS_RSA_C)
1482 const mbedtls_pk_info_t *pk_info;
1483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001487#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (keylen == 0) {
1490 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1491 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001492
1493#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001495#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001496 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001498 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 } else {
1500 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001501 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001503 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001504
1505 if (ret == 0) {
1506 p = pem.buf;
1507 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1508 mbedtls_pem_free(&pem);
1509 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1510 }
1511
1512 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1513 mbedtls_pem_free(&pem);
1514 return ret;
1515 }
1516
Valerio Setti201e6432024-02-01 17:19:37 +01001517 if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 mbedtls_pk_free(ctx);
1519 }
1520
1521 mbedtls_pem_free(&pem);
1522 return ret;
1523 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1524 mbedtls_pem_free(&pem);
1525 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001526 }
1527#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001528
1529 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001531 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 } else {
1533 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001534 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 key, NULL, 0, &len);
1536 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539 /*
1540 * Was PEM encoded
1541 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001542 p = pem.buf;
1543
Jethro Beekman01672442023-04-19 14:08:14 +02001544 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_pem_free(&pem);
1546 return ret;
1547 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1548 mbedtls_pem_free(&pem);
1549 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001550 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001553
1554#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1556 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001557 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001558
1559 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1560 return ret;
1561 }
1562
1563 p = (unsigned char *) key;
Valerio Setti201e6432024-02-01 17:19:37 +01001564 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, keylen);
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ret == 0) {
1566 return ret;
1567 }
1568 mbedtls_pk_free(ctx);
Valerio Settidccfd362024-01-23 17:07:59 +01001569 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001571 }
1572#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573 p = (unsigned char *) key;
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578}
1579
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001580/***********************************************************************
1581 *
1582 * Top-level functions, with filesystem support
1583 *
1584 **********************************************************************/
1585
1586#if defined(MBEDTLS_FS_IO)
1587/*
1588 * Load all data from a file into a given buffer.
1589 *
1590 * The file is expected to contain either PEM or DER encoded data.
1591 * A terminating null byte is always appended. It is included in the announced
1592 * length only if the data looks like it is PEM encoded.
1593 */
1594int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1595{
1596 FILE *f;
1597 long size;
1598
1599 if ((f = fopen(path, "rb")) == NULL) {
1600 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1601 }
1602
1603 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1604 mbedtls_setbuf(f, NULL);
1605
1606 fseek(f, 0, SEEK_END);
1607 if ((size = ftell(f)) == -1) {
1608 fclose(f);
1609 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1610 }
1611 fseek(f, 0, SEEK_SET);
1612
1613 *n = (size_t) size;
1614
1615 if (*n + 1 == 0 ||
1616 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1617 fclose(f);
1618 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1619 }
1620
1621 if (fread(*buf, 1, *n, f) != *n) {
1622 fclose(f);
1623
1624 mbedtls_zeroize_and_free(*buf, *n);
1625
1626 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1627 }
1628
1629 fclose(f);
1630
1631 (*buf)[*n] = '\0';
1632
1633 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1634 ++*n;
1635 }
1636
1637 return 0;
1638}
1639
1640/*
1641 * Load and parse a private key
1642 */
1643int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1644 const char *path, const char *pwd,
1645 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1646{
1647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1648 size_t n;
1649 unsigned char *buf;
1650
1651 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1652 return ret;
1653 }
1654
1655 if (pwd == NULL) {
1656 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1657 } else {
1658 ret = mbedtls_pk_parse_key(ctx, buf, n,
1659 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1660 }
1661
1662 mbedtls_zeroize_and_free(buf, n);
1663
1664 return ret;
1665}
1666
1667/*
1668 * Load and parse a public key
1669 */
1670int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1671{
1672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1673 size_t n;
1674 unsigned char *buf;
1675
1676 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1677 return ret;
1678 }
1679
1680 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1681
1682 mbedtls_zeroize_and_free(buf, n);
1683
1684 return ret;
1685}
1686#endif /* MBEDTLS_FS_IO */
1687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_PK_PARSE_C */