blob: 007e1fc03f7172be5107549be90e26b4622cd733 [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)
171 psa_status_t status;
172 (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é-Gonnard54708982023-07-26 15:38:36 +0200176
177 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
178 &pk->pub_raw_len);
179 return psa_pk_status_to_mbedtls(status);
180#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
181 int ret;
182 psa_status_t status;
183 (void) f_rng;
184 (void) p_rng;
185
186 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
187 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
188 size_t key_len;
189 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
190 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
191 size_t curve_bits;
192 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
193 psa_status_t destruction_status;
194
195 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);
197
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200198 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200199 ret = psa_pk_status_to_mbedtls(status);
200 if (ret != 0) {
201 return ret;
202 }
203
204 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
205 ret = psa_pk_status_to_mbedtls(status);
206 destruction_status = psa_destroy_key(key_id);
207 if (ret != 0) {
208 return ret;
209 } else if (destruction_status != PSA_SUCCESS) {
210 return psa_pk_status_to_mbedtls(destruction_status);
211 }
212 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
213#else /* MBEDTLS_USE_PSA_CRYPTO */
214 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200215 (void) prv;
216 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200217
218 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
219#endif /* MBEDTLS_USE_PSA_CRYPTO */
220}
221
222#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
223/*
224 * Create a temporary ecp_keypair for converting an EC point in compressed
225 * format to an uncompressed one
226 *
227 * Consumes everything or fails - inherited from
228 * mbedtls_ecp_point_read_binary().
229 */
230static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
231 const unsigned char *in_start, size_t in_len,
232 unsigned char *out_buf, size_t out_buf_size,
233 size_t *out_buf_len)
234{
235#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
236 mbedtls_ecp_keypair ecp_key;
237 mbedtls_ecp_group_id ecp_group_id;
238 int ret;
239
240 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
241
242 mbedtls_ecp_keypair_init(&ecp_key);
243 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
244 if (ret != 0) {
245 return ret;
246 }
247 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
248 in_start, in_len);
249 if (ret != 0) {
250 goto exit;
251 }
252 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
253 MBEDTLS_ECP_PF_UNCOMPRESSED,
254 out_buf_len, out_buf, out_buf_size);
255
256exit:
257 mbedtls_ecp_keypair_free(&ecp_key);
258 return ret;
259#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
260 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
261#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
262}
263#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
264
265/*
266 * EC public key is an EC point
267 *
268 * The caller is responsible for clearing the structure upon failure if
269 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
270 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
271 */
272static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
273 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200274{
Janos Follath24eed8d2019-11-22 13:21:35 +0000275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200276
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200277#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
278 mbedtls_svc_key_id_t key;
279 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
280 size_t len = (size_t) (end - *p);
281
282 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
283 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100285
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200286 if ((**p == 0x02) || (**p == 0x03)) {
287 /* Compressed format, not supported by PSA Crypto.
288 * Try converting using functions from ECP_LIGHT. */
289 ret = pk_ecc_read_compressed(pk, *p, len,
290 pk->pub_raw,
291 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
292 &pk->pub_raw_len);
293 if (ret != 0) {
294 return ret;
295 }
296 } else {
297 /* Uncompressed format */
298 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
299 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
300 }
301 memcpy(pk->pub_raw, *p, len);
302 pk->pub_raw_len = len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200304
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200305 /* Validate the key by trying to importing it */
306 psa_set_key_usage_flags(&key_attrs, 0);
307 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
308 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
309 psa_set_key_bits(&key_attrs, pk->ec_bits);
310
311 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
312 &key) != PSA_SUCCESS) ||
313 (psa_destroy_key(key) != PSA_SUCCESS)) {
314 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
315 pk->pub_raw_len = 0;
316 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200318 ret = 0;
319#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
320 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
321 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
322 (const unsigned char *) *p,
323 end - *p)) == 0) {
324 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200326#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200327
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200328 /*
329 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
330 * consumed all bytes or failed, and memcpy consumed all bytes too.
331 */
332 *p = (unsigned char *) end;
333
334 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200335}
336
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200337/***********************************************************************
338 *
339 * Unsorted (yet!) from this point on until the next section header
340 *
341 **********************************************************************/
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200344/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100345 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
346 * WARNING: the resulting group should only be used with
347 * pk_group_id_from_specified(), since its base point may not be set correctly
348 * if it was encoded compressed.
349 *
350 * SpecifiedECDomain ::= SEQUENCE {
351 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
352 * fieldID FieldID {{FieldTypes}},
353 * curve Curve,
354 * base ECPoint,
355 * order INTEGER,
356 * cofactor INTEGER OPTIONAL,
357 * hash HashAlgorithm OPTIONAL,
358 * ...
359 * }
360 *
361 * We only support prime-field as field type, and ignore hash and cofactor.
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364{
Janos Follath24eed8d2019-11-22 13:21:35 +0000365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200367 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368 const unsigned char *end_field, *end_curve;
369 size_t len;
370 int ver;
371
372 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
375 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (ver < 1 || ver > 3) {
378 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
379 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100380
381 /*
382 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
383 * fieldType FIELD-ID.&id({IOSet}),
384 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
385 * }
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
388 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
389 return ret;
390 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
392 end_field = p + len;
393
394 /*
395 * FIELD-ID ::= TYPE-IDENTIFIER
396 * FieldTypes FIELD-ID ::= {
397 * { Prime-p IDENTIFIED BY prime-field } |
398 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
399 * }
400 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
403 return ret;
404 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
407 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
408 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409 }
410
411 p += len;
412
413 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (p != end_field) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
422 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
423 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
425 /*
426 * Curve ::= SEQUENCE {
427 * a FieldElement,
428 * b FieldElement,
429 * seed BIT STRING OPTIONAL
430 * -- Shall be present if used in SpecifiedECDomain
431 * -- with version equal to ecdpVer2 or ecdpVer3
432 * }
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
435 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
436 return ret;
437 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438
439 end_curve = p + len;
440
441 /*
442 * FieldElement ::= OCTET STRING
443 * containing an integer in the case of a prime field
444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
446 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
447 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448 }
449
450 p += len;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
453 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455 }
456
457 p += len;
458
459 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (p != end_curve) {
465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
467 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
469 /*
470 * ECPoint ::= OCTET STRING
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
473 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
474 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
477 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100478 /*
479 * If we can't read the point because it's compressed, cheat by
480 * reading only the X coordinate and the parity bit of Y.
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
483 (p[0] != 0x02 && p[0] != 0x03) ||
484 len != mbedtls_mpi_size(&grp->P) + 1 ||
485 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
486 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
487 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
488 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100489 }
490 }
491
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 p += len;
493
494 /*
495 * order INTEGER
496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
498 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
499 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100502
503 /*
504 * Allow optional elements by purposefully not enforcing p == end here.
505 */
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100508}
509
510/*
511 * Find the group id associated with an (almost filled) group as generated by
512 * pk_group_from_specified(), or return an error if unknown.
513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100515{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100516 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_ecp_group ref;
518 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100523 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_ecp_group_free(&ref);
525 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100526
527 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
529 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
530 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
531 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
532 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
533 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
534 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100535 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100537 break;
538 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100539 }
540
541cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100543
544 *grp_id = *id;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100551}
552
553/*
554 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
557 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100558{
Janos Follath24eed8d2019-11-22 13:21:35 +0000559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100565 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100569
570cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000571 /* The API respecting lifecycle for mbedtls_ecp_group struct is
572 * _init(), _load() and _free(). In pk_group_id_from_specified() the
573 * temporary grp breaks that flow and it's members are populated
574 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
575 * which is assuming a group populated by _setup() may not clean-up
576 * properly -> Manually free it's members.
577 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000578 mbedtls_mpi_free(&grp.N);
579 mbedtls_mpi_free(&grp.P);
580 mbedtls_mpi_free(&grp.A);
581 mbedtls_mpi_free(&grp.B);
582 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100587
Valerio Setti4064dbb2023-05-17 15:33:07 +0200588
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200589/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200590 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200591 * ECParameters ::= CHOICE {
592 * namedCurve OBJECT IDENTIFIER
593 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
594 * -- implicitCurve NULL
595 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200596 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200597static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
598 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200599{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200601
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200602 if (end - *p < 1) {
603 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
604 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200605 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200606
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200607 /* Tag may be either OID or SEQUENCE */
608 params->tag = **p;
609 if (params->tag != MBEDTLS_ASN1_OID
610#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
611 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
612#endif
613 ) {
614 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
615 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
616 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200617
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200618 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200619 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
620 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200621
622 params->p = *p;
623 *p += params->len;
624
625 if (*p != end) {
626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
627 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
628 }
629
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200630 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200631}
632
633/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100635 *
636 * ECParameters ::= CHOICE {
637 * namedCurve OBJECT IDENTIFIER
638 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
639 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200641static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642{
Janos Follath24eed8d2019-11-22 13:21:35 +0000643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (params->tag == MBEDTLS_ASN1_OID) {
647 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
648 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
649 }
650 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
653 return ret;
654 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100655#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100657#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100658 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200659
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200660 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661}
662
Jethro Beekman01672442023-04-19 14:08:14 +0200663#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
664
665/*
666 * Load an RFC8410 EC key, which doesn't have any parameters
667 */
668static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
669 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200670 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200671{
672 if (params->tag != 0 || params->len != 0) {
673 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
674 }
675
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200676 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200677}
678
679/*
680 * Parse an RFC 8410 encoded private EC key
681 *
682 * CurvePrivateKey ::= OCTET STRING
683 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200684static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200685 unsigned char *key, size_t keylen, const unsigned char *end,
686 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
687{
688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
689 size_t len;
690
691 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
692 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
693 }
694
695 if (key + len != end) {
696 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
697 }
698
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200699 /*
700 * Load the private key
701 */
702 ret = pk_ecc_set_key(pk, key, len);
703 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200704 return ret;
705 }
Jethro Beekman01672442023-04-19 14:08:14 +0200706
Valerio Setti4064dbb2023-05-17 15:33:07 +0200707 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
708 * which never contain a public key. As such, derive the public key
709 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200710 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200711 return ret;
712 }
713
Jethro Beekman01672442023-04-19 14:08:14 +0200714 return 0;
715}
716#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200717
Valerio Setti81d75122023-06-14 14:49:33 +0200718#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721/*
722 * RSAPublicKey ::= SEQUENCE {
723 * modulus INTEGER, -- n
724 * publicExponent INTEGER -- e
725 * }
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727static int pk_get_rsapubkey(unsigned char **p,
728 const unsigned char *end,
729 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730{
Janos Follath24eed8d2019-11-22 13:21:35 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732 size_t len;
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
736 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
737 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (*p + len != end) {
740 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
741 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
742 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
747 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
750 NULL, 0, NULL, 0)) != 0) {
751 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
752 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753
754 *p += len;
755
756 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
758 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
759 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
762 NULL, 0, *p, len)) != 0) {
763 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
764 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765
766 *p += len;
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (mbedtls_rsa_complete(rsa) != 0 ||
769 mbedtls_rsa_check_pubkey(rsa) != 0) {
770 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000771 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (*p != end) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
775 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
776 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
782/* Get a PK algorithm identifier
783 *
784 * AlgorithmIdentifier ::= SEQUENCE {
785 * algorithm OBJECT IDENTIFIER,
786 * parameters ANY DEFINED BY algorithm OPTIONAL }
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788static int pk_get_pk_alg(unsigned char **p,
789 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200790 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
791 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792{
Janos Follath24eed8d2019-11-22 13:21:35 +0000793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
799 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801
Jethro Beekman01672442023-04-19 14:08:14 +0200802 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200803#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200804 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
805 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
806 if (ret == 0) {
807 *pk_alg = MBEDTLS_PK_ECKEY;
808 }
809 }
810#else
811 (void) ec_grp_id;
812#endif
813 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
817 /*
818 * No parameters with RSA (only for EC)
819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (*pk_alg == MBEDTLS_PK_RSA &&
821 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
822 params->len != 0)) {
823 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827}
828
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200829/* Helper for Montgomery curves */
830#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
831#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
832 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
833#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
834
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835/*
836 * SubjectPublicKeyInfo ::= SEQUENCE {
837 * algorithm AlgorithmIdentifier,
838 * subjectPublicKey BIT STRING }
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
841 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842{
Janos Follath24eed8d2019-11-22 13:21:35 +0000843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_asn1_buf alg_params;
846 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200847 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
851 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
852 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 }
854
855 end = *p + len;
856
Jethro Beekman01672442023-04-19 14:08:14 +0200857 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 return ret;
859 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
862 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
863 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (*p + len != end) {
866 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
868 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
871 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
872 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
875 return ret;
876 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (pk_alg == MBEDTLS_PK_RSA) {
880 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200883#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200885#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200886 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200887 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200888 } else
889#endif
890 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200891 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200892 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200894 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200897#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (ret == 0 && *p != end) {
901 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
902 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
903 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (ret != 0) {
906 mbedtls_pk_free(pk);
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910}
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100914 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
915 *
916 * The value zero is:
917 * - never a valid value for an RSA parameter
918 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
919 *
920 * Since values can't be omitted in PKCS#1, passing a zero value to
921 * rsa_complete() would be incorrect, so reject zero values early.
922 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923static int asn1_get_nonzero_mpi(unsigned char **p,
924 const unsigned char *end,
925 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100926{
927 int ret;
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 ret = mbedtls_asn1_get_mpi(p, end, X);
930 if (ret != 0) {
931 return ret;
932 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
935 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
936 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100939}
940
941/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942 * Parse a PKCS#1 encoded private RSA key
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
945 const unsigned char *key,
946 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100948 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 size_t len;
950 unsigned char *p, *end;
951
Hanno Beckerefa14e82017-10-11 19:45:19 +0100952 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100954
Paul Bakker1a7550a2013-09-15 13:01:22 +0200955 p = (unsigned char *) key;
956 end = p + keylen;
957
958 /*
959 * This function parses the RSAPrivateKey (PKCS#1)
960 *
961 * RSAPrivateKey ::= SEQUENCE {
962 * version Version,
963 * modulus INTEGER, -- n
964 * publicExponent INTEGER, -- e
965 * privateExponent INTEGER, -- d
966 * prime1 INTEGER, -- p
967 * prime2 INTEGER, -- q
968 * exponent1 INTEGER, -- d mod (p-1)
969 * exponent2 INTEGER, -- d mod (q-1)
970 * coefficient INTEGER, -- (inverse of q) mod p
971 * otherPrimeInfos OtherPrimeInfos OPTIONAL
972 * }
973 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
975 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
976 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977 }
978
979 end = p + len;
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
982 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (version != 0) {
986 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987 }
988
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100989 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
991 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
992 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100993 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100996 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
998 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
999 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001000 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001002
1003 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1005 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1006 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001007 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001009
1010 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1012 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1013 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001014 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001016
1017 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1019 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1020 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001021 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001023
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001024#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001025 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1027 * that they can be easily recomputed from D, P and Q. However by
1028 * parsing them from the PKCS1 structure it is possible to avoid
1029 * recalculating them which both reduces the overhead of loading
1030 * RSA private keys into memory and also avoids side channels which
1031 * can arise when computing those values, since all of D, P, and Q
1032 * are secret. See https://eprint.iacr.org/2020/055 for a
1033 * description of one such attack.
1034 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001035
Jack Lloyd80cc8112020-01-22 17:34:29 -05001036 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1038 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1039 goto cleanup;
1040 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001041
1042 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1044 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1045 goto cleanup;
1046 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001047
1048 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1050 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1051 goto cleanup;
1052 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001053
Jack Lloyd60239752020-01-27 17:53:36 -05001054#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001055 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1057 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1058 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1059 goto cleanup;
1060 }
Jack Lloyd60239752020-01-27 17:53:36 -05001061#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001062
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001063 /* rsa_complete() doesn't complete anything with the default
1064 * implementation but is still called:
1065 * - for the benefit of alternative implementation that may want to
1066 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1067 * - as is also sanity-checks the key
1068 *
1069 * Furthermore, we also check the public part for consistency with
1070 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1073 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001074 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001075 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (p != end) {
1078 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1079 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 }
1081
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001082cleanup:
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001087 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((ret & 0xff80) == 0) {
1089 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1090 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095 }
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100
Valerio Setti81d75122023-06-14 14:49:33 +02001101#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102/*
1103 * Parse a SEC1 encoded private EC key
1104 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001105static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 const unsigned char *key, size_t keylen,
1107 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108{
Janos Follath24eed8d2019-11-22 13:21:35 +00001109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001110 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001111 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001112 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001114 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115 unsigned char *end = p + keylen;
1116 unsigned char *end2;
1117
1118 /*
1119 * RFC 5915, or SEC1 Appendix C.4
1120 *
1121 * ECPrivateKey ::= SEQUENCE {
1122 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1123 * privateKey OCTET STRING,
1124 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1125 * publicKey [1] BIT STRING OPTIONAL
1126 * }
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1129 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1130 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131 }
1132
1133 end = p + len;
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1136 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1137 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (version != 1) {
1140 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1141 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1144 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1145 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146
Valerio Setti6b062ee2023-06-30 17:32:57 +02001147 /* Keep a reference to the position fo the private key. It will be used
1148 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001149 d = p;
1150 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001151
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 p += len;
1153
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001154 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001156 /*
1157 * Is 'parameters' present?
1158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1160 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1161 0)) == 0) {
1162 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001163 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001168 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001169 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001170
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001171 /*
1172 * Load the private key
1173 */
1174 ret = pk_ecc_set_key(pk, d, d_len);
1175 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001176 return ret;
1177 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001180 /*
1181 * Is 'publickey' present? If not, or if we can't read it (eg because it
1182 * is compressed), create it from the private key.
1183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1185 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1186 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001187 end2 = p + len;
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1191 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (p + len != end2) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1195 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1196 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197
Valerio Setti4064dbb2023-05-17 15:33:07 +02001198 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001199 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201 /*
1202 * The only acceptable failure mode of pk_get_ecpubkey() above
1203 * is if the point format is not recognized.
1204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1206 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1207 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001208 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001211 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001213
Valerio Setti34f67552023-04-03 15:19:18 +02001214 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001215 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001216 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001217 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001218 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221}
Valerio Setti81d75122023-06-14 14:49:33 +02001222#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001224/***********************************************************************
1225 *
1226 * PKCS#8 parsing functions
1227 *
1228 **********************************************************************/
1229
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230/*
1231 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001232 *
1233 * Notes:
1234 *
1235 * - This function does not own the key buffer. It is the
1236 * responsibility of the caller to take care of zeroizing
1237 * and freeing it after use.
1238 *
1239 * - The function is responsible for freeing the provided
1240 * PK context on failure.
1241 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 */
1243static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_pk_context *pk,
1245 const unsigned char *key, size_t keylen,
1246 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247{
1248 int ret, version;
1249 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 unsigned char *p = (unsigned char *) key;
1252 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001254 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256
Valerio Setti81d75122023-06-14 14:49:33 +02001257#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001258 (void) f_rng;
1259 (void) p_rng;
1260#endif
1261
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001263 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 *
1265 * PrivateKeyInfo ::= SEQUENCE {
1266 * version Version,
1267 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1268 * privateKey PrivateKey,
1269 * attributes [0] IMPLICIT Attributes OPTIONAL }
1270 *
1271 * Version ::= INTEGER
1272 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1273 * PrivateKey ::= OCTET STRING
1274 *
1275 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1276 */
1277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1279 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1280 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 }
1282
1283 end = p + len;
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1286 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001287 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (version != 0) {
1290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1291 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292
Jethro Beekman01672442023-04-19 14:08:14 +02001293 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return ret;
1295 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1299 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (len < 1) {
1302 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1303 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1304 }
1305
1306 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1307 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1308 }
1309
1310 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1311 return ret;
1312 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (pk_alg == MBEDTLS_PK_RSA) {
1316 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1317 mbedtls_pk_free(pk);
1318 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 }
1320 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001322#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001324#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001325 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001326 if ((ret =
1327 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001328 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001329 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001330 p_rng)) != 0) {
1331 mbedtls_pk_free(pk);
1332 return ret;
1333 }
1334 } else
1335#endif
1336 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001337 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1338 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001339 mbedtls_pk_free(pk);
1340 return ret;
1341 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 }
1343 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001344#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001347 end = p + len;
1348 if (end != (key + keylen)) {
1349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1351 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354}
1355
1356/*
1357 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001358 *
1359 * To save space, the decryption happens in-place on the given key buffer.
1360 * Also, while this function may modify the keybuffer, it doesn't own it,
1361 * and instead it is the responsibility of the caller to zeroize and properly
1362 * free it after use.
1363 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001366MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 mbedtls_pk_context *pk,
1368 unsigned char *key, size_t keylen,
1369 const unsigned char *pwd, size_t pwdlen,
1370 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001372 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001374 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1377#if defined(MBEDTLS_PKCS12_C)
1378 mbedtls_cipher_type_t cipher_alg;
1379 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001381 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Hanno Becker2aa80a72017-09-07 15:28:45 +01001383 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384 end = p + keylen;
1385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (pwdlen == 0) {
1387 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1388 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
1390 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001391 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 *
1393 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1394 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1395 * encryptedData EncryptedData
1396 * }
1397 *
1398 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1399 *
1400 * EncryptedData ::= OCTET STRING
1401 *
1402 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001403 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1406 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 }
1409
1410 end = p + len;
1411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1414 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1418 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
Hanno Beckerfab35692017-08-25 13:38:26 +01001420 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421
1422 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001423 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001427 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001428 cipher_alg, md_alg,
1429 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1431 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1432 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001436
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001437 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_PKCS12_C */
1440#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001442 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1443 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1445 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001450
1451 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001454 {
1455 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001456 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (decrypted == 0) {
1459 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1460 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001461 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001465/***********************************************************************
1466 *
1467 * Top-level functions, with format auto-discovery
1468 *
1469 **********************************************************************/
1470
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471/*
1472 * Parse a private key
1473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1475 const unsigned char *key, size_t keylen,
1476 const unsigned char *pwd, size_t pwdlen,
1477 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478{
Janos Follath24eed8d2019-11-22 13:21:35 +00001479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001484#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (keylen == 0) {
1487 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1488 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001489
1490#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001494 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001496 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 } else {
1498 ret = mbedtls_pem_read_buffer(&pem,
1499 "-----BEGIN RSA PRIVATE KEY-----",
1500 "-----END RSA PRIVATE KEY-----",
1501 key, pwd, pwdlen, &len);
1502 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (ret == 0) {
1505 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1506 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1507 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1508 pem.buf, pem.buflen)) != 0) {
1509 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510 }
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 mbedtls_pem_free(&pem);
1513 return ret;
1514 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1515 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1516 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1517 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1518 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1519 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522
Valerio Setti81d75122023-06-14 14:49:33 +02001523#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001524 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001526 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 } else {
1528 ret = mbedtls_pem_read_buffer(&pem,
1529 "-----BEGIN EC PRIVATE KEY-----",
1530 "-----END EC PRIVATE KEY-----",
1531 key, pwd, pwdlen, &len);
1532 }
1533 if (ret == 0) {
1534 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001537 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 pem.buf, pem.buflen,
1539 f_rng, p_rng)) != 0) {
1540 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541 }
1542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 mbedtls_pem_free(&pem);
1544 return ret;
1545 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1546 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1547 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1548 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1549 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1550 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551 }
Valerio Setti81d75122023-06-14 14:49:33 +02001552#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001554 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001556 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 } else {
1558 ret = mbedtls_pem_read_buffer(&pem,
1559 "-----BEGIN PRIVATE KEY-----",
1560 "-----END PRIVATE KEY-----",
1561 key, NULL, 0, &len);
1562 }
1563 if (ret == 0) {
1564 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1565 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1566 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567 }
1568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 mbedtls_pem_free(&pem);
1570 return ret;
1571 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1572 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001576 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001578 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 } else {
1580 ret = mbedtls_pem_read_buffer(&pem,
1581 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1582 "-----END ENCRYPTED PRIVATE KEY-----",
1583 key, NULL, 0, &len);
1584 }
1585 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001586 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1587 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 }
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 mbedtls_pem_free(&pem);
1592 return ret;
1593 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1594 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597#else
1598 ((void) pwd);
1599 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601
1602 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001603 * At this point we only know it's not a PEM formatted key. Could be any
1604 * of the known DER encoded private key formats
1605 *
1606 * We try the different DER format parsers to see if one passes without
1607 * error
1608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001611 unsigned char *key_copy;
1612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1614 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1615 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001618
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001619 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1620 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001621
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001622 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001623 }
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (ret == 0) {
1626 return 0;
1627 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 mbedtls_pk_free(pk);
1630 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1633 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1638 if (ret == 0) {
1639 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001640 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_pk_free(pk);
1643 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1648 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1649 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1650 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651 }
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 mbedtls_pk_free(pk);
1654 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656
Valerio Setti81d75122023-06-14 14:49:33 +02001657#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1659 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001660 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 key, keylen, f_rng, p_rng) == 0) {
1662 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001665#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666
Valerio Setti81d75122023-06-14 14:49:33 +02001667 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001668 * it is ok to leave the PK context initialized but not
1669 * freed: It is the caller's responsibility to call pk_init()
1670 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001671 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001672 * isn't, this leads to mbedtls_pk_free() being called
1673 * twice, once here and once by the caller, but this is
1674 * also ok and in line with the mbedtls_pk_free() calls
1675 * on failed PEM parsing attempts. */
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001678}
1679
1680/*
1681 * Parse a public key
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1684 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685{
Janos Follath24eed8d2019-11-22 13:21:35 +00001686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001688#if defined(MBEDTLS_RSA_C)
1689 const mbedtls_pk_info_t *pk_info;
1690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001694#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (keylen == 0) {
1697 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1698 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001699
1700#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001702#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001703 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001705 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 } else {
1707 ret = mbedtls_pem_read_buffer(&pem,
1708 "-----BEGIN RSA PUBLIC KEY-----",
1709 "-----END RSA PUBLIC KEY-----",
1710 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001711 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001712
1713 if (ret == 0) {
1714 p = pem.buf;
1715 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1716 mbedtls_pem_free(&pem);
1717 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1718 }
1719
1720 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1721 mbedtls_pem_free(&pem);
1722 return ret;
1723 }
1724
1725 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1726 mbedtls_pk_free(ctx);
1727 }
1728
1729 mbedtls_pem_free(&pem);
1730 return ret;
1731 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1732 mbedtls_pem_free(&pem);
1733 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001734 }
1735#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001736
1737 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001739 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 } else {
1741 ret = mbedtls_pem_read_buffer(&pem,
1742 "-----BEGIN PUBLIC KEY-----",
1743 "-----END PUBLIC KEY-----",
1744 key, NULL, 0, &len);
1745 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001748 /*
1749 * Was PEM encoded
1750 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001751 p = pem.buf;
1752
Jethro Beekman01672442023-04-19 14:08:14 +02001753 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 mbedtls_pem_free(&pem);
1755 return ret;
1756 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1757 mbedtls_pem_free(&pem);
1758 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001762
1763#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1765 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001767
1768 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1769 return ret;
1770 }
1771
1772 p = (unsigned char *) key;
1773 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1774 if (ret == 0) {
1775 return ret;
1776 }
1777 mbedtls_pk_free(ctx);
1778 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1779 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1780 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001781 }
1782#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001783 p = (unsigned char *) key;
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001788}
1789
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001790/***********************************************************************
1791 *
1792 * Top-level functions, with filesystem support
1793 *
1794 **********************************************************************/
1795
1796#if defined(MBEDTLS_FS_IO)
1797/*
1798 * Load all data from a file into a given buffer.
1799 *
1800 * The file is expected to contain either PEM or DER encoded data.
1801 * A terminating null byte is always appended. It is included in the announced
1802 * length only if the data looks like it is PEM encoded.
1803 */
1804int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1805{
1806 FILE *f;
1807 long size;
1808
1809 if ((f = fopen(path, "rb")) == NULL) {
1810 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1811 }
1812
1813 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1814 mbedtls_setbuf(f, NULL);
1815
1816 fseek(f, 0, SEEK_END);
1817 if ((size = ftell(f)) == -1) {
1818 fclose(f);
1819 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1820 }
1821 fseek(f, 0, SEEK_SET);
1822
1823 *n = (size_t) size;
1824
1825 if (*n + 1 == 0 ||
1826 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1827 fclose(f);
1828 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1829 }
1830
1831 if (fread(*buf, 1, *n, f) != *n) {
1832 fclose(f);
1833
1834 mbedtls_zeroize_and_free(*buf, *n);
1835
1836 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1837 }
1838
1839 fclose(f);
1840
1841 (*buf)[*n] = '\0';
1842
1843 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1844 ++*n;
1845 }
1846
1847 return 0;
1848}
1849
1850/*
1851 * Load and parse a private key
1852 */
1853int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1854 const char *path, const char *pwd,
1855 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1856{
1857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1858 size_t n;
1859 unsigned char *buf;
1860
1861 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1862 return ret;
1863 }
1864
1865 if (pwd == NULL) {
1866 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1867 } else {
1868 ret = mbedtls_pk_parse_key(ctx, buf, n,
1869 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1870 }
1871
1872 mbedtls_zeroize_and_free(buf, n);
1873
1874 return ret;
1875}
1876
1877/*
1878 * Load and parse a public key
1879 */
1880int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1881{
1882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1883 size_t n;
1884 unsigned char *buf;
1885
1886 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1887 return ret;
1888 }
1889
1890 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1891
1892 mbedtls_zeroize_and_free(buf, n);
1893
1894 return ret;
1895}
1896#endif /* MBEDTLS_FS_IO */
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_PK_PARSE_C */