2 * Copyright 2006-2016 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 "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
19 static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX
*ctx
, int operation
)
23 EVP_ASYM_CIPHER
*cipher
= NULL
;
24 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
25 const char *supported_ciph
= NULL
;
28 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
32 evp_pkey_ctx_free_old_ops(ctx
);
33 ctx
->operation
= operation
;
36 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
37 * calls can be removed.
41 if (ctx
->keytype
== NULL
|| ctx
->engine
!= NULL
)
44 /* Ensure that the key is provided. If not, go legacy */
45 tmp_keymgmt
= ctx
->keymgmt
;
46 provkey
= evp_pkey_make_provided(ctx
->pkey
, ctx
->libctx
,
47 &tmp_keymgmt
, ctx
->propquery
, 0);
50 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) {
51 ERR_clear_last_mark();
52 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
55 EVP_KEYMGMT_free(ctx
->keymgmt
);
56 ctx
->keymgmt
= tmp_keymgmt
;
58 if (ctx
->keymgmt
->query_operation_name
!= NULL
)
60 ctx
->keymgmt
->query_operation_name(OSSL_OP_ASYM_CIPHER
);
63 * If we didn't get a supported ciph, assume there is one with the
64 * same name as the key type.
66 if (supported_ciph
== NULL
)
67 supported_ciph
= ctx
->keytype
;
70 * Because we cleared out old ops, we shouldn't need to worry about
71 * checking if cipher is already there.
74 EVP_ASYM_CIPHER_fetch(ctx
->libctx
, supported_ciph
, ctx
->propquery
);
77 || (EVP_KEYMGMT_provider(ctx
->keymgmt
)
78 != EVP_ASYM_CIPHER_provider(cipher
))) {
80 * We don't need to free ctx->keymgmt here, as it's not necessarily
81 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
83 EVP_ASYM_CIPHER_free(cipher
);
88 * TODO remove this when legacy is gone
89 * If we don't have the full support we need with provided methods,
90 * let's go see if legacy does.
94 /* No more legacy from here down to legacy: */
96 ctx
->op
.ciph
.cipher
= cipher
;
97 ctx
->op
.ciph
.ciphprovctx
= cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
98 if (ctx
->op
.ciph
.ciphprovctx
== NULL
) {
99 /* The provider key can stay in the cache */
100 EVPerr(0, EVP_R_INITIALIZATION_ERROR
);
105 case EVP_PKEY_OP_ENCRYPT
:
106 if (cipher
->encrypt_init
== NULL
) {
107 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
111 ret
= cipher
->encrypt_init(ctx
->op
.ciph
.ciphprovctx
, provkey
);
113 case EVP_PKEY_OP_DECRYPT
:
114 if (cipher
->decrypt_init
== NULL
) {
115 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
119 ret
= cipher
->decrypt_init(ctx
->op
.ciph
.ciphprovctx
, provkey
);
122 EVPerr(0, EVP_R_INITIALIZATION_ERROR
);
127 cipher
->freectx(ctx
->op
.ciph
.ciphprovctx
);
128 ctx
->op
.ciph
.ciphprovctx
= NULL
;
135 * TODO remove this when legacy is gone
136 * If we don't have the full support we need with provided methods,
137 * let's go see if legacy does.
141 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
142 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
145 switch(ctx
->operation
) {
146 case EVP_PKEY_OP_ENCRYPT
:
147 if (ctx
->pmeth
->encrypt_init
== NULL
)
149 ret
= ctx
->pmeth
->encrypt_init(ctx
);
151 case EVP_PKEY_OP_DECRYPT
:
152 if (ctx
->pmeth
->decrypt_init
== NULL
)
154 ret
= ctx
->pmeth
->decrypt_init(ctx
);
157 EVPerr(0, EVP_R_INITIALIZATION_ERROR
);
163 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
167 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX
*ctx
)
169 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_ENCRYPT
);
172 int EVP_PKEY_encrypt(EVP_PKEY_CTX
*ctx
,
173 unsigned char *out
, size_t *outlen
,
174 const unsigned char *in
, size_t inlen
)
179 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
183 if (ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
) {
184 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED
);
188 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
191 ret
= ctx
->op
.ciph
.cipher
->encrypt(ctx
->op
.ciph
.ciphprovctx
, out
, outlen
,
192 (out
== NULL
? 0 : *outlen
), in
, inlen
);
196 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->encrypt
== NULL
) {
197 EVPerr(EVP_F_EVP_PKEY_ENCRYPT
,
198 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
201 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_ENCRYPT
)
202 return ctx
->pmeth
->encrypt(ctx
, out
, outlen
, in
, inlen
);
205 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX
*ctx
)
207 return evp_pkey_asym_cipher_init(ctx
, EVP_PKEY_OP_DECRYPT
);
210 int EVP_PKEY_decrypt(EVP_PKEY_CTX
*ctx
,
211 unsigned char *out
, size_t *outlen
,
212 const unsigned char *in
, size_t inlen
)
217 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
221 if (ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
222 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED
);
226 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
229 ret
= ctx
->op
.ciph
.cipher
->decrypt(ctx
->op
.ciph
.ciphprovctx
, out
, outlen
,
230 (out
== NULL
? 0 : *outlen
), in
, inlen
);
234 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->decrypt
== NULL
) {
235 EVPerr(EVP_F_EVP_PKEY_DECRYPT
,
236 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
239 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_DECRYPT
)
240 return ctx
->pmeth
->decrypt(ctx
, out
, outlen
, in
, inlen
);
244 static EVP_ASYM_CIPHER
*evp_asym_cipher_new(OSSL_PROVIDER
*prov
)
246 EVP_ASYM_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER
));
248 if (cipher
== NULL
) {
249 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
253 cipher
->lock
= CRYPTO_THREAD_lock_new();
254 if (cipher
->lock
== NULL
) {
255 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
256 OPENSSL_free(cipher
);
260 ossl_provider_up_ref(prov
);
266 static void *evp_asym_cipher_from_dispatch(int name_id
,
267 const OSSL_DISPATCH
*fns
,
270 EVP_ASYM_CIPHER
*cipher
= NULL
;
271 int ctxfncnt
= 0, encfncnt
= 0, decfncnt
= 0;
272 int gparamfncnt
= 0, sparamfncnt
= 0;
274 if ((cipher
= evp_asym_cipher_new(prov
)) == NULL
) {
275 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
279 cipher
->name_id
= name_id
;
281 for (; fns
->function_id
!= 0; fns
++) {
282 switch (fns
->function_id
) {
283 case OSSL_FUNC_ASYM_CIPHER_NEWCTX
:
284 if (cipher
->newctx
!= NULL
)
286 cipher
->newctx
= OSSL_get_OP_asym_cipher_newctx(fns
);
289 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT
:
290 if (cipher
->encrypt_init
!= NULL
)
292 cipher
->encrypt_init
= OSSL_get_OP_asym_cipher_encrypt_init(fns
);
295 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT
:
296 if (cipher
->encrypt
!= NULL
)
298 cipher
->encrypt
= OSSL_get_OP_asym_cipher_encrypt(fns
);
301 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT
:
302 if (cipher
->decrypt_init
!= NULL
)
304 cipher
->decrypt_init
= OSSL_get_OP_asym_cipher_decrypt_init(fns
);
307 case OSSL_FUNC_ASYM_CIPHER_DECRYPT
:
308 if (cipher
->decrypt
!= NULL
)
310 cipher
->decrypt
= OSSL_get_OP_asym_cipher_decrypt(fns
);
313 case OSSL_FUNC_ASYM_CIPHER_FREECTX
:
314 if (cipher
->freectx
!= NULL
)
316 cipher
->freectx
= OSSL_get_OP_asym_cipher_freectx(fns
);
319 case OSSL_FUNC_ASYM_CIPHER_DUPCTX
:
320 if (cipher
->dupctx
!= NULL
)
322 cipher
->dupctx
= OSSL_get_OP_asym_cipher_dupctx(fns
);
324 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS
:
325 if (cipher
->get_ctx_params
!= NULL
)
327 cipher
->get_ctx_params
328 = OSSL_get_OP_asym_cipher_get_ctx_params(fns
);
331 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS
:
332 if (cipher
->gettable_ctx_params
!= NULL
)
334 cipher
->gettable_ctx_params
335 = OSSL_get_OP_asym_cipher_gettable_ctx_params(fns
);
338 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS
:
339 if (cipher
->set_ctx_params
!= NULL
)
341 cipher
->set_ctx_params
342 = OSSL_get_OP_asym_cipher_set_ctx_params(fns
);
345 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS
:
346 if (cipher
->settable_ctx_params
!= NULL
)
348 cipher
->settable_ctx_params
349 = OSSL_get_OP_asym_cipher_settable_ctx_params(fns
);
355 || (encfncnt
!= 0 && encfncnt
!= 2)
356 || (decfncnt
!= 0 && decfncnt
!= 2)
357 || (encfncnt
!= 2 && decfncnt
!= 2)
358 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
359 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
361 * In order to be a consistent set of functions we must have at least
362 * a set of context functions (newctx and freectx) as well as a pair of
363 * "cipher" functions: (encrypt_init, encrypt) or
364 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
365 * optional, but if one of them is present then the other one must also
366 * be present. The same applies to get_ctx_params and
367 * gettable_ctx_params. The dupctx function is optional.
369 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
375 EVP_ASYM_CIPHER_free(cipher
);
379 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER
*cipher
)
381 if (cipher
!= NULL
) {
384 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
, cipher
->lock
);
387 ossl_provider_free(cipher
->prov
);
388 CRYPTO_THREAD_lock_free(cipher
->lock
);
389 OPENSSL_free(cipher
);
393 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER
*cipher
)
397 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
, cipher
->lock
);
401 OSSL_PROVIDER
*EVP_ASYM_CIPHER_provider(const EVP_ASYM_CIPHER
*cipher
)
406 EVP_ASYM_CIPHER
*EVP_ASYM_CIPHER_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
407 const char *properties
)
409 return evp_generic_fetch(ctx
, OSSL_OP_ASYM_CIPHER
, algorithm
, properties
,
410 evp_asym_cipher_from_dispatch
,
411 (int (*)(void *))EVP_ASYM_CIPHER_up_ref
,
412 (void (*)(void *))EVP_ASYM_CIPHER_free
);
415 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER
*cipher
, const char *name
)
417 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
420 int EVP_ASYM_CIPHER_number(const EVP_ASYM_CIPHER
*cipher
)
422 return cipher
->name_id
;
425 void EVP_ASYM_CIPHER_do_all_provided(OPENSSL_CTX
*libctx
,
426 void (*fn
)(EVP_ASYM_CIPHER
*cipher
,
430 evp_generic_do_all(libctx
, OSSL_OP_ASYM_CIPHER
,
431 (void (*)(void *, void *))fn
, arg
,
432 evp_asym_cipher_from_dispatch
,
433 (void (*)(void *))EVP_ASYM_CIPHER_free
);
437 void EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER
*cipher
,
438 void (*fn
)(const char *name
, void *data
),
441 if (cipher
->prov
!= NULL
)
442 evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);