blob: 5ba9645dac712b38276bf83194923b0e298d63ed [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1a7550a2013-09-15 13:01:22 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pk.h"
13#include "mbedtls/asn1.h"
14#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050015#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020016#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <string.h>
20
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020021#if defined(MBEDTLS_USE_PSA_CRYPTO)
22#include "mbedtls/psa_util.h"
23#include "psa/crypto.h"
24#endif
25
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020026/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029#endif
Valerio Setti81d75122023-06-14 14:49:33 +020030#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020031#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020032#include "pk_internal.h"
33#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020034
35/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020044#endif
45
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020046#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
47
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020048/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020049 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020050 * ECC setters
51 *
52 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
53 * this macro will not appear outside this section.
54 * 2. All inputs are raw: no metadata, no ASN.1 until the next section.
55 *
56 **********************************************************************/
57
58/*
59 * Set the group used by this key.
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020060 *
61 * [in/out] pk: in: must have been pk_setup() to an ECC type
62 * out: will have group (curve) information set
63 * [in] grp_in: a supported group ID (not NONE)
Paul Bakker1a7550a2013-09-15 13:01:22 +020064 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020065static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
66{
67#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
68 size_t ec_bits;
69 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
70
71 /* group may already be initialized; if so, make sure IDs match */
72 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
73 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
74 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
75 }
76
77 /* set group */
78 pk->ec_family = ec_family;
79 pk->ec_bits = ec_bits;
80
81 return 0;
82#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
83 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
84
85 /* grp may already be initialized; if so, make sure IDs match */
86 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
87 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
88 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
89 }
90
91 /* set group */
92 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
93#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
94}
95
96/*
97 * Set the private key material
98 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020099 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
100 * out: will have the private key set.
101 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200102 */
103static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200104 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200105{
106#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
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
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200871/* Helper for Montgomery curves */
872#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
873#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
874 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
875#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
876
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877/*
878 * SubjectPublicKeyInfo ::= SEQUENCE {
879 * algorithm AlgorithmIdentifier,
880 * subjectPublicKey BIT STRING }
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
883 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884{
Janos Follath24eed8d2019-11-22 13:21:35 +0000885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_asn1_buf alg_params;
888 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200889 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
893 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
894 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 }
896
897 end = *p + len;
898
Jethro Beekman01672442023-04-19 14:08:14 +0200899 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return ret;
901 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (*p + len != end) {
908 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
909 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
910 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
913 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
914 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
917 return ret;
918 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (pk_alg == MBEDTLS_PK_RSA) {
922 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200925#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200927#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200928 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200929 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200930 } else
931#endif
932 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200933 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200934 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000936 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200937 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200940#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (ret == 0 && *p != end) {
944 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
945 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
946 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ret != 0) {
949 mbedtls_pk_free(pk);
950 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953}
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100957 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
958 *
959 * The value zero is:
960 * - never a valid value for an RSA parameter
961 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
962 *
963 * Since values can't be omitted in PKCS#1, passing a zero value to
964 * rsa_complete() would be incorrect, so reject zero values early.
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static int asn1_get_nonzero_mpi(unsigned char **p,
967 const unsigned char *end,
968 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100969{
970 int ret;
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 ret = mbedtls_asn1_get_mpi(p, end, X);
973 if (ret != 0) {
974 return ret;
975 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
978 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
979 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982}
983
984/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985 * Parse a PKCS#1 encoded private RSA key
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
988 const unsigned char *key,
989 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100991 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 size_t len;
993 unsigned char *p, *end;
994
Hanno Beckerefa14e82017-10-11 19:45:19 +0100995 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100997
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 p = (unsigned char *) key;
999 end = p + keylen;
1000
1001 /*
1002 * This function parses the RSAPrivateKey (PKCS#1)
1003 *
1004 * RSAPrivateKey ::= SEQUENCE {
1005 * version Version,
1006 * modulus INTEGER, -- n
1007 * publicExponent INTEGER, -- e
1008 * privateExponent INTEGER, -- d
1009 * prime1 INTEGER, -- p
1010 * prime2 INTEGER, -- q
1011 * exponent1 INTEGER, -- d mod (p-1)
1012 * exponent2 INTEGER, -- d mod (q-1)
1013 * coefficient INTEGER, -- (inverse of q) mod p
1014 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1015 * }
1016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1018 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1019 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
1022 end = p + len;
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1025 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 }
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (version != 0) {
1029 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 }
1031
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001032 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1034 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1035 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001036 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1041 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1042 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001043 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045
1046 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1048 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1049 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052
1053 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1056 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059
1060 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1062 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1063 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001067#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001068 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1070 * that they can be easily recomputed from D, P and Q. However by
1071 * parsing them from the PKCS1 structure it is possible to avoid
1072 * recalculating them which both reduces the overhead of loading
1073 * RSA private keys into memory and also avoids side channels which
1074 * can arise when computing those values, since all of D, P, and Q
1075 * are secret. See https://eprint.iacr.org/2020/055 for a
1076 * description of one such attack.
1077 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001078
Jack Lloyd80cc8112020-01-22 17:34:29 -05001079 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1081 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1082 goto cleanup;
1083 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084
1085 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1088 goto cleanup;
1089 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001090
1091 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1093 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1094 goto cleanup;
1095 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001096
Jack Lloyd60239752020-01-27 17:53:36 -05001097#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001098 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1100 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1101 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1102 goto cleanup;
1103 }
Jack Lloyd60239752020-01-27 17:53:36 -05001104#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001105
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001106 /* rsa_complete() doesn't complete anything with the default
1107 * implementation but is still called:
1108 * - for the benefit of alternative implementation that may want to
1109 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1110 * - as is also sanity-checks the key
1111 *
1112 * Furthermore, we also check the public part for consistency with
1113 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1116 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001117 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001118 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (p != end) {
1121 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1122 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 }
1124
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001125cleanup:
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001130 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret & 0xff80) == 0) {
1132 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1133 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001134 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Valerio Setti81d75122023-06-14 14:49:33 +02001144#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145/*
1146 * Parse a SEC1 encoded private EC key
1147 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001148static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 const unsigned char *key, size_t keylen,
1150 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151{
Janos Follath24eed8d2019-11-22 13:21:35 +00001152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001153 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001154 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001155 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001157 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 unsigned char *end = p + keylen;
1159 unsigned char *end2;
1160
1161 /*
1162 * RFC 5915, or SEC1 Appendix C.4
1163 *
1164 * ECPrivateKey ::= SEQUENCE {
1165 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1166 * privateKey OCTET STRING,
1167 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1168 * publicKey [1] BIT STRING OPTIONAL
1169 * }
1170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1172 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1173 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174 }
1175
1176 end = p + len;
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1179 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1180 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (version != 1) {
1183 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1184 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1188 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Valerio Setti6b062ee2023-06-30 17:32:57 +02001190 /* Keep a reference to the position fo the private key. It will be used
1191 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001192 d = p;
1193 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001194
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 p += len;
1196
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001199 /*
1200 * Is 'parameters' present?
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1203 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1204 0)) == 0) {
1205 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001206 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001208 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001211 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001212 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001213
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001214 /*
1215 * Load the private key
1216 */
1217 ret = pk_ecc_set_key(pk, d, d_len);
1218 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001219 return ret;
1220 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001223 /*
1224 * Is 'publickey' present? If not, or if we can't read it (eg because it
1225 * is compressed), create it from the private key.
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1228 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1229 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001230 end2 = p + len;
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1233 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1234 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (p + len != end2) {
1237 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1239 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001241 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001242 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001245 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 * is if the point format is not recognized.
1247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1249 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1250 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001251 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001254 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001256
Valerio Setti34f67552023-04-03 15:19:18 +02001257 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001258 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001259 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001260 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001261 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264}
Valerio Setti81d75122023-06-14 14:49:33 +02001265#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001267/***********************************************************************
1268 *
1269 * PKCS#8 parsing functions
1270 *
1271 **********************************************************************/
1272
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273/*
1274 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001275 *
1276 * Notes:
1277 *
1278 * - This function does not own the key buffer. It is the
1279 * responsibility of the caller to take care of zeroizing
1280 * and freeing it after use.
1281 *
1282 * - The function is responsible for freeing the provided
1283 * PK context on failure.
1284 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 */
1286static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 mbedtls_pk_context *pk,
1288 const unsigned char *key, size_t keylen,
1289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290{
1291 int ret, version;
1292 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 unsigned char *p = (unsigned char *) key;
1295 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001297 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299
Valerio Setti81d75122023-06-14 14:49:33 +02001300#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001301 (void) f_rng;
1302 (void) p_rng;
1303#endif
1304
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001306 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 *
1308 * PrivateKeyInfo ::= SEQUENCE {
1309 * version Version,
1310 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1311 * privateKey PrivateKey,
1312 * attributes [0] IMPLICIT Attributes OPTIONAL }
1313 *
1314 * Version ::= INTEGER
1315 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1316 * PrivateKey ::= OCTET STRING
1317 *
1318 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1319 */
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1322 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 }
1325
1326 end = p + len;
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1329 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001330 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (version != 0) {
1333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1334 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335
Jethro Beekman01672442023-04-19 14:08:14 +02001336 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return ret;
1338 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1342 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (len < 1) {
1345 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1346 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1347 }
1348
1349 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1350 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1351 }
1352
1353 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1354 return ret;
1355 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (pk_alg == MBEDTLS_PK_RSA) {
1359 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1360 mbedtls_pk_free(pk);
1361 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 }
1363 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001365#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001367#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001368 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001369 if ((ret =
1370 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001371 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001372 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001373 p_rng)) != 0) {
1374 mbedtls_pk_free(pk);
1375 return ret;
1376 }
1377 } else
1378#endif
1379 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001380 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1381 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001382 mbedtls_pk_free(pk);
1383 return ret;
1384 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385 }
1386 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001387#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001390 end = p + len;
1391 if (end != (key + keylen)) {
1392 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1393 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1394 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397}
1398
1399/*
1400 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001401 *
1402 * To save space, the decryption happens in-place on the given key buffer.
1403 * Also, while this function may modify the keybuffer, it doesn't own it,
1404 * and instead it is the responsibility of the caller to zeroize and properly
1405 * free it after use.
1406 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001409MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 mbedtls_pk_context *pk,
1411 unsigned char *key, size_t keylen,
1412 const unsigned char *pwd, size_t pwdlen,
1413 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001415 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001417 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001420#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_cipher_type_t cipher_alg;
1422 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001424 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425
Hanno Becker2aa80a72017-09-07 15:28:45 +01001426 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 end = p + keylen;
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (pwdlen == 0) {
1430 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1431 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
1433 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001434 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 *
1436 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1437 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1438 * encryptedData EncryptedData
1439 * }
1440 *
1441 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1442 *
1443 * EncryptedData ::= OCTET STRING
1444 *
1445 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001446 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1449 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451 }
1452
1453 end = p + len;
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1457 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1461 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
Hanno Beckerfab35692017-08-25 13:38:26 +01001463 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464
1465 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001466 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 */
Valerio Settie581e142023-12-29 16:35:07 +01001468#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001470 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001471 cipher_alg, md_alg,
1472 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1474 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001479
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001480 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 } else
Valerio Settie581e142023-12-29 16:35:07 +01001482#endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
1483#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001485 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1486 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1488 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1489 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001493
1494 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 } else
Valerio Settie581e142023-12-29 16:35:07 +01001496#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001497 {
1498 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001499 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (decrypted == 0) {
1502 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1503 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001504 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001508/***********************************************************************
1509 *
1510 * Top-level functions, with format auto-discovery
1511 *
1512 **********************************************************************/
1513
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514/*
1515 * Parse a private key
1516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1518 const unsigned char *key, size_t keylen,
1519 const unsigned char *pwd, size_t pwdlen,
1520 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521{
Janos Follath24eed8d2019-11-22 13:21:35 +00001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001527#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (keylen == 0) {
1530 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1531 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001532
1533#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001537 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001539 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 } else {
1541 ret = mbedtls_pem_read_buffer(&pem,
1542 "-----BEGIN RSA PRIVATE KEY-----",
1543 "-----END RSA PRIVATE KEY-----",
1544 key, pwd, pwdlen, &len);
1545 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (ret == 0) {
1548 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1549 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1550 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1551 pem.buf, pem.buflen)) != 0) {
1552 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553 }
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_pem_free(&pem);
1556 return ret;
1557 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1558 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1559 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1560 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1561 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1562 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565
Valerio Setti81d75122023-06-14 14:49:33 +02001566#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001567 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001569 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 } else {
1571 ret = mbedtls_pem_read_buffer(&pem,
1572 "-----BEGIN EC PRIVATE KEY-----",
1573 "-----END EC PRIVATE KEY-----",
1574 key, pwd, pwdlen, &len);
1575 }
1576 if (ret == 0) {
1577 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001580 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 pem.buf, pem.buflen,
1582 f_rng, p_rng)) != 0) {
1583 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584 }
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_pem_free(&pem);
1587 return ret;
1588 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1589 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1590 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1591 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1592 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1593 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001594 }
Valerio Setti81d75122023-06-14 14:49:33 +02001595#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001597 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001599 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 } else {
1601 ret = mbedtls_pem_read_buffer(&pem,
1602 "-----BEGIN PRIVATE KEY-----",
1603 "-----END PRIVATE KEY-----",
1604 key, NULL, 0, &len);
1605 }
1606 if (ret == 0) {
1607 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1608 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1609 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001610 }
1611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 mbedtls_pem_free(&pem);
1613 return ret;
1614 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1615 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001616 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001619 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001621 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 } else {
1623 ret = mbedtls_pem_read_buffer(&pem,
1624 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1625 "-----END ENCRYPTED PRIVATE KEY-----",
1626 key, NULL, 0, &len);
1627 }
1628 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001629 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1630 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 mbedtls_pem_free(&pem);
1635 return ret;
1636 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1637 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640#else
1641 ((void) pwd);
1642 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644
1645 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001646 * At this point we only know it's not a PEM formatted key. Could be any
1647 * of the known DER encoded private key formats
1648 *
1649 * We try the different DER format parsers to see if one passes without
1650 * error
1651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001654 unsigned char *key_copy;
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1657 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1658 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001661
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001662 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1663 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001664
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001665 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666 }
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (ret == 0) {
1669 return 0;
1670 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_pk_free(pk);
1673 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1676 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1681 if (ret == 0) {
1682 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001683 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 mbedtls_pk_free(pk);
1686 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1691 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1692 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1693 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694 }
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 mbedtls_pk_free(pk);
1697 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699
Valerio Setti81d75122023-06-14 14:49:33 +02001700#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1702 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001703 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 key, keylen, f_rng, p_rng) == 0) {
1705 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001708#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001709
Valerio Setti81d75122023-06-14 14:49:33 +02001710 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001711 * it is ok to leave the PK context initialized but not
1712 * freed: It is the caller's responsibility to call pk_init()
1713 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001714 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001715 * isn't, this leads to mbedtls_pk_free() being called
1716 * twice, once here and once by the caller, but this is
1717 * also ok and in line with the mbedtls_pk_free() calls
1718 * on failed PEM parsing attempts. */
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001721}
1722
1723/*
1724 * Parse a public key
1725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1727 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728{
Janos Follath24eed8d2019-11-22 13:21:35 +00001729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001731#if defined(MBEDTLS_RSA_C)
1732 const mbedtls_pk_info_t *pk_info;
1733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001735 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001737#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (keylen == 0) {
1740 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1741 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001742
1743#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001745#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001746 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001748 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 } else {
1750 ret = mbedtls_pem_read_buffer(&pem,
1751 "-----BEGIN RSA PUBLIC KEY-----",
1752 "-----END RSA PUBLIC KEY-----",
1753 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001755
1756 if (ret == 0) {
1757 p = pem.buf;
1758 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1759 mbedtls_pem_free(&pem);
1760 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1761 }
1762
1763 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1764 mbedtls_pem_free(&pem);
1765 return ret;
1766 }
1767
1768 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1769 mbedtls_pk_free(ctx);
1770 }
1771
1772 mbedtls_pem_free(&pem);
1773 return ret;
1774 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1775 mbedtls_pem_free(&pem);
1776 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001777 }
1778#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001779
1780 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001782 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 } else {
1784 ret = mbedtls_pem_read_buffer(&pem,
1785 "-----BEGIN PUBLIC KEY-----",
1786 "-----END PUBLIC KEY-----",
1787 key, NULL, 0, &len);
1788 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001791 /*
1792 * Was PEM encoded
1793 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001794 p = pem.buf;
1795
Jethro Beekman01672442023-04-19 14:08:14 +02001796 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 mbedtls_pem_free(&pem);
1798 return ret;
1799 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1800 mbedtls_pem_free(&pem);
1801 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001802 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001805
1806#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1808 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001809 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001810
1811 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1812 return ret;
1813 }
1814
1815 p = (unsigned char *) key;
1816 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1817 if (ret == 0) {
1818 return ret;
1819 }
1820 mbedtls_pk_free(ctx);
1821 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1822 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1823 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001824 }
1825#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001826 p = (unsigned char *) key;
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001831}
1832
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001833/***********************************************************************
1834 *
1835 * Top-level functions, with filesystem support
1836 *
1837 **********************************************************************/
1838
1839#if defined(MBEDTLS_FS_IO)
1840/*
1841 * Load all data from a file into a given buffer.
1842 *
1843 * The file is expected to contain either PEM or DER encoded data.
1844 * A terminating null byte is always appended. It is included in the announced
1845 * length only if the data looks like it is PEM encoded.
1846 */
1847int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1848{
1849 FILE *f;
1850 long size;
1851
1852 if ((f = fopen(path, "rb")) == NULL) {
1853 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1854 }
1855
1856 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1857 mbedtls_setbuf(f, NULL);
1858
1859 fseek(f, 0, SEEK_END);
1860 if ((size = ftell(f)) == -1) {
1861 fclose(f);
1862 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1863 }
1864 fseek(f, 0, SEEK_SET);
1865
1866 *n = (size_t) size;
1867
1868 if (*n + 1 == 0 ||
1869 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1870 fclose(f);
1871 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1872 }
1873
1874 if (fread(*buf, 1, *n, f) != *n) {
1875 fclose(f);
1876
1877 mbedtls_zeroize_and_free(*buf, *n);
1878
1879 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1880 }
1881
1882 fclose(f);
1883
1884 (*buf)[*n] = '\0';
1885
1886 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1887 ++*n;
1888 }
1889
1890 return 0;
1891}
1892
1893/*
1894 * Load and parse a private key
1895 */
1896int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1897 const char *path, const char *pwd,
1898 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1899{
1900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1901 size_t n;
1902 unsigned char *buf;
1903
1904 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1905 return ret;
1906 }
1907
1908 if (pwd == NULL) {
1909 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1910 } else {
1911 ret = mbedtls_pk_parse_key(ctx, buf, n,
1912 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1913 }
1914
1915 mbedtls_zeroize_and_free(buf, n);
1916
1917 return ret;
1918}
1919
1920/*
1921 * Load and parse a public key
1922 */
1923int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1924{
1925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1926 size_t n;
1927 unsigned char *buf;
1928
1929 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1930 return ret;
1931 }
1932
1933 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1934
1935 mbedtls_zeroize_and_free(buf, n);
1936
1937 return ret;
1938}
1939#endif /* MBEDTLS_FS_IO */
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#endif /* MBEDTLS_PK_PARSE_C */