blob: ade8a04cab9c5daf7697d9f8d8b56ff991dc3b0c [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é-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ecp.h"
Jethro Beekman01672442023-04-19 14:08:14 +020036#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
37#include "pkwrite.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
51
Valerio Setti34f67552023-04-03 15:19:18 +020052#if defined(MBEDTLS_PSA_CRYPTO_C)
53#include "mbedtls/psa_util.h"
54#endif
55
56#if defined(MBEDTLS_USE_PSA_CRYPTO)
57#include "psa/crypto.h"
58#endif
59
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061
Gilles Peskine832f3492017-11-30 11:42:12 +010062#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020063/*
64 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020065 *
66 * The file is expected to contain either PEM or DER encoded data.
67 * A terminating null byte is always appended. It is included in the announced
68 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020069 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020071{
72 FILE *f;
73 long size;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if ((f = fopen(path, "rb")) == NULL) {
76 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
77 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
Gilles Peskineda0913b2022-06-30 17:03:40 +020079 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 fseek(f, 0, SEEK_END);
83 if ((size = ftell(f)) == -1) {
84 fclose(f);
85 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020086 }
Gilles Peskine449bd832023-01-11 14:50:10 +010087 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
89 *n = (size_t) size;
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (*n + 1 == 0 ||
92 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
93 fclose(f);
94 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (fread(*buf, 1, *n, f) != *n) {
98 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_platform_zeroize(*buf, *n);
101 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
108 (*buf)[*n] = '\0';
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200111 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115}
116
117/*
118 * Load and parse a private key
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
121 const char *path, const char *pwd,
122 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123{
Janos Follath24eed8d2019-11-22 13:21:35 +0000124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 size_t n;
126 unsigned char *buf;
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
129 return ret;
130 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (pwd == NULL) {
133 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
134 } else {
135 ret = mbedtls_pk_parse_key(ctx, buf, n,
136 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
137 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_platform_zeroize(buf, n);
140 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143}
144
145/*
146 * Load and parse a public key
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149{
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 size_t n;
152 unsigned char *buf;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
155 return ret;
156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_platform_zeroize(buf, n);
161 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166
Valerio Setti0d2980f2023-04-05 18:17:13 +0200167#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169 *
170 * ECParameters ::= CHOICE {
171 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100172 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 * }
175 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
177 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178{
Janos Follath24eed8d2019-11-22 13:21:35 +0000179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (end - *p < 1) {
182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
183 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
184 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100185
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100186 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100191#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 ) {
193 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
194 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200
201 params->p = *p;
202 *p += params->len;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (*p != end) {
205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
206 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210}
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100214 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
215 * WARNING: the resulting group should only be used with
216 * pk_group_id_from_specified(), since its base point may not be set correctly
217 * if it was encoded compressed.
218 *
219 * SpecifiedECDomain ::= SEQUENCE {
220 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
221 * fieldID FieldID {{FieldTypes}},
222 * curve Curve,
223 * base ECPoint,
224 * order INTEGER,
225 * cofactor INTEGER OPTIONAL,
226 * hash HashAlgorithm OPTIONAL,
227 * ...
228 * }
229 *
230 * We only support prime-field as field type, and ignore hash and cofactor.
231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100233{
Janos Follath24eed8d2019-11-22 13:21:35 +0000234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100235 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200236 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237 const unsigned char *end_field, *end_curve;
238 size_t len;
239 int ver;
240
241 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
243 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
244 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (ver < 1 || ver > 3) {
247 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
248 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100249
250 /*
251 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
252 * fieldType FIELD-ID.&id({IOSet}),
253 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
254 * }
255 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
257 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
258 return ret;
259 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100260
261 end_field = p + len;
262
263 /*
264 * FIELD-ID ::= TYPE-IDENTIFIER
265 * FieldTypes FIELD-ID ::= {
266 * { Prime-p IDENTIFIED BY prime-field } |
267 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
268 * }
269 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
272 return ret;
273 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
276 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
277 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278 }
279
280 p += len;
281
282 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
285 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (p != end_field) {
290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
291 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
292 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
294 /*
295 * Curve ::= SEQUENCE {
296 * a FieldElement,
297 * b FieldElement,
298 * seed BIT STRING OPTIONAL
299 * -- Shall be present if used in SpecifiedECDomain
300 * -- with version equal to ecdpVer2 or ecdpVer3
301 * }
302 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
304 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
305 return ret;
306 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100307
308 end_curve = p + len;
309
310 /*
311 * FieldElement ::= OCTET STRING
312 * containing an integer in the case of a prime field
313 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
315 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317 }
318
319 p += len;
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
322 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324 }
325
326 p += len;
327
328 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (p != end_curve) {
334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
335 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
336 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100337
338 /*
339 * ECPoint ::= OCTET STRING
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
342 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
343 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
346 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100347 /*
348 * If we can't read the point because it's compressed, cheat by
349 * reading only the X coordinate and the parity bit of Y.
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
352 (p[0] != 0x02 && p[0] != 0x03) ||
353 len != mbedtls_mpi_size(&grp->P) + 1 ||
354 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
355 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
356 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
357 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100358 }
359 }
360
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361 p += len;
362
363 /*
364 * order INTEGER
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
368 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371
372 /*
373 * Allow optional elements by purposefully not enforcing p == end here.
374 */
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377}
378
379/*
380 * Find the group id associated with an (almost filled) group as generated by
381 * pk_group_from_specified(), or return an error if unknown.
382 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383static 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 +0100384{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100385 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_ecp_group ref;
387 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100392 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_ecp_group_free(&ref);
394 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
396 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
398 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
399 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
403 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 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 +0100406 break;
407 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 }
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
Jethro Beekman01672442023-04-19 14:08:14 +0200498#if defined(MBEDTLS_ECP_LIGHT)
499/*
500 * Helper function for deriving a public key from its private counterpart.
501 */
502static int pk_derive_public_key(mbedtls_ecp_keypair *eck,
503 const unsigned char *d, size_t d_len,
504 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
505{
506 int ret;
507#if defined(MBEDTLS_USE_PSA_CRYPTO)
508 psa_status_t status, destruction_status;
509 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
510 size_t curve_bits;
511 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
512 /* This buffer is used to store the private key at first and then the
513 * public one (but not at the same time). Therefore we size it for the
514 * latter since it's bigger. */
515 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
516 size_t key_len;
517 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
518
519 (void) f_rng;
520 (void) p_rng;
521
522 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
523 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
524
525 status = psa_import_key(&key_attr, d, d_len, &key_id);
526 ret = psa_pk_status_to_mbedtls(status);
527 if (ret != 0) {
528 return ret;
529 }
530
531 mbedtls_platform_zeroize(key_buf, sizeof(key_buf));
532
533 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
534 ret = psa_pk_status_to_mbedtls(status);
535 destruction_status = psa_destroy_key(key_id);
536 if (ret != 0) {
537 return ret;
538 } else if (destruction_status != PSA_SUCCESS) {
539 return psa_pk_status_to_mbedtls(destruction_status);
540 }
541
542 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
543#else /* MBEDTLS_USE_PSA_CRYPTO */
544 (void) d;
545 (void) d_len;
546
547 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
548#endif /* MBEDTLS_USE_PSA_CRYPTO */
549 return ret;
550}
551
552#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
553
554/*
555 * Load an RFC8410 EC key, which doesn't have any parameters
556 */
557static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
558 mbedtls_ecp_group_id grp_id,
559 mbedtls_ecp_group *grp)
560{
561 if (params->tag != 0 || params->len != 0) {
562 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
563 }
564
565 return mbedtls_ecp_group_load(grp, grp_id);
566}
567
568/*
569 * Parse an RFC 8410 encoded private EC key
570 *
571 * CurvePrivateKey ::= OCTET STRING
572 */
573static int pk_parse_key_rfc8410_der(mbedtls_ecp_keypair *eck,
574 unsigned char *key, size_t keylen, const unsigned char *end,
575 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
576{
577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
578 size_t len;
579
580 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
581 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
582 }
583
584 if (key + len != end) {
585 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
586 }
587
588 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
589 mbedtls_ecp_keypair_free(eck);
590 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
591 }
592
Jethro Beekman2e662c62023-05-03 12:56:54 +0200593 // pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
594 // which never contain a public key. As such, derive the public key
595 // unconditionally.
Jethro Beekman01672442023-04-19 14:08:14 +0200596 if ((ret = pk_derive_public_key(eck, key, len, f_rng, p_rng)) != 0) {
597 mbedtls_ecp_keypair_free(eck);
598 return ret;
599 }
600
601 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
602 mbedtls_ecp_keypair_free(eck);
603 return ret;
604 }
605
606 return 0;
607}
608#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
609#endif /* MBEDTLS_ECP_LIGHT */
610
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611/*
612 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100613 *
614 * The caller is responsible for clearing the structure upon failure if
615 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
619 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620{
Janos Follath24eed8d2019-11-22 13:21:35 +0000621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
624 (const unsigned char *) *p, end - *p)) == 0) {
625 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200626 }
627
628 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 */
631 *p = (unsigned char *) end;
632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200635#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638/*
639 * RSAPublicKey ::= SEQUENCE {
640 * modulus INTEGER, -- n
641 * publicExponent INTEGER -- e
642 * }
643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644static int pk_get_rsapubkey(unsigned char **p,
645 const unsigned char *end,
646 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647{
Janos Follath24eed8d2019-11-22 13:21:35 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649 size_t len;
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
652 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
653 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
654 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (*p + len != end) {
657 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
658 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
659 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100661 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
664 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
667 NULL, 0, NULL, 0)) != 0) {
668 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
669 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100670
671 *p += len;
672
673 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
675 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
676 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
679 NULL, 0, *p, len)) != 0) {
680 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
681 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100682
683 *p += len;
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (mbedtls_rsa_complete(rsa) != 0 ||
686 mbedtls_rsa_check_pubkey(rsa) != 0) {
687 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000688 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (*p != end) {
691 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
692 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
693 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698
699/* Get a PK algorithm identifier
700 *
701 * AlgorithmIdentifier ::= SEQUENCE {
702 * algorithm OBJECT IDENTIFIER,
703 * parameters ANY DEFINED BY algorithm OPTIONAL }
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705static int pk_get_pk_alg(unsigned char **p,
706 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200707 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
708 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709{
Janos Follath24eed8d2019-11-22 13:21:35 +0000710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
716 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
717 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718
Jethro Beekman01672442023-04-19 14:08:14 +0200719 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
720#if defined(MBEDTLS_ECP_LIGHT)
721 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
722 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
723 if (ret == 0) {
724 *pk_alg = MBEDTLS_PK_ECKEY;
725 }
726 }
727#else
728 (void) ec_grp_id;
729#endif
730 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
732 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733
734 /*
735 * No parameters with RSA (only for EC)
736 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (*pk_alg == MBEDTLS_PK_RSA &&
738 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
739 params->len != 0)) {
740 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 }
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200744}
745
746/*
747 * SubjectPublicKeyInfo ::= SEQUENCE {
748 * algorithm AlgorithmIdentifier,
749 * subjectPublicKey BIT STRING }
750 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
752 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200753{
Janos Follath24eed8d2019-11-22 13:21:35 +0000754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_asn1_buf alg_params;
757 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200758 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
762 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764 }
765
766 end = *p + len;
767
Jethro Beekman01672442023-04-19 14:08:14 +0200768 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return ret;
770 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
774 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (*p + len != end) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
779 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
782 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
783 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
786 return ret;
787 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (pk_alg == MBEDTLS_PK_RSA) {
791 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200794#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200796#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
797 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
798 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, &mbedtls_pk_ec(*pk)->grp);
799 } else
800#endif
801 {
802 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
803 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ret == 0) {
805 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
806 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200807 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200808#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (ret == 0 && *p != end) {
812 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
813 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
814 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (ret != 0) {
817 mbedtls_pk_free(pk);
818 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821}
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100825 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
826 *
827 * The value zero is:
828 * - never a valid value for an RSA parameter
829 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
830 *
831 * Since values can't be omitted in PKCS#1, passing a zero value to
832 * rsa_complete() would be incorrect, so reject zero values early.
833 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834static int asn1_get_nonzero_mpi(unsigned char **p,
835 const unsigned char *end,
836 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100837{
838 int ret;
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 ret = mbedtls_asn1_get_mpi(p, end, X);
841 if (ret != 0) {
842 return ret;
843 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
846 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
847 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100850}
851
852/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 * Parse a PKCS#1 encoded private RSA key
854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
856 const unsigned char *key,
857 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100859 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860 size_t len;
861 unsigned char *p, *end;
862
Hanno Beckerefa14e82017-10-11 19:45:19 +0100863 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100865
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 p = (unsigned char *) key;
867 end = p + keylen;
868
869 /*
870 * This function parses the RSAPrivateKey (PKCS#1)
871 *
872 * RSAPrivateKey ::= SEQUENCE {
873 * version Version,
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER, -- e
876 * privateExponent INTEGER, -- d
877 * prime1 INTEGER, -- p
878 * prime2 INTEGER, -- q
879 * exponent1 INTEGER, -- d mod (p-1)
880 * exponent2 INTEGER, -- d mod (q-1)
881 * coefficient INTEGER, -- (inverse of q) mod p
882 * otherPrimeInfos OtherPrimeInfos OPTIONAL
883 * }
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
886 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
887 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888 }
889
890 end = p + len;
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
893 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 }
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (version != 0) {
897 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100900 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
902 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
903 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100904 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100907 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
909 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
910 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100911 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100913
914 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
916 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
917 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100918 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100920
921 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
923 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
924 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100925 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100927
928 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
930 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
931 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100932 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100934
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100935#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500936 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
938 * that they can be easily recomputed from D, P and Q. However by
939 * parsing them from the PKCS1 structure it is possible to avoid
940 * recalculating them which both reduces the overhead of loading
941 * RSA private keys into memory and also avoids side channels which
942 * can arise when computing those values, since all of D, P, and Q
943 * are secret. See https://eprint.iacr.org/2020/055 for a
944 * description of one such attack.
945 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500946
Jack Lloyd80cc8112020-01-22 17:34:29 -0500947 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
949 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
950 goto cleanup;
951 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500952
953 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
955 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
956 goto cleanup;
957 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500958
959 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
961 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
962 goto cleanup;
963 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500964
Jack Lloyd60239752020-01-27 17:53:36 -0500965#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800966 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
968 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
969 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
970 goto cleanup;
971 }
Jack Lloyd60239752020-01-27 17:53:36 -0500972#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500973
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100974 /* rsa_complete() doesn't complete anything with the default
975 * implementation but is still called:
976 * - for the benefit of alternative implementation that may want to
977 * pre-compute stuff beyond what's provided (eg Montgomery factors)
978 * - as is also sanity-checks the key
979 *
980 * Furthermore, we also check the public part for consistency with
981 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
984 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100985 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100986 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (p != end) {
989 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
990 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991 }
992
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100993cleanup:
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100998 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if ((ret & 0xff80) == 0) {
1000 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1001 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001002 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 }
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011
Valerio Setti0d2980f2023-04-05 18:17:13 +02001012#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013/*
1014 * Parse a SEC1 encoded private EC key
1015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
1017 const unsigned char *key, size_t keylen,
1018 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019{
Janos Follath24eed8d2019-11-22 13:21:35 +00001020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001021 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001022 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001023 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001025 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 unsigned char *end = p + keylen;
1027 unsigned char *end2;
1028
1029 /*
1030 * RFC 5915, or SEC1 Appendix C.4
1031 *
1032 * ECPrivateKey ::= SEQUENCE {
1033 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1034 * privateKey OCTET STRING,
1035 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1036 * publicKey [1] BIT STRING OPTIONAL
1037 * }
1038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1040 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1041 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001042 }
1043
1044 end = p + len;
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1047 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1048 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (version != 1) {
1051 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1052 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1055 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1056 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057
Jethro Beekman01672442023-04-19 14:08:14 +02001058 d = p;
1059 d_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1061 mbedtls_ecp_keypair_free(eck);
1062 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063 }
1064
1065 p += len;
1066
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001067 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001069 /*
1070 * Is 'parameters' present?
1071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1073 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1074 0)) == 0) {
1075 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
1076 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
1077 mbedtls_ecp_keypair_free(eck);
1078 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001079 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1081 mbedtls_ecp_keypair_free(eck);
1082 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001083 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001084 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001087 /*
1088 * Is 'publickey' present? If not, or if we can't read it (eg because it
1089 * is compressed), create it from the private key.
1090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1092 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1093 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001094 end2 = p + len;
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1097 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1098 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (p + len != end2) {
1101 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1102 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1103 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001106 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001108 /*
1109 * The only acceptable failure mode of pk_get_ecpubkey() above
1110 * is if the point format is not recognized.
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1113 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1114 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001115 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1117 mbedtls_ecp_keypair_free(eck);
1118 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001119 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001121
Valerio Setti34f67552023-04-03 15:19:18 +02001122 if (!pubkey_done) {
Jethro Beekman01672442023-04-19 14:08:14 +02001123 if ((ret = pk_derive_public_key(eck, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001124 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001125 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001126 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001127 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1130 mbedtls_ecp_keypair_free(eck);
1131 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001136#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137
1138/*
1139 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001140 *
1141 * Notes:
1142 *
1143 * - This function does not own the key buffer. It is the
1144 * responsibility of the caller to take care of zeroizing
1145 * and freeing it after use.
1146 *
1147 * - The function is responsible for freeing the provided
1148 * PK context on failure.
1149 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 */
1151static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_pk_context *pk,
1153 const unsigned char *key, size_t keylen,
1154 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155{
1156 int ret, version;
1157 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 unsigned char *p = (unsigned char *) key;
1160 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001162 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
Jethro Beekman01672442023-04-19 14:08:14 +02001165#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001166 (void) f_rng;
1167 (void) p_rng;
1168#endif
1169
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001171 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 *
1173 * PrivateKeyInfo ::= SEQUENCE {
1174 * version Version,
1175 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1176 * privateKey PrivateKey,
1177 * attributes [0] IMPLICIT Attributes OPTIONAL }
1178 *
1179 * Version ::= INTEGER
1180 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1181 * PrivateKey ::= OCTET STRING
1182 *
1183 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1184 */
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1187 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 }
1190
1191 end = p + len;
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (version != 0) {
1198 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
Jethro Beekman01672442023-04-19 14:08:14 +02001201 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return ret;
1203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (len < 1) {
1210 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1211 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1212 }
1213
1214 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1215 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1216 }
1217
1218 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1219 return ret;
1220 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (pk_alg == MBEDTLS_PK_RSA) {
1224 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1225 mbedtls_pk_free(pk);
1226 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 }
1228 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001230#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001232#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
1233 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
1234 if ((ret =
1235 pk_use_ecparams_rfc8410(&params, ec_grp_id, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1236 (ret =
1237 pk_parse_key_rfc8410_der(mbedtls_pk_ec(*pk), p, len, end, f_rng,
1238 p_rng)) != 0) {
1239 mbedtls_pk_free(pk);
1240 return ret;
1241 }
1242 } else
1243#endif
1244 {
1245 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1246 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1247 mbedtls_pk_free(pk);
1248 return ret;
1249 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 }
1251 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001252#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256}
1257
1258/*
1259 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001260 *
1261 * To save space, the decryption happens in-place on the given key buffer.
1262 * Also, while this function may modify the keybuffer, it doesn't own it,
1263 * and instead it is the responsibility of the caller to zeroize and properly
1264 * free it after use.
1265 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 mbedtls_pk_context *pk,
1270 unsigned char *key, size_t keylen,
1271 const unsigned char *pwd, size_t pwdlen,
1272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001274 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001276 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1279#if defined(MBEDTLS_PKCS12_C)
1280 mbedtls_cipher_type_t cipher_alg;
1281 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282#endif
1283
Hanno Becker2aa80a72017-09-07 15:28:45 +01001284 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 end = p + keylen;
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (pwdlen == 0) {
1288 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1289 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
1291 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001292 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 *
1294 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1295 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1296 * encryptedData EncryptedData
1297 * }
1298 *
1299 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1300 *
1301 * EncryptedData ::= OCTET STRING
1302 *
1303 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001304 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1307 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 }
1310
1311 end = p + len;
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1314 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1315 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1319 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320
Hanno Beckerfab35692017-08-25 13:38:26 +01001321 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322
1323 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001324 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1328 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1329 cipher_alg, md_alg,
1330 pwd, pwdlen, p, len, buf)) != 0) {
1331 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1332 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1333 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001337
1338 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_PKCS12_C */
1341#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1343 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1344 p, len, buf)) != 0) {
1345 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1346 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1347 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001351
1352 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001355 {
1356 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001357 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (decrypted == 0) {
1360 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1361 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366
1367/*
1368 * Parse a private key
1369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1371 const unsigned char *key, size_t keylen,
1372 const unsigned char *pwd, size_t pwdlen,
1373 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374{
Janos Follath24eed8d2019-11-22 13:21:35 +00001375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001380#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (keylen == 0) {
1383 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1384 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001385
1386#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001390 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001392 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 } else {
1394 ret = mbedtls_pem_read_buffer(&pem,
1395 "-----BEGIN RSA PRIVATE KEY-----",
1396 "-----END RSA PRIVATE KEY-----",
1397 key, pwd, pwdlen, &len);
1398 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (ret == 0) {
1401 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1402 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1403 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1404 pem.buf, pem.buflen)) != 0) {
1405 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 }
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 mbedtls_pem_free(&pem);
1409 return ret;
1410 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1411 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1412 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1413 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1414 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1415 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Valerio Setti0d2980f2023-04-05 18:17:13 +02001419#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001420 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001422 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 } else {
1424 ret = mbedtls_pem_read_buffer(&pem,
1425 "-----BEGIN EC PRIVATE KEY-----",
1426 "-----END EC PRIVATE KEY-----",
1427 key, pwd, pwdlen, &len);
1428 }
1429 if (ret == 0) {
1430 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1433 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1434 pem.buf, pem.buflen,
1435 f_rng, p_rng)) != 0) {
1436 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 }
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_pem_free(&pem);
1440 return ret;
1441 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1442 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1443 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1444 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1445 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1446 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001448#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 } else {
1454 ret = mbedtls_pem_read_buffer(&pem,
1455 "-----BEGIN PRIVATE KEY-----",
1456 "-----END PRIVATE KEY-----",
1457 key, NULL, 0, &len);
1458 }
1459 if (ret == 0) {
1460 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1461 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1462 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 }
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 mbedtls_pem_free(&pem);
1466 return ret;
1467 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1468 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001472 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001474 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 } else {
1476 ret = mbedtls_pem_read_buffer(&pem,
1477 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1478 "-----END ENCRYPTED PRIVATE KEY-----",
1479 key, NULL, 0, &len);
1480 }
1481 if (ret == 0) {
1482 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1483 pwd, pwdlen, f_rng, p_rng)) != 0) {
1484 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 }
1486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_pem_free(&pem);
1488 return ret;
1489 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1490 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493#else
1494 ((void) pwd);
1495 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497
1498 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001499 * At this point we only know it's not a PEM formatted key. Could be any
1500 * of the known DER encoded private key formats
1501 *
1502 * We try the different DER format parsers to see if one passes without
1503 * error
1504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001507 unsigned char *key_copy;
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1510 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1511 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1516 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 mbedtls_platform_zeroize(key_copy, keylen);
1519 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 }
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (ret == 0) {
1523 return 0;
1524 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 mbedtls_pk_free(pk);
1527 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1530 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1535 if (ret == 0) {
1536 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001537 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 mbedtls_pk_free(pk);
1540 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1545 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1546 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1547 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548 }
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 mbedtls_pk_free(pk);
1551 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001553
Valerio Setti0d2980f2023-04-05 18:17:13 +02001554#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1556 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1557 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1558 key, keylen, f_rng, p_rng) == 0) {
1559 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001562#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563
Hanno Becker780f0a42018-10-10 11:23:33 +01001564 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1565 * it is ok to leave the PK context initialized but not
1566 * freed: It is the caller's responsibility to call pk_init()
1567 * before calling this function, and to call pk_free()
1568 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1569 * isn't, this leads to mbedtls_pk_free() being called
1570 * twice, once here and once by the caller, but this is
1571 * also ok and in line with the mbedtls_pk_free() calls
1572 * on failed PEM parsing attempts. */
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575}
1576
1577/*
1578 * Parse a public key
1579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1581 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582{
Janos Follath24eed8d2019-11-22 13:21:35 +00001583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001585#if defined(MBEDTLS_RSA_C)
1586 const mbedtls_pk_info_t *pk_info;
1587#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001591#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (keylen == 0) {
1594 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1595 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001596
1597#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001599#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001600 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001602 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 } else {
1604 ret = mbedtls_pem_read_buffer(&pem,
1605 "-----BEGIN RSA PUBLIC KEY-----",
1606 "-----END RSA PUBLIC KEY-----",
1607 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001608 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001609
1610 if (ret == 0) {
1611 p = pem.buf;
1612 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1613 mbedtls_pem_free(&pem);
1614 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1615 }
1616
1617 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1618 mbedtls_pem_free(&pem);
1619 return ret;
1620 }
1621
1622 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1623 mbedtls_pk_free(ctx);
1624 }
1625
1626 mbedtls_pem_free(&pem);
1627 return ret;
1628 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1629 mbedtls_pem_free(&pem);
1630 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001631 }
1632#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001633
1634 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001636 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 } else {
1638 ret = mbedtls_pem_read_buffer(&pem,
1639 "-----BEGIN PUBLIC KEY-----",
1640 "-----END PUBLIC KEY-----",
1641 key, NULL, 0, &len);
1642 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645 /*
1646 * Was PEM encoded
1647 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001648 p = pem.buf;
1649
Jethro Beekman01672442023-04-19 14:08:14 +02001650 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 mbedtls_pem_free(&pem);
1652 return ret;
1653 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1654 mbedtls_pem_free(&pem);
1655 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001659
1660#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1662 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001664
1665 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1666 return ret;
1667 }
1668
1669 p = (unsigned char *) key;
1670 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1671 if (ret == 0) {
1672 return ret;
1673 }
1674 mbedtls_pk_free(ctx);
1675 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1676 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1677 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001678 }
1679#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001680 p = (unsigned char *) key;
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685}
1686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#endif /* MBEDTLS_PK_PARSE_C */