blob: 3f67786e44753848712fa05b46ac0164af03e108 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Valerio Settie0e63112023-05-18 18:48:07 +020058/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020059#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020060#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
61 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020062#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020063
Gilles Peskine832f3492017-11-30 11:42:12 +010064#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020065/*
66 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020067 *
68 * The file is expected to contain either PEM or DER encoded data.
69 * A terminating null byte is always appended. It is included in the announced
70 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073{
74 FILE *f;
75 long size;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((f = fopen(path, "rb")) == NULL) {
78 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
79 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskineda0913b2022-06-30 17:03:40 +020081 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 fseek(f, 0, SEEK_END);
85 if ((size = ftell(f)) == -1) {
86 fclose(f);
87 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 }
Gilles Peskine449bd832023-01-11 14:50:10 +010089 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
91 *n = (size_t) size;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (*n + 1 == 0 ||
94 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
95 fclose(f);
96 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (fread(*buf, 1, *n, f) != *n) {
100 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100102 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
109 (*buf)[*n] = '\0';
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117
118/*
119 * Load and parse a private key
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
122 const char *path, const char *pwd,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 size_t n;
127 unsigned char *buf;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
130 return ret;
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (pwd == NULL) {
134 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
135 } else {
136 ret = mbedtls_pk_parse_key(ctx, buf, n,
137 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100140 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143}
144
145/*
146 * Load and parse a public key
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 size_t n;
152 unsigned char *buf;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
155 return ret;
156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100160 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Valerio Setti81d75122023-06-14 14:49:33 +0200166#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (end - *p < 1) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
183 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100190#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 params->p = *p;
201 *p += params->len;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (*p != end) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200235 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
243 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ver < 1 || ver > 3) {
246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
257 return ret;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 end_field = p + len;
261
262 /*
263 * FIELD-ID ::= TYPE-IDENTIFIER
264 * FieldTypes FIELD-ID ::= {
265 * { Prime-p IDENTIFIED BY prime-field } |
266 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
267 * }
268 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
271 return ret;
272 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
275 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
276 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 }
278
279 p += len;
280
281 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (p != end_field) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
291 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /*
294 * Curve ::= SEQUENCE {
295 * a FieldElement,
296 * b FieldElement,
297 * seed BIT STRING OPTIONAL
298 * -- Shall be present if used in SpecifiedECDomain
299 * -- with version equal to ecdpVer2 or ecdpVer3
300 * }
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
304 return ret;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
314 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316 }
317
318 p += len;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
321 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (p != end_curve) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
342 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
345 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
351 (p[0] != 0x02 && p[0] != 0x03) ||
352 len != mbedtls_mpi_size(&grp->P) + 1 ||
353 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
354 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
356 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 }
358 }
359
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 p += len;
361
362 /*
363 * order INTEGER
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
367 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_free(&ref);
393 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
395 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
397 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
398 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405 break;
406 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 }
408
409cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411
412 *grp_id = *id;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419}
420
421/*
422 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
425 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426{
Janos Follath24eed8d2019-11-22 13:21:35 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000439 /* The API respecting lifecycle for mbedtls_ecp_group struct is
440 * _init(), _load() and _free(). In pk_group_id_from_specified() the
441 * temporary grp breaks that flow and it's members are populated
442 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
443 * which is assuming a group populated by _setup() may not clean-up
444 * properly -> Manually free it's members.
445 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000446 mbedtls_mpi_free(&grp.N);
447 mbedtls_mpi_free(&grp.P);
448 mbedtls_mpi_free(&grp.A);
449 mbedtls_mpi_free(&grp.B);
450 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200456/*
457 * Set the group used by this key.
458 */
459static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200460{
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200461#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
462 size_t ec_bits;
463 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200464
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200465 /* group may already be initialized; if so, make sure IDs match */
466 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
467 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200468 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
469 }
470
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200471 /* set group */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200472 pk->ec_family = ec_family;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200473 pk->ec_bits = ec_bits;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200474
475 return 0;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200476#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
477 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
478
479 /* grp may already be initialized; if so, make sure IDs match */
480 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
481 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
482 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
483 }
484
485 /* set group */
486 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200487#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200488}
Valerio Setti4064dbb2023-05-17 15:33:07 +0200489
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490/*
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200491 * Set the private key material
492 *
493 * Must have already set the group with pk_ecc_set_group().
494 *
495 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
496 */
497static int pk_ecc_set_key(mbedtls_pk_context *pk,
498 unsigned char *key, size_t len)
499{
500#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
502 psa_status_t status;
503
504 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
505 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
506 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200507 /* Montgomery allows only ECDH, others ECDSA too */
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200508 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
509 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200510 psa_set_key_enrollment_algorithm(&attributes,
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200511 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200512 }
513 psa_set_key_usage_flags(&attributes, flags);
514
515 status = psa_import_key(&attributes, key, len, &pk->priv_id);
516 return psa_pk_status_to_mbedtls(status);
517
518#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
519
520 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
521 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
522 if (ret != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
524 }
525 return 0;
526#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
527}
528
529/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100531 *
532 * ECParameters ::= CHOICE {
533 * namedCurve OBJECT IDENTIFIER
534 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
535 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200537static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538{
Janos Follath24eed8d2019-11-22 13:21:35 +0000539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (params->tag == MBEDTLS_ASN1_OID) {
543 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
544 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
545 }
546 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
549 return ret;
550 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100551#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100553#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100554 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200556 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557}
558
Jethro Beekman01672442023-04-19 14:08:14 +0200559/*
560 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200561 *
562 * Note: the private key information is always available from pk,
563 * however for convenience the serialized version is also passed,
564 * as it's available at each calling site, and useful in some configs
565 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200566 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200567static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200568 const unsigned char *d, size_t d_len,
569 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
570{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200571#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200572 psa_status_t status;
573 (void) f_rng;
574 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200575 (void) d;
576 (void) d_len;
577
578 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
579 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200580 return psa_pk_status_to_mbedtls(status);
581#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
582 int ret;
583 psa_status_t status;
584 (void) f_rng;
585 (void) p_rng;
586
Valerio Setti00e8dd12023-05-18 18:56:59 +0200587 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
588 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
589 size_t key_len;
590 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200591 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
592 size_t curve_bits;
593 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200594 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200595
596 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
597 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
598
599 status = psa_import_key(&key_attr, d, d_len, &key_id);
600 ret = psa_pk_status_to_mbedtls(status);
601 if (ret != 0) {
602 return ret;
603 }
604
Jethro Beekman01672442023-04-19 14:08:14 +0200605 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
606 ret = psa_pk_status_to_mbedtls(status);
607 destruction_status = psa_destroy_key(key_id);
608 if (ret != 0) {
609 return ret;
610 } else if (destruction_status != PSA_SUCCESS) {
611 return psa_pk_status_to_mbedtls(destruction_status);
612 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200613 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200614#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200615 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200616 (void) d;
617 (void) d_len;
618
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200619 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200621}
622
623#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
624
625/*
626 * Load an RFC8410 EC key, which doesn't have any parameters
627 */
628static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
629 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200630 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200631{
632 if (params->tag != 0 || params->len != 0) {
633 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
634 }
635
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200636 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200637}
638
639/*
640 * Parse an RFC 8410 encoded private EC key
641 *
642 * CurvePrivateKey ::= OCTET STRING
643 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200644static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200645 unsigned char *key, size_t keylen, const unsigned char *end,
646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
647{
648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
649 size_t len;
650
651 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
652 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
653 }
654
655 if (key + len != end) {
656 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
657 }
658
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659 /*
660 * Load the private key
661 */
662 ret = pk_ecc_set_key(pk, key, len);
663 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200664 return ret;
665 }
Jethro Beekman01672442023-04-19 14:08:14 +0200666
Valerio Setti4064dbb2023-05-17 15:33:07 +0200667 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
668 * which never contain a public key. As such, derive the public key
669 * unconditionally. */
670 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200671 return ret;
672 }
673
Jethro Beekman01672442023-04-19 14:08:14 +0200674 return 0;
675}
676#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200677
Valerio Settiaddeee42023-06-14 10:46:55 +0200678#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200679/*
680 * Create a temporary ecp_keypair for converting an EC point in compressed
681 * format to an uncompressed one
682 */
683static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
684 const unsigned char *in_start, size_t in_len,
685 size_t *out_buf_len, unsigned char *out_buf,
686 size_t out_buf_size)
687{
688 mbedtls_ecp_keypair ecp_key;
689 mbedtls_ecp_group_id ecp_group_id;
690 int ret;
691
692 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
693
694 mbedtls_ecp_keypair_init(&ecp_key);
695 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
696 if (ret != 0) {
697 return ret;
698 }
699 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
700 in_start, in_len);
701 if (ret != 0) {
702 goto exit;
703 }
704 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
705 MBEDTLS_ECP_PF_UNCOMPRESSED,
706 out_buf_len, out_buf, out_buf_size);
707
708exit:
709 mbedtls_ecp_keypair_free(&ecp_key);
710 return ret;
711}
Valerio Settiaddeee42023-06-14 10:46:55 +0200712#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200713
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714/*
715 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100716 *
717 * The caller is responsible for clearing the structure upon failure if
718 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200722 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723{
Janos Follath24eed8d2019-11-22 13:21:35 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
727 mbedtls_svc_key_id_t key;
728 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
729 size_t len = (end - *p);
730
731 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
732 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 }
734
Valerio Setti4064dbb2023-05-17 15:33:07 +0200735 /* Compressed point format are not supported yet by PSA crypto. As a
736 * consequence ecp functions are used to "convert" the point to
737 * uncompressed format */
738 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200739#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200740 ret = pk_convert_compressed_ec(pk, *p, len,
741 &(pk->pub_raw_len), pk->pub_raw,
742 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
743 if (ret != 0) {
744 return ret;
745 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200746#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
747 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
748#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200749 } else {
750 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100751 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200752 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200753 }
754 memcpy(pk->pub_raw, *p, (end - *p));
755 pk->pub_raw_len = end - *p;
756 }
757
758 /* Validate the key by trying to importing it */
759 psa_set_key_usage_flags(&key_attrs, 0);
760 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
761 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
762 psa_set_key_bits(&key_attrs, pk->ec_bits);
763
764 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
765 &key) != PSA_SUCCESS) ||
766 (psa_destroy_key(key) != PSA_SUCCESS)) {
767 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
768 pk->pub_raw_len = 0;
769 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
770 }
771 ret = 0;
772#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
773 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
774 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
775 (const unsigned char *) *p,
776 end - *p)) == 0) {
777 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
778 }
779#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
780
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200783 */
784 *p = (unsigned char *) end;
785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787}
Valerio Setti81d75122023-06-14 14:49:33 +0200788#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791/*
792 * RSAPublicKey ::= SEQUENCE {
793 * modulus INTEGER, -- n
794 * publicExponent INTEGER -- e
795 * }
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797static int pk_get_rsapubkey(unsigned char **p,
798 const unsigned char *end,
799 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800{
Janos Follath24eed8d2019-11-22 13:21:35 +0000801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802 size_t len;
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
805 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
807 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (*p + len != end) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
811 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
812 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
817 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
820 NULL, 0, NULL, 0)) != 0) {
821 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
822 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100823
824 *p += len;
825
826 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
829 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
832 NULL, 0, *p, len)) != 0) {
833 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
834 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100835
836 *p += len;
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (mbedtls_rsa_complete(rsa) != 0 ||
839 mbedtls_rsa_check_pubkey(rsa) != 0) {
840 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000841 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (*p != end) {
844 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
845 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
846 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
852/* Get a PK algorithm identifier
853 *
854 * AlgorithmIdentifier ::= SEQUENCE {
855 * algorithm OBJECT IDENTIFIER,
856 * parameters ANY DEFINED BY algorithm OPTIONAL }
857 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static int pk_get_pk_alg(unsigned char **p,
859 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200860 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
861 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862{
Janos Follath24eed8d2019-11-22 13:21:35 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
869 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
870 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Jethro Beekman01672442023-04-19 14:08:14 +0200872 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200873#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200874 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
875 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
876 if (ret == 0) {
877 *pk_alg = MBEDTLS_PK_ECKEY;
878 }
879 }
880#else
881 (void) ec_grp_id;
882#endif
883 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
885 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
887 /*
888 * No parameters with RSA (only for EC)
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (*pk_alg == MBEDTLS_PK_RSA &&
891 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
892 params->len != 0)) {
893 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 }
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897}
898
899/*
900 * SubjectPublicKeyInfo ::= SEQUENCE {
901 * algorithm AlgorithmIdentifier,
902 * subjectPublicKey BIT STRING }
903 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
905 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906{
Janos Follath24eed8d2019-11-22 13:21:35 +0000907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_asn1_buf alg_params;
910 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200911 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
915 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
916 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917 }
918
919 end = *p + len;
920
Jethro Beekman01672442023-04-19 14:08:14 +0200921 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 return ret;
923 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
926 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (*p + len != end) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
931 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
932 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
935 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
939 return ret;
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (pk_alg == MBEDTLS_PK_RSA) {
944 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200947#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200949#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200950 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200951 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200952 } else
953#endif
954 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200955 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200956 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200958 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200961#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (ret == 0 && *p != end) {
965 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
966 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
967 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (ret != 0) {
970 mbedtls_pk_free(pk);
971 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974}
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100978 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
979 *
980 * The value zero is:
981 * - never a valid value for an RSA parameter
982 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
983 *
984 * Since values can't be omitted in PKCS#1, passing a zero value to
985 * rsa_complete() would be incorrect, so reject zero values early.
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987static int asn1_get_nonzero_mpi(unsigned char **p,
988 const unsigned char *end,
989 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100990{
991 int ret;
992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 ret = mbedtls_asn1_get_mpi(p, end, X);
994 if (ret != 0) {
995 return ret;
996 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
999 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1000 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001003}
1004
1005/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 * Parse a PKCS#1 encoded private RSA key
1007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1009 const unsigned char *key,
1010 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001012 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013 size_t len;
1014 unsigned char *p, *end;
1015
Hanno Beckerefa14e82017-10-11 19:45:19 +01001016 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001018
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019 p = (unsigned char *) key;
1020 end = p + keylen;
1021
1022 /*
1023 * This function parses the RSAPrivateKey (PKCS#1)
1024 *
1025 * RSAPrivateKey ::= SEQUENCE {
1026 * version Version,
1027 * modulus INTEGER, -- n
1028 * publicExponent INTEGER, -- e
1029 * privateExponent INTEGER, -- d
1030 * prime1 INTEGER, -- p
1031 * prime2 INTEGER, -- q
1032 * exponent1 INTEGER, -- d mod (p-1)
1033 * exponent2 INTEGER, -- d mod (q-1)
1034 * coefficient INTEGER, -- (inverse of q) mod p
1035 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1036 * }
1037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1039 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1040 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041 }
1042
1043 end = p + len;
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1046 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 }
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (version != 0) {
1050 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 }
1052
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001053 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1056 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1062 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1063 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066
1067 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1069 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1070 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001071 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001073
1074 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1076 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1077 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001078 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001080
1081 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1083 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1084 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001085 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001087
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001088#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001089 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1091 * that they can be easily recomputed from D, P and Q. However by
1092 * parsing them from the PKCS1 structure it is possible to avoid
1093 * recalculating them which both reduces the overhead of loading
1094 * RSA private keys into memory and also avoids side channels which
1095 * can arise when computing those values, since all of D, P, and Q
1096 * are secret. See https://eprint.iacr.org/2020/055 for a
1097 * description of one such attack.
1098 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001099
Jack Lloyd80cc8112020-01-22 17:34:29 -05001100 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1102 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1103 goto cleanup;
1104 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001105
1106 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1108 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1109 goto cleanup;
1110 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001111
1112 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1114 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1115 goto cleanup;
1116 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001117
Jack Lloyd60239752020-01-27 17:53:36 -05001118#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001119 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1121 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1122 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1123 goto cleanup;
1124 }
Jack Lloyd60239752020-01-27 17:53:36 -05001125#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001126
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001127 /* rsa_complete() doesn't complete anything with the default
1128 * implementation but is still called:
1129 * - for the benefit of alternative implementation that may want to
1130 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1131 * - as is also sanity-checks the key
1132 *
1133 * Furthermore, we also check the public part for consistency with
1134 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1137 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001138 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001139 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (p != end) {
1142 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1143 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 }
1145
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001146cleanup:
1147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001151 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((ret & 0xff80) == 0) {
1153 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1154 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001155 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
Valerio Setti81d75122023-06-14 14:49:33 +02001165#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166/*
1167 * Parse a SEC1 encoded private EC key
1168 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001169static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 const unsigned char *key, size_t keylen,
1171 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172{
Janos Follath24eed8d2019-11-22 13:21:35 +00001173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001174 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001175 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001176 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001178 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 unsigned char *end = p + keylen;
1180 unsigned char *end2;
1181
1182 /*
1183 * RFC 5915, or SEC1 Appendix C.4
1184 *
1185 * ECPrivateKey ::= SEQUENCE {
1186 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1187 * privateKey OCTET STRING,
1188 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1189 * publicKey [1] BIT STRING OPTIONAL
1190 * }
1191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1193 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 }
1196
1197 end = p + len;
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1201 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (version != 1) {
1204 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210
Valerio Setti6b062ee2023-06-30 17:32:57 +02001211 /* Keep a reference to the position fo the private key. It will be used
1212 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001213 d = p;
1214 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001215
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 p += len;
1217
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001220 /*
1221 * Is 'parameters' present?
1222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1224 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1225 0)) == 0) {
1226 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001227 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001232 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001233 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001234
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001235 /*
1236 * Load the private key
1237 */
1238 ret = pk_ecc_set_key(pk, d, d_len);
1239 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001240 return ret;
1241 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 /*
1245 * Is 'publickey' present? If not, or if we can't read it (eg because it
1246 * is compressed), create it from the private key.
1247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1249 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1250 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001251 end2 = p + len;
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1254 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1255 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (p + len != end2) {
1258 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1259 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1260 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001261
Valerio Setti4064dbb2023-05-17 15:33:07 +02001262 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001263 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001265 /*
1266 * The only acceptable failure mode of pk_get_ecpubkey() above
1267 * is if the point format is not recognized.
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1270 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1271 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001272 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001275 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001277
Valerio Setti34f67552023-04-03 15:19:18 +02001278 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001279 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001280 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001281 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001282 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285}
Valerio Setti81d75122023-06-14 14:49:33 +02001286#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
1288/*
1289 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001290 *
1291 * Notes:
1292 *
1293 * - This function does not own the key buffer. It is the
1294 * responsibility of the caller to take care of zeroizing
1295 * and freeing it after use.
1296 *
1297 * - The function is responsible for freeing the provided
1298 * PK context on failure.
1299 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 */
1301static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 mbedtls_pk_context *pk,
1303 const unsigned char *key, size_t keylen,
1304 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305{
1306 int ret, version;
1307 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 unsigned char *p = (unsigned char *) key;
1310 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001312 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314
Valerio Setti81d75122023-06-14 14:49:33 +02001315#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001316 (void) f_rng;
1317 (void) p_rng;
1318#endif
1319
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001321 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 *
1323 * PrivateKeyInfo ::= SEQUENCE {
1324 * version Version,
1325 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1326 * privateKey PrivateKey,
1327 * attributes [0] IMPLICIT Attributes OPTIONAL }
1328 *
1329 * Version ::= INTEGER
1330 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1331 * PrivateKey ::= OCTET STRING
1332 *
1333 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1334 */
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1337 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 }
1340
1341 end = p + len;
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001345 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (version != 0) {
1348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1349 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350
Jethro Beekman01672442023-04-19 14:08:14 +02001351 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 return ret;
1353 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1356 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1357 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (len < 1) {
1360 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1361 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1362 }
1363
1364 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1365 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1366 }
1367
1368 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1369 return ret;
1370 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (pk_alg == MBEDTLS_PK_RSA) {
1374 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1375 mbedtls_pk_free(pk);
1376 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 }
1378 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001380#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001382#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001383 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001384 if ((ret =
1385 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001386 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001387 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001388 p_rng)) != 0) {
1389 mbedtls_pk_free(pk);
1390 return ret;
1391 }
1392 } else
1393#endif
1394 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001395 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1396 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001397 mbedtls_pk_free(pk);
1398 return ret;
1399 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 }
1401 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001402#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001405 end = p + len;
1406 if (end != (key + keylen)) {
1407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1408 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1409 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412}
1413
1414/*
1415 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001416 *
1417 * To save space, the decryption happens in-place on the given key buffer.
1418 * Also, while this function may modify the keybuffer, it doesn't own it,
1419 * and instead it is the responsibility of the caller to zeroize and properly
1420 * free it after use.
1421 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001424MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 mbedtls_pk_context *pk,
1426 unsigned char *key, size_t keylen,
1427 const unsigned char *pwd, size_t pwdlen,
1428 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001430 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001432 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1435#if defined(MBEDTLS_PKCS12_C)
1436 mbedtls_cipher_type_t cipher_alg;
1437 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001439 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Hanno Becker2aa80a72017-09-07 15:28:45 +01001441 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 end = p + keylen;
1443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (pwdlen == 0) {
1445 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
1448 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001449 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450 *
1451 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1452 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1453 * encryptedData EncryptedData
1454 * }
1455 *
1456 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1457 *
1458 * EncryptedData ::= OCTET STRING
1459 *
1460 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001461 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1464 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466 }
1467
1468 end = p + len;
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1471 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1472 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1475 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1476 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477
Hanno Beckerfab35692017-08-25 13:38:26 +01001478 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479
1480 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001481 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001485 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001486 cipher_alg, md_alg,
1487 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1489 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1490 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001494
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001495 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_PKCS12_C */
1498#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001500 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1501 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1503 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001508
1509 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001512 {
1513 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001514 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (decrypted == 0) {
1517 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1518 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001519 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522
1523/*
1524 * Parse a private key
1525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1527 const unsigned char *key, size_t keylen,
1528 const unsigned char *pwd, size_t pwdlen,
1529 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530{
Janos Follath24eed8d2019-11-22 13:21:35 +00001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001536#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (keylen == 0) {
1539 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1540 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001541
1542#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001546 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001548 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 } else {
1550 ret = mbedtls_pem_read_buffer(&pem,
1551 "-----BEGIN RSA PRIVATE KEY-----",
1552 "-----END RSA PRIVATE KEY-----",
1553 key, pwd, pwdlen, &len);
1554 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (ret == 0) {
1557 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1558 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1559 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1560 pem.buf, pem.buflen)) != 0) {
1561 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562 }
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 mbedtls_pem_free(&pem);
1565 return ret;
1566 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1567 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1568 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1569 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1570 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1571 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574
Valerio Setti81d75122023-06-14 14:49:33 +02001575#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001576 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001578 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 } else {
1580 ret = mbedtls_pem_read_buffer(&pem,
1581 "-----BEGIN EC PRIVATE KEY-----",
1582 "-----END EC PRIVATE KEY-----",
1583 key, pwd, pwdlen, &len);
1584 }
1585 if (ret == 0) {
1586 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001589 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 pem.buf, pem.buflen,
1591 f_rng, p_rng)) != 0) {
1592 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001593 }
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_pem_free(&pem);
1596 return ret;
1597 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1598 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1599 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1600 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1601 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1602 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603 }
Valerio Setti81d75122023-06-14 14:49:33 +02001604#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001605
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001606 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001608 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 } else {
1610 ret = mbedtls_pem_read_buffer(&pem,
1611 "-----BEGIN PRIVATE KEY-----",
1612 "-----END PRIVATE KEY-----",
1613 key, NULL, 0, &len);
1614 }
1615 if (ret == 0) {
1616 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1617 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1618 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619 }
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 mbedtls_pem_free(&pem);
1622 return ret;
1623 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1624 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001625 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001628 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001630 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 } else {
1632 ret = mbedtls_pem_read_buffer(&pem,
1633 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1634 "-----END ENCRYPTED PRIVATE KEY-----",
1635 key, NULL, 0, &len);
1636 }
1637 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001638 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1639 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641 }
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_pem_free(&pem);
1644 return ret;
1645 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1646 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001649#else
1650 ((void) pwd);
1651 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653
1654 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001655 * At this point we only know it's not a PEM formatted key. Could be any
1656 * of the known DER encoded private key formats
1657 *
1658 * We try the different DER format parsers to see if one passes without
1659 * error
1660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001663 unsigned char *key_copy;
1664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1666 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1667 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001670
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001671 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1672 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001673
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001674 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001675 }
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (ret == 0) {
1678 return 0;
1679 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 mbedtls_pk_free(pk);
1682 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1685 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1690 if (ret == 0) {
1691 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001692 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_pk_free(pk);
1695 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1700 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1701 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1702 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001703 }
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 mbedtls_pk_free(pk);
1706 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708
Valerio Setti81d75122023-06-14 14:49:33 +02001709#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1711 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001712 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 key, keylen, f_rng, p_rng) == 0) {
1714 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001717#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718
Valerio Setti81d75122023-06-14 14:49:33 +02001719 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001720 * it is ok to leave the PK context initialized but not
1721 * freed: It is the caller's responsibility to call pk_init()
1722 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001723 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001724 * isn't, this leads to mbedtls_pk_free() being called
1725 * twice, once here and once by the caller, but this is
1726 * also ok and in line with the mbedtls_pk_free() calls
1727 * on failed PEM parsing attempts. */
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730}
1731
1732/*
1733 * Parse a public key
1734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1736 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001737{
Janos Follath24eed8d2019-11-22 13:21:35 +00001738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001739 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001740#if defined(MBEDTLS_RSA_C)
1741 const mbedtls_pk_info_t *pk_info;
1742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001744 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001746#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (keylen == 0) {
1749 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1750 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001751
1752#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001754#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001755 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001757 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 } else {
1759 ret = mbedtls_pem_read_buffer(&pem,
1760 "-----BEGIN RSA PUBLIC KEY-----",
1761 "-----END RSA PUBLIC KEY-----",
1762 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001763 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001764
1765 if (ret == 0) {
1766 p = pem.buf;
1767 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1768 mbedtls_pem_free(&pem);
1769 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1770 }
1771
1772 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1773 mbedtls_pem_free(&pem);
1774 return ret;
1775 }
1776
1777 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1778 mbedtls_pk_free(ctx);
1779 }
1780
1781 mbedtls_pem_free(&pem);
1782 return ret;
1783 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1784 mbedtls_pem_free(&pem);
1785 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001786 }
1787#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001788
1789 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001791 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 } else {
1793 ret = mbedtls_pem_read_buffer(&pem,
1794 "-----BEGIN PUBLIC KEY-----",
1795 "-----END PUBLIC KEY-----",
1796 key, NULL, 0, &len);
1797 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001800 /*
1801 * Was PEM encoded
1802 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001803 p = pem.buf;
1804
Jethro Beekman01672442023-04-19 14:08:14 +02001805 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 mbedtls_pem_free(&pem);
1807 return ret;
1808 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1809 mbedtls_pem_free(&pem);
1810 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001814
1815#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1817 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001819
1820 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1821 return ret;
1822 }
1823
1824 p = (unsigned char *) key;
1825 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1826 if (ret == 0) {
1827 return ret;
1828 }
1829 mbedtls_pk_free(ctx);
1830 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1831 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1832 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001833 }
1834#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001835 p = (unsigned char *) key;
1836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001840}
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#endif /* MBEDTLS_PK_PARSE_C */