blob: fbf8cbfc11508d96c940d2a6a16226b514a56317 [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
Valerio Settie0e63112023-05-18 18:48:07 +020058/* Helper for Montgomery curves */
Valerio Setti81d75122023-06-14 14:49:33 +020059#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Settie0e63112023-05-18 18:48:07 +020060#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
61 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
Valerio Setti81d75122023-06-14 14:49:33 +020062#endif /* MBEDTLS_PK_HAVE_ECC_KEYS && MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Settie0e63112023-05-18 18:48:07 +020063
Valerio Setti81d75122023-06-14 14:49:33 +020064#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 *
67 * ECParameters ::= CHOICE {
68 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010069 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +020070 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 * }
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
74 mbedtls_asn1_buf *params)
Paul Bakker1a7550a2013-09-15 13:01:22 +020075{
Janos Follath24eed8d2019-11-22 13:21:35 +000076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (end - *p < 1) {
79 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
80 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
81 }
Sanne Woudab2b29d52017-08-21 15:58:12 +010082
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010083 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 params->tag = **p;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (params->tag != MBEDTLS_ASN1_OID
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +010087 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +010088#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010089 ) {
90 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
91 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010092 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
95 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +010096 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020097
98 params->p = *p;
99 *p += params->len;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (*p != end) {
102 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
103 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
104 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100111 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
112 * WARNING: the resulting group should only be used with
113 * pk_group_id_from_specified(), since its base point may not be set correctly
114 * if it was encoded compressed.
115 *
116 * SpecifiedECDomain ::= SEQUENCE {
117 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
118 * fieldID FieldID {{FieldTypes}},
119 * curve Curve,
120 * base ECPoint,
121 * order INTEGER,
122 * cofactor INTEGER OPTIONAL,
123 * hash HashAlgorithm OPTIONAL,
124 * ...
125 * }
126 *
127 * We only support prime-field as field type, and ignore hash and cofactor.
128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100130{
Janos Follath24eed8d2019-11-22 13:21:35 +0000131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100132 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200133 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100134 const unsigned char *end_field, *end_curve;
135 size_t len;
136 int ver;
137
138 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
140 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
141 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ver < 1 || ver > 3) {
144 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
145 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100146
147 /*
148 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
149 * fieldType FIELD-ID.&id({IOSet}),
150 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
151 * }
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
154 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
155 return ret;
156 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100157
158 end_field = p + len;
159
160 /*
161 * FIELD-ID ::= TYPE-IDENTIFIER
162 * FieldTypes FIELD-ID ::= {
163 * { Prime-p IDENTIFIED BY prime-field } |
164 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
165 * }
166 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
169 return ret;
170 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
173 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
174 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100175 }
176
177 p += len;
178
179 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
182 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (p != end_field) {
187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
188 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
189 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190
191 /*
192 * Curve ::= SEQUENCE {
193 * a FieldElement,
194 * b FieldElement,
195 * seed BIT STRING OPTIONAL
196 * -- Shall be present if used in SpecifiedECDomain
197 * -- with version equal to ecdpVer2 or ecdpVer3
198 * }
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
201 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
202 return ret;
203 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100204
205 end_curve = p + len;
206
207 /*
208 * FieldElement ::= OCTET STRING
209 * containing an integer in the case of a prime field
210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
212 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
213 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100214 }
215
216 p += len;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
219 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100221 }
222
223 p += len;
224
225 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100227 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (p != end_curve) {
231 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
232 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
233 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234
235 /*
236 * ECPoint ::= OCTET STRING
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
240 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
243 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100244 /*
245 * If we can't read the point because it's compressed, cheat by
246 * reading only the X coordinate and the parity bit of Y.
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
249 (p[0] != 0x02 && p[0] != 0x03) ||
250 len != mbedtls_mpi_size(&grp->P) + 1 ||
251 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
252 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
253 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
254 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100255 }
256 }
257
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100258 p += len;
259
260 /*
261 * order INTEGER
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
265 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100268
269 /*
270 * Allow optional elements by purposefully not enforcing p == end here.
271 */
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274}
275
276/*
277 * Find the group id associated with an (almost filled) group as generated by
278 * pk_group_from_specified(), or return an error if unknown.
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280static 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 +0100281{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100282 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_ecp_group ref;
284 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_ecp_group_free(&ref);
291 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
295 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
296 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
297 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
298 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
299 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
300 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100301 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 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 +0100303 break;
304 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100305 }
306
307cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309
310 *grp_id = *id;
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317}
318
319/*
320 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
323 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324{
Janos Follath24eed8d2019-11-22 13:21:35 +0000325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100335
336cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000337 /* The API respecting lifecycle for mbedtls_ecp_group struct is
338 * _init(), _load() and _free(). In pk_group_id_from_specified() the
339 * temporary grp breaks that flow and it's members are populated
340 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
341 * which is assuming a group populated by _setup() may not clean-up
342 * properly -> Manually free it's members.
343 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000344 mbedtls_mpi_free(&grp.N);
345 mbedtls_mpi_free(&grp.P);
346 mbedtls_mpi_free(&grp.A);
347 mbedtls_mpi_free(&grp.B);
348 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100353
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200354/*
355 * Set the group used by this key.
356 */
357static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200358{
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200359#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
360 size_t ec_bits;
361 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200362
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200363 /* group may already be initialized; if so, make sure IDs match */
364 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
365 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200366 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
367 }
368
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200369 /* set group */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200370 pk->ec_family = ec_family;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200371 pk->ec_bits = ec_bits;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200372
373 return 0;
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200374#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
375 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
376
377 /* grp may already be initialized; if so, make sure IDs match */
378 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
379 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
380 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
381 }
382
383 /* set group */
384 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200385#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200386}
Valerio Setti4064dbb2023-05-17 15:33:07 +0200387
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388/*
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200389 * Set the private key material
390 *
391 * Must have already set the group with pk_ecc_set_group().
392 *
393 * The 'key' argument points to the raw private key (no ASN.1 wrapping).
394 */
395static int pk_ecc_set_key(mbedtls_pk_context *pk,
396 unsigned char *key, size_t len)
397{
398#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
400 psa_status_t status;
401
402 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
403 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
404 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200405 /* Montgomery allows only ECDH, others ECDSA too */
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200406 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
407 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200408 psa_set_key_enrollment_algorithm(&attributes,
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200409 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200410 }
411 psa_set_key_usage_flags(&attributes, flags);
412
413 status = psa_import_key(&attributes, key, len, &pk->priv_id);
414 return psa_pk_status_to_mbedtls(status);
415
416#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
417
418 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
419 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, len);
420 if (ret != 0) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
422 }
423 return 0;
424#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
425}
426
427/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200428 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429 *
430 * ECParameters ::= CHOICE {
431 * namedCurve OBJECT IDENTIFIER
432 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
433 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200434 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200435static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200436{
Janos Follath24eed8d2019-11-22 13:21:35 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (params->tag == MBEDTLS_ASN1_OID) {
441 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
442 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
443 }
444 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
447 return ret;
448 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100449#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100451#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200453
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200454 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200455}
456
Jethro Beekman01672442023-04-19 14:08:14 +0200457/*
458 * Helper function for deriving a public key from its private counterpart.
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200459 *
460 * Note: the private key information is always available from pk,
461 * however for convenience the serialized version is also passed,
462 * as it's available at each calling site, and useful in some configs
463 * (as otherwise we're have to re-serialize it from the pk context).
Jethro Beekman01672442023-04-19 14:08:14 +0200464 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200465static int pk_derive_public_key(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200466 const unsigned char *d, size_t d_len,
467 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
468{
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200469#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200470 psa_status_t status;
471 (void) f_rng;
472 (void) p_rng;
Valerio Setti00e8dd12023-05-18 18:56:59 +0200473 (void) d;
474 (void) d_len;
475
476 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
477 &pk->pub_raw_len);
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200478 return psa_pk_status_to_mbedtls(status);
479#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
480 int ret;
481 psa_status_t status;
482 (void) f_rng;
483 (void) p_rng;
484
Valerio Setti00e8dd12023-05-18 18:56:59 +0200485 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
486 unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
487 size_t key_len;
488 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Jethro Beekman01672442023-04-19 14:08:14 +0200489 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
490 size_t curve_bits;
491 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Valerio Setti00e8dd12023-05-18 18:56:59 +0200492 psa_status_t destruction_status;
Jethro Beekman01672442023-04-19 14:08:14 +0200493
494 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
495 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
496
497 status = psa_import_key(&key_attr, d, d_len, &key_id);
498 ret = psa_pk_status_to_mbedtls(status);
499 if (ret != 0) {
500 return ret;
501 }
502
Jethro Beekman01672442023-04-19 14:08:14 +0200503 status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len);
504 ret = psa_pk_status_to_mbedtls(status);
505 destruction_status = psa_destroy_key(key_id);
506 if (ret != 0) {
507 return ret;
508 } else if (destruction_status != PSA_SUCCESS) {
509 return psa_pk_status_to_mbedtls(destruction_status);
510 }
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200511 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
Jethro Beekman01672442023-04-19 14:08:14 +0200512#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti00e8dd12023-05-18 18:56:59 +0200513 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Jethro Beekman01672442023-04-19 14:08:14 +0200514 (void) d;
515 (void) d_len;
516
Manuel Pégourié-Gonnardd5b43722023-07-24 12:06:22 +0200517 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Jethro Beekman01672442023-04-19 14:08:14 +0200518#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jethro Beekman01672442023-04-19 14:08:14 +0200519}
520
521#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
522
523/*
524 * Load an RFC8410 EC key, which doesn't have any parameters
525 */
526static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
527 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200528 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200529{
530 if (params->tag != 0 || params->len != 0) {
531 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
532 }
533
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200534 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200535}
536
537/*
538 * Parse an RFC 8410 encoded private EC key
539 *
540 * CurvePrivateKey ::= OCTET STRING
541 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200542static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200543 unsigned char *key, size_t keylen, const unsigned char *end,
544 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
545{
546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
547 size_t len;
548
549 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
551 }
552
553 if (key + len != end) {
554 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
555 }
556
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200557 /*
558 * Load the private key
559 */
560 ret = pk_ecc_set_key(pk, key, len);
561 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200562 return ret;
563 }
Jethro Beekman01672442023-04-19 14:08:14 +0200564
Valerio Setti4064dbb2023-05-17 15:33:07 +0200565 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
566 * which never contain a public key. As such, derive the public key
567 * unconditionally. */
568 if ((ret = pk_derive_public_key(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200569 return ret;
570 }
571
Jethro Beekman01672442023-04-19 14:08:14 +0200572 return 0;
573}
574#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200575
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200576#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200577/*
578 * Create a temporary ecp_keypair for converting an EC point in compressed
579 * format to an uncompressed one
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200580 *
581 * Consumes everything or fails - inherited from
582 * mbedtls_ecp_point_read_binary().
Valerio Setti4064dbb2023-05-17 15:33:07 +0200583 */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200584static int pk_ecc_read_compressed(mbedtls_pk_context *pk,
585 const unsigned char *in_start, size_t in_len,
586 unsigned char *out_buf, size_t out_buf_size,
587 size_t *out_buf_len)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200588{
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200589#if defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Valerio Setti4064dbb2023-05-17 15:33:07 +0200590 mbedtls_ecp_keypair ecp_key;
591 mbedtls_ecp_group_id ecp_group_id;
592 int ret;
593
594 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
595
596 mbedtls_ecp_keypair_init(&ecp_key);
597 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
598 if (ret != 0) {
599 return ret;
600 }
601 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
602 in_start, in_len);
603 if (ret != 0) {
604 goto exit;
605 }
606 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
607 MBEDTLS_ECP_PF_UNCOMPRESSED,
608 out_buf_len, out_buf, out_buf_size);
609
610exit:
611 mbedtls_ecp_keypair_free(&ecp_key);
612 return ret;
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200613#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
614 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
615#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200616}
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200617#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jethro Beekman01672442023-04-19 14:08:14 +0200618
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619/*
620 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100621 *
622 * The caller is responsible for clearing the structure upon failure if
623 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200625 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200627 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628{
Janos Follath24eed8d2019-11-22 13:21:35 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630
Valerio Setti4064dbb2023-05-17 15:33:07 +0200631#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
632 mbedtls_svc_key_id_t key;
633 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200634 size_t len = (size_t) (end - *p);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200635
636 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
637 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638 }
639
Valerio Setti4064dbb2023-05-17 15:33:07 +0200640 if ((**p == 0x02) || (**p == 0x03)) {
Manuel Pégourié-Gonnarde82fcd92023-07-26 10:53:25 +0200641 /* Compressed format, not supported by PSA Crypto.
642 * Try converting using functions from ECP_LIGHT. */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200643 ret = pk_ecc_read_compressed(pk, *p, len,
644 pk->pub_raw,
645 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE,
646 &pk->pub_raw_len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200647 if (ret != 0) {
648 return ret;
649 }
650 } else {
651 /* Uncompressed format */
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200652 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
Valerio Setti016264b2023-05-22 18:40:35 +0200653 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200654 }
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200655 memcpy(pk->pub_raw, *p, len);
656 pk->pub_raw_len = len;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200657 }
658
659 /* Validate the key by trying to importing it */
660 psa_set_key_usage_flags(&key_attrs, 0);
661 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
662 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
663 psa_set_key_bits(&key_attrs, pk->ec_bits);
664
665 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
666 &key) != PSA_SUCCESS) ||
667 (psa_destroy_key(key) != PSA_SUCCESS)) {
668 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
669 pk->pub_raw_len = 0;
670 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
671 }
672 ret = 0;
673#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
674 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
675 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
676 (const unsigned char *) *p,
677 end - *p)) == 0) {
678 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
679 }
680#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
681
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 /*
Manuel Pégourié-Gonnarddf151bb2023-07-26 11:06:46 +0200683 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
684 * consumed all bytes or failed, and memcpy consumed all bytes too.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685 */
686 *p = (unsigned char *) end;
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689}
Valerio Setti81d75122023-06-14 14:49:33 +0200690#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693/*
694 * RSAPublicKey ::= SEQUENCE {
695 * modulus INTEGER, -- n
696 * publicExponent INTEGER -- e
697 * }
698 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int pk_get_rsapubkey(unsigned char **p,
700 const unsigned char *end,
701 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702{
Janos Follath24eed8d2019-11-22 13:21:35 +0000703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200704 size_t len;
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
707 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
708 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
709 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (*p + len != end) {
712 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
713 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
714 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100716 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
718 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
719 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
722 NULL, 0, NULL, 0)) != 0) {
723 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
724 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100725
726 *p += len;
727
728 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
730 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
731 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
734 NULL, 0, *p, len)) != 0) {
735 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
736 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100737
738 *p += len;
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (mbedtls_rsa_complete(rsa) != 0 ||
741 mbedtls_rsa_check_pubkey(rsa) != 0) {
742 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000743 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (*p != end) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
747 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
748 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200753
754/* Get a PK algorithm identifier
755 *
756 * AlgorithmIdentifier ::= SEQUENCE {
757 * algorithm OBJECT IDENTIFIER,
758 * parameters ANY DEFINED BY algorithm OPTIONAL }
759 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760static int pk_get_pk_alg(unsigned char **p,
761 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200762 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
763 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764{
Janos Follath24eed8d2019-11-22 13:21:35 +0000765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
772 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773
Jethro Beekman01672442023-04-19 14:08:14 +0200774 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200775#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200776 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
777 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
778 if (ret == 0) {
779 *pk_alg = MBEDTLS_PK_ECKEY;
780 }
781 }
782#else
783 (void) ec_grp_id;
784#endif
785 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
787 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788
789 /*
790 * No parameters with RSA (only for EC)
791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (*pk_alg == MBEDTLS_PK_RSA &&
793 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
794 params->len != 0)) {
795 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200796 }
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200799}
800
801/*
802 * SubjectPublicKeyInfo ::= SEQUENCE {
803 * algorithm AlgorithmIdentifier,
804 * subjectPublicKey BIT STRING }
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
807 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808{
Janos Follath24eed8d2019-11-22 13:21:35 +0000809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200810 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_asn1_buf alg_params;
812 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200813 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
817 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
818 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819 }
820
821 end = *p + len;
822
Jethro Beekman01672442023-04-19 14:08:14 +0200823 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 return ret;
825 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
829 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (*p + len != end) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
833 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
834 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
837 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
838 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
841 return ret;
842 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (pk_alg == MBEDTLS_PK_RSA) {
846 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200849#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200851#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200852 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200853 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200854 } else
855#endif
856 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200857 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200858 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (ret == 0) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200860 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200863#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ret == 0 && *p != end) {
867 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
868 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
869 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (ret != 0) {
872 mbedtls_pk_free(pk);
873 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876}
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100880 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
881 *
882 * The value zero is:
883 * - never a valid value for an RSA parameter
884 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
885 *
886 * Since values can't be omitted in PKCS#1, passing a zero value to
887 * rsa_complete() would be incorrect, so reject zero values early.
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int asn1_get_nonzero_mpi(unsigned char **p,
890 const unsigned char *end,
891 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100892{
893 int ret;
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 ret = mbedtls_asn1_get_mpi(p, end, X);
896 if (ret != 0) {
897 return ret;
898 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
901 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
902 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100905}
906
907/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 * Parse a PKCS#1 encoded private RSA key
909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
911 const unsigned char *key,
912 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100914 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915 size_t len;
916 unsigned char *p, *end;
917
Hanno Beckerefa14e82017-10-11 19:45:19 +0100918 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100920
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921 p = (unsigned char *) key;
922 end = p + keylen;
923
924 /*
925 * This function parses the RSAPrivateKey (PKCS#1)
926 *
927 * RSAPrivateKey ::= SEQUENCE {
928 * version Version,
929 * modulus INTEGER, -- n
930 * publicExponent INTEGER, -- e
931 * privateExponent INTEGER, -- d
932 * prime1 INTEGER, -- p
933 * prime2 INTEGER, -- q
934 * exponent1 INTEGER, -- d mod (p-1)
935 * exponent2 INTEGER, -- d mod (q-1)
936 * coefficient INTEGER, -- (inverse of q) mod p
937 * otherPrimeInfos OtherPrimeInfos OPTIONAL
938 * }
939 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
941 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
942 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943 }
944
945 end = p + len;
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
948 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 }
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (version != 0) {
952 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953 }
954
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100955 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
957 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
958 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100959 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100962 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
964 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
965 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100966 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100968
969 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
971 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
972 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100973 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100975
976 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
978 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
979 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100980 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100982
983 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
985 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
986 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100987 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100989
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100990#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500991 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
993 * that they can be easily recomputed from D, P and Q. However by
994 * parsing them from the PKCS1 structure it is possible to avoid
995 * recalculating them which both reduces the overhead of loading
996 * RSA private keys into memory and also avoids side channels which
997 * can arise when computing those values, since all of D, P, and Q
998 * are secret. See https://eprint.iacr.org/2020/055 for a
999 * description of one such attack.
1000 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001001
Jack Lloyd80cc8112020-01-22 17:34:29 -05001002 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1004 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1005 goto cleanup;
1006 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001007
1008 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1010 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1011 goto cleanup;
1012 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001013
1014 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1016 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1017 goto cleanup;
1018 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001019
Jack Lloyd60239752020-01-27 17:53:36 -05001020#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001021 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1023 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1024 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1025 goto cleanup;
1026 }
Jack Lloyd60239752020-01-27 17:53:36 -05001027#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001028
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001029 /* rsa_complete() doesn't complete anything with the default
1030 * implementation but is still called:
1031 * - for the benefit of alternative implementation that may want to
1032 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1033 * - as is also sanity-checks the key
1034 *
1035 * Furthermore, we also check the public part for consistency with
1036 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1039 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001040 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001041 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (p != end) {
1044 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1045 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 }
1047
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048cleanup:
1049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001053 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret & 0xff80) == 0) {
1055 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1056 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 }
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066
Valerio Setti81d75122023-06-14 14:49:33 +02001067#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068/*
1069 * Parse a SEC1 encoded private EC key
1070 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001071static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 const unsigned char *key, size_t keylen,
1073 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074{
Janos Follath24eed8d2019-11-22 13:21:35 +00001075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001076 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001077 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001078 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001079 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001080 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 unsigned char *end = p + keylen;
1082 unsigned char *end2;
1083
1084 /*
1085 * RFC 5915, or SEC1 Appendix C.4
1086 *
1087 * ECPrivateKey ::= SEQUENCE {
1088 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1089 * privateKey OCTET STRING,
1090 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1091 * publicKey [1] BIT STRING OPTIONAL
1092 * }
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1095 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1096 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 }
1098
1099 end = p + len;
1100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1102 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1103 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (version != 1) {
1106 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1107 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1110 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1111 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112
Valerio Setti6b062ee2023-06-30 17:32:57 +02001113 /* Keep a reference to the position fo the private key. It will be used
1114 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001115 d = p;
1116 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001117
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 p += len;
1119
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001120 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001122 /*
1123 * Is 'parameters' present?
1124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1126 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1127 0)) == 0) {
1128 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001129 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001131 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001134 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001135 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001136
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001137 /*
1138 * Load the private key
1139 */
1140 ret = pk_ecc_set_key(pk, d, d_len);
1141 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001142 return ret;
1143 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001146 /*
1147 * Is 'publickey' present? If not, or if we can't read it (eg because it
1148 * is compressed), create it from the private key.
1149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1151 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1152 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001153 end2 = p + len;
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1156 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1157 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (p + len != end2) {
1160 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1161 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1162 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001163
Valerio Setti4064dbb2023-05-17 15:33:07 +02001164 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001165 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001167 /*
1168 * The only acceptable failure mode of pk_get_ecpubkey() above
1169 * is if the point format is not recognized.
1170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1172 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1173 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001177 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001179
Valerio Setti34f67552023-04-03 15:19:18 +02001180 if (!pubkey_done) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001181 if ((ret = pk_derive_public_key(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001182 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001183 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001184 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187}
Valerio Setti81d75122023-06-14 14:49:33 +02001188#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001190/***********************************************************************
1191 *
1192 * PKCS#8 parsing functions
1193 *
1194 **********************************************************************/
1195
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196/*
1197 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001198 *
1199 * Notes:
1200 *
1201 * - This function does not own the key buffer. It is the
1202 * responsibility of the caller to take care of zeroizing
1203 * and freeing it after use.
1204 *
1205 * - The function is responsible for freeing the provided
1206 * PK context on failure.
1207 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208 */
1209static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_pk_context *pk,
1211 const unsigned char *key, size_t keylen,
1212 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213{
1214 int ret, version;
1215 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 unsigned char *p = (unsigned char *) key;
1218 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001220 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
Valerio Setti81d75122023-06-14 14:49:33 +02001223#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001224 (void) f_rng;
1225 (void) p_rng;
1226#endif
1227
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001229 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230 *
1231 * PrivateKeyInfo ::= SEQUENCE {
1232 * version Version,
1233 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1234 * privateKey PrivateKey,
1235 * attributes [0] IMPLICIT Attributes OPTIONAL }
1236 *
1237 * Version ::= INTEGER
1238 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1239 * PrivateKey ::= OCTET STRING
1240 *
1241 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1242 */
1243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1245 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1246 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
1248
1249 end = p + len;
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001253 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (version != 0) {
1256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1257 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258
Jethro Beekman01672442023-04-19 14:08:14 +02001259 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 return ret;
1261 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1265 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (len < 1) {
1268 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1269 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1270 }
1271
1272 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1273 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1274 }
1275
1276 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1277 return ret;
1278 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (pk_alg == MBEDTLS_PK_RSA) {
1282 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1283 mbedtls_pk_free(pk);
1284 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 }
1286 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001288#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001290#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001291 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001292 if ((ret =
1293 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001294 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001295 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001296 p_rng)) != 0) {
1297 mbedtls_pk_free(pk);
1298 return ret;
1299 }
1300 } else
1301#endif
1302 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001303 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1304 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001305 mbedtls_pk_free(pk);
1306 return ret;
1307 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 }
1309 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001310#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001313 end = p + len;
1314 if (end != (key + keylen)) {
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1316 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1317 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320}
1321
1322/*
1323 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001324 *
1325 * To save space, the decryption happens in-place on the given key buffer.
1326 * Also, while this function may modify the keybuffer, it doesn't own it,
1327 * and instead it is the responsibility of the caller to zeroize and properly
1328 * free it after use.
1329 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001332MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 mbedtls_pk_context *pk,
1334 unsigned char *key, size_t keylen,
1335 const unsigned char *pwd, size_t pwdlen,
1336 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001338 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001340 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1343#if defined(MBEDTLS_PKCS12_C)
1344 mbedtls_cipher_type_t cipher_alg;
1345 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001347 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Hanno Becker2aa80a72017-09-07 15:28:45 +01001349 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 end = p + keylen;
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (pwdlen == 0) {
1353 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1354 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
1356 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001357 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 *
1359 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1360 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1361 * encryptedData EncryptedData
1362 * }
1363 *
1364 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1365 *
1366 * EncryptedData ::= OCTET STRING
1367 *
1368 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001369 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1372 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1373 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 }
1375
1376 end = p + len;
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1380 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1383 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1384 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Hanno Beckerfab35692017-08-25 13:38:26 +01001386 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
1388 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001389 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001393 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001394 cipher_alg, md_alg,
1395 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1397 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1398 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001402
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001403 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_PKCS12_C */
1406#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001408 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1409 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1411 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1412 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001416
1417 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001420 {
1421 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001422 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (decrypted == 0) {
1425 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1426 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001427 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001431/***********************************************************************
1432 *
1433 * Top-level functions, with format auto-discovery
1434 *
1435 **********************************************************************/
1436
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437/*
1438 * Parse a private key
1439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1441 const unsigned char *key, size_t keylen,
1442 const unsigned char *pwd, size_t pwdlen,
1443 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444{
Janos Follath24eed8d2019-11-22 13:21:35 +00001445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001450#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (keylen == 0) {
1453 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1454 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001455
1456#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001460 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001462 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 } else {
1464 ret = mbedtls_pem_read_buffer(&pem,
1465 "-----BEGIN RSA PRIVATE KEY-----",
1466 "-----END RSA PRIVATE KEY-----",
1467 key, pwd, pwdlen, &len);
1468 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (ret == 0) {
1471 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1472 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1473 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1474 pem.buf, pem.buflen)) != 0) {
1475 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 }
1477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 mbedtls_pem_free(&pem);
1479 return ret;
1480 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1481 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1482 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1483 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1484 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1485 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488
Valerio Setti81d75122023-06-14 14:49:33 +02001489#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001490 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001492 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 } else {
1494 ret = mbedtls_pem_read_buffer(&pem,
1495 "-----BEGIN EC PRIVATE KEY-----",
1496 "-----END EC PRIVATE KEY-----",
1497 key, pwd, pwdlen, &len);
1498 }
1499 if (ret == 0) {
1500 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001503 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 pem.buf, pem.buflen,
1505 f_rng, p_rng)) != 0) {
1506 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 }
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 mbedtls_pem_free(&pem);
1510 return ret;
1511 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1512 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1513 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1514 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1515 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1516 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517 }
Valerio Setti81d75122023-06-14 14:49:33 +02001518#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001520 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001522 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 } else {
1524 ret = mbedtls_pem_read_buffer(&pem,
1525 "-----BEGIN PRIVATE KEY-----",
1526 "-----END PRIVATE KEY-----",
1527 key, NULL, 0, &len);
1528 }
1529 if (ret == 0) {
1530 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1531 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1532 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 }
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_pem_free(&pem);
1536 return ret;
1537 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1538 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001542 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001544 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 } else {
1546 ret = mbedtls_pem_read_buffer(&pem,
1547 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1548 "-----END ENCRYPTED PRIVATE KEY-----",
1549 key, NULL, 0, &len);
1550 }
1551 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001552 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1553 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555 }
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_pem_free(&pem);
1558 return ret;
1559 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1560 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563#else
1564 ((void) pwd);
1565 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567
1568 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001569 * At this point we only know it's not a PEM formatted key. Could be any
1570 * of the known DER encoded private key formats
1571 *
1572 * We try the different DER format parsers to see if one passes without
1573 * error
1574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001577 unsigned char *key_copy;
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1580 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1581 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001584
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001585 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1586 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001587
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001588 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 }
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ret == 0) {
1592 return 0;
1593 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_pk_free(pk);
1596 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1599 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1604 if (ret == 0) {
1605 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001606 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 mbedtls_pk_free(pk);
1609 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1614 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1615 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1616 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001617 }
1618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 mbedtls_pk_free(pk);
1620 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622
Valerio Setti81d75122023-06-14 14:49:33 +02001623#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1625 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001626 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 key, keylen, f_rng, p_rng) == 0) {
1628 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001631#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632
Valerio Setti81d75122023-06-14 14:49:33 +02001633 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001634 * it is ok to leave the PK context initialized but not
1635 * freed: It is the caller's responsibility to call pk_init()
1636 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001637 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001638 * isn't, this leads to mbedtls_pk_free() being called
1639 * twice, once here and once by the caller, but this is
1640 * also ok and in line with the mbedtls_pk_free() calls
1641 * on failed PEM parsing attempts. */
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644}
1645
1646/*
1647 * Parse a public key
1648 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1650 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651{
Janos Follath24eed8d2019-11-22 13:21:35 +00001652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001654#if defined(MBEDTLS_RSA_C)
1655 const mbedtls_pk_info_t *pk_info;
1656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001658 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001660#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (keylen == 0) {
1663 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1664 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001665
1666#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001668#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001669 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001671 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 } else {
1673 ret = mbedtls_pem_read_buffer(&pem,
1674 "-----BEGIN RSA PUBLIC KEY-----",
1675 "-----END RSA PUBLIC KEY-----",
1676 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001678
1679 if (ret == 0) {
1680 p = pem.buf;
1681 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1682 mbedtls_pem_free(&pem);
1683 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1684 }
1685
1686 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1687 mbedtls_pem_free(&pem);
1688 return ret;
1689 }
1690
1691 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1692 mbedtls_pk_free(ctx);
1693 }
1694
1695 mbedtls_pem_free(&pem);
1696 return ret;
1697 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1698 mbedtls_pem_free(&pem);
1699 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001700 }
1701#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001702
1703 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001705 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 } else {
1707 ret = mbedtls_pem_read_buffer(&pem,
1708 "-----BEGIN PUBLIC KEY-----",
1709 "-----END PUBLIC KEY-----",
1710 key, NULL, 0, &len);
1711 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001714 /*
1715 * Was PEM encoded
1716 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001717 p = pem.buf;
1718
Jethro Beekman01672442023-04-19 14:08:14 +02001719 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_pem_free(&pem);
1721 return ret;
1722 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1723 mbedtls_pem_free(&pem);
1724 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001728
1729#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1731 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001732 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001733
1734 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1735 return ret;
1736 }
1737
1738 p = (unsigned char *) key;
1739 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1740 if (ret == 0) {
1741 return ret;
1742 }
1743 mbedtls_pk_free(ctx);
1744 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1745 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1746 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001747 }
1748#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001749 p = (unsigned char *) key;
1750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001754}
1755
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001756/***********************************************************************
1757 *
1758 * Top-level functions, with filesystem support
1759 *
1760 **********************************************************************/
1761
1762#if defined(MBEDTLS_FS_IO)
1763/*
1764 * Load all data from a file into a given buffer.
1765 *
1766 * The file is expected to contain either PEM or DER encoded data.
1767 * A terminating null byte is always appended. It is included in the announced
1768 * length only if the data looks like it is PEM encoded.
1769 */
1770int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1771{
1772 FILE *f;
1773 long size;
1774
1775 if ((f = fopen(path, "rb")) == NULL) {
1776 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1777 }
1778
1779 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1780 mbedtls_setbuf(f, NULL);
1781
1782 fseek(f, 0, SEEK_END);
1783 if ((size = ftell(f)) == -1) {
1784 fclose(f);
1785 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1786 }
1787 fseek(f, 0, SEEK_SET);
1788
1789 *n = (size_t) size;
1790
1791 if (*n + 1 == 0 ||
1792 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1793 fclose(f);
1794 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1795 }
1796
1797 if (fread(*buf, 1, *n, f) != *n) {
1798 fclose(f);
1799
1800 mbedtls_zeroize_and_free(*buf, *n);
1801
1802 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1803 }
1804
1805 fclose(f);
1806
1807 (*buf)[*n] = '\0';
1808
1809 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1810 ++*n;
1811 }
1812
1813 return 0;
1814}
1815
1816/*
1817 * Load and parse a private key
1818 */
1819int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1820 const char *path, const char *pwd,
1821 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1822{
1823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1824 size_t n;
1825 unsigned char *buf;
1826
1827 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1828 return ret;
1829 }
1830
1831 if (pwd == NULL) {
1832 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1833 } else {
1834 ret = mbedtls_pk_parse_key(ctx, buf, n,
1835 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1836 }
1837
1838 mbedtls_zeroize_and_free(buf, n);
1839
1840 return ret;
1841}
1842
1843/*
1844 * Load and parse a public key
1845 */
1846int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1847{
1848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1849 size_t n;
1850 unsigned char *buf;
1851
1852 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1853 return ret;
1854 }
1855
1856 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1857
1858 mbedtls_zeroize_and_free(buf, n);
1859
1860 return ret;
1861}
1862#endif /* MBEDTLS_FS_IO */
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#endif /* MBEDTLS_PK_PARSE_C */