blob: e8678ed34874f819af3dc2b740c6712bc27b5b89 [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{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200287#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200288
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200289 /* Load the key */
290 if (*pub == 0x04) {
291 /* Uncompressed format, directly supported by PSA */
292 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200293 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
294 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200295 memcpy(pk->pub_raw, pub, pub_len);
296 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200297 } else {
298 /* Other format, try the fallback */
299 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
300 if (ret != 0) {
301 return ret;
302 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200304
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200305 /* Validate the key by trying to import it */
306 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
307 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
308
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200309 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200310 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
311 psa_set_key_bits(&key_attrs, pk->ec_bits);
312
313 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200314 &key_id) != PSA_SUCCESS) ||
315 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
316 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200318
319 return 0;
320
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200321#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200322
323 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200324 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200325 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
326 if (ret != 0) {
327 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200329 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
330
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200331#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200332}
333
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200334/***********************************************************************
335 *
336 * Unsorted (yet!) from this point on until the next section header
337 *
338 **********************************************************************/
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200341/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
343 * WARNING: the resulting group should only be used with
344 * pk_group_id_from_specified(), since its base point may not be set correctly
345 * if it was encoded compressed.
346 *
347 * SpecifiedECDomain ::= SEQUENCE {
348 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
349 * fieldID FieldID {{FieldTypes}},
350 * curve Curve,
351 * base ECPoint,
352 * order INTEGER,
353 * cofactor INTEGER OPTIONAL,
354 * hash HashAlgorithm OPTIONAL,
355 * ...
356 * }
357 *
358 * We only support prime-field as field type, and ignore hash and cofactor.
359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361{
Janos Follath24eed8d2019-11-22 13:21:35 +0000362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100363 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200364 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365 const unsigned char *end_field, *end_curve;
366 size_t len;
367 int ver;
368
369 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
371 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
372 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (ver < 1 || ver > 3) {
375 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
376 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
378 /*
379 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
380 * fieldType FIELD-ID.&id({IOSet}),
381 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
382 * }
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
385 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
386 return ret;
387 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388
389 end_field = p + len;
390
391 /*
392 * FIELD-ID ::= TYPE-IDENTIFIER
393 * FieldTypes FIELD-ID ::= {
394 * { Prime-p IDENTIFIED BY prime-field } |
395 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
396 * }
397 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
398 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
400 return ret;
401 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
404 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
405 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100406 }
407
408 p += len;
409
410 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
413 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (p != end_field) {
418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
419 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
420 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
422 /*
423 * Curve ::= SEQUENCE {
424 * a FieldElement,
425 * b FieldElement,
426 * seed BIT STRING OPTIONAL
427 * -- Shall be present if used in SpecifiedECDomain
428 * -- with version equal to ecdpVer2 or ecdpVer3
429 * }
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
432 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
433 return ret;
434 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
436 end_curve = p + len;
437
438 /*
439 * FieldElement ::= OCTET STRING
440 * containing an integer in the case of a prime field
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
443 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
444 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100445 }
446
447 p += len;
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
450 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
451 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452 }
453
454 p += len;
455
456 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (p != end_curve) {
462 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
463 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
464 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465
466 /*
467 * ECPoint ::= OCTET STRING
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
470 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
471 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
474 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100475 /*
476 * If we can't read the point because it's compressed, cheat by
477 * reading only the X coordinate and the parity bit of Y.
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
480 (p[0] != 0x02 && p[0] != 0x03) ||
481 len != mbedtls_mpi_size(&grp->P) + 1 ||
482 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
483 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
484 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
485 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100486 }
487 }
488
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100489 p += len;
490
491 /*
492 * order INTEGER
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
495 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
496 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100499
500 /*
501 * Allow optional elements by purposefully not enforcing p == end here.
502 */
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100505}
506
507/*
508 * Find the group id associated with an (almost filled) group as generated by
509 * pk_group_from_specified(), or return an error if unknown.
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511static 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 +0100512{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100513 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_ecp_group ref;
515 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100520 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_ecp_group_free(&ref);
522 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100523
524 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
526 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
527 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
528 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
529 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
530 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
531 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100532 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 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 +0100534 break;
535 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100536 }
537
538cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100540
541 *grp_id = *id;
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548}
549
550/*
551 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
552 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
554 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100555{
Janos Follath24eed8d2019-11-22 13:21:35 +0000556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100562 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100566
567cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000568 /* The API respecting lifecycle for mbedtls_ecp_group struct is
569 * _init(), _load() and _free(). In pk_group_id_from_specified() the
570 * temporary grp breaks that flow and it's members are populated
571 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
572 * which is assuming a group populated by _setup() may not clean-up
573 * properly -> Manually free it's members.
574 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000575 mbedtls_mpi_free(&grp.N);
576 mbedtls_mpi_free(&grp.P);
577 mbedtls_mpi_free(&grp.A);
578 mbedtls_mpi_free(&grp.B);
579 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100584
Valerio Setti4064dbb2023-05-17 15:33:07 +0200585
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200586/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200587 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200588 * ECParameters ::= CHOICE {
589 * namedCurve OBJECT IDENTIFIER
590 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
591 * -- implicitCurve NULL
592 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200593 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200594static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
595 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200596{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200598
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200599 if (end - *p < 1) {
600 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
601 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200602 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200603
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200604 /* Tag may be either OID or SEQUENCE */
605 params->tag = **p;
606 if (params->tag != MBEDTLS_ASN1_OID
607#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
608 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
609#endif
610 ) {
611 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
612 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
613 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200614
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200615 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200616 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
617 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200618
619 params->p = *p;
620 *p += params->len;
621
622 if (*p != end) {
623 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
624 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
625 }
626
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200627 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200628}
629
630/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100632 *
633 * ECParameters ::= CHOICE {
634 * namedCurve OBJECT IDENTIFIER
635 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
636 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200638static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639{
Janos Follath24eed8d2019-11-22 13:21:35 +0000640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (params->tag == MBEDTLS_ASN1_OID) {
644 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
645 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
646 }
647 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
650 return ret;
651 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100652#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100654#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100655 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200657 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658}
659
Jethro Beekman01672442023-04-19 14:08:14 +0200660#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
661
662/*
663 * Load an RFC8410 EC key, which doesn't have any parameters
664 */
665static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
666 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200667 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200668{
669 if (params->tag != 0 || params->len != 0) {
670 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
671 }
672
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200673 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200674}
675
676/*
677 * Parse an RFC 8410 encoded private EC key
678 *
679 * CurvePrivateKey ::= OCTET STRING
680 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200681static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200682 unsigned char *key, size_t keylen, const unsigned char *end,
683 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
684{
685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
686 size_t len;
687
688 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
689 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
690 }
691
692 if (key + len != end) {
693 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
694 }
695
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200696 /*
697 * Load the private key
698 */
699 ret = pk_ecc_set_key(pk, key, len);
700 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200701 return ret;
702 }
Jethro Beekman01672442023-04-19 14:08:14 +0200703
Valerio Setti4064dbb2023-05-17 15:33:07 +0200704 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
705 * which never contain a public key. As such, derive the public key
706 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200707 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200708 return ret;
709 }
710
Jethro Beekman01672442023-04-19 14:08:14 +0200711 return 0;
712}
713#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200714
Valerio Setti81d75122023-06-14 14:49:33 +0200715#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718/*
719 * RSAPublicKey ::= SEQUENCE {
720 * modulus INTEGER, -- n
721 * publicExponent INTEGER -- e
722 * }
723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724static int pk_get_rsapubkey(unsigned char **p,
725 const unsigned char *end,
726 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727{
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729 size_t len;
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
732 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
733 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
734 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (*p + len != end) {
737 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
738 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
739 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100741 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
743 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
744 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
747 NULL, 0, NULL, 0)) != 0) {
748 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
749 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100750
751 *p += len;
752
753 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
755 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
756 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
759 NULL, 0, *p, len)) != 0) {
760 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
761 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762
763 *p += len;
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (mbedtls_rsa_complete(rsa) != 0 ||
766 mbedtls_rsa_check_pubkey(rsa) != 0) {
767 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000768 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (*p != end) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
772 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
773 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778
779/* Get a PK algorithm identifier
780 *
781 * AlgorithmIdentifier ::= SEQUENCE {
782 * algorithm OBJECT IDENTIFIER,
783 * parameters ANY DEFINED BY algorithm OPTIONAL }
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785static int pk_get_pk_alg(unsigned char **p,
786 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200787 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
788 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789{
Janos Follath24eed8d2019-11-22 13:21:35 +0000790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
796 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
797 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200798
Jethro Beekman01672442023-04-19 14:08:14 +0200799 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200800#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200801 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
802 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
803 if (ret == 0) {
804 *pk_alg = MBEDTLS_PK_ECKEY;
805 }
806 }
807#else
808 (void) ec_grp_id;
809#endif
810 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
812 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813
814 /*
815 * No parameters with RSA (only for EC)
816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (*pk_alg == MBEDTLS_PK_RSA &&
818 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
819 params->len != 0)) {
820 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824}
825
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200826/* Helper for Montgomery curves */
827#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
828#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
829 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
830#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
831
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832/*
833 * SubjectPublicKeyInfo ::= SEQUENCE {
834 * algorithm AlgorithmIdentifier,
835 * subjectPublicKey BIT STRING }
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
838 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839{
Janos Follath24eed8d2019-11-22 13:21:35 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_asn1_buf alg_params;
843 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200844 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
848 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
849 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 }
851
852 end = *p + len;
853
Jethro Beekman01672442023-04-19 14:08:14 +0200854 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return ret;
856 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
859 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
860 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (*p + len != end) {
863 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
865 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
868 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
869 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
872 return ret;
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (pk_alg == MBEDTLS_PK_RSA) {
877 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200880#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200882#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200883 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200884 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200885 } else
886#endif
887 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200888 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200889 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200891 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
892 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200895#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (ret == 0 && *p != end) {
899 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
900 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
901 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (ret != 0) {
904 mbedtls_pk_free(pk);
905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908}
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100912 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
913 *
914 * The value zero is:
915 * - never a valid value for an RSA parameter
916 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
917 *
918 * Since values can't be omitted in PKCS#1, passing a zero value to
919 * rsa_complete() would be incorrect, so reject zero values early.
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921static int asn1_get_nonzero_mpi(unsigned char **p,
922 const unsigned char *end,
923 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100924{
925 int ret;
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 ret = mbedtls_asn1_get_mpi(p, end, X);
928 if (ret != 0) {
929 return ret;
930 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
933 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
934 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100937}
938
939/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 * Parse a PKCS#1 encoded private RSA key
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
943 const unsigned char *key,
944 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100946 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947 size_t len;
948 unsigned char *p, *end;
949
Hanno Beckerefa14e82017-10-11 19:45:19 +0100950 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100952
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953 p = (unsigned char *) key;
954 end = p + keylen;
955
956 /*
957 * This function parses the RSAPrivateKey (PKCS#1)
958 *
959 * RSAPrivateKey ::= SEQUENCE {
960 * version Version,
961 * modulus INTEGER, -- n
962 * publicExponent INTEGER, -- e
963 * privateExponent INTEGER, -- d
964 * prime1 INTEGER, -- p
965 * prime2 INTEGER, -- q
966 * exponent1 INTEGER, -- d mod (p-1)
967 * exponent2 INTEGER, -- d mod (q-1)
968 * coefficient INTEGER, -- (inverse of q) mod p
969 * otherPrimeInfos OtherPrimeInfos OPTIONAL
970 * }
971 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
973 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
974 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975 }
976
977 end = p + len;
978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
980 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 }
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (version != 0) {
984 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985 }
986
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
989 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
990 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100991 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100994 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
996 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
997 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100998 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001000
1001 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1003 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1004 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001005 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001007
1008 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1010 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1011 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001012 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001014
1015 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1017 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1018 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001019 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001021
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001022#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001023 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1025 * that they can be easily recomputed from D, P and Q. However by
1026 * parsing them from the PKCS1 structure it is possible to avoid
1027 * recalculating them which both reduces the overhead of loading
1028 * RSA private keys into memory and also avoids side channels which
1029 * can arise when computing those values, since all of D, P, and Q
1030 * are secret. See https://eprint.iacr.org/2020/055 for a
1031 * description of one such attack.
1032 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001033
Jack Lloyd80cc8112020-01-22 17:34:29 -05001034 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1036 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1037 goto cleanup;
1038 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001039
1040 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1042 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1043 goto cleanup;
1044 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001045
1046 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1048 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1049 goto cleanup;
1050 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001051
Jack Lloyd60239752020-01-27 17:53:36 -05001052#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001053 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1056 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1057 goto cleanup;
1058 }
Jack Lloyd60239752020-01-27 17:53:36 -05001059#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001060
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001061 /* rsa_complete() doesn't complete anything with the default
1062 * implementation but is still called:
1063 * - for the benefit of alternative implementation that may want to
1064 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1065 * - as is also sanity-checks the key
1066 *
1067 * Furthermore, we also check the public part for consistency with
1068 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1071 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001072 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001073 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (p != end) {
1076 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1077 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 }
1079
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001080cleanup:
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001085 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret & 0xff80) == 0) {
1087 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1088 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001089 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 }
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098
Valerio Setti81d75122023-06-14 14:49:33 +02001099#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100/*
1101 * Parse a SEC1 encoded private EC key
1102 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001103static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 const unsigned char *key, size_t keylen,
1105 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106{
Janos Follath24eed8d2019-11-22 13:21:35 +00001107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001108 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001109 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001110 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001112 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 unsigned char *end = p + keylen;
1114 unsigned char *end2;
1115
1116 /*
1117 * RFC 5915, or SEC1 Appendix C.4
1118 *
1119 * ECPrivateKey ::= SEQUENCE {
1120 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1121 * privateKey OCTET STRING,
1122 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1123 * publicKey [1] BIT STRING OPTIONAL
1124 * }
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1128 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 }
1130
1131 end = p + len;
1132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1135 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (version != 1) {
1138 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1139 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1142 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1143 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144
Valerio Setti6b062ee2023-06-30 17:32:57 +02001145 /* Keep a reference to the position fo the private key. It will be used
1146 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001147 d = p;
1148 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001149
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 p += len;
1151
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001152 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001154 /*
1155 * Is 'parameters' present?
1156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1158 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1159 0)) == 0) {
1160 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001161 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001163 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001166 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001167 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001168
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001169 /*
1170 * Load the private key
1171 */
1172 ret = pk_ecc_set_key(pk, d, d_len);
1173 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001174 return ret;
1175 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001178 /*
1179 * Is 'publickey' present? If not, or if we can't read it (eg because it
1180 * is compressed), create it from the private key.
1181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1183 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1184 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001185 end2 = p + len;
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1189 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (p + len != end2) {
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1193 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1194 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001195
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001196 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001199 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001200 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201 * is if the point format is not recognized.
1202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1204 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1205 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001211
Valerio Setti34f67552023-04-03 15:19:18 +02001212 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001213 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001214 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001215 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001216 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219}
Valerio Setti81d75122023-06-14 14:49:33 +02001220#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001222/***********************************************************************
1223 *
1224 * PKCS#8 parsing functions
1225 *
1226 **********************************************************************/
1227
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228/*
1229 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001230 *
1231 * Notes:
1232 *
1233 * - This function does not own the key buffer. It is the
1234 * responsibility of the caller to take care of zeroizing
1235 * and freeing it after use.
1236 *
1237 * - The function is responsible for freeing the provided
1238 * PK context on failure.
1239 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 */
1241static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_pk_context *pk,
1243 const unsigned char *key, size_t keylen,
1244 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245{
1246 int ret, version;
1247 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 unsigned char *p = (unsigned char *) key;
1250 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001252 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Valerio Setti81d75122023-06-14 14:49:33 +02001255#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001256 (void) f_rng;
1257 (void) p_rng;
1258#endif
1259
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001261 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 *
1263 * PrivateKeyInfo ::= SEQUENCE {
1264 * version Version,
1265 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1266 * privateKey PrivateKey,
1267 * attributes [0] IMPLICIT Attributes OPTIONAL }
1268 *
1269 * Version ::= INTEGER
1270 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1271 * PrivateKey ::= OCTET STRING
1272 *
1273 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1274 */
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1277 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1278 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 }
1280
1281 end = p + len;
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001285 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (version != 0) {
1288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1289 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
Jethro Beekman01672442023-04-19 14:08:14 +02001291 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 return ret;
1293 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1297 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (len < 1) {
1300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1301 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1302 }
1303
1304 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1305 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1306 }
1307
1308 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1309 return ret;
1310 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (pk_alg == MBEDTLS_PK_RSA) {
1314 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1315 mbedtls_pk_free(pk);
1316 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 }
1318 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001320#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001322#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001323 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001324 if ((ret =
1325 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001326 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001327 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001328 p_rng)) != 0) {
1329 mbedtls_pk_free(pk);
1330 return ret;
1331 }
1332 } else
1333#endif
1334 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001335 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1336 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001337 mbedtls_pk_free(pk);
1338 return ret;
1339 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 }
1341 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001342#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001345 end = p + len;
1346 if (end != (key + keylen)) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1348 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1349 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352}
1353
1354/*
1355 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001356 *
1357 * To save space, the decryption happens in-place on the given key buffer.
1358 * Also, while this function may modify the keybuffer, it doesn't own it,
1359 * and instead it is the responsibility of the caller to zeroize and properly
1360 * free it after use.
1361 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001364MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 mbedtls_pk_context *pk,
1366 unsigned char *key, size_t keylen,
1367 const unsigned char *pwd, size_t pwdlen,
1368 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001370 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001372 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1375#if defined(MBEDTLS_PKCS12_C)
1376 mbedtls_cipher_type_t cipher_alg;
1377 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001379 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380
Hanno Becker2aa80a72017-09-07 15:28:45 +01001381 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382 end = p + keylen;
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (pwdlen == 0) {
1385 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1386 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
1388 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001389 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390 *
1391 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1392 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1393 * encryptedData EncryptedData
1394 * }
1395 *
1396 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1397 *
1398 * EncryptedData ::= OCTET STRING
1399 *
1400 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001401 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1404 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1405 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 }
1407
1408 end = p + len;
1409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1412 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1416 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417
Hanno Beckerfab35692017-08-25 13:38:26 +01001418 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
1420 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001421 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001425 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001426 cipher_alg, md_alg,
1427 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1429 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1430 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001434
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001435 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_PKCS12_C */
1438#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001440 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1441 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1443 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1444 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001448
1449 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001452 {
1453 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001454 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (decrypted == 0) {
1457 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1458 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001459 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001463/***********************************************************************
1464 *
1465 * Top-level functions, with format auto-discovery
1466 *
1467 **********************************************************************/
1468
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469/*
1470 * Parse a private key
1471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1473 const unsigned char *key, size_t keylen,
1474 const unsigned char *pwd, size_t pwdlen,
1475 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476{
Janos Follath24eed8d2019-11-22 13:21:35 +00001477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001482#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (keylen == 0) {
1485 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1486 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001487
1488#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001492 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001494 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 } else {
1496 ret = mbedtls_pem_read_buffer(&pem,
1497 "-----BEGIN RSA PRIVATE KEY-----",
1498 "-----END RSA PRIVATE KEY-----",
1499 key, pwd, pwdlen, &len);
1500 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (ret == 0) {
1503 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1504 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1505 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1506 pem.buf, pem.buflen)) != 0) {
1507 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508 }
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 mbedtls_pem_free(&pem);
1511 return ret;
1512 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1513 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1514 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1515 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1516 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1517 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Valerio Setti81d75122023-06-14 14:49:33 +02001521#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001522 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001524 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 } else {
1526 ret = mbedtls_pem_read_buffer(&pem,
1527 "-----BEGIN EC PRIVATE KEY-----",
1528 "-----END EC PRIVATE KEY-----",
1529 key, pwd, pwdlen, &len);
1530 }
1531 if (ret == 0) {
1532 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001535 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 pem.buf, pem.buflen,
1537 f_rng, p_rng)) != 0) {
1538 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539 }
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_pem_free(&pem);
1542 return ret;
1543 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1544 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1545 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1546 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1547 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1548 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549 }
Valerio Setti81d75122023-06-14 14:49:33 +02001550#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001552 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001554 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 } else {
1556 ret = mbedtls_pem_read_buffer(&pem,
1557 "-----BEGIN PRIVATE KEY-----",
1558 "-----END PRIVATE KEY-----",
1559 key, NULL, 0, &len);
1560 }
1561 if (ret == 0) {
1562 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1563 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1564 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565 }
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 mbedtls_pem_free(&pem);
1568 return ret;
1569 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1570 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001574 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001576 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 } else {
1578 ret = mbedtls_pem_read_buffer(&pem,
1579 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1580 "-----END ENCRYPTED PRIVATE KEY-----",
1581 key, NULL, 0, &len);
1582 }
1583 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001584 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1585 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587 }
1588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 mbedtls_pem_free(&pem);
1590 return ret;
1591 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1592 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595#else
1596 ((void) pwd);
1597 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001599
1600 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001601 * At this point we only know it's not a PEM formatted key. Could be any
1602 * of the known DER encoded private key formats
1603 *
1604 * We try the different DER format parsers to see if one passes without
1605 * error
1606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001609 unsigned char *key_copy;
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1612 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1613 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001616
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001617 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1618 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001619
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001620 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621 }
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ret == 0) {
1624 return 0;
1625 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 mbedtls_pk_free(pk);
1628 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1631 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1636 if (ret == 0) {
1637 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001638 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_pk_free(pk);
1641 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1646 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1647 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1648 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001649 }
1650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 mbedtls_pk_free(pk);
1652 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654
Valerio Setti81d75122023-06-14 14:49:33 +02001655#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1657 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001658 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 key, keylen, f_rng, p_rng) == 0) {
1660 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001663#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664
Valerio Setti81d75122023-06-14 14:49:33 +02001665 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001666 * it is ok to leave the PK context initialized but not
1667 * freed: It is the caller's responsibility to call pk_init()
1668 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001669 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001670 * isn't, this leads to mbedtls_pk_free() being called
1671 * twice, once here and once by the caller, but this is
1672 * also ok and in line with the mbedtls_pk_free() calls
1673 * on failed PEM parsing attempts. */
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676}
1677
1678/*
1679 * Parse a public key
1680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1682 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001683{
Janos Follath24eed8d2019-11-22 13:21:35 +00001684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001686#if defined(MBEDTLS_RSA_C)
1687 const mbedtls_pk_info_t *pk_info;
1688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001692#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (keylen == 0) {
1695 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1696 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001697
1698#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001700#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001701 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001703 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 } else {
1705 ret = mbedtls_pem_read_buffer(&pem,
1706 "-----BEGIN RSA PUBLIC KEY-----",
1707 "-----END RSA PUBLIC KEY-----",
1708 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001709 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001710
1711 if (ret == 0) {
1712 p = pem.buf;
1713 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1714 mbedtls_pem_free(&pem);
1715 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1716 }
1717
1718 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1719 mbedtls_pem_free(&pem);
1720 return ret;
1721 }
1722
1723 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1724 mbedtls_pk_free(ctx);
1725 }
1726
1727 mbedtls_pem_free(&pem);
1728 return ret;
1729 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1730 mbedtls_pem_free(&pem);
1731 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001732 }
1733#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001734
1735 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001737 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 } else {
1739 ret = mbedtls_pem_read_buffer(&pem,
1740 "-----BEGIN PUBLIC KEY-----",
1741 "-----END PUBLIC KEY-----",
1742 key, NULL, 0, &len);
1743 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746 /*
1747 * Was PEM encoded
1748 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001749 p = pem.buf;
1750
Jethro Beekman01672442023-04-19 14:08:14 +02001751 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_pem_free(&pem);
1753 return ret;
1754 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1755 mbedtls_pem_free(&pem);
1756 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001760
1761#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1763 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001765
1766 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1767 return ret;
1768 }
1769
1770 p = (unsigned char *) key;
1771 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1772 if (ret == 0) {
1773 return ret;
1774 }
1775 mbedtls_pk_free(ctx);
1776 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1777 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1778 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001779 }
1780#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001781 p = (unsigned char *) key;
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001786}
1787
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001788/***********************************************************************
1789 *
1790 * Top-level functions, with filesystem support
1791 *
1792 **********************************************************************/
1793
1794#if defined(MBEDTLS_FS_IO)
1795/*
1796 * Load all data from a file into a given buffer.
1797 *
1798 * The file is expected to contain either PEM or DER encoded data.
1799 * A terminating null byte is always appended. It is included in the announced
1800 * length only if the data looks like it is PEM encoded.
1801 */
1802int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1803{
1804 FILE *f;
1805 long size;
1806
1807 if ((f = fopen(path, "rb")) == NULL) {
1808 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1809 }
1810
1811 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1812 mbedtls_setbuf(f, NULL);
1813
1814 fseek(f, 0, SEEK_END);
1815 if ((size = ftell(f)) == -1) {
1816 fclose(f);
1817 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1818 }
1819 fseek(f, 0, SEEK_SET);
1820
1821 *n = (size_t) size;
1822
1823 if (*n + 1 == 0 ||
1824 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1825 fclose(f);
1826 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1827 }
1828
1829 if (fread(*buf, 1, *n, f) != *n) {
1830 fclose(f);
1831
1832 mbedtls_zeroize_and_free(*buf, *n);
1833
1834 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1835 }
1836
1837 fclose(f);
1838
1839 (*buf)[*n] = '\0';
1840
1841 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1842 ++*n;
1843 }
1844
1845 return 0;
1846}
1847
1848/*
1849 * Load and parse a private key
1850 */
1851int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1852 const char *path, const char *pwd,
1853 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1854{
1855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1856 size_t n;
1857 unsigned char *buf;
1858
1859 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1860 return ret;
1861 }
1862
1863 if (pwd == NULL) {
1864 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1865 } else {
1866 ret = mbedtls_pk_parse_key(ctx, buf, n,
1867 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1868 }
1869
1870 mbedtls_zeroize_and_free(buf, n);
1871
1872 return ret;
1873}
1874
1875/*
1876 * Load and parse a public key
1877 */
1878int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1879{
1880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1881 size_t n;
1882 unsigned char *buf;
1883
1884 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1885 return ret;
1886 }
1887
1888 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1889
1890 mbedtls_zeroize_and_free(buf, n);
1891
1892 return ret;
1893}
1894#endif /* MBEDTLS_FS_IO */
1895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#endif /* MBEDTLS_PK_PARSE_C */