blob: c2ececb44c4216ad1f20d20771f1c88a223df283 [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 Setti4064dbb2023-05-17 15:33:07 +0200517 /* grp may already be initialized; if so, make sure IDs match */
518 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
519 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
521 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522
Valerio Setti4064dbb2023-05-17 15:33:07 +0200523 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
524 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return ret;
526 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200527#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
528 ret = pk_update_psa_ecparams(pk, grp_id);
529#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530
Valerio Setti4064dbb2023-05-17 15:33:07 +0200531 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200532}
533
Jethro Beekman01672442023-04-19 14:08:14 +0200534/*
535 * Helper function for deriving a public key from its private counterpart.
536 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200537static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200538 const unsigned char *d, size_t d_len,
539 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
540{
541 int ret;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200542 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200543#if defined(MBEDTLS_USE_PSA_CRYPTO)
544 psa_status_t status, destruction_status;
545 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
546 size_t curve_bits;
547 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200548#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200549 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
550 size_t key_len;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200551#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200552 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
553
554 (void) f_rng;
555 (void) p_rng;
556
557 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
558 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
559
560 status = psa_import_key(&key_attr, d, d_len, &key_id);
561 ret = psa_pk_status_to_mbedtls(status);
562 if (ret != 0) {
563 return ret;
564 }
565
Valerio Setti4064dbb2023-05-17 15:33:07 +0200566#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
567 status = psa_export_public_key(key_id, pk->pub_raw, sizeof(pk->pub_raw),
568 &pk->pub_raw_len);
569#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200570 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200571#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200572 ret = psa_pk_status_to_mbedtls(status);
573 destruction_status = psa_destroy_key(key_id);
574 if (ret != 0) {
575 return ret;
576 } else if (destruction_status != PSA_SUCCESS) {
577 return psa_pk_status_to_mbedtls(destruction_status);
578 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200579#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200580 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200581#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200582#else /* MBEDTLS_USE_PSA_CRYPTO */
583 (void) d;
584 (void) d_len;
585
586 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
587#endif /* MBEDTLS_USE_PSA_CRYPTO */
588 return ret;
589}
590
591#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
592
593/*
594 * Load an RFC8410 EC key, which doesn't have any parameters
595 */
596static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
597 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200598 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200599{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200600 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
601 int ret;
602
Jethro Beekman01672442023-04-19 14:08:14 +0200603 if (params->tag != 0 || params->len != 0) {
604 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
605 }
606
Valerio Setti4064dbb2023-05-17 15:33:07 +0200607 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
608 if (ret != 0) {
609 return ret;
610 }
611
612#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
613 ret = pk_update_psa_ecparams(pk, grp_id);
614#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
615 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200616}
617
618/*
619 * Parse an RFC 8410 encoded private EC key
620 *
621 * CurvePrivateKey ::= OCTET STRING
622 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200623static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200624 unsigned char *key, size_t keylen, const unsigned char *end,
625 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
626{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200627 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
629 size_t len;
630
631 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
632 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
633 }
634
635 if (key + len != end) {
636 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
637 }
638
639 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
640 mbedtls_ecp_keypair_free(eck);
641 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
642 }
643
Valerio Setti4064dbb2023-05-17 15:33:07 +0200644 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
645 * which never contain a public key. As such, derive the public key
646 * unconditionally. */
647 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200648 mbedtls_ecp_keypair_free(eck);
649 return ret;
650 }
651
652 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
653 mbedtls_ecp_keypair_free(eck);
654 return ret;
655 }
656
657 return 0;
658}
659#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200660
661#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
662/*
663 * Create a temporary ecp_keypair for converting an EC point in compressed
664 * format to an uncompressed one
665 */
666static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
667 const unsigned char *in_start, size_t in_len,
668 size_t *out_buf_len, unsigned char *out_buf,
669 size_t out_buf_size)
670{
671 mbedtls_ecp_keypair ecp_key;
672 mbedtls_ecp_group_id ecp_group_id;
673 int ret;
674
675 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
676
677 mbedtls_ecp_keypair_init(&ecp_key);
678 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
679 if (ret != 0) {
680 return ret;
681 }
682 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
683 in_start, in_len);
684 if (ret != 0) {
685 goto exit;
686 }
687 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
688 MBEDTLS_ECP_PF_UNCOMPRESSED,
689 out_buf_len, out_buf, out_buf_size);
690
691exit:
692 mbedtls_ecp_keypair_free(&ecp_key);
693 return ret;
694}
695#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200696
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697/*
698 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100699 *
700 * The caller is responsible for clearing the structure upon failure if
701 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200705 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706{
Janos Follath24eed8d2019-11-22 13:21:35 +0000707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708
Valerio Setti4064dbb2023-05-17 15:33:07 +0200709#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
710 mbedtls_svc_key_id_t key;
711 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
712 size_t len = (end - *p);
713
714 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
715 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716 }
717
Valerio Setti4064dbb2023-05-17 15:33:07 +0200718 /* Compressed point format are not supported yet by PSA crypto. As a
719 * consequence ecp functions are used to "convert" the point to
720 * uncompressed format */
721 if ((**p == 0x02) || (**p == 0x03)) {
722 ret = pk_convert_compressed_ec(pk, *p, len,
723 &(pk->pub_raw_len), pk->pub_raw,
724 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
725 if (ret != 0) {
726 return ret;
727 }
728 } else {
729 /* Uncompressed format */
730 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200731 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200732 }
733 memcpy(pk->pub_raw, *p, (end - *p));
734 pk->pub_raw_len = end - *p;
735 }
736
737 /* Validate the key by trying to importing it */
738 psa_set_key_usage_flags(&key_attrs, 0);
739 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
740 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
741 psa_set_key_bits(&key_attrs, pk->ec_bits);
742
743 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
744 &key) != PSA_SUCCESS) ||
745 (psa_destroy_key(key) != PSA_SUCCESS)) {
746 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
747 pk->pub_raw_len = 0;
748 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
749 }
750 ret = 0;
751#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
752 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
753 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
754 (const unsigned char *) *p,
755 end - *p)) == 0) {
756 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
757 }
758#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
759
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762 */
763 *p = (unsigned char *) end;
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200767#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770/*
771 * RSAPublicKey ::= SEQUENCE {
772 * modulus INTEGER, -- n
773 * publicExponent INTEGER -- e
774 * }
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776static int pk_get_rsapubkey(unsigned char **p,
777 const unsigned char *end,
778 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779{
Janos Follath24eed8d2019-11-22 13:21:35 +0000780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781 size_t len;
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
784 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
786 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (*p + len != end) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
791 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
796 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
799 NULL, 0, NULL, 0)) != 0) {
800 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
801 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802
803 *p += len;
804
805 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
807 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
808 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
811 NULL, 0, *p, len)) != 0) {
812 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
813 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814
815 *p += len;
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (mbedtls_rsa_complete(rsa) != 0 ||
818 mbedtls_rsa_check_pubkey(rsa) != 0) {
819 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000820 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (*p != end) {
823 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
824 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
825 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830
831/* Get a PK algorithm identifier
832 *
833 * AlgorithmIdentifier ::= SEQUENCE {
834 * algorithm OBJECT IDENTIFIER,
835 * parameters ANY DEFINED BY algorithm OPTIONAL }
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837static int pk_get_pk_alg(unsigned char **p,
838 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200839 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
840 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841{
Janos Follath24eed8d2019-11-22 13:21:35 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
849 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Jethro Beekman01672442023-04-19 14:08:14 +0200851 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
852#if defined(MBEDTLS_ECP_LIGHT)
853 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
854 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
855 if (ret == 0) {
856 *pk_alg = MBEDTLS_PK_ECKEY;
857 }
858 }
859#else
860 (void) ec_grp_id;
861#endif
862 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
864 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865
866 /*
867 * No parameters with RSA (only for EC)
868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (*pk_alg == MBEDTLS_PK_RSA &&
870 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
871 params->len != 0)) {
872 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 }
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876}
877
878/*
879 * SubjectPublicKeyInfo ::= SEQUENCE {
880 * algorithm AlgorithmIdentifier,
881 * subjectPublicKey BIT STRING }
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
884 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885{
Janos Follath24eed8d2019-11-22 13:21:35 +0000886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_asn1_buf alg_params;
889 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200890 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
894 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
895 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896 }
897
898 end = *p + len;
899
Jethro Beekman01672442023-04-19 14:08:14 +0200900 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 return ret;
902 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
905 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
906 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (*p + len != end) {
909 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
910 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
911 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
914 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
915 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
918 return ret;
919 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (pk_alg == MBEDTLS_PK_RSA) {
923 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200924 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200926#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200928#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
929 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200930 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200931 } else
932#endif
933 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200934 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200935 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200937 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200940#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (ret == 0 && *p != end) {
944 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
945 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
946 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ret != 0) {
949 mbedtls_pk_free(pk);
950 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953}
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100957 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
958 *
959 * The value zero is:
960 * - never a valid value for an RSA parameter
961 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
962 *
963 * Since values can't be omitted in PKCS#1, passing a zero value to
964 * rsa_complete() would be incorrect, so reject zero values early.
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static int asn1_get_nonzero_mpi(unsigned char **p,
967 const unsigned char *end,
968 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100969{
970 int ret;
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 ret = mbedtls_asn1_get_mpi(p, end, X);
973 if (ret != 0) {
974 return ret;
975 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
978 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
979 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982}
983
984/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985 * Parse a PKCS#1 encoded private RSA key
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
988 const unsigned char *key,
989 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100991 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 size_t len;
993 unsigned char *p, *end;
994
Hanno Beckerefa14e82017-10-11 19:45:19 +0100995 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100997
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 p = (unsigned char *) key;
999 end = p + keylen;
1000
1001 /*
1002 * This function parses the RSAPrivateKey (PKCS#1)
1003 *
1004 * RSAPrivateKey ::= SEQUENCE {
1005 * version Version,
1006 * modulus INTEGER, -- n
1007 * publicExponent INTEGER, -- e
1008 * privateExponent INTEGER, -- d
1009 * prime1 INTEGER, -- p
1010 * prime2 INTEGER, -- q
1011 * exponent1 INTEGER, -- d mod (p-1)
1012 * exponent2 INTEGER, -- d mod (q-1)
1013 * coefficient INTEGER, -- (inverse of q) mod p
1014 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1015 * }
1016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1018 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1019 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
1022 end = p + len;
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1025 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 }
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (version != 0) {
1029 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 }
1031
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001032 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1034 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1035 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001036 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1041 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1042 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001043 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045
1046 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1048 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1049 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052
1053 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1055 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1056 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059
1060 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1062 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1063 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001067#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001068 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1070 * that they can be easily recomputed from D, P and Q. However by
1071 * parsing them from the PKCS1 structure it is possible to avoid
1072 * recalculating them which both reduces the overhead of loading
1073 * RSA private keys into memory and also avoids side channels which
1074 * can arise when computing those values, since all of D, P, and Q
1075 * are secret. See https://eprint.iacr.org/2020/055 for a
1076 * description of one such attack.
1077 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001078
Jack Lloyd80cc8112020-01-22 17:34:29 -05001079 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1081 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1082 goto cleanup;
1083 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084
1085 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1088 goto cleanup;
1089 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001090
1091 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1093 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1094 goto cleanup;
1095 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001096
Jack Lloyd60239752020-01-27 17:53:36 -05001097#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001098 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1100 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1101 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1102 goto cleanup;
1103 }
Jack Lloyd60239752020-01-27 17:53:36 -05001104#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001105
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001106 /* rsa_complete() doesn't complete anything with the default
1107 * implementation but is still called:
1108 * - for the benefit of alternative implementation that may want to
1109 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1110 * - as is also sanity-checks the key
1111 *
1112 * Furthermore, we also check the public part for consistency with
1113 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1116 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001117 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001118 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (p != end) {
1121 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1122 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 }
1124
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001125cleanup:
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001130 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret & 0xff80) == 0) {
1132 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1133 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001134 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Valerio Setti0d2980f2023-04-05 18:17:13 +02001144#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145/*
1146 * Parse a SEC1 encoded private EC key
1147 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001148static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 const unsigned char *key, size_t keylen,
1150 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151{
Janos Follath24eed8d2019-11-22 13:21:35 +00001152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001153 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001154 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001155 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001157 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 unsigned char *end = p + keylen;
1159 unsigned char *end2;
Valerio Setti4064dbb2023-05-17 15:33:07 +02001160 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161
1162 /*
1163 * RFC 5915, or SEC1 Appendix C.4
1164 *
1165 * ECPrivateKey ::= SEQUENCE {
1166 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1167 * privateKey OCTET STRING,
1168 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1169 * publicKey [1] BIT STRING OPTIONAL
1170 * }
1171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1173 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 }
1176
1177 end = p + len;
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1180 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1181 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (version != 1) {
1184 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1185 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1189 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
Jethro Beekman01672442023-04-19 14:08:14 +02001191 d = p;
1192 d_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1194 mbedtls_ecp_keypair_free(eck);
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196 }
1197
1198 p += len;
1199
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001200 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202 /*
1203 * Is 'parameters' present?
1204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1206 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1207 0)) == 0) {
1208 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001209 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_ecp_keypair_free(eck);
1211 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1214 mbedtls_ecp_keypair_free(eck);
1215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001216 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001217 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001220 /*
1221 * Is 'publickey' present? If not, or if we can't read it (eg because it
1222 * is compressed), create it from the private key.
1223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1225 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1226 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001227 end2 = p + len;
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1231 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (p + len != end2) {
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1236 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237
Valerio Setti4064dbb2023-05-17 15:33:07 +02001238 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001239 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001241 /*
1242 * The only acceptable failure mode of pk_get_ecpubkey() above
1243 * is if the point format is not recognized.
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1247 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001248 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1250 mbedtls_ecp_keypair_free(eck);
1251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001252 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001254
Valerio Setti34f67552023-04-03 15:19:18 +02001255 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001256 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001257 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001258 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001259 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001260 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1263 mbedtls_ecp_keypair_free(eck);
1264 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 }
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001269#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270
1271/*
1272 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001273 *
1274 * Notes:
1275 *
1276 * - This function does not own the key buffer. It is the
1277 * responsibility of the caller to take care of zeroizing
1278 * and freeing it after use.
1279 *
1280 * - The function is responsible for freeing the provided
1281 * PK context on failure.
1282 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 */
1284static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 mbedtls_pk_context *pk,
1286 const unsigned char *key, size_t keylen,
1287 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288{
1289 int ret, version;
1290 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292 unsigned char *p = (unsigned char *) key;
1293 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001295 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Jethro Beekman01672442023-04-19 14:08:14 +02001298#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001299 (void) f_rng;
1300 (void) p_rng;
1301#endif
1302
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001304 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 *
1306 * PrivateKeyInfo ::= SEQUENCE {
1307 * version Version,
1308 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1309 * privateKey PrivateKey,
1310 * attributes [0] IMPLICIT Attributes OPTIONAL }
1311 *
1312 * Version ::= INTEGER
1313 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1314 * PrivateKey ::= OCTET STRING
1315 *
1316 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1317 */
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1320 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1321 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 }
1323
1324 end = p + len;
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001328 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (version != 0) {
1331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1332 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333
Jethro Beekman01672442023-04-19 14:08:14 +02001334 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 return ret;
1336 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1340 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (len < 1) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1344 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1345 }
1346
1347 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1348 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1349 }
1350
1351 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1352 return ret;
1353 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (pk_alg == MBEDTLS_PK_RSA) {
1357 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1358 mbedtls_pk_free(pk);
1359 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 }
1361 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001363#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001365#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
1366 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001367 if ((ret =
1368 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001369 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001370 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001371 p_rng)) != 0) {
1372 mbedtls_pk_free(pk);
1373 return ret;
1374 }
1375 } else
1376#endif
1377 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001378 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1379 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001380 mbedtls_pk_free(pk);
1381 return ret;
1382 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383 }
1384 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001385#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389}
1390
1391/*
1392 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001393 *
1394 * To save space, the decryption happens in-place on the given key buffer.
1395 * Also, while this function may modify the keybuffer, it doesn't own it,
1396 * and instead it is the responsibility of the caller to zeroize and properly
1397 * free it after use.
1398 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_pk_context *pk,
1403 unsigned char *key, size_t keylen,
1404 const unsigned char *pwd, size_t pwdlen,
1405 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001407 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001409 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1412#if defined(MBEDTLS_PKCS12_C)
1413 mbedtls_cipher_type_t cipher_alg;
1414 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415#endif
1416
Hanno Becker2aa80a72017-09-07 15:28:45 +01001417 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 end = p + keylen;
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (pwdlen == 0) {
1421 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1422 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
1424 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001425 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 *
1427 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1428 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1429 * encryptedData EncryptedData
1430 * }
1431 *
1432 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1433 *
1434 * EncryptedData ::= OCTET STRING
1435 *
1436 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001437 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 }
1443
1444 end = p + len;
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1447 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1448 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1451 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1452 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453
Hanno Beckerfab35692017-08-25 13:38:26 +01001454 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455
1456 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001457 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1461 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1462 cipher_alg, md_alg,
1463 pwd, pwdlen, p, len, buf)) != 0) {
1464 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1465 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1466 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001470
1471 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PKCS12_C */
1474#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1476 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1477 p, len, buf)) != 0) {
1478 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1479 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1480 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001484
1485 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001488 {
1489 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001490 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (decrypted == 0) {
1493 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1494 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499
1500/*
1501 * Parse a private key
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1504 const unsigned char *key, size_t keylen,
1505 const unsigned char *pwd, size_t pwdlen,
1506 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507{
Janos Follath24eed8d2019-11-22 13:21:35 +00001508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001511 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001513#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (keylen == 0) {
1516 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1517 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001518
1519#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001523 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001525 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 } else {
1527 ret = mbedtls_pem_read_buffer(&pem,
1528 "-----BEGIN RSA PRIVATE KEY-----",
1529 "-----END RSA PRIVATE KEY-----",
1530 key, pwd, pwdlen, &len);
1531 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (ret == 0) {
1534 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1535 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1536 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1537 pem.buf, pem.buflen)) != 0) {
1538 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539 }
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_pem_free(&pem);
1542 return ret;
1543 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1544 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1545 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1546 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1547 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1548 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551
Valerio Setti0d2980f2023-04-05 18:17:13 +02001552#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001553 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001555 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 } else {
1557 ret = mbedtls_pem_read_buffer(&pem,
1558 "-----BEGIN EC PRIVATE KEY-----",
1559 "-----END EC PRIVATE KEY-----",
1560 key, pwd, pwdlen, &len);
1561 }
1562 if (ret == 0) {
1563 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001566 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 pem.buf, pem.buflen,
1568 f_rng, p_rng)) != 0) {
1569 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001570 }
1571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 mbedtls_pem_free(&pem);
1573 return ret;
1574 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1575 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1576 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1577 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1578 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1579 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001580 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001581#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001583 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001585 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 } else {
1587 ret = mbedtls_pem_read_buffer(&pem,
1588 "-----BEGIN PRIVATE KEY-----",
1589 "-----END PRIVATE KEY-----",
1590 key, NULL, 0, &len);
1591 }
1592 if (ret == 0) {
1593 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1594 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1595 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_pem_free(&pem);
1599 return ret;
1600 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1601 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001605 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001607 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else {
1609 ret = mbedtls_pem_read_buffer(&pem,
1610 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1611 "-----END ENCRYPTED PRIVATE KEY-----",
1612 key, NULL, 0, &len);
1613 }
1614 if (ret == 0) {
1615 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1616 pwd, pwdlen, f_rng, p_rng)) != 0) {
1617 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 mbedtls_pem_free(&pem);
1621 return ret;
1622 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1623 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001626#else
1627 ((void) pwd);
1628 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001630
1631 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001632 * At this point we only know it's not a PEM formatted key. Could be any
1633 * of the known DER encoded private key formats
1634 *
1635 * We try the different DER format parsers to see if one passes without
1636 * error
1637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001640 unsigned char *key_copy;
1641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1643 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1644 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1649 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 mbedtls_platform_zeroize(key_copy, keylen);
1652 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 }
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ret == 0) {
1656 return 0;
1657 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 mbedtls_pk_free(pk);
1660 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1663 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1668 if (ret == 0) {
1669 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001670 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_pk_free(pk);
1673 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1678 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1679 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1680 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681 }
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 mbedtls_pk_free(pk);
1684 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Valerio Setti0d2980f2023-04-05 18:17:13 +02001687#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1689 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001690 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 key, keylen, f_rng, p_rng) == 0) {
1692 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001695#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696
Valerio Setti4064dbb2023-05-17 15:33:07 +02001697 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_LIGHT isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001698 * it is ok to leave the PK context initialized but not
1699 * freed: It is the caller's responsibility to call pk_init()
1700 * before calling this function, and to call pk_free()
Valerio Setti4064dbb2023-05-17 15:33:07 +02001701 * when it fails. If MBEDTLS_ECP_LIGHT is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001702 * isn't, this leads to mbedtls_pk_free() being called
1703 * twice, once here and once by the caller, but this is
1704 * also ok and in line with the mbedtls_pk_free() calls
1705 * on failed PEM parsing attempts. */
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708}
1709
1710/*
1711 * Parse a public key
1712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1714 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001715{
Janos Follath24eed8d2019-11-22 13:21:35 +00001716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001717 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001718#if defined(MBEDTLS_RSA_C)
1719 const mbedtls_pk_info_t *pk_info;
1720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001722 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001724#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (keylen == 0) {
1727 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1728 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001729
1730#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001732#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001733 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001735 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 } else {
1737 ret = mbedtls_pem_read_buffer(&pem,
1738 "-----BEGIN RSA PUBLIC KEY-----",
1739 "-----END RSA PUBLIC KEY-----",
1740 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001742
1743 if (ret == 0) {
1744 p = pem.buf;
1745 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1746 mbedtls_pem_free(&pem);
1747 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1748 }
1749
1750 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1751 mbedtls_pem_free(&pem);
1752 return ret;
1753 }
1754
1755 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1756 mbedtls_pk_free(ctx);
1757 }
1758
1759 mbedtls_pem_free(&pem);
1760 return ret;
1761 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1762 mbedtls_pem_free(&pem);
1763 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001764 }
1765#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001766
1767 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001769 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 } else {
1771 ret = mbedtls_pem_read_buffer(&pem,
1772 "-----BEGIN PUBLIC KEY-----",
1773 "-----END PUBLIC KEY-----",
1774 key, NULL, 0, &len);
1775 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001778 /*
1779 * Was PEM encoded
1780 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001781 p = pem.buf;
1782
Jethro Beekman01672442023-04-19 14:08:14 +02001783 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 mbedtls_pem_free(&pem);
1785 return ret;
1786 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1787 mbedtls_pem_free(&pem);
1788 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001789 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001792
1793#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1795 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001796 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001797
1798 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1799 return ret;
1800 }
1801
1802 p = (unsigned char *) key;
1803 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1804 if (ret == 0) {
1805 return ret;
1806 }
1807 mbedtls_pk_free(ctx);
1808 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1809 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1810 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001811 }
1812#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001813 p = (unsigned char *) key;
1814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001818}
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_PK_PARSE_C */