blob: 91d6eb59eaad80bbed71c0d24bb03f2026c91cd7 [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;
857 } else if (ret & 0x7f) {
858 /* In case of ASN1 error codes add MBEDTLS_ERR_PK_INVALID_PUBKEY. */
859 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
860 } else {
861 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY;
862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200865#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200867#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200868 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200869 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200870 } else
871#endif
872 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200873 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200874 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000876 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200877 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200880#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (ret == 0 && *p != end) {
884 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
885 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
886 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (ret != 0) {
889 mbedtls_pk_free(pk);
890 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893}
894
Valerio Setti81d75122023-06-14 14:49:33 +0200895#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896/*
897 * Parse a SEC1 encoded private EC key
898 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200899static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 const unsigned char *key, size_t keylen,
901 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902{
Janos Follath24eed8d2019-11-22 13:21:35 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100904 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +0200905 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700906 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +0200908 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 unsigned char *end = p + keylen;
910 unsigned char *end2;
911
912 /*
913 * RFC 5915, or SEC1 Appendix C.4
914 *
915 * ECPrivateKey ::= SEQUENCE {
916 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
917 * privateKey OCTET STRING,
918 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
919 * publicKey [1] BIT STRING OPTIONAL
920 * }
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
923 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
924 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925 }
926
927 end = p + len;
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (version != 1) {
934 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
938 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Valerio Setti6b062ee2023-06-30 17:32:57 +0200941 /* Keep a reference to the position fo the private key. It will be used
942 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +0200943 d = p;
944 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200945
Paul Bakker1a7550a2013-09-15 13:01:22 +0200946 p += len;
947
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950 /*
951 * Is 'parameters' present?
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
954 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
955 0)) == 0) {
956 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +0200957 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200959 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100962 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800963 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200965 /*
966 * Load the private key
967 */
968 ret = pk_ecc_set_key(pk, d, d_len);
969 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +0200970 return ret;
971 }
Valerio Setti6b062ee2023-06-30 17:32:57 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200974 /*
975 * Is 'publickey' present? If not, or if we can't read it (eg because it
976 * is compressed), create it from the private key.
977 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
979 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
980 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200981 end2 = p + len;
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
984 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
985 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (p + len != end2) {
988 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
989 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
990 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200991
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000992 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200993 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200995 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200996 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200997 * is if the point format is not recognized.
998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1000 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1001 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001002 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001005 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001007
Valerio Setti34f67552023-04-03 15:19:18 +02001008 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001009 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001010 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001011 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001012 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015}
Valerio Setti81d75122023-06-14 14:49:33 +02001016#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001018/***********************************************************************
1019 *
1020 * PKCS#8 parsing functions
1021 *
1022 **********************************************************************/
1023
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024/*
1025 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001026 *
1027 * Notes:
1028 *
1029 * - This function does not own the key buffer. It is the
1030 * responsibility of the caller to take care of zeroizing
1031 * and freeing it after use.
1032 *
1033 * - The function is responsible for freeing the provided
1034 * PK context on failure.
1035 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 */
1037static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_pk_context *pk,
1039 const unsigned char *key, size_t keylen,
1040 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041{
1042 int ret, version;
1043 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045 unsigned char *p = (unsigned char *) key;
1046 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001048 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050
Valerio Setti81d75122023-06-14 14:49:33 +02001051#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001052 (void) f_rng;
1053 (void) p_rng;
1054#endif
1055
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001057 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 *
1059 * PrivateKeyInfo ::= SEQUENCE {
1060 * version Version,
1061 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1062 * privateKey PrivateKey,
1063 * attributes [0] IMPLICIT Attributes OPTIONAL }
1064 *
1065 * Version ::= INTEGER
1066 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1067 * PrivateKey ::= OCTET STRING
1068 *
1069 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1070 */
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1073 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1074 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 }
1076
1077 end = p + len;
1078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1080 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001081 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (version != 0) {
1084 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1085 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086
Jethro Beekman01672442023-04-19 14:08:14 +02001087 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return ret;
1089 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1092 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1093 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (len < 1) {
1096 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1097 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1098 }
1099
1100 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1101 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1102 }
1103
1104 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1105 return ret;
1106 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (pk_alg == MBEDTLS_PK_RSA) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001110 if ((ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), p, len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_pk_free(pk);
1112 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 }
1114 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001116#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001118#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001119 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001120 if ((ret =
1121 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001122 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001123 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001124 p_rng)) != 0) {
1125 mbedtls_pk_free(pk);
1126 return ret;
1127 }
1128 } else
1129#endif
1130 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001131 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1132 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001133 mbedtls_pk_free(pk);
1134 return ret;
1135 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 }
1137 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001138#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001141 end = p + len;
1142 if (end != (key + keylen)) {
1143 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1144 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1145 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148}
1149
1150/*
1151 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001152 *
1153 * To save space, the decryption happens in-place on the given key buffer.
1154 * Also, while this function may modify the keybuffer, it doesn't own it,
1155 * and instead it is the responsibility of the caller to zeroize and properly
1156 * free it after use.
1157 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001160MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_pk_context *pk,
1162 unsigned char *key, size_t keylen,
1163 const unsigned char *pwd, size_t pwdlen,
1164 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001166 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001168 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001171#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_cipher_type_t cipher_alg;
1173 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001175 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
Hanno Becker2aa80a72017-09-07 15:28:45 +01001177 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 end = p + keylen;
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (pwdlen == 0) {
1181 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1182 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
1184 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001185 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 *
1187 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1188 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1189 * encryptedData EncryptedData
1190 * }
1191 *
1192 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1193 *
1194 * EncryptedData ::= OCTET STRING
1195 *
1196 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001197 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 }
1203
1204 end = p + len;
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213
Hanno Beckerfab35692017-08-25 13:38:26 +01001214 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
1216 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001217 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 */
Valerio Settie581e142023-12-29 16:35:07 +01001219#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001221 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001222 cipher_alg, md_alg,
1223 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1225 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1226 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001230
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001231 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 } else
Valerio Settie581e142023-12-29 16:35:07 +01001233#endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
1234#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001236 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1237 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1239 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1240 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001244
1245 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 } else
Valerio Settie581e142023-12-29 16:35:07 +01001247#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001248 {
1249 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001250 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (decrypted == 0) {
1253 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1254 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001255 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001259/***********************************************************************
1260 *
1261 * Top-level functions, with format auto-discovery
1262 *
1263 **********************************************************************/
1264
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265/*
1266 * Parse a private key
1267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1269 const unsigned char *key, size_t keylen,
1270 const unsigned char *pwd, size_t pwdlen,
1271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272{
Janos Follath24eed8d2019-11-22 13:21:35 +00001273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001278#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (keylen == 0) {
1281 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1282 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001283
1284#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001288 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001290 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 } else {
1292 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001293 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 key, pwd, pwdlen, &len);
1295 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (ret == 0) {
1298 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1299 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti135ebde2024-02-01 17:00:29 +01001300 (ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk),
Valerio Settifd49a462024-01-23 08:35:11 +01001301 pem.buf, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_pem_free(&pem);
1306 return ret;
1307 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1308 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1309 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1310 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1311 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1312 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315
Valerio Setti81d75122023-06-14 14:49:33 +02001316#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001317 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001319 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 } else {
1321 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001322 PEM_BEGIN_PRIVATE_KEY_EC,
1323 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 key, pwd, pwdlen, &len);
1325 }
1326 if (ret == 0) {
1327 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001330 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 pem.buf, pem.buflen,
1332 f_rng, p_rng)) != 0) {
1333 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 }
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 mbedtls_pem_free(&pem);
1337 return ret;
1338 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1339 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1340 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1341 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1342 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1343 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 }
Valerio Setti81d75122023-06-14 14:49:33 +02001345#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001347 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001349 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 } else {
1351 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001352 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 key, NULL, 0, &len);
1354 }
1355 if (ret == 0) {
1356 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1357 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1358 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 }
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 mbedtls_pem_free(&pem);
1362 return ret;
1363 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1364 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001368 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001370 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 } else {
1372 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001373 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1374 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 key, NULL, 0, &len);
1376 }
1377 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001378 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1379 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 }
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 mbedtls_pem_free(&pem);
1384 return ret;
1385 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1386 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389#else
1390 ((void) pwd);
1391 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
1394 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001395 * At this point we only know it's not a PEM formatted key. Could be any
1396 * of the known DER encoded private key formats
1397 *
1398 * We try the different DER format parsers to see if one passes without
1399 * error
1400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001403 unsigned char *key_copy;
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1406 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1407 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001410
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001411 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1412 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001413
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001414 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415 }
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (ret == 0) {
1418 return 0;
1419 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 mbedtls_pk_free(pk);
1422 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1425 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1430 if (ret == 0) {
1431 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001432 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_pk_free(pk);
1435 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1440 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti135ebde2024-02-01 17:00:29 +01001441 mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 }
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 mbedtls_pk_free(pk);
1446 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448
Valerio Setti81d75122023-06-14 14:49:33 +02001449#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1451 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001452 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 key, keylen, f_rng, p_rng) == 0) {
1454 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001457#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
Valerio Setti81d75122023-06-14 14:49:33 +02001459 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001460 * it is ok to leave the PK context initialized but not
1461 * freed: It is the caller's responsibility to call pk_init()
1462 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001463 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001464 * isn't, this leads to mbedtls_pk_free() being called
1465 * twice, once here and once by the caller, but this is
1466 * also ok and in line with the mbedtls_pk_free() calls
1467 * on failed PEM parsing attempts. */
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470}
1471
1472/*
1473 * Parse a public key
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1476 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477{
Janos Follath24eed8d2019-11-22 13:21:35 +00001478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001480#if defined(MBEDTLS_RSA_C)
1481 const mbedtls_pk_info_t *pk_info;
1482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001486#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (keylen == 0) {
1489 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1490 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001491
1492#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001494#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001495 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001497 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 } else {
1499 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001500 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001502 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001503
1504 if (ret == 0) {
1505 p = pem.buf;
1506 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1507 mbedtls_pem_free(&pem);
1508 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1509 }
1510
1511 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1512 mbedtls_pem_free(&pem);
1513 return ret;
1514 }
1515
Valerio Setti201e6432024-02-01 17:19:37 +01001516 if ((ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, pem.buflen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 mbedtls_pk_free(ctx);
1518 }
1519
1520 mbedtls_pem_free(&pem);
1521 return ret;
1522 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1523 mbedtls_pem_free(&pem);
1524 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001525 }
1526#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001527
1528 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001530 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 } else {
1532 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001533 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 key, NULL, 0, &len);
1535 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538 /*
1539 * Was PEM encoded
1540 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001541 p = pem.buf;
1542
Jethro Beekman01672442023-04-19 14:08:14 +02001543 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_pem_free(&pem);
1545 return ret;
1546 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1547 mbedtls_pem_free(&pem);
1548 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001552
1553#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1555 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001556 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001557
1558 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1559 return ret;
1560 }
1561
1562 p = (unsigned char *) key;
Valerio Setti201e6432024-02-01 17:19:37 +01001563 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*ctx), p, keylen);
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if (ret == 0) {
1565 return ret;
1566 }
1567 mbedtls_pk_free(ctx);
Valerio Settidccfd362024-01-23 17:07:59 +01001568 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001570 }
1571#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572 p = (unsigned char *) key;
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577}
1578
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001579/***********************************************************************
1580 *
1581 * Top-level functions, with filesystem support
1582 *
1583 **********************************************************************/
1584
1585#if defined(MBEDTLS_FS_IO)
1586/*
1587 * Load all data from a file into a given buffer.
1588 *
1589 * The file is expected to contain either PEM or DER encoded data.
1590 * A terminating null byte is always appended. It is included in the announced
1591 * length only if the data looks like it is PEM encoded.
1592 */
1593int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1594{
1595 FILE *f;
1596 long size;
1597
1598 if ((f = fopen(path, "rb")) == NULL) {
1599 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1600 }
1601
1602 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1603 mbedtls_setbuf(f, NULL);
1604
1605 fseek(f, 0, SEEK_END);
1606 if ((size = ftell(f)) == -1) {
1607 fclose(f);
1608 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1609 }
1610 fseek(f, 0, SEEK_SET);
1611
1612 *n = (size_t) size;
1613
1614 if (*n + 1 == 0 ||
1615 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1616 fclose(f);
1617 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1618 }
1619
1620 if (fread(*buf, 1, *n, f) != *n) {
1621 fclose(f);
1622
1623 mbedtls_zeroize_and_free(*buf, *n);
1624
1625 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1626 }
1627
1628 fclose(f);
1629
1630 (*buf)[*n] = '\0';
1631
1632 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1633 ++*n;
1634 }
1635
1636 return 0;
1637}
1638
1639/*
1640 * Load and parse a private key
1641 */
1642int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1643 const char *path, const char *pwd,
1644 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1645{
1646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1647 size_t n;
1648 unsigned char *buf;
1649
1650 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1651 return ret;
1652 }
1653
1654 if (pwd == NULL) {
1655 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1656 } else {
1657 ret = mbedtls_pk_parse_key(ctx, buf, n,
1658 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1659 }
1660
1661 mbedtls_zeroize_and_free(buf, n);
1662
1663 return ret;
1664}
1665
1666/*
1667 * Load and parse a public key
1668 */
1669int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1670{
1671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1672 size_t n;
1673 unsigned char *buf;
1674
1675 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1676 return ret;
1677 }
1678
1679 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1680
1681 mbedtls_zeroize_and_free(buf, n);
1682
1683 return ret;
1684}
1685#endif /* MBEDTLS_FS_IO */
1686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#endif /* MBEDTLS_PK_PARSE_C */