blob: 826412a54edd874a9bf85c4f4b894e68822a90c9 [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/*
229 * Create a temporary ecp_keypair for converting an EC point in compressed
230 * format to an uncompressed one
231 *
232 * Consumes everything or fails - inherited from
233 * mbedtls_ecp_point_read_binary().
234 */
235static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
236 const unsigned char *in_start, size_t in_len,
237 unsigned char *out_buf, size_t out_buf_size,
238 size_t *out_buf_len)
239{
240#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
241 mbedtls_ecp_keypair ecp_key;
242 mbedtls_ecp_group_id ecp_group_id;
243 int ret;
244
245 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
246
247 mbedtls_ecp_keypair_init(&ecp_key);
248 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
249 if (ret != 0) {
250 return ret;
251 }
252 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
253 in_start, in_len);
254 if (ret != 0) {
255 goto exit;
256 }
257 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
258 MBEDTLS_ECP_PF_UNCOMPRESSED,
259 out_buf_len, out_buf, out_buf_size);
260
261exit:
262 mbedtls_ecp_keypair_free(&ecp_key);
263 return ret;
264#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
265 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
266#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
267}
268#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
269
270/*
271 * EC public key is an EC point
272 *
273 * The caller is responsible for clearing the structure upon failure if
274 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
275 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
276 */
277static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
278 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200279{
Janos Follath24eed8d2019-11-22 13:21:35 +0000280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200281
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200282#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
283 mbedtls_svc_key_id_t key;
284 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
285 size_t len = (size_t) (end - *p);
286
287 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
288 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100290
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200291 if ((**p == 0x02) || (**p == 0x03)) {
292 /* Compressed format, not supported by PSA Crypto.
293 * Try converting using functions from ECP_LIGHT. */
294 ret = pk_ecc_read_compressed(pk, *p, len,
295 pk->pub_raw,
296 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
297 &pk->pub_raw_len);
298 if (ret != 0) {
299 return ret;
300 }
301 } else {
302 /* Uncompressed format */
303 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
304 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
305 }
306 memcpy(pk->pub_raw, *p, len);
307 pk->pub_raw_len = len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200309
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200310 /* Validate the key by trying to importing it */
311 psa_set_key_usage_flags(&key_attrs, 0);
312 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
313 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
314 psa_set_key_bits(&key_attrs, pk->ec_bits);
315
316 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
317 &key) != PSA_SUCCESS) ||
318 (psa_destroy_key(key) != PSA_SUCCESS)) {
319 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
320 pk->pub_raw_len = 0;
321 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200323 ret = 0;
324#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
325 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
326 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
327 (const unsigned char *) *p,
328 end - *p)) == 0) {
329 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
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
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200333 /*
334 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
335 * consumed all bytes or failed, and memcpy consumed all bytes too.
336 */
337 *p = (unsigned char *) end;
338
339 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200340}
341
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200342/***********************************************************************
343 *
344 * Unsorted (yet!) from this point on until the next section header
345 *
346 **********************************************************************/
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200349/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
351 * WARNING: the resulting group should only be used with
352 * pk_group_id_from_specified(), since its base point may not be set correctly
353 * if it was encoded compressed.
354 *
355 * SpecifiedECDomain ::= SEQUENCE {
356 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
357 * fieldID FieldID {{FieldTypes}},
358 * curve Curve,
359 * base ECPoint,
360 * order INTEGER,
361 * cofactor INTEGER OPTIONAL,
362 * hash HashAlgorithm OPTIONAL,
363 * ...
364 * }
365 *
366 * We only support prime-field as field type, and ignore hash and cofactor.
367 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100369{
Janos Follath24eed8d2019-11-22 13:21:35 +0000370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200372 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373 const unsigned char *end_field, *end_curve;
374 size_t len;
375 int ver;
376
377 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
380 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (ver < 1 || ver > 3) {
383 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
384 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385
386 /*
387 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
388 * fieldType FIELD-ID.&id({IOSet}),
389 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
390 * }
391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
393 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
394 return ret;
395 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396
397 end_field = p + len;
398
399 /*
400 * FIELD-ID ::= TYPE-IDENTIFIER
401 * FieldTypes FIELD-ID ::= {
402 * { Prime-p IDENTIFIED BY prime-field } |
403 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
404 * }
405 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
408 return ret;
409 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
412 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
413 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 }
415
416 p += len;
417
418 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
421 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (p != end_field) {
426 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
428 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430 /*
431 * Curve ::= SEQUENCE {
432 * a FieldElement,
433 * b FieldElement,
434 * seed BIT STRING OPTIONAL
435 * -- Shall be present if used in SpecifiedECDomain
436 * -- with version equal to ecdpVer2 or ecdpVer3
437 * }
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
441 return ret;
442 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100443
444 end_curve = p + len;
445
446 /*
447 * FieldElement ::= OCTET STRING
448 * containing an integer in the case of a prime field
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
451 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453 }
454
455 p += len;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
458 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
459 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460 }
461
462 p += len;
463
464 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (p != end_curve) {
470 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
471 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
472 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100473
474 /*
475 * ECPoint ::= OCTET STRING
476 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
478 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
479 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
482 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100483 /*
484 * If we can't read the point because it's compressed, cheat by
485 * reading only the X coordinate and the parity bit of Y.
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
488 (p[0] != 0x02 && p[0] != 0x03) ||
489 len != mbedtls_mpi_size(&grp->P) + 1 ||
490 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
491 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
492 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
493 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100494 }
495 }
496
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497 p += len;
498
499 /*
500 * order INTEGER
501 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
503 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
504 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100507
508 /*
509 * Allow optional elements by purposefully not enforcing p == end here.
510 */
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100513}
514
515/*
516 * Find the group id associated with an (almost filled) group as generated by
517 * pk_group_from_specified(), or return an error if unknown.
518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519static 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 +0100520{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100521 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ecp_group ref;
523 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100528 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_ecp_group_free(&ref);
530 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100531
532 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
534 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
535 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
536 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
537 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
538 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
539 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100540 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 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 +0100542 break;
543 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100544 }
545
546cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548
549 *grp_id = *id;
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100556}
557
558/*
559 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
562 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100563{
Janos Follath24eed8d2019-11-22 13:21:35 +0000564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100570 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100574
575cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000576 /* The API respecting lifecycle for mbedtls_ecp_group struct is
577 * _init(), _load() and _free(). In pk_group_id_from_specified() the
578 * temporary grp breaks that flow and it's members are populated
579 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
580 * which is assuming a group populated by _setup() may not clean-up
581 * properly -> Manually free it's members.
582 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000583 mbedtls_mpi_free(&grp.N);
584 mbedtls_mpi_free(&grp.P);
585 mbedtls_mpi_free(&grp.A);
586 mbedtls_mpi_free(&grp.B);
587 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100592
Valerio Setti4064dbb2023-05-17 15:33:07 +0200593
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200594/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200595 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200596 * ECParameters ::= CHOICE {
597 * namedCurve OBJECT IDENTIFIER
598 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
599 * -- implicitCurve NULL
600 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200601 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200602static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
603 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200604{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200606
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200607 if (end - *p < 1) {
608 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
609 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200610 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200611
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200612 /* Tag may be either OID or SEQUENCE */
613 params->tag = **p;
614 if (params->tag != MBEDTLS_ASN1_OID
615#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
616 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
617#endif
618 ) {
619 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
620 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
621 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200622
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200623 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200624 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
625 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200626
627 params->p = *p;
628 *p += params->len;
629
630 if (*p != end) {
631 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
632 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
633 }
634
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200635 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200636}
637
638/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100640 *
641 * ECParameters ::= CHOICE {
642 * namedCurve OBJECT IDENTIFIER
643 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
644 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200646static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647{
Janos Follath24eed8d2019-11-22 13:21:35 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (params->tag == MBEDTLS_ASN1_OID) {
652 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
653 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
654 }
655 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
658 return ret;
659 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100660#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100662#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100663 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200665 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666}
667
Jethro Beekman01672442023-04-19 14:08:14 +0200668#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
669
670/*
671 * Load an RFC8410 EC key, which doesn't have any parameters
672 */
673static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
674 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200675 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200676{
677 if (params->tag != 0 || params->len != 0) {
678 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
679 }
680
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200681 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200682}
683
684/*
685 * Parse an RFC 8410 encoded private EC key
686 *
687 * CurvePrivateKey ::= OCTET STRING
688 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200689static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200690 unsigned char *key, size_t keylen, const unsigned char *end,
691 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
692{
693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
694 size_t len;
695
696 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
697 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
698 }
699
700 if (key + len != end) {
701 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
702 }
703
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200704 /*
705 * Load the private key
706 */
707 ret = pk_ecc_set_key(pk, key, len);
708 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200709 return ret;
710 }
Jethro Beekman01672442023-04-19 14:08:14 +0200711
Valerio Setti4064dbb2023-05-17 15:33:07 +0200712 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
713 * which never contain a public key. As such, derive the public key
714 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200715 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200716 return ret;
717 }
718
Jethro Beekman01672442023-04-19 14:08:14 +0200719 return 0;
720}
721#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200722
Valerio Setti81d75122023-06-14 14:49:33 +0200723#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726/*
727 * RSAPublicKey ::= SEQUENCE {
728 * modulus INTEGER, -- n
729 * publicExponent INTEGER -- e
730 * }
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732static int pk_get_rsapubkey(unsigned char **p,
733 const unsigned char *end,
734 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735{
Janos Follath24eed8d2019-11-22 13:21:35 +0000736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 size_t len;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
740 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
741 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
742 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (*p + len != end) {
745 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
746 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
747 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100749 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
751 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
752 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
755 NULL, 0, NULL, 0)) != 0) {
756 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
757 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758
759 *p += len;
760
761 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
764 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
767 NULL, 0, *p, len)) != 0) {
768 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
769 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100770
771 *p += len;
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (mbedtls_rsa_complete(rsa) != 0 ||
774 mbedtls_rsa_check_pubkey(rsa) != 0) {
775 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000776 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (*p != end) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
781 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786
787/* Get a PK algorithm identifier
788 *
789 * AlgorithmIdentifier ::= SEQUENCE {
790 * algorithm OBJECT IDENTIFIER,
791 * parameters ANY DEFINED BY algorithm OPTIONAL }
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int pk_get_pk_alg(unsigned char **p,
794 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200795 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
796 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797{
Janos Follath24eed8d2019-11-22 13:21:35 +0000798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
805 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806
Jethro Beekman01672442023-04-19 14:08:14 +0200807 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200808#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200809 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
810 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
811 if (ret == 0) {
812 *pk_alg = MBEDTLS_PK_ECKEY;
813 }
814 }
815#else
816 (void) ec_grp_id;
817#endif
818 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
820 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
822 /*
823 * No parameters with RSA (only for EC)
824 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (*pk_alg == MBEDTLS_PK_RSA &&
826 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
827 params->len != 0)) {
828 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832}
833
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200834/* Helper for Montgomery curves */
835#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
836#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
837 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
838#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
839
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840/*
841 * SubjectPublicKeyInfo ::= SEQUENCE {
842 * algorithm AlgorithmIdentifier,
843 * subjectPublicKey BIT STRING }
844 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
846 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847{
Janos Follath24eed8d2019-11-22 13:21:35 +0000848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_asn1_buf alg_params;
851 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200852 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
856 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
857 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858 }
859
860 end = *p + len;
861
Jethro Beekman01672442023-04-19 14:08:14 +0200862 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return ret;
864 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
867 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
868 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (*p + len != end) {
871 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
872 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
876 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
877 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
880 return ret;
881 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (pk_alg == MBEDTLS_PK_RSA) {
885 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200888#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200890#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200891 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200892 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200893 } else
894#endif
895 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200896 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200897 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200899 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200902#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (ret == 0 && *p != end) {
906 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
907 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
908 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (ret != 0) {
911 mbedtls_pk_free(pk);
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915}
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100919 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
920 *
921 * The value zero is:
922 * - never a valid value for an RSA parameter
923 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
924 *
925 * Since values can't be omitted in PKCS#1, passing a zero value to
926 * rsa_complete() would be incorrect, so reject zero values early.
927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928static int asn1_get_nonzero_mpi(unsigned char **p,
929 const unsigned char *end,
930 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100931{
932 int ret;
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 ret = mbedtls_asn1_get_mpi(p, end, X);
935 if (ret != 0) {
936 return ret;
937 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
940 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
941 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100944}
945
946/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947 * Parse a PKCS#1 encoded private RSA key
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
950 const unsigned char *key,
951 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100953 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 size_t len;
955 unsigned char *p, *end;
956
Hanno Beckerefa14e82017-10-11 19:45:19 +0100957 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100959
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960 p = (unsigned char *) key;
961 end = p + keylen;
962
963 /*
964 * This function parses the RSAPrivateKey (PKCS#1)
965 *
966 * RSAPrivateKey ::= SEQUENCE {
967 * version Version,
968 * modulus INTEGER, -- n
969 * publicExponent INTEGER, -- e
970 * privateExponent INTEGER, -- d
971 * prime1 INTEGER, -- p
972 * prime2 INTEGER, -- q
973 * exponent1 INTEGER, -- d mod (p-1)
974 * exponent2 INTEGER, -- d mod (q-1)
975 * coefficient INTEGER, -- (inverse of q) mod p
976 * otherPrimeInfos OtherPrimeInfos OPTIONAL
977 * }
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
980 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
981 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 }
983
984 end = p + len;
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
987 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 }
989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (version != 0) {
991 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 }
993
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100994 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
996 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
997 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100998 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001001 /* Import E */
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 NULL, &T)) != 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 D */
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, NULL, NULL,
1011 &T, 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 P */
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, &T, NULL,
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
1022 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1024 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1025 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001026 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001028
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001029#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001030 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1032 * that they can be easily recomputed from D, P and Q. However by
1033 * parsing them from the PKCS1 structure it is possible to avoid
1034 * recalculating them which both reduces the overhead of loading
1035 * RSA private keys into memory and also avoids side channels which
1036 * can arise when computing those values, since all of D, P, and Q
1037 * are secret. See https://eprint.iacr.org/2020/055 for a
1038 * description of one such attack.
1039 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001040
Jack Lloyd80cc8112020-01-22 17:34:29 -05001041 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1043 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1044 goto cleanup;
1045 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001046
1047 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1049 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1050 goto cleanup;
1051 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001052
1053 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1056 goto cleanup;
1057 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001058
Jack Lloyd60239752020-01-27 17:53:36 -05001059#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001060 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1062 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1063 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1064 goto cleanup;
1065 }
Jack Lloyd60239752020-01-27 17:53:36 -05001066#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001067
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001068 /* rsa_complete() doesn't complete anything with the default
1069 * implementation but is still called:
1070 * - for the benefit of alternative implementation that may want to
1071 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1072 * - as is also sanity-checks the key
1073 *
1074 * Furthermore, we also check the public part for consistency with
1075 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1078 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001079 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001080 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (p != end) {
1083 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1084 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085 }
1086
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001087cleanup:
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001092 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret & 0xff80) == 0) {
1094 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1095 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001096 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 }
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
Valerio Setti81d75122023-06-14 14:49:33 +02001106#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107/*
1108 * Parse a SEC1 encoded private EC key
1109 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001110static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 const unsigned char *key, size_t keylen,
1112 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113{
Janos Follath24eed8d2019-11-22 13:21:35 +00001114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001115 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001116 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001117 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001119 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120 unsigned char *end = p + keylen;
1121 unsigned char *end2;
1122
1123 /*
1124 * RFC 5915, or SEC1 Appendix C.4
1125 *
1126 * ECPrivateKey ::= SEQUENCE {
1127 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1128 * privateKey OCTET STRING,
1129 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1130 * publicKey [1] BIT STRING OPTIONAL
1131 * }
1132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1135 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 }
1137
1138 end = p + len;
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1141 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1142 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (version != 1) {
1145 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1146 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1149 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1150 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151
Valerio Setti6b062ee2023-06-30 17:32:57 +02001152 /* Keep a reference to the position fo the private key. It will be used
1153 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001154 d = p;
1155 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001156
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157 p += len;
1158
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001159 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001161 /*
1162 * Is 'parameters' present?
1163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1165 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1166 0)) == 0) {
1167 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001168 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001170 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001173 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001174 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001175
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001176 /*
1177 * Load the private key
1178 */
1179 ret = pk_ecc_set_key(pk, d, d_len);
1180 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001181 return ret;
1182 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001185 /*
1186 * Is 'publickey' present? If not, or if we can't read it (eg because it
1187 * is compressed), create it from the private key.
1188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1190 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1191 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001192 end2 = p + len;
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1196 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (p + len != end2) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1200 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1201 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202
Valerio Setti4064dbb2023-05-17 15:33:07 +02001203 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001204 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001206 /*
1207 * The only acceptable failure mode of pk_get_ecpubkey() above
1208 * is if the point format is not recognized.
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1211 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1212 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001213 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001216 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001218
Valerio Setti34f67552023-04-03 15:19:18 +02001219 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001220 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001221 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001222 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001223 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226}
Valerio Setti81d75122023-06-14 14:49:33 +02001227#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001229/***********************************************************************
1230 *
1231 * PKCS#8 parsing functions
1232 *
1233 **********************************************************************/
1234
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235/*
1236 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001237 *
1238 * Notes:
1239 *
1240 * - This function does not own the key buffer. It is the
1241 * responsibility of the caller to take care of zeroizing
1242 * and freeing it after use.
1243 *
1244 * - The function is responsible for freeing the provided
1245 * PK context on failure.
1246 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 */
1248static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 mbedtls_pk_context *pk,
1250 const unsigned char *key, size_t keylen,
1251 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252{
1253 int ret, version;
1254 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 unsigned char *p = (unsigned char *) key;
1257 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001259 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261
Valerio Setti81d75122023-06-14 14:49:33 +02001262#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001263 (void) f_rng;
1264 (void) p_rng;
1265#endif
1266
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001268 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269 *
1270 * PrivateKeyInfo ::= SEQUENCE {
1271 * version Version,
1272 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1273 * privateKey PrivateKey,
1274 * attributes [0] IMPLICIT Attributes OPTIONAL }
1275 *
1276 * Version ::= INTEGER
1277 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1278 * PrivateKey ::= OCTET STRING
1279 *
1280 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1281 */
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1284 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1285 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 }
1287
1288 end = p + len;
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001292 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (version != 0) {
1295 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1296 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Jethro Beekman01672442023-04-19 14:08:14 +02001298 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return ret;
1300 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1303 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1304 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (len < 1) {
1307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1308 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1309 }
1310
1311 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1312 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1313 }
1314
1315 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1316 return ret;
1317 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (pk_alg == MBEDTLS_PK_RSA) {
1321 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1322 mbedtls_pk_free(pk);
1323 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 }
1325 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001327#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001329#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001330 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001331 if ((ret =
1332 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001333 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001334 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001335 p_rng)) != 0) {
1336 mbedtls_pk_free(pk);
1337 return ret;
1338 }
1339 } else
1340#endif
1341 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001342 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1343 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001344 mbedtls_pk_free(pk);
1345 return ret;
1346 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 }
1348 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001349#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001352 end = p + len;
1353 if (end != (key + keylen)) {
1354 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1355 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1356 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359}
1360
1361/*
1362 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001363 *
1364 * To save space, the decryption happens in-place on the given key buffer.
1365 * Also, while this function may modify the keybuffer, it doesn't own it,
1366 * and instead it is the responsibility of the caller to zeroize and properly
1367 * free it after use.
1368 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001371MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 mbedtls_pk_context *pk,
1373 unsigned char *key, size_t keylen,
1374 const unsigned char *pwd, size_t pwdlen,
1375 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001377 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001379 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1382#if defined(MBEDTLS_PKCS12_C)
1383 mbedtls_cipher_type_t cipher_alg;
1384 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001386 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Hanno Becker2aa80a72017-09-07 15:28:45 +01001388 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 end = p + keylen;
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (pwdlen == 0) {
1392 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1393 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
1395 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001396 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 *
1398 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1399 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1400 * encryptedData EncryptedData
1401 * }
1402 *
1403 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1404 *
1405 * EncryptedData ::= OCTET STRING
1406 *
1407 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001408 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1411 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413 }
1414
1415 end = p + len;
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1419 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Hanno Beckerfab35692017-08-25 13:38:26 +01001425 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426
1427 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001428 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001432 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001433 cipher_alg, md_alg,
1434 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1436 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1437 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001441
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001442 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_PKCS12_C */
1445#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001447 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1448 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1450 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1451 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001455
1456 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001459 {
1460 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001461 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (decrypted == 0) {
1464 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1465 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001466 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001470/***********************************************************************
1471 *
1472 * Top-level functions, with format auto-discovery
1473 *
1474 **********************************************************************/
1475
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476/*
1477 * Parse a private key
1478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1480 const unsigned char *key, size_t keylen,
1481 const unsigned char *pwd, size_t pwdlen,
1482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483{
Janos Follath24eed8d2019-11-22 13:21:35 +00001484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001489#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (keylen == 0) {
1492 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1493 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001494
1495#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001499 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001501 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 } else {
1503 ret = mbedtls_pem_read_buffer(&pem,
1504 "-----BEGIN RSA PRIVATE KEY-----",
1505 "-----END RSA PRIVATE KEY-----",
1506 key, pwd, pwdlen, &len);
1507 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (ret == 0) {
1510 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1511 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1512 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1513 pem.buf, pem.buflen)) != 0) {
1514 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515 }
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 mbedtls_pem_free(&pem);
1518 return ret;
1519 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1520 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1521 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1522 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1523 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1524 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527
Valerio Setti81d75122023-06-14 14:49:33 +02001528#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001529 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001531 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 } else {
1533 ret = mbedtls_pem_read_buffer(&pem,
1534 "-----BEGIN EC PRIVATE KEY-----",
1535 "-----END EC PRIVATE KEY-----",
1536 key, pwd, pwdlen, &len);
1537 }
1538 if (ret == 0) {
1539 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001542 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 pem.buf, pem.buflen,
1544 f_rng, p_rng)) != 0) {
1545 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001546 }
1547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 mbedtls_pem_free(&pem);
1549 return ret;
1550 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1551 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1552 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1553 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1554 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1555 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556 }
Valerio Setti81d75122023-06-14 14:49:33 +02001557#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001559 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001561 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 } else {
1563 ret = mbedtls_pem_read_buffer(&pem,
1564 "-----BEGIN PRIVATE KEY-----",
1565 "-----END PRIVATE KEY-----",
1566 key, NULL, 0, &len);
1567 }
1568 if (ret == 0) {
1569 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1570 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1571 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572 }
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 mbedtls_pem_free(&pem);
1575 return ret;
1576 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1577 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001581 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001583 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 } else {
1585 ret = mbedtls_pem_read_buffer(&pem,
1586 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1587 "-----END ENCRYPTED PRIVATE KEY-----",
1588 key, NULL, 0, &len);
1589 }
1590 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001591 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1592 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001594 }
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 mbedtls_pem_free(&pem);
1597 return ret;
1598 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1599 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602#else
1603 ((void) pwd);
1604 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606
1607 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001608 * At this point we only know it's not a PEM formatted key. Could be any
1609 * of the known DER encoded private key formats
1610 *
1611 * We try the different DER format parsers to see if one passes without
1612 * error
1613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001616 unsigned char *key_copy;
1617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1619 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1620 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001623
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001624 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1625 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001626
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001627 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001628 }
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (ret == 0) {
1631 return 0;
1632 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 mbedtls_pk_free(pk);
1635 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1638 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1643 if (ret == 0) {
1644 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001645 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 mbedtls_pk_free(pk);
1648 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1653 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1654 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1655 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656 }
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 mbedtls_pk_free(pk);
1659 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661
Valerio Setti81d75122023-06-14 14:49:33 +02001662#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1664 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001665 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 key, keylen, f_rng, p_rng) == 0) {
1667 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001668 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001670#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671
Valerio Setti81d75122023-06-14 14:49:33 +02001672 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001673 * it is ok to leave the PK context initialized but not
1674 * freed: It is the caller's responsibility to call pk_init()
1675 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001676 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001677 * isn't, this leads to mbedtls_pk_free() being called
1678 * twice, once here and once by the caller, but this is
1679 * also ok and in line with the mbedtls_pk_free() calls
1680 * on failed PEM parsing attempts. */
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001683}
1684
1685/*
1686 * Parse a public key
1687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1689 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690{
Janos Follath24eed8d2019-11-22 13:21:35 +00001691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001693#if defined(MBEDTLS_RSA_C)
1694 const mbedtls_pk_info_t *pk_info;
1695#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001699#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 if (keylen == 0) {
1702 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1703 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001704
1705#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001707#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001708 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001710 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 } else {
1712 ret = mbedtls_pem_read_buffer(&pem,
1713 "-----BEGIN RSA PUBLIC KEY-----",
1714 "-----END RSA PUBLIC KEY-----",
1715 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001717
1718 if (ret == 0) {
1719 p = pem.buf;
1720 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1721 mbedtls_pem_free(&pem);
1722 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1723 }
1724
1725 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1726 mbedtls_pem_free(&pem);
1727 return ret;
1728 }
1729
1730 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1731 mbedtls_pk_free(ctx);
1732 }
1733
1734 mbedtls_pem_free(&pem);
1735 return ret;
1736 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1737 mbedtls_pem_free(&pem);
1738 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001739 }
1740#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001741
1742 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001744 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 } else {
1746 ret = mbedtls_pem_read_buffer(&pem,
1747 "-----BEGIN PUBLIC KEY-----",
1748 "-----END PUBLIC KEY-----",
1749 key, NULL, 0, &len);
1750 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001753 /*
1754 * Was PEM encoded
1755 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001756 p = pem.buf;
1757
Jethro Beekman01672442023-04-19 14:08:14 +02001758 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 mbedtls_pem_free(&pem);
1760 return ret;
1761 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1762 mbedtls_pem_free(&pem);
1763 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001767
1768#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1770 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001771 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001772
1773 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1774 return ret;
1775 }
1776
1777 p = (unsigned char *) key;
1778 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1779 if (ret == 0) {
1780 return ret;
1781 }
1782 mbedtls_pk_free(ctx);
1783 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1784 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1785 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001786 }
1787#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001788 p = (unsigned char *) key;
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001793}
1794
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001795/***********************************************************************
1796 *
1797 * Top-level functions, with filesystem support
1798 *
1799 **********************************************************************/
1800
1801#if defined(MBEDTLS_FS_IO)
1802/*
1803 * Load all data from a file into a given buffer.
1804 *
1805 * The file is expected to contain either PEM or DER encoded data.
1806 * A terminating null byte is always appended. It is included in the announced
1807 * length only if the data looks like it is PEM encoded.
1808 */
1809int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1810{
1811 FILE *f;
1812 long size;
1813
1814 if ((f = fopen(path, "rb")) == NULL) {
1815 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1816 }
1817
1818 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1819 mbedtls_setbuf(f, NULL);
1820
1821 fseek(f, 0, SEEK_END);
1822 if ((size = ftell(f)) == -1) {
1823 fclose(f);
1824 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1825 }
1826 fseek(f, 0, SEEK_SET);
1827
1828 *n = (size_t) size;
1829
1830 if (*n + 1 == 0 ||
1831 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1832 fclose(f);
1833 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1834 }
1835
1836 if (fread(*buf, 1, *n, f) != *n) {
1837 fclose(f);
1838
1839 mbedtls_zeroize_and_free(*buf, *n);
1840
1841 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1842 }
1843
1844 fclose(f);
1845
1846 (*buf)[*n] = '\0';
1847
1848 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1849 ++*n;
1850 }
1851
1852 return 0;
1853}
1854
1855/*
1856 * Load and parse a private key
1857 */
1858int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1859 const char *path, const char *pwd,
1860 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1861{
1862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1863 size_t n;
1864 unsigned char *buf;
1865
1866 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1867 return ret;
1868 }
1869
1870 if (pwd == NULL) {
1871 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1872 } else {
1873 ret = mbedtls_pk_parse_key(ctx, buf, n,
1874 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1875 }
1876
1877 mbedtls_zeroize_and_free(buf, n);
1878
1879 return ret;
1880}
1881
1882/*
1883 * Load and parse a public key
1884 */
1885int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1886{
1887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1888 size_t n;
1889 unsigned char *buf;
1890
1891 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1892 return ret;
1893 }
1894
1895 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1896
1897 mbedtls_zeroize_and_free(buf, n);
1898
1899 return ret;
1900}
1901#endif /* MBEDTLS_FS_IO */
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#endif /* MBEDTLS_PK_PARSE_C */