blob: edebf92ff73aee6eb325ba000e4fe797c55dbbdb [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"
Paul Bakker1a7550a2013-09-15 13:01:22 +020018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <string.h>
20
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020021#if defined(MBEDTLS_USE_PSA_CRYPTO)
22#include "mbedtls/psa_util.h"
23#include "psa/crypto.h"
24#endif
25
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020026/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029#endif
Valerio Setti81d75122023-06-14 14:49:33 +020030#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020031#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020032#include "pk_internal.h"
33#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020034
35/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020044#endif
45
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020046#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
47
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020048/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020049 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020050 * ECC setters
51 *
52 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
53 * this macro will not appear outside this section.
54 * 2. All inputs are raw: no metadata, no ASN.1 until the next section.
55 *
56 **********************************************************************/
57
58/*
59 * Set the group used by this key.
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020060 *
61 * [in/out] pk: in: must have been pk_setup() to an ECC type
62 * out: will have group (curve) information set
63 * [in] grp_in: a supported group ID (not NONE)
Paul Bakker1a7550a2013-09-15 13:01:22 +020064 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020065static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
66{
67#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
68 size_t ec_bits;
69 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
70
71 /* group may already be initialized; if so, make sure IDs match */
72 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
73 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
74 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
75 }
76
77 /* set group */
78 pk->ec_family = ec_family;
79 pk->ec_bits = ec_bits;
80
81 return 0;
82#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
83 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
84
85 /* grp may already be initialized; if so, make sure IDs match */
86 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
87 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
88 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
89 }
90
91 /* set group */
92 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
93#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
94}
95
96/*
97 * Set the private key material
98 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020099 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
100 * out: will have the private key set.
101 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200102 */
103static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200104 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200105{
106#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
108 psa_status_t status;
109
110 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
111 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
112 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
113 /* Montgomery allows only ECDH, others ECDSA too */
114 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
115 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
116 psa_set_key_enrollment_algorithm(&attributes,
117 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
118 }
119 psa_set_key_usage_flags(&attributes, flags);
120
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200121 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200122 return psa_pk_status_to_mbedtls(status);
123
124#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
125
126 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200127 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200128 if (ret != 0) {
129 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
130 }
131 return 0;
132#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
133}
134
135/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200136 * Derive a public key from its private counterpart.
137 * Computationally intensive, only use when public key is not available.
138 *
139 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
140 * out: will have the public key set.
141 * [in] prv, prv_len: the raw private key (see note below).
142 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200143 *
144 * Note: the private key information is always available from pk,
145 * however for convenience the serialized version is also passed,
146 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200147 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200148 *
149 * There are three implementations of this function:
150 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
151 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
152 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200153 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200154static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
155 const unsigned char *prv, size_t prv_len,
156 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200157{
158#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200159
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200160 (void) f_rng;
161 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200162 (void) prv;
163 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200164 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200165
166 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
167 &pk->pub_raw_len);
168 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200169
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200170#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200171
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200172 (void) f_rng;
173 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200174 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200175
176 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200177 size_t curve_bits;
178 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200179
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200180 /* Import private key into PSA, from serialized input */
181 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
182 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200183 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
184 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200185 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200186 if (status != PSA_SUCCESS) {
187 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200188 }
189
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200190 /* Export public key from PSA */
191 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
192 size_t pub_len;
193 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
194 psa_status_t destruction_status = psa_destroy_key(key_id);
195 if (status != PSA_SUCCESS) {
196 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200197 } else if (destruction_status != PSA_SUCCESS) {
198 return psa_pk_status_to_mbedtls(destruction_status);
199 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200200
201 /* Load serialized public key into ecp_keypair structure */
202 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
203
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200204#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200205
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200206 (void) prv;
207 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200208
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200209 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200210 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 +0200211
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200212#endif /* MBEDTLS_USE_PSA_CRYPTO */
213}
214
215#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
216/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200217 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200218 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200219 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
220 * functions to handle keys. However, currently psa_import_key() does not
221 * support compressed points. In case that support was explicitly requested,
222 * this fallback uses ECP functions to get the job done. This is the reason
223 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
224 *
225 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
226 * out: will have the public key set.
227 * [in] pub, pub_len: the public key as an ECPoint,
228 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200229 *
230 * Return:
231 * - 0 on success;
232 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
233 * but not supported;
234 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200235 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200236static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
237 const unsigned char *pub,
238 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200239{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200240#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200241 (void) pk;
242 (void) pub;
243 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200244 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200245#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200246 mbedtls_ecp_keypair ecp_key;
247 mbedtls_ecp_group_id ecp_group_id;
248 int ret;
249
250 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
251
252 mbedtls_ecp_keypair_init(&ecp_key);
253 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
254 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200255 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200256 }
257 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200258 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200259 if (ret != 0) {
260 goto exit;
261 }
262 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
263 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200264 &pk->pub_raw_len, pk->pub_raw,
265 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200266
267exit:
268 mbedtls_ecp_keypair_free(&ecp_key);
269 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200270#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
271}
272#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
273
274/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200275 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200276 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200277 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
278 * out: will have the public key set.
279 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200280 *
281 * Return:
282 * - 0 on success;
283 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
284 * but not supported;
285 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200286 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200287static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
288 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200289{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200290#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200291
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200292 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200293 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
294 /* Format directly supported by PSA:
295 * - non-Weierstrass curves that only have one format;
296 * - uncompressed format for Weierstrass curves. */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200297 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200298 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
299 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200300 memcpy(pk->pub_raw, pub, pub_len);
301 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200302 } else {
303 /* Other format, try the fallback */
304 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
305 if (ret != 0) {
306 return ret;
307 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200309
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200310 /* Validate the key by trying to import it */
311 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
312 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
313
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200314 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200315 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
316 psa_set_key_bits(&key_attrs, pk->ec_bits);
317
318 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200319 &key_id) != PSA_SUCCESS) ||
320 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
321 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200323
324 return 0;
325
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200326#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200327
328 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200329 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200330 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
331 if (ret != 0) {
332 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200334 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
335
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200336#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200337}
338
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200339/***********************************************************************
340 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200341 * Low-level ECC parsing: optional support for SpecifiedECDomain
342 *
343 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200344 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200345 * - pk_ecc_group_id_from_specified()
346 *
347 * All the other functions are internal to this section.
348 *
349 * The two "public" functions have a dummy variant provided
350 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
351 * abstraction layer for this macro, which should not appear outside
352 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200353 *
354 **********************************************************************/
355
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200356#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
357/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200358static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200359{
360 (void) tag;
361 return 0;
362}
363
364/* See the "real" version for documentation */
365static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
366 mbedtls_ecp_group_id *grp_id)
367{
368 (void) params;
369 (void) grp_id;
370 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
371}
372#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
373/*
374 * Tell if the passed tag might be the start of SpecifiedECDomain
375 * (that is, a sequence).
376 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200377static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200378{
379 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
380}
381
Paul Bakker1a7550a2013-09-15 13:01:22 +0200382/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
384 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200385 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100386 * if it was encoded compressed.
387 *
388 * SpecifiedECDomain ::= SEQUENCE {
389 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
390 * fieldID FieldID {{FieldTypes}},
391 * curve Curve,
392 * base ECPoint,
393 * order INTEGER,
394 * cofactor INTEGER OPTIONAL,
395 * hash HashAlgorithm OPTIONAL,
396 * ...
397 * }
398 *
399 * We only support prime-field as field type, and ignore hash and cofactor.
400 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402{
Janos Follath24eed8d2019-11-22 13:21:35 +0000403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200405 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100406 const unsigned char *end_field, *end_curve;
407 size_t len;
408 int ver;
409
410 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
413 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (ver < 1 || ver > 3) {
416 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418
419 /*
420 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
421 * fieldType FIELD-ID.&id({IOSet}),
422 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
423 * }
424 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
426 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
427 return ret;
428 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430 end_field = p + len;
431
432 /*
433 * FIELD-ID ::= TYPE-IDENTIFIER
434 * FieldTypes FIELD-ID ::= {
435 * { Prime-p IDENTIFIED BY prime-field } |
436 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
437 * }
438 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
441 return ret;
442 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
445 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
446 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447 }
448
449 p += len;
450
451 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
454 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (p != end_field) {
459 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
460 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
461 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
463 /*
464 * Curve ::= SEQUENCE {
465 * a FieldElement,
466 * b FieldElement,
467 * seed BIT STRING OPTIONAL
468 * -- Shall be present if used in SpecifiedECDomain
469 * -- with version equal to ecdpVer2 or ecdpVer3
470 * }
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
473 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
474 return ret;
475 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100476
477 end_curve = p + len;
478
479 /*
480 * FieldElement ::= OCTET STRING
481 * containing an integer in the case of a prime field
482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
484 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100486 }
487
488 p += len;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
491 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100493 }
494
495 p += len;
496
497 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100499 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (p != end_curve) {
503 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
504 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
505 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100506
507 /*
508 * ECPoint ::= OCTET STRING
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
512 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
515 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100516 /*
517 * If we can't read the point because it's compressed, cheat by
518 * reading only the X coordinate and the parity bit of Y.
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
521 (p[0] != 0x02 && p[0] != 0x03) ||
522 len != mbedtls_mpi_size(&grp->P) + 1 ||
523 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
524 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
525 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
526 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100527 }
528 }
529
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100530 p += len;
531
532 /*
533 * order INTEGER
534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
536 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
537 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100540
541 /*
542 * Allow optional elements by purposefully not enforcing p == end here.
543 */
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100546}
547
548/*
549 * Find the group id associated with an (almost filled) group as generated by
550 * pk_group_from_specified(), or return an error if unknown.
551 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552static 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 +0100553{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100554 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_ecp_group ref;
556 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100561 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_ecp_group_free(&ref);
563 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100564
565 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
567 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
568 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
569 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
570 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
571 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
572 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100573 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 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 +0100575 break;
576 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100577 }
578
579cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100581
582 *grp_id = *id;
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100589}
590
591/*
592 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
593 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200594static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
595 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100596{
Janos Follath24eed8d2019-11-22 13:21:35 +0000597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100603 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100607
608cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000609 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200610 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000611 * temporary grp breaks that flow and it's members are populated
612 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
613 * which is assuming a group populated by _setup() may not clean-up
614 * properly -> Manually free it's members.
615 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000616 mbedtls_mpi_free(&grp.N);
617 mbedtls_mpi_free(&grp.P);
618 mbedtls_mpi_free(&grp.A);
619 mbedtls_mpi_free(&grp.B);
620 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100625
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200626/***********************************************************************
627 *
628 * Unsorted (yet!) from this point on until the next section header
629 *
630 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200631
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200632/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200633 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200634 * ECParameters ::= CHOICE {
635 * namedCurve OBJECT IDENTIFIER
636 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
637 * -- implicitCurve NULL
638 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200639 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200640static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
641 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200642{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200644
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200645 if (end - *p < 1) {
646 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
647 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200648 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200649
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200650 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200651 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200652 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200653 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
655 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
656 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200657
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200658 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
660 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200661
662 params->p = *p;
663 *p += params->len;
664
665 if (*p != end) {
666 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
667 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
668 }
669
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200670 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200671}
672
673/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100675 *
676 * ECParameters ::= CHOICE {
677 * namedCurve OBJECT IDENTIFIER
678 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
679 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200681static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682{
Janos Follath24eed8d2019-11-22 13:21:35 +0000683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (params->tag == MBEDTLS_ASN1_OID) {
687 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
688 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
689 }
690 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200691 ret = pk_ecc_group_id_from_specified(params, &grp_id);
692 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return ret;
694 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100695 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200697 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698}
699
Jethro Beekman01672442023-04-19 14:08:14 +0200700#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
701
702/*
703 * Load an RFC8410 EC key, which doesn't have any parameters
704 */
705static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
706 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200707 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200708{
709 if (params->tag != 0 || params->len != 0) {
710 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
711 }
712
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200713 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200714}
715
716/*
717 * Parse an RFC 8410 encoded private EC key
718 *
719 * CurvePrivateKey ::= OCTET STRING
720 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200721static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200722 unsigned char *key, size_t keylen, const unsigned char *end,
723 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
724{
725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
726 size_t len;
727
728 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
729 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
730 }
731
732 if (key + len != end) {
733 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
734 }
735
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200736 /*
737 * Load the private key
738 */
739 ret = pk_ecc_set_key(pk, key, len);
740 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200741 return ret;
742 }
Jethro Beekman01672442023-04-19 14:08:14 +0200743
Valerio Setti4064dbb2023-05-17 15:33:07 +0200744 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
745 * which never contain a public key. As such, derive the public key
746 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200747 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200748 return ret;
749 }
750
Jethro Beekman01672442023-04-19 14:08:14 +0200751 return 0;
752}
753#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200754
Valerio Setti81d75122023-06-14 14:49:33 +0200755#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758/*
759 * RSAPublicKey ::= SEQUENCE {
760 * modulus INTEGER, -- n
761 * publicExponent INTEGER -- e
762 * }
763 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int pk_get_rsapubkey(unsigned char **p,
765 const unsigned char *end,
766 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200767{
Janos Follath24eed8d2019-11-22 13:21:35 +0000768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769 size_t len;
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
772 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
774 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (*p + len != end) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
779 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100781 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
787 NULL, 0, NULL, 0)) != 0) {
788 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
789 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790
791 *p += len;
792
793 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
796 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
799 NULL, 0, *p, len)) != 0) {
800 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
801 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802
803 *p += len;
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (mbedtls_rsa_complete(rsa) != 0 ||
806 mbedtls_rsa_check_pubkey(rsa) != 0) {
807 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000808 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (*p != end) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
812 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
813 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818
819/* Get a PK algorithm identifier
820 *
821 * AlgorithmIdentifier ::= SEQUENCE {
822 * algorithm OBJECT IDENTIFIER,
823 * parameters ANY DEFINED BY algorithm OPTIONAL }
824 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825static int pk_get_pk_alg(unsigned char **p,
826 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200827 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
828 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200829{
Janos Follath24eed8d2019-11-22 13:21:35 +0000830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
836 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
837 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838
Jethro Beekman01672442023-04-19 14:08:14 +0200839 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200840#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200841 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
842 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
843 if (ret == 0) {
844 *pk_alg = MBEDTLS_PK_ECKEY;
845 }
846 }
847#else
848 (void) ec_grp_id;
849#endif
850 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
852 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853
854 /*
855 * No parameters with RSA (only for EC)
856 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (*pk_alg == MBEDTLS_PK_RSA &&
858 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
859 params->len != 0)) {
860 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861 }
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864}
865
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200866/* Helper for Montgomery curves */
867#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
868#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
869 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
870#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
871
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872/*
873 * SubjectPublicKeyInfo ::= SEQUENCE {
874 * algorithm AlgorithmIdentifier,
875 * subjectPublicKey BIT STRING }
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
878 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879{
Janos Follath24eed8d2019-11-22 13:21:35 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_asn1_buf alg_params;
883 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200884 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
888 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
889 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890 }
891
892 end = *p + len;
893
Jethro Beekman01672442023-04-19 14:08:14 +0200894 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return ret;
896 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
900 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (*p + len != end) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
904 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
908 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
909 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
912 return ret;
913 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (pk_alg == MBEDTLS_PK_RSA) {
917 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200920#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200922#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200923 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200924 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200925 } else
926#endif
927 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200928 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200929 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000931 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200932 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200935#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (ret == 0 && *p != end) {
939 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
940 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
941 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (ret != 0) {
944 mbedtls_pk_free(pk);
945 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948}
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100952 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
953 *
954 * The value zero is:
955 * - never a valid value for an RSA parameter
956 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
957 *
958 * Since values can't be omitted in PKCS#1, passing a zero value to
959 * rsa_complete() would be incorrect, so reject zero values early.
960 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961static int asn1_get_nonzero_mpi(unsigned char **p,
962 const unsigned char *end,
963 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100964{
965 int ret;
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 ret = mbedtls_asn1_get_mpi(p, end, X);
968 if (ret != 0) {
969 return ret;
970 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
973 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
974 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100977}
978
979/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980 * Parse a PKCS#1 encoded private RSA key
981 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
983 const unsigned char *key,
984 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100986 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987 size_t len;
988 unsigned char *p, *end;
989
Hanno Beckerefa14e82017-10-11 19:45:19 +0100990 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100992
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993 p = (unsigned char *) key;
994 end = p + keylen;
995
996 /*
997 * This function parses the RSAPrivateKey (PKCS#1)
998 *
999 * RSAPrivateKey ::= SEQUENCE {
1000 * version Version,
1001 * modulus INTEGER, -- n
1002 * publicExponent INTEGER, -- e
1003 * privateExponent INTEGER, -- d
1004 * prime1 INTEGER, -- p
1005 * prime2 INTEGER, -- q
1006 * exponent1 INTEGER, -- d mod (p-1)
1007 * exponent2 INTEGER, -- d mod (q-1)
1008 * coefficient INTEGER, -- (inverse of q) mod p
1009 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1010 * }
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1013 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1014 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015 }
1016
1017 end = p + len;
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1020 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 }
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (version != 0) {
1024 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 }
1026
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001027 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1029 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1030 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001031 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001034 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1036 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1037 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001038 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001040
1041 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1043 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1044 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001047
1048 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1050 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1051 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001054
1055 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1057 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1058 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001062#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001063 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1065 * that they can be easily recomputed from D, P and Q. However by
1066 * parsing them from the PKCS1 structure it is possible to avoid
1067 * recalculating them which both reduces the overhead of loading
1068 * RSA private keys into memory and also avoids side channels which
1069 * can arise when computing those values, since all of D, P, and Q
1070 * are secret. See https://eprint.iacr.org/2020/055 for a
1071 * description of one such attack.
1072 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001073
Jack Lloyd80cc8112020-01-22 17:34:29 -05001074 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1076 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1077 goto cleanup;
1078 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001079
1080 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1082 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1083 goto cleanup;
1084 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001085
1086 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1088 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1089 goto cleanup;
1090 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001091
Jack Lloyd60239752020-01-27 17:53:36 -05001092#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001093 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1095 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1096 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1097 goto cleanup;
1098 }
Jack Lloyd60239752020-01-27 17:53:36 -05001099#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001100
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001101 /* rsa_complete() doesn't complete anything with the default
1102 * implementation but is still called:
1103 * - for the benefit of alternative implementation that may want to
1104 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1105 * - as is also sanity-checks the key
1106 *
1107 * Furthermore, we also check the public part for consistency with
1108 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1111 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001112 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001113 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (p != end) {
1116 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1117 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 }
1119
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001120cleanup:
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001125 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if ((ret & 0xff80) == 0) {
1127 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1128 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001129 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138
Valerio Setti81d75122023-06-14 14:49:33 +02001139#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140/*
1141 * Parse a SEC1 encoded private EC key
1142 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001143static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 const unsigned char *key, size_t keylen,
1145 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146{
Janos Follath24eed8d2019-11-22 13:21:35 +00001147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001148 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001149 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001150 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001152 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 unsigned char *end = p + keylen;
1154 unsigned char *end2;
1155
1156 /*
1157 * RFC 5915, or SEC1 Appendix C.4
1158 *
1159 * ECPrivateKey ::= SEQUENCE {
1160 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1161 * privateKey OCTET STRING,
1162 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1163 * publicKey [1] BIT STRING OPTIONAL
1164 * }
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1168 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 }
1170
1171 end = p + len;
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1175 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (version != 1) {
1178 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1179 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1183 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184
Valerio Setti6b062ee2023-06-30 17:32:57 +02001185 /* Keep a reference to the position fo the private key. It will be used
1186 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001187 d = p;
1188 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001189
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 p += len;
1191
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001192 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001194 /*
1195 * Is 'parameters' present?
1196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1198 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1199 0)) == 0) {
1200 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001201 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001206 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001207 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001208
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001209 /*
1210 * Load the private key
1211 */
1212 ret = pk_ecc_set_key(pk, d, d_len);
1213 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001214 return ret;
1215 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218 /*
1219 * Is 'publickey' present? If not, or if we can't read it (eg because it
1220 * is compressed), create it from the private key.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1223 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1224 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001225 end2 = p + len;
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1229 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (p + len != end2) {
1232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1233 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1234 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001236 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001239 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001240 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001241 * is if the point format is not recognized.
1242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1244 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1245 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001251
Valerio Setti34f67552023-04-03 15:19:18 +02001252 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001253 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001254 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001255 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001256 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259}
Valerio Setti81d75122023-06-14 14:49:33 +02001260#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001262/***********************************************************************
1263 *
1264 * PKCS#8 parsing functions
1265 *
1266 **********************************************************************/
1267
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268/*
1269 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001270 *
1271 * Notes:
1272 *
1273 * - This function does not own the key buffer. It is the
1274 * responsibility of the caller to take care of zeroizing
1275 * and freeing it after use.
1276 *
1277 * - The function is responsible for freeing the provided
1278 * PK context on failure.
1279 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 */
1281static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_pk_context *pk,
1283 const unsigned char *key, size_t keylen,
1284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285{
1286 int ret, version;
1287 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289 unsigned char *p = (unsigned char *) key;
1290 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001292 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294
Valerio Setti81d75122023-06-14 14:49:33 +02001295#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001296 (void) f_rng;
1297 (void) p_rng;
1298#endif
1299
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001301 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 *
1303 * PrivateKeyInfo ::= SEQUENCE {
1304 * version Version,
1305 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1306 * privateKey PrivateKey,
1307 * attributes [0] IMPLICIT Attributes OPTIONAL }
1308 *
1309 * Version ::= INTEGER
1310 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1311 * PrivateKey ::= OCTET STRING
1312 *
1313 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1314 */
1315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1317 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 }
1320
1321 end = p + len;
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001325 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (version != 0) {
1328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1329 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330
Jethro Beekman01672442023-04-19 14:08:14 +02001331 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 return ret;
1333 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1337 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (len < 1) {
1340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1341 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1342 }
1343
1344 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1345 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1346 }
1347
1348 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1349 return ret;
1350 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (pk_alg == MBEDTLS_PK_RSA) {
1354 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1355 mbedtls_pk_free(pk);
1356 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 }
1358 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001360#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001362#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001363 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001364 if ((ret =
1365 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001366 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001367 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001368 p_rng)) != 0) {
1369 mbedtls_pk_free(pk);
1370 return ret;
1371 }
1372 } else
1373#endif
1374 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001375 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1376 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001377 mbedtls_pk_free(pk);
1378 return ret;
1379 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 }
1381 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001382#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001385 end = p + len;
1386 if (end != (key + keylen)) {
1387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1389 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392}
1393
1394/*
1395 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001396 *
1397 * To save space, the decryption happens in-place on the given key buffer.
1398 * Also, while this function may modify the keybuffer, it doesn't own it,
1399 * and instead it is the responsibility of the caller to zeroize and properly
1400 * free it after use.
1401 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001404MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 mbedtls_pk_context *pk,
1406 unsigned char *key, size_t keylen,
1407 const unsigned char *pwd, size_t pwdlen,
1408 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001410 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001412 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie86677d2023-10-12 16:05:10 +02001415#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mbedtls_cipher_type_t cipher_alg;
1417 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001419 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420
Hanno Becker2aa80a72017-09-07 15:28:45 +01001421 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 end = p + keylen;
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (pwdlen == 0) {
1425 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1426 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427
1428 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001429 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 *
1431 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1432 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1433 * encryptedData EncryptedData
1434 * }
1435 *
1436 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1437 *
1438 * EncryptedData ::= OCTET STRING
1439 *
1440 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001441 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1444 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 }
1447
1448 end = p + len;
1449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1451 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1452 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1456 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
Hanno Beckerfab35692017-08-25 13:38:26 +01001458 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459
1460 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001461 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462 */
Valerio Settie86677d2023-10-12 16:05:10 +02001463#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001465 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001466 cipher_alg, md_alg,
1467 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1469 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1470 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001474
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001475 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_PKCS12_C */
Valerio Settie86677d2023-10-12 16:05:10 +02001478#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001480 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1481 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1483 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1484 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001488
1489 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001492 {
1493 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001494 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (decrypted == 0) {
1497 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1498 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001499 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001503/***********************************************************************
1504 *
1505 * Top-level functions, with format auto-discovery
1506 *
1507 **********************************************************************/
1508
Paul Bakker1a7550a2013-09-15 13:01:22 +02001509/*
1510 * Parse a private key
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1513 const unsigned char *key, size_t keylen,
1514 const unsigned char *pwd, size_t pwdlen,
1515 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516{
Janos Follath24eed8d2019-11-22 13:21:35 +00001517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001522#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (keylen == 0) {
1525 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1526 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001527
1528#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001532 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001534 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 } else {
1536 ret = mbedtls_pem_read_buffer(&pem,
1537 "-----BEGIN RSA PRIVATE KEY-----",
1538 "-----END RSA PRIVATE KEY-----",
1539 key, pwd, pwdlen, &len);
1540 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (ret == 0) {
1543 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1544 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1545 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1546 pem.buf, pem.buflen)) != 0) {
1547 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548 }
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 mbedtls_pem_free(&pem);
1551 return ret;
1552 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1553 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1554 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1555 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1556 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1557 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560
Valerio Setti81d75122023-06-14 14:49:33 +02001561#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001562 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001564 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 } else {
1566 ret = mbedtls_pem_read_buffer(&pem,
1567 "-----BEGIN EC PRIVATE KEY-----",
1568 "-----END EC PRIVATE KEY-----",
1569 key, pwd, pwdlen, &len);
1570 }
1571 if (ret == 0) {
1572 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001575 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 pem.buf, pem.buflen,
1577 f_rng, p_rng)) != 0) {
1578 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001579 }
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mbedtls_pem_free(&pem);
1582 return ret;
1583 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1584 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1585 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1586 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1587 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1588 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 }
Valerio Setti81d75122023-06-14 14:49:33 +02001590#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001592 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001594 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 } else {
1596 ret = mbedtls_pem_read_buffer(&pem,
1597 "-----BEGIN PRIVATE KEY-----",
1598 "-----END PRIVATE KEY-----",
1599 key, NULL, 0, &len);
1600 }
1601 if (ret == 0) {
1602 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1603 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1604 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001605 }
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_pem_free(&pem);
1608 return ret;
1609 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1610 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001611 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001614 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001616 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 } else {
1618 ret = mbedtls_pem_read_buffer(&pem,
1619 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1620 "-----END ENCRYPTED PRIVATE KEY-----",
1621 key, NULL, 0, &len);
1622 }
1623 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001624 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1625 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001627 }
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 mbedtls_pem_free(&pem);
1630 return ret;
1631 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1632 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001635#else
1636 ((void) pwd);
1637 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001639
1640 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001641 * At this point we only know it's not a PEM formatted key. Could be any
1642 * of the known DER encoded private key formats
1643 *
1644 * We try the different DER format parsers to see if one passes without
1645 * error
1646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001649 unsigned char *key_copy;
1650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1652 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1653 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001656
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001657 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1658 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001659
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001660 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 }
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ret == 0) {
1664 return 0;
1665 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_pk_free(pk);
1668 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1671 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1676 if (ret == 0) {
1677 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001678 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 mbedtls_pk_free(pk);
1681 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1686 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1687 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1688 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 mbedtls_pk_free(pk);
1692 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694
Valerio Setti81d75122023-06-14 14:49:33 +02001695#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1697 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001698 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 key, keylen, f_rng, p_rng) == 0) {
1700 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001703#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001704
Valerio Setti81d75122023-06-14 14:49:33 +02001705 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001706 * it is ok to leave the PK context initialized but not
1707 * freed: It is the caller's responsibility to call pk_init()
1708 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001709 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001710 * isn't, this leads to mbedtls_pk_free() being called
1711 * twice, once here and once by the caller, but this is
1712 * also ok and in line with the mbedtls_pk_free() calls
1713 * on failed PEM parsing attempts. */
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001716}
1717
1718/*
1719 * Parse a public key
1720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1722 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001723{
Janos Follath24eed8d2019-11-22 13:21:35 +00001724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001726#if defined(MBEDTLS_RSA_C)
1727 const mbedtls_pk_info_t *pk_info;
1728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001732#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (keylen == 0) {
1735 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1736 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001737
1738#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001740#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001741 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001743 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 } else {
1745 ret = mbedtls_pem_read_buffer(&pem,
1746 "-----BEGIN RSA PUBLIC KEY-----",
1747 "-----END RSA PUBLIC KEY-----",
1748 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001749 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001750
1751 if (ret == 0) {
1752 p = pem.buf;
1753 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1754 mbedtls_pem_free(&pem);
1755 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1756 }
1757
1758 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1759 mbedtls_pem_free(&pem);
1760 return ret;
1761 }
1762
1763 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1764 mbedtls_pk_free(ctx);
1765 }
1766
1767 mbedtls_pem_free(&pem);
1768 return ret;
1769 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1770 mbedtls_pem_free(&pem);
1771 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001772 }
1773#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001774
1775 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001777 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 } else {
1779 ret = mbedtls_pem_read_buffer(&pem,
1780 "-----BEGIN PUBLIC KEY-----",
1781 "-----END PUBLIC KEY-----",
1782 key, NULL, 0, &len);
1783 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001786 /*
1787 * Was PEM encoded
1788 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001789 p = pem.buf;
1790
Jethro Beekman01672442023-04-19 14:08:14 +02001791 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 mbedtls_pem_free(&pem);
1793 return ret;
1794 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1795 mbedtls_pem_free(&pem);
1796 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001800
1801#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1803 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001804 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001805
1806 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1807 return ret;
1808 }
1809
1810 p = (unsigned char *) key;
1811 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1812 if (ret == 0) {
1813 return ret;
1814 }
1815 mbedtls_pk_free(ctx);
1816 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1817 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1818 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001819 }
1820#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001821 p = (unsigned char *) key;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001826}
1827
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001828/***********************************************************************
1829 *
1830 * Top-level functions, with filesystem support
1831 *
1832 **********************************************************************/
1833
1834#if defined(MBEDTLS_FS_IO)
1835/*
1836 * Load all data from a file into a given buffer.
1837 *
1838 * The file is expected to contain either PEM or DER encoded data.
1839 * A terminating null byte is always appended. It is included in the announced
1840 * length only if the data looks like it is PEM encoded.
1841 */
1842int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1843{
1844 FILE *f;
1845 long size;
1846
1847 if ((f = fopen(path, "rb")) == NULL) {
1848 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1849 }
1850
1851 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1852 mbedtls_setbuf(f, NULL);
1853
1854 fseek(f, 0, SEEK_END);
1855 if ((size = ftell(f)) == -1) {
1856 fclose(f);
1857 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1858 }
1859 fseek(f, 0, SEEK_SET);
1860
1861 *n = (size_t) size;
1862
1863 if (*n + 1 == 0 ||
1864 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1865 fclose(f);
1866 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1867 }
1868
1869 if (fread(*buf, 1, *n, f) != *n) {
1870 fclose(f);
1871
1872 mbedtls_zeroize_and_free(*buf, *n);
1873
1874 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1875 }
1876
1877 fclose(f);
1878
1879 (*buf)[*n] = '\0';
1880
1881 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1882 ++*n;
1883 }
1884
1885 return 0;
1886}
1887
1888/*
1889 * Load and parse a private key
1890 */
1891int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1892 const char *path, const char *pwd,
1893 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1894{
1895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1896 size_t n;
1897 unsigned char *buf;
1898
1899 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1900 return ret;
1901 }
1902
1903 if (pwd == NULL) {
1904 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1905 } else {
1906 ret = mbedtls_pk_parse_key(ctx, buf, n,
1907 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1908 }
1909
1910 mbedtls_zeroize_and_free(buf, n);
1911
1912 return ret;
1913}
1914
1915/*
1916 * Load and parse a public key
1917 */
1918int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1919{
1920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1921 size_t n;
1922 unsigned char *buf;
1923
1924 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1925 return ret;
1926 }
1927
1928 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1929
1930 mbedtls_zeroize_and_free(buf, n);
1931
1932 return ret;
1933}
1934#endif /* MBEDTLS_FS_IO */
1935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_PK_PARSE_C */