blob: e70953e9afbffb8d1fd810bd013389fc98c7424d [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Valerio Setti77a75682023-05-15 11:18:46 +020029#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Jethro Beekman01672442023-04-19 14:08:14 +020037#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
38#include "pkwrite.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +020040#if defined(MBEDTLS_ECP_LIGHT)
41#include "pk_internal.h"
42#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
55
Valerio Setti34f67552023-04-03 15:19:18 +020056#if defined(MBEDTLS_PSA_CRYPTO_C)
57#include "mbedtls/psa_util.h"
58#endif
59
60#if defined(MBEDTLS_USE_PSA_CRYPTO)
61#include "psa/crypto.h"
62#endif
63
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020065
Valerio Settie0e63112023-05-18 18:48:07 +020066/* Helper for Montgomery curves */
67#if defined(MBEDTLS_ECP_LIGHT) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
68#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
69 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
70#endif /* MBEDTLS_ECP_LIGHT && MBEDTLS_PK_HAVE_RFC8410_CURVES */
71
Gilles Peskine832f3492017-11-30 11:42:12 +010072#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073/*
74 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020075 *
76 * The file is expected to contain either PEM or DER encoded data.
77 * A terminating null byte is always appended. It is included in the announced
78 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020081{
82 FILE *f;
83 long size;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if ((f = fopen(path, "rb")) == NULL) {
86 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
87 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
Gilles Peskineda0913b2022-06-30 17:03:40 +020089 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fseek(f, 0, SEEK_END);
93 if ((size = ftell(f)) == -1) {
94 fclose(f);
95 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
Gilles Peskine449bd832023-01-11 14:50:10 +010097 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 *n = (size_t) size;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (*n + 1 == 0 ||
102 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
103 fclose(f);
104 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (fread(*buf, 1, *n, f) != *n) {
108 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_platform_zeroize(*buf, *n);
111 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
118 (*buf)[*n] = '\0';
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200121 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125}
126
127/*
128 * Load and parse a private key
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
131 const char *path, const char *pwd,
132 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133{
Janos Follath24eed8d2019-11-22 13:21:35 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 size_t n;
136 unsigned char *buf;
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
139 return ret;
140 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (pwd == NULL) {
143 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
144 } else {
145 ret = mbedtls_pk_parse_key(ctx, buf, n,
146 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_platform_zeroize(buf, n);
150 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153}
154
155/*
156 * Load and parse a public key
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159{
Janos Follath24eed8d2019-11-22 13:21:35 +0000160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 size_t n;
162 unsigned char *buf;
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
165 return ret;
166 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_platform_zeroize(buf, n);
171 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176
Valerio Setti0d2980f2023-04-05 18:17:13 +0200177#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179 *
180 * ECParameters ::= CHOICE {
181 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100182 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 * }
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
187 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188{
Janos Follath24eed8d2019-11-22 13:21:35 +0000189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (end - *p < 1) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
194 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100195
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 ) {
203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
204 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210
211 params->p = *p;
212 *p += params->len;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (*p != end) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
217 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
225 * WARNING: the resulting group should only be used with
226 * pk_group_id_from_specified(), since its base point may not be set correctly
227 * if it was encoded compressed.
228 *
229 * SpecifiedECDomain ::= SEQUENCE {
230 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
231 * fieldID FieldID {{FieldTypes}},
232 * curve Curve,
233 * base ECPoint,
234 * order INTEGER,
235 * cofactor INTEGER OPTIONAL,
236 * hash HashAlgorithm OPTIONAL,
237 * ...
238 * }
239 *
240 * We only support prime-field as field type, and ignore hash and cofactor.
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100243{
Janos Follath24eed8d2019-11-22 13:21:35 +0000244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200246 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100247 const unsigned char *end_field, *end_curve;
248 size_t len;
249 int ver;
250
251 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
254 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ver < 1 || ver > 3) {
257 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 /*
261 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
262 * fieldType FIELD-ID.&id({IOSet}),
263 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
264 * }
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
268 return ret;
269 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270
271 end_field = p + len;
272
273 /*
274 * FIELD-ID ::= TYPE-IDENTIFIER
275 * FieldTypes FIELD-ID ::= {
276 * { Prime-p IDENTIFIED BY prime-field } |
277 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
278 * }
279 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
286 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
287 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 }
289
290 p += len;
291
292 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
295 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (p != end_field) {
300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
302 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303
304 /*
305 * Curve ::= SEQUENCE {
306 * a FieldElement,
307 * b FieldElement,
308 * seed BIT STRING OPTIONAL
309 * -- Shall be present if used in SpecifiedECDomain
310 * -- with version equal to ecdpVer2 or ecdpVer3
311 * }
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
315 return ret;
316 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317
318 end_curve = p + len;
319
320 /*
321 * FieldElement ::= OCTET STRING
322 * containing an integer in the case of a prime field
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->A, 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
332 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 }
335
336 p += len;
337
338 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (p != end_curve) {
344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
345 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
346 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100347
348 /*
349 * ECPoint ::= OCTET STRING
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
353 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
356 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 /*
358 * If we can't read the point because it's compressed, cheat by
359 * reading only the X coordinate and the parity bit of Y.
360 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
362 (p[0] != 0x02 && p[0] != 0x03) ||
363 len != mbedtls_mpi_size(&grp->P) + 1 ||
364 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
365 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
366 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
367 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100368 }
369 }
370
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371 p += len;
372
373 /*
374 * order INTEGER
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
378 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
382 /*
383 * Allow optional elements by purposefully not enforcing p == end here.
384 */
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387}
388
389/*
390 * Find the group id associated with an (almost filled) group as generated by
391 * pk_group_from_specified(), or return an error if unknown.
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static 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 +0100394{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100395 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_group ref;
397 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_ecp_group_free(&ref);
404 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
408 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
409 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
410 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
411 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
412 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
413 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 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 +0100416 break;
417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418 }
419
420cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 *grp_id = *id;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430}
431
432/*
433 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
436 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000450 /* The API respecting lifecycle for mbedtls_ecp_group struct is
451 * _init(), _load() and _free(). In pk_group_id_from_specified() the
452 * temporary grp breaks that flow and it's members are populated
453 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
454 * which is assuming a group populated by _setup() may not clean-up
455 * properly -> Manually free it's members.
456 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000457 mbedtls_mpi_free(&grp.N);
458 mbedtls_mpi_free(&grp.P);
459 mbedtls_mpi_free(&grp.A);
460 mbedtls_mpi_free(&grp.B);
461 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
Valerio Setti4064dbb2023-05-17 15:33:07 +0200467#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
468/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
469 * ecp_keypair structure with proper group ID. The purpose of this helper
470 * function is to update ec_family and ec_bits accordingly. */
471static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
472 mbedtls_ecp_group_id grp_id)
473{
474 psa_ecc_family_t ec_family;
475 size_t bits;
476
477 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
478
479 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
481 }
482
483 pk->ec_family = ec_family;
484 pk->ec_bits = bits;
485
486 return 0;
487}
488#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
489
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 *
493 * ECParameters ::= CHOICE {
494 * namedCurve OBJECT IDENTIFIER
495 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
496 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200498static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (params->tag == MBEDTLS_ASN1_OID) {
504 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
505 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
506 }
507 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
510 return ret;
511 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100512#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100514#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Valerio Setti00e8dd12023-05-18 18:56:59 +0200517#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
518 ret = pk_update_psa_ecparams(pk, grp_id);
519#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200520 /* grp may already be initialized; if so, make sure IDs match */
521 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
522 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
524 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525
Valerio Setti4064dbb2023-05-17 15:33:07 +0200526 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
527 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return ret;
529 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200530#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531
Valerio Setti4064dbb2023-05-17 15:33:07 +0200532 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533}
534
Jethro Beekman01672442023-04-19 14:08:14 +0200535/*
536 * Helper function for deriving a public key from its private counterpart.
537 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200538static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200539 const unsigned char *d, size_t d_len,
540 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
541{
542 int ret;
543#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200544 psa_status_t status;
545 (void) f_rng;
546 (void) p_rng;
547#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
548 (void) d;
549 (void) d_len;
550
551 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
552 &pk->pub_raw_len);
553 ret = psa_pk_status_to_mbedtls(status);
554#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
555 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
556 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
557 size_t key_len;
558 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200559 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
560 size_t curve_bits;
561 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200562 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200563
564 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
565 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
566
567 status = psa_import_key(&key_attr, d, d_len, &key_id);
568 ret = psa_pk_status_to_mbedtls(status);
569 if (ret != 0) {
570 return ret;
571 }
572
Jethro Beekman01672442023-04-19 14:08:14 +0200573 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
574 ret = psa_pk_status_to_mbedtls(status);
575 destruction_status = psa_destroy_key(key_id);
576 if (ret != 0) {
577 return ret;
578 } else if (destruction_status != PSA_SUCCESS) {
579 return psa_pk_status_to_mbedtls(destruction_status);
580 }
Jethro Beekman01672442023-04-19 14:08:14 +0200581 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200582#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200583#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200584 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200585 (void) d;
586 (void) d_len;
587
588 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
589#endif /* MBEDTLS_USE_PSA_CRYPTO */
590 return ret;
591}
592
593#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
594
595/*
596 * Load an RFC8410 EC key, which doesn't have any parameters
597 */
598static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
599 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200600 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200601{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200602 int ret;
603
Jethro Beekman01672442023-04-19 14:08:14 +0200604 if (params->tag != 0 || params->len != 0) {
605 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
606 }
607
Valerio Setti00e8dd12023-05-18 18:56:59 +0200608#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
609 ret = pk_update_psa_ecparams(pk, grp_id);
610#else
611 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200612 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
613 if (ret != 0) {
614 return ret;
615 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200616#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200617 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200618}
619
620/*
621 * Parse an RFC 8410 encoded private EC key
622 *
623 * CurvePrivateKey ::= OCTET STRING
624 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200625static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200626 unsigned char *key, size_t keylen, const unsigned char *end,
627 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
628{
629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
630 size_t len;
631
632 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
634 }
635
636 if (key + len != end) {
637 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
638 }
639
Valerio Setti00e8dd12023-05-18 18:56:59 +0200640#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
642 psa_status_t status;
643
644 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
646 PSA_KEY_USAGE_DERIVE);
647 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200648
649 status = psa_import_key(&attributes, key, len, &pk->priv_id);
650 if (status != PSA_SUCCESS) {
651 ret = psa_pk_status_to_mbedtls(status);
652 return ret;
653 }
654#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
655 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
656
Jethro Beekman01672442023-04-19 14:08:14 +0200657 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
658 mbedtls_ecp_keypair_free(eck);
659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
660 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200661#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200662
Valerio Setti4064dbb2023-05-17 15:33:07 +0200663 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
664 * which never contain a public key. As such, derive the public key
665 * unconditionally. */
666 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200667#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200668 mbedtls_ecp_keypair_free(eck);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200669#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200670 return ret;
671 }
672
Valerio Setti00e8dd12023-05-18 18:56:59 +0200673 /* When MBEDTLS_PK_USE_PSA_EC_DATA the key is checked while importing it
674 * into PSA. */
675#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200676 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
677 mbedtls_ecp_keypair_free(eck);
678 return ret;
679 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200680#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200681
682 return 0;
683}
684#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200685
686#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
687/*
688 * Create a temporary ecp_keypair for converting an EC point in compressed
689 * format to an uncompressed one
690 */
691static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
692 const unsigned char *in_start, size_t in_len,
693 size_t *out_buf_len, unsigned char *out_buf,
694 size_t out_buf_size)
695{
696 mbedtls_ecp_keypair ecp_key;
697 mbedtls_ecp_group_id ecp_group_id;
698 int ret;
699
700 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
701
702 mbedtls_ecp_keypair_init(&ecp_key);
703 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
704 if (ret != 0) {
705 return ret;
706 }
707 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
708 in_start, in_len);
709 if (ret != 0) {
710 goto exit;
711 }
712 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
713 MBEDTLS_ECP_PF_UNCOMPRESSED,
714 out_buf_len, out_buf, out_buf_size);
715
716exit:
717 mbedtls_ecp_keypair_free(&ecp_key);
718 return ret;
719}
720#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200721
Paul Bakker1a7550a2013-09-15 13:01:22 +0200722/*
723 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100724 *
725 * The caller is responsible for clearing the structure upon failure if
726 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200730 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733
Valerio Setti4064dbb2023-05-17 15:33:07 +0200734#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
735 mbedtls_svc_key_id_t key;
736 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
737 size_t len = (end - *p);
738
739 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
740 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 }
742
Valerio Setti4064dbb2023-05-17 15:33:07 +0200743 /* Compressed point format are not supported yet by PSA crypto. As a
744 * consequence ecp functions are used to "convert" the point to
745 * uncompressed format */
746 if ((**p == 0x02) || (**p == 0x03)) {
747 ret = pk_convert_compressed_ec(pk, *p, len,
748 &(pk->pub_raw_len), pk->pub_raw,
749 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
750 if (ret != 0) {
751 return ret;
752 }
753 } else {
754 /* Uncompressed format */
755 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200756 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200757 }
758 memcpy(pk->pub_raw, *p, (end - *p));
759 pk->pub_raw_len = end - *p;
760 }
761
762 /* Validate the key by trying to importing it */
763 psa_set_key_usage_flags(&key_attrs, 0);
764 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
765 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
766 psa_set_key_bits(&key_attrs, pk->ec_bits);
767
768 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
769 &key) != PSA_SUCCESS) ||
770 (psa_destroy_key(key) != PSA_SUCCESS)) {
771 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
772 pk->pub_raw_len = 0;
773 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
774 }
775 ret = 0;
776#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
777 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
778 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
779 (const unsigned char *) *p,
780 end - *p)) == 0) {
781 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
782 }
783#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
784
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787 */
788 *p = (unsigned char *) end;
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200792#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795/*
796 * RSAPublicKey ::= SEQUENCE {
797 * modulus INTEGER, -- n
798 * publicExponent INTEGER -- e
799 * }
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801static int pk_get_rsapubkey(unsigned char **p,
802 const unsigned char *end,
803 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804{
Janos Follath24eed8d2019-11-22 13:21:35 +0000805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806 size_t len;
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
809 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (*p + len != end) {
814 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
816 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
821 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
824 NULL, 0, NULL, 0)) != 0) {
825 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
826 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827
828 *p += len;
829
830 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
836 NULL, 0, *p, len)) != 0) {
837 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
838 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839
840 *p += len;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (mbedtls_rsa_complete(rsa) != 0 ||
843 mbedtls_rsa_check_pubkey(rsa) != 0) {
844 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000845 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (*p != end) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
849 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
850 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
856/* Get a PK algorithm identifier
857 *
858 * AlgorithmIdentifier ::= SEQUENCE {
859 * algorithm OBJECT IDENTIFIER,
860 * parameters ANY DEFINED BY algorithm OPTIONAL }
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int pk_get_pk_alg(unsigned char **p,
863 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200864 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
865 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
873 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
874 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
Jethro Beekman01672442023-04-19 14:08:14 +0200876 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
877#if defined(MBEDTLS_ECP_LIGHT)
878 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
879 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
880 if (ret == 0) {
881 *pk_alg = MBEDTLS_PK_ECKEY;
882 }
883 }
884#else
885 (void) ec_grp_id;
886#endif
887 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
889 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
891 /*
892 * No parameters with RSA (only for EC)
893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (*pk_alg == MBEDTLS_PK_RSA &&
895 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
896 params->len != 0)) {
897 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901}
902
903/*
904 * SubjectPublicKeyInfo ::= SEQUENCE {
905 * algorithm AlgorithmIdentifier,
906 * subjectPublicKey BIT STRING }
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
909 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910{
Janos Follath24eed8d2019-11-22 13:21:35 +0000911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_asn1_buf alg_params;
914 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200915 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
919 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
920 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921 }
922
923 end = *p + len;
924
Jethro Beekman01672442023-04-19 14:08:14 +0200925 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 return ret;
927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (*p + len != end) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
939 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
943 return ret;
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (pk_alg == MBEDTLS_PK_RSA) {
948 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200951#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200953#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200954 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200955 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200956 } else
957#endif
958 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200959 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200962 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200965#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret == 0 && *p != end) {
969 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
970 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
971 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (ret != 0) {
974 mbedtls_pk_free(pk);
975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978}
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
983 *
984 * The value zero is:
985 * - never a valid value for an RSA parameter
986 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
987 *
988 * Since values can't be omitted in PKCS#1, passing a zero value to
989 * rsa_complete() would be incorrect, so reject zero values early.
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991static int asn1_get_nonzero_mpi(unsigned char **p,
992 const unsigned char *end,
993 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100994{
995 int ret;
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 ret = mbedtls_asn1_get_mpi(p, end, X);
998 if (ret != 0) {
999 return ret;
1000 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1003 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1004 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001007}
1008
1009/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010 * Parse a PKCS#1 encoded private RSA key
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1013 const unsigned char *key,
1014 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001016 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 size_t len;
1018 unsigned char *p, *end;
1019
Hanno Beckerefa14e82017-10-11 19:45:19 +01001020 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001022
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 p = (unsigned char *) key;
1024 end = p + keylen;
1025
1026 /*
1027 * This function parses the RSAPrivateKey (PKCS#1)
1028 *
1029 * RSAPrivateKey ::= SEQUENCE {
1030 * version Version,
1031 * modulus INTEGER, -- n
1032 * publicExponent INTEGER, -- e
1033 * privateExponent INTEGER, -- d
1034 * prime1 INTEGER, -- p
1035 * prime2 INTEGER, -- q
1036 * exponent1 INTEGER, -- d mod (p-1)
1037 * exponent2 INTEGER, -- d mod (q-1)
1038 * coefficient INTEGER, -- (inverse of q) mod p
1039 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1040 * }
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1043 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1044 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045 }
1046
1047 end = p + len;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1050 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (version != 0) {
1054 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 }
1056
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1059 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1060 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1066 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1067 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070
1071 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1073 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1074 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077
1078 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1080 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1081 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001082 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001084
1085 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1088 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001089 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001092#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001093 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1095 * that they can be easily recomputed from D, P and Q. However by
1096 * parsing them from the PKCS1 structure it is possible to avoid
1097 * recalculating them which both reduces the overhead of loading
1098 * RSA private keys into memory and also avoids side channels which
1099 * can arise when computing those values, since all of D, P, and Q
1100 * are secret. See https://eprint.iacr.org/2020/055 for a
1101 * description of one such attack.
1102 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001103
Jack Lloyd80cc8112020-01-22 17:34:29 -05001104 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001109
1110 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1112 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1113 goto cleanup;
1114 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001115
1116 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1118 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1119 goto cleanup;
1120 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001121
Jack Lloyd60239752020-01-27 17:53:36 -05001122#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001123 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1126 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1127 goto cleanup;
1128 }
Jack Lloyd60239752020-01-27 17:53:36 -05001129#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001130
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001131 /* rsa_complete() doesn't complete anything with the default
1132 * implementation but is still called:
1133 * - for the benefit of alternative implementation that may want to
1134 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1135 * - as is also sanity-checks the key
1136 *
1137 * Furthermore, we also check the public part for consistency with
1138 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1141 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001142 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001143 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (p != end) {
1146 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1147 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 }
1149
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001150cleanup:
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001155 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if ((ret & 0xff80) == 0) {
1157 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1158 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001159 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
Valerio Setti0d2980f2023-04-05 18:17:13 +02001169#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170/*
1171 * Parse a SEC1 encoded private EC key
1172 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001173static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 const unsigned char *key, size_t keylen,
1175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176{
Janos Follath24eed8d2019-11-22 13:21:35 +00001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001178 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001179 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001180 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001182 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 unsigned char *end = p + keylen;
1184 unsigned char *end2;
Valerio Setti4064dbb2023-05-17 15:33:07 +02001185 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001186#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1188 psa_status_t status;
1189 uint8_t priv_key_raw[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
1190 size_t priv_key_len;
1191#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
1193 /*
1194 * RFC 5915, or SEC1 Appendix C.4
1195 *
1196 * ECPrivateKey ::= SEQUENCE {
1197 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1198 * privateKey OCTET STRING,
1199 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1200 * publicKey [1] BIT STRING OPTIONAL
1201 * }
1202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1204 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 }
1207
1208 end = p + len;
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (version != 1) {
1215 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1216 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1220 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Jethro Beekman01672442023-04-19 14:08:14 +02001222 d = p;
1223 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001224
1225#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1226 if (len > MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH) {
1227 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1228 }
1229 memcpy(priv_key_raw, p, len);
1230 priv_key_len = len;
1231#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1233 mbedtls_ecp_keypair_free(eck);
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001236#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237
1238 p += len;
1239
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001242 /*
1243 * Is 'parameters' present?
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1246 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1247 0)) == 0) {
1248 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001249 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 mbedtls_ecp_keypair_free(eck);
1251 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1254 mbedtls_ecp_keypair_free(eck);
1255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001256 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001257 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001260 /*
1261 * Is 'publickey' present? If not, or if we can't read it (eg because it
1262 * is compressed), create it from the private key.
1263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1265 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1266 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001267 end2 = p + len;
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1271 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (p + len != end2) {
1274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1275 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1276 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001277
Valerio Setti4064dbb2023-05-17 15:33:07 +02001278 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001279 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001281 /*
1282 * The only acceptable failure mode of pk_get_ecpubkey() above
1283 * is if the point format is not recognized.
1284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1286 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1287 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001288 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1290 mbedtls_ecp_keypair_free(eck);
1291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001292 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001294
Valerio Setti00e8dd12023-05-18 18:56:59 +02001295#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1296 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1297 /* Setting largest masks for usage and key algorithms */
1298 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1299 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001300 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001301#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1302 psa_set_key_algorithm(&attributes,
1303 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1304#else
1305 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1306#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001307 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001308
1309 status = psa_import_key(&attributes, priv_key_raw, priv_key_len,
1310 &pk->priv_id);
1311 if (status != PSA_SUCCESS) {
1312 ret = psa_pk_status_to_mbedtls(status);
1313 return ret;
1314 }
1315#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1316
Valerio Setti34f67552023-04-03 15:19:18 +02001317 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001318 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001319 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001320 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001321 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001322 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323
Valerio Setti00e8dd12023-05-18 18:56:59 +02001324#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1326 mbedtls_ecp_keypair_free(eck);
1327 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001329#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001333#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334
1335/*
1336 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001337 *
1338 * Notes:
1339 *
1340 * - This function does not own the key buffer. It is the
1341 * responsibility of the caller to take care of zeroizing
1342 * and freeing it after use.
1343 *
1344 * - The function is responsible for freeing the provided
1345 * PK context on failure.
1346 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 */
1348static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 mbedtls_pk_context *pk,
1350 const unsigned char *key, size_t keylen,
1351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352{
1353 int ret, version;
1354 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 unsigned char *p = (unsigned char *) key;
1357 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001359 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Jethro Beekman01672442023-04-19 14:08:14 +02001362#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001363 (void) f_rng;
1364 (void) p_rng;
1365#endif
1366
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001368 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 *
1370 * PrivateKeyInfo ::= SEQUENCE {
1371 * version Version,
1372 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1373 * privateKey PrivateKey,
1374 * attributes [0] IMPLICIT Attributes OPTIONAL }
1375 *
1376 * Version ::= INTEGER
1377 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1378 * PrivateKey ::= OCTET STRING
1379 *
1380 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1381 */
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1384 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 }
1387
1388 end = p + len;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001392 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (version != 0) {
1395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1396 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397
Jethro Beekman01672442023-04-19 14:08:14 +02001398 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return ret;
1400 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1403 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1404 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (len < 1) {
1407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1408 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1409 }
1410
1411 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1412 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1413 }
1414
1415 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1416 return ret;
1417 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (pk_alg == MBEDTLS_PK_RSA) {
1421 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1422 mbedtls_pk_free(pk);
1423 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 }
1425 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001427#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001429#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001430 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001431 if ((ret =
1432 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001433 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001434 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001435 p_rng)) != 0) {
1436 mbedtls_pk_free(pk);
1437 return ret;
1438 }
1439 } else
1440#endif
1441 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001442 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1443 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001444 mbedtls_pk_free(pk);
1445 return ret;
1446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447 }
1448 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001449#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453}
1454
1455/*
1456 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001457 *
1458 * To save space, the decryption happens in-place on the given key buffer.
1459 * Also, while this function may modify the keybuffer, it doesn't own it,
1460 * and instead it is the responsibility of the caller to zeroize and properly
1461 * free it after use.
1462 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_pk_context *pk,
1467 unsigned char *key, size_t keylen,
1468 const unsigned char *pwd, size_t pwdlen,
1469 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001471 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001473 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1476#if defined(MBEDTLS_PKCS12_C)
1477 mbedtls_cipher_type_t cipher_alg;
1478 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479#endif
1480
Hanno Becker2aa80a72017-09-07 15:28:45 +01001481 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 end = p + keylen;
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (pwdlen == 0) {
1485 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1486 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487
1488 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001489 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490 *
1491 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1492 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1493 * encryptedData EncryptedData
1494 * }
1495 *
1496 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1497 *
1498 * EncryptedData ::= OCTET STRING
1499 *
1500 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001501 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1504 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1505 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506 }
1507
1508 end = p + len;
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1512 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1515 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1516 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517
Hanno Beckerfab35692017-08-25 13:38:26 +01001518 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
1520 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001521 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1525 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1526 cipher_alg, md_alg,
1527 pwd, pwdlen, p, len, buf)) != 0) {
1528 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1529 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1530 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001534
1535 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_PKCS12_C */
1538#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1540 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1541 p, len, buf)) != 0) {
1542 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1543 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1544 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001547 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001548
1549 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001552 {
1553 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001554 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (decrypted == 0) {
1557 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1558 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563
1564/*
1565 * Parse a private key
1566 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1568 const unsigned char *key, size_t keylen,
1569 const unsigned char *pwd, size_t pwdlen,
1570 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001571{
Janos Follath24eed8d2019-11-22 13:21:35 +00001572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001577#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (keylen == 0) {
1580 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1581 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001582
1583#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001587 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001589 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 } else {
1591 ret = mbedtls_pem_read_buffer(&pem,
1592 "-----BEGIN RSA PRIVATE KEY-----",
1593 "-----END RSA PRIVATE KEY-----",
1594 key, pwd, pwdlen, &len);
1595 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (ret == 0) {
1598 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1599 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1600 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1601 pem.buf, pem.buflen)) != 0) {
1602 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603 }
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 mbedtls_pem_free(&pem);
1606 return ret;
1607 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1608 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1609 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1610 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1611 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1612 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001615
Valerio Setti0d2980f2023-04-05 18:17:13 +02001616#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001617 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001619 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 } else {
1621 ret = mbedtls_pem_read_buffer(&pem,
1622 "-----BEGIN EC PRIVATE KEY-----",
1623 "-----END EC PRIVATE KEY-----",
1624 key, pwd, pwdlen, &len);
1625 }
1626 if (ret == 0) {
1627 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001630 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 pem.buf, pem.buflen,
1632 f_rng, p_rng)) != 0) {
1633 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634 }
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_pem_free(&pem);
1637 return ret;
1638 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1639 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1640 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1641 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1642 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1643 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001645#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001647 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001649 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 } else {
1651 ret = mbedtls_pem_read_buffer(&pem,
1652 "-----BEGIN PRIVATE KEY-----",
1653 "-----END PRIVATE KEY-----",
1654 key, NULL, 0, &len);
1655 }
1656 if (ret == 0) {
1657 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1658 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1659 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001660 }
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 mbedtls_pem_free(&pem);
1663 return ret;
1664 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1665 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001669 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001671 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 } else {
1673 ret = mbedtls_pem_read_buffer(&pem,
1674 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1675 "-----END ENCRYPTED PRIVATE KEY-----",
1676 key, NULL, 0, &len);
1677 }
1678 if (ret == 0) {
1679 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1680 pwd, pwdlen, f_rng, p_rng)) != 0) {
1681 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682 }
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 mbedtls_pem_free(&pem);
1685 return ret;
1686 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1687 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690#else
1691 ((void) pwd);
1692 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694
1695 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001696 * At this point we only know it's not a PEM formatted key. Could be any
1697 * of the known DER encoded private key formats
1698 *
1699 * We try the different DER format parsers to see if one passes without
1700 * error
1701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001704 unsigned char *key_copy;
1705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1707 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1708 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1713 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_platform_zeroize(key_copy, keylen);
1716 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717 }
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (ret == 0) {
1720 return 0;
1721 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 mbedtls_pk_free(pk);
1724 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1727 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1732 if (ret == 0) {
1733 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001734 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 mbedtls_pk_free(pk);
1737 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1742 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1743 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1744 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001745 }
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 mbedtls_pk_free(pk);
1748 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001750
Valerio Setti0d2980f2023-04-05 18:17:13 +02001751#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1753 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001754 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 key, keylen, f_rng, p_rng) == 0) {
1756 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001759#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001760
Valerio Setti4064dbb2023-05-17 15:33:07 +02001761 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_LIGHT isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001762 * it is ok to leave the PK context initialized but not
1763 * freed: It is the caller's responsibility to call pk_init()
1764 * before calling this function, and to call pk_free()
Valerio Setti4064dbb2023-05-17 15:33:07 +02001765 * when it fails. If MBEDTLS_ECP_LIGHT is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001766 * isn't, this leads to mbedtls_pk_free() being called
1767 * twice, once here and once by the caller, but this is
1768 * also ok and in line with the mbedtls_pk_free() calls
1769 * on failed PEM parsing attempts. */
1770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001772}
1773
1774/*
1775 * Parse a public key
1776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001777int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1778 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001779{
Janos Follath24eed8d2019-11-22 13:21:35 +00001780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001781 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001782#if defined(MBEDTLS_RSA_C)
1783 const mbedtls_pk_info_t *pk_info;
1784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001786 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001788#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (keylen == 0) {
1791 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1792 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001793
1794#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001796#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001797 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001799 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 } else {
1801 ret = mbedtls_pem_read_buffer(&pem,
1802 "-----BEGIN RSA PUBLIC KEY-----",
1803 "-----END RSA PUBLIC KEY-----",
1804 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001805 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001806
1807 if (ret == 0) {
1808 p = pem.buf;
1809 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1810 mbedtls_pem_free(&pem);
1811 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1812 }
1813
1814 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1815 mbedtls_pem_free(&pem);
1816 return ret;
1817 }
1818
1819 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1820 mbedtls_pk_free(ctx);
1821 }
1822
1823 mbedtls_pem_free(&pem);
1824 return ret;
1825 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1826 mbedtls_pem_free(&pem);
1827 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001828 }
1829#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001830
1831 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001833 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 } else {
1835 ret = mbedtls_pem_read_buffer(&pem,
1836 "-----BEGIN PUBLIC KEY-----",
1837 "-----END PUBLIC KEY-----",
1838 key, NULL, 0, &len);
1839 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001842 /*
1843 * Was PEM encoded
1844 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001845 p = pem.buf;
1846
Jethro Beekman01672442023-04-19 14:08:14 +02001847 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 mbedtls_pem_free(&pem);
1849 return ret;
1850 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1851 mbedtls_pem_free(&pem);
1852 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001856
1857#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1859 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001860 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001861
1862 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1863 return ret;
1864 }
1865
1866 p = (unsigned char *) key;
1867 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1868 if (ret == 0) {
1869 return ret;
1870 }
1871 mbedtls_pk_free(ctx);
1872 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1873 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1874 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001875 }
1876#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001877 p = (unsigned char *) key;
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001882}
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884#endif /* MBEDTLS_PK_PARSE_C */