blob: 91f56062d32d9833d78a9cfd86149f0d4e3c32f6 [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é-Gonnard54708982023-07-26 15:38:36 +0200241 */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200242static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
243 const unsigned char *pub,
244 size_t pub_len)
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200245{
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200246#if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
247 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
248#else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200249 mbedtls_ecp_keypair ecp_key;
250 mbedtls_ecp_group_id ecp_group_id;
251 int ret;
252
253 ecp_group_id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
254
255 mbedtls_ecp_keypair_init(&ecp_key);
256 ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
257 if (ret != 0) {
258 return ret;
259 }
260 ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200261 pub, pub_len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200262 if (ret != 0) {
263 goto exit;
264 }
265 ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
266 MBEDTLS_ECP_PF_UNCOMPRESSED,
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200267 &pk->pub_raw_len, pk->pub_raw,
268 sizeof(pk->pub_raw));
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200269
270exit:
271 mbedtls_ecp_keypair_free(&ecp_key);
272 return ret;
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200273#endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
274}
275#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
276
277/*
278 * EC public key is an EC point
279 *
280 * The caller is responsible for clearing the structure upon failure if
281 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
282 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
283 */
284static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
285 mbedtls_pk_context *pk)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200286{
Janos Follath24eed8d2019-11-22 13:21:35 +0000287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200288
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200289#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
290 mbedtls_svc_key_id_t key;
291 psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
292 size_t len = (size_t) (end - *p);
293
294 if (len > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) {
295 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 }
Sanne Woudab2b29d52017-08-21 15:58:12 +0100297
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200298 if ((**p == 0x02) || (**p == 0x03)) {
299 /* Compressed format, not supported by PSA Crypto.
300 * Try converting using functions from ECP_LIGHT. */
Manuel Pégourié-Gonnard681e30b2023-07-26 23:03:35 +0200301 ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, *p, len);
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200302 if (ret != 0) {
303 return ret;
304 }
305 } else {
306 /* Uncompressed format */
307 if (len > MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN) {
308 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
309 }
310 memcpy(pk->pub_raw, *p, len);
311 pk->pub_raw_len = len;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200313
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200314 /* Validate the key by trying to importing it */
315 psa_set_key_usage_flags(&key_attrs, 0);
316 psa_set_key_algorithm(&key_attrs, PSA_ALG_ECDSA_ANY);
317 psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
318 psa_set_key_bits(&key_attrs, pk->ec_bits);
319
320 if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
321 &key) != PSA_SUCCESS) ||
322 (psa_destroy_key(key) != PSA_SUCCESS)) {
323 mbedtls_platform_zeroize(pk->pub_raw, MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
324 pk->pub_raw_len = 0;
325 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200327 ret = 0;
328#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
329 mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
330 if ((ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q,
331 (const unsigned char *) *p,
332 end - *p)) == 0) {
333 ret = mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200335#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200336
Manuel Pégourié-Gonnard54708982023-07-26 15:38:36 +0200337 /*
338 * We know mbedtls_ecp_point_read_binary and pk_ecc_read_compressed either
339 * consumed all bytes or failed, and memcpy consumed all bytes too.
340 */
341 *p = (unsigned char *) end;
342
343 return ret;
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) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200903 ret = pk_get_ecpubkey(p, end, pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200906#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (ret == 0 && *p != end) {
910 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
911 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (ret != 0) {
915 mbedtls_pk_free(pk);
916 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919}
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100923 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
924 *
925 * The value zero is:
926 * - never a valid value for an RSA parameter
927 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
928 *
929 * Since values can't be omitted in PKCS#1, passing a zero value to
930 * rsa_complete() would be incorrect, so reject zero values early.
931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932static int asn1_get_nonzero_mpi(unsigned char **p,
933 const unsigned char *end,
934 mbedtls_mpi *X)
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100935{
936 int ret;
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 ret = mbedtls_asn1_get_mpi(p, end, X);
939 if (ret != 0) {
940 return ret;
941 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
944 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
945 }
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return 0;
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100948}
949
950/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951 * Parse a PKCS#1 encoded private RSA key
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
954 const unsigned char *key,
955 size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100957 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958 size_t len;
959 unsigned char *p, *end;
960
Hanno Beckerefa14e82017-10-11 19:45:19 +0100961 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_mpi_init(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100963
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964 p = (unsigned char *) key;
965 end = p + keylen;
966
967 /*
968 * This function parses the RSAPrivateKey (PKCS#1)
969 *
970 * RSAPrivateKey ::= SEQUENCE {
971 * version Version,
972 * modulus INTEGER, -- n
973 * publicExponent INTEGER, -- e
974 * privateExponent INTEGER, -- d
975 * prime1 INTEGER, -- p
976 * prime2 INTEGER, -- q
977 * exponent1 INTEGER, -- d mod (p-1)
978 * exponent2 INTEGER, -- d mod (q-1)
979 * coefficient INTEGER, -- (inverse of q) mod p
980 * otherPrimeInfos OtherPrimeInfos OPTIONAL
981 * }
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
984 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
985 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986 }
987
988 end = p + len;
989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
991 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (version != 0) {
995 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 }
997
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100998 /* Import N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1000 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
1001 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001002 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001005 /* Import E */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1007 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1008 NULL, &T)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001009 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001011
1012 /* Import D */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1014 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
1015 &T, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001016 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001018
1019 /* Import P */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1021 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
1022 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001023 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001025
1026 /* Import Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1028 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
1029 NULL, NULL)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001030 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001032
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +01001033#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001034 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
1036 * that they can be easily recomputed from D, P and Q. However by
1037 * parsing them from the PKCS1 structure it is possible to avoid
1038 * recalculating them which both reduces the overhead of loading
1039 * RSA private keys into memory and also avoids side channels which
1040 * can arise when computing those values, since all of D, P, and Q
1041 * are secret. See https://eprint.iacr.org/2020/055 for a
1042 * description of one such attack.
1043 */
Jack Lloyd8c2631b2020-01-23 17:23:52 -05001044
Jack Lloyd80cc8112020-01-22 17:34:29 -05001045 /* Import DP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1047 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
1048 goto cleanup;
1049 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001050
1051 /* Import DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1053 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
1054 goto cleanup;
1055 }
Jack Lloyd80cc8112020-01-22 17:34:29 -05001056
1057 /* Import QP */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1059 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
1060 goto cleanup;
1061 }
Jack Lloyd2e9eef42020-01-28 14:43:52 -05001062
Jack Lloyd60239752020-01-27 17:53:36 -05001063#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001064 /* Verify existence of the CRT params */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1066 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
1067 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
1068 goto cleanup;
1069 }
Jack Lloyd60239752020-01-27 17:53:36 -05001070#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -05001071
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001072 /* rsa_complete() doesn't complete anything with the default
1073 * implementation but is still called:
1074 * - for the benefit of alternative implementation that may want to
1075 * pre-compute stuff beyond what's provided (eg Montgomery factors)
1076 * - as is also sanity-checks the key
1077 *
1078 * Furthermore, we also check the public part for consistency with
1079 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
1082 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001083 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +01001084 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (p != end) {
1087 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1088 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089 }
1090
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001091cleanup:
1092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_mpi_free(&T);
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (ret != 0) {
Hanno Beckerefa14e82017-10-11 19:45:19 +01001096 /* Wrap error code if it's coming from a lower level */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret & 0xff80) == 0) {
1098 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1099 } else {
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001100 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +01001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_rsa_free(rsa);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 }
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109
Valerio Setti81d75122023-06-14 14:49:33 +02001110#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111/*
1112 * Parse a SEC1 encoded private EC key
1113 */
Valerio Setti4064dbb2023-05-17 15:33:07 +02001114static int pk_parse_key_sec1_der(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 const unsigned char *key, size_t keylen,
1116 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117{
Janos Follath24eed8d2019-11-22 13:21:35 +00001118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001119 int version, pubkey_done;
Jethro Beekman01672442023-04-19 14:08:14 +02001120 size_t len, d_len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001121 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122 unsigned char *p = (unsigned char *) key;
Jethro Beekman01672442023-04-19 14:08:14 +02001123 unsigned char *d;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124 unsigned char *end = p + keylen;
1125 unsigned char *end2;
1126
1127 /*
1128 * RFC 5915, or SEC1 Appendix C.4
1129 *
1130 * ECPrivateKey ::= SEQUENCE {
1131 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1132 * privateKey OCTET STRING,
1133 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1134 * publicKey [1] BIT STRING OPTIONAL
1135 * }
1136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1138 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1139 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 }
1141
1142 end = p + len;
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1145 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1146 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (version != 1) {
1149 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
1150 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1153 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1154 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155
Valerio Setti6b062ee2023-06-30 17:32:57 +02001156 /* Keep a reference to the position fo the private key. It will be used
1157 * later in this function. */
Jethro Beekman01672442023-04-19 14:08:14 +02001158 d = p;
1159 d_len = len;
Valerio Setti00e8dd12023-05-18 18:56:59 +02001160
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 p += len;
1162
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001163 pubkey_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001165 /*
1166 * Is 'parameters' present?
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1169 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1170 0)) == 0) {
1171 if ((ret = pk_get_ecparams(&p, p + len, &params)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001172 (ret = pk_use_ecparams(&params, pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 return ret;
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001177 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001178 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001179
Manuel Pégourié-Gonnarddcd98ff2023-07-25 11:58:31 +02001180 /*
1181 * Load the private key
1182 */
1183 ret = pk_ecc_set_key(pk, d, d_len);
1184 if (ret != 0) {
Manuel Pégourié-Gonnard6db11d52023-07-25 11:20:48 +02001185 return ret;
1186 }
Valerio Setti6b062ee2023-06-30 17:32:57 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (p != end) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001189 /*
1190 * Is 'publickey' present? If not, or if we can't read it (eg because it
1191 * is compressed), create it from the private key.
1192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1194 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
1195 1)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001196 end2 = p + len;
1197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) {
1199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1200 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (p + len != end2) {
1203 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1204 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1205 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001206
Valerio Setti4064dbb2023-05-17 15:33:07 +02001207 if ((ret = pk_get_ecpubkey(&p, end2, pk)) == 0) {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001208 pubkey_done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 } else {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001210 /*
1211 * The only acceptable failure mode of pk_get_ecpubkey() above
1212 * is if the point format is not recognized.
1213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) {
1215 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1216 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001217 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001220 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001222
Valerio Setti34f67552023-04-03 15:19:18 +02001223 if (!pubkey_done) {
Manuel Pégourié-Gonnardde251942023-07-26 22:33:58 +02001224 if ((ret = pk_ecc_set_pubkey_from_prv(pk, d, d_len, f_rng, p_rng)) != 0) {
Valerio Setti520c0382023-04-07 11:38:09 +02001225 return ret;
Valerio Setti34f67552023-04-03 15:19:18 +02001226 }
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001227 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230}
Valerio Setti81d75122023-06-14 14:49:33 +02001231#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001233/***********************************************************************
1234 *
1235 * PKCS#8 parsing functions
1236 *
1237 **********************************************************************/
1238
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239/*
1240 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001241 *
1242 * Notes:
1243 *
1244 * - This function does not own the key buffer. It is the
1245 * responsibility of the caller to take care of zeroizing
1246 * and freeing it after use.
1247 *
1248 * - The function is responsible for freeing the provided
1249 * PK context on failure.
1250 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 */
1252static int pk_parse_key_pkcs8_unencrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 mbedtls_pk_context *pk,
1254 const unsigned char *key, size_t keylen,
1255 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256{
1257 int ret, version;
1258 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 unsigned char *p = (unsigned char *) key;
1261 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Jethro Beekman01672442023-04-19 14:08:14 +02001263 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265
Valerio Setti81d75122023-06-14 14:49:33 +02001266#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001267 (void) f_rng;
1268 (void) p_rng;
1269#endif
1270
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001272 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 *
1274 * PrivateKeyInfo ::= SEQUENCE {
1275 * version Version,
1276 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1277 * privateKey PrivateKey,
1278 * attributes [0] IMPLICIT Attributes OPTIONAL }
1279 *
1280 * Version ::= INTEGER
1281 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1282 * PrivateKey ::= OCTET STRING
1283 *
1284 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1285 */
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1288 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1289 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 }
1291
1292 end = p + len;
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
1295 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Chris Jonesfdb588b2021-04-14 18:15:24 +01001296 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (version != 0) {
1299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret);
1300 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301
Jethro Beekman01672442023-04-19 14:08:14 +02001302 if ((ret = pk_get_pk_alg(&p, end, &pk_alg, &params, &ec_grp_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return ret;
1304 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1308 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (len < 1) {
1311 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1312 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
1313 }
1314
1315 if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) {
1316 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1317 }
1318
1319 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) {
1320 return ret;
1321 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (pk_alg == MBEDTLS_PK_RSA) {
1325 if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) {
1326 mbedtls_pk_free(pk);
1327 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 }
1329 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001331#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
Jethro Beekman01672442023-04-19 14:08:14 +02001333#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti00e8dd12023-05-18 18:56:59 +02001334 if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001335 if ((ret =
1336 pk_use_ecparams_rfc8410(&params, ec_grp_id, pk)) != 0 ||
Jethro Beekman01672442023-04-19 14:08:14 +02001337 (ret =
Valerio Setti4064dbb2023-05-17 15:33:07 +02001338 pk_parse_key_rfc8410_der(pk, p, len, end, f_rng,
Jethro Beekman01672442023-04-19 14:08:14 +02001339 p_rng)) != 0) {
1340 mbedtls_pk_free(pk);
1341 return ret;
1342 }
1343 } else
1344#endif
1345 {
Valerio Setti4064dbb2023-05-17 15:33:07 +02001346 if ((ret = pk_use_ecparams(&params, pk)) != 0 ||
1347 (ret = pk_parse_key_sec1_der(pk, p, len, f_rng, p_rng)) != 0) {
Jethro Beekman01672442023-04-19 14:08:14 +02001348 mbedtls_pk_free(pk);
1349 return ret;
1350 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 }
1352 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001353#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001356 end = p + len;
1357 if (end != (key + keylen)) {
1358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1359 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1360 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363}
1364
1365/*
1366 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001367 *
1368 * To save space, the decryption happens in-place on the given key buffer.
1369 * Also, while this function may modify the keybuffer, it doesn't own it,
1370 * and instead it is the responsibility of the caller to zeroize and properly
1371 * free it after use.
1372 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001375MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 mbedtls_pk_context *pk,
1377 unsigned char *key, size_t keylen,
1378 const unsigned char *pwd, size_t pwdlen,
1379 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001381 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001383 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1386#if defined(MBEDTLS_PKCS12_C)
1387 mbedtls_cipher_type_t cipher_alg;
1388 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389#endif
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001390 size_t outlen = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391
Hanno Becker2aa80a72017-09-07 15:28:45 +01001392 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 end = p + keylen;
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (pwdlen == 0) {
1396 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1397 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398
1399 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001400 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 *
1402 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1403 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1404 * encryptedData EncryptedData
1405 * }
1406 *
1407 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1408 *
1409 * EncryptedData ::= OCTET STRING
1410 *
1411 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001412 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1415 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417 }
1418
1419 end = p + len;
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) {
1422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1423 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1426 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
1427 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428
Hanno Beckerfab35692017-08-25 13:38:26 +01001429 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430
1431 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001432 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PKCS12_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) {
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001436 if ((ret = mbedtls_pkcs12_pbe_ext(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +01001437 cipher_alg, md_alg,
1438 pwd, pwdlen, p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) {
1440 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1441 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444 }
Waleed Elmelegyd5278962023-09-12 14:42:49 +01001445
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001446 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_PKCS12_C */
1449#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) {
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001451 if ((ret = mbedtls_pkcs5_pbes2_ext(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1452 p, len, buf, len, &outlen)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) {
1454 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1455 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001459
1460 decrypted = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001463 {
1464 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001465 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (decrypted == 0) {
1468 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1469 }
Waleed Elmelegyc9f40402023-08-08 15:28:15 +01001470 return pk_parse_key_pkcs8_unencrypted_der(pk, buf, outlen, f_rng, p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001474/***********************************************************************
1475 *
1476 * Top-level functions, with format auto-discovery
1477 *
1478 **********************************************************************/
1479
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480/*
1481 * Parse a private key
1482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
1484 const unsigned char *key, size_t keylen,
1485 const unsigned char *pwd, size_t pwdlen,
1486 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487{
Janos Follath24eed8d2019-11-22 13:21:35 +00001488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001493#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (keylen == 0) {
1496 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1497 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001498
1499#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 mbedtls_pem_init(&pem);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001503 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001505 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 } else {
1507 ret = mbedtls_pem_read_buffer(&pem,
1508 "-----BEGIN RSA PRIVATE KEY-----",
1509 "-----END RSA PRIVATE KEY-----",
1510 key, pwd, pwdlen, &len);
1511 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ret == 0) {
1514 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1515 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
1516 (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
1517 pem.buf, pem.buflen)) != 0) {
1518 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519 }
1520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 mbedtls_pem_free(&pem);
1522 return ret;
1523 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1524 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1525 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1526 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1527 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1528 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531
Valerio Setti81d75122023-06-14 14:49:33 +02001532#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001533 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001535 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 } else {
1537 ret = mbedtls_pem_read_buffer(&pem,
1538 "-----BEGIN EC PRIVATE KEY-----",
1539 "-----END EC PRIVATE KEY-----",
1540 key, pwd, pwdlen, &len);
1541 }
1542 if (ret == 0) {
1543 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
Valerio Setti4064dbb2023-05-17 15:33:07 +02001546 (ret = pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 pem.buf, pem.buflen,
1548 f_rng, p_rng)) != 0) {
1549 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001550 }
1551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 mbedtls_pem_free(&pem);
1553 return ret;
1554 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) {
1555 return MBEDTLS_ERR_PK_PASSWORD_MISMATCH;
1556 } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) {
1557 return MBEDTLS_ERR_PK_PASSWORD_REQUIRED;
1558 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1559 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 }
Valerio Setti81d75122023-06-14 14:49:33 +02001561#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001563 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001565 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 } else {
1567 ret = mbedtls_pem_read_buffer(&pem,
1568 "-----BEGIN PRIVATE KEY-----",
1569 "-----END PRIVATE KEY-----",
1570 key, NULL, 0, &len);
1571 }
1572 if (ret == 0) {
1573 if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk,
1574 pem.buf, pem.buflen, f_rng, p_rng)) != 0) {
1575 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576 }
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 mbedtls_pem_free(&pem);
1579 return ret;
1580 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1581 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001582 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001585 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001587 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 } else {
1589 ret = mbedtls_pem_read_buffer(&pem,
1590 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1591 "-----END ENCRYPTED PRIVATE KEY-----",
1592 key, NULL, 0, &len);
1593 }
1594 if (ret == 0) {
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001595 if ((ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, pem.buf, pem.buflen,
1596 pwd, pwdlen, f_rng, p_rng)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 mbedtls_pk_free(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598 }
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 mbedtls_pem_free(&pem);
1601 return ret;
1602 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1603 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001606#else
1607 ((void) pwd);
1608 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001610
1611 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001612 * At this point we only know it's not a PEM formatted key. Could be any
1613 * of the known DER encoded private key formats
1614 *
1615 * We try the different DER format parsers to see if one passes without
1616 * error
1617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (pwdlen != 0) {
Hanno Beckerfab35692017-08-25 13:38:26 +01001620 unsigned char *key_copy;
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) {
1623 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1624 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 memcpy(key_copy, key, keylen);
Hanno Beckerfab35692017-08-25 13:38:26 +01001627
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +01001628 ret = mbedtls_pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen,
1629 pwd, pwdlen, f_rng, p_rng);
Hanno Beckerfab35692017-08-25 13:38:26 +01001630
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001631 mbedtls_zeroize_and_free(key_copy, keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (ret == 0) {
1635 return 0;
1636 }
Hanno Beckerfab35692017-08-25 13:38:26 +01001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_pk_free(pk);
1639 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) {
1642 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen, f_rng, p_rng);
1647 if (ret == 0) {
1648 return 0;
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001649 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 mbedtls_pk_free(pk);
1652 mbedtls_pk_init(pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
1657 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
1658 pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) {
1659 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001660 }
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 mbedtls_pk_free(pk);
1663 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001665
Valerio Setti81d75122023-06-14 14:49:33 +02001666#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
1668 if (mbedtls_pk_setup(pk, pk_info) == 0 &&
Valerio Setti4064dbb2023-05-17 15:33:07 +02001669 pk_parse_key_sec1_der(pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 key, keylen, f_rng, p_rng) == 0) {
1671 return 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 mbedtls_pk_free(pk);
Valerio Setti81d75122023-06-14 14:49:33 +02001674#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001675
Valerio Setti81d75122023-06-14 14:49:33 +02001676 /* If MBEDTLS_RSA_C is defined but MBEDTLS_PK_HAVE_ECC_KEYS isn't,
Hanno Becker780f0a42018-10-10 11:23:33 +01001677 * it is ok to leave the PK context initialized but not
1678 * freed: It is the caller's responsibility to call pk_init()
1679 * before calling this function, and to call pk_free()
Valerio Setti81d75122023-06-14 14:49:33 +02001680 * when it fails. If MBEDTLS_PK_HAVE_ECC_KEYS is defined but MBEDTLS_RSA_C
Hanno Becker780f0a42018-10-10 11:23:33 +01001681 * isn't, this leads to mbedtls_pk_free() being called
1682 * twice, once here and once by the caller, but this is
1683 * also ok and in line with the mbedtls_pk_free() calls
1684 * on failed PEM parsing attempts. */
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001687}
1688
1689/*
1690 * Parse a public key
1691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1693 const unsigned char *key, size_t keylen)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001694{
Janos Follath24eed8d2019-11-22 13:21:35 +00001695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001696 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001697#if defined(MBEDTLS_RSA_C)
1698 const mbedtls_pk_info_t *pk_info;
1699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001701 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001703#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (keylen == 0) {
1706 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
1707 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001708
1709#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 mbedtls_pem_init(&pem);
Ron Eldord0c56de2017-10-10 17:03:08 +03001711#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001712 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001714 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 } else {
1716 ret = mbedtls_pem_read_buffer(&pem,
1717 "-----BEGIN RSA PUBLIC KEY-----",
1718 "-----END RSA PUBLIC KEY-----",
1719 key, NULL, 0, &len);
Ron Eldord0c56de2017-10-10 17:03:08 +03001720 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001721
1722 if (ret == 0) {
1723 p = pem.buf;
1724 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1725 mbedtls_pem_free(&pem);
1726 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
1727 }
1728
1729 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1730 mbedtls_pem_free(&pem);
1731 return ret;
1732 }
1733
1734 if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) {
1735 mbedtls_pk_free(ctx);
1736 }
1737
1738 mbedtls_pem_free(&pem);
1739 return ret;
1740 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1741 mbedtls_pem_free(&pem);
1742 return ret;
Ron Eldord0c56de2017-10-10 17:03:08 +03001743 }
1744#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001745
1746 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (key[keylen - 1] != '\0') {
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001748 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 } else {
1750 ret = mbedtls_pem_read_buffer(&pem,
1751 "-----BEGIN PUBLIC KEY-----",
1752 "-----END PUBLIC KEY-----",
1753 key, NULL, 0, &len);
1754 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (ret == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001757 /*
1758 * Was PEM encoded
1759 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001760 p = pem.buf;
1761
Jethro Beekman01672442023-04-19 14:08:14 +02001762 ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 mbedtls_pem_free(&pem);
1764 return ret;
1765 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1766 mbedtls_pem_free(&pem);
1767 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 mbedtls_pem_free(&pem);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001771
1772#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) {
1774 return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Ron Eldor40b14a82017-10-16 19:30:00 +03001775 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001776
1777 if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) {
1778 return ret;
1779 }
1780
1781 p = (unsigned char *) key;
1782 ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx));
1783 if (ret == 0) {
1784 return ret;
1785 }
1786 mbedtls_pk_free(ctx);
1787 if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
1788 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) {
1789 return ret;
Ron Eldor40b14a82017-10-16 19:30:00 +03001790 }
1791#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001792 p = (unsigned char *) key;
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return ret;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001797}
1798
Manuel Pégourié-Gonnard212517b2023-07-26 12:05:38 +02001799/***********************************************************************
1800 *
1801 * Top-level functions, with filesystem support
1802 *
1803 **********************************************************************/
1804
1805#if defined(MBEDTLS_FS_IO)
1806/*
1807 * Load all data from a file into a given buffer.
1808 *
1809 * The file is expected to contain either PEM or DER encoded data.
1810 * A terminating null byte is always appended. It is included in the announced
1811 * length only if the data looks like it is PEM encoded.
1812 */
1813int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
1814{
1815 FILE *f;
1816 long size;
1817
1818 if ((f = fopen(path, "rb")) == NULL) {
1819 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1820 }
1821
1822 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
1823 mbedtls_setbuf(f, NULL);
1824
1825 fseek(f, 0, SEEK_END);
1826 if ((size = ftell(f)) == -1) {
1827 fclose(f);
1828 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1829 }
1830 fseek(f, 0, SEEK_SET);
1831
1832 *n = (size_t) size;
1833
1834 if (*n + 1 == 0 ||
1835 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
1836 fclose(f);
1837 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1838 }
1839
1840 if (fread(*buf, 1, *n, f) != *n) {
1841 fclose(f);
1842
1843 mbedtls_zeroize_and_free(*buf, *n);
1844
1845 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
1846 }
1847
1848 fclose(f);
1849
1850 (*buf)[*n] = '\0';
1851
1852 if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
1853 ++*n;
1854 }
1855
1856 return 0;
1857}
1858
1859/*
1860 * Load and parse a private key
1861 */
1862int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1863 const char *path, const char *pwd,
1864 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1865{
1866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1867 size_t n;
1868 unsigned char *buf;
1869
1870 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1871 return ret;
1872 }
1873
1874 if (pwd == NULL) {
1875 ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0, f_rng, p_rng);
1876 } else {
1877 ret = mbedtls_pk_parse_key(ctx, buf, n,
1878 (const unsigned char *) pwd, strlen(pwd), f_rng, p_rng);
1879 }
1880
1881 mbedtls_zeroize_and_free(buf, n);
1882
1883 return ret;
1884}
1885
1886/*
1887 * Load and parse a public key
1888 */
1889int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
1890{
1891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1892 size_t n;
1893 unsigned char *buf;
1894
1895 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1896 return ret;
1897 }
1898
1899 ret = mbedtls_pk_parse_public_key(ctx, buf, n);
1900
1901 mbedtls_zeroize_and_free(buf, n);
1902
1903 return ret;
1904}
1905#endif /* MBEDTLS_FS_IO */
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#endif /* MBEDTLS_PK_PARSE_C */