blob: d5ffc862d8b9dcaf0a0bc4c878c976a1b74025be [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/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 *
493 * ECParameters ::= CHOICE {
494 * namedCurve OBJECT IDENTIFIER
495 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
496 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200498static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (params->tag == MBEDTLS_ASN1_OID) {
504 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
505 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
506 }
507 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
510 return ret;
511 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100512#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100514#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200517 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518}
519
Jethro Beekman01672442023-04-19 14:08:14 +0200520/*
521 * Helper function for deriving a public key from its private counterpart.
522 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200523static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200524 const unsigned char *d, size_t d_len,
525 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
526{
527 int ret;
528#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200529 psa_status_t status;
530 (void) f_rng;
531 (void) p_rng;
532#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
533 (void) d;
534 (void) d_len;
535
536 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
537 &pk->pub_raw_len);
538 ret = psa_pk_status_to_mbedtls(status);
539#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
540 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
541 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
542 size_t key_len;
543 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200544 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
545 size_t curve_bits;
546 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200547 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200548
549 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
550 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
551
552 status = psa_import_key(&key_attr, d, d_len, &key_id);
553 ret = psa_pk_status_to_mbedtls(status);
554 if (ret != 0) {
555 return ret;
556 }
557
Jethro Beekman01672442023-04-19 14:08:14 +0200558 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
559 ret = psa_pk_status_to_mbedtls(status);
560 destruction_status = psa_destroy_key(key_id);
561 if (ret != 0) {
562 return ret;
563 } else if (destruction_status != PSA_SUCCESS) {
564 return psa_pk_status_to_mbedtls(destruction_status);
565 }
Jethro Beekman01672442023-04-19 14:08:14 +0200566 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200567#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200568#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200569 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200570 (void) d;
571 (void) d_len;
572
573 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
574#endif /* MBEDTLS_USE_PSA_CRYPTO */
575 return ret;
576}
577
578#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
579
580/*
581 * Load an RFC8410 EC key, which doesn't have any parameters
582 */
583static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
584 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200585 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200586{
587 if (params->tag != 0 || params->len != 0) {
588 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
589 }
590
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200591 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200592}
593
594/*
595 * Parse an RFC 8410 encoded private EC key
596 *
597 * CurvePrivateKey ::= OCTET STRING
598 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200599static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200600 unsigned char *key, size_t keylen, const unsigned char *end,
601 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
602{
603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
604 size_t len;
605
606 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
607 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
608 }
609
610 if (key + len != end) {
611 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
612 }
613
Valerio Setti00e8dd12023-05-18 18:56:59 +0200614#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
616 psa_status_t status;
617
618 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200619 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
620 PSA_KEY_USAGE_DERIVE);
621 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200622
623 status = psa_import_key(&attributes, key, len, &pk->priv_id);
624 if (status != PSA_SUCCESS) {
625 ret = psa_pk_status_to_mbedtls(status);
626 return ret;
627 }
628#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
629 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
630
Valerio Setti805e4a02023-06-30 17:16:19 +0200631 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200632 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
633 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200634#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200635
Valerio Setti4064dbb2023-05-17 15:33:07 +0200636 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
637 * which never contain a public key. As such, derive the public key
638 * unconditionally. */
639 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200640 return ret;
641 }
642
Jethro Beekman01672442023-04-19 14:08:14 +0200643 return 0;
644}
645#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200646
Valerio Settiaddeee42023-06-14 10:46:55 +0200647#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200648/*
649 * Create a temporary ecp_keypair for converting an EC point in compressed
650 * format to an uncompressed one
651 */
652static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
653 const unsigned char *in_start, size_t in_len,
654 size_t *out_buf_len, unsigned char *out_buf,
655 size_t out_buf_size)
656{
657 mbedtls_ecp_keypair ecp_key;
658 mbedtls_ecp_group_id ecp_group_id;
659 int ret;
660
661 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
662
663 mbedtls_ecp_keypair_init(&ecp_key);
664 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
665 if (ret != 0) {
666 return ret;
667 }
668 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
669 in_start, in_len);
670 if (ret != 0) {
671 goto exit;
672 }
673 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
674 MBEDTLS_ECP_PF_UNCOMPRESSED,
675 out_buf_len, out_buf, out_buf_size);
676
677exit:
678 mbedtls_ecp_keypair_free(&ecp_key);
679 return ret;
680}
Valerio Settiaddeee42023-06-14 10:46:55 +0200681#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200682
Paul Bakker1a7550a2013-09-15 13:01:22 +0200683/*
684 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100685 *
686 * The caller is responsible for clearing the structure upon failure if
687 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200691 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692{
Janos Follath24eed8d2019-11-22 13:21:35 +0000693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694
Valerio Setti4064dbb2023-05-17 15:33:07 +0200695#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
696 mbedtls_svc_key_id_t key;
697 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
698 size_t len = (end - *p);
699
700 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
701 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702 }
703
Valerio Setti4064dbb2023-05-17 15:33:07 +0200704 /* Compressed point format are not supported yet by PSA crypto. As a
705 * consequence ecp functions are used to "convert" the point to
706 * uncompressed format */
707 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200708#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200709 ret = pk_convert_compressed_ec(pk, *p, len,
710 &(pk->pub_raw_len), pk->pub_raw,
711 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
712 if (ret != 0) {
713 return ret;
714 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200715#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
716 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
717#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200718 } else {
719 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100720 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200721 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200722 }
723 memcpy(pk->pub_raw, *p, (end - *p));
724 pk->pub_raw_len = end - *p;
725 }
726
727 /* Validate the key by trying to importing it */
728 psa_set_key_usage_flags(&key_attrs, 0);
729 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
730 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
731 psa_set_key_bits(&key_attrs, pk->ec_bits);
732
733 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
734 &key) != PSA_SUCCESS) ||
735 (psa_destroy_key(key) != PSA_SUCCESS)) {
736 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
737 pk->pub_raw_len = 0;
738 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
739 }
740 ret = 0;
741#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
742 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
743 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
744 (const unsigned char *) *p,
745 end - *p)) == 0) {
746 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
747 }
748#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
749
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752 */
753 *p = (unsigned char *) end;
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756}
Valerio Setti81d75122023-06-14 14:49:33 +0200757#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760/*
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER -- e
764 * }
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int pk_get_rsapubkey(unsigned char **p,
767 const unsigned char *end,
768 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769{
Janos Follath24eed8d2019-11-22 13:21:35 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200771 size_t len;
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
774 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
776 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (*p + len != end) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
781 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100783 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
786 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
789 NULL, 0, NULL, 0)) != 0) {
790 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
791 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100792
793 *p += len;
794
795 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
797 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
798 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
801 NULL, 0, *p, len)) != 0) {
802 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
803 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100804
805 *p += len;
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (mbedtls_rsa_complete(rsa) != 0 ||
808 mbedtls_rsa_check_pubkey(rsa) != 0) {
809 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000810 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (*p != end) {
813 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
814 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820
821/* Get a PK algorithm identifier
822 *
823 * AlgorithmIdentifier ::= SEQUENCE {
824 * algorithm OBJECT IDENTIFIER,
825 * parameters ANY DEFINED BY algorithm OPTIONAL }
826 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827static int pk_get_pk_alg(unsigned char **p,
828 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200829 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
830 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831{
Janos Follath24eed8d2019-11-22 13:21:35 +0000832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
838 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
839 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Jethro Beekman01672442023-04-19 14:08:14 +0200841 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200842#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200843 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
844 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
845 if (ret == 0) {
846 *pk_alg = MBEDTLS_PK_ECKEY;
847 }
848 }
849#else
850 (void) ec_grp_id;
851#endif
852 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
854 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
856 /*
857 * No parameters with RSA (only for EC)
858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (*pk_alg == MBEDTLS_PK_RSA &&
860 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
861 params->len != 0)) {
862 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 }
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866}
867
868/*
869 * SubjectPublicKeyInfo ::= SEQUENCE {
870 * algorithm AlgorithmIdentifier,
871 * subjectPublicKey BIT STRING }
872 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
874 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875{
Janos Follath24eed8d2019-11-22 13:21:35 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_asn1_buf alg_params;
879 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200880 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
884 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
885 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 }
887
888 end = *p + len;
889
Jethro Beekman01672442023-04-19 14:08:14 +0200890 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return ret;
892 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
895 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
896 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (*p + len != end) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
900 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
901 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
904 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
908 return ret;
909 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (pk_alg == MBEDTLS_PK_RSA) {
913 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200916#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200918#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200919 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200920 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200921 } else
922#endif
923 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200924 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200925 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200927 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200930#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (ret == 0 && *p != end) {
934 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (ret != 0) {
939 mbedtls_pk_free(pk);
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943}
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200946/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100947 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
948 *
949 * The value zero is:
950 * - never a valid value for an RSA parameter
951 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
952 *
953 * Since values can't be omitted in PKCS#1, passing a zero value to
954 * rsa_complete() would be incorrect, so reject zero values early.
955 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956static int asn1_get_nonzero_mpi(unsigned char **p,
957 const unsigned char *end,
958 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100959{
960 int ret;
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ret = mbedtls_asn1_get_mpi(p, end, X);
963 if (ret != 0) {
964 return ret;
965 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
968 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
969 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100972}
973
974/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975 * Parse a PKCS#1 encoded private RSA key
976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
978 const unsigned char *key,
979 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100981 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 size_t len;
983 unsigned char *p, *end;
984
Hanno Beckerefa14e82017-10-11 19:45:19 +0100985 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 p = (unsigned char *) key;
989 end = p + keylen;
990
991 /*
992 * This function parses the RSAPrivateKey (PKCS#1)
993 *
994 * RSAPrivateKey ::= SEQUENCE {
995 * version Version,
996 * modulus INTEGER, -- n
997 * publicExponent INTEGER, -- e
998 * privateExponent INTEGER, -- d
999 * prime1 INTEGER, -- p
1000 * prime2 INTEGER, -- q
1001 * exponent1 INTEGER, -- d mod (p-1)
1002 * exponent2 INTEGER, -- d mod (q-1)
1003 * coefficient INTEGER, -- (inverse of q) mod p
1004 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1005 * }
1006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1008 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1009 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010 }
1011
1012 end = p + len;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1015 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 }
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if (version != 0) {
1019 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001022 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1024 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1025 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001026 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001029 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1031 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1032 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001033 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001035
1036 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1038 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1039 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001040 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001042
1043 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1045 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1046 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001047 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001049
1050 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1052 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1053 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001054 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001057#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001058 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1060 * that they can be easily recomputed from D, P and Q. However by
1061 * parsing them from the PKCS1 structure it is possible to avoid
1062 * recalculating them which both reduces the overhead of loading
1063 * RSA private keys into memory and also avoids side channels which
1064 * can arise when computing those values, since all of D, P, and Q
1065 * are secret. See https://eprint.iacr.org/2020/055 for a
1066 * description of one such attack.
1067 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001068
Jack Lloyd80cc8112020-01-22 17:34:29 -05001069 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1071 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1072 goto cleanup;
1073 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001074
1075 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1077 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1078 goto cleanup;
1079 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001080
1081 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1083 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1084 goto cleanup;
1085 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001086
Jack Lloyd60239752020-01-27 17:53:36 -05001087#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001088 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1090 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1091 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1092 goto cleanup;
1093 }
Jack Lloyd60239752020-01-27 17:53:36 -05001094#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001095
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001096 /* rsa_complete() doesn't complete anything with the default
1097 * implementation but is still called:
1098 * - for the benefit of alternative implementation that may want to
1099 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1100 * - as is also sanity-checks the key
1101 *
1102 * Furthermore, we also check the public part for consistency with
1103 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1106 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001107 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001108 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (p != end) {
1111 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1112 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 }
1114
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001115cleanup:
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001120 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if ((ret & 0xff80) == 0) {
1122 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1123 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001124 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133
Valerio Setti81d75122023-06-14 14:49:33 +02001134#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135/*
1136 * Parse a SEC1 encoded private EC key
1137 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001138static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 const unsigned char *key, size_t keylen,
1140 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141{
Janos Follath24eed8d2019-11-22 13:21:35 +00001142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001143 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001144 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001145 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001147 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 unsigned char *end = p + keylen;
1149 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001150#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1152 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001153#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1154 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001155#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156
1157 /*
1158 * RFC 5915, or SEC1 Appendix C.4
1159 *
1160 * ECPrivateKey ::= SEQUENCE {
1161 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1162 * privateKey OCTET STRING,
1163 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1164 * publicKey [1] BIT STRING OPTIONAL
1165 * }
1166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1169 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 }
1171
1172 end = p + len;
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1176 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (version != 1) {
1179 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1180 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1184 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Valerio Setti6b062ee2023-06-30 17:32:57 +02001186 /* Keep a reference to the position fo the private key. It will be used
1187 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001188 d = p;
1189 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001190
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 p += len;
1192
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001193 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001195 /*
1196 * Is 'parameters' present?
1197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1199 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1200 0)) == 0) {
1201 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001202 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001207 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001208 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001209
Valerio Setti6b062ee2023-06-30 17:32:57 +02001210
1211#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1212 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1213 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1214 }
1215#endif
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218 /*
1219 * Is 'publickey' present? If not, or if we can't read it (eg because it
1220 * is compressed), create it from the private key.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1223 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1224 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001225 end2 = p + len;
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1229 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (p + len != end2) {
1232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1233 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1234 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235
Valerio Setti4064dbb2023-05-17 15:33:07 +02001236 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001239 /*
1240 * The only acceptable failure mode of pk_get_ecpubkey() above
1241 * is if the point format is not recognized.
1242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1244 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1245 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001251
Valerio Setti00e8dd12023-05-18 18:56:59 +02001252#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1253 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1254 /* Setting largest masks for usage and key algorithms */
1255 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1256 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001257 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001258#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1259 psa_set_key_algorithm(&attributes,
1260 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1261#else
1262 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1263#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001264 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001265
Valerio Settia541e012023-05-24 14:31:21 +02001266 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001267 if (status != PSA_SUCCESS) {
1268 ret = psa_pk_status_to_mbedtls(status);
1269 return ret;
1270 }
1271#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1272
Valerio Setti34f67552023-04-03 15:19:18 +02001273 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001274 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001275 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001276 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001277 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280}
Valerio Setti81d75122023-06-14 14:49:33 +02001281#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282
1283/*
1284 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001285 *
1286 * Notes:
1287 *
1288 * - This function does not own the key buffer. It is the
1289 * responsibility of the caller to take care of zeroizing
1290 * and freeing it after use.
1291 *
1292 * - The function is responsible for freeing the provided
1293 * PK context on failure.
1294 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295 */
1296static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 mbedtls_pk_context *pk,
1298 const unsigned char *key, size_t keylen,
1299 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300{
1301 int ret, version;
1302 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 unsigned char *p = (unsigned char *) key;
1305 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001307 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309
Valerio Setti81d75122023-06-14 14:49:33 +02001310#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001311 (void) f_rng;
1312 (void) p_rng;
1313#endif
1314
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001316 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 *
1318 * PrivateKeyInfo ::= SEQUENCE {
1319 * version Version,
1320 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1321 * privateKey PrivateKey,
1322 * attributes [0] IMPLICIT Attributes OPTIONAL }
1323 *
1324 * Version ::= INTEGER
1325 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1326 * PrivateKey ::= OCTET STRING
1327 *
1328 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1329 */
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1332 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 }
1335
1336 end = p + len;
1337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001340 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (version != 0) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Jethro Beekman01672442023-04-19 14:08:14 +02001346 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return ret;
1348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if (len < 1) {
1355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1356 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1357 }
1358
1359 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1360 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1361 }
1362
1363 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1364 return ret;
1365 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (pk_alg == MBEDTLS_PK_RSA) {
1369 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1370 mbedtls_pk_free(pk);
1371 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 }
1373 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001375#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001377#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001378 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001379 if ((ret =
1380 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001381 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001382 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001383 p_rng)) != 0) {
1384 mbedtls_pk_free(pk);
1385 return ret;
1386 }
1387 } else
1388#endif
1389 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001390 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1391 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001392 mbedtls_pk_free(pk);
1393 return ret;
1394 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001395 }
1396 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001397#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001400 end = p + len;
1401 if (end != (key + keylen)) {
1402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1403 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1404 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407}
1408
1409/*
1410 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001411 *
1412 * To save space, the decryption happens in-place on the given key buffer.
1413 * Also, while this function may modify the keybuffer, it doesn't own it,
1414 * and instead it is the responsibility of the caller to zeroize and properly
1415 * free it after use.
1416 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001419MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 mbedtls_pk_context *pk,
1421 unsigned char *key, size_t keylen,
1422 const unsigned char *pwd, size_t pwdlen,
1423 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001425 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001427 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1430#if defined(MBEDTLS_PKCS12_C)
1431 mbedtls_cipher_type_t cipher_alg;
1432 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001434 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435
Hanno Becker2aa80a72017-09-07 15:28:45 +01001436 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 end = p + keylen;
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (pwdlen == 0) {
1440 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1441 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
1443 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001444 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 *
1446 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1447 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1448 * encryptedData EncryptedData
1449 * }
1450 *
1451 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1452 *
1453 * EncryptedData ::= OCTET STRING
1454 *
1455 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001456 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1459 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 }
1462
1463 end = p + len;
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1466 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1467 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1470 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1471 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472
Hanno Beckerfab35692017-08-25 13:38:26 +01001473 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474
1475 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001476 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001480 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001481 cipher_alg, md_alg,
1482 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1484 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1485 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001489
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001490 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PKCS12_C */
1493#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001495 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1496 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1498 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1499 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001503
1504 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001507 {
1508 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001509 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (decrypted == 0) {
1512 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1513 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001514 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517
1518/*
1519 * Parse a private key
1520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1522 const unsigned char *key, size_t keylen,
1523 const unsigned char *pwd, size_t pwdlen,
1524 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525{
Janos Follath24eed8d2019-11-22 13:21:35 +00001526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001531#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (keylen == 0) {
1534 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1535 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001536
1537#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001541 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001543 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 } else {
1545 ret = mbedtls_pem_read_buffer(&pem,
1546 "-----BEGIN RSA PRIVATE KEY-----",
1547 "-----END RSA PRIVATE KEY-----",
1548 key, pwd, pwdlen, &len);
1549 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (ret == 0) {
1552 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1553 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1554 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1555 pem.buf, pem.buflen)) != 0) {
1556 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001557 }
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_pem_free(&pem);
1560 return ret;
1561 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1562 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1563 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1564 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1565 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1566 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001569
Valerio Setti81d75122023-06-14 14:49:33 +02001570#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001571 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001573 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 } else {
1575 ret = mbedtls_pem_read_buffer(&pem,
1576 "-----BEGIN EC PRIVATE KEY-----",
1577 "-----END EC PRIVATE KEY-----",
1578 key, pwd, pwdlen, &len);
1579 }
1580 if (ret == 0) {
1581 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001584 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 pem.buf, pem.buflen,
1586 f_rng, p_rng)) != 0) {
1587 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001588 }
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 mbedtls_pem_free(&pem);
1591 return ret;
1592 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1593 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1594 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1595 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1596 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1597 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598 }
Valerio Setti81d75122023-06-14 14:49:33 +02001599#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001600
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001601 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001603 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 } else {
1605 ret = mbedtls_pem_read_buffer(&pem,
1606 "-----BEGIN PRIVATE KEY-----",
1607 "-----END PRIVATE KEY-----",
1608 key, NULL, 0, &len);
1609 }
1610 if (ret == 0) {
1611 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1612 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1613 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001614 }
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 mbedtls_pem_free(&pem);
1617 return ret;
1618 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1619 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001620 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001623 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001625 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 } else {
1627 ret = mbedtls_pem_read_buffer(&pem,
1628 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1629 "-----END ENCRYPTED PRIVATE KEY-----",
1630 key, NULL, 0, &len);
1631 }
1632 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001633 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1634 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636 }
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_pem_free(&pem);
1639 return ret;
1640 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1641 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644#else
1645 ((void) pwd);
1646 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001648
1649 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001650 * At this point we only know it's not a PEM formatted key. Could be any
1651 * of the known DER encoded private key formats
1652 *
1653 * We try the different DER format parsers to see if one passes without
1654 * error
1655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001658 unsigned char *key_copy;
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1661 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1662 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001665
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001666 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1667 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001668
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001669 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001670 }
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (ret == 0) {
1673 return 0;
1674 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 mbedtls_pk_free(pk);
1677 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1680 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1685 if (ret == 0) {
1686 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001687 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_pk_free(pk);
1690 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1695 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1696 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1697 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698 }
1699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 mbedtls_pk_free(pk);
1701 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001703
Valerio Setti81d75122023-06-14 14:49:33 +02001704#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1706 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001707 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 key, keylen, f_rng, p_rng) == 0) {
1709 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001712#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713
Valerio Setti81d75122023-06-14 14:49:33 +02001714 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001715 * it is ok to leave the PK context initialized but not
1716 * freed: It is the caller's responsibility to call pk_init()
1717 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001718 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001719 * isn't, this leads to mbedtls_pk_free() being called
1720 * twice, once here and once by the caller, but this is
1721 * also ok and in line with the mbedtls_pk_free() calls
1722 * on failed PEM parsing attempts. */
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725}
1726
1727/*
1728 * Parse a public key
1729 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1731 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001732{
Janos Follath24eed8d2019-11-22 13:21:35 +00001733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001734 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001735#if defined(MBEDTLS_RSA_C)
1736 const mbedtls_pk_info_t *pk_info;
1737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001739 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001741#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (keylen == 0) {
1744 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1745 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001746
1747#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001749#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001750 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001752 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 } else {
1754 ret = mbedtls_pem_read_buffer(&pem,
1755 "-----BEGIN RSA PUBLIC KEY-----",
1756 "-----END RSA PUBLIC KEY-----",
1757 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001759
1760 if (ret == 0) {
1761 p = pem.buf;
1762 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1763 mbedtls_pem_free(&pem);
1764 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1765 }
1766
1767 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1768 mbedtls_pem_free(&pem);
1769 return ret;
1770 }
1771
1772 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1773 mbedtls_pk_free(ctx);
1774 }
1775
1776 mbedtls_pem_free(&pem);
1777 return ret;
1778 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1779 mbedtls_pem_free(&pem);
1780 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001781 }
1782#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001783
1784 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001786 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 } else {
1788 ret = mbedtls_pem_read_buffer(&pem,
1789 "-----BEGIN PUBLIC KEY-----",
1790 "-----END PUBLIC KEY-----",
1791 key, NULL, 0, &len);
1792 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001795 /*
1796 * Was PEM encoded
1797 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001798 p = pem.buf;
1799
Jethro Beekman01672442023-04-19 14:08:14 +02001800 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_pem_free(&pem);
1802 return ret;
1803 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1804 mbedtls_pem_free(&pem);
1805 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001806 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001809
1810#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1812 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001813 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001814
1815 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1816 return ret;
1817 }
1818
1819 p = (unsigned char *) key;
1820 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1821 if (ret == 0) {
1822 return ret;
1823 }
1824 mbedtls_pk_free(ctx);
1825 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1826 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1827 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001828 }
1829#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001830 p = (unsigned char *) key;
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001835}
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#endif /* MBEDTLS_PK_PARSE_C */