blob: ccca692b7e1190c81e635d77fe97c5978b1b0eb9 [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 Galanakis8692ec82023-01-20 15:27:32 +0000432 /* The API respecting lifecycle for mbedtls_ecp_group struct is
433 * _init(), _load() and _free(). In pk_group_id_from_specified() the
434 * temporary grp breaks that flow and it's members are populated
435 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
436 * which is assuming a group populated by _setup() may not clean-up
437 * properly -> Manually free it's members.
438 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000439 mbedtls_mpi_free(&grp.N);
440 mbedtls_mpi_free(&grp.P);
441 mbedtls_mpi_free(&grp.A);
442 mbedtls_mpi_free(&grp.B);
443 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200450 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451 *
452 * ECParameters ::= CHOICE {
453 * namedCurve OBJECT IDENTIFIER
454 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
455 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200458{
Janos Follath24eed8d2019-11-22 13:21:35 +0000459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (params->tag == MBEDTLS_ASN1_OID) {
463 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
464 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
465 }
466 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
469 return ret;
470 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100471#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100473#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475
476 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800477 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) {
480 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
481 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) {
484 return ret;
485 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488}
489
490/*
491 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100492 *
493 * The caller is responsible for clearing the structure upon failure if
494 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
498 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
503 (const unsigned char *) *p, end - *p)) == 0) {
504 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505 }
506
507 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509 */
510 *p = (unsigned char *) end;
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517/*
518 * RSAPublicKey ::= SEQUENCE {
519 * modulus INTEGER, -- n
520 * publicExponent INTEGER -- e
521 * }
522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523static int pk_get_rsapubkey(unsigned char **p,
524 const unsigned char *end,
525 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526{
Janos Follath24eed8d2019-11-22 13:21:35 +0000527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528 size_t len;
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
531 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
532 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
533 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (*p + len != end) {
536 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
537 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
538 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100540 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
542 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
543 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
546 NULL, 0, NULL, 0)) != 0) {
547 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
548 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100549
550 *p += len;
551
552 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
554 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
555 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
558 NULL, 0, *p, len)) != 0) {
559 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
560 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100561
562 *p += len;
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (mbedtls_rsa_complete(rsa) != 0 ||
565 mbedtls_rsa_check_pubkey(rsa) != 0) {
566 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000567 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (*p != end) {
570 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
571 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
572 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200577
578/* Get a PK algorithm identifier
579 *
580 * AlgorithmIdentifier ::= SEQUENCE {
581 * algorithm OBJECT IDENTIFIER,
582 * parameters ANY DEFINED BY algorithm OPTIONAL }
583 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584static int pk_get_pk_alg(unsigned char **p,
585 const unsigned char *end,
586 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587{
Janos Follath24eed8d2019-11-22 13:21:35 +0000588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
594 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
595 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (mbedtls_oid_get_pk_alg(&alg_oid, pk_alg) != 0) {
598 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
599 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600
601 /*
602 * No parameters with RSA (only for EC)
603 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (*pk_alg == MBEDTLS_PK_RSA &&
605 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
606 params->len != 0)) {
607 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608 }
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611}
612
613/*
614 * SubjectPublicKeyInfo ::= SEQUENCE {
615 * algorithm AlgorithmIdentifier,
616 * subjectPublicKey BIT STRING }
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
619 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620{
Janos Follath24eed8d2019-11-22 13:21:35 +0000621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_asn1_buf alg_params;
624 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
625 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
629 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 }
631
632 end = *p + len;
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params)) != 0) {
635 return ret;
636 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
639 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
640 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (*p + len != end) {
643 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
644 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
645 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
648 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
649 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
652 return ret;
653 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (pk_alg == MBEDTLS_PK_RSA) {
657 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_RSA_C */
660#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
662 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
663 if (ret == 0) {
664 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
665 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (ret == 0 && *p != end) {
671 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
672 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
673 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (ret != 0) {
676 mbedtls_pk_free(pk);
677 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680}
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200683/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100684 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
685 *
686 * The value zero is:
687 * - never a valid value for an RSA parameter
688 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
689 *
690 * Since values can't be omitted in PKCS#1, passing a zero value to
691 * rsa_complete() would be incorrect, so reject zero values early.
692 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693static int asn1_get_nonzero_mpi(unsigned char **p,
694 const unsigned char *end,
695 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100696{
697 int ret;
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 ret = mbedtls_asn1_get_mpi(p, end, X);
700 if (ret != 0) {
701 return ret;
702 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
705 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
706 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100709}
710
711/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712 * Parse a PKCS#1 encoded private RSA key
713 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
715 const unsigned char *key,
716 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100718 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 size_t len;
720 unsigned char *p, *end;
721
Hanno Beckerefa14e82017-10-11 19:45:19 +0100722 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100724
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 p = (unsigned char *) key;
726 end = p + keylen;
727
728 /*
729 * This function parses the RSAPrivateKey (PKCS#1)
730 *
731 * RSAPrivateKey ::= SEQUENCE {
732 * version Version,
733 * modulus INTEGER, -- n
734 * publicExponent INTEGER, -- e
735 * privateExponent INTEGER, -- d
736 * prime1 INTEGER, -- p
737 * prime2 INTEGER, -- q
738 * exponent1 INTEGER, -- d mod (p-1)
739 * exponent2 INTEGER, -- d mod (q-1)
740 * coefficient INTEGER, -- (inverse of q) mod p
741 * otherPrimeInfos OtherPrimeInfos OPTIONAL
742 * }
743 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
745 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747 }
748
749 end = p + len;
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
752 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200753 }
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (version != 0) {
756 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757 }
758
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100759 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
761 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
762 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100766 /* Import E */
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 NULL, &T)) != 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 D */
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, NULL, NULL,
776 &T, 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 P */
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, &T, NULL,
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
787 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
789 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
790 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100794#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500795 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
797 * that they can be easily recomputed from D, P and Q. However by
798 * parsing them from the PKCS1 structure it is possible to avoid
799 * recalculating them which both reduces the overhead of loading
800 * RSA private keys into memory and also avoids side channels which
801 * can arise when computing those values, since all of D, P, and Q
802 * are secret. See https://eprint.iacr.org/2020/055 for a
803 * description of one such attack.
804 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500805
Jack Lloyd80cc8112020-01-22 17:34:29 -0500806 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
808 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
809 goto cleanup;
810 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500811
812 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
814 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
815 goto cleanup;
816 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500817
818 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
820 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
821 goto cleanup;
822 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500823
Jack Lloyd60239752020-01-27 17:53:36 -0500824#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800825 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
827 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
828 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
829 goto cleanup;
830 }
Jack Lloyd60239752020-01-27 17:53:36 -0500831#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500832
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100833 /* rsa_complete() doesn't complete anything with the default
834 * implementation but is still called:
835 * - for the benefit of alternative implementation that may want to
836 * pre-compute stuff beyond what's provided (eg Montgomery factors)
837 * - as is also sanity-checks the key
838 *
839 * Furthermore, we also check the public part for consistency with
840 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
843 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100844 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100845 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (p != end) {
848 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
849 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 }
851
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100852cleanup:
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100857 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if ((ret & 0xff80) == 0) {
859 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
860 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865 }
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872/*
873 * Parse a SEC1 encoded private EC key
874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
876 const unsigned char *key, size_t keylen,
877 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100880 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700882 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 unsigned char *p = (unsigned char *) key;
884 unsigned char *end = p + keylen;
885 unsigned char *end2;
886
887 /*
888 * RFC 5915, or SEC1 Appendix C.4
889 *
890 * ECPrivateKey ::= SEQUENCE {
891 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
892 * privateKey OCTET STRING,
893 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
894 * publicKey [1] BIT STRING OPTIONAL
895 * }
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
898 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900 }
901
902 end = p + len;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
905 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
906 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (version != 1) {
909 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
910 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
913 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
914 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
917 mbedtls_ecp_keypair_free(eck);
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 }
920
921 p += len;
922
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200923 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200925 /*
926 * Is 'parameters' present?
927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
929 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
930 0)) == 0) {
931 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
932 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
933 mbedtls_ecp_keypair_free(eck);
934 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200935 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
937 mbedtls_ecp_keypair_free(eck);
938 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100939 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800940 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200943 /*
944 * Is 'publickey' present? If not, or if we can't read it (eg because it
945 * is compressed), create it from the private key.
946 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
948 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
949 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950 end2 = p + len;
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
953 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
954 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (p + len != end2) {
957 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
958 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
959 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200962 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964 /*
965 * The only acceptable failure mode of pk_get_ecpubkey() above
966 * is if the point format is not recognized.
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
969 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
970 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200971 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
973 mbedtls_ecp_keypair_free(eck);
974 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (!pubkey_done &&
979 (ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
980 f_rng, p_rng)) != 0) {
981 mbedtls_ecp_keypair_free(eck);
982 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200983 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
986 mbedtls_ecp_keypair_free(eck);
987 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988 }
989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993
994/*
995 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100996 *
997 * Notes:
998 *
999 * - This function does not own the key buffer. It is the
1000 * responsibility of the caller to take care of zeroizing
1001 * and freeing it after use.
1002 *
1003 * - The function is responsible for freeing the provided
1004 * PK context on failure.
1005 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 */
1007static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_pk_context *pk,
1009 const unsigned char *key, size_t keylen,
1010 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011{
1012 int ret, version;
1013 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015 unsigned char *p = (unsigned char *) key;
1016 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1018 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001020#if !defined(MBEDTLS_ECP_C)
1021 (void) f_rng;
1022 (void) p_rng;
1023#endif
1024
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001026 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 *
1028 * PrivateKeyInfo ::= SEQUENCE {
1029 * version Version,
1030 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1031 * privateKey PrivateKey,
1032 * attributes [0] IMPLICIT Attributes OPTIONAL }
1033 *
1034 * Version ::= INTEGER
1035 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1036 * PrivateKey ::= OCTET STRING
1037 *
1038 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1039 */
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1042 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1043 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 }
1045
1046 end = p + len;
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1049 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001050 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (version != 0) {
1053 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1054 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1057 return ret;
1058 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1061 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1062 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (len < 1) {
1065 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1066 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1067 }
1068
1069 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1070 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1071 }
1072
1073 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1074 return ret;
1075 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (pk_alg == MBEDTLS_PK_RSA) {
1079 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1080 mbedtls_pk_free(pk);
1081 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082 }
1083 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#endif /* MBEDTLS_RSA_C */
1085#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1087 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1088 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
1089 mbedtls_pk_free(pk);
1090 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 }
1092 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097}
1098
1099/*
1100 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001101 *
1102 * To save space, the decryption happens in-place on the given key buffer.
1103 * Also, while this function may modify the keybuffer, it doesn't own it,
1104 * and instead it is the responsibility of the caller to zeroize and properly
1105 * free it after use.
1106 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_pk_context *pk,
1111 unsigned char *key, size_t keylen,
1112 const unsigned char *pwd, size_t pwdlen,
1113 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001115 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001117 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1120#if defined(MBEDTLS_PKCS12_C)
1121 mbedtls_cipher_type_t cipher_alg;
1122 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123#endif
1124
Hanno Becker2aa80a72017-09-07 15:28:45 +01001125 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 end = p + keylen;
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (pwdlen == 0) {
1129 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1130 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
1132 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001133 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 *
1135 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1136 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1137 * encryptedData EncryptedData
1138 * }
1139 *
1140 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1141 *
1142 * EncryptedData ::= OCTET STRING
1143 *
1144 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001145 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1148 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1149 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 }
1151
1152 end = p + len;
1153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1155 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1156 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1159 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1160 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161
Hanno Beckerfab35692017-08-25 13:38:26 +01001162 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
1164 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001165 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1169 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1170 cipher_alg, md_alg,
1171 pwd, pwdlen, p, len, buf)) != 0) {
1172 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1173 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1174 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001178
1179 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#endif /* MBEDTLS_PKCS12_C */
1182#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1184 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1185 p, len, buf)) != 0) {
1186 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1187 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1188 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001192
1193 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001196 {
1197 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (decrypted == 0) {
1201 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1202 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207
1208/*
1209 * Parse a private key
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1212 const unsigned char *key, size_t keylen,
1213 const unsigned char *pwd, size_t pwdlen,
1214 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215{
Janos Follath24eed8d2019-11-22 13:21:35 +00001216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001221#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (keylen == 0) {
1224 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1225 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001226
1227#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001231 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001233 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 } else {
1235 ret = mbedtls_pem_read_buffer(&pem,
1236 "-----BEGIN RSA PRIVATE KEY-----",
1237 "-----END RSA PRIVATE KEY-----",
1238 key, pwd, pwdlen, &len);
1239 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (ret == 0) {
1242 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1243 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1244 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1245 pem.buf, pem.buflen)) != 0) {
1246 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
1248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 mbedtls_pem_free(&pem);
1250 return ret;
1251 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1252 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1253 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1254 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1255 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1256 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001261 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001263 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 } else {
1265 ret = mbedtls_pem_read_buffer(&pem,
1266 "-----BEGIN EC PRIVATE KEY-----",
1267 "-----END EC PRIVATE KEY-----",
1268 key, pwd, pwdlen, &len);
1269 }
1270 if (ret == 0) {
1271 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1274 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1275 pem.buf, pem.buflen,
1276 f_rng, p_rng)) != 0) {
1277 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 }
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_pem_free(&pem);
1281 return ret;
1282 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1283 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1284 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1285 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1286 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1287 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001293 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 } else {
1295 ret = mbedtls_pem_read_buffer(&pem,
1296 "-----BEGIN PRIVATE KEY-----",
1297 "-----END PRIVATE KEY-----",
1298 key, NULL, 0, &len);
1299 }
1300 if (ret == 0) {
1301 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1302 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1303 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_pem_free(&pem);
1307 return ret;
1308 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1309 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001313 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001315 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 } else {
1317 ret = mbedtls_pem_read_buffer(&pem,
1318 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1319 "-----END ENCRYPTED PRIVATE KEY-----",
1320 key, NULL, 0, &len);
1321 }
1322 if (ret == 0) {
1323 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1324 pwd, pwdlen, f_rng, p_rng)) != 0) {
1325 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 mbedtls_pem_free(&pem);
1329 return ret;
1330 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1331 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334#else
1335 ((void) pwd);
1336 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338
1339 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001340 * At this point we only know it's not a PEM formatted key. Could be any
1341 * of the known DER encoded private key formats
1342 *
1343 * We try the different DER format parsers to see if one passes without
1344 * error
1345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001348 unsigned char *key_copy;
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1351 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1352 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1357 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_platform_zeroize(key_copy, keylen);
1360 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (ret == 0) {
1364 return 0;
1365 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 mbedtls_pk_free(pk);
1368 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1371 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1376 if (ret == 0) {
1377 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001378 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 mbedtls_pk_free(pk);
1381 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1386 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1387 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1388 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 }
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 mbedtls_pk_free(pk);
1392 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1397 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1398 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1399 key, keylen, f_rng, p_rng) == 0) {
1400 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Hanno Becker780f0a42018-10-10 11:23:33 +01001405 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1406 * it is ok to leave the PK context initialized but not
1407 * freed: It is the caller's responsibility to call pk_init()
1408 * before calling this function, and to call pk_free()
1409 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1410 * isn't, this leads to mbedtls_pk_free() being called
1411 * twice, once here and once by the caller, but this is
1412 * also ok and in line with the mbedtls_pk_free() calls
1413 * on failed PEM parsing attempts. */
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416}
1417
1418/*
1419 * Parse a public key
1420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1422 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423{
Janos Follath24eed8d2019-11-22 13:21:35 +00001424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001426#if defined(MBEDTLS_RSA_C)
1427 const mbedtls_pk_info_t *pk_info;
1428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001432#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (keylen == 0) {
1435 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1436 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001437
1438#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001440#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 } else {
1445 ret = mbedtls_pem_read_buffer(&pem,
1446 "-----BEGIN RSA PUBLIC KEY-----",
1447 "-----END RSA PUBLIC KEY-----",
1448 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001449 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001450
1451 if (ret == 0) {
1452 p = pem.buf;
1453 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1454 mbedtls_pem_free(&pem);
1455 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1456 }
1457
1458 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1459 mbedtls_pem_free(&pem);
1460 return ret;
1461 }
1462
1463 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1464 mbedtls_pk_free(ctx);
1465 }
1466
1467 mbedtls_pem_free(&pem);
1468 return ret;
1469 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1470 mbedtls_pem_free(&pem);
1471 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001472 }
1473#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001474
1475 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001477 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 } else {
1479 ret = mbedtls_pem_read_buffer(&pem,
1480 "-----BEGIN PUBLIC KEY-----",
1481 "-----END PUBLIC KEY-----",
1482 key, NULL, 0, &len);
1483 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 /*
1487 * Was PEM encoded
1488 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001489 p = pem.buf;
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1492 mbedtls_pem_free(&pem);
1493 return ret;
1494 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1495 mbedtls_pem_free(&pem);
1496 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001500
1501#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1503 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001504 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001505
1506 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1507 return ret;
1508 }
1509
1510 p = (unsigned char *) key;
1511 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1512 if (ret == 0) {
1513 return ret;
1514 }
1515 mbedtls_pk_free(ctx);
1516 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1517 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1518 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001519 }
1520#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 p = (unsigned char *) key;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526}
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_PK_PARSE_C */