blob: 11a5b3803666c1777e1a7a0f8546c991fe760b39 [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 Setti34f67552023-04-03 15:19:18 +0200879#if defined(MBEDTLS_USE_PSA_CRYPTO)
880/*
881 * Helper function for deriving a public key from its private counterpart by
882 * using PSA functions
883 */
884static int pk_derive_public_key(mbedtls_ecp_group *grp, mbedtls_ecp_point *Q,
885 const mbedtls_mpi *d)
886{
887 psa_status_t status;
888 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
889 size_t curve_bits;
890 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(grp->id, &curve_bits);
891 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
892 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
893 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
894 int ret;
895
896 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
897 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
898
899 ret = mbedtls_mpi_write_binary(d, key_buf, key_len);
900 if (ret != 0) {
901 return ret;
902 }
903
904 status = psa_import_key(&key_attr, key_buf, key_len, &key_id);
905 if (status != PSA_SUCCESS) {
906 ret = psa_pk_status_to_mbedtls(status);
907 return ret;
908 }
909
910 mbedtls_platform_zeroize(key_buf, sizeof(key_buf));
911 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
912 if (status != PSA_SUCCESS) {
913 ret = psa_pk_status_to_mbedtls(status);
914 status = psa_destroy_key(key_id);
915 return (status != PSA_SUCCESS) ? psa_pk_status_to_mbedtls(status) : ret;
916 }
917
918 ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len);
919
920 status = psa_destroy_key(key_id);
921 if (status != PSA_SUCCESS) {
922 return psa_pk_status_to_mbedtls(status);
923 }
924
925 return ret;
926}
927#endif /* MBEDTLS_USE_PSA_CRYPTO */
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200930/*
931 * Parse a SEC1 encoded private EC key
932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
934 const unsigned char *key, size_t keylen,
935 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936{
Janos Follath24eed8d2019-11-22 13:21:35 +0000937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100938 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200939 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700940 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941 unsigned char *p = (unsigned char *) key;
942 unsigned char *end = p + keylen;
943 unsigned char *end2;
944
945 /*
946 * RFC 5915, or SEC1 Appendix C.4
947 *
948 * ECPrivateKey ::= SEQUENCE {
949 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
950 * privateKey OCTET STRING,
951 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
952 * publicKey [1] BIT STRING OPTIONAL
953 * }
954 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
956 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
957 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958 }
959
960 end = p + len;
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
963 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
964 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (version != 1) {
967 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
968 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
971 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
972 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
975 mbedtls_ecp_keypair_free(eck);
976 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977 }
978
979 p += len;
980
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200981 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200983 /*
984 * Is 'parameters' present?
985 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
987 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
988 0)) == 0) {
989 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
990 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
991 mbedtls_ecp_keypair_free(eck);
992 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200993 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
995 mbedtls_ecp_keypair_free(eck);
996 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100997 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800998 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001001 /*
1002 * Is 'publickey' present? If not, or if we can't read it (eg because it
1003 * is compressed), create it from the private key.
1004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1006 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1007 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001008 end2 = p + len;
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1011 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1012 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (p + len != end2) {
1015 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1016 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1017 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001020 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001022 /*
1023 * The only acceptable failure mode of pk_get_ecpubkey() above
1024 * is if the point format is not recognized.
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1027 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1028 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001029 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1031 mbedtls_ecp_keypair_free(eck);
1032 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001033 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001035
Valerio Setti34f67552023-04-03 15:19:18 +02001036 if (!pubkey_done) {
1037#if defined(MBEDTLS_USE_PSA_CRYPTO)
1038 (void) f_rng;
1039 (void) p_rng;
1040 if ((ret = pk_derive_public_key(&eck->grp, &eck->Q, &eck->d)) != 0) {
1041 mbedtls_ecp_keypair_free(eck);
1042 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1043 }
1044#else /* MBEDTLS_USE_PSA_CRYPTO */
1045 if ((ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
1046 f_rng, p_rng)) != 0) {
1047 mbedtls_ecp_keypair_free(eck);
1048 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1049 }
1050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001051 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1054 mbedtls_ecp_keypair_free(eck);
1055 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 }
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061
1062/*
1063 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001064 *
1065 * Notes:
1066 *
1067 * - This function does not own the key buffer. It is the
1068 * responsibility of the caller to take care of zeroizing
1069 * and freeing it after use.
1070 *
1071 * - The function is responsible for freeing the provided
1072 * PK context on failure.
1073 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074 */
1075static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 mbedtls_pk_context *pk,
1077 const unsigned char *key, size_t keylen,
1078 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001079{
1080 int ret, version;
1081 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083 unsigned char *p = (unsigned char *) key;
1084 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1086 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001088#if !defined(MBEDTLS_ECP_C)
1089 (void) f_rng;
1090 (void) p_rng;
1091#endif
1092
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001094 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095 *
1096 * PrivateKeyInfo ::= SEQUENCE {
1097 * version Version,
1098 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1099 * privateKey PrivateKey,
1100 * attributes [0] IMPLICIT Attributes OPTIONAL }
1101 *
1102 * Version ::= INTEGER
1103 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1104 * PrivateKey ::= OCTET STRING
1105 *
1106 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1107 */
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1110 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1111 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 }
1113
1114 end = p + len;
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1117 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001118 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (version != 0) {
1121 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1122 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1125 return ret;
1126 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1129 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1130 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (len < 1) {
1133 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1134 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1135 }
1136
1137 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1138 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1139 }
1140
1141 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1142 return ret;
1143 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (pk_alg == MBEDTLS_PK_RSA) {
1147 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1148 mbedtls_pk_free(pk);
1149 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 }
1151 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_RSA_C */
1153#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1155 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1156 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1157 mbedtls_pk_free(pk);
1158 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 }
1160 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165}
1166
1167/*
1168 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001169 *
1170 * To save space, the decryption happens in-place on the given key buffer.
1171 * Also, while this function may modify the keybuffer, it doesn't own it,
1172 * and instead it is the responsibility of the caller to zeroize and properly
1173 * free it after use.
1174 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_pk_context *pk,
1179 unsigned char *key, size_t keylen,
1180 const unsigned char *pwd, size_t pwdlen,
1181 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001183 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001185 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1188#if defined(MBEDTLS_PKCS12_C)
1189 mbedtls_cipher_type_t cipher_alg;
1190 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191#endif
1192
Hanno Becker2aa80a72017-09-07 15:28:45 +01001193 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 end = p + keylen;
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (pwdlen == 0) {
1197 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199
1200 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001201 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 *
1203 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1204 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1205 * encryptedData EncryptedData
1206 * }
1207 *
1208 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1209 *
1210 * EncryptedData ::= OCTET STRING
1211 *
1212 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001213 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1216 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 }
1219
1220 end = p + len;
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1224 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1228 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Hanno Beckerfab35692017-08-25 13:38:26 +01001230 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231
1232 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001233 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1237 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1238 cipher_alg, md_alg,
1239 pwd, pwdlen, p, len, buf)) != 0) {
1240 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1241 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1242 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001246
1247 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#endif /* MBEDTLS_PKCS12_C */
1250#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1252 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1253 p, len, buf)) != 0) {
1254 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1255 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1256 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001260
1261 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001264 {
1265 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001266 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (decrypted == 0) {
1269 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1270 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275
1276/*
1277 * Parse a private key
1278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1280 const unsigned char *key, size_t keylen,
1281 const unsigned char *pwd, size_t pwdlen,
1282 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283{
Janos Follath24eed8d2019-11-22 13:21:35 +00001284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001289#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (keylen == 0) {
1292 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1293 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001294
1295#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001299 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001301 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 } else {
1303 ret = mbedtls_pem_read_buffer(&pem,
1304 "-----BEGIN RSA PRIVATE KEY-----",
1305 "-----END RSA PRIVATE KEY-----",
1306 key, pwd, pwdlen, &len);
1307 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ret == 0) {
1310 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1311 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1312 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1313 pem.buf, pem.buflen)) != 0) {
1314 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315 }
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 mbedtls_pem_free(&pem);
1318 return ret;
1319 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1320 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1321 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1322 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1323 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1324 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001329 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001331 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 } else {
1333 ret = mbedtls_pem_read_buffer(&pem,
1334 "-----BEGIN EC PRIVATE KEY-----",
1335 "-----END EC PRIVATE KEY-----",
1336 key, pwd, pwdlen, &len);
1337 }
1338 if (ret == 0) {
1339 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1342 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1343 pem.buf, pem.buflen,
1344 f_rng, p_rng)) != 0) {
1345 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346 }
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 mbedtls_pem_free(&pem);
1349 return ret;
1350 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1351 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1352 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1353 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1354 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1355 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001359 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001361 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 } else {
1363 ret = mbedtls_pem_read_buffer(&pem,
1364 "-----BEGIN PRIVATE KEY-----",
1365 "-----END PRIVATE KEY-----",
1366 key, NULL, 0, &len);
1367 }
1368 if (ret == 0) {
1369 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1370 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1371 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 }
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 mbedtls_pem_free(&pem);
1375 return ret;
1376 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1377 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001381 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001383 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 } else {
1385 ret = mbedtls_pem_read_buffer(&pem,
1386 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1387 "-----END ENCRYPTED PRIVATE KEY-----",
1388 key, NULL, 0, &len);
1389 }
1390 if (ret == 0) {
1391 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1392 pwd, pwdlen, f_rng, p_rng)) != 0) {
1393 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394 }
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 mbedtls_pem_free(&pem);
1397 return ret;
1398 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1399 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402#else
1403 ((void) pwd);
1404 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406
1407 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001408 * At this point we only know it's not a PEM formatted key. Could be any
1409 * of the known DER encoded private key formats
1410 *
1411 * We try the different DER format parsers to see if one passes without
1412 * error
1413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001416 unsigned char *key_copy;
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1419 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1420 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1425 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 mbedtls_platform_zeroize(key_copy, keylen);
1428 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429 }
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (ret == 0) {
1432 return 0;
1433 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_pk_free(pk);
1436 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1439 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1444 if (ret == 0) {
1445 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 mbedtls_pk_free(pk);
1449 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1454 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1455 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1456 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457 }
1458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 mbedtls_pk_free(pk);
1460 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1465 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1466 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1467 key, keylen, f_rng, p_rng) == 0) {
1468 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472
Hanno Becker780f0a42018-10-10 11:23:33 +01001473 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1474 * it is ok to leave the PK context initialized but not
1475 * freed: It is the caller's responsibility to call pk_init()
1476 * before calling this function, and to call pk_free()
1477 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1478 * isn't, this leads to mbedtls_pk_free() being called
1479 * twice, once here and once by the caller, but this is
1480 * also ok and in line with the mbedtls_pk_free() calls
1481 * on failed PEM parsing attempts. */
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484}
1485
1486/*
1487 * Parse a public key
1488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1490 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491{
Janos Follath24eed8d2019-11-22 13:21:35 +00001492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001494#if defined(MBEDTLS_RSA_C)
1495 const mbedtls_pk_info_t *pk_info;
1496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001498 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001500#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (keylen == 0) {
1503 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1504 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001505
1506#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001508#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001509 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001511 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 } else {
1513 ret = mbedtls_pem_read_buffer(&pem,
1514 "-----BEGIN RSA PUBLIC KEY-----",
1515 "-----END RSA PUBLIC KEY-----",
1516 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001517 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001518
1519 if (ret == 0) {
1520 p = pem.buf;
1521 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1522 mbedtls_pem_free(&pem);
1523 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1524 }
1525
1526 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1527 mbedtls_pem_free(&pem);
1528 return ret;
1529 }
1530
1531 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1532 mbedtls_pk_free(ctx);
1533 }
1534
1535 mbedtls_pem_free(&pem);
1536 return ret;
1537 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1538 mbedtls_pem_free(&pem);
1539 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001540 }
1541#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001542
1543 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 } else {
1547 ret = mbedtls_pem_read_buffer(&pem,
1548 "-----BEGIN PUBLIC KEY-----",
1549 "-----END PUBLIC KEY-----",
1550 key, NULL, 0, &len);
1551 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554 /*
1555 * Was PEM encoded
1556 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001557 p = pem.buf;
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1560 mbedtls_pem_free(&pem);
1561 return ret;
1562 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1563 mbedtls_pem_free(&pem);
1564 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001568
1569#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1571 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001572 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001573
1574 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1575 return ret;
1576 }
1577
1578 p = (unsigned char *) key;
1579 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1580 if (ret == 0) {
1581 return ret;
1582 }
1583 mbedtls_pk_free(ctx);
1584 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1585 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1586 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001587 }
1588#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 p = (unsigned char *) key;
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_PK_PARSE_C */