blob: 37d501640d24b8e2843fe55d744a5351cdfe6513 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1a7550a2013-09-15 13:01:22 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pk.h"
13#include "mbedtls/asn1.h"
14#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050015#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000016#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020017
Rich Evans00ab4702015-02-06 13:43:58 +000018#include <string.h>
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
38
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041/* Parameter validation macros based on platform_util.h */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042#define PK_VALIDATE_RET(cond) \
43 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA)
44#define PK_VALIDATE(cond) \
45 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046
Gilles Peskine832f3492017-11-30 11:42:12 +010047#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020048/*
49 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020050 *
51 * The file is expected to contain either PEM or DER encoded data.
52 * A terminating null byte is always appended. It is included in the announced
53 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020054 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker1a7550a2013-09-15 13:01:22 +020056{
57 FILE *f;
58 long size;
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 PK_VALIDATE_RET(path != NULL);
61 PK_VALIDATE_RET(buf != NULL);
62 PK_VALIDATE_RET(n != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 if ((f = fopen(path, "rb")) == NULL) {
65 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067
68 fseek(f, 0, SEEK_END);
69 if ((size = ftell(f)) == -1) {
70 fclose(f);
71 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
72 }
73 fseek(f, 0, SEEK_SET);
Paul Bakker1a7550a2013-09-15 13:01:22 +020074
75 *n = (size_t) size;
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 if (*n + 1 == 0 ||
78 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
79 fclose(f);
80 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020081 }
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 if (fread(*buf, 1, *n, f) != *n) {
84 fclose(f);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 mbedtls_platform_zeroize(*buf, *n);
87 mbedtls_free(*buf);
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 }
91
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 fclose(f);
Paul Bakker1a7550a2013-09-15 13:01:22 +020093
94 (*buf)[*n] = '\0';
95
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020097 ++*n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101}
102
103/*
104 * Load and parse a private key
105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
107 const char *path, const char *pwd)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108{
Janos Follath24eed8d2019-11-22 13:21:35 +0000109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110 size_t n;
111 unsigned char *buf;
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 PK_VALIDATE_RET(ctx != NULL);
114 PK_VALIDATE_RET(path != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
117 return ret;
118 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if (pwd == NULL) {
121 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0);
122 } else {
123 ret = mbedtls_pk_parse_key(ctx, buf, n,
124 (const unsigned char *) pwd, strlen(pwd));
125 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 mbedtls_platform_zeroize(buf, n);
128 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131}
132
133/*
134 * Load and parse a public key
135 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137{
Janos Follath24eed8d2019-11-22 13:21:35 +0000138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139 size_t n;
140 unsigned char *buf;
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 PK_VALIDATE_RET(ctx != NULL);
143 PK_VALIDATE_RET(path != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
146 return ret;
147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 mbedtls_platform_zeroize(buf, n);
152 mbedtls_free(buf);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100181 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100182#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100278 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100322 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100380 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100403 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
405 *grp_id = *id;
406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100409 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100423 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_ecp_group_free(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200439 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440 *
441 * ECParameters ::= CHOICE {
442 * namedCurve OBJECT IDENTIFIER
443 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
444 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200445 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200447{
Janos Follath24eed8d2019-11-22 13:21:35 +0000448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 if (params->tag == MBEDTLS_ASN1_OID) {
452 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
453 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
454 }
455 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
458 return ret;
459 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100460#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100462#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200464
465 /*
Shaun Case0e7791f2021-12-20 21:14:10 -0800466 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) {
469 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
470 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) {
473 return ret;
474 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477}
478
479/*
480 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100481 *
482 * The caller is responsible for clearing the structure upon failure if
483 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
487 mbedtls_ecp_keypair *key)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488{
Janos Follath24eed8d2019-11-22 13:21:35 +0000489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
492 (const unsigned char *) *p, end - *p)) == 0) {
493 ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494 }
495
496 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498 */
499 *p = (unsigned char *) end;
500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506/*
507 * RSAPublicKey ::= SEQUENCE {
508 * modulus INTEGER, -- n
509 * publicExponent INTEGER -- e
510 * }
511 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512static int pk_get_rsapubkey(unsigned char **p,
513 const unsigned char *end,
514 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515{
Janos Follath24eed8d2019-11-22 13:21:35 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517 size_t len;
518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
520 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
521 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
522 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 if (*p + len != end) {
525 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
526 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
527 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100529 /* Import N */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
531 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
532 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
535 NULL, 0, NULL, 0)) != 0) {
536 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
537 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100538
539 *p += len;
540
541 /* Import E */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
543 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
544 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
547 NULL, 0, *p, len)) != 0) {
548 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
549 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100550
551 *p += len;
552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553 if (mbedtls_rsa_complete(rsa) != 0 ||
554 mbedtls_rsa_check_pubkey(rsa) != 0) {
555 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000556 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 if (*p != end) {
559 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
560 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
561 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200566
567/* Get a PK algorithm identifier
568 *
569 * AlgorithmIdentifier ::= SEQUENCE {
570 * algorithm OBJECT IDENTIFIER,
571 * parameters ANY DEFINED BY algorithm OPTIONAL }
572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573static int pk_get_pk_alg(unsigned char **p,
574 const unsigned char *end,
575 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200576{
Janos Follath24eed8d2019-11-22 13:21:35 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
583 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
584 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 if (mbedtls_oid_get_pk_alg(&alg_oid, pk_alg) != 0) {
587 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
588 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
590 /*
591 * No parameters with RSA (only for EC)
592 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 if (*pk_alg == MBEDTLS_PK_RSA &&
594 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
595 params->len != 0)) {
596 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200597 }
598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600}
601
602/*
603 * SubjectPublicKeyInfo ::= SEQUENCE {
604 * algorithm AlgorithmIdentifier,
605 * subjectPublicKey BIT STRING }
606 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
608 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609{
Janos Follath24eed8d2019-11-22 13:21:35 +0000610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_asn1_buf alg_params;
613 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
614 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 PK_VALIDATE_RET(p != NULL);
617 PK_VALIDATE_RET(*p != NULL);
618 PK_VALIDATE_RET(end != NULL);
619 PK_VALIDATE_RET(pk != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
622 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
623 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200624 }
625
626 end = *p + len;
627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params)) != 0) {
629 return ret;
630 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
633 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
634 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (*p + len != end) {
637 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
639 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
642 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
643 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
646 return ret;
647 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100650 if (pk_alg == MBEDTLS_PK_RSA) {
651 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#endif /* MBEDTLS_RSA_C */
654#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
656 ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
657 if (ret == 0) {
658 ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
659 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#endif /* MBEDTLS_ECP_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 if (ret == 0 && *p != end) {
665 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
666 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
667 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669 if (ret != 0) {
670 mbedtls_pk_free(pk);
671 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674}
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200677/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100678 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
679 *
680 * The value zero is:
681 * - never a valid value for an RSA parameter
682 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
683 *
684 * Since values can't be omitted in PKCS#1, passing a zero value to
685 * rsa_complete() would be incorrect, so reject zero values early.
686 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687static int asn1_get_nonzero_mpi(unsigned char **p,
688 const unsigned char *end,
689 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100690{
691 int ret;
692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 ret = mbedtls_asn1_get_mpi(p, end, X);
694 if (ret != 0) {
695 return ret;
696 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
699 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
700 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100703}
704
705/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706 * Parse a PKCS#1 encoded private RSA key
707 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
709 const unsigned char *key,
710 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100712 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713 size_t len;
714 unsigned char *p, *end;
715
Hanno Beckerefa14e82017-10-11 19:45:19 +0100716 mbedtls_mpi T;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100718
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 p = (unsigned char *) key;
720 end = p + keylen;
721
722 /*
723 * This function parses the RSAPrivateKey (PKCS#1)
724 *
725 * RSAPrivateKey ::= SEQUENCE {
726 * version Version,
727 * modulus INTEGER, -- n
728 * publicExponent INTEGER, -- e
729 * privateExponent INTEGER, -- d
730 * prime1 INTEGER, -- p
731 * prime2 INTEGER, -- q
732 * exponent1 INTEGER, -- d mod (p-1)
733 * exponent2 INTEGER, -- d mod (q-1)
734 * coefficient INTEGER, -- (inverse of q) mod p
735 * otherPrimeInfos OtherPrimeInfos OPTIONAL
736 * }
737 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
739 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
740 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 }
742
743 end = p + len;
744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747 }
748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 if (version != 0) {
750 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200751 }
752
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753 /* Import N */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
755 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
756 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760 /* Import E */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
762 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
763 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100764 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100766
767 /* Import D */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
769 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
770 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100771 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100772 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100773
774 /* Import P */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
776 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
777 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100778 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100780
781 /* Import Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
783 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
784 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100785 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100787
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100788#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500789 /*
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
791 * that they can be easily recomputed from D, P and Q. However by
792 * parsing them from the PKCS1 structure it is possible to avoid
793 * recalculating them which both reduces the overhead of loading
794 * RSA private keys into memory and also avoids side channels which
795 * can arise when computing those values, since all of D, P, and Q
796 * are secret. See https://eprint.iacr.org/2020/055 for a
797 * description of one such attack.
798 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500799
Jack Lloyd80cc8112020-01-22 17:34:29 -0500800 /* Import DP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
802 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
803 goto cleanup;
804 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500805
806 /* Import DQ */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
808 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
809 goto cleanup;
810 }
Jack Lloyd80cc8112020-01-22 17:34:29 -0500811
812 /* Import QP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
814 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
815 goto cleanup;
816 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500817
Jack Lloyd60239752020-01-27 17:53:36 -0500818#else
Shaun Case0e7791f2021-12-20 21:14:10 -0800819 /* Verify existence of the CRT params */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
821 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
822 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
823 goto cleanup;
824 }
Jack Lloyd60239752020-01-27 17:53:36 -0500825#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500826
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100827 /* rsa_complete() doesn't complete anything with the default
828 * implementation but is still called:
829 * - for the benefit of alternative implementation that may want to
830 * pre-compute stuff beyond what's provided (eg Montgomery factors)
831 * - as is also sanity-checks the key
832 *
833 * Furthermore, we also check the public part for consistency with
834 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
835 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
837 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100839 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 if (p != end) {
842 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
843 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 }
845
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846cleanup:
847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100851 /* Wrap error code if it's coming from a lower level */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 if ((ret & 0xff80) == 0) {
853 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
854 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100855 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859 }
860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866/*
867 * Parse a SEC1 encoded private EC key
868 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
870 const unsigned char *key,
871 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100874 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877 unsigned char *p = (unsigned char *) key;
878 unsigned char *end = p + keylen;
879 unsigned char *end2;
880
881 /*
882 * RFC 5915, or SEC1 Appendix C.4
883 *
884 * ECPrivateKey ::= SEQUENCE {
885 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
886 * privateKey OCTET STRING,
887 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
888 * publicKey [1] BIT STRING OPTIONAL
889 * }
890 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
892 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
893 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 }
895
896 end = p + len;
897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
900 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (version != 1) {
903 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
904 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
907 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
908 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) {
911 mbedtls_ecp_keypair_free(eck);
912 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913 }
914
915 p += len;
916
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200917 pubkey_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200919 /*
920 * Is 'parameters' present?
921 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
923 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
924 0)) == 0) {
925 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
926 (ret = pk_use_ecparams(&params, &eck->grp)) != 0) {
927 mbedtls_ecp_keypair_free(eck);
928 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200929 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
931 mbedtls_ecp_keypair_free(eck);
932 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100933 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800934 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200937 /*
938 * Is 'publickey' present? If not, or if we can't read it (eg because it
939 * is compressed), create it from the private key.
940 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
942 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
943 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200944 end2 = p + len;
945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
947 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
948 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if (p + len != end2) {
951 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
952 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
953 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200956 pubkey_done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200958 /*
959 * The only acceptable failure mode of pk_get_ecpubkey() above
960 * is if the point format is not recognized.
961 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
963 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
964 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200965 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
967 mbedtls_ecp_keypair_free(eck);
968 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200969 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200970 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 if (!pubkey_done &&
973 (ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
974 NULL, NULL)) != 0) {
975 mbedtls_ecp_keypair_free(eck);
976 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200977 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) {
980 mbedtls_ecp_keypair_free(eck);
981 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 }
983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987
988/*
989 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100990 *
991 * Notes:
992 *
993 * - This function does not own the key buffer. It is the
994 * responsibility of the caller to take care of zeroizing
995 * and freeing it after use.
996 *
997 * - The function is responsible for freeing the provided
998 * PK context on failure.
999 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000 */
1001static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 mbedtls_pk_context *pk,
1003 const unsigned char *key,
1004 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005{
1006 int ret, version;
1007 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 unsigned char *p = (unsigned char *) key;
1010 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1012 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013
1014 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001015 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 *
1017 * PrivateKeyInfo ::= SEQUENCE {
1018 * version Version,
1019 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1020 * privateKey PrivateKey,
1021 * attributes [0] IMPLICIT Attributes OPTIONAL }
1022 *
1023 * Version ::= INTEGER
1024 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1025 * PrivateKey ::= OCTET STRING
1026 *
1027 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1028 */
1029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1031 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1032 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 }
1034
1035 end = p + len;
1036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1038 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001039 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 if (version != 0) {
1042 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1043 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params)) != 0) {
1046 return ret;
1047 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1050 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1051 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053 if (len < 1) {
1054 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1055 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1056 }
1057
1058 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1059 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1060 }
1061
1062 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1063 return ret;
1064 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 if (pk_alg == MBEDTLS_PK_RSA) {
1068 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1069 mbedtls_pk_free(pk);
1070 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071 }
1072 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_RSA_C */
1074#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
1076 if ((ret = pk_use_ecparams(&params, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
1077 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) {
1078 mbedtls_pk_free(pk);
1079 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 }
1081 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#endif /* MBEDTLS_ECP_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086}
1087
1088/*
1089 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001090 *
1091 * To save space, the decryption happens in-place on the given key buffer.
1092 * Also, while this function may modify the keybuffer, it doesn't own it,
1093 * and instead it is the responsibility of the caller to zeroize and properly
1094 * free it after use.
1095 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098static int pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 mbedtls_pk_context *pk,
1100 unsigned char *key, size_t keylen,
1101 const unsigned char *pwd, size_t pwdlen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001103 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001105 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1108#if defined(MBEDTLS_PKCS12_C)
1109 mbedtls_cipher_type_t cipher_alg;
1110 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111#endif
1112
Hanno Becker2aa80a72017-09-07 15:28:45 +01001113 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 end = p + keylen;
1115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 if (pwdlen == 0) {
1117 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1118 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119
1120 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001121 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122 *
1123 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1124 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1125 * encryptedData EncryptedData
1126 * }
1127 *
1128 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1129 *
1130 * EncryptedData ::= OCTET STRING
1131 *
1132 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001133 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1137 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 }
1139
1140 end = p + len;
1141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1143 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1144 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1147 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1148 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
Hanno Beckerfab35692017-08-25 13:38:26 +01001150 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151
1152 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001153 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
1157 if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1158 cipher_alg, md_alg,
1159 pwd, pwdlen, p, len, buf)) != 0) {
1160 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1161 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1162 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001166
1167 decrypted = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 } else if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid) == 0) {
1169 if ((ret = mbedtls_pkcs12_pbe_sha1_rc4_128(&pbe_params,
1170 MBEDTLS_PKCS12_PBE_DECRYPT,
1171 pwd, pwdlen,
1172 p, len, buf)) != 0) {
1173 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174 }
1175
1176 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 if (*buf != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
1180 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001182
1183 decrypted = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001184 } else
1185#endif /* MBEDTLS_PKCS12_C */
1186#if defined(MBEDTLS_PKCS5_C)
1187 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
1188 if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1189 p, len, buf)) != 0) {
1190 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1191 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1192 }
1193
1194 return ret;
1195 }
1196
1197 decrypted = 1;
1198 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001200 {
1201 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001202 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 if (decrypted == 0) {
1205 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1206 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
1212/*
1213 * Parse a private key
1214 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1216 const unsigned char *key, size_t keylen,
1217 const unsigned char *pwd, size_t pwdlen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218{
Janos Follath24eed8d2019-11-22 13:21:35 +00001219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Dave Rodgman94210b12023-06-28 14:08:07 +01001220 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001224#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225
Dave Rodgman94210b12023-06-28 14:08:07 +01001226 (void) pk_info;
1227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 PK_VALIDATE_RET(pk != NULL);
1229 if (keylen == 0) {
1230 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1231 }
1232 PK_VALIDATE_RET(key != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001233
1234#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001238 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001240 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 } else {
1242 ret = mbedtls_pem_read_buffer(&pem,
1243 "-----BEGIN RSA PRIVATE KEY-----",
1244 "-----END RSA PRIVATE KEY-----",
1245 key, pwd, pwdlen, &len);
1246 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 if (ret == 0) {
1249 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1250 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1251 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1252 pem.buf, pem.buflen)) != 0) {
1253 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254 }
1255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 mbedtls_pem_free(&pem);
1257 return ret;
1258 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1259 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1260 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1261 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1262 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1263 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001268 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001270 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 } else {
1272 ret = mbedtls_pem_read_buffer(&pem,
1273 "-----BEGIN EC PRIVATE KEY-----",
1274 "-----END EC PRIVATE KEY-----",
1275 key, pwd, pwdlen, &len);
1276 }
1277 if (ret == 0) {
1278 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1281 (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1282 pem.buf, pem.buflen)) != 0) {
1283 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 }
1285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 mbedtls_pem_free(&pem);
1287 return ret;
1288 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1289 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1290 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1291 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1292 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1293 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001297 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001299 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 } else {
1301 ret = mbedtls_pem_read_buffer(&pem,
1302 "-----BEGIN PRIVATE KEY-----",
1303 "-----END PRIVATE KEY-----",
1304 key, NULL, 0, &len);
1305 }
1306 if (ret == 0) {
1307 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1308 pem.buf, pem.buflen)) != 0) {
1309 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 }
1311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 mbedtls_pem_free(&pem);
1313 return ret;
1314 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1315 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001319 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001321 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 } else {
1323 ret = mbedtls_pem_read_buffer(&pem,
1324 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1325 "-----END ENCRYPTED PRIVATE KEY-----",
1326 key, NULL, 0, &len);
1327 }
1328 if (ret == 0) {
1329 if ((ret = pk_parse_key_pkcs8_encrypted_der(pk,
1330 pem.buf, pem.buflen,
1331 pwd, pwdlen)) != 0) {
1332 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 }
1334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 mbedtls_pem_free(&pem);
1336 return ret;
1337 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1338 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341#else
1342 ((void) pwd);
1343 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
1346 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001347 * At this point we only know it's not a PEM formatted key. Could be any
1348 * of the known DER encoded private key formats
1349 *
1350 * We try the different DER format parsers to see if one passes without
1351 * error
1352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001355 unsigned char *key_copy;
1356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1358 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1359 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1364 pwd, pwdlen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 mbedtls_platform_zeroize(key_copy, keylen);
1367 mbedtls_free(key_copy);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368 }
1369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370 if (ret == 0) {
1371 return 0;
1372 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 mbedtls_pk_free(pk);
1375 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1378 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen);
1383 if (ret == 0) {
1384 return 0;
Kenneth Soerensenc4e950e2019-01-03 12:39:29 +01001385 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 mbedtls_pk_free(pk);
1388 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1393 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1394 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1395 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 }
1397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 mbedtls_pk_free(pk);
1399 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1404 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1405 pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
1406 key, keylen) == 0) {
1407 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 mbedtls_pk_free(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411
Hanno Becker780f0a42018-10-10 11:23:33 +01001412 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1413 * it is ok to leave the PK context initialized but not
1414 * freed: It is the caller's responsibility to call pk_init()
1415 * before calling this function, and to call pk_free()
1416 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1417 * isn't, this leads to mbedtls_pk_free() being called
1418 * twice, once here and once by the caller, but this is
1419 * also ok and in line with the mbedtls_pk_free() calls
1420 * on failed PEM parsing attempts. */
1421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423}
1424
1425/*
1426 * Parse a public key
1427 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1429 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430{
Janos Follath24eed8d2019-11-22 13:21:35 +00001431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001433#if defined(MBEDTLS_RSA_C)
1434 const mbedtls_pk_info_t *pk_info;
1435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001439#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 PK_VALIDATE_RET(ctx != NULL);
1442 if (keylen == 0) {
1443 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1444 }
1445 PK_VALIDATE_RET(key != NULL || keylen == 0);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001446
1447#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001449#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 } else {
1454 ret = mbedtls_pem_read_buffer(&pem,
1455 "-----BEGIN RSA PUBLIC KEY-----",
1456 "-----END RSA PUBLIC KEY-----",
1457 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001458 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459
1460 if (ret == 0) {
1461 p = pem.buf;
1462 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1463 mbedtls_pem_free(&pem);
1464 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1465 }
1466
1467 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1468 mbedtls_pem_free(&pem);
1469 return ret;
1470 }
1471
1472 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1473 mbedtls_pk_free(ctx);
1474 }
1475
1476 mbedtls_pem_free(&pem);
1477 return ret;
1478 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1479 mbedtls_pem_free(&pem);
1480 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001481 }
1482#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483
1484 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001486 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 } else {
1488 ret = mbedtls_pem_read_buffer(&pem,
1489 "-----BEGIN PUBLIC KEY-----",
1490 "-----END PUBLIC KEY-----",
1491 key, NULL, 0, &len);
1492 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495 /*
1496 * Was PEM encoded
1497 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001498 p = pem.buf;
1499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
1501 mbedtls_pem_free(&pem);
1502 return ret;
1503 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1504 mbedtls_pem_free(&pem);
1505 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001509
1510#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001511 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1512 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001513 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001514
1515 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1516 return ret;
1517 }
1518
1519 p = (unsigned char *) key;
1520 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1521 if (ret == 0) {
1522 return ret;
1523 }
1524 mbedtls_pk_free(ctx);
1525 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1526 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1527 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001528 }
1529#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 p = (unsigned char *) key;
1531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_PK_PARSE_C */