blob: d12984fb07d35096f50242274f65d184df152f36 [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 *
348 * Unsorted (yet!) from this point on until the next section header
349 *
350 **********************************************************************/
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200353/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
355 * WARNING: the resulting group should only be used with
356 * pk_group_id_from_specified(), since its base point may not be set correctly
357 * if it was encoded compressed.
358 *
359 * SpecifiedECDomain ::= SEQUENCE {
360 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
361 * fieldID FieldID {{FieldTypes}},
362 * curve Curve,
363 * base ECPoint,
364 * order INTEGER,
365 * cofactor INTEGER OPTIONAL,
366 * hash HashAlgorithm OPTIONAL,
367 * ...
368 * }
369 *
370 * We only support prime-field as field type, and ignore hash and cofactor.
371 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373{
Janos Follath24eed8d2019-11-22 13:21:35 +0000374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375 unsigned char *p = params->p;
Jethro Beekman01672442023-04-19 14:08:14 +0200376 const unsigned char *const end = params->p + params->len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377 const unsigned char *end_field, *end_curve;
378 size_t len;
379 int ver;
380
381 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) {
383 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
384 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ver < 1 || ver > 3) {
387 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
388 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
390 /*
391 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
392 * fieldType FIELD-ID.&id({IOSet}),
393 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
394 * }
395 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
397 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
398 return ret;
399 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400
401 end_field = p + len;
402
403 /*
404 * FIELD-ID ::= TYPE-IDENTIFIER
405 * FieldTypes FIELD-ID ::= {
406 * { Prime-p IDENTIFIED BY prime-field } |
407 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
408 * }
409 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
410 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) {
412 return ret;
413 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
416 memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
417 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418 }
419
420 p += len;
421
422 /* Prime-p ::= INTEGER -- Field of size p. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) {
424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
425 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 grp->pbits = mbedtls_mpi_bitlen(&grp->P);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (p != end_field) {
430 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
431 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
432 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
434 /*
435 * Curve ::= SEQUENCE {
436 * a FieldElement,
437 * b FieldElement,
438 * seed BIT STRING OPTIONAL
439 * -- Shall be present if used in SpecifiedECDomain
440 * -- with version equal to ecdpVer2 or ecdpVer3
441 * }
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
444 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
445 return ret;
446 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447
448 end_curve = p + len;
449
450 /*
451 * FieldElement ::= OCTET STRING
452 * containing an integer in the case of a prime field
453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
455 (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
456 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457 }
458
459 p += len;
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
462 (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464 }
465
466 p += len;
467
468 /* Ignore seed BIT STRING OPTIONAL */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (p != end_curve) {
474 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
475 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
476 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100477
478 /*
479 * ECPoint ::= OCTET STRING
480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
482 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
483 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G,
486 (const unsigned char *) p, len)) != 0) {
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100487 /*
488 * If we can't read the point because it's compressed, cheat by
489 * reading only the X coordinate and the parity bit of Y.
490 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
492 (p[0] != 0x02 && p[0] != 0x03) ||
493 len != mbedtls_mpi_size(&grp->P) + 1 ||
494 mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
495 mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
496 mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
497 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100498 }
499 }
500
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100501 p += len;
502
503 /*
504 * order INTEGER
505 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) {
507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
508 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 grp->nbits = mbedtls_mpi_bitlen(&grp->N);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100511
512 /*
513 * Allow optional elements by purposefully not enforcing p == end here.
514 */
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return 0;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100517}
518
519/*
520 * Find the group id associated with an (almost filled) group as generated by
521 * pk_group_from_specified(), or return an error if unknown.
522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523static 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 +0100524{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100525 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_ecp_group ref;
527 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_ecp_group_init(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100532 /* Load the group associated to that id */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 mbedtls_ecp_group_free(&ref);
534 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id));
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100535
536 /* Compare to the group we were given, starting with easy tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
538 mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
539 mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
540 mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
541 mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
542 mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
543 mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100544 /* For Y we may only know the parity bit, so compare only that */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 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 +0100546 break;
547 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100548 }
549
550cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_ecp_group_free(&ref);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100552
553 *grp_id = *id;
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100560}
561
562/*
563 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
564 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100565static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
566 mbedtls_ecp_group_id *grp_id)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100567{
Janos Follath24eed8d2019-11-22 13:21:35 +0000568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_ecp_group_init(&grp);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if ((ret = pk_group_from_specified(params, &grp)) != 0) {
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100574 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 ret = pk_group_id_from_group(&grp, grp_id);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100578
579cleanup:
Minos Galanakis8692ec82023-01-20 15:27:32 +0000580 /* The API respecting lifecycle for mbedtls_ecp_group struct is
581 * _init(), _load() and _free(). In pk_group_id_from_specified() the
582 * temporary grp breaks that flow and it's members are populated
583 * by pk_group_id_from_group(). As such mbedtls_ecp_group_free()
584 * which is assuming a group populated by _setup() may not clean-up
585 * properly -> Manually free it's members.
586 */
Minos Galanakisc8e381a2023-01-19 16:08:34 +0000587 mbedtls_mpi_free(&grp.N);
588 mbedtls_mpi_free(&grp.P);
589 mbedtls_mpi_free(&grp.A);
590 mbedtls_mpi_free(&grp.B);
591 mbedtls_ecp_point_free(&grp.G);
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100596
Valerio Setti4064dbb2023-05-17 15:33:07 +0200597
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200598/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200599 *
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200600 * ECParameters ::= CHOICE {
601 * namedCurve OBJECT IDENTIFIER
602 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
603 * -- implicitCurve NULL
604 * }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200605 */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200606static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
607 mbedtls_asn1_buf *params)
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200608{
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200610
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200611 if (end - *p < 1) {
612 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
613 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200614 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200615
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200616 /* Tag may be either OID or SEQUENCE */
617 params->tag = **p;
618 if (params->tag != MBEDTLS_ASN1_OID
619#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
620 && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
621#endif
622 ) {
623 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
624 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
625 }
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200626
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200627 if ((ret = mbedtls_asn1_get_tag(p, end, &params->len, params->tag)) != 0) {
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200628 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
629 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200630
631 params->p = *p;
632 *p += params->len;
633
634 if (*p != end) {
635 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
636 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
637 }
638
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200639 return 0;
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200640}
641
642/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100644 *
645 * ECParameters ::= CHOICE {
646 * namedCurve OBJECT IDENTIFIER
647 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
648 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200650static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651{
Janos Follath24eed8d2019-11-22 13:21:35 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (params->tag == MBEDTLS_ASN1_OID) {
656 if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) {
657 return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE;
658 }
659 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) {
662 return ret;
663 }
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100664#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100666#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100667 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200669 return pk_ecc_set_group(pk, grp_id);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670}
671
Jethro Beekman01672442023-04-19 14:08:14 +0200672#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
673
674/*
675 * Load an RFC8410 EC key, which doesn't have any parameters
676 */
677static int pk_use_ecparams_rfc8410(const mbedtls_asn1_buf *params,
678 mbedtls_ecp_group_id grp_id,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200679 mbedtls_pk_context *pk)
Jethro Beekman01672442023-04-19 14:08:14 +0200680{
681 if (params->tag != 0 || params->len != 0) {
682 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
683 }
684
Manuel Pégourié-Gonnard25858522023-07-24 11:44:55 +0200685 return pk_ecc_set_group(pk, grp_id);
Jethro Beekman01672442023-04-19 14:08:14 +0200686}
687
688/*
689 * Parse an RFC 8410 encoded private EC key
690 *
691 * CurvePrivateKey ::= OCTET STRING
692 */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200693static int pk_parse_key_rfc8410_der(mbedtls_pk_context *pk,
Jethro Beekman01672442023-04-19 14:08:14 +0200694 unsigned char *key, size_t keylen, const unsigned char *end,
695 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
696{
697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
698 size_t len;
699
700 if ((ret = mbedtls_asn1_get_tag(&key, (key + keylen), &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
701 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
702 }
703
704 if (key + len != end) {
705 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
706 }
707
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +0200708 /*
709 * Load the private key
710 */
711 ret = pk_ecc_set_key(pk, key, len);
712 if (ret != 0) {
Valerio Setti00e8dd12023-05-18 18:56:59 +0200713 return ret;
714 }
Jethro Beekman01672442023-04-19 14:08:14 +0200715
Valerio Setti4064dbb2023-05-17 15:33:07 +0200716 /* pk_parse_key_pkcs8_unencrypted_der() only supports version 1 PKCS8 keys,
717 * which never contain a public key. As such, derive the public key
718 * unconditionally. */
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +0200719 if ((ret = pk_ecc_set_pubkey_from_prv(pk, key, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +0200720 return ret;
721 }
722
Jethro Beekman01672442023-04-19 14:08:14 +0200723 return 0;
724}
725#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200726
Valerio Setti81d75122023-06-14 14:49:33 +0200727#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730/*
731 * RSAPublicKey ::= SEQUENCE {
732 * modulus INTEGER, -- n
733 * publicExponent INTEGER -- e
734 * }
735 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int pk_get_rsapubkey(unsigned char **p,
737 const unsigned char *end,
738 mbedtls_rsa_context *rsa)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739{
Janos Follath24eed8d2019-11-22 13:21:35 +0000740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 size_t len;
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
744 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
745 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
746 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (*p + len != end) {
749 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
750 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
751 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
755 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
756 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
759 NULL, 0, NULL, 0)) != 0) {
760 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
761 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762
763 *p += len;
764
765 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
767 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
768 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
771 NULL, 0, *p, len)) != 0) {
772 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
773 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774
775 *p += len;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (mbedtls_rsa_complete(rsa) != 0 ||
778 mbedtls_rsa_check_pubkey(rsa) != 0) {
779 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
Hanno Becker895c5ab2018-01-05 08:08:09 +0000780 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (*p != end) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
784 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
785 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790
791/* Get a PK algorithm identifier
792 *
793 * AlgorithmIdentifier ::= SEQUENCE {
794 * algorithm OBJECT IDENTIFIER,
795 * parameters ANY DEFINED BY algorithm OPTIONAL }
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797static int pk_get_pk_alg(unsigned char **p,
798 const unsigned char *end,
Jethro Beekman01672442023-04-19 14:08:14 +0200799 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params,
800 mbedtls_ecp_group_id *ec_grp_id)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801{
Janos Follath24eed8d2019-11-22 13:21:35 +0000802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 memset(params, 0, sizeof(mbedtls_asn1_buf));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) {
808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret);
809 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200810
Jethro Beekman01672442023-04-19 14:08:14 +0200811 ret = mbedtls_oid_get_pk_alg(&alg_oid, pk_alg);
Valerio Setti81d75122023-06-14 14:49:33 +0200812#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jethro Beekman01672442023-04-19 14:08:14 +0200813 if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
814 ret = mbedtls_oid_get_ec_grp_algid(&alg_oid, ec_grp_id);
815 if (ret == 0) {
816 *pk_alg = MBEDTLS_PK_ECKEY;
817 }
818 }
819#else
820 (void) ec_grp_id;
821#endif
822 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
824 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825
826 /*
827 * No parameters with RSA (only for EC)
828 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (*pk_alg == MBEDTLS_PK_RSA &&
830 ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
831 params->len != 0)) {
832 return MBEDTLS_ERR_PK_INVALID_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833 }
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836}
837
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200838/* Helper for Montgomery curves */
839#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
840#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
841 ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
842#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
843
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844/*
845 * SubjectPublicKeyInfo ::= SEQUENCE {
846 * algorithm AlgorithmIdentifier,
847 * subjectPublicKey BIT STRING }
848 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
850 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851{
Janos Follath24eed8d2019-11-22 13:21:35 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_asn1_buf alg_params;
855 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +0200856 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
860 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
861 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 }
863
864 end = *p + len;
865
Jethro Beekman01672442023-04-19 14:08:14 +0200866 if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return ret;
868 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
871 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
872 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (*p + len != end) {
875 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
877 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
880 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
881 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
884 return ret;
885 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (pk_alg == MBEDTLS_PK_RSA) {
889 ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk));
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200892#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200894#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +0200895 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200896 ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200897 } else
898#endif
899 {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200900 ret = pk_use_ecparams(&alg_params, pk);
Jethro Beekman01672442023-04-19 14:08:14 +0200901 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (ret == 0) {
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +0200903 ret = pk_ecc_set_pubkey(pk, *p, end - *p);
904 *p += end - *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200907#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (ret == 0 && *p != end) {
911 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
912 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
913 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (ret != 0) {
916 mbedtls_pk_free(pk);
917 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920}
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100924 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
925 *
926 * The value zero is:
927 * - never a valid value for an RSA parameter
928 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
929 *
930 * Since values can't be omitted in PKCS#1, passing a zero value to
931 * rsa_complete() would be incorrect, so reject zero values early.
932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int asn1_get_nonzero_mpi(unsigned char **p,
934 const unsigned char *end,
935 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100936{
937 int ret;
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ret = mbedtls_asn1_get_mpi(p, end, X);
940 if (ret != 0) {
941 return ret;
942 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
945 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
946 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100949}
950
951/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952 * Parse a PKCS#1 encoded private RSA key
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
955 const unsigned char *key,
956 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200957{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100958 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200959 size_t len;
960 unsigned char *p, *end;
961
Hanno Beckerefa14e82017-10-11 19:45:19 +0100962 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100964
Paul Bakker1a7550a2013-09-15 13:01:22 +0200965 p = (unsigned char *) key;
966 end = p + keylen;
967
968 /*
969 * This function parses the RSAPrivateKey (PKCS#1)
970 *
971 * RSAPrivateKey ::= SEQUENCE {
972 * version Version,
973 * modulus INTEGER, -- n
974 * publicExponent INTEGER, -- e
975 * privateExponent INTEGER, -- d
976 * prime1 INTEGER, -- p
977 * prime2 INTEGER, -- q
978 * exponent1 INTEGER, -- d mod (p-1)
979 * exponent2 INTEGER, -- d mod (q-1)
980 * coefficient INTEGER, -- (inverse of q) mod p
981 * otherPrimeInfos OtherPrimeInfos OPTIONAL
982 * }
983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
985 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
986 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987 }
988
989 end = p + len;
990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
992 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993 }
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (version != 0) {
996 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 }
998
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100999 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1001 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1002 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001003 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001006 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1008 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1009 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001010 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001012
1013 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1015 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1016 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001017 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001019
1020 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1022 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1023 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001024 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001026
1027 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1029 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1030 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001031 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001033
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001034#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001035 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1037 * that they can be easily recomputed from D, P and Q. However by
1038 * parsing them from the PKCS1 structure it is possible to avoid
1039 * recalculating them which both reduces the overhead of loading
1040 * RSA private keys into memory and also avoids side channels which
1041 * can arise when computing those values, since all of D, P, and Q
1042 * are secret. See https://eprint.iacr.org/2020/055 for a
1043 * description of one such attack.
1044 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001045
Jack Lloyd80cc8112020-01-22 17:34:29 -05001046 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1048 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1049 goto cleanup;
1050 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001051
1052 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1054 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1055 goto cleanup;
1056 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001057
1058 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1060 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1061 goto cleanup;
1062 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001063
Jack Lloyd60239752020-01-27 17:53:36 -05001064#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001065 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1067 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1068 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1069 goto cleanup;
1070 }
Jack Lloyd60239752020-01-27 17:53:36 -05001071#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001072
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001073 /* rsa_complete() doesn't complete anything with the default
1074 * implementation but is still called:
1075 * - for the benefit of alternative implementation that may want to
1076 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1077 * - as is also sanity-checks the key
1078 *
1079 * Furthermore, we also check the public part for consistency with
1080 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1083 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001084 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001085 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (p != end) {
1088 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1089 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090 }
1091
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001092cleanup:
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001097 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if ((ret & 0xff80) == 0) {
1099 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1100 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001101 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105 }
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110
Valerio Setti81d75122023-06-14 14:49:33 +02001111#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112/*
1113 * Parse a SEC1 encoded private EC key
1114 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001115static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 const unsigned char *key, size_t keylen,
1117 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118{
Janos Follath24eed8d2019-11-22 13:21:35 +00001119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001120 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001121 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001122 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001124 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 unsigned char *end = p + keylen;
1126 unsigned char *end2;
1127
1128 /*
1129 * RFC 5915, or SEC1 Appendix C.4
1130 *
1131 * ECPrivateKey ::= SEQUENCE {
1132 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1133 * privateKey OCTET STRING,
1134 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1135 * publicKey [1] BIT STRING OPTIONAL
1136 * }
1137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1139 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1140 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 }
1142
1143 end = p + len;
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1146 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1147 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (version != 1) {
1150 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1151 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1154 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1155 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156
Valerio Setti6b062ee2023-06-30 17:32:57 +02001157 /* Keep a reference to the position fo the private key. It will be used
1158 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001159 d = p;
1160 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001161
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 p += len;
1163
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001164 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001166 /*
1167 * Is 'parameters' present?
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1170 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1171 0)) == 0) {
1172 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001173 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001178 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001179 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001180
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001181 /*
1182 * Load the private key
1183 */
1184 ret = pk_ecc_set_key(pk, d, d_len);
1185 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001186 return ret;
1187 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001190 /*
1191 * Is 'publickey' present? If not, or if we can't read it (eg because it
1192 * is compressed), create it from the private key.
1193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1195 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1196 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001197 end2 = p + len;
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1201 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (p + len != end2) {
1204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1206 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001207
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001208 if ((ret = pk_ecc_set_pubkey(pk, p, end2 - p)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001209 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001211 /*
Manuel Pégourié-Gonnarde4c883b2023-07-26 23:31:01 +02001212 * The only acceptable failure mode of pk_ecc_set_pubkey() above
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001213 * is if the point format is not recognized.
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1216 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1217 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001221 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001223
Valerio Setti34f67552023-04-03 15:19:18 +02001224 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001225 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001226 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001227 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001228 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231}
Valerio Setti81d75122023-06-14 14:49:33 +02001232#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001234/***********************************************************************
1235 *
1236 * PKCS#8 parsing functions
1237 *
1238 **********************************************************************/
1239
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240/*
1241 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001242 *
1243 * Notes:
1244 *
1245 * - This function does not own the key buffer. It is the
1246 * responsibility of the caller to take care of zeroizing
1247 * and freeing it after use.
1248 *
1249 * - The function is responsible for freeing the provided
1250 * PK context on failure.
1251 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252 */
1253static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 mbedtls_pk_context *pk,
1255 const unsigned char *key, size_t keylen,
1256 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257{
1258 int ret, version;
1259 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261 unsigned char *p = (unsigned char *) key;
1262 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001264 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266
Valerio Setti81d75122023-06-14 14:49:33 +02001267#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001268 (void) f_rng;
1269 (void) p_rng;
1270#endif
1271
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001273 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 *
1275 * PrivateKeyInfo ::= SEQUENCE {
1276 * version Version,
1277 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1278 * privateKey PrivateKey,
1279 * attributes [0] IMPLICIT Attributes OPTIONAL }
1280 *
1281 * Version ::= INTEGER
1282 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1283 * PrivateKey ::= OCTET STRING
1284 *
1285 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1286 */
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1289 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291 }
1292
1293 end = p + len;
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001297 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (version != 0) {
1300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1301 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302
Jethro Beekman01672442023-04-19 14:08:14 +02001303 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return ret;
1305 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1309 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (len < 1) {
1312 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1313 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1314 }
1315
1316 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1317 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1318 }
1319
1320 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1321 return ret;
1322 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (pk_alg == MBEDTLS_PK_RSA) {
1326 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1327 mbedtls_pk_free(pk);
1328 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 }
1330 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001332#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001334#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001335 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001336 if ((ret =
1337 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001338 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001339 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001340 p_rng)) != 0) {
1341 mbedtls_pk_free(pk);
1342 return ret;
1343 }
1344 } else
1345#endif
1346 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001347 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1348 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001349 mbedtls_pk_free(pk);
1350 return ret;
1351 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352 }
1353 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001354#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001357 end = p + len;
1358 if (end != (key + keylen)) {
1359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1360 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1361 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364}
1365
1366/*
1367 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001368 *
1369 * To save space, the decryption happens in-place on the given key buffer.
1370 * Also, while this function may modify the keybuffer, it doesn't own it,
1371 * and instead it is the responsibility of the caller to zeroize and properly
1372 * free it after use.
1373 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001376MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 mbedtls_pk_context *pk,
1378 unsigned char *key, size_t keylen,
1379 const unsigned char *pwd, size_t pwdlen,
1380 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001382 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001384 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1387#if defined(MBEDTLS_PKCS12_C)
1388 mbedtls_cipher_type_t cipher_alg;
1389 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001391 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Hanno Becker2aa80a72017-09-07 15:28:45 +01001393 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394 end = p + keylen;
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (pwdlen == 0) {
1397 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1398 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399
1400 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001401 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001402 *
1403 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1404 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1405 * encryptedData EncryptedData
1406 * }
1407 *
1408 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1409 *
1410 * EncryptedData ::= OCTET STRING
1411 *
1412 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001413 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1416 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 }
1419
1420 end = p + len;
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1424 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1428 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429
Hanno Beckerfab35692017-08-25 13:38:26 +01001430 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431
1432 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001433 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001437 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001438 cipher_alg, md_alg,
1439 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1441 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1442 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001446
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001447 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_PKCS12_C */
1450#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001452 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1453 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1455 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1456 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001460
1461 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001464 {
1465 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001466 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (decrypted == 0) {
1469 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1470 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001471 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001475/***********************************************************************
1476 *
1477 * Top-level functions, with format auto-discovery
1478 *
1479 **********************************************************************/
1480
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481/*
1482 * Parse a private key
1483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1485 const unsigned char *key, size_t keylen,
1486 const unsigned char *pwd, size_t pwdlen,
1487 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488{
Janos Follath24eed8d2019-11-22 13:21:35 +00001489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001494#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (keylen == 0) {
1497 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1498 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001499
1500#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001504 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001506 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 } else {
1508 ret = mbedtls_pem_read_buffer(&pem,
1509 "-----BEGIN RSA PRIVATE KEY-----",
1510 "-----END RSA PRIVATE KEY-----",
1511 key, pwd, pwdlen, &len);
1512 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if (ret == 0) {
1515 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1516 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1517 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1518 pem.buf, pem.buflen)) != 0) {
1519 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 }
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_pem_free(&pem);
1523 return ret;
1524 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1525 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1526 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1527 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1528 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1529 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532
Valerio Setti81d75122023-06-14 14:49:33 +02001533#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001534 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001536 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 } else {
1538 ret = mbedtls_pem_read_buffer(&pem,
1539 "-----BEGIN EC PRIVATE KEY-----",
1540 "-----END EC PRIVATE KEY-----",
1541 key, pwd, pwdlen, &len);
1542 }
1543 if (ret == 0) {
1544 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001547 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 pem.buf, pem.buflen,
1549 f_rng, p_rng)) != 0) {
1550 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001551 }
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 mbedtls_pem_free(&pem);
1554 return ret;
1555 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1556 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1557 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1558 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1559 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1560 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 }
Valerio Setti81d75122023-06-14 14:49:33 +02001562#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001564 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001566 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 } else {
1568 ret = mbedtls_pem_read_buffer(&pem,
1569 "-----BEGIN PRIVATE KEY-----",
1570 "-----END PRIVATE KEY-----",
1571 key, NULL, 0, &len);
1572 }
1573 if (ret == 0) {
1574 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1575 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1576 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577 }
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 mbedtls_pem_free(&pem);
1580 return ret;
1581 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1582 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001583 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001586 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001588 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 } else {
1590 ret = mbedtls_pem_read_buffer(&pem,
1591 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1592 "-----END ENCRYPTED PRIVATE KEY-----",
1593 key, NULL, 0, &len);
1594 }
1595 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001596 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1597 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001599 }
1600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 mbedtls_pem_free(&pem);
1602 return ret;
1603 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1604 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607#else
1608 ((void) pwd);
1609 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001611
1612 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001613 * At this point we only know it's not a PEM formatted key. Could be any
1614 * of the known DER encoded private key formats
1615 *
1616 * We try the different DER format parsers to see if one passes without
1617 * error
1618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001621 unsigned char *key_copy;
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1624 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1625 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001628
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001629 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1630 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001631
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001632 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001633 }
1634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (ret == 0) {
1636 return 0;
1637 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_pk_free(pk);
1640 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1643 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1648 if (ret == 0) {
1649 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001650 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 mbedtls_pk_free(pk);
1653 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1658 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1659 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1660 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 }
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 mbedtls_pk_free(pk);
1664 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001666
Valerio Setti81d75122023-06-14 14:49:33 +02001667#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1669 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001670 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 key, keylen, f_rng, p_rng) == 0) {
1672 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001675#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676
Valerio Setti81d75122023-06-14 14:49:33 +02001677 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001678 * it is ok to leave the PK context initialized but not
1679 * freed: It is the caller's responsibility to call pk_init()
1680 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001681 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001682 * isn't, this leads to mbedtls_pk_free() being called
1683 * twice, once here and once by the caller, but this is
1684 * also ok and in line with the mbedtls_pk_free() calls
1685 * on failed PEM parsing attempts. */
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688}
1689
1690/*
1691 * Parse a public key
1692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001693int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1694 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001695{
Janos Follath24eed8d2019-11-22 13:21:35 +00001696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001697 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001698#if defined(MBEDTLS_RSA_C)
1699 const mbedtls_pk_info_t *pk_info;
1700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001702 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001704#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (keylen == 0) {
1707 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1708 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001709
1710#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001712#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001713 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001715 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 } else {
1717 ret = mbedtls_pem_read_buffer(&pem,
1718 "-----BEGIN RSA PUBLIC KEY-----",
1719 "-----END RSA PUBLIC KEY-----",
1720 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001722
1723 if (ret == 0) {
1724 p = pem.buf;
1725 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1726 mbedtls_pem_free(&pem);
1727 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1728 }
1729
1730 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1731 mbedtls_pem_free(&pem);
1732 return ret;
1733 }
1734
1735 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1736 mbedtls_pk_free(ctx);
1737 }
1738
1739 mbedtls_pem_free(&pem);
1740 return ret;
1741 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1742 mbedtls_pem_free(&pem);
1743 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001744 }
1745#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001746
1747 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001749 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 } else {
1751 ret = mbedtls_pem_read_buffer(&pem,
1752 "-----BEGIN PUBLIC KEY-----",
1753 "-----END PUBLIC KEY-----",
1754 key, NULL, 0, &len);
1755 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001758 /*
1759 * Was PEM encoded
1760 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001761 p = pem.buf;
1762
Jethro Beekman01672442023-04-19 14:08:14 +02001763 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 mbedtls_pem_free(&pem);
1765 return ret;
1766 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1767 mbedtls_pem_free(&pem);
1768 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001769 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001772
1773#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1775 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001777
1778 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1779 return ret;
1780 }
1781
1782 p = (unsigned char *) key;
1783 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1784 if (ret == 0) {
1785 return ret;
1786 }
1787 mbedtls_pk_free(ctx);
1788 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1789 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1790 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001791 }
1792#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001793 p = (unsigned char *) key;
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001798}
1799
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001800/***********************************************************************
1801 *
1802 * Top-level functions, with filesystem support
1803 *
1804 **********************************************************************/
1805
1806#if defined(MBEDTLS_FS_IO)
1807/*
1808 * Load all data from a file into a given buffer.
1809 *
1810 * The file is expected to contain either PEM or DER encoded data.
1811 * A terminating null byte is always appended. It is included in the announced
1812 * length only if the data looks like it is PEM encoded.
1813 */
1814int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1815{
1816 FILE *f;
1817 long size;
1818
1819 if ((f = fopen(path, "rb")) == NULL) {
1820 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1821 }
1822
1823 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1824 mbedtls_setbuf(f, NULL);
1825
1826 fseek(f, 0, SEEK_END);
1827 if ((size = ftell(f)) == -1) {
1828 fclose(f);
1829 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1830 }
1831 fseek(f, 0, SEEK_SET);
1832
1833 *n = (size_t) size;
1834
1835 if (*n + 1 == 0 ||
1836 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1837 fclose(f);
1838 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1839 }
1840
1841 if (fread(*buf, 1, *n, f) != *n) {
1842 fclose(f);
1843
1844 mbedtls_zeroize_and_free(*buf, *n);
1845
1846 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1847 }
1848
1849 fclose(f);
1850
1851 (*buf)[*n] = '\0';
1852
1853 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1854 ++*n;
1855 }
1856
1857 return 0;
1858}
1859
1860/*
1861 * Load and parse a private key
1862 */
1863int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1864 const char *path, const char *pwd,
1865 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1866{
1867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1868 size_t n;
1869 unsigned char *buf;
1870
1871 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1872 return ret;
1873 }
1874
1875 if (pwd == NULL) {
1876 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1877 } else {
1878 ret = mbedtls_pk_parse_key(ctx, buf, n,
1879 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1880 }
1881
1882 mbedtls_zeroize_and_free(buf, n);
1883
1884 return ret;
1885}
1886
1887/*
1888 * Load and parse a public key
1889 */
1890int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1891{
1892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1893 size_t n;
1894 unsigned char *buf;
1895
1896 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1897 return ret;
1898 }
1899
1900 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1901
1902 mbedtls_zeroize_and_free(buf, n);
1903
1904 return ret;
1905}
1906#endif /* MBEDTLS_FS_IO */
1907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_PK_PARSE_C */