blob: c33b74203b8e70d50e3043fc6cc198aa0987bca5 [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;
Valerio Settifbbafa02023-12-06 10:07:34 +0100108 psa_key_usage_t flags;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200109 psa_status_t status;
110
111 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Settifbbafa02023-12-06 10:07:34 +0100112 if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
113 /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
114 * the PK module cannot do ECDH. When the key will be used in TLS for
115 * ECDH, it will be exported and then re-imported with proper flags
116 * and algorithm. */
117 flags = PSA_KEY_USAGE_EXPORT;
118 } else {
119 psa_set_key_algorithm(&attributes,
120 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
121 flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
122 PSA_KEY_USAGE_EXPORT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200123 }
124 psa_set_key_usage_flags(&attributes, flags);
125
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200126 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200127 return psa_pk_status_to_mbedtls(status);
128
129#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
130
131 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200132 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200133 if (ret != 0) {
134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
135 }
136 return 0;
137#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
138}
139
140/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200141 * Derive a public key from its private counterpart.
142 * Computationally intensive, only use when public key is not available.
143 *
144 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
145 * out: will have the public key set.
146 * [in] prv, prv_len: the raw private key (see note below).
147 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200148 *
149 * Note: the private key information is always available from pk,
150 * however for convenience the serialized version is also passed,
151 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200152 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200153 *
154 * There are three implementations of this function:
155 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
156 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
157 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200158 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200159static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
160 const unsigned char *prv, size_t prv_len,
161 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200162{
163#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200164
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200165 (void) f_rng;
166 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200167 (void) prv;
168 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200169 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200170
171 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
172 &pk->pub_raw_len);
173 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200174
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200175#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200176
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200177 (void) f_rng;
178 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200179 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200180
181 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200182 size_t curve_bits;
183 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200184
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200185 /* Import private key into PSA, from serialized input */
186 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
187 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200188 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
189 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200190 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200191 if (status != PSA_SUCCESS) {
192 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200193 }
194
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200195 /* Export public key from PSA */
196 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
197 size_t pub_len;
198 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
199 psa_status_t destruction_status = psa_destroy_key(key_id);
200 if (status != PSA_SUCCESS) {
201 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200202 } else if (destruction_status != PSA_SUCCESS) {
203 return psa_pk_status_to_mbedtls(destruction_status);
204 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200205
206 /* Load serialized public key into ecp_keypair structure */
207 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
208
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200209#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200210
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200211 (void) prv;
212 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200213
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200214 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200215 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 +0200216
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200217#endif /* MBEDTLS_USE_PSA_CRYPTO */
218}
219
220#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
221/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200222 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200223 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200224 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
225 * functions to handle keys. However, currently psa_import_key() does not
226 * support compressed points. In case that support was explicitly requested,
227 * this fallback uses ECP functions to get the job done. This is the reason
228 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
229 *
230 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
231 * out: will have the public key set.
232 * [in] pub, pub_len: the public key as an ECPoint,
233 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200234 *
235 * Return:
236 * - 0 on success;
237 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
238 * but not supported;
239 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200240 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200241static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
242 const unsigned char *pub,
243 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200244{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200245#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200246 (void) pk;
247 (void) pub;
248 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200249 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200250#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200251 mbedtls_ecp_keypair ecp_key;
252 mbedtls_ecp_group_id ecp_group_id;
253 int ret;
254
255 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
256
257 mbedtls_ecp_keypair_init(&ecp_key);
258 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
259 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200260 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200261 }
262 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200263 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200264 if (ret != 0) {
265 goto exit;
266 }
267 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
268 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200269 &pk->pub_raw_len, pk->pub_raw,
270 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200271
272exit:
273 mbedtls_ecp_keypair_free(&ecp_key);
274 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200275#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
276}
277#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
278
279/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200280 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200281 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200282 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
283 * out: will have the public key set.
284 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200285 *
286 * Return:
287 * - 0 on success;
288 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
289 * but not supported;
290 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200291 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200292static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
293 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200294{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200295#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200296
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200297 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200298 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
299 /* Format directly supported by PSA:
300 * - non-Weierstrass curves that only have one format;
301 * - uncompressed format for Weierstrass curves. */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200302 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200303 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
304 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200305 memcpy(pk->pub_raw, pub, pub_len);
306 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200307 } else {
308 /* Other format, try the fallback */
309 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
310 if (ret != 0) {
311 return ret;
312 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200314
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200315 /* Validate the key by trying to import it */
316 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
318
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200319 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200320 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
321 psa_set_key_bits(&key_attrs, pk->ec_bits);
322
323 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200324 &key_id) != PSA_SUCCESS) ||
325 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
326 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200328
329 return 0;
330
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200331#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200332
333 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200334 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200335 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
336 if (ret != 0) {
337 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200339 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
340
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200341#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200342}
343
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200344/***********************************************************************
345 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200346 * Low-level ECC parsing: optional support for SpecifiedECDomain
347 *
348 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200349 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200350 * - pk_ecc_group_id_from_specified()
351 *
352 * All the other functions are internal to this section.
353 *
354 * The two "public" functions have a dummy variant provided
355 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
356 * abstraction layer for this macro, which should not appear outside
357 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200358 *
359 **********************************************************************/
360
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200361#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
362/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200363static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200364{
365 (void) tag;
366 return 0;
367}
368
369/* See the "real" version for documentation */
370static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
371 mbedtls_ecp_group_id *grp_id)
372{
373 (void) params;
374 (void) grp_id;
375 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
376}
377#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
378/*
379 * Tell if the passed tag might be the start of SpecifiedECDomain
380 * (that is, a sequence).
381 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200382static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200383{
384 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
385}
386
Paul Bakker1a7550a2013-09-15 13:01:22 +0200387/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
389 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200390 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 * if it was encoded compressed.
392 *
393 * SpecifiedECDomain ::= SEQUENCE {
394 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
395 * fieldID FieldID {{FieldTypes}},
396 * curve Curve,
397 * base ECPoint,
398 * order INTEGER,
399 * cofactor INTEGER OPTIONAL,
400 * hash HashAlgorithm OPTIONAL,
401 * ...
402 * }
403 *
404 * We only support prime-field as field type, and ignore hash and cofactor.
405 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407{
Janos Follath24eed8d2019-11-22 13:21:35 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200410 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 const unsigned char *end_field, *end_curve;
412 size_t len;
413 int ver;
414
415 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
418 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (ver < 1 || ver > 3) {
421 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
422 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423
424 /*
425 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
426 * fieldType FIELD-ID.&id({IOSet}),
427 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
428 * }
429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
431 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
432 return ret;
433 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434
435 end_field = p + len;
436
437 /*
438 * FIELD-ID ::= TYPE-IDENTIFIER
439 * FieldTypes FIELD-ID ::= {
440 * { Prime-p IDENTIFIED BY prime-field } |
441 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
442 * }
443 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
446 return ret;
447 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
450 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
451 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452 }
453
454 p += len;
455
456 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
459 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (p != end_field) {
464 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
465 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
466 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467
468 /*
469 * Curve ::= SEQUENCE {
470 * a FieldElement,
471 * b FieldElement,
472 * seed BIT STRING OPTIONAL
473 * -- Shall be present if used in SpecifiedECDomain
474 * -- with version equal to ecdpVer2 or ecdpVer3
475 * }
476 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
478 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
479 return ret;
480 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100481
482 end_curve = p + len;
483
484 /*
485 * FieldElement ::= OCTET STRING
486 * containing an integer in the case of a prime field
487 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
489 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
490 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100491 }
492
493 p += len;
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
496 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100498 }
499
500 p += len;
501
502 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100504 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (p != end_curve) {
508 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
509 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
510 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100511
512 /*
513 * ECPoint ::= OCTET STRING
514 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
516 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
517 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
520 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100521 /*
522 * If we can't read the point because it's compressed, cheat by
523 * reading only the X coordinate and the parity bit of Y.
524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
526 (p[0] != 0x02 && p[0] != 0x03) ||
527 len != mbedtls_mpi_size(&grp->P) + 1 ||
528 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
529 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
530 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
531 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100532 }
533 }
534
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100535 p += len;
536
537 /*
538 * order INTEGER
539 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
541 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
542 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100545
546 /*
547 * Allow optional elements by purposefully not enforcing p == end here.
548 */
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100551}
552
553/*
554 * Find the group id associated with an (almost filled) group as generated by
555 * pk_group_from_specified(), or return an error if unknown.
556 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557static 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 +0100558{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100559 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_ecp_group ref;
561 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100566 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ecp_group_free(&ref);
568 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100569
570 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
572 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
573 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
574 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
575 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
576 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
577 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100578 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 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 +0100580 break;
581 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100582 }
583
584cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100586
587 *grp_id = *id;
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100594}
595
596/*
597 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
598 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200599static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
600 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100601{
Janos Follath24eed8d2019-11-22 13:21:35 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100608 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100612
613cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000614 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200615 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000616 * temporary grp breaks that flow and it's members are populated
617 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
618 * which is assuming a group populated by _setup() may not clean-up
619 * properly -> Manually free it's members.
620 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000621 mbedtls_mpi_free(&grp.N);
622 mbedtls_mpi_free(&grp.P);
623 mbedtls_mpi_free(&grp.A);
624 mbedtls_mpi_free(&grp.B);
625 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100630
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200631/***********************************************************************
632 *
633 * Unsorted (yet!) from this point on until the next section header
634 *
635 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200636
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200637/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200638 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200639 * ECParameters ::= CHOICE {
640 * namedCurve OBJECT IDENTIFIER
641 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
642 * -- implicitCurve NULL
643 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200644 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200645static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
646 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200647{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200649
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200650 if (end - *p < 1) {
651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
652 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200653 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200654
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200655 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200656 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200657 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200658 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
660 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
661 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200662
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200663 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200664 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
665 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200666
667 params->p = *p;
668 *p += params->len;
669
670 if (*p != end) {
671 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
672 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
673 }
674
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200675 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200676}
677
678/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200679 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100680 *
681 * ECParameters ::= CHOICE {
682 * namedCurve OBJECT IDENTIFIER
683 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
684 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200686static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687{
Janos Follath24eed8d2019-11-22 13:21:35 +0000688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (params->tag == MBEDTLS_ASN1_OID) {
692 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
693 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
694 }
695 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200696 ret = pk_ecc_group_id_from_specified(params, &grp_id);
697 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return ret;
699 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100700 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200702 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703}
704
Jethro Beekman01672442023-04-19 14:08:14 +0200705#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
706
707/*
708 * Load an RFC8410 EC key, which doesn't have any parameters
709 */
710static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
711 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200712 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200713{
714 if (params->tag != 0 || params->len != 0) {
715 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
716 }
717
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200718 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200719}
720
721/*
722 * Parse an RFC 8410 encoded private EC key
723 *
724 * CurvePrivateKey ::= OCTET STRING
725 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200727 unsigned char *key, size_t keylen, const unsigned char *end,
728 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
729{
730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
731 size_t len;
732
733 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
734 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
735 }
736
737 if (key + len != end) {
738 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
739 }
740
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200741 /*
742 * Load the private key
743 */
744 ret = pk_ecc_set_key(pk, key, len);
745 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200746 return ret;
747 }
Jethro Beekman01672442023-04-19 14:08:14 +0200748
Valerio Setti4064dbb2023-05-17 15:33:07 +0200749 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
750 * which never contain a public key. As such, derive the public key
751 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200752 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200753 return ret;
754 }
755
Jethro Beekman01672442023-04-19 14:08:14 +0200756 return 0;
757}
758#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200759
Valerio Setti81d75122023-06-14 14:49:33 +0200760#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200763/*
764 * RSAPublicKey ::= SEQUENCE {
765 * modulus INTEGER, -- n
766 * publicExponent INTEGER -- e
767 * }
768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769static int pk_get_rsapubkey(unsigned char **p,
770 const unsigned char *end,
771 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772{
Janos Follath24eed8d2019-11-22 13:21:35 +0000773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774 size_t len;
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
777 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
779 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (*p + len != end) {
782 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
783 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100786 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
788 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
789 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
792 NULL, 0, NULL, 0)) != 0) {
793 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
794 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100795
796 *p += len;
797
798 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
800 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
801 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
804 NULL, 0, *p, len)) != 0) {
805 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
806 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100807
808 *p += len;
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (mbedtls_rsa_complete(rsa) != 0 ||
811 mbedtls_rsa_check_pubkey(rsa) != 0) {
812 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000813 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (*p != end) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
817 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
818 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200823
824/* Get a PK algorithm identifier
825 *
826 * AlgorithmIdentifier ::= SEQUENCE {
827 * algorithm OBJECT IDENTIFIER,
828 * parameters ANY DEFINED BY algorithm OPTIONAL }
829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830static int pk_get_pk_alg(unsigned char **p,
831 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200832 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
833 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834{
Janos Follath24eed8d2019-11-22 13:21:35 +0000835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
841 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
842 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200843
Jethro Beekman01672442023-04-19 14:08:14 +0200844 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200845#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200846 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
847 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
848 if (ret == 0) {
849 *pk_alg = MBEDTLS_PK_ECKEY;
850 }
851 }
852#else
853 (void) ec_grp_id;
854#endif
855 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
857 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
859 /*
860 * No parameters with RSA (only for EC)
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (*pk_alg == MBEDTLS_PK_RSA &&
863 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
864 params->len != 0)) {
865 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869}
870
871/*
872 * SubjectPublicKeyInfo ::= SEQUENCE {
873 * algorithm AlgorithmIdentifier,
874 * subjectPublicKey BIT STRING }
875 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
877 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_asn1_buf alg_params;
882 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200883 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
887 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
888 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 }
890
891 end = *p + len;
892
Jethro Beekman01672442023-04-19 14:08:14 +0200893 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return ret;
895 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
898 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
899 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (*p + len != end) {
902 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
903 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
904 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
907 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
908 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
911 return ret;
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (pk_alg == MBEDTLS_PK_RSA) {
916 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200919#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200921#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200922 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200923 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200924 } else
925#endif
926 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200927 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200928 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000930 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200931 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200934#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ret == 0 && *p != end) {
938 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
939 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (ret != 0) {
943 mbedtls_pk_free(pk);
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947}
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100951 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
952 *
953 * The value zero is:
954 * - never a valid value for an RSA parameter
955 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
956 *
957 * Since values can't be omitted in PKCS#1, passing a zero value to
958 * rsa_complete() would be incorrect, so reject zero values early.
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960static int asn1_get_nonzero_mpi(unsigned char **p,
961 const unsigned char *end,
962 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100963{
964 int ret;
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = mbedtls_asn1_get_mpi(p, end, X);
967 if (ret != 0) {
968 return ret;
969 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
972 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
973 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976}
977
978/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 * Parse a PKCS#1 encoded private RSA key
980 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
982 const unsigned char *key,
983 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100985 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986 size_t len;
987 unsigned char *p, *end;
988
Hanno Beckerefa14e82017-10-11 19:45:19 +0100989 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100991
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 p = (unsigned char *) key;
993 end = p + keylen;
994
995 /*
996 * This function parses the RSAPrivateKey (PKCS#1)
997 *
998 * RSAPrivateKey ::= SEQUENCE {
999 * version Version,
1000 * modulus INTEGER, -- n
1001 * publicExponent INTEGER, -- e
1002 * privateExponent INTEGER, -- d
1003 * prime1 INTEGER, -- p
1004 * prime2 INTEGER, -- q
1005 * exponent1 INTEGER, -- d mod (p-1)
1006 * exponent2 INTEGER, -- d mod (q-1)
1007 * coefficient INTEGER, -- (inverse of q) mod p
1008 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1009 * }
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1012 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1013 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 }
1015
1016 end = p + len;
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1019 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (version != 0) {
1023 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 }
1025
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001026 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1028 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1029 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001030 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001033 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1035 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1036 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039
1040 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1042 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1043 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001044 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046
1047 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1049 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1050 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001051 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001053
1054 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1056 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1057 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001058 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001061#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001062 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1064 * that they can be easily recomputed from D, P and Q. However by
1065 * parsing them from the PKCS1 structure it is possible to avoid
1066 * recalculating them which both reduces the overhead of loading
1067 * RSA private keys into memory and also avoids side channels which
1068 * can arise when computing those values, since all of D, P, and Q
1069 * are secret. See https://eprint.iacr.org/2020/055 for a
1070 * description of one such attack.
1071 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001072
Jack Lloyd80cc8112020-01-22 17:34:29 -05001073 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1075 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1076 goto cleanup;
1077 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001078
1079 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1081 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1082 goto cleanup;
1083 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084
1085 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1088 goto cleanup;
1089 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001090
Jack Lloyd60239752020-01-27 17:53:36 -05001091#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001092 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1094 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1095 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1096 goto cleanup;
1097 }
Jack Lloyd60239752020-01-27 17:53:36 -05001098#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001099
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001100 /* rsa_complete() doesn't complete anything with the default
1101 * implementation but is still called:
1102 * - for the benefit of alternative implementation that may want to
1103 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1104 * - as is also sanity-checks the key
1105 *
1106 * Furthermore, we also check the public part for consistency with
1107 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1110 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001111 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001112 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (p != end) {
1115 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1116 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 }
1118
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001119cleanup:
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001124 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if ((ret & 0xff80) == 0) {
1126 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1127 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137
Valerio Setti81d75122023-06-14 14:49:33 +02001138#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139/*
1140 * Parse a SEC1 encoded private EC key
1141 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001142static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 const unsigned char *key, size_t keylen,
1144 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145{
Janos Follath24eed8d2019-11-22 13:21:35 +00001146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001147 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001148 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001149 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001151 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 unsigned char *end = p + keylen;
1153 unsigned char *end2;
1154
1155 /*
1156 * RFC 5915, or SEC1 Appendix C.4
1157 *
1158 * ECPrivateKey ::= SEQUENCE {
1159 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1160 * privateKey OCTET STRING,
1161 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1162 * publicKey [1] BIT STRING OPTIONAL
1163 * }
1164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1166 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1167 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 }
1169
1170 end = p + len;
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1173 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1174 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (version != 1) {
1177 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1178 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1182 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
Valerio Setti6b062ee2023-06-30 17:32:57 +02001184 /* Keep a reference to the position fo the private key. It will be used
1185 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001186 d = p;
1187 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001188
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 p += len;
1190
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001191 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001193 /*
1194 * Is 'parameters' present?
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1197 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1198 0)) == 0) {
1199 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001200 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001205 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001206 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001207
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001208 /*
1209 * Load the private key
1210 */
1211 ret = pk_ecc_set_key(pk, d, d_len);
1212 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001213 return ret;
1214 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001217 /*
1218 * Is 'publickey' present? If not, or if we can't read it (eg because it
1219 * is compressed), create it from the private key.
1220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1222 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1223 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001224 end2 = p + len;
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1228 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (p + len != end2) {
1231 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1232 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1233 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001234
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001235 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001236 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001239 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240 * is if the point format is not recognized.
1241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1243 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1244 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001245 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001248 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001250
Valerio Setti34f67552023-04-03 15:19:18 +02001251 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001252 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001253 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001254 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001255 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258}
Valerio Setti81d75122023-06-14 14:49:33 +02001259#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001261/***********************************************************************
1262 *
1263 * PKCS#8 parsing functions
1264 *
1265 **********************************************************************/
1266
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267/*
1268 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001269 *
1270 * Notes:
1271 *
1272 * - This function does not own the key buffer. It is the
1273 * responsibility of the caller to take care of zeroizing
1274 * and freeing it after use.
1275 *
1276 * - The function is responsible for freeing the provided
1277 * PK context on failure.
1278 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 */
1280static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_pk_context *pk,
1282 const unsigned char *key, size_t keylen,
1283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284{
1285 int ret, version;
1286 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 unsigned char *p = (unsigned char *) key;
1289 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001291 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Valerio Setti81d75122023-06-14 14:49:33 +02001294#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001295 (void) f_rng;
1296 (void) p_rng;
1297#endif
1298
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001300 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301 *
1302 * PrivateKeyInfo ::= SEQUENCE {
1303 * version Version,
1304 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1305 * privateKey PrivateKey,
1306 * attributes [0] IMPLICIT Attributes OPTIONAL }
1307 *
1308 * Version ::= INTEGER
1309 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1310 * PrivateKey ::= OCTET STRING
1311 *
1312 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1313 */
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1316 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1317 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318 }
1319
1320 end = p + len;
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001324 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (version != 0) {
1327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1328 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
Jethro Beekman01672442023-04-19 14:08:14 +02001330 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 return ret;
1332 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1336 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (len < 1) {
1339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1340 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1341 }
1342
1343 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1344 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1345 }
1346
1347 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1348 return ret;
1349 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (pk_alg == MBEDTLS_PK_RSA) {
1353 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1354 mbedtls_pk_free(pk);
1355 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 }
1357 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001359#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001361#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001362 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001363 if ((ret =
1364 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001365 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001366 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001367 p_rng)) != 0) {
1368 mbedtls_pk_free(pk);
1369 return ret;
1370 }
1371 } else
1372#endif
1373 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001374 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1375 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001376 mbedtls_pk_free(pk);
1377 return ret;
1378 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 }
1380 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001381#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001384 end = p + len;
1385 if (end != (key + keylen)) {
1386 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1387 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1388 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391}
1392
1393/*
1394 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001395 *
1396 * To save space, the decryption happens in-place on the given key buffer.
1397 * Also, while this function may modify the keybuffer, it doesn't own it,
1398 * and instead it is the responsibility of the caller to zeroize and properly
1399 * free it after use.
1400 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001403MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 mbedtls_pk_context *pk,
1405 unsigned char *key, size_t keylen,
1406 const unsigned char *pwd, size_t pwdlen,
1407 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001409 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001411 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie86677d2023-10-12 16:05:10 +02001414#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_cipher_type_t cipher_alg;
1416 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001418 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
Hanno Becker2aa80a72017-09-07 15:28:45 +01001420 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 end = p + keylen;
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (pwdlen == 0) {
1424 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1425 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426
1427 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001428 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429 *
1430 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1431 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1432 * encryptedData EncryptedData
1433 * }
1434 *
1435 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1436 *
1437 * EncryptedData ::= OCTET STRING
1438 *
1439 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001440 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1443 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1444 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 }
1446
1447 end = p + len;
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1451 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1455 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456
Hanno Beckerfab35692017-08-25 13:38:26 +01001457 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
1459 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001460 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 */
Valerio Settie86677d2023-10-12 16:05:10 +02001462#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001464 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001465 cipher_alg, md_alg,
1466 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1468 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1469 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001473
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001474 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#endif /* MBEDTLS_PKCS12_C */
Valerio Settie86677d2023-10-12 16:05:10 +02001477#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001479 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1480 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1482 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1483 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001487
1488 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001491 {
1492 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001493 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (decrypted == 0) {
1496 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1497 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001498 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001502/***********************************************************************
1503 *
1504 * Top-level functions, with format auto-discovery
1505 *
1506 **********************************************************************/
1507
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508/*
1509 * Parse a private key
1510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1512 const unsigned char *key, size_t keylen,
1513 const unsigned char *pwd, size_t pwdlen,
1514 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515{
Janos Follath24eed8d2019-11-22 13:21:35 +00001516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001521#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (keylen == 0) {
1524 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1525 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001526
1527#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001531 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001533 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 } else {
1535 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001536 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 key, pwd, pwdlen, &len);
1538 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (ret == 0) {
1541 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1542 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1543 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1544 pem.buf, pem.buflen)) != 0) {
1545 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001546 }
1547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 mbedtls_pem_free(&pem);
1549 return ret;
1550 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1551 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1552 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1553 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1554 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1555 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558
Valerio Setti81d75122023-06-14 14:49:33 +02001559#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001560 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001562 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 } else {
1564 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001565 PEM_BEGIN_PRIVATE_KEY_EC,
1566 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 key, pwd, pwdlen, &len);
1568 }
1569 if (ret == 0) {
1570 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001573 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 pem.buf, pem.buflen,
1575 f_rng, p_rng)) != 0) {
1576 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577 }
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 mbedtls_pem_free(&pem);
1580 return ret;
1581 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1582 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1583 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1584 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1585 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1586 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587 }
Valerio Setti81d75122023-06-14 14:49:33 +02001588#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001590 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001592 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else {
1594 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001595 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 key, NULL, 0, &len);
1597 }
1598 if (ret == 0) {
1599 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1600 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1601 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602 }
1603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 mbedtls_pem_free(&pem);
1605 return ret;
1606 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1607 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001611 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001613 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 } else {
1615 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001616 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1617 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 key, NULL, 0, &len);
1619 }
1620 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001621 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1622 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624 }
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 mbedtls_pem_free(&pem);
1627 return ret;
1628 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1629 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632#else
1633 ((void) pwd);
1634 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636
1637 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001638 * At this point we only know it's not a PEM formatted key. Could be any
1639 * of the known DER encoded private key formats
1640 *
1641 * We try the different DER format parsers to see if one passes without
1642 * error
1643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001646 unsigned char *key_copy;
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1649 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1650 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001653
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001654 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1655 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001656
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001657 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001658 }
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (ret == 0) {
1661 return 0;
1662 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 mbedtls_pk_free(pk);
1665 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1668 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001669 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1673 if (ret == 0) {
1674 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001675 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_pk_free(pk);
1678 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1683 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1684 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1685 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_pk_free(pk);
1689 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Valerio Setti81d75122023-06-14 14:49:33 +02001692#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1694 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001695 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 key, keylen, f_rng, p_rng) == 0) {
1697 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001700#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701
Valerio Setti81d75122023-06-14 14:49:33 +02001702 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001703 * it is ok to leave the PK context initialized but not
1704 * freed: It is the caller's responsibility to call pk_init()
1705 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001706 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001707 * isn't, this leads to mbedtls_pk_free() being called
1708 * twice, once here and once by the caller, but this is
1709 * also ok and in line with the mbedtls_pk_free() calls
1710 * on failed PEM parsing attempts. */
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713}
1714
1715/*
1716 * Parse a public key
1717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1719 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001720{
Janos Follath24eed8d2019-11-22 13:21:35 +00001721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001722 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001723#if defined(MBEDTLS_RSA_C)
1724 const mbedtls_pk_info_t *pk_info;
1725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001727 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001729#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (keylen == 0) {
1732 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1733 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001734
1735#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001737#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001738 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001740 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 } else {
1742 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001743 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001746
1747 if (ret == 0) {
1748 p = pem.buf;
1749 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1750 mbedtls_pem_free(&pem);
1751 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1752 }
1753
1754 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1755 mbedtls_pem_free(&pem);
1756 return ret;
1757 }
1758
1759 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1760 mbedtls_pk_free(ctx);
1761 }
1762
1763 mbedtls_pem_free(&pem);
1764 return ret;
1765 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1766 mbedtls_pem_free(&pem);
1767 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001768 }
1769#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001770
1771 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001773 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 } else {
1775 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001776 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 key, NULL, 0, &len);
1778 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001781 /*
1782 * Was PEM encoded
1783 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001784 p = pem.buf;
1785
Jethro Beekman01672442023-04-19 14:08:14 +02001786 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mbedtls_pem_free(&pem);
1788 return ret;
1789 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1790 mbedtls_pem_free(&pem);
1791 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001795
1796#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1798 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001800
1801 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1802 return ret;
1803 }
1804
1805 p = (unsigned char *) key;
1806 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1807 if (ret == 0) {
1808 return ret;
1809 }
1810 mbedtls_pk_free(ctx);
1811 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1812 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1813 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001814 }
1815#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001816 p = (unsigned char *) key;
1817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001821}
1822
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001823/***********************************************************************
1824 *
1825 * Top-level functions, with filesystem support
1826 *
1827 **********************************************************************/
1828
1829#if defined(MBEDTLS_FS_IO)
1830/*
1831 * Load all data from a file into a given buffer.
1832 *
1833 * The file is expected to contain either PEM or DER encoded data.
1834 * A terminating null byte is always appended. It is included in the announced
1835 * length only if the data looks like it is PEM encoded.
1836 */
1837int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1838{
1839 FILE *f;
1840 long size;
1841
1842 if ((f = fopen(path, "rb")) == NULL) {
1843 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1844 }
1845
1846 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1847 mbedtls_setbuf(f, NULL);
1848
1849 fseek(f, 0, SEEK_END);
1850 if ((size = ftell(f)) == -1) {
1851 fclose(f);
1852 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1853 }
1854 fseek(f, 0, SEEK_SET);
1855
1856 *n = (size_t) size;
1857
1858 if (*n + 1 == 0 ||
1859 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1860 fclose(f);
1861 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1862 }
1863
1864 if (fread(*buf, 1, *n, f) != *n) {
1865 fclose(f);
1866
1867 mbedtls_zeroize_and_free(*buf, *n);
1868
1869 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1870 }
1871
1872 fclose(f);
1873
1874 (*buf)[*n] = '\0';
1875
1876 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1877 ++*n;
1878 }
1879
1880 return 0;
1881}
1882
1883/*
1884 * Load and parse a private key
1885 */
1886int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1887 const char *path, const char *pwd,
1888 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1889{
1890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1891 size_t n;
1892 unsigned char *buf;
1893
1894 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1895 return ret;
1896 }
1897
1898 if (pwd == NULL) {
1899 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1900 } else {
1901 ret = mbedtls_pk_parse_key(ctx, buf, n,
1902 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1903 }
1904
1905 mbedtls_zeroize_and_free(buf, n);
1906
1907 return ret;
1908}
1909
1910/*
1911 * Load and parse a public key
1912 */
1913int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1914{
1915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1916 size_t n;
1917 unsigned char *buf;
1918
1919 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1920 return ret;
1921 }
1922
1923 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1924
1925 mbedtls_zeroize_and_free(buf, n);
1926
1927 return ret;
1928}
1929#endif /* MBEDTLS_FS_IO */
1930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_PK_PARSE_C */