blob: 07fce5c1c90e23fa9feb0de7523596ce81da8e18 [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"
Valerio Setti77a75682023-05-15 11:18:46 +020029#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Jethro Beekman01672442023-04-19 14:08:14 +020037#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
38#include "pkwrite.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +020040#if defined(MBEDTLS_ECP_LIGHT)
41#include "pk_internal.h"
42#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
55
Valerio Setti34f67552023-04-03 15:19:18 +020056#if defined(MBEDTLS_PSA_CRYPTO_C)
57#include "mbedtls/psa_util.h"
58#endif
59
60#if defined(MBEDTLS_USE_PSA_CRYPTO)
61#include "psa/crypto.h"
62#endif
63
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020065
Valerio Settie0e63112023-05-18 18:48:07 +020066/* Helper for Montgomery curves */
67#if defined(MBEDTLS_ECP_LIGHT) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
68#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
69 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
70#endif /* MBEDTLS_ECP_LIGHT && MBEDTLS_PK_HAVE_RFC8410_CURVES */
71
Gilles Peskine832f3492017-11-30 11:42:12 +010072#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073/*
74 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020075 *
76 * The file is expected to contain either PEM or DER encoded data.
77 * A terminating null byte is always appended. It is included in the announced
78 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020081{
82 FILE *f;
83 long size;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if ((f = fopen(path, "rb")) == NULL) {
86 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
87 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
Gilles Peskineda0913b2022-06-30 17:03:40 +020089 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fseek(f, 0, SEEK_END);
93 if ((size = ftell(f)) == -1) {
94 fclose(f);
95 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
Gilles Peskine449bd832023-01-11 14:50:10 +010097 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 *n = (size_t) size;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (*n + 1 == 0 ||
102 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
103 fclose(f);
104 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (fread(*buf, 1, *n, f) != *n) {
108 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_platform_zeroize(*buf, *n);
111 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
118 (*buf)[*n] = '\0';
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200121 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125}
126
127/*
128 * Load and parse a private key
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
131 const char *path, const char *pwd,
132 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133{
Janos Follath24eed8d2019-11-22 13:21:35 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 size_t n;
136 unsigned char *buf;
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
139 return ret;
140 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (pwd == NULL) {
143 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
144 } else {
145 ret = mbedtls_pk_parse_key(ctx, buf, n,
146 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_platform_zeroize(buf, n);
150 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153}
154
155/*
156 * Load and parse a public key
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159{
Janos Follath24eed8d2019-11-22 13:21:35 +0000160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 size_t n;
162 unsigned char *buf;
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
165 return ret;
166 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_platform_zeroize(buf, n);
171 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176
Valerio Setti0d2980f2023-04-05 18:17:13 +0200177#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179 *
180 * ECParameters ::= CHOICE {
181 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100182 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 * }
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
187 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188{
Janos Follath24eed8d2019-11-22 13:21:35 +0000189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (end - *p < 1) {
192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
194 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100195
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 ) {
203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
204 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210
211 params->p = *p;
212 *p += params->len;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (*p != end) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
217 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
225 * WARNING: the resulting group should only be used with
226 * pk_group_id_from_specified(), since its base point may not be set correctly
227 * if it was encoded compressed.
228 *
229 * SpecifiedECDomain ::= SEQUENCE {
230 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
231 * fieldID FieldID {{FieldTypes}},
232 * curve Curve,
233 * base ECPoint,
234 * order INTEGER,
235 * cofactor INTEGER OPTIONAL,
236 * hash HashAlgorithm OPTIONAL,
237 * ...
238 * }
239 *
240 * We only support prime-field as field type, and ignore hash and cofactor.
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100243{
Janos Follath24eed8d2019-11-22 13:21:35 +0000244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200246 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100247 const unsigned char *end_field, *end_curve;
248 size_t len;
249 int ver;
250
251 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
254 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ver < 1 || ver > 3) {
257 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
258 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 /*
261 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
262 * fieldType FIELD-ID.&id({IOSet}),
263 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
264 * }
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
268 return ret;
269 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270
271 end_field = p + len;
272
273 /*
274 * FIELD-ID ::= TYPE-IDENTIFIER
275 * FieldTypes FIELD-ID ::= {
276 * { Prime-p IDENTIFIED BY prime-field } |
277 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
278 * }
279 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
286 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
287 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 }
289
290 p += len;
291
292 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
295 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (p != end_field) {
300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
302 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303
304 /*
305 * Curve ::= SEQUENCE {
306 * a FieldElement,
307 * b FieldElement,
308 * seed BIT STRING OPTIONAL
309 * -- Shall be present if used in SpecifiedECDomain
310 * -- with version equal to ecdpVer2 or ecdpVer3
311 * }
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
315 return ret;
316 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317
318 end_curve = p + len;
319
320 /*
321 * FieldElement ::= OCTET STRING
322 * containing an integer in the case of a prime field
323 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
325 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 }
328
329 p += len;
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
332 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 }
335
336 p += len;
337
338 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (p != end_curve) {
344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
345 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
346 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100347
348 /*
349 * ECPoint ::= OCTET STRING
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
353 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
356 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 /*
358 * If we can't read the point because it's compressed, cheat by
359 * reading only the X coordinate and the parity bit of Y.
360 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
362 (p[0] != 0x02 && p[0] != 0x03) ||
363 len != mbedtls_mpi_size(&grp->P) + 1 ||
364 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
365 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
366 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
367 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100368 }
369 }
370
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371 p += len;
372
373 /*
374 * order INTEGER
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
377 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
378 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
382 /*
383 * Allow optional elements by purposefully not enforcing p == end here.
384 */
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387}
388
389/*
390 * Find the group id associated with an (almost filled) group as generated by
391 * pk_group_from_specified(), or return an error if unknown.
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static 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 +0100394{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100395 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_group ref;
397 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_ecp_group_free(&ref);
404 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
408 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
409 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
410 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
411 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
412 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
413 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 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 +0100416 break;
417 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418 }
419
420cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 *grp_id = *id;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430}
431
432/*
433 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
436 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000450 /* The API respecting lifecycle for mbedtls_ecp_group struct is
451 * _init(), _load() and _free(). In pk_group_id_from_specified() the
452 * temporary grp breaks that flow and it's members are populated
453 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
454 * which is assuming a group populated by _setup() may not clean-up
455 * properly -> Manually free it's members.
456 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000457 mbedtls_mpi_free(&grp.N);
458 mbedtls_mpi_free(&grp.P);
459 mbedtls_mpi_free(&grp.A);
460 mbedtls_mpi_free(&grp.B);
461 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
Valerio Setti4064dbb2023-05-17 15:33:07 +0200467#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
468/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
469 * ecp_keypair structure with proper group ID. The purpose of this helper
470 * function is to update ec_family and ec_bits accordingly. */
471static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
472 mbedtls_ecp_group_id grp_id)
473{
474 psa_ecc_family_t ec_family;
475 size_t bits;
476
477 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
478
479 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
481 }
482
483 pk->ec_family = ec_family;
484 pk->ec_bits = bits;
485
486 return 0;
487}
488#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
489
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 *
493 * ECParameters ::= CHOICE {
494 * namedCurve OBJECT IDENTIFIER
495 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
496 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200498static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (params->tag == MBEDTLS_ASN1_OID) {
504 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
505 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
506 }
507 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
510 return ret;
511 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100512#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100514#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Valerio Setti00e8dd12023-05-18 18:56:59 +0200517#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
518 ret = pk_update_psa_ecparams(pk, grp_id);
519#else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200520 /* grp may already be initialized; if so, make sure IDs match */
521 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
522 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
524 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525
Valerio Setti4064dbb2023-05-17 15:33:07 +0200526 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
527 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return ret;
529 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200530#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531
Valerio Setti4064dbb2023-05-17 15:33:07 +0200532 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533}
534
Jethro Beekman01672442023-04-19 14:08:14 +0200535/*
536 * Helper function for deriving a public key from its private counterpart.
537 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200538static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200539 const unsigned char *d, size_t d_len,
540 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
541{
542 int ret;
543#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200544 psa_status_t status;
545 (void) f_rng;
546 (void) p_rng;
547#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
548 (void) d;
549 (void) d_len;
550
551 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
552 &pk->pub_raw_len);
553 ret = psa_pk_status_to_mbedtls(status);
554#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
555 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
556 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
557 size_t key_len;
558 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200559 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
560 size_t curve_bits;
561 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200562 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200563
564 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
565 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
566
567 status = psa_import_key(&key_attr, d, d_len, &key_id);
568 ret = psa_pk_status_to_mbedtls(status);
569 if (ret != 0) {
570 return ret;
571 }
572
Jethro Beekman01672442023-04-19 14:08:14 +0200573 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
574 ret = psa_pk_status_to_mbedtls(status);
575 destruction_status = psa_destroy_key(key_id);
576 if (ret != 0) {
577 return ret;
578 } else if (destruction_status != PSA_SUCCESS) {
579 return psa_pk_status_to_mbedtls(destruction_status);
580 }
Jethro Beekman01672442023-04-19 14:08:14 +0200581 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200582#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200583#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200584 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200585 (void) d;
586 (void) d_len;
587
588 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
589#endif /* MBEDTLS_USE_PSA_CRYPTO */
590 return ret;
591}
592
593#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
594
595/*
596 * Load an RFC8410 EC key, which doesn't have any parameters
597 */
598static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
599 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200600 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200601{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200602 int ret;
603
Jethro Beekman01672442023-04-19 14:08:14 +0200604 if (params->tag != 0 || params->len != 0) {
605 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
606 }
607
Valerio Setti00e8dd12023-05-18 18:56:59 +0200608#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
609 ret = pk_update_psa_ecparams(pk, grp_id);
610#else
611 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200612 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
613 if (ret != 0) {
614 return ret;
615 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200616#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +0200617 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200618}
619
620/*
621 * Parse an RFC 8410 encoded private EC key
622 *
623 * CurvePrivateKey ::= OCTET STRING
624 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200625static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200626 unsigned char *key, size_t keylen, const unsigned char *end,
627 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
628{
629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
630 size_t len;
631
632 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
634 }
635
636 if (key + len != end) {
637 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
638 }
639
Valerio Setti00e8dd12023-05-18 18:56:59 +0200640#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
642 psa_status_t status;
643
644 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
Valerio Setti51aa52e2023-05-24 12:37:50 +0200645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
646 PSA_KEY_USAGE_DERIVE);
647 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200648
649 status = psa_import_key(&attributes, key, len, &pk->priv_id);
650 if (status != PSA_SUCCESS) {
651 ret = psa_pk_status_to_mbedtls(status);
652 return ret;
653 }
654#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
655 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
656
Jethro Beekman01672442023-04-19 14:08:14 +0200657 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
658 mbedtls_ecp_keypair_free(eck);
659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
660 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200661#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200662
Valerio Setti4064dbb2023-05-17 15:33:07 +0200663 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
664 * which never contain a public key. As such, derive the public key
665 * unconditionally. */
666 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200667#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200668 mbedtls_ecp_keypair_free(eck);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200669#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200670 return ret;
671 }
672
Valerio Setti00e8dd12023-05-18 18:56:59 +0200673 /* When MBEDTLS_PK_USE_PSA_EC_DATA the key is checked while importing it
674 * into PSA. */
675#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200676 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
677 mbedtls_ecp_keypair_free(eck);
678 return ret;
679 }
Valerio Setti00e8dd12023-05-18 18:56:59 +0200680#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200681
682 return 0;
683}
684#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200685
686#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
687/*
688 * Create a temporary ecp_keypair for converting an EC point in compressed
689 * format to an uncompressed one
690 */
691static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
692 const unsigned char *in_start, size_t in_len,
693 size_t *out_buf_len, unsigned char *out_buf,
694 size_t out_buf_size)
695{
696 mbedtls_ecp_keypair ecp_key;
697 mbedtls_ecp_group_id ecp_group_id;
698 int ret;
699
700 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
701
702 mbedtls_ecp_keypair_init(&ecp_key);
703 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
704 if (ret != 0) {
705 return ret;
706 }
707 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
708 in_start, in_len);
709 if (ret != 0) {
710 goto exit;
711 }
712 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
713 MBEDTLS_ECP_PF_UNCOMPRESSED,
714 out_buf_len, out_buf, out_buf_size);
715
716exit:
717 mbedtls_ecp_keypair_free(&ecp_key);
718 return ret;
719}
720#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200721
Paul Bakker1a7550a2013-09-15 13:01:22 +0200722/*
723 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100724 *
725 * The caller is responsible for clearing the structure upon failure if
726 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200730 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733
Valerio Setti4064dbb2023-05-17 15:33:07 +0200734#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
735 mbedtls_svc_key_id_t key;
736 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
737 size_t len = (end - *p);
738
739 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
740 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 }
742
Valerio Setti4064dbb2023-05-17 15:33:07 +0200743 /* Compressed point format are not supported yet by PSA crypto. As a
744 * consequence ecp functions are used to "convert" the point to
745 * uncompressed format */
746 if ((**p == 0x02) || (**p == 0x03)) {
747 ret = pk_convert_compressed_ec(pk, *p, len,
748 &(pk->pub_raw_len), pk->pub_raw,
749 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
750 if (ret != 0) {
751 return ret;
752 }
753 } else {
754 /* Uncompressed format */
755 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200756 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200757 }
758 memcpy(pk->pub_raw, *p, (end - *p));
759 pk->pub_raw_len = end - *p;
760 }
761
762 /* Validate the key by trying to importing it */
763 psa_set_key_usage_flags(&key_attrs, 0);
764 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
765 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
766 psa_set_key_bits(&key_attrs, pk->ec_bits);
767
768 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
769 &key) != PSA_SUCCESS) ||
770 (psa_destroy_key(key) != PSA_SUCCESS)) {
771 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
772 pk->pub_raw_len = 0;
773 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
774 }
775 ret = 0;
776#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
777 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
778 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
779 (const unsigned char *) *p,
780 end - *p)) == 0) {
781 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
782 }
783#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
784
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787 */
788 *p = (unsigned char *) end;
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200792#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795/*
796 * RSAPublicKey ::= SEQUENCE {
797 * modulus INTEGER, -- n
798 * publicExponent INTEGER -- e
799 * }
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801static int pk_get_rsapubkey(unsigned char **p,
802 const unsigned char *end,
803 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804{
Janos Follath24eed8d2019-11-22 13:21:35 +0000805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806 size_t len;
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
809 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
811 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (*p + len != end) {
814 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
816 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
821 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
824 NULL, 0, NULL, 0)) != 0) {
825 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
826 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827
828 *p += len;
829
830 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
833 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
836 NULL, 0, *p, len)) != 0) {
837 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
838 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839
840 *p += len;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (mbedtls_rsa_complete(rsa) != 0 ||
843 mbedtls_rsa_check_pubkey(rsa) != 0) {
844 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000845 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (*p != end) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
849 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
850 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
856/* Get a PK algorithm identifier
857 *
858 * AlgorithmIdentifier ::= SEQUENCE {
859 * algorithm OBJECT IDENTIFIER,
860 * parameters ANY DEFINED BY algorithm OPTIONAL }
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static int pk_get_pk_alg(unsigned char **p,
863 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200864 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
865 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
873 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
874 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
Jethro Beekman01672442023-04-19 14:08:14 +0200876 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
877#if defined(MBEDTLS_ECP_LIGHT)
878 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
879 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
880 if (ret == 0) {
881 *pk_alg = MBEDTLS_PK_ECKEY;
882 }
883 }
884#else
885 (void) ec_grp_id;
886#endif
887 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
889 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
891 /*
892 * No parameters with RSA (only for EC)
893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (*pk_alg == MBEDTLS_PK_RSA &&
895 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
896 params->len != 0)) {
897 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901}
902
903/*
904 * SubjectPublicKeyInfo ::= SEQUENCE {
905 * algorithm AlgorithmIdentifier,
906 * subjectPublicKey BIT STRING }
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
909 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910{
Janos Follath24eed8d2019-11-22 13:21:35 +0000911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_asn1_buf alg_params;
914 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200915 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
919 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
920 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921 }
922
923 end = *p + len;
924
Jethro Beekman01672442023-04-19 14:08:14 +0200925 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 return ret;
927 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
931 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (*p + len != end) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
936 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
939 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
943 return ret;
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (pk_alg == MBEDTLS_PK_RSA) {
948 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200951#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200953#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200954 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200955 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200956 } else
957#endif
958 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200959 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200962 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200965#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret == 0 && *p != end) {
969 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
970 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
971 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (ret != 0) {
974 mbedtls_pk_free(pk);
975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978}
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
983 *
984 * The value zero is:
985 * - never a valid value for an RSA parameter
986 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
987 *
988 * Since values can't be omitted in PKCS#1, passing a zero value to
989 * rsa_complete() would be incorrect, so reject zero values early.
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991static int asn1_get_nonzero_mpi(unsigned char **p,
992 const unsigned char *end,
993 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100994{
995 int ret;
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 ret = mbedtls_asn1_get_mpi(p, end, X);
998 if (ret != 0) {
999 return ret;
1000 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
1003 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1004 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +01001007}
1008
1009/*
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010 * Parse a PKCS#1 encoded private RSA key
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
1013 const unsigned char *key,
1014 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015{
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001016 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 size_t len;
1018 unsigned char *p, *end;
1019
Hanno Beckerefa14e82017-10-11 19:45:19 +01001020 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001022
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 p = (unsigned char *) key;
1024 end = p + keylen;
1025
1026 /*
1027 * This function parses the RSAPrivateKey (PKCS#1)
1028 *
1029 * RSAPrivateKey ::= SEQUENCE {
1030 * version Version,
1031 * modulus INTEGER, -- n
1032 * publicExponent INTEGER, -- e
1033 * privateExponent INTEGER, -- d
1034 * prime1 INTEGER, -- p
1035 * prime2 INTEGER, -- q
1036 * exponent1 INTEGER, -- d mod (p-1)
1037 * exponent2 INTEGER, -- d mod (q-1)
1038 * coefficient INTEGER, -- (inverse of q) mod p
1039 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1040 * }
1041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1043 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1044 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045 }
1046
1047 end = p + len;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1050 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (version != 0) {
1054 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 }
1056
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1059 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1060 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1066 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1067 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001070
1071 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1073 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1074 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001075 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001077
1078 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1080 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1081 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001082 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001084
1085 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1088 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001089 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001092#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001093 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1095 * that they can be easily recomputed from D, P and Q. However by
1096 * parsing them from the PKCS1 structure it is possible to avoid
1097 * recalculating them which both reduces the overhead of loading
1098 * RSA private keys into memory and also avoids side channels which
1099 * can arise when computing those values, since all of D, P, and Q
1100 * are secret. See https://eprint.iacr.org/2020/055 for a
1101 * description of one such attack.
1102 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001103
Jack Lloyd80cc8112020-01-22 17:34:29 -05001104 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001109
1110 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1112 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1113 goto cleanup;
1114 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001115
1116 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1118 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1119 goto cleanup;
1120 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001121
Jack Lloyd60239752020-01-27 17:53:36 -05001122#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001123 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1125 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1126 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1127 goto cleanup;
1128 }
Jack Lloyd60239752020-01-27 17:53:36 -05001129#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001130
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001131 /* rsa_complete() doesn't complete anything with the default
1132 * implementation but is still called:
1133 * - for the benefit of alternative implementation that may want to
1134 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1135 * - as is also sanity-checks the key
1136 *
1137 * Furthermore, we also check the public part for consistency with
1138 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1141 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001142 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001143 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (p != end) {
1146 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1147 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 }
1149
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001150cleanup:
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001155 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if ((ret & 0xff80) == 0) {
1157 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1158 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001159 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
Valerio Setti0d2980f2023-04-05 18:17:13 +02001169#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170/*
1171 * Parse a SEC1 encoded private EC key
1172 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001173static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 const unsigned char *key, size_t keylen,
1175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176{
Janos Follath24eed8d2019-11-22 13:21:35 +00001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001178 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001179 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001180 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001182 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 unsigned char *end = p + keylen;
1184 unsigned char *end2;
Valerio Setti4064dbb2023-05-17 15:33:07 +02001185 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001186#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1188 psa_status_t status;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001189#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
1191 /*
1192 * RFC 5915, or SEC1 Appendix C.4
1193 *
1194 * ECPrivateKey ::= SEQUENCE {
1195 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1196 * privateKey OCTET STRING,
1197 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1198 * publicKey [1] BIT STRING OPTIONAL
1199 * }
1200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204 }
1205
1206 end = p + len;
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1210 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (version != 1) {
1213 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1214 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1218 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219
Jethro Beekman01672442023-04-19 14:08:14 +02001220 d = p;
1221 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001222
Valerio Settia541e012023-05-24 14:31:21 +02001223#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1225 mbedtls_ecp_keypair_free(eck);
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001228#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
1230 p += len;
1231
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001234 /*
1235 * Is 'parameters' present?
1236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1238 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1239 0)) == 0) {
1240 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001241 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_ecp_keypair_free(eck);
1243 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1246 mbedtls_ecp_keypair_free(eck);
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001248 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001249 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001252 /*
1253 * Is 'publickey' present? If not, or if we can't read it (eg because it
1254 * is compressed), create it from the private key.
1255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1257 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1258 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259 end2 = p + len;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1263 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (p + len != end2) {
1266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1268 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001269
Valerio Setti4064dbb2023-05-17 15:33:07 +02001270 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001271 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001273 /*
1274 * The only acceptable failure mode of pk_get_ecpubkey() above
1275 * is if the point format is not recognized.
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1278 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1279 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1282 mbedtls_ecp_keypair_free(eck);
1283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001284 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001286
Valerio Setti00e8dd12023-05-18 18:56:59 +02001287#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1288 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
1289 /* Setting largest masks for usage and key algorithms */
1290 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
1291 PSA_KEY_USAGE_SIGN_MESSAGE |
Valerio Setti51aa52e2023-05-24 12:37:50 +02001292 PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001293#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1294 psa_set_key_algorithm(&attributes,
1295 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH));
1296#else
1297 psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
1298#endif
Valerio Setti51aa52e2023-05-24 12:37:50 +02001299 psa_set_key_enrollment_algorithm(&attributes, PSA_ALG_ECDH);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001300
Valerio Settia541e012023-05-24 14:31:21 +02001301 status = psa_import_key(&attributes, d, d_len, &pk->priv_id);
Valerio Setti00e8dd12023-05-18 18:56:59 +02001302 if (status != PSA_SUCCESS) {
1303 ret = psa_pk_status_to_mbedtls(status);
1304 return ret;
1305 }
1306#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1307
Valerio Setti34f67552023-04-03 15:19:18 +02001308 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001309 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001310 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001311 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001312 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001313 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314
Valerio Setti00e8dd12023-05-18 18:56:59 +02001315#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1317 mbedtls_ecp_keypair_free(eck);
1318 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 }
Valerio Setti00e8dd12023-05-18 18:56:59 +02001320#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001324#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325
1326/*
1327 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001328 *
1329 * Notes:
1330 *
1331 * - This function does not own the key buffer. It is the
1332 * responsibility of the caller to take care of zeroizing
1333 * and freeing it after use.
1334 *
1335 * - The function is responsible for freeing the provided
1336 * PK context on failure.
1337 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 */
1339static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 mbedtls_pk_context *pk,
1341 const unsigned char *key, size_t keylen,
1342 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343{
1344 int ret, version;
1345 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 unsigned char *p = (unsigned char *) key;
1348 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001350 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
Jethro Beekman01672442023-04-19 14:08:14 +02001353#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001354 (void) f_rng;
1355 (void) p_rng;
1356#endif
1357
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001359 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 *
1361 * PrivateKeyInfo ::= SEQUENCE {
1362 * version Version,
1363 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1364 * privateKey PrivateKey,
1365 * attributes [0] IMPLICIT Attributes OPTIONAL }
1366 *
1367 * Version ::= INTEGER
1368 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1369 * PrivateKey ::= OCTET STRING
1370 *
1371 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1372 */
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1375 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 }
1378
1379 end = p + len;
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1382 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001383 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (version != 0) {
1386 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1387 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388
Jethro Beekman01672442023-04-19 14:08:14 +02001389 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return ret;
1391 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1394 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1395 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (len < 1) {
1398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1399 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1400 }
1401
1402 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1403 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1404 }
1405
1406 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1407 return ret;
1408 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (pk_alg == MBEDTLS_PK_RSA) {
1412 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1413 mbedtls_pk_free(pk);
1414 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415 }
1416 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001418#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001420#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001421 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001422 if ((ret =
1423 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001424 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001425 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001426 p_rng)) != 0) {
1427 mbedtls_pk_free(pk);
1428 return ret;
1429 }
1430 } else
1431#endif
1432 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001433 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1434 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001435 mbedtls_pk_free(pk);
1436 return ret;
1437 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 }
1439 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001440#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444}
1445
1446/*
1447 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001448 *
1449 * To save space, the decryption happens in-place on the given key buffer.
1450 * Also, while this function may modify the keybuffer, it doesn't own it,
1451 * and instead it is the responsibility of the caller to zeroize and properly
1452 * free it after use.
1453 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_pk_context *pk,
1458 unsigned char *key, size_t keylen,
1459 const unsigned char *pwd, size_t pwdlen,
1460 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001462 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001464 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1467#if defined(MBEDTLS_PKCS12_C)
1468 mbedtls_cipher_type_t cipher_alg;
1469 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470#endif
1471
Hanno Becker2aa80a72017-09-07 15:28:45 +01001472 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 end = p + keylen;
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (pwdlen == 0) {
1476 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1477 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
1479 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001480 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 *
1482 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1483 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1484 * encryptedData EncryptedData
1485 * }
1486 *
1487 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1488 *
1489 * EncryptedData ::= OCTET STRING
1490 *
1491 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001492 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1495 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1496 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
1498
1499 end = p + len;
1500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1502 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1503 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1506 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1507 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508
Hanno Beckerfab35692017-08-25 13:38:26 +01001509 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510
1511 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001512 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1516 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1517 cipher_alg, md_alg,
1518 pwd, pwdlen, p, len, buf)) != 0) {
1519 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1520 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1521 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001525
1526 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_PKCS12_C */
1529#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1531 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1532 p, len, buf)) != 0) {
1533 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1534 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1535 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001539
1540 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001543 {
1544 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001545 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (decrypted == 0) {
1548 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1549 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554
1555/*
1556 * Parse a private key
1557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1559 const unsigned char *key, size_t keylen,
1560 const unsigned char *pwd, size_t pwdlen,
1561 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562{
Janos Follath24eed8d2019-11-22 13:21:35 +00001563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001568#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (keylen == 0) {
1571 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1572 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001573
1574#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001578 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001580 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 } else {
1582 ret = mbedtls_pem_read_buffer(&pem,
1583 "-----BEGIN RSA PRIVATE KEY-----",
1584 "-----END RSA PRIVATE KEY-----",
1585 key, pwd, pwdlen, &len);
1586 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ret == 0) {
1589 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1590 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1591 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1592 pem.buf, pem.buflen)) != 0) {
1593 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001594 }
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 mbedtls_pem_free(&pem);
1597 return ret;
1598 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1599 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1600 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1601 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1602 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1603 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606
Valerio Setti0d2980f2023-04-05 18:17:13 +02001607#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001608 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001610 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 } else {
1612 ret = mbedtls_pem_read_buffer(&pem,
1613 "-----BEGIN EC PRIVATE KEY-----",
1614 "-----END EC PRIVATE KEY-----",
1615 key, pwd, pwdlen, &len);
1616 }
1617 if (ret == 0) {
1618 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001621 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 pem.buf, pem.buflen,
1623 f_rng, p_rng)) != 0) {
1624 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001625 }
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 mbedtls_pem_free(&pem);
1628 return ret;
1629 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1630 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1631 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1632 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1633 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1634 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001635 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001636#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001637
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001638 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001640 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else {
1642 ret = mbedtls_pem_read_buffer(&pem,
1643 "-----BEGIN PRIVATE KEY-----",
1644 "-----END PRIVATE KEY-----",
1645 key, NULL, 0, &len);
1646 }
1647 if (ret == 0) {
1648 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1649 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1650 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651 }
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 mbedtls_pem_free(&pem);
1654 return ret;
1655 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1656 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001660 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001662 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 } else {
1664 ret = mbedtls_pem_read_buffer(&pem,
1665 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1666 "-----END ENCRYPTED PRIVATE KEY-----",
1667 key, NULL, 0, &len);
1668 }
1669 if (ret == 0) {
1670 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1671 pwd, pwdlen, f_rng, p_rng)) != 0) {
1672 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001673 }
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 mbedtls_pem_free(&pem);
1676 return ret;
1677 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1678 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681#else
1682 ((void) pwd);
1683 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685
1686 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001687 * At this point we only know it's not a PEM formatted key. Could be any
1688 * of the known DER encoded private key formats
1689 *
1690 * We try the different DER format parsers to see if one passes without
1691 * error
1692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001695 unsigned char *key_copy;
1696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1698 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1699 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1704 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_platform_zeroize(key_copy, keylen);
1707 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708 }
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (ret == 0) {
1711 return 0;
1712 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_pk_free(pk);
1715 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1718 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1723 if (ret == 0) {
1724 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001725 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_pk_free(pk);
1728 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1733 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1734 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1735 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001736 }
1737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 mbedtls_pk_free(pk);
1739 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001741
Valerio Setti0d2980f2023-04-05 18:17:13 +02001742#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1744 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001745 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 key, keylen, f_rng, p_rng) == 0) {
1747 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001750#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001751
Valerio Setti4064dbb2023-05-17 15:33:07 +02001752 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_LIGHT isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001753 * it is ok to leave the PK context initialized but not
1754 * freed: It is the caller's responsibility to call pk_init()
1755 * before calling this function, and to call pk_free()
Valerio Setti4064dbb2023-05-17 15:33:07 +02001756 * when it fails. If MBEDTLS_ECP_LIGHT is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001757 * isn't, this leads to mbedtls_pk_free() being called
1758 * twice, once here and once by the caller, but this is
1759 * also ok and in line with the mbedtls_pk_free() calls
1760 * on failed PEM parsing attempts. */
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001763}
1764
1765/*
1766 * Parse a public key
1767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1769 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001770{
Janos Follath24eed8d2019-11-22 13:21:35 +00001771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001772 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001773#if defined(MBEDTLS_RSA_C)
1774 const mbedtls_pk_info_t *pk_info;
1775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001777 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001779#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (keylen == 0) {
1782 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1783 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001784
1785#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001787#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001788 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001790 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 } else {
1792 ret = mbedtls_pem_read_buffer(&pem,
1793 "-----BEGIN RSA PUBLIC KEY-----",
1794 "-----END RSA PUBLIC KEY-----",
1795 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001796 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001797
1798 if (ret == 0) {
1799 p = pem.buf;
1800 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1801 mbedtls_pem_free(&pem);
1802 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1803 }
1804
1805 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1806 mbedtls_pem_free(&pem);
1807 return ret;
1808 }
1809
1810 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1811 mbedtls_pk_free(ctx);
1812 }
1813
1814 mbedtls_pem_free(&pem);
1815 return ret;
1816 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1817 mbedtls_pem_free(&pem);
1818 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001819 }
1820#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001821
1822 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001824 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 } else {
1826 ret = mbedtls_pem_read_buffer(&pem,
1827 "-----BEGIN PUBLIC KEY-----",
1828 "-----END PUBLIC KEY-----",
1829 key, NULL, 0, &len);
1830 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001833 /*
1834 * Was PEM encoded
1835 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001836 p = pem.buf;
1837
Jethro Beekman01672442023-04-19 14:08:14 +02001838 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 mbedtls_pem_free(&pem);
1840 return ret;
1841 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1842 mbedtls_pem_free(&pem);
1843 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001844 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001847
1848#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1850 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001851 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001852
1853 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1854 return ret;
1855 }
1856
1857 p = (unsigned char *) key;
1858 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1859 if (ret == 0) {
1860 return ret;
1861 }
1862 mbedtls_pk_free(ctx);
1863 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1864 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1865 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001866 }
1867#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001868 p = (unsigned char *) key;
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001873}
1874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_PK_PARSE_C */