blob: edcc2e2912a059d35d2162ac3323719c4a9efec1 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Valerio Settie0e63112023-05-18 18:48:07 +020058/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020059#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020060#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
61 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020062#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020063
Gilles Peskine832f3492017-11-30 11:42:12 +010064#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020065/*
66 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020067 *
68 * The file is expected to contain either PEM or DER encoded data.
69 * A terminating null byte is always appended. It is included in the announced
70 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073{
74 FILE *f;
75 long size;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((f = fopen(path, "rb")) == NULL) {
78 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
79 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskineda0913b2022-06-30 17:03:40 +020081 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 fseek(f, 0, SEEK_END);
85 if ((size = ftell(f)) == -1) {
86 fclose(f);
87 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 }
Gilles Peskine449bd832023-01-11 14:50:10 +010089 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
91 *n = (size_t) size;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (*n + 1 == 0 ||
94 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
95 fclose(f);
96 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (fread(*buf, 1, *n, f) != *n) {
100 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100102 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
109 (*buf)[*n] = '\0';
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117
118/*
119 * Load and parse a private key
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
122 const char *path, const char *pwd,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 size_t n;
127 unsigned char *buf;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
130 return ret;
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (pwd == NULL) {
134 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
135 } else {
136 ret = mbedtls_pk_parse_key(ctx, buf, n,
137 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100140 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143}
144
145/*
146 * Load and parse a public key
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 size_t n;
152 unsigned char *buf;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
155 return ret;
156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100160 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Valerio Setti81d75122023-06-14 14:49:33 +0200166#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (end - *p < 1) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
183 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100190#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 params->p = *p;
201 *p += params->len;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (*p != end) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200235 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
243 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ver < 1 || ver > 3) {
246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
257 return ret;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 end_field = p + len;
261
262 /*
263 * FIELD-ID ::= TYPE-IDENTIFIER
264 * FieldTypes FIELD-ID ::= {
265 * { Prime-p IDENTIFIED BY prime-field } |
266 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
267 * }
268 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
271 return ret;
272 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
275 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
276 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 }
278
279 p += len;
280
281 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (p != end_field) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
291 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /*
294 * Curve ::= SEQUENCE {
295 * a FieldElement,
296 * b FieldElement,
297 * seed BIT STRING OPTIONAL
298 * -- Shall be present if used in SpecifiedECDomain
299 * -- with version equal to ecdpVer2 or ecdpVer3
300 * }
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
304 return ret;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
314 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316 }
317
318 p += len;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
321 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (p != end_curve) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
342 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
345 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
351 (p[0] != 0x02 && p[0] != 0x03) ||
352 len != mbedtls_mpi_size(&grp->P) + 1 ||
353 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
354 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
356 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 }
358 }
359
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 p += len;
361
362 /*
363 * order INTEGER
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
367 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_free(&ref);
393 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
395 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
397 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
398 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405 break;
406 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 }
408
409cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411
412 *grp_id = *id;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419}
420
421/*
422 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
425 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426{
Janos Follath24eed8d2019-11-22 13:21:35 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000439 /* The API respecting lifecycle for mbedtls_ecp_group struct is
440 * _init(), _load() and _free(). In pk_group_id_from_specified() the
441 * temporary grp breaks that flow and it's members are populated
442 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
443 * which is assuming a group populated by _setup() may not clean-up
444 * properly -> Manually free it's members.
445 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000446 mbedtls_mpi_free(&grp.N);
447 mbedtls_mpi_free(&grp.P);
448 mbedtls_mpi_free(&grp.A);
449 mbedtls_mpi_free(&grp.B);
450 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200456/*
457 * Set the group used by this key.
458 */
459static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200460{
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200461#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
462 size_t ec_bits;
463 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200464
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200465 /* group may already be initialized; if so, make sure IDs match */
466 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
467 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200468 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
469 }
470
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200471 /* set group */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200472 pk->ec_family = ec_family;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200473 pk->ec_bits = ec_bits;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200474
475 return 0;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200476#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
477 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
478
479 /* grp may already be initialized; if so, make sure IDs match */
480 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
481 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
482 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
483 }
484
485 /* set group */
486 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200487#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200488}
Valerio Setti4064dbb2023-05-17 15:33:07 +0200489
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 *
493 * ECParameters ::= CHOICE {
494 * namedCurve OBJECT IDENTIFIER
495 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
496 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200498static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (params->tag == MBEDTLS_ASN1_OID) {
504 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
505 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
506 }
507 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
510 return ret;
511 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100512#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100514#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200517 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518}
519
Jethro Beekman01672442023-04-19 14:08:14 +0200520/*
521 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200522 *
523 * Note: the private key information is always available from pk,
524 * however for convenience the serialized version is also passed,
525 * as it's available at each calling site, and useful in some configs
526 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200527 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200528static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200529 const unsigned char *d, size_t d_len,
530 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
531{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200532#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200533 psa_status_t status;
534 (void) f_rng;
535 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200536 (void) d;
537 (void) d_len;
538
539 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
540 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200541 return psa_pk_status_to_mbedtls(status);
542#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
543 int ret;
544 psa_status_t status;
545 (void) f_rng;
546 (void) p_rng;
547
Valerio Setti00e8dd12023-05-18 18:56:59 +0200548 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
549 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
550 size_t key_len;
551 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200552 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
553 size_t curve_bits;
554 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200555 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200556
557 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
558 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
559
560 status = psa_import_key(&key_attr, d, d_len, &key_id);
561 ret = psa_pk_status_to_mbedtls(status);
562 if (ret != 0) {
563 return ret;
564 }
565
Jethro Beekman01672442023-04-19 14:08:14 +0200566 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
567 ret = psa_pk_status_to_mbedtls(status);
568 destruction_status = psa_destroy_key(key_id);
569 if (ret != 0) {
570 return ret;
571 } else if (destruction_status != PSA_SUCCESS) {
572 return psa_pk_status_to_mbedtls(destruction_status);
573 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200574 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200575#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200576 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200577 (void) d;
578 (void) d_len;
579
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200580 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200581#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200582}
583
584#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
585
586/*
587 * Load an RFC8410 EC key, which doesn't have any parameters
588 */
589static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
590 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200591 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200592{
593 if (params->tag != 0 || params->len != 0) {
594 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
595 }
596
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200597 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200598}
599
600/*
601 * Parse an RFC 8410 encoded private EC key
602 *
603 * CurvePrivateKey ::= OCTET STRING
604 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200605static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200606 unsigned char *key, size_t keylen, const unsigned char *end,
607 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
608{
609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
610 size_t len;
611
612 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
613 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
614 }
615
616 if (key + len != end) {
617 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
618 }
619
Valerio Setti00e8dd12023-05-18 18:56:59 +0200620#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
622 psa_status_t status;
623
624 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200625 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
626 PSA_KEY_USAGE_DERIVE);
627 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200628
629 status = psa_import_key(&attributes, key, len, &pk->priv_id);
630 if (status != PSA_SUCCESS) {
631 ret = psa_pk_status_to_mbedtls(status);
632 return ret;
633 }
634#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
635 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
636
Valerio Setti805e4a02023-06-30 17:16:19 +0200637 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200638 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
639 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200640#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200641
Valerio Setti4064dbb2023-05-17 15:33:07 +0200642 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
643 * which never contain a public key. As such, derive the public key
644 * unconditionally. */
645 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200646 return ret;
647 }
648
Jethro Beekman01672442023-04-19 14:08:14 +0200649 return 0;
650}
651#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200652
Valerio Settiaddeee42023-06-14 10:46:55 +0200653#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200654/*
655 * Create a temporary ecp_keypair for converting an EC point in compressed
656 * format to an uncompressed one
657 */
658static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
659 const unsigned char *in_start, size_t in_len,
660 size_t *out_buf_len, unsigned char *out_buf,
661 size_t out_buf_size)
662{
663 mbedtls_ecp_keypair ecp_key;
664 mbedtls_ecp_group_id ecp_group_id;
665 int ret;
666
667 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
668
669 mbedtls_ecp_keypair_init(&ecp_key);
670 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
671 if (ret != 0) {
672 return ret;
673 }
674 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
675 in_start, in_len);
676 if (ret != 0) {
677 goto exit;
678 }
679 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
680 MBEDTLS_ECP_PF_UNCOMPRESSED,
681 out_buf_len, out_buf, out_buf_size);
682
683exit:
684 mbedtls_ecp_keypair_free(&ecp_key);
685 return ret;
686}
Valerio Settiaddeee42023-06-14 10:46:55 +0200687#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200688
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689/*
690 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100691 *
692 * The caller is responsible for clearing the structure upon failure if
693 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200697 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698{
Janos Follath24eed8d2019-11-22 13:21:35 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700
Valerio Setti4064dbb2023-05-17 15:33:07 +0200701#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
702 mbedtls_svc_key_id_t key;
703 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
704 size_t len = (end - *p);
705
706 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
707 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708 }
709
Valerio Setti4064dbb2023-05-17 15:33:07 +0200710 /* Compressed point format are not supported yet by PSA crypto. As a
711 * consequence ecp functions are used to "convert" the point to
712 * uncompressed format */
713 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200714#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200715 ret = pk_convert_compressed_ec(pk, *p, len,
716 &(pk->pub_raw_len), pk->pub_raw,
717 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
718 if (ret != 0) {
719 return ret;
720 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200721#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
722 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
723#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200724 } else {
725 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100726 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200727 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200728 }
729 memcpy(pk->pub_raw, *p, (end - *p));
730 pk->pub_raw_len = end - *p;
731 }
732
733 /* Validate the key by trying to importing it */
734 psa_set_key_usage_flags(&key_attrs, 0);
735 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
736 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
737 psa_set_key_bits(&key_attrs, pk->ec_bits);
738
739 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
740 &key) != PSA_SUCCESS) ||
741 (psa_destroy_key(key) != PSA_SUCCESS)) {
742 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
743 pk->pub_raw_len = 0;
744 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
745 }
746 ret = 0;
747#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
748 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
749 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
750 (const unsigned char *) *p,
751 end - *p)) == 0) {
752 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
753 }
754#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
755
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758 */
759 *p = (unsigned char *) end;
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762}
Valerio Setti81d75122023-06-14 14:49:33 +0200763#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766/*
767 * RSAPublicKey ::= SEQUENCE {
768 * modulus INTEGER, -- n
769 * publicExponent INTEGER -- e
770 * }
771 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772static int pk_get_rsapubkey(unsigned char **p,
773 const unsigned char *end,
774 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775{
Janos Follath24eed8d2019-11-22 13:21:35 +0000776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777 size_t len;
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
780 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
781 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
782 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (*p + len != end) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
786 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
787 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100789 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
792 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
795 NULL, 0, NULL, 0)) != 0) {
796 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
797 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100798
799 *p += len;
800
801 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
803 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
804 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
807 NULL, 0, *p, len)) != 0) {
808 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
809 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100810
811 *p += len;
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (mbedtls_rsa_complete(rsa) != 0 ||
814 mbedtls_rsa_check_pubkey(rsa) != 0) {
815 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000816 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (*p != end) {
819 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
820 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
821 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826
827/* Get a PK algorithm identifier
828 *
829 * AlgorithmIdentifier ::= SEQUENCE {
830 * algorithm OBJECT IDENTIFIER,
831 * parameters ANY DEFINED BY algorithm OPTIONAL }
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833static int pk_get_pk_alg(unsigned char **p,
834 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200835 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
836 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837{
Janos Follath24eed8d2019-11-22 13:21:35 +0000838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
844 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
845 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Jethro Beekman01672442023-04-19 14:08:14 +0200847 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200848#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200849 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
850 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
851 if (ret == 0) {
852 *pk_alg = MBEDTLS_PK_ECKEY;
853 }
854 }
855#else
856 (void) ec_grp_id;
857#endif
858 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
860 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861
862 /*
863 * No parameters with RSA (only for EC)
864 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (*pk_alg == MBEDTLS_PK_RSA &&
866 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
867 params->len != 0)) {
868 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 }
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872}
873
874/*
875 * SubjectPublicKeyInfo ::= SEQUENCE {
876 * algorithm AlgorithmIdentifier,
877 * subjectPublicKey BIT STRING }
878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
880 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881{
Janos Follath24eed8d2019-11-22 13:21:35 +0000882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_asn1_buf alg_params;
885 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200886 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
890 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
891 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 }
893
894 end = *p + len;
895
Jethro Beekman01672442023-04-19 14:08:14 +0200896 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 return ret;
898 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
901 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
902 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (*p + len != end) {
905 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
906 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
910 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
911 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
914 return ret;
915 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (pk_alg == MBEDTLS_PK_RSA) {
919 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200922#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200924#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200925 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200926 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200927 } else
928#endif
929 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200930 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200931 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200933 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200935 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200936#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (ret == 0 && *p != end) {
940 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
941 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
942 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (ret != 0) {
945 mbedtls_pk_free(pk);
946 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949}
950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100953 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
954 *
955 * The value zero is:
956 * - never a valid value for an RSA parameter
957 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
958 *
959 * Since values can't be omitted in PKCS#1, passing a zero value to
960 * rsa_complete() would be incorrect, so reject zero values early.
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static int asn1_get_nonzero_mpi(unsigned char **p,
963 const unsigned char *end,
964 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100965{
966 int ret;
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 ret = mbedtls_asn1_get_mpi(p, end, X);
969 if (ret != 0) {
970 return ret;
971 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
974 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
975 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100978}
979
980/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 * Parse a PKCS#1 encoded private RSA key
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
984 const unsigned char *key,
985 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 size_t len;
989 unsigned char *p, *end;
990
Hanno Beckerefa14e82017-10-11 19:45:19 +0100991 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100993
Paul Bakker1a7550a2013-09-15 13:01:22 +0200994 p = (unsigned char *) key;
995 end = p + keylen;
996
997 /*
998 * This function parses the RSAPrivateKey (PKCS#1)
999 *
1000 * RSAPrivateKey ::= SEQUENCE {
1001 * version Version,
1002 * modulus INTEGER, -- n
1003 * publicExponent INTEGER, -- e
1004 * privateExponent INTEGER, -- d
1005 * prime1 INTEGER, -- p
1006 * prime2 INTEGER, -- q
1007 * exponent1 INTEGER, -- d mod (p-1)
1008 * exponent2 INTEGER, -- d mod (q-1)
1009 * coefficient INTEGER, -- (inverse of q) mod p
1010 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1011 * }
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1014 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1015 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 }
1017
1018 end = p + len;
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1021 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 }
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (version != 0) {
1025 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 }
1027
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001028 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1030 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1031 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001032 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001035 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1037 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1038 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041
1042 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1044 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1045 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048
1049 /* Import P */
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, &T, NULL,
1052 NULL, NULL)) != 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 Q */
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, &T,
1059 NULL, 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
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001063#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001064 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1066 * that they can be easily recomputed from D, P and Q. However by
1067 * parsing them from the PKCS1 structure it is possible to avoid
1068 * recalculating them which both reduces the overhead of loading
1069 * RSA private keys into memory and also avoids side channels which
1070 * can arise when computing those values, since all of D, P, and Q
1071 * are secret. See https://eprint.iacr.org/2020/055 for a
1072 * description of one such attack.
1073 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001074
Jack Lloyd80cc8112020-01-22 17:34:29 -05001075 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1077 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1078 goto cleanup;
1079 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001080
1081 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1083 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1084 goto cleanup;
1085 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001086
1087 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1089 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1090 goto cleanup;
1091 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001092
Jack Lloyd60239752020-01-27 17:53:36 -05001093#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001094 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1096 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1097 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1098 goto cleanup;
1099 }
Jack Lloyd60239752020-01-27 17:53:36 -05001100#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001101
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001102 /* rsa_complete() doesn't complete anything with the default
1103 * implementation but is still called:
1104 * - for the benefit of alternative implementation that may want to
1105 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1106 * - as is also sanity-checks the key
1107 *
1108 * Furthermore, we also check the public part for consistency with
1109 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1112 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001113 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001114 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (p != end) {
1117 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1118 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 }
1120
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001121cleanup:
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001126 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if ((ret & 0xff80) == 0) {
1128 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1129 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 }
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139
Valerio Setti81d75122023-06-14 14:49:33 +02001140#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141/*
1142 * Parse a SEC1 encoded private EC key
1143 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001144static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 const unsigned char *key, size_t keylen,
1146 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147{
Janos Follath24eed8d2019-11-22 13:21:35 +00001148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001149 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001150 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001151 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001153 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 unsigned char *end = p + keylen;
1155 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001156#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1158 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001159#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1160 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001161#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162
1163 /*
1164 * RFC 5915, or SEC1 Appendix C.4
1165 *
1166 * ECPrivateKey ::= SEQUENCE {
1167 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1168 * privateKey OCTET STRING,
1169 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1170 * publicKey [1] BIT STRING OPTIONAL
1171 * }
1172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1174 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 }
1177
1178 end = p + len;
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1182 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (version != 1) {
1185 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1186 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1190 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191
Valerio Setti6b062ee2023-06-30 17:32:57 +02001192 /* Keep a reference to the position fo the private key. It will be used
1193 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001194 d = p;
1195 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001196
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 p += len;
1198
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001199 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201 /*
1202 * Is 'parameters' present?
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1205 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1206 0)) == 0) {
1207 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001208 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001213 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001214 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001215
Valerio Setti6b062ee2023-06-30 17:32:57 +02001216
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001217#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1218 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1219 /* Setting largest masks for usage and key algorithms */
1220 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1221 PSA_KEY_USAGE_SIGN_MESSAGE |
1222 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
1223#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1224 psa_set_key_algorithm(&attributes,
1225 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1226#else
1227 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1228#endif
1229 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
1230
1231 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
1232 if (status != PSA_SUCCESS) {
1233 ret = psa_pk_status_to_mbedtls(status);
1234 return ret;
1235 }
1236#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti6b062ee2023-06-30 17:32:57 +02001237 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1239 }
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001240#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti6b062ee2023-06-30 17:32:57 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001243 /*
1244 * Is 'publickey' present? If not, or if we can't read it (eg because it
1245 * is compressed), create it from the private key.
1246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1248 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1249 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001250 end2 = p + len;
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1254 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (p + len != end2) {
1257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1258 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1259 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001260
Valerio Setti4064dbb2023-05-17 15:33:07 +02001261 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001262 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264 /*
1265 * The only acceptable failure mode of pk_get_ecpubkey() above
1266 * is if the point format is not recognized.
1267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1269 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1270 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001271 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001274 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001276
Valerio Setti34f67552023-04-03 15:19:18 +02001277 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001278 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001279 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001280 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001281 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284}
Valerio Setti81d75122023-06-14 14:49:33 +02001285#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
1287/*
1288 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001289 *
1290 * Notes:
1291 *
1292 * - This function does not own the key buffer. It is the
1293 * responsibility of the caller to take care of zeroizing
1294 * and freeing it after use.
1295 *
1296 * - The function is responsible for freeing the provided
1297 * PK context on failure.
1298 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 */
1300static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_pk_context *pk,
1302 const unsigned char *key, size_t keylen,
1303 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304{
1305 int ret, version;
1306 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 unsigned char *p = (unsigned char *) key;
1309 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001311 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313
Valerio Setti81d75122023-06-14 14:49:33 +02001314#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001315 (void) f_rng;
1316 (void) p_rng;
1317#endif
1318
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001320 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 *
1322 * PrivateKeyInfo ::= SEQUENCE {
1323 * version Version,
1324 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1325 * privateKey PrivateKey,
1326 * attributes [0] IMPLICIT Attributes OPTIONAL }
1327 *
1328 * Version ::= INTEGER
1329 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1330 * PrivateKey ::= OCTET STRING
1331 *
1332 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1333 */
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 }
1339
1340 end = p + len;
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (version != 0) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Jethro Beekman01672442023-04-19 14:08:14 +02001350 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return ret;
1352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (len < 1) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1360 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1361 }
1362
1363 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1364 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1365 }
1366
1367 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1368 return ret;
1369 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (pk_alg == MBEDTLS_PK_RSA) {
1373 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1374 mbedtls_pk_free(pk);
1375 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 }
1377 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001379#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001381#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001382 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001383 if ((ret =
1384 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001385 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001386 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001387 p_rng)) != 0) {
1388 mbedtls_pk_free(pk);
1389 return ret;
1390 }
1391 } else
1392#endif
1393 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001394 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1395 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001396 mbedtls_pk_free(pk);
1397 return ret;
1398 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 }
1400 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001401#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001404 end = p + len;
1405 if (end != (key + keylen)) {
1406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1407 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1408 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411}
1412
1413/*
1414 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001415 *
1416 * To save space, the decryption happens in-place on the given key buffer.
1417 * Also, while this function may modify the keybuffer, it doesn't own it,
1418 * and instead it is the responsibility of the caller to zeroize and properly
1419 * free it after use.
1420 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001423MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_pk_context *pk,
1425 unsigned char *key, size_t keylen,
1426 const unsigned char *pwd, size_t pwdlen,
1427 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001429 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001431 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1434#if defined(MBEDTLS_PKCS12_C)
1435 mbedtls_cipher_type_t cipher_alg;
1436 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001438 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Hanno Becker2aa80a72017-09-07 15:28:45 +01001440 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 end = p + keylen;
1442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (pwdlen == 0) {
1444 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1445 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446
1447 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001448 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 *
1450 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1451 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1452 * encryptedData EncryptedData
1453 * }
1454 *
1455 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1456 *
1457 * EncryptedData ::= OCTET STRING
1458 *
1459 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001460 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1463 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1464 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 }
1466
1467 end = p + len;
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1470 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1471 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1474 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476
Hanno Beckerfab35692017-08-25 13:38:26 +01001477 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
1479 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001480 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001484 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001485 cipher_alg, md_alg,
1486 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1488 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1489 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001493
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001494 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_PKCS12_C */
1497#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001499 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1500 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1502 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1503 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001507
1508 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001511 {
1512 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001513 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (decrypted == 0) {
1516 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1517 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001518 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521
1522/*
1523 * Parse a private key
1524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1526 const unsigned char *key, size_t keylen,
1527 const unsigned char *pwd, size_t pwdlen,
1528 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529{
Janos Follath24eed8d2019-11-22 13:21:35 +00001530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001535#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (keylen == 0) {
1538 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1539 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001540
1541#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001547 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 } else {
1549 ret = mbedtls_pem_read_buffer(&pem,
1550 "-----BEGIN RSA PRIVATE KEY-----",
1551 "-----END RSA PRIVATE KEY-----",
1552 key, pwd, pwdlen, &len);
1553 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (ret == 0) {
1556 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1557 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1558 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1559 pem.buf, pem.buflen)) != 0) {
1560 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_pem_free(&pem);
1564 return ret;
1565 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1566 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1567 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1568 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1569 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1570 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573
Valerio Setti81d75122023-06-14 14:49:33 +02001574#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001575 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 } else {
1579 ret = mbedtls_pem_read_buffer(&pem,
1580 "-----BEGIN EC PRIVATE KEY-----",
1581 "-----END EC PRIVATE KEY-----",
1582 key, pwd, pwdlen, &len);
1583 }
1584 if (ret == 0) {
1585 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001588 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 pem.buf, pem.buflen,
1590 f_rng, p_rng)) != 0) {
1591 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592 }
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_pem_free(&pem);
1595 return ret;
1596 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1597 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1598 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1599 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1600 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1601 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602 }
Valerio Setti81d75122023-06-14 14:49:33 +02001603#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001605 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001607 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else {
1609 ret = mbedtls_pem_read_buffer(&pem,
1610 "-----BEGIN PRIVATE KEY-----",
1611 "-----END PRIVATE KEY-----",
1612 key, NULL, 0, &len);
1613 }
1614 if (ret == 0) {
1615 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1616 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1617 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 mbedtls_pem_free(&pem);
1621 return ret;
1622 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1623 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001627 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001629 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 } else {
1631 ret = mbedtls_pem_read_buffer(&pem,
1632 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1633 "-----END ENCRYPTED PRIVATE KEY-----",
1634 key, NULL, 0, &len);
1635 }
1636 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001637 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1638 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640 }
1641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_pem_free(&pem);
1643 return ret;
1644 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1645 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001648#else
1649 ((void) pwd);
1650 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652
1653 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001654 * At this point we only know it's not a PEM formatted key. Could be any
1655 * of the known DER encoded private key formats
1656 *
1657 * We try the different DER format parsers to see if one passes without
1658 * error
1659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001662 unsigned char *key_copy;
1663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1665 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1666 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001669
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001670 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1671 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001672
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001673 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674 }
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (ret == 0) {
1677 return 0;
1678 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 mbedtls_pk_free(pk);
1681 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1684 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1689 if (ret == 0) {
1690 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001691 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_pk_free(pk);
1694 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1699 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1700 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1701 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 mbedtls_pk_free(pk);
1705 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001707
Valerio Setti81d75122023-06-14 14:49:33 +02001708#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1710 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001711 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 key, keylen, f_rng, p_rng) == 0) {
1713 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001716#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717
Valerio Setti81d75122023-06-14 14:49:33 +02001718 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001719 * it is ok to leave the PK context initialized but not
1720 * freed: It is the caller's responsibility to call pk_init()
1721 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001722 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001723 * isn't, this leads to mbedtls_pk_free() being called
1724 * twice, once here and once by the caller, but this is
1725 * also ok and in line with the mbedtls_pk_free() calls
1726 * on failed PEM parsing attempts. */
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001729}
1730
1731/*
1732 * Parse a public key
1733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1735 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001736{
Janos Follath24eed8d2019-11-22 13:21:35 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001738 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001739#if defined(MBEDTLS_RSA_C)
1740 const mbedtls_pk_info_t *pk_info;
1741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001743 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001745#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (keylen == 0) {
1748 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1749 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001750
1751#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001753#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001754 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001756 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 } else {
1758 ret = mbedtls_pem_read_buffer(&pem,
1759 "-----BEGIN RSA PUBLIC KEY-----",
1760 "-----END RSA PUBLIC KEY-----",
1761 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001763
1764 if (ret == 0) {
1765 p = pem.buf;
1766 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1767 mbedtls_pem_free(&pem);
1768 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1769 }
1770
1771 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1772 mbedtls_pem_free(&pem);
1773 return ret;
1774 }
1775
1776 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1777 mbedtls_pk_free(ctx);
1778 }
1779
1780 mbedtls_pem_free(&pem);
1781 return ret;
1782 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1783 mbedtls_pem_free(&pem);
1784 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001785 }
1786#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001787
1788 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001790 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 } else {
1792 ret = mbedtls_pem_read_buffer(&pem,
1793 "-----BEGIN PUBLIC KEY-----",
1794 "-----END PUBLIC KEY-----",
1795 key, NULL, 0, &len);
1796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001799 /*
1800 * Was PEM encoded
1801 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001802 p = pem.buf;
1803
Jethro Beekman01672442023-04-19 14:08:14 +02001804 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 mbedtls_pem_free(&pem);
1806 return ret;
1807 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1808 mbedtls_pem_free(&pem);
1809 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001810 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001813
1814#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1816 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001818
1819 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1820 return ret;
1821 }
1822
1823 p = (unsigned char *) key;
1824 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1825 if (ret == 0) {
1826 return ret;
1827 }
1828 mbedtls_pk_free(ctx);
1829 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1830 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1831 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001832 }
1833#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001834 p = (unsigned char *) key;
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001839}
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_PK_PARSE_C */