blob: 9d87a715068a0e4ab424ce422884398a90cd19ee [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
159 * (as otherwise we're 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é-Gonnardfac98192023-07-27 09:19:42 +0200253 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200254#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200255 mbedtls_ecp_keypair ecp_key;
256 mbedtls_ecp_group_id ecp_group_id;
257 int ret;
258
259 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
260
261 mbedtls_ecp_keypair_init(&ecp_key);
262 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
263 if (ret != 0) {
264 return ret;
265 }
266 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200267 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200268 if (ret != 0) {
269 goto exit;
270 }
271 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
272 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200273 &pk->pub_raw_len, pk->pub_raw,
274 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200275
276exit:
277 mbedtls_ecp_keypair_free(&ecp_key);
278 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200279#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
280}
281#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
282
283/*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200284 * Set the public key.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200285 *
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200286 * [in/out] pk: in: must have its group set, see pk_ecc_set_group().
287 * out: will have the public key set.
288 * [in] pub, pub_len: the raw public key (an ECPoint).
Manuel Pégourié-Gonnardfac98192023-07-27 09:19:42 +0200289 *
290 * Return:
291 * - 0 on success;
292 * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
293 * but not supported;
294 * - another error code otherwise.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200295 */
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200296static int pk_ecc_set_pubkey(mbedtls_pk_context *pk,
297 const unsigned char *pub, size_t pub_len)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200298{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200299#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200300
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200301 /* Load the key */
302 if (*pub == 0x04) {
303 /* Uncompressed format, directly supported by PSA */
304 if (pub_len > sizeof(pk->pub_raw)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200305 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
306 }
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200307 memcpy(pk->pub_raw, pub, pub_len);
308 pk->pub_raw_len = pub_len;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200309 } else {
310 /* Other format, try the fallback */
311 int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
312 if (ret != 0) {
313 return ret;
314 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200316
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200317 /* Validate the key by trying to import it */
318 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
319 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
320
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200321 psa_set_key_usage_flags(&key_attrs, 0);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200322 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
323 psa_set_key_bits(&key_attrs, pk->ec_bits);
324
325 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200326 &key_id) != PSA_SUCCESS) ||
327 (psa_destroy_key(key_id) != PSA_SUCCESS)) {
328 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200330
331 return 0;
332
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200333#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200334
335 int ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200336 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200337 ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
338 if (ret != 0) {
339 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 }
Manuel Pégourié-Gonnardff72ea92023-07-26 23:56:05 +0200341 return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
342
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200343#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200344}
345
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200346/***********************************************************************
347 *
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200348 * Low-level ECC parsing: optional support for SpecifiedECDomain
349 *
350 * There are two functions here that are used by the rest of the code:
351 * - pk_ecc_tag_may_be_speficied_ec_domain()
352 * - pk_ecc_group_id_from_specified()
353 *
354 * All the other functions are internal to this section.
355 *
356 * The two "public" functions have a dummy variant provided
357 * in configs without MBEDTLS_PK_PARSE_EC_EXTENDED. This acts as an
358 * abstraction layer for this macro, which should not appear outside
359 * this section.
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200360 *
361 **********************************************************************/
362
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200363#if !defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
364/* See the "real" version for documentation */
365static int pk_ecc_tag_may_be_specified_ec_domain(int tag)
366{
367 (void) tag;
368 return 0;
369}
370
371/* See the "real" version for documentation */
372static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
373 mbedtls_ecp_group_id *grp_id)
374{
375 (void) params;
376 (void) grp_id;
377 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
378}
379#else /* MBEDTLS_PK_PARSE_EC_EXTENDED */
380/*
381 * Tell if the passed tag might be the start of SpecifiedECDomain
382 * (that is, a sequence).
383 */
384static int pk_ecc_tag_may_be_specified_ec_domain(int tag)
385{
386 return tag == (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
387}
388
Paul Bakker1a7550a2013-09-15 13:01:22 +0200389/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
391 * WARNING: the resulting group should only be used with
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200392 * pk_ecc_group_id_from_specified(), since its base point may not be set correctly
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393 * if it was encoded compressed.
394 *
395 * SpecifiedECDomain ::= SEQUENCE {
396 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
397 * fieldID FieldID {{FieldTypes}},
398 * curve Curve,
399 * base ECPoint,
400 * order INTEGER,
401 * cofactor INTEGER OPTIONAL,
402 * hash HashAlgorithm OPTIONAL,
403 * ...
404 * }
405 *
406 * We only support prime-field as field type, and ignore hash and cofactor.
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409{
Janos Follath24eed8d2019-11-22 13:21:35 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200412 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100413 const unsigned char *end_field, *end_curve;
414 size_t len;
415 int ver;
416
417 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
420 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (ver < 1 || ver > 3) {
423 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
424 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425
426 /*
427 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
428 * fieldType FIELD-ID.&id({IOSet}),
429 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
430 * }
431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
433 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
434 return ret;
435 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436
437 end_field = p + len;
438
439 /*
440 * FIELD-ID ::= TYPE-IDENTIFIER
441 * FieldTypes FIELD-ID ::= {
442 * { Prime-p IDENTIFIED BY prime-field } |
443 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
444 * }
445 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
448 return ret;
449 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
452 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
453 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454 }
455
456 p += len;
457
458 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
460 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
461 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (p != end_field) {
466 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
467 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
468 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100469
470 /*
471 * Curve ::= SEQUENCE {
472 * a FieldElement,
473 * b FieldElement,
474 * seed BIT STRING OPTIONAL
475 * -- Shall be present if used in SpecifiedECDomain
476 * -- with version equal to ecdpVer2 or ecdpVer3
477 * }
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
480 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
481 return ret;
482 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483
484 end_curve = p + len;
485
486 /*
487 * FieldElement ::= OCTET STRING
488 * containing an integer in the case of a prime field
489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
491 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100493 }
494
495 p += len;
496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
498 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
499 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100500 }
501
502 p += len;
503
504 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100506 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (p != end_curve) {
510 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
511 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
512 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100513
514 /*
515 * ECPoint ::= OCTET STRING
516 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
518 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
519 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
522 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100523 /*
524 * If we can't read the point because it's compressed, cheat by
525 * reading only the X coordinate and the parity bit of Y.
526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
528 (p[0] != 0x02 && p[0] != 0x03) ||
529 len != mbedtls_mpi_size(&grp->P) + 1 ||
530 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
531 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
532 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
533 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100534 }
535 }
536
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100537 p += len;
538
539 /*
540 * order INTEGER
541 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
543 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
544 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100547
548 /*
549 * Allow optional elements by purposefully not enforcing p == end here.
550 */
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100553}
554
555/*
556 * Find the group id associated with an (almost filled) group as generated by
557 * pk_group_from_specified(), or return an error if unknown.
558 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559static 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 +0100560{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100561 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_ecp_group ref;
563 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100568 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 mbedtls_ecp_group_free(&ref);
570 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100571
572 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
574 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
575 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
576 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
577 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
578 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
579 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100580 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 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 +0100582 break;
583 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100584 }
585
586cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100588
589 *grp_id = *id;
590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100596}
597
598/*
599 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
600 */
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200601static int pk_ecc_group_id_from_specified(const mbedtls_asn1_buf *params,
602 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100603{
Janos Follath24eed8d2019-11-22 13:21:35 +0000604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100610 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100614
615cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000616 /* The API respecting lifecycle for mbedtls_ecp_group struct is
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200617 * _init(), _load() and _free(). In pk_ecc_group_id_from_specified() the
Minos Galanakis8692ec82023-01-20 15:27:32 +0000618 * temporary grp breaks that flow and it's members are populated
619 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
620 * which is assuming a group populated by _setup() may not clean-up
621 * properly -> Manually free it's members.
622 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000623 mbedtls_mpi_free(&grp.N);
624 mbedtls_mpi_free(&grp.P);
625 mbedtls_mpi_free(&grp.A);
626 mbedtls_mpi_free(&grp.B);
627 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100632
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200633/***********************************************************************
634 *
635 * Unsorted (yet!) from this point on until the next section header
636 *
637 **********************************************************************/
Valerio Setti4064dbb2023-05-17 15:33:07 +0200638
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200639/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200640 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200641 * ECParameters ::= CHOICE {
642 * namedCurve OBJECT IDENTIFIER
643 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
644 * -- implicitCurve NULL
645 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200646 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200647static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
648 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200649{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200651
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200652 if (end - *p < 1) {
653 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
654 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200655 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200656
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200657 /* Acceptable tags: OID for namedCurve, or specifiedECDomain */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200658 params->tag = **p;
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200659 if (params->tag != MBEDTLS_ASN1_OID &&
660 !pk_ecc_tag_may_be_specified_ec_domain(params->tag)) {
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
662 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
663 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200664
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200665 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200666 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
667 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200668
669 params->p = *p;
670 *p += params->len;
671
672 if (*p != end) {
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
674 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
675 }
676
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200677 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200678}
679
680/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200681 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100682 *
683 * ECParameters ::= CHOICE {
684 * namedCurve OBJECT IDENTIFIER
685 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
686 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200688static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689{
Janos Follath24eed8d2019-11-22 13:21:35 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (params->tag == MBEDTLS_ASN1_OID) {
694 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
695 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
696 }
697 } else {
Manuel Pégourié-Gonnard12ea63a2023-07-27 12:20:16 +0200698 ret = pk_ecc_group_id_from_specified(params, &grp_id);
699 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return ret;
701 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100702 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200704 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200705}
706
Jethro Beekman01672442023-04-19 14:08:14 +0200707#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
708
709/*
710 * Load an RFC8410 EC key, which doesn't have any parameters
711 */
712static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
713 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200714 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200715{
716 if (params->tag != 0 || params->len != 0) {
717 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
718 }
719
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200720 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200721}
722
723/*
724 * Parse an RFC 8410 encoded private EC key
725 *
726 * CurvePrivateKey ::= OCTET STRING
727 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200728static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200729 unsigned char *key, size_t keylen, const unsigned char *end,
730 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
731{
732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
733 size_t len;
734
735 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
736 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
737 }
738
739 if (key + len != end) {
740 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
741 }
742
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200743 /*
744 * Load the private key
745 */
746 ret = pk_ecc_set_key(pk, key, len);
747 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200748 return ret;
749 }
Jethro Beekman01672442023-04-19 14:08:14 +0200750
Valerio Setti4064dbb2023-05-17 15:33:07 +0200751 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
752 * which never contain a public key. As such, derive the public key
753 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200754 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200755 return ret;
756 }
757
Jethro Beekman01672442023-04-19 14:08:14 +0200758 return 0;
759}
760#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200761
Valerio Setti81d75122023-06-14 14:49:33 +0200762#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765/*
766 * RSAPublicKey ::= SEQUENCE {
767 * modulus INTEGER, -- n
768 * publicExponent INTEGER -- e
769 * }
770 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771static int pk_get_rsapubkey(unsigned char **p,
772 const unsigned char *end,
773 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774{
Janos Follath24eed8d2019-11-22 13:21:35 +0000775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776 size_t len;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
779 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
780 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
781 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (*p + len != end) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
785 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
786 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100788 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
790 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
791 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
794 NULL, 0, NULL, 0)) != 0) {
795 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
796 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100797
798 *p += len;
799
800 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
802 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
803 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
806 NULL, 0, *p, len)) != 0) {
807 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
808 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809
810 *p += len;
811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (mbedtls_rsa_complete(rsa) != 0 ||
813 mbedtls_rsa_check_pubkey(rsa) != 0) {
814 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000815 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (*p != end) {
818 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
819 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
820 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825
826/* Get a PK algorithm identifier
827 *
828 * AlgorithmIdentifier ::= SEQUENCE {
829 * algorithm OBJECT IDENTIFIER,
830 * parameters ANY DEFINED BY algorithm OPTIONAL }
831 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static int pk_get_pk_alg(unsigned char **p,
833 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200834 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
835 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836{
Janos Follath24eed8d2019-11-22 13:21:35 +0000837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
843 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
844 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845
Jethro Beekman01672442023-04-19 14:08:14 +0200846 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200847#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200848 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
849 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
850 if (ret == 0) {
851 *pk_alg = MBEDTLS_PK_ECKEY;
852 }
853 }
854#else
855 (void) ec_grp_id;
856#endif
857 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
859 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
861 /*
862 * No parameters with RSA (only for EC)
863 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (*pk_alg == MBEDTLS_PK_RSA &&
865 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
866 params->len != 0)) {
867 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868 }
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871}
872
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200873/* Helper for Montgomery curves */
874#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
875#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
876 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
877#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
878
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879/*
880 * SubjectPublicKeyInfo ::= SEQUENCE {
881 * algorithm AlgorithmIdentifier,
882 * subjectPublicKey BIT STRING }
883 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
885 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886{
Janos Follath24eed8d2019-11-22 13:21:35 +0000887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_asn1_buf alg_params;
890 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200891 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
895 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
896 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 }
898
899 end = *p + len;
900
Jethro Beekman01672442023-04-19 14:08:14 +0200901 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return ret;
903 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
906 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
907 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (*p + len != end) {
910 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
911 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
915 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
916 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
919 return ret;
920 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (pk_alg == MBEDTLS_PK_RSA) {
924 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200927#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200929#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200930 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200931 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200932 } else
933#endif
934 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200935 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200936 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200938 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
939 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200942#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (ret == 0 && *p != end) {
946 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
947 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
948 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ret != 0) {
951 mbedtls_pk_free(pk);
952 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200955}
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100959 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
960 *
961 * The value zero is:
962 * - never a valid value for an RSA parameter
963 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
964 *
965 * Since values can't be omitted in PKCS#1, passing a zero value to
966 * rsa_complete() would be incorrect, so reject zero values early.
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static int asn1_get_nonzero_mpi(unsigned char **p,
969 const unsigned char *end,
970 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100971{
972 int ret;
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 ret = mbedtls_asn1_get_mpi(p, end, X);
975 if (ret != 0) {
976 return ret;
977 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
980 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
981 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100984}
985
986/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987 * Parse a PKCS#1 encoded private RSA key
988 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
990 const unsigned char *key,
991 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100993 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200994 size_t len;
995 unsigned char *p, *end;
996
Hanno Beckerefa14e82017-10-11 19:45:19 +0100997 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100999
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000 p = (unsigned char *) key;
1001 end = p + keylen;
1002
1003 /*
1004 * This function parses the RSAPrivateKey (PKCS#1)
1005 *
1006 * RSAPrivateKey ::= SEQUENCE {
1007 * version Version,
1008 * modulus INTEGER, -- n
1009 * publicExponent INTEGER, -- e
1010 * privateExponent INTEGER, -- d
1011 * prime1 INTEGER, -- p
1012 * prime2 INTEGER, -- q
1013 * exponent1 INTEGER, -- d mod (p-1)
1014 * exponent2 INTEGER, -- d mod (q-1)
1015 * coefficient INTEGER, -- (inverse of q) mod p
1016 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1017 * }
1018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1020 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1021 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 }
1023
1024 end = p + len;
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1027 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028 }
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (version != 0) {
1031 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032 }
1033
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001034 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1036 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1037 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001038 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001041 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1043 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1044 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001045 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001047
1048 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1050 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1051 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001052 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001054
1055 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1057 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1058 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001059 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001061
1062 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1064 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1065 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001066 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001068
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001069#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001070 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1072 * that they can be easily recomputed from D, P and Q. However by
1073 * parsing them from the PKCS1 structure it is possible to avoid
1074 * recalculating them which both reduces the overhead of loading
1075 * RSA private keys into memory and also avoids side channels which
1076 * can arise when computing those values, since all of D, P, and Q
1077 * are secret. See https://eprint.iacr.org/2020/055 for a
1078 * description of one such attack.
1079 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001080
Jack Lloyd80cc8112020-01-22 17:34:29 -05001081 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1083 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1084 goto cleanup;
1085 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001086
1087 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1089 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1090 goto cleanup;
1091 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001092
1093 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1095 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1096 goto cleanup;
1097 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001098
Jack Lloyd60239752020-01-27 17:53:36 -05001099#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001100 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1102 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1103 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1104 goto cleanup;
1105 }
Jack Lloyd60239752020-01-27 17:53:36 -05001106#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001107
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001108 /* rsa_complete() doesn't complete anything with the default
1109 * implementation but is still called:
1110 * - for the benefit of alternative implementation that may want to
1111 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1112 * - as is also sanity-checks the key
1113 *
1114 * Furthermore, we also check the public part for consistency with
1115 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1118 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001119 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001120 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (p != end) {
1123 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1124 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 }
1126
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001127cleanup:
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001132 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if ((ret & 0xff80) == 0) {
1134 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1135 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001136 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
Valerio Setti81d75122023-06-14 14:49:33 +02001146#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147/*
1148 * Parse a SEC1 encoded private EC key
1149 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001150static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 const unsigned char *key, size_t keylen,
1152 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153{
Janos Follath24eed8d2019-11-22 13:21:35 +00001154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001155 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001156 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001157 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001159 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 unsigned char *end = p + keylen;
1161 unsigned char *end2;
1162
1163 /*
1164 * RFC 5915, or SEC1 Appendix C.4
1165 *
1166 * ECPrivateKey ::= SEQUENCE {
1167 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1168 * privateKey OCTET STRING,
1169 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1170 * publicKey [1] BIT STRING OPTIONAL
1171 * }
1172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1174 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 }
1177
1178 end = p + len;
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1182 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (version != 1) {
1185 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1186 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1190 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191
Valerio Setti6b062ee2023-06-30 17:32:57 +02001192 /* Keep a reference to the position fo the private key. It will be used
1193 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001194 d = p;
1195 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001196
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 p += len;
1198
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001199 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201 /*
1202 * Is 'parameters' present?
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1205 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1206 0)) == 0) {
1207 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001208 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001213 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001214 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001215
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001216 /*
1217 * Load the private key
1218 */
1219 ret = pk_ecc_set_key(pk, d, d_len);
1220 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001221 return ret;
1222 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001225 /*
1226 * Is 'publickey' present? If not, or if we can't read it (eg because it
1227 * is compressed), create it from the private key.
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1230 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1231 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001232 end2 = p + len;
1233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1235 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1236 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (p + len != end2) {
1239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1240 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1241 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001242
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001243 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001244 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001246 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001247 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001248 * is if the point format is not recognized.
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1251 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1252 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001256 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001258
Valerio Setti34f67552023-04-03 15:19:18 +02001259 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001260 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001261 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001262 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001263 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266}
Valerio Setti81d75122023-06-14 14:49:33 +02001267#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001269/***********************************************************************
1270 *
1271 * PKCS#8 parsing functions
1272 *
1273 **********************************************************************/
1274
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275/*
1276 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001277 *
1278 * Notes:
1279 *
1280 * - This function does not own the key buffer. It is the
1281 * responsibility of the caller to take care of zeroizing
1282 * and freeing it after use.
1283 *
1284 * - The function is responsible for freeing the provided
1285 * PK context on failure.
1286 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 */
1288static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_pk_context *pk,
1290 const unsigned char *key, size_t keylen,
1291 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292{
1293 int ret, version;
1294 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 unsigned char *p = (unsigned char *) key;
1297 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001299 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
Valerio Setti81d75122023-06-14 14:49:33 +02001302#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001303 (void) f_rng;
1304 (void) p_rng;
1305#endif
1306
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001308 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 *
1310 * PrivateKeyInfo ::= SEQUENCE {
1311 * version Version,
1312 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1313 * privateKey PrivateKey,
1314 * attributes [0] IMPLICIT Attributes OPTIONAL }
1315 *
1316 * Version ::= INTEGER
1317 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1318 * PrivateKey ::= OCTET STRING
1319 *
1320 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1321 */
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1324 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1325 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 }
1327
1328 end = p + len;
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001332 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (version != 0) {
1335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1336 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
Jethro Beekman01672442023-04-19 14:08:14 +02001338 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 return ret;
1340 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (len < 1) {
1347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1348 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1349 }
1350
1351 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1352 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1353 }
1354
1355 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1356 return ret;
1357 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (pk_alg == MBEDTLS_PK_RSA) {
1361 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1362 mbedtls_pk_free(pk);
1363 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 }
1365 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001367#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001369#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001370 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001371 if ((ret =
1372 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001373 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001374 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001375 p_rng)) != 0) {
1376 mbedtls_pk_free(pk);
1377 return ret;
1378 }
1379 } else
1380#endif
1381 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001382 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1383 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001384 mbedtls_pk_free(pk);
1385 return ret;
1386 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387 }
1388 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001389#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001392 end = p + len;
1393 if (end != (key + keylen)) {
1394 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1395 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1396 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399}
1400
1401/*
1402 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001403 *
1404 * To save space, the decryption happens in-place on the given key buffer.
1405 * Also, while this function may modify the keybuffer, it doesn't own it,
1406 * and instead it is the responsibility of the caller to zeroize and properly
1407 * free it after use.
1408 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001411MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 mbedtls_pk_context *pk,
1413 unsigned char *key, size_t keylen,
1414 const unsigned char *pwd, size_t pwdlen,
1415 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001417 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001419 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1422#if defined(MBEDTLS_PKCS12_C)
1423 mbedtls_cipher_type_t cipher_alg;
1424 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001426 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427
Hanno Becker2aa80a72017-09-07 15:28:45 +01001428 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429 end = p + keylen;
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (pwdlen == 0) {
1432 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1433 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
1435 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001436 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 *
1438 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1439 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1440 * encryptedData EncryptedData
1441 * }
1442 *
1443 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1444 *
1445 * EncryptedData ::= OCTET STRING
1446 *
1447 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001448 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1451 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 }
1454
1455 end = p + len;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1459 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1462 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1463 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464
Hanno Beckerfab35692017-08-25 13:38:26 +01001465 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466
1467 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001468 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001472 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001473 cipher_alg, md_alg,
1474 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1476 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1477 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001481
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001482 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_PKCS12_C */
1485#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001487 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1488 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1490 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1491 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001495
1496 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001499 {
1500 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001501 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (decrypted == 0) {
1504 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1505 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001506 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001509
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001510/***********************************************************************
1511 *
1512 * Top-level functions, with format auto-discovery
1513 *
1514 **********************************************************************/
1515
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516/*
1517 * Parse a private key
1518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1520 const unsigned char *key, size_t keylen,
1521 const unsigned char *pwd, size_t pwdlen,
1522 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523{
Janos Follath24eed8d2019-11-22 13:21:35 +00001524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001529#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (keylen == 0) {
1532 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1533 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001534
1535#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001539 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001541 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 } else {
1543 ret = mbedtls_pem_read_buffer(&pem,
1544 "-----BEGIN RSA PRIVATE KEY-----",
1545 "-----END RSA PRIVATE KEY-----",
1546 key, pwd, pwdlen, &len);
1547 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (ret == 0) {
1550 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1551 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1552 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1553 pem.buf, pem.buflen)) != 0) {
1554 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555 }
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_pem_free(&pem);
1558 return ret;
1559 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1560 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1561 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1562 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1563 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1564 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567
Valerio Setti81d75122023-06-14 14:49:33 +02001568#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001569 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001571 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 } else {
1573 ret = mbedtls_pem_read_buffer(&pem,
1574 "-----BEGIN EC PRIVATE KEY-----",
1575 "-----END EC PRIVATE KEY-----",
1576 key, pwd, pwdlen, &len);
1577 }
1578 if (ret == 0) {
1579 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001582 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 pem.buf, pem.buflen,
1584 f_rng, p_rng)) != 0) {
1585 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001586 }
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_pem_free(&pem);
1589 return ret;
1590 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1591 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1592 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1593 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1594 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1595 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596 }
Valerio Setti81d75122023-06-14 14:49:33 +02001597#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001599 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001601 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 } else {
1603 ret = mbedtls_pem_read_buffer(&pem,
1604 "-----BEGIN PRIVATE KEY-----",
1605 "-----END PRIVATE KEY-----",
1606 key, NULL, 0, &len);
1607 }
1608 if (ret == 0) {
1609 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1610 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1611 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001612 }
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_pem_free(&pem);
1615 return ret;
1616 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1617 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001618 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001621 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001623 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 } else {
1625 ret = mbedtls_pem_read_buffer(&pem,
1626 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1627 "-----END ENCRYPTED PRIVATE KEY-----",
1628 key, NULL, 0, &len);
1629 }
1630 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001631 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1632 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001634 }
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_pem_free(&pem);
1637 return ret;
1638 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1639 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642#else
1643 ((void) pwd);
1644 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
1647 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001648 * At this point we only know it's not a PEM formatted key. Could be any
1649 * of the known DER encoded private key formats
1650 *
1651 * We try the different DER format parsers to see if one passes without
1652 * error
1653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001656 unsigned char *key_copy;
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1659 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1660 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001663
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001664 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1665 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001666
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001667 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001668 }
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ret == 0) {
1671 return 0;
1672 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_pk_free(pk);
1675 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1678 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1683 if (ret == 0) {
1684 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001685 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 mbedtls_pk_free(pk);
1688 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1693 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1694 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1695 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696 }
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 mbedtls_pk_free(pk);
1699 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701
Valerio Setti81d75122023-06-14 14:49:33 +02001702#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1704 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001705 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 key, keylen, f_rng, p_rng) == 0) {
1707 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001708 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001710#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001711
Valerio Setti81d75122023-06-14 14:49:33 +02001712 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001713 * it is ok to leave the PK context initialized but not
1714 * freed: It is the caller's responsibility to call pk_init()
1715 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001716 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001717 * isn't, this leads to mbedtls_pk_free() being called
1718 * twice, once here and once by the caller, but this is
1719 * also ok and in line with the mbedtls_pk_free() calls
1720 * on failed PEM parsing attempts. */
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001723}
1724
1725/*
1726 * Parse a public key
1727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1729 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730{
Janos Follath24eed8d2019-11-22 13:21:35 +00001731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001732 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001733#if defined(MBEDTLS_RSA_C)
1734 const mbedtls_pk_info_t *pk_info;
1735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001737 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001739#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (keylen == 0) {
1742 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1743 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001744
1745#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001747#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001748 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001750 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 } else {
1752 ret = mbedtls_pem_read_buffer(&pem,
1753 "-----BEGIN RSA PUBLIC KEY-----",
1754 "-----END RSA PUBLIC KEY-----",
1755 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001756 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001757
1758 if (ret == 0) {
1759 p = pem.buf;
1760 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1761 mbedtls_pem_free(&pem);
1762 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1763 }
1764
1765 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1766 mbedtls_pem_free(&pem);
1767 return ret;
1768 }
1769
1770 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1771 mbedtls_pk_free(ctx);
1772 }
1773
1774 mbedtls_pem_free(&pem);
1775 return ret;
1776 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1777 mbedtls_pem_free(&pem);
1778 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001779 }
1780#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001781
1782 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001784 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 } else {
1786 ret = mbedtls_pem_read_buffer(&pem,
1787 "-----BEGIN PUBLIC KEY-----",
1788 "-----END PUBLIC KEY-----",
1789 key, NULL, 0, &len);
1790 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001793 /*
1794 * Was PEM encoded
1795 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001796 p = pem.buf;
1797
Jethro Beekman01672442023-04-19 14:08:14 +02001798 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 mbedtls_pem_free(&pem);
1800 return ret;
1801 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1802 mbedtls_pem_free(&pem);
1803 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001804 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001807
1808#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1810 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001812
1813 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1814 return ret;
1815 }
1816
1817 p = (unsigned char *) key;
1818 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1819 if (ret == 0) {
1820 return ret;
1821 }
1822 mbedtls_pk_free(ctx);
1823 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1824 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1825 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001826 }
1827#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001828 p = (unsigned char *) key;
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001833}
1834
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001835/***********************************************************************
1836 *
1837 * Top-level functions, with filesystem support
1838 *
1839 **********************************************************************/
1840
1841#if defined(MBEDTLS_FS_IO)
1842/*
1843 * Load all data from a file into a given buffer.
1844 *
1845 * The file is expected to contain either PEM or DER encoded data.
1846 * A terminating null byte is always appended. It is included in the announced
1847 * length only if the data looks like it is PEM encoded.
1848 */
1849int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1850{
1851 FILE *f;
1852 long size;
1853
1854 if ((f = fopen(path, "rb")) == NULL) {
1855 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1856 }
1857
1858 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1859 mbedtls_setbuf(f, NULL);
1860
1861 fseek(f, 0, SEEK_END);
1862 if ((size = ftell(f)) == -1) {
1863 fclose(f);
1864 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1865 }
1866 fseek(f, 0, SEEK_SET);
1867
1868 *n = (size_t) size;
1869
1870 if (*n + 1 == 0 ||
1871 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1872 fclose(f);
1873 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1874 }
1875
1876 if (fread(*buf, 1, *n, f) != *n) {
1877 fclose(f);
1878
1879 mbedtls_zeroize_and_free(*buf, *n);
1880
1881 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1882 }
1883
1884 fclose(f);
1885
1886 (*buf)[*n] = '\0';
1887
1888 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1889 ++*n;
1890 }
1891
1892 return 0;
1893}
1894
1895/*
1896 * Load and parse a private key
1897 */
1898int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1899 const char *path, const char *pwd,
1900 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1901{
1902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1903 size_t n;
1904 unsigned char *buf;
1905
1906 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1907 return ret;
1908 }
1909
1910 if (pwd == NULL) {
1911 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1912 } else {
1913 ret = mbedtls_pk_parse_key(ctx, buf, n,
1914 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1915 }
1916
1917 mbedtls_zeroize_and_free(buf, n);
1918
1919 return ret;
1920}
1921
1922/*
1923 * Load and parse a public key
1924 */
1925int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1926{
1927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1928 size_t n;
1929 unsigned char *buf;
1930
1931 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1932 return ret;
1933 }
1934
1935 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1936
1937 mbedtls_zeroize_and_free(buf, n);
1938
1939 return ret;
1940}
1941#endif /* MBEDTLS_FS_IO */
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#endif /* MBEDTLS_PK_PARSE_C */