blob: 114a5638957a3966433be933b7114cd882fb06be [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/*
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200491 * Set the private key material
492 *
493 * Must have already set the group with pk_ecc_set_group().
494 *
495 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
496 */
497static int pk_ecc_set_key(mbedtls_pk_context *pk,
498 unsigned char *key, size_t len)
499{
500#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
502 psa_status_t status;
503
504 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
505 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
506 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
507 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
508 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
509#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
510 psa_set_key_enrollment_algorithm(&attributes,
511 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
512#else
513 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
514#endif
515 }
516 psa_set_key_usage_flags(&attributes, flags);
517
518 status = psa_import_key(&attributes, key, len, &pk->priv_id);
519 return psa_pk_status_to_mbedtls(status);
520
521#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
522
523 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
524 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
525 if (ret != 0) {
526 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
527 }
528 return 0;
529#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
530}
531
532/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100534 *
535 * ECParameters ::= CHOICE {
536 * namedCurve OBJECT IDENTIFIER
537 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
538 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200540static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541{
Janos Follath24eed8d2019-11-22 13:21:35 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (params->tag == MBEDTLS_ASN1_OID) {
546 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
547 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
548 }
549 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
552 return ret;
553 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100554#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100556#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100557 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200558
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200559 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200560}
561
Jethro Beekman01672442023-04-19 14:08:14 +0200562/*
563 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200564 *
565 * Note: the private key information is always available from pk,
566 * however for convenience the serialized version is also passed,
567 * as it's available at each calling site, and useful in some configs
568 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200569 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200570static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200571 const unsigned char *d, size_t d_len,
572 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
573{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200574#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200575 psa_status_t status;
576 (void) f_rng;
577 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200578 (void) d;
579 (void) d_len;
580
581 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
582 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200583 return psa_pk_status_to_mbedtls(status);
584#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
585 int ret;
586 psa_status_t status;
587 (void) f_rng;
588 (void) p_rng;
589
Valerio Setti00e8dd12023-05-18 18:56:59 +0200590 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
591 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
592 size_t key_len;
593 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200594 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
595 size_t curve_bits;
596 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200597 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200598
599 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
600 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
601
602 status = psa_import_key(&key_attr, d, d_len, &key_id);
603 ret = psa_pk_status_to_mbedtls(status);
604 if (ret != 0) {
605 return ret;
606 }
607
Jethro Beekman01672442023-04-19 14:08:14 +0200608 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
609 ret = psa_pk_status_to_mbedtls(status);
610 destruction_status = psa_destroy_key(key_id);
611 if (ret != 0) {
612 return ret;
613 } else if (destruction_status != PSA_SUCCESS) {
614 return psa_pk_status_to_mbedtls(destruction_status);
615 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200616 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200617#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200618 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200619 (void) d;
620 (void) d_len;
621
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200622 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200623#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200624}
625
626#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
627
628/*
629 * Load an RFC8410 EC key, which doesn't have any parameters
630 */
631static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
632 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200633 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200634{
635 if (params->tag != 0 || params->len != 0) {
636 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
637 }
638
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200639 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200640}
641
642/*
643 * Parse an RFC 8410 encoded private EC key
644 *
645 * CurvePrivateKey ::= OCTET STRING
646 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200647static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200648 unsigned char *key, size_t keylen, const unsigned char *end,
649 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
650{
651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
652 size_t len;
653
654 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
656 }
657
658 if (key + len != end) {
659 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
660 }
661
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200662 /*
663 * Load the private key
664 */
665 ret = pk_ecc_set_key(pk, key, len);
666 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200667 return ret;
668 }
Jethro Beekman01672442023-04-19 14:08:14 +0200669
Valerio Setti4064dbb2023-05-17 15:33:07 +0200670 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
671 * which never contain a public key. As such, derive the public key
672 * unconditionally. */
673 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200674 return ret;
675 }
676
Jethro Beekman01672442023-04-19 14:08:14 +0200677 return 0;
678}
679#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200680
Valerio Settiaddeee42023-06-14 10:46:55 +0200681#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200682/*
683 * Create a temporary ecp_keypair for converting an EC point in compressed
684 * format to an uncompressed one
685 */
686static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
687 const unsigned char *in_start, size_t in_len,
688 size_t *out_buf_len, unsigned char *out_buf,
689 size_t out_buf_size)
690{
691 mbedtls_ecp_keypair ecp_key;
692 mbedtls_ecp_group_id ecp_group_id;
693 int ret;
694
695 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
696
697 mbedtls_ecp_keypair_init(&ecp_key);
698 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
699 if (ret != 0) {
700 return ret;
701 }
702 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
703 in_start, in_len);
704 if (ret != 0) {
705 goto exit;
706 }
707 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
708 MBEDTLS_ECP_PF_UNCOMPRESSED,
709 out_buf_len, out_buf, out_buf_size);
710
711exit:
712 mbedtls_ecp_keypair_free(&ecp_key);
713 return ret;
714}
Valerio Settiaddeee42023-06-14 10:46:55 +0200715#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200716
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717/*
718 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100719 *
720 * The caller is responsible for clearing the structure upon failure if
721 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200725 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726{
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728
Valerio Setti4064dbb2023-05-17 15:33:07 +0200729#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
730 mbedtls_svc_key_id_t key;
731 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
732 size_t len = (end - *p);
733
734 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
735 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 }
737
Valerio Setti4064dbb2023-05-17 15:33:07 +0200738 /* Compressed point format are not supported yet by PSA crypto. As a
739 * consequence ecp functions are used to "convert" the point to
740 * uncompressed format */
741 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200742#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200743 ret = pk_convert_compressed_ec(pk, *p, len,
744 &(pk->pub_raw_len), pk->pub_raw,
745 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
746 if (ret != 0) {
747 return ret;
748 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200749#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
750 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
751#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200752 } else {
753 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100754 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200755 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200756 }
757 memcpy(pk->pub_raw, *p, (end - *p));
758 pk->pub_raw_len = end - *p;
759 }
760
761 /* Validate the key by trying to importing it */
762 psa_set_key_usage_flags(&key_attrs, 0);
763 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
764 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
765 psa_set_key_bits(&key_attrs, pk->ec_bits);
766
767 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
768 &key) != PSA_SUCCESS) ||
769 (psa_destroy_key(key) != PSA_SUCCESS)) {
770 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
771 pk->pub_raw_len = 0;
772 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
773 }
774 ret = 0;
775#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
776 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
777 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
778 (const unsigned char *) *p,
779 end - *p)) == 0) {
780 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
781 }
782#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
783
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786 */
787 *p = (unsigned char *) end;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790}
Valerio Setti81d75122023-06-14 14:49:33 +0200791#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794/*
795 * RSAPublicKey ::= SEQUENCE {
796 * modulus INTEGER, -- n
797 * publicExponent INTEGER -- e
798 * }
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800static int pk_get_rsapubkey(unsigned char **p,
801 const unsigned char *end,
802 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803{
Janos Follath24eed8d2019-11-22 13:21:35 +0000804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200805 size_t len;
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
808 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
810 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (*p + len != end) {
813 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
814 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100817 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
819 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
820 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
823 NULL, 0, NULL, 0)) != 0) {
824 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
825 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100826
827 *p += len;
828
829 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
831 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
832 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
835 NULL, 0, *p, len)) != 0) {
836 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
837 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838
839 *p += len;
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (mbedtls_rsa_complete(rsa) != 0 ||
842 mbedtls_rsa_check_pubkey(rsa) != 0) {
843 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000844 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (*p != end) {
847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
848 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
849 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
855/* Get a PK algorithm identifier
856 *
857 * AlgorithmIdentifier ::= SEQUENCE {
858 * algorithm OBJECT IDENTIFIER,
859 * parameters ANY DEFINED BY algorithm OPTIONAL }
860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int pk_get_pk_alg(unsigned char **p,
862 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200863 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
864 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
872 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Jethro Beekman01672442023-04-19 14:08:14 +0200875 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200876#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200877 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
878 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
879 if (ret == 0) {
880 *pk_alg = MBEDTLS_PK_ECKEY;
881 }
882 }
883#else
884 (void) ec_grp_id;
885#endif
886 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
888 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889
890 /*
891 * No parameters with RSA (only for EC)
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (*pk_alg == MBEDTLS_PK_RSA &&
894 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
895 params->len != 0)) {
896 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 }
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900}
901
902/*
903 * SubjectPublicKeyInfo ::= SEQUENCE {
904 * algorithm AlgorithmIdentifier,
905 * subjectPublicKey BIT STRING }
906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
908 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909{
Janos Follath24eed8d2019-11-22 13:21:35 +0000910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_asn1_buf alg_params;
913 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200914 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
918 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920 }
921
922 end = *p + len;
923
Jethro Beekman01672442023-04-19 14:08:14 +0200924 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return ret;
926 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
929 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (*p + len != end) {
933 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
934 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
938 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
942 return ret;
943 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (pk_alg == MBEDTLS_PK_RSA) {
947 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200950#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200952#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200953 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200954 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200955 } else
956#endif
957 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200958 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200959 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200961 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200964#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (ret == 0 && *p != end) {
968 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
969 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
970 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (ret != 0) {
973 mbedtls_pk_free(pk);
974 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977}
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100981 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
982 *
983 * The value zero is:
984 * - never a valid value for an RSA parameter
985 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
986 *
987 * Since values can't be omitted in PKCS#1, passing a zero value to
988 * rsa_complete() would be incorrect, so reject zero values early.
989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990static int asn1_get_nonzero_mpi(unsigned char **p,
991 const unsigned char *end,
992 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100993{
994 int ret;
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 ret = mbedtls_asn1_get_mpi(p, end, X);
997 if (ret != 0) {
998 return ret;
999 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1002 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1003 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001006}
1007
1008/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 * Parse a PKCS#1 encoded private RSA key
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1012 const unsigned char *key,
1013 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001015 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 size_t len;
1017 unsigned char *p, *end;
1018
Hanno Beckerefa14e82017-10-11 19:45:19 +01001019 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001021
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 p = (unsigned char *) key;
1023 end = p + keylen;
1024
1025 /*
1026 * This function parses the RSAPrivateKey (PKCS#1)
1027 *
1028 * RSAPrivateKey ::= SEQUENCE {
1029 * version Version,
1030 * modulus INTEGER, -- n
1031 * publicExponent INTEGER, -- e
1032 * privateExponent INTEGER, -- d
1033 * prime1 INTEGER, -- p
1034 * prime2 INTEGER, -- q
1035 * exponent1 INTEGER, -- d mod (p-1)
1036 * exponent2 INTEGER, -- d mod (q-1)
1037 * coefficient INTEGER, -- (inverse of q) mod p
1038 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1039 * }
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1042 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1043 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 }
1045
1046 end = p + len;
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1049 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 }
1051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (version != 0) {
1053 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 }
1055
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001056 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1058 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1059 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001063 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1065 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1066 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001067 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001069
1070 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1072 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1073 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001074 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001076
1077 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1079 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1080 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001081 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001083
1084 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1086 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1087 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001088 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001090
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001091#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001092 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1094 * that they can be easily recomputed from D, P and Q. However by
1095 * parsing them from the PKCS1 structure it is possible to avoid
1096 * recalculating them which both reduces the overhead of loading
1097 * RSA private keys into memory and also avoids side channels which
1098 * can arise when computing those values, since all of D, P, and Q
1099 * are secret. See https://eprint.iacr.org/2020/055 for a
1100 * description of one such attack.
1101 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001102
Jack Lloyd80cc8112020-01-22 17:34:29 -05001103 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1105 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1106 goto cleanup;
1107 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001108
1109 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1111 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1112 goto cleanup;
1113 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001114
1115 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1117 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1118 goto cleanup;
1119 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001120
Jack Lloyd60239752020-01-27 17:53:36 -05001121#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001122 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1124 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1126 goto cleanup;
1127 }
Jack Lloyd60239752020-01-27 17:53:36 -05001128#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001129
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001130 /* rsa_complete() doesn't complete anything with the default
1131 * implementation but is still called:
1132 * - for the benefit of alternative implementation that may want to
1133 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1134 * - as is also sanity-checks the key
1135 *
1136 * Furthermore, we also check the public part for consistency with
1137 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1140 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001142 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (p != end) {
1145 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1146 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 }
1148
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001149cleanup:
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001154 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((ret & 0xff80) == 0) {
1156 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1157 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001158 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167
Valerio Setti81d75122023-06-14 14:49:33 +02001168#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169/*
1170 * Parse a SEC1 encoded private EC key
1171 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001172static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 const unsigned char *key, size_t keylen,
1174 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175{
Janos Follath24eed8d2019-11-22 13:21:35 +00001176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001177 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001178 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001179 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001181 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 unsigned char *end = p + keylen;
1183 unsigned char *end2;
1184
1185 /*
1186 * RFC 5915, or SEC1 Appendix C.4
1187 *
1188 * ECPrivateKey ::= SEQUENCE {
1189 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1190 * privateKey OCTET STRING,
1191 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1192 * publicKey [1] BIT STRING OPTIONAL
1193 * }
1194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 }
1199
1200 end = p + len;
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (version != 1) {
1207 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213
Valerio Setti6b062ee2023-06-30 17:32:57 +02001214 /* Keep a reference to the position fo the private key. It will be used
1215 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001216 d = p;
1217 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001218
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 p += len;
1220
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001221 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001223 /*
1224 * Is 'parameters' present?
1225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1227 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1228 0)) == 0) {
1229 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001230 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001235 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001236 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001238 /*
1239 * Load the private key
1240 */
1241 ret = pk_ecc_set_key(pk, d, d_len);
1242 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001243 return ret;
1244 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001247 /*
1248 * Is 'publickey' present? If not, or if we can't read it (eg because it
1249 * is compressed), create it from the private key.
1250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1252 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1253 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001254 end2 = p + len;
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1258 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (p + len != end2) {
1261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1262 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1263 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264
Valerio Setti4064dbb2023-05-17 15:33:07 +02001265 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001266 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001268 /*
1269 * The only acceptable failure mode of pk_get_ecpubkey() above
1270 * is if the point format is not recognized.
1271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1273 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1274 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001278 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001280
Valerio Setti34f67552023-04-03 15:19:18 +02001281 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001282 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001283 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001284 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001285 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288}
Valerio Setti81d75122023-06-14 14:49:33 +02001289#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
1291/*
1292 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001293 *
1294 * Notes:
1295 *
1296 * - This function does not own the key buffer. It is the
1297 * responsibility of the caller to take care of zeroizing
1298 * and freeing it after use.
1299 *
1300 * - The function is responsible for freeing the provided
1301 * PK context on failure.
1302 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 */
1304static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_pk_context *pk,
1306 const unsigned char *key, size_t keylen,
1307 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308{
1309 int ret, version;
1310 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 unsigned char *p = (unsigned char *) key;
1313 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001315 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
Valerio Setti81d75122023-06-14 14:49:33 +02001318#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001319 (void) f_rng;
1320 (void) p_rng;
1321#endif
1322
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001324 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 *
1326 * PrivateKeyInfo ::= SEQUENCE {
1327 * version Version,
1328 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1329 * privateKey PrivateKey,
1330 * attributes [0] IMPLICIT Attributes OPTIONAL }
1331 *
1332 * Version ::= INTEGER
1333 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1334 * PrivateKey ::= OCTET STRING
1335 *
1336 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1337 */
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1340 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 }
1343
1344 end = p + len;
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (version != 0) {
1351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Jethro Beekman01672442023-04-19 14:08:14 +02001354 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return ret;
1356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1360 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (len < 1) {
1363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1364 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1365 }
1366
1367 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1368 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1369 }
1370
1371 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1372 return ret;
1373 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (pk_alg == MBEDTLS_PK_RSA) {
1377 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1378 mbedtls_pk_free(pk);
1379 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 }
1381 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001383#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001385#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001386 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001387 if ((ret =
1388 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001389 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001390 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001391 p_rng)) != 0) {
1392 mbedtls_pk_free(pk);
1393 return ret;
1394 }
1395 } else
1396#endif
1397 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001398 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1399 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001400 mbedtls_pk_free(pk);
1401 return ret;
1402 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 }
1404 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001405#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001408 end = p + len;
1409 if (end != (key + keylen)) {
1410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1411 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1412 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415}
1416
1417/*
1418 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001419 *
1420 * To save space, the decryption happens in-place on the given key buffer.
1421 * Also, while this function may modify the keybuffer, it doesn't own it,
1422 * and instead it is the responsibility of the caller to zeroize and properly
1423 * free it after use.
1424 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001427MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_pk_context *pk,
1429 unsigned char *key, size_t keylen,
1430 const unsigned char *pwd, size_t pwdlen,
1431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001433 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001435 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1438#if defined(MBEDTLS_PKCS12_C)
1439 mbedtls_cipher_type_t cipher_alg;
1440 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001442 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Hanno Becker2aa80a72017-09-07 15:28:45 +01001444 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 end = p + keylen;
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (pwdlen == 0) {
1448 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1449 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
1451 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001452 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 *
1454 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1455 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1456 * encryptedData EncryptedData
1457 * }
1458 *
1459 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1460 *
1461 * EncryptedData ::= OCTET STRING
1462 *
1463 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001464 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1467 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1468 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 }
1470
1471 end = p + len;
1472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1474 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1478 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1479 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480
Hanno Beckerfab35692017-08-25 13:38:26 +01001481 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482
1483 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001484 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001488 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001489 cipher_alg, md_alg,
1490 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1492 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1493 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001497
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001498 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_PKCS12_C */
1501#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001503 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1504 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1506 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1507 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001511
1512 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001515 {
1516 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001517 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (decrypted == 0) {
1520 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1521 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001522 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
1526/*
1527 * Parse a private key
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1530 const unsigned char *key, size_t keylen,
1531 const unsigned char *pwd, size_t pwdlen,
1532 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533{
Janos Follath24eed8d2019-11-22 13:21:35 +00001534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001539#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (keylen == 0) {
1542 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1543 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001544
1545#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001549 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001551 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 } else {
1553 ret = mbedtls_pem_read_buffer(&pem,
1554 "-----BEGIN RSA PRIVATE KEY-----",
1555 "-----END RSA PRIVATE KEY-----",
1556 key, pwd, pwdlen, &len);
1557 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ret == 0) {
1560 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1561 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1562 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1563 pem.buf, pem.buflen)) != 0) {
1564 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565 }
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 mbedtls_pem_free(&pem);
1568 return ret;
1569 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1570 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1571 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1572 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1573 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1574 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577
Valerio Setti81d75122023-06-14 14:49:33 +02001578#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001579 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001581 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 } else {
1583 ret = mbedtls_pem_read_buffer(&pem,
1584 "-----BEGIN EC PRIVATE KEY-----",
1585 "-----END EC PRIVATE KEY-----",
1586 key, pwd, pwdlen, &len);
1587 }
1588 if (ret == 0) {
1589 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001592 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 pem.buf, pem.buflen,
1594 f_rng, p_rng)) != 0) {
1595 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_pem_free(&pem);
1599 return ret;
1600 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1601 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1602 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1603 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1604 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1605 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606 }
Valerio Setti81d75122023-06-14 14:49:33 +02001607#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001609 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001611 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 } else {
1613 ret = mbedtls_pem_read_buffer(&pem,
1614 "-----BEGIN PRIVATE KEY-----",
1615 "-----END PRIVATE KEY-----",
1616 key, NULL, 0, &len);
1617 }
1618 if (ret == 0) {
1619 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1620 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1621 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 mbedtls_pem_free(&pem);
1625 return ret;
1626 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1627 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001628 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001631 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001633 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 } else {
1635 ret = mbedtls_pem_read_buffer(&pem,
1636 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1637 "-----END ENCRYPTED PRIVATE KEY-----",
1638 key, NULL, 0, &len);
1639 }
1640 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001641 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1642 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644 }
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 mbedtls_pem_free(&pem);
1647 return ret;
1648 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1649 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652#else
1653 ((void) pwd);
1654 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656
1657 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001658 * At this point we only know it's not a PEM formatted key. Could be any
1659 * of the known DER encoded private key formats
1660 *
1661 * We try the different DER format parsers to see if one passes without
1662 * error
1663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001666 unsigned char *key_copy;
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1669 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1670 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001673
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001674 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1675 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001676
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001677 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001678 }
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if (ret == 0) {
1681 return 0;
1682 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 mbedtls_pk_free(pk);
1685 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1688 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1693 if (ret == 0) {
1694 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001695 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 mbedtls_pk_free(pk);
1698 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1703 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1704 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1705 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001706 }
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 mbedtls_pk_free(pk);
1709 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711
Valerio Setti81d75122023-06-14 14:49:33 +02001712#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1714 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001715 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 key, keylen, f_rng, p_rng) == 0) {
1717 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001720#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001721
Valerio Setti81d75122023-06-14 14:49:33 +02001722 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001723 * it is ok to leave the PK context initialized but not
1724 * freed: It is the caller's responsibility to call pk_init()
1725 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001726 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001727 * isn't, this leads to mbedtls_pk_free() being called
1728 * twice, once here and once by the caller, but this is
1729 * also ok and in line with the mbedtls_pk_free() calls
1730 * on failed PEM parsing attempts. */
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733}
1734
1735/*
1736 * Parse a public key
1737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1739 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740{
Janos Follath24eed8d2019-11-22 13:21:35 +00001741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001742 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001743#if defined(MBEDTLS_RSA_C)
1744 const mbedtls_pk_info_t *pk_info;
1745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001747 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001749#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (keylen == 0) {
1752 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1753 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001754
1755#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001757#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001758 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001760 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 } else {
1762 ret = mbedtls_pem_read_buffer(&pem,
1763 "-----BEGIN RSA PUBLIC KEY-----",
1764 "-----END RSA PUBLIC KEY-----",
1765 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001767
1768 if (ret == 0) {
1769 p = pem.buf;
1770 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1771 mbedtls_pem_free(&pem);
1772 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1773 }
1774
1775 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1776 mbedtls_pem_free(&pem);
1777 return ret;
1778 }
1779
1780 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1781 mbedtls_pk_free(ctx);
1782 }
1783
1784 mbedtls_pem_free(&pem);
1785 return ret;
1786 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1787 mbedtls_pem_free(&pem);
1788 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001789 }
1790#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001791
1792 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001794 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 } else {
1796 ret = mbedtls_pem_read_buffer(&pem,
1797 "-----BEGIN PUBLIC KEY-----",
1798 "-----END PUBLIC KEY-----",
1799 key, NULL, 0, &len);
1800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001803 /*
1804 * Was PEM encoded
1805 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001806 p = pem.buf;
1807
Jethro Beekman01672442023-04-19 14:08:14 +02001808 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_pem_free(&pem);
1810 return ret;
1811 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1812 mbedtls_pem_free(&pem);
1813 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001817
1818#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1820 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001822
1823 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1824 return ret;
1825 }
1826
1827 p = (unsigned char *) key;
1828 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1829 if (ret == 0) {
1830 return ret;
1831 }
1832 mbedtls_pk_free(ctx);
1833 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1834 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1835 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001836 }
1837#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001838 p = (unsigned char *) key;
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001843}
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#endif /* MBEDTLS_PK_PARSE_C */