blob: 72eed097f1f486396b7caca567fad7ce04d23435 [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
Valerio Settie0e63112023-05-18 18:48:07 +020058/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020059#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020060#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
61 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020062#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020063
Gilles Peskine832f3492017-11-30 11:42:12 +010064#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020065/*
66 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020067 *
68 * The file is expected to contain either PEM or DER encoded data.
69 * A terminating null byte is always appended. It is included in the announced
70 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073{
74 FILE *f;
75 long size;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((f = fopen(path, "rb")) == NULL) {
78 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
79 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskineda0913b2022-06-30 17:03:40 +020081 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 fseek(f, 0, SEEK_END);
85 if ((size = ftell(f)) == -1) {
86 fclose(f);
87 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 }
Gilles Peskine449bd832023-01-11 14:50:10 +010089 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
91 *n = (size_t) size;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (*n + 1 == 0 ||
94 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
95 fclose(f);
96 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (fread(*buf, 1, *n, f) != *n) {
100 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100102 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
109 (*buf)[*n] = '\0';
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117
118/*
119 * Load and parse a private key
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
122 const char *path, const char *pwd,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 size_t n;
127 unsigned char *buf;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
130 return ret;
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (pwd == NULL) {
134 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
135 } else {
136 ret = mbedtls_pk_parse_key(ctx, buf, n,
137 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100140 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143}
144
145/*
146 * Load and parse a public key
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 size_t n;
152 unsigned char *buf;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
155 return ret;
156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100160 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Valerio Setti81d75122023-06-14 14:49:33 +0200166#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (end - *p < 1) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
183 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100190#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 params->p = *p;
201 *p += params->len;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (*p != end) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200235 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
243 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ver < 1 || ver > 3) {
246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
257 return ret;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 end_field = p + len;
261
262 /*
263 * FIELD-ID ::= TYPE-IDENTIFIER
264 * FieldTypes FIELD-ID ::= {
265 * { Prime-p IDENTIFIED BY prime-field } |
266 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
267 * }
268 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
271 return ret;
272 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
275 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
276 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 }
278
279 p += len;
280
281 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (p != end_field) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
291 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /*
294 * Curve ::= SEQUENCE {
295 * a FieldElement,
296 * b FieldElement,
297 * seed BIT STRING OPTIONAL
298 * -- Shall be present if used in SpecifiedECDomain
299 * -- with version equal to ecdpVer2 or ecdpVer3
300 * }
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
304 return ret;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
314 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316 }
317
318 p += len;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
321 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (p != end_curve) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
342 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
345 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
351 (p[0] != 0x02 && p[0] != 0x03) ||
352 len != mbedtls_mpi_size(&grp->P) + 1 ||
353 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
354 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
356 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 }
358 }
359
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 p += len;
361
362 /*
363 * order INTEGER
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
367 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static 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 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_free(&ref);
393 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
395 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
397 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
398 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 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 +0100405 break;
406 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 }
408
409cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411
412 *grp_id = *id;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419}
420
421/*
422 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
425 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426{
Janos Follath24eed8d2019-11-22 13:21:35 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000439 /* The API respecting lifecycle for mbedtls_ecp_group struct is
440 * _init(), _load() and _free(). In pk_group_id_from_specified() the
441 * temporary grp breaks that flow and it's members are populated
442 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
443 * which is assuming a group populated by _setup() may not clean-up
444 * properly -> Manually free it's members.
445 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000446 mbedtls_mpi_free(&grp.N);
447 mbedtls_mpi_free(&grp.P);
448 mbedtls_mpi_free(&grp.A);
449 mbedtls_mpi_free(&grp.B);
450 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200456/*
457 * Set the group used by this key.
458 */
459static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200460{
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200461#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
462 size_t ec_bits;
463 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200464
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200465 /* group may already be initialized; if so, make sure IDs match */
466 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
467 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200468 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
469 }
470
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200471 /* set group */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200472 pk->ec_family = ec_family;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200473 pk->ec_bits = ec_bits;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200474
475 return 0;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200476#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
477 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
478
479 /* grp may already be initialized; if so, make sure IDs match */
480 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
481 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
482 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
483 }
484
485 /* set group */
486 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200487#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200488}
Valerio Setti4064dbb2023-05-17 15:33:07 +0200489
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490/*
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200491 * Set the private key material
492 *
493 * Must have already set the group with pk_ecc_set_group().
494 *
495 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
496 */
497static int pk_ecc_set_key(mbedtls_pk_context *pk,
498 unsigned char *key, size_t len)
499{
500#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
502 psa_status_t status;
503
504 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
505 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
506 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200507 /* Montgomery allows only ECDH, others ECDSA too */
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200508 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
509 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200510 psa_set_key_enrollment_algorithm(&attributes,
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200511 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200512 }
513 psa_set_key_usage_flags(&attributes, flags);
514
515 status = psa_import_key(&attributes, key, len, &pk->priv_id);
516 return psa_pk_status_to_mbedtls(status);
517
518#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
519
520 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
521 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
522 if (ret != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
524 }
525 return 0;
526#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
527}
528
529/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100531 *
532 * ECParameters ::= CHOICE {
533 * namedCurve OBJECT IDENTIFIER
534 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
535 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200537static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538{
Janos Follath24eed8d2019-11-22 13:21:35 +0000539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (params->tag == MBEDTLS_ASN1_OID) {
543 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
544 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
545 }
546 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
549 return ret;
550 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100551#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100553#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100554 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200556 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557}
558
Jethro Beekman01672442023-04-19 14:08:14 +0200559/*
560 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200561 *
562 * Note: the private key information is always available from pk,
563 * however for convenience the serialized version is also passed,
564 * as it's available at each calling site, and useful in some configs
565 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200566 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200567static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200568 const unsigned char *d, size_t d_len,
569 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
570{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200571#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200572 psa_status_t status;
573 (void) f_rng;
574 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200575 (void) d;
576 (void) d_len;
577
578 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
579 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200580 return psa_pk_status_to_mbedtls(status);
581#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
582 int ret;
583 psa_status_t status;
584 (void) f_rng;
585 (void) p_rng;
586
Valerio Setti00e8dd12023-05-18 18:56:59 +0200587 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
588 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
589 size_t key_len;
590 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200591 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
592 size_t curve_bits;
593 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200594 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200595
596 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
597 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
598
599 status = psa_import_key(&key_attr, d, d_len, &key_id);
600 ret = psa_pk_status_to_mbedtls(status);
601 if (ret != 0) {
602 return ret;
603 }
604
Jethro Beekman01672442023-04-19 14:08:14 +0200605 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
606 ret = psa_pk_status_to_mbedtls(status);
607 destruction_status = psa_destroy_key(key_id);
608 if (ret != 0) {
609 return ret;
610 } else if (destruction_status != PSA_SUCCESS) {
611 return psa_pk_status_to_mbedtls(destruction_status);
612 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200613 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200614#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200615 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200616 (void) d;
617 (void) d_len;
618
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200619 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200621}
622
623#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
624
625/*
626 * Load an RFC8410 EC key, which doesn't have any parameters
627 */
628static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
629 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200630 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200631{
632 if (params->tag != 0 || params->len != 0) {
633 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
634 }
635
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200636 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200637}
638
639/*
640 * Parse an RFC 8410 encoded private EC key
641 *
642 * CurvePrivateKey ::= OCTET STRING
643 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200644static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200645 unsigned char *key, size_t keylen, const unsigned char *end,
646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
647{
648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
649 size_t len;
650
651 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
652 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
653 }
654
655 if (key + len != end) {
656 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
657 }
658
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659 /*
660 * Load the private key
661 */
662 ret = pk_ecc_set_key(pk, key, len);
663 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200664 return ret;
665 }
Jethro Beekman01672442023-04-19 14:08:14 +0200666
Valerio Setti4064dbb2023-05-17 15:33:07 +0200667 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
668 * which never contain a public key. As such, derive the public key
669 * unconditionally. */
670 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200671 return ret;
672 }
673
Jethro Beekman01672442023-04-19 14:08:14 +0200674 return 0;
675}
676#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200677
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200678#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200679/*
680 * Create a temporary ecp_keypair for converting an EC point in compressed
681 * format to an uncompressed one
682 */
683static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
684 const unsigned char *in_start, size_t in_len,
685 size_t *out_buf_len, unsigned char *out_buf,
686 size_t out_buf_size)
687{
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200688#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200689 mbedtls_ecp_keypair ecp_key;
690 mbedtls_ecp_group_id ecp_group_id;
691 int ret;
692
693 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
694
695 mbedtls_ecp_keypair_init(&ecp_key);
696 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
697 if (ret != 0) {
698 return ret;
699 }
700 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
701 in_start, in_len);
702 if (ret != 0) {
703 goto exit;
704 }
705 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
706 MBEDTLS_ECP_PF_UNCOMPRESSED,
707 out_buf_len, out_buf, out_buf_size);
708
709exit:
710 mbedtls_ecp_keypair_free(&ecp_key);
711 return ret;
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200712#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
713 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
714#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200715}
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200716#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200717
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718/*
719 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100720 *
721 * The caller is responsible for clearing the structure upon failure if
722 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727{
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729
Valerio Setti4064dbb2023-05-17 15:33:07 +0200730#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
731 mbedtls_svc_key_id_t key;
732 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
733 size_t len = (end - *p);
734
735 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
736 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 }
738
Valerio Setti4064dbb2023-05-17 15:33:07 +0200739 if ((**p == 0x02) || (**p == 0x03)) {
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200740 /* Compressed format, not supported by PSA Crypto.
741 * Try converting using functions from ECP_LIGHT. */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200742 ret = pk_convert_compressed_ec(pk, *p, len,
743 &(pk->pub_raw_len), pk->pub_raw,
744 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
745 if (ret != 0) {
746 return ret;
747 }
748 } else {
749 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100750 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200751 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200752 }
753 memcpy(pk->pub_raw, *p, (end - *p));
754 pk->pub_raw_len = end - *p;
755 }
756
757 /* Validate the key by trying to importing it */
758 psa_set_key_usage_flags(&key_attrs, 0);
759 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
760 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
761 psa_set_key_bits(&key_attrs, pk->ec_bits);
762
763 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
764 &key) != PSA_SUCCESS) ||
765 (psa_destroy_key(key) != PSA_SUCCESS)) {
766 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
767 pk->pub_raw_len = 0;
768 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
769 }
770 ret = 0;
771#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
772 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
773 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
774 (const unsigned char *) *p,
775 end - *p)) == 0) {
776 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
777 }
778#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
779
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782 */
783 *p = (unsigned char *) end;
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786}
Valerio Setti81d75122023-06-14 14:49:33 +0200787#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790/*
791 * RSAPublicKey ::= SEQUENCE {
792 * modulus INTEGER, -- n
793 * publicExponent INTEGER -- e
794 * }
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static int pk_get_rsapubkey(unsigned char **p,
797 const unsigned char *end,
798 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200799{
Janos Follath24eed8d2019-11-22 13:21:35 +0000800 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801 size_t len;
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
804 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
806 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (*p + len != end) {
809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
810 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100813 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
815 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
816 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
819 NULL, 0, NULL, 0)) != 0) {
820 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
821 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100822
823 *p += len;
824
825 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
827 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
828 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
831 NULL, 0, *p, len)) != 0) {
832 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
835 *p += len;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (mbedtls_rsa_complete(rsa) != 0 ||
838 mbedtls_rsa_check_pubkey(rsa) != 0) {
839 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000840 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (*p != end) {
843 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
844 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
845 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
851/* Get a PK algorithm identifier
852 *
853 * AlgorithmIdentifier ::= SEQUENCE {
854 * algorithm OBJECT IDENTIFIER,
855 * parameters ANY DEFINED BY algorithm OPTIONAL }
856 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857static int pk_get_pk_alg(unsigned char **p,
858 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200859 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
860 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861{
Janos Follath24eed8d2019-11-22 13:21:35 +0000862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
868 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
869 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Jethro Beekman01672442023-04-19 14:08:14 +0200871 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200872#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200873 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
874 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
875 if (ret == 0) {
876 *pk_alg = MBEDTLS_PK_ECKEY;
877 }
878 }
879#else
880 (void) ec_grp_id;
881#endif
882 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
884 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885
886 /*
887 * No parameters with RSA (only for EC)
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (*pk_alg == MBEDTLS_PK_RSA &&
890 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
891 params->len != 0)) {
892 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893 }
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896}
897
898/*
899 * SubjectPublicKeyInfo ::= SEQUENCE {
900 * algorithm AlgorithmIdentifier,
901 * subjectPublicKey BIT STRING }
902 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
904 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905{
Janos Follath24eed8d2019-11-22 13:21:35 +0000906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_asn1_buf alg_params;
909 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200910 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916 }
917
918 end = *p + len;
919
Jethro Beekman01672442023-04-19 14:08:14 +0200920 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 return ret;
922 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
926 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (*p + len != end) {
929 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
930 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
934 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
938 return ret;
939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (pk_alg == MBEDTLS_PK_RSA) {
943 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200946#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200948#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200949 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200950 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200951 } else
952#endif
953 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200954 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200955 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200957 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200959 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200960#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (ret == 0 && *p != end) {
964 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
965 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
966 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret != 0) {
969 mbedtls_pk_free(pk);
970 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973}
974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100977 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
978 *
979 * The value zero is:
980 * - never a valid value for an RSA parameter
981 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
982 *
983 * Since values can't be omitted in PKCS#1, passing a zero value to
984 * rsa_complete() would be incorrect, so reject zero values early.
985 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static int asn1_get_nonzero_mpi(unsigned char **p,
987 const unsigned char *end,
988 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100989{
990 int ret;
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 ret = mbedtls_asn1_get_mpi(p, end, X);
993 if (ret != 0) {
994 return ret;
995 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
998 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
999 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001002}
1003
1004/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 * Parse a PKCS#1 encoded private RSA key
1006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1008 const unsigned char *key,
1009 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001011 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012 size_t len;
1013 unsigned char *p, *end;
1014
Hanno Beckerefa14e82017-10-11 19:45:19 +01001015 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001017
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 p = (unsigned char *) key;
1019 end = p + keylen;
1020
1021 /*
1022 * This function parses the RSAPrivateKey (PKCS#1)
1023 *
1024 * RSAPrivateKey ::= SEQUENCE {
1025 * version Version,
1026 * modulus INTEGER, -- n
1027 * publicExponent INTEGER, -- e
1028 * privateExponent INTEGER, -- d
1029 * prime1 INTEGER, -- p
1030 * prime2 INTEGER, -- q
1031 * exponent1 INTEGER, -- d mod (p-1)
1032 * exponent2 INTEGER, -- d mod (q-1)
1033 * coefficient INTEGER, -- (inverse of q) mod p
1034 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1035 * }
1036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1038 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1039 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 }
1041
1042 end = p + len;
1043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1045 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 }
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (version != 0) {
1049 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 }
1051
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1054 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1055 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1061 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1062 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001063 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001065
1066 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1068 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1069 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001072
1073 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1075 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1076 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001079
1080 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1082 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1083 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001084 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001086
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001087#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001088 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1090 * that they can be easily recomputed from D, P and Q. However by
1091 * parsing them from the PKCS1 structure it is possible to avoid
1092 * recalculating them which both reduces the overhead of loading
1093 * RSA private keys into memory and also avoids side channels which
1094 * can arise when computing those values, since all of D, P, and Q
1095 * are secret. See https://eprint.iacr.org/2020/055 for a
1096 * description of one such attack.
1097 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001098
Jack Lloyd80cc8112020-01-22 17:34:29 -05001099 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1101 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1102 goto cleanup;
1103 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001104
1105 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1107 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1108 goto cleanup;
1109 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001110
1111 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1113 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1114 goto cleanup;
1115 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001116
Jack Lloyd60239752020-01-27 17:53:36 -05001117#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001118 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1120 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1121 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1122 goto cleanup;
1123 }
Jack Lloyd60239752020-01-27 17:53:36 -05001124#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001125
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001126 /* rsa_complete() doesn't complete anything with the default
1127 * implementation but is still called:
1128 * - for the benefit of alternative implementation that may want to
1129 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1130 * - as is also sanity-checks the key
1131 *
1132 * Furthermore, we also check the public part for consistency with
1133 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1136 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001137 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001138 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (p != end) {
1141 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1142 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 }
1144
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001145cleanup:
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001150 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if ((ret & 0xff80) == 0) {
1152 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1153 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001154 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
Valerio Setti81d75122023-06-14 14:49:33 +02001164#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165/*
1166 * Parse a SEC1 encoded private EC key
1167 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001168static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 const unsigned char *key, size_t keylen,
1170 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171{
Janos Follath24eed8d2019-11-22 13:21:35 +00001172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001173 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001174 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001175 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001177 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 unsigned char *end = p + keylen;
1179 unsigned char *end2;
1180
1181 /*
1182 * RFC 5915, or SEC1 Appendix C.4
1183 *
1184 * ECPrivateKey ::= SEQUENCE {
1185 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1186 * privateKey OCTET STRING,
1187 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1188 * publicKey [1] BIT STRING OPTIONAL
1189 * }
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1192 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1193 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 }
1195
1196 end = p + len;
1197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (version != 1) {
1203 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209
Valerio Setti6b062ee2023-06-30 17:32:57 +02001210 /* Keep a reference to the position fo the private key. It will be used
1211 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001212 d = p;
1213 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001214
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 p += len;
1216
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001217 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001219 /*
1220 * Is 'parameters' present?
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1223 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1224 0)) == 0) {
1225 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001226 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001231 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001232 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001234 /*
1235 * Load the private key
1236 */
1237 ret = pk_ecc_set_key(pk, d, d_len);
1238 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001239 return ret;
1240 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243 /*
1244 * Is 'publickey' present? If not, or if we can't read it (eg because it
1245 * is compressed), create it from the private key.
1246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1248 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1249 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001250 end2 = p + len;
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1254 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (p + len != end2) {
1257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1258 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1259 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001260
Valerio Setti4064dbb2023-05-17 15:33:07 +02001261 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001262 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264 /*
1265 * The only acceptable failure mode of pk_get_ecpubkey() above
1266 * is if the point format is not recognized.
1267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1269 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1270 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001271 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001274 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001276
Valerio Setti34f67552023-04-03 15:19:18 +02001277 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001278 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001279 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001280 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001281 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284}
Valerio Setti81d75122023-06-14 14:49:33 +02001285#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
1287/*
1288 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001289 *
1290 * Notes:
1291 *
1292 * - This function does not own the key buffer. It is the
1293 * responsibility of the caller to take care of zeroizing
1294 * and freeing it after use.
1295 *
1296 * - The function is responsible for freeing the provided
1297 * PK context on failure.
1298 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 */
1300static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_pk_context *pk,
1302 const unsigned char *key, size_t keylen,
1303 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304{
1305 int ret, version;
1306 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 unsigned char *p = (unsigned char *) key;
1309 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001311 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313
Valerio Setti81d75122023-06-14 14:49:33 +02001314#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001315 (void) f_rng;
1316 (void) p_rng;
1317#endif
1318
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001320 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 *
1322 * PrivateKeyInfo ::= SEQUENCE {
1323 * version Version,
1324 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1325 * privateKey PrivateKey,
1326 * attributes [0] IMPLICIT Attributes OPTIONAL }
1327 *
1328 * Version ::= INTEGER
1329 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1330 * PrivateKey ::= OCTET STRING
1331 *
1332 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1333 */
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 }
1339
1340 end = p + len;
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (version != 0) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Jethro Beekman01672442023-04-19 14:08:14 +02001350 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return ret;
1352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (len < 1) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1360 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1361 }
1362
1363 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1364 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1365 }
1366
1367 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1368 return ret;
1369 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (pk_alg == MBEDTLS_PK_RSA) {
1373 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1374 mbedtls_pk_free(pk);
1375 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 }
1377 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001379#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001381#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001382 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001383 if ((ret =
1384 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001385 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001386 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001387 p_rng)) != 0) {
1388 mbedtls_pk_free(pk);
1389 return ret;
1390 }
1391 } else
1392#endif
1393 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001394 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1395 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001396 mbedtls_pk_free(pk);
1397 return ret;
1398 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 }
1400 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001401#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001404 end = p + len;
1405 if (end != (key + keylen)) {
1406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1407 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1408 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411}
1412
1413/*
1414 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001415 *
1416 * To save space, the decryption happens in-place on the given key buffer.
1417 * Also, while this function may modify the keybuffer, it doesn't own it,
1418 * and instead it is the responsibility of the caller to zeroize and properly
1419 * free it after use.
1420 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001423MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_pk_context *pk,
1425 unsigned char *key, size_t keylen,
1426 const unsigned char *pwd, size_t pwdlen,
1427 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001429 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001431 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1434#if defined(MBEDTLS_PKCS12_C)
1435 mbedtls_cipher_type_t cipher_alg;
1436 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001438 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Hanno Becker2aa80a72017-09-07 15:28:45 +01001440 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 end = p + keylen;
1442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (pwdlen == 0) {
1444 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1445 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446
1447 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001448 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 *
1450 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1451 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1452 * encryptedData EncryptedData
1453 * }
1454 *
1455 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1456 *
1457 * EncryptedData ::= OCTET STRING
1458 *
1459 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001460 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1463 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1464 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 }
1466
1467 end = p + len;
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1470 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1471 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1474 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476
Hanno Beckerfab35692017-08-25 13:38:26 +01001477 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
1479 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001480 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001484 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001485 cipher_alg, md_alg,
1486 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1488 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1489 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001493
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001494 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_PKCS12_C */
1497#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001499 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1500 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1502 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1503 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001507
1508 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001511 {
1512 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001513 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (decrypted == 0) {
1516 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1517 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001518 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521
1522/*
1523 * Parse a private key
1524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1526 const unsigned char *key, size_t keylen,
1527 const unsigned char *pwd, size_t pwdlen,
1528 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529{
Janos Follath24eed8d2019-11-22 13:21:35 +00001530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001535#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (keylen == 0) {
1538 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1539 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001540
1541#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001547 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 } else {
1549 ret = mbedtls_pem_read_buffer(&pem,
1550 "-----BEGIN RSA PRIVATE KEY-----",
1551 "-----END RSA PRIVATE KEY-----",
1552 key, pwd, pwdlen, &len);
1553 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (ret == 0) {
1556 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1557 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1558 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1559 pem.buf, pem.buflen)) != 0) {
1560 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_pem_free(&pem);
1564 return ret;
1565 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1566 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1567 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1568 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1569 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1570 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573
Valerio Setti81d75122023-06-14 14:49:33 +02001574#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001575 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 } else {
1579 ret = mbedtls_pem_read_buffer(&pem,
1580 "-----BEGIN EC PRIVATE KEY-----",
1581 "-----END EC PRIVATE KEY-----",
1582 key, pwd, pwdlen, &len);
1583 }
1584 if (ret == 0) {
1585 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001588 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 pem.buf, pem.buflen,
1590 f_rng, p_rng)) != 0) {
1591 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592 }
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_pem_free(&pem);
1595 return ret;
1596 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1597 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1598 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1599 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1600 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1601 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602 }
Valerio Setti81d75122023-06-14 14:49:33 +02001603#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001605 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001607 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else {
1609 ret = mbedtls_pem_read_buffer(&pem,
1610 "-----BEGIN PRIVATE KEY-----",
1611 "-----END PRIVATE KEY-----",
1612 key, NULL, 0, &len);
1613 }
1614 if (ret == 0) {
1615 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1616 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1617 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 mbedtls_pem_free(&pem);
1621 return ret;
1622 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1623 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001627 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001629 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 } else {
1631 ret = mbedtls_pem_read_buffer(&pem,
1632 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1633 "-----END ENCRYPTED PRIVATE KEY-----",
1634 key, NULL, 0, &len);
1635 }
1636 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001637 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1638 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640 }
1641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_pem_free(&pem);
1643 return ret;
1644 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1645 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001648#else
1649 ((void) pwd);
1650 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652
1653 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001654 * At this point we only know it's not a PEM formatted key. Could be any
1655 * of the known DER encoded private key formats
1656 *
1657 * We try the different DER format parsers to see if one passes without
1658 * error
1659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001662 unsigned char *key_copy;
1663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1665 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1666 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001669
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001670 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1671 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001672
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001673 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674 }
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (ret == 0) {
1677 return 0;
1678 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 mbedtls_pk_free(pk);
1681 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1684 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1689 if (ret == 0) {
1690 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001691 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_pk_free(pk);
1694 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1699 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1700 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1701 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 mbedtls_pk_free(pk);
1705 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001707
Valerio Setti81d75122023-06-14 14:49:33 +02001708#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1710 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001711 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 key, keylen, f_rng, p_rng) == 0) {
1713 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001716#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717
Valerio Setti81d75122023-06-14 14:49:33 +02001718 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001719 * it is ok to leave the PK context initialized but not
1720 * freed: It is the caller's responsibility to call pk_init()
1721 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001722 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001723 * isn't, this leads to mbedtls_pk_free() being called
1724 * twice, once here and once by the caller, but this is
1725 * also ok and in line with the mbedtls_pk_free() calls
1726 * on failed PEM parsing attempts. */
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001729}
1730
1731/*
1732 * Parse a public key
1733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1735 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001736{
Janos Follath24eed8d2019-11-22 13:21:35 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001738 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001739#if defined(MBEDTLS_RSA_C)
1740 const mbedtls_pk_info_t *pk_info;
1741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001743 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001745#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (keylen == 0) {
1748 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1749 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001750
1751#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001753#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001754 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001756 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 } else {
1758 ret = mbedtls_pem_read_buffer(&pem,
1759 "-----BEGIN RSA PUBLIC KEY-----",
1760 "-----END RSA PUBLIC KEY-----",
1761 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001763
1764 if (ret == 0) {
1765 p = pem.buf;
1766 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1767 mbedtls_pem_free(&pem);
1768 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1769 }
1770
1771 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1772 mbedtls_pem_free(&pem);
1773 return ret;
1774 }
1775
1776 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1777 mbedtls_pk_free(ctx);
1778 }
1779
1780 mbedtls_pem_free(&pem);
1781 return ret;
1782 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1783 mbedtls_pem_free(&pem);
1784 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001785 }
1786#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001787
1788 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001790 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 } else {
1792 ret = mbedtls_pem_read_buffer(&pem,
1793 "-----BEGIN PUBLIC KEY-----",
1794 "-----END PUBLIC KEY-----",
1795 key, NULL, 0, &len);
1796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001799 /*
1800 * Was PEM encoded
1801 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001802 p = pem.buf;
1803
Jethro Beekman01672442023-04-19 14:08:14 +02001804 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 mbedtls_pem_free(&pem);
1806 return ret;
1807 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1808 mbedtls_pem_free(&pem);
1809 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001810 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001813
1814#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1816 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001818
1819 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1820 return ret;
1821 }
1822
1823 p = (unsigned char *) key;
1824 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1825 if (ret == 0) {
1826 return ret;
1827 }
1828 mbedtls_pk_free(ctx);
1829 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1830 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1831 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001832 }
1833#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001834 p = (unsigned char *) key;
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001839}
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_PK_PARSE_C */