blob: 5a35c43fbbffad77e8a1592a1afb55b0151891da [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.
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020073static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
74{
75#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
76 size_t ec_bits;
77 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
78
79 /* group may already be initialized; if so, make sure IDs match */
80 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
81 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
82 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
83 }
84
85 /* set group */
86 pk->ec_family = ec_family;
87 pk->ec_bits = ec_bits;
88
89 return 0;
90#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
91 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
92
93 /* grp may already be initialized; if so, make sure IDs match */
94 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
95 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
96 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
97 }
98
99 /* set group */
100 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
101#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
102}
103
104/*
105 * Set the private key material
106 *
107 * Must have already set the group with pk_ecc_set_group().
108 *
109 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
110 */
111static int pk_ecc_set_key(mbedtls_pk_context *pk,
112 unsigned char *key, size_t len)
113{
114#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
116 psa_status_t status;
117
118 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
119 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
120 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
121 /* Montgomery allows only ECDH, others ECDSA too */
122 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
123 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
124 psa_set_key_enrollment_algorithm(&attributes,
125 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
126 }
127 psa_set_key_usage_flags(&attributes, flags);
128
129 status = psa_import_key(&attributes, key, len, &pk->priv_id);
130 return psa_pk_status_to_mbedtls(status);
131
132#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
133
134 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
135 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
136 if (ret != 0) {
137 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
138 }
139 return 0;
140#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
141}
142
143/*
144 * Helper function for deriving a public key from its private counterpart.
145 *
146 * Note: the private key information is always available from pk,
147 * however for convenience the serialized version is also passed,
148 * as it's available at each calling site, and useful in some configs
149 * (as otherwise we're have to re-serialize it from the pk context).
150 */
151static int pk_derive_public_key(mbedtls_pk_context *pk,
152 const unsigned char *d, size_t d_len,
153 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
154{
155#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
156 psa_status_t status;
157 (void) f_rng;
158 (void) p_rng;
159 (void) d;
160 (void) d_len;
161
162 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
163 &pk->pub_raw_len);
164 return psa_pk_status_to_mbedtls(status);
165#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
166 int ret;
167 psa_status_t status;
168 (void) f_rng;
169 (void) p_rng;
170
171 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
172 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
173 size_t key_len;
174 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
175 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
176 size_t curve_bits;
177 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
178 psa_status_t destruction_status;
179
180 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
181 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
182
183 status = psa_import_key(&key_attr, d, d_len, &key_id);
184 ret = psa_pk_status_to_mbedtls(status);
185 if (ret != 0) {
186 return ret;
187 }
188
189 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
190 ret = psa_pk_status_to_mbedtls(status);
191 destruction_status = psa_destroy_key(key_id);
192 if (ret != 0) {
193 return ret;
194 } else if (destruction_status != PSA_SUCCESS) {
195 return psa_pk_status_to_mbedtls(destruction_status);
196 }
197 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
198#else /* MBEDTLS_USE_PSA_CRYPTO */
199 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
200 (void) d;
201 (void) d_len;
202
203 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
204#endif /* MBEDTLS_USE_PSA_CRYPTO */
205}
206
207#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
208/*
209 * Create a temporary ecp_keypair for converting an EC point in compressed
210 * format to an uncompressed one
211 *
212 * Consumes everything or fails - inherited from
213 * mbedtls_ecp_point_read_binary().
214 */
215static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
216 const unsigned char *in_start, size_t in_len,
217 unsigned char *out_buf, size_t out_buf_size,
218 size_t *out_buf_len)
219{
220#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
221 mbedtls_ecp_keypair ecp_key;
222 mbedtls_ecp_group_id ecp_group_id;
223 int ret;
224
225 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
226
227 mbedtls_ecp_keypair_init(&ecp_key);
228 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
229 if (ret != 0) {
230 return ret;
231 }
232 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
233 in_start, in_len);
234 if (ret != 0) {
235 goto exit;
236 }
237 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
238 MBEDTLS_ECP_PF_UNCOMPRESSED,
239 out_buf_len, out_buf, out_buf_size);
240
241exit:
242 mbedtls_ecp_keypair_free(&ecp_key);
243 return ret;
244#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
245 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
246#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
247}
248#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
249
250/*
251 * EC public key is an EC point
252 *
253 * The caller is responsible for clearing the structure upon failure if
254 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
255 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
256 */
257static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
258 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200259{
Janos Follath24eed8d2019-11-22 13:21:35 +0000260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200261
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200262#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
263 mbedtls_svc_key_id_t key;
264 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
265 size_t len = (size_t) (end - *p);
266
267 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
268 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100270
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200271 if ((**p == 0x02) || (**p == 0x03)) {
272 /* Compressed format, not supported by PSA Crypto.
273 * Try converting using functions from ECP_LIGHT. */
274 ret = pk_ecc_read_compressed(pk, *p, len,
275 pk->pub_raw,
276 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
277 &pk->pub_raw_len);
278 if (ret != 0) {
279 return ret;
280 }
281 } else {
282 /* Uncompressed format */
283 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
284 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
285 }
286 memcpy(pk->pub_raw, *p, len);
287 pk->pub_raw_len = len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200289
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200290 /* Validate the key by trying to importing it */
291 psa_set_key_usage_flags(&key_attrs, 0);
292 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
293 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
294 psa_set_key_bits(&key_attrs, pk->ec_bits);
295
296 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
297 &key) != PSA_SUCCESS) ||
298 (psa_destroy_key(key) != PSA_SUCCESS)) {
299 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
300 pk->pub_raw_len = 0;
301 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100302 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200303 ret = 0;
304#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
305 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
306 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
307 (const unsigned char *) *p,
308 end - *p)) == 0) {
309 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200311#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200312
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200313 /*
314 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
315 * consumed all bytes or failed, and memcpy consumed all bytes too.
316 */
317 *p = (unsigned char *) end;
318
319 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200320}
321
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200322/***********************************************************************
323 *
324 * Unsorted (yet!) from this point on until the next section header
325 *
326 **********************************************************************/
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200329/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
331 * WARNING: the resulting group should only be used with
332 * pk_group_id_from_specified(), since its base point may not be set correctly
333 * if it was encoded compressed.
334 *
335 * SpecifiedECDomain ::= SEQUENCE {
336 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
337 * fieldID FieldID {{FieldTypes}},
338 * curve Curve,
339 * base ECPoint,
340 * order INTEGER,
341 * cofactor INTEGER OPTIONAL,
342 * hash HashAlgorithm OPTIONAL,
343 * ...
344 * }
345 *
346 * We only support prime-field as field type, and ignore hash and cofactor.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349{
Janos Follath24eed8d2019-11-22 13:21:35 +0000350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100351 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200352 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100353 const unsigned char *end_field, *end_curve;
354 size_t len;
355 int ver;
356
357 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
360 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ver < 1 || ver > 3) {
363 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
364 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365
366 /*
367 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
368 * fieldType FIELD-ID.&id({IOSet}),
369 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
370 * }
371 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
373 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
374 return ret;
375 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
377 end_field = p + len;
378
379 /*
380 * FIELD-ID ::= TYPE-IDENTIFIER
381 * FieldTypes FIELD-ID ::= {
382 * { Prime-p IDENTIFIED BY prime-field } |
383 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
384 * }
385 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
388 return ret;
389 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
392 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
393 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394 }
395
396 p += len;
397
398 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
401 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (p != end_field) {
406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
407 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
408 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409
410 /*
411 * Curve ::= SEQUENCE {
412 * a FieldElement,
413 * b FieldElement,
414 * seed BIT STRING OPTIONAL
415 * -- Shall be present if used in SpecifiedECDomain
416 * -- with version equal to ecdpVer2 or ecdpVer3
417 * }
418 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
420 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
421 return ret;
422 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423
424 end_curve = p + len;
425
426 /*
427 * FieldElement ::= OCTET STRING
428 * containing an integer in the case of a prime field
429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
431 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433 }
434
435 p += len;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
438 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
439 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440 }
441
442 p += len;
443
444 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (p != end_curve) {
450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
451 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
452 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453
454 /*
455 * ECPoint ::= OCTET STRING
456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
459 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
462 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100463 /*
464 * If we can't read the point because it's compressed, cheat by
465 * reading only the X coordinate and the parity bit of Y.
466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
468 (p[0] != 0x02 && p[0] != 0x03) ||
469 len != mbedtls_mpi_size(&grp->P) + 1 ||
470 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
471 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
472 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
473 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100474 }
475 }
476
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100477 p += len;
478
479 /*
480 * order INTEGER
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
483 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
484 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100487
488 /*
489 * Allow optional elements by purposefully not enforcing p == end here.
490 */
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100493}
494
495/*
496 * Find the group id associated with an (almost filled) group as generated by
497 * pk_group_from_specified(), or return an error if unknown.
498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499static 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 +0100500{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100501 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_ecp_group ref;
503 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100508 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_ecp_group_free(&ref);
510 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100511
512 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
514 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
515 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
516 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
517 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
518 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
519 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100520 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 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 +0100522 break;
523 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100524 }
525
526cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100528
529 *grp_id = *id;
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100536}
537
538/*
539 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
542 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100543{
Janos Follath24eed8d2019-11-22 13:21:35 +0000544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100550 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100554
555cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000556 /* The API respecting lifecycle for mbedtls_ecp_group struct is
557 * _init(), _load() and _free(). In pk_group_id_from_specified() the
558 * temporary grp breaks that flow and it's members are populated
559 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
560 * which is assuming a group populated by _setup() may not clean-up
561 * properly -> Manually free it's members.
562 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000563 mbedtls_mpi_free(&grp.N);
564 mbedtls_mpi_free(&grp.P);
565 mbedtls_mpi_free(&grp.A);
566 mbedtls_mpi_free(&grp.B);
567 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100572
Valerio Setti4064dbb2023-05-17 15:33:07 +0200573
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200574/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200575 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200576 * ECParameters ::= CHOICE {
577 * namedCurve OBJECT IDENTIFIER
578 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
579 * -- implicitCurve NULL
580 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200581 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200582static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
583 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200584{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200586
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200587 if (end - *p < 1) {
588 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
589 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200590 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200591
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200592 /* Tag may be either OID or SEQUENCE */
593 params->tag = **p;
594 if (params->tag != MBEDTLS_ASN1_OID
595#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
596 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
597#endif
598 ) {
599 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
600 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
601 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200602
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200603 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200604 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
605 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200606
607 params->p = *p;
608 *p += params->len;
609
610 if (*p != end) {
611 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
612 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
613 }
614
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200615 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200616}
617
618/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100620 *
621 * ECParameters ::= CHOICE {
622 * namedCurve OBJECT IDENTIFIER
623 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
624 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200625 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200626static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627{
Janos Follath24eed8d2019-11-22 13:21:35 +0000628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (params->tag == MBEDTLS_ASN1_OID) {
632 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
633 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
634 }
635 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
638 return ret;
639 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100640#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100642#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100643 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200645 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646}
647
Jethro Beekman01672442023-04-19 14:08:14 +0200648#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
649
650/*
651 * Load an RFC8410 EC key, which doesn't have any parameters
652 */
653static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
654 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200655 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200656{
657 if (params->tag != 0 || params->len != 0) {
658 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
659 }
660
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200661 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200662}
663
664/*
665 * Parse an RFC 8410 encoded private EC key
666 *
667 * CurvePrivateKey ::= OCTET STRING
668 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200669static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200670 unsigned char *key, size_t keylen, const unsigned char *end,
671 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
672{
673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
674 size_t len;
675
676 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
677 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
678 }
679
680 if (key + len != end) {
681 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
682 }
683
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200684 /*
685 * Load the private key
686 */
687 ret = pk_ecc_set_key(pk, key, len);
688 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200689 return ret;
690 }
Jethro Beekman01672442023-04-19 14:08:14 +0200691
Valerio Setti4064dbb2023-05-17 15:33:07 +0200692 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
693 * which never contain a public key. As such, derive the public key
694 * unconditionally. */
695 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200696 return ret;
697 }
698
Jethro Beekman01672442023-04-19 14:08:14 +0200699 return 0;
700}
701#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200702
Valerio Setti81d75122023-06-14 14:49:33 +0200703#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706/*
707 * RSAPublicKey ::= SEQUENCE {
708 * modulus INTEGER, -- n
709 * publicExponent INTEGER -- e
710 * }
711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712static int pk_get_rsapubkey(unsigned char **p,
713 const unsigned char *end,
714 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715{
Janos Follath24eed8d2019-11-22 13:21:35 +0000716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717 size_t len;
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
720 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
721 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
722 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (*p + len != end) {
725 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
726 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
727 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100729 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
731 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
732 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
735 NULL, 0, NULL, 0)) != 0) {
736 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
737 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100738
739 *p += len;
740
741 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
743 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
744 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
747 NULL, 0, *p, len)) != 0) {
748 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
749 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100750
751 *p += len;
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (mbedtls_rsa_complete(rsa) != 0 ||
754 mbedtls_rsa_check_pubkey(rsa) != 0) {
755 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000756 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (*p != end) {
759 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
760 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
761 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766
767/* Get a PK algorithm identifier
768 *
769 * AlgorithmIdentifier ::= SEQUENCE {
770 * algorithm OBJECT IDENTIFIER,
771 * parameters ANY DEFINED BY algorithm OPTIONAL }
772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773static int pk_get_pk_alg(unsigned char **p,
774 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200775 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
776 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777{
Janos Follath24eed8d2019-11-22 13:21:35 +0000778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
785 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786
Jethro Beekman01672442023-04-19 14:08:14 +0200787 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200788#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200789 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
790 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
791 if (ret == 0) {
792 *pk_alg = MBEDTLS_PK_ECKEY;
793 }
794 }
795#else
796 (void) ec_grp_id;
797#endif
798 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801
802 /*
803 * No parameters with RSA (only for EC)
804 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (*pk_alg == MBEDTLS_PK_RSA &&
806 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
807 params->len != 0)) {
808 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200809 }
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812}
813
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200814/* Helper for Montgomery curves */
815#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
816#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
817 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
818#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
819
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820/*
821 * SubjectPublicKeyInfo ::= SEQUENCE {
822 * algorithm AlgorithmIdentifier,
823 * subjectPublicKey BIT STRING }
824 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
826 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827{
Janos Follath24eed8d2019-11-22 13:21:35 +0000828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200829 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_asn1_buf alg_params;
831 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200832 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
836 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
837 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838 }
839
840 end = *p + len;
841
Jethro Beekman01672442023-04-19 14:08:14 +0200842 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 return ret;
844 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
848 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (*p + len != end) {
851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
852 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
853 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
856 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
857 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
860 return ret;
861 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (pk_alg == MBEDTLS_PK_RSA) {
865 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200868#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200870#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200871 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200872 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200873 } else
874#endif
875 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200876 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200877 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200879 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200882#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (ret == 0 && *p != end) {
886 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
887 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
888 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (ret != 0) {
891 mbedtls_pk_free(pk);
892 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895}
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100899 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
900 *
901 * The value zero is:
902 * - never a valid value for an RSA parameter
903 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
904 *
905 * Since values can't be omitted in PKCS#1, passing a zero value to
906 * rsa_complete() would be incorrect, so reject zero values early.
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908static int asn1_get_nonzero_mpi(unsigned char **p,
909 const unsigned char *end,
910 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100911{
912 int ret;
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 ret = mbedtls_asn1_get_mpi(p, end, X);
915 if (ret != 0) {
916 return ret;
917 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
920 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
921 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100924}
925
926/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927 * Parse a PKCS#1 encoded private RSA key
928 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
930 const unsigned char *key,
931 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100933 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934 size_t len;
935 unsigned char *p, *end;
936
Hanno Beckerefa14e82017-10-11 19:45:19 +0100937 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100939
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 p = (unsigned char *) key;
941 end = p + keylen;
942
943 /*
944 * This function parses the RSAPrivateKey (PKCS#1)
945 *
946 * RSAPrivateKey ::= SEQUENCE {
947 * version Version,
948 * modulus INTEGER, -- n
949 * publicExponent INTEGER, -- e
950 * privateExponent INTEGER, -- d
951 * prime1 INTEGER, -- p
952 * prime2 INTEGER, -- q
953 * exponent1 INTEGER, -- d mod (p-1)
954 * exponent2 INTEGER, -- d mod (q-1)
955 * coefficient INTEGER, -- (inverse of q) mod p
956 * otherPrimeInfos OtherPrimeInfos OPTIONAL
957 * }
958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
960 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
961 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962 }
963
964 end = p + len;
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
967 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968 }
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (version != 0) {
971 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972 }
973
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100974 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
976 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
977 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100978 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100981 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
983 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
984 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100985 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987
988 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
990 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
991 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100992 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100994
995 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
997 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
998 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100999 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001001
1002 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1004 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1005 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001006 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001008
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001009#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001010 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1012 * that they can be easily recomputed from D, P and Q. However by
1013 * parsing them from the PKCS1 structure it is possible to avoid
1014 * recalculating them which both reduces the overhead of loading
1015 * RSA private keys into memory and also avoids side channels which
1016 * can arise when computing those values, since all of D, P, and Q
1017 * are secret. See https://eprint.iacr.org/2020/055 for a
1018 * description of one such attack.
1019 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001020
Jack Lloyd80cc8112020-01-22 17:34:29 -05001021 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1023 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1024 goto cleanup;
1025 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001026
1027 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1029 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1030 goto cleanup;
1031 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001032
1033 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1035 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1036 goto cleanup;
1037 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001038
Jack Lloyd60239752020-01-27 17:53:36 -05001039#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001040 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1042 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1043 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1044 goto cleanup;
1045 }
Jack Lloyd60239752020-01-27 17:53:36 -05001046#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001047
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001048 /* rsa_complete() doesn't complete anything with the default
1049 * implementation but is still called:
1050 * - for the benefit of alternative implementation that may want to
1051 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1052 * - as is also sanity-checks the key
1053 *
1054 * Furthermore, we also check the public part for consistency with
1055 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1058 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001060 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (p != end) {
1063 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1064 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 }
1066
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001067cleanup:
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001072 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((ret & 0xff80) == 0) {
1074 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1075 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001076 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085
Valerio Setti81d75122023-06-14 14:49:33 +02001086#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087/*
1088 * Parse a SEC1 encoded private EC key
1089 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001090static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 const unsigned char *key, size_t keylen,
1092 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093{
Janos Follath24eed8d2019-11-22 13:21:35 +00001094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001095 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001096 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001097 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001099 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 unsigned char *end = p + keylen;
1101 unsigned char *end2;
1102
1103 /*
1104 * RFC 5915, or SEC1 Appendix C.4
1105 *
1106 * ECPrivateKey ::= SEQUENCE {
1107 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1108 * privateKey OCTET STRING,
1109 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1110 * publicKey [1] BIT STRING OPTIONAL
1111 * }
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1114 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1115 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 }
1117
1118 end = p + len;
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1121 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1122 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (version != 1) {
1125 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1126 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1129 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1130 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
Valerio Setti6b062ee2023-06-30 17:32:57 +02001132 /* Keep a reference to the position fo the private key. It will be used
1133 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001134 d = p;
1135 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001136
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 p += len;
1138
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001139 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001141 /*
1142 * Is 'parameters' present?
1143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1145 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1146 0)) == 0) {
1147 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001148 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001153 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001154 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001155
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001156 /*
1157 * Load the private key
1158 */
1159 ret = pk_ecc_set_key(pk, d, d_len);
1160 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001161 return ret;
1162 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001165 /*
1166 * Is 'publickey' present? If not, or if we can't read it (eg because it
1167 * is compressed), create it from the private key.
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1170 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1171 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001172 end2 = p + len;
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1176 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (p + len != end2) {
1179 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1180 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1181 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001182
Valerio Setti4064dbb2023-05-17 15:33:07 +02001183 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001184 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001186 /*
1187 * The only acceptable failure mode of pk_get_ecpubkey() above
1188 * is if the point format is not recognized.
1189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1191 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1192 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001193 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001198
Valerio Setti34f67552023-04-03 15:19:18 +02001199 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001200 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001201 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001202 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206}
Valerio Setti81d75122023-06-14 14:49:33 +02001207#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001209/***********************************************************************
1210 *
1211 * PKCS#8 parsing functions
1212 *
1213 **********************************************************************/
1214
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215/*
1216 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001217 *
1218 * Notes:
1219 *
1220 * - This function does not own the key buffer. It is the
1221 * responsibility of the caller to take care of zeroizing
1222 * and freeing it after use.
1223 *
1224 * - The function is responsible for freeing the provided
1225 * PK context on failure.
1226 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 */
1228static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 mbedtls_pk_context *pk,
1230 const unsigned char *key, size_t keylen,
1231 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232{
1233 int ret, version;
1234 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236 unsigned char *p = (unsigned char *) key;
1237 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001239 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241
Valerio Setti81d75122023-06-14 14:49:33 +02001242#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001243 (void) f_rng;
1244 (void) p_rng;
1245#endif
1246
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001248 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 *
1250 * PrivateKeyInfo ::= SEQUENCE {
1251 * version Version,
1252 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1253 * privateKey PrivateKey,
1254 * attributes [0] IMPLICIT Attributes OPTIONAL }
1255 *
1256 * Version ::= INTEGER
1257 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1258 * PrivateKey ::= OCTET STRING
1259 *
1260 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1261 */
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1264 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1265 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 }
1267
1268 end = p + len;
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1271 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001272 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (version != 0) {
1275 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1276 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277
Jethro Beekman01672442023-04-19 14:08:14 +02001278 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return ret;
1280 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1284 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (len < 1) {
1287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1288 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1289 }
1290
1291 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1292 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1293 }
1294
1295 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1296 return ret;
1297 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (pk_alg == MBEDTLS_PK_RSA) {
1301 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1302 mbedtls_pk_free(pk);
1303 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 }
1305 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001307#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001309#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001310 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001311 if ((ret =
1312 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001313 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001314 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001315 p_rng)) != 0) {
1316 mbedtls_pk_free(pk);
1317 return ret;
1318 }
1319 } else
1320#endif
1321 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001322 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1323 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001324 mbedtls_pk_free(pk);
1325 return ret;
1326 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 }
1328 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001329#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001332 end = p + len;
1333 if (end != (key + keylen)) {
1334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1335 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1336 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339}
1340
1341/*
1342 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001343 *
1344 * To save space, the decryption happens in-place on the given key buffer.
1345 * Also, while this function may modify the keybuffer, it doesn't own it,
1346 * and instead it is the responsibility of the caller to zeroize and properly
1347 * free it after use.
1348 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001351MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_pk_context *pk,
1353 unsigned char *key, size_t keylen,
1354 const unsigned char *pwd, size_t pwdlen,
1355 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001357 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001359 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1362#if defined(MBEDTLS_PKCS12_C)
1363 mbedtls_cipher_type_t cipher_alg;
1364 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001366 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
Hanno Becker2aa80a72017-09-07 15:28:45 +01001368 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 end = p + keylen;
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (pwdlen == 0) {
1372 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1373 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
1375 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001376 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 *
1378 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1379 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1380 * encryptedData EncryptedData
1381 * }
1382 *
1383 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1384 *
1385 * EncryptedData ::= OCTET STRING
1386 *
1387 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001388 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1391 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1392 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 }
1394
1395 end = p + len;
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1399 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1403 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Hanno Beckerfab35692017-08-25 13:38:26 +01001405 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406
1407 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001408 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001412 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001413 cipher_alg, md_alg,
1414 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1416 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1417 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001421
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001422 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_PKCS12_C */
1425#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001427 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1428 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1430 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1431 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001435
1436 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001439 {
1440 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001441 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (decrypted == 0) {
1444 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1445 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001446 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001450/***********************************************************************
1451 *
1452 * Top-level functions, with format auto-discovery
1453 *
1454 **********************************************************************/
1455
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456/*
1457 * Parse a private key
1458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1460 const unsigned char *key, size_t keylen,
1461 const unsigned char *pwd, size_t pwdlen,
1462 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463{
Janos Follath24eed8d2019-11-22 13:21:35 +00001464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001469#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (keylen == 0) {
1472 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1473 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001474
1475#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001479 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001481 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 } else {
1483 ret = mbedtls_pem_read_buffer(&pem,
1484 "-----BEGIN RSA PRIVATE KEY-----",
1485 "-----END RSA PRIVATE KEY-----",
1486 key, pwd, pwdlen, &len);
1487 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (ret == 0) {
1490 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1491 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1492 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1493 pem.buf, pem.buflen)) != 0) {
1494 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495 }
1496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 mbedtls_pem_free(&pem);
1498 return ret;
1499 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1500 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1501 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1502 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1503 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1504 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507
Valerio Setti81d75122023-06-14 14:49:33 +02001508#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001509 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001511 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 } else {
1513 ret = mbedtls_pem_read_buffer(&pem,
1514 "-----BEGIN EC PRIVATE KEY-----",
1515 "-----END EC PRIVATE KEY-----",
1516 key, pwd, pwdlen, &len);
1517 }
1518 if (ret == 0) {
1519 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001522 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 pem.buf, pem.buflen,
1524 f_rng, p_rng)) != 0) {
1525 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526 }
1527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 mbedtls_pem_free(&pem);
1529 return ret;
1530 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1531 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1532 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1533 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1534 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1535 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536 }
Valerio Setti81d75122023-06-14 14:49:33 +02001537#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001539 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001541 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 } else {
1543 ret = mbedtls_pem_read_buffer(&pem,
1544 "-----BEGIN PRIVATE KEY-----",
1545 "-----END PRIVATE KEY-----",
1546 key, NULL, 0, &len);
1547 }
1548 if (ret == 0) {
1549 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1550 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1551 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552 }
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 mbedtls_pem_free(&pem);
1555 return ret;
1556 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1557 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001561 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001563 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 } else {
1565 ret = mbedtls_pem_read_buffer(&pem,
1566 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1567 "-----END ENCRYPTED PRIVATE KEY-----",
1568 key, NULL, 0, &len);
1569 }
1570 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001571 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1572 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574 }
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 mbedtls_pem_free(&pem);
1577 return ret;
1578 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1579 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582#else
1583 ((void) pwd);
1584 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586
1587 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001588 * At this point we only know it's not a PEM formatted key. Could be any
1589 * of the known DER encoded private key formats
1590 *
1591 * We try the different DER format parsers to see if one passes without
1592 * error
1593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001596 unsigned char *key_copy;
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1599 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1600 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001603
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001604 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1605 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001606
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001607 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608 }
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (ret == 0) {
1611 return 0;
1612 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_pk_free(pk);
1615 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1618 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1623 if (ret == 0) {
1624 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001625 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 mbedtls_pk_free(pk);
1628 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1633 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1634 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1635 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636 }
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_pk_free(pk);
1639 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Valerio Setti81d75122023-06-14 14:49:33 +02001642#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1644 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001645 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 key, keylen, f_rng, p_rng) == 0) {
1647 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001650#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651
Valerio Setti81d75122023-06-14 14:49:33 +02001652 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001653 * it is ok to leave the PK context initialized but not
1654 * freed: It is the caller's responsibility to call pk_init()
1655 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001656 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001657 * isn't, this leads to mbedtls_pk_free() being called
1658 * twice, once here and once by the caller, but this is
1659 * also ok and in line with the mbedtls_pk_free() calls
1660 * on failed PEM parsing attempts. */
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001663}
1664
1665/*
1666 * Parse a public key
1667 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1669 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001670{
Janos Follath24eed8d2019-11-22 13:21:35 +00001671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001672 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001673#if defined(MBEDTLS_RSA_C)
1674 const mbedtls_pk_info_t *pk_info;
1675#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001677 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001679#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (keylen == 0) {
1682 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1683 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001684
1685#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001687#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001688 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001690 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 } else {
1692 ret = mbedtls_pem_read_buffer(&pem,
1693 "-----BEGIN RSA PUBLIC KEY-----",
1694 "-----END RSA PUBLIC KEY-----",
1695 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001696 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001697
1698 if (ret == 0) {
1699 p = pem.buf;
1700 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1701 mbedtls_pem_free(&pem);
1702 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1703 }
1704
1705 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1706 mbedtls_pem_free(&pem);
1707 return ret;
1708 }
1709
1710 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1711 mbedtls_pk_free(ctx);
1712 }
1713
1714 mbedtls_pem_free(&pem);
1715 return ret;
1716 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1717 mbedtls_pem_free(&pem);
1718 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001719 }
1720#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001721
1722 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001724 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 } else {
1726 ret = mbedtls_pem_read_buffer(&pem,
1727 "-----BEGIN PUBLIC KEY-----",
1728 "-----END PUBLIC KEY-----",
1729 key, NULL, 0, &len);
1730 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733 /*
1734 * Was PEM encoded
1735 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001736 p = pem.buf;
1737
Jethro Beekman01672442023-04-19 14:08:14 +02001738 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 mbedtls_pem_free(&pem);
1740 return ret;
1741 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1742 mbedtls_pem_free(&pem);
1743 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001747
1748#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1750 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001751 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001752
1753 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1754 return ret;
1755 }
1756
1757 p = (unsigned char *) key;
1758 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1759 if (ret == 0) {
1760 return ret;
1761 }
1762 mbedtls_pk_free(ctx);
1763 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1764 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1765 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001766 }
1767#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001768 p = (unsigned char *) key;
1769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001773}
1774
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001775/***********************************************************************
1776 *
1777 * Top-level functions, with filesystem support
1778 *
1779 **********************************************************************/
1780
1781#if defined(MBEDTLS_FS_IO)
1782/*
1783 * Load all data from a file into a given buffer.
1784 *
1785 * The file is expected to contain either PEM or DER encoded data.
1786 * A terminating null byte is always appended. It is included in the announced
1787 * length only if the data looks like it is PEM encoded.
1788 */
1789int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1790{
1791 FILE *f;
1792 long size;
1793
1794 if ((f = fopen(path, "rb")) == NULL) {
1795 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1796 }
1797
1798 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1799 mbedtls_setbuf(f, NULL);
1800
1801 fseek(f, 0, SEEK_END);
1802 if ((size = ftell(f)) == -1) {
1803 fclose(f);
1804 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1805 }
1806 fseek(f, 0, SEEK_SET);
1807
1808 *n = (size_t) size;
1809
1810 if (*n + 1 == 0 ||
1811 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1812 fclose(f);
1813 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1814 }
1815
1816 if (fread(*buf, 1, *n, f) != *n) {
1817 fclose(f);
1818
1819 mbedtls_zeroize_and_free(*buf, *n);
1820
1821 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1822 }
1823
1824 fclose(f);
1825
1826 (*buf)[*n] = '\0';
1827
1828 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1829 ++*n;
1830 }
1831
1832 return 0;
1833}
1834
1835/*
1836 * Load and parse a private key
1837 */
1838int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1839 const char *path, const char *pwd,
1840 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1841{
1842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1843 size_t n;
1844 unsigned char *buf;
1845
1846 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1847 return ret;
1848 }
1849
1850 if (pwd == NULL) {
1851 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1852 } else {
1853 ret = mbedtls_pk_parse_key(ctx, buf, n,
1854 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1855 }
1856
1857 mbedtls_zeroize_and_free(buf, n);
1858
1859 return ret;
1860}
1861
1862/*
1863 * Load and parse a public key
1864 */
1865int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1866{
1867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1868 size_t n;
1869 unsigned char *buf;
1870
1871 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1872 return ret;
1873 }
1874
1875 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1876
1877 mbedtls_zeroize_and_free(buf, n);
1878
1879 return ret;
1880}
1881#endif /* MBEDTLS_FS_IO */
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_PK_PARSE_C */