blob: 9bc88015afe8c4ee5dc2fd1777f2ec937426f4ff [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
Gilles Peskine832f3492017-11-30 11:42:12 +010066#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020067/*
68 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020069 *
70 * The file is expected to contain either PEM or DER encoded data.
71 * A terminating null byte is always appended. It is included in the announced
72 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020073 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020075{
76 FILE *f;
77 long size;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if ((f = fopen(path, "rb")) == NULL) {
80 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
81 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020082
Gilles Peskineda0913b2022-06-30 17:03:40 +020083 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 fseek(f, 0, SEEK_END);
87 if ((size = ftell(f)) == -1) {
88 fclose(f);
89 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 }
Gilles Peskine449bd832023-01-11 14:50:10 +010091 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020092
93 *n = (size_t) size;
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (*n + 1 == 0 ||
96 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
97 fclose(f);
98 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020099 }
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (fread(*buf, 1, *n, f) != *n) {
102 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_platform_zeroize(*buf, *n);
105 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108 }
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111
112 (*buf)[*n] = '\0';
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200115 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119}
120
121/*
122 * Load and parse a private key
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
125 const char *path, const char *pwd,
126 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127{
Janos Follath24eed8d2019-11-22 13:21:35 +0000128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129 size_t n;
130 unsigned char *buf;
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
133 return ret;
134 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (pwd == NULL) {
137 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
138 } else {
139 ret = mbedtls_pk_parse_key(ctx, buf, n,
140 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
141 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_platform_zeroize(buf, n);
144 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147}
148
149/*
150 * Load and parse a public key
151 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153{
Janos Follath24eed8d2019-11-22 13:21:35 +0000154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155 size_t n;
156 unsigned char *buf;
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
159 return ret;
160 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_platform_zeroize(buf, n);
165 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170
Valerio Setti0d2980f2023-04-05 18:17:13 +0200171#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 *
174 * ECParameters ::= CHOICE {
175 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100176 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 * }
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
181 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182{
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (end - *p < 1) {
186 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
187 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
188 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100189
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100195#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 ) {
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
198 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204
205 params->p = *p;
206 *p += params->len;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (*p != end) {
209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
210 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
211 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214}
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100218 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
219 * WARNING: the resulting group should only be used with
220 * pk_group_id_from_specified(), since its base point may not be set correctly
221 * if it was encoded compressed.
222 *
223 * SpecifiedECDomain ::= SEQUENCE {
224 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
225 * fieldID FieldID {{FieldTypes}},
226 * curve Curve,
227 * base ECPoint,
228 * order INTEGER,
229 * cofactor INTEGER OPTIONAL,
230 * hash HashAlgorithm OPTIONAL,
231 * ...
232 * }
233 *
234 * We only support prime-field as field type, and ignore hash and cofactor.
235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237{
Janos Follath24eed8d2019-11-22 13:21:35 +0000238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100239 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200240 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100241 const unsigned char *end_field, *end_curve;
242 size_t len;
243 int ver;
244
245 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
248 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ver < 1 || ver > 3) {
251 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
252 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100253
254 /*
255 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
256 * fieldType FIELD-ID.&id({IOSet}),
257 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
258 * }
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
261 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
262 return ret;
263 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100264
265 end_field = p + len;
266
267 /*
268 * FIELD-ID ::= TYPE-IDENTIFIER
269 * FieldTypes FIELD-ID ::= {
270 * { Prime-p IDENTIFIED BY prime-field } |
271 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
272 * }
273 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
276 return ret;
277 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
280 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
281 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100282 }
283
284 p += len;
285
286 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
289 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (p != end_field) {
294 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
295 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
296 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297
298 /*
299 * Curve ::= SEQUENCE {
300 * a FieldElement,
301 * b FieldElement,
302 * seed BIT STRING OPTIONAL
303 * -- Shall be present if used in SpecifiedECDomain
304 * -- with version equal to ecdpVer2 or ecdpVer3
305 * }
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
309 return ret;
310 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311
312 end_curve = p + len;
313
314 /*
315 * FieldElement ::= OCTET STRING
316 * containing an integer in the case of a prime field
317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
319 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
320 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 }
322
323 p += len;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
326 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328 }
329
330 p += len;
331
332 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (p != end_curve) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
340 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100341
342 /*
343 * ECPoint ::= OCTET STRING
344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
347 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
350 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100351 /*
352 * If we can't read the point because it's compressed, cheat by
353 * reading only the X coordinate and the parity bit of Y.
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
356 (p[0] != 0x02 && p[0] != 0x03) ||
357 len != mbedtls_mpi_size(&grp->P) + 1 ||
358 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
359 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
360 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
361 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100362 }
363 }
364
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365 p += len;
366
367 /*
368 * order INTEGER
369 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
371 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
372 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375
376 /*
377 * Allow optional elements by purposefully not enforcing p == end here.
378 */
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381}
382
383/*
384 * Find the group id associated with an (almost filled) group as generated by
385 * pk_group_from_specified(), or return an error if unknown.
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387static 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 +0100388{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100389 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ecp_group ref;
391 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_ecp_group_free(&ref);
398 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399
400 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
402 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
403 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
404 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
405 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
406 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
407 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 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 +0100410 break;
411 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412 }
413
414cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416
417 *grp_id = *id;
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424}
425
426/*
427 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
430 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431{
Janos Follath24eed8d2019-11-22 13:21:35 +0000432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
443cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000444 /* The API respecting lifecycle for mbedtls_ecp_group struct is
445 * _init(), _load() and _free(). In pk_group_id_from_specified() the
446 * temporary grp breaks that flow and it's members are populated
447 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
448 * which is assuming a group populated by _setup() may not clean-up
449 * properly -> Manually free it's members.
450 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000451 mbedtls_mpi_free(&grp.N);
452 mbedtls_mpi_free(&grp.P);
453 mbedtls_mpi_free(&grp.A);
454 mbedtls_mpi_free(&grp.B);
455 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460
Valerio Setti4064dbb2023-05-17 15:33:07 +0200461#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
462/* Functions pk_use_ecparams() and pk_use_ecparams_rfc8410() update the
463 * ecp_keypair structure with proper group ID. The purpose of this helper
464 * function is to update ec_family and ec_bits accordingly. */
465static int pk_update_psa_ecparams(mbedtls_pk_context *pk,
466 mbedtls_ecp_group_id grp_id)
467{
468 psa_ecc_family_t ec_family;
469 size_t bits;
470
471 ec_family = mbedtls_ecc_group_to_psa(grp_id, &bits);
472
473 if ((pk->ec_family != 0) && (pk->ec_family != ec_family)) {
474 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
475 }
476
477 pk->ec_family = ec_family;
478 pk->ec_bits = bits;
479
480 return 0;
481}
482#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
483
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100484/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100486 *
487 * ECParameters ::= CHOICE {
488 * namedCurve OBJECT IDENTIFIER
489 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
490 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200492static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493{
Janos Follath24eed8d2019-11-22 13:21:35 +0000494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (params->tag == MBEDTLS_ASN1_OID) {
498 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
499 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
500 }
501 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
504 return ret;
505 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100506#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100508#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100509 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510
Valerio Setti4064dbb2023-05-17 15:33:07 +0200511 /* grp may already be initialized; if so, make sure IDs match */
512 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
513 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
515 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Valerio Setti4064dbb2023-05-17 15:33:07 +0200517 if ((ret = mbedtls_ecp_group_load(&(mbedtls_pk_ec_rw(*pk)->grp),
518 grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return ret;
520 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200521#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
522 ret = pk_update_psa_ecparams(pk, grp_id);
523#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200524
Valerio Setti4064dbb2023-05-17 15:33:07 +0200525 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526}
527
Jethro Beekman01672442023-04-19 14:08:14 +0200528/*
529 * Helper function for deriving a public key from its private counterpart.
530 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200531static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200532 const unsigned char *d, size_t d_len,
533 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
534{
535 int ret;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200536 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200537#if defined(MBEDTLS_USE_PSA_CRYPTO)
538 psa_status_t status, destruction_status;
539 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
540 size_t curve_bits;
541 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200542#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200543 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
544 size_t key_len;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200545#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200546 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
547
548 (void) f_rng;
549 (void) p_rng;
550
551 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
552 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
553
554 status = psa_import_key(&key_attr, d, d_len, &key_id);
555 ret = psa_pk_status_to_mbedtls(status);
556 if (ret != 0) {
557 return ret;
558 }
559
Valerio Setti4064dbb2023-05-17 15:33:07 +0200560#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
561 status = psa_export_public_key(key_id, pk->pub_raw, sizeof(pk->pub_raw),
562 &pk->pub_raw_len);
563#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200564 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200565#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200566 ret = psa_pk_status_to_mbedtls(status);
567 destruction_status = psa_destroy_key(key_id);
568 if (ret != 0) {
569 return ret;
570 } else if (destruction_status != PSA_SUCCESS) {
571 return psa_pk_status_to_mbedtls(destruction_status);
572 }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200573#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Jethro Beekman01672442023-04-19 14:08:14 +0200574 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200575#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200576#else /* MBEDTLS_USE_PSA_CRYPTO */
577 (void) d;
578 (void) d_len;
579
580 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
581#endif /* MBEDTLS_USE_PSA_CRYPTO */
582 return ret;
583}
584
585#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
586
587/*
588 * Load an RFC8410 EC key, which doesn't have any parameters
589 */
590static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
591 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200592 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200593{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200594 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
595 int ret;
596
Jethro Beekman01672442023-04-19 14:08:14 +0200597 if (params->tag != 0 || params->len != 0) {
598 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
599 }
600
Valerio Setti4064dbb2023-05-17 15:33:07 +0200601 ret = mbedtls_ecp_group_load(&(ecp->grp), grp_id);
602 if (ret != 0) {
603 return ret;
604 }
605
606#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
607 ret = pk_update_psa_ecparams(pk, grp_id);
608#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
609 return ret;
Jethro Beekman01672442023-04-19 14:08:14 +0200610}
611
612/*
613 * Parse an RFC 8410 encoded private EC key
614 *
615 * CurvePrivateKey ::= OCTET STRING
616 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200617static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200618 unsigned char *key, size_t keylen, const unsigned char *end,
619 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
620{
Valerio Setti4064dbb2023-05-17 15:33:07 +0200621 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
623 size_t len;
624
625 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
627 }
628
629 if (key + len != end) {
630 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
631 }
632
633 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
634 mbedtls_ecp_keypair_free(eck);
635 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
636 }
637
Valerio Setti4064dbb2023-05-17 15:33:07 +0200638 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
639 * which never contain a public key. As such, derive the public key
640 * unconditionally. */
641 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200642 mbedtls_ecp_keypair_free(eck);
643 return ret;
644 }
645
646 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
647 mbedtls_ecp_keypair_free(eck);
648 return ret;
649 }
650
651 return 0;
652}
653#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200654
655#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
656/*
657 * Create a temporary ecp_keypair for converting an EC point in compressed
658 * format to an uncompressed one
659 */
660static int pk_convert_compressed_ec(mbedtls_pk_context *pk,
661 const unsigned char *in_start, size_t in_len,
662 size_t *out_buf_len, unsigned char *out_buf,
663 size_t out_buf_size)
664{
665 mbedtls_ecp_keypair ecp_key;
666 mbedtls_ecp_group_id ecp_group_id;
667 int ret;
668
669 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
670
671 mbedtls_ecp_keypair_init(&ecp_key);
672 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
673 if (ret != 0) {
674 return ret;
675 }
676 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
677 in_start, in_len);
678 if (ret != 0) {
679 goto exit;
680 }
681 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
682 MBEDTLS_ECP_PF_UNCOMPRESSED,
683 out_buf_len, out_buf, out_buf_size);
684
685exit:
686 mbedtls_ecp_keypair_free(&ecp_key);
687 return ret;
688}
689#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200690
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691/*
692 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100693 *
694 * The caller is responsible for clearing the structure upon failure if
695 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200699 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700{
Janos Follath24eed8d2019-11-22 13:21:35 +0000701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702
Valerio Setti4064dbb2023-05-17 15:33:07 +0200703#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
704 mbedtls_svc_key_id_t key;
705 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
706 size_t len = (end - *p);
707
708 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
709 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710 }
711
Valerio Setti4064dbb2023-05-17 15:33:07 +0200712 /* Compressed point format are not supported yet by PSA crypto. As a
713 * consequence ecp functions are used to "convert" the point to
714 * uncompressed format */
715 if ((**p == 0x02) || (**p == 0x03)) {
716 ret = pk_convert_compressed_ec(pk, *p, len,
717 &(pk->pub_raw_len), pk->pub_raw,
718 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
719 if (ret != 0) {
720 return ret;
721 }
722 } else {
723 /* Uncompressed format */
724 if ((end - *p) > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200725 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726 }
727 memcpy(pk->pub_raw, *p, (end - *p));
728 pk->pub_raw_len = end - *p;
729 }
730
731 /* Validate the key by trying to importing it */
732 psa_set_key_usage_flags(&key_attrs, 0);
733 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
734 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
735 psa_set_key_bits(&key_attrs, pk->ec_bits);
736
737 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
738 &key) != PSA_SUCCESS) ||
739 (psa_destroy_key(key) != PSA_SUCCESS)) {
740 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
741 pk->pub_raw_len = 0;
742 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
743 }
744 ret = 0;
745#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
746 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
747 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
748 (const unsigned char *) *p,
749 end - *p)) == 0) {
750 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
751 }
752#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
753
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756 */
757 *p = (unsigned char *) end;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200760}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200761#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764/*
765 * RSAPublicKey ::= SEQUENCE {
766 * modulus INTEGER, -- n
767 * publicExponent INTEGER -- e
768 * }
769 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770static int pk_get_rsapubkey(unsigned char **p,
771 const unsigned char *end,
772 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773{
Janos Follath24eed8d2019-11-22 13:21:35 +0000774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775 size_t len;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
778 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
780 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (*p + len != end) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
784 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
785 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100787 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
790 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
793 NULL, 0, NULL, 0)) != 0) {
794 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
795 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100796
797 *p += len;
798
799 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
801 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
802 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
805 NULL, 0, *p, len)) != 0) {
806 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
807 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100808
809 *p += len;
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (mbedtls_rsa_complete(rsa) != 0 ||
812 mbedtls_rsa_check_pubkey(rsa) != 0) {
813 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000814 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (*p != end) {
817 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
818 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
819 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824
825/* Get a PK algorithm identifier
826 *
827 * AlgorithmIdentifier ::= SEQUENCE {
828 * algorithm OBJECT IDENTIFIER,
829 * parameters ANY DEFINED BY algorithm OPTIONAL }
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static int pk_get_pk_alg(unsigned char **p,
832 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200833 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
834 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835{
Janos Follath24eed8d2019-11-22 13:21:35 +0000836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
843 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Jethro Beekman01672442023-04-19 14:08:14 +0200845 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
846#if defined(MBEDTLS_ECP_LIGHT)
847 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
848 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
849 if (ret == 0) {
850 *pk_alg = MBEDTLS_PK_ECKEY;
851 }
852 }
853#else
854 (void) ec_grp_id;
855#endif
856 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
858 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859
860 /*
861 * No parameters with RSA (only for EC)
862 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (*pk_alg == MBEDTLS_PK_RSA &&
864 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
865 params->len != 0)) {
866 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 }
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870}
871
872/*
873 * SubjectPublicKeyInfo ::= SEQUENCE {
874 * algorithm AlgorithmIdentifier,
875 * subjectPublicKey BIT STRING }
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
878 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879{
Janos Follath24eed8d2019-11-22 13:21:35 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_asn1_buf alg_params;
883 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200884 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
888 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
889 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890 }
891
892 end = *p + len;
893
Jethro Beekman01672442023-04-19 14:08:14 +0200894 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return ret;
896 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
900 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (*p + len != end) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
904 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
908 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
909 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
912 return ret;
913 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (pk_alg == MBEDTLS_PK_RSA) {
917 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200920#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200922#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
923 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200924 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200925 } else
926#endif
927 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200928 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200929 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200931 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200934#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ret == 0 && *p != end) {
938 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
939 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (ret != 0) {
943 mbedtls_pk_free(pk);
944 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947}
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100951 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
952 *
953 * The value zero is:
954 * - never a valid value for an RSA parameter
955 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
956 *
957 * Since values can't be omitted in PKCS#1, passing a zero value to
958 * rsa_complete() would be incorrect, so reject zero values early.
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960static int asn1_get_nonzero_mpi(unsigned char **p,
961 const unsigned char *end,
962 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100963{
964 int ret;
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = mbedtls_asn1_get_mpi(p, end, X);
967 if (ret != 0) {
968 return ret;
969 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
972 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
973 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100976}
977
978/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 * Parse a PKCS#1 encoded private RSA key
980 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
982 const unsigned char *key,
983 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100985 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986 size_t len;
987 unsigned char *p, *end;
988
Hanno Beckerefa14e82017-10-11 19:45:19 +0100989 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100991
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 p = (unsigned char *) key;
993 end = p + keylen;
994
995 /*
996 * This function parses the RSAPrivateKey (PKCS#1)
997 *
998 * RSAPrivateKey ::= SEQUENCE {
999 * version Version,
1000 * modulus INTEGER, -- n
1001 * publicExponent INTEGER, -- e
1002 * privateExponent INTEGER, -- d
1003 * prime1 INTEGER, -- p
1004 * prime2 INTEGER, -- q
1005 * exponent1 INTEGER, -- d mod (p-1)
1006 * exponent2 INTEGER, -- d mod (q-1)
1007 * coefficient INTEGER, -- (inverse of q) mod p
1008 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1009 * }
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1012 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1013 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 }
1015
1016 end = p + len;
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1019 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (version != 0) {
1023 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 }
1025
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001026 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1028 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1029 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001030 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001033 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1035 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1036 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001039
1040 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1042 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1043 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001044 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001046
1047 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1049 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1050 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001051 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001053
1054 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1056 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1057 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001058 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001061#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001062 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1064 * that they can be easily recomputed from D, P and Q. However by
1065 * parsing them from the PKCS1 structure it is possible to avoid
1066 * recalculating them which both reduces the overhead of loading
1067 * RSA private keys into memory and also avoids side channels which
1068 * can arise when computing those values, since all of D, P, and Q
1069 * are secret. See https://eprint.iacr.org/2020/055 for a
1070 * description of one such attack.
1071 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001072
Jack Lloyd80cc8112020-01-22 17:34:29 -05001073 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1075 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1076 goto cleanup;
1077 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001078
1079 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1081 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1082 goto cleanup;
1083 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084
1085 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1087 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1088 goto cleanup;
1089 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001090
Jack Lloyd60239752020-01-27 17:53:36 -05001091#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001092 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1094 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1095 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1096 goto cleanup;
1097 }
Jack Lloyd60239752020-01-27 17:53:36 -05001098#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001099
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001100 /* rsa_complete() doesn't complete anything with the default
1101 * implementation but is still called:
1102 * - for the benefit of alternative implementation that may want to
1103 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1104 * - as is also sanity-checks the key
1105 *
1106 * Furthermore, we also check the public part for consistency with
1107 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1110 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001111 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001112 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (p != end) {
1115 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1116 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 }
1118
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001119cleanup:
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001124 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if ((ret & 0xff80) == 0) {
1126 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1127 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001128 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137
Valerio Setti0d2980f2023-04-05 18:17:13 +02001138#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139/*
1140 * Parse a SEC1 encoded private EC key
1141 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001142static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 const unsigned char *key, size_t keylen,
1144 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145{
Janos Follath24eed8d2019-11-22 13:21:35 +00001146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001147 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001148 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001149 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001151 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 unsigned char *end = p + keylen;
1153 unsigned char *end2;
Valerio Setti4064dbb2023-05-17 15:33:07 +02001154 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155
1156 /*
1157 * RFC 5915, or SEC1 Appendix C.4
1158 *
1159 * ECPrivateKey ::= SEQUENCE {
1160 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1161 * privateKey OCTET STRING,
1162 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1163 * publicKey [1] BIT STRING OPTIONAL
1164 * }
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1168 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 }
1170
1171 end = p + len;
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1175 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (version != 1) {
1178 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1179 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1183 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184
Jethro Beekman01672442023-04-19 14:08:14 +02001185 d = p;
1186 d_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1188 mbedtls_ecp_keypair_free(eck);
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 }
1191
1192 p += len;
1193
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001194 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001196 /*
1197 * Is 'parameters' present?
1198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1200 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1201 0)) == 0) {
1202 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001203 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 mbedtls_ecp_keypair_free(eck);
1205 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1208 mbedtls_ecp_keypair_free(eck);
1209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001210 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001211 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001214 /*
1215 * Is 'publickey' present? If not, or if we can't read it (eg because it
1216 * is compressed), create it from the private key.
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1219 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1220 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001221 end2 = p + len;
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1225 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (p + len != end2) {
1228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1229 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1230 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001231
Valerio Setti4064dbb2023-05-17 15:33:07 +02001232 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001233 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235 /*
1236 * The only acceptable failure mode of pk_get_ecpubkey() above
1237 * is if the point format is not recognized.
1238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1240 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1241 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001242 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1244 mbedtls_ecp_keypair_free(eck);
1245 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001248
Valerio Setti34f67552023-04-03 15:19:18 +02001249 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001250 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001251 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001252 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001253 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001254 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1257 mbedtls_ecp_keypair_free(eck);
1258 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 }
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001263#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264
1265/*
1266 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001267 *
1268 * Notes:
1269 *
1270 * - This function does not own the key buffer. It is the
1271 * responsibility of the caller to take care of zeroizing
1272 * and freeing it after use.
1273 *
1274 * - The function is responsible for freeing the provided
1275 * PK context on failure.
1276 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 */
1278static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_pk_context *pk,
1280 const unsigned char *key, size_t keylen,
1281 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282{
1283 int ret, version;
1284 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 unsigned char *p = (unsigned char *) key;
1287 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001289 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
Jethro Beekman01672442023-04-19 14:08:14 +02001292#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001293 (void) f_rng;
1294 (void) p_rng;
1295#endif
1296
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001298 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 *
1300 * PrivateKeyInfo ::= SEQUENCE {
1301 * version Version,
1302 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1303 * privateKey PrivateKey,
1304 * attributes [0] IMPLICIT Attributes OPTIONAL }
1305 *
1306 * Version ::= INTEGER
1307 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1308 * PrivateKey ::= OCTET STRING
1309 *
1310 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1311 */
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 }
1317
1318 end = p + len;
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1321 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001322 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (version != 0) {
1325 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1326 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327
Jethro Beekman01672442023-04-19 14:08:14 +02001328 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 return ret;
1330 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1334 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (len < 1) {
1337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1338 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1339 }
1340
1341 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1342 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1343 }
1344
1345 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1346 return ret;
1347 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (pk_alg == MBEDTLS_PK_RSA) {
1351 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1352 mbedtls_pk_free(pk);
1353 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 }
1355 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001357#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001359#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
1360 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001361 if ((ret =
1362 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001363 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001364 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001365 p_rng)) != 0) {
1366 mbedtls_pk_free(pk);
1367 return ret;
1368 }
1369 } else
1370#endif
1371 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001372 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1373 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001374 mbedtls_pk_free(pk);
1375 return ret;
1376 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 }
1378 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001379#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383}
1384
1385/*
1386 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001387 *
1388 * To save space, the decryption happens in-place on the given key buffer.
1389 * Also, while this function may modify the keybuffer, it doesn't own it,
1390 * and instead it is the responsibility of the caller to zeroize and properly
1391 * free it after use.
1392 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001395static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 mbedtls_pk_context *pk,
1397 unsigned char *key, size_t keylen,
1398 const unsigned char *pwd, size_t pwdlen,
1399 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001401 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001403 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1406#if defined(MBEDTLS_PKCS12_C)
1407 mbedtls_cipher_type_t cipher_alg;
1408 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409#endif
1410
Hanno Becker2aa80a72017-09-07 15:28:45 +01001411 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 end = p + keylen;
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (pwdlen == 0) {
1415 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1416 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417
1418 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001419 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420 *
1421 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1422 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1423 * encryptedData EncryptedData
1424 * }
1425 *
1426 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1427 *
1428 * EncryptedData ::= OCTET STRING
1429 *
1430 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001431 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1434 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 }
1437
1438 end = p + len;
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1442 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1446 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Hanno Beckerfab35692017-08-25 13:38:26 +01001448 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449
1450 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001451 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1455 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1456 cipher_alg, md_alg,
1457 pwd, pwdlen, p, len, buf)) != 0) {
1458 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1459 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1460 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001464
1465 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#endif /* MBEDTLS_PKCS12_C */
1468#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1470 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1471 p, len, buf)) != 0) {
1472 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1473 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1474 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001478
1479 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001482 {
1483 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001484 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (decrypted == 0) {
1487 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1488 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
1494/*
1495 * Parse a private key
1496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1498 const unsigned char *key, size_t keylen,
1499 const unsigned char *pwd, size_t pwdlen,
1500 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501{
Janos Follath24eed8d2019-11-22 13:21:35 +00001502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001507#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (keylen == 0) {
1510 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1511 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001512
1513#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001517 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001519 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 } else {
1521 ret = mbedtls_pem_read_buffer(&pem,
1522 "-----BEGIN RSA PRIVATE KEY-----",
1523 "-----END RSA PRIVATE KEY-----",
1524 key, pwd, pwdlen, &len);
1525 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ret == 0) {
1528 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1529 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1530 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1531 pem.buf, pem.buflen)) != 0) {
1532 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 }
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_pem_free(&pem);
1536 return ret;
1537 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1538 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1539 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1540 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1541 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1542 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545
Valerio Setti0d2980f2023-04-05 18:17:13 +02001546#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001547 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001549 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 } else {
1551 ret = mbedtls_pem_read_buffer(&pem,
1552 "-----BEGIN EC PRIVATE KEY-----",
1553 "-----END EC PRIVATE KEY-----",
1554 key, pwd, pwdlen, &len);
1555 }
1556 if (ret == 0) {
1557 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001560 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 pem.buf, pem.buflen,
1562 f_rng, p_rng)) != 0) {
1563 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001564 }
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 mbedtls_pem_free(&pem);
1567 return ret;
1568 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1569 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1570 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1571 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1572 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1573 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001574 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001575#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001577 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001579 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 } else {
1581 ret = mbedtls_pem_read_buffer(&pem,
1582 "-----BEGIN PRIVATE KEY-----",
1583 "-----END PRIVATE KEY-----",
1584 key, NULL, 0, &len);
1585 }
1586 if (ret == 0) {
1587 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1588 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1589 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590 }
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_pem_free(&pem);
1593 return ret;
1594 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1595 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001599 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001601 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 } else {
1603 ret = mbedtls_pem_read_buffer(&pem,
1604 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1605 "-----END ENCRYPTED PRIVATE KEY-----",
1606 key, NULL, 0, &len);
1607 }
1608 if (ret == 0) {
1609 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1610 pwd, pwdlen, f_rng, p_rng)) != 0) {
1611 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001612 }
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_pem_free(&pem);
1615 return ret;
1616 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1617 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001620#else
1621 ((void) pwd);
1622 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001624
1625 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001626 * At this point we only know it's not a PEM formatted key. Could be any
1627 * of the known DER encoded private key formats
1628 *
1629 * We try the different DER format parsers to see if one passes without
1630 * error
1631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001634 unsigned char *key_copy;
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1637 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1638 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1643 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 mbedtls_platform_zeroize(key_copy, keylen);
1646 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001647 }
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (ret == 0) {
1650 return 0;
1651 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 mbedtls_pk_free(pk);
1654 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1657 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1662 if (ret == 0) {
1663 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001664 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 mbedtls_pk_free(pk);
1667 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1672 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1673 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1674 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001675 }
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_pk_free(pk);
1678 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001680
Valerio Setti0d2980f2023-04-05 18:17:13 +02001681#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1683 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001684 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 key, keylen, f_rng, p_rng) == 0) {
1686 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001689#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001690
Valerio Setti4064dbb2023-05-17 15:33:07 +02001691 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_LIGHT isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001692 * it is ok to leave the PK context initialized but not
1693 * freed: It is the caller's responsibility to call pk_init()
1694 * before calling this function, and to call pk_free()
Valerio Setti4064dbb2023-05-17 15:33:07 +02001695 * when it fails. If MBEDTLS_ECP_LIGHT is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001696 * isn't, this leads to mbedtls_pk_free() being called
1697 * twice, once here and once by the caller, but this is
1698 * also ok and in line with the mbedtls_pk_free() calls
1699 * on failed PEM parsing attempts. */
1700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702}
1703
1704/*
1705 * Parse a public key
1706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1708 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001709{
Janos Follath24eed8d2019-11-22 13:21:35 +00001710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001712#if defined(MBEDTLS_RSA_C)
1713 const mbedtls_pk_info_t *pk_info;
1714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001716 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001718#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (keylen == 0) {
1721 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1722 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001723
1724#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001726#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001727 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001729 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 } else {
1731 ret = mbedtls_pem_read_buffer(&pem,
1732 "-----BEGIN RSA PUBLIC KEY-----",
1733 "-----END RSA PUBLIC KEY-----",
1734 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001736
1737 if (ret == 0) {
1738 p = pem.buf;
1739 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1740 mbedtls_pem_free(&pem);
1741 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1742 }
1743
1744 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1745 mbedtls_pem_free(&pem);
1746 return ret;
1747 }
1748
1749 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1750 mbedtls_pk_free(ctx);
1751 }
1752
1753 mbedtls_pem_free(&pem);
1754 return ret;
1755 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1756 mbedtls_pem_free(&pem);
1757 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001758 }
1759#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001760
1761 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001763 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 } else {
1765 ret = mbedtls_pem_read_buffer(&pem,
1766 "-----BEGIN PUBLIC KEY-----",
1767 "-----END PUBLIC KEY-----",
1768 key, NULL, 0, &len);
1769 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001772 /*
1773 * Was PEM encoded
1774 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001775 p = pem.buf;
1776
Jethro Beekman01672442023-04-19 14:08:14 +02001777 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 mbedtls_pem_free(&pem);
1779 return ret;
1780 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1781 mbedtls_pem_free(&pem);
1782 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001783 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001786
1787#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1789 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001791
1792 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1793 return ret;
1794 }
1795
1796 p = (unsigned char *) key;
1797 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1798 if (ret == 0) {
1799 return ret;
1800 }
1801 mbedtls_pk_free(ctx);
1802 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1803 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1804 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001805 }
1806#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001807 p = (unsigned char *) key;
1808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001812}
1813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_PK_PARSE_C */