blob: 67bb72032f61d07b38ac0899938f549d5cb440a4 [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"
Valerio Setti81d75122023-06-14 14:49:33 +020037#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti4064dbb2023-05-17 15:33:07 +020038#include "pk_internal.h"
39#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
52
Valerio Setti34f67552023-04-03 15:19:18 +020053#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020054#include "psa_util_internal.h"
Valerio Setti34f67552023-04-03 15:19:18 +020055#endif
56
57#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
59#endif
60
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020062
Valerio Settie0e63112023-05-18 18:48:07 +020063/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020064#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020065#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
66 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020067#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020068
Gilles Peskine832f3492017-11-30 11:42:12 +010069#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020070/*
71 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020072 *
73 * The file is expected to contain either PEM or DER encoded data.
74 * A terminating null byte is always appended. It is included in the announced
75 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 */
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020078{
79 FILE *f;
80 long size;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((f = fopen(path, "rb")) == NULL) {
83 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
84 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020085
Gilles Peskineda0913b2022-06-30 17:03:40 +020086 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 fseek(f, 0, SEEK_END);
90 if ((size = ftell(f)) == -1) {
91 fclose(f);
92 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020093 }
Gilles Peskine449bd832023-01-11 14:50:10 +010094 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020095
96 *n = (size_t) size;
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (*n + 1 == 0 ||
99 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
100 fclose(f);
101 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 }
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (fread(*buf, 1, *n, f) != *n) {
105 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100106
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100107 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113
114 (*buf)[*n] = '\0';
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200117 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121}
122
123/*
124 * Load and parse a private key
125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
127 const char *path, const char *pwd,
128 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129{
Janos Follath24eed8d2019-11-22 13:21:35 +0000130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131 size_t n;
132 unsigned char *buf;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
135 return ret;
136 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (pwd == NULL) {
139 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
140 } else {
141 ret = mbedtls_pk_parse_key(ctx, buf, n,
142 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
143 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100145 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148}
149
150/*
151 * Load and parse a public key
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154{
Janos Follath24eed8d2019-11-22 13:21:35 +0000155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156 size_t n;
157 unsigned char *buf;
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
160 return ret;
161 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100165 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170
Valerio Setti81d75122023-06-14 14:49:33 +0200171#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 *
174 * ECParameters ::= CHOICE {
175 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100176 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 * }
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
181 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182{
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (end - *p < 1) {
186 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
187 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
188 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100189
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100195#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 ) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
198 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204
205 params->p = *p;
206 *p += params->len;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (*p != end) {
209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
210 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
211 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214}
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100218 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
219 * WARNING: the resulting group should only be used with
220 * pk_group_id_from_specified(), since its base point may not be set correctly
221 * if it was encoded compressed.
222 *
223 * SpecifiedECDomain ::= SEQUENCE {
224 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
225 * fieldID FieldID {{FieldTypes}},
226 * curve Curve,
227 * base ECPoint,
228 * order INTEGER,
229 * cofactor INTEGER OPTIONAL,
230 * hash HashAlgorithm OPTIONAL,
231 * ...
232 * }
233 *
234 * We only support prime-field as field type, and ignore hash and cofactor.
235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237{
Janos Follath24eed8d2019-11-22 13:21:35 +0000238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100239 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200240 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100241 const unsigned char *end_field, *end_curve;
242 size_t len;
243 int ver;
244
245 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
248 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ver < 1 || ver > 3) {
251 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
252 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100253
254 /*
255 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
256 * fieldType FIELD-ID.&id({IOSet}),
257 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
258 * }
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
261 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
262 return ret;
263 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100264
265 end_field = p + len;
266
267 /*
268 * FIELD-ID ::= TYPE-IDENTIFIER
269 * FieldTypes FIELD-ID ::= {
270 * { Prime-p IDENTIFIED BY prime-field } |
271 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
272 * }
273 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
276 return ret;
277 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
280 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
281 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100282 }
283
284 p += len;
285
286 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
289 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (p != end_field) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
295 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
296 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297
298 /*
299 * Curve ::= SEQUENCE {
300 * a FieldElement,
301 * b FieldElement,
302 * seed BIT STRING OPTIONAL
303 * -- Shall be present if used in SpecifiedECDomain
304 * -- with version equal to ecdpVer2 or ecdpVer3
305 * }
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
309 return ret;
310 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311
312 end_curve = p + len;
313
314 /*
315 * FieldElement ::= OCTET STRING
316 * containing an integer in the case of a prime field
317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
319 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
320 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 }
322
323 p += len;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
326 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328 }
329
330 p += len;
331
332 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (p != end_curve) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
340 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100341
342 /*
343 * ECPoint ::= OCTET STRING
344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
347 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
350 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100351 /*
352 * If we can't read the point because it's compressed, cheat by
353 * reading only the X coordinate and the parity bit of Y.
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
356 (p[0] != 0x02 && p[0] != 0x03) ||
357 len != mbedtls_mpi_size(&grp->P) + 1 ||
358 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
359 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
360 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
361 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100362 }
363 }
364
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365 p += len;
366
367 /*
368 * order INTEGER
369 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
371 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
372 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375
376 /*
377 * Allow optional elements by purposefully not enforcing p == end here.
378 */
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381}
382
383/*
384 * Find the group id associated with an (almost filled) group as generated by
385 * pk_group_from_specified(), or return an error if unknown.
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387static 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 +0100388{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100389 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ecp_group ref;
391 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_ecp_group_free(&ref);
398 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399
400 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
402 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
403 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
404 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
405 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
406 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
407 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 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 +0100410 break;
411 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412 }
413
414cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416
417 *grp_id = *id;
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424}
425
426/*
427 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
430 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431{
Janos Follath24eed8d2019-11-22 13:21:35 +0000432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
443cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000444 /* The API respecting lifecycle for mbedtls_ecp_group struct is
445 * _init(), _load() and _free(). In pk_group_id_from_specified() the
446 * temporary grp breaks that flow and it's members are populated
447 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
448 * which is assuming a group populated by _setup() may not clean-up
449 * properly -> Manually free it's members.
450 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000451 mbedtls_mpi_free(&grp.N);
452 mbedtls_mpi_free(&grp.P);
453 mbedtls_mpi_free(&grp.A);
454 mbedtls_mpi_free(&grp.B);
455 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Valerio Setti4064dbb2023-05-17 15:33:07 +0200461#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
462/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
463 * ecp_keypair structure with proper group ID. The purpose of this helper
464 * function is to update ec_family and ec_bits accordingly. */
465static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
466 mbedtls_ecp_group_id grp_id)
467{
468 psa_ecc_family_t ec_family;
469 size_t bits;
470
471 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
472
473 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
474 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
475 }
476
477 pk->ec_family = ec_family;
478 pk->ec_bits = bits;
479
480 return 0;
481}
482#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
483
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100484/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100486 *
487 * ECParameters ::= CHOICE {
488 * namedCurve OBJECT IDENTIFIER
489 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
490 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200492static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493{
Janos Follath24eed8d2019-11-22 13:21:35 +0000494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (params->tag == MBEDTLS_ASN1_OID) {
498 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
499 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
500 }
501 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
504 return ret;
505 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100506#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100508#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100509 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510
Valerio Setti00e8dd12023-05-18 18:56:59 +0200511#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
512 ret = pk_update_psa_ecparams(pk, grp_id);
513#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200514 /* grp may already be initialized; if so, make sure IDs match */
515 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
516 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
518 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519
Valerio Setti4064dbb2023-05-17 15:33:07 +0200520 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
521 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return ret;
523 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200524#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525
Valerio Setti4064dbb2023-05-17 15:33:07 +0200526 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527}
528
Jethro Beekman01672442023-04-19 14:08:14 +0200529/*
530 * Helper function for deriving a public key from its private counterpart.
531 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200532static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200533 const unsigned char *d, size_t d_len,
534 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
535{
536 int ret;
537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200538 psa_status_t status;
539 (void) f_rng;
540 (void) p_rng;
541#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
542 (void) d;
543 (void) d_len;
544
545 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
546 &pk->pub_raw_len);
547 ret = psa_pk_status_to_mbedtls(status);
548#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
549 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
550 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
551 size_t key_len;
552 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200553 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
554 size_t curve_bits;
555 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200556 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200557
558 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
559 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
560
561 status = psa_import_key(&key_attr, d, d_len, &key_id);
562 ret = psa_pk_status_to_mbedtls(status);
563 if (ret != 0) {
564 return ret;
565 }
566
Jethro Beekman01672442023-04-19 14:08:14 +0200567 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
568 ret = psa_pk_status_to_mbedtls(status);
569 destruction_status = psa_destroy_key(key_id);
570 if (ret != 0) {
571 return ret;
572 } else if (destruction_status != PSA_SUCCESS) {
573 return psa_pk_status_to_mbedtls(destruction_status);
574 }
Jethro Beekman01672442023-04-19 14:08:14 +0200575 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200576#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200577#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200578 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200579 (void) d;
580 (void) d_len;
581
582 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
583#endif /* MBEDTLS_USE_PSA_CRYPTO */
584 return ret;
585}
586
587#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
588
589/*
590 * Load an RFC8410 EC key, which doesn't have any parameters
591 */
592static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
593 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200594 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200595{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200596 int ret;
597
Jethro Beekman01672442023-04-19 14:08:14 +0200598 if (params->tag != 0 || params->len != 0) {
599 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
600 }
601
Valerio Setti00e8dd12023-05-18 18:56:59 +0200602#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
603 ret = pk_update_psa_ecparams(pk, grp_id);
604#else
605 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200606 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
607 if (ret != 0) {
608 return ret;
609 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200610#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200611 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200612}
613
614/*
615 * Parse an RFC 8410 encoded private EC key
616 *
617 * CurvePrivateKey ::= OCTET STRING
618 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200619static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200620 unsigned char *key, size_t keylen, const unsigned char *end,
621 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
622{
623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
624 size_t len;
625
626 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
627 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
628 }
629
630 if (key + len != end) {
631 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
632 }
633
Valerio Setti00e8dd12023-05-18 18:56:59 +0200634#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
636 psa_status_t status;
637
638 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200639 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
640 PSA_KEY_USAGE_DERIVE);
641 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200642
643 status = psa_import_key(&attributes, key, len, &pk->priv_id);
644 if (status != PSA_SUCCESS) {
645 ret = psa_pk_status_to_mbedtls(status);
646 return ret;
647 }
648#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
649 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
650
Valerio Setti805e4a02023-06-30 17:16:19 +0200651 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200652 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
653 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200654#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200655
Valerio Setti4064dbb2023-05-17 15:33:07 +0200656 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
657 * which never contain a public key. As such, derive the public key
658 * unconditionally. */
659 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200660 return ret;
661 }
662
Jethro Beekman01672442023-04-19 14:08:14 +0200663 return 0;
664}
665#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200666
Valerio Settiaddeee42023-06-14 10:46:55 +0200667#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200668/*
669 * Create a temporary ecp_keypair for converting an EC point in compressed
670 * format to an uncompressed one
671 */
672static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
673 const unsigned char *in_start, size_t in_len,
674 size_t *out_buf_len, unsigned char *out_buf,
675 size_t out_buf_size)
676{
677 mbedtls_ecp_keypair ecp_key;
678 mbedtls_ecp_group_id ecp_group_id;
679 int ret;
680
681 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
682
683 mbedtls_ecp_keypair_init(&ecp_key);
684 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
685 if (ret != 0) {
686 return ret;
687 }
688 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
689 in_start, in_len);
690 if (ret != 0) {
691 goto exit;
692 }
693 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
694 MBEDTLS_ECP_PF_UNCOMPRESSED,
695 out_buf_len, out_buf, out_buf_size);
696
697exit:
698 mbedtls_ecp_keypair_free(&ecp_key);
699 return ret;
700}
Valerio Settiaddeee42023-06-14 10:46:55 +0200701#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200702
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703/*
704 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100705 *
706 * The caller is responsible for clearing the structure upon failure if
707 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200711 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712{
Janos Follath24eed8d2019-11-22 13:21:35 +0000713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714
Valerio Setti4064dbb2023-05-17 15:33:07 +0200715#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
716 mbedtls_svc_key_id_t key;
717 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
718 size_t len = (end - *p);
719
720 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
721 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200722 }
723
Valerio Setti4064dbb2023-05-17 15:33:07 +0200724 /* Compressed point format are not supported yet by PSA crypto. As a
725 * consequence ecp functions are used to "convert" the point to
726 * uncompressed format */
727 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200728#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200729 ret = pk_convert_compressed_ec(pk, *p, len,
730 &(pk->pub_raw_len), pk->pub_raw,
731 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
732 if (ret != 0) {
733 return ret;
734 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200735#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
736 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
737#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200738 } else {
739 /* Uncompressed format */
740 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200741 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200742 }
743 memcpy(pk->pub_raw, *p, (end - *p));
744 pk->pub_raw_len = end - *p;
745 }
746
747 /* Validate the key by trying to importing it */
748 psa_set_key_usage_flags(&key_attrs, 0);
749 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
750 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
751 psa_set_key_bits(&key_attrs, pk->ec_bits);
752
753 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
754 &key) != PSA_SUCCESS) ||
755 (psa_destroy_key(key) != PSA_SUCCESS)) {
756 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
757 pk->pub_raw_len = 0;
758 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
759 }
760 ret = 0;
761#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
762 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
763 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
764 (const unsigned char *) *p,
765 end - *p)) == 0) {
766 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
767 }
768#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
769
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772 */
773 *p = (unsigned char *) end;
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776}
Valerio Setti81d75122023-06-14 14:49:33 +0200777#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780/*
781 * RSAPublicKey ::= SEQUENCE {
782 * modulus INTEGER, -- n
783 * publicExponent INTEGER -- e
784 * }
785 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786static int pk_get_rsapubkey(unsigned char **p,
787 const unsigned char *end,
788 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789{
Janos Follath24eed8d2019-11-22 13:21:35 +0000790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791 size_t len;
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
794 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (*p + len != end) {
799 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
801 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100803 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
806 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
809 NULL, 0, NULL, 0)) != 0) {
810 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
811 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812
813 *p += len;
814
815 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
817 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
818 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
821 NULL, 0, *p, len)) != 0) {
822 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
823 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824
825 *p += len;
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (mbedtls_rsa_complete(rsa) != 0 ||
828 mbedtls_rsa_check_pubkey(rsa) != 0) {
829 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000830 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (*p != end) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
834 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
835 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
841/* Get a PK algorithm identifier
842 *
843 * AlgorithmIdentifier ::= SEQUENCE {
844 * algorithm OBJECT IDENTIFIER,
845 * parameters ANY DEFINED BY algorithm OPTIONAL }
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847static int pk_get_pk_alg(unsigned char **p,
848 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200849 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
850 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851{
Janos Follath24eed8d2019-11-22 13:21:35 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
858 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
859 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Jethro Beekman01672442023-04-19 14:08:14 +0200861 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200862#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200863 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
864 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
865 if (ret == 0) {
866 *pk_alg = MBEDTLS_PK_ECKEY;
867 }
868 }
869#else
870 (void) ec_grp_id;
871#endif
872 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
874 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
876 /*
877 * No parameters with RSA (only for EC)
878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (*pk_alg == MBEDTLS_PK_RSA &&
880 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
881 params->len != 0)) {
882 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 }
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886}
887
888/*
889 * SubjectPublicKeyInfo ::= SEQUENCE {
890 * algorithm AlgorithmIdentifier,
891 * subjectPublicKey BIT STRING }
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
894 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895{
Janos Follath24eed8d2019-11-22 13:21:35 +0000896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_asn1_buf alg_params;
899 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200900 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
904 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
905 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 }
907
908 end = *p + len;
909
Jethro Beekman01672442023-04-19 14:08:14 +0200910 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 return ret;
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
916 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (*p + len != end) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
920 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
921 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
924 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
925 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
928 return ret;
929 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (pk_alg == MBEDTLS_PK_RSA) {
933 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200936#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200938#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200939 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200940 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200941 } else
942#endif
943 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200944 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200945 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200947 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200950#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (ret == 0 && *p != end) {
954 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
955 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
956 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (ret != 0) {
959 mbedtls_pk_free(pk);
960 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963}
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100967 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
968 *
969 * The value zero is:
970 * - never a valid value for an RSA parameter
971 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
972 *
973 * Since values can't be omitted in PKCS#1, passing a zero value to
974 * rsa_complete() would be incorrect, so reject zero values early.
975 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int asn1_get_nonzero_mpi(unsigned char **p,
977 const unsigned char *end,
978 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100979{
980 int ret;
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 ret = mbedtls_asn1_get_mpi(p, end, X);
983 if (ret != 0) {
984 return ret;
985 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
988 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
989 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100992}
993
994/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 * Parse a PKCS#1 encoded private RSA key
996 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
998 const unsigned char *key,
999 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001001 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002 size_t len;
1003 unsigned char *p, *end;
1004
Hanno Beckerefa14e82017-10-11 19:45:19 +01001005 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001007
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 p = (unsigned char *) key;
1009 end = p + keylen;
1010
1011 /*
1012 * This function parses the RSAPrivateKey (PKCS#1)
1013 *
1014 * RSAPrivateKey ::= SEQUENCE {
1015 * version Version,
1016 * modulus INTEGER, -- n
1017 * publicExponent INTEGER, -- e
1018 * privateExponent INTEGER, -- d
1019 * prime1 INTEGER, -- p
1020 * prime2 INTEGER, -- q
1021 * exponent1 INTEGER, -- d mod (p-1)
1022 * exponent2 INTEGER, -- d mod (q-1)
1023 * coefficient INTEGER, -- (inverse of q) mod p
1024 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1025 * }
1026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1028 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1029 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 }
1031
1032 end = p + len;
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1035 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 }
1037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (version != 0) {
1039 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 }
1041
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001042 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1044 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1045 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001049 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1051 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1052 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001053 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055
1056 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1058 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1059 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001062
1063 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1065 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1066 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001067 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001069
1070 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1072 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1073 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001074 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001076
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001077#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001078 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1080 * that they can be easily recomputed from D, P and Q. However by
1081 * parsing them from the PKCS1 structure it is possible to avoid
1082 * recalculating them which both reduces the overhead of loading
1083 * RSA private keys into memory and also avoids side channels which
1084 * can arise when computing those values, since all of D, P, and Q
1085 * are secret. See https://eprint.iacr.org/2020/055 for a
1086 * description of one such attack.
1087 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001088
Jack Lloyd80cc8112020-01-22 17:34:29 -05001089 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1091 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1092 goto cleanup;
1093 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001094
1095 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1097 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1098 goto cleanup;
1099 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001100
1101 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1103 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1104 goto cleanup;
1105 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001106
Jack Lloyd60239752020-01-27 17:53:36 -05001107#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001108 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1110 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1111 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1112 goto cleanup;
1113 }
Jack Lloyd60239752020-01-27 17:53:36 -05001114#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001115
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001116 /* rsa_complete() doesn't complete anything with the default
1117 * implementation but is still called:
1118 * - for the benefit of alternative implementation that may want to
1119 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1120 * - as is also sanity-checks the key
1121 *
1122 * Furthermore, we also check the public part for consistency with
1123 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1126 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001127 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001128 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (p != end) {
1131 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1132 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 }
1134
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001135cleanup:
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001140 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((ret & 0xff80) == 0) {
1142 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1143 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001144 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 }
1149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153
Valerio Setti81d75122023-06-14 14:49:33 +02001154#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155/*
1156 * Parse a SEC1 encoded private EC key
1157 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001158static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 const unsigned char *key, size_t keylen,
1160 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161{
Janos Follath24eed8d2019-11-22 13:21:35 +00001162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001163 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001164 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001165 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001167 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 unsigned char *end = p + keylen;
1169 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001170#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1172 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001173#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1174 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001175#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
1177 /*
1178 * RFC 5915, or SEC1 Appendix C.4
1179 *
1180 * ECPrivateKey ::= SEQUENCE {
1181 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1182 * privateKey OCTET STRING,
1183 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1184 * publicKey [1] BIT STRING OPTIONAL
1185 * }
1186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1188 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 }
1191
1192 end = p + len;
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (version != 1) {
1199 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Valerio Setti6b062ee2023-06-30 17:32:57 +02001206 /* Keep a reference to the position fo the private key. It will be used
1207 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001208 d = p;
1209 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001210
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211 p += len;
1212
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001213 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001215 /*
1216 * Is 'parameters' present?
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1219 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1220 0)) == 0) {
1221 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001222 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001224 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001227 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001228 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001229
Valerio Setti6b062ee2023-06-30 17:32:57 +02001230
1231#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1232 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1233 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1234 }
1235#endif
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238 /*
1239 * Is 'publickey' present? If not, or if we can't read it (eg because it
1240 * is compressed), create it from the private key.
1241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1243 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1244 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001245 end2 = p + len;
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1249 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (p + len != end2) {
1252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1254 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001255
Valerio Setti4064dbb2023-05-17 15:33:07 +02001256 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001257 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259 /*
1260 * The only acceptable failure mode of pk_get_ecpubkey() above
1261 * is if the point format is not recognized.
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1264 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1265 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001266 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001269 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001271
Valerio Setti00e8dd12023-05-18 18:56:59 +02001272#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1273 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1274 /* Setting largest masks for usage and key algorithms */
1275 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1276 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001277 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001278#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1279 psa_set_key_algorithm(&attributes,
1280 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1281#else
1282 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1283#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001284 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001285
Valerio Settia541e012023-05-24 14:31:21 +02001286 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001287 if (status != PSA_SUCCESS) {
1288 ret = psa_pk_status_to_mbedtls(status);
1289 return ret;
1290 }
1291#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1292
Valerio Setti34f67552023-04-03 15:19:18 +02001293 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001294 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001295 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001296 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001297 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300}
Valerio Setti81d75122023-06-14 14:49:33 +02001301#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302
1303/*
1304 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001305 *
1306 * Notes:
1307 *
1308 * - This function does not own the key buffer. It is the
1309 * responsibility of the caller to take care of zeroizing
1310 * and freeing it after use.
1311 *
1312 * - The function is responsible for freeing the provided
1313 * PK context on failure.
1314 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315 */
1316static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 mbedtls_pk_context *pk,
1318 const unsigned char *key, size_t keylen,
1319 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320{
1321 int ret, version;
1322 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 unsigned char *p = (unsigned char *) key;
1325 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001327 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
Valerio Setti81d75122023-06-14 14:49:33 +02001330#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001331 (void) f_rng;
1332 (void) p_rng;
1333#endif
1334
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001336 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337 *
1338 * PrivateKeyInfo ::= SEQUENCE {
1339 * version Version,
1340 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1341 * privateKey PrivateKey,
1342 * attributes [0] IMPLICIT Attributes OPTIONAL }
1343 *
1344 * Version ::= INTEGER
1345 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1346 * PrivateKey ::= OCTET STRING
1347 *
1348 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1349 */
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1352 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1353 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 }
1355
1356 end = p + len;
1357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001360 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (version != 0) {
1363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1364 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365
Jethro Beekman01672442023-04-19 14:08:14 +02001366 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return ret;
1368 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1371 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1372 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (len < 1) {
1375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1376 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1377 }
1378
1379 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1380 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1381 }
1382
1383 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1384 return ret;
1385 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if (pk_alg == MBEDTLS_PK_RSA) {
1389 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1390 mbedtls_pk_free(pk);
1391 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 }
1393 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001395#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001397#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001398 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001399 if ((ret =
1400 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001401 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001402 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001403 p_rng)) != 0) {
1404 mbedtls_pk_free(pk);
1405 return ret;
1406 }
1407 } else
1408#endif
1409 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001410 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1411 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001412 mbedtls_pk_free(pk);
1413 return ret;
1414 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415 }
1416 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001417#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001420 end = p + len;
1421 if (end != (key + keylen)) {
1422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1423 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1424 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001425
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
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001454 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455
Hanno Becker2aa80a72017-09-07 15:28:45 +01001456 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457 end = p + keylen;
1458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (pwdlen == 0) {
1460 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1461 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
1463 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001464 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 *
1466 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1467 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1468 * encryptedData EncryptedData
1469 * }
1470 *
1471 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1472 *
1473 * EncryptedData ::= OCTET STRING
1474 *
1475 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001476 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1479 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1480 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 }
1482
1483 end = p + len;
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1486 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1487 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1490 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1491 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
Hanno Beckerfab35692017-08-25 13:38:26 +01001493 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
1495 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001496 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001500 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001501 cipher_alg, md_alg,
1502 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1504 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1505 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001509
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001510 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_PKCS12_C */
1513#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001515 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1516 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1518 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1519 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001523
1524 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001527 {
1528 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001529 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (decrypted == 0) {
1532 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1533 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001534 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, 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
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001689 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690 }
1691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (ret == 0) {
1693 return 0;
1694 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 mbedtls_pk_free(pk);
1697 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1700 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1705 if (ret == 0) {
1706 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001707 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 mbedtls_pk_free(pk);
1710 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1715 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1716 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1717 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718 }
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_pk_free(pk);
1721 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001723
Valerio Setti81d75122023-06-14 14:49:33 +02001724#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1726 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001727 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 key, keylen, f_rng, p_rng) == 0) {
1729 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001732#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733
Valerio Setti81d75122023-06-14 14:49:33 +02001734 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001735 * it is ok to leave the PK context initialized but not
1736 * freed: It is the caller's responsibility to call pk_init()
1737 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001738 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001739 * isn't, this leads to mbedtls_pk_free() being called
1740 * twice, once here and once by the caller, but this is
1741 * also ok and in line with the mbedtls_pk_free() calls
1742 * on failed PEM parsing attempts. */
1743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001745}
1746
1747/*
1748 * Parse a public key
1749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1751 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001752{
Janos Follath24eed8d2019-11-22 13:21:35 +00001753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001754 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001755#if defined(MBEDTLS_RSA_C)
1756 const mbedtls_pk_info_t *pk_info;
1757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001759 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001761#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (keylen == 0) {
1764 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1765 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001766
1767#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001769#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001770 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001772 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 } else {
1774 ret = mbedtls_pem_read_buffer(&pem,
1775 "-----BEGIN RSA PUBLIC KEY-----",
1776 "-----END RSA PUBLIC KEY-----",
1777 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001779
1780 if (ret == 0) {
1781 p = pem.buf;
1782 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1783 mbedtls_pem_free(&pem);
1784 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1785 }
1786
1787 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1788 mbedtls_pem_free(&pem);
1789 return ret;
1790 }
1791
1792 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1793 mbedtls_pk_free(ctx);
1794 }
1795
1796 mbedtls_pem_free(&pem);
1797 return ret;
1798 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1799 mbedtls_pem_free(&pem);
1800 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001801 }
1802#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001803
1804 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001806 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 } else {
1808 ret = mbedtls_pem_read_buffer(&pem,
1809 "-----BEGIN PUBLIC KEY-----",
1810 "-----END PUBLIC KEY-----",
1811 key, NULL, 0, &len);
1812 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001815 /*
1816 * Was PEM encoded
1817 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001818 p = pem.buf;
1819
Jethro Beekman01672442023-04-19 14:08:14 +02001820 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 mbedtls_pem_free(&pem);
1822 return ret;
1823 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1824 mbedtls_pem_free(&pem);
1825 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001829
1830#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1832 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001834
1835 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1836 return ret;
1837 }
1838
1839 p = (unsigned char *) key;
1840 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1841 if (ret == 0) {
1842 return ret;
1843 }
1844 mbedtls_pk_free(ctx);
1845 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1846 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1847 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001848 }
1849#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001850 p = (unsigned char *) key;
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001855}
1856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857#endif /* MBEDTLS_PK_PARSE_C */