blob: f03ace261059dee3932c99b6e4121598c5b67ea1 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Valerio Setti77a75682023-05-15 11:18:46 +020029#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Jethro Beekman01672442023-04-19 14:08:14 +020037#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
38#include "pkwrite.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Valerio Setti81d75122023-06-14 14:49:33 +020040#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti4064dbb2023-05-17 15:33:07 +020041#include "pk_internal.h"
42#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
55
Valerio Setti34f67552023-04-03 15:19:18 +020056#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020057#include "psa_util_internal.h"
Valerio Setti34f67552023-04-03 15:19:18 +020058#endif
59
60#if defined(MBEDTLS_USE_PSA_CRYPTO)
61#include "psa/crypto.h"
62#endif
63
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020065
Valerio Settie0e63112023-05-18 18:48:07 +020066/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020067#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020068#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
69 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020070#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020071
Gilles Peskine832f3492017-11-30 11:42:12 +010072#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073/*
74 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020075 *
76 * The file is expected to contain either PEM or DER encoded data.
77 * A terminating null byte is always appended. It is included in the announced
78 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020081{
82 FILE *f;
83 long size;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if ((f = fopen(path, "rb")) == NULL) {
86 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
87 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
Gilles Peskineda0913b2022-06-30 17:03:40 +020089 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fseek(f, 0, SEEK_END);
93 if ((size = ftell(f)) == -1) {
94 fclose(f);
95 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
Gilles Peskine449bd832023-01-11 14:50:10 +010097 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 *n = (size_t) size;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (*n + 1 == 0 ||
102 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
103 fclose(f);
104 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (fread(*buf, 1, *n, f) != *n) {
108 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100109
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100110 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113 }
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116
117 (*buf)[*n] = '\0';
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200120 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124}
125
126/*
127 * Load and parse a private key
128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
130 const char *path, const char *pwd,
131 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132{
Janos Follath24eed8d2019-11-22 13:21:35 +0000133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 size_t n;
135 unsigned char *buf;
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
138 return ret;
139 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (pwd == NULL) {
142 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
143 } else {
144 ret = mbedtls_pk_parse_key(ctx, buf, n,
145 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
146 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100148 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151}
152
153/*
154 * Load and parse a public key
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157{
Janos Follath24eed8d2019-11-22 13:21:35 +0000158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159 size_t n;
160 unsigned char *buf;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
163 return ret;
164 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100168 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173
Valerio Setti81d75122023-06-14 14:49:33 +0200174#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176 *
177 * ECParameters ::= CHOICE {
178 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100179 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 * }
182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
184 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185{
Janos Follath24eed8d2019-11-22 13:21:35 +0000186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (end - *p < 1) {
189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
190 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
191 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100192
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100193 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200194 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100198#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 ) {
200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
201 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100202 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
208 params->p = *p;
209 *p += params->len;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (*p != end) {
212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
213 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
214 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217}
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100221 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
222 * WARNING: the resulting group should only be used with
223 * pk_group_id_from_specified(), since its base point may not be set correctly
224 * if it was encoded compressed.
225 *
226 * SpecifiedECDomain ::= SEQUENCE {
227 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
228 * fieldID FieldID {{FieldTypes}},
229 * curve Curve,
230 * base ECPoint,
231 * order INTEGER,
232 * cofactor INTEGER OPTIONAL,
233 * hash HashAlgorithm OPTIONAL,
234 * ...
235 * }
236 *
237 * We only support prime-field as field type, and ignore hash and cofactor.
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100240{
Janos Follath24eed8d2019-11-22 13:21:35 +0000241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100242 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200243 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244 const unsigned char *end_field, *end_curve;
245 size_t len;
246 int ver;
247
248 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
250 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
251 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (ver < 1 || ver > 3) {
254 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
255 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100256
257 /*
258 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
259 * fieldType FIELD-ID.&id({IOSet}),
260 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
261 * }
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
264 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
265 return ret;
266 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100267
268 end_field = p + len;
269
270 /*
271 * FIELD-ID ::= TYPE-IDENTIFIER
272 * FieldTypes FIELD-ID ::= {
273 * { Prime-p IDENTIFIED BY prime-field } |
274 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
275 * }
276 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
279 return ret;
280 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
283 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
284 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285 }
286
287 p += len;
288
289 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
292 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (p != end_field) {
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
298 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
299 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100300
301 /*
302 * Curve ::= SEQUENCE {
303 * a FieldElement,
304 * b FieldElement,
305 * seed BIT STRING OPTIONAL
306 * -- Shall be present if used in SpecifiedECDomain
307 * -- with version equal to ecdpVer2 or ecdpVer3
308 * }
309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
311 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
312 return ret;
313 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100314
315 end_curve = p + len;
316
317 /*
318 * FieldElement ::= OCTET STRING
319 * containing an integer in the case of a prime field
320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
322 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324 }
325
326 p += len;
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
329 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 }
332
333 p += len;
334
335 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100337 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (p != end_curve) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
343 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100344
345 /*
346 * ECPoint ::= OCTET STRING
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
350 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
353 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100354 /*
355 * If we can't read the point because it's compressed, cheat by
356 * reading only the X coordinate and the parity bit of Y.
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
359 (p[0] != 0x02 && p[0] != 0x03) ||
360 len != mbedtls_mpi_size(&grp->P) + 1 ||
361 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
362 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
363 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
364 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100365 }
366 }
367
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368 p += len;
369
370 /*
371 * order INTEGER
372 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
375 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100378
379 /*
380 * Allow optional elements by purposefully not enforcing p == end here.
381 */
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100384}
385
386/*
387 * Find the group id associated with an (almost filled) group as generated by
388 * pk_group_from_specified(), or return an error if unknown.
389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390static 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 +0100391{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100392 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_ecp_group ref;
394 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 mbedtls_ecp_group_free(&ref);
401 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
403 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
405 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
406 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
407 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
408 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
409 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
410 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 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 +0100413 break;
414 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100415 }
416
417cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
420 *grp_id = *id;
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427}
428
429/*
430 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
433 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434{
Janos Follath24eed8d2019-11-22 13:21:35 +0000435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100445
446cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000447 /* The API respecting lifecycle for mbedtls_ecp_group struct is
448 * _init(), _load() and _free(). In pk_group_id_from_specified() the
449 * temporary grp breaks that flow and it's members are populated
450 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
451 * which is assuming a group populated by _setup() may not clean-up
452 * properly -> Manually free it's members.
453 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000454 mbedtls_mpi_free(&grp.N);
455 mbedtls_mpi_free(&grp.P);
456 mbedtls_mpi_free(&grp.A);
457 mbedtls_mpi_free(&grp.B);
458 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463
Valerio Setti4064dbb2023-05-17 15:33:07 +0200464#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
465/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
466 * ecp_keypair structure with proper group ID. The purpose of this helper
467 * function is to update ec_family and ec_bits accordingly. */
468static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
469 mbedtls_ecp_group_id grp_id)
470{
471 psa_ecc_family_t ec_family;
472 size_t bits;
473
474 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
475
476 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
477 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
478 }
479
480 pk->ec_family = ec_family;
481 pk->ec_bits = bits;
482
483 return 0;
484}
485#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
486
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100487/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100489 *
490 * ECParameters ::= CHOICE {
491 * namedCurve OBJECT IDENTIFIER
492 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
493 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200495static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496{
Janos Follath24eed8d2019-11-22 13:21:35 +0000497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (params->tag == MBEDTLS_ASN1_OID) {
501 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
502 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
503 }
504 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
507 return ret;
508 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100509#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100511#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100512 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513
Valerio Setti00e8dd12023-05-18 18:56:59 +0200514#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
515 ret = pk_update_psa_ecparams(pk, grp_id);
516#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200517 /* grp may already be initialized; if so, make sure IDs match */
518 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
519 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
521 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522
Valerio Setti4064dbb2023-05-17 15:33:07 +0200523 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
524 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return ret;
526 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200527#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528
Valerio Setti4064dbb2023-05-17 15:33:07 +0200529 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530}
531
Jethro Beekman01672442023-04-19 14:08:14 +0200532/*
533 * Helper function for deriving a public key from its private counterpart.
534 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200535static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200536 const unsigned char *d, size_t d_len,
537 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
538{
539 int ret;
540#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200541 psa_status_t status;
542 (void) f_rng;
543 (void) p_rng;
544#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
545 (void) d;
546 (void) d_len;
547
548 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
549 &pk->pub_raw_len);
550 ret = psa_pk_status_to_mbedtls(status);
551#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
552 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
553 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
554 size_t key_len;
555 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200556 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
557 size_t curve_bits;
558 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200559 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200560
561 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
562 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
563
564 status = psa_import_key(&key_attr, d, d_len, &key_id);
565 ret = psa_pk_status_to_mbedtls(status);
566 if (ret != 0) {
567 return ret;
568 }
569
Jethro Beekman01672442023-04-19 14:08:14 +0200570 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
571 ret = psa_pk_status_to_mbedtls(status);
572 destruction_status = psa_destroy_key(key_id);
573 if (ret != 0) {
574 return ret;
575 } else if (destruction_status != PSA_SUCCESS) {
576 return psa_pk_status_to_mbedtls(destruction_status);
577 }
Jethro Beekman01672442023-04-19 14:08:14 +0200578 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200579#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200580#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200581 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200582 (void) d;
583 (void) d_len;
584
585 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
586#endif /* MBEDTLS_USE_PSA_CRYPTO */
587 return ret;
588}
589
590#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
591
592/*
593 * Load an RFC8410 EC key, which doesn't have any parameters
594 */
595static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
596 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200597 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200598{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200599 int ret;
600
Jethro Beekman01672442023-04-19 14:08:14 +0200601 if (params->tag != 0 || params->len != 0) {
602 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
603 }
604
Valerio Setti00e8dd12023-05-18 18:56:59 +0200605#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
606 ret = pk_update_psa_ecparams(pk, grp_id);
607#else
608 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200609 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
610 if (ret != 0) {
611 return ret;
612 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200613#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200614 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200615}
616
617/*
618 * Parse an RFC 8410 encoded private EC key
619 *
620 * CurvePrivateKey ::= OCTET STRING
621 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200622static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200623 unsigned char *key, size_t keylen, const unsigned char *end,
624 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
625{
626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
627 size_t len;
628
629 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
630 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
631 }
632
633 if (key + len != end) {
634 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
635 }
636
Valerio Setti00e8dd12023-05-18 18:56:59 +0200637#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
639 psa_status_t status;
640
641 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200642 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
643 PSA_KEY_USAGE_DERIVE);
644 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200645
646 status = psa_import_key(&attributes, key, len, &pk->priv_id);
647 if (status != PSA_SUCCESS) {
648 ret = psa_pk_status_to_mbedtls(status);
649 return ret;
650 }
651#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
652 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
653
Valerio Setti805e4a02023-06-30 17:16:19 +0200654 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
656 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200657#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200658
Valerio Setti4064dbb2023-05-17 15:33:07 +0200659 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
660 * which never contain a public key. As such, derive the public key
661 * unconditionally. */
662 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200663 return ret;
664 }
665
Jethro Beekman01672442023-04-19 14:08:14 +0200666 return 0;
667}
668#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200669
Valerio Settiaddeee42023-06-14 10:46:55 +0200670#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200671/*
672 * Create a temporary ecp_keypair for converting an EC point in compressed
673 * format to an uncompressed one
674 */
675static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
676 const unsigned char *in_start, size_t in_len,
677 size_t *out_buf_len, unsigned char *out_buf,
678 size_t out_buf_size)
679{
680 mbedtls_ecp_keypair ecp_key;
681 mbedtls_ecp_group_id ecp_group_id;
682 int ret;
683
684 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
685
686 mbedtls_ecp_keypair_init(&ecp_key);
687 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
688 if (ret != 0) {
689 return ret;
690 }
691 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
692 in_start, in_len);
693 if (ret != 0) {
694 goto exit;
695 }
696 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
697 MBEDTLS_ECP_PF_UNCOMPRESSED,
698 out_buf_len, out_buf, out_buf_size);
699
700exit:
701 mbedtls_ecp_keypair_free(&ecp_key);
702 return ret;
703}
Valerio Settiaddeee42023-06-14 10:46:55 +0200704#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200705
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706/*
707 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100708 *
709 * The caller is responsible for clearing the structure upon failure if
710 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200714 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715{
Janos Follath24eed8d2019-11-22 13:21:35 +0000716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717
Valerio Setti4064dbb2023-05-17 15:33:07 +0200718#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
719 mbedtls_svc_key_id_t key;
720 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
721 size_t len = (end - *p);
722
723 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
724 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 }
726
Valerio Setti4064dbb2023-05-17 15:33:07 +0200727 /* Compressed point format are not supported yet by PSA crypto. As a
728 * consequence ecp functions are used to "convert" the point to
729 * uncompressed format */
730 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200731#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200732 ret = pk_convert_compressed_ec(pk, *p, len,
733 &(pk->pub_raw_len), pk->pub_raw,
734 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
735 if (ret != 0) {
736 return ret;
737 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200738#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
739 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
740#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200741 } else {
742 /* Uncompressed format */
743 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200744 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200745 }
746 memcpy(pk->pub_raw, *p, (end - *p));
747 pk->pub_raw_len = end - *p;
748 }
749
750 /* Validate the key by trying to importing it */
751 psa_set_key_usage_flags(&key_attrs, 0);
752 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
753 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
754 psa_set_key_bits(&key_attrs, pk->ec_bits);
755
756 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
757 &key) != PSA_SUCCESS) ||
758 (psa_destroy_key(key) != PSA_SUCCESS)) {
759 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
760 pk->pub_raw_len = 0;
761 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
762 }
763 ret = 0;
764#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
765 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
766 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
767 (const unsigned char *) *p,
768 end - *p)) == 0) {
769 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
770 }
771#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
772
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775 */
776 *p = (unsigned char *) end;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779}
Valerio Setti81d75122023-06-14 14:49:33 +0200780#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200783/*
784 * RSAPublicKey ::= SEQUENCE {
785 * modulus INTEGER, -- n
786 * publicExponent INTEGER -- e
787 * }
788 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int pk_get_rsapubkey(unsigned char **p,
790 const unsigned char *end,
791 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792{
Janos Follath24eed8d2019-11-22 13:21:35 +0000793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794 size_t len;
795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
797 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
799 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (*p + len != end) {
802 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
803 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
804 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200805
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
809 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
812 NULL, 0, NULL, 0)) != 0) {
813 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
814 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100815
816 *p += len;
817
818 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
821 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
824 NULL, 0, *p, len)) != 0) {
825 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
826 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827
828 *p += len;
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (mbedtls_rsa_complete(rsa) != 0 ||
831 mbedtls_rsa_check_pubkey(rsa) != 0) {
832 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (*p != end) {
836 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
837 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
838 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200843
844/* Get a PK algorithm identifier
845 *
846 * AlgorithmIdentifier ::= SEQUENCE {
847 * algorithm OBJECT IDENTIFIER,
848 * parameters ANY DEFINED BY algorithm OPTIONAL }
849 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850static int pk_get_pk_alg(unsigned char **p,
851 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200852 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
853 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854{
Janos Follath24eed8d2019-11-22 13:21:35 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
861 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
Jethro Beekman01672442023-04-19 14:08:14 +0200864 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200865#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200866 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
867 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
868 if (ret == 0) {
869 *pk_alg = MBEDTLS_PK_ECKEY;
870 }
871 }
872#else
873 (void) ec_grp_id;
874#endif
875 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
877 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
879 /*
880 * No parameters with RSA (only for EC)
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (*pk_alg == MBEDTLS_PK_RSA &&
883 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
884 params->len != 0)) {
885 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 }
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889}
890
891/*
892 * SubjectPublicKeyInfo ::= SEQUENCE {
893 * algorithm AlgorithmIdentifier,
894 * subjectPublicKey BIT STRING }
895 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
897 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898{
Janos Follath24eed8d2019-11-22 13:21:35 +0000899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_asn1_buf alg_params;
902 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200903 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
907 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
908 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 }
910
911 end = *p + len;
912
Jethro Beekman01672442023-04-19 14:08:14 +0200913 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return ret;
915 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
919 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (*p + len != end) {
922 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
923 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
924 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
927 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
928 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
931 return ret;
932 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (pk_alg == MBEDTLS_PK_RSA) {
936 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200939#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200941#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200942 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200943 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200944 } else
945#endif
946 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200947 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200948 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200950 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200953#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (ret == 0 && *p != end) {
957 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
958 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
959 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (ret != 0) {
962 mbedtls_pk_free(pk);
963 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966}
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100970 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
971 *
972 * The value zero is:
973 * - never a valid value for an RSA parameter
974 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
975 *
976 * Since values can't be omitted in PKCS#1, passing a zero value to
977 * rsa_complete() would be incorrect, so reject zero values early.
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979static int asn1_get_nonzero_mpi(unsigned char **p,
980 const unsigned char *end,
981 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982{
983 int ret;
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 ret = mbedtls_asn1_get_mpi(p, end, X);
986 if (ret != 0) {
987 return ret;
988 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
991 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
992 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100995}
996
997/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 * Parse a PKCS#1 encoded private RSA key
999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1001 const unsigned char *key,
1002 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001004 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 size_t len;
1006 unsigned char *p, *end;
1007
Hanno Beckerefa14e82017-10-11 19:45:19 +01001008 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001010
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011 p = (unsigned char *) key;
1012 end = p + keylen;
1013
1014 /*
1015 * This function parses the RSAPrivateKey (PKCS#1)
1016 *
1017 * RSAPrivateKey ::= SEQUENCE {
1018 * version Version,
1019 * modulus INTEGER, -- n
1020 * publicExponent INTEGER, -- e
1021 * privateExponent INTEGER, -- d
1022 * prime1 INTEGER, -- p
1023 * prime2 INTEGER, -- q
1024 * exponent1 INTEGER, -- d mod (p-1)
1025 * exponent2 INTEGER, -- d mod (q-1)
1026 * coefficient INTEGER, -- (inverse of q) mod p
1027 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1028 * }
1029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1031 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1032 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 }
1034
1035 end = p + len;
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1038 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 }
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (version != 0) {
1042 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043 }
1044
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1047 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1048 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001049 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1054 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1055 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001058
1059 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1061 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1062 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001063 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001065
1066 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1068 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1069 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001072
1073 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1075 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1076 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001079
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001080#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001081 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1083 * that they can be easily recomputed from D, P and Q. However by
1084 * parsing them from the PKCS1 structure it is possible to avoid
1085 * recalculating them which both reduces the overhead of loading
1086 * RSA private keys into memory and also avoids side channels which
1087 * can arise when computing those values, since all of D, P, and Q
1088 * are secret. See https://eprint.iacr.org/2020/055 for a
1089 * description of one such attack.
1090 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001091
Jack Lloyd80cc8112020-01-22 17:34:29 -05001092 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1094 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1095 goto cleanup;
1096 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001097
1098 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1100 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1101 goto cleanup;
1102 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001103
1104 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001109
Jack Lloyd60239752020-01-27 17:53:36 -05001110#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001111 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1113 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1114 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1115 goto cleanup;
1116 }
Jack Lloyd60239752020-01-27 17:53:36 -05001117#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001118
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001119 /* rsa_complete() doesn't complete anything with the default
1120 * implementation but is still called:
1121 * - for the benefit of alternative implementation that may want to
1122 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1123 * - as is also sanity-checks the key
1124 *
1125 * Furthermore, we also check the public part for consistency with
1126 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1129 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001131 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (p != end) {
1134 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1135 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 }
1137
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001138cleanup:
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001143 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((ret & 0xff80) == 0) {
1145 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1146 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001147 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 }
1152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156
Valerio Setti81d75122023-06-14 14:49:33 +02001157#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158/*
1159 * Parse a SEC1 encoded private EC key
1160 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001161static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 const unsigned char *key, size_t keylen,
1163 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164{
Janos Follath24eed8d2019-11-22 13:21:35 +00001165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001166 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001167 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001168 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001170 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171 unsigned char *end = p + keylen;
1172 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001173#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1175 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001176#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1177 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001178#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179
1180 /*
1181 * RFC 5915, or SEC1 Appendix C.4
1182 *
1183 * ECPrivateKey ::= SEQUENCE {
1184 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1185 * privateKey OCTET STRING,
1186 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1187 * publicKey [1] BIT STRING OPTIONAL
1188 * }
1189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1191 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 }
1194
1195 end = p + len;
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if (version != 1) {
1202 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
Valerio Setti6b062ee2023-06-30 17:32:57 +02001209 /* Keep a reference to the position fo the private key. It will be used
1210 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001211 d = p;
1212 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001213
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 p += len;
1215
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001216 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218 /*
1219 * Is 'parameters' present?
1220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1222 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1223 0)) == 0) {
1224 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001225 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001230 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001231 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232
Valerio Setti6b062ee2023-06-30 17:32:57 +02001233
1234#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1235 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1236 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1237 }
1238#endif
1239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001241 /*
1242 * Is 'publickey' present? If not, or if we can't read it (eg because it
1243 * is compressed), create it from the private key.
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1246 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1247 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001248 end2 = p + len;
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1252 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (p + len != end2) {
1255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1256 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1257 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001258
Valerio Setti4064dbb2023-05-17 15:33:07 +02001259 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001260 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001262 /*
1263 * The only acceptable failure mode of pk_get_ecpubkey() above
1264 * is if the point format is not recognized.
1265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1267 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1268 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001272 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001274
Valerio Setti00e8dd12023-05-18 18:56:59 +02001275#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1276 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1277 /* Setting largest masks for usage and key algorithms */
1278 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1279 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001280 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001281#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1282 psa_set_key_algorithm(&attributes,
1283 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1284#else
1285 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1286#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001287 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001288
Valerio Settia541e012023-05-24 14:31:21 +02001289 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001290 if (status != PSA_SUCCESS) {
1291 ret = psa_pk_status_to_mbedtls(status);
1292 return ret;
1293 }
1294#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1295
Valerio Setti34f67552023-04-03 15:19:18 +02001296 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001297 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001298 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001299 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001300 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303}
Valerio Setti81d75122023-06-14 14:49:33 +02001304#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305
1306/*
1307 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001308 *
1309 * Notes:
1310 *
1311 * - This function does not own the key buffer. It is the
1312 * responsibility of the caller to take care of zeroizing
1313 * and freeing it after use.
1314 *
1315 * - The function is responsible for freeing the provided
1316 * PK context on failure.
1317 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318 */
1319static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 mbedtls_pk_context *pk,
1321 const unsigned char *key, size_t keylen,
1322 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323{
1324 int ret, version;
1325 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 unsigned char *p = (unsigned char *) key;
1328 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001330 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332
Valerio Setti81d75122023-06-14 14:49:33 +02001333#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001334 (void) f_rng;
1335 (void) p_rng;
1336#endif
1337
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001339 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 *
1341 * PrivateKeyInfo ::= SEQUENCE {
1342 * version Version,
1343 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1344 * privateKey PrivateKey,
1345 * attributes [0] IMPLICIT Attributes OPTIONAL }
1346 *
1347 * Version ::= INTEGER
1348 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1349 * PrivateKey ::= OCTET STRING
1350 *
1351 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1352 */
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1355 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1356 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 }
1358
1359 end = p + len;
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1362 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001363 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (version != 0) {
1366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1367 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
Jethro Beekman01672442023-04-19 14:08:14 +02001369 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return ret;
1371 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1375 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (len < 1) {
1378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1379 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1380 }
1381
1382 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1383 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1384 }
1385
1386 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1387 return ret;
1388 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (pk_alg == MBEDTLS_PK_RSA) {
1392 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1393 mbedtls_pk_free(pk);
1394 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001395 }
1396 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001398#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001400#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001401 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001402 if ((ret =
1403 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001404 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001405 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001406 p_rng)) != 0) {
1407 mbedtls_pk_free(pk);
1408 return ret;
1409 }
1410 } else
1411#endif
1412 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001413 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1414 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001415 mbedtls_pk_free(pk);
1416 return ret;
1417 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 }
1419 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001420#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424}
1425
1426/*
1427 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001428 *
1429 * To save space, the decryption happens in-place on the given key buffer.
1430 * Also, while this function may modify the keybuffer, it doesn't own it,
1431 * and instead it is the responsibility of the caller to zeroize and properly
1432 * free it after use.
1433 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 mbedtls_pk_context *pk,
1438 unsigned char *key, size_t keylen,
1439 const unsigned char *pwd, size_t pwdlen,
1440 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001442 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001444 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1447#if defined(MBEDTLS_PKCS12_C)
1448 mbedtls_cipher_type_t cipher_alg;
1449 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450#endif
1451
Hanno Becker2aa80a72017-09-07 15:28:45 +01001452 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 end = p + keylen;
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (pwdlen == 0) {
1456 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1457 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
1459 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001460 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 *
1462 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1463 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1464 * encryptedData EncryptedData
1465 * }
1466 *
1467 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1468 *
1469 * EncryptedData ::= OCTET STRING
1470 *
1471 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001472 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1475 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1476 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 }
1478
1479 end = p + len;
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1482 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1483 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1486 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1487 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488
Hanno Beckerfab35692017-08-25 13:38:26 +01001489 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
1491 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001492 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1496 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1497 cipher_alg, md_alg,
1498 pwd, pwdlen, p, len, buf)) != 0) {
1499 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1500 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1501 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001505
1506 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PKCS12_C */
1509#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1511 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1512 p, len, buf)) != 0) {
1513 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1514 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001519
1520 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001523 {
1524 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001525 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (decrypted == 0) {
1528 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1529 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534
1535/*
1536 * Parse a private key
1537 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001538int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1539 const unsigned char *key, size_t keylen,
1540 const unsigned char *pwd, size_t pwdlen,
1541 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001542{
Janos Follath24eed8d2019-11-22 13:21:35 +00001543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001546 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001548#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (keylen == 0) {
1551 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1552 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001553
1554#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001558 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001560 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 } else {
1562 ret = mbedtls_pem_read_buffer(&pem,
1563 "-----BEGIN RSA PRIVATE KEY-----",
1564 "-----END RSA PRIVATE KEY-----",
1565 key, pwd, pwdlen, &len);
1566 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (ret == 0) {
1569 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1570 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1571 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1572 pem.buf, pem.buflen)) != 0) {
1573 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574 }
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 mbedtls_pem_free(&pem);
1577 return ret;
1578 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1579 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1580 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1581 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1582 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1583 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586
Valerio Setti81d75122023-06-14 14:49:33 +02001587#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001588 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001590 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 } else {
1592 ret = mbedtls_pem_read_buffer(&pem,
1593 "-----BEGIN EC PRIVATE KEY-----",
1594 "-----END EC PRIVATE KEY-----",
1595 key, pwd, pwdlen, &len);
1596 }
1597 if (ret == 0) {
1598 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001601 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 pem.buf, pem.buflen,
1603 f_rng, p_rng)) != 0) {
1604 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001605 }
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_pem_free(&pem);
1608 return ret;
1609 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1610 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1611 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1612 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1613 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1614 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001615 }
Valerio Setti81d75122023-06-14 14:49:33 +02001616#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001617
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001618 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001620 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 } else {
1622 ret = mbedtls_pem_read_buffer(&pem,
1623 "-----BEGIN PRIVATE KEY-----",
1624 "-----END PRIVATE KEY-----",
1625 key, NULL, 0, &len);
1626 }
1627 if (ret == 0) {
1628 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1629 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1630 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 mbedtls_pem_free(&pem);
1634 return ret;
1635 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1636 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001637 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001640 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001642 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 } else {
1644 ret = mbedtls_pem_read_buffer(&pem,
1645 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1646 "-----END ENCRYPTED PRIVATE KEY-----",
1647 key, NULL, 0, &len);
1648 }
1649 if (ret == 0) {
1650 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1651 pwd, pwdlen, f_rng, p_rng)) != 0) {
1652 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 }
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 mbedtls_pem_free(&pem);
1656 return ret;
1657 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1658 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661#else
1662 ((void) pwd);
1663 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001665
1666 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001667 * At this point we only know it's not a PEM formatted key. Could be any
1668 * of the known DER encoded private key formats
1669 *
1670 * We try the different DER format parsers to see if one passes without
1671 * error
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001675 unsigned char *key_copy;
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1678 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1679 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1684 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001685
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001686 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687 }
1688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (ret == 0) {
1690 return 0;
1691 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_pk_free(pk);
1694 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1697 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1702 if (ret == 0) {
1703 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001704 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_pk_free(pk);
1707 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1712 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1713 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1714 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001715 }
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 mbedtls_pk_free(pk);
1718 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001720
Valerio Setti81d75122023-06-14 14:49:33 +02001721#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1723 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001724 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 key, keylen, f_rng, p_rng) == 0) {
1726 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001727 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001729#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730
Valerio Setti81d75122023-06-14 14:49:33 +02001731 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001732 * it is ok to leave the PK context initialized but not
1733 * freed: It is the caller's responsibility to call pk_init()
1734 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001735 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001736 * isn't, this leads to mbedtls_pk_free() being called
1737 * twice, once here and once by the caller, but this is
1738 * also ok and in line with the mbedtls_pk_free() calls
1739 * on failed PEM parsing attempts. */
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001742}
1743
1744/*
1745 * Parse a public key
1746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1748 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001749{
Janos Follath24eed8d2019-11-22 13:21:35 +00001750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001752#if defined(MBEDTLS_RSA_C)
1753 const mbedtls_pk_info_t *pk_info;
1754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001756 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001758#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (keylen == 0) {
1761 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1762 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001763
1764#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001766#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001767 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001769 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 } else {
1771 ret = mbedtls_pem_read_buffer(&pem,
1772 "-----BEGIN RSA PUBLIC KEY-----",
1773 "-----END RSA PUBLIC KEY-----",
1774 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001775 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001776
1777 if (ret == 0) {
1778 p = pem.buf;
1779 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1780 mbedtls_pem_free(&pem);
1781 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1782 }
1783
1784 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1785 mbedtls_pem_free(&pem);
1786 return ret;
1787 }
1788
1789 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1790 mbedtls_pk_free(ctx);
1791 }
1792
1793 mbedtls_pem_free(&pem);
1794 return ret;
1795 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1796 mbedtls_pem_free(&pem);
1797 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001798 }
1799#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001800
1801 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001803 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 } else {
1805 ret = mbedtls_pem_read_buffer(&pem,
1806 "-----BEGIN PUBLIC KEY-----",
1807 "-----END PUBLIC KEY-----",
1808 key, NULL, 0, &len);
1809 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001812 /*
1813 * Was PEM encoded
1814 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001815 p = pem.buf;
1816
Jethro Beekman01672442023-04-19 14:08:14 +02001817 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 mbedtls_pem_free(&pem);
1819 return ret;
1820 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1821 mbedtls_pem_free(&pem);
1822 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001823 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001826
1827#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1829 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001830 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001831
1832 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1833 return ret;
1834 }
1835
1836 p = (unsigned char *) key;
1837 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1838 if (ret == 0) {
1839 return ret;
1840 }
1841 mbedtls_pk_free(ctx);
1842 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1843 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1844 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001845 }
1846#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001847 p = (unsigned char *) key;
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001852}
1853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#endif /* MBEDTLS_PK_PARSE_C */