blob: fa0570c0702bdda2833d23afdbb8656b2a955174 [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
Jethro Beekman01672442023-04-19 14:08:14 +0200669 return 0;
670}
671#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200672
Valerio Settiaddeee42023-06-14 10:46:55 +0200673#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200674/*
675 * Create a temporary ecp_keypair for converting an EC point in compressed
676 * format to an uncompressed one
677 */
678static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
679 const unsigned char *in_start, size_t in_len,
680 size_t *out_buf_len, unsigned char *out_buf,
681 size_t out_buf_size)
682{
683 mbedtls_ecp_keypair ecp_key;
684 mbedtls_ecp_group_id ecp_group_id;
685 int ret;
686
687 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
688
689 mbedtls_ecp_keypair_init(&ecp_key);
690 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
691 if (ret != 0) {
692 return ret;
693 }
694 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
695 in_start, in_len);
696 if (ret != 0) {
697 goto exit;
698 }
699 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
700 MBEDTLS_ECP_PF_UNCOMPRESSED,
701 out_buf_len, out_buf, out_buf_size);
702
703exit:
704 mbedtls_ecp_keypair_free(&ecp_key);
705 return ret;
706}
Valerio Settiaddeee42023-06-14 10:46:55 +0200707#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200708
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709/*
710 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100711 *
712 * The caller is responsible for clearing the structure upon failure if
713 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200717 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718{
Janos Follath24eed8d2019-11-22 13:21:35 +0000719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720
Valerio Setti4064dbb2023-05-17 15:33:07 +0200721#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
722 mbedtls_svc_key_id_t key;
723 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
724 size_t len = (end - *p);
725
726 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
727 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 }
729
Valerio Setti4064dbb2023-05-17 15:33:07 +0200730 /* Compressed point format are not supported yet by PSA crypto. As a
731 * consequence ecp functions are used to "convert" the point to
732 * uncompressed format */
733 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200734#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200735 ret = pk_convert_compressed_ec(pk, *p, len,
736 &(pk->pub_raw_len), pk->pub_raw,
737 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
738 if (ret != 0) {
739 return ret;
740 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200741#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
742 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
743#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200744 } else {
745 /* Uncompressed format */
746 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200747 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200748 }
749 memcpy(pk->pub_raw, *p, (end - *p));
750 pk->pub_raw_len = end - *p;
751 }
752
753 /* Validate the key by trying to importing it */
754 psa_set_key_usage_flags(&key_attrs, 0);
755 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
756 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
757 psa_set_key_bits(&key_attrs, pk->ec_bits);
758
759 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
760 &key) != PSA_SUCCESS) ||
761 (psa_destroy_key(key) != PSA_SUCCESS)) {
762 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
763 pk->pub_raw_len = 0;
764 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
765 }
766 ret = 0;
767#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
768 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
769 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
770 (const unsigned char *) *p,
771 end - *p)) == 0) {
772 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
773 }
774#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
775
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778 */
779 *p = (unsigned char *) end;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782}
Valerio Setti81d75122023-06-14 14:49:33 +0200783#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786/*
787 * RSAPublicKey ::= SEQUENCE {
788 * modulus INTEGER, -- n
789 * publicExponent INTEGER -- e
790 * }
791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int pk_get_rsapubkey(unsigned char **p,
793 const unsigned char *end,
794 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795{
Janos Follath24eed8d2019-11-22 13:21:35 +0000796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 size_t len;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
800 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
801 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
802 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (*p + len != end) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
806 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
807 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
812 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
815 NULL, 0, NULL, 0)) != 0) {
816 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
817 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818
819 *p += len;
820
821 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
823 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
824 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
827 NULL, 0, *p, len)) != 0) {
828 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
829 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830
831 *p += len;
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (mbedtls_rsa_complete(rsa) != 0 ||
834 mbedtls_rsa_check_pubkey(rsa) != 0) {
835 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000836 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (*p != end) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
840 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
841 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
847/* Get a PK algorithm identifier
848 *
849 * AlgorithmIdentifier ::= SEQUENCE {
850 * algorithm OBJECT IDENTIFIER,
851 * parameters ANY DEFINED BY algorithm OPTIONAL }
852 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853static int pk_get_pk_alg(unsigned char **p,
854 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200855 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
856 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857{
Janos Follath24eed8d2019-11-22 13:21:35 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
864 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
865 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866
Jethro Beekman01672442023-04-19 14:08:14 +0200867 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200868#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200869 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
870 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
871 if (ret == 0) {
872 *pk_alg = MBEDTLS_PK_ECKEY;
873 }
874 }
875#else
876 (void) ec_grp_id;
877#endif
878 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
880 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881
882 /*
883 * No parameters with RSA (only for EC)
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (*pk_alg == MBEDTLS_PK_RSA &&
886 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
887 params->len != 0)) {
888 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892}
893
894/*
895 * SubjectPublicKeyInfo ::= SEQUENCE {
896 * algorithm AlgorithmIdentifier,
897 * subjectPublicKey BIT STRING }
898 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
900 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901{
Janos Follath24eed8d2019-11-22 13:21:35 +0000902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_asn1_buf alg_params;
905 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200906 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
910 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 }
913
914 end = *p + len;
915
Jethro Beekman01672442023-04-19 14:08:14 +0200916 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 return ret;
918 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
921 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
922 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (*p + len != end) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
926 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
930 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
934 return ret;
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (pk_alg == MBEDTLS_PK_RSA) {
939 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200942#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200944#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200945 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200946 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200947 } else
948#endif
949 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200950 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200951 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200953 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200955 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200956#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ret == 0 && *p != end) {
960 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
961 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
962 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (ret != 0) {
965 mbedtls_pk_free(pk);
966 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969}
970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100973 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
974 *
975 * The value zero is:
976 * - never a valid value for an RSA parameter
977 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
978 *
979 * Since values can't be omitted in PKCS#1, passing a zero value to
980 * rsa_complete() would be incorrect, so reject zero values early.
981 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982static int asn1_get_nonzero_mpi(unsigned char **p,
983 const unsigned char *end,
984 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100985{
986 int ret;
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 ret = mbedtls_asn1_get_mpi(p, end, X);
989 if (ret != 0) {
990 return ret;
991 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
994 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
995 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100998}
999
1000/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001 * Parse a PKCS#1 encoded private RSA key
1002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1004 const unsigned char *key,
1005 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001007 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 size_t len;
1009 unsigned char *p, *end;
1010
Hanno Beckerefa14e82017-10-11 19:45:19 +01001011 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001013
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 p = (unsigned char *) key;
1015 end = p + keylen;
1016
1017 /*
1018 * This function parses the RSAPrivateKey (PKCS#1)
1019 *
1020 * RSAPrivateKey ::= SEQUENCE {
1021 * version Version,
1022 * modulus INTEGER, -- n
1023 * publicExponent INTEGER, -- e
1024 * privateExponent INTEGER, -- d
1025 * prime1 INTEGER, -- p
1026 * prime2 INTEGER, -- q
1027 * exponent1 INTEGER, -- d mod (p-1)
1028 * exponent2 INTEGER, -- d mod (q-1)
1029 * coefficient INTEGER, -- (inverse of q) mod p
1030 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1031 * }
1032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1034 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1035 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 }
1037
1038 end = p + len;
1039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1041 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001042 }
1043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (version != 0) {
1045 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 }
1047
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1050 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1051 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1057 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1058 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061
1062 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1064 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1065 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068
1069 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1071 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1072 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001073 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075
1076 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1078 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1079 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001080 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001082
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001083#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001084 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1086 * that they can be easily recomputed from D, P and Q. However by
1087 * parsing them from the PKCS1 structure it is possible to avoid
1088 * recalculating them which both reduces the overhead of loading
1089 * RSA private keys into memory and also avoids side channels which
1090 * can arise when computing those values, since all of D, P, and Q
1091 * are secret. See https://eprint.iacr.org/2020/055 for a
1092 * description of one such attack.
1093 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001094
Jack Lloyd80cc8112020-01-22 17:34:29 -05001095 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1097 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1098 goto cleanup;
1099 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001100
1101 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1103 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1104 goto cleanup;
1105 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001106
1107 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1109 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1110 goto cleanup;
1111 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001112
Jack Lloyd60239752020-01-27 17:53:36 -05001113#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001114 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1116 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1117 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1118 goto cleanup;
1119 }
Jack Lloyd60239752020-01-27 17:53:36 -05001120#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001121
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001122 /* rsa_complete() doesn't complete anything with the default
1123 * implementation but is still called:
1124 * - for the benefit of alternative implementation that may want to
1125 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1126 * - as is also sanity-checks the key
1127 *
1128 * Furthermore, we also check the public part for consistency with
1129 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1132 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001133 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001134 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (p != end) {
1137 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1138 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 }
1140
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141cleanup:
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001146 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ret & 0xff80) == 0) {
1148 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1149 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001150 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159
Valerio Setti81d75122023-06-14 14:49:33 +02001160#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161/*
1162 * Parse a SEC1 encoded private EC key
1163 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001164static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 const unsigned char *key, size_t keylen,
1166 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167{
Janos Follath24eed8d2019-11-22 13:21:35 +00001168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001169 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001170 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001171 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001173 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174 unsigned char *end = p + keylen;
1175 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001176#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1178 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001179#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1180 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001181#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
1183 /*
1184 * RFC 5915, or SEC1 Appendix C.4
1185 *
1186 * ECPrivateKey ::= SEQUENCE {
1187 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1188 * privateKey OCTET STRING,
1189 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1190 * publicKey [1] BIT STRING OPTIONAL
1191 * }
1192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196 }
1197
1198 end = p + len;
1199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1202 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (version != 1) {
1205 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1210 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
Valerio Setti6b062ee2023-06-30 17:32:57 +02001212 /* Keep a reference to the position fo the private key. It will be used
1213 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001214 d = p;
1215 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001216
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 p += len;
1218
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001219 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001221 /*
1222 * Is 'parameters' present?
1223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1225 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1226 0)) == 0) {
1227 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001228 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001233 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001234 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235
Valerio Setti6b062ee2023-06-30 17:32:57 +02001236
1237#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1238 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1240 }
1241#endif
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 /*
1245 * Is 'publickey' present? If not, or if we can't read it (eg because it
1246 * is compressed), create it from the private key.
1247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1249 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1250 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001251 end2 = p + len;
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1254 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1255 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (p + len != end2) {
1258 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1259 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1260 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001261
Valerio Setti4064dbb2023-05-17 15:33:07 +02001262 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001263 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001265 /*
1266 * The only acceptable failure mode of pk_get_ecpubkey() above
1267 * is if the point format is not recognized.
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1270 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1271 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001272 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001275 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001277
Valerio Setti00e8dd12023-05-18 18:56:59 +02001278#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1279 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1280 /* Setting largest masks for usage and key algorithms */
1281 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1282 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001283 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001284#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1285 psa_set_key_algorithm(&attributes,
1286 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1287#else
1288 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1289#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001290 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001291
Valerio Settia541e012023-05-24 14:31:21 +02001292 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001293 if (status != PSA_SUCCESS) {
1294 ret = psa_pk_status_to_mbedtls(status);
1295 return ret;
1296 }
1297#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1298
Valerio Setti34f67552023-04-03 15:19:18 +02001299 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001300 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001301 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001302 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001303 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306}
Valerio Setti81d75122023-06-14 14:49:33 +02001307#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308
1309/*
1310 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001311 *
1312 * Notes:
1313 *
1314 * - This function does not own the key buffer. It is the
1315 * responsibility of the caller to take care of zeroizing
1316 * and freeing it after use.
1317 *
1318 * - The function is responsible for freeing the provided
1319 * PK context on failure.
1320 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 */
1322static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 mbedtls_pk_context *pk,
1324 const unsigned char *key, size_t keylen,
1325 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326{
1327 int ret, version;
1328 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 unsigned char *p = (unsigned char *) key;
1331 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001333 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335
Valerio Setti81d75122023-06-14 14:49:33 +02001336#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001337 (void) f_rng;
1338 (void) p_rng;
1339#endif
1340
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001342 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 *
1344 * PrivateKeyInfo ::= SEQUENCE {
1345 * version Version,
1346 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1347 * privateKey PrivateKey,
1348 * attributes [0] IMPLICIT Attributes OPTIONAL }
1349 *
1350 * Version ::= INTEGER
1351 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1352 * PrivateKey ::= OCTET STRING
1353 *
1354 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1355 */
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1358 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 }
1361
1362 end = p + len;
1363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1365 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001366 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (version != 0) {
1369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1370 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371
Jethro Beekman01672442023-04-19 14:08:14 +02001372 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return ret;
1374 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1378 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (len < 1) {
1381 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1382 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1383 }
1384
1385 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1386 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1387 }
1388
1389 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1390 return ret;
1391 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (pk_alg == MBEDTLS_PK_RSA) {
1395 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1396 mbedtls_pk_free(pk);
1397 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398 }
1399 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001401#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001403#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001404 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001405 if ((ret =
1406 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001407 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001408 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001409 p_rng)) != 0) {
1410 mbedtls_pk_free(pk);
1411 return ret;
1412 }
1413 } else
1414#endif
1415 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001416 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1417 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001418 mbedtls_pk_free(pk);
1419 return ret;
1420 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 }
1422 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001423#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427}
1428
1429/*
1430 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001431 *
1432 * To save space, the decryption happens in-place on the given key buffer.
1433 * Also, while this function may modify the keybuffer, it doesn't own it,
1434 * and instead it is the responsibility of the caller to zeroize and properly
1435 * free it after use.
1436 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_pk_context *pk,
1441 unsigned char *key, size_t keylen,
1442 const unsigned char *pwd, size_t pwdlen,
1443 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001445 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001447 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1450#if defined(MBEDTLS_PKCS12_C)
1451 mbedtls_cipher_type_t cipher_alg;
1452 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453#endif
1454
Hanno Becker2aa80a72017-09-07 15:28:45 +01001455 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 end = p + keylen;
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (pwdlen == 0) {
1459 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1460 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461
1462 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001463 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464 *
1465 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1466 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1467 * encryptedData EncryptedData
1468 * }
1469 *
1470 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1471 *
1472 * EncryptedData ::= OCTET STRING
1473 *
1474 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001475 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1478 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1479 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 }
1481
1482 end = p + len;
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1486 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1489 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1490 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
Hanno Beckerfab35692017-08-25 13:38:26 +01001492 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
1494 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001495 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1499 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1500 cipher_alg, md_alg,
1501 pwd, pwdlen, p, len, buf)) != 0) {
1502 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1503 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001508
1509 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_PKCS12_C */
1512#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1514 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1515 p, len, buf)) != 0) {
1516 if (ret == MBEDTLS_ERR_PKCS5_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_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001526 {
1527 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001528 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (decrypted == 0) {
1531 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1532 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
1538/*
1539 * Parse a private key
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1542 const unsigned char *key, size_t keylen,
1543 const unsigned char *pwd, size_t pwdlen,
1544 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545{
Janos Follath24eed8d2019-11-22 13:21:35 +00001546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001551#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (keylen == 0) {
1554 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1555 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001556
1557#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001561 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001563 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 } else {
1565 ret = mbedtls_pem_read_buffer(&pem,
1566 "-----BEGIN RSA PRIVATE KEY-----",
1567 "-----END RSA PRIVATE KEY-----",
1568 key, pwd, pwdlen, &len);
1569 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if (ret == 0) {
1572 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1573 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1574 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1575 pem.buf, pem.buflen)) != 0) {
1576 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577 }
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 mbedtls_pem_free(&pem);
1580 return ret;
1581 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1582 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1583 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1584 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1585 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1586 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589
Valerio Setti81d75122023-06-14 14:49:33 +02001590#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001591 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001593 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 } else {
1595 ret = mbedtls_pem_read_buffer(&pem,
1596 "-----BEGIN EC PRIVATE KEY-----",
1597 "-----END EC PRIVATE KEY-----",
1598 key, pwd, pwdlen, &len);
1599 }
1600 if (ret == 0) {
1601 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001604 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 pem.buf, pem.buflen,
1606 f_rng, p_rng)) != 0) {
1607 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608 }
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 mbedtls_pem_free(&pem);
1611 return ret;
1612 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1613 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1614 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1615 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1616 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1617 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
Valerio Setti81d75122023-06-14 14:49:33 +02001619#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001620
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001621 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001623 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 } else {
1625 ret = mbedtls_pem_read_buffer(&pem,
1626 "-----BEGIN PRIVATE KEY-----",
1627 "-----END PRIVATE KEY-----",
1628 key, NULL, 0, &len);
1629 }
1630 if (ret == 0) {
1631 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1632 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1633 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634 }
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_pem_free(&pem);
1637 return ret;
1638 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1639 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001643 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001645 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 } else {
1647 ret = mbedtls_pem_read_buffer(&pem,
1648 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1649 "-----END ENCRYPTED PRIVATE KEY-----",
1650 key, NULL, 0, &len);
1651 }
1652 if (ret == 0) {
1653 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1654 pwd, pwdlen, f_rng, p_rng)) != 0) {
1655 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656 }
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 mbedtls_pem_free(&pem);
1659 return ret;
1660 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1661 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664#else
1665 ((void) pwd);
1666 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001668
1669 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001670 * At this point we only know it's not a PEM formatted key. Could be any
1671 * of the known DER encoded private key formats
1672 *
1673 * We try the different DER format parsers to see if one passes without
1674 * error
1675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001678 unsigned char *key_copy;
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1681 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1682 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1687 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_platform_zeroize(key_copy, keylen);
1690 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691 }
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 if (ret == 0) {
1694 return 0;
1695 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 mbedtls_pk_free(pk);
1698 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1701 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1706 if (ret == 0) {
1707 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001708 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 mbedtls_pk_free(pk);
1711 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1716 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1717 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1718 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001719 }
1720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 mbedtls_pk_free(pk);
1722 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001724
Valerio Setti81d75122023-06-14 14:49:33 +02001725#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1727 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001728 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 key, keylen, f_rng, p_rng) == 0) {
1730 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001731 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001733#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001734
Valerio Setti81d75122023-06-14 14:49:33 +02001735 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001736 * it is ok to leave the PK context initialized but not
1737 * freed: It is the caller's responsibility to call pk_init()
1738 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001739 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001740 * isn't, this leads to mbedtls_pk_free() being called
1741 * twice, once here and once by the caller, but this is
1742 * also ok and in line with the mbedtls_pk_free() calls
1743 * on failed PEM parsing attempts. */
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746}
1747
1748/*
1749 * Parse a public key
1750 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1752 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001753{
Janos Follath24eed8d2019-11-22 13:21:35 +00001754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001755 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001756#if defined(MBEDTLS_RSA_C)
1757 const mbedtls_pk_info_t *pk_info;
1758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001760 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001762#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if (keylen == 0) {
1765 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1766 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001767
1768#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001770#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001771 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001773 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 } else {
1775 ret = mbedtls_pem_read_buffer(&pem,
1776 "-----BEGIN RSA PUBLIC KEY-----",
1777 "-----END RSA PUBLIC KEY-----",
1778 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001779 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001780
1781 if (ret == 0) {
1782 p = pem.buf;
1783 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1784 mbedtls_pem_free(&pem);
1785 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1786 }
1787
1788 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1789 mbedtls_pem_free(&pem);
1790 return ret;
1791 }
1792
1793 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1794 mbedtls_pk_free(ctx);
1795 }
1796
1797 mbedtls_pem_free(&pem);
1798 return ret;
1799 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1800 mbedtls_pem_free(&pem);
1801 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001802 }
1803#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001804
1805 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001807 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 } else {
1809 ret = mbedtls_pem_read_buffer(&pem,
1810 "-----BEGIN PUBLIC KEY-----",
1811 "-----END PUBLIC KEY-----",
1812 key, NULL, 0, &len);
1813 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001816 /*
1817 * Was PEM encoded
1818 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001819 p = pem.buf;
1820
Jethro Beekman01672442023-04-19 14:08:14 +02001821 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 mbedtls_pem_free(&pem);
1823 return ret;
1824 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1825 mbedtls_pem_free(&pem);
1826 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001830
1831#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1833 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001835
1836 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1837 return ret;
1838 }
1839
1840 p = (unsigned char *) key;
1841 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1842 if (ret == 0) {
1843 return ret;
1844 }
1845 mbedtls_pk_free(ctx);
1846 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1847 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1848 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001849 }
1850#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001851 p = (unsigned char *) key;
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001856}
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_PK_PARSE_C */