blob: 1dd5653e2414e3c277dfeadb2f432cb1434a96ac [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020032/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035#endif
Valerio Setti81d75122023-06-14 14:49:33 +020036#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020037#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020038#include "pk_internal.h"
39#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020040
41/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
51
Valerio Setti34f67552023-04-03 15:19:18 +020052#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020053#include "psa_util_internal.h"
Valerio Setti34f67552023-04-03 15:19:18 +020054#endif
55
56#if defined(MBEDTLS_USE_PSA_CRYPTO)
57#include "psa/crypto.h"
58#endif
59
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061
Valerio Settie0e63112023-05-18 18:48:07 +020062/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020063#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020064#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
65 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020066#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020067
Gilles Peskine832f3492017-11-30 11:42:12 +010068#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020069/*
70 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020071 *
72 * The file is expected to contain either PEM or DER encoded data.
73 * A terminating null byte is always appended. It is included in the announced
74 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020075 */
Gilles Peskine449bd832023-01-11 14:50:10 +010076int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020077{
78 FILE *f;
79 long size;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if ((f = fopen(path, "rb")) == NULL) {
82 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
83 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020084
Gilles Peskineda0913b2022-06-30 17:03:40 +020085 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010086 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 fseek(f, 0, SEEK_END);
89 if ((size = ftell(f)) == -1) {
90 fclose(f);
91 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020092 }
Gilles Peskine449bd832023-01-11 14:50:10 +010093 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020094
95 *n = (size_t) size;
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (*n + 1 == 0 ||
98 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
99 fclose(f);
100 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101 }
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (fread(*buf, 1, *n, f) != *n) {
104 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100105
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100106 mbedtls_zeroize_and_free(*buf, *n);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109 }
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112
113 (*buf)[*n] = '\0';
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200116 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120}
121
122/*
123 * Load and parse a private key
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
126 const char *path, const char *pwd,
127 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128{
Janos Follath24eed8d2019-11-22 13:21:35 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130 size_t n;
131 unsigned char *buf;
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
134 return ret;
135 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (pwd == NULL) {
138 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
139 } else {
140 ret = mbedtls_pk_parse_key(ctx, buf, n,
141 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
142 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100144 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147}
148
149/*
150 * Load and parse a public key
151 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153{
Janos Follath24eed8d2019-11-22 13:21:35 +0000154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155 size_t n;
156 unsigned char *buf;
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
159 return ret;
160 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100164 mbedtls_zeroize_and_free(buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Valerio Setti81d75122023-06-14 14:49:33 +0200170#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 *
173 * ECParameters ::= CHOICE {
174 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100175 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 * }
178 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
180 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181{
Janos Follath24eed8d2019-11-22 13:21:35 +0000182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (end - *p < 1) {
185 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
186 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
187 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100188
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100189 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100194#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 ) {
196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
197 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
201 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100202 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200203
204 params->p = *p;
205 *p += params->len;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (*p != end) {
208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
209 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
210 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213}
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100217 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
218 * WARNING: the resulting group should only be used with
219 * pk_group_id_from_specified(), since its base point may not be set correctly
220 * if it was encoded compressed.
221 *
222 * SpecifiedECDomain ::= SEQUENCE {
223 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
224 * fieldID FieldID {{FieldTypes}},
225 * curve Curve,
226 * base ECPoint,
227 * order INTEGER,
228 * cofactor INTEGER OPTIONAL,
229 * hash HashAlgorithm OPTIONAL,
230 * ...
231 * }
232 *
233 * We only support prime-field as field type, and ignore hash and cofactor.
234 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100235static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100238 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200239 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100240 const unsigned char *end_field, *end_curve;
241 size_t len;
242 int ver;
243
244 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
246 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ver < 1 || ver > 3) {
250 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
251 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252
253 /*
254 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
255 * fieldType FIELD-ID.&id({IOSet}),
256 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
257 * }
258 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
260 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
261 return ret;
262 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100263
264 end_field = p + len;
265
266 /*
267 * FIELD-ID ::= TYPE-IDENTIFIER
268 * FieldTypes FIELD-ID ::= {
269 * { Prime-p IDENTIFIED BY prime-field } |
270 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
271 * }
272 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
275 return ret;
276 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
279 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
280 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281 }
282
283 p += len;
284
285 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
288 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (p != end_field) {
293 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
294 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
295 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
297 /*
298 * Curve ::= SEQUENCE {
299 * a FieldElement,
300 * b FieldElement,
301 * seed BIT STRING OPTIONAL
302 * -- Shall be present if used in SpecifiedECDomain
303 * -- with version equal to ecdpVer2 or ecdpVer3
304 * }
305 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
307 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
308 return ret;
309 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100310
311 end_curve = p + len;
312
313 /*
314 * FieldElement ::= OCTET STRING
315 * containing an integer in the case of a prime field
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
318 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100320 }
321
322 p += len;
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
325 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 }
328
329 p += len;
330
331 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100333 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (p != end_curve) {
337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
339 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340
341 /*
342 * ECPoint ::= OCTET STRING
343 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
345 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
346 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
349 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100350 /*
351 * If we can't read the point because it's compressed, cheat by
352 * reading only the X coordinate and the parity bit of Y.
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
355 (p[0] != 0x02 && p[0] != 0x03) ||
356 len != mbedtls_mpi_size(&grp->P) + 1 ||
357 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
358 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
359 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
360 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100361 }
362 }
363
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364 p += len;
365
366 /*
367 * order INTEGER
368 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
371 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100374
375 /*
376 * Allow optional elements by purposefully not enforcing p == end here.
377 */
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100380}
381
382/*
383 * Find the group id associated with an (almost filled) group as generated by
384 * pk_group_from_specified(), or return an error if unknown.
385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386static 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 +0100387{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100388 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ecp_group ref;
390 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 mbedtls_ecp_group_free(&ref);
397 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
399 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
401 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
403 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
404 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
405 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
406 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 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 +0100409 break;
410 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 }
412
413cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100415
416 *grp_id = *id;
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423}
424
425/*
426 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
429 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430{
Janos Follath24eed8d2019-11-22 13:21:35 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000443 /* The API respecting lifecycle for mbedtls_ecp_group struct is
444 * _init(), _load() and _free(). In pk_group_id_from_specified() the
445 * temporary grp breaks that flow and it's members are populated
446 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
447 * which is assuming a group populated by _setup() may not clean-up
448 * properly -> Manually free it's members.
449 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000450 mbedtls_mpi_free(&grp.N);
451 mbedtls_mpi_free(&grp.P);
452 mbedtls_mpi_free(&grp.A);
453 mbedtls_mpi_free(&grp.B);
454 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459
Valerio Setti4064dbb2023-05-17 15:33:07 +0200460#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
461/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
462 * ecp_keypair structure with proper group ID. The purpose of this helper
463 * function is to update ec_family and ec_bits accordingly. */
464static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
465 mbedtls_ecp_group_id grp_id)
466{
467 psa_ecc_family_t ec_family;
468 size_t bits;
469
470 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
471
472 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
473 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
474 }
475
476 pk->ec_family = ec_family;
477 pk->ec_bits = bits;
478
479 return 0;
480}
481#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
482
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100485 *
486 * ECParameters ::= CHOICE {
487 * namedCurve OBJECT IDENTIFIER
488 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
489 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200491static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492{
Janos Follath24eed8d2019-11-22 13:21:35 +0000493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (params->tag == MBEDTLS_ASN1_OID) {
497 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
498 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
499 }
500 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
503 return ret;
504 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100505#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100507#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100508 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509
Valerio Setti00e8dd12023-05-18 18:56:59 +0200510#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
511 ret = pk_update_psa_ecparams(pk, grp_id);
512#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200513 /* grp may already be initialized; if so, make sure IDs match */
514 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
515 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
517 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518
Valerio Setti4064dbb2023-05-17 15:33:07 +0200519 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
520 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return ret;
522 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200523#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200524
Valerio Setti4064dbb2023-05-17 15:33:07 +0200525 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526}
527
Jethro Beekman01672442023-04-19 14:08:14 +0200528/*
529 * Helper function for deriving a public key from its private counterpart.
530 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200531static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200532 const unsigned char *d, size_t d_len,
533 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
534{
535 int ret;
536#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200537 psa_status_t status;
538 (void) f_rng;
539 (void) p_rng;
540#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
541 (void) d;
542 (void) d_len;
543
544 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
545 &pk->pub_raw_len);
546 ret = psa_pk_status_to_mbedtls(status);
547#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
548 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
549 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
550 size_t key_len;
551 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200552 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
553 size_t curve_bits;
554 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200555 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200556
557 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
558 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
559
560 status = psa_import_key(&key_attr, d, d_len, &key_id);
561 ret = psa_pk_status_to_mbedtls(status);
562 if (ret != 0) {
563 return ret;
564 }
565
Jethro Beekman01672442023-04-19 14:08:14 +0200566 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
567 ret = psa_pk_status_to_mbedtls(status);
568 destruction_status = psa_destroy_key(key_id);
569 if (ret != 0) {
570 return ret;
571 } else if (destruction_status != PSA_SUCCESS) {
572 return psa_pk_status_to_mbedtls(destruction_status);
573 }
Jethro Beekman01672442023-04-19 14:08:14 +0200574 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200575#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200576#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200577 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200578 (void) d;
579 (void) d_len;
580
581 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
582#endif /* MBEDTLS_USE_PSA_CRYPTO */
583 return ret;
584}
585
586#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
587
588/*
589 * Load an RFC8410 EC key, which doesn't have any parameters
590 */
591static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
592 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200593 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200594{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200595 int ret;
596
Jethro Beekman01672442023-04-19 14:08:14 +0200597 if (params->tag != 0 || params->len != 0) {
598 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
599 }
600
Valerio Setti00e8dd12023-05-18 18:56:59 +0200601#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
602 ret = pk_update_psa_ecparams(pk, grp_id);
603#else
604 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200605 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
606 if (ret != 0) {
607 return ret;
608 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200609#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200610 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200611}
612
613/*
614 * Parse an RFC 8410 encoded private EC key
615 *
616 * CurvePrivateKey ::= OCTET STRING
617 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200618static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200619 unsigned char *key, size_t keylen, const unsigned char *end,
620 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
621{
622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
623 size_t len;
624
625 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
627 }
628
629 if (key + len != end) {
630 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
631 }
632
Valerio Setti00e8dd12023-05-18 18:56:59 +0200633#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
635 psa_status_t status;
636
637 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200638 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
639 PSA_KEY_USAGE_DERIVE);
640 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200641
642 status = psa_import_key(&attributes, key, len, &pk->priv_id);
643 if (status != PSA_SUCCESS) {
644 ret = psa_pk_status_to_mbedtls(status);
645 return ret;
646 }
647#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
648 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
649
Valerio Setti805e4a02023-06-30 17:16:19 +0200650 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
652 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200653#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200654
Valerio Setti4064dbb2023-05-17 15:33:07 +0200655 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
656 * which never contain a public key. As such, derive the public key
657 * unconditionally. */
658 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200659 return ret;
660 }
661
Jethro Beekman01672442023-04-19 14:08:14 +0200662 return 0;
663}
664#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200665
Valerio Settiaddeee42023-06-14 10:46:55 +0200666#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200667/*
668 * Create a temporary ecp_keypair for converting an EC point in compressed
669 * format to an uncompressed one
670 */
671static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
672 const unsigned char *in_start, size_t in_len,
673 size_t *out_buf_len, unsigned char *out_buf,
674 size_t out_buf_size)
675{
676 mbedtls_ecp_keypair ecp_key;
677 mbedtls_ecp_group_id ecp_group_id;
678 int ret;
679
680 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
681
682 mbedtls_ecp_keypair_init(&ecp_key);
683 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
684 if (ret != 0) {
685 return ret;
686 }
687 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
688 in_start, in_len);
689 if (ret != 0) {
690 goto exit;
691 }
692 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
693 MBEDTLS_ECP_PF_UNCOMPRESSED,
694 out_buf_len, out_buf, out_buf_size);
695
696exit:
697 mbedtls_ecp_keypair_free(&ecp_key);
698 return ret;
699}
Valerio Settiaddeee42023-06-14 10:46:55 +0200700#endif /* MBEDTLS_PK_USE_PSA_EC_DATA && MBEDTLS_PK_PARSE_EC_COMPRESSED */
Jethro Beekman01672442023-04-19 14:08:14 +0200701
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702/*
703 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100704 *
705 * The caller is responsible for clearing the structure upon failure if
706 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200710 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711{
Janos Follath24eed8d2019-11-22 13:21:35 +0000712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713
Valerio Setti4064dbb2023-05-17 15:33:07 +0200714#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
715 mbedtls_svc_key_id_t key;
716 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
717 size_t len = (end - *p);
718
719 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
720 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721 }
722
Valerio Setti4064dbb2023-05-17 15:33:07 +0200723 /* Compressed point format are not supported yet by PSA crypto. As a
724 * consequence ecp functions are used to "convert" the point to
725 * uncompressed format */
726 if ((**p == 0x02) || (**p == 0x03)) {
Valerio Settiaddeee42023-06-14 10:46:55 +0200727#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200728 ret = pk_convert_compressed_ec(pk, *p, len,
729 &(pk->pub_raw_len), pk->pub_raw,
730 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
731 if (ret != 0) {
732 return ret;
733 }
Valerio Settiaddeee42023-06-14 10:46:55 +0200734#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
735 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
736#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200737 } else {
738 /* Uncompressed format */
Dave Rodgman38c32282023-09-22 10:51:37 +0100739 if ((size_t) (end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200740 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200741 }
742 memcpy(pk->pub_raw, *p, (end - *p));
743 pk->pub_raw_len = end - *p;
744 }
745
746 /* Validate the key by trying to importing it */
747 psa_set_key_usage_flags(&key_attrs, 0);
748 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
749 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
750 psa_set_key_bits(&key_attrs, pk->ec_bits);
751
752 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
753 &key) != PSA_SUCCESS) ||
754 (psa_destroy_key(key) != PSA_SUCCESS)) {
755 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
756 pk->pub_raw_len = 0;
757 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
758 }
759 ret = 0;
760#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
761 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
762 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
763 (const unsigned char *) *p,
764 end - *p)) == 0) {
765 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
766 }
767#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
768
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200771 */
772 *p = (unsigned char *) end;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775}
Valerio Setti81d75122023-06-14 14:49:33 +0200776#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779/*
780 * RSAPublicKey ::= SEQUENCE {
781 * modulus INTEGER, -- n
782 * publicExponent INTEGER -- e
783 * }
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785static int pk_get_rsapubkey(unsigned char **p,
786 const unsigned char *end,
787 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788{
Janos Follath24eed8d2019-11-22 13:21:35 +0000789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790 size_t len;
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
793 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
795 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (*p + len != end) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
799 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
800 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
805 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
808 NULL, 0, NULL, 0)) != 0) {
809 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
810 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100811
812 *p += len;
813
814 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
817 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
820 NULL, 0, *p, len)) != 0) {
821 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
822 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100823
824 *p += len;
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (mbedtls_rsa_complete(rsa) != 0 ||
827 mbedtls_rsa_check_pubkey(rsa) != 0) {
828 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000829 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (*p != end) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
833 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
834 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
840/* Get a PK algorithm identifier
841 *
842 * AlgorithmIdentifier ::= SEQUENCE {
843 * algorithm OBJECT IDENTIFIER,
844 * parameters ANY DEFINED BY algorithm OPTIONAL }
845 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846static int pk_get_pk_alg(unsigned char **p,
847 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200848 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
849 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850{
Janos Follath24eed8d2019-11-22 13:21:35 +0000851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
857 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
858 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859
Jethro Beekman01672442023-04-19 14:08:14 +0200860 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200861#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200862 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
863 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
864 if (ret == 0) {
865 *pk_alg = MBEDTLS_PK_ECKEY;
866 }
867 }
868#else
869 (void) ec_grp_id;
870#endif
871 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
875 /*
876 * No parameters with RSA (only for EC)
877 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (*pk_alg == MBEDTLS_PK_RSA &&
879 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
880 params->len != 0)) {
881 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882 }
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885}
886
887/*
888 * SubjectPublicKeyInfo ::= SEQUENCE {
889 * algorithm AlgorithmIdentifier,
890 * subjectPublicKey BIT STRING }
891 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
893 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894{
Janos Follath24eed8d2019-11-22 13:21:35 +0000895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_asn1_buf alg_params;
898 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200899 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 }
906
907 end = *p + len;
908
Jethro Beekman01672442023-04-19 14:08:14 +0200909 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 return ret;
911 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
914 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
915 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (*p + len != end) {
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
919 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
920 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
923 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
924 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
927 return ret;
928 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (pk_alg == MBEDTLS_PK_RSA) {
932 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200935#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200937#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200938 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200939 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200940 } else
941#endif
942 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200943 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200944 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200946 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200949#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (ret == 0 && *p != end) {
953 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
954 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
955 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (ret != 0) {
958 mbedtls_pk_free(pk);
959 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962}
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200965/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100966 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
967 *
968 * The value zero is:
969 * - never a valid value for an RSA parameter
970 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
971 *
972 * Since values can't be omitted in PKCS#1, passing a zero value to
973 * rsa_complete() would be incorrect, so reject zero values early.
974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static int asn1_get_nonzero_mpi(unsigned char **p,
976 const unsigned char *end,
977 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100978{
979 int ret;
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 ret = mbedtls_asn1_get_mpi(p, end, X);
982 if (ret != 0) {
983 return ret;
984 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
987 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
988 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100991}
992
993/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200994 * Parse a PKCS#1 encoded private RSA key
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
997 const unsigned char *key,
998 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001000 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001 size_t len;
1002 unsigned char *p, *end;
1003
Hanno Beckerefa14e82017-10-11 19:45:19 +01001004 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001006
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 p = (unsigned char *) key;
1008 end = p + keylen;
1009
1010 /*
1011 * This function parses the RSAPrivateKey (PKCS#1)
1012 *
1013 * RSAPrivateKey ::= SEQUENCE {
1014 * version Version,
1015 * modulus INTEGER, -- n
1016 * publicExponent INTEGER, -- e
1017 * privateExponent INTEGER, -- d
1018 * prime1 INTEGER, -- p
1019 * prime2 INTEGER, -- q
1020 * exponent1 INTEGER, -- d mod (p-1)
1021 * exponent2 INTEGER, -- d mod (q-1)
1022 * coefficient INTEGER, -- (inverse of q) mod p
1023 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1024 * }
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1027 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1028 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029 }
1030
1031 end = p + len;
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1034 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 }
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (version != 0) {
1038 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 }
1040
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1043 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1044 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1050 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1051 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001054
1055 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1057 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1058 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061
1062 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1064 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1065 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068
1069 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1071 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1072 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001073 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001076#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001077 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1079 * that they can be easily recomputed from D, P and Q. However by
1080 * parsing them from the PKCS1 structure it is possible to avoid
1081 * recalculating them which both reduces the overhead of loading
1082 * RSA private keys into memory and also avoids side channels which
1083 * can arise when computing those values, since all of D, P, and Q
1084 * are secret. See https://eprint.iacr.org/2020/055 for a
1085 * description of one such attack.
1086 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001087
Jack Lloyd80cc8112020-01-22 17:34:29 -05001088 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1090 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1091 goto cleanup;
1092 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001093
1094 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1096 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1097 goto cleanup;
1098 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001099
1100 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1102 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1103 goto cleanup;
1104 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001105
Jack Lloyd60239752020-01-27 17:53:36 -05001106#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001107 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1109 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1110 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1111 goto cleanup;
1112 }
Jack Lloyd60239752020-01-27 17:53:36 -05001113#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001114
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001115 /* rsa_complete() doesn't complete anything with the default
1116 * implementation but is still called:
1117 * - for the benefit of alternative implementation that may want to
1118 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1119 * - as is also sanity-checks the key
1120 *
1121 * Furthermore, we also check the public part for consistency with
1122 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1125 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001126 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001127 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (p != end) {
1130 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1131 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 }
1133
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001134cleanup:
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001139 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret & 0xff80) == 0) {
1141 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1142 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001143 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 }
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Valerio Setti81d75122023-06-14 14:49:33 +02001153#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154/*
1155 * Parse a SEC1 encoded private EC key
1156 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001157static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 const unsigned char *key, size_t keylen,
1159 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160{
Janos Follath24eed8d2019-11-22 13:21:35 +00001161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001162 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001163 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001164 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001166 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 unsigned char *end = p + keylen;
1168 unsigned char *end2;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001169#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1171 psa_status_t status;
Valerio Setti81d75122023-06-14 14:49:33 +02001172#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1173 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001174#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
1176 /*
1177 * RFC 5915, or SEC1 Appendix C.4
1178 *
1179 * ECPrivateKey ::= SEQUENCE {
1180 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1181 * privateKey OCTET STRING,
1182 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1183 * publicKey [1] BIT STRING OPTIONAL
1184 * }
1185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1187 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 }
1190
1191 end = p + len;
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (version != 1) {
1198 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Valerio Setti6b062ee2023-06-30 17:32:57 +02001205 /* Keep a reference to the position fo the private key. It will be used
1206 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001207 d = p;
1208 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001209
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210 p += len;
1211
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001214 /*
1215 * Is 'parameters' present?
1216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1218 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1219 0)) == 0) {
1220 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001221 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001226 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001227 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001228
Valerio Setti6b062ee2023-06-30 17:32:57 +02001229
1230#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1231 if ((ret = mbedtls_ecp_read_key(eck->grp.id, eck, d, d_len)) != 0) {
1232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1233 }
1234#endif
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237 /*
1238 * Is 'publickey' present? If not, or if we can't read it (eg because it
1239 * is compressed), create it from the private key.
1240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1242 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1243 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 end2 = p + len;
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1248 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (p + len != end2) {
1251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1252 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1253 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001254
Valerio Setti4064dbb2023-05-17 15:33:07 +02001255 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001256 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001258 /*
1259 * The only acceptable failure mode of pk_get_ecpubkey() above
1260 * is if the point format is not recognized.
1261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1263 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1264 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001265 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001268 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001270
Valerio Setti00e8dd12023-05-18 18:56:59 +02001271#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1272 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1273 /* Setting largest masks for usage and key algorithms */
1274 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1275 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001276 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001277#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1278 psa_set_key_algorithm(&attributes,
1279 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1280#else
1281 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1282#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001283 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001284
Valerio Settia541e012023-05-24 14:31:21 +02001285 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001286 if (status != PSA_SUCCESS) {
1287 ret = psa_pk_status_to_mbedtls(status);
1288 return ret;
1289 }
1290#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1291
Valerio Setti34f67552023-04-03 15:19:18 +02001292 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001293 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001294 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001295 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001296 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299}
Valerio Setti81d75122023-06-14 14:49:33 +02001300#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
1302/*
1303 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001304 *
1305 * Notes:
1306 *
1307 * - This function does not own the key buffer. It is the
1308 * responsibility of the caller to take care of zeroizing
1309 * and freeing it after use.
1310 *
1311 * - The function is responsible for freeing the provided
1312 * PK context on failure.
1313 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 */
1315static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 mbedtls_pk_context *pk,
1317 const unsigned char *key, size_t keylen,
1318 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319{
1320 int ret, version;
1321 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 unsigned char *p = (unsigned char *) key;
1324 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001326 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328
Valerio Setti81d75122023-06-14 14:49:33 +02001329#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001330 (void) f_rng;
1331 (void) p_rng;
1332#endif
1333
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001335 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336 *
1337 * PrivateKeyInfo ::= SEQUENCE {
1338 * version Version,
1339 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1340 * privateKey PrivateKey,
1341 * attributes [0] IMPLICIT Attributes OPTIONAL }
1342 *
1343 * Version ::= INTEGER
1344 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1345 * PrivateKey ::= OCTET STRING
1346 *
1347 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1348 */
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1351 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353 }
1354
1355 end = p + len;
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001359 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (version != 0) {
1362 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1363 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Jethro Beekman01672442023-04-19 14:08:14 +02001365 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return ret;
1367 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1371 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (len < 1) {
1374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1375 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1376 }
1377
1378 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1379 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1380 }
1381
1382 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1383 return ret;
1384 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (pk_alg == MBEDTLS_PK_RSA) {
1388 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1389 mbedtls_pk_free(pk);
1390 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 }
1392 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001394#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001396#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001397 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001398 if ((ret =
1399 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001400 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001401 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001402 p_rng)) != 0) {
1403 mbedtls_pk_free(pk);
1404 return ret;
1405 }
1406 } else
1407#endif
1408 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001409 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1410 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001411 mbedtls_pk_free(pk);
1412 return ret;
1413 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 }
1415 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001416#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001419 end = p + len;
1420 if (end != (key + keylen)) {
1421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1422 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1423 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426}
1427
1428/*
1429 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001430 *
1431 * To save space, the decryption happens in-place on the given key buffer.
1432 * Also, while this function may modify the keybuffer, it doesn't own it,
1433 * and instead it is the responsibility of the caller to zeroize and properly
1434 * free it after use.
1435 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001438MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_pk_context *pk,
1440 unsigned char *key, size_t keylen,
1441 const unsigned char *pwd, size_t pwdlen,
1442 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001444 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001446 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1449#if defined(MBEDTLS_PKCS12_C)
1450 mbedtls_cipher_type_t cipher_alg;
1451 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001453 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454
Hanno Becker2aa80a72017-09-07 15:28:45 +01001455 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 end = p + keylen;
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (pwdlen == 0) {
1459 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1460 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461
1462 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001463 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464 *
1465 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1466 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1467 * encryptedData EncryptedData
1468 * }
1469 *
1470 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1471 *
1472 * EncryptedData ::= OCTET STRING
1473 *
1474 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001475 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1478 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1479 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 }
1481
1482 end = p + len;
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1485 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1486 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1489 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1490 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
Hanno Beckerfab35692017-08-25 13:38:26 +01001492 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
1494 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001495 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001499 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001500 cipher_alg, md_alg,
1501 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1503 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001508
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001509 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_PKCS12_C */
1512#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001514 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1515 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1517 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1518 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001522
1523 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001526 {
1527 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001528 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (decrypted == 0) {
1531 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1532 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001533 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
1537/*
1538 * Parse a private key
1539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1541 const unsigned char *key, size_t keylen,
1542 const unsigned char *pwd, size_t pwdlen,
1543 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544{
Janos Follath24eed8d2019-11-22 13:21:35 +00001545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001550#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (keylen == 0) {
1553 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1554 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001555
1556#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001560 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001562 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 } else {
1564 ret = mbedtls_pem_read_buffer(&pem,
1565 "-----BEGIN RSA PRIVATE KEY-----",
1566 "-----END RSA PRIVATE KEY-----",
1567 key, pwd, pwdlen, &len);
1568 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (ret == 0) {
1571 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1572 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1573 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1574 pem.buf, pem.buflen)) != 0) {
1575 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576 }
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 mbedtls_pem_free(&pem);
1579 return ret;
1580 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1581 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1582 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1583 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1584 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1585 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001588
Valerio Setti81d75122023-06-14 14:49:33 +02001589#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001590 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001592 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else {
1594 ret = mbedtls_pem_read_buffer(&pem,
1595 "-----BEGIN EC PRIVATE KEY-----",
1596 "-----END EC PRIVATE KEY-----",
1597 key, pwd, pwdlen, &len);
1598 }
1599 if (ret == 0) {
1600 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001603 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 pem.buf, pem.buflen,
1605 f_rng, p_rng)) != 0) {
1606 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607 }
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 mbedtls_pem_free(&pem);
1610 return ret;
1611 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1612 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1613 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1614 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1615 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1616 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001617 }
Valerio Setti81d75122023-06-14 14:49:33 +02001618#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001620 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001622 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 } else {
1624 ret = mbedtls_pem_read_buffer(&pem,
1625 "-----BEGIN PRIVATE KEY-----",
1626 "-----END PRIVATE KEY-----",
1627 key, NULL, 0, &len);
1628 }
1629 if (ret == 0) {
1630 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1631 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1632 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001633 }
1634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_pem_free(&pem);
1636 return ret;
1637 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1638 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001639 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001642 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001644 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 } else {
1646 ret = mbedtls_pem_read_buffer(&pem,
1647 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1648 "-----END ENCRYPTED PRIVATE KEY-----",
1649 key, NULL, 0, &len);
1650 }
1651 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001652 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1653 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001655 }
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 mbedtls_pem_free(&pem);
1658 return ret;
1659 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1660 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001663#else
1664 ((void) pwd);
1665 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001667
1668 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001669 * At this point we only know it's not a PEM formatted key. Could be any
1670 * of the known DER encoded private key formats
1671 *
1672 * We try the different DER format parsers to see if one passes without
1673 * error
1674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001677 unsigned char *key_copy;
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1680 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1681 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001684
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001685 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1686 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001687
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001688 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (ret == 0) {
1692 return 0;
1693 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 mbedtls_pk_free(pk);
1696 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1699 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1704 if (ret == 0) {
1705 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001706 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 mbedtls_pk_free(pk);
1709 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1714 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1715 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1716 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717 }
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_pk_free(pk);
1720 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001722
Valerio Setti81d75122023-06-14 14:49:33 +02001723#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1725 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001726 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 key, keylen, f_rng, p_rng) == 0) {
1728 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001729 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001731#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001732
Valerio Setti81d75122023-06-14 14:49:33 +02001733 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001734 * it is ok to leave the PK context initialized but not
1735 * freed: It is the caller's responsibility to call pk_init()
1736 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001737 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001738 * isn't, this leads to mbedtls_pk_free() being called
1739 * twice, once here and once by the caller, but this is
1740 * also ok and in line with the mbedtls_pk_free() calls
1741 * on failed PEM parsing attempts. */
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001744}
1745
1746/*
1747 * Parse a public key
1748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1750 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751{
Janos Follath24eed8d2019-11-22 13:21:35 +00001752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001753 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001754#if defined(MBEDTLS_RSA_C)
1755 const mbedtls_pk_info_t *pk_info;
1756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001758 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001760#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (keylen == 0) {
1763 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1764 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001765
1766#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001768#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001769 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001771 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 } else {
1773 ret = mbedtls_pem_read_buffer(&pem,
1774 "-----BEGIN RSA PUBLIC KEY-----",
1775 "-----END RSA PUBLIC KEY-----",
1776 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001777 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001778
1779 if (ret == 0) {
1780 p = pem.buf;
1781 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1782 mbedtls_pem_free(&pem);
1783 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1784 }
1785
1786 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1787 mbedtls_pem_free(&pem);
1788 return ret;
1789 }
1790
1791 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1792 mbedtls_pk_free(ctx);
1793 }
1794
1795 mbedtls_pem_free(&pem);
1796 return ret;
1797 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1798 mbedtls_pem_free(&pem);
1799 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001800 }
1801#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001802
1803 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001805 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 } else {
1807 ret = mbedtls_pem_read_buffer(&pem,
1808 "-----BEGIN PUBLIC KEY-----",
1809 "-----END PUBLIC KEY-----",
1810 key, NULL, 0, &len);
1811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001814 /*
1815 * Was PEM encoded
1816 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001817 p = pem.buf;
1818
Jethro Beekman01672442023-04-19 14:08:14 +02001819 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 mbedtls_pem_free(&pem);
1821 return ret;
1822 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1823 mbedtls_pem_free(&pem);
1824 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001828
1829#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1831 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001833
1834 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1835 return ret;
1836 }
1837
1838 p = (unsigned char *) key;
1839 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1840 if (ret == 0) {
1841 return ret;
1842 }
1843 mbedtls_pk_free(ctx);
1844 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1845 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1846 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001847 }
1848#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001849 p = (unsigned char *) key;
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001854}
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#endif /* MBEDTLS_PK_PARSE_C */