blob: 73e7d8bf03034108a041536480c48289c7813df8 [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{
888 psa_status_t status;
889 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);
909 if (status != PSA_SUCCESS) {
910 ret = psa_pk_status_to_mbedtls(status);
911 return ret;
912 }
913
914 mbedtls_platform_zeroize(key_buf, sizeof(key_buf));
915 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
916 if (status != PSA_SUCCESS) {
917 ret = psa_pk_status_to_mbedtls(status);
918 status = psa_destroy_key(key_id);
919 return (status != PSA_SUCCESS) ? psa_pk_status_to_mbedtls(status) : ret;
920 }
921
922 ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len);
923
924 status = psa_destroy_key(key_id);
925 if (status != PSA_SUCCESS) {
926 return psa_pk_status_to_mbedtls(status);
927 }
928
929 return ret;
930}
931#endif /* MBEDTLS_USE_PSA_CRYPTO */
932
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933/*
934 * Parse a SEC1 encoded private EC key
935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
937 const unsigned char *key, size_t keylen,
938 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939{
Janos Follath24eed8d2019-11-22 13:21:35 +0000940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100941 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700943 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 unsigned char *p = (unsigned char *) key;
945 unsigned char *end = p + keylen;
946 unsigned char *end2;
947
948 /*
949 * RFC 5915, or SEC1 Appendix C.4
950 *
951 * ECPrivateKey ::= SEQUENCE {
952 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
953 * privateKey OCTET STRING,
954 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
955 * publicKey [1] BIT STRING OPTIONAL
956 * }
957 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
959 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
960 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961 }
962
963 end = p + len;
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
966 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
967 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (version != 1) {
970 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
971 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
974 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
978 mbedtls_ecp_keypair_free(eck);
979 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980 }
981
982 p += len;
983
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200984 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200986 /*
987 * Is 'parameters' present?
988 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
990 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
991 0)) == 0) {
992 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
993 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
994 mbedtls_ecp_keypair_free(eck);
995 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200996 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
998 mbedtls_ecp_keypair_free(eck);
999 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001000 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001001 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001004 /*
1005 * Is 'publickey' present? If not, or if we can't read it (eg because it
1006 * is compressed), create it from the private key.
1007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1009 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1010 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001011 end2 = p + len;
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1014 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1015 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (p + len != end2) {
1018 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1019 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1020 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001023 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001025 /*
1026 * The only acceptable failure mode of pk_get_ecpubkey() above
1027 * is if the point format is not recognized.
1028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1030 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1031 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001032 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1034 mbedtls_ecp_keypair_free(eck);
1035 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001036 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001038
Valerio Setti34f67552023-04-03 15:19:18 +02001039 if (!pubkey_done) {
1040#if defined(MBEDTLS_USE_PSA_CRYPTO)
1041 (void) f_rng;
1042 (void) p_rng;
1043 if ((ret = pk_derive_public_key(&eck->grp, &eck->Q, &eck->d)) != 0) {
1044 mbedtls_ecp_keypair_free(eck);
1045 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1046 }
1047#else /* MBEDTLS_USE_PSA_CRYPTO */
1048 if ((ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
1049 f_rng, p_rng)) != 0) {
1050 mbedtls_ecp_keypair_free(eck);
1051 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1052 }
1053#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001054 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1057 mbedtls_ecp_keypair_free(eck);
1058 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 }
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064
1065/*
1066 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001067 *
1068 * Notes:
1069 *
1070 * - This function does not own the key buffer. It is the
1071 * responsibility of the caller to take care of zeroizing
1072 * and freeing it after use.
1073 *
1074 * - The function is responsible for freeing the provided
1075 * PK context on failure.
1076 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077 */
1078static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_pk_context *pk,
1080 const unsigned char *key, size_t keylen,
1081 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082{
1083 int ret, version;
1084 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 unsigned char *p = (unsigned char *) key;
1087 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1089 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001091#if !defined(MBEDTLS_ECP_C)
1092 (void) f_rng;
1093 (void) p_rng;
1094#endif
1095
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001097 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098 *
1099 * PrivateKeyInfo ::= SEQUENCE {
1100 * version Version,
1101 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1102 * privateKey PrivateKey,
1103 * attributes [0] IMPLICIT Attributes OPTIONAL }
1104 *
1105 * Version ::= INTEGER
1106 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1107 * PrivateKey ::= OCTET STRING
1108 *
1109 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1110 */
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1113 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1114 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115 }
1116
1117 end = p + len;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1120 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001121 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (version != 0) {
1124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1125 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1128 return ret;
1129 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1132 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1133 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (len < 1) {
1136 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1137 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1138 }
1139
1140 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1141 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1142 }
1143
1144 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1145 return ret;
1146 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (pk_alg == MBEDTLS_PK_RSA) {
1150 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1151 mbedtls_pk_free(pk);
1152 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 }
1154 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_RSA_C */
1156#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1158 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1159 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1160 mbedtls_pk_free(pk);
1161 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 }
1163 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168}
1169
1170/*
1171 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001172 *
1173 * To save space, the decryption happens in-place on the given key buffer.
1174 * Also, while this function may modify the keybuffer, it doesn't own it,
1175 * and instead it is the responsibility of the caller to zeroize and properly
1176 * free it after use.
1177 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 mbedtls_pk_context *pk,
1182 unsigned char *key, size_t keylen,
1183 const unsigned char *pwd, size_t pwdlen,
1184 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001186 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001188 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1191#if defined(MBEDTLS_PKCS12_C)
1192 mbedtls_cipher_type_t cipher_alg;
1193 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194#endif
1195
Hanno Becker2aa80a72017-09-07 15:28:45 +01001196 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 end = p + keylen;
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (pwdlen == 0) {
1200 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1201 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202
1203 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001204 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205 *
1206 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1207 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1208 * encryptedData EncryptedData
1209 * }
1210 *
1211 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1212 *
1213 * EncryptedData ::= OCTET STRING
1214 *
1215 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001216 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221 }
1222
1223 end = p + len;
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1227 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1231 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232
Hanno Beckerfab35692017-08-25 13:38:26 +01001233 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234
1235 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001236 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1240 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1241 cipher_alg, md_alg,
1242 pwd, pwdlen, p, len, buf)) != 0) {
1243 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1244 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1245 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001249
1250 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#endif /* MBEDTLS_PKCS12_C */
1253#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1255 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1256 p, len, buf)) != 0) {
1257 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1258 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1259 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001263
1264 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001267 {
1268 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001269 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (decrypted == 0) {
1272 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1273 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278
1279/*
1280 * Parse a private key
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1283 const unsigned char *key, size_t keylen,
1284 const unsigned char *pwd, size_t pwdlen,
1285 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286{
Janos Follath24eed8d2019-11-22 13:21:35 +00001287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (keylen == 0) {
1295 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1296 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001297
1298#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001302 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001304 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 } else {
1306 ret = mbedtls_pem_read_buffer(&pem,
1307 "-----BEGIN RSA PRIVATE KEY-----",
1308 "-----END RSA PRIVATE KEY-----",
1309 key, pwd, pwdlen, &len);
1310 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ret == 0) {
1313 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1314 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1315 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1316 pem.buf, pem.buflen)) != 0) {
1317 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318 }
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 mbedtls_pem_free(&pem);
1321 return ret;
1322 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1323 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1324 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1325 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1326 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1327 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001332 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001334 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 } else {
1336 ret = mbedtls_pem_read_buffer(&pem,
1337 "-----BEGIN EC PRIVATE KEY-----",
1338 "-----END EC PRIVATE KEY-----",
1339 key, pwd, pwdlen, &len);
1340 }
1341 if (ret == 0) {
1342 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1345 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1346 pem.buf, pem.buflen,
1347 f_rng, p_rng)) != 0) {
1348 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 }
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 mbedtls_pem_free(&pem);
1352 return ret;
1353 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1354 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1355 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1356 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1357 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1358 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001362 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001364 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 } else {
1366 ret = mbedtls_pem_read_buffer(&pem,
1367 "-----BEGIN PRIVATE KEY-----",
1368 "-----END PRIVATE KEY-----",
1369 key, NULL, 0, &len);
1370 }
1371 if (ret == 0) {
1372 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1373 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1374 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 }
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 mbedtls_pem_free(&pem);
1378 return ret;
1379 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1380 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001384 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001386 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 } else {
1388 ret = mbedtls_pem_read_buffer(&pem,
1389 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1390 "-----END ENCRYPTED PRIVATE KEY-----",
1391 key, NULL, 0, &len);
1392 }
1393 if (ret == 0) {
1394 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1395 pwd, pwdlen, f_rng, p_rng)) != 0) {
1396 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 }
1398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 mbedtls_pem_free(&pem);
1400 return ret;
1401 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1402 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405#else
1406 ((void) pwd);
1407 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
1410 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001411 * At this point we only know it's not a PEM formatted key. Could be any
1412 * of the known DER encoded private key formats
1413 *
1414 * We try the different DER format parsers to see if one passes without
1415 * error
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001419 unsigned char *key_copy;
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1422 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1423 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1428 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_platform_zeroize(key_copy, keylen);
1431 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 }
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (ret == 0) {
1435 return 0;
1436 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 mbedtls_pk_free(pk);
1439 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1442 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1447 if (ret == 0) {
1448 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001449 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 mbedtls_pk_free(pk);
1452 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1457 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1458 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1459 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 }
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 mbedtls_pk_free(pk);
1463 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1468 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1469 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1470 key, keylen, f_rng, p_rng) == 0) {
1471 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475
Hanno Becker780f0a42018-10-10 11:23:33 +01001476 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1477 * it is ok to leave the PK context initialized but not
1478 * freed: It is the caller's responsibility to call pk_init()
1479 * before calling this function, and to call pk_free()
1480 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1481 * isn't, this leads to mbedtls_pk_free() being called
1482 * twice, once here and once by the caller, but this is
1483 * also ok and in line with the mbedtls_pk_free() calls
1484 * on failed PEM parsing attempts. */
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487}
1488
1489/*
1490 * Parse a public key
1491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1493 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494{
Janos Follath24eed8d2019-11-22 13:21:35 +00001495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001497#if defined(MBEDTLS_RSA_C)
1498 const mbedtls_pk_info_t *pk_info;
1499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001503#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (keylen == 0) {
1506 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1507 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001508
1509#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001511#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001512 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001514 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 } else {
1516 ret = mbedtls_pem_read_buffer(&pem,
1517 "-----BEGIN RSA PUBLIC KEY-----",
1518 "-----END RSA PUBLIC KEY-----",
1519 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001520 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001521
1522 if (ret == 0) {
1523 p = pem.buf;
1524 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1525 mbedtls_pem_free(&pem);
1526 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1527 }
1528
1529 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1530 mbedtls_pem_free(&pem);
1531 return ret;
1532 }
1533
1534 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1535 mbedtls_pk_free(ctx);
1536 }
1537
1538 mbedtls_pem_free(&pem);
1539 return ret;
1540 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1541 mbedtls_pem_free(&pem);
1542 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001543 }
1544#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545
1546 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001548 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 } else {
1550 ret = mbedtls_pem_read_buffer(&pem,
1551 "-----BEGIN PUBLIC KEY-----",
1552 "-----END PUBLIC KEY-----",
1553 key, NULL, 0, &len);
1554 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001557 /*
1558 * Was PEM encoded
1559 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001560 p = pem.buf;
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1563 mbedtls_pem_free(&pem);
1564 return ret;
1565 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1566 mbedtls_pem_free(&pem);
1567 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001568 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001571
1572#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1574 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001575 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001576
1577 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1578 return ret;
1579 }
1580
1581 p = (unsigned char *) key;
1582 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1583 if (ret == 0) {
1584 return ret;
1585 }
1586 mbedtls_pk_free(ctx);
1587 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1588 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1589 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001590 }
1591#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592 p = (unsigned char *) key;
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597}
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_PK_PARSE_C */