blob: 53e6dd0ba51c2b3805a113fccbcb7df8478120e2 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020052
Gilles Peskine832f3492017-11-30 11:42:12 +010053#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020054/*
55 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020056 *
57 * The file is expected to contain either PEM or DER encoded data.
58 * A terminating null byte is always appended. It is included in the announced
59 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020060 */
Gilles Peskine449bd832023-01-11 14:50:10 +010061int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020062{
63 FILE *f;
64 long size;
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if ((f = fopen(path, "rb")) == NULL) {
67 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
68 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020069
Gilles Peskineda0913b2022-06-30 17:03:40 +020070 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_setbuf(f, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 fseek(f, 0, SEEK_END);
74 if ((size = ftell(f)) == -1) {
75 fclose(f);
76 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020077 }
Gilles Peskine449bd832023-01-11 14:50:10 +010078 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020079
80 *n = (size_t) size;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (*n + 1 == 0 ||
83 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
84 fclose(f);
85 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020086 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (fread(*buf, 1, *n, f) != *n) {
89 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_platform_zeroize(*buf, *n);
92 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 (*buf)[*n] = '\0';
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200102 ++*n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106}
107
108/*
109 * Load and parse a private key
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
112 const char *path, const char *pwd,
113 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114{
Janos Follath24eed8d2019-11-22 13:21:35 +0000115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116 size_t n;
117 unsigned char *buf;
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
120 return ret;
121 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (pwd == NULL) {
124 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
125 } else {
126 ret = mbedtls_pk_parse_key(ctx, buf, n,
127 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
128 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_platform_zeroize(buf, n);
131 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134}
135
136/*
137 * Load and parse a public key
138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140{
Janos Follath24eed8d2019-11-22 13:21:35 +0000141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142 size_t n;
143 unsigned char *buf;
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
146 return ret;
147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_platform_zeroize(buf, n);
152 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_ECP_C)
159/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160 *
161 * ECParameters ::= CHOICE {
162 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100163 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165 * }
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
168 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169{
Janos Follath24eed8d2019-11-22 13:21:35 +0000170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (end - *p < 1) {
173 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
174 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
175 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100176
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100177 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100182#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 ) {
184 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
185 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100186 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191
192 params->p = *p;
193 *p += params->len;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*p != end) {
196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
197 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
206 * WARNING: the resulting group should only be used with
207 * pk_group_id_from_specified(), since its base point may not be set correctly
208 * if it was encoded compressed.
209 *
210 * SpecifiedECDomain ::= SEQUENCE {
211 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
212 * fieldID FieldID {{FieldTypes}},
213 * curve Curve,
214 * base ECPoint,
215 * order INTEGER,
216 * cofactor INTEGER OPTIONAL,
217 * hash HashAlgorithm OPTIONAL,
218 * ...
219 * }
220 *
221 * We only support prime-field as field type, and ignore hash and cofactor.
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224{
Janos Follath24eed8d2019-11-22 13:21:35 +0000225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100226 unsigned char *p = params->p;
227 const unsigned char * const end = params->p + params->len;
228 const unsigned char *end_field, *end_curve;
229 size_t len;
230 int ver;
231
232 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
235 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (ver < 1 || ver > 3) {
238 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
239 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100240
241 /*
242 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
243 * fieldType FIELD-ID.&id({IOSet}),
244 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
245 * }
246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
249 return ret;
250 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100251
252 end_field = p + len;
253
254 /*
255 * FIELD-ID ::= TYPE-IDENTIFIER
256 * FieldTypes FIELD-ID ::= {
257 * { Prime-p IDENTIFIED BY prime-field } |
258 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
259 * }
260 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
263 return ret;
264 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
267 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
268 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100269 }
270
271 p += len;
272
273 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
275 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
276 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (p != end_field) {
281 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
282 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
283 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
285 /*
286 * Curve ::= SEQUENCE {
287 * a FieldElement,
288 * b FieldElement,
289 * seed BIT STRING OPTIONAL
290 * -- Shall be present if used in SpecifiedECDomain
291 * -- with version equal to ecdpVer2 or ecdpVer3
292 * }
293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
295 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
296 return ret;
297 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100298
299 end_curve = p + len;
300
301 /*
302 * FieldElement ::= OCTET STRING
303 * containing an integer in the case of a prime field
304 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
306 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308 }
309
310 p += len;
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
313 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
314 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
316
317 p += len;
318
319 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (p != end_curve) {
325 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
326 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
327 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328
329 /*
330 * ECPoint ::= OCTET STRING
331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
334 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
337 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100338 /*
339 * If we can't read the point because it's compressed, cheat by
340 * reading only the X coordinate and the parity bit of Y.
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
343 (p[0] != 0x02 && p[0] != 0x03) ||
344 len != mbedtls_mpi_size(&grp->P) + 1 ||
345 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
346 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
347 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
348 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100349 }
350 }
351
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352 p += len;
353
354 /*
355 * order INTEGER
356 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
359 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100362
363 /*
364 * Allow optional elements by purposefully not enforcing p == end here.
365 */
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368}
369
370/*
371 * Find the group id associated with an (almost filled) group as generated by
372 * pk_group_from_specified(), or return an error if unknown.
373 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374static 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 +0100375{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100376 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_ecp_group ref;
378 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_ecp_group_free(&ref);
385 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100386
387 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
389 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
390 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
391 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
392 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
393 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
394 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 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 +0100397 break;
398 }
399
400 }
401
402cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
405 *grp_id = *id;
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412}
413
414/*
415 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
418 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419{
Janos Follath24eed8d2019-11-22 13:21:35 +0000420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431cleanup:
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000432 mbedtls_mpi_free(&grp.N);
433 mbedtls_mpi_free(&grp.P);
434 mbedtls_mpi_free(&grp.A);
435 mbedtls_mpi_free(&grp.B);
436 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200443 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 *
445 * ECParameters ::= CHOICE {
446 * namedCurve OBJECT IDENTIFIER
447 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
448 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200451{
Janos Follath24eed8d2019-11-22 13:21:35 +0000452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (params->tag == MBEDTLS_ASN1_OID) {
456 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
457 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
458 }
459 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
462 return ret;
463 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100464#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100466#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200468
469 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800470 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) {
473 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
474 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) {
477 return ret;
478 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481}
482
483/*
484 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100485 *
486 * The caller is responsible for clearing the structure upon failure if
487 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
491 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492{
Janos Follath24eed8d2019-11-22 13:21:35 +0000493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
496 (const unsigned char *) *p, end - *p)) == 0) {
497 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498 }
499
500 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502 */
503 *p = (unsigned char *) end;
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510/*
511 * RSAPublicKey ::= SEQUENCE {
512 * modulus INTEGER, -- n
513 * publicExponent INTEGER -- e
514 * }
515 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516static int pk_get_rsapubkey(unsigned char **p,
517 const unsigned char *end,
518 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519{
Janos Follath24eed8d2019-11-22 13:21:35 +0000520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521 size_t len;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
524 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
525 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
526 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (*p + len != end) {
529 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
530 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
531 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200532
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100533 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
535 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
536 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
539 NULL, 0, NULL, 0)) != 0) {
540 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
541 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542
543 *p += len;
544
545 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
547 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
548 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
551 NULL, 0, *p, len)) != 0) {
552 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
553 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100554
555 *p += len;
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (mbedtls_rsa_complete(rsa) != 0 ||
558 mbedtls_rsa_check_pubkey(rsa) != 0) {
559 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000560 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (*p != end) {
563 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
564 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
565 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200570
571/* Get a PK algorithm identifier
572 *
573 * AlgorithmIdentifier ::= SEQUENCE {
574 * algorithm OBJECT IDENTIFIER,
575 * parameters ANY DEFINED BY algorithm OPTIONAL }
576 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577static int pk_get_pk_alg(unsigned char **p,
578 const unsigned char *end,
579 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200580{
Janos Follath24eed8d2019-11-22 13:21:35 +0000581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
587 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
588 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (mbedtls_oid_get_pk_alg(&alg_oid, pk_alg) != 0) {
591 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
592 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593
594 /*
595 * No parameters with RSA (only for EC)
596 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (*pk_alg == MBEDTLS_PK_RSA &&
598 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
599 params->len != 0)) {
600 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200601 }
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604}
605
606/*
607 * SubjectPublicKeyInfo ::= SEQUENCE {
608 * algorithm AlgorithmIdentifier,
609 * subjectPublicKey BIT STRING }
610 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
612 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613{
Janos Follath24eed8d2019-11-22 13:21:35 +0000614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_asn1_buf alg_params;
617 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
618 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
621 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
622 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623 }
624
625 end = *p + len;
626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params)) != 0) {
628 return ret;
629 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
632 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
633 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (*p + len != end) {
636 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
637 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
638 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
641 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
642 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
645 return ret;
646 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (pk_alg == MBEDTLS_PK_RSA) {
650 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_RSA_C */
653#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
655 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
656 if (ret == 0) {
657 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
658 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200659 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (ret == 0 && *p != end) {
664 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
665 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
666 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (ret != 0) {
669 mbedtls_pk_free(pk);
670 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673}
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100677 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
678 *
679 * The value zero is:
680 * - never a valid value for an RSA parameter
681 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
682 *
683 * Since values can't be omitted in PKCS#1, passing a zero value to
684 * rsa_complete() would be incorrect, so reject zero values early.
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int asn1_get_nonzero_mpi(unsigned char **p,
687 const unsigned char *end,
688 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100689{
690 int ret;
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 ret = mbedtls_asn1_get_mpi(p, end, X);
693 if (ret != 0) {
694 return ret;
695 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
698 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
699 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100702}
703
704/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200705 * Parse a PKCS#1 encoded private RSA key
706 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
708 const unsigned char *key,
709 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100711 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712 size_t len;
713 unsigned char *p, *end;
714
Hanno Beckerefa14e82017-10-11 19:45:19 +0100715 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100717
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718 p = (unsigned char *) key;
719 end = p + keylen;
720
721 /*
722 * This function parses the RSAPrivateKey (PKCS#1)
723 *
724 * RSAPrivateKey ::= SEQUENCE {
725 * version Version,
726 * modulus INTEGER, -- n
727 * publicExponent INTEGER, -- e
728 * privateExponent INTEGER, -- d
729 * prime1 INTEGER, -- p
730 * prime2 INTEGER, -- q
731 * exponent1 INTEGER, -- d mod (p-1)
732 * exponent2 INTEGER, -- d mod (q-1)
733 * coefficient INTEGER, -- (inverse of q) mod p
734 * otherPrimeInfos OtherPrimeInfos OPTIONAL
735 * }
736 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
738 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
739 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740 }
741
742 end = p + len;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
745 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200746 }
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (version != 0) {
749 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750 }
751
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100752 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
754 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
755 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100756 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100759 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
761 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
762 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765
766 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
768 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
769 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100770 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772
773 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
775 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
776 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100777 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779
780 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
782 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
783 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100786
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100787#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500788 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
790 * that they can be easily recomputed from D, P and Q. However by
791 * parsing them from the PKCS1 structure it is possible to avoid
792 * recalculating them which both reduces the overhead of loading
793 * RSA private keys into memory and also avoids side channels which
794 * can arise when computing those values, since all of D, P, and Q
795 * are secret. See https://eprint.iacr.org/2020/055 for a
796 * description of one such attack.
797 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500798
Jack Lloyd80cc8112020-01-22 17:34:29 -0500799 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
801 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
802 goto cleanup;
803 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500804
805 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
807 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
808 goto cleanup;
809 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500810
811 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
813 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
814 goto cleanup;
815 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500816
Jack Lloyd60239752020-01-27 17:53:36 -0500817#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800818 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
820 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
821 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
822 goto cleanup;
823 }
Jack Lloyd60239752020-01-27 17:53:36 -0500824#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500825
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100826 /* rsa_complete() doesn't complete anything with the default
827 * implementation but is still called:
828 * - for the benefit of alternative implementation that may want to
829 * pre-compute stuff beyond what's provided (eg Montgomery factors)
830 * - as is also sanity-checks the key
831 *
832 * Furthermore, we also check the public part for consistency with
833 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
836 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100837 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100838 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (p != end) {
841 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
842 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200843 }
844
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845cleanup:
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100850 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if ((ret & 0xff80) == 0) {
852 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
853 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858 }
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865/*
866 * Parse a SEC1 encoded private EC key
867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
869 const unsigned char *key, size_t keylen,
870 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871{
Janos Follath24eed8d2019-11-22 13:21:35 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100873 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700875 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876 unsigned char *p = (unsigned char *) key;
877 unsigned char *end = p + keylen;
878 unsigned char *end2;
879
880 /*
881 * RFC 5915, or SEC1 Appendix C.4
882 *
883 * ECPrivateKey ::= SEQUENCE {
884 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
885 * privateKey OCTET STRING,
886 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
887 * publicKey [1] BIT STRING OPTIONAL
888 * }
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
891 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
892 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893 }
894
895 end = p + len;
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
898 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
899 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (version != 1) {
902 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
903 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
906 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
910 mbedtls_ecp_keypair_free(eck);
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 }
913
914 p += len;
915
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200916 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200918 /*
919 * Is 'parameters' present?
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
922 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
923 0)) == 0) {
924 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
925 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
926 mbedtls_ecp_keypair_free(eck);
927 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200928 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
930 mbedtls_ecp_keypair_free(eck);
931 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100932 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800933 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200936 /*
937 * Is 'publickey' present? If not, or if we can't read it (eg because it
938 * is compressed), create it from the private key.
939 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
941 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
942 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200943 end2 = p + len;
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
946 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
947 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (p + len != end2) {
950 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
951 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
952 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200955 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200957 /*
958 * The only acceptable failure mode of pk_get_ecpubkey() above
959 * is if the point format is not recognized.
960 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
962 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
963 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
966 mbedtls_ecp_keypair_free(eck);
967 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200968 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (!pubkey_done &&
972 (ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
973 f_rng, p_rng)) != 0) {
974 mbedtls_ecp_keypair_free(eck);
975 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200976 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
979 mbedtls_ecp_keypair_free(eck);
980 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 }
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986
987/*
988 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100989 *
990 * Notes:
991 *
992 * - This function does not own the key buffer. It is the
993 * responsibility of the caller to take care of zeroizing
994 * and freeing it after use.
995 *
996 * - The function is responsible for freeing the provided
997 * PK context on failure.
998 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999 */
1000static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_pk_context *pk,
1002 const unsigned char *key, size_t keylen,
1003 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004{
1005 int ret, version;
1006 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 unsigned char *p = (unsigned char *) key;
1009 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1011 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001013#if !defined(MBEDTLS_ECP_C)
1014 (void) f_rng;
1015 (void) p_rng;
1016#endif
1017
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001019 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 *
1021 * PrivateKeyInfo ::= SEQUENCE {
1022 * version Version,
1023 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1024 * privateKey PrivateKey,
1025 * attributes [0] IMPLICIT Attributes OPTIONAL }
1026 *
1027 * Version ::= INTEGER
1028 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1029 * PrivateKey ::= OCTET STRING
1030 *
1031 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1032 */
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1035 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1036 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 }
1038
1039 end = p + len;
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1042 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001043 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (version != 0) {
1046 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1047 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1050 return ret;
1051 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1054 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1055 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (len < 1) {
1058 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1059 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1060 }
1061
1062 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1063 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1064 }
1065
1066 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1067 return ret;
1068 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (pk_alg == MBEDTLS_PK_RSA) {
1072 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1073 mbedtls_pk_free(pk);
1074 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 }
1076 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_RSA_C */
1078#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1080 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1081 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1082 mbedtls_pk_free(pk);
1083 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084 }
1085 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090}
1091
1092/*
1093 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001094 *
1095 * To save space, the decryption happens in-place on the given key buffer.
1096 * Also, while this function may modify the keybuffer, it doesn't own it,
1097 * and instead it is the responsibility of the caller to zeroize and properly
1098 * free it after use.
1099 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_pk_context *pk,
1104 unsigned char *key, size_t keylen,
1105 const unsigned char *pwd, size_t pwdlen,
1106 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001108 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001110 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1113#if defined(MBEDTLS_PKCS12_C)
1114 mbedtls_cipher_type_t cipher_alg;
1115 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116#endif
1117
Hanno Becker2aa80a72017-09-07 15:28:45 +01001118 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 end = p + keylen;
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (pwdlen == 0) {
1122 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1123 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124
1125 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001126 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127 *
1128 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1129 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1130 * encryptedData EncryptedData
1131 * }
1132 *
1133 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1134 *
1135 * EncryptedData ::= OCTET STRING
1136 *
1137 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001138 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1141 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1142 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 }
1144
1145 end = p + len;
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1148 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1149 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1152 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1153 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154
Hanno Beckerfab35692017-08-25 13:38:26 +01001155 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156
1157 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001158 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1162 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1163 cipher_alg, md_alg,
1164 pwd, pwdlen, p, len, buf)) != 0) {
1165 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1166 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1167 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001171
1172 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS12_C */
1175#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1177 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1178 p, len, buf)) != 0) {
1179 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1180 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1181 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001185
1186 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001189 {
1190 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001191 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (decrypted == 0) {
1194 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1195 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200
1201/*
1202 * Parse a private key
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1205 const unsigned char *key, size_t keylen,
1206 const unsigned char *pwd, size_t pwdlen,
1207 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208{
Janos Follath24eed8d2019-11-22 13:21:35 +00001209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001214#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (keylen == 0) {
1217 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1218 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001219
1220#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001224 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001226 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 } else {
1228 ret = mbedtls_pem_read_buffer(&pem,
1229 "-----BEGIN RSA PRIVATE KEY-----",
1230 "-----END RSA PRIVATE KEY-----",
1231 key, pwd, pwdlen, &len);
1232 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ret == 0) {
1235 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1236 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1237 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1238 pem.buf, pem.buflen)) != 0) {
1239 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 }
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_pem_free(&pem);
1243 return ret;
1244 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1245 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1246 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1247 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1248 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1249 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001254 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001256 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 } else {
1258 ret = mbedtls_pem_read_buffer(&pem,
1259 "-----BEGIN EC PRIVATE KEY-----",
1260 "-----END EC PRIVATE KEY-----",
1261 key, pwd, pwdlen, &len);
1262 }
1263 if (ret == 0) {
1264 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1267 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1268 pem.buf, pem.buflen,
1269 f_rng, p_rng)) != 0) {
1270 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271 }
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 mbedtls_pem_free(&pem);
1274 return ret;
1275 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1276 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1277 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1278 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1279 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1280 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001284 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001286 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 } else {
1288 ret = mbedtls_pem_read_buffer(&pem,
1289 "-----BEGIN PRIVATE KEY-----",
1290 "-----END PRIVATE KEY-----",
1291 key, NULL, 0, &len);
1292 }
1293 if (ret == 0) {
1294 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1295 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1296 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 mbedtls_pem_free(&pem);
1300 return ret;
1301 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1302 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001306 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001308 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 } else {
1310 ret = mbedtls_pem_read_buffer(&pem,
1311 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1312 "-----END ENCRYPTED PRIVATE KEY-----",
1313 key, NULL, 0, &len);
1314 }
1315 if (ret == 0) {
1316 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1317 pwd, pwdlen, f_rng, p_rng)) != 0) {
1318 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 }
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 mbedtls_pem_free(&pem);
1322 return ret;
1323 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1324 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327#else
1328 ((void) pwd);
1329 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331
1332 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001333 * At this point we only know it's not a PEM formatted key. Could be any
1334 * of the known DER encoded private key formats
1335 *
1336 * We try the different DER format parsers to see if one passes without
1337 * error
1338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001341 unsigned char *key_copy;
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1344 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1345 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1350 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_platform_zeroize(key_copy, keylen);
1353 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 }
1355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (ret == 0) {
1357 return 0;
1358 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 mbedtls_pk_free(pk);
1361 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1364 return ret;
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
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1369 if (ret == 0) {
1370 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001371 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 mbedtls_pk_free(pk);
1374 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1379 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1380 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1381 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 mbedtls_pk_free(pk);
1385 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1390 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1391 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1392 key, keylen, f_rng, p_rng) == 0) {
1393 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397
Hanno Becker780f0a42018-10-10 11:23:33 +01001398 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1399 * it is ok to leave the PK context initialized but not
1400 * freed: It is the caller's responsibility to call pk_init()
1401 * before calling this function, and to call pk_free()
1402 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1403 * isn't, this leads to mbedtls_pk_free() being called
1404 * twice, once here and once by the caller, but this is
1405 * also ok and in line with the mbedtls_pk_free() calls
1406 * on failed PEM parsing attempts. */
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409}
1410
1411/*
1412 * Parse a public key
1413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1415 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416{
Janos Follath24eed8d2019-11-22 13:21:35 +00001417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001419#if defined(MBEDTLS_RSA_C)
1420 const mbedtls_pk_info_t *pk_info;
1421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001425#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (keylen == 0) {
1428 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1429 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001430
1431#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001433#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001436 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 } else {
1438 ret = mbedtls_pem_read_buffer(&pem,
1439 "-----BEGIN RSA PUBLIC KEY-----",
1440 "-----END RSA PUBLIC KEY-----",
1441 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001443
1444 if (ret == 0) {
1445 p = pem.buf;
1446 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1447 mbedtls_pem_free(&pem);
1448 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1449 }
1450
1451 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1452 mbedtls_pem_free(&pem);
1453 return ret;
1454 }
1455
1456 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1457 mbedtls_pk_free(ctx);
1458 }
1459
1460 mbedtls_pem_free(&pem);
1461 return ret;
1462 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1463 mbedtls_pem_free(&pem);
1464 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001465 }
1466#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001467
1468 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001470 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else {
1472 ret = mbedtls_pem_read_buffer(&pem,
1473 "-----BEGIN PUBLIC KEY-----",
1474 "-----END PUBLIC KEY-----",
1475 key, NULL, 0, &len);
1476 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 /*
1480 * Was PEM encoded
1481 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001482 p = pem.buf;
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1485 mbedtls_pem_free(&pem);
1486 return ret;
1487 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1488 mbedtls_pem_free(&pem);
1489 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001493
1494#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1496 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001497 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001498
1499 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1500 return ret;
1501 }
1502
1503 p = (unsigned char *) key;
1504 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1505 if (ret == 0) {
1506 return ret;
1507 }
1508 mbedtls_pk_free(ctx);
1509 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1510 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1511 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001512 }
1513#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514 p = (unsigned char *) key;
1515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519}
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_PK_PARSE_C */