v / thirdparty / mbedtls / library / pk.c
1603 lines · 1409 sloc · 53.46 KB · 3d9911f887ecec942f9ae2a5be02d064f233b729
Raw
1/*
2 * Public Key abstraction layer
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include "common.h"
9
10#if defined(MBEDTLS_PK_C)
11#include "mbedtls/pk.h"
12#include "pk_wrap.h"
13#include "pkwrite.h"
14#include "pk_internal.h"
15
16#include "mbedtls/platform_util.h"
17#include "mbedtls/error.h"
18
19#if defined(MBEDTLS_RSA_C)
20#include "mbedtls/rsa.h"
21#include "rsa_internal.h"
22#endif
23#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
24#include "mbedtls/ecp.h"
25#endif
26#if defined(MBEDTLS_ECDSA_C)
27#include "mbedtls/ecdsa.h"
28#endif
29
30#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
31#include "psa_util_internal.h"
32#include "mbedtls/psa_util.h"
33#endif
34
35#include <limits.h>
36#include <stdint.h>
37
38#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
39#include "mbedtls/platform.h" // for calloc/free
40#endif
41
42#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
43#define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
44 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
45
46#define MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN \
47 PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
48
49#define MBEDTLS_PK_MAX_PUBKEY_RAW_LEN 0
50#if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && \
51 MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN > MBEDTLS_PK_MAX_PUBKEY_RAW_LEN
52#undef MBEDTLS_PK_MAX_PUBKEY_RAW_LEN
53#define MBEDTLS_PK_MAX_PUBKEY_RAW_LEN MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN
54#endif
55#if (defined(MBEDTLS_RSA_C) || \
56 (defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY))) && \
57 MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN > MBEDTLS_PK_MAX_PUBKEY_RAW_LEN
58#undef MBEDTLS_PK_MAX_PUBKEY_RAW_LEN
59#define MBEDTLS_PK_MAX_PUBKEY_RAW_LEN MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN
60#endif
61#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
62
63/*
64 * Initialise a mbedtls_pk_context
65 */
66void mbedtls_pk_init(mbedtls_pk_context *ctx)
67{
68 ctx->pk_info = NULL;
69 ctx->pk_ctx = NULL;
70#if defined(MBEDTLS_USE_PSA_CRYPTO)
71 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
72#endif /* MBEDTLS_USE_PSA_CRYPTO */
73#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
74 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
75 ctx->pub_raw_len = 0;
76 ctx->ec_family = 0;
77 ctx->ec_bits = 0;
78#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
79}
80
81/*
82 * Free (the components of) a mbedtls_pk_context
83 */
84void mbedtls_pk_free(mbedtls_pk_context *ctx)
85{
86 if (ctx == NULL) {
87 return;
88 }
89
90 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
91 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
92 }
93
94#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
95 /* The ownership of the priv_id key for opaque keys is external of the PK
96 * module. It's the user responsibility to clear it after use. */
97 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
98 psa_destroy_key(ctx->priv_id);
99 }
100#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
101
102 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
103}
104
105#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
106/*
107 * Initialize a restart context
108 */
109void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
110{
111 ctx->pk_info = NULL;
112 ctx->rs_ctx = NULL;
113}
114
115/*
116 * Free the components of a restart context
117 */
118void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
119{
120 if (ctx == NULL || ctx->pk_info == NULL ||
121 ctx->pk_info->rs_free_func == NULL) {
122 return;
123 }
124
125 ctx->pk_info->rs_free_func(ctx->rs_ctx);
126
127 ctx->pk_info = NULL;
128 ctx->rs_ctx = NULL;
129}
130#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
131
132/*
133 * Get pk_info structure from type
134 */
135const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
136{
137 switch (pk_type) {
138#if defined(MBEDTLS_RSA_C)
139 case MBEDTLS_PK_RSA:
140 return &mbedtls_rsa_info;
141#endif /* MBEDTLS_RSA_C */
142#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
143 case MBEDTLS_PK_ECKEY:
144 return &mbedtls_eckey_info;
145 case MBEDTLS_PK_ECKEY_DH:
146 return &mbedtls_eckeydh_info;
147#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
148#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
149 case MBEDTLS_PK_ECDSA:
150 return &mbedtls_ecdsa_info;
151#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
152 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
153 default:
154 return NULL;
155 }
156}
157
158/*
159 * Initialise context
160 */
161int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
162{
163 if (info == NULL || ctx->pk_info != NULL) {
164 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
165 }
166
167 if ((info->ctx_alloc_func != NULL) &&
168 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
169 return MBEDTLS_ERR_PK_ALLOC_FAILED;
170 }
171
172 ctx->pk_info = info;
173
174 return 0;
175}
176
177#if defined(MBEDTLS_USE_PSA_CRYPTO)
178/*
179 * Initialise a PSA-wrapping context
180 */
181int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
182 const mbedtls_svc_key_id_t key)
183{
184 const mbedtls_pk_info_t *info = NULL;
185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
186 psa_key_type_t type;
187
188 if (ctx == NULL || ctx->pk_info != NULL) {
189 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
190 }
191
192 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
193 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
194 }
195 type = psa_get_key_type(&attributes);
196 psa_reset_key_attributes(&attributes);
197
198#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
199 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
200 info = &mbedtls_ecdsa_opaque_info;
201 } else
202#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
203 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
204 info = &mbedtls_rsa_opaque_info;
205 } else {
206 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
207 }
208
209 ctx->pk_info = info;
210 ctx->priv_id = key;
211
212 return 0;
213}
214#endif /* MBEDTLS_USE_PSA_CRYPTO */
215
216#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
217/*
218 * Initialize an RSA-alt context
219 */
220int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
221 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
222 mbedtls_pk_rsa_alt_sign_func sign_func,
223 mbedtls_pk_rsa_alt_key_len_func key_len_func)
224{
225 mbedtls_rsa_alt_context *rsa_alt;
226 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
227
228 if (ctx->pk_info != NULL) {
229 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
230 }
231
232 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
233 return MBEDTLS_ERR_PK_ALLOC_FAILED;
234 }
235
236 ctx->pk_info = info;
237
238 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
239
240 rsa_alt->key = key;
241 rsa_alt->decrypt_func = decrypt_func;
242 rsa_alt->sign_func = sign_func;
243 rsa_alt->key_len_func = key_len_func;
244
245 return 0;
246}
247#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
248
249/*
250 * Tell if a PK can do the operations of the given type
251 */
252int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
253{
254 /* A context with null pk_info is not set up yet and can't do anything.
255 * For backward compatibility, also accept NULL instead of a context
256 * pointer. */
257 if (ctx == NULL || ctx->pk_info == NULL) {
258 return 0;
259 }
260
261 return ctx->pk_info->can_do(type);
262}
263
264#if defined(MBEDTLS_USE_PSA_CRYPTO)
265/*
266 * Tell if a PK can do the operations of the given PSA algorithm
267 */
268int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
269 psa_key_usage_t usage)
270{
271 psa_key_usage_t key_usage;
272
273 /* A context with null pk_info is not set up yet and can't do anything.
274 * For backward compatibility, also accept NULL instead of a context
275 * pointer. */
276 if (ctx == NULL || ctx->pk_info == NULL) {
277 return 0;
278 }
279
280 /* Filter out non allowed algorithms */
281 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
282 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
283 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
284 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
285 PSA_ALG_IS_ECDH(alg) == 0) {
286 return 0;
287 }
288
289 /* Filter out non allowed usage flags */
290 if (usage == 0 ||
291 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
292 PSA_KEY_USAGE_DECRYPT |
293 PSA_KEY_USAGE_DERIVE)) != 0) {
294 return 0;
295 }
296
297 /* Wildcard hash is not allowed */
298 if (PSA_ALG_IS_SIGN_HASH(alg) &&
299 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
300 return 0;
301 }
302
303 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
304 mbedtls_pk_type_t type;
305
306 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
307 type = MBEDTLS_PK_ECKEY;
308 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
309 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
310 type = MBEDTLS_PK_RSA;
311 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
312 type = MBEDTLS_PK_RSASSA_PSS;
313 } else {
314 return 0;
315 }
316
317 if (ctx->pk_info->can_do(type) == 0) {
318 return 0;
319 }
320
321 switch (type) {
322 case MBEDTLS_PK_ECKEY:
323 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
324 break;
325 case MBEDTLS_PK_RSA:
326 case MBEDTLS_PK_RSASSA_PSS:
327 key_usage = PSA_KEY_USAGE_SIGN_HASH |
328 PSA_KEY_USAGE_SIGN_MESSAGE |
329 PSA_KEY_USAGE_DECRYPT;
330 break;
331 default:
332 /* Should never happen */
333 return 0;
334 }
335
336 return (key_usage & usage) == usage;
337 }
338
339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
340 psa_status_t status;
341
342 status = psa_get_key_attributes(ctx->priv_id, &attributes);
343 if (status != PSA_SUCCESS) {
344 return 0;
345 }
346
347 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
348 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
349 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
350 * Even though we don't officially support using other implementations of PSA
351 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
352 * separated. */
353#if defined(MBEDTLS_PSA_CRYPTO_C)
354 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
355#endif /* MBEDTLS_PSA_CRYPTO_C */
356 key_usage = psa_get_key_usage_flags(&attributes);
357 psa_reset_key_attributes(&attributes);
358
359 if ((key_usage & usage) != usage) {
360 return 0;
361 }
362
363 /*
364 * Common case: the key alg [or alg2] only allows alg.
365 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
366 * directly.
367 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
368 * a fixed hash on key_alg [or key_alg2].
369 */
370 if (alg == key_alg) {
371 return 1;
372 }
373#if defined(MBEDTLS_PSA_CRYPTO_C)
374 if (alg == key_alg2) {
375 return 1;
376 }
377#endif /* MBEDTLS_PSA_CRYPTO_C */
378
379 /*
380 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
381 * and alg is the same hash-and-sign family with any hash,
382 * then alg is compliant with this key alg
383 */
384 if (PSA_ALG_IS_SIGN_HASH(alg)) {
385 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
386 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
387 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
388 return 1;
389 }
390#if defined(MBEDTLS_PSA_CRYPTO_C)
391 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
392 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
393 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
394 return 1;
395 }
396#endif /* MBEDTLS_PSA_CRYPTO_C */
397 }
398
399 return 0;
400}
401#endif /* MBEDTLS_USE_PSA_CRYPTO */
402
403#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
404#if defined(MBEDTLS_RSA_C)
405static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
406 int want_crypt)
407{
408 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
409 if (want_crypt) {
410 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
411 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
412 } else {
413 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
414 }
415 } else {
416 if (want_crypt) {
417 return PSA_ALG_RSA_PKCS1V15_CRYPT;
418 } else {
419 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
420 }
421 }
422}
423#endif /* MBEDTLS_RSA_C */
424
425int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
426 psa_key_usage_t usage,
427 psa_key_attributes_t *attributes)
428{
429 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
430
431 psa_key_usage_t more_usage = usage;
432 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
433 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
434 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
435 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
436 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
437 more_usage |= PSA_KEY_USAGE_ENCRYPT;
438 }
439 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
440
441 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
442 usage == PSA_KEY_USAGE_VERIFY_HASH ||
443 usage == PSA_KEY_USAGE_ENCRYPT);
444
445 switch (pk_type) {
446#if defined(MBEDTLS_RSA_C)
447 case MBEDTLS_PK_RSA:
448 {
449 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
450 switch (usage) {
451 case PSA_KEY_USAGE_SIGN_MESSAGE:
452 case PSA_KEY_USAGE_SIGN_HASH:
453 case PSA_KEY_USAGE_VERIFY_MESSAGE:
454 case PSA_KEY_USAGE_VERIFY_HASH:
455 /* Nothing to do. */
456 break;
457 case PSA_KEY_USAGE_DECRYPT:
458 case PSA_KEY_USAGE_ENCRYPT:
459 want_crypt = 1;
460 break;
461 default:
462 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
463 }
464 /* Detect the presence of a private key in a way that works both
465 * in CRT and non-CRT configurations. */
466 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
467 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
468 if (want_private && !has_private) {
469 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
470 }
471 psa_set_key_type(attributes, (want_private ?
472 PSA_KEY_TYPE_RSA_KEY_PAIR :
473 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
474 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
475 psa_set_key_algorithm(attributes,
476 psa_algorithm_for_rsa(rsa, want_crypt));
477 break;
478 }
479#endif /* MBEDTLS_RSA_C */
480
481#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
482 case MBEDTLS_PK_ECKEY:
483 case MBEDTLS_PK_ECKEY_DH:
484 case MBEDTLS_PK_ECDSA:
485 {
486 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
487 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
488#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
489 psa_ecc_family_t family = pk->ec_family;
490 size_t bits = pk->ec_bits;
491 int has_private = 0;
492 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
493 has_private = 1;
494 }
495#else
496 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
497 int has_private = (ec->d.n != 0);
498 size_t bits = 0;
499 psa_ecc_family_t family =
500 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
501#endif
502 psa_algorithm_t alg = 0;
503 switch (usage) {
504 case PSA_KEY_USAGE_SIGN_MESSAGE:
505 case PSA_KEY_USAGE_SIGN_HASH:
506 case PSA_KEY_USAGE_VERIFY_MESSAGE:
507 case PSA_KEY_USAGE_VERIFY_HASH:
508 if (!sign_ok) {
509 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
510 }
511#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
512 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
513#else
514 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
515#endif
516 break;
517 case PSA_KEY_USAGE_DERIVE:
518 alg = PSA_ALG_ECDH;
519 if (!derive_ok) {
520 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
521 }
522 break;
523 default:
524 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
525 }
526 if (want_private && !has_private) {
527 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
528 }
529 psa_set_key_type(attributes, (want_private ?
530 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
531 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
532 psa_set_key_bits(attributes, bits);
533 psa_set_key_algorithm(attributes, alg);
534 break;
535 }
536#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
537
538#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
539 case MBEDTLS_PK_RSA_ALT:
540 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
541#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
542
543#if defined(MBEDTLS_USE_PSA_CRYPTO)
544 case MBEDTLS_PK_OPAQUE:
545 {
546 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
548 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
549 if (status != PSA_SUCCESS) {
550 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
551 }
552 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
553 switch (usage) {
554 case PSA_KEY_USAGE_SIGN_MESSAGE:
555 case PSA_KEY_USAGE_SIGN_HASH:
556 case PSA_KEY_USAGE_VERIFY_MESSAGE:
557 case PSA_KEY_USAGE_VERIFY_HASH:
558 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
559 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
560 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
561 }
562 break;
563 case PSA_KEY_USAGE_DECRYPT:
564 case PSA_KEY_USAGE_ENCRYPT:
565 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
566 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
567 }
568 break;
569 case PSA_KEY_USAGE_DERIVE:
570 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
571 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
572 }
573 break;
574 default:
575 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
576 }
577 psa_key_type_t new_type = old_type;
578 /* Opaque keys are always key pairs, so we don't need a check
579 * on the input if the required usage is private. We just need
580 * to adjust the type correctly if the required usage is public. */
581 if (!want_private) {
582 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
583 }
584 more_usage = psa_get_key_usage_flags(&old_attributes);
585 if ((usage & more_usage) == 0) {
586 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
587 }
588 psa_set_key_type(attributes, new_type);
589 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
590 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
591 break;
592 }
593#endif /* MBEDTLS_USE_PSA_CRYPTO */
594
595 default:
596 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
597 }
598
599 psa_set_key_usage_flags(attributes, more_usage);
600 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
601 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
602 * Even though we don't officially support using other implementations of PSA
603 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
604 * separated. */
605#if defined(MBEDTLS_PSA_CRYPTO_C)
606 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
607#endif
608
609 return 0;
610}
611
612#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
613static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
614 psa_key_type_t old_type, size_t old_bits,
615 const psa_key_attributes_t *attributes,
616 mbedtls_svc_key_id_t *new_key_id)
617{
618#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
619 unsigned char *key_buffer = NULL;
620 size_t key_buffer_size = 0;
621#else
622 unsigned char key_buffer[PK_EXPORT_KEY_STACK_BUFFER_SIZE];
623 const size_t key_buffer_size = sizeof(key_buffer);
624#endif
625 size_t key_length = 0;
626
627 /* We are exporting from a PK object, so we know key type is valid for PK */
628#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
629 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(old_type, old_bits);
630 key_buffer = mbedtls_calloc(1, key_buffer_size);
631 if (key_buffer == NULL) {
632 return MBEDTLS_ERR_PK_ALLOC_FAILED;
633 }
634#else
635 (void) old_type;
636 (void) old_bits;
637#endif
638
639 psa_status_t status = psa_export_key(old_key_id,
640 key_buffer, key_buffer_size,
641 &key_length);
642 if (status != PSA_SUCCESS) {
643 goto cleanup;
644 }
645 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
646 mbedtls_platform_zeroize(key_buffer, key_length);
647
648cleanup:
649#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
650 mbedtls_free(key_buffer);
651#endif
652 return status;
653}
654
655static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
656 const psa_key_attributes_t *attributes,
657 mbedtls_svc_key_id_t *new_key_id)
658{
659 /* Normally, we prefer copying: it's more efficient and works even
660 * for non-exportable keys. */
661 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
662 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
663 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
664 /* There are edge cases where copying won't work, but export+import
665 * might:
666 * - If the old key does not allow PSA_KEY_USAGE_COPY.
667 * - If the old key's usage does not allow what attributes wants.
668 * Because the key was intended for use in the pk module, and may
669 * have had a policy chosen solely for what pk needs rather than
670 * based on a detailed understanding of PSA policies, we are a bit
671 * more liberal than psa_copy_key() here.
672 */
673 /* Here we need to check that the types match, otherwise we risk
674 * importing nonsensical data. */
675 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
676 status = psa_get_key_attributes(old_key_id, &old_attributes);
677 if (status != PSA_SUCCESS) {
678 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
679 }
680 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
681 size_t old_bits = psa_get_key_bits(&old_attributes);
682 psa_reset_key_attributes(&old_attributes);
683 if (old_type != psa_get_key_type(attributes)) {
684 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
685 }
686 status = export_import_into_psa(old_key_id, old_type, old_bits,
687 attributes, new_key_id);
688 }
689 return PSA_PK_TO_MBEDTLS_ERR(status);
690}
691#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
692
693static int import_pair_into_psa(const mbedtls_pk_context *pk,
694 const psa_key_attributes_t *attributes,
695 mbedtls_svc_key_id_t *key_id)
696{
697 switch (mbedtls_pk_get_type(pk)) {
698#if defined(MBEDTLS_RSA_C)
699 case MBEDTLS_PK_RSA:
700 {
701 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
702 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
703 }
704 size_t key_bits = psa_get_key_bits(attributes);
705 size_t key_buffer_size = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits);
706 unsigned char *key_buffer = mbedtls_calloc(1, key_buffer_size);
707 if (key_buffer == NULL) {
708 return MBEDTLS_ERR_PK_ALLOC_FAILED;
709 }
710 unsigned char *const key_end = key_buffer + key_buffer_size;
711 unsigned char *key_data = key_end;
712 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
713 key_buffer, &key_data);
714 if (ret < 0) {
715 goto cleanup_rsa;
716 }
717 size_t key_length = key_end - key_data;
718 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
719 key_data, key_length,
720 key_id));
721cleanup_rsa:
722 mbedtls_zeroize_and_free(key_buffer, key_buffer_size);
723 return ret;
724 }
725#endif /* MBEDTLS_RSA_C */
726
727#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
728 case MBEDTLS_PK_ECKEY:
729 case MBEDTLS_PK_ECKEY_DH:
730 case MBEDTLS_PK_ECDSA:
731 {
732 /* We need to check the curve family, otherwise the import could
733 * succeed with nonsensical data.
734 * We don't check the bit-size: it's optional in attributes,
735 * and if it's specified, psa_import_key() will know from the key
736 * data length and will check that the bit-size matches. */
737 psa_key_type_t to_type = psa_get_key_type(attributes);
738#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
739 psa_ecc_family_t from_family = pk->ec_family;
740#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
741 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
742 size_t from_bits = 0;
743 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
744 &from_bits);
745#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
746 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
747 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
748 }
749
750#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
751 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
752 /* We have a public key and want a key pair. */
753 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
754 }
755 return copy_into_psa(pk->priv_id, attributes, key_id);
756#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
757 if (ec->d.n == 0) {
758 /* Private key not set. Assume the input is a public key only.
759 * (The other possibility is that it's an incomplete object
760 * where the group is set but neither the public key nor
761 * the private key. This is not possible through ecp.h
762 * functions, so we don't bother reporting a more suitable
763 * error in that case.) */
764 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
765 }
766 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
767 size_t key_length = 0;
768 int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
769 key_buffer, sizeof(key_buffer));
770 if (ret < 0) {
771 return ret;
772 }
773 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
774 key_buffer, key_length,
775 key_id));
776 mbedtls_platform_zeroize(key_buffer, key_length);
777 return ret;
778#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
779 }
780#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
781
782#if defined(MBEDTLS_USE_PSA_CRYPTO)
783 case MBEDTLS_PK_OPAQUE:
784 return copy_into_psa(pk->priv_id, attributes, key_id);
785#endif /* MBEDTLS_USE_PSA_CRYPTO */
786
787 default:
788 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
789 }
790}
791
792static int import_public_into_psa(const mbedtls_pk_context *pk,
793 const psa_key_attributes_t *attributes,
794 mbedtls_svc_key_id_t *key_id)
795{
796 psa_key_type_t psa_type = psa_get_key_type(attributes);
797
798#if defined(MBEDTLS_RSA_C) || \
799 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
800 defined(MBEDTLS_USE_PSA_CRYPTO)
801 unsigned char key_buffer[MBEDTLS_PK_MAX_PUBKEY_RAW_LEN];
802#endif
803 unsigned char *key_data = NULL;
804 size_t key_length = 0;
805
806 switch (mbedtls_pk_get_type(pk)) {
807#if defined(MBEDTLS_RSA_C)
808 case MBEDTLS_PK_RSA:
809 {
810 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
811 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
812 }
813 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
814 key_data = key_end;
815 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
816 key_buffer, &key_data);
817 if (ret < 0) {
818 return ret;
819 }
820 key_length = (size_t) ret;
821 break;
822 }
823#endif /*MBEDTLS_RSA_C */
824
825#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
826 case MBEDTLS_PK_ECKEY:
827 case MBEDTLS_PK_ECKEY_DH:
828 case MBEDTLS_PK_ECDSA:
829 {
830 /* We need to check the curve family, otherwise the import could
831 * succeed with nonsensical data.
832 * We don't check the bit-size: it's optional in attributes,
833 * and if it's specified, psa_import_key() will know from the key
834 * data length and will check that the bit-size matches. */
835#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
836 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
837 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
838 }
839 key_data = (unsigned char *) pk->pub_raw;
840 key_length = pk->pub_raw_len;
841#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
842 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
843 size_t from_bits = 0;
844 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
845 &from_bits);
846 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
847 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
848 }
849 int ret = mbedtls_ecp_write_public_key(
850 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
851 &key_length, key_buffer, sizeof(key_buffer));
852 if (ret < 0) {
853 return ret;
854 }
855 key_data = key_buffer;
856#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
857 break;
858 }
859#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
860
861#if defined(MBEDTLS_USE_PSA_CRYPTO)
862 case MBEDTLS_PK_OPAQUE:
863 {
864 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
865 psa_status_t status =
866 psa_get_key_attributes(pk->priv_id, &old_attributes);
867 if (status != PSA_SUCCESS) {
868 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
869 }
870 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
871 psa_reset_key_attributes(&old_attributes);
872 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
873 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
874 }
875 status = psa_export_public_key(pk->priv_id,
876 key_buffer, sizeof(key_buffer),
877 &key_length);
878 if (status != PSA_SUCCESS) {
879 return PSA_PK_TO_MBEDTLS_ERR(status);
880 }
881 key_data = key_buffer;
882 break;
883 }
884#endif /* MBEDTLS_USE_PSA_CRYPTO */
885
886 default:
887 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
888 }
889
890 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
891 key_data, key_length,
892 key_id));
893}
894
895int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
896 const psa_key_attributes_t *attributes,
897 mbedtls_svc_key_id_t *key_id)
898{
899 /* Set the output immediately so that it won't contain garbage even
900 * if we error out before calling psa_import_key(). */
901 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
902
903#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
904 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
905 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
906 }
907#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
908
909 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
910 if (want_public) {
911 return import_public_into_psa(pk, attributes, key_id);
912 } else {
913 return import_pair_into_psa(pk, attributes, key_id);
914 }
915}
916
917static int is_valid_for_pk(psa_key_type_t key_type)
918{
919#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
920 if (PSA_KEY_TYPE_IS_ECC(key_type)) {
921 return 1;
922 }
923#endif
924#if defined(MBEDTLS_RSA_C)
925 if (PSA_KEY_TYPE_IS_RSA(key_type)) {
926 return 1;
927 }
928#endif
929 return 0;
930}
931
932static int copy_from_psa(mbedtls_svc_key_id_t key_id,
933 mbedtls_pk_context *pk,
934 int public_only)
935{
936 psa_status_t status;
937 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
938 psa_key_type_t key_type;
939 size_t key_bits;
940#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
941 unsigned char *exp_key = NULL;
942 size_t exp_key_size = 0;
943#else
944 unsigned char exp_key[PK_EXPORT_KEY_STACK_BUFFER_SIZE];
945 const size_t exp_key_size = sizeof(exp_key);
946#endif
947 size_t exp_key_len;
948 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
949
950 if (pk == NULL) {
951 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
952 }
953
954 status = psa_get_key_attributes(key_id, &key_attr);
955 if (status != PSA_SUCCESS) {
956 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
957 }
958
959 key_type = psa_get_key_type(&key_attr);
960 if (!is_valid_for_pk(key_type)) {
961 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
962 }
963
964 if (public_only) {
965 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
966 }
967 key_bits = psa_get_key_bits(&key_attr);
968
969#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
970 exp_key_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
971 exp_key = mbedtls_calloc(1, exp_key_size);
972 if (exp_key == NULL) {
973 return MBEDTLS_ERR_PK_ALLOC_FAILED;
974 }
975#endif
976
977 if (public_only) {
978 status = psa_export_public_key(key_id, exp_key, exp_key_size, &exp_key_len);
979 } else {
980 status = psa_export_key(key_id, exp_key, exp_key_size, &exp_key_len);
981 }
982 if (status != PSA_SUCCESS) {
983 ret = PSA_PK_TO_MBEDTLS_ERR(status);
984 goto exit;
985 }
986
987 key_type = psa_get_key_type(&key_attr);
988 if (public_only) {
989 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
990 }
991 key_bits = psa_get_key_bits(&key_attr);
992
993#if defined(MBEDTLS_RSA_C)
994 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
995 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
996
997 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
998 if (ret != 0) {
999 goto exit;
1000 }
1001
1002 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
1003 ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
1004 } else {
1005 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
1006 }
1007 if (ret != 0) {
1008 goto exit;
1009 }
1010
1011 psa_algorithm_t alg_type = psa_get_key_algorithm(&key_attr);
1012 mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
1013 if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
1014 md_type = mbedtls_md_type_from_psa_alg(alg_type);
1015 }
1016
1017 if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
1018 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
1019 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
1020 alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
1021 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
1022 }
1023 if (ret != 0) {
1024 goto exit;
1025 }
1026 } else
1027#endif /* MBEDTLS_RSA_C */
1028#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
1029 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
1030 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
1031 mbedtls_ecp_group_id grp_id;
1032
1033 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
1034 if (ret != 0) {
1035 goto exit;
1036 }
1037
1038 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
1039 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
1040 if (ret != 0) {
1041 goto exit;
1042 }
1043
1044 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
1045 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
1046 if (ret != 0) {
1047 goto exit;
1048 }
1049 ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
1050 mbedtls_psa_get_random,
1051 MBEDTLS_PSA_RANDOM_STATE);
1052 } else {
1053 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
1054 }
1055 if (ret != 0) {
1056 goto exit;
1057 }
1058 } else
1059#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
1060 {
1061 (void) key_bits;
1062 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1063 goto exit;
1064 }
1065
1066exit:
1067 mbedtls_platform_zeroize(exp_key, exp_key_size);
1068#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
1069 mbedtls_free(exp_key);
1070#endif
1071 psa_reset_key_attributes(&key_attr);
1072
1073 return ret;
1074}
1075
1076int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
1077 mbedtls_pk_context *pk)
1078{
1079 return copy_from_psa(key_id, pk, 0);
1080}
1081
1082int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
1083 mbedtls_pk_context *pk)
1084{
1085 return copy_from_psa(key_id, pk, 1);
1086}
1087#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
1088
1089/*
1090 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
1091 */
1092static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
1093{
1094 if (*hash_len != 0) {
1095 return 0;
1096 }
1097
1098 *hash_len = mbedtls_md_get_size_from_type(md_alg);
1099
1100 if (*hash_len == 0) {
1101 return -1;
1102 }
1103
1104 return 0;
1105}
1106
1107#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1108/*
1109 * Helper to set up a restart context if needed
1110 */
1111static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
1112 const mbedtls_pk_info_t *info)
1113{
1114 /* Don't do anything if already set up or invalid */
1115 if (ctx == NULL || ctx->pk_info != NULL) {
1116 return 0;
1117 }
1118
1119 /* Should never happen when we're called */
1120 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
1121 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1122 }
1123
1124 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
1125 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1126 }
1127
1128 ctx->pk_info = info;
1129
1130 return 0;
1131}
1132#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1133
1134/*
1135 * Verify a signature (restartable)
1136 */
1137int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
1138 mbedtls_md_type_t md_alg,
1139 const unsigned char *hash, size_t hash_len,
1140 const unsigned char *sig, size_t sig_len,
1141 mbedtls_pk_restart_ctx *rs_ctx)
1142{
1143 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1144 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1145 }
1146
1147 if (ctx->pk_info == NULL ||
1148 pk_hashlen_helper(md_alg, &hash_len) != 0) {
1149 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1150 }
1151
1152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1153 /* optimization: use non-restartable version if restart disabled */
1154 if (rs_ctx != NULL &&
1155 mbedtls_ecp_restart_is_enabled() &&
1156 ctx->pk_info->verify_rs_func != NULL) {
1157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1158
1159 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1160 return ret;
1161 }
1162
1163 ret = ctx->pk_info->verify_rs_func(ctx,
1164 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
1165
1166 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1167 mbedtls_pk_restart_free(rs_ctx);
1168 }
1169
1170 return ret;
1171 }
1172#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1173 (void) rs_ctx;
1174#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1175
1176 if (ctx->pk_info->verify_func == NULL) {
1177 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1178 }
1179
1180 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
1181 sig, sig_len);
1182}
1183
1184/*
1185 * Verify a signature
1186 */
1187int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1188 const unsigned char *hash, size_t hash_len,
1189 const unsigned char *sig, size_t sig_len)
1190{
1191 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
1192 sig, sig_len, NULL);
1193}
1194
1195/*
1196 * Verify a signature with options
1197 */
1198int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
1199 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1200 const unsigned char *hash, size_t hash_len,
1201 const unsigned char *sig, size_t sig_len)
1202{
1203 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1204 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1205 }
1206
1207 if (ctx->pk_info == NULL) {
1208 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1209 }
1210
1211 if (!mbedtls_pk_can_do(ctx, type)) {
1212 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1213 }
1214
1215 if (type != MBEDTLS_PK_RSASSA_PSS) {
1216 /* General case: no options */
1217 if (options != NULL) {
1218 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1219 }
1220
1221 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
1222 }
1223
1224 /* Ensure the PK context is of the right type otherwise mbedtls_pk_rsa()
1225 * below would return a NULL pointer. */
1226 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
1227 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1228 }
1229
1230#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1232 const mbedtls_pk_rsassa_pss_options *pss_opts;
1233
1234#if SIZE_MAX > UINT_MAX
1235 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1236 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1237 }
1238#endif
1239
1240 if (options == NULL) {
1241 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1242 }
1243
1244 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
1245
1246#if defined(MBEDTLS_USE_PSA_CRYPTO)
1247 if (pss_opts->mgf1_hash_id == md_alg) {
1248 unsigned char buf[PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
1249 unsigned char *p;
1250 int key_len;
1251 size_t signature_length;
1252 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1253 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
1254
1255 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
1256 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1258 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1259 p = buf + sizeof(buf);
1260 key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
1261
1262 if (key_len < 0) {
1263 return key_len;
1264 }
1265
1266 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1267 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1268 psa_set_key_algorithm(&attributes, psa_sig_alg);
1269
1270 status = psa_import_key(&attributes,
1271 buf + sizeof(buf) - key_len, key_len,
1272 &key_id);
1273 if (status != PSA_SUCCESS) {
1274 psa_destroy_key(key_id);
1275 return PSA_PK_TO_MBEDTLS_ERR(status);
1276 }
1277
1278 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1279 * on a valid signature with trailing data in a buffer, but
1280 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1281 * so for this reason the passed sig_len is overwritten. Smaller
1282 * signature lengths should not be accepted for verification. */
1283 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1284 mbedtls_pk_get_len(ctx) : sig_len;
1285 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1286 hash_len, sig, signature_length);
1287 destruction_status = psa_destroy_key(key_id);
1288
1289 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1290 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1291 }
1292
1293 if (status == PSA_SUCCESS) {
1294 status = destruction_status;
1295 }
1296
1297 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1298 } else
1299#endif /* MBEDTLS_USE_PSA_CRYPTO */
1300 {
1301 if (sig_len < mbedtls_pk_get_len(ctx)) {
1302 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1303 }
1304
1305 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1306 md_alg, (unsigned int) hash_len, hash,
1307 pss_opts->mgf1_hash_id,
1308 pss_opts->expected_salt_len,
1309 sig);
1310 if (ret != 0) {
1311 return ret;
1312 }
1313
1314 if (sig_len > mbedtls_pk_get_len(ctx)) {
1315 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1316 }
1317
1318 return 0;
1319 }
1320#else
1321 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1322#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
1323}
1324
1325/*
1326 * Make a signature (restartable)
1327 */
1328int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1329 mbedtls_md_type_t md_alg,
1330 const unsigned char *hash, size_t hash_len,
1331 unsigned char *sig, size_t sig_size, size_t *sig_len,
1332 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1333 mbedtls_pk_restart_ctx *rs_ctx)
1334{
1335 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1336 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1337 }
1338
1339 if (ctx == NULL) return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1340 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1341 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1342 }
1343
1344#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1345 /* optimization: use non-restartable version if restart disabled */
1346 if (rs_ctx != NULL &&
1347 mbedtls_ecp_restart_is_enabled() &&
1348 ctx->pk_info->sign_rs_func != NULL) {
1349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1350
1351 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1352 return ret;
1353 }
1354
1355 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
1356 hash, hash_len,
1357 sig, sig_size, sig_len,
1358 f_rng, p_rng, rs_ctx->rs_ctx);
1359
1360 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1361 mbedtls_pk_restart_free(rs_ctx);
1362 }
1363
1364 return ret;
1365 }
1366#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1367 (void) rs_ctx;
1368#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1369
1370 if (ctx->pk_info->sign_func == NULL) {
1371 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1372 }
1373
1374 return ctx->pk_info->sign_func(ctx, md_alg,
1375 hash, hash_len,
1376 sig, sig_size, sig_len,
1377 f_rng, p_rng);
1378}
1379
1380/*
1381 * Make a signature
1382 */
1383int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1384 const unsigned char *hash, size_t hash_len,
1385 unsigned char *sig, size_t sig_size, size_t *sig_len,
1386 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1387{
1388 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1389 sig, sig_size, sig_len,
1390 f_rng, p_rng, NULL);
1391}
1392
1393/*
1394 * Make a signature given a signature type.
1395 */
1396int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1397 mbedtls_pk_context *ctx,
1398 mbedtls_md_type_t md_alg,
1399 const unsigned char *hash, size_t hash_len,
1400 unsigned char *sig, size_t sig_size, size_t *sig_len,
1401 int (*f_rng)(void *, unsigned char *, size_t),
1402 void *p_rng)
1403{
1404 if (ctx->pk_info == NULL) {
1405 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1406 }
1407
1408 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1409 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1410 }
1411
1412 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1413 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1414 sig, sig_size, sig_len, f_rng, p_rng);
1415 }
1416
1417#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1418
1419#if defined(MBEDTLS_USE_PSA_CRYPTO)
1420 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
1421 if (psa_md_alg == 0) {
1422 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1423 }
1424
1425 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
1426 psa_status_t status;
1427
1428 /* PSA_ALG_RSA_PSS() behaves the same as PSA_ALG_RSA_PSS_ANY_SALT() when
1429 * performing a signature, but they are encoded differently. Instead of
1430 * extracting the proper one from the wrapped key policy, just try both. */
1431 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
1432 hash, hash_len,
1433 sig, sig_size, sig_len);
1434 if (status == PSA_ERROR_NOT_PERMITTED) {
1435 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg),
1436 hash, hash_len,
1437 sig, sig_size, sig_len);
1438 }
1439 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1440 }
1441
1442 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1443 ctx->pk_ctx, hash, hash_len,
1444 sig, sig_size, sig_len);
1445#else /* MBEDTLS_USE_PSA_CRYPTO */
1446
1447 if (sig_size < mbedtls_pk_get_len(ctx)) {
1448 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1449 }
1450
1451 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1452 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1453 }
1454
1455 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1456
1457 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1458 (unsigned int) hash_len, hash, sig);
1459 if (ret == 0) {
1460 *sig_len = rsa_ctx->len;
1461 }
1462 return ret;
1463
1464#endif /* MBEDTLS_USE_PSA_CRYPTO */
1465
1466#else
1467 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1468#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
1469}
1470
1471/*
1472 * Decrypt message
1473 */
1474int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1475 const unsigned char *input, size_t ilen,
1476 unsigned char *output, size_t *olen, size_t osize,
1477 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1478{
1479 if (ctx->pk_info == NULL) {
1480 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1481 }
1482
1483 if (ctx->pk_info->decrypt_func == NULL) {
1484 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1485 }
1486
1487 return ctx->pk_info->decrypt_func(ctx, input, ilen,
1488 output, olen, osize, f_rng, p_rng);
1489}
1490
1491/*
1492 * Encrypt message
1493 */
1494int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1495 const unsigned char *input, size_t ilen,
1496 unsigned char *output, size_t *olen, size_t osize,
1497 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1498{
1499 if (ctx->pk_info == NULL) {
1500 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1501 }
1502
1503 if (ctx->pk_info->encrypt_func == NULL) {
1504 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1505 }
1506
1507 return ctx->pk_info->encrypt_func(ctx, input, ilen,
1508 output, olen, osize, f_rng, p_rng);
1509}
1510
1511/*
1512 * Check public-private key pair
1513 */
1514int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1515 const mbedtls_pk_context *prv,
1516 int (*f_rng)(void *, unsigned char *, size_t),
1517 void *p_rng)
1518{
1519 if (pub->pk_info == NULL ||
1520 prv->pk_info == NULL) {
1521 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1522 }
1523
1524 if (f_rng == NULL) {
1525 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1526 }
1527
1528 if (prv->pk_info->check_pair_func == NULL) {
1529 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1530 }
1531
1532 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1533 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1534 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1535 }
1536 } else {
1537 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1538 (pub->pk_info != prv->pk_info)) {
1539 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1540 }
1541 }
1542
1543 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1544 (mbedtls_pk_context *) prv,
1545 f_rng, p_rng);
1546}
1547
1548/*
1549 * Get key size in bits
1550 */
1551size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
1552{
1553 /* For backward compatibility, accept NULL or a context that
1554 * isn't set up yet, and return a fake value that should be safe. */
1555 if (ctx == NULL || ctx->pk_info == NULL) {
1556 return 0;
1557 }
1558
1559 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
1560}
1561
1562/*
1563 * Export debug information
1564 */
1565int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
1566{
1567 if (ctx->pk_info == NULL) {
1568 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1569 }
1570
1571 if (ctx->pk_info->debug_func == NULL) {
1572 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1573 }
1574
1575 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
1576 return 0;
1577}
1578
1579/*
1580 * Access the PK type name
1581 */
1582const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
1583{
1584 if (ctx == NULL || ctx->pk_info == NULL) {
1585 return "invalid PK";
1586 }
1587
1588 return ctx->pk_info->name;
1589}
1590
1591/*
1592 * Access the PK type
1593 */
1594mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
1595{
1596 if (ctx == NULL || ctx->pk_info == NULL) {
1597 return MBEDTLS_PK_NONE;
1598 }
1599
1600 return ctx->pk_info->type;
1601}
1602
1603#endif /* MBEDTLS_PK_C */
1604