blob: b2127b2e5ba15437d580017f70748f03df821f9d [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 Setti135ebde2024-02-01 17:00:29 +0100853 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), p, end);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200856#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200858#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200859 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200860 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200861 } else
862#endif
863 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200864 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200865 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000867 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200868 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200871#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (ret == 0 && *p != end) {
875 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
877 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (ret != 0) {
880 mbedtls_pk_free(pk);
881 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884}
885
Valerio Setti81d75122023-06-14 14:49:33 +0200886#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887/*
888 * Parse a SEC1 encoded private EC key
889 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200890static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 const unsigned char *key, size_t keylen,
892 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893{
Janos Follath24eed8d2019-11-22 13:21:35 +0000894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100895 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +0200896 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700897 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +0200899 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900 unsigned char *end = p + keylen;
901 unsigned char *end2;
902
903 /*
904 * RFC 5915, or SEC1 Appendix C.4
905 *
906 * ECPrivateKey ::= SEQUENCE {
907 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
908 * privateKey OCTET STRING,
909 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
910 * publicKey [1] BIT STRING OPTIONAL
911 * }
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916 }
917
918 end = p + len;
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
921 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
922 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (version != 1) {
925 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
926 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
929 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Valerio Setti6b062ee2023-06-30 17:32:57 +0200932 /* Keep a reference to the position fo the private key. It will be used
933 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +0200934 d = p;
935 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200936
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937 p += len;
938
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200939 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200941 /*
942 * Is 'parameters' present?
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
945 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
946 0)) == 0) {
947 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +0200948 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100953 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800954 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200955
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200956 /*
957 * Load the private key
958 */
959 ret = pk_ecc_set_key(pk, d, d_len);
960 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +0200961 return ret;
962 }
Valerio Setti6b062ee2023-06-30 17:32:57 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200965 /*
966 * Is 'publickey' present? If not, or if we can't read it (eg because it
967 * is compressed), create it from the private key.
968 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
970 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
971 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200972 end2 = p + len;
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
975 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
976 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (p + len != end2) {
979 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
980 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
981 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200982
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000983 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200984 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200986 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200987 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200988 * is if the point format is not recognized.
989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
991 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
992 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200993 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200996 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100998
Valerio Setti34f67552023-04-03 15:19:18 +0200999 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001000 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001001 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001002 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001003 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006}
Valerio Setti81d75122023-06-14 14:49:33 +02001007#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001009/***********************************************************************
1010 *
1011 * PKCS#8 parsing functions
1012 *
1013 **********************************************************************/
1014
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015/*
1016 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001017 *
1018 * Notes:
1019 *
1020 * - This function does not own the key buffer. It is the
1021 * responsibility of the caller to take care of zeroizing
1022 * and freeing it after use.
1023 *
1024 * - The function is responsible for freeing the provided
1025 * PK context on failure.
1026 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 */
1028static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 mbedtls_pk_context *pk,
1030 const unsigned char *key, size_t keylen,
1031 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032{
1033 int ret, version;
1034 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 unsigned char *p = (unsigned char *) key;
1037 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001039 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
Valerio Setti81d75122023-06-14 14:49:33 +02001042#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001043 (void) f_rng;
1044 (void) p_rng;
1045#endif
1046
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001048 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049 *
1050 * PrivateKeyInfo ::= SEQUENCE {
1051 * version Version,
1052 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1053 * privateKey PrivateKey,
1054 * attributes [0] IMPLICIT Attributes OPTIONAL }
1055 *
1056 * Version ::= INTEGER
1057 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1058 * PrivateKey ::= OCTET STRING
1059 *
1060 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1061 */
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1064 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1065 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066 }
1067
1068 end = p + len;
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1071 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001072 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (version != 0) {
1075 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1076 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077
Jethro Beekman01672442023-04-19 14:08:14 +02001078 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return ret;
1080 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1083 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1084 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (len < 1) {
1087 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1088 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1089 }
1090
1091 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1092 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1093 }
1094
1095 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1096 return ret;
1097 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001101 if ((ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), p, len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_pk_free(pk);
1103 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 }
1105 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001107#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001109#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001110 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001111 if ((ret =
1112 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001113 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001114 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001115 p_rng)) != 0) {
1116 mbedtls_pk_free(pk);
1117 return ret;
1118 }
1119 } else
1120#endif
1121 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001122 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1123 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001124 mbedtls_pk_free(pk);
1125 return ret;
1126 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127 }
1128 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001129#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001132 end = p + len;
1133 if (end != (key + keylen)) {
1134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1135 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1136 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139}
1140
1141/*
1142 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001143 *
1144 * To save space, the decryption happens in-place on the given key buffer.
1145 * Also, while this function may modify the keybuffer, it doesn't own it,
1146 * and instead it is the responsibility of the caller to zeroize and properly
1147 * free it after use.
1148 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001151MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_pk_context *pk,
1153 unsigned char *key, size_t keylen,
1154 const unsigned char *pwd, size_t pwdlen,
1155 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001157 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001159 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001162#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_cipher_type_t cipher_alg;
1164 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001166 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167
Hanno Becker2aa80a72017-09-07 15:28:45 +01001168 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 end = p + keylen;
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (pwdlen == 0) {
1172 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1173 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174
1175 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001176 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 *
1178 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1179 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1180 * encryptedData EncryptedData
1181 * }
1182 *
1183 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1184 *
1185 * EncryptedData ::= OCTET STRING
1186 *
1187 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001188 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1191 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 }
1194
1195 end = p + len;
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Hanno Beckerfab35692017-08-25 13:38:26 +01001205 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206
1207 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001208 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 */
Valerio Settie581e142023-12-29 16:35:07 +01001210#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001212 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001213 cipher_alg, md_alg,
1214 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1216 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1217 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001221
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001222 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 } else
Valerio Settie581e142023-12-29 16:35:07 +01001224#endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
1225#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001227 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1228 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1230 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1231 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001235
1236 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 } else
Valerio Settie581e142023-12-29 16:35:07 +01001238#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001239 {
1240 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001241 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (decrypted == 0) {
1244 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1245 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001246 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001250/***********************************************************************
1251 *
1252 * Top-level functions, with format auto-discovery
1253 *
1254 **********************************************************************/
1255
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256/*
1257 * Parse a private key
1258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1260 const unsigned char *key, size_t keylen,
1261 const unsigned char *pwd, size_t pwdlen,
1262 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263{
Janos Follath24eed8d2019-11-22 13:21:35 +00001264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001269#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (keylen == 0) {
1272 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1273 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001274
1275#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001279 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001281 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 } else {
1283 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001284 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 key, pwd, pwdlen, &len);
1286 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (ret == 0) {
1289 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1290 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti135ebde2024-02-01 17:00:29 +01001291 (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk),
Valerio Settifd49a462024-01-23 08:35:11 +01001292 pem.buf, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 mbedtls_pem_free(&pem);
1297 return ret;
1298 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1299 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1300 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1301 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1302 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1303 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306
Valerio Setti81d75122023-06-14 14:49:33 +02001307#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001308 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001310 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 } else {
1312 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001313 PEM_BEGIN_PRIVATE_KEY_EC,
1314 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 key, pwd, pwdlen, &len);
1316 }
1317 if (ret == 0) {
1318 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001321 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 pem.buf, pem.buflen,
1323 f_rng, p_rng)) != 0) {
1324 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 }
1326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_pem_free(&pem);
1328 return ret;
1329 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1330 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1331 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1332 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1333 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1334 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 }
Valerio Setti81d75122023-06-14 14:49:33 +02001336#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001338 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001340 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 } else {
1342 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001343 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 key, NULL, 0, &len);
1345 }
1346 if (ret == 0) {
1347 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1348 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1349 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 }
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_pem_free(&pem);
1353 return ret;
1354 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1355 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001359 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001361 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 } else {
1363 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001364 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1365 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 key, NULL, 0, &len);
1367 }
1368 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001369 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1370 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 }
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 mbedtls_pem_free(&pem);
1375 return ret;
1376 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1377 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380#else
1381 ((void) pwd);
1382 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384
1385 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001386 * At this point we only know it's not a PEM formatted key. Could be any
1387 * of the known DER encoded private key formats
1388 *
1389 * We try the different DER format parsers to see if one passes without
1390 * error
1391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001394 unsigned char *key_copy;
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1397 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1398 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001401
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001402 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1403 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001404
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001405 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 }
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (ret == 0) {
1409 return 0;
1410 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 mbedtls_pk_free(pk);
1413 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1416 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1421 if (ret == 0) {
1422 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 mbedtls_pk_free(pk);
1426 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1431 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti135ebde2024-02-01 17:00:29 +01001432 mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 }
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_pk_free(pk);
1437 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Valerio Setti81d75122023-06-14 14:49:33 +02001440#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1442 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001443 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 key, keylen, f_rng, p_rng) == 0) {
1445 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001448#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
Valerio Setti81d75122023-06-14 14:49:33 +02001450 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001451 * it is ok to leave the PK context initialized but not
1452 * freed: It is the caller's responsibility to call pk_init()
1453 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001454 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001455 * isn't, this leads to mbedtls_pk_free() being called
1456 * twice, once here and once by the caller, but this is
1457 * also ok and in line with the mbedtls_pk_free() calls
1458 * on failed PEM parsing attempts. */
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461}
1462
1463/*
1464 * Parse a public key
1465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1467 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001468{
Janos Follath24eed8d2019-11-22 13:21:35 +00001469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001471#if defined(MBEDTLS_RSA_C)
1472 const mbedtls_pk_info_t *pk_info;
1473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001477#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (keylen == 0) {
1480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1481 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001482
1483#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001485#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001486 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001488 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 } else {
1490 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001491 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001493 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001494
1495 if (ret == 0) {
1496 p = pem.buf;
1497 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1498 mbedtls_pem_free(&pem);
1499 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1500 }
1501
1502 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1503 mbedtls_pem_free(&pem);
1504 return ret;
1505 }
1506
Valerio Setti135ebde2024-02-01 17:00:29 +01001507 if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), &p, p + pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 mbedtls_pk_free(ctx);
1509 }
1510
1511 mbedtls_pem_free(&pem);
1512 return ret;
1513 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1514 mbedtls_pem_free(&pem);
1515 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001516 }
1517#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001518
1519 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001521 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 } else {
1523 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001524 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 key, NULL, 0, &len);
1526 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529 /*
1530 * Was PEM encoded
1531 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001532 p = pem.buf;
1533
Jethro Beekman01672442023-04-19 14:08:14 +02001534 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_pem_free(&pem);
1536 return ret;
1537 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1538 mbedtls_pem_free(&pem);
1539 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001543
1544#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1546 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001547 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001548
1549 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1550 return ret;
1551 }
1552
1553 p = (unsigned char *) key;
Valerio Setti135ebde2024-02-01 17:00:29 +01001554 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), &p, p + keylen);
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (ret == 0) {
1556 return ret;
1557 }
1558 mbedtls_pk_free(ctx);
Valerio Settidccfd362024-01-23 17:07:59 +01001559 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001561 }
1562#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563 p = (unsigned char *) key;
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001568}
1569
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001570/***********************************************************************
1571 *
1572 * Top-level functions, with filesystem support
1573 *
1574 **********************************************************************/
1575
1576#if defined(MBEDTLS_FS_IO)
1577/*
1578 * Load all data from a file into a given buffer.
1579 *
1580 * The file is expected to contain either PEM or DER encoded data.
1581 * A terminating null byte is always appended. It is included in the announced
1582 * length only if the data looks like it is PEM encoded.
1583 */
1584int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1585{
1586 FILE *f;
1587 long size;
1588
1589 if ((f = fopen(path, "rb")) == NULL) {
1590 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1591 }
1592
1593 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1594 mbedtls_setbuf(f, NULL);
1595
1596 fseek(f, 0, SEEK_END);
1597 if ((size = ftell(f)) == -1) {
1598 fclose(f);
1599 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1600 }
1601 fseek(f, 0, SEEK_SET);
1602
1603 *n = (size_t) size;
1604
1605 if (*n + 1 == 0 ||
1606 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1607 fclose(f);
1608 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1609 }
1610
1611 if (fread(*buf, 1, *n, f) != *n) {
1612 fclose(f);
1613
1614 mbedtls_zeroize_and_free(*buf, *n);
1615
1616 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1617 }
1618
1619 fclose(f);
1620
1621 (*buf)[*n] = '\0';
1622
1623 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1624 ++*n;
1625 }
1626
1627 return 0;
1628}
1629
1630/*
1631 * Load and parse a private key
1632 */
1633int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1634 const char *path, const char *pwd,
1635 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1636{
1637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1638 size_t n;
1639 unsigned char *buf;
1640
1641 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1642 return ret;
1643 }
1644
1645 if (pwd == NULL) {
1646 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1647 } else {
1648 ret = mbedtls_pk_parse_key(ctx, buf, n,
1649 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1650 }
1651
1652 mbedtls_zeroize_and_free(buf, n);
1653
1654 return ret;
1655}
1656
1657/*
1658 * Load and parse a public key
1659 */
1660int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1661{
1662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1663 size_t n;
1664 unsigned char *buf;
1665
1666 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1667 return ret;
1668 }
1669
1670 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1671
1672 mbedtls_zeroize_and_free(buf, n);
1673
1674 return ret;
1675}
1676#endif /* MBEDTLS_FS_IO */
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_PK_PARSE_C */