blob: 98094a36fef8d03ca33f2779f7146d3d9f4c24cd [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"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020058#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
59
Valerio Settie0e63112023-05-18 18:48:07 +020060/* Helper for Montgomery curves */
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020061#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020062#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
63 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020064#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +020067 *
68 * ECParameters ::= CHOICE {
69 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010070 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 * }
73 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
75 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +020076{
Janos Follath24eed8d2019-11-22 13:21:35 +000077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (end - *p < 1) {
80 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
81 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
82 }
Sanne Woudab2b29d52017-08-21 15:58:12 +010083
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010084 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +020085 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +010088 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +010089#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010090 ) {
91 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
92 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010093 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
96 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010097 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020098
99 params->p = *p;
100 *p += params->len;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (*p != end) {
103 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
104 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
105 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108}
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100112 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
113 * WARNING: the resulting group should only be used with
114 * pk_group_id_from_specified(), since its base point may not be set correctly
115 * if it was encoded compressed.
116 *
117 * SpecifiedECDomain ::= SEQUENCE {
118 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
119 * fieldID FieldID {{FieldTypes}},
120 * curve Curve,
121 * base ECPoint,
122 * order INTEGER,
123 * cofactor INTEGER OPTIONAL,
124 * hash HashAlgorithm OPTIONAL,
125 * ...
126 * }
127 *
128 * We only support prime-field as field type, and ignore hash and cofactor.
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100131{
Janos Follath24eed8d2019-11-22 13:21:35 +0000132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100133 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200134 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100135 const unsigned char *end_field, *end_curve;
136 size_t len;
137 int ver;
138
139 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
141 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
142 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (ver < 1 || ver > 3) {
145 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
146 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100147
148 /*
149 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
150 * fieldType FIELD-ID.&id({IOSet}),
151 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
152 * }
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
155 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
156 return ret;
157 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100158
159 end_field = p + len;
160
161 /*
162 * FIELD-ID ::= TYPE-IDENTIFIER
163 * FieldTypes FIELD-ID ::= {
164 * { Prime-p IDENTIFIED BY prime-field } |
165 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
166 * }
167 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
168 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
170 return ret;
171 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
174 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
175 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100176 }
177
178 p += len;
179
180 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
183 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (p != end_field) {
188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
189 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
190 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100191
192 /*
193 * Curve ::= SEQUENCE {
194 * a FieldElement,
195 * b FieldElement,
196 * seed BIT STRING OPTIONAL
197 * -- Shall be present if used in SpecifiedECDomain
198 * -- with version equal to ecdpVer2 or ecdpVer3
199 * }
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
203 return ret;
204 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205
206 end_curve = p + len;
207
208 /*
209 * FieldElement ::= OCTET STRING
210 * containing an integer in the case of a prime field
211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
213 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100215 }
216
217 p += len;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
220 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100222 }
223
224 p += len;
225
226 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (p != end_curve) {
232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
233 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
234 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100235
236 /*
237 * ECPoint ::= OCTET STRING
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
241 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
244 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100245 /*
246 * If we can't read the point because it's compressed, cheat by
247 * reading only the X coordinate and the parity bit of Y.
248 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
250 (p[0] != 0x02 && p[0] != 0x03) ||
251 len != mbedtls_mpi_size(&grp->P) + 1 ||
252 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
253 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
254 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
255 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100256 }
257 }
258
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259 p += len;
260
261 /*
262 * order INTEGER
263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
265 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
266 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100269
270 /*
271 * Allow optional elements by purposefully not enforcing p == end here.
272 */
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100275}
276
277/*
278 * Find the group id associated with an (almost filled) group as generated by
279 * pk_group_from_specified(), or return an error if unknown.
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281static 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 +0100282{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100283 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_ecp_group ref;
285 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100290 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_ecp_group_free(&ref);
292 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
294 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
296 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
297 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
298 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
299 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
300 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
301 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100302 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 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 +0100304 break;
305 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306 }
307
308cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100310
311 *grp_id = *id;
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100318}
319
320/*
321 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
324 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325{
Janos Follath24eed8d2019-11-22 13:21:35 +0000326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000338 /* The API respecting lifecycle for mbedtls_ecp_group struct is
339 * _init(), _load() and _free(). In pk_group_id_from_specified() the
340 * temporary grp breaks that flow and it's members are populated
341 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
342 * which is assuming a group populated by _setup() may not clean-up
343 * properly -> Manually free it's members.
344 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000345 mbedtls_mpi_free(&grp.N);
346 mbedtls_mpi_free(&grp.P);
347 mbedtls_mpi_free(&grp.A);
348 mbedtls_mpi_free(&grp.B);
349 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200355/*
356 * Set the group used by this key.
357 */
358static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200359{
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200360#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
361 size_t ec_bits;
362 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200363
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200364 /* group may already be initialized; if so, make sure IDs match */
365 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
366 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200367 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
368 }
369
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200370 /* set group */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200371 pk->ec_family = ec_family;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200372 pk->ec_bits = ec_bits;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200373
374 return 0;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200375#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
376 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
377
378 /* grp may already be initialized; if so, make sure IDs match */
379 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
380 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
381 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
382 }
383
384 /* set group */
385 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200386#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200387}
Valerio Setti4064dbb2023-05-17 15:33:07 +0200388
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389/*
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200390 * Set the private key material
391 *
392 * Must have already set the group with pk_ecc_set_group().
393 *
394 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
395 */
396static int pk_ecc_set_key(mbedtls_pk_context *pk,
397 unsigned char *key, size_t len)
398{
399#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
401 psa_status_t status;
402
403 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
404 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
405 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200406 /* Montgomery allows only ECDH, others ECDSA too */
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200407 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
408 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200409 psa_set_key_enrollment_algorithm(&attributes,
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200410 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200411 }
412 psa_set_key_usage_flags(&attributes, flags);
413
414 status = psa_import_key(&attributes, key, len, &pk->priv_id);
415 return psa_pk_status_to_mbedtls(status);
416
417#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
418
419 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
420 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
421 if (ret != 0) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
423 }
424 return 0;
425#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
426}
427
428/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200429 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430 *
431 * ECParameters ::= CHOICE {
432 * namedCurve OBJECT IDENTIFIER
433 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
434 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200435 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200436static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (params->tag == MBEDTLS_ASN1_OID) {
442 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
443 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
444 }
445 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
448 return ret;
449 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100450#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100452#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200454
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200455 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200456}
457
Jethro Beekman01672442023-04-19 14:08:14 +0200458/*
459 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200460 *
461 * Note: the private key information is always available from pk,
462 * however for convenience the serialized version is also passed,
463 * as it's available at each calling site, and useful in some configs
464 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200465 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200466static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200467 const unsigned char *d, size_t d_len,
468 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
469{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200470#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200471 psa_status_t status;
472 (void) f_rng;
473 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200474 (void) d;
475 (void) d_len;
476
477 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
478 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200479 return psa_pk_status_to_mbedtls(status);
480#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
481 int ret;
482 psa_status_t status;
483 (void) f_rng;
484 (void) p_rng;
485
Valerio Setti00e8dd12023-05-18 18:56:59 +0200486 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
487 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
488 size_t key_len;
489 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200490 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
491 size_t curve_bits;
492 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200493 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200494
495 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
496 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
497
498 status = psa_import_key(&key_attr, d, d_len, &key_id);
499 ret = psa_pk_status_to_mbedtls(status);
500 if (ret != 0) {
501 return ret;
502 }
503
Jethro Beekman01672442023-04-19 14:08:14 +0200504 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
505 ret = psa_pk_status_to_mbedtls(status);
506 destruction_status = psa_destroy_key(key_id);
507 if (ret != 0) {
508 return ret;
509 } else if (destruction_status != PSA_SUCCESS) {
510 return psa_pk_status_to_mbedtls(destruction_status);
511 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200512 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200513#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200514 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200515 (void) d;
516 (void) d_len;
517
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200518 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200519#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200520}
521
522#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
523
524/*
525 * Load an RFC8410 EC key, which doesn't have any parameters
526 */
527static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
528 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200529 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200530{
531 if (params->tag != 0 || params->len != 0) {
532 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
533 }
534
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200535 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200536}
537
538/*
539 * Parse an RFC 8410 encoded private EC key
540 *
541 * CurvePrivateKey ::= OCTET STRING
542 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200543static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200544 unsigned char *key, size_t keylen, const unsigned char *end,
545 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
546{
547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
548 size_t len;
549
550 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
551 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
552 }
553
554 if (key + len != end) {
555 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
556 }
557
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200558 /*
559 * Load the private key
560 */
561 ret = pk_ecc_set_key(pk, key, len);
562 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200563 return ret;
564 }
Jethro Beekman01672442023-04-19 14:08:14 +0200565
Valerio Setti4064dbb2023-05-17 15:33:07 +0200566 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
567 * which never contain a public key. As such, derive the public key
568 * unconditionally. */
569 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200570 return ret;
571 }
572
Jethro Beekman01672442023-04-19 14:08:14 +0200573 return 0;
574}
575#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200576
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200577#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200578/*
579 * Create a temporary ecp_keypair for converting an EC point in compressed
580 * format to an uncompressed one
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200581 *
582 * Consumes everything or fails - inherited from
583 * mbedtls_ecp_point_read_binary().
Valerio Setti4064dbb2023-05-17 15:33:07 +0200584 */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200585static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
586 const unsigned char *in_start, size_t in_len,
587 unsigned char *out_buf, size_t out_buf_size,
588 size_t *out_buf_len)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200589{
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200590#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200591 mbedtls_ecp_keypair ecp_key;
592 mbedtls_ecp_group_id ecp_group_id;
593 int ret;
594
595 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
596
597 mbedtls_ecp_keypair_init(&ecp_key);
598 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
599 if (ret != 0) {
600 return ret;
601 }
602 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
603 in_start, in_len);
604 if (ret != 0) {
605 goto exit;
606 }
607 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
608 MBEDTLS_ECP_PF_UNCOMPRESSED,
609 out_buf_len, out_buf, out_buf_size);
610
611exit:
612 mbedtls_ecp_keypair_free(&ecp_key);
613 return ret;
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200614#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
615 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
616#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200617}
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200618#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200619
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620/*
621 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100622 *
623 * The caller is responsible for clearing the structure upon failure if
624 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200626 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200628 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629{
Janos Follath24eed8d2019-11-22 13:21:35 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631
Valerio Setti4064dbb2023-05-17 15:33:07 +0200632#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
633 mbedtls_svc_key_id_t key;
634 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200635 size_t len = (size_t) (end - *p);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200636
637 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
638 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639 }
640
Valerio Setti4064dbb2023-05-17 15:33:07 +0200641 if ((**p == 0x02) || (**p == 0x03)) {
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200642 /* Compressed format, not supported by PSA Crypto.
643 * Try converting using functions from ECP_LIGHT. */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200644 ret = pk_ecc_read_compressed(pk, *p, len,
645 pk->pub_raw,
646 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
647 &pk->pub_raw_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200648 if (ret != 0) {
649 return ret;
650 }
651 } else {
652 /* Uncompressed format */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200653 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200654 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200655 }
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200656 memcpy(pk->pub_raw, *p, len);
657 pk->pub_raw_len = len;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200658 }
659
660 /* Validate the key by trying to importing it */
661 psa_set_key_usage_flags(&key_attrs, 0);
662 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
663 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
664 psa_set_key_bits(&key_attrs, pk->ec_bits);
665
666 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
667 &key) != PSA_SUCCESS) ||
668 (psa_destroy_key(key) != PSA_SUCCESS)) {
669 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
670 pk->pub_raw_len = 0;
671 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
672 }
673 ret = 0;
674#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
675 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
676 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
677 (const unsigned char *) *p,
678 end - *p)) == 0) {
679 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
680 }
681#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
682
Paul Bakker1a7550a2013-09-15 13:01:22 +0200683 /*
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200684 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
685 * consumed all bytes or failed, and memcpy consumed all bytes too.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686 */
687 *p = (unsigned char *) end;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690}
Valerio Setti81d75122023-06-14 14:49:33 +0200691#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694/*
695 * RSAPublicKey ::= SEQUENCE {
696 * modulus INTEGER, -- n
697 * publicExponent INTEGER -- e
698 * }
699 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static int pk_get_rsapubkey(unsigned char **p,
701 const unsigned char *end,
702 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703{
Janos Follath24eed8d2019-11-22 13:21:35 +0000704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200705 size_t len;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
708 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
709 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
710 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (*p + len != end) {
713 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
714 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
715 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100717 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
719 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
720 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
723 NULL, 0, NULL, 0)) != 0) {
724 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
725 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100726
727 *p += len;
728
729 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
731 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
732 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
735 NULL, 0, *p, len)) != 0) {
736 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
737 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100738
739 *p += len;
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (mbedtls_rsa_complete(rsa) != 0 ||
742 mbedtls_rsa_check_pubkey(rsa) != 0) {
743 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000744 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (*p != end) {
747 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
749 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754
755/* Get a PK algorithm identifier
756 *
757 * AlgorithmIdentifier ::= SEQUENCE {
758 * algorithm OBJECT IDENTIFIER,
759 * parameters ANY DEFINED BY algorithm OPTIONAL }
760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int pk_get_pk_alg(unsigned char **p,
762 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200763 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
764 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765{
Janos Follath24eed8d2019-11-22 13:21:35 +0000766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
772 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
773 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774
Jethro Beekman01672442023-04-19 14:08:14 +0200775 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200776#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200777 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
778 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
779 if (ret == 0) {
780 *pk_alg = MBEDTLS_PK_ECKEY;
781 }
782 }
783#else
784 (void) ec_grp_id;
785#endif
786 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
788 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
790 /*
791 * No parameters with RSA (only for EC)
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (*pk_alg == MBEDTLS_PK_RSA &&
794 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
795 params->len != 0)) {
796 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800}
801
802/*
803 * SubjectPublicKeyInfo ::= SEQUENCE {
804 * algorithm AlgorithmIdentifier,
805 * subjectPublicKey BIT STRING }
806 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
808 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200809{
Janos Follath24eed8d2019-11-22 13:21:35 +0000810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_asn1_buf alg_params;
813 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200814 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
818 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
819 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820 }
821
822 end = *p + len;
823
Jethro Beekman01672442023-04-19 14:08:14 +0200824 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return ret;
826 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
829 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
830 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (*p + len != end) {
833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
834 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
835 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
838 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
839 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
842 return ret;
843 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (pk_alg == MBEDTLS_PK_RSA) {
847 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200850#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200852#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200853 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200854 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200855 } else
856#endif
857 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200858 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200859 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200861 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200864#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (ret == 0 && *p != end) {
868 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
869 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
870 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (ret != 0) {
873 mbedtls_pk_free(pk);
874 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877}
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100881 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
882 *
883 * The value zero is:
884 * - never a valid value for an RSA parameter
885 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
886 *
887 * Since values can't be omitted in PKCS#1, passing a zero value to
888 * rsa_complete() would be incorrect, so reject zero values early.
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890static int asn1_get_nonzero_mpi(unsigned char **p,
891 const unsigned char *end,
892 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100893{
894 int ret;
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 ret = mbedtls_asn1_get_mpi(p, end, X);
897 if (ret != 0) {
898 return ret;
899 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
902 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
903 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100906}
907
908/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 * Parse a PKCS#1 encoded private RSA key
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
912 const unsigned char *key,
913 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100915 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916 size_t len;
917 unsigned char *p, *end;
918
Hanno Beckerefa14e82017-10-11 19:45:19 +0100919 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100921
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922 p = (unsigned char *) key;
923 end = p + keylen;
924
925 /*
926 * This function parses the RSAPrivateKey (PKCS#1)
927 *
928 * RSAPrivateKey ::= SEQUENCE {
929 * version Version,
930 * modulus INTEGER, -- n
931 * publicExponent INTEGER, -- e
932 * privateExponent INTEGER, -- d
933 * prime1 INTEGER, -- p
934 * prime2 INTEGER, -- q
935 * exponent1 INTEGER, -- d mod (p-1)
936 * exponent2 INTEGER, -- d mod (q-1)
937 * coefficient INTEGER, -- (inverse of q) mod p
938 * otherPrimeInfos OtherPrimeInfos OPTIONAL
939 * }
940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
942 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
943 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 }
945
946 end = p + len;
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
949 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (version != 0) {
953 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 }
955
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100956 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
958 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
959 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100960 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100963 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
965 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
966 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100967 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100969
970 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
972 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
973 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100974 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100976
977 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
979 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
980 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100981 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100983
984 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
986 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
987 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100988 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100990
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100991#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500992 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
994 * that they can be easily recomputed from D, P and Q. However by
995 * parsing them from the PKCS1 structure it is possible to avoid
996 * recalculating them which both reduces the overhead of loading
997 * RSA private keys into memory and also avoids side channels which
998 * can arise when computing those values, since all of D, P, and Q
999 * are secret. See https://eprint.iacr.org/2020/055 for a
1000 * description of one such attack.
1001 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001002
Jack Lloyd80cc8112020-01-22 17:34:29 -05001003 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1005 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1006 goto cleanup;
1007 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001008
1009 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1011 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1012 goto cleanup;
1013 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001014
1015 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1017 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1018 goto cleanup;
1019 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001020
Jack Lloyd60239752020-01-27 17:53:36 -05001021#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001022 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1024 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1025 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1026 goto cleanup;
1027 }
Jack Lloyd60239752020-01-27 17:53:36 -05001028#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001029
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001030 /* rsa_complete() doesn't complete anything with the default
1031 * implementation but is still called:
1032 * - for the benefit of alternative implementation that may want to
1033 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1034 * - as is also sanity-checks the key
1035 *
1036 * Furthermore, we also check the public part for consistency with
1037 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1040 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001042 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (p != end) {
1045 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1046 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 }
1048
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001049cleanup:
1050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001054 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((ret & 0xff80) == 0) {
1056 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1057 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001058 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 }
1063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067
Valerio Setti81d75122023-06-14 14:49:33 +02001068#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069/*
1070 * Parse a SEC1 encoded private EC key
1071 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001072static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 const unsigned char *key, size_t keylen,
1074 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075{
Janos Follath24eed8d2019-11-22 13:21:35 +00001076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001077 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001078 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001079 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001081 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082 unsigned char *end = p + keylen;
1083 unsigned char *end2;
1084
1085 /*
1086 * RFC 5915, or SEC1 Appendix C.4
1087 *
1088 * ECPrivateKey ::= SEQUENCE {
1089 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1090 * privateKey OCTET STRING,
1091 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1092 * publicKey [1] BIT STRING OPTIONAL
1093 * }
1094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1096 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1097 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098 }
1099
1100 end = p + len;
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1103 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1104 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (version != 1) {
1107 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1108 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1111 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1112 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113
Valerio Setti6b062ee2023-06-30 17:32:57 +02001114 /* Keep a reference to the position fo the private key. It will be used
1115 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001116 d = p;
1117 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001118
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 p += len;
1120
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001121 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001123 /*
1124 * Is 'parameters' present?
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1127 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1128 0)) == 0) {
1129 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001130 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001135 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001136 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001137
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001138 /*
1139 * Load the private key
1140 */
1141 ret = pk_ecc_set_key(pk, d, d_len);
1142 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001143 return ret;
1144 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001147 /*
1148 * Is 'publickey' present? If not, or if we can't read it (eg because it
1149 * is compressed), create it from the private key.
1150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1152 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1153 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001154 end2 = p + len;
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1157 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1158 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (p + len != end2) {
1161 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1162 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1163 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001164
Valerio Setti4064dbb2023-05-17 15:33:07 +02001165 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001166 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001168 /*
1169 * The only acceptable failure mode of pk_get_ecpubkey() above
1170 * is if the point format is not recognized.
1171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1173 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1174 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001178 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001180
Valerio Setti34f67552023-04-03 15:19:18 +02001181 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001182 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001183 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001184 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001185 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188}
Valerio Setti81d75122023-06-14 14:49:33 +02001189#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001191/***********************************************************************
1192 *
1193 * PKCS#8 parsing functions
1194 *
1195 **********************************************************************/
1196
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197/*
1198 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001199 *
1200 * Notes:
1201 *
1202 * - This function does not own the key buffer. It is the
1203 * responsibility of the caller to take care of zeroizing
1204 * and freeing it after use.
1205 *
1206 * - The function is responsible for freeing the provided
1207 * PK context on failure.
1208 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 */
1210static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 mbedtls_pk_context *pk,
1212 const unsigned char *key, size_t keylen,
1213 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214{
1215 int ret, version;
1216 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 unsigned char *p = (unsigned char *) key;
1219 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001221 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223
Valerio Setti81d75122023-06-14 14:49:33 +02001224#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001225 (void) f_rng;
1226 (void) p_rng;
1227#endif
1228
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001230 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231 *
1232 * PrivateKeyInfo ::= SEQUENCE {
1233 * version Version,
1234 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1235 * privateKey PrivateKey,
1236 * attributes [0] IMPLICIT Attributes OPTIONAL }
1237 *
1238 * Version ::= INTEGER
1239 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1240 * PrivateKey ::= OCTET STRING
1241 *
1242 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1243 */
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1246 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248 }
1249
1250 end = p + len;
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001254 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (version != 0) {
1257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1258 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
Jethro Beekman01672442023-04-19 14:08:14 +02001260 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return ret;
1262 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1265 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1266 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (len < 1) {
1269 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1270 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1271 }
1272
1273 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1274 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1275 }
1276
1277 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1278 return ret;
1279 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (pk_alg == MBEDTLS_PK_RSA) {
1283 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1284 mbedtls_pk_free(pk);
1285 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 }
1287 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001289#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001291#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001292 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001293 if ((ret =
1294 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001295 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001296 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001297 p_rng)) != 0) {
1298 mbedtls_pk_free(pk);
1299 return ret;
1300 }
1301 } else
1302#endif
1303 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001304 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1305 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001306 mbedtls_pk_free(pk);
1307 return ret;
1308 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 }
1310 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001311#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001314 end = p + len;
1315 if (end != (key + keylen)) {
1316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1317 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1318 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321}
1322
1323/*
1324 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001325 *
1326 * To save space, the decryption happens in-place on the given key buffer.
1327 * Also, while this function may modify the keybuffer, it doesn't own it,
1328 * and instead it is the responsibility of the caller to zeroize and properly
1329 * free it after use.
1330 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001333MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 mbedtls_pk_context *pk,
1335 unsigned char *key, size_t keylen,
1336 const unsigned char *pwd, size_t pwdlen,
1337 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001339 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001341 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1344#if defined(MBEDTLS_PKCS12_C)
1345 mbedtls_cipher_type_t cipher_alg;
1346 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001348 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Hanno Becker2aa80a72017-09-07 15:28:45 +01001350 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 end = p + keylen;
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (pwdlen == 0) {
1354 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1355 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
1357 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001358 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 *
1360 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1361 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1362 * encryptedData EncryptedData
1363 * }
1364 *
1365 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1366 *
1367 * EncryptedData ::= OCTET STRING
1368 *
1369 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001370 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1373 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1374 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 }
1376
1377 end = p + len;
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1380 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1381 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1385 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386
Hanno Beckerfab35692017-08-25 13:38:26 +01001387 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388
1389 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001390 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001394 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001395 cipher_alg, md_alg,
1396 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1398 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1399 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001403
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001404 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#endif /* MBEDTLS_PKCS12_C */
1407#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001409 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1410 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1412 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1413 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001417
1418 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001421 {
1422 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (decrypted == 0) {
1426 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1427 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001428 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001432/***********************************************************************
1433 *
1434 * Top-level functions, with format auto-discovery
1435 *
1436 **********************************************************************/
1437
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438/*
1439 * Parse a private key
1440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1442 const unsigned char *key, size_t keylen,
1443 const unsigned char *pwd, size_t pwdlen,
1444 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445{
Janos Follath24eed8d2019-11-22 13:21:35 +00001446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001451#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (keylen == 0) {
1454 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1455 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001456
1457#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001461 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001463 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 } else {
1465 ret = mbedtls_pem_read_buffer(&pem,
1466 "-----BEGIN RSA PRIVATE KEY-----",
1467 "-----END RSA PRIVATE KEY-----",
1468 key, pwd, pwdlen, &len);
1469 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ret == 0) {
1472 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1473 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1474 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1475 pem.buf, pem.buflen)) != 0) {
1476 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 }
1478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 mbedtls_pem_free(&pem);
1480 return ret;
1481 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1482 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1483 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1484 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1485 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1486 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489
Valerio Setti81d75122023-06-14 14:49:33 +02001490#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001491 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001493 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 } else {
1495 ret = mbedtls_pem_read_buffer(&pem,
1496 "-----BEGIN EC PRIVATE KEY-----",
1497 "-----END EC PRIVATE KEY-----",
1498 key, pwd, pwdlen, &len);
1499 }
1500 if (ret == 0) {
1501 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001504 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 pem.buf, pem.buflen,
1506 f_rng, p_rng)) != 0) {
1507 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001508 }
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 mbedtls_pem_free(&pem);
1511 return ret;
1512 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1513 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1514 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1515 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1516 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1517 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518 }
Valerio Setti81d75122023-06-14 14:49:33 +02001519#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001521 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001523 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else {
1525 ret = mbedtls_pem_read_buffer(&pem,
1526 "-----BEGIN PRIVATE KEY-----",
1527 "-----END PRIVATE KEY-----",
1528 key, NULL, 0, &len);
1529 }
1530 if (ret == 0) {
1531 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1532 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1533 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534 }
1535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_pem_free(&pem);
1537 return ret;
1538 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1539 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001543 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 } else {
1547 ret = mbedtls_pem_read_buffer(&pem,
1548 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1549 "-----END ENCRYPTED PRIVATE KEY-----",
1550 key, NULL, 0, &len);
1551 }
1552 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001553 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1554 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556 }
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 mbedtls_pem_free(&pem);
1559 return ret;
1560 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1561 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001564#else
1565 ((void) pwd);
1566 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001568
1569 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001570 * At this point we only know it's not a PEM formatted key. Could be any
1571 * of the known DER encoded private key formats
1572 *
1573 * We try the different DER format parsers to see if one passes without
1574 * error
1575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001578 unsigned char *key_copy;
1579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1581 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1582 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001585
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001586 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1587 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001588
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001589 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001590 }
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (ret == 0) {
1593 return 0;
1594 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 mbedtls_pk_free(pk);
1597 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1600 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1605 if (ret == 0) {
1606 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001607 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 mbedtls_pk_free(pk);
1610 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1615 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1616 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1617 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 mbedtls_pk_free(pk);
1621 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001623
Valerio Setti81d75122023-06-14 14:49:33 +02001624#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1626 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001627 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 key, keylen, f_rng, p_rng) == 0) {
1629 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001630 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001632#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001633
Valerio Setti81d75122023-06-14 14:49:33 +02001634 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001635 * it is ok to leave the PK context initialized but not
1636 * freed: It is the caller's responsibility to call pk_init()
1637 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001638 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001639 * isn't, this leads to mbedtls_pk_free() being called
1640 * twice, once here and once by the caller, but this is
1641 * also ok and in line with the mbedtls_pk_free() calls
1642 * on failed PEM parsing attempts. */
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645}
1646
1647/*
1648 * Parse a public key
1649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1651 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001652{
Janos Follath24eed8d2019-11-22 13:21:35 +00001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001655#if defined(MBEDTLS_RSA_C)
1656 const mbedtls_pk_info_t *pk_info;
1657#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001659 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001661#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (keylen == 0) {
1664 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1665 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001666
1667#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001669#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001670 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001672 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 } else {
1674 ret = mbedtls_pem_read_buffer(&pem,
1675 "-----BEGIN RSA PUBLIC KEY-----",
1676 "-----END RSA PUBLIC KEY-----",
1677 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001679
1680 if (ret == 0) {
1681 p = pem.buf;
1682 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1683 mbedtls_pem_free(&pem);
1684 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1685 }
1686
1687 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1688 mbedtls_pem_free(&pem);
1689 return ret;
1690 }
1691
1692 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1693 mbedtls_pk_free(ctx);
1694 }
1695
1696 mbedtls_pem_free(&pem);
1697 return ret;
1698 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1699 mbedtls_pem_free(&pem);
1700 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001701 }
1702#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001703
1704 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001706 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 } else {
1708 ret = mbedtls_pem_read_buffer(&pem,
1709 "-----BEGIN PUBLIC KEY-----",
1710 "-----END PUBLIC KEY-----",
1711 key, NULL, 0, &len);
1712 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001715 /*
1716 * Was PEM encoded
1717 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001718 p = pem.buf;
1719
Jethro Beekman01672442023-04-19 14:08:14 +02001720 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 mbedtls_pem_free(&pem);
1722 return ret;
1723 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1724 mbedtls_pem_free(&pem);
1725 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001726 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001729
1730#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1732 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001733 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001734
1735 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1736 return ret;
1737 }
1738
1739 p = (unsigned char *) key;
1740 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1741 if (ret == 0) {
1742 return ret;
1743 }
1744 mbedtls_pk_free(ctx);
1745 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1746 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1747 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001748 }
1749#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001750 p = (unsigned char *) key;
1751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001755}
1756
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001757/***********************************************************************
1758 *
1759 * Top-level functions, with filesystem support
1760 *
1761 **********************************************************************/
1762
1763#if defined(MBEDTLS_FS_IO)
1764/*
1765 * Load all data from a file into a given buffer.
1766 *
1767 * The file is expected to contain either PEM or DER encoded data.
1768 * A terminating null byte is always appended. It is included in the announced
1769 * length only if the data looks like it is PEM encoded.
1770 */
1771int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1772{
1773 FILE *f;
1774 long size;
1775
1776 if ((f = fopen(path, "rb")) == NULL) {
1777 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1778 }
1779
1780 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1781 mbedtls_setbuf(f, NULL);
1782
1783 fseek(f, 0, SEEK_END);
1784 if ((size = ftell(f)) == -1) {
1785 fclose(f);
1786 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1787 }
1788 fseek(f, 0, SEEK_SET);
1789
1790 *n = (size_t) size;
1791
1792 if (*n + 1 == 0 ||
1793 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1794 fclose(f);
1795 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1796 }
1797
1798 if (fread(*buf, 1, *n, f) != *n) {
1799 fclose(f);
1800
1801 mbedtls_zeroize_and_free(*buf, *n);
1802
1803 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1804 }
1805
1806 fclose(f);
1807
1808 (*buf)[*n] = '\0';
1809
1810 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1811 ++*n;
1812 }
1813
1814 return 0;
1815}
1816
1817/*
1818 * Load and parse a private key
1819 */
1820int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1821 const char *path, const char *pwd,
1822 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1823{
1824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1825 size_t n;
1826 unsigned char *buf;
1827
1828 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1829 return ret;
1830 }
1831
1832 if (pwd == NULL) {
1833 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1834 } else {
1835 ret = mbedtls_pk_parse_key(ctx, buf, n,
1836 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1837 }
1838
1839 mbedtls_zeroize_and_free(buf, n);
1840
1841 return ret;
1842}
1843
1844/*
1845 * Load and parse a public key
1846 */
1847int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1848{
1849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1850 size_t n;
1851 unsigned char *buf;
1852
1853 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1854 return ret;
1855 }
1856
1857 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1858
1859 mbedtls_zeroize_and_free(buf, n);
1860
1861 return ret;
1862}
1863#endif /* MBEDTLS_FS_IO */
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#endif /* MBEDTLS_PK_PARSE_C */