blob: 3fca2c44f77c4d7b5c8ca981a76b4b3296c47937 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Valerio Settie0e63112023-05-18 18:48:07 +020058/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020059#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020060#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
61 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020062#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020063
Gilles Peskine832f3492017-11-30 11:42:12 +010064#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020065/*
66 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020067 *
68 * The file is expected to contain either PEM or DER encoded data.
69 * A terminating null byte is always appended. It is included in the announced
70 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073{
74 FILE *f;
75 long size;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((f = fopen(path, "rb")) == NULL) {
78 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
79 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskineda0913b2022-06-30 17:03:40 +020081 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 fseek(f, 0, SEEK_END);
85 if ((size = ftell(f)) == -1) {
86 fclose(f);
87 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 }
Gilles Peskine449bd832023-01-11 14:50:10 +010089 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
91 *n = (size_t) size;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (*n + 1 == 0 ||
94 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
95 fclose(f);
96 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (fread(*buf, 1, *n, f) != *n) {
100 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100102 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
109 (*buf)[*n] = '\0';
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117
118/*
119 * Load and parse a private key
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
122 const char *path, const char *pwd,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 size_t n;
127 unsigned char *buf;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
130 return ret;
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (pwd == NULL) {
134 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
135 } else {
136 ret = mbedtls_pk_parse_key(ctx, buf, n,
137 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100140 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143}
144
145/*
146 * Load and parse a public key
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 size_t n;
152 unsigned char *buf;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
155 return ret;
156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100160 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Valerio Setti81d75122023-06-14 14:49:33 +0200166#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (end - *p < 1) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
183 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100190#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 params->p = *p;
201 *p += params->len;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (*p != end) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200235 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
243 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ver < 1 || ver > 3) {
246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
257 return ret;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 end_field = p + len;
261
262 /*
263 * FIELD-ID ::= TYPE-IDENTIFIER
264 * FieldTypes FIELD-ID ::= {
265 * { Prime-p IDENTIFIED BY prime-field } |
266 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
267 * }
268 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
271 return ret;
272 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
275 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
276 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 }
278
279 p += len;
280
281 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (p != end_field) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
291 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /*
294 * Curve ::= SEQUENCE {
295 * a FieldElement,
296 * b FieldElement,
297 * seed BIT STRING OPTIONAL
298 * -- Shall be present if used in SpecifiedECDomain
299 * -- with version equal to ecdpVer2 or ecdpVer3
300 * }
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
304 return ret;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
314 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316 }
317
318 p += len;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
321 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (p != end_curve) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
342 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
345 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
351 (p[0] != 0x02 && p[0] != 0x03) ||
352 len != mbedtls_mpi_size(&grp->P) + 1 ||
353 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
354 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
356 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 }
358 }
359
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 p += len;
361
362 /*
363 * order INTEGER
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
367 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_free(&ref);
393 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
395 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
397 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
398 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405 break;
406 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 }
408
409cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411
412 *grp_id = *id;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419}
420
421/*
422 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
425 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426{
Janos Follath24eed8d2019-11-22 13:21:35 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000439 /* The API respecting lifecycle for mbedtls_ecp_group struct is
440 * _init(), _load() and _free(). In pk_group_id_from_specified() the
441 * temporary grp breaks that flow and it's members are populated
442 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
443 * which is assuming a group populated by _setup() may not clean-up
444 * properly -> Manually free it's members.
445 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000446 mbedtls_mpi_free(&grp.N);
447 mbedtls_mpi_free(&grp.P);
448 mbedtls_mpi_free(&grp.A);
449 mbedtls_mpi_free(&grp.B);
450 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Valerio Setti4064dbb2023-05-17 15:33:07 +0200456#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
457/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
458 * ecp_keypair structure with proper group ID. The purpose of this helper
459 * function is to update ec_family and ec_bits accordingly. */
460static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
461 mbedtls_ecp_group_id grp_id)
462{
463 psa_ecc_family_t ec_family;
464 size_t bits;
465
466 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
467
468 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
469 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
470 }
471
472 pk->ec_family = ec_family;
473 pk->ec_bits = bits;
474
475 return 0;
476}
477#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
478
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100479/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100481 *
482 * ECParameters ::= CHOICE {
483 * namedCurve OBJECT IDENTIFIER
484 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
485 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200487static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488{
Janos Follath24eed8d2019-11-22 13:21:35 +0000489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (params->tag == MBEDTLS_ASN1_OID) {
493 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
494 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
495 }
496 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
499 return ret;
500 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100501#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100503#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505
Valerio Setti00e8dd12023-05-18 18:56:59 +0200506#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
507 ret = pk_update_psa_ecparams(pk, grp_id);
508#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200509 /* grp may already be initialized; if so, make sure IDs match */
510 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
511 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
513 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514
Valerio Setti4064dbb2023-05-17 15:33:07 +0200515 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
516 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return ret;
518 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200519#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520
Valerio Setti4064dbb2023-05-17 15:33:07 +0200521 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522}
523
Jethro Beekman01672442023-04-19 14:08:14 +0200524/*
525 * Helper function for deriving a public key from its private counterpart.
526 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200527static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200528 const unsigned char *d, size_t d_len,
529 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
530{
531 int ret;
532#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200533 psa_status_t status;
534 (void) f_rng;
535 (void) p_rng;
536#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
537 (void) d;
538 (void) d_len;
539
540 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
541 &pk->pub_raw_len);
542 ret = psa_pk_status_to_mbedtls(status);
543#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
544 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
545 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
546 size_t key_len;
547 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200548 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
549 size_t curve_bits;
550 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200551 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200552
553 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
554 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
555
556 status = psa_import_key(&key_attr, d, d_len, &key_id);
557 ret = psa_pk_status_to_mbedtls(status);
558 if (ret != 0) {
559 return ret;
560 }
561
Jethro Beekman01672442023-04-19 14:08:14 +0200562 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
563 ret = psa_pk_status_to_mbedtls(status);
564 destruction_status = psa_destroy_key(key_id);
565 if (ret != 0) {
566 return ret;
567 } else if (destruction_status != PSA_SUCCESS) {
568 return psa_pk_status_to_mbedtls(destruction_status);
569 }
Jethro Beekman01672442023-04-19 14:08:14 +0200570 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200571#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200572#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200573 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200574 (void) d;
575 (void) d_len;
576
577 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
578#endif /* MBEDTLS_USE_PSA_CRYPTO */
579 return ret;
580}
581
582#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
583
584/*
585 * Load an RFC8410 EC key, which doesn't have any parameters
586 */
587static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
588 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200589 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200590{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200591 int ret;
592
Jethro Beekman01672442023-04-19 14:08:14 +0200593 if (params->tag != 0 || params->len != 0) {
594 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
595 }
596
Valerio Setti00e8dd12023-05-18 18:56:59 +0200597#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
598 ret = pk_update_psa_ecparams(pk, grp_id);
599#else
600 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200601 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
602 if (ret != 0) {
603 return ret;
604 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200605#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200606 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200607}
608
609/*
610 * Parse an RFC 8410 encoded private EC key
611 *
612 * CurvePrivateKey ::= OCTET STRING
613 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200614static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200615 unsigned char *key, size_t keylen, const unsigned char *end,
616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
617{
618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
619 size_t len;
620
621 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
622 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
623 }
624
625 if (key + len != end) {
626 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
627 }
628
Valerio Setti00e8dd12023-05-18 18:56:59 +0200629#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
631 psa_status_t status;
632
633 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
635 PSA_KEY_USAGE_DERIVE);
636 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200637
638 status = psa_import_key(&attributes, key, len, &pk->priv_id);
639 if (status != PSA_SUCCESS) {
640 ret = psa_pk_status_to_mbedtls(status);
641 return ret;
642 }
643#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
644 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
645
Valerio Setti805e4a02023-06-30 17:16:19 +0200646 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200647 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
648 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200649#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200650
Valerio Setti4064dbb2023-05-17 15:33:07 +0200651 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
652 * which never contain a public key. As such, derive the public key
653 * unconditionally. */
654 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200655 return ret;
656 }
657
Jethro Beekman01672442023-04-19 14:08:14 +0200658 return 0;
659}
660#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200661
Valerio Settiaddeee42023-06-14 10:46:55 +0200662#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200663/*
664 * Create a temporary ecp_keypair for converting an EC point in compressed
665 * format to an uncompressed one
666 */
667static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
668 const unsigned char *in_start, size_t in_len,
669 size_t *out_buf_len, unsigned char *out_buf,
670 size_t out_buf_size)
671{
672 mbedtls_ecp_keypair ecp_key;
673 mbedtls_ecp_group_id ecp_group_id;
674 int ret;
675
676 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
677
678 mbedtls_ecp_keypair_init(&ecp_key);
679 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
680 if (ret != 0) {
681 return ret;
682 }
683 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
684 in_start, in_len);
685 if (ret != 0) {
686 goto exit;
687 }
688 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
689 MBEDTLS_ECP_PF_UNCOMPRESSED,
690 out_buf_len, out_buf, out_buf_size);
691
692exit:
693 mbedtls_ecp_keypair_free(&ecp_key);
694 return ret;
695}
Valerio Settiaddeee42023-06-14 10:46:55 +0200696#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200697
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698/*
699 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100700 *
701 * The caller is responsible for clearing the structure upon failure if
702 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200706 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707{
Janos Follath24eed8d2019-11-22 13:21:35 +0000708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709
Valerio Setti4064dbb2023-05-17 15:33:07 +0200710#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
711 mbedtls_svc_key_id_t key;
712 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
713 size_t len = (end - *p);
714
715 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
716 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717 }
718
Valerio Setti4064dbb2023-05-17 15:33:07 +0200719 /* Compressed point format are not supported yet by PSA crypto. As a
720 * consequence ecp functions are used to "convert" the point to
721 * uncompressed format */
722 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200723#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200724 ret = pk_convert_compressed_ec(pk, *p, len,
725 &(pk->pub_raw_len), pk->pub_raw,
726 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
727 if (ret != 0) {
728 return ret;
729 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200730#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
731 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
732#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200733 } else {
734 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100735 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200736 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200737 }
738 memcpy(pk->pub_raw, *p, (end - *p));
739 pk->pub_raw_len = end - *p;
740 }
741
742 /* Validate the key by trying to importing it */
743 psa_set_key_usage_flags(&key_attrs, 0);
744 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
745 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
746 psa_set_key_bits(&key_attrs, pk->ec_bits);
747
748 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
749 &key) != PSA_SUCCESS) ||
750 (psa_destroy_key(key) != PSA_SUCCESS)) {
751 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
752 pk->pub_raw_len = 0;
753 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
754 }
755 ret = 0;
756#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
757 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
758 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
759 (const unsigned char *) *p,
760 end - *p)) == 0) {
761 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
762 }
763#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
764
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200767 */
768 *p = (unsigned char *) end;
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200771}
Valerio Setti81d75122023-06-14 14:49:33 +0200772#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775/*
776 * RSAPublicKey ::= SEQUENCE {
777 * modulus INTEGER, -- n
778 * publicExponent INTEGER -- e
779 * }
780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int pk_get_rsapubkey(unsigned char **p,
782 const unsigned char *end,
783 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784{
Janos Follath24eed8d2019-11-22 13:21:35 +0000785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786 size_t len;
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
789 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
790 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
791 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (*p + len != end) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
795 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100798 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
800 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
801 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
804 NULL, 0, NULL, 0)) != 0) {
805 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
806 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100807
808 *p += len;
809
810 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
812 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
813 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
816 NULL, 0, *p, len)) != 0) {
817 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
818 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100819
820 *p += len;
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (mbedtls_rsa_complete(rsa) != 0 ||
823 mbedtls_rsa_check_pubkey(rsa) != 0) {
824 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000825 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (*p != end) {
828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
829 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
830 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835
836/* Get a PK algorithm identifier
837 *
838 * AlgorithmIdentifier ::= SEQUENCE {
839 * algorithm OBJECT IDENTIFIER,
840 * parameters ANY DEFINED BY algorithm OPTIONAL }
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842static int pk_get_pk_alg(unsigned char **p,
843 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200844 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
845 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846{
Janos Follath24eed8d2019-11-22 13:21:35 +0000847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
853 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
854 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
Jethro Beekman01672442023-04-19 14:08:14 +0200856 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200857#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200858 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
859 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
860 if (ret == 0) {
861 *pk_alg = MBEDTLS_PK_ECKEY;
862 }
863 }
864#else
865 (void) ec_grp_id;
866#endif
867 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
869 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
871 /*
872 * No parameters with RSA (only for EC)
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (*pk_alg == MBEDTLS_PK_RSA &&
875 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
876 params->len != 0)) {
877 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 }
879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881}
882
883/*
884 * SubjectPublicKeyInfo ::= SEQUENCE {
885 * algorithm AlgorithmIdentifier,
886 * subjectPublicKey BIT STRING }
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
889 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890{
Janos Follath24eed8d2019-11-22 13:21:35 +0000891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_asn1_buf alg_params;
894 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200895 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
899 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
900 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 }
902
903 end = *p + len;
904
Jethro Beekman01672442023-04-19 14:08:14 +0200905 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return ret;
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
910 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
911 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (*p + len != end) {
914 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
915 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
916 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
919 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
920 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
923 return ret;
924 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (pk_alg == MBEDTLS_PK_RSA) {
928 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200931#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200933#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200934 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200935 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200936 } else
937#endif
938 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200939 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200940 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200942 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200945#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ret == 0 && *p != end) {
949 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
950 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
951 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (ret != 0) {
954 mbedtls_pk_free(pk);
955 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958}
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100962 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
963 *
964 * The value zero is:
965 * - never a valid value for an RSA parameter
966 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
967 *
968 * Since values can't be omitted in PKCS#1, passing a zero value to
969 * rsa_complete() would be incorrect, so reject zero values early.
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971static int asn1_get_nonzero_mpi(unsigned char **p,
972 const unsigned char *end,
973 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974{
975 int ret;
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 ret = mbedtls_asn1_get_mpi(p, end, X);
978 if (ret != 0) {
979 return ret;
980 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
983 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
984 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100987}
988
989/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990 * Parse a PKCS#1 encoded private RSA key
991 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
993 const unsigned char *key,
994 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100996 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 size_t len;
998 unsigned char *p, *end;
999
Hanno Beckerefa14e82017-10-11 19:45:19 +01001000 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001002
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003 p = (unsigned char *) key;
1004 end = p + keylen;
1005
1006 /*
1007 * This function parses the RSAPrivateKey (PKCS#1)
1008 *
1009 * RSAPrivateKey ::= SEQUENCE {
1010 * version Version,
1011 * modulus INTEGER, -- n
1012 * publicExponent INTEGER, -- e
1013 * privateExponent INTEGER, -- d
1014 * prime1 INTEGER, -- p
1015 * prime2 INTEGER, -- q
1016 * exponent1 INTEGER, -- d mod (p-1)
1017 * exponent2 INTEGER, -- d mod (q-1)
1018 * coefficient INTEGER, -- (inverse of q) mod p
1019 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1020 * }
1021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1023 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1024 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 }
1026
1027 end = p + len;
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1030 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 }
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (version != 0) {
1034 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 }
1036
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1039 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1040 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001044 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1046 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1047 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050
1051 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1053 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1054 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057
1058 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1060 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1061 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001062 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064
1065 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1067 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1068 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001069 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001071
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001072#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001073 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1075 * that they can be easily recomputed from D, P and Q. However by
1076 * parsing them from the PKCS1 structure it is possible to avoid
1077 * recalculating them which both reduces the overhead of loading
1078 * RSA private keys into memory and also avoids side channels which
1079 * can arise when computing those values, since all of D, P, and Q
1080 * are secret. See https://eprint.iacr.org/2020/055 for a
1081 * description of one such attack.
1082 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001083
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1086 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1087 goto cleanup;
1088 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001089
1090 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1092 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1093 goto cleanup;
1094 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001095
1096 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1098 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1099 goto cleanup;
1100 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001101
Jack Lloyd60239752020-01-27 17:53:36 -05001102#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001103 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1105 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd60239752020-01-27 17:53:36 -05001109#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001110
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001111 /* rsa_complete() doesn't complete anything with the default
1112 * implementation but is still called:
1113 * - for the benefit of alternative implementation that may want to
1114 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1115 * - as is also sanity-checks the key
1116 *
1117 * Furthermore, we also check the public part for consistency with
1118 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1121 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001122 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001123 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (p != end) {
1126 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1127 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 }
1129
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130cleanup:
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001135 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret & 0xff80) == 0) {
1137 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1138 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001139 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 }
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148
Valerio Setti81d75122023-06-14 14:49:33 +02001149#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150/*
1151 * Parse a SEC1 encoded private EC key
1152 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001153static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 const unsigned char *key, size_t keylen,
1155 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156{
Janos Follath24eed8d2019-11-22 13:21:35 +00001157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001158 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001159 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001160 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001162 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 unsigned char *end = p + keylen;
1164 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001165#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1167 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001168#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1169 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001170#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171
1172 /*
1173 * RFC 5915, or SEC1 Appendix C.4
1174 *
1175 * ECPrivateKey ::= SEQUENCE {
1176 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1177 * privateKey OCTET STRING,
1178 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1179 * publicKey [1] BIT STRING OPTIONAL
1180 * }
1181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1183 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1184 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185 }
1186
1187 end = p + len;
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1191 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (version != 1) {
1194 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
Valerio Setti6b062ee2023-06-30 17:32:57 +02001201 /* Keep a reference to the position fo the private key. It will be used
1202 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001203 d = p;
1204 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001205
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 p += len;
1207
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001208 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001210 /*
1211 * Is 'parameters' present?
1212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1214 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1215 0)) == 0) {
1216 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001217 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001222 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001223 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001224
Valerio Setti6b062ee2023-06-30 17:32:57 +02001225
1226#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1227 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1229 }
1230#endif
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233 /*
1234 * Is 'publickey' present? If not, or if we can't read it (eg because it
1235 * is compressed), create it from the private key.
1236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1238 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1239 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240 end2 = p + len;
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1243 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1244 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (p + len != end2) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1248 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1249 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001250
Valerio Setti4064dbb2023-05-17 15:33:07 +02001251 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001252 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001254 /*
1255 * The only acceptable failure mode of pk_get_ecpubkey() above
1256 * is if the point format is not recognized.
1257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1259 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1260 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001261 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001266
Valerio Setti00e8dd12023-05-18 18:56:59 +02001267#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1268 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1269 /* Setting largest masks for usage and key algorithms */
1270 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1271 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001272 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001273#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1274 psa_set_key_algorithm(&attributes,
1275 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1276#else
1277 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1278#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001279 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001280
Valerio Settia541e012023-05-24 14:31:21 +02001281 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001282 if (status != PSA_SUCCESS) {
1283 ret = psa_pk_status_to_mbedtls(status);
1284 return ret;
1285 }
1286#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1287
Valerio Setti34f67552023-04-03 15:19:18 +02001288 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001289 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001290 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001291 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001292 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295}
Valerio Setti81d75122023-06-14 14:49:33 +02001296#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
1298/*
1299 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001300 *
1301 * Notes:
1302 *
1303 * - This function does not own the key buffer. It is the
1304 * responsibility of the caller to take care of zeroizing
1305 * and freeing it after use.
1306 *
1307 * - The function is responsible for freeing the provided
1308 * PK context on failure.
1309 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 */
1311static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_pk_context *pk,
1313 const unsigned char *key, size_t keylen,
1314 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315{
1316 int ret, version;
1317 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 unsigned char *p = (unsigned char *) key;
1320 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001322 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324
Valerio Setti81d75122023-06-14 14:49:33 +02001325#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001326 (void) f_rng;
1327 (void) p_rng;
1328#endif
1329
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001331 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332 *
1333 * PrivateKeyInfo ::= SEQUENCE {
1334 * version Version,
1335 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1336 * privateKey PrivateKey,
1337 * attributes [0] IMPLICIT Attributes OPTIONAL }
1338 *
1339 * Version ::= INTEGER
1340 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1341 * PrivateKey ::= OCTET STRING
1342 *
1343 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1344 */
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 }
1350
1351 end = p + len;
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1354 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001355 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (version != 0) {
1358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1359 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360
Jethro Beekman01672442023-04-19 14:08:14 +02001361 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return ret;
1363 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1367 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (len < 1) {
1370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1371 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1372 }
1373
1374 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1375 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1376 }
1377
1378 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1379 return ret;
1380 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (pk_alg == MBEDTLS_PK_RSA) {
1384 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1385 mbedtls_pk_free(pk);
1386 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387 }
1388 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001390#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001392#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001393 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001394 if ((ret =
1395 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001396 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001397 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001398 p_rng)) != 0) {
1399 mbedtls_pk_free(pk);
1400 return ret;
1401 }
1402 } else
1403#endif
1404 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001405 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1406 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001407 mbedtls_pk_free(pk);
1408 return ret;
1409 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 }
1411 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001412#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001415 end = p + len;
1416 if (end != (key + keylen)) {
1417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1418 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1419 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422}
1423
1424/*
1425 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001426 *
1427 * To save space, the decryption happens in-place on the given key buffer.
1428 * Also, while this function may modify the keybuffer, it doesn't own it,
1429 * and instead it is the responsibility of the caller to zeroize and properly
1430 * free it after use.
1431 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001434MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_pk_context *pk,
1436 unsigned char *key, size_t keylen,
1437 const unsigned char *pwd, size_t pwdlen,
1438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001440 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001442 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1445#if defined(MBEDTLS_PKCS12_C)
1446 mbedtls_cipher_type_t cipher_alg;
1447 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001449 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Hanno Becker2aa80a72017-09-07 15:28:45 +01001451 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452 end = p + keylen;
1453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (pwdlen == 0) {
1455 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1456 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
1458 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001459 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 *
1461 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1462 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1463 * encryptedData EncryptedData
1464 * }
1465 *
1466 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1467 *
1468 * EncryptedData ::= OCTET STRING
1469 *
1470 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001471 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1474 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1475 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 }
1477
1478 end = p + len;
1479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1481 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1482 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1486 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487
Hanno Beckerfab35692017-08-25 13:38:26 +01001488 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489
1490 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001491 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001495 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001496 cipher_alg, md_alg,
1497 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1499 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1500 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001504
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001505 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_PKCS12_C */
1508#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001510 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1511 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1513 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1514 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001518
1519 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001522 {
1523 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001524 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (decrypted == 0) {
1527 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1528 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001529 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532
1533/*
1534 * Parse a private key
1535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001536int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1537 const unsigned char *key, size_t keylen,
1538 const unsigned char *pwd, size_t pwdlen,
1539 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540{
Janos Follath24eed8d2019-11-22 13:21:35 +00001541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001546#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (keylen == 0) {
1549 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1550 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001551
1552#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001556 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001558 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 } else {
1560 ret = mbedtls_pem_read_buffer(&pem,
1561 "-----BEGIN RSA PRIVATE KEY-----",
1562 "-----END RSA PRIVATE KEY-----",
1563 key, pwd, pwdlen, &len);
1564 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ret == 0) {
1567 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1568 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1569 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1570 pem.buf, pem.buflen)) != 0) {
1571 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572 }
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 mbedtls_pem_free(&pem);
1575 return ret;
1576 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1577 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1578 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1579 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1580 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1581 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584
Valerio Setti81d75122023-06-14 14:49:33 +02001585#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001586 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001588 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 } else {
1590 ret = mbedtls_pem_read_buffer(&pem,
1591 "-----BEGIN EC PRIVATE KEY-----",
1592 "-----END EC PRIVATE KEY-----",
1593 key, pwd, pwdlen, &len);
1594 }
1595 if (ret == 0) {
1596 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001599 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 pem.buf, pem.buflen,
1601 f_rng, p_rng)) != 0) {
1602 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603 }
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 mbedtls_pem_free(&pem);
1606 return ret;
1607 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1608 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1609 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1610 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1611 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1612 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001613 }
Valerio Setti81d75122023-06-14 14:49:33 +02001614#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001615
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001616 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001618 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 } else {
1620 ret = mbedtls_pem_read_buffer(&pem,
1621 "-----BEGIN PRIVATE KEY-----",
1622 "-----END PRIVATE KEY-----",
1623 key, NULL, 0, &len);
1624 }
1625 if (ret == 0) {
1626 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1627 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1628 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629 }
1630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 mbedtls_pem_free(&pem);
1632 return ret;
1633 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1634 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001635 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001638 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001640 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else {
1642 ret = mbedtls_pem_read_buffer(&pem,
1643 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1644 "-----END ENCRYPTED PRIVATE KEY-----",
1645 key, NULL, 0, &len);
1646 }
1647 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001648 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1649 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651 }
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 mbedtls_pem_free(&pem);
1654 return ret;
1655 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1656 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001659#else
1660 ((void) pwd);
1661 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001663
1664 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001665 * At this point we only know it's not a PEM formatted key. Could be any
1666 * of the known DER encoded private key formats
1667 *
1668 * We try the different DER format parsers to see if one passes without
1669 * error
1670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001673 unsigned char *key_copy;
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1676 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1677 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001680
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001681 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1682 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001683
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001684 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 }
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (ret == 0) {
1688 return 0;
1689 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 mbedtls_pk_free(pk);
1692 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1695 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1700 if (ret == 0) {
1701 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001702 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 mbedtls_pk_free(pk);
1705 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1710 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1711 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1712 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713 }
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_pk_free(pk);
1716 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718
Valerio Setti81d75122023-06-14 14:49:33 +02001719#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1721 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001722 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 key, keylen, f_rng, p_rng) == 0) {
1724 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001727#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728
Valerio Setti81d75122023-06-14 14:49:33 +02001729 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001730 * it is ok to leave the PK context initialized but not
1731 * freed: It is the caller's responsibility to call pk_init()
1732 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001733 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001734 * isn't, this leads to mbedtls_pk_free() being called
1735 * twice, once here and once by the caller, but this is
1736 * also ok and in line with the mbedtls_pk_free() calls
1737 * on failed PEM parsing attempts. */
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740}
1741
1742/*
1743 * Parse a public key
1744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1746 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001747{
Janos Follath24eed8d2019-11-22 13:21:35 +00001748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001749 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001750#if defined(MBEDTLS_RSA_C)
1751 const mbedtls_pk_info_t *pk_info;
1752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001754 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001756#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (keylen == 0) {
1759 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1760 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001761
1762#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001764#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001765 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001767 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 } else {
1769 ret = mbedtls_pem_read_buffer(&pem,
1770 "-----BEGIN RSA PUBLIC KEY-----",
1771 "-----END RSA PUBLIC KEY-----",
1772 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001773 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001774
1775 if (ret == 0) {
1776 p = pem.buf;
1777 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1778 mbedtls_pem_free(&pem);
1779 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1780 }
1781
1782 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1783 mbedtls_pem_free(&pem);
1784 return ret;
1785 }
1786
1787 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1788 mbedtls_pk_free(ctx);
1789 }
1790
1791 mbedtls_pem_free(&pem);
1792 return ret;
1793 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1794 mbedtls_pem_free(&pem);
1795 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001796 }
1797#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001798
1799 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001801 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 } else {
1803 ret = mbedtls_pem_read_buffer(&pem,
1804 "-----BEGIN PUBLIC KEY-----",
1805 "-----END PUBLIC KEY-----",
1806 key, NULL, 0, &len);
1807 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001810 /*
1811 * Was PEM encoded
1812 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001813 p = pem.buf;
1814
Jethro Beekman01672442023-04-19 14:08:14 +02001815 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 mbedtls_pem_free(&pem);
1817 return ret;
1818 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1819 mbedtls_pem_free(&pem);
1820 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001824
1825#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1827 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001829
1830 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1831 return ret;
1832 }
1833
1834 p = (unsigned char *) key;
1835 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1836 if (ret == 0) {
1837 return ret;
1838 }
1839 mbedtls_pk_free(ctx);
1840 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1841 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1842 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001843 }
1844#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001845 p = (unsigned char *) key;
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001850}
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_PK_PARSE_C */