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 char *supported_ciph
= NULL
;
30 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
34 evp_pkey_ctx_free_old_ops(ctx
);
35 ctx
->operation
= operation
;
38 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
39 * calls can be removed.
43 if (evp_pkey_ctx_is_legacy(ctx
))
47 * Ensure that the key is provided, either natively, or as a cached export.
50 tmp_keymgmt
= ctx
->keymgmt
;
51 provkey
= evp_pkey_export_to_provider(ctx
->pkey
, ctx
->libctx
,
52 &tmp_keymgmt
, ctx
->propquery
);
55 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) {
56 ERR_clear_last_mark();
57 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
60 EVP_KEYMGMT_free(ctx
->keymgmt
);
61 ctx
->keymgmt
= tmp_keymgmt
;
63 if (ctx
->keymgmt
->query_operation_name
!= NULL
)
65 ctx
->keymgmt
->query_operation_name(OSSL_OP_ASYM_CIPHER
);
68 * If we didn't get a supported ciph, assume there is one with the
69 * same name as the key type.
71 if (supported_ciph
== NULL
)
72 supported_ciph
= ctx
->keytype
;
75 * Because we cleared out old ops, we shouldn't need to worry about
76 * checking if cipher is already there.
79 EVP_ASYM_CIPHER_fetch(ctx
->libctx
, supported_ciph
, ctx
->propquery
);
82 || (EVP_KEYMGMT_provider(ctx
->keymgmt
)
83 != EVP_ASYM_CIPHER_provider(cipher
))) {
85 * We don't need to free ctx->keymgmt here, as it's not necessarily
86 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
88 EVP_ASYM_CIPHER_free(cipher
);
93 * TODO remove this when legacy is gone
94 * If we don't have the full support we need with provided methods,
95 * let's go see if legacy does.
99 /* No more legacy from here down to legacy: */
101 ctx
->op
.ciph
.cipher
= cipher
;
102 ctx
->op
.ciph
.algctx
= cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
103 if (ctx
->op
.ciph
.algctx
== NULL
) {
104 /* The provider key can stay in the cache */
105 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
110 case EVP_PKEY_OP_ENCRYPT
:
111 if (cipher
->encrypt_init
== NULL
) {
112 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
116 ret
= cipher
->encrypt_init(ctx
->op
.ciph
.algctx
, provkey
, params
);
118 case EVP_PKEY_OP_DECRYPT
:
119 if (cipher
->decrypt_init
== NULL
) {
120 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
124 ret
= cipher
->decrypt_init(ctx
->op
.ciph
.algctx
, provkey
, params
);
127 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
137 * TODO remove this when legacy is gone
138 * If we don't have the full support we need with provided methods,
139 * let's go see if legacy does.
143 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
144 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
147 switch(ctx
->operation
) {
148 case EVP_PKEY_OP_ENCRYPT
:
149 if (ctx
->pmeth
->encrypt_init
== NULL
)
151 ret
= ctx
->pmeth
->encrypt_init(ctx
);
153 case EVP_PKEY_OP_DECRYPT
:
154 if (ctx
->pmeth
->decrypt_init
== NULL
)
156 ret
= ctx
->pmeth
->decrypt_init(ctx
);
159 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
165 evp_pkey_ctx_free_old_ops(ctx
);
166 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
171 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX
*ctx
)
173 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_ENCRYPT
, NULL
);
176 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
178 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_ENCRYPT
, params
);
181 int EVP_PKEY_encrypt(EVP_PKEY_CTX
*ctx
,
182 unsigned char *out
, size_t *outlen
,
183 const unsigned char *in
, size_t inlen
)
188 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
192 if (ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
) {
193 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
197 if (ctx
->op
.ciph
.algctx
== NULL
)
200 ret
= ctx
->op
.ciph
.cipher
->encrypt(ctx
->op
.ciph
.algctx
, out
, outlen
,
201 (out
== NULL
? 0 : *outlen
), in
, inlen
);
205 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
206 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
209 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_ENCRYPT
)
210 return ctx
->pmeth
->encrypt(ctx
, out
, outlen
, in
, inlen
);
213 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX
*ctx
)
215 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_DECRYPT
, NULL
);
218 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
220 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_DECRYPT
, params
);
223 int EVP_PKEY_decrypt(EVP_PKEY_CTX
*ctx
,
224 unsigned char *out
, size_t *outlen
,
225 const unsigned char *in
, size_t inlen
)
230 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
234 if (ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
235 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
239 if (ctx
->op
.ciph
.algctx
== NULL
)
242 ret
= ctx
->op
.ciph
.cipher
->decrypt(ctx
->op
.ciph
.algctx
, out
, outlen
,
243 (out
== NULL
? 0 : *outlen
), in
, inlen
);
247 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->decrypt
== NULL
) {
248 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
251 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_DECRYPT
)
252 return ctx
->pmeth
->decrypt(ctx
, out
, outlen
, in
, inlen
);
256 static EVP_ASYM_CIPHER
*evp_asym_cipher_new(OSSL_PROVIDER
*prov
)
258 EVP_ASYM_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER
));
260 if (cipher
== NULL
) {
261 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
265 cipher
->lock
= CRYPTO_THREAD_lock_new();
266 if (cipher
->lock
== NULL
) {
267 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
268 OPENSSL_free(cipher
);
272 ossl_provider_up_ref(prov
);
278 static void *evp_asym_cipher_from_algorithm(int name_id
,
279 const OSSL_ALGORITHM
*algodef
,
282 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
283 EVP_ASYM_CIPHER
*cipher
= NULL
;
284 int ctxfncnt
= 0, encfncnt
= 0, decfncnt
= 0;
285 int gparamfncnt
= 0, sparamfncnt
= 0;
287 if ((cipher
= evp_asym_cipher_new(prov
)) == NULL
) {
288 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
292 cipher
->name_id
= name_id
;
293 if ((cipher
->type_name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
)
295 cipher
->description
= algodef
->algorithm_description
;
297 for (; fns
->function_id
!= 0; fns
++) {
298 switch (fns
->function_id
) {
299 case OSSL_FUNC_ASYM_CIPHER_NEWCTX
:
300 if (cipher
->newctx
!= NULL
)
302 cipher
->newctx
= OSSL_FUNC_asym_cipher_newctx(fns
);
305 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT
:
306 if (cipher
->encrypt_init
!= NULL
)
308 cipher
->encrypt_init
= OSSL_FUNC_asym_cipher_encrypt_init(fns
);
311 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT
:
312 if (cipher
->encrypt
!= NULL
)
314 cipher
->encrypt
= OSSL_FUNC_asym_cipher_encrypt(fns
);
317 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT
:
318 if (cipher
->decrypt_init
!= NULL
)
320 cipher
->decrypt_init
= OSSL_FUNC_asym_cipher_decrypt_init(fns
);
323 case OSSL_FUNC_ASYM_CIPHER_DECRYPT
:
324 if (cipher
->decrypt
!= NULL
)
326 cipher
->decrypt
= OSSL_FUNC_asym_cipher_decrypt(fns
);
329 case OSSL_FUNC_ASYM_CIPHER_FREECTX
:
330 if (cipher
->freectx
!= NULL
)
332 cipher
->freectx
= OSSL_FUNC_asym_cipher_freectx(fns
);
335 case OSSL_FUNC_ASYM_CIPHER_DUPCTX
:
336 if (cipher
->dupctx
!= NULL
)
338 cipher
->dupctx
= OSSL_FUNC_asym_cipher_dupctx(fns
);
340 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS
:
341 if (cipher
->get_ctx_params
!= NULL
)
343 cipher
->get_ctx_params
344 = OSSL_FUNC_asym_cipher_get_ctx_params(fns
);
347 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS
:
348 if (cipher
->gettable_ctx_params
!= NULL
)
350 cipher
->gettable_ctx_params
351 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns
);
354 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS
:
355 if (cipher
->set_ctx_params
!= NULL
)
357 cipher
->set_ctx_params
358 = OSSL_FUNC_asym_cipher_set_ctx_params(fns
);
361 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS
:
362 if (cipher
->settable_ctx_params
!= NULL
)
364 cipher
->settable_ctx_params
365 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns
);
371 || (encfncnt
!= 0 && encfncnt
!= 2)
372 || (decfncnt
!= 0 && decfncnt
!= 2)
373 || (encfncnt
!= 2 && decfncnt
!= 2)
374 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
375 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
377 * In order to be a consistent set of functions we must have at least
378 * a set of context functions (newctx and freectx) as well as a pair of
379 * "cipher" functions: (encrypt_init, encrypt) or
380 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
381 * optional, but if one of them is present then the other one must also
382 * be present. The same applies to get_ctx_params and
383 * gettable_ctx_params. The dupctx function is optional.
385 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
391 EVP_ASYM_CIPHER_free(cipher
);
395 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER
*cipher
)
401 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
, cipher
->lock
);
404 OPENSSL_free(cipher
->type_name
);
405 ossl_provider_free(cipher
->prov
);
406 CRYPTO_THREAD_lock_free(cipher
->lock
);
407 OPENSSL_free(cipher
);
410 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER
*cipher
)
414 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
, cipher
->lock
);
418 OSSL_PROVIDER
*EVP_ASYM_CIPHER_provider(const EVP_ASYM_CIPHER
*cipher
)
423 EVP_ASYM_CIPHER
*EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
424 const char *properties
)
426 return evp_generic_fetch(ctx
, OSSL_OP_ASYM_CIPHER
, algorithm
, properties
,
427 evp_asym_cipher_from_algorithm
,
428 (int (*)(void *))EVP_ASYM_CIPHER_up_ref
,
429 (void (*)(void *))EVP_ASYM_CIPHER_free
);
432 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER
*cipher
, const char *name
)
434 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
437 int EVP_ASYM_CIPHER_number(const EVP_ASYM_CIPHER
*cipher
)
439 return cipher
->name_id
;
442 const char *EVP_ASYM_CIPHER_name(const EVP_ASYM_CIPHER
*cipher
)
444 return cipher
->type_name
;
447 const char *EVP_ASYM_CIPHER_description(const EVP_ASYM_CIPHER
*cipher
)
449 return cipher
->description
;
452 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
453 void (*fn
)(EVP_ASYM_CIPHER
*cipher
,
457 evp_generic_do_all(libctx
, OSSL_OP_ASYM_CIPHER
,
458 (void (*)(void *, void *))fn
, arg
,
459 evp_asym_cipher_from_algorithm
,
460 (void (*)(void *))EVP_ASYM_CIPHER_free
);
464 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER
*cipher
,
465 void (*fn
)(const char *name
, void *data
),
468 if (cipher
->prov
!= NULL
)
469 return evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
474 const OSSL_PARAM
*EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER
*cip
)
478 if (cip
== NULL
|| cip
->gettable_ctx_params
== NULL
)
481 provctx
= ossl_provider_ctx(EVP_ASYM_CIPHER_provider(cip
));
482 return cip
->gettable_ctx_params(NULL
, provctx
);
485 const OSSL_PARAM
*EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER
*cip
)
489 if (cip
== NULL
|| cip
->settable_ctx_params
== NULL
)
492 provctx
= ossl_provider_ctx(EVP_ASYM_CIPHER_provider(cip
));
493 return cip
->settable_ctx_params(NULL
, provctx
);