blob: f897c1e8c163b4d4812ba4326fffa45e6cdf7b62 [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
159 * (as otherwise we're 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é-Gonnard54708982023-07-26 15:38:36 +0200241 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200242static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
243 const unsigned char *pub,
244 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200245{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200246#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
247 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
248#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200249 mbedtls_ecp_keypair ecp_key;
250 mbedtls_ecp_group_id ecp_group_id;
251 int ret;
252
253 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
254
255 mbedtls_ecp_keypair_init(&ecp_key);
256 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
257 if (ret != 0) {
258 return ret;
259 }
260 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200261 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200262 if (ret != 0) {
263 goto exit;
264 }
265 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
266 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200267 &pk->pub_raw_len, pk->pub_raw,
268 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200269
270exit:
271 mbedtls_ecp_keypair_free(&ecp_key);
272 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200273#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
274}
275#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
276
277/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200278 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200279 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200280 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
281 * out: will have the public key set.
282 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200283 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200284static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
285 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200286{
Janos Follath24eed8d2019-11-22 13:21:35 +0000287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200288
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200289#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
290 mbedtls_svc_key_id_t key;
291 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200292
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200293 if (pub_len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200294 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100296
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200297 if ((*pub == 0x02) || (*pub == 0x03)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200298 /* Compressed format, not supported by PSA Crypto.
299 * Try converting using functions from ECP_LIGHT. */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200300 ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200301 if (ret != 0) {
302 return ret;
303 }
304 } else {
305 /* Uncompressed format */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200306 if (pub_len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200307 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
308 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200309 memcpy(pk->pub_raw, pub, pub_len);
310 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200312
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200313 /* Validate the key by trying to importing it */
314 psa_set_key_usage_flags(&key_attrs, 0);
315 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
316 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
317 psa_set_key_bits(&key_attrs, pk->ec_bits);
318
319 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
320 &key) != PSA_SUCCESS) ||
321 (psa_destroy_key(key) != PSA_SUCCESS)) {
322 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
323 pk->pub_raw_len = 0;
324 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200326 ret = 0;
327#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
328 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
329 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200330 pub, pub_len)) == 0) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200331 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200333#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200334 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200335}
336
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200337/***********************************************************************
338 *
339 * Unsorted (yet!) from this point on until the next section header
340 *
341 **********************************************************************/
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200344/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100345 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
346 * WARNING: the resulting group should only be used with
347 * pk_group_id_from_specified(), since its base point may not be set correctly
348 * if it was encoded compressed.
349 *
350 * SpecifiedECDomain ::= SEQUENCE {
351 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
352 * fieldID FieldID {{FieldTypes}},
353 * curve Curve,
354 * base ECPoint,
355 * order INTEGER,
356 * cofactor INTEGER OPTIONAL,
357 * hash HashAlgorithm OPTIONAL,
358 * ...
359 * }
360 *
361 * We only support prime-field as field type, and ignore hash and cofactor.
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364{
Janos Follath24eed8d2019-11-22 13:21:35 +0000365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200367 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368 const unsigned char *end_field, *end_curve;
369 size_t len;
370 int ver;
371
372 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
375 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (ver < 1 || ver > 3) {
378 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
379 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100380
381 /*
382 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
383 * fieldType FIELD-ID.&id({IOSet}),
384 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
385 * }
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
388 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
389 return ret;
390 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
392 end_field = p + len;
393
394 /*
395 * FIELD-ID ::= TYPE-IDENTIFIER
396 * FieldTypes FIELD-ID ::= {
397 * { Prime-p IDENTIFIED BY prime-field } |
398 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
399 * }
400 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
403 return ret;
404 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
407 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
408 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409 }
410
411 p += len;
412
413 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (p != end_field) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
422 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
423 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
425 /*
426 * Curve ::= SEQUENCE {
427 * a FieldElement,
428 * b FieldElement,
429 * seed BIT STRING OPTIONAL
430 * -- Shall be present if used in SpecifiedECDomain
431 * -- with version equal to ecdpVer2 or ecdpVer3
432 * }
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
435 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
436 return ret;
437 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438
439 end_curve = p + len;
440
441 /*
442 * FieldElement ::= OCTET STRING
443 * containing an integer in the case of a prime field
444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
446 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
447 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448 }
449
450 p += len;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
453 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455 }
456
457 p += len;
458
459 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (p != end_curve) {
465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
467 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
469 /*
470 * ECPoint ::= OCTET STRING
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
473 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
474 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
477 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100478 /*
479 * If we can't read the point because it's compressed, cheat by
480 * reading only the X coordinate and the parity bit of Y.
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
483 (p[0] != 0x02 && p[0] != 0x03) ||
484 len != mbedtls_mpi_size(&grp->P) + 1 ||
485 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
486 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
487 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
488 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100489 }
490 }
491
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 p += len;
493
494 /*
495 * order INTEGER
496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
498 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
499 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100502
503 /*
504 * Allow optional elements by purposefully not enforcing p == end here.
505 */
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100508}
509
510/*
511 * Find the group id associated with an (almost filled) group as generated by
512 * pk_group_from_specified(), or return an error if unknown.
513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514static 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 +0100515{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100516 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_ecp_group ref;
518 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100523 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_ecp_group_free(&ref);
525 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100526
527 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
529 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
530 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
531 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
532 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
533 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
534 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100535 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 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 +0100537 break;
538 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100539 }
540
541cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100543
544 *grp_id = *id;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100551}
552
553/*
554 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
557 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100558{
Janos Follath24eed8d2019-11-22 13:21:35 +0000559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100565 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100569
570cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000571 /* The API respecting lifecycle for mbedtls_ecp_group struct is
572 * _init(), _load() and _free(). In pk_group_id_from_specified() the
573 * temporary grp breaks that flow and it's members are populated
574 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
575 * which is assuming a group populated by _setup() may not clean-up
576 * properly -> Manually free it's members.
577 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000578 mbedtls_mpi_free(&grp.N);
579 mbedtls_mpi_free(&grp.P);
580 mbedtls_mpi_free(&grp.A);
581 mbedtls_mpi_free(&grp.B);
582 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100587
Valerio Setti4064dbb2023-05-17 15:33:07 +0200588
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200589/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200590 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200591 * ECParameters ::= CHOICE {
592 * namedCurve OBJECT IDENTIFIER
593 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
594 * -- implicitCurve NULL
595 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200596 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200597static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
598 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200599{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200601
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200602 if (end - *p < 1) {
603 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
604 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200605 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200606
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200607 /* Tag may be either OID or SEQUENCE */
608 params->tag = **p;
609 if (params->tag != MBEDTLS_ASN1_OID
610#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
611 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
612#endif
613 ) {
614 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
615 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
616 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200617
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200618 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200619 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
620 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200621
622 params->p = *p;
623 *p += params->len;
624
625 if (*p != end) {
626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
627 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
628 }
629
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200630 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200631}
632
633/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100635 *
636 * ECParameters ::= CHOICE {
637 * namedCurve OBJECT IDENTIFIER
638 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
639 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200641static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642{
Janos Follath24eed8d2019-11-22 13:21:35 +0000643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (params->tag == MBEDTLS_ASN1_OID) {
647 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
648 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
649 }
650 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
653 return ret;
654 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100655#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100657#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100658 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200659
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200660 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661}
662
Jethro Beekman01672442023-04-19 14:08:14 +0200663#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
664
665/*
666 * Load an RFC8410 EC key, which doesn't have any parameters
667 */
668static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
669 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200670 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200671{
672 if (params->tag != 0 || params->len != 0) {
673 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
674 }
675
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200676 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200677}
678
679/*
680 * Parse an RFC 8410 encoded private EC key
681 *
682 * CurvePrivateKey ::= OCTET STRING
683 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200684static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200685 unsigned char *key, size_t keylen, const unsigned char *end,
686 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
687{
688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
689 size_t len;
690
691 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
692 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
693 }
694
695 if (key + len != end) {
696 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
697 }
698
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200699 /*
700 * Load the private key
701 */
702 ret = pk_ecc_set_key(pk, key, len);
703 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200704 return ret;
705 }
Jethro Beekman01672442023-04-19 14:08:14 +0200706
Valerio Setti4064dbb2023-05-17 15:33:07 +0200707 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
708 * which never contain a public key. As such, derive the public key
709 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200710 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200711 return ret;
712 }
713
Jethro Beekman01672442023-04-19 14:08:14 +0200714 return 0;
715}
716#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200717
Valerio Setti81d75122023-06-14 14:49:33 +0200718#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721/*
722 * RSAPublicKey ::= SEQUENCE {
723 * modulus INTEGER, -- n
724 * publicExponent INTEGER -- e
725 * }
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727static int pk_get_rsapubkey(unsigned char **p,
728 const unsigned char *end,
729 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730{
Janos Follath24eed8d2019-11-22 13:21:35 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732 size_t len;
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
736 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
737 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (*p + len != end) {
740 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
741 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
742 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
747 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
750 NULL, 0, NULL, 0)) != 0) {
751 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
752 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753
754 *p += len;
755
756 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
758 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
759 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
762 NULL, 0, *p, len)) != 0) {
763 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
764 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765
766 *p += len;
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (mbedtls_rsa_complete(rsa) != 0 ||
769 mbedtls_rsa_check_pubkey(rsa) != 0) {
770 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000771 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (*p != end) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
775 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
776 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
782/* Get a PK algorithm identifier
783 *
784 * AlgorithmIdentifier ::= SEQUENCE {
785 * algorithm OBJECT IDENTIFIER,
786 * parameters ANY DEFINED BY algorithm OPTIONAL }
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788static int pk_get_pk_alg(unsigned char **p,
789 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200790 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
791 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792{
Janos Follath24eed8d2019-11-22 13:21:35 +0000793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
799 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801
Jethro Beekman01672442023-04-19 14:08:14 +0200802 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200803#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200804 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
805 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
806 if (ret == 0) {
807 *pk_alg = MBEDTLS_PK_ECKEY;
808 }
809 }
810#else
811 (void) ec_grp_id;
812#endif
813 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
817 /*
818 * No parameters with RSA (only for EC)
819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (*pk_alg == MBEDTLS_PK_RSA &&
821 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
822 params->len != 0)) {
823 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827}
828
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200829/* Helper for Montgomery curves */
830#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
831#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
832 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
833#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
834
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835/*
836 * SubjectPublicKeyInfo ::= SEQUENCE {
837 * algorithm AlgorithmIdentifier,
838 * subjectPublicKey BIT STRING }
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
841 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842{
Janos Follath24eed8d2019-11-22 13:21:35 +0000843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_asn1_buf alg_params;
846 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200847 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
851 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
852 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 }
854
855 end = *p + len;
856
Jethro Beekman01672442023-04-19 14:08:14 +0200857 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 return ret;
859 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
862 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
863 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (*p + len != end) {
866 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
868 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
871 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
872 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
875 return ret;
876 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (pk_alg == MBEDTLS_PK_RSA) {
880 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200883#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200885#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200886 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200887 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200888 } else
889#endif
890 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200891 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200892 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200894 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
895 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200898#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (ret == 0 && *p != end) {
902 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
903 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
904 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (ret != 0) {
907 mbedtls_pk_free(pk);
908 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911}
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100915 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
916 *
917 * The value zero is:
918 * - never a valid value for an RSA parameter
919 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
920 *
921 * Since values can't be omitted in PKCS#1, passing a zero value to
922 * rsa_complete() would be incorrect, so reject zero values early.
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924static int asn1_get_nonzero_mpi(unsigned char **p,
925 const unsigned char *end,
926 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100927{
928 int ret;
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 ret = mbedtls_asn1_get_mpi(p, end, X);
931 if (ret != 0) {
932 return ret;
933 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
936 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
937 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100940}
941
942/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943 * Parse a PKCS#1 encoded private RSA key
944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
946 const unsigned char *key,
947 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100949 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950 size_t len;
951 unsigned char *p, *end;
952
Hanno Beckerefa14e82017-10-11 19:45:19 +0100953 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100955
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956 p = (unsigned char *) key;
957 end = p + keylen;
958
959 /*
960 * This function parses the RSAPrivateKey (PKCS#1)
961 *
962 * RSAPrivateKey ::= SEQUENCE {
963 * version Version,
964 * modulus INTEGER, -- n
965 * publicExponent INTEGER, -- e
966 * privateExponent INTEGER, -- d
967 * prime1 INTEGER, -- p
968 * prime2 INTEGER, -- q
969 * exponent1 INTEGER, -- d mod (p-1)
970 * exponent2 INTEGER, -- d mod (q-1)
971 * coefficient INTEGER, -- (inverse of q) mod p
972 * otherPrimeInfos OtherPrimeInfos OPTIONAL
973 * }
974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
976 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
977 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978 }
979
980 end = p + len;
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
983 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 }
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (version != 0) {
987 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 }
989
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100990 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
992 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
993 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100994 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100997 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
999 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1000 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001001 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001003
1004 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1006 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1007 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001008 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001010
1011 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1013 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1014 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001015 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001017
1018 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1020 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1021 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001022 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001024
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001025#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001026 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1028 * that they can be easily recomputed from D, P and Q. However by
1029 * parsing them from the PKCS1 structure it is possible to avoid
1030 * recalculating them which both reduces the overhead of loading
1031 * RSA private keys into memory and also avoids side channels which
1032 * can arise when computing those values, since all of D, P, and Q
1033 * are secret. See https://eprint.iacr.org/2020/055 for a
1034 * description of one such attack.
1035 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001036
Jack Lloyd80cc8112020-01-22 17:34:29 -05001037 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1039 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1040 goto cleanup;
1041 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001042
1043 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1045 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1046 goto cleanup;
1047 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001048
1049 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1051 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1052 goto cleanup;
1053 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001054
Jack Lloyd60239752020-01-27 17:53:36 -05001055#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001056 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1058 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1059 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1060 goto cleanup;
1061 }
Jack Lloyd60239752020-01-27 17:53:36 -05001062#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001063
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001064 /* rsa_complete() doesn't complete anything with the default
1065 * implementation but is still called:
1066 * - for the benefit of alternative implementation that may want to
1067 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1068 * - as is also sanity-checks the key
1069 *
1070 * Furthermore, we also check the public part for consistency with
1071 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1074 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001076 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (p != end) {
1079 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1080 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 }
1082
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001083cleanup:
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001088 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if ((ret & 0xff80) == 0) {
1090 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1091 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001092 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 }
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101
Valerio Setti81d75122023-06-14 14:49:33 +02001102#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103/*
1104 * Parse a SEC1 encoded private EC key
1105 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001106static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 const unsigned char *key, size_t keylen,
1108 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109{
Janos Follath24eed8d2019-11-22 13:21:35 +00001110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001111 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001112 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001113 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001115 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 unsigned char *end = p + keylen;
1117 unsigned char *end2;
1118
1119 /*
1120 * RFC 5915, or SEC1 Appendix C.4
1121 *
1122 * ECPrivateKey ::= SEQUENCE {
1123 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1124 * privateKey OCTET STRING,
1125 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1126 * publicKey [1] BIT STRING OPTIONAL
1127 * }
1128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1131 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 }
1133
1134 end = p + len;
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1137 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (version != 1) {
1141 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1142 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1145 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1146 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147
Valerio Setti6b062ee2023-06-30 17:32:57 +02001148 /* Keep a reference to the position fo the private key. It will be used
1149 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001150 d = p;
1151 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001152
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 p += len;
1154
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001155 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001157 /*
1158 * Is 'parameters' present?
1159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1161 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1162 0)) == 0) {
1163 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001164 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001169 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001170 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001171
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001172 /*
1173 * Load the private key
1174 */
1175 ret = pk_ecc_set_key(pk, d, d_len);
1176 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001177 return ret;
1178 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001181 /*
1182 * Is 'publickey' present? If not, or if we can't read it (eg because it
1183 * is compressed), create it from the private key.
1184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1186 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1187 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001188 end2 = p + len;
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1192 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (p + len != end2) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1196 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1197 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001198
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001199 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001200 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001203 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001204 * is if the point format is not recognized.
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1207 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1208 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001214
Valerio Setti34f67552023-04-03 15:19:18 +02001215 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001216 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001217 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001218 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001219 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222}
Valerio Setti81d75122023-06-14 14:49:33 +02001223#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001225/***********************************************************************
1226 *
1227 * PKCS#8 parsing functions
1228 *
1229 **********************************************************************/
1230
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231/*
1232 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001233 *
1234 * Notes:
1235 *
1236 * - This function does not own the key buffer. It is the
1237 * responsibility of the caller to take care of zeroizing
1238 * and freeing it after use.
1239 *
1240 * - The function is responsible for freeing the provided
1241 * PK context on failure.
1242 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 */
1244static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 mbedtls_pk_context *pk,
1246 const unsigned char *key, size_t keylen,
1247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248{
1249 int ret, version;
1250 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252 unsigned char *p = (unsigned char *) key;
1253 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001255 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257
Valerio Setti81d75122023-06-14 14:49:33 +02001258#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001259 (void) f_rng;
1260 (void) p_rng;
1261#endif
1262
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001264 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 *
1266 * PrivateKeyInfo ::= SEQUENCE {
1267 * version Version,
1268 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1269 * privateKey PrivateKey,
1270 * attributes [0] IMPLICIT Attributes OPTIONAL }
1271 *
1272 * Version ::= INTEGER
1273 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1274 * PrivateKey ::= OCTET STRING
1275 *
1276 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1277 */
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1281 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282 }
1283
1284 end = p + len;
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001288 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (version != 0) {
1291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1292 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Jethro Beekman01672442023-04-19 14:08:14 +02001294 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 return ret;
1296 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1300 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (len < 1) {
1303 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1304 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1305 }
1306
1307 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1308 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1309 }
1310
1311 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1312 return ret;
1313 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (pk_alg == MBEDTLS_PK_RSA) {
1317 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1318 mbedtls_pk_free(pk);
1319 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 }
1321 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001323#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001325#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001326 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001327 if ((ret =
1328 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001329 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001330 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001331 p_rng)) != 0) {
1332 mbedtls_pk_free(pk);
1333 return ret;
1334 }
1335 } else
1336#endif
1337 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001338 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1339 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001340 mbedtls_pk_free(pk);
1341 return ret;
1342 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 }
1344 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001345#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001348 end = p + len;
1349 if (end != (key + keylen)) {
1350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1352 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355}
1356
1357/*
1358 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001359 *
1360 * To save space, the decryption happens in-place on the given key buffer.
1361 * Also, while this function may modify the keybuffer, it doesn't own it,
1362 * and instead it is the responsibility of the caller to zeroize and properly
1363 * free it after use.
1364 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001367MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 mbedtls_pk_context *pk,
1369 unsigned char *key, size_t keylen,
1370 const unsigned char *pwd, size_t pwdlen,
1371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001373 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001375 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1378#if defined(MBEDTLS_PKCS12_C)
1379 mbedtls_cipher_type_t cipher_alg;
1380 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001382 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383
Hanno Becker2aa80a72017-09-07 15:28:45 +01001384 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385 end = p + keylen;
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (pwdlen == 0) {
1388 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1389 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390
1391 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001392 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 *
1394 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1395 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1396 * encryptedData EncryptedData
1397 * }
1398 *
1399 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1400 *
1401 * EncryptedData ::= OCTET STRING
1402 *
1403 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001404 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1407 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1408 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 }
1410
1411 end = p + len;
1412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1415 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1419 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420
Hanno Beckerfab35692017-08-25 13:38:26 +01001421 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
1423 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001424 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001428 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001429 cipher_alg, md_alg,
1430 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1432 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1433 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001437
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001438 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_PKCS12_C */
1441#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001443 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1444 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1446 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1447 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001451
1452 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001455 {
1456 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001457 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (decrypted == 0) {
1460 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1461 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001462 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001466/***********************************************************************
1467 *
1468 * Top-level functions, with format auto-discovery
1469 *
1470 **********************************************************************/
1471
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472/*
1473 * Parse a private key
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1476 const unsigned char *key, size_t keylen,
1477 const unsigned char *pwd, size_t pwdlen,
1478 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479{
Janos Follath24eed8d2019-11-22 13:21:35 +00001480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001485#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (keylen == 0) {
1488 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1489 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001490
1491#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001495 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001497 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 } else {
1499 ret = mbedtls_pem_read_buffer(&pem,
1500 "-----BEGIN RSA PRIVATE KEY-----",
1501 "-----END RSA PRIVATE KEY-----",
1502 key, pwd, pwdlen, &len);
1503 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (ret == 0) {
1506 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1507 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1508 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1509 pem.buf, pem.buflen)) != 0) {
1510 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001511 }
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 mbedtls_pem_free(&pem);
1514 return ret;
1515 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1516 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1517 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1518 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1519 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1520 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523
Valerio Setti81d75122023-06-14 14:49:33 +02001524#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001525 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001527 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 } else {
1529 ret = mbedtls_pem_read_buffer(&pem,
1530 "-----BEGIN EC PRIVATE KEY-----",
1531 "-----END EC PRIVATE KEY-----",
1532 key, pwd, pwdlen, &len);
1533 }
1534 if (ret == 0) {
1535 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001538 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 pem.buf, pem.buflen,
1540 f_rng, p_rng)) != 0) {
1541 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001542 }
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_pem_free(&pem);
1545 return ret;
1546 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1547 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1548 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1549 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1550 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1551 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552 }
Valerio Setti81d75122023-06-14 14:49:33 +02001553#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001555 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001557 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 } else {
1559 ret = mbedtls_pem_read_buffer(&pem,
1560 "-----BEGIN PRIVATE KEY-----",
1561 "-----END PRIVATE KEY-----",
1562 key, NULL, 0, &len);
1563 }
1564 if (ret == 0) {
1565 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1566 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1567 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001568 }
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 mbedtls_pem_free(&pem);
1571 return ret;
1572 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1573 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001579 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 } else {
1581 ret = mbedtls_pem_read_buffer(&pem,
1582 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1583 "-----END ENCRYPTED PRIVATE KEY-----",
1584 key, NULL, 0, &len);
1585 }
1586 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001587 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1588 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590 }
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_pem_free(&pem);
1593 return ret;
1594 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1595 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598#else
1599 ((void) pwd);
1600 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602
1603 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001604 * At this point we only know it's not a PEM formatted key. Could be any
1605 * of the known DER encoded private key formats
1606 *
1607 * We try the different DER format parsers to see if one passes without
1608 * error
1609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001612 unsigned char *key_copy;
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1615 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1616 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001619
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001620 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1621 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001622
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001623 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624 }
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (ret == 0) {
1627 return 0;
1628 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 mbedtls_pk_free(pk);
1631 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1634 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1639 if (ret == 0) {
1640 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001641 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_pk_free(pk);
1644 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1649 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1650 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1651 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652 }
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 mbedtls_pk_free(pk);
1655 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657
Valerio Setti81d75122023-06-14 14:49:33 +02001658#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1660 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001661 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 key, keylen, f_rng, p_rng) == 0) {
1663 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001666#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001667
Valerio Setti81d75122023-06-14 14:49:33 +02001668 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001669 * it is ok to leave the PK context initialized but not
1670 * freed: It is the caller's responsibility to call pk_init()
1671 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001672 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001673 * isn't, this leads to mbedtls_pk_free() being called
1674 * twice, once here and once by the caller, but this is
1675 * also ok and in line with the mbedtls_pk_free() calls
1676 * on failed PEM parsing attempts. */
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679}
1680
1681/*
1682 * Parse a public key
1683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1685 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686{
Janos Follath24eed8d2019-11-22 13:21:35 +00001687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001689#if defined(MBEDTLS_RSA_C)
1690 const mbedtls_pk_info_t *pk_info;
1691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001695#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if (keylen == 0) {
1698 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1699 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001700
1701#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001703#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001704 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001706 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 } else {
1708 ret = mbedtls_pem_read_buffer(&pem,
1709 "-----BEGIN RSA PUBLIC KEY-----",
1710 "-----END RSA PUBLIC KEY-----",
1711 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001713
1714 if (ret == 0) {
1715 p = pem.buf;
1716 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1717 mbedtls_pem_free(&pem);
1718 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1719 }
1720
1721 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1722 mbedtls_pem_free(&pem);
1723 return ret;
1724 }
1725
1726 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1727 mbedtls_pk_free(ctx);
1728 }
1729
1730 mbedtls_pem_free(&pem);
1731 return ret;
1732 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1733 mbedtls_pem_free(&pem);
1734 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001735 }
1736#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001737
1738 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001740 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 } else {
1742 ret = mbedtls_pem_read_buffer(&pem,
1743 "-----BEGIN PUBLIC KEY-----",
1744 "-----END PUBLIC KEY-----",
1745 key, NULL, 0, &len);
1746 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001749 /*
1750 * Was PEM encoded
1751 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001752 p = pem.buf;
1753
Jethro Beekman01672442023-04-19 14:08:14 +02001754 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 mbedtls_pem_free(&pem);
1756 return ret;
1757 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1758 mbedtls_pem_free(&pem);
1759 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001760 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001763
1764#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1766 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001768
1769 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1770 return ret;
1771 }
1772
1773 p = (unsigned char *) key;
1774 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1775 if (ret == 0) {
1776 return ret;
1777 }
1778 mbedtls_pk_free(ctx);
1779 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1780 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1781 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001782 }
1783#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001784 p = (unsigned char *) key;
1785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001789}
1790
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001791/***********************************************************************
1792 *
1793 * Top-level functions, with filesystem support
1794 *
1795 **********************************************************************/
1796
1797#if defined(MBEDTLS_FS_IO)
1798/*
1799 * Load all data from a file into a given buffer.
1800 *
1801 * The file is expected to contain either PEM or DER encoded data.
1802 * A terminating null byte is always appended. It is included in the announced
1803 * length only if the data looks like it is PEM encoded.
1804 */
1805int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1806{
1807 FILE *f;
1808 long size;
1809
1810 if ((f = fopen(path, "rb")) == NULL) {
1811 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1812 }
1813
1814 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1815 mbedtls_setbuf(f, NULL);
1816
1817 fseek(f, 0, SEEK_END);
1818 if ((size = ftell(f)) == -1) {
1819 fclose(f);
1820 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1821 }
1822 fseek(f, 0, SEEK_SET);
1823
1824 *n = (size_t) size;
1825
1826 if (*n + 1 == 0 ||
1827 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1828 fclose(f);
1829 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1830 }
1831
1832 if (fread(*buf, 1, *n, f) != *n) {
1833 fclose(f);
1834
1835 mbedtls_zeroize_and_free(*buf, *n);
1836
1837 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1838 }
1839
1840 fclose(f);
1841
1842 (*buf)[*n] = '\0';
1843
1844 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1845 ++*n;
1846 }
1847
1848 return 0;
1849}
1850
1851/*
1852 * Load and parse a private key
1853 */
1854int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1855 const char *path, const char *pwd,
1856 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1857{
1858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1859 size_t n;
1860 unsigned char *buf;
1861
1862 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1863 return ret;
1864 }
1865
1866 if (pwd == NULL) {
1867 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1868 } else {
1869 ret = mbedtls_pk_parse_key(ctx, buf, n,
1870 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1871 }
1872
1873 mbedtls_zeroize_and_free(buf, n);
1874
1875 return ret;
1876}
1877
1878/*
1879 * Load and parse a public key
1880 */
1881int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1882{
1883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1884 size_t n;
1885 unsigned char *buf;
1886
1887 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1888 return ret;
1889 }
1890
1891 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1892
1893 mbedtls_zeroize_and_free(buf, n);
1894
1895 return ret;
1896}
1897#endif /* MBEDTLS_FS_IO */
1898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_PK_PARSE_C */