blob: 93f435d10148cb98a47db2875b100f5b91863716 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
50
Valerio Setti34f67552023-04-03 15:19:18 +020051#if defined(MBEDTLS_PSA_CRYPTO_C)
52#include "mbedtls/psa_util.h"
53#endif
54
55#if defined(MBEDTLS_USE_PSA_CRYPTO)
56#include "psa/crypto.h"
57#endif
58
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020060
Gilles Peskine832f3492017-11-30 11:42:12 +010061#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020062/*
63 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020064 *
65 * The file is expected to contain either PEM or DER encoded data.
66 * A terminating null byte is always appended. It is included in the announced
67 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020068 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020070{
71 FILE *f;
72 long size;
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if ((f = fopen(path, "rb")) == NULL) {
75 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
76 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020077
Gilles Peskineda0913b2022-06-30 17:03:40 +020078 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 fseek(f, 0, SEEK_END);
82 if ((size = ftell(f)) == -1) {
83 fclose(f);
84 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020085 }
Gilles Peskine449bd832023-01-11 14:50:10 +010086 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020087
88 *n = (size_t) size;
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (*n + 1 == 0 ||
91 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
92 fclose(f);
93 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020094 }
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (fread(*buf, 1, *n, f) != *n) {
97 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_platform_zeroize(*buf, *n);
100 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 }
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106
107 (*buf)[*n] = '\0';
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200110 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114}
115
116/*
117 * Load and parse a private key
118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
120 const char *path, const char *pwd,
121 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122{
Janos Follath24eed8d2019-11-22 13:21:35 +0000123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124 size_t n;
125 unsigned char *buf;
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
128 return ret;
129 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (pwd == NULL) {
132 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
133 } else {
134 ret = mbedtls_pk_parse_key(ctx, buf, n,
135 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
136 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_platform_zeroize(buf, n);
139 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142}
143
144/*
145 * Load and parse a public key
146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148{
Janos Follath24eed8d2019-11-22 13:21:35 +0000149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 size_t n;
151 unsigned char *buf;
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
154 return ret;
155 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_platform_zeroize(buf, n);
160 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_ECP_C)
167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (end - *p < 1) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
183 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100190#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 params->p = *p;
201 *p += params->len;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (*p != end) {
204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
235 const unsigned char * const end = params->p + params->len;
236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
243 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ver < 1 || ver > 3) {
246 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
257 return ret;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 end_field = p + len;
261
262 /*
263 * FIELD-ID ::= TYPE-IDENTIFIER
264 * FieldTypes FIELD-ID ::= {
265 * { Prime-p IDENTIFIED BY prime-field } |
266 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
267 * }
268 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
271 return ret;
272 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
275 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
276 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 }
278
279 p += len;
280
281 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
284 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (p != end_field) {
289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
291 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /*
294 * Curve ::= SEQUENCE {
295 * a FieldElement,
296 * b FieldElement,
297 * seed BIT STRING OPTIONAL
298 * -- Shall be present if used in SpecifiedECDomain
299 * -- with version equal to ecdpVer2 or ecdpVer3
300 * }
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
304 return ret;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
314 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316 }
317
318 p += len;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
321 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
322 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (p != end_curve) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
342 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
345 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
351 (p[0] != 0x02 && p[0] != 0x03) ||
352 len != mbedtls_mpi_size(&grp->P) + 1 ||
353 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
354 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
356 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 }
358 }
359
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 p += len;
361
362 /*
363 * order INTEGER
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
367 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_ecp_group_free(&ref);
393 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
395 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
397 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
398 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405 break;
406 }
407
408 }
409
410cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412
413 *grp_id = *id;
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100420}
421
422/*
423 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
424 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
426 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427{
Janos Follath24eed8d2019-11-22 13:21:35 +0000428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438
439cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000440 /* The API respecting lifecycle for mbedtls_ecp_group struct is
441 * _init(), _load() and _free(). In pk_group_id_from_specified() the
442 * temporary grp breaks that flow and it's members are populated
443 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
444 * which is assuming a group populated by _setup() may not clean-up
445 * properly -> Manually free it's members.
446 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000447 mbedtls_mpi_free(&grp.N);
448 mbedtls_mpi_free(&grp.P);
449 mbedtls_mpi_free(&grp.A);
450 mbedtls_mpi_free(&grp.B);
451 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456
457/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200458 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459 *
460 * ECParameters ::= CHOICE {
461 * namedCurve OBJECT IDENTIFIER
462 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
463 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200464 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200466{
Janos Follath24eed8d2019-11-22 13:21:35 +0000467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (params->tag == MBEDTLS_ASN1_OID) {
471 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
472 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
473 }
474 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
477 return ret;
478 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100479#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100481#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100482 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200483
484 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800485 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) {
488 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
489 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) {
492 return ret;
493 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496}
497
498/*
499 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100500 *
501 * The caller is responsible for clearing the structure upon failure if
502 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
506 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507{
Janos Follath24eed8d2019-11-22 13:21:35 +0000508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
511 (const unsigned char *) *p, end - *p)) == 0) {
512 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513 }
514
515 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517 */
518 *p = (unsigned char *) end;
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525/*
526 * RSAPublicKey ::= SEQUENCE {
527 * modulus INTEGER, -- n
528 * publicExponent INTEGER -- e
529 * }
530 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531static int pk_get_rsapubkey(unsigned char **p,
532 const unsigned char *end,
533 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534{
Janos Follath24eed8d2019-11-22 13:21:35 +0000535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 size_t len;
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
539 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
540 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
541 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (*p + len != end) {
544 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
545 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
546 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200547
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100548 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
551 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
554 NULL, 0, NULL, 0)) != 0) {
555 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
556 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100557
558 *p += len;
559
560 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
562 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
563 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
566 NULL, 0, *p, len)) != 0) {
567 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
568 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100569
570 *p += len;
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (mbedtls_rsa_complete(rsa) != 0 ||
573 mbedtls_rsa_check_pubkey(rsa) != 0) {
574 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000575 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (*p != end) {
578 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
579 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
580 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585
586/* Get a PK algorithm identifier
587 *
588 * AlgorithmIdentifier ::= SEQUENCE {
589 * algorithm OBJECT IDENTIFIER,
590 * parameters ANY DEFINED BY algorithm OPTIONAL }
591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static int pk_get_pk_alg(unsigned char **p,
593 const unsigned char *end,
594 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595{
Janos Follath24eed8d2019-11-22 13:21:35 +0000596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
602 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
603 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (mbedtls_oid_get_pk_alg(&alg_oid, pk_alg) != 0) {
606 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
607 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608
609 /*
610 * No parameters with RSA (only for EC)
611 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (*pk_alg == MBEDTLS_PK_RSA &&
613 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
614 params->len != 0)) {
615 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616 }
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619}
620
621/*
622 * SubjectPublicKeyInfo ::= SEQUENCE {
623 * algorithm AlgorithmIdentifier,
624 * subjectPublicKey BIT STRING }
625 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
627 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628{
Janos Follath24eed8d2019-11-22 13:21:35 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_asn1_buf alg_params;
632 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
633 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
636 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
637 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638 }
639
640 end = *p + len;
641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params)) != 0) {
643 return ret;
644 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
647 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
648 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (*p + len != end) {
651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
652 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
653 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
656 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
657 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
660 return ret;
661 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (pk_alg == MBEDTLS_PK_RSA) {
665 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#endif /* MBEDTLS_RSA_C */
668#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
670 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
671 if (ret == 0) {
672 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
673 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (ret == 0 && *p != end) {
679 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
680 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
681 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (ret != 0) {
684 mbedtls_pk_free(pk);
685 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688}
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100692 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
693 *
694 * The value zero is:
695 * - never a valid value for an RSA parameter
696 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
697 *
698 * Since values can't be omitted in PKCS#1, passing a zero value to
699 * rsa_complete() would be incorrect, so reject zero values early.
700 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static int asn1_get_nonzero_mpi(unsigned char **p,
702 const unsigned char *end,
703 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100704{
705 int ret;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 ret = mbedtls_asn1_get_mpi(p, end, X);
708 if (ret != 0) {
709 return ret;
710 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
713 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
714 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100717}
718
719/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 * Parse a PKCS#1 encoded private RSA key
721 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
723 const unsigned char *key,
724 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100726 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 size_t len;
728 unsigned char *p, *end;
729
Hanno Beckerefa14e82017-10-11 19:45:19 +0100730 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100732
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 p = (unsigned char *) key;
734 end = p + keylen;
735
736 /*
737 * This function parses the RSAPrivateKey (PKCS#1)
738 *
739 * RSAPrivateKey ::= SEQUENCE {
740 * version Version,
741 * modulus INTEGER, -- n
742 * publicExponent INTEGER, -- e
743 * privateExponent INTEGER, -- d
744 * prime1 INTEGER, -- p
745 * prime2 INTEGER, -- q
746 * exponent1 INTEGER, -- d mod (p-1)
747 * exponent2 INTEGER, -- d mod (q-1)
748 * coefficient INTEGER, -- (inverse of q) mod p
749 * otherPrimeInfos OtherPrimeInfos OPTIONAL
750 * }
751 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
753 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755 }
756
757 end = p + len;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
760 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 }
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (version != 0) {
764 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765 }
766
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100767 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
769 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
770 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100771 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
776 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
777 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100778 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100780
781 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
783 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
784 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100785 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100787
788 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
790 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
791 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100792 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100794
795 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
797 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
798 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100801
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100802#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500803 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
805 * that they can be easily recomputed from D, P and Q. However by
806 * parsing them from the PKCS1 structure it is possible to avoid
807 * recalculating them which both reduces the overhead of loading
808 * RSA private keys into memory and also avoids side channels which
809 * can arise when computing those values, since all of D, P, and Q
810 * are secret. See https://eprint.iacr.org/2020/055 for a
811 * description of one such attack.
812 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500813
Jack Lloyd80cc8112020-01-22 17:34:29 -0500814 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
816 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
817 goto cleanup;
818 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500819
820 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
822 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
823 goto cleanup;
824 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500825
826 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
828 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
829 goto cleanup;
830 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500831
Jack Lloyd60239752020-01-27 17:53:36 -0500832#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800833 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
835 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
836 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
837 goto cleanup;
838 }
Jack Lloyd60239752020-01-27 17:53:36 -0500839#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500840
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100841 /* rsa_complete() doesn't complete anything with the default
842 * implementation but is still called:
843 * - for the benefit of alternative implementation that may want to
844 * pre-compute stuff beyond what's provided (eg Montgomery factors)
845 * - as is also sanity-checks the key
846 *
847 * Furthermore, we also check the public part for consistency with
848 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
849 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
851 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100852 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100853 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (p != end) {
856 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
857 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858 }
859
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100860cleanup:
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100865 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if ((ret & 0xff80) == 0) {
867 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
868 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100869 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 }
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
Valerio Settiaad63062023-04-04 12:58:15 +0200879#if defined(MBEDTLS_ECP_C)
Valerio Setti34f67552023-04-03 15:19:18 +0200880#if defined(MBEDTLS_USE_PSA_CRYPTO)
881/*
882 * Helper function for deriving a public key from its private counterpart by
Valerio Settiaad63062023-04-04 12:58:15 +0200883 * using PSA functions.
Valerio Setti34f67552023-04-03 15:19:18 +0200884 */
885static int pk_derive_public_key(mbedtls_ecp_group *grp, mbedtls_ecp_point *Q,
886 const mbedtls_mpi *d)
887{
Valerio Setti1df94f82023-04-07 08:59:24 +0200888 psa_status_t status, destruction_status;
Valerio Setti34f67552023-04-03 15:19:18 +0200889 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
890 size_t curve_bits;
891 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(grp->id, &curve_bits);
Valerio Setti4bf73ad2023-04-04 10:48:57 +0200892 /* This buffer is used to store the private key at first and then the
893 * public one (but not at the same time). Therefore we size it for the
894 * latter since it's bigger. */
895 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti34f67552023-04-03 15:19:18 +0200896 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
897 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
898 int ret;
899
900 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
901 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
902
903 ret = mbedtls_mpi_write_binary(d, key_buf, key_len);
904 if (ret != 0) {
905 return ret;
906 }
907
908 status = psa_import_key(&key_attr, key_buf, key_len, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +0200909 ret = psa_pk_status_to_mbedtls(status);
910 if (ret != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +0200911 return ret;
912 }
913
914 mbedtls_platform_zeroize(key_buf, sizeof(key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +0200915
Valerio Setti34f67552023-04-03 15:19:18 +0200916 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
Valerio Setti1df94f82023-04-07 08:59:24 +0200917 ret = psa_pk_status_to_mbedtls(status);
918 destruction_status = psa_destroy_key(key_id);
919 if (ret != 0) {
920 return ret;
921 } else if (destruction_status != PSA_SUCCESS) {
922 return psa_pk_status_to_mbedtls(destruction_status);
Valerio Setti34f67552023-04-03 15:19:18 +0200923 }
924
925 ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len);
926
Valerio Setti34f67552023-04-03 15:19:18 +0200927 return ret;
928}
929#endif /* MBEDTLS_USE_PSA_CRYPTO */
930
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931/*
932 * Parse a SEC1 encoded private EC key
933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
935 const unsigned char *key, size_t keylen,
936 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937{
Janos Follath24eed8d2019-11-22 13:21:35 +0000938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100939 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700941 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942 unsigned char *p = (unsigned char *) key;
943 unsigned char *end = p + keylen;
944 unsigned char *end2;
945
946 /*
947 * RFC 5915, or SEC1 Appendix C.4
948 *
949 * ECPrivateKey ::= SEQUENCE {
950 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
951 * privateKey OCTET STRING,
952 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
953 * publicKey [1] BIT STRING OPTIONAL
954 * }
955 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
957 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
958 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200959 }
960
961 end = p + len;
962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
964 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
965 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (version != 1) {
968 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
969 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
972 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
973 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
976 mbedtls_ecp_keypair_free(eck);
977 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978 }
979
980 p += len;
981
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200982 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200984 /*
985 * Is 'parameters' present?
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
988 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
989 0)) == 0) {
990 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
991 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
992 mbedtls_ecp_keypair_free(eck);
993 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200994 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
996 mbedtls_ecp_keypair_free(eck);
997 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100998 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800999 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001002 /*
1003 * Is 'publickey' present? If not, or if we can't read it (eg because it
1004 * is compressed), create it from the private key.
1005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1007 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1008 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001009 end2 = p + len;
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1012 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1013 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (p + len != end2) {
1016 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1017 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1018 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001021 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001023 /*
1024 * The only acceptable failure mode of pk_get_ecpubkey() above
1025 * is if the point format is not recognized.
1026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1028 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1029 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1032 mbedtls_ecp_keypair_free(eck);
1033 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001034 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001036
Valerio Setti34f67552023-04-03 15:19:18 +02001037 if (!pubkey_done) {
1038#if defined(MBEDTLS_USE_PSA_CRYPTO)
1039 (void) f_rng;
1040 (void) p_rng;
1041 if ((ret = pk_derive_public_key(&eck->grp, &eck->Q, &eck->d)) != 0) {
1042 mbedtls_ecp_keypair_free(eck);
1043 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1044 }
1045#else /* MBEDTLS_USE_PSA_CRYPTO */
1046 if ((ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
1047 f_rng, p_rng)) != 0) {
1048 mbedtls_ecp_keypair_free(eck);
1049 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1050 }
1051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001052 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1055 mbedtls_ecp_keypair_free(eck);
1056 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
1063/*
1064 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001065 *
1066 * Notes:
1067 *
1068 * - This function does not own the key buffer. It is the
1069 * responsibility of the caller to take care of zeroizing
1070 * and freeing it after use.
1071 *
1072 * - The function is responsible for freeing the provided
1073 * PK context on failure.
1074 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 */
1076static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 mbedtls_pk_context *pk,
1078 const unsigned char *key, size_t keylen,
1079 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080{
1081 int ret, version;
1082 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084 unsigned char *p = (unsigned char *) key;
1085 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1087 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001089#if !defined(MBEDTLS_ECP_C)
1090 (void) f_rng;
1091 (void) p_rng;
1092#endif
1093
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001095 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 *
1097 * PrivateKeyInfo ::= SEQUENCE {
1098 * version Version,
1099 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1100 * privateKey PrivateKey,
1101 * attributes [0] IMPLICIT Attributes OPTIONAL }
1102 *
1103 * Version ::= INTEGER
1104 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1105 * PrivateKey ::= OCTET STRING
1106 *
1107 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1108 */
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1111 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1112 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 }
1114
1115 end = p + len;
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1118 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001119 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (version != 0) {
1122 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1123 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1126 return ret;
1127 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1130 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (len < 1) {
1134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1135 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1136 }
1137
1138 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1139 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1140 }
1141
1142 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1143 return ret;
1144 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (pk_alg == MBEDTLS_PK_RSA) {
1148 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1149 mbedtls_pk_free(pk);
1150 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 }
1152 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#endif /* MBEDTLS_RSA_C */
1154#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1156 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1157 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1158 mbedtls_pk_free(pk);
1159 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 }
1161 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166}
1167
1168/*
1169 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001170 *
1171 * To save space, the decryption happens in-place on the given key buffer.
1172 * Also, while this function may modify the keybuffer, it doesn't own it,
1173 * and instead it is the responsibility of the caller to zeroize and properly
1174 * free it after use.
1175 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_pk_context *pk,
1180 unsigned char *key, size_t keylen,
1181 const unsigned char *pwd, size_t pwdlen,
1182 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001184 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001186 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1189#if defined(MBEDTLS_PKCS12_C)
1190 mbedtls_cipher_type_t cipher_alg;
1191 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192#endif
1193
Hanno Becker2aa80a72017-09-07 15:28:45 +01001194 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 end = p + keylen;
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (pwdlen == 0) {
1198 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
1201 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001202 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 *
1204 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1205 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1206 * encryptedData EncryptedData
1207 * }
1208 *
1209 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1210 *
1211 * EncryptedData ::= OCTET STRING
1212 *
1213 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001214 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 }
1220
1221 end = p + len;
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1225 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1229 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230
Hanno Beckerfab35692017-08-25 13:38:26 +01001231 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232
1233 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001234 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1238 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1239 cipher_alg, md_alg,
1240 pwd, pwdlen, p, len, buf)) != 0) {
1241 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1242 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1243 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001247
1248 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_PKCS12_C */
1251#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1253 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1254 p, len, buf)) != 0) {
1255 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1256 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1257 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001261
1262 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001265 {
1266 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001267 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (decrypted == 0) {
1270 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1271 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276
1277/*
1278 * Parse a private key
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1281 const unsigned char *key, size_t keylen,
1282 const unsigned char *pwd, size_t pwdlen,
1283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284{
Janos Follath24eed8d2019-11-22 13:21:35 +00001285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001290#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (keylen == 0) {
1293 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1294 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001295
1296#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001300 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001302 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 } else {
1304 ret = mbedtls_pem_read_buffer(&pem,
1305 "-----BEGIN RSA PRIVATE KEY-----",
1306 "-----END RSA PRIVATE KEY-----",
1307 key, pwd, pwdlen, &len);
1308 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (ret == 0) {
1311 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1312 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1313 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1314 pem.buf, pem.buflen)) != 0) {
1315 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 }
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 mbedtls_pem_free(&pem);
1319 return ret;
1320 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1321 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1322 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1323 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1324 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1325 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001330 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001332 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 } else {
1334 ret = mbedtls_pem_read_buffer(&pem,
1335 "-----BEGIN EC PRIVATE KEY-----",
1336 "-----END EC PRIVATE KEY-----",
1337 key, pwd, pwdlen, &len);
1338 }
1339 if (ret == 0) {
1340 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1343 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1344 pem.buf, pem.buflen,
1345 f_rng, p_rng)) != 0) {
1346 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 }
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 mbedtls_pem_free(&pem);
1350 return ret;
1351 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1352 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1353 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1354 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1355 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1356 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001360 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001362 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 } else {
1364 ret = mbedtls_pem_read_buffer(&pem,
1365 "-----BEGIN PRIVATE KEY-----",
1366 "-----END PRIVATE KEY-----",
1367 key, NULL, 0, &len);
1368 }
1369 if (ret == 0) {
1370 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1371 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1372 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 }
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 mbedtls_pem_free(&pem);
1376 return ret;
1377 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1378 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001382 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001384 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 } else {
1386 ret = mbedtls_pem_read_buffer(&pem,
1387 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1388 "-----END ENCRYPTED PRIVATE KEY-----",
1389 key, NULL, 0, &len);
1390 }
1391 if (ret == 0) {
1392 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1393 pwd, pwdlen, f_rng, p_rng)) != 0) {
1394 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001395 }
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 mbedtls_pem_free(&pem);
1398 return ret;
1399 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1400 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403#else
1404 ((void) pwd);
1405 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407
1408 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001409 * At this point we only know it's not a PEM formatted key. Could be any
1410 * of the known DER encoded private key formats
1411 *
1412 * We try the different DER format parsers to see if one passes without
1413 * error
1414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001417 unsigned char *key_copy;
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1420 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1421 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1426 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_platform_zeroize(key_copy, keylen);
1429 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 }
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (ret == 0) {
1433 return 0;
1434 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_pk_free(pk);
1437 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1440 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1445 if (ret == 0) {
1446 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001447 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 mbedtls_pk_free(pk);
1450 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1455 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1456 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1457 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 }
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_pk_free(pk);
1461 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1466 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1467 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1468 key, keylen, f_rng, p_rng) == 0) {
1469 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473
Hanno Becker780f0a42018-10-10 11:23:33 +01001474 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1475 * it is ok to leave the PK context initialized but not
1476 * freed: It is the caller's responsibility to call pk_init()
1477 * before calling this function, and to call pk_free()
1478 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1479 * isn't, this leads to mbedtls_pk_free() being called
1480 * twice, once here and once by the caller, but this is
1481 * also ok and in line with the mbedtls_pk_free() calls
1482 * on failed PEM parsing attempts. */
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485}
1486
1487/*
1488 * Parse a public key
1489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1491 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492{
Janos Follath24eed8d2019-11-22 13:21:35 +00001493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001495#if defined(MBEDTLS_RSA_C)
1496 const mbedtls_pk_info_t *pk_info;
1497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001501#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (keylen == 0) {
1504 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1505 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001506
1507#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001509#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001510 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001512 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 } else {
1514 ret = mbedtls_pem_read_buffer(&pem,
1515 "-----BEGIN RSA PUBLIC KEY-----",
1516 "-----END RSA PUBLIC KEY-----",
1517 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001518 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001519
1520 if (ret == 0) {
1521 p = pem.buf;
1522 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1523 mbedtls_pem_free(&pem);
1524 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1525 }
1526
1527 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1528 mbedtls_pem_free(&pem);
1529 return ret;
1530 }
1531
1532 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1533 mbedtls_pk_free(ctx);
1534 }
1535
1536 mbedtls_pem_free(&pem);
1537 return ret;
1538 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1539 mbedtls_pem_free(&pem);
1540 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001541 }
1542#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001543
1544 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001546 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 } else {
1548 ret = mbedtls_pem_read_buffer(&pem,
1549 "-----BEGIN PUBLIC KEY-----",
1550 "-----END PUBLIC KEY-----",
1551 key, NULL, 0, &len);
1552 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555 /*
1556 * Was PEM encoded
1557 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001558 p = pem.buf;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1561 mbedtls_pem_free(&pem);
1562 return ret;
1563 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1564 mbedtls_pem_free(&pem);
1565 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001569
1570#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1572 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001573 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001574
1575 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1576 return ret;
1577 }
1578
1579 p = (unsigned char *) key;
1580 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1581 if (ret == 0) {
1582 return ret;
1583 }
1584 mbedtls_pk_free(ctx);
1585 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1586 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1587 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001588 }
1589#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590 p = (unsigned char *) key;
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595}
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_PK_PARSE_C */