blob: 1091c424e0098822d9813c0c3f32356d47271cd8 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Valerio Setti77a75682023-05-15 11:18:46 +020029#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Jethro Beekman01672442023-04-19 14:08:14 +020037#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
38#include "pkwrite.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Valerio Setti81d75122023-06-14 14:49:33 +020040#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti4064dbb2023-05-17 15:33:07 +020041#include "pk_internal.h"
42#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
55
Valerio Setti34f67552023-04-03 15:19:18 +020056#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020057#include "psa_util_internal.h"
Valerio Setti34f67552023-04-03 15:19:18 +020058#endif
59
60#if defined(MBEDTLS_USE_PSA_CRYPTO)
61#include "psa/crypto.h"
62#endif
63
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020065
Valerio Settie0e63112023-05-18 18:48:07 +020066/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020067#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020068#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
69 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020070#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020071
Gilles Peskine832f3492017-11-30 11:42:12 +010072#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073/*
74 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020075 *
76 * The file is expected to contain either PEM or DER encoded data.
77 * A terminating null byte is always appended. It is included in the announced
78 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020081{
82 FILE *f;
83 long size;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if ((f = fopen(path, "rb")) == NULL) {
86 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
87 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
Gilles Peskineda0913b2022-06-30 17:03:40 +020089 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fseek(f, 0, SEEK_END);
93 if ((size = ftell(f)) == -1) {
94 fclose(f);
95 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
Gilles Peskine449bd832023-01-11 14:50:10 +010097 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 *n = (size_t) size;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (*n + 1 == 0 ||
102 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
103 fclose(f);
104 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (fread(*buf, 1, *n, f) != *n) {
108 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_platform_zeroize(*buf, *n);
111 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
118 (*buf)[*n] = '\0';
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200121 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125}
126
127/*
128 * Load and parse a private key
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
131 const char *path, const char *pwd,
132 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133{
Janos Follath24eed8d2019-11-22 13:21:35 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 size_t n;
136 unsigned char *buf;
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
139 return ret;
140 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (pwd == NULL) {
143 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
144 } else {
145 ret = mbedtls_pk_parse_key(ctx, buf, n,
146 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_platform_zeroize(buf, n);
150 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153}
154
155/*
156 * Load and parse a public key
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159{
Janos Follath24eed8d2019-11-22 13:21:35 +0000160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 size_t n;
162 unsigned char *buf;
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
165 return ret;
166 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_platform_zeroize(buf, n);
171 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176
Valerio Setti81d75122023-06-14 14:49:33 +0200177#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179 *
180 * ECParameters ::= CHOICE {
181 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100182 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 * }
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
187 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188{
Janos Follath24eed8d2019-11-22 13:21:35 +0000189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (end - *p < 1) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
194 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100195
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 ) {
203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
204 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210
211 params->p = *p;
212 *p += params->len;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (*p != end) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
217 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
225 * WARNING: the resulting group should only be used with
226 * pk_group_id_from_specified(), since its base point may not be set correctly
227 * if it was encoded compressed.
228 *
229 * SpecifiedECDomain ::= SEQUENCE {
230 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
231 * fieldID FieldID {{FieldTypes}},
232 * curve Curve,
233 * base ECPoint,
234 * order INTEGER,
235 * cofactor INTEGER OPTIONAL,
236 * hash HashAlgorithm OPTIONAL,
237 * ...
238 * }
239 *
240 * We only support prime-field as field type, and ignore hash and cofactor.
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100243{
Janos Follath24eed8d2019-11-22 13:21:35 +0000244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200246 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100247 const unsigned char *end_field, *end_curve;
248 size_t len;
249 int ver;
250
251 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
254 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ver < 1 || ver > 3) {
257 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 /*
261 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
262 * fieldType FIELD-ID.&id({IOSet}),
263 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
264 * }
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
268 return ret;
269 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270
271 end_field = p + len;
272
273 /*
274 * FIELD-ID ::= TYPE-IDENTIFIER
275 * FieldTypes FIELD-ID ::= {
276 * { Prime-p IDENTIFIED BY prime-field } |
277 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
278 * }
279 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
286 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
287 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 }
289
290 p += len;
291
292 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
295 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (p != end_field) {
300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
302 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303
304 /*
305 * Curve ::= SEQUENCE {
306 * a FieldElement,
307 * b FieldElement,
308 * seed BIT STRING OPTIONAL
309 * -- Shall be present if used in SpecifiedECDomain
310 * -- with version equal to ecdpVer2 or ecdpVer3
311 * }
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
315 return ret;
316 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317
318 end_curve = p + len;
319
320 /*
321 * FieldElement ::= OCTET STRING
322 * containing an integer in the case of a prime field
323 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
325 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 }
328
329 p += len;
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
332 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 }
335
336 p += len;
337
338 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (p != end_curve) {
344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
345 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
346 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100347
348 /*
349 * ECPoint ::= OCTET STRING
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
353 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
356 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 /*
358 * If we can't read the point because it's compressed, cheat by
359 * reading only the X coordinate and the parity bit of Y.
360 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
362 (p[0] != 0x02 && p[0] != 0x03) ||
363 len != mbedtls_mpi_size(&grp->P) + 1 ||
364 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
365 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
366 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
367 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100368 }
369 }
370
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371 p += len;
372
373 /*
374 * order INTEGER
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
378 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
382 /*
383 * Allow optional elements by purposefully not enforcing p == end here.
384 */
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387}
388
389/*
390 * Find the group id associated with an (almost filled) group as generated by
391 * pk_group_from_specified(), or return an error if unknown.
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static 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 +0100394{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100395 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_group ref;
397 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_ecp_group_free(&ref);
404 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
408 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
409 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
410 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
411 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
412 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
413 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 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 +0100416 break;
417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418 }
419
420cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 *grp_id = *id;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430}
431
432/*
433 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
436 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000450 /* The API respecting lifecycle for mbedtls_ecp_group struct is
451 * _init(), _load() and _free(). In pk_group_id_from_specified() the
452 * temporary grp breaks that flow and it's members are populated
453 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
454 * which is assuming a group populated by _setup() may not clean-up
455 * properly -> Manually free it's members.
456 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000457 mbedtls_mpi_free(&grp.N);
458 mbedtls_mpi_free(&grp.P);
459 mbedtls_mpi_free(&grp.A);
460 mbedtls_mpi_free(&grp.B);
461 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
Valerio Setti4064dbb2023-05-17 15:33:07 +0200467#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
468/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
469 * ecp_keypair structure with proper group ID. The purpose of this helper
470 * function is to update ec_family and ec_bits accordingly. */
471static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
472 mbedtls_ecp_group_id grp_id)
473{
474 psa_ecc_family_t ec_family;
475 size_t bits;
476
477 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
478
479 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
481 }
482
483 pk->ec_family = ec_family;
484 pk->ec_bits = bits;
485
486 return 0;
487}
488#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
489
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
Valerio Setti00e8dd12023-05-18 18:56:59 +0200517#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
518 ret = pk_update_psa_ecparams(pk, grp_id);
519#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200520 /* grp may already be initialized; if so, make sure IDs match */
521 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
522 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
524 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525
Valerio Setti4064dbb2023-05-17 15:33:07 +0200526 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
527 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return ret;
529 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200530#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531
Valerio Setti4064dbb2023-05-17 15:33:07 +0200532 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533}
534
Jethro Beekman01672442023-04-19 14:08:14 +0200535/*
536 * Helper function for deriving a public key from its private counterpart.
537 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200538static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200539 const unsigned char *d, size_t d_len,
540 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
541{
542 int ret;
543#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200544 psa_status_t status;
545 (void) f_rng;
546 (void) p_rng;
547#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
548 (void) d;
549 (void) d_len;
550
551 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
552 &pk->pub_raw_len);
553 ret = psa_pk_status_to_mbedtls(status);
554#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
555 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
556 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
557 size_t key_len;
558 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200559 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
560 size_t curve_bits;
561 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200562 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200563
564 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
565 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
566
567 status = psa_import_key(&key_attr, d, d_len, &key_id);
568 ret = psa_pk_status_to_mbedtls(status);
569 if (ret != 0) {
570 return ret;
571 }
572
Jethro Beekman01672442023-04-19 14:08:14 +0200573 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
574 ret = psa_pk_status_to_mbedtls(status);
575 destruction_status = psa_destroy_key(key_id);
576 if (ret != 0) {
577 return ret;
578 } else if (destruction_status != PSA_SUCCESS) {
579 return psa_pk_status_to_mbedtls(destruction_status);
580 }
Jethro Beekman01672442023-04-19 14:08:14 +0200581 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200582#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200583#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200584 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200585 (void) d;
586 (void) d_len;
587
588 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
589#endif /* MBEDTLS_USE_PSA_CRYPTO */
590 return ret;
591}
592
593#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
594
595/*
596 * Load an RFC8410 EC key, which doesn't have any parameters
597 */
598static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
599 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200600 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200601{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200602 int ret;
603
Jethro Beekman01672442023-04-19 14:08:14 +0200604 if (params->tag != 0 || params->len != 0) {
605 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
606 }
607
Valerio Setti00e8dd12023-05-18 18:56:59 +0200608#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
609 ret = pk_update_psa_ecparams(pk, grp_id);
610#else
611 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200612 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
613 if (ret != 0) {
614 return ret;
615 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200616#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200617 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200618}
619
620/*
621 * Parse an RFC 8410 encoded private EC key
622 *
623 * CurvePrivateKey ::= OCTET STRING
624 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200625static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200626 unsigned char *key, size_t keylen, const unsigned char *end,
627 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
628{
629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
630 size_t len;
631
632 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
634 }
635
636 if (key + len != end) {
637 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
638 }
639
Valerio Setti00e8dd12023-05-18 18:56:59 +0200640#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
642 psa_status_t status;
643
644 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
646 PSA_KEY_USAGE_DERIVE);
647 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200648
649 status = psa_import_key(&attributes, key, len, &pk->priv_id);
650 if (status != PSA_SUCCESS) {
651 ret = psa_pk_status_to_mbedtls(status);
652 return ret;
653 }
654#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
655 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
656
Valerio Setti805e4a02023-06-30 17:16:19 +0200657 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
659 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200660#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200661
Valerio Setti4064dbb2023-05-17 15:33:07 +0200662 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
663 * which never contain a public key. As such, derive the public key
664 * unconditionally. */
665 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200666 return ret;
667 }
668
Valerio Setti00e8dd12023-05-18 18:56:59 +0200669 /* When MBEDTLS_PK_USE_PSA_EC_DATA the key is checked while importing it
670 * into PSA. */
671#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200672 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200673 return ret;
674 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200675#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200676
677 return 0;
678}
679#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200680
Valerio Settiaddeee42023-06-14 10:46:55 +0200681#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200682/*
683 * Create a temporary ecp_keypair for converting an EC point in compressed
684 * format to an uncompressed one
685 */
686static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
687 const unsigned char *in_start, size_t in_len,
688 size_t *out_buf_len, unsigned char *out_buf,
689 size_t out_buf_size)
690{
691 mbedtls_ecp_keypair ecp_key;
692 mbedtls_ecp_group_id ecp_group_id;
693 int ret;
694
695 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
696
697 mbedtls_ecp_keypair_init(&ecp_key);
698 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
699 if (ret != 0) {
700 return ret;
701 }
702 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
703 in_start, in_len);
704 if (ret != 0) {
705 goto exit;
706 }
707 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
708 MBEDTLS_ECP_PF_UNCOMPRESSED,
709 out_buf_len, out_buf, out_buf_size);
710
711exit:
712 mbedtls_ecp_keypair_free(&ecp_key);
713 return ret;
714}
Valerio Settiaddeee42023-06-14 10:46:55 +0200715#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200716
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717/*
718 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100719 *
720 * The caller is responsible for clearing the structure upon failure if
721 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200725 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726{
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728
Valerio Setti4064dbb2023-05-17 15:33:07 +0200729#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
730 mbedtls_svc_key_id_t key;
731 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
732 size_t len = (end - *p);
733
734 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
735 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 }
737
Valerio Setti4064dbb2023-05-17 15:33:07 +0200738 /* Compressed point format are not supported yet by PSA crypto. As a
739 * consequence ecp functions are used to "convert" the point to
740 * uncompressed format */
741 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200742#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200743 ret = pk_convert_compressed_ec(pk, *p, len,
744 &(pk->pub_raw_len), pk->pub_raw,
745 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
746 if (ret != 0) {
747 return ret;
748 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200749#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
750 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
751#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200752 } else {
753 /* Uncompressed format */
754 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200755 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200756 }
757 memcpy(pk->pub_raw, *p, (end - *p));
758 pk->pub_raw_len = end - *p;
759 }
760
761 /* Validate the key by trying to importing it */
762 psa_set_key_usage_flags(&key_attrs, 0);
763 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
764 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
765 psa_set_key_bits(&key_attrs, pk->ec_bits);
766
767 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
768 &key) != PSA_SUCCESS) ||
769 (psa_destroy_key(key) != PSA_SUCCESS)) {
770 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
771 pk->pub_raw_len = 0;
772 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
773 }
774 ret = 0;
775#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
776 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
777 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
778 (const unsigned char *) *p,
779 end - *p)) == 0) {
780 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
781 }
782#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
783
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786 */
787 *p = (unsigned char *) end;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790}
Valerio Setti81d75122023-06-14 14:49:33 +0200791#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794/*
795 * RSAPublicKey ::= SEQUENCE {
796 * modulus INTEGER, -- n
797 * publicExponent INTEGER -- e
798 * }
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800static int pk_get_rsapubkey(unsigned char **p,
801 const unsigned char *end,
802 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803{
Janos Follath24eed8d2019-11-22 13:21:35 +0000804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200805 size_t len;
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
808 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
810 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (*p + len != 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
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100817 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
819 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
820 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
823 NULL, 0, NULL, 0)) != 0) {
824 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
825 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100826
827 *p += len;
828
829 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
831 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
832 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
835 NULL, 0, *p, len)) != 0) {
836 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
837 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838
839 *p += len;
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (mbedtls_rsa_complete(rsa) != 0 ||
842 mbedtls_rsa_check_pubkey(rsa) != 0) {
843 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000844 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (*p != end) {
847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
848 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
849 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
855/* Get a PK algorithm identifier
856 *
857 * AlgorithmIdentifier ::= SEQUENCE {
858 * algorithm OBJECT IDENTIFIER,
859 * parameters ANY DEFINED BY algorithm OPTIONAL }
860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int pk_get_pk_alg(unsigned char **p,
862 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200863 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
864 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
872 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Jethro Beekman01672442023-04-19 14:08:14 +0200875 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200876#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200877 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
878 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
879 if (ret == 0) {
880 *pk_alg = MBEDTLS_PK_ECKEY;
881 }
882 }
883#else
884 (void) ec_grp_id;
885#endif
886 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
888 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889
890 /*
891 * No parameters with RSA (only for EC)
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (*pk_alg == MBEDTLS_PK_RSA &&
894 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
895 params->len != 0)) {
896 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 }
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900}
901
902/*
903 * SubjectPublicKeyInfo ::= SEQUENCE {
904 * algorithm AlgorithmIdentifier,
905 * subjectPublicKey BIT STRING }
906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
908 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909{
Janos Follath24eed8d2019-11-22 13:21:35 +0000910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_asn1_buf alg_params;
913 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200914 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
918 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920 }
921
922 end = *p + len;
923
Jethro Beekman01672442023-04-19 14:08:14 +0200924 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return ret;
926 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
929 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (*p + len != end) {
933 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
934 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
938 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
942 return ret;
943 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (pk_alg == MBEDTLS_PK_RSA) {
947 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200950#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200952#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200953 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200954 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200955 } else
956#endif
957 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200958 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200959 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200961 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200964#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (ret == 0 && *p != end) {
968 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
969 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
970 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (ret != 0) {
973 mbedtls_pk_free(pk);
974 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977}
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100981 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
982 *
983 * The value zero is:
984 * - never a valid value for an RSA parameter
985 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
986 *
987 * Since values can't be omitted in PKCS#1, passing a zero value to
988 * rsa_complete() would be incorrect, so reject zero values early.
989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990static int asn1_get_nonzero_mpi(unsigned char **p,
991 const unsigned char *end,
992 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100993{
994 int ret;
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 ret = mbedtls_asn1_get_mpi(p, end, X);
997 if (ret != 0) {
998 return ret;
999 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1002 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1003 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001006}
1007
1008/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 * Parse a PKCS#1 encoded private RSA key
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1012 const unsigned char *key,
1013 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001015 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 size_t len;
1017 unsigned char *p, *end;
1018
Hanno Beckerefa14e82017-10-11 19:45:19 +01001019 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001021
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 p = (unsigned char *) key;
1023 end = p + keylen;
1024
1025 /*
1026 * This function parses the RSAPrivateKey (PKCS#1)
1027 *
1028 * RSAPrivateKey ::= SEQUENCE {
1029 * version Version,
1030 * modulus INTEGER, -- n
1031 * publicExponent INTEGER, -- e
1032 * privateExponent INTEGER, -- d
1033 * prime1 INTEGER, -- p
1034 * prime2 INTEGER, -- q
1035 * exponent1 INTEGER, -- d mod (p-1)
1036 * exponent2 INTEGER, -- d mod (q-1)
1037 * coefficient INTEGER, -- (inverse of q) mod p
1038 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1039 * }
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1042 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1043 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 }
1045
1046 end = p + len;
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1049 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 }
1051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (version != 0) {
1053 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 }
1055
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1058 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1059 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001063 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1065 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1066 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001067 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001069
1070 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1072 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1073 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001074 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001076
1077 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1079 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1080 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001081 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001083
1084 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1086 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1087 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001088 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001090
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001091#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001092 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1094 * that they can be easily recomputed from D, P and Q. However by
1095 * parsing them from the PKCS1 structure it is possible to avoid
1096 * recalculating them which both reduces the overhead of loading
1097 * RSA private keys into memory and also avoids side channels which
1098 * can arise when computing those values, since all of D, P, and Q
1099 * are secret. See https://eprint.iacr.org/2020/055 for a
1100 * description of one such attack.
1101 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001102
Jack Lloyd80cc8112020-01-22 17:34:29 -05001103 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1105 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1106 goto cleanup;
1107 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001108
1109 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1111 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1112 goto cleanup;
1113 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001114
1115 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1117 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1118 goto cleanup;
1119 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001120
Jack Lloyd60239752020-01-27 17:53:36 -05001121#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001122 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1124 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1126 goto cleanup;
1127 }
Jack Lloyd60239752020-01-27 17:53:36 -05001128#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001129
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001130 /* rsa_complete() doesn't complete anything with the default
1131 * implementation but is still called:
1132 * - for the benefit of alternative implementation that may want to
1133 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1134 * - as is also sanity-checks the key
1135 *
1136 * Furthermore, we also check the public part for consistency with
1137 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1140 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001142 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (p != end) {
1145 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1146 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 }
1148
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001149cleanup:
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001154 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((ret & 0xff80) == 0) {
1156 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1157 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001158 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167
Valerio Setti81d75122023-06-14 14:49:33 +02001168#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169/*
1170 * Parse a SEC1 encoded private EC key
1171 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001172static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 const unsigned char *key, size_t keylen,
1174 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175{
Janos Follath24eed8d2019-11-22 13:21:35 +00001176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001177 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001178 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001179 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001181 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 unsigned char *end = p + keylen;
1183 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001184#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1186 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001187#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1188 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001189#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
1191 /*
1192 * RFC 5915, or SEC1 Appendix C.4
1193 *
1194 * ECPrivateKey ::= SEQUENCE {
1195 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1196 * privateKey OCTET STRING,
1197 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1198 * publicKey [1] BIT STRING OPTIONAL
1199 * }
1200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204 }
1205
1206 end = p + len;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1210 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (version != 1) {
1213 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1214 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1218 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219
Valerio Setti6b062ee2023-06-30 17:32:57 +02001220 /* Keep a reference to the position fo the private key. It will be used
1221 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001222 d = p;
1223 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001224
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225 p += len;
1226
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001227 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001229 /*
1230 * Is 'parameters' present?
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1233 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1234 0)) == 0) {
1235 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001236 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001241 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001242 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243
Valerio Setti6b062ee2023-06-30 17:32:57 +02001244
1245#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1246 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1248 }
1249#endif
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001252 /*
1253 * Is 'publickey' present? If not, or if we can't read it (eg because it
1254 * is compressed), create it from the private key.
1255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1257 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1258 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259 end2 = p + len;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1263 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (p + len != end2) {
1266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1268 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001269
Valerio Setti4064dbb2023-05-17 15:33:07 +02001270 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001271 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001273 /*
1274 * The only acceptable failure mode of pk_get_ecpubkey() above
1275 * is if the point format is not recognized.
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1278 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1279 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001283 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001285
Valerio Setti00e8dd12023-05-18 18:56:59 +02001286#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1287 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1288 /* Setting largest masks for usage and key algorithms */
1289 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1290 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001291 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001292#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1293 psa_set_key_algorithm(&attributes,
1294 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1295#else
1296 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1297#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001298 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001299
Valerio Settia541e012023-05-24 14:31:21 +02001300 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001301 if (status != PSA_SUCCESS) {
1302 ret = psa_pk_status_to_mbedtls(status);
1303 return ret;
1304 }
1305#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1306
Valerio Setti34f67552023-04-03 15:19:18 +02001307 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001308 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001309 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001310 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001311 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312
Valerio Setti00e8dd12023-05-18 18:56:59 +02001313#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001317#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320}
Valerio Setti81d75122023-06-14 14:49:33 +02001321#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322
1323/*
1324 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001325 *
1326 * Notes:
1327 *
1328 * - This function does not own the key buffer. It is the
1329 * responsibility of the caller to take care of zeroizing
1330 * and freeing it after use.
1331 *
1332 * - The function is responsible for freeing the provided
1333 * PK context on failure.
1334 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 */
1336static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 mbedtls_pk_context *pk,
1338 const unsigned char *key, size_t keylen,
1339 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340{
1341 int ret, version;
1342 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 unsigned char *p = (unsigned char *) key;
1345 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001347 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Valerio Setti81d75122023-06-14 14:49:33 +02001350#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001351 (void) f_rng;
1352 (void) p_rng;
1353#endif
1354
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001356 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 *
1358 * PrivateKeyInfo ::= SEQUENCE {
1359 * version Version,
1360 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1361 * privateKey PrivateKey,
1362 * attributes [0] IMPLICIT Attributes OPTIONAL }
1363 *
1364 * Version ::= INTEGER
1365 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1366 * PrivateKey ::= OCTET STRING
1367 *
1368 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1369 */
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1372 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1373 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 }
1375
1376 end = p + len;
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001380 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (version != 0) {
1383 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1384 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Jethro Beekman01672442023-04-19 14:08:14 +02001386 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return ret;
1388 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1392 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (len < 1) {
1395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1396 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1397 }
1398
1399 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1400 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1401 }
1402
1403 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1404 return ret;
1405 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (pk_alg == MBEDTLS_PK_RSA) {
1409 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1410 mbedtls_pk_free(pk);
1411 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 }
1413 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001415#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001417#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001418 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001419 if ((ret =
1420 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001421 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001422 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001423 p_rng)) != 0) {
1424 mbedtls_pk_free(pk);
1425 return ret;
1426 }
1427 } else
1428#endif
1429 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001430 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1431 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001432 mbedtls_pk_free(pk);
1433 return ret;
1434 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 }
1436 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001437#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441}
1442
1443/*
1444 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001445 *
1446 * To save space, the decryption happens in-place on the given key buffer.
1447 * Also, while this function may modify the keybuffer, it doesn't own it,
1448 * and instead it is the responsibility of the caller to zeroize and properly
1449 * free it after use.
1450 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_pk_context *pk,
1455 unsigned char *key, size_t keylen,
1456 const unsigned char *pwd, size_t pwdlen,
1457 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001459 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001461 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1464#if defined(MBEDTLS_PKCS12_C)
1465 mbedtls_cipher_type_t cipher_alg;
1466 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467#endif
1468
Hanno Becker2aa80a72017-09-07 15:28:45 +01001469 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 end = p + keylen;
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (pwdlen == 0) {
1473 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1474 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475
1476 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001477 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 *
1479 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1480 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1481 * encryptedData EncryptedData
1482 * }
1483 *
1484 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1485 *
1486 * EncryptedData ::= OCTET STRING
1487 *
1488 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001489 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1492 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1493 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494 }
1495
1496 end = p + len;
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1499 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1500 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1503 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505
Hanno Beckerfab35692017-08-25 13:38:26 +01001506 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507
1508 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001509 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1513 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1514 cipher_alg, md_alg,
1515 pwd, pwdlen, p, len, buf)) != 0) {
1516 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1517 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1518 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001522
1523 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_PKCS12_C */
1526#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1528 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1529 p, len, buf)) != 0) {
1530 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1531 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1532 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001536
1537 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001540 {
1541 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001542 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (decrypted == 0) {
1545 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1546 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551
1552/*
1553 * Parse a private key
1554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1556 const unsigned char *key, size_t keylen,
1557 const unsigned char *pwd, size_t pwdlen,
1558 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001559{
Janos Follath24eed8d2019-11-22 13:21:35 +00001560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001565#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (keylen == 0) {
1568 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1569 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001570
1571#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001575 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 } else {
1579 ret = mbedtls_pem_read_buffer(&pem,
1580 "-----BEGIN RSA PRIVATE KEY-----",
1581 "-----END RSA PRIVATE KEY-----",
1582 key, pwd, pwdlen, &len);
1583 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (ret == 0) {
1586 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1587 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1588 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1589 pem.buf, pem.buflen)) != 0) {
1590 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591 }
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 mbedtls_pem_free(&pem);
1594 return ret;
1595 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1596 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1597 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1598 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1599 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1600 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603
Valerio Setti81d75122023-06-14 14:49:33 +02001604#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001605 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001607 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else {
1609 ret = mbedtls_pem_read_buffer(&pem,
1610 "-----BEGIN EC PRIVATE KEY-----",
1611 "-----END EC PRIVATE KEY-----",
1612 key, pwd, pwdlen, &len);
1613 }
1614 if (ret == 0) {
1615 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001618 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 pem.buf, pem.buflen,
1620 f_rng, p_rng)) != 0) {
1621 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 mbedtls_pem_free(&pem);
1625 return ret;
1626 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1627 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1628 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1629 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1630 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1631 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632 }
Valerio Setti81d75122023-06-14 14:49:33 +02001633#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001635 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001637 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 } else {
1639 ret = mbedtls_pem_read_buffer(&pem,
1640 "-----BEGIN PRIVATE KEY-----",
1641 "-----END PRIVATE KEY-----",
1642 key, NULL, 0, &len);
1643 }
1644 if (ret == 0) {
1645 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1646 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1647 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001648 }
1649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_pem_free(&pem);
1651 return ret;
1652 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1653 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001657 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001659 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 } else {
1661 ret = mbedtls_pem_read_buffer(&pem,
1662 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1663 "-----END ENCRYPTED PRIVATE KEY-----",
1664 key, NULL, 0, &len);
1665 }
1666 if (ret == 0) {
1667 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1668 pwd, pwdlen, f_rng, p_rng)) != 0) {
1669 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001670 }
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_pem_free(&pem);
1673 return ret;
1674 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1675 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001678#else
1679 ((void) pwd);
1680 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682
1683 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001684 * At this point we only know it's not a PEM formatted key. Could be any
1685 * of the known DER encoded private key formats
1686 *
1687 * We try the different DER format parsers to see if one passes without
1688 * error
1689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001692 unsigned char *key_copy;
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1695 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1696 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1701 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_platform_zeroize(key_copy, keylen);
1704 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001705 }
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 if (ret == 0) {
1708 return 0;
1709 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 mbedtls_pk_free(pk);
1712 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1715 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1720 if (ret == 0) {
1721 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001722 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 mbedtls_pk_free(pk);
1725 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1730 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1731 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1732 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733 }
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 mbedtls_pk_free(pk);
1736 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001738
Valerio Setti81d75122023-06-14 14:49:33 +02001739#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1741 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001742 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 key, keylen, f_rng, p_rng) == 0) {
1744 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001747#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001748
Valerio Setti81d75122023-06-14 14:49:33 +02001749 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001750 * it is ok to leave the PK context initialized but not
1751 * freed: It is the caller's responsibility to call pk_init()
1752 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001753 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001754 * isn't, this leads to mbedtls_pk_free() being called
1755 * twice, once here and once by the caller, but this is
1756 * also ok and in line with the mbedtls_pk_free() calls
1757 * on failed PEM parsing attempts. */
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001760}
1761
1762/*
1763 * Parse a public key
1764 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1766 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001767{
Janos Follath24eed8d2019-11-22 13:21:35 +00001768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001769 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001770#if defined(MBEDTLS_RSA_C)
1771 const mbedtls_pk_info_t *pk_info;
1772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001774 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001776#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (keylen == 0) {
1779 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1780 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001781
1782#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001784#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001785 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001787 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 } else {
1789 ret = mbedtls_pem_read_buffer(&pem,
1790 "-----BEGIN RSA PUBLIC KEY-----",
1791 "-----END RSA PUBLIC KEY-----",
1792 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001793 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001794
1795 if (ret == 0) {
1796 p = pem.buf;
1797 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1798 mbedtls_pem_free(&pem);
1799 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1800 }
1801
1802 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1803 mbedtls_pem_free(&pem);
1804 return ret;
1805 }
1806
1807 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1808 mbedtls_pk_free(ctx);
1809 }
1810
1811 mbedtls_pem_free(&pem);
1812 return ret;
1813 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1814 mbedtls_pem_free(&pem);
1815 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001816 }
1817#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001818
1819 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001821 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 } else {
1823 ret = mbedtls_pem_read_buffer(&pem,
1824 "-----BEGIN PUBLIC KEY-----",
1825 "-----END PUBLIC KEY-----",
1826 key, NULL, 0, &len);
1827 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001830 /*
1831 * Was PEM encoded
1832 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001833 p = pem.buf;
1834
Jethro Beekman01672442023-04-19 14:08:14 +02001835 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 mbedtls_pem_free(&pem);
1837 return ret;
1838 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1839 mbedtls_pem_free(&pem);
1840 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001841 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001844
1845#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1847 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001848 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001849
1850 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1851 return ret;
1852 }
1853
1854 p = (unsigned char *) key;
1855 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1856 if (ret == 0) {
1857 return ret;
1858 }
1859 mbedtls_pk_free(ctx);
1860 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1861 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1862 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001863 }
1864#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001865 p = (unsigned char *) key;
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001870}
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_PK_PARSE_C */