blob: b8e3c741efa753778c9118d4946e35f2a5bb0d3f [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
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1a7550a2013-09-15 13:01:22 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pk.h"
13#include "mbedtls/asn1.h"
14#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050015#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000016#include "mbedtls/error.h"
Valerio Setti77a75682023-05-15 11:18:46 +020017#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <string.h>
20
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020023#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/ecp.h"
Valerio Setti81d75122023-06-14 14:49:33 +020025#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti4064dbb2023-05-17 15:33:07 +020026#include "pk_internal.h"
27#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
40
Valerio Setti34f67552023-04-03 15:19:18 +020041#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020042#include "psa_util_internal.h"
Valerio Setti34f67552023-04-03 15:19:18 +020043#endif
44
45#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "psa/crypto.h"
47#endif
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050
Valerio Settie0e63112023-05-18 18:48:07 +020051/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020052#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020053#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
54 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020055#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020056
Gilles Peskine832f3492017-11-30 11:42:12 +010057#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020058/*
59 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020060 *
61 * The file is expected to contain either PEM or DER encoded data.
62 * A terminating null byte is always appended. It is included in the announced
63 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020064 */
Gilles Peskine449bd832023-01-11 14:50:10 +010065int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020066{
67 FILE *f;
68 long size;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if ((f = fopen(path, "rb")) == NULL) {
71 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
72 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020073
Gilles Peskineda0913b2022-06-30 17:03:40 +020074 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 fseek(f, 0, SEEK_END);
78 if ((size = ftell(f)) == -1) {
79 fclose(f);
80 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020081 }
Gilles Peskine449bd832023-01-11 14:50:10 +010082 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020083
84 *n = (size_t) size;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (*n + 1 == 0 ||
87 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
88 fclose(f);
89 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (fread(*buf, 1, *n, f) != *n) {
93 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010094
Tom Cosgroveca8c61b2023-07-17 15:17:40 +010095 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101
102 (*buf)[*n] = '\0';
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200105 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109}
110
111/*
112 * Load and parse a private key
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
115 const char *path, const char *pwd,
116 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117{
Janos Follath24eed8d2019-11-22 13:21:35 +0000118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119 size_t n;
120 unsigned char *buf;
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
123 return ret;
124 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (pwd == NULL) {
127 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
128 } else {
129 ret = mbedtls_pk_parse_key(ctx, buf, n,
130 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100133 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136}
137
138/*
139 * Load and parse a public key
140 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142{
Janos Follath24eed8d2019-11-22 13:21:35 +0000143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 size_t n;
145 unsigned char *buf;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
148 return ret;
149 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100153 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158
Valerio Setti81d75122023-06-14 14:49:33 +0200159#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 *
162 * ECParameters ::= CHOICE {
163 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100164 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 * }
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
169 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170{
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (end - *p < 1) {
174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
175 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
176 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100177
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100178 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100183#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 ) {
185 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
186 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100187 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100191 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200192
193 params->p = *p;
194 *p += params->len;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (*p != end) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
198 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100206 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
207 * WARNING: the resulting group should only be used with
208 * pk_group_id_from_specified(), since its base point may not be set correctly
209 * if it was encoded compressed.
210 *
211 * SpecifiedECDomain ::= SEQUENCE {
212 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
213 * fieldID FieldID {{FieldTypes}},
214 * curve Curve,
215 * base ECPoint,
216 * order INTEGER,
217 * cofactor INTEGER OPTIONAL,
218 * hash HashAlgorithm OPTIONAL,
219 * ...
220 * }
221 *
222 * We only support prime-field as field type, and ignore hash and cofactor.
223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100225{
Janos Follath24eed8d2019-11-22 13:21:35 +0000226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100227 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200228 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100229 const unsigned char *end_field, *end_curve;
230 size_t len;
231 int ver;
232
233 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
235 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
236 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (ver < 1 || ver > 3) {
239 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
240 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100241
242 /*
243 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
244 * fieldType FIELD-ID.&id({IOSet}),
245 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
246 * }
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
249 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
250 return ret;
251 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252
253 end_field = p + len;
254
255 /*
256 * FIELD-ID ::= TYPE-IDENTIFIER
257 * FieldTypes FIELD-ID ::= {
258 * { Prime-p IDENTIFIED BY prime-field } |
259 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
260 * }
261 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
264 return ret;
265 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
268 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
269 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270 }
271
272 p += len;
273
274 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
277 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (p != end_field) {
282 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
283 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
286 /*
287 * Curve ::= SEQUENCE {
288 * a FieldElement,
289 * b FieldElement,
290 * seed BIT STRING OPTIONAL
291 * -- Shall be present if used in SpecifiedECDomain
292 * -- with version equal to ecdpVer2 or ecdpVer3
293 * }
294 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
296 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
297 return ret;
298 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299
300 end_curve = p + len;
301
302 /*
303 * FieldElement ::= OCTET STRING
304 * containing an integer in the case of a prime field
305 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
307 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 }
310
311 p += len;
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->B, 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
320 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (p != end_curve) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
328 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329
330 /*
331 * ECPoint ::= OCTET STRING
332 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
338 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100339 /*
340 * If we can't read the point because it's compressed, cheat by
341 * reading only the X coordinate and the parity bit of Y.
342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
344 (p[0] != 0x02 && p[0] != 0x03) ||
345 len != mbedtls_mpi_size(&grp->P) + 1 ||
346 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
347 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
348 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
349 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100350 }
351 }
352
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100353 p += len;
354
355 /*
356 * order INTEGER
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
360 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100363
364 /*
365 * Allow optional elements by purposefully not enforcing p == end here.
366 */
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100369}
370
371/*
372 * Find the group id associated with an (almost filled) group as generated by
373 * pk_group_from_specified(), or return an error if unknown.
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375static 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 +0100376{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100377 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_ecp_group ref;
379 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100384 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 mbedtls_ecp_group_free(&ref);
386 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
388 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
390 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
391 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
392 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
393 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
394 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
395 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 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 +0100398 break;
399 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400 }
401
402cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
405 *grp_id = *id;
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412}
413
414/*
415 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
418 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419{
Janos Follath24eed8d2019-11-22 13:21:35 +0000420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000432 /* The API respecting lifecycle for mbedtls_ecp_group struct is
433 * _init(), _load() and _free(). In pk_group_id_from_specified() the
434 * temporary grp breaks that flow and it's members are populated
435 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
436 * which is assuming a group populated by _setup() may not clean-up
437 * properly -> Manually free it's members.
438 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000439 mbedtls_mpi_free(&grp.N);
440 mbedtls_mpi_free(&grp.P);
441 mbedtls_mpi_free(&grp.A);
442 mbedtls_mpi_free(&grp.B);
443 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
Valerio Setti4064dbb2023-05-17 15:33:07 +0200449#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
450/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
451 * ecp_keypair structure with proper group ID. The purpose of this helper
452 * function is to update ec_family and ec_bits accordingly. */
453static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
454 mbedtls_ecp_group_id grp_id)
455{
456 psa_ecc_family_t ec_family;
457 size_t bits;
458
459 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
460
461 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
462 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
463 }
464
465 pk->ec_family = ec_family;
466 pk->ec_bits = bits;
467
468 return 0;
469}
470#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
471
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100472/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200473 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474 *
475 * ECParameters ::= CHOICE {
476 * namedCurve OBJECT IDENTIFIER
477 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
478 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200480static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481{
Janos Follath24eed8d2019-11-22 13:21:35 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (params->tag == MBEDTLS_ASN1_OID) {
486 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
487 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
488 }
489 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
492 return ret;
493 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100494#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100496#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498
Valerio Setti00e8dd12023-05-18 18:56:59 +0200499#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
500 ret = pk_update_psa_ecparams(pk, grp_id);
501#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200502 /* grp may already be initialized; if so, make sure IDs match */
503 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
504 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
506 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507
Valerio Setti4064dbb2023-05-17 15:33:07 +0200508 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
509 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return ret;
511 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200512#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513
Valerio Setti4064dbb2023-05-17 15:33:07 +0200514 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515}
516
Jethro Beekman01672442023-04-19 14:08:14 +0200517/*
518 * Helper function for deriving a public key from its private counterpart.
519 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200520static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200521 const unsigned char *d, size_t d_len,
522 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
523{
524 int ret;
525#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200526 psa_status_t status;
527 (void) f_rng;
528 (void) p_rng;
529#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
530 (void) d;
531 (void) d_len;
532
533 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
534 &pk->pub_raw_len);
535 ret = psa_pk_status_to_mbedtls(status);
536#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
537 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
538 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
539 size_t key_len;
540 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200541 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
542 size_t curve_bits;
543 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200544 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200545
546 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
547 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
548
549 status = psa_import_key(&key_attr, d, d_len, &key_id);
550 ret = psa_pk_status_to_mbedtls(status);
551 if (ret != 0) {
552 return ret;
553 }
554
Jethro Beekman01672442023-04-19 14:08:14 +0200555 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
556 ret = psa_pk_status_to_mbedtls(status);
557 destruction_status = psa_destroy_key(key_id);
558 if (ret != 0) {
559 return ret;
560 } else if (destruction_status != PSA_SUCCESS) {
561 return psa_pk_status_to_mbedtls(destruction_status);
562 }
Jethro Beekman01672442023-04-19 14:08:14 +0200563 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200564#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200565#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200566 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200567 (void) d;
568 (void) d_len;
569
570 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
571#endif /* MBEDTLS_USE_PSA_CRYPTO */
572 return ret;
573}
574
575#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
576
577/*
578 * Load an RFC8410 EC key, which doesn't have any parameters
579 */
580static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
581 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200582 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200583{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200584 int ret;
585
Jethro Beekman01672442023-04-19 14:08:14 +0200586 if (params->tag != 0 || params->len != 0) {
587 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
588 }
589
Valerio Setti00e8dd12023-05-18 18:56:59 +0200590#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
591 ret = pk_update_psa_ecparams(pk, grp_id);
592#else
593 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200594 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
595 if (ret != 0) {
596 return ret;
597 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200598#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200599 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200600}
601
602/*
603 * Parse an RFC 8410 encoded private EC key
604 *
605 * CurvePrivateKey ::= OCTET STRING
606 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200607static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200608 unsigned char *key, size_t keylen, const unsigned char *end,
609 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
610{
611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
612 size_t len;
613
614 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
615 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
616 }
617
618 if (key + len != end) {
619 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
620 }
621
Valerio Setti00e8dd12023-05-18 18:56:59 +0200622#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_status_t status;
625
626 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200627 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
628 PSA_KEY_USAGE_DERIVE);
629 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200630
631 status = psa_import_key(&attributes, key, len, &pk->priv_id);
632 if (status != PSA_SUCCESS) {
633 ret = psa_pk_status_to_mbedtls(status);
634 return ret;
635 }
636#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
637 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
638
Valerio Setti805e4a02023-06-30 17:16:19 +0200639 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200640 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
641 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200642#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200643
Valerio Setti4064dbb2023-05-17 15:33:07 +0200644 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
645 * which never contain a public key. As such, derive the public key
646 * unconditionally. */
647 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200648 return ret;
649 }
650
Jethro Beekman01672442023-04-19 14:08:14 +0200651 return 0;
652}
653#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200654
Valerio Settiaddeee42023-06-14 10:46:55 +0200655#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200656/*
657 * Create a temporary ecp_keypair for converting an EC point in compressed
658 * format to an uncompressed one
659 */
660static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
661 const unsigned char *in_start, size_t in_len,
662 size_t *out_buf_len, unsigned char *out_buf,
663 size_t out_buf_size)
664{
665 mbedtls_ecp_keypair ecp_key;
666 mbedtls_ecp_group_id ecp_group_id;
667 int ret;
668
669 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
670
671 mbedtls_ecp_keypair_init(&ecp_key);
672 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
673 if (ret != 0) {
674 return ret;
675 }
676 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
677 in_start, in_len);
678 if (ret != 0) {
679 goto exit;
680 }
681 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
682 MBEDTLS_ECP_PF_UNCOMPRESSED,
683 out_buf_len, out_buf, out_buf_size);
684
685exit:
686 mbedtls_ecp_keypair_free(&ecp_key);
687 return ret;
688}
Valerio Settiaddeee42023-06-14 10:46:55 +0200689#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200690
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691/*
692 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100693 *
694 * The caller is responsible for clearing the structure upon failure if
695 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200699 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700{
Janos Follath24eed8d2019-11-22 13:21:35 +0000701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702
Valerio Setti4064dbb2023-05-17 15:33:07 +0200703#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
704 mbedtls_svc_key_id_t key;
705 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
706 size_t len = (end - *p);
707
708 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
709 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710 }
711
Valerio Setti4064dbb2023-05-17 15:33:07 +0200712 /* Compressed point format are not supported yet by PSA crypto. As a
713 * consequence ecp functions are used to "convert" the point to
714 * uncompressed format */
715 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200716#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200717 ret = pk_convert_compressed_ec(pk, *p, len,
718 &(pk->pub_raw_len), pk->pub_raw,
719 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
720 if (ret != 0) {
721 return ret;
722 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200723#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
724 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
725#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726 } else {
727 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100728 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200729 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200730 }
731 memcpy(pk->pub_raw, *p, (end - *p));
732 pk->pub_raw_len = end - *p;
733 }
734
735 /* Validate the key by trying to importing it */
736 psa_set_key_usage_flags(&key_attrs, 0);
737 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
738 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
739 psa_set_key_bits(&key_attrs, pk->ec_bits);
740
741 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
742 &key) != PSA_SUCCESS) ||
743 (psa_destroy_key(key) != PSA_SUCCESS)) {
744 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
745 pk->pub_raw_len = 0;
746 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
747 }
748 ret = 0;
749#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
750 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
751 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
752 (const unsigned char *) *p,
753 end - *p)) == 0) {
754 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
755 }
756#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
757
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760 */
761 *p = (unsigned char *) end;
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764}
Valerio Setti81d75122023-06-14 14:49:33 +0200765#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768/*
769 * RSAPublicKey ::= SEQUENCE {
770 * modulus INTEGER, -- n
771 * publicExponent INTEGER -- e
772 * }
773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774static int pk_get_rsapubkey(unsigned char **p,
775 const unsigned char *end,
776 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777{
Janos Follath24eed8d2019-11-22 13:21:35 +0000778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779 size_t len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (*p + len != end) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
789 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
794 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
797 NULL, 0, NULL, 0)) != 0) {
798 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
799 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100800
801 *p += len;
802
803 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
806 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
809 NULL, 0, *p, len)) != 0) {
810 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
811 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812
813 *p += len;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (mbedtls_rsa_complete(rsa) != 0 ||
816 mbedtls_rsa_check_pubkey(rsa) != 0) {
817 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000818 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (*p != end) {
821 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
822 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
823 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828
829/* Get a PK algorithm identifier
830 *
831 * AlgorithmIdentifier ::= SEQUENCE {
832 * algorithm OBJECT IDENTIFIER,
833 * parameters ANY DEFINED BY algorithm OPTIONAL }
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static int pk_get_pk_alg(unsigned char **p,
836 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200837 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
838 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839{
Janos Follath24eed8d2019-11-22 13:21:35 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
846 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
847 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848
Jethro Beekman01672442023-04-19 14:08:14 +0200849 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200850#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200851 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
852 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
853 if (ret == 0) {
854 *pk_alg = MBEDTLS_PK_ECKEY;
855 }
856 }
857#else
858 (void) ec_grp_id;
859#endif
860 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
864 /*
865 * No parameters with RSA (only for EC)
866 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (*pk_alg == MBEDTLS_PK_RSA &&
868 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
869 params->len != 0)) {
870 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874}
875
876/*
877 * SubjectPublicKeyInfo ::= SEQUENCE {
878 * algorithm AlgorithmIdentifier,
879 * subjectPublicKey BIT STRING }
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
882 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883{
Janos Follath24eed8d2019-11-22 13:21:35 +0000884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_asn1_buf alg_params;
887 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200888 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
892 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
893 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 }
895
896 end = *p + len;
897
Jethro Beekman01672442023-04-19 14:08:14 +0200898 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return ret;
900 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
904 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (*p + len != end) {
907 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
908 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
909 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
912 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
913 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
916 return ret;
917 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (pk_alg == MBEDTLS_PK_RSA) {
921 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200924#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200926#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200927 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200928 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200929 } else
930#endif
931 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200932 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200933 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200935 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200938#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ret == 0 && *p != end) {
942 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
943 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ret != 0) {
947 mbedtls_pk_free(pk);
948 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951}
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100955 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
956 *
957 * The value zero is:
958 * - never a valid value for an RSA parameter
959 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
960 *
961 * Since values can't be omitted in PKCS#1, passing a zero value to
962 * rsa_complete() would be incorrect, so reject zero values early.
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964static int asn1_get_nonzero_mpi(unsigned char **p,
965 const unsigned char *end,
966 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100967{
968 int ret;
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 ret = mbedtls_asn1_get_mpi(p, end, X);
971 if (ret != 0) {
972 return ret;
973 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
976 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
977 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100980}
981
982/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983 * Parse a PKCS#1 encoded private RSA key
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
986 const unsigned char *key,
987 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100989 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990 size_t len;
991 unsigned char *p, *end;
992
Hanno Beckerefa14e82017-10-11 19:45:19 +0100993 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100995
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 p = (unsigned char *) key;
997 end = p + keylen;
998
999 /*
1000 * This function parses the RSAPrivateKey (PKCS#1)
1001 *
1002 * RSAPrivateKey ::= SEQUENCE {
1003 * version Version,
1004 * modulus INTEGER, -- n
1005 * publicExponent INTEGER, -- e
1006 * privateExponent INTEGER, -- d
1007 * prime1 INTEGER, -- p
1008 * prime2 INTEGER, -- q
1009 * exponent1 INTEGER, -- d mod (p-1)
1010 * exponent2 INTEGER, -- d mod (q-1)
1011 * coefficient INTEGER, -- (inverse of q) mod p
1012 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1013 * }
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1016 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1017 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 }
1019
1020 end = p + len;
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1023 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 }
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (version != 0) {
1027 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028 }
1029
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001030 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1032 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1033 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001034 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1039 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1040 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001043
1044 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1046 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1047 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050
1051 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1053 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1054 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057
1058 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1060 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1061 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001062 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001065#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001066 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1068 * that they can be easily recomputed from D, P and Q. However by
1069 * parsing them from the PKCS1 structure it is possible to avoid
1070 * recalculating them which both reduces the overhead of loading
1071 * RSA private keys into memory and also avoids side channels which
1072 * can arise when computing those values, since all of D, P, and Q
1073 * are secret. See https://eprint.iacr.org/2020/055 for a
1074 * description of one such attack.
1075 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001076
Jack Lloyd80cc8112020-01-22 17:34:29 -05001077 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1079 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1080 goto cleanup;
1081 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001082
1083 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1085 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1086 goto cleanup;
1087 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001088
1089 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1091 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1092 goto cleanup;
1093 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001094
Jack Lloyd60239752020-01-27 17:53:36 -05001095#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001096 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1098 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1099 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1100 goto cleanup;
1101 }
Jack Lloyd60239752020-01-27 17:53:36 -05001102#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001103
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001104 /* rsa_complete() doesn't complete anything with the default
1105 * implementation but is still called:
1106 * - for the benefit of alternative implementation that may want to
1107 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1108 * - as is also sanity-checks the key
1109 *
1110 * Furthermore, we also check the public part for consistency with
1111 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1114 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001115 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001116 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (p != end) {
1119 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1120 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001121 }
1122
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001123cleanup:
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001128 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((ret & 0xff80) == 0) {
1130 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1131 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001132 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 }
1137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
Valerio Setti81d75122023-06-14 14:49:33 +02001142#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143/*
1144 * Parse a SEC1 encoded private EC key
1145 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001146static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 const unsigned char *key, size_t keylen,
1148 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149{
Janos Follath24eed8d2019-11-22 13:21:35 +00001150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001151 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001152 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001153 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001155 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 unsigned char *end = p + keylen;
1157 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001158#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1160 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001161#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1162 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001163#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
1165 /*
1166 * RFC 5915, or SEC1 Appendix C.4
1167 *
1168 * ECPrivateKey ::= SEQUENCE {
1169 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1170 * privateKey OCTET STRING,
1171 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1172 * publicKey [1] BIT STRING OPTIONAL
1173 * }
1174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1176 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1177 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 }
1179
1180 end = p + len;
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1184 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (version != 1) {
1187 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1188 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1192 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193
Valerio Setti6b062ee2023-06-30 17:32:57 +02001194 /* Keep a reference to the position fo the private key. It will be used
1195 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001196 d = p;
1197 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001198
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 p += len;
1200
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001203 /*
1204 * Is 'parameters' present?
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1207 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1208 0)) == 0) {
1209 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001210 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001215 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001216 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001217
Valerio Setti6b062ee2023-06-30 17:32:57 +02001218
1219#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1220 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1222 }
1223#endif
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001226 /*
1227 * Is 'publickey' present? If not, or if we can't read it (eg because it
1228 * is compressed), create it from the private key.
1229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1231 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1232 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233 end2 = p + len;
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1236 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1237 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (p + len != end2) {
1240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1241 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1242 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243
Valerio Setti4064dbb2023-05-17 15:33:07 +02001244 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001245 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001247 /*
1248 * The only acceptable failure mode of pk_get_ecpubkey() above
1249 * is if the point format is not recognized.
1250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1252 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1253 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001254 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001257 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001259
Valerio Setti00e8dd12023-05-18 18:56:59 +02001260#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1261 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1262 /* Setting largest masks for usage and key algorithms */
1263 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1264 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001265 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001266#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1267 psa_set_key_algorithm(&attributes,
1268 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1269#else
1270 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1271#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001272 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001273
Valerio Settia541e012023-05-24 14:31:21 +02001274 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001275 if (status != PSA_SUCCESS) {
1276 ret = psa_pk_status_to_mbedtls(status);
1277 return ret;
1278 }
1279#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1280
Valerio Setti34f67552023-04-03 15:19:18 +02001281 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001282 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001283 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001284 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001285 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288}
Valerio Setti81d75122023-06-14 14:49:33 +02001289#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
1291/*
1292 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001293 *
1294 * Notes:
1295 *
1296 * - This function does not own the key buffer. It is the
1297 * responsibility of the caller to take care of zeroizing
1298 * and freeing it after use.
1299 *
1300 * - The function is responsible for freeing the provided
1301 * PK context on failure.
1302 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 */
1304static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_pk_context *pk,
1306 const unsigned char *key, size_t keylen,
1307 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308{
1309 int ret, version;
1310 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 unsigned char *p = (unsigned char *) key;
1313 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001315 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
Valerio Setti81d75122023-06-14 14:49:33 +02001318#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001319 (void) f_rng;
1320 (void) p_rng;
1321#endif
1322
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001324 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 *
1326 * PrivateKeyInfo ::= SEQUENCE {
1327 * version Version,
1328 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1329 * privateKey PrivateKey,
1330 * attributes [0] IMPLICIT Attributes OPTIONAL }
1331 *
1332 * Version ::= INTEGER
1333 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1334 * PrivateKey ::= OCTET STRING
1335 *
1336 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1337 */
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1340 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 }
1343
1344 end = p + len;
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (version != 0) {
1351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Jethro Beekman01672442023-04-19 14:08:14 +02001354 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return ret;
1356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1360 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (len < 1) {
1363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1364 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1365 }
1366
1367 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1368 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1369 }
1370
1371 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1372 return ret;
1373 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (pk_alg == MBEDTLS_PK_RSA) {
1377 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1378 mbedtls_pk_free(pk);
1379 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 }
1381 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001383#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001385#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001386 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001387 if ((ret =
1388 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001389 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001390 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001391 p_rng)) != 0) {
1392 mbedtls_pk_free(pk);
1393 return ret;
1394 }
1395 } else
1396#endif
1397 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001398 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1399 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001400 mbedtls_pk_free(pk);
1401 return ret;
1402 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 }
1404 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001405#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001408 end = p + len;
1409 if (end != (key + keylen)) {
1410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1411 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1412 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415}
1416
1417/*
1418 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001419 *
1420 * To save space, the decryption happens in-place on the given key buffer.
1421 * Also, while this function may modify the keybuffer, it doesn't own it,
1422 * and instead it is the responsibility of the caller to zeroize and properly
1423 * free it after use.
1424 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001427MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_pk_context *pk,
1429 unsigned char *key, size_t keylen,
1430 const unsigned char *pwd, size_t pwdlen,
1431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001433 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001435 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1438#if defined(MBEDTLS_PKCS12_C)
1439 mbedtls_cipher_type_t cipher_alg;
1440 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001442 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Hanno Becker2aa80a72017-09-07 15:28:45 +01001444 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 end = p + keylen;
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (pwdlen == 0) {
1448 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1449 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
1451 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001452 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 *
1454 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1455 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1456 * encryptedData EncryptedData
1457 * }
1458 *
1459 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1460 *
1461 * EncryptedData ::= OCTET STRING
1462 *
1463 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001464 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1467 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1468 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 }
1470
1471 end = p + len;
1472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1474 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1478 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1479 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480
Hanno Beckerfab35692017-08-25 13:38:26 +01001481 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482
1483 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001484 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001488 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001489 cipher_alg, md_alg,
1490 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1492 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1493 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001497
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001498 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_PKCS12_C */
1501#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001503 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1504 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1506 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1507 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001511
1512 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001515 {
1516 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001517 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (decrypted == 0) {
1520 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1521 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001522 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
1526/*
1527 * Parse a private key
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1530 const unsigned char *key, size_t keylen,
1531 const unsigned char *pwd, size_t pwdlen,
1532 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533{
Janos Follath24eed8d2019-11-22 13:21:35 +00001534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001539#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (keylen == 0) {
1542 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1543 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001544
1545#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001549 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001551 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 } else {
1553 ret = mbedtls_pem_read_buffer(&pem,
1554 "-----BEGIN RSA PRIVATE KEY-----",
1555 "-----END RSA PRIVATE KEY-----",
1556 key, pwd, pwdlen, &len);
1557 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ret == 0) {
1560 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1561 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1562 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1563 pem.buf, pem.buflen)) != 0) {
1564 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565 }
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 mbedtls_pem_free(&pem);
1568 return ret;
1569 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1570 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1571 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1572 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1573 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1574 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577
Valerio Setti81d75122023-06-14 14:49:33 +02001578#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001579 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001581 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 } else {
1583 ret = mbedtls_pem_read_buffer(&pem,
1584 "-----BEGIN EC PRIVATE KEY-----",
1585 "-----END EC PRIVATE KEY-----",
1586 key, pwd, pwdlen, &len);
1587 }
1588 if (ret == 0) {
1589 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001592 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 pem.buf, pem.buflen,
1594 f_rng, p_rng)) != 0) {
1595 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_pem_free(&pem);
1599 return ret;
1600 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1601 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1602 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1603 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1604 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1605 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606 }
Valerio Setti81d75122023-06-14 14:49:33 +02001607#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001609 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001611 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 } else {
1613 ret = mbedtls_pem_read_buffer(&pem,
1614 "-----BEGIN PRIVATE KEY-----",
1615 "-----END PRIVATE KEY-----",
1616 key, NULL, 0, &len);
1617 }
1618 if (ret == 0) {
1619 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1620 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1621 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 mbedtls_pem_free(&pem);
1625 return ret;
1626 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1627 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001628 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001631 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001633 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 } else {
1635 ret = mbedtls_pem_read_buffer(&pem,
1636 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1637 "-----END ENCRYPTED PRIVATE KEY-----",
1638 key, NULL, 0, &len);
1639 }
1640 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001641 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1642 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644 }
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 mbedtls_pem_free(&pem);
1647 return ret;
1648 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1649 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652#else
1653 ((void) pwd);
1654 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656
1657 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001658 * At this point we only know it's not a PEM formatted key. Could be any
1659 * of the known DER encoded private key formats
1660 *
1661 * We try the different DER format parsers to see if one passes without
1662 * error
1663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001666 unsigned char *key_copy;
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1669 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1670 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001673
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001674 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1675 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001676
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001677 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001678 }
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if (ret == 0) {
1681 return 0;
1682 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 mbedtls_pk_free(pk);
1685 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1688 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1693 if (ret == 0) {
1694 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001695 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 mbedtls_pk_free(pk);
1698 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1703 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1704 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1705 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001706 }
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 mbedtls_pk_free(pk);
1709 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711
Valerio Setti81d75122023-06-14 14:49:33 +02001712#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1714 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001715 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 key, keylen, f_rng, p_rng) == 0) {
1717 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001720#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001721
Valerio Setti81d75122023-06-14 14:49:33 +02001722 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001723 * it is ok to leave the PK context initialized but not
1724 * freed: It is the caller's responsibility to call pk_init()
1725 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001726 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001727 * isn't, this leads to mbedtls_pk_free() being called
1728 * twice, once here and once by the caller, but this is
1729 * also ok and in line with the mbedtls_pk_free() calls
1730 * on failed PEM parsing attempts. */
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733}
1734
1735/*
1736 * Parse a public key
1737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1739 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740{
Janos Follath24eed8d2019-11-22 13:21:35 +00001741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001742 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001743#if defined(MBEDTLS_RSA_C)
1744 const mbedtls_pk_info_t *pk_info;
1745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001747 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001749#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (keylen == 0) {
1752 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1753 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001754
1755#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001757#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001758 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001760 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 } else {
1762 ret = mbedtls_pem_read_buffer(&pem,
1763 "-----BEGIN RSA PUBLIC KEY-----",
1764 "-----END RSA PUBLIC KEY-----",
1765 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001767
1768 if (ret == 0) {
1769 p = pem.buf;
1770 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1771 mbedtls_pem_free(&pem);
1772 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1773 }
1774
1775 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1776 mbedtls_pem_free(&pem);
1777 return ret;
1778 }
1779
1780 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1781 mbedtls_pk_free(ctx);
1782 }
1783
1784 mbedtls_pem_free(&pem);
1785 return ret;
1786 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1787 mbedtls_pem_free(&pem);
1788 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001789 }
1790#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001791
1792 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001794 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 } else {
1796 ret = mbedtls_pem_read_buffer(&pem,
1797 "-----BEGIN PUBLIC KEY-----",
1798 "-----END PUBLIC KEY-----",
1799 key, NULL, 0, &len);
1800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001803 /*
1804 * Was PEM encoded
1805 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001806 p = pem.buf;
1807
Jethro Beekman01672442023-04-19 14:08:14 +02001808 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_pem_free(&pem);
1810 return ret;
1811 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1812 mbedtls_pem_free(&pem);
1813 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001817
1818#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1820 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001822
1823 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1824 return ret;
1825 }
1826
1827 p = (unsigned char *) key;
1828 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1829 if (ret == 0) {
1830 return ret;
1831 }
1832 mbedtls_pk_free(ctx);
1833 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1834 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1835 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001836 }
1837#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001838 p = (unsigned char *) key;
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001843}
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#endif /* MBEDTLS_PK_PARSE_C */