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