blob: 5f95545af6f9c57ca222866f6f822d9cf67c4081 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1a7550a2013-09-15 13:01:22 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pk.h"
13#include "mbedtls/asn1.h"
14#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050015#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020016#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Tomi Fontanilles851d8df2023-12-19 15:44:52 +020018#include "mbedtls/ecp.h"
Valerio Setti3cc486a2023-11-30 08:09:47 +010019#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <string.h>
22
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020023#if defined(MBEDTLS_USE_PSA_CRYPTO)
24#include "mbedtls/psa_util.h"
25#include "psa/crypto.h"
26#endif
27
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020028/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020031#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020032
33/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
43
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020044#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
45
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020046/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020047 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020048 * ECC setters
49 *
50 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
51 * this macro will not appear outside this section.
52 * 2. All inputs are raw: no metadata, no ASN.1 until the next section.
53 *
54 **********************************************************************/
55
56/*
57 * Set the group used by this key.
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020058 *
59 * [in/out] pk: in: must have been pk_setup() to an ECC type
60 * out: will have group (curve) information set
61 * [in] grp_in: a supported group ID (not NONE)
Paul Bakker1a7550a2013-09-15 13:01:22 +020062 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020063static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
64{
65#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
66 size_t ec_bits;
67 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
68
69 /* group may already be initialized; if so, make sure IDs match */
70 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
71 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
72 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
73 }
74
75 /* set group */
76 pk->ec_family = ec_family;
77 pk->ec_bits = ec_bits;
78
79 return 0;
80#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
81 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
82
83 /* grp may already be initialized; if so, make sure IDs match */
84 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
85 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
86 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
87 }
88
89 /* set group */
90 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
91#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
92}
93
94/*
95 * Set the private key material
96 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020097 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
98 * out: will have the private key set.
99 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200100 */
101static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200102 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200103{
104#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Settifbbafa02023-12-06 10:07:34 +0100106 psa_key_usage_t flags;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200107 psa_status_t status;
108
109 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Settifbbafa02023-12-06 10:07:34 +0100110 if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
111 /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
112 * the PK module cannot do ECDH. When the key will be used in TLS for
113 * ECDH, it will be exported and then re-imported with proper flags
114 * and algorithm. */
115 flags = PSA_KEY_USAGE_EXPORT;
116 } else {
117 psa_set_key_algorithm(&attributes,
118 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
119 flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
120 PSA_KEY_USAGE_EXPORT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200121 }
122 psa_set_key_usage_flags(&attributes, flags);
123
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200124 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200125 return psa_pk_status_to_mbedtls(status);
126
127#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
128
129 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200130 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200131 if (ret != 0) {
132 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
133 }
134 return 0;
135#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
136}
137
138/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200139 * Derive a public key from its private counterpart.
140 * Computationally intensive, only use when public key is not available.
141 *
142 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
143 * out: will have the public key set.
144 * [in] prv, prv_len: the raw private key (see note below).
145 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200146 *
147 * Note: the private key information is always available from pk,
148 * however for convenience the serialized version is also passed,
149 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200150 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200151 *
152 * There are three implementations of this function:
153 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
154 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
155 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200156 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200157static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
158 const unsigned char *prv, size_t prv_len,
159 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200160{
161#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200162
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200163 (void) f_rng;
164 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200165 (void) prv;
166 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200167 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200168
169 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
170 &pk->pub_raw_len);
171 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200172
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200173#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200174
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200175 (void) f_rng;
176 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200177 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200178
179 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200180 size_t curve_bits;
181 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200182
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200183 /* Import private key into PSA, from serialized input */
184 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
185 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200186 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
187 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200188 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200189 if (status != PSA_SUCCESS) {
190 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200191 }
192
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200193 /* Export public key from PSA */
194 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
195 size_t pub_len;
196 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
197 psa_status_t destruction_status = psa_destroy_key(key_id);
198 if (status != PSA_SUCCESS) {
199 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200200 } else if (destruction_status != PSA_SUCCESS) {
201 return psa_pk_status_to_mbedtls(destruction_status);
202 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200203
204 /* Load serialized public key into ecp_keypair structure */
205 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
206
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200207#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200208
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200209 (void) prv;
210 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200211
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200212 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200213 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 +0200214
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200215#endif /* MBEDTLS_USE_PSA_CRYPTO */
216}
217
218#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
219/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200220 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200221 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200222 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
223 * functions to handle keys. However, currently psa_import_key() does not
224 * support compressed points. In case that support was explicitly requested,
225 * this fallback uses ECP functions to get the job done. This is the reason
226 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
227 *
228 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
229 * out: will have the public key set.
230 * [in] pub, pub_len: the public key as an ECPoint,
231 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200232 *
233 * Return:
234 * - 0 on success;
235 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
236 * but not supported;
237 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200238 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200239static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
240 const unsigned char *pub,
241 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200242{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200243#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200244 (void) pk;
245 (void) pub;
246 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200247 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200248#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200249 mbedtls_ecp_keypair ecp_key;
250 mbedtls_ecp_group_id ecp_group_id;
251 int ret;
252
Valerio Settid36c3132023-12-21 14:03:51 +0100253 ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200254
255 mbedtls_ecp_keypair_init(&ecp_key);
256 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
257 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200258 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200259 }
260 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200261 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200262 if (ret != 0) {
263 goto exit;
264 }
265 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
266 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200267 &pk->pub_raw_len, pk->pub_raw,
268 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200269
270exit:
271 mbedtls_ecp_keypair_free(&ecp_key);
272 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200273#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
274}
275#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
276
277/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200278 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200279 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200280 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
281 * out: will have the public key set.
282 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200283 *
284 * Return:
285 * - 0 on success;
286 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
287 * but not supported;
288 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200289 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200290static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
291 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200292{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200293#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200294
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200295 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200296 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
297 /* Format directly supported by PSA:
298 * - non-Weierstrass curves that only have one format;
299 * - uncompressed format for Weierstrass curves. */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200300 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200301 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
302 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200303 memcpy(pk->pub_raw, pub, pub_len);
304 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200305 } else {
306 /* Other format, try the fallback */
307 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
308 if (ret != 0) {
309 return ret;
310 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200312
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200313 /* Validate the key by trying to import it */
314 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
315 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
316
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200317 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200318 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
319 psa_set_key_bits(&key_attrs, pk->ec_bits);
320
321 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200322 &key_id) != PSA_SUCCESS) ||
323 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
324 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200326
327 return 0;
328
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200329#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200330
331 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200332 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200333 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
334 if (ret != 0) {
335 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200337 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
338
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200339#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200340}
341
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200342/***********************************************************************
343 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200344 * Low-level ECC parsing: optional support for SpecifiedECDomain
345 *
346 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200347 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200348 * - pk_ecc_group_id_from_specified()
349 *
350 * All the other functions are internal to this section.
351 *
352 * The two "public" functions have a dummy variant provided
353 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
354 * abstraction layer for this macro, which should not appear outside
355 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200356 *
357 **********************************************************************/
358
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200359#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
360/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200361static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200362{
363 (void) tag;
364 return 0;
365}
366
367/* See the "real" version for documentation */
368static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
369 mbedtls_ecp_group_id *grp_id)
370{
371 (void) params;
372 (void) grp_id;
373 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
374}
375#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
376/*
377 * Tell if the passed tag might be the start of SpecifiedECDomain
378 * (that is, a sequence).
379 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200380static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200381{
382 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
383}
384
Paul Bakker1a7550a2013-09-15 13:01:22 +0200385/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100386 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
387 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200388 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389 * if it was encoded compressed.
390 *
391 * SpecifiedECDomain ::= SEQUENCE {
392 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
393 * fieldID FieldID {{FieldTypes}},
394 * curve Curve,
395 * base ECPoint,
396 * order INTEGER,
397 * cofactor INTEGER OPTIONAL,
398 * hash HashAlgorithm OPTIONAL,
399 * ...
400 * }
401 *
402 * We only support prime-field as field type, and ignore hash and cofactor.
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405{
Janos Follath24eed8d2019-11-22 13:21:35 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200408 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409 const unsigned char *end_field, *end_curve;
410 size_t len;
411 int ver;
412
413 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ver < 1 || ver > 3) {
419 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
420 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
422 /*
423 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
424 * fieldType FIELD-ID.&id({IOSet}),
425 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
426 * }
427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
429 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
430 return ret;
431 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100432
433 end_field = p + len;
434
435 /*
436 * FIELD-ID ::= TYPE-IDENTIFIER
437 * FieldTypes FIELD-ID ::= {
438 * { Prime-p IDENTIFIED BY prime-field } |
439 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
440 * }
441 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
444 return ret;
445 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
448 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
449 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100450 }
451
452 p += len;
453
454 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
457 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (p != end_field) {
462 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
464 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465
466 /*
467 * Curve ::= SEQUENCE {
468 * a FieldElement,
469 * b FieldElement,
470 * seed BIT STRING OPTIONAL
471 * -- Shall be present if used in SpecifiedECDomain
472 * -- with version equal to ecdpVer2 or ecdpVer3
473 * }
474 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
476 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
477 return ret;
478 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100479
480 end_curve = p + len;
481
482 /*
483 * FieldElement ::= OCTET STRING
484 * containing an integer in the case of a prime field
485 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
487 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
488 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100489 }
490
491 p += len;
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
494 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
495 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100496 }
497
498 p += len;
499
500 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100502 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (p != end_curve) {
506 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
507 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
508 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100509
510 /*
511 * ECPoint ::= OCTET STRING
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
514 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
515 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
518 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100519 /*
520 * If we can't read the point because it's compressed, cheat by
521 * reading only the X coordinate and the parity bit of Y.
522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
524 (p[0] != 0x02 && p[0] != 0x03) ||
525 len != mbedtls_mpi_size(&grp->P) + 1 ||
526 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
527 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
528 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
529 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100530 }
531 }
532
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100533 p += len;
534
535 /*
536 * order INTEGER
537 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
539 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
540 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100543
544 /*
545 * Allow optional elements by purposefully not enforcing p == end here.
546 */
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100549}
550
551/*
552 * Find the group id associated with an (almost filled) group as generated by
553 * pk_group_from_specified(), or return an error if unknown.
554 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555static 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 +0100556{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100557 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_ecp_group ref;
559 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100564 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_ecp_group_free(&ref);
566 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100567
568 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
570 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
571 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
572 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
573 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
574 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
575 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100576 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 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 +0100578 break;
579 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100580 }
581
582cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100584
585 *grp_id = *id;
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100592}
593
594/*
595 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
596 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200597static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
598 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100599{
Janos Follath24eed8d2019-11-22 13:21:35 +0000600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100606 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100610
611cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000612 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200613 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000614 * temporary grp breaks that flow and it's members are populated
615 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
616 * which is assuming a group populated by _setup() may not clean-up
617 * properly -> Manually free it's members.
618 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000619 mbedtls_mpi_free(&grp.N);
620 mbedtls_mpi_free(&grp.P);
621 mbedtls_mpi_free(&grp.A);
622 mbedtls_mpi_free(&grp.B);
623 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100628
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200629/***********************************************************************
630 *
631 * Unsorted (yet!) from this point on until the next section header
632 *
633 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200634
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200635/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200636 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200637 * ECParameters ::= CHOICE {
638 * namedCurve OBJECT IDENTIFIER
639 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
640 * -- implicitCurve NULL
641 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200642 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200643static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
644 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200645{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200647
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200648 if (end - *p < 1) {
649 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
650 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200651 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200652
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200653 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200654 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200655 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200656 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200657 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
658 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
659 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200660
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200661 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200662 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
663 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200664
665 params->p = *p;
666 *p += params->len;
667
668 if (*p != end) {
669 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
670 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
671 }
672
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200673 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200674}
675
676/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200677 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100678 *
679 * ECParameters ::= CHOICE {
680 * namedCurve OBJECT IDENTIFIER
681 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
682 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200683 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200684static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685{
Janos Follath24eed8d2019-11-22 13:21:35 +0000686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (params->tag == MBEDTLS_ASN1_OID) {
690 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
691 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
692 }
693 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200694 ret = pk_ecc_group_id_from_specified(params, &grp_id);
695 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return ret;
697 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100698 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200700 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701}
702
Jethro Beekman01672442023-04-19 14:08:14 +0200703#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
704
705/*
706 * Load an RFC8410 EC key, which doesn't have any parameters
707 */
708static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
709 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200710 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200711{
712 if (params->tag != 0 || params->len != 0) {
713 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
714 }
715
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200716 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200717}
718
719/*
720 * Parse an RFC 8410 encoded private EC key
721 *
722 * CurvePrivateKey ::= OCTET STRING
723 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200724static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200725 unsigned char *key, size_t keylen, const unsigned char *end,
726 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
727{
728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
729 size_t len;
730
731 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
732 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
733 }
734
735 if (key + len != end) {
736 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
737 }
738
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200739 /*
740 * Load the private key
741 */
742 ret = pk_ecc_set_key(pk, key, len);
743 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200744 return ret;
745 }
Jethro Beekman01672442023-04-19 14:08:14 +0200746
Valerio Setti4064dbb2023-05-17 15:33:07 +0200747 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
748 * which never contain a public key. As such, derive the public key
749 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200750 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200751 return ret;
752 }
753
Jethro Beekman01672442023-04-19 14:08:14 +0200754 return 0;
755}
756#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200757
Valerio Setti81d75122023-06-14 14:49:33 +0200758#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761/*
762 * RSAPublicKey ::= SEQUENCE {
763 * modulus INTEGER, -- n
764 * publicExponent INTEGER -- e
765 * }
766 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767static int pk_get_rsapubkey(unsigned char **p,
768 const unsigned char *end,
769 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770{
Janos Follath24eed8d2019-11-22 13:21:35 +0000771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772 size_t len;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
776 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
777 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (*p + len != end) {
780 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
781 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
782 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200783
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
786 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
787 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
790 NULL, 0, NULL, 0)) != 0) {
791 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
792 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793
794 *p += len;
795
796 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
799 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
802 NULL, 0, *p, len)) != 0) {
803 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
804 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100805
806 *p += len;
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (mbedtls_rsa_complete(rsa) != 0 ||
809 mbedtls_rsa_check_pubkey(rsa) != 0) {
810 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000811 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (*p != end) {
814 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
816 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
822/* Get a PK algorithm identifier
823 *
824 * AlgorithmIdentifier ::= SEQUENCE {
825 * algorithm OBJECT IDENTIFIER,
826 * parameters ANY DEFINED BY algorithm OPTIONAL }
827 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828static int pk_get_pk_alg(unsigned char **p,
829 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200830 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
831 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832{
Janos Follath24eed8d2019-11-22 13:21:35 +0000833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
840 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841
Jethro Beekman01672442023-04-19 14:08:14 +0200842 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200843#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200844 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
845 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
846 if (ret == 0) {
847 *pk_alg = MBEDTLS_PK_ECKEY;
848 }
849 }
850#else
851 (void) ec_grp_id;
852#endif
853 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
855 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856
857 /*
858 * No parameters with RSA (only for EC)
859 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (*pk_alg == MBEDTLS_PK_RSA &&
861 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
862 params->len != 0)) {
863 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864 }
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867}
868
869/*
870 * SubjectPublicKeyInfo ::= SEQUENCE {
871 * algorithm AlgorithmIdentifier,
872 * subjectPublicKey BIT STRING }
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
875 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876{
Janos Follath24eed8d2019-11-22 13:21:35 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_asn1_buf alg_params;
880 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200881 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
885 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
886 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 }
888
889 end = *p + len;
890
Jethro Beekman01672442023-04-19 14:08:14 +0200891 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return ret;
893 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
896 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
897 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (*p + len != end) {
900 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
901 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
902 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
905 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
906 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
909 return ret;
910 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (pk_alg == MBEDTLS_PK_RSA) {
914 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200917#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200919#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200920 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200921 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200922 } else
923#endif
924 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200925 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200926 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (ret == 0) {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000928 ret = pk_ecc_set_pubkey(pk, *p, (size_t) (end - *p));
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200929 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200932#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ret == 0 && *p != end) {
936 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
937 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
938 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (ret != 0) {
941 mbedtls_pk_free(pk);
942 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945}
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100949 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
950 *
951 * The value zero is:
952 * - never a valid value for an RSA parameter
953 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
954 *
955 * Since values can't be omitted in PKCS#1, passing a zero value to
956 * rsa_complete() would be incorrect, so reject zero values early.
957 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958static int asn1_get_nonzero_mpi(unsigned char **p,
959 const unsigned char *end,
960 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100961{
962 int ret;
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 ret = mbedtls_asn1_get_mpi(p, end, X);
965 if (ret != 0) {
966 return ret;
967 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
970 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
971 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974}
975
976/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977 * Parse a PKCS#1 encoded private RSA key
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
980 const unsigned char *key,
981 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100983 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 size_t len;
985 unsigned char *p, *end;
986
Hanno Beckerefa14e82017-10-11 19:45:19 +0100987 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100989
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990 p = (unsigned char *) key;
991 end = p + keylen;
992
993 /*
994 * This function parses the RSAPrivateKey (PKCS#1)
995 *
996 * RSAPrivateKey ::= SEQUENCE {
997 * version Version,
998 * modulus INTEGER, -- n
999 * publicExponent INTEGER, -- e
1000 * privateExponent INTEGER, -- d
1001 * prime1 INTEGER, -- p
1002 * prime2 INTEGER, -- q
1003 * exponent1 INTEGER, -- d mod (p-1)
1004 * exponent2 INTEGER, -- d mod (q-1)
1005 * coefficient INTEGER, -- (inverse of q) mod p
1006 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1007 * }
1008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1010 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1011 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012 }
1013
1014 end = p + len;
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1017 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 }
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (version != 0) {
1021 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 }
1023
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001024 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1026 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1027 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001028 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001031 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1033 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1034 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001035 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037
1038 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1040 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1041 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001042 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001044
1045 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1047 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1048 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001049 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001051
1052 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1054 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1055 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001058
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001059#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001060 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1062 * that they can be easily recomputed from D, P and Q. However by
1063 * parsing them from the PKCS1 structure it is possible to avoid
1064 * recalculating them which both reduces the overhead of loading
1065 * RSA private keys into memory and also avoids side channels which
1066 * can arise when computing those values, since all of D, P, and Q
1067 * are secret. See https://eprint.iacr.org/2020/055 for a
1068 * description of one such attack.
1069 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001070
Jack Lloyd80cc8112020-01-22 17:34:29 -05001071 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1073 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1074 goto cleanup;
1075 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001076
1077 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1079 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1080 goto cleanup;
1081 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001082
1083 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1085 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1086 goto cleanup;
1087 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001088
Jack Lloyd60239752020-01-27 17:53:36 -05001089#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001090 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1092 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1093 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1094 goto cleanup;
1095 }
Jack Lloyd60239752020-01-27 17:53:36 -05001096#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001097
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001098 /* rsa_complete() doesn't complete anything with the default
1099 * implementation but is still called:
1100 * - for the benefit of alternative implementation that may want to
1101 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1102 * - as is also sanity-checks the key
1103 *
1104 * Furthermore, we also check the public part for consistency with
1105 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1108 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001109 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001110 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (p != end) {
1113 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1114 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115 }
1116
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001117cleanup:
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001122 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if ((ret & 0xff80) == 0) {
1124 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1125 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001126 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135
Valerio Setti81d75122023-06-14 14:49:33 +02001136#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137/*
1138 * Parse a SEC1 encoded private EC key
1139 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001140static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 const unsigned char *key, size_t keylen,
1142 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143{
Janos Follath24eed8d2019-11-22 13:21:35 +00001144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001145 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001146 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001147 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001149 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 unsigned char *end = p + keylen;
1151 unsigned char *end2;
1152
1153 /*
1154 * RFC 5915, or SEC1 Appendix C.4
1155 *
1156 * ECPrivateKey ::= SEQUENCE {
1157 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1158 * privateKey OCTET STRING,
1159 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1160 * publicKey [1] BIT STRING OPTIONAL
1161 * }
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 }
1167
1168 end = p + len;
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1171 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1172 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (version != 1) {
1175 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1176 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1179 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1180 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Valerio Setti6b062ee2023-06-30 17:32:57 +02001182 /* Keep a reference to the position fo the private key. It will be used
1183 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001184 d = p;
1185 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001186
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187 p += len;
1188
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001189 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001191 /*
1192 * Is 'parameters' present?
1193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1195 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1196 0)) == 0) {
1197 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001198 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001203 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001204 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001205
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001206 /*
1207 * Load the private key
1208 */
1209 ret = pk_ecc_set_key(pk, d, d_len);
1210 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001211 return ret;
1212 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001215 /*
1216 * Is 'publickey' present? If not, or if we can't read it (eg because it
1217 * is compressed), create it from the private key.
1218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1220 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1221 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001222 end2 = p + len;
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1226 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (p + len != end2) {
1229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1230 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1231 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001233 if ((ret = pk_ecc_set_pubkey(pk, p, (size_t) (end2 - p))) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001234 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001236 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001237 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238 * is if the point format is not recognized.
1239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1241 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1242 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001248
Valerio Setti34f67552023-04-03 15:19:18 +02001249 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001250 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001251 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001252 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001253 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256}
Valerio Setti81d75122023-06-14 14:49:33 +02001257#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001259/***********************************************************************
1260 *
1261 * PKCS#8 parsing functions
1262 *
1263 **********************************************************************/
1264
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265/*
1266 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001267 *
1268 * Notes:
1269 *
1270 * - This function does not own the key buffer. It is the
1271 * responsibility of the caller to take care of zeroizing
1272 * and freeing it after use.
1273 *
1274 * - The function is responsible for freeing the provided
1275 * PK context on failure.
1276 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 */
1278static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_pk_context *pk,
1280 const unsigned char *key, size_t keylen,
1281 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282{
1283 int ret, version;
1284 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 unsigned char *p = (unsigned char *) key;
1287 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001289 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
Valerio Setti81d75122023-06-14 14:49:33 +02001292#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001293 (void) f_rng;
1294 (void) p_rng;
1295#endif
1296
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001298 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 *
1300 * PrivateKeyInfo ::= SEQUENCE {
1301 * version Version,
1302 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1303 * privateKey PrivateKey,
1304 * attributes [0] IMPLICIT Attributes OPTIONAL }
1305 *
1306 * Version ::= INTEGER
1307 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1308 * PrivateKey ::= OCTET STRING
1309 *
1310 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1311 */
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 }
1317
1318 end = p + len;
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1321 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001322 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (version != 0) {
1325 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1326 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327
Jethro Beekman01672442023-04-19 14:08:14 +02001328 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 return ret;
1330 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1334 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (len < 1) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1338 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1339 }
1340
1341 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1342 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1343 }
1344
1345 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1346 return ret;
1347 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (pk_alg == MBEDTLS_PK_RSA) {
1351 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1352 mbedtls_pk_free(pk);
1353 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 }
1355 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001357#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001359#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001360 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001361 if ((ret =
1362 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001363 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001364 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001365 p_rng)) != 0) {
1366 mbedtls_pk_free(pk);
1367 return ret;
1368 }
1369 } else
1370#endif
1371 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001372 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1373 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001374 mbedtls_pk_free(pk);
1375 return ret;
1376 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 }
1378 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001379#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001382 end = p + len;
1383 if (end != (key + keylen)) {
1384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1385 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1386 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389}
1390
1391/*
1392 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001393 *
1394 * To save space, the decryption happens in-place on the given key buffer.
1395 * Also, while this function may modify the keybuffer, it doesn't own it,
1396 * and instead it is the responsibility of the caller to zeroize and properly
1397 * free it after use.
1398 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001401MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_pk_context *pk,
1403 unsigned char *key, size_t keylen,
1404 const unsigned char *pwd, size_t pwdlen,
1405 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001407 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001409 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie581e142023-12-29 16:35:07 +01001412#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_cipher_type_t cipher_alg;
1414 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001416 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417
Hanno Becker2aa80a72017-09-07 15:28:45 +01001418 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419 end = p + keylen;
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (pwdlen == 0) {
1422 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
1425 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001426 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 *
1428 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1429 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1430 * encryptedData EncryptedData
1431 * }
1432 *
1433 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1434 *
1435 * EncryptedData ::= OCTET STRING
1436 *
1437 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001438 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1441 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 }
1444
1445 end = p + len;
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1448 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1449 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1453 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454
Hanno Beckerfab35692017-08-25 13:38:26 +01001455 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456
1457 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001458 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459 */
Valerio Settie581e142023-12-29 16:35:07 +01001460#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001462 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001463 cipher_alg, md_alg,
1464 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1466 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1467 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001471
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001472 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else
Valerio Settie581e142023-12-29 16:35:07 +01001474#endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
1475#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) && defined(MBEDTLS_CIPHER_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001477 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1478 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1480 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1481 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001485
1486 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 } else
Valerio Settie581e142023-12-29 16:35:07 +01001488#endif /* MBEDTLS_PKCS5_C && MBEDTLS_CIPHER_PADDING_PKCS7 && MBEDTLS_CIPHER_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001489 {
1490 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001491 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (decrypted == 0) {
1494 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1495 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001496 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001500/***********************************************************************
1501 *
1502 * Top-level functions, with format auto-discovery
1503 *
1504 **********************************************************************/
1505
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506/*
1507 * Parse a private key
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1510 const unsigned char *key, size_t keylen,
1511 const unsigned char *pwd, size_t pwdlen,
1512 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513{
Janos Follath24eed8d2019-11-22 13:21:35 +00001514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001519#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (keylen == 0) {
1522 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1523 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001524
1525#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001529 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001531 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 } else {
1533 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001534 PEM_BEGIN_PRIVATE_KEY_RSA, PEM_END_PRIVATE_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 key, pwd, pwdlen, &len);
1536 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ret == 0) {
1539 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1540 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1541 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1542 pem.buf, pem.buflen)) != 0) {
1543 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544 }
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_pem_free(&pem);
1547 return ret;
1548 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1549 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1550 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1551 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1552 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1553 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556
Valerio Setti81d75122023-06-14 14:49:33 +02001557#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001558 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001560 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 } else {
1562 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001563 PEM_BEGIN_PRIVATE_KEY_EC,
1564 PEM_END_PRIVATE_KEY_EC,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 key, pwd, pwdlen, &len);
1566 }
1567 if (ret == 0) {
1568 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001571 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 pem.buf, pem.buflen,
1573 f_rng, p_rng)) != 0) {
1574 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575 }
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 mbedtls_pem_free(&pem);
1578 return ret;
1579 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1580 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1581 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1582 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1583 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1584 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001585 }
Valerio Setti81d75122023-06-14 14:49:33 +02001586#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001588 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001590 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 } else {
1592 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001593 PEM_BEGIN_PRIVATE_KEY_PKCS8, PEM_END_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 key, NULL, 0, &len);
1595 }
1596 if (ret == 0) {
1597 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1598 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1599 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001600 }
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 mbedtls_pem_free(&pem);
1603 return ret;
1604 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1605 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001609 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001611 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 } else {
1613 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001614 PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8,
1615 PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 key, NULL, 0, &len);
1617 }
1618 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001619 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1620 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 mbedtls_pem_free(&pem);
1625 return ret;
1626 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1627 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001630#else
1631 ((void) pwd);
1632 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634
1635 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001636 * At this point we only know it's not a PEM formatted key. Could be any
1637 * of the known DER encoded private key formats
1638 *
1639 * We try the different DER format parsers to see if one passes without
1640 * error
1641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001644 unsigned char *key_copy;
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1647 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1648 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001651
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001652 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1653 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001654
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001655 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656 }
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (ret == 0) {
1659 return 0;
1660 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 mbedtls_pk_free(pk);
1663 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1666 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1671 if (ret == 0) {
1672 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001673 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 mbedtls_pk_free(pk);
1676 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1681 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1682 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1683 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684 }
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 mbedtls_pk_free(pk);
1687 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689
Valerio Setti81d75122023-06-14 14:49:33 +02001690#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1692 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001693 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 key, keylen, f_rng, p_rng) == 0) {
1695 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001698#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699
Valerio Setti81d75122023-06-14 14:49:33 +02001700 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001701 * it is ok to leave the PK context initialized but not
1702 * freed: It is the caller's responsibility to call pk_init()
1703 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001704 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001705 * isn't, this leads to mbedtls_pk_free() being called
1706 * twice, once here and once by the caller, but this is
1707 * also ok and in line with the mbedtls_pk_free() calls
1708 * on failed PEM parsing attempts. */
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711}
1712
1713/*
1714 * Parse a public key
1715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001716int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1717 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718{
Janos Follath24eed8d2019-11-22 13:21:35 +00001719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001720 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001721#if defined(MBEDTLS_RSA_C)
1722 const mbedtls_pk_info_t *pk_info;
1723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001727#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (keylen == 0) {
1730 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1731 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001732
1733#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001735#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001736 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001738 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 } else {
1740 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001741 PEM_BEGIN_PUBLIC_KEY_RSA, PEM_END_PUBLIC_KEY_RSA,
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001743 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001744
1745 if (ret == 0) {
1746 p = pem.buf;
1747 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1748 mbedtls_pem_free(&pem);
1749 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1750 }
1751
1752 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1753 mbedtls_pem_free(&pem);
1754 return ret;
1755 }
1756
1757 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1758 mbedtls_pk_free(ctx);
1759 }
1760
1761 mbedtls_pem_free(&pem);
1762 return ret;
1763 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1764 mbedtls_pem_free(&pem);
1765 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001766 }
1767#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001768
1769 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001771 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 } else {
1773 ret = mbedtls_pem_read_buffer(&pem,
Valerio Setti854c7372023-11-28 08:37:57 +01001774 PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 key, NULL, 0, &len);
1776 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001779 /*
1780 * Was PEM encoded
1781 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001782 p = pem.buf;
1783
Jethro Beekman01672442023-04-19 14:08:14 +02001784 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mbedtls_pem_free(&pem);
1786 return ret;
1787 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1788 mbedtls_pem_free(&pem);
1789 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001793
1794#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1796 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001798
1799 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1800 return ret;
1801 }
1802
1803 p = (unsigned char *) key;
1804 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1805 if (ret == 0) {
1806 return ret;
1807 }
1808 mbedtls_pk_free(ctx);
1809 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1810 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1811 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001812 }
1813#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814 p = (unsigned char *) key;
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001819}
1820
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001821/***********************************************************************
1822 *
1823 * Top-level functions, with filesystem support
1824 *
1825 **********************************************************************/
1826
1827#if defined(MBEDTLS_FS_IO)
1828/*
1829 * Load all data from a file into a given buffer.
1830 *
1831 * The file is expected to contain either PEM or DER encoded data.
1832 * A terminating null byte is always appended. It is included in the announced
1833 * length only if the data looks like it is PEM encoded.
1834 */
1835int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1836{
1837 FILE *f;
1838 long size;
1839
1840 if ((f = fopen(path, "rb")) == NULL) {
1841 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1842 }
1843
1844 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1845 mbedtls_setbuf(f, NULL);
1846
1847 fseek(f, 0, SEEK_END);
1848 if ((size = ftell(f)) == -1) {
1849 fclose(f);
1850 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1851 }
1852 fseek(f, 0, SEEK_SET);
1853
1854 *n = (size_t) size;
1855
1856 if (*n + 1 == 0 ||
1857 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1858 fclose(f);
1859 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1860 }
1861
1862 if (fread(*buf, 1, *n, f) != *n) {
1863 fclose(f);
1864
1865 mbedtls_zeroize_and_free(*buf, *n);
1866
1867 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1868 }
1869
1870 fclose(f);
1871
1872 (*buf)[*n] = '\0';
1873
1874 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1875 ++*n;
1876 }
1877
1878 return 0;
1879}
1880
1881/*
1882 * Load and parse a private key
1883 */
1884int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1885 const char *path, const char *pwd,
1886 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1887{
1888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1889 size_t n;
1890 unsigned char *buf;
1891
1892 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1893 return ret;
1894 }
1895
1896 if (pwd == NULL) {
1897 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1898 } else {
1899 ret = mbedtls_pk_parse_key(ctx, buf, n,
1900 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1901 }
1902
1903 mbedtls_zeroize_and_free(buf, n);
1904
1905 return ret;
1906}
1907
1908/*
1909 * Load and parse a public key
1910 */
1911int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1912{
1913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1914 size_t n;
1915 unsigned char *buf;
1916
1917 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1918 return ret;
1919 }
1920
1921 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1922
1923 mbedtls_zeroize_and_free(buf, n);
1924
1925 return ret;
1926}
1927#endif /* MBEDTLS_FS_IO */
1928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#endif /* MBEDTLS_PK_PARSE_C */