blob: 3fa1a8422762c2cd3d363f7550773f6cbad74f55 [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;
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200507 /* Montgomery allows only ECDH, others ECDSA too */
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200508 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
509 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200510 psa_set_key_enrollment_algorithm(&attributes,
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200511 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200512 }
513 psa_set_key_usage_flags(&attributes, flags);
514
515 status = psa_import_key(&attributes, key, len, &pk->priv_id);
516 return psa_pk_status_to_mbedtls(status);
517
518#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
519
520 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
521 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
522 if (ret != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
524 }
525 return 0;
526#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
527}
528
529/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100531 *
532 * ECParameters ::= CHOICE {
533 * namedCurve OBJECT IDENTIFIER
534 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
535 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200537static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538{
Janos Follath24eed8d2019-11-22 13:21:35 +0000539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (params->tag == MBEDTLS_ASN1_OID) {
543 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
544 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
545 }
546 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
549 return ret;
550 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100551#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100553#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100554 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200556 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557}
558
Jethro Beekman01672442023-04-19 14:08:14 +0200559/*
560 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200561 *
562 * Note: the private key information is always available from pk,
563 * however for convenience the serialized version is also passed,
564 * as it's available at each calling site, and useful in some configs
565 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200566 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200567static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200568 const unsigned char *d, size_t d_len,
569 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
570{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200571#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200572 psa_status_t status;
573 (void) f_rng;
574 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200575 (void) d;
576 (void) d_len;
577
578 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
579 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200580 return psa_pk_status_to_mbedtls(status);
581#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
582 int ret;
583 psa_status_t status;
584 (void) f_rng;
585 (void) p_rng;
586
Valerio Setti00e8dd12023-05-18 18:56:59 +0200587 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
588 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
589 size_t key_len;
590 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200591 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
592 size_t curve_bits;
593 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200594 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200595
596 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
597 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
598
599 status = psa_import_key(&key_attr, d, d_len, &key_id);
600 ret = psa_pk_status_to_mbedtls(status);
601 if (ret != 0) {
602 return ret;
603 }
604
Jethro Beekman01672442023-04-19 14:08:14 +0200605 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
606 ret = psa_pk_status_to_mbedtls(status);
607 destruction_status = psa_destroy_key(key_id);
608 if (ret != 0) {
609 return ret;
610 } else if (destruction_status != PSA_SUCCESS) {
611 return psa_pk_status_to_mbedtls(destruction_status);
612 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200613 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200614#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200615 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200616 (void) d;
617 (void) d_len;
618
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200619 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200621}
622
623#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
624
625/*
626 * Load an RFC8410 EC key, which doesn't have any parameters
627 */
628static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
629 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200630 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200631{
632 if (params->tag != 0 || params->len != 0) {
633 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
634 }
635
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200636 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200637}
638
639/*
640 * Parse an RFC 8410 encoded private EC key
641 *
642 * CurvePrivateKey ::= OCTET STRING
643 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200644static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200645 unsigned char *key, size_t keylen, const unsigned char *end,
646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
647{
648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
649 size_t len;
650
651 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
652 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
653 }
654
655 if (key + len != end) {
656 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
657 }
658
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659 /*
660 * Load the private key
661 */
662 ret = pk_ecc_set_key(pk, key, len);
663 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200664 return ret;
665 }
Jethro Beekman01672442023-04-19 14:08:14 +0200666
Valerio Setti4064dbb2023-05-17 15:33:07 +0200667 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
668 * which never contain a public key. As such, derive the public key
669 * unconditionally. */
670 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200671 return ret;
672 }
673
Jethro Beekman01672442023-04-19 14:08:14 +0200674 return 0;
675}
676#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200677
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200678#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200679/*
680 * Create a temporary ecp_keypair for converting an EC point in compressed
681 * format to an uncompressed one
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200682 *
683 * Consumes everything or fails - inherited from
684 * mbedtls_ecp_point_read_binary().
Valerio Setti4064dbb2023-05-17 15:33:07 +0200685 */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200686static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
687 const unsigned char *in_start, size_t in_len,
688 unsigned char *out_buf, size_t out_buf_size,
689 size_t *out_buf_len)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200690{
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200691#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200692 mbedtls_ecp_keypair ecp_key;
693 mbedtls_ecp_group_id ecp_group_id;
694 int ret;
695
696 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
697
698 mbedtls_ecp_keypair_init(&ecp_key);
699 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
700 if (ret != 0) {
701 return ret;
702 }
703 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
704 in_start, in_len);
705 if (ret != 0) {
706 goto exit;
707 }
708 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
709 MBEDTLS_ECP_PF_UNCOMPRESSED,
710 out_buf_len, out_buf, out_buf_size);
711
712exit:
713 mbedtls_ecp_keypair_free(&ecp_key);
714 return ret;
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200715#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
716 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
717#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200718}
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200719#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200720
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721/*
722 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100723 *
724 * The caller is responsible for clearing the structure upon failure if
725 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200729 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730{
Janos Follath24eed8d2019-11-22 13:21:35 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732
Valerio Setti4064dbb2023-05-17 15:33:07 +0200733#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
734 mbedtls_svc_key_id_t key;
735 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200736 size_t len = (size_t) (end - *p);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200737
738 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
739 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740 }
741
Valerio Setti4064dbb2023-05-17 15:33:07 +0200742 if ((**p == 0x02) || (**p == 0x03)) {
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200743 /* Compressed format, not supported by PSA Crypto.
744 * Try converting using functions from ECP_LIGHT. */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200745 ret = pk_ecc_read_compressed(pk, *p, len,
746 pk->pub_raw,
747 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
748 &pk->pub_raw_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200749 if (ret != 0) {
750 return ret;
751 }
752 } else {
753 /* Uncompressed format */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200754 if (len > 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 }
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200757 memcpy(pk->pub_raw, *p, len);
758 pk->pub_raw_len = len;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200759 }
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é-Gonnarddf151bb2023-07-26 11:06:46 +0200785 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
786 * consumed all bytes or failed, and memcpy consumed all bytes too.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787 */
788 *p = (unsigned char *) end;
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791}
Valerio Setti81d75122023-06-14 14:49:33 +0200792#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795/*
796 * RSAPublicKey ::= SEQUENCE {
797 * modulus INTEGER, -- n
798 * publicExponent INTEGER -- e
799 * }
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801static int pk_get_rsapubkey(unsigned char **p,
802 const unsigned char *end,
803 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804{
Janos Follath24eed8d2019-11-22 13:21:35 +0000805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806 size_t len;
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
809 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (*p + len != end) {
814 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
816 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
821 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
824 NULL, 0, NULL, 0)) != 0) {
825 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
826 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827
828 *p += len;
829
830 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
836 NULL, 0, *p, len)) != 0) {
837 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
838 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839
840 *p += len;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (mbedtls_rsa_complete(rsa) != 0 ||
843 mbedtls_rsa_check_pubkey(rsa) != 0) {
844 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000845 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (*p != end) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
849 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
850 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
856/* Get a PK algorithm identifier
857 *
858 * AlgorithmIdentifier ::= SEQUENCE {
859 * algorithm OBJECT IDENTIFIER,
860 * parameters ANY DEFINED BY algorithm OPTIONAL }
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int pk_get_pk_alg(unsigned char **p,
863 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200864 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
865 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
873 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
874 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
Jethro Beekman01672442023-04-19 14:08:14 +0200876 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200877#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200878 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
879 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
880 if (ret == 0) {
881 *pk_alg = MBEDTLS_PK_ECKEY;
882 }
883 }
884#else
885 (void) ec_grp_id;
886#endif
887 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
889 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
891 /*
892 * No parameters with RSA (only for EC)
893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (*pk_alg == MBEDTLS_PK_RSA &&
895 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
896 params->len != 0)) {
897 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901}
902
903/*
904 * SubjectPublicKeyInfo ::= SEQUENCE {
905 * algorithm AlgorithmIdentifier,
906 * subjectPublicKey BIT STRING }
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
909 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910{
Janos Follath24eed8d2019-11-22 13:21:35 +0000911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_asn1_buf alg_params;
914 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200915 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
919 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
920 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921 }
922
923 end = *p + len;
924
Jethro Beekman01672442023-04-19 14:08:14 +0200925 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 return ret;
927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (*p + len != end) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
939 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
943 return ret;
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (pk_alg == MBEDTLS_PK_RSA) {
948 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200951#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200953#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200954 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200955 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200956 } else
957#endif
958 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200959 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200962 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200965#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret == 0 && *p != end) {
969 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
970 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
971 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (ret != 0) {
974 mbedtls_pk_free(pk);
975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978}
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
983 *
984 * The value zero is:
985 * - never a valid value for an RSA parameter
986 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
987 *
988 * Since values can't be omitted in PKCS#1, passing a zero value to
989 * rsa_complete() would be incorrect, so reject zero values early.
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991static int asn1_get_nonzero_mpi(unsigned char **p,
992 const unsigned char *end,
993 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100994{
995 int ret;
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 ret = mbedtls_asn1_get_mpi(p, end, X);
998 if (ret != 0) {
999 return ret;
1000 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1003 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1004 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001007}
1008
1009/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010 * Parse a PKCS#1 encoded private RSA key
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1013 const unsigned char *key,
1014 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001016 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 size_t len;
1018 unsigned char *p, *end;
1019
Hanno Beckerefa14e82017-10-11 19:45:19 +01001020 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001022
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 p = (unsigned char *) key;
1024 end = p + keylen;
1025
1026 /*
1027 * This function parses the RSAPrivateKey (PKCS#1)
1028 *
1029 * RSAPrivateKey ::= SEQUENCE {
1030 * version Version,
1031 * modulus INTEGER, -- n
1032 * publicExponent INTEGER, -- e
1033 * privateExponent INTEGER, -- d
1034 * prime1 INTEGER, -- p
1035 * prime2 INTEGER, -- q
1036 * exponent1 INTEGER, -- d mod (p-1)
1037 * exponent2 INTEGER, -- d mod (q-1)
1038 * coefficient INTEGER, -- (inverse of q) mod p
1039 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1040 * }
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1043 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1044 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045 }
1046
1047 end = p + len;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1050 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (version != 0) {
1054 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 }
1056
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1059 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1060 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1066 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1067 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070
1071 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1073 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1074 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077
1078 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1080 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1081 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001082 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001084
1085 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1088 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001089 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001092#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001093 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1095 * that they can be easily recomputed from D, P and Q. However by
1096 * parsing them from the PKCS1 structure it is possible to avoid
1097 * recalculating them which both reduces the overhead of loading
1098 * RSA private keys into memory and also avoids side channels which
1099 * can arise when computing those values, since all of D, P, and Q
1100 * are secret. See https://eprint.iacr.org/2020/055 for a
1101 * description of one such attack.
1102 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001103
Jack Lloyd80cc8112020-01-22 17:34:29 -05001104 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001109
1110 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1112 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1113 goto cleanup;
1114 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001115
1116 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1118 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1119 goto cleanup;
1120 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001121
Jack Lloyd60239752020-01-27 17:53:36 -05001122#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001123 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1126 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1127 goto cleanup;
1128 }
Jack Lloyd60239752020-01-27 17:53:36 -05001129#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001130
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001131 /* rsa_complete() doesn't complete anything with the default
1132 * implementation but is still called:
1133 * - for the benefit of alternative implementation that may want to
1134 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1135 * - as is also sanity-checks the key
1136 *
1137 * Furthermore, we also check the public part for consistency with
1138 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1141 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001142 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001143 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (p != end) {
1146 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1147 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 }
1149
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001150cleanup:
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001155 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if ((ret & 0xff80) == 0) {
1157 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1158 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001159 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
Valerio Setti81d75122023-06-14 14:49:33 +02001169#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170/*
1171 * Parse a SEC1 encoded private EC key
1172 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001173static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 const unsigned char *key, size_t keylen,
1175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176{
Janos Follath24eed8d2019-11-22 13:21:35 +00001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001178 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001179 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001180 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001182 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 unsigned char *end = p + keylen;
1184 unsigned char *end2;
1185
1186 /*
1187 * RFC 5915, or SEC1 Appendix C.4
1188 *
1189 * ECPrivateKey ::= SEQUENCE {
1190 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1191 * privateKey OCTET STRING,
1192 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1193 * publicKey [1] BIT STRING OPTIONAL
1194 * }
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1197 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 }
1200
1201 end = p + len;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (version != 1) {
1208 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1213 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214
Valerio Setti6b062ee2023-06-30 17:32:57 +02001215 /* Keep a reference to the position fo the private key. It will be used
1216 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001217 d = p;
1218 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001219
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 p += len;
1221
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001222 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001224 /*
1225 * Is 'parameters' present?
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1228 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1229 0)) == 0) {
1230 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001231 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001236 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001237 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001238
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001239 /*
1240 * Load the private key
1241 */
1242 ret = pk_ecc_set_key(pk, d, d_len);
1243 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001244 return ret;
1245 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001248 /*
1249 * Is 'publickey' present? If not, or if we can't read it (eg because it
1250 * is compressed), create it from the private key.
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1253 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1254 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001255 end2 = p + len;
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1258 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1259 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (p + len != end2) {
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1263 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1264 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001265
Valerio Setti4064dbb2023-05-17 15:33:07 +02001266 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001267 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001269 /*
1270 * The only acceptable failure mode of pk_get_ecpubkey() above
1271 * is if the point format is not recognized.
1272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1274 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1275 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001276 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001279 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001281
Valerio Setti34f67552023-04-03 15:19:18 +02001282 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001283 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001284 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001285 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001286 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289}
Valerio Setti81d75122023-06-14 14:49:33 +02001290#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
1292/*
1293 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001294 *
1295 * Notes:
1296 *
1297 * - This function does not own the key buffer. It is the
1298 * responsibility of the caller to take care of zeroizing
1299 * and freeing it after use.
1300 *
1301 * - The function is responsible for freeing the provided
1302 * PK context on failure.
1303 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 */
1305static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_pk_context *pk,
1307 const unsigned char *key, size_t keylen,
1308 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309{
1310 int ret, version;
1311 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 unsigned char *p = (unsigned char *) key;
1314 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001316 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318
Valerio Setti81d75122023-06-14 14:49:33 +02001319#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001320 (void) f_rng;
1321 (void) p_rng;
1322#endif
1323
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001325 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 *
1327 * PrivateKeyInfo ::= SEQUENCE {
1328 * version Version,
1329 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1330 * privateKey PrivateKey,
1331 * attributes [0] IMPLICIT Attributes OPTIONAL }
1332 *
1333 * Version ::= INTEGER
1334 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1335 * PrivateKey ::= OCTET STRING
1336 *
1337 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1338 */
1339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1341 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1342 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 }
1344
1345 end = p + len;
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001349 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (version != 0) {
1352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1353 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354
Jethro Beekman01672442023-04-19 14:08:14 +02001355 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return ret;
1357 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1360 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1361 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (len < 1) {
1364 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1365 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1366 }
1367
1368 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1369 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1370 }
1371
1372 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1373 return ret;
1374 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (pk_alg == MBEDTLS_PK_RSA) {
1378 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1379 mbedtls_pk_free(pk);
1380 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 }
1382 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001384#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001386#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001387 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001388 if ((ret =
1389 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001390 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001391 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001392 p_rng)) != 0) {
1393 mbedtls_pk_free(pk);
1394 return ret;
1395 }
1396 } else
1397#endif
1398 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001399 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1400 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001401 mbedtls_pk_free(pk);
1402 return ret;
1403 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 }
1405 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001406#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001409 end = p + len;
1410 if (end != (key + keylen)) {
1411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1412 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1413 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416}
1417
1418/*
1419 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001420 *
1421 * To save space, the decryption happens in-place on the given key buffer.
1422 * Also, while this function may modify the keybuffer, it doesn't own it,
1423 * and instead it is the responsibility of the caller to zeroize and properly
1424 * free it after use.
1425 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001428MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 mbedtls_pk_context *pk,
1430 unsigned char *key, size_t keylen,
1431 const unsigned char *pwd, size_t pwdlen,
1432 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001434 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001436 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1439#if defined(MBEDTLS_PKCS12_C)
1440 mbedtls_cipher_type_t cipher_alg;
1441 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001443 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444
Hanno Becker2aa80a72017-09-07 15:28:45 +01001445 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 end = p + keylen;
1447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (pwdlen == 0) {
1449 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1450 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451
1452 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001453 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 *
1455 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1456 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1457 * encryptedData EncryptedData
1458 * }
1459 *
1460 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1461 *
1462 * EncryptedData ::= OCTET STRING
1463 *
1464 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001465 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1468 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1469 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 }
1471
1472 end = p + len;
1473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1475 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1476 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1479 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1480 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481
Hanno Beckerfab35692017-08-25 13:38:26 +01001482 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483
1484 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001485 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001489 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001490 cipher_alg, md_alg,
1491 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1493 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1494 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001498
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001499 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_PKCS12_C */
1502#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001504 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1505 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1507 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1508 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001511 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001512
1513 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001516 {
1517 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001518 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (decrypted == 0) {
1521 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1522 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001523 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526
1527/*
1528 * Parse a private key
1529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1531 const unsigned char *key, size_t keylen,
1532 const unsigned char *pwd, size_t pwdlen,
1533 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534{
Janos Follath24eed8d2019-11-22 13:21:35 +00001535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001540#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (keylen == 0) {
1543 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1544 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001545
1546#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001550 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001552 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 } else {
1554 ret = mbedtls_pem_read_buffer(&pem,
1555 "-----BEGIN RSA PRIVATE KEY-----",
1556 "-----END RSA PRIVATE KEY-----",
1557 key, pwd, pwdlen, &len);
1558 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (ret == 0) {
1561 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1562 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1563 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1564 pem.buf, pem.buflen)) != 0) {
1565 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566 }
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 mbedtls_pem_free(&pem);
1569 return ret;
1570 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1571 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1572 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1573 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1574 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1575 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578
Valerio Setti81d75122023-06-14 14:49:33 +02001579#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001580 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001582 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 } else {
1584 ret = mbedtls_pem_read_buffer(&pem,
1585 "-----BEGIN EC PRIVATE KEY-----",
1586 "-----END EC PRIVATE KEY-----",
1587 key, pwd, pwdlen, &len);
1588 }
1589 if (ret == 0) {
1590 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001593 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 pem.buf, pem.buflen,
1595 f_rng, p_rng)) != 0) {
1596 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597 }
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_pem_free(&pem);
1600 return ret;
1601 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1602 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1603 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1604 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1605 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1606 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607 }
Valerio Setti81d75122023-06-14 14:49:33 +02001608#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001609
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001610 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001612 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 } else {
1614 ret = mbedtls_pem_read_buffer(&pem,
1615 "-----BEGIN PRIVATE KEY-----",
1616 "-----END PRIVATE KEY-----",
1617 key, NULL, 0, &len);
1618 }
1619 if (ret == 0) {
1620 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1621 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1622 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001623 }
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 mbedtls_pem_free(&pem);
1626 return ret;
1627 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1628 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001632 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001634 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else {
1636 ret = mbedtls_pem_read_buffer(&pem,
1637 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1638 "-----END ENCRYPTED PRIVATE KEY-----",
1639 key, NULL, 0, &len);
1640 }
1641 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001642 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1643 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645 }
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 mbedtls_pem_free(&pem);
1648 return ret;
1649 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1650 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653#else
1654 ((void) pwd);
1655 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657
1658 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001659 * At this point we only know it's not a PEM formatted key. Could be any
1660 * of the known DER encoded private key formats
1661 *
1662 * We try the different DER format parsers to see if one passes without
1663 * error
1664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001667 unsigned char *key_copy;
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1670 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1671 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001674
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001675 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1676 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001677
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001678 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679 }
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (ret == 0) {
1682 return 0;
1683 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 mbedtls_pk_free(pk);
1686 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1689 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1694 if (ret == 0) {
1695 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001696 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 mbedtls_pk_free(pk);
1699 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1704 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1705 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1706 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001707 }
1708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 mbedtls_pk_free(pk);
1710 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001712
Valerio Setti81d75122023-06-14 14:49:33 +02001713#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1715 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001716 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 key, keylen, f_rng, p_rng) == 0) {
1718 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001719 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001721#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001722
Valerio Setti81d75122023-06-14 14:49:33 +02001723 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001724 * it is ok to leave the PK context initialized but not
1725 * freed: It is the caller's responsibility to call pk_init()
1726 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001727 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001728 * isn't, this leads to mbedtls_pk_free() being called
1729 * twice, once here and once by the caller, but this is
1730 * also ok and in line with the mbedtls_pk_free() calls
1731 * on failed PEM parsing attempts. */
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001734}
1735
1736/*
1737 * Parse a public key
1738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1740 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001741{
Janos Follath24eed8d2019-11-22 13:21:35 +00001742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001743 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001744#if defined(MBEDTLS_RSA_C)
1745 const mbedtls_pk_info_t *pk_info;
1746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001748 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001750#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (keylen == 0) {
1753 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1754 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001755
1756#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001758#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001759 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001761 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 } else {
1763 ret = mbedtls_pem_read_buffer(&pem,
1764 "-----BEGIN RSA PUBLIC KEY-----",
1765 "-----END RSA PUBLIC KEY-----",
1766 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001768
1769 if (ret == 0) {
1770 p = pem.buf;
1771 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1772 mbedtls_pem_free(&pem);
1773 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1774 }
1775
1776 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1777 mbedtls_pem_free(&pem);
1778 return ret;
1779 }
1780
1781 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1782 mbedtls_pk_free(ctx);
1783 }
1784
1785 mbedtls_pem_free(&pem);
1786 return ret;
1787 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1788 mbedtls_pem_free(&pem);
1789 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001790 }
1791#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001792
1793 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001795 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 } else {
1797 ret = mbedtls_pem_read_buffer(&pem,
1798 "-----BEGIN PUBLIC KEY-----",
1799 "-----END PUBLIC KEY-----",
1800 key, NULL, 0, &len);
1801 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001804 /*
1805 * Was PEM encoded
1806 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001807 p = pem.buf;
1808
Jethro Beekman01672442023-04-19 14:08:14 +02001809 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_pem_free(&pem);
1811 return ret;
1812 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1813 mbedtls_pem_free(&pem);
1814 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001815 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001818
1819#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1821 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001823
1824 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1825 return ret;
1826 }
1827
1828 p = (unsigned char *) key;
1829 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1830 if (ret == 0) {
1831 return ret;
1832 }
1833 mbedtls_pk_free(ctx);
1834 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1835 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1836 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001837 }
1838#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001839 p = (unsigned char *) key;
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001844}
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#endif /* MBEDTLS_PK_PARSE_C */