blob: 87b707dc8897b827cc043590a15ed5749845e1dc [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
52
Valerio Setti34f67552023-04-03 15:19:18 +020053#if defined(MBEDTLS_PSA_CRYPTO_C)
54#include "mbedtls/psa_util.h"
55#endif
56
57#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
59#endif
60
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020062
Gilles Peskine832f3492017-11-30 11:42:12 +010063#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020064/*
65 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020066 *
67 * The file is expected to contain either PEM or DER encoded data.
68 * A terminating null byte is always appended. It is included in the announced
69 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020072{
73 FILE *f;
74 long size;
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if ((f = fopen(path, "rb")) == NULL) {
77 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
78 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020079
Gilles Peskineda0913b2022-06-30 17:03:40 +020080 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 fseek(f, 0, SEEK_END);
84 if ((size = ftell(f)) == -1) {
85 fclose(f);
86 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020087 }
Gilles Peskine449bd832023-01-11 14:50:10 +010088 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020089
90 *n = (size_t) size;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (*n + 1 == 0 ||
93 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
94 fclose(f);
95 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (fread(*buf, 1, *n, f) != *n) {
99 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_platform_zeroize(*buf, *n);
102 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
109 (*buf)[*n] = '\0';
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200112 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116}
117
118/*
119 * Load and parse a private key
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
122 const char *path, const char *pwd,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 size_t n;
127 unsigned char *buf;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
130 return ret;
131 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (pwd == NULL) {
134 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
135 } else {
136 ret = mbedtls_pk_parse_key(ctx, buf, n,
137 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
138 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_platform_zeroize(buf, n);
141 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144}
145
146/*
147 * Load and parse a public key
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150{
Janos Follath24eed8d2019-11-22 13:21:35 +0000151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152 size_t n;
153 unsigned char *buf;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
156 return ret;
157 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_platform_zeroize(buf, n);
162 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Valerio Setti0d2980f2023-04-05 18:17:13 +0200168#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170 *
171 * ECParameters ::= CHOICE {
172 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100173 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175 * }
176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
178 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179{
Janos Follath24eed8d2019-11-22 13:21:35 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (end - *p < 1) {
183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
184 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
185 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100186
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100187 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100192#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 ) {
194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
195 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201
202 params->p = *p;
203 *p += params->len;
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (*p != end) {
206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
207 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100215 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
216 * WARNING: the resulting group should only be used with
217 * pk_group_id_from_specified(), since its base point may not be set correctly
218 * if it was encoded compressed.
219 *
220 * SpecifiedECDomain ::= SEQUENCE {
221 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
222 * fieldID FieldID {{FieldTypes}},
223 * curve Curve,
224 * base ECPoint,
225 * order INTEGER,
226 * cofactor INTEGER OPTIONAL,
227 * hash HashAlgorithm OPTIONAL,
228 * ...
229 * }
230 *
231 * We only support prime-field as field type, and ignore hash and cofactor.
232 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234{
Janos Follath24eed8d2019-11-22 13:21:35 +0000235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200237 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100238 const unsigned char *end_field, *end_curve;
239 size_t len;
240 int ver;
241
242 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
245 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (ver < 1 || ver > 3) {
248 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
249 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100250
251 /*
252 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
253 * fieldType FIELD-ID.&id({IOSet}),
254 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
255 * }
256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
258 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
259 return ret;
260 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100261
262 end_field = p + len;
263
264 /*
265 * FIELD-ID ::= TYPE-IDENTIFIER
266 * FieldTypes FIELD-ID ::= {
267 * { Prime-p IDENTIFIED BY prime-field } |
268 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
269 * }
270 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
273 return ret;
274 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
277 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
278 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100279 }
280
281 p += len;
282
283 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
285 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
286 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (p != end_field) {
291 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
292 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
293 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100294
295 /*
296 * Curve ::= SEQUENCE {
297 * a FieldElement,
298 * b FieldElement,
299 * seed BIT STRING OPTIONAL
300 * -- Shall be present if used in SpecifiedECDomain
301 * -- with version equal to ecdpVer2 or ecdpVer3
302 * }
303 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
305 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
306 return ret;
307 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308
309 end_curve = p + len;
310
311 /*
312 * FieldElement ::= OCTET STRING
313 * containing an integer in the case of a prime field
314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
316 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
317 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100318 }
319
320 p += len;
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
323 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 }
326
327 p += len;
328
329 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (p != end_curve) {
335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
336 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
337 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338
339 /*
340 * ECPoint ::= OCTET STRING
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
344 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
347 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100348 /*
349 * If we can't read the point because it's compressed, cheat by
350 * reading only the X coordinate and the parity bit of Y.
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
353 (p[0] != 0x02 && p[0] != 0x03) ||
354 len != mbedtls_mpi_size(&grp->P) + 1 ||
355 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
356 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
357 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
358 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100359 }
360 }
361
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100362 p += len;
363
364 /*
365 * order INTEGER
366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
368 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
369 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100372
373 /*
374 * Allow optional elements by purposefully not enforcing p == end here.
375 */
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100378}
379
380/*
381 * Find the group id associated with an (almost filled) group as generated by
382 * pk_group_from_specified(), or return an error if unknown.
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384static 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 +0100385{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100386 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_ecp_group ref;
388 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_ecp_group_free(&ref);
395 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396
397 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
399 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
400 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
401 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
402 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
403 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
404 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 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 +0100407 break;
408 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409 }
410
411cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100413
414 *grp_id = *id;
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421}
422
423/*
424 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
427 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428{
Janos Follath24eed8d2019-11-22 13:21:35 +0000429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000441 /* The API respecting lifecycle for mbedtls_ecp_group struct is
442 * _init(), _load() and _free(). In pk_group_id_from_specified() the
443 * temporary grp breaks that flow and it's members are populated
444 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
445 * which is assuming a group populated by _setup() may not clean-up
446 * properly -> Manually free it's members.
447 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000448 mbedtls_mpi_free(&grp.N);
449 mbedtls_mpi_free(&grp.P);
450 mbedtls_mpi_free(&grp.A);
451 mbedtls_mpi_free(&grp.B);
452 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457
458/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200459 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460 *
461 * ECParameters ::= CHOICE {
462 * namedCurve OBJECT IDENTIFIER
463 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
464 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467{
Janos Follath24eed8d2019-11-22 13:21:35 +0000468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (params->tag == MBEDTLS_ASN1_OID) {
472 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
473 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
474 }
475 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
478 return ret;
479 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100480#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100482#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484
485 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800486 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) {
489 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
490 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) {
493 return ret;
494 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497}
498
Jethro Beekman01672442023-04-19 14:08:14 +0200499#if defined(MBEDTLS_ECP_LIGHT)
500/*
501 * Helper function for deriving a public key from its private counterpart.
502 */
503static int pk_derive_public_key(mbedtls_ecp_keypair *eck,
504 const unsigned char *d, size_t d_len,
505 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
506{
507 int ret;
508#if defined(MBEDTLS_USE_PSA_CRYPTO)
509 psa_status_t status, destruction_status;
510 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
511 size_t curve_bits;
512 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
513 /* This buffer is used to store the private key at first and then the
514 * public one (but not at the same time). Therefore we size it for the
515 * latter since it's bigger. */
516 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
517 size_t key_len;
518 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
519
520 (void) f_rng;
521 (void) p_rng;
522
523 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
524 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
525
526 status = psa_import_key(&key_attr, d, d_len, &key_id);
527 ret = psa_pk_status_to_mbedtls(status);
528 if (ret != 0) {
529 return ret;
530 }
531
532 mbedtls_platform_zeroize(key_buf, sizeof(key_buf));
533
534 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
535 ret = psa_pk_status_to_mbedtls(status);
536 destruction_status = psa_destroy_key(key_id);
537 if (ret != 0) {
538 return ret;
539 } else if (destruction_status != PSA_SUCCESS) {
540 return psa_pk_status_to_mbedtls(destruction_status);
541 }
542
543 ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
544#else /* MBEDTLS_USE_PSA_CRYPTO */
545 (void) d;
546 (void) d_len;
547
548 ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
549#endif /* MBEDTLS_USE_PSA_CRYPTO */
550 return ret;
551}
552
553#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
554
555/*
556 * Load an RFC8410 EC key, which doesn't have any parameters
557 */
558static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
559 mbedtls_ecp_group_id grp_id,
560 mbedtls_ecp_group *grp)
561{
562 if (params->tag != 0 || params->len != 0) {
563 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
564 }
565
566 return mbedtls_ecp_group_load(grp, grp_id);
567}
568
569/*
570 * Parse an RFC 8410 encoded private EC key
571 *
572 * CurvePrivateKey ::= OCTET STRING
573 */
574static int pk_parse_key_rfc8410_der(mbedtls_ecp_keypair *eck,
575 unsigned char *key, size_t keylen, const unsigned char *end,
576 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
577{
578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
579 size_t len;
580
581 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
583 }
584
585 if (key + len != end) {
586 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
587 }
588
589 if ((ret = mbedtls_mpi_read_binary_le(&eck->d, key, len)) != 0) {
590 mbedtls_ecp_keypair_free(eck);
591 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
592 }
593
Jethro Beekman2e662c62023-05-03 12:56:54 +0200594 // pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
595 // which never contain a public key. As such, derive the public key
596 // unconditionally.
Jethro Beekman01672442023-04-19 14:08:14 +0200597 if ((ret = pk_derive_public_key(eck, key, len, f_rng, p_rng)) != 0) {
598 mbedtls_ecp_keypair_free(eck);
599 return ret;
600 }
601
602 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
603 mbedtls_ecp_keypair_free(eck);
604 return ret;
605 }
606
607 return 0;
608}
609#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
610#endif /* MBEDTLS_ECP_LIGHT */
611
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612/*
613 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100614 *
615 * The caller is responsible for clearing the structure upon failure if
616 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
620 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200621{
Janos Follath24eed8d2019-11-22 13:21:35 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
625 (const unsigned char *) *p, end - *p)) == 0) {
626 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627 }
628
629 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631 */
632 *p = (unsigned char *) end;
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200636#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639/*
640 * RSAPublicKey ::= SEQUENCE {
641 * modulus INTEGER, -- n
642 * publicExponent INTEGER -- e
643 * }
644 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645static int pk_get_rsapubkey(unsigned char **p,
646 const unsigned char *end,
647 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648{
Janos Follath24eed8d2019-11-22 13:21:35 +0000649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650 size_t len;
651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
653 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
655 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (*p + len != end) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
659 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
660 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100662 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
664 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
665 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
668 NULL, 0, NULL, 0)) != 0) {
669 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
670 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100671
672 *p += len;
673
674 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
676 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
677 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
680 NULL, 0, *p, len)) != 0) {
681 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
682 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100683
684 *p += len;
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (mbedtls_rsa_complete(rsa) != 0 ||
687 mbedtls_rsa_check_pubkey(rsa) != 0) {
688 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000689 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (*p != end) {
692 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
693 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
694 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699
700/* Get a PK algorithm identifier
701 *
702 * AlgorithmIdentifier ::= SEQUENCE {
703 * algorithm OBJECT IDENTIFIER,
704 * parameters ANY DEFINED BY algorithm OPTIONAL }
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706static int pk_get_pk_alg(unsigned char **p,
707 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200708 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
709 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710{
Janos Follath24eed8d2019-11-22 13:21:35 +0000711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
717 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
718 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719
Jethro Beekman01672442023-04-19 14:08:14 +0200720 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
721#if defined(MBEDTLS_ECP_LIGHT)
722 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
723 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
724 if (ret == 0) {
725 *pk_alg = MBEDTLS_PK_ECKEY;
726 }
727 }
728#else
729 (void) ec_grp_id;
730#endif
731 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
733 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734
735 /*
736 * No parameters with RSA (only for EC)
737 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (*pk_alg == MBEDTLS_PK_RSA &&
739 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
740 params->len != 0)) {
741 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200742 }
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745}
746
747/*
748 * SubjectPublicKeyInfo ::= SEQUENCE {
749 * algorithm AlgorithmIdentifier,
750 * subjectPublicKey BIT STRING }
751 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
753 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754{
Janos Follath24eed8d2019-11-22 13:21:35 +0000755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_asn1_buf alg_params;
758 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200759 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
763 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
764 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765 }
766
767 end = *p + len;
768
Jethro Beekman01672442023-04-19 14:08:14 +0200769 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return ret;
771 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
775 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (*p + len != end) {
778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
780 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
783 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
787 return ret;
788 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (pk_alg == MBEDTLS_PK_RSA) {
792 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200795#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200797#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
798 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti77a75682023-05-15 11:18:46 +0200799 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, &mbedtls_pk_ec_rw(*pk)->grp);
Jethro Beekman01672442023-04-19 14:08:14 +0200800 } else
801#endif
802 {
Valerio Setti77a75682023-05-15 11:18:46 +0200803 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec_rw(*pk)->grp);
Jethro Beekman01672442023-04-19 14:08:14 +0200804 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (ret == 0) {
Valerio Setti77a75682023-05-15 11:18:46 +0200806 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec_rw(*pk));
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200809#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (ret == 0 && *p != end) {
813 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
814 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
815 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (ret != 0) {
818 mbedtls_pk_free(pk);
819 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822}
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100826 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
827 *
828 * The value zero is:
829 * - never a valid value for an RSA parameter
830 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
831 *
832 * Since values can't be omitted in PKCS#1, passing a zero value to
833 * rsa_complete() would be incorrect, so reject zero values early.
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static int asn1_get_nonzero_mpi(unsigned char **p,
836 const unsigned char *end,
837 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100838{
839 int ret;
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 ret = mbedtls_asn1_get_mpi(p, end, X);
842 if (ret != 0) {
843 return ret;
844 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
847 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
848 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100851}
852
853/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854 * Parse a PKCS#1 encoded private RSA key
855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
857 const unsigned char *key,
858 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100860 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861 size_t len;
862 unsigned char *p, *end;
863
Hanno Beckerefa14e82017-10-11 19:45:19 +0100864 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100866
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 p = (unsigned char *) key;
868 end = p + keylen;
869
870 /*
871 * This function parses the RSAPrivateKey (PKCS#1)
872 *
873 * RSAPrivateKey ::= SEQUENCE {
874 * version Version,
875 * modulus INTEGER, -- n
876 * publicExponent INTEGER, -- e
877 * privateExponent INTEGER, -- d
878 * prime1 INTEGER, -- p
879 * prime2 INTEGER, -- q
880 * exponent1 INTEGER, -- d mod (p-1)
881 * exponent2 INTEGER, -- d mod (q-1)
882 * coefficient INTEGER, -- (inverse of q) mod p
883 * otherPrimeInfos OtherPrimeInfos OPTIONAL
884 * }
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
887 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
888 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 }
890
891 end = p + len;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
894 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 }
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (version != 0) {
898 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899 }
900
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100901 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
903 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
904 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100905 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100908 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
910 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
911 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100912 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100914
915 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
917 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
918 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100919 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100921
922 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
924 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
925 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100926 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100928
929 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
931 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
932 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100933 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100935
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100936#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500937 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
939 * that they can be easily recomputed from D, P and Q. However by
940 * parsing them from the PKCS1 structure it is possible to avoid
941 * recalculating them which both reduces the overhead of loading
942 * RSA private keys into memory and also avoids side channels which
943 * can arise when computing those values, since all of D, P, and Q
944 * are secret. See https://eprint.iacr.org/2020/055 for a
945 * description of one such attack.
946 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500947
Jack Lloyd80cc8112020-01-22 17:34:29 -0500948 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
950 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
951 goto cleanup;
952 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500953
954 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
956 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
957 goto cleanup;
958 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500959
960 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
962 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
963 goto cleanup;
964 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500965
Jack Lloyd60239752020-01-27 17:53:36 -0500966#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800967 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
969 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
970 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
971 goto cleanup;
972 }
Jack Lloyd60239752020-01-27 17:53:36 -0500973#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500974
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100975 /* rsa_complete() doesn't complete anything with the default
976 * implementation but is still called:
977 * - for the benefit of alternative implementation that may want to
978 * pre-compute stuff beyond what's provided (eg Montgomery factors)
979 * - as is also sanity-checks the key
980 *
981 * Furthermore, we also check the public part for consistency with
982 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
985 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100986 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100987 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if (p != end) {
990 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
991 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 }
993
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100994cleanup:
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100999 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if ((ret & 0xff80) == 0) {
1001 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1002 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001003 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 }
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Valerio Setti0d2980f2023-04-05 18:17:13 +02001013#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014/*
1015 * Parse a SEC1 encoded private EC key
1016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
1018 const unsigned char *key, size_t keylen,
1019 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020{
Janos Follath24eed8d2019-11-22 13:21:35 +00001021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001022 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001023 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001024 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001026 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 unsigned char *end = p + keylen;
1028 unsigned char *end2;
1029
1030 /*
1031 * RFC 5915, or SEC1 Appendix C.4
1032 *
1033 * ECPrivateKey ::= SEQUENCE {
1034 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1035 * privateKey OCTET STRING,
1036 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1037 * publicKey [1] BIT STRING OPTIONAL
1038 * }
1039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1041 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1042 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043 }
1044
1045 end = p + len;
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1048 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1049 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (version != 1) {
1052 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1053 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1056 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1057 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058
Jethro Beekman01672442023-04-19 14:08:14 +02001059 d = p;
1060 d_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
1062 mbedtls_ecp_keypair_free(eck);
1063 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 }
1065
1066 p += len;
1067
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001068 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001070 /*
1071 * Is 'parameters' present?
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1074 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1075 0)) == 0) {
1076 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
1077 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
1078 mbedtls_ecp_keypair_free(eck);
1079 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1082 mbedtls_ecp_keypair_free(eck);
1083 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001084 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001085 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001088 /*
1089 * Is 'publickey' present? If not, or if we can't read it (eg because it
1090 * is compressed), create it from the private key.
1091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1093 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1094 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001095 end2 = p + len;
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1098 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1099 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (p + len != end2) {
1102 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1103 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1104 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001107 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001109 /*
1110 * The only acceptable failure mode of pk_get_ecpubkey() above
1111 * is if the point format is not recognized.
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1114 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1115 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
1118 mbedtls_ecp_keypair_free(eck);
1119 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001120 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001121 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001122
Valerio Setti34f67552023-04-03 15:19:18 +02001123 if (!pubkey_done) {
Jethro Beekman01672442023-04-19 14:08:14 +02001124 if ((ret = pk_derive_public_key(eck, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti34f67552023-04-03 15:19:18 +02001125 mbedtls_ecp_keypair_free(eck);
Valerio Setti520c0382023-04-07 11:38:09 +02001126 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001127 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001128 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
1131 mbedtls_ecp_keypair_free(eck);
1132 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136}
Valerio Setti0d2980f2023-04-05 18:17:13 +02001137#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138
1139/*
1140 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001141 *
1142 * Notes:
1143 *
1144 * - This function does not own the key buffer. It is the
1145 * responsibility of the caller to take care of zeroizing
1146 * and freeing it after use.
1147 *
1148 * - The function is responsible for freeing the provided
1149 * PK context on failure.
1150 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 */
1152static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 mbedtls_pk_context *pk,
1154 const unsigned char *key, size_t keylen,
1155 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156{
1157 int ret, version;
1158 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 unsigned char *p = (unsigned char *) key;
1161 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001163 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165
Jethro Beekman01672442023-04-19 14:08:14 +02001166#if !defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001167 (void) f_rng;
1168 (void) p_rng;
1169#endif
1170
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001172 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173 *
1174 * PrivateKeyInfo ::= SEQUENCE {
1175 * version Version,
1176 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1177 * privateKey PrivateKey,
1178 * attributes [0] IMPLICIT Attributes OPTIONAL }
1179 *
1180 * Version ::= INTEGER
1181 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1182 * PrivateKey ::= OCTET STRING
1183 *
1184 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1185 */
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1188 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 }
1191
1192 end = p + len;
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (version != 0) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
Jethro Beekman01672442023-04-19 14:08:14 +02001202 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 return ret;
1204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1207 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (len < 1) {
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1212 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1213 }
1214
1215 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1216 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1217 }
1218
1219 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1220 return ret;
1221 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (pk_alg == MBEDTLS_PK_RSA) {
1225 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1226 mbedtls_pk_free(pk);
1227 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 }
1229 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001231#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001233#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
1234 if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
Valerio Setti77a75682023-05-15 11:18:46 +02001235 if ((ret = pk_use_ecparams_rfc8410(&params, ec_grp_id,
1236 &mbedtls_pk_ec_rw(*pk)->grp)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001237 (ret =
Valerio Setti77a75682023-05-15 11:18:46 +02001238 pk_parse_key_rfc8410_der(mbedtls_pk_ec_rw(*pk), p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001239 p_rng)) != 0) {
1240 mbedtls_pk_free(pk);
1241 return ret;
1242 }
1243 } else
1244#endif
1245 {
Valerio Setti77a75682023-05-15 11:18:46 +02001246 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec_rw(*pk)->grp)) != 0 ||
1247 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk), p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001248 mbedtls_pk_free(pk);
1249 return ret;
1250 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 }
1252 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +02001253#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257}
1258
1259/*
1260 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001261 *
1262 * To save space, the decryption happens in-place on the given key buffer.
1263 * Also, while this function may modify the keybuffer, it doesn't own it,
1264 * and instead it is the responsibility of the caller to zeroize and properly
1265 * free it after use.
1266 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 mbedtls_pk_context *pk,
1271 unsigned char *key, size_t keylen,
1272 const unsigned char *pwd, size_t pwdlen,
1273 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001275 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001277 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1280#if defined(MBEDTLS_PKCS12_C)
1281 mbedtls_cipher_type_t cipher_alg;
1282 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283#endif
1284
Hanno Becker2aa80a72017-09-07 15:28:45 +01001285 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 end = p + keylen;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (pwdlen == 0) {
1289 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1290 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
1292 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001293 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 *
1295 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1296 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1297 * encryptedData EncryptedData
1298 * }
1299 *
1300 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1301 *
1302 * EncryptedData ::= OCTET STRING
1303 *
1304 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001305 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 }
1311
1312 end = p + len;
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1316 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1320 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321
Hanno Beckerfab35692017-08-25 13:38:26 +01001322 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323
1324 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001325 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1329 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1330 cipher_alg, md_alg,
1331 pwd, pwdlen, p, len, buf)) != 0) {
1332 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1333 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1334 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001338
1339 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_PKCS12_C */
1342#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1344 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1345 p, len, buf)) != 0) {
1346 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1347 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1348 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001352
1353 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001356 {
1357 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001358 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (decrypted == 0) {
1361 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1362 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
1368/*
1369 * Parse a private key
1370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1372 const unsigned char *key, size_t keylen,
1373 const unsigned char *pwd, size_t pwdlen,
1374 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375{
Janos Follath24eed8d2019-11-22 13:21:35 +00001376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001381#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (keylen == 0) {
1384 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1385 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001386
1387#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001391 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001393 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 } else {
1395 ret = mbedtls_pem_read_buffer(&pem,
1396 "-----BEGIN RSA PRIVATE KEY-----",
1397 "-----END RSA PRIVATE KEY-----",
1398 key, pwd, pwdlen, &len);
1399 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (ret == 0) {
1402 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1403 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1404 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1405 pem.buf, pem.buflen)) != 0) {
1406 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 }
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_pem_free(&pem);
1410 return ret;
1411 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1412 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1413 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1414 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1415 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1416 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419
Valerio Setti0d2980f2023-04-05 18:17:13 +02001420#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001421 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001423 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 } else {
1425 ret = mbedtls_pem_read_buffer(&pem,
1426 "-----BEGIN EC PRIVATE KEY-----",
1427 "-----END EC PRIVATE KEY-----",
1428 key, pwd, pwdlen, &len);
1429 }
1430 if (ret == 0) {
1431 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti77a75682023-05-15 11:18:46 +02001434 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk),
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 pem.buf, pem.buflen,
1436 f_rng, p_rng)) != 0) {
1437 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 }
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_pem_free(&pem);
1441 return ret;
1442 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1443 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1444 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1445 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1446 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1447 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448 }
Valerio Setti0d2980f2023-04-05 18:17:13 +02001449#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001451 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 } else {
1455 ret = mbedtls_pem_read_buffer(&pem,
1456 "-----BEGIN PRIVATE KEY-----",
1457 "-----END PRIVATE KEY-----",
1458 key, NULL, 0, &len);
1459 }
1460 if (ret == 0) {
1461 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1462 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1463 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464 }
1465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_pem_free(&pem);
1467 return ret;
1468 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1469 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001473 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001475 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 } else {
1477 ret = mbedtls_pem_read_buffer(&pem,
1478 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1479 "-----END ENCRYPTED PRIVATE KEY-----",
1480 key, NULL, 0, &len);
1481 }
1482 if (ret == 0) {
1483 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1484 pwd, pwdlen, f_rng, p_rng)) != 0) {
1485 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 }
1487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_pem_free(&pem);
1489 return ret;
1490 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1491 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494#else
1495 ((void) pwd);
1496 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001498
1499 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001500 * At this point we only know it's not a PEM formatted key. Could be any
1501 * of the known DER encoded private key formats
1502 *
1503 * We try the different DER format parsers to see if one passes without
1504 * error
1505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001508 unsigned char *key_copy;
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1511 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1512 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1517 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 mbedtls_platform_zeroize(key_copy, keylen);
1520 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 }
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (ret == 0) {
1524 return 0;
1525 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 mbedtls_pk_free(pk);
1528 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1531 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1536 if (ret == 0) {
1537 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001538 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_pk_free(pk);
1541 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1546 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1547 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1548 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001549 }
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 mbedtls_pk_free(pk);
1552 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554
Valerio Setti0d2980f2023-04-05 18:17:13 +02001555#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1557 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti77a75682023-05-15 11:18:46 +02001558 pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk),
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 key, keylen, f_rng, p_rng) == 0) {
1560 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_pk_free(pk);
Valerio Setti0d2980f2023-04-05 18:17:13 +02001563#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001564
Hanno Becker780f0a42018-10-10 11:23:33 +01001565 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1566 * it is ok to leave the PK context initialized but not
1567 * freed: It is the caller's responsibility to call pk_init()
1568 * before calling this function, and to call pk_free()
1569 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1570 * isn't, this leads to mbedtls_pk_free() being called
1571 * twice, once here and once by the caller, but this is
1572 * also ok and in line with the mbedtls_pk_free() calls
1573 * on failed PEM parsing attempts. */
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576}
1577
1578/*
1579 * Parse a public key
1580 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1582 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001583{
Janos Follath24eed8d2019-11-22 13:21:35 +00001584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001585 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001586#if defined(MBEDTLS_RSA_C)
1587 const mbedtls_pk_info_t *pk_info;
1588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001592#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (keylen == 0) {
1595 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1596 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001597
1598#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001600#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001601 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001603 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 } else {
1605 ret = mbedtls_pem_read_buffer(&pem,
1606 "-----BEGIN RSA PUBLIC KEY-----",
1607 "-----END RSA PUBLIC KEY-----",
1608 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001609 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001610
1611 if (ret == 0) {
1612 p = pem.buf;
1613 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1614 mbedtls_pem_free(&pem);
1615 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1616 }
1617
1618 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1619 mbedtls_pem_free(&pem);
1620 return ret;
1621 }
1622
1623 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1624 mbedtls_pk_free(ctx);
1625 }
1626
1627 mbedtls_pem_free(&pem);
1628 return ret;
1629 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1630 mbedtls_pem_free(&pem);
1631 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001632 }
1633#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001634
1635 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001637 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 } else {
1639 ret = mbedtls_pem_read_buffer(&pem,
1640 "-----BEGIN PUBLIC KEY-----",
1641 "-----END PUBLIC KEY-----",
1642 key, NULL, 0, &len);
1643 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646 /*
1647 * Was PEM encoded
1648 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001649 p = pem.buf;
1650
Jethro Beekman01672442023-04-19 14:08:14 +02001651 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 mbedtls_pem_free(&pem);
1653 return ret;
1654 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1655 mbedtls_pem_free(&pem);
1656 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001660
1661#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1663 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001664 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001665
1666 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1667 return ret;
1668 }
1669
1670 p = (unsigned char *) key;
1671 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1672 if (ret == 0) {
1673 return ret;
1674 }
1675 mbedtls_pk_free(ctx);
1676 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1677 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1678 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001679 }
1680#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681 p = (unsigned char *) key;
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686}
1687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_PK_PARSE_C */