2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "internal/provider.h"
16 #include "internal/core.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
20 static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX
*ctx
, int operation
,
21 const OSSL_PARAM params
[])
25 EVP_ASYM_CIPHER
*cipher
= NULL
;
26 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
27 const OSSL_PROVIDER
*tmp_prov
= NULL
;
28 const char *supported_ciph
= NULL
;
32 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
36 evp_pkey_ctx_free_old_ops(ctx
);
37 ctx
->operation
= operation
;
41 if (evp_pkey_ctx_is_legacy(ctx
))
44 if (ctx
->pkey
== NULL
) {
45 ERR_clear_last_mark();
46 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_KEY_SET
);
51 * Try to derive the supported asym cipher from |ctx->keymgmt|.
53 if (!ossl_assert(ctx
->pkey
->keymgmt
== NULL
54 || ctx
->pkey
->keymgmt
== ctx
->keymgmt
)) {
55 ERR_clear_last_mark();
56 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
60 = evp_keymgmt_util_query_operation_name(ctx
->keymgmt
,
62 if (supported_ciph
== NULL
) {
63 ERR_clear_last_mark();
64 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
69 * We perform two iterations:
71 * 1. Do the normal asym cipher fetch, using the fetching data given by
73 * 2. Do the provider specific asym cipher fetch, from the same provider
76 * We then try to fetch the keymgmt from the same provider as the
77 * asym cipher, and try to export |ctx->pkey| to that keymgmt (when
78 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
79 * is a no-op, but we call it anyway to not complicate the code even
81 * If the export call succeeds (returns a non-NULL provider key pointer),
82 * we're done and can perform the operation itself. If not, we perform
83 * the second iteration, or jump to legacy.
85 for (iter
= 1, provkey
= NULL
; iter
< 3 && provkey
== NULL
; iter
++) {
86 EVP_KEYMGMT
*tmp_keymgmt_tofree
;
89 * If we're on the second iteration, free the results from the first.
90 * They are NULL on the first iteration, so no need to check what
93 EVP_ASYM_CIPHER_free(cipher
);
94 EVP_KEYMGMT_free(tmp_keymgmt
);
98 cipher
= EVP_ASYM_CIPHER_fetch(ctx
->libctx
, supported_ciph
,
101 tmp_prov
= EVP_ASYM_CIPHER_get0_provider(cipher
);
104 tmp_prov
= EVP_KEYMGMT_get0_provider(ctx
->keymgmt
);
106 evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER
*)tmp_prov
,
107 supported_ciph
, ctx
->propquery
);
116 * Ensure that the key is provided, either natively, or as a cached
117 * export. We start by fetching the keymgmt with the same name as
118 * |ctx->pkey|, but from the provider of the asym cipher method, using
119 * the same property query as when fetching the asym cipher method.
120 * With the keymgmt we found (if we did), we try to export |ctx->pkey|
121 * to it (evp_pkey_export_to_provider() is smart enough to only actually
122 * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
124 tmp_keymgmt_tofree
= tmp_keymgmt
125 = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER
*)tmp_prov
,
126 EVP_KEYMGMT_get0_name(ctx
->keymgmt
),
128 if (tmp_keymgmt
!= NULL
)
129 provkey
= evp_pkey_export_to_provider(ctx
->pkey
, ctx
->libctx
,
130 &tmp_keymgmt
, ctx
->propquery
);
131 if (tmp_keymgmt
== NULL
)
132 EVP_KEYMGMT_free(tmp_keymgmt_tofree
);
135 if (provkey
== NULL
) {
136 EVP_ASYM_CIPHER_free(cipher
);
142 /* No more legacy from here down to legacy: */
144 ctx
->op
.ciph
.cipher
= cipher
;
145 ctx
->op
.ciph
.algctx
= cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
146 if (ctx
->op
.ciph
.algctx
== NULL
) {
147 /* The provider key can stay in the cache */
148 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
153 case EVP_PKEY_OP_ENCRYPT
:
154 if (cipher
->encrypt_init
== NULL
) {
155 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
159 ret
= cipher
->encrypt_init(ctx
->op
.ciph
.algctx
, provkey
, params
);
161 case EVP_PKEY_OP_DECRYPT
:
162 if (cipher
->decrypt_init
== NULL
) {
163 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
167 ret
= cipher
->decrypt_init(ctx
->op
.ciph
.algctx
, provkey
, params
);
170 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
176 EVP_KEYMGMT_free(tmp_keymgmt
);
181 * If we don't have the full support we need with provided methods,
182 * let's go see if legacy does.
185 EVP_KEYMGMT_free(tmp_keymgmt
);
188 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
189 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
192 switch (ctx
->operation
) {
193 case EVP_PKEY_OP_ENCRYPT
:
194 if (ctx
->pmeth
->encrypt_init
== NULL
)
196 ret
= ctx
->pmeth
->encrypt_init(ctx
);
198 case EVP_PKEY_OP_DECRYPT
:
199 if (ctx
->pmeth
->decrypt_init
== NULL
)
201 ret
= ctx
->pmeth
->decrypt_init(ctx
);
204 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
210 evp_pkey_ctx_free_old_ops(ctx
);
211 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
213 EVP_KEYMGMT_free(tmp_keymgmt
);
217 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX
*ctx
)
219 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_ENCRYPT
, NULL
);
222 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
224 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_ENCRYPT
, params
);
227 int EVP_PKEY_encrypt(EVP_PKEY_CTX
*ctx
,
228 unsigned char *out
, size_t *outlen
,
229 const unsigned char *in
, size_t inlen
)
234 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
238 if (ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
) {
239 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
243 if (ctx
->op
.ciph
.algctx
== NULL
)
246 ret
= ctx
->op
.ciph
.cipher
->encrypt(ctx
->op
.ciph
.algctx
, out
, outlen
,
247 (out
== NULL
? 0 : *outlen
), in
, inlen
);
251 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
252 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
255 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_ENCRYPT
)
256 return ctx
->pmeth
->encrypt(ctx
, out
, outlen
, in
, inlen
);
259 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX
*ctx
)
261 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_DECRYPT
, NULL
);
264 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
266 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_DECRYPT
, params
);
269 int EVP_PKEY_decrypt(EVP_PKEY_CTX
*ctx
,
270 unsigned char *out
, size_t *outlen
,
271 const unsigned char *in
, size_t inlen
)
276 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
280 if (ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
281 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
285 if (ctx
->op
.ciph
.algctx
== NULL
)
288 ret
= ctx
->op
.ciph
.cipher
->decrypt(ctx
->op
.ciph
.algctx
, out
, outlen
,
289 (out
== NULL
? 0 : *outlen
), in
, inlen
);
293 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->decrypt
== NULL
) {
294 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
297 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_DECRYPT
)
298 return ctx
->pmeth
->decrypt(ctx
, out
, outlen
, in
, inlen
);
301 /* decrypt to new buffer of dynamic size, checking any pre-determined size */
302 int evp_pkey_decrypt_alloc(EVP_PKEY_CTX
*ctx
, unsigned char **outp
,
303 size_t *outlenp
, size_t expected_outlen
,
304 const unsigned char *in
, size_t inlen
)
306 if (EVP_PKEY_decrypt(ctx
, NULL
, outlenp
, in
, inlen
) <= 0
307 || (*outp
= OPENSSL_malloc(*outlenp
)) == NULL
)
309 if (EVP_PKEY_decrypt(ctx
, *outp
, outlenp
, in
, inlen
) <= 0
311 || (expected_outlen
!= 0 && *outlenp
!= expected_outlen
)) {
312 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
313 OPENSSL_clear_free(*outp
, *outlenp
);
320 static EVP_ASYM_CIPHER
*evp_asym_cipher_new(OSSL_PROVIDER
*prov
)
322 EVP_ASYM_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER
));
327 if (!CRYPTO_NEW_REF(&cipher
->refcnt
, 1)) {
328 OPENSSL_free(cipher
);
332 ossl_provider_up_ref(prov
);
337 static void *evp_asym_cipher_from_algorithm(int name_id
,
338 const OSSL_ALGORITHM
*algodef
,
341 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
342 EVP_ASYM_CIPHER
*cipher
= NULL
;
343 int ctxfncnt
= 0, encfncnt
= 0, decfncnt
= 0;
344 int gparamfncnt
= 0, sparamfncnt
= 0;
346 if ((cipher
= evp_asym_cipher_new(prov
)) == NULL
) {
347 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
351 cipher
->name_id
= name_id
;
352 if ((cipher
->type_name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
)
354 cipher
->description
= algodef
->algorithm_description
;
356 for (; fns
->function_id
!= 0; fns
++) {
357 switch (fns
->function_id
) {
358 case OSSL_FUNC_ASYM_CIPHER_NEWCTX
:
359 if (cipher
->newctx
!= NULL
)
361 cipher
->newctx
= OSSL_FUNC_asym_cipher_newctx(fns
);
364 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT
:
365 if (cipher
->encrypt_init
!= NULL
)
367 cipher
->encrypt_init
= OSSL_FUNC_asym_cipher_encrypt_init(fns
);
370 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT
:
371 if (cipher
->encrypt
!= NULL
)
373 cipher
->encrypt
= OSSL_FUNC_asym_cipher_encrypt(fns
);
376 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT
:
377 if (cipher
->decrypt_init
!= NULL
)
379 cipher
->decrypt_init
= OSSL_FUNC_asym_cipher_decrypt_init(fns
);
382 case OSSL_FUNC_ASYM_CIPHER_DECRYPT
:
383 if (cipher
->decrypt
!= NULL
)
385 cipher
->decrypt
= OSSL_FUNC_asym_cipher_decrypt(fns
);
388 case OSSL_FUNC_ASYM_CIPHER_FREECTX
:
389 if (cipher
->freectx
!= NULL
)
391 cipher
->freectx
= OSSL_FUNC_asym_cipher_freectx(fns
);
394 case OSSL_FUNC_ASYM_CIPHER_DUPCTX
:
395 if (cipher
->dupctx
!= NULL
)
397 cipher
->dupctx
= OSSL_FUNC_asym_cipher_dupctx(fns
);
399 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS
:
400 if (cipher
->get_ctx_params
!= NULL
)
402 cipher
->get_ctx_params
403 = OSSL_FUNC_asym_cipher_get_ctx_params(fns
);
406 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS
:
407 if (cipher
->gettable_ctx_params
!= NULL
)
409 cipher
->gettable_ctx_params
410 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns
);
413 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS
:
414 if (cipher
->set_ctx_params
!= NULL
)
416 cipher
->set_ctx_params
417 = OSSL_FUNC_asym_cipher_set_ctx_params(fns
);
420 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS
:
421 if (cipher
->settable_ctx_params
!= NULL
)
423 cipher
->settable_ctx_params
424 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns
);
430 || (encfncnt
!= 0 && encfncnt
!= 2)
431 || (decfncnt
!= 0 && decfncnt
!= 2)
432 || (encfncnt
!= 2 && decfncnt
!= 2)
433 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
434 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
436 * In order to be a consistent set of functions we must have at least
437 * a set of context functions (newctx and freectx) as well as a pair of
438 * "cipher" functions: (encrypt_init, encrypt) or
439 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
440 * optional, but if one of them is present then the other one must also
441 * be present. The same applies to get_ctx_params and
442 * gettable_ctx_params. The dupctx function is optional.
444 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
450 EVP_ASYM_CIPHER_free(cipher
);
454 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER
*cipher
)
460 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
);
463 OPENSSL_free(cipher
->type_name
);
464 ossl_provider_free(cipher
->prov
);
465 CRYPTO_FREE_REF(&cipher
->refcnt
);
466 OPENSSL_free(cipher
);
469 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER
*cipher
)
473 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
);
477 OSSL_PROVIDER
*EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER
*cipher
)
482 EVP_ASYM_CIPHER
*EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
483 const char *properties
)
485 return evp_generic_fetch(ctx
, OSSL_OP_ASYM_CIPHER
, algorithm
, properties
,
486 evp_asym_cipher_from_algorithm
,
487 (int (*)(void *))EVP_ASYM_CIPHER_up_ref
,
488 (void (*)(void *))EVP_ASYM_CIPHER_free
);
491 EVP_ASYM_CIPHER
*evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER
*prov
,
492 const char *algorithm
,
493 const char *properties
)
495 return evp_generic_fetch_from_prov(prov
, OSSL_OP_ASYM_CIPHER
,
496 algorithm
, properties
,
497 evp_asym_cipher_from_algorithm
,
498 (int (*)(void *))EVP_ASYM_CIPHER_up_ref
,
499 (void (*)(void *))EVP_ASYM_CIPHER_free
);
502 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER
*cipher
, const char *name
)
504 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
507 int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER
*cipher
)
509 return cipher
->name_id
;
512 const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER
*cipher
)
514 return cipher
->type_name
;
517 const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER
*cipher
)
519 return cipher
->description
;
522 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
523 void (*fn
)(EVP_ASYM_CIPHER
*cipher
,
527 evp_generic_do_all(libctx
, OSSL_OP_ASYM_CIPHER
,
528 (void (*)(void *, void *))fn
, arg
,
529 evp_asym_cipher_from_algorithm
,
530 (int (*)(void *))EVP_ASYM_CIPHER_up_ref
,
531 (void (*)(void *))EVP_ASYM_CIPHER_free
);
535 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER
*cipher
,
536 void (*fn
)(const char *name
, void *data
),
539 if (cipher
->prov
!= NULL
)
540 return evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
545 const OSSL_PARAM
*EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER
*cip
)
549 if (cip
== NULL
|| cip
->gettable_ctx_params
== NULL
)
552 provctx
= ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip
));
553 return cip
->gettable_ctx_params(NULL
, provctx
);
556 const OSSL_PARAM
*EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER
*cip
)
560 if (cip
== NULL
|| cip
->settable_ctx_params
== NULL
)
563 provctx
= ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip
));
564 return cip
->settable_ctx_params(NULL
, provctx
);