blob: 820c8d1cf4376666a6f1beb50ad23043223b79f2 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020028#include "mbedtls/platform.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard5fcbe4c2023-07-06 13:02:51 +020033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
35#include "psa/crypto.h"
36#endif
37
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020038/* Key types */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Valerio Setti81d75122023-06-14 14:49:33 +020042#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020043#include "mbedtls/ecp.h"
Valerio Setti4064dbb2023-05-17 15:33:07 +020044#include "pk_internal.h"
45#endif
Manuel Pégourié-Gonnardda88c382023-07-06 12:31:43 +020046
47/* Extended formats */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020056#endif
57
Manuel Pégourié-Gonnard997a95e2023-07-26 15:18:30 +020058#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
59
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020060/***********************************************************************
Paul Bakker1a7550a2013-09-15 13:01:22 +020061 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020062 * ECC setters
63 *
64 * 1. This is an abstraction layer around MBEDTLS_PK_USE_PSA_EC_DATA:
65 * this macro will not appear outside this section.
66 * 2. All inputs are raw: no metadata, no ASN.1 until the next section.
67 *
68 **********************************************************************/
69
70/*
71 * Set the group used by this key.
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +020072 *
73 * [in/out] pk: in: must have been pk_setup() to an ECC type
74 * out: will have group (curve) information set
75 * [in] grp_in: a supported group ID (not NONE)
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +020077static int pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
78{
79#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
80 size_t ec_bits;
81 psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
82
83 /* group may already be initialized; if so, make sure IDs match */
84 if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
85 (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
86 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
87 }
88
89 /* set group */
90 pk->ec_family = ec_family;
91 pk->ec_bits = ec_bits;
92
93 return 0;
94#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
95 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
96
97 /* grp may already be initialized; if so, make sure IDs match */
98 if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
99 mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
100 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
101 }
102
103 /* set group */
104 return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
105#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
106}
107
108/*
109 * Set the private key material
110 *
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200111 * [in/out] pk: in: must have the group set already, see pk_ecc_set_group().
112 * out: will have the private key set.
113 * [in] key, key_len: the raw private key (no ASN.1 wrapping).
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200114 */
115static int pk_ecc_set_key(mbedtls_pk_context *pk,
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200116 unsigned char *key, size_t key_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200117{
118#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
120 psa_status_t status;
121
122 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
123 psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
124 psa_key_usage_t flags = PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_DERIVE;
125 /* Montgomery allows only ECDH, others ECDSA too */
126 if (pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
127 flags |= PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE;
128 psa_set_key_enrollment_algorithm(&attributes,
129 MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
130 }
131 psa_set_key_usage_flags(&attributes, flags);
132
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200133 status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200134 return psa_pk_status_to_mbedtls(status);
135
136#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
137
138 mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
Manuel Pégourié-Gonnardd1aa6422023-07-26 22:24:23 +0200139 int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200140 if (ret != 0) {
141 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
142 }
143 return 0;
144#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
145}
146
147/*
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200148 * Derive a public key from its private counterpart.
149 * Computationally intensive, only use when public key is not available.
150 *
151 * [in/out] pk: in: must have the private key set, see pk_ecc_set_key().
152 * out: will have the public key set.
153 * [in] prv, prv_len: the raw private key (see note below).
154 * [in] f_rng, p_rng: RNG function and context.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200155 *
156 * Note: the private key information is always available from pk,
157 * however for convenience the serialized version is also passed,
158 * as it's available at each calling site, and useful in some configs
Manuel Pégourié-Gonnard94cf1f82023-08-02 12:09:24 +0200159 * (as otherwise we would have to re-serialize it from the pk context).
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200160 *
161 * There are three implementations of this function:
162 * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
163 * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
164 * 3. not MBEDTLS_USE_PSA_CRYPTO.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200165 */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200166static int pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
167 const unsigned char *prv, size_t prv_len,
168 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200169{
170#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200171
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200172 (void) f_rng;
173 (void) p_rng;
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200174 (void) prv;
175 (void) prv_len;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200176 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200177
178 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
179 &pk->pub_raw_len);
180 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200181
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200182#elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200183
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200184 (void) f_rng;
185 (void) p_rng;
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200186 psa_status_t status;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200187
188 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200189 size_t curve_bits;
190 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200191
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200192 /* Import private key into PSA, from serialized input */
193 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
194 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200195 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
196 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200197 status = psa_import_key(&key_attr, prv, prv_len, &key_id);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200198 if (status != PSA_SUCCESS) {
199 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200200 }
201
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200202 /* Export public key from PSA */
203 unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
204 size_t pub_len;
205 status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
206 psa_status_t destruction_status = psa_destroy_key(key_id);
207 if (status != PSA_SUCCESS) {
208 return psa_pk_status_to_mbedtls(status);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200209 } else if (destruction_status != PSA_SUCCESS) {
210 return psa_pk_status_to_mbedtls(destruction_status);
211 }
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200212
213 /* Load serialized public key into ecp_keypair structure */
214 return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
215
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200216#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200217
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200218 (void) prv;
219 (void) prv_len;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200220
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200221 mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200222 return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
Manuel Pégourié-Gonnard0b8e4562023-07-26 22:43:25 +0200223
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200224#endif /* MBEDTLS_USE_PSA_CRYPTO */
225}
226
227#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
228/*
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200229 * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200230 *
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200231 * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
232 * functions to handle keys. However, currently psa_import_key() does not
233 * support compressed points. In case that support was explicitly requested,
234 * this fallback uses ECP functions to get the job done. This is the reason
235 * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
236 *
237 * [in/out] pk: in: must have the group set, see pk_ecc_set_group().
238 * out: will have the public key set.
239 * [in] pub, pub_len: the public key as an ECPoint,
240 * in any format supported by ECP.
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200241 *
242 * Return:
243 * - 0 on success;
244 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
245 * but not supported;
246 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200247 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200248static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
249 const unsigned char *pub,
250 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200251{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200252#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
Manuel Pégourié-Gonnard53d3e402023-08-01 11:19:24 +0200253 (void) pk;
254 (void) pub;
255 (void) pub_len;
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200256 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200257#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200258 mbedtls_ecp_keypair ecp_key;
259 mbedtls_ecp_group_id ecp_group_id;
260 int ret;
261
262 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
263
264 mbedtls_ecp_keypair_init(&ecp_key);
265 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
266 if (ret != 0) {
Manuel Pégourié-Gonnard842ffc52023-08-02 12:10:51 +0200267 goto exit;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200268 }
269 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200270 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200271 if (ret != 0) {
272 goto exit;
273 }
274 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
275 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200276 &pk->pub_raw_len, pk->pub_raw,
277 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200278
279exit:
280 mbedtls_ecp_keypair_free(&ecp_key);
281 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200282#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
283}
284#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
285
286/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200287 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200288 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200289 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
290 * out: will have the public key set.
291 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200292 *
293 * Return:
294 * - 0 on success;
295 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
296 * but not supported;
297 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200298 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200299static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
300 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200301{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200302#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200303
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200304 /* Load the key */
305 if (*pub == 0x04) {
306 /* Uncompressed format, directly supported by PSA */
307 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200308 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
309 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200310 memcpy(pk->pub_raw, pub, pub_len);
311 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200312 } else {
313 /* Other format, try the fallback */
314 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
315 if (ret != 0) {
316 return ret;
317 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100318 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200319
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200320 /* Validate the key by trying to import it */
321 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
322 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
323
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200324 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200325 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
326 psa_set_key_bits(&key_attrs, pk->ec_bits);
327
328 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200329 &key_id) != PSA_SUCCESS) ||
330 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
331 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200333
334 return 0;
335
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200336#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200337
338 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200339 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200340 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
341 if (ret != 0) {
342 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200344 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
345
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200346#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200347}
348
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200349/***********************************************************************
350 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200351 * Low-level ECC parsing: optional support for SpecifiedECDomain
352 *
353 * There are two functions here that are used by the rest of the code:
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200354 * - pk_ecc_tag_is_speficied_ec_domain()
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200355 * - pk_ecc_group_id_from_specified()
356 *
357 * All the other functions are internal to this section.
358 *
359 * The two "public" functions have a dummy variant provided
360 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
361 * abstraction layer for this macro, which should not appear outside
362 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200363 *
364 **********************************************************************/
365
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200366#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
367/* See the "real" version for documentation */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200368static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200369{
370 (void) tag;
371 return 0;
372}
373
374/* See the "real" version for documentation */
375static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
376 mbedtls_ecp_group_id *grp_id)
377{
378 (void) params;
379 (void) grp_id;
380 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
381}
382#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
383/*
384 * Tell if the passed tag might be the start of SpecifiedECDomain
385 * (that is, a sequence).
386 */
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200387static int pk_ecc_tag_is_specified_ec_domain(int tag)
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200388{
389 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
390}
391
Paul Bakker1a7550a2013-09-15 13:01:22 +0200392/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
394 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200395 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 * if it was encoded compressed.
397 *
398 * SpecifiedECDomain ::= SEQUENCE {
399 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
400 * fieldID FieldID {{FieldTypes}},
401 * curve Curve,
402 * base ECPoint,
403 * order INTEGER,
404 * cofactor INTEGER OPTIONAL,
405 * hash HashAlgorithm OPTIONAL,
406 * ...
407 * }
408 *
409 * We only support prime-field as field type, and ignore hash and cofactor.
410 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412{
Janos Follath24eed8d2019-11-22 13:21:35 +0000413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200415 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416 const unsigned char *end_field, *end_curve;
417 size_t len;
418 int ver;
419
420 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
423 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ver < 1 || ver > 3) {
426 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
427 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
429 /*
430 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
431 * fieldType FIELD-ID.&id({IOSet}),
432 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
433 * }
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
436 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
437 return ret;
438 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440 end_field = p + len;
441
442 /*
443 * FIELD-ID ::= TYPE-IDENTIFIER
444 * FieldTypes FIELD-ID ::= {
445 * { Prime-p IDENTIFIED BY prime-field } |
446 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
447 * }
448 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
451 return ret;
452 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
455 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
456 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457 }
458
459 p += len;
460
461 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
464 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (p != end_field) {
469 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
470 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
471 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100472
473 /*
474 * Curve ::= SEQUENCE {
475 * a FieldElement,
476 * b FieldElement,
477 * seed BIT STRING OPTIONAL
478 * -- Shall be present if used in SpecifiedECDomain
479 * -- with version equal to ecdpVer2 or ecdpVer3
480 * }
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
483 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
484 return ret;
485 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100486
487 end_curve = p + len;
488
489 /*
490 * FieldElement ::= OCTET STRING
491 * containing an integer in the case of a prime field
492 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
494 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
495 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100496 }
497
498 p += len;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
501 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
502 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100503 }
504
505 p += len;
506
507 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100509 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (p != end_curve) {
513 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
514 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
515 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100516
517 /*
518 * ECPoint ::= OCTET STRING
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
521 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
522 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
525 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100526 /*
527 * If we can't read the point because it's compressed, cheat by
528 * reading only the X coordinate and the parity bit of Y.
529 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
531 (p[0] != 0x02 && p[0] != 0x03) ||
532 len != mbedtls_mpi_size(&grp->P) + 1 ||
533 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
534 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
535 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
536 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100537 }
538 }
539
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100540 p += len;
541
542 /*
543 * order INTEGER
544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
546 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
547 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100550
551 /*
552 * Allow optional elements by purposefully not enforcing p == end here.
553 */
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100556}
557
558/*
559 * Find the group id associated with an (almost filled) group as generated by
560 * pk_group_from_specified(), or return an error if unknown.
561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562static 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 +0100563{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100564 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_ecp_group ref;
566 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100571 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_ecp_group_free(&ref);
573 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100574
575 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
577 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
578 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
579 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
580 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
581 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
582 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100583 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 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 +0100585 break;
586 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100587 }
588
589cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100591
592 *grp_id = *id;
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100599}
600
601/*
602 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
603 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200604static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
605 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100606{
Janos Follath24eed8d2019-11-22 13:21:35 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100613 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100617
618cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000619 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200620 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000621 * temporary grp breaks that flow and it's members are populated
622 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
623 * which is assuming a group populated by _setup() may not clean-up
624 * properly -> Manually free it's members.
625 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000626 mbedtls_mpi_free(&grp.N);
627 mbedtls_mpi_free(&grp.P);
628 mbedtls_mpi_free(&grp.A);
629 mbedtls_mpi_free(&grp.B);
630 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100635
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200636/***********************************************************************
637 *
638 * Unsorted (yet!) from this point on until the next section header
639 *
640 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200641
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200642/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200643 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200644 * ECParameters ::= CHOICE {
645 * namedCurve OBJECT IDENTIFIER
646 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
647 * -- implicitCurve NULL
648 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200649 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200650static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
651 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200652{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200654
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200655 if (end - *p < 1) {
656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
657 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200658 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200659
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200660 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200661 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200662 if (params->tag != MBEDTLS_ASN1_OID &&
Manuel Pégourié-Gonnardf1b76332023-08-02 12:14:19 +0200663 !pk_ecc_tag_is_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200664 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
665 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
666 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200667
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200668 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200669 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
670 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200671
672 params->p = *p;
673 *p += params->len;
674
675 if (*p != end) {
676 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
677 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
678 }
679
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200680 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200681}
682
683/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200684 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100685 *
686 * ECParameters ::= CHOICE {
687 * namedCurve OBJECT IDENTIFIER
688 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
689 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200691static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692{
Janos Follath24eed8d2019-11-22 13:21:35 +0000693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (params->tag == MBEDTLS_ASN1_OID) {
697 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
698 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
699 }
700 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200701 ret = pk_ecc_group_id_from_specified(params, &grp_id);
702 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return ret;
704 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100705 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200707 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708}
709
Jethro Beekman01672442023-04-19 14:08:14 +0200710#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
711
712/*
713 * Load an RFC8410 EC key, which doesn't have any parameters
714 */
715static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
716 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200717 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200718{
719 if (params->tag != 0 || params->len != 0) {
720 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
721 }
722
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200723 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200724}
725
726/*
727 * Parse an RFC 8410 encoded private EC key
728 *
729 * CurvePrivateKey ::= OCTET STRING
730 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200731static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200732 unsigned char *key, size_t keylen, const unsigned char *end,
733 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
734{
735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
736 size_t len;
737
738 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
739 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
740 }
741
742 if (key + len != end) {
743 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
744 }
745
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200746 /*
747 * Load the private key
748 */
749 ret = pk_ecc_set_key(pk, key, len);
750 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200751 return ret;
752 }
Jethro Beekman01672442023-04-19 14:08:14 +0200753
Valerio Setti4064dbb2023-05-17 15:33:07 +0200754 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
755 * which never contain a public key. As such, derive the public key
756 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200757 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200758 return ret;
759 }
760
Jethro Beekman01672442023-04-19 14:08:14 +0200761 return 0;
762}
763#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200764
Valerio Setti81d75122023-06-14 14:49:33 +0200765#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768/*
769 * RSAPublicKey ::= SEQUENCE {
770 * modulus INTEGER, -- n
771 * publicExponent INTEGER -- e
772 * }
773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774static int pk_get_rsapubkey(unsigned char **p,
775 const unsigned char *end,
776 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777{
Janos Follath24eed8d2019-11-22 13:21:35 +0000778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200779 size_t len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
784 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (*p + len != end) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
789 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
794 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
797 NULL, 0, NULL, 0)) != 0) {
798 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
799 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100800
801 *p += len;
802
803 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
806 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
809 NULL, 0, *p, len)) != 0) {
810 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
811 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812
813 *p += len;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (mbedtls_rsa_complete(rsa) != 0 ||
816 mbedtls_rsa_check_pubkey(rsa) != 0) {
817 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000818 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (*p != end) {
821 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
822 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
823 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828
829/* Get a PK algorithm identifier
830 *
831 * AlgorithmIdentifier ::= SEQUENCE {
832 * algorithm OBJECT IDENTIFIER,
833 * parameters ANY DEFINED BY algorithm OPTIONAL }
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static int pk_get_pk_alg(unsigned char **p,
836 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200837 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
838 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839{
Janos Follath24eed8d2019-11-22 13:21:35 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
846 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
847 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848
Jethro Beekman01672442023-04-19 14:08:14 +0200849 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200850#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200851 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
852 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
853 if (ret == 0) {
854 *pk_alg = MBEDTLS_PK_ECKEY;
855 }
856 }
857#else
858 (void) ec_grp_id;
859#endif
860 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
864 /*
865 * No parameters with RSA (only for EC)
866 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (*pk_alg == MBEDTLS_PK_RSA &&
868 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
869 params->len != 0)) {
870 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874}
875
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200876/* Helper for Montgomery curves */
877#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
878#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
879 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
880#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
881
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882/*
883 * SubjectPublicKeyInfo ::= SEQUENCE {
884 * algorithm AlgorithmIdentifier,
885 * subjectPublicKey BIT STRING }
886 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
888 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889{
Janos Follath24eed8d2019-11-22 13:21:35 +0000890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_asn1_buf alg_params;
893 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200894 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
898 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900 }
901
902 end = *p + len;
903
Jethro Beekman01672442023-04-19 14:08:14 +0200904 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return ret;
906 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
909 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
910 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (*p + len != end) {
913 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
914 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
915 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
918 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
919 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
922 return ret;
923 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (pk_alg == MBEDTLS_PK_RSA) {
927 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200930#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200932#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200933 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200934 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200935 } else
936#endif
937 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200938 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200939 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200941 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
942 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200945#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ret == 0 && *p != end) {
949 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
950 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
951 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (ret != 0) {
954 mbedtls_pk_free(pk);
955 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958}
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100962 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
963 *
964 * The value zero is:
965 * - never a valid value for an RSA parameter
966 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
967 *
968 * Since values can't be omitted in PKCS#1, passing a zero value to
969 * rsa_complete() would be incorrect, so reject zero values early.
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971static int asn1_get_nonzero_mpi(unsigned char **p,
972 const unsigned char *end,
973 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100974{
975 int ret;
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 ret = mbedtls_asn1_get_mpi(p, end, X);
978 if (ret != 0) {
979 return ret;
980 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
983 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
984 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100987}
988
989/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990 * Parse a PKCS#1 encoded private RSA key
991 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
993 const unsigned char *key,
994 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100996 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 size_t len;
998 unsigned char *p, *end;
999
Hanno Beckerefa14e82017-10-11 19:45:19 +01001000 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001002
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003 p = (unsigned char *) key;
1004 end = p + keylen;
1005
1006 /*
1007 * This function parses the RSAPrivateKey (PKCS#1)
1008 *
1009 * RSAPrivateKey ::= SEQUENCE {
1010 * version Version,
1011 * modulus INTEGER, -- n
1012 * publicExponent INTEGER, -- e
1013 * privateExponent INTEGER, -- d
1014 * prime1 INTEGER, -- p
1015 * prime2 INTEGER, -- q
1016 * exponent1 INTEGER, -- d mod (p-1)
1017 * exponent2 INTEGER, -- d mod (q-1)
1018 * coefficient INTEGER, -- (inverse of q) mod p
1019 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1020 * }
1021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1023 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1024 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 }
1026
1027 end = p + len;
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1030 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 }
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (version != 0) {
1034 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 }
1036
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001037 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1039 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1040 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001044 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1046 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1047 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001048 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001050
1051 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1053 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1054 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001055 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001057
1058 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1060 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1061 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001062 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001064
1065 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1067 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1068 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001069 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001071
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001072#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001073 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1075 * that they can be easily recomputed from D, P and Q. However by
1076 * parsing them from the PKCS1 structure it is possible to avoid
1077 * recalculating them which both reduces the overhead of loading
1078 * RSA private keys into memory and also avoids side channels which
1079 * can arise when computing those values, since all of D, P, and Q
1080 * are secret. See https://eprint.iacr.org/2020/055 for a
1081 * description of one such attack.
1082 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001083
Jack Lloyd80cc8112020-01-22 17:34:29 -05001084 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1086 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1087 goto cleanup;
1088 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001089
1090 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1092 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1093 goto cleanup;
1094 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001095
1096 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1098 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1099 goto cleanup;
1100 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001101
Jack Lloyd60239752020-01-27 17:53:36 -05001102#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001103 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1105 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1106 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1107 goto cleanup;
1108 }
Jack Lloyd60239752020-01-27 17:53:36 -05001109#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001110
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001111 /* rsa_complete() doesn't complete anything with the default
1112 * implementation but is still called:
1113 * - for the benefit of alternative implementation that may want to
1114 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1115 * - as is also sanity-checks the key
1116 *
1117 * Furthermore, we also check the public part for consistency with
1118 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1121 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001122 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001123 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (p != end) {
1126 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1127 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 }
1129
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130cleanup:
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001135 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret & 0xff80) == 0) {
1137 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1138 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001139 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 }
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148
Valerio Setti81d75122023-06-14 14:49:33 +02001149#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150/*
1151 * Parse a SEC1 encoded private EC key
1152 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001153static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 const unsigned char *key, size_t keylen,
1155 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156{
Janos Follath24eed8d2019-11-22 13:21:35 +00001157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001158 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001159 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001160 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001162 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 unsigned char *end = p + keylen;
1164 unsigned char *end2;
1165
1166 /*
1167 * RFC 5915, or SEC1 Appendix C.4
1168 *
1169 * ECPrivateKey ::= SEQUENCE {
1170 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1171 * privateKey OCTET STRING,
1172 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1173 * publicKey [1] BIT STRING OPTIONAL
1174 * }
1175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1177 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1178 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 }
1180
1181 end = p + len;
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1184 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1185 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (version != 1) {
1188 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1189 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1193 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
Valerio Setti6b062ee2023-06-30 17:32:57 +02001195 /* Keep a reference to the position fo the private key. It will be used
1196 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001197 d = p;
1198 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001199
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200 p += len;
1201
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001204 /*
1205 * Is 'parameters' present?
1206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1208 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1209 0)) == 0) {
1210 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001211 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001213 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001216 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001217 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001219 /*
1220 * Load the private key
1221 */
1222 ret = pk_ecc_set_key(pk, d, d_len);
1223 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001224 return ret;
1225 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001228 /*
1229 * Is 'publickey' present? If not, or if we can't read it (eg because it
1230 * is compressed), create it from the private key.
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1233 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1234 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001235 end2 = p + len;
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1239 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (p + len != end2) {
1242 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1243 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1244 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001245
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001246 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001247 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001249 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001250 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001251 * is if the point format is not recognized.
1252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1254 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1255 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001256 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001259 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001261
Valerio Setti34f67552023-04-03 15:19:18 +02001262 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001263 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001264 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001265 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001266 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269}
Valerio Setti81d75122023-06-14 14:49:33 +02001270#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001272/***********************************************************************
1273 *
1274 * PKCS#8 parsing functions
1275 *
1276 **********************************************************************/
1277
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278/*
1279 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001280 *
1281 * Notes:
1282 *
1283 * - This function does not own the key buffer. It is the
1284 * responsibility of the caller to take care of zeroizing
1285 * and freeing it after use.
1286 *
1287 * - The function is responsible for freeing the provided
1288 * PK context on failure.
1289 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 */
1291static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 mbedtls_pk_context *pk,
1293 const unsigned char *key, size_t keylen,
1294 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295{
1296 int ret, version;
1297 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 unsigned char *p = (unsigned char *) key;
1300 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001302 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304
Valerio Setti81d75122023-06-14 14:49:33 +02001305#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001306 (void) f_rng;
1307 (void) p_rng;
1308#endif
1309
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001311 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 *
1313 * PrivateKeyInfo ::= SEQUENCE {
1314 * version Version,
1315 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1316 * privateKey PrivateKey,
1317 * attributes [0] IMPLICIT Attributes OPTIONAL }
1318 *
1319 * Version ::= INTEGER
1320 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1321 * PrivateKey ::= OCTET STRING
1322 *
1323 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1324 */
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 }
1330
1331 end = p + len;
1332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001335 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (version != 0) {
1338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1339 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340
Jethro Beekman01672442023-04-19 14:08:14 +02001341 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 return ret;
1343 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1347 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (len < 1) {
1350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1351 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1352 }
1353
1354 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1355 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1356 }
1357
1358 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1359 return ret;
1360 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (pk_alg == MBEDTLS_PK_RSA) {
1364 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1365 mbedtls_pk_free(pk);
1366 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367 }
1368 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001370#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001372#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001373 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001374 if ((ret =
1375 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001376 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001377 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001378 p_rng)) != 0) {
1379 mbedtls_pk_free(pk);
1380 return ret;
1381 }
1382 } else
1383#endif
1384 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001385 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1386 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001387 mbedtls_pk_free(pk);
1388 return ret;
1389 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390 }
1391 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001392#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001395 end = p + len;
1396 if (end != (key + keylen)) {
1397 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1398 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1399 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402}
1403
1404/*
1405 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001406 *
1407 * To save space, the decryption happens in-place on the given key buffer.
1408 * Also, while this function may modify the keybuffer, it doesn't own it,
1409 * and instead it is the responsibility of the caller to zeroize and properly
1410 * free it after use.
1411 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001414MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_pk_context *pk,
1416 unsigned char *key, size_t keylen,
1417 const unsigned char *pwd, size_t pwdlen,
1418 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001420 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001422 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1425#if defined(MBEDTLS_PKCS12_C)
1426 mbedtls_cipher_type_t cipher_alg;
1427 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001429 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430
Hanno Becker2aa80a72017-09-07 15:28:45 +01001431 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 end = p + keylen;
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (pwdlen == 0) {
1435 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1436 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437
1438 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001439 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440 *
1441 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1442 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1443 * encryptedData EncryptedData
1444 * }
1445 *
1446 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1447 *
1448 * EncryptedData ::= OCTET STRING
1449 *
1450 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001451 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 }
1457
1458 end = p + len;
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1461 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1462 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1466 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467
Hanno Beckerfab35692017-08-25 13:38:26 +01001468 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469
1470 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001471 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001475 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001476 cipher_alg, md_alg,
1477 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1479 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1480 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001484
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001485 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PKCS12_C */
1488#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001490 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1491 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1493 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1494 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001498
1499 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001502 {
1503 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (decrypted == 0) {
1507 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1508 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001509 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001512
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001513/***********************************************************************
1514 *
1515 * Top-level functions, with format auto-discovery
1516 *
1517 **********************************************************************/
1518
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519/*
1520 * Parse a private key
1521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1523 const unsigned char *key, size_t keylen,
1524 const unsigned char *pwd, size_t pwdlen,
1525 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526{
Janos Follath24eed8d2019-11-22 13:21:35 +00001527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001532#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (keylen == 0) {
1535 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1536 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001537
1538#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_RSA_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 RSA PRIVATE KEY-----",
1548 "-----END RSA PRIVATE KEY-----",
1549 key, pwd, pwdlen, &len);
1550 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (ret == 0) {
1553 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1554 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1555 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1556 pem.buf, pem.buflen)) != 0) {
1557 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558 }
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_pem_free(&pem);
1561 return ret;
1562 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1563 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1564 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1565 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1566 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1567 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001570
Valerio Setti81d75122023-06-14 14:49:33 +02001571#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001572 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001574 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 } else {
1576 ret = mbedtls_pem_read_buffer(&pem,
1577 "-----BEGIN EC PRIVATE KEY-----",
1578 "-----END EC PRIVATE KEY-----",
1579 key, pwd, pwdlen, &len);
1580 }
1581 if (ret == 0) {
1582 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001585 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 pem.buf, pem.buflen,
1587 f_rng, p_rng)) != 0) {
1588 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 }
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 mbedtls_pem_free(&pem);
1592 return ret;
1593 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1594 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1595 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1596 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1597 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1598 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001599 }
Valerio Setti81d75122023-06-14 14:49:33 +02001600#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001602 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001604 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 } else {
1606 ret = mbedtls_pem_read_buffer(&pem,
1607 "-----BEGIN PRIVATE KEY-----",
1608 "-----END PRIVATE KEY-----",
1609 key, NULL, 0, &len);
1610 }
1611 if (ret == 0) {
1612 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1613 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1614 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001615 }
1616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 mbedtls_pem_free(&pem);
1618 return ret;
1619 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1620 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001624 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001626 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 } else {
1628 ret = mbedtls_pem_read_buffer(&pem,
1629 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1630 "-----END ENCRYPTED PRIVATE KEY-----",
1631 key, NULL, 0, &len);
1632 }
1633 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001634 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1635 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001637 }
1638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_pem_free(&pem);
1640 return ret;
1641 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1642 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645#else
1646 ((void) pwd);
1647 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001649
1650 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001651 * At this point we only know it's not a PEM formatted key. Could be any
1652 * of the known DER encoded private key formats
1653 *
1654 * We try the different DER format parsers to see if one passes without
1655 * error
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001659 unsigned char *key_copy;
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1662 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1663 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001666
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001667 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1668 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001669
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001670 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671 }
1672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (ret == 0) {
1674 return 0;
1675 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_pk_free(pk);
1678 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1681 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1686 if (ret == 0) {
1687 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001688 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 mbedtls_pk_free(pk);
1691 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1696 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1697 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1698 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001699 }
1700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 mbedtls_pk_free(pk);
1702 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001704
Valerio Setti81d75122023-06-14 14:49:33 +02001705#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1707 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001708 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 key, keylen, f_rng, p_rng) == 0) {
1710 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001713#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001714
Valerio Setti81d75122023-06-14 14:49:33 +02001715 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001716 * it is ok to leave the PK context initialized but not
1717 * freed: It is the caller's responsibility to call pk_init()
1718 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001719 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001720 * isn't, this leads to mbedtls_pk_free() being called
1721 * twice, once here and once by the caller, but this is
1722 * also ok and in line with the mbedtls_pk_free() calls
1723 * on failed PEM parsing attempts. */
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001726}
1727
1728/*
1729 * Parse a public key
1730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1732 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733{
Janos Follath24eed8d2019-11-22 13:21:35 +00001734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001735 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001736#if defined(MBEDTLS_RSA_C)
1737 const mbedtls_pk_info_t *pk_info;
1738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001742#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 if (keylen == 0) {
1745 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1746 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001747
1748#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001750#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001751 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001753 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 } else {
1755 ret = mbedtls_pem_read_buffer(&pem,
1756 "-----BEGIN RSA PUBLIC KEY-----",
1757 "-----END RSA PUBLIC KEY-----",
1758 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001760
1761 if (ret == 0) {
1762 p = pem.buf;
1763 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1764 mbedtls_pem_free(&pem);
1765 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1766 }
1767
1768 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1769 mbedtls_pem_free(&pem);
1770 return ret;
1771 }
1772
1773 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1774 mbedtls_pk_free(ctx);
1775 }
1776
1777 mbedtls_pem_free(&pem);
1778 return ret;
1779 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1780 mbedtls_pem_free(&pem);
1781 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001782 }
1783#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001784
1785 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001787 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 } else {
1789 ret = mbedtls_pem_read_buffer(&pem,
1790 "-----BEGIN PUBLIC KEY-----",
1791 "-----END PUBLIC KEY-----",
1792 key, NULL, 0, &len);
1793 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001796 /*
1797 * Was PEM encoded
1798 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001799 p = pem.buf;
1800
Jethro Beekman01672442023-04-19 14:08:14 +02001801 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 mbedtls_pem_free(&pem);
1803 return ret;
1804 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1805 mbedtls_pem_free(&pem);
1806 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001807 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001810
1811#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1813 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001815
1816 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1817 return ret;
1818 }
1819
1820 p = (unsigned char *) key;
1821 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1822 if (ret == 0) {
1823 return ret;
1824 }
1825 mbedtls_pk_free(ctx);
1826 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1827 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1828 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001829 }
1830#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001831 p = (unsigned char *) key;
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001836}
1837
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001838/***********************************************************************
1839 *
1840 * Top-level functions, with filesystem support
1841 *
1842 **********************************************************************/
1843
1844#if defined(MBEDTLS_FS_IO)
1845/*
1846 * Load all data from a file into a given buffer.
1847 *
1848 * The file is expected to contain either PEM or DER encoded data.
1849 * A terminating null byte is always appended. It is included in the announced
1850 * length only if the data looks like it is PEM encoded.
1851 */
1852int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1853{
1854 FILE *f;
1855 long size;
1856
1857 if ((f = fopen(path, "rb")) == NULL) {
1858 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1859 }
1860
1861 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1862 mbedtls_setbuf(f, NULL);
1863
1864 fseek(f, 0, SEEK_END);
1865 if ((size = ftell(f)) == -1) {
1866 fclose(f);
1867 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1868 }
1869 fseek(f, 0, SEEK_SET);
1870
1871 *n = (size_t) size;
1872
1873 if (*n + 1 == 0 ||
1874 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1875 fclose(f);
1876 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1877 }
1878
1879 if (fread(*buf, 1, *n, f) != *n) {
1880 fclose(f);
1881
1882 mbedtls_zeroize_and_free(*buf, *n);
1883
1884 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1885 }
1886
1887 fclose(f);
1888
1889 (*buf)[*n] = '\0';
1890
1891 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1892 ++*n;
1893 }
1894
1895 return 0;
1896}
1897
1898/*
1899 * Load and parse a private key
1900 */
1901int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1902 const char *path, const char *pwd,
1903 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1904{
1905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1906 size_t n;
1907 unsigned char *buf;
1908
1909 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1910 return ret;
1911 }
1912
1913 if (pwd == NULL) {
1914 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1915 } else {
1916 ret = mbedtls_pk_parse_key(ctx, buf, n,
1917 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1918 }
1919
1920 mbedtls_zeroize_and_free(buf, n);
1921
1922 return ret;
1923}
1924
1925/*
1926 * Load and parse a public key
1927 */
1928int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1929{
1930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1931 size_t n;
1932 unsigned char *buf;
1933
1934 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1935 return ret;
1936 }
1937
1938 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1939
1940 mbedtls_zeroize_and_free(buf, n);
1941
1942 return ret;
1943}
1944#endif /* MBEDTLS_FS_IO */
1945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_PK_PARSE_C */