blob: 617e4fdd9191bf0dab216a5327130546dfe4c4f2 [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
593 if ((ret = pk_derive_public_key(eck, key, len, f_rng, p_rng)) != 0) {
594 mbedtls_ecp_keypair_free(eck);
595 return ret;
596 }
597
598 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
599 mbedtls_ecp_keypair_free(eck);
600 return ret;
601 }
602
603 return 0;
604}
605#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
606#endif /* MBEDTLS_ECP_LIGHT */
607
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608/*
609 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100610 *
611 * The caller is responsible for clearing the structure upon failure if
612 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
616 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617{
Janos Follath24eed8d2019-11-22 13:21:35 +0000618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
621 (const unsigned char *) *p, end - *p)) == 0) {
622 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623 }
624
625 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627 */
628 *p = (unsigned char *) end;
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200632#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635/*
636 * RSAPublicKey ::= SEQUENCE {
637 * modulus INTEGER, -- n
638 * publicExponent INTEGER -- e
639 * }
640 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641static int pk_get_rsapubkey(unsigned char **p,
642 const unsigned char *end,
643 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644{
Janos Follath24eed8d2019-11-22 13:21:35 +0000645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646 size_t len;
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
649 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
650 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
651 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (*p + len != end) {
654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
655 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
656 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100658 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
660 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
661 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
664 NULL, 0, NULL, 0)) != 0) {
665 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
666 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100667
668 *p += len;
669
670 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
672 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
673 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
676 NULL, 0, *p, len)) != 0) {
677 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
678 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100679
680 *p += len;
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (mbedtls_rsa_complete(rsa) != 0 ||
683 mbedtls_rsa_check_pubkey(rsa) != 0) {
684 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000685 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (*p != end) {
688 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
689 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
690 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695
696/* Get a PK algorithm identifier
697 *
698 * AlgorithmIdentifier ::= SEQUENCE {
699 * algorithm OBJECT IDENTIFIER,
700 * parameters ANY DEFINED BY algorithm OPTIONAL }
701 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702static int pk_get_pk_alg(unsigned char **p,
703 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200704 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
705 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706{
Janos Follath24eed8d2019-11-22 13:21:35 +0000707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
713 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
714 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715
Jethro Beekman01672442023-04-19 14:08:14 +0200716 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
717#if defined(MBEDTLS_ECP_LIGHT)
718 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
719 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
720 if (ret == 0) {
721 *pk_alg = MBEDTLS_PK_ECKEY;
722 }
723 }
724#else
725 (void) ec_grp_id;
726#endif
727 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
729 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730
731 /*
732 * No parameters with RSA (only for EC)
733 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (*pk_alg == MBEDTLS_PK_RSA &&
735 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
736 params->len != 0)) {
737 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738 }
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741}
742
743/*
744 * SubjectPublicKeyInfo ::= SEQUENCE {
745 * algorithm AlgorithmIdentifier,
746 * subjectPublicKey BIT STRING }
747 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
749 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750{
Janos Follath24eed8d2019-11-22 13:21:35 +0000751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_asn1_buf alg_params;
754 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200755 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
759 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
760 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 }
762
763 end = *p + len;
764
Jethro Beekman01672442023-04-19 14:08:14 +0200765 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 return ret;
767 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
770 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
771 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (*p + len != end) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
775 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
776 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
779 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
780 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
783 return ret;
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (pk_alg == MBEDTLS_PK_RSA) {
788 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200791#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200793#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
794 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
795 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, &mbedtls_pk_ec(*pk)->grp);
796 } else
797#endif
798 {
799 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
800 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (ret == 0) {
802 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
803 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200805#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (ret == 0 && *p != end) {
809 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
810 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (ret != 0) {
814 mbedtls_pk_free(pk);
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818}
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100822 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
823 *
824 * The value zero is:
825 * - never a valid value for an RSA parameter
826 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
827 *
828 * Since values can't be omitted in PKCS#1, passing a zero value to
829 * rsa_complete() would be incorrect, so reject zero values early.
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static int asn1_get_nonzero_mpi(unsigned char **p,
832 const unsigned char *end,
833 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100834{
835 int ret;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 ret = mbedtls_asn1_get_mpi(p, end, X);
838 if (ret != 0) {
839 return ret;
840 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
843 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
844 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100847}
848
849/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 * Parse a PKCS#1 encoded private RSA key
851 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
853 const unsigned char *key,
854 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100856 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857 size_t len;
858 unsigned char *p, *end;
859
Hanno Beckerefa14e82017-10-11 19:45:19 +0100860 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100862
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 p = (unsigned char *) key;
864 end = p + keylen;
865
866 /*
867 * This function parses the RSAPrivateKey (PKCS#1)
868 *
869 * RSAPrivateKey ::= SEQUENCE {
870 * version Version,
871 * modulus INTEGER, -- n
872 * publicExponent INTEGER, -- e
873 * privateExponent INTEGER, -- d
874 * prime1 INTEGER, -- p
875 * prime2 INTEGER, -- q
876 * exponent1 INTEGER, -- d mod (p-1)
877 * exponent2 INTEGER, -- d mod (q-1)
878 * coefficient INTEGER, -- (inverse of q) mod p
879 * otherPrimeInfos OtherPrimeInfos OPTIONAL
880 * }
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
883 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
884 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 }
886
887 end = p + len;
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
890 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891 }
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (version != 0) {
894 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 }
896
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100897 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
899 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
900 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100901 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100904 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
906 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
907 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100908 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100910
911 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
913 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
914 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100915 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100917
918 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
920 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
921 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100922 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100924
925 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
927 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
928 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100929 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100931
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100932#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500933 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
935 * that they can be easily recomputed from D, P and Q. However by
936 * parsing them from the PKCS1 structure it is possible to avoid
937 * recalculating them which both reduces the overhead of loading
938 * RSA private keys into memory and also avoids side channels which
939 * can arise when computing those values, since all of D, P, and Q
940 * are secret. See https://eprint.iacr.org/2020/055 for a
941 * description of one such attack.
942 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500943
Jack Lloyd80cc8112020-01-22 17:34:29 -0500944 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
946 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
947 goto cleanup;
948 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500949
950 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
952 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
953 goto cleanup;
954 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500955
956 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
958 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
959 goto cleanup;
960 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500961
Jack Lloyd60239752020-01-27 17:53:36 -0500962#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800963 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
965 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
966 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
967 goto cleanup;
968 }
Jack Lloyd60239752020-01-27 17:53:36 -0500969#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500970
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100971 /* rsa_complete() doesn't complete anything with the default
972 * implementation but is still called:
973 * - for the benefit of alternative implementation that may want to
974 * pre-compute stuff beyond what's provided (eg Montgomery factors)
975 * - as is also sanity-checks the key
976 *
977 * Furthermore, we also check the public part for consistency with
978 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
979 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
981 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100982 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100983 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (p != end) {
986 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
987 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 }
989
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100990cleanup:
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100995 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if ((ret & 0xff80) == 0) {
997 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
998 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100999 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008
Valerio Setti0d2980f2023-04-05 18:17:13 +02001009#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010/*
1011 * Parse a SEC1 encoded private EC key
1012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
1014 const unsigned char *key, size_t keylen,
1015 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016{
Janos Follath24eed8d2019-11-22 13:21:35 +00001017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001018 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001019 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001020 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001022 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 unsigned char *end = p + keylen;
1024 unsigned char *end2;
1025
1026 /*
1027 * RFC 5915, or SEC1 Appendix C.4
1028 *
1029 * ECPrivateKey ::= SEQUENCE {
1030 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1031 * privateKey OCTET STRING,
1032 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1033 * publicKey [1] BIT STRING OPTIONAL
1034 * }
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1037 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1038 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 }
1040
1041 end = p + len;
1042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1044 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1045 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (version != 1) {
1048 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1049 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1052 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1053 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054
Jethro Beekman01672442023-04-19 14:08:14 +02001055 d = p;
1056 d_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1058 mbedtls_ecp_keypair_free(eck);
1059 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 }
1061
1062 p += len;
1063
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001064 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001066 /*
1067 * Is 'parameters' present?
1068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1070 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1071 0)) == 0) {
1072 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
1073 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
1074 mbedtls_ecp_keypair_free(eck);
1075 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1078 mbedtls_ecp_keypair_free(eck);
1079 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001080 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001081 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001084 /*
1085 * Is 'publickey' present? If not, or if we can't read it (eg because it
1086 * is compressed), create it from the private key.
1087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1089 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1090 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001091 end2 = p + len;
1092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1094 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1095 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (p + len != end2) {
1098 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1099 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1100 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001103 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001105 /*
1106 * The only acceptable failure mode of pk_get_ecpubkey() above
1107 * is if the point format is not recognized.
1108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1110 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1111 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1114 mbedtls_ecp_keypair_free(eck);
1115 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001116 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001118
Valerio Setti34f67552023-04-03 15:19:18 +02001119 if (!pubkey_done) {
Jethro Beekman01672442023-04-19 14:08:14 +02001120 if ((ret = pk_derive_public_key(eck, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001121 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001122 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001123 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001124 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1127 mbedtls_ecp_keypair_free(eck);
1128 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 }
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001133#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134
1135/*
1136 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001137 *
1138 * Notes:
1139 *
1140 * - This function does not own the key buffer. It is the
1141 * responsibility of the caller to take care of zeroizing
1142 * and freeing it after use.
1143 *
1144 * - The function is responsible for freeing the provided
1145 * PK context on failure.
1146 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 */
1148static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 mbedtls_pk_context *pk,
1150 const unsigned char *key, size_t keylen,
1151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152{
1153 int ret, version;
1154 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 unsigned char *p = (unsigned char *) key;
1157 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001159 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161
Jethro Beekman01672442023-04-19 14:08:14 +02001162#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001163 (void) f_rng;
1164 (void) p_rng;
1165#endif
1166
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001168 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 *
1170 * PrivateKeyInfo ::= SEQUENCE {
1171 * version Version,
1172 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1173 * privateKey PrivateKey,
1174 * attributes [0] IMPLICIT Attributes OPTIONAL }
1175 *
1176 * Version ::= INTEGER
1177 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1178 * PrivateKey ::= OCTET STRING
1179 *
1180 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1181 */
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1184 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1185 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 }
1187
1188 end = p + len;
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001192 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (version != 0) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Jethro Beekman01672442023-04-19 14:08:14 +02001198 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return ret;
1200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (len < 1) {
1207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1208 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1209 }
1210
1211 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1212 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1213 }
1214
1215 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1216 return ret;
1217 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (pk_alg == MBEDTLS_PK_RSA) {
1221 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1222 mbedtls_pk_free(pk);
1223 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224 }
1225 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001227#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001229#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
1230 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
1231 if ((ret =
1232 pk_use_ecparams_rfc8410(&params, ec_grp_id, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1233 (ret =
1234 pk_parse_key_rfc8410_der(mbedtls_pk_ec(*pk), p, len, end, f_rng,
1235 p_rng)) != 0) {
1236 mbedtls_pk_free(pk);
1237 return ret;
1238 }
1239 } else
1240#endif
1241 {
1242 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1243 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1244 mbedtls_pk_free(pk);
1245 return ret;
1246 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
1248 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001249#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253}
1254
1255/*
1256 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001257 *
1258 * To save space, the decryption happens in-place on the given key buffer.
1259 * Also, while this function may modify the keybuffer, it doesn't own it,
1260 * and instead it is the responsibility of the caller to zeroize and properly
1261 * free it after use.
1262 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_pk_context *pk,
1267 unsigned char *key, size_t keylen,
1268 const unsigned char *pwd, size_t pwdlen,
1269 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001271 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001273 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1276#if defined(MBEDTLS_PKCS12_C)
1277 mbedtls_cipher_type_t cipher_alg;
1278 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279#endif
1280
Hanno Becker2aa80a72017-09-07 15:28:45 +01001281 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282 end = p + keylen;
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (pwdlen == 0) {
1285 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1286 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
1288 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001289 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 *
1291 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1292 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1293 * encryptedData EncryptedData
1294 * }
1295 *
1296 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1297 *
1298 * EncryptedData ::= OCTET STRING
1299 *
1300 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001301 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1304 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1305 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306 }
1307
1308 end = p + len;
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1311 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1312 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1316 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
Hanno Beckerfab35692017-08-25 13:38:26 +01001318 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319
1320 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001321 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1325 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1326 cipher_alg, md_alg,
1327 pwd, pwdlen, p, len, buf)) != 0) {
1328 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1329 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1330 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001334
1335 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_PKCS12_C */
1338#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1340 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1341 p, len, buf)) != 0) {
1342 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1343 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001348
1349 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001352 {
1353 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001354 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (decrypted == 0) {
1357 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1358 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363
1364/*
1365 * Parse a private key
1366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1368 const unsigned char *key, size_t keylen,
1369 const unsigned char *pwd, size_t pwdlen,
1370 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371{
Janos Follath24eed8d2019-11-22 13:21:35 +00001372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001377#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (keylen == 0) {
1380 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1381 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001382
1383#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001387 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001389 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 } else {
1391 ret = mbedtls_pem_read_buffer(&pem,
1392 "-----BEGIN RSA PRIVATE KEY-----",
1393 "-----END RSA PRIVATE KEY-----",
1394 key, pwd, pwdlen, &len);
1395 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (ret == 0) {
1398 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1399 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1400 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1401 pem.buf, pem.buflen)) != 0) {
1402 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 mbedtls_pem_free(&pem);
1406 return ret;
1407 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1408 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1409 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1410 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1411 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1412 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415
Valerio Setti0d2980f2023-04-05 18:17:13 +02001416#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001417 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001419 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 } else {
1421 ret = mbedtls_pem_read_buffer(&pem,
1422 "-----BEGIN EC PRIVATE KEY-----",
1423 "-----END EC PRIVATE KEY-----",
1424 key, pwd, pwdlen, &len);
1425 }
1426 if (ret == 0) {
1427 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1430 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1431 pem.buf, pem.buflen,
1432 f_rng, p_rng)) != 0) {
1433 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 }
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_pem_free(&pem);
1437 return ret;
1438 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1439 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1440 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1441 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1442 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1443 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001445#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 } else {
1451 ret = mbedtls_pem_read_buffer(&pem,
1452 "-----BEGIN PRIVATE KEY-----",
1453 "-----END PRIVATE KEY-----",
1454 key, NULL, 0, &len);
1455 }
1456 if (ret == 0) {
1457 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1458 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1459 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 }
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 mbedtls_pem_free(&pem);
1463 return ret;
1464 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1465 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001469 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001471 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 } else {
1473 ret = mbedtls_pem_read_buffer(&pem,
1474 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1475 "-----END ENCRYPTED PRIVATE KEY-----",
1476 key, NULL, 0, &len);
1477 }
1478 if (ret == 0) {
1479 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1480 pwd, pwdlen, f_rng, p_rng)) != 0) {
1481 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 }
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 mbedtls_pem_free(&pem);
1485 return ret;
1486 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1487 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490#else
1491 ((void) pwd);
1492 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
1495 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001496 * At this point we only know it's not a PEM formatted key. Could be any
1497 * of the known DER encoded private key formats
1498 *
1499 * We try the different DER format parsers to see if one passes without
1500 * error
1501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001504 unsigned char *key_copy;
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1507 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1508 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1513 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 mbedtls_platform_zeroize(key_copy, keylen);
1516 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517 }
1518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (ret == 0) {
1520 return 0;
1521 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_pk_free(pk);
1524 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1527 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1532 if (ret == 0) {
1533 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001534 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_pk_free(pk);
1537 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1542 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1543 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1544 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545 }
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 mbedtls_pk_free(pk);
1548 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001550
Valerio Setti0d2980f2023-04-05 18:17:13 +02001551#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1553 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1554 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1555 key, keylen, f_rng, p_rng) == 0) {
1556 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001557 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001559#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560
Hanno Becker780f0a42018-10-10 11:23:33 +01001561 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1562 * it is ok to leave the PK context initialized but not
1563 * freed: It is the caller's responsibility to call pk_init()
1564 * before calling this function, and to call pk_free()
1565 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1566 * isn't, this leads to mbedtls_pk_free() being called
1567 * twice, once here and once by the caller, but this is
1568 * also ok and in line with the mbedtls_pk_free() calls
1569 * on failed PEM parsing attempts. */
1570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001572}
1573
1574/*
1575 * Parse a public key
1576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1578 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001579{
Janos Follath24eed8d2019-11-22 13:21:35 +00001580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001581 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001582#if defined(MBEDTLS_RSA_C)
1583 const mbedtls_pk_info_t *pk_info;
1584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001588#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (keylen == 0) {
1591 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1592 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001593
1594#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001596#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001597 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001599 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 } else {
1601 ret = mbedtls_pem_read_buffer(&pem,
1602 "-----BEGIN RSA PUBLIC KEY-----",
1603 "-----END RSA PUBLIC KEY-----",
1604 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001605 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001606
1607 if (ret == 0) {
1608 p = pem.buf;
1609 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1610 mbedtls_pem_free(&pem);
1611 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1612 }
1613
1614 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1615 mbedtls_pem_free(&pem);
1616 return ret;
1617 }
1618
1619 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1620 mbedtls_pk_free(ctx);
1621 }
1622
1623 mbedtls_pem_free(&pem);
1624 return ret;
1625 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1626 mbedtls_pem_free(&pem);
1627 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001628 }
1629#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001630
1631 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001633 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 } else {
1635 ret = mbedtls_pem_read_buffer(&pem,
1636 "-----BEGIN PUBLIC KEY-----",
1637 "-----END PUBLIC KEY-----",
1638 key, NULL, 0, &len);
1639 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642 /*
1643 * Was PEM encoded
1644 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001645 p = pem.buf;
1646
Jethro Beekman01672442023-04-19 14:08:14 +02001647 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 mbedtls_pem_free(&pem);
1649 return ret;
1650 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1651 mbedtls_pem_free(&pem);
1652 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001656
1657#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1659 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001660 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001661
1662 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1663 return ret;
1664 }
1665
1666 p = (unsigned char *) key;
1667 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1668 if (ret == 0) {
1669 return ret;
1670 }
1671 mbedtls_pk_free(ctx);
1672 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1673 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1674 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001675 }
1676#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001677 p = (unsigned char *) key;
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682}
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_PK_PARSE_C */