blob: 1f6133e01e829e3b5e37a466aaf5f426b4e64f1f [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"
Valerio Setti3cc486a2023-11-30 08:09:47 +010018#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020019
Rich Evans00ab4702015-02-06 13:43:58 +000020#include <string.h>
21
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020022#if defined(MBEDTLS_USE_PSA_CRYPTO)
23#include "mbedtls/psa_util.h"
24#include "psa/crypto.h"
25#endif
26
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020027/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030#endif
Valerio Setti81d75122023-06-14 14:49:33 +020031#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020032#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020033#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
866/*
867 * SubjectPublicKeyInfo ::= SEQUENCE {
868 * algorithm AlgorithmIdentifier,
869 * subjectPublicKey BIT STRING }
870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
872 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873{
Janos Follath24eed8d2019-11-22 13:21:35 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_asn1_buf alg_params;
877 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200878 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
882 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
883 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884 }
885
886 end = *p + len;
887
Jethro Beekman01672442023-04-19 14:08:14 +0200888 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return ret;
890 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
893 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
894 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (*p + len != end) {
897 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
899 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
902 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
903 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
906 return ret;
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (pk_alg == MBEDTLS_PK_RSA) {
911 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200914#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200916#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200917 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200918 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200919 } else
920#endif
921 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200922 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200923 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200925 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
926 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200929#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (ret == 0 && *p != end) {
933 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
934 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ret != 0) {
938 mbedtls_pk_free(pk);
939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942}
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100946 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
947 *
948 * The value zero is:
949 * - never a valid value for an RSA parameter
950 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
951 *
952 * Since values can't be omitted in PKCS#1, passing a zero value to
953 * rsa_complete() would be incorrect, so reject zero values early.
954 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955static int asn1_get_nonzero_mpi(unsigned char **p,
956 const unsigned char *end,
957 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100958{
959 int ret;
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 ret = mbedtls_asn1_get_mpi(p, end, X);
962 if (ret != 0) {
963 return ret;
964 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
967 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
968 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100971}
972
973/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974 * Parse a PKCS#1 encoded private RSA key
975 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
977 const unsigned char *key,
978 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100980 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 size_t len;
982 unsigned char *p, *end;
983
Hanno Beckerefa14e82017-10-11 19:45:19 +0100984 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100986
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987 p = (unsigned char *) key;
988 end = p + keylen;
989
990 /*
991 * This function parses the RSAPrivateKey (PKCS#1)
992 *
993 * RSAPrivateKey ::= SEQUENCE {
994 * version Version,
995 * modulus INTEGER, -- n
996 * publicExponent INTEGER, -- e
997 * privateExponent INTEGER, -- d
998 * prime1 INTEGER, -- p
999 * prime2 INTEGER, -- q
1000 * exponent1 INTEGER, -- d mod (p-1)
1001 * exponent2 INTEGER, -- d mod (q-1)
1002 * coefficient INTEGER, -- (inverse of q) mod p
1003 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1004 * }
1005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1007 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1008 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 }
1010
1011 end = p + len;
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1014 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015 }
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (version != 0) {
1018 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019 }
1020
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001021 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1023 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1024 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001025 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001028 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1030 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1031 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001032 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001034
1035 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1037 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1038 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041
1042 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1044 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1045 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048
1049 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1051 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1052 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001053 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001056#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001057 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1059 * that they can be easily recomputed from D, P and Q. However by
1060 * parsing them from the PKCS1 structure it is possible to avoid
1061 * recalculating them which both reduces the overhead of loading
1062 * RSA private keys into memory and also avoids side channels which
1063 * can arise when computing those values, since all of D, P, and Q
1064 * are secret. See https://eprint.iacr.org/2020/055 for a
1065 * description of one such attack.
1066 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001067
Jack Lloyd80cc8112020-01-22 17:34:29 -05001068 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1070 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1071 goto cleanup;
1072 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001073
1074 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1076 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1077 goto cleanup;
1078 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001079
1080 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1082 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1083 goto cleanup;
1084 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001085
Jack Lloyd60239752020-01-27 17:53:36 -05001086#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001087 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1089 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1090 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1091 goto cleanup;
1092 }
Jack Lloyd60239752020-01-27 17:53:36 -05001093#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001094
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001095 /* rsa_complete() doesn't complete anything with the default
1096 * implementation but is still called:
1097 * - for the benefit of alternative implementation that may want to
1098 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1099 * - as is also sanity-checks the key
1100 *
1101 * Furthermore, we also check the public part for consistency with
1102 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1105 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001106 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001107 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (p != end) {
1110 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1111 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 }
1113
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001114cleanup:
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001119 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret & 0xff80) == 0) {
1121 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1122 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001123 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127 }
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132
Valerio Setti81d75122023-06-14 14:49:33 +02001133#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134/*
1135 * Parse a SEC1 encoded private EC key
1136 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001137static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 const unsigned char *key, size_t keylen,
1139 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140{
Janos Follath24eed8d2019-11-22 13:21:35 +00001141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001142 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001143 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001144 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001146 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 unsigned char *end = p + keylen;
1148 unsigned char *end2;
1149
1150 /*
1151 * RFC 5915, or SEC1 Appendix C.4
1152 *
1153 * ECPrivateKey ::= SEQUENCE {
1154 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1155 * privateKey OCTET STRING,
1156 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1157 * publicKey [1] BIT STRING OPTIONAL
1158 * }
1159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1161 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1162 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
1164
1165 end = p + len;
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1168 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1169 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (version != 1) {
1172 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1173 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1176 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1177 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178
Valerio Setti6b062ee2023-06-30 17:32:57 +02001179 /* Keep a reference to the position fo the private key. It will be used
1180 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001181 d = p;
1182 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001183
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 p += len;
1185
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001186 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001188 /*
1189 * Is 'parameters' present?
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1192 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1193 0)) == 0) {
1194 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001195 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001200 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001201 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001203 /*
1204 * Load the private key
1205 */
1206 ret = pk_ecc_set_key(pk, d, d_len);
1207 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001208 return ret;
1209 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212 /*
1213 * Is 'publickey' present? If not, or if we can't read it (eg because it
1214 * is compressed), create it from the private key.
1215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1217 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1218 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001219 end2 = p + len;
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1222 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1223 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (p + len != end2) {
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1227 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1228 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001229
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001230 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001231 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001234 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235 * is if the point format is not recognized.
1236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1238 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1239 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001245
Valerio Setti34f67552023-04-03 15:19:18 +02001246 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001247 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001248 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001249 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001250 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253}
Valerio Setti81d75122023-06-14 14:49:33 +02001254#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001256/***********************************************************************
1257 *
1258 * PKCS#8 parsing functions
1259 *
1260 **********************************************************************/
1261
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262/*
1263 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001264 *
1265 * Notes:
1266 *
1267 * - This function does not own the key buffer. It is the
1268 * responsibility of the caller to take care of zeroizing
1269 * and freeing it after use.
1270 *
1271 * - The function is responsible for freeing the provided
1272 * PK context on failure.
1273 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 */
1275static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 mbedtls_pk_context *pk,
1277 const unsigned char *key, size_t keylen,
1278 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279{
1280 int ret, version;
1281 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 unsigned char *p = (unsigned char *) key;
1284 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001286 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288
Valerio Setti81d75122023-06-14 14:49:33 +02001289#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001290 (void) f_rng;
1291 (void) p_rng;
1292#endif
1293
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001295 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 *
1297 * PrivateKeyInfo ::= SEQUENCE {
1298 * version Version,
1299 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1300 * privateKey PrivateKey,
1301 * attributes [0] IMPLICIT Attributes OPTIONAL }
1302 *
1303 * Version ::= INTEGER
1304 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1305 * PrivateKey ::= OCTET STRING
1306 *
1307 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1308 */
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1311 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1312 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 }
1314
1315 end = p + len;
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001319 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (version != 0) {
1322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1323 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324
Jethro Beekman01672442023-04-19 14:08:14 +02001325 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 return ret;
1327 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1331 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (len < 1) {
1334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1335 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1336 }
1337
1338 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1339 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1340 }
1341
1342 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1343 return ret;
1344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (pk_alg == MBEDTLS_PK_RSA) {
1348 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1349 mbedtls_pk_free(pk);
1350 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 }
1352 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001354#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001356#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001357 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001358 if ((ret =
1359 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001360 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001361 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001362 p_rng)) != 0) {
1363 mbedtls_pk_free(pk);
1364 return ret;
1365 }
1366 } else
1367#endif
1368 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001369 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1370 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001371 mbedtls_pk_free(pk);
1372 return ret;
1373 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 }
1375 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001376#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001379 end = p + len;
1380 if (end != (key + keylen)) {
1381 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1382 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1383 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386}
1387
1388/*
1389 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001390 *
1391 * To save space, the decryption happens in-place on the given key buffer.
1392 * Also, while this function may modify the keybuffer, it doesn't own it,
1393 * and instead it is the responsibility of the caller to zeroize and properly
1394 * free it after use.
1395 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001398MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 mbedtls_pk_context *pk,
1400 unsigned char *key, size_t keylen,
1401 const unsigned char *pwd, size_t pwdlen,
1402 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001404 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001406 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie86677d2023-10-12 16:05:10 +02001409#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_cipher_type_t cipher_alg;
1411 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001413 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Hanno Becker2aa80a72017-09-07 15:28:45 +01001415 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 end = p + keylen;
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (pwdlen == 0) {
1419 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1420 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421
1422 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001423 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 *
1425 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1426 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1427 * encryptedData EncryptedData
1428 * }
1429 *
1430 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1431 *
1432 * EncryptedData ::= OCTET STRING
1433 *
1434 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001435 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1438 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1439 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440 }
1441
1442 end = p + len;
1443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1449 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1450 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451
Hanno Beckerfab35692017-08-25 13:38:26 +01001452 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453
1454 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001455 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 */
Valerio Settie86677d2023-10-12 16:05:10 +02001457#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001459 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001460 cipher_alg, md_alg,
1461 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1463 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1464 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001468
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001469 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_PKCS12_C */
Valerio Settie86677d2023-10-12 16:05:10 +02001472#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001474 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1475 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1477 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1478 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001482
1483 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001486 {
1487 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001488 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (decrypted == 0) {
1491 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1492 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001493 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001497/***********************************************************************
1498 *
1499 * Top-level functions, with format auto-discovery
1500 *
1501 **********************************************************************/
1502
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503/*
1504 * Parse a private key
1505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1507 const unsigned char *key, size_t keylen,
1508 const unsigned char *pwd, size_t pwdlen,
1509 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510{
Janos Follath24eed8d2019-11-22 13:21:35 +00001511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001516#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (keylen == 0) {
1519 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1520 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001521
1522#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001526 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001528 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 } else {
1530 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001531 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 key, pwd, pwdlen, &len);
1533 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (ret == 0) {
1536 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1537 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1538 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1539 pem.buf, pem.buflen)) != 0) {
1540 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541 }
1542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 mbedtls_pem_free(&pem);
1544 return ret;
1545 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1546 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1547 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1548 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1549 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1550 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553
Valerio Setti81d75122023-06-14 14:49:33 +02001554#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001555 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001557 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 } else {
1559 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001560 PEM_BEGIN_PRIVATE_KEY_EC,
1561 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 key, pwd, pwdlen, &len);
1563 }
1564 if (ret == 0) {
1565 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001568 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 pem.buf, pem.buflen,
1570 f_rng, p_rng)) != 0) {
1571 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572 }
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 mbedtls_pem_free(&pem);
1575 return ret;
1576 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1577 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1578 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1579 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1580 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1581 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582 }
Valerio Setti81d75122023-06-14 14:49:33 +02001583#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001585 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001587 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 } else {
1589 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001590 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 key, NULL, 0, &len);
1592 }
1593 if (ret == 0) {
1594 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1595 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1596 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597 }
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_pem_free(&pem);
1600 return ret;
1601 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1602 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001606 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001608 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 } else {
1610 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001611 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1612 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 key, NULL, 0, &len);
1614 }
1615 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001616 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1617 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619 }
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 mbedtls_pem_free(&pem);
1622 return ret;
1623 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1624 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001627#else
1628 ((void) pwd);
1629 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001631
1632 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001633 * At this point we only know it's not a PEM formatted key. Could be any
1634 * of the known DER encoded private key formats
1635 *
1636 * We try the different DER format parsers to see if one passes without
1637 * error
1638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001641 unsigned char *key_copy;
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1644 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1645 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001648
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001649 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1650 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001651
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001652 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 }
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ret == 0) {
1656 return 0;
1657 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 mbedtls_pk_free(pk);
1660 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1663 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1668 if (ret == 0) {
1669 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001670 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_pk_free(pk);
1673 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1678 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1679 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1680 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681 }
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 mbedtls_pk_free(pk);
1684 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Valerio Setti81d75122023-06-14 14:49:33 +02001687#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1689 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001690 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 key, keylen, f_rng, p_rng) == 0) {
1692 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001695#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Valerio Setti81d75122023-06-14 14:49:33 +02001697 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001698 * it is ok to leave the PK context initialized but not
1699 * freed: It is the caller's responsibility to call pk_init()
1700 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001701 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001702 * isn't, this leads to mbedtls_pk_free() being called
1703 * twice, once here and once by the caller, but this is
1704 * also ok and in line with the mbedtls_pk_free() calls
1705 * on failed PEM parsing attempts. */
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708}
1709
1710/*
1711 * Parse a public key
1712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1714 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001715{
Janos Follath24eed8d2019-11-22 13:21:35 +00001716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001718#if defined(MBEDTLS_RSA_C)
1719 const mbedtls_pk_info_t *pk_info;
1720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001722 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001724#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (keylen == 0) {
1727 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1728 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001729
1730#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001732#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001733 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001735 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 } else {
1737 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001738 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001740 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001741
1742 if (ret == 0) {
1743 p = pem.buf;
1744 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1745 mbedtls_pem_free(&pem);
1746 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1747 }
1748
1749 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1750 mbedtls_pem_free(&pem);
1751 return ret;
1752 }
1753
1754 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1755 mbedtls_pk_free(ctx);
1756 }
1757
1758 mbedtls_pem_free(&pem);
1759 return ret;
1760 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1761 mbedtls_pem_free(&pem);
1762 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001763 }
1764#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001765
1766 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001768 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 } else {
1770 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001771 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 key, NULL, 0, &len);
1773 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001776 /*
1777 * Was PEM encoded
1778 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001779 p = pem.buf;
1780
Jethro Beekman01672442023-04-19 14:08:14 +02001781 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_pem_free(&pem);
1783 return ret;
1784 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1785 mbedtls_pem_free(&pem);
1786 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001787 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001790
1791#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1793 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001794 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001795
1796 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1797 return ret;
1798 }
1799
1800 p = (unsigned char *) key;
1801 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1802 if (ret == 0) {
1803 return ret;
1804 }
1805 mbedtls_pk_free(ctx);
1806 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1807 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1808 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001809 }
1810#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001811 p = (unsigned char *) key;
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001816}
1817
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001818/***********************************************************************
1819 *
1820 * Top-level functions, with filesystem support
1821 *
1822 **********************************************************************/
1823
1824#if defined(MBEDTLS_FS_IO)
1825/*
1826 * Load all data from a file into a given buffer.
1827 *
1828 * The file is expected to contain either PEM or DER encoded data.
1829 * A terminating null byte is always appended. It is included in the announced
1830 * length only if the data looks like it is PEM encoded.
1831 */
1832int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1833{
1834 FILE *f;
1835 long size;
1836
1837 if ((f = fopen(path, "rb")) == NULL) {
1838 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1839 }
1840
1841 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1842 mbedtls_setbuf(f, NULL);
1843
1844 fseek(f, 0, SEEK_END);
1845 if ((size = ftell(f)) == -1) {
1846 fclose(f);
1847 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1848 }
1849 fseek(f, 0, SEEK_SET);
1850
1851 *n = (size_t) size;
1852
1853 if (*n + 1 == 0 ||
1854 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1855 fclose(f);
1856 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1857 }
1858
1859 if (fread(*buf, 1, *n, f) != *n) {
1860 fclose(f);
1861
1862 mbedtls_zeroize_and_free(*buf, *n);
1863
1864 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1865 }
1866
1867 fclose(f);
1868
1869 (*buf)[*n] = '\0';
1870
1871 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1872 ++*n;
1873 }
1874
1875 return 0;
1876}
1877
1878/*
1879 * Load and parse a private key
1880 */
1881int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1882 const char *path, const char *pwd,
1883 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1884{
1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1886 size_t n;
1887 unsigned char *buf;
1888
1889 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1890 return ret;
1891 }
1892
1893 if (pwd == NULL) {
1894 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1895 } else {
1896 ret = mbedtls_pk_parse_key(ctx, buf, n,
1897 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1898 }
1899
1900 mbedtls_zeroize_and_free(buf, n);
1901
1902 return ret;
1903}
1904
1905/*
1906 * Load and parse a public key
1907 */
1908int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1909{
1910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1911 size_t n;
1912 unsigned char *buf;
1913
1914 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1915 return ret;
1916 }
1917
1918 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1919
1920 mbedtls_zeroize_and_free(buf, n);
1921
1922 return ret;
1923}
1924#endif /* MBEDTLS_FS_IO */
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#endif /* MBEDTLS_PK_PARSE_C */