blob: 18b40ceb84ab46eb4ed334a664ef7e0cc51310da [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));
645 /* Setting largest masks for usage and key algorithms */
646 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
647 PSA_KEY_USAGE_SIGN_MESSAGE |
648 PSA_KEY_USAGE_EXPORT);
649#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
650 psa_set_key_algorithm(&attributes,
651 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
652#else
653 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
654#endif
655
656 status = psa_import_key(&attributes, key, len, &pk->priv_id);
657 if (status != PSA_SUCCESS) {
658 ret = psa_pk_status_to_mbedtls(status);
659 return ret;
660 }
661#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
662 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
663
Jethro Beekman01672442023-04-19 14:08:14 +0200664 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
665 mbedtls_ecp_keypair_free(eck);
666 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
667 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200668#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200669
Valerio Setti4064dbb2023-05-17 15:33:07 +0200670 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
671 * which never contain a public key. As such, derive the public key
672 * unconditionally. */
673 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200674#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200675 mbedtls_ecp_keypair_free(eck);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200676#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200677 return ret;
678 }
679
Valerio Setti00e8dd12023-05-18 18:56:59 +0200680 /* When MBEDTLS_PK_USE_PSA_EC_DATA the key is checked while importing it
681 * into PSA. */
682#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200683 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
684 mbedtls_ecp_keypair_free(eck);
685 return ret;
686 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200687#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200688
689 return 0;
690}
691#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200692
693#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
694/*
695 * Create a temporary ecp_keypair for converting an EC point in compressed
696 * format to an uncompressed one
697 */
698static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
699 const unsigned char *in_start, size_t in_len,
700 size_t *out_buf_len, unsigned char *out_buf,
701 size_t out_buf_size)
702{
703 mbedtls_ecp_keypair ecp_key;
704 mbedtls_ecp_group_id ecp_group_id;
705 int ret;
706
707 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
708
709 mbedtls_ecp_keypair_init(&ecp_key);
710 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
711 if (ret != 0) {
712 return ret;
713 }
714 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
715 in_start, in_len);
716 if (ret != 0) {
717 goto exit;
718 }
719 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
720 MBEDTLS_ECP_PF_UNCOMPRESSED,
721 out_buf_len, out_buf, out_buf_size);
722
723exit:
724 mbedtls_ecp_keypair_free(&ecp_key);
725 return ret;
726}
727#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200728
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729/*
730 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100731 *
732 * The caller is responsible for clearing the structure upon failure if
733 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200737 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738{
Janos Follath24eed8d2019-11-22 13:21:35 +0000739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740
Valerio Setti4064dbb2023-05-17 15:33:07 +0200741#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
742 mbedtls_svc_key_id_t key;
743 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
744 size_t len = (end - *p);
745
746 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
747 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748 }
749
Valerio Setti4064dbb2023-05-17 15:33:07 +0200750 /* Compressed point format are not supported yet by PSA crypto. As a
751 * consequence ecp functions are used to "convert" the point to
752 * uncompressed format */
753 if ((**p == 0x02) || (**p == 0x03)) {
754 ret = pk_convert_compressed_ec(pk, *p, len,
755 &(pk->pub_raw_len), pk->pub_raw,
756 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
757 if (ret != 0) {
758 return ret;
759 }
760 } else {
761 /* Uncompressed format */
762 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200763 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200764 }
765 memcpy(pk->pub_raw, *p, (end - *p));
766 pk->pub_raw_len = end - *p;
767 }
768
769 /* Validate the key by trying to importing it */
770 psa_set_key_usage_flags(&key_attrs, 0);
771 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
772 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
773 psa_set_key_bits(&key_attrs, pk->ec_bits);
774
775 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
776 &key) != PSA_SUCCESS) ||
777 (psa_destroy_key(key) != PSA_SUCCESS)) {
778 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
779 pk->pub_raw_len = 0;
780 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
781 }
782 ret = 0;
783#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
784 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
785 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
786 (const unsigned char *) *p,
787 end - *p)) == 0) {
788 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
789 }
790#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
791
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794 */
795 *p = (unsigned char *) end;
796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200798}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200799#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802/*
803 * RSAPublicKey ::= SEQUENCE {
804 * modulus INTEGER, -- n
805 * publicExponent INTEGER -- e
806 * }
807 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808static int pk_get_rsapubkey(unsigned char **p,
809 const unsigned char *end,
810 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811{
Janos Follath24eed8d2019-11-22 13:21:35 +0000812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813 size_t len;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
816 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
817 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
818 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (*p + len != end) {
821 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
822 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
823 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
827 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
828 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
831 NULL, 0, NULL, 0)) != 0) {
832 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
835 *p += len;
836
837 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
840 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
843 NULL, 0, *p, len)) != 0) {
844 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
845 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
847 *p += len;
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (mbedtls_rsa_complete(rsa) != 0 ||
850 mbedtls_rsa_check_pubkey(rsa) != 0) {
851 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000852 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (*p != end) {
855 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
857 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862
863/* Get a PK algorithm identifier
864 *
865 * AlgorithmIdentifier ::= SEQUENCE {
866 * algorithm OBJECT IDENTIFIER,
867 * parameters ANY DEFINED BY algorithm OPTIONAL }
868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int pk_get_pk_alg(unsigned char **p,
870 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200871 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
872 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873{
Janos Follath24eed8d2019-11-22 13:21:35 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
880 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
881 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Jethro Beekman01672442023-04-19 14:08:14 +0200883 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
884#if defined(MBEDTLS_ECP_LIGHT)
885 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
886 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
887 if (ret == 0) {
888 *pk_alg = MBEDTLS_PK_ECKEY;
889 }
890 }
891#else
892 (void) ec_grp_id;
893#endif
894 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
896 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
898 /*
899 * No parameters with RSA (only for EC)
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (*pk_alg == MBEDTLS_PK_RSA &&
902 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
903 params->len != 0)) {
904 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908}
909
910/*
911 * SubjectPublicKeyInfo ::= SEQUENCE {
912 * algorithm AlgorithmIdentifier,
913 * subjectPublicKey BIT STRING }
914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
916 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917{
Janos Follath24eed8d2019-11-22 13:21:35 +0000918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_asn1_buf alg_params;
921 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200922 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
926 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
927 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928 }
929
930 end = *p + len;
931
Jethro Beekman01672442023-04-19 14:08:14 +0200932 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return ret;
934 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
937 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
938 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (*p + len != end) {
941 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
942 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
943 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
946 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
947 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
950 return ret;
951 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (pk_alg == MBEDTLS_PK_RSA) {
955 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200958#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200960#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200961 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200962 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200963 } else
964#endif
965 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200966 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200967 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200969 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200972#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (ret == 0 && *p != end) {
976 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
977 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
978 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (ret != 0) {
981 mbedtls_pk_free(pk);
982 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985}
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100989 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
990 *
991 * The value zero is:
992 * - never a valid value for an RSA parameter
993 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
994 *
995 * Since values can't be omitted in PKCS#1, passing a zero value to
996 * rsa_complete() would be incorrect, so reject zero values early.
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static int asn1_get_nonzero_mpi(unsigned char **p,
999 const unsigned char *end,
1000 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001001{
1002 int ret;
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 ret = mbedtls_asn1_get_mpi(p, end, X);
1005 if (ret != 0) {
1006 return ret;
1007 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1010 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1011 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001014}
1015
1016/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 * Parse a PKCS#1 encoded private RSA key
1018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1020 const unsigned char *key,
1021 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001023 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 size_t len;
1025 unsigned char *p, *end;
1026
Hanno Beckerefa14e82017-10-11 19:45:19 +01001027 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001029
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 p = (unsigned char *) key;
1031 end = p + keylen;
1032
1033 /*
1034 * This function parses the RSAPrivateKey (PKCS#1)
1035 *
1036 * RSAPrivateKey ::= SEQUENCE {
1037 * version Version,
1038 * modulus INTEGER, -- n
1039 * publicExponent INTEGER, -- e
1040 * privateExponent INTEGER, -- d
1041 * prime1 INTEGER, -- p
1042 * prime2 INTEGER, -- q
1043 * exponent1 INTEGER, -- d mod (p-1)
1044 * exponent2 INTEGER, -- d mod (q-1)
1045 * coefficient INTEGER, -- (inverse of q) mod p
1046 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1047 * }
1048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1050 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1051 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052 }
1053
1054 end = p + len;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1057 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 }
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (version != 0) {
1061 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 }
1063
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1066 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1067 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001071 /* Import E */
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 NULL, &T)) != 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 D */
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, NULL, NULL,
1081 &T, 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 P */
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, &T, NULL,
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
1092 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1094 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1095 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001096 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001098
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001099#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001100 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1102 * that they can be easily recomputed from D, P and Q. However by
1103 * parsing them from the PKCS1 structure it is possible to avoid
1104 * recalculating them which both reduces the overhead of loading
1105 * RSA private keys into memory and also avoids side channels which
1106 * can arise when computing those values, since all of D, P, and Q
1107 * are secret. See https://eprint.iacr.org/2020/055 for a
1108 * description of one such attack.
1109 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001110
Jack Lloyd80cc8112020-01-22 17:34:29 -05001111 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1113 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1114 goto cleanup;
1115 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001116
1117 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1119 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1120 goto cleanup;
1121 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001122
1123 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1126 goto cleanup;
1127 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001128
Jack Lloyd60239752020-01-27 17:53:36 -05001129#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001130 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1132 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1133 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1134 goto cleanup;
1135 }
Jack Lloyd60239752020-01-27 17:53:36 -05001136#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001137
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001138 /* rsa_complete() doesn't complete anything with the default
1139 * implementation but is still called:
1140 * - for the benefit of alternative implementation that may want to
1141 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1142 * - as is also sanity-checks the key
1143 *
1144 * Furthermore, we also check the public part for consistency with
1145 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1148 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001149 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001150 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (p != end) {
1153 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1154 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155 }
1156
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001157cleanup:
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001162 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if ((ret & 0xff80) == 0) {
1164 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1165 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001166 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Valerio Setti0d2980f2023-04-05 18:17:13 +02001176#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177/*
1178 * Parse a SEC1 encoded private EC key
1179 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001180static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 const unsigned char *key, size_t keylen,
1182 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183{
Janos Follath24eed8d2019-11-22 13:21:35 +00001184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001185 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001186 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001187 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001189 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 unsigned char *end = p + keylen;
1191 unsigned char *end2;
Valerio Setti4064dbb2023-05-17 15:33:07 +02001192 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001193#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1195 psa_status_t status;
1196 uint8_t priv_key_raw[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
1197 size_t priv_key_len;
1198#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199
1200 /*
1201 * RFC 5915, or SEC1 Appendix C.4
1202 *
1203 * ECPrivateKey ::= SEQUENCE {
1204 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1205 * privateKey OCTET STRING,
1206 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1207 * publicKey [1] BIT STRING OPTIONAL
1208 * }
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1211 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 }
1214
1215 end = p + len;
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1219 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (version != 1) {
1222 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1223 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1227 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Jethro Beekman01672442023-04-19 14:08:14 +02001229 d = p;
1230 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001231
1232#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1233 if (len > MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH) {
1234 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1235 }
1236 memcpy(priv_key_raw, p, len);
1237 priv_key_len = len;
1238#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1240 mbedtls_ecp_keypair_free(eck);
1241 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001243#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244
1245 p += len;
1246
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001247 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249 /*
1250 * Is 'parameters' present?
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1253 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1254 0)) == 0) {
1255 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001256 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_ecp_keypair_free(eck);
1258 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1261 mbedtls_ecp_keypair_free(eck);
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001263 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001264 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001267 /*
1268 * Is 'publickey' present? If not, or if we can't read it (eg because it
1269 * is compressed), create it from the private key.
1270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1272 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1273 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001274 end2 = p + len;
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1277 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1278 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (p + len != end2) {
1281 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1282 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1283 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001284
Valerio Setti4064dbb2023-05-17 15:33:07 +02001285 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001286 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001288 /*
1289 * The only acceptable failure mode of pk_get_ecpubkey() above
1290 * is if the point format is not recognized.
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1293 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1294 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1297 mbedtls_ecp_keypair_free(eck);
1298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001299 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001301
Valerio Setti00e8dd12023-05-18 18:56:59 +02001302#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1303 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1304 /* Setting largest masks for usage and key algorithms */
1305 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1306 PSA_KEY_USAGE_SIGN_MESSAGE |
1307 PSA_KEY_USAGE_EXPORT);
1308#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1309 psa_set_key_algorithm(&attributes,
1310 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1311#else
1312 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1313#endif
1314
1315 status = psa_import_key(&attributes, priv_key_raw, priv_key_len,
1316 &pk->priv_id);
1317 if (status != PSA_SUCCESS) {
1318 ret = psa_pk_status_to_mbedtls(status);
1319 return ret;
1320 }
1321#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1322
Valerio Setti34f67552023-04-03 15:19:18 +02001323 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001324 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001325 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001326 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001327 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001328 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
Valerio Setti00e8dd12023-05-18 18:56:59 +02001330#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1332 mbedtls_ecp_keypair_free(eck);
1333 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001335#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001339#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340
1341/*
1342 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001343 *
1344 * Notes:
1345 *
1346 * - This function does not own the key buffer. It is the
1347 * responsibility of the caller to take care of zeroizing
1348 * and freeing it after use.
1349 *
1350 * - The function is responsible for freeing the provided
1351 * PK context on failure.
1352 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353 */
1354static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 mbedtls_pk_context *pk,
1356 const unsigned char *key, size_t keylen,
1357 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358{
1359 int ret, version;
1360 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 unsigned char *p = (unsigned char *) key;
1363 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001365 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
Jethro Beekman01672442023-04-19 14:08:14 +02001368#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001369 (void) f_rng;
1370 (void) p_rng;
1371#endif
1372
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001374 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 *
1376 * PrivateKeyInfo ::= SEQUENCE {
1377 * version Version,
1378 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1379 * privateKey PrivateKey,
1380 * attributes [0] IMPLICIT Attributes OPTIONAL }
1381 *
1382 * Version ::= INTEGER
1383 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1384 * PrivateKey ::= OCTET STRING
1385 *
1386 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1387 */
1388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1390 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 }
1393
1394 end = p + len;
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1397 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001398 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (version != 0) {
1401 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1402 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Jethro Beekman01672442023-04-19 14:08:14 +02001404 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return ret;
1406 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1409 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1410 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (len < 1) {
1413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1414 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1415 }
1416
1417 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1418 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1419 }
1420
1421 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1422 return ret;
1423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (pk_alg == MBEDTLS_PK_RSA) {
1427 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1428 mbedtls_pk_free(pk);
1429 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 }
1431 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001433#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001435#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001436 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001437 if ((ret =
1438 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001439 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001440 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001441 p_rng)) != 0) {
1442 mbedtls_pk_free(pk);
1443 return ret;
1444 }
1445 } else
1446#endif
1447 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001448 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1449 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001450 mbedtls_pk_free(pk);
1451 return ret;
1452 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 }
1454 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001455#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459}
1460
1461/*
1462 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001463 *
1464 * To save space, the decryption happens in-place on the given key buffer.
1465 * Also, while this function may modify the keybuffer, it doesn't own it,
1466 * and instead it is the responsibility of the caller to zeroize and properly
1467 * free it after use.
1468 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 mbedtls_pk_context *pk,
1473 unsigned char *key, size_t keylen,
1474 const unsigned char *pwd, size_t pwdlen,
1475 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001477 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001479 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1482#if defined(MBEDTLS_PKCS12_C)
1483 mbedtls_cipher_type_t cipher_alg;
1484 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485#endif
1486
Hanno Becker2aa80a72017-09-07 15:28:45 +01001487 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488 end = p + keylen;
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (pwdlen == 0) {
1491 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1492 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
1494 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001495 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 *
1497 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1498 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1499 * encryptedData EncryptedData
1500 * }
1501 *
1502 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1503 *
1504 * EncryptedData ::= OCTET STRING
1505 *
1506 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001507 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1510 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001512 }
1513
1514 end = p + len;
1515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1517 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1518 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1521 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1522 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523
Hanno Beckerfab35692017-08-25 13:38:26 +01001524 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
1526 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001527 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1531 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1532 cipher_alg, md_alg,
1533 pwd, pwdlen, p, len, buf)) != 0) {
1534 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1535 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1536 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001540
1541 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_PKCS12_C */
1544#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1546 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1547 p, len, buf)) != 0) {
1548 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1549 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1550 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001554
1555 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001558 {
1559 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001560 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (decrypted == 0) {
1563 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1564 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001569
1570/*
1571 * Parse a private key
1572 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1574 const unsigned char *key, size_t keylen,
1575 const unsigned char *pwd, size_t pwdlen,
1576 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577{
Janos Follath24eed8d2019-11-22 13:21:35 +00001578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001581 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001583#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (keylen == 0) {
1586 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1587 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001588
1589#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001593 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001595 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 } else {
1597 ret = mbedtls_pem_read_buffer(&pem,
1598 "-----BEGIN RSA PRIVATE KEY-----",
1599 "-----END RSA PRIVATE KEY-----",
1600 key, pwd, pwdlen, &len);
1601 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (ret == 0) {
1604 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1605 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1606 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1607 pem.buf, pem.buflen)) != 0) {
1608 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001609 }
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 mbedtls_pem_free(&pem);
1612 return ret;
1613 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1614 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1615 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1616 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1617 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1618 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621
Valerio Setti0d2980f2023-04-05 18:17:13 +02001622#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001623 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001625 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 } else {
1627 ret = mbedtls_pem_read_buffer(&pem,
1628 "-----BEGIN EC PRIVATE KEY-----",
1629 "-----END EC PRIVATE KEY-----",
1630 key, pwd, pwdlen, &len);
1631 }
1632 if (ret == 0) {
1633 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001636 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 pem.buf, pem.buflen,
1638 f_rng, p_rng)) != 0) {
1639 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640 }
1641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_pem_free(&pem);
1643 return ret;
1644 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1645 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1646 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1647 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1648 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1649 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001650 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001651#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001653 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001655 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 } else {
1657 ret = mbedtls_pem_read_buffer(&pem,
1658 "-----BEGIN PRIVATE KEY-----",
1659 "-----END PRIVATE KEY-----",
1660 key, NULL, 0, &len);
1661 }
1662 if (ret == 0) {
1663 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1664 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1665 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666 }
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 mbedtls_pem_free(&pem);
1669 return ret;
1670 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1671 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001672 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001675 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001677 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 } else {
1679 ret = mbedtls_pem_read_buffer(&pem,
1680 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1681 "-----END ENCRYPTED PRIVATE KEY-----",
1682 key, NULL, 0, &len);
1683 }
1684 if (ret == 0) {
1685 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1686 pwd, pwdlen, f_rng, p_rng)) != 0) {
1687 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 mbedtls_pem_free(&pem);
1691 return ret;
1692 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1693 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696#else
1697 ((void) pwd);
1698 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700
1701 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001702 * At this point we only know it's not a PEM formatted key. Could be any
1703 * of the known DER encoded private key formats
1704 *
1705 * We try the different DER format parsers to see if one passes without
1706 * error
1707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001710 unsigned char *key_copy;
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1713 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1714 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1719 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 mbedtls_platform_zeroize(key_copy, keylen);
1722 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001723 }
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (ret == 0) {
1726 return 0;
1727 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 mbedtls_pk_free(pk);
1730 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1733 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1738 if (ret == 0) {
1739 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001740 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 mbedtls_pk_free(pk);
1743 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1748 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1749 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1750 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751 }
1752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 mbedtls_pk_free(pk);
1754 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001756
Valerio Setti0d2980f2023-04-05 18:17:13 +02001757#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1759 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001760 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 key, keylen, f_rng, p_rng) == 0) {
1762 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001763 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001765#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001766
Valerio Setti4064dbb2023-05-17 15:33:07 +02001767 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_LIGHT isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001768 * it is ok to leave the PK context initialized but not
1769 * freed: It is the caller's responsibility to call pk_init()
1770 * before calling this function, and to call pk_free()
Valerio Setti4064dbb2023-05-17 15:33:07 +02001771 * when it fails. If MBEDTLS_ECP_LIGHT is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001772 * isn't, this leads to mbedtls_pk_free() being called
1773 * twice, once here and once by the caller, but this is
1774 * also ok and in line with the mbedtls_pk_free() calls
1775 * on failed PEM parsing attempts. */
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001778}
1779
1780/*
1781 * Parse a public key
1782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1784 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001785{
Janos Follath24eed8d2019-11-22 13:21:35 +00001786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001787 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001788#if defined(MBEDTLS_RSA_C)
1789 const mbedtls_pk_info_t *pk_info;
1790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001792 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001794#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (keylen == 0) {
1797 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1798 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001799
1800#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001802#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001803 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001805 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 } else {
1807 ret = mbedtls_pem_read_buffer(&pem,
1808 "-----BEGIN RSA PUBLIC KEY-----",
1809 "-----END RSA PUBLIC KEY-----",
1810 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001812
1813 if (ret == 0) {
1814 p = pem.buf;
1815 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1816 mbedtls_pem_free(&pem);
1817 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1818 }
1819
1820 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1821 mbedtls_pem_free(&pem);
1822 return ret;
1823 }
1824
1825 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1826 mbedtls_pk_free(ctx);
1827 }
1828
1829 mbedtls_pem_free(&pem);
1830 return ret;
1831 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1832 mbedtls_pem_free(&pem);
1833 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001834 }
1835#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001836
1837 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001839 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 } else {
1841 ret = mbedtls_pem_read_buffer(&pem,
1842 "-----BEGIN PUBLIC KEY-----",
1843 "-----END PUBLIC KEY-----",
1844 key, NULL, 0, &len);
1845 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001848 /*
1849 * Was PEM encoded
1850 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001851 p = pem.buf;
1852
Jethro Beekman01672442023-04-19 14:08:14 +02001853 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 mbedtls_pem_free(&pem);
1855 return ret;
1856 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1857 mbedtls_pem_free(&pem);
1858 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001862
1863#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1865 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001866 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001867
1868 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1869 return ret;
1870 }
1871
1872 p = (unsigned char *) key;
1873 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1874 if (ret == 0) {
1875 return ret;
1876 }
1877 mbedtls_pk_free(ctx);
1878 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1879 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1880 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001881 }
1882#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001883 p = (unsigned char *) key;
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001888}
1889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#endif /* MBEDTLS_PK_PARSE_C */