blob: ef57cee806a14760b978fad50ff0a64843681c25 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020058#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
59
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020060/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020061 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020062 * ECC setters
63 *
64 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
65 * this macro will not appear outside this section.
66 * 2. All inputs are raw: no metadata, no ASN.1 until the next section.
67 *
68 **********************************************************************/
69
70/*
71 * Set the group used by this key.
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020072 *
73 * [in/out] pk: in: must have been pk_setup() to an ECC type
74 * out: will have group (curve) information set
75 * [in] grp_in: a supported group ID (not NONE)
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020077static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
78{
79#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
80 size_t ec_bits;
81 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
82
83 /* group may already be initialized; if so, make sure IDs match */
84 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
85 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
86 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
87 }
88
89 /* set group */
90 pk->ec_family = ec_family;
91 pk->ec_bits = ec_bits;
92
93 return 0;
94#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
95 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
96
97 /* grp may already be initialized; if so, make sure IDs match */
98 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
99 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
100 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
101 }
102
103 /* set group */
104 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
105#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
106}
107
108/*
109 * Set the private key material
110 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200111 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
112 * out: will have the private key set.
113 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200114 */
115static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200116 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200117{
118#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
120 psa_status_t status;
121
122 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
123 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
124 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
125 /* Montgomery allows only ECDH, others ECDSA too */
126 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
127 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
128 psa_set_key_enrollment_algorithm(&attributes,
129 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
130 }
131 psa_set_key_usage_flags(&attributes, flags);
132
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200133 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200134 return psa_pk_status_to_mbedtls(status);
135
136#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
137
138 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200139 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200140 if (ret != 0) {
141 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
142 }
143 return 0;
144#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
145}
146
147/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200148 * Derive a public key from its private counterpart.
149 * Computationally intensive, only use when public key is not available.
150 *
151 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
152 * out: will have the public key set.
153 * [in] prv, prv_len: the raw private key (see note below).
154 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200155 *
156 * Note: the private key information is always available from pk,
157 * however for convenience the serialized version is also passed,
158 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200159 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200160 *
161 * There are three implementations of this function:
162 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
163 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
164 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200165 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200166static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
167 const unsigned char *prv, size_t prv_len,
168 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200169{
170#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200171
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200172 (void) f_rng;
173 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200174 (void) prv;
175 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200176 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200177
178 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
179 &pk->pub_raw_len);
180 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200181
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200182#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200183
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200184 (void) f_rng;
185 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200186 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200187
188 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200189 size_t curve_bits;
190 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200191
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200192 /* Import private key into PSA, from serialized input */
193 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
194 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200195 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
196 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200197 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200198 if (status != PSA_SUCCESS) {
199 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200200 }
201
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200202 /* Export public key from PSA */
203 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
204 size_t pub_len;
205 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
206 psa_status_t destruction_status = psa_destroy_key(key_id);
207 if (status != PSA_SUCCESS) {
208 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200209 } else if (destruction_status != PSA_SUCCESS) {
210 return psa_pk_status_to_mbedtls(destruction_status);
211 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200212
213 /* Load serialized public key into ecp_keypair structure */
214 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
215
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200216#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200217
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200218 (void) prv;
219 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200220
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200221 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200222 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 +0200223
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200224#endif /* MBEDTLS_USE_PSA_CRYPTO */
225}
226
227#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
228/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200229 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200230 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200231 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
232 * functions to handle keys. However, currently psa_import_key() does not
233 * support compressed points. In case that support was explicitly requested,
234 * this fallback uses ECP functions to get the job done. This is the reason
235 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
236 *
237 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
238 * out: will have the public key set.
239 * [in] pub, pub_len: the public key as an ECPoint,
240 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200241 *
242 * Return:
243 * - 0 on success;
244 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
245 * but not supported;
246 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200247 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200248static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
249 const unsigned char *pub,
250 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200251{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200252#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200253 (void) pk;
254 (void) pub;
255 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200256 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200257#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200258 mbedtls_ecp_keypair ecp_key;
259 mbedtls_ecp_group_id ecp_group_id;
260 int ret;
261
262 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
263
264 mbedtls_ecp_keypair_init(&ecp_key);
265 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
266 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200267 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200268 }
269 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200270 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200271 if (ret != 0) {
272 goto exit;
273 }
274 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
275 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200276 &pk->pub_raw_len, pk->pub_raw,
277 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200278
279exit:
280 mbedtls_ecp_keypair_free(&ecp_key);
281 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200282#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
283}
284#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
285
286/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200287 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200288 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200289 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
290 * out: will have the public key set.
291 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200292 *
293 * Return:
294 * - 0 on success;
295 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
296 * but not supported;
297 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200298 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200299static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
300 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200301{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200302#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200303
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200304 /* Load the key */
Manuel Pégourié-Gonnard52e95482023-08-03 10:22:41 +0200305 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
306 /* Format directly supported by PSA:
307 * - non-Weierstrass curves that only have one format;
308 * - uncompressed format for Weierstrass curves. */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200309 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200310 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
311 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200312 memcpy(pk->pub_raw, pub, pub_len);
313 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200314 } else {
315 /* Other format, try the fallback */
316 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
317 if (ret != 0) {
318 return ret;
319 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100320 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200321
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200322 /* Validate the key by trying to import it */
323 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
324 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
325
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200326 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200327 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
328 psa_set_key_bits(&key_attrs, pk->ec_bits);
329
330 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200331 &key_id) != PSA_SUCCESS) ||
332 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
333 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200335
336 return 0;
337
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200338#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200339
340 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200341 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200342 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
343 if (ret != 0) {
344 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200346 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
347
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200348#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200349}
350
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200351/***********************************************************************
352 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200353 * Low-level ECC parsing: optional support for SpecifiedECDomain
354 *
355 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200356 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200357 * - pk_ecc_group_id_from_specified()
358 *
359 * All the other functions are internal to this section.
360 *
361 * The two "public" functions have a dummy variant provided
362 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
363 * abstraction layer for this macro, which should not appear outside
364 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200365 *
366 **********************************************************************/
367
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200368#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
369/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200370static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200371{
372 (void) tag;
373 return 0;
374}
375
376/* See the "real" version for documentation */
377static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
378 mbedtls_ecp_group_id *grp_id)
379{
380 (void) params;
381 (void) grp_id;
382 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
383}
384#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
385/*
386 * Tell if the passed tag might be the start of SpecifiedECDomain
387 * (that is, a sequence).
388 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200389static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200390{
391 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
392}
393
Paul Bakker1a7550a2013-09-15 13:01:22 +0200394/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
396 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200397 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398 * if it was encoded compressed.
399 *
400 * SpecifiedECDomain ::= SEQUENCE {
401 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
402 * fieldID FieldID {{FieldTypes}},
403 * curve Curve,
404 * base ECPoint,
405 * order INTEGER,
406 * cofactor INTEGER OPTIONAL,
407 * hash HashAlgorithm OPTIONAL,
408 * ...
409 * }
410 *
411 * We only support prime-field as field type, and ignore hash and cofactor.
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414{
Janos Follath24eed8d2019-11-22 13:21:35 +0000415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200417 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418 const unsigned char *end_field, *end_curve;
419 size_t len;
420 int ver;
421
422 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
425 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (ver < 1 || ver > 3) {
428 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
429 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431 /*
432 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
433 * fieldType FIELD-ID.&id({IOSet}),
434 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
435 * }
436 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
438 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
439 return ret;
440 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442 end_field = p + len;
443
444 /*
445 * FIELD-ID ::= TYPE-IDENTIFIER
446 * FieldTypes FIELD-ID ::= {
447 * { Prime-p IDENTIFIED BY prime-field } |
448 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
449 * }
450 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
453 return ret;
454 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
457 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
458 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459 }
460
461 p += len;
462
463 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
466 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (p != end_field) {
471 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
472 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
473 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474
475 /*
476 * Curve ::= SEQUENCE {
477 * a FieldElement,
478 * b FieldElement,
479 * seed BIT STRING OPTIONAL
480 * -- Shall be present if used in SpecifiedECDomain
481 * -- with version equal to ecdpVer2 or ecdpVer3
482 * }
483 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
485 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
486 return ret;
487 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100488
489 end_curve = p + len;
490
491 /*
492 * FieldElement ::= OCTET STRING
493 * containing an integer in the case of a prime field
494 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
496 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100498 }
499
500 p += len;
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
503 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
504 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100505 }
506
507 p += len;
508
509 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100511 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (p != end_curve) {
515 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
516 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
517 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100518
519 /*
520 * ECPoint ::= OCTET STRING
521 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
524 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
527 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100528 /*
529 * If we can't read the point because it's compressed, cheat by
530 * reading only the X coordinate and the parity bit of Y.
531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
533 (p[0] != 0x02 && p[0] != 0x03) ||
534 len != mbedtls_mpi_size(&grp->P) + 1 ||
535 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
536 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
537 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
538 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100539 }
540 }
541
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100542 p += len;
543
544 /*
545 * order INTEGER
546 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
548 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
549 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100552
553 /*
554 * Allow optional elements by purposefully not enforcing p == end here.
555 */
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100558}
559
560/*
561 * Find the group id associated with an (almost filled) group as generated by
562 * pk_group_from_specified(), or return an error if unknown.
563 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564static 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 +0100565{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100566 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_ecp_group ref;
568 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100573 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_ecp_group_free(&ref);
575 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100576
577 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
579 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
580 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
581 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
582 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
583 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
584 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100585 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 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 +0100587 break;
588 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100589 }
590
591cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100593
594 *grp_id = *id;
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100601}
602
603/*
604 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
605 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200606static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
607 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100608{
Janos Follath24eed8d2019-11-22 13:21:35 +0000609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100615 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100619
620cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000621 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200622 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000623 * temporary grp breaks that flow and it's members are populated
624 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
625 * which is assuming a group populated by _setup() may not clean-up
626 * properly -> Manually free it's members.
627 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000628 mbedtls_mpi_free(&grp.N);
629 mbedtls_mpi_free(&grp.P);
630 mbedtls_mpi_free(&grp.A);
631 mbedtls_mpi_free(&grp.B);
632 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100637
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200638/***********************************************************************
639 *
640 * Unsorted (yet!) from this point on until the next section header
641 *
642 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200643
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200644/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200645 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200646 * ECParameters ::= CHOICE {
647 * namedCurve OBJECT IDENTIFIER
648 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
649 * -- implicitCurve NULL
650 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200651 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200652static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
653 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200654{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200656
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200657 if (end - *p < 1) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
659 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200660 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200661
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200662 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200663 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200664 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200665 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200666 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
667 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
668 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200669
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200670 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200671 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
672 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200673
674 params->p = *p;
675 *p += params->len;
676
677 if (*p != end) {
678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
679 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
680 }
681
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200682 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200683}
684
685/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100687 *
688 * ECParameters ::= CHOICE {
689 * namedCurve OBJECT IDENTIFIER
690 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
691 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200693static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694{
Janos Follath24eed8d2019-11-22 13:21:35 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (params->tag == MBEDTLS_ASN1_OID) {
699 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
700 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
701 }
702 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200703 ret = pk_ecc_group_id_from_specified(params, &grp_id);
704 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return ret;
706 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100707 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200709 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710}
711
Jethro Beekman01672442023-04-19 14:08:14 +0200712#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
713
714/*
715 * Load an RFC8410 EC key, which doesn't have any parameters
716 */
717static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
718 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200719 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200720{
721 if (params->tag != 0 || params->len != 0) {
722 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
723 }
724
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200725 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200726}
727
728/*
729 * Parse an RFC 8410 encoded private EC key
730 *
731 * CurvePrivateKey ::= OCTET STRING
732 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200733static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200734 unsigned char *key, size_t keylen, const unsigned char *end,
735 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
736{
737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
738 size_t len;
739
740 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
741 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
742 }
743
744 if (key + len != end) {
745 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
746 }
747
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200748 /*
749 * Load the private key
750 */
751 ret = pk_ecc_set_key(pk, key, len);
752 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200753 return ret;
754 }
Jethro Beekman01672442023-04-19 14:08:14 +0200755
Valerio Setti4064dbb2023-05-17 15:33:07 +0200756 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
757 * which never contain a public key. As such, derive the public key
758 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200759 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200760 return ret;
761 }
762
Jethro Beekman01672442023-04-19 14:08:14 +0200763 return 0;
764}
765#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200766
Valerio Setti81d75122023-06-14 14:49:33 +0200767#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770/*
771 * RSAPublicKey ::= SEQUENCE {
772 * modulus INTEGER, -- n
773 * publicExponent INTEGER -- e
774 * }
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776static int pk_get_rsapubkey(unsigned char **p,
777 const unsigned char *end,
778 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779{
Janos Follath24eed8d2019-11-22 13:21:35 +0000780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781 size_t len;
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
784 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
786 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (*p + len != end) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
791 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
799 NULL, 0, NULL, 0)) != 0) {
800 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
801 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802
803 *p += len;
804
805 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
807 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
808 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
811 NULL, 0, *p, len)) != 0) {
812 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
813 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814
815 *p += len;
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (mbedtls_rsa_complete(rsa) != 0 ||
818 mbedtls_rsa_check_pubkey(rsa) != 0) {
819 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000820 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (*p != end) {
823 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
824 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
825 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830
831/* Get a PK algorithm identifier
832 *
833 * AlgorithmIdentifier ::= SEQUENCE {
834 * algorithm OBJECT IDENTIFIER,
835 * parameters ANY DEFINED BY algorithm OPTIONAL }
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837static int pk_get_pk_alg(unsigned char **p,
838 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200839 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
840 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841{
Janos Follath24eed8d2019-11-22 13:21:35 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
849 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Jethro Beekman01672442023-04-19 14:08:14 +0200851 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200852#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200853 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
854 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
855 if (ret == 0) {
856 *pk_alg = MBEDTLS_PK_ECKEY;
857 }
858 }
859#else
860 (void) ec_grp_id;
861#endif
862 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
864 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865
866 /*
867 * No parameters with RSA (only for EC)
868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (*pk_alg == MBEDTLS_PK_RSA &&
870 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
871 params->len != 0)) {
872 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 }
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876}
877
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200878/* Helper for Montgomery curves */
879#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
880#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
881 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
882#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
883
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884/*
885 * SubjectPublicKeyInfo ::= SEQUENCE {
886 * algorithm AlgorithmIdentifier,
887 * subjectPublicKey BIT STRING }
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
890 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891{
Janos Follath24eed8d2019-11-22 13:21:35 +0000892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_asn1_buf alg_params;
895 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200896 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
900 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
901 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902 }
903
904 end = *p + len;
905
Jethro Beekman01672442023-04-19 14:08:14 +0200906 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 return ret;
908 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (*p + len != end) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
916 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
917 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
920 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
921 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
924 return ret;
925 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (pk_alg == MBEDTLS_PK_RSA) {
929 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200930 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200932#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200934#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200935 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200936 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200937 } else
938#endif
939 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200940 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200941 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200943 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
944 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200946 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200947#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ret == 0 && *p != end) {
951 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
952 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
953 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (ret != 0) {
956 mbedtls_pk_free(pk);
957 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960}
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100964 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
965 *
966 * The value zero is:
967 * - never a valid value for an RSA parameter
968 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
969 *
970 * Since values can't be omitted in PKCS#1, passing a zero value to
971 * rsa_complete() would be incorrect, so reject zero values early.
972 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973static int asn1_get_nonzero_mpi(unsigned char **p,
974 const unsigned char *end,
975 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976{
977 int ret;
978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 ret = mbedtls_asn1_get_mpi(p, end, X);
980 if (ret != 0) {
981 return ret;
982 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
985 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
986 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100989}
990
991/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 * Parse a PKCS#1 encoded private RSA key
993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
995 const unsigned char *key,
996 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100998 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999 size_t len;
1000 unsigned char *p, *end;
1001
Hanno Beckerefa14e82017-10-11 19:45:19 +01001002 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001004
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 p = (unsigned char *) key;
1006 end = p + keylen;
1007
1008 /*
1009 * This function parses the RSAPrivateKey (PKCS#1)
1010 *
1011 * RSAPrivateKey ::= SEQUENCE {
1012 * version Version,
1013 * modulus INTEGER, -- n
1014 * publicExponent INTEGER, -- e
1015 * privateExponent INTEGER, -- d
1016 * prime1 INTEGER, -- p
1017 * prime2 INTEGER, -- q
1018 * exponent1 INTEGER, -- d mod (p-1)
1019 * exponent2 INTEGER, -- d mod (q-1)
1020 * coefficient INTEGER, -- (inverse of q) mod p
1021 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1022 * }
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1025 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1026 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 }
1028
1029 end = p + len;
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1032 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 }
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (version != 0) {
1036 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 }
1038
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1041 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1042 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001043 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1048 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1049 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052
1053 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1056 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059
1060 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1062 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1063 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066
1067 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1069 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1070 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001071 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001073
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001074#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001075 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1077 * that they can be easily recomputed from D, P and Q. However by
1078 * parsing them from the PKCS1 structure it is possible to avoid
1079 * recalculating them which both reduces the overhead of loading
1080 * RSA private keys into memory and also avoids side channels which
1081 * can arise when computing those values, since all of D, P, and Q
1082 * are secret. See https://eprint.iacr.org/2020/055 for a
1083 * description of one such attack.
1084 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001085
Jack Lloyd80cc8112020-01-22 17:34:29 -05001086 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1088 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1089 goto cleanup;
1090 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001091
1092 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1094 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1095 goto cleanup;
1096 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001097
1098 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1100 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1101 goto cleanup;
1102 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001103
Jack Lloyd60239752020-01-27 17:53:36 -05001104#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001105 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1107 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1108 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1109 goto cleanup;
1110 }
Jack Lloyd60239752020-01-27 17:53:36 -05001111#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001112
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001113 /* rsa_complete() doesn't complete anything with the default
1114 * implementation but is still called:
1115 * - for the benefit of alternative implementation that may want to
1116 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1117 * - as is also sanity-checks the key
1118 *
1119 * Furthermore, we also check the public part for consistency with
1120 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1123 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001124 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001125 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (p != end) {
1128 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1129 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 }
1131
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001132cleanup:
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001137 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if ((ret & 0xff80) == 0) {
1139 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1140 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150
Valerio Setti81d75122023-06-14 14:49:33 +02001151#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152/*
1153 * Parse a SEC1 encoded private EC key
1154 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001155static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 const unsigned char *key, size_t keylen,
1157 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158{
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001160 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001161 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001162 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001164 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 unsigned char *end = p + keylen;
1166 unsigned char *end2;
1167
1168 /*
1169 * RFC 5915, or SEC1 Appendix C.4
1170 *
1171 * ECPrivateKey ::= SEQUENCE {
1172 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1173 * privateKey OCTET STRING,
1174 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1175 * publicKey [1] BIT STRING OPTIONAL
1176 * }
1177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1179 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1180 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 }
1182
1183 end = p + len;
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1186 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1187 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (version != 1) {
1190 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1191 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196
Valerio Setti6b062ee2023-06-30 17:32:57 +02001197 /* Keep a reference to the position fo the private key. It will be used
1198 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001199 d = p;
1200 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001201
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 p += len;
1203
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001204 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001206 /*
1207 * Is 'parameters' present?
1208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1210 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1211 0)) == 0) {
1212 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001213 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001218 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001219 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001220
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001221 /*
1222 * Load the private key
1223 */
1224 ret = pk_ecc_set_key(pk, d, d_len);
1225 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001226 return ret;
1227 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001230 /*
1231 * Is 'publickey' present? If not, or if we can't read it (eg because it
1232 * is compressed), create it from the private key.
1233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1235 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1236 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237 end2 = p + len;
1238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1241 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (p + len != end2) {
1244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1245 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1246 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001247
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001248 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001251 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001252 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001253 * is if the point format is not recognized.
1254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1256 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1257 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001258 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001261 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001263
Valerio Setti34f67552023-04-03 15:19:18 +02001264 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001265 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001266 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001267 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001268 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271}
Valerio Setti81d75122023-06-14 14:49:33 +02001272#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001274/***********************************************************************
1275 *
1276 * PKCS#8 parsing functions
1277 *
1278 **********************************************************************/
1279
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280/*
1281 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001282 *
1283 * Notes:
1284 *
1285 * - This function does not own the key buffer. It is the
1286 * responsibility of the caller to take care of zeroizing
1287 * and freeing it after use.
1288 *
1289 * - The function is responsible for freeing the provided
1290 * PK context on failure.
1291 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292 */
1293static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_pk_context *pk,
1295 const unsigned char *key, size_t keylen,
1296 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297{
1298 int ret, version;
1299 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301 unsigned char *p = (unsigned char *) key;
1302 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001304 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306
Valerio Setti81d75122023-06-14 14:49:33 +02001307#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001308 (void) f_rng;
1309 (void) p_rng;
1310#endif
1311
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001313 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 *
1315 * PrivateKeyInfo ::= SEQUENCE {
1316 * version Version,
1317 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1318 * privateKey PrivateKey,
1319 * attributes [0] IMPLICIT Attributes OPTIONAL }
1320 *
1321 * Version ::= INTEGER
1322 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1323 * PrivateKey ::= OCTET STRING
1324 *
1325 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1326 */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1329 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 }
1332
1333 end = p + len;
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001337 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (version != 0) {
1340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1341 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342
Jethro Beekman01672442023-04-19 14:08:14 +02001343 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return ret;
1345 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1349 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (len < 1) {
1352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1353 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1354 }
1355
1356 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1357 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1358 }
1359
1360 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1361 return ret;
1362 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (pk_alg == MBEDTLS_PK_RSA) {
1366 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1367 mbedtls_pk_free(pk);
1368 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 }
1370 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001372#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001374#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001375 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001376 if ((ret =
1377 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001378 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001379 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001380 p_rng)) != 0) {
1381 mbedtls_pk_free(pk);
1382 return ret;
1383 }
1384 } else
1385#endif
1386 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001387 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1388 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001389 mbedtls_pk_free(pk);
1390 return ret;
1391 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 }
1393 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001394#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001397 end = p + len;
1398 if (end != (key + keylen)) {
1399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1401 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404}
1405
1406/*
1407 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001408 *
1409 * To save space, the decryption happens in-place on the given key buffer.
1410 * Also, while this function may modify the keybuffer, it doesn't own it,
1411 * and instead it is the responsibility of the caller to zeroize and properly
1412 * free it after use.
1413 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001416MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 mbedtls_pk_context *pk,
1418 unsigned char *key, size_t keylen,
1419 const unsigned char *pwd, size_t pwdlen,
1420 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001422 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001424 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
Valerio Settie86677d2023-10-12 16:05:10 +02001427#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_cipher_type_t cipher_alg;
1429 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001431 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
Hanno Becker2aa80a72017-09-07 15:28:45 +01001433 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 end = p + keylen;
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (pwdlen == 0) {
1437 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1438 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
1440 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001441 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 *
1443 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1444 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1445 * encryptedData EncryptedData
1446 * }
1447 *
1448 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1449 *
1450 * EncryptedData ::= OCTET STRING
1451 *
1452 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001453 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1456 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1457 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 }
1459
1460 end = p + len;
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1464 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1467 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1468 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469
Hanno Beckerfab35692017-08-25 13:38:26 +01001470 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471
1472 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001473 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474 */
Valerio Settie86677d2023-10-12 16:05:10 +02001475#if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001477 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001478 cipher_alg, md_alg,
1479 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1481 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1482 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001486
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001487 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PKCS12_C */
Valerio Settie86677d2023-10-12 16:05:10 +02001490#if defined(MBEDTLS_PKCS5_C) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001492 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1493 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1495 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1496 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001500
1501 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001504 {
1505 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001506 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (decrypted == 0) {
1509 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1510 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001511 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001515/***********************************************************************
1516 *
1517 * Top-level functions, with format auto-discovery
1518 *
1519 **********************************************************************/
1520
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521/*
1522 * Parse a private key
1523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1525 const unsigned char *key, size_t keylen,
1526 const unsigned char *pwd, size_t pwdlen,
1527 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528{
Janos Follath24eed8d2019-11-22 13:21:35 +00001529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001534#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if (keylen == 0) {
1537 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1538 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001539
1540#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001544 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001546 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 } else {
1548 ret = mbedtls_pem_read_buffer(&pem,
1549 "-----BEGIN RSA PRIVATE KEY-----",
1550 "-----END RSA PRIVATE KEY-----",
1551 key, pwd, pwdlen, &len);
1552 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (ret == 0) {
1555 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1556 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1557 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1558 pem.buf, pem.buflen)) != 0) {
1559 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 }
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_pem_free(&pem);
1563 return ret;
1564 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1565 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1566 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1567 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1568 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1569 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001570 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572
Valerio Setti81d75122023-06-14 14:49:33 +02001573#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001574 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001576 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 } else {
1578 ret = mbedtls_pem_read_buffer(&pem,
1579 "-----BEGIN EC PRIVATE KEY-----",
1580 "-----END EC PRIVATE KEY-----",
1581 key, pwd, pwdlen, &len);
1582 }
1583 if (ret == 0) {
1584 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001587 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 pem.buf, pem.buflen,
1589 f_rng, p_rng)) != 0) {
1590 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591 }
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 mbedtls_pem_free(&pem);
1594 return ret;
1595 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1596 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1597 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1598 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1599 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1600 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601 }
Valerio Setti81d75122023-06-14 14:49:33 +02001602#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001604 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001606 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 } else {
1608 ret = mbedtls_pem_read_buffer(&pem,
1609 "-----BEGIN PRIVATE KEY-----",
1610 "-----END PRIVATE KEY-----",
1611 key, NULL, 0, &len);
1612 }
1613 if (ret == 0) {
1614 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1615 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1616 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001617 }
1618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 mbedtls_pem_free(&pem);
1620 return ret;
1621 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1622 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001623 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001626 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001628 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 } else {
1630 ret = mbedtls_pem_read_buffer(&pem,
1631 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1632 "-----END ENCRYPTED PRIVATE KEY-----",
1633 key, NULL, 0, &len);
1634 }
1635 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001636 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1637 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001639 }
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 mbedtls_pem_free(&pem);
1642 return ret;
1643 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1644 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001647#else
1648 ((void) pwd);
1649 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651
1652 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001653 * At this point we only know it's not a PEM formatted key. Could be any
1654 * of the known DER encoded private key formats
1655 *
1656 * We try the different DER format parsers to see if one passes without
1657 * error
1658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001661 unsigned char *key_copy;
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1664 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1665 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001668
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001669 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1670 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001671
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001672 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001673 }
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (ret == 0) {
1676 return 0;
1677 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_pk_free(pk);
1680 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1683 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1688 if (ret == 0) {
1689 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001690 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 mbedtls_pk_free(pk);
1693 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1698 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1699 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1700 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701 }
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_pk_free(pk);
1704 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001706
Valerio Setti81d75122023-06-14 14:49:33 +02001707#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1709 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001710 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 key, keylen, f_rng, p_rng) == 0) {
1712 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001715#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001716
Valerio Setti81d75122023-06-14 14:49:33 +02001717 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001718 * it is ok to leave the PK context initialized but not
1719 * freed: It is the caller's responsibility to call pk_init()
1720 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001721 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001722 * isn't, this leads to mbedtls_pk_free() being called
1723 * twice, once here and once by the caller, but this is
1724 * also ok and in line with the mbedtls_pk_free() calls
1725 * on failed PEM parsing attempts. */
1726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728}
1729
1730/*
1731 * Parse a public key
1732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1734 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001735{
Janos Follath24eed8d2019-11-22 13:21:35 +00001736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001737 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001738#if defined(MBEDTLS_RSA_C)
1739 const mbedtls_pk_info_t *pk_info;
1740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001742 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001744#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (keylen == 0) {
1747 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1748 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001749
1750#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001752#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001753 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001755 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 } else {
1757 ret = mbedtls_pem_read_buffer(&pem,
1758 "-----BEGIN RSA PUBLIC KEY-----",
1759 "-----END RSA PUBLIC KEY-----",
1760 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001762
1763 if (ret == 0) {
1764 p = pem.buf;
1765 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1766 mbedtls_pem_free(&pem);
1767 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1768 }
1769
1770 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1771 mbedtls_pem_free(&pem);
1772 return ret;
1773 }
1774
1775 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1776 mbedtls_pk_free(ctx);
1777 }
1778
1779 mbedtls_pem_free(&pem);
1780 return ret;
1781 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1782 mbedtls_pem_free(&pem);
1783 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001784 }
1785#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001786
1787 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001789 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 } else {
1791 ret = mbedtls_pem_read_buffer(&pem,
1792 "-----BEGIN PUBLIC KEY-----",
1793 "-----END PUBLIC KEY-----",
1794 key, NULL, 0, &len);
1795 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001798 /*
1799 * Was PEM encoded
1800 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001801 p = pem.buf;
1802
Jethro Beekman01672442023-04-19 14:08:14 +02001803 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 mbedtls_pem_free(&pem);
1805 return ret;
1806 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1807 mbedtls_pem_free(&pem);
1808 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001809 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001812
1813#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1815 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001816 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001817
1818 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1819 return ret;
1820 }
1821
1822 p = (unsigned char *) key;
1823 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1824 if (ret == 0) {
1825 return ret;
1826 }
1827 mbedtls_pk_free(ctx);
1828 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1829 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1830 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001831 }
1832#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001833 p = (unsigned char *) key;
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001838}
1839
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001840/***********************************************************************
1841 *
1842 * Top-level functions, with filesystem support
1843 *
1844 **********************************************************************/
1845
1846#if defined(MBEDTLS_FS_IO)
1847/*
1848 * Load all data from a file into a given buffer.
1849 *
1850 * The file is expected to contain either PEM or DER encoded data.
1851 * A terminating null byte is always appended. It is included in the announced
1852 * length only if the data looks like it is PEM encoded.
1853 */
1854int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1855{
1856 FILE *f;
1857 long size;
1858
1859 if ((f = fopen(path, "rb")) == NULL) {
1860 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1861 }
1862
1863 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1864 mbedtls_setbuf(f, NULL);
1865
1866 fseek(f, 0, SEEK_END);
1867 if ((size = ftell(f)) == -1) {
1868 fclose(f);
1869 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1870 }
1871 fseek(f, 0, SEEK_SET);
1872
1873 *n = (size_t) size;
1874
1875 if (*n + 1 == 0 ||
1876 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1877 fclose(f);
1878 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1879 }
1880
1881 if (fread(*buf, 1, *n, f) != *n) {
1882 fclose(f);
1883
1884 mbedtls_zeroize_and_free(*buf, *n);
1885
1886 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1887 }
1888
1889 fclose(f);
1890
1891 (*buf)[*n] = '\0';
1892
1893 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1894 ++*n;
1895 }
1896
1897 return 0;
1898}
1899
1900/*
1901 * Load and parse a private key
1902 */
1903int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1904 const char *path, const char *pwd,
1905 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1906{
1907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1908 size_t n;
1909 unsigned char *buf;
1910
1911 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1912 return ret;
1913 }
1914
1915 if (pwd == NULL) {
1916 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1917 } else {
1918 ret = mbedtls_pk_parse_key(ctx, buf, n,
1919 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1920 }
1921
1922 mbedtls_zeroize_and_free(buf, n);
1923
1924 return ret;
1925}
1926
1927/*
1928 * Load and parse a public key
1929 */
1930int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1931{
1932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1933 size_t n;
1934 unsigned char *buf;
1935
1936 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1937 return ret;
1938 }
1939
1940 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1941
1942 mbedtls_zeroize_and_free(buf, n);
1943
1944 return ret;
1945}
1946#endif /* MBEDTLS_FS_IO */
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#endif /* MBEDTLS_PK_PARSE_C */