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;
39 if (evp_pkey_ctx_is_legacy(ctx))
43 * Ensure that the key is provided, either natively, or as a cached export.
46 tmp_keymgmt = ctx->keymgmt;
47 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
48 &tmp_keymgmt, ctx->propquery);
51 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
52 ERR_clear_last_mark();
53 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
56 EVP_KEYMGMT_free(ctx->keymgmt);
57 ctx->keymgmt = tmp_keymgmt;
59 if (ctx->keymgmt->query_operation_name != NULL)
61 ctx->keymgmt->query_operation_name(OSSL_OP_ASYM_CIPHER);
64 * If we didn't get a supported ciph, assume there is one with the
65 * same name as the key type.
67 if (supported_ciph == NULL)
68 supported_ciph = ctx->keytype;
71 * Because we cleared out old ops, we shouldn't need to worry about
72 * checking if cipher is already there.
75 EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, ctx->propquery);
78 || (EVP_KEYMGMT_get0_provider(ctx->keymgmt)
79 != EVP_ASYM_CIPHER_get0_provider(cipher))) {
81 * We don't need to free ctx->keymgmt here, as it's not necessarily
82 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
84 EVP_ASYM_CIPHER_free(cipher);
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.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
98 if (ctx->op.ciph.algctx == NULL) {
99 /* The provider key can stay in the cache */
100 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
105 case EVP_PKEY_OP_ENCRYPT:
106 if (cipher->encrypt_init == NULL) {
107 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
111 ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
113 case EVP_PKEY_OP_DECRYPT:
114 if (cipher->decrypt_init == NULL) {
115 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
119 ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
122 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
132 * If we don't have the full support we need with provided methods,
133 * let's go see if legacy does.
137 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
138 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
141 switch(ctx->operation) {
142 case EVP_PKEY_OP_ENCRYPT:
143 if (ctx->pmeth->encrypt_init == NULL)
145 ret = ctx->pmeth->encrypt_init(ctx);
147 case EVP_PKEY_OP_DECRYPT:
148 if (ctx->pmeth->decrypt_init == NULL)
150 ret = ctx->pmeth->decrypt_init(ctx);
153 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
159 evp_pkey_ctx_free_old_ops(ctx);
160 ctx->operation = EVP_PKEY_OP_UNDEFINED;
165 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
167 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
170 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
172 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
175 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
176 unsigned char *out, size_t *outlen,
177 const unsigned char *in, size_t inlen)
182 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
186 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
187 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
191 if (ctx->op.ciph.algctx == NULL)
194 ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.algctx, out, outlen,
195 (out == NULL ? 0 : *outlen), in, inlen);
199 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
200 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
203 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
204 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
207 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
209 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
212 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
214 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
217 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
218 unsigned char *out, size_t *outlen,
219 const unsigned char *in, size_t inlen)
224 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
228 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
229 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
233 if (ctx->op.ciph.algctx == NULL)
236 ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.algctx, out, outlen,
237 (out == NULL ? 0 : *outlen), in, inlen);
241 if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
242 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
245 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
246 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
250 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
252 EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
254 if (cipher == NULL) {
255 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
259 cipher->lock = CRYPTO_THREAD_lock_new();
260 if (cipher->lock == NULL) {
261 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
262 OPENSSL_free(cipher);
266 ossl_provider_up_ref(prov);
272 static void *evp_asym_cipher_from_algorithm(int name_id,
273 const OSSL_ALGORITHM *algodef,
276 const OSSL_DISPATCH *fns = algodef->implementation;
277 EVP_ASYM_CIPHER *cipher = NULL;
278 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
279 int gparamfncnt = 0, sparamfncnt = 0;
281 if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
282 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
286 cipher->name_id = name_id;
287 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
289 cipher->description = algodef->algorithm_description;
291 for (; fns->function_id != 0; fns++) {
292 switch (fns->function_id) {
293 case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
294 if (cipher->newctx != NULL)
296 cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
299 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
300 if (cipher->encrypt_init != NULL)
302 cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
305 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
306 if (cipher->encrypt != NULL)
308 cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
311 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
312 if (cipher->decrypt_init != NULL)
314 cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
317 case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
318 if (cipher->decrypt != NULL)
320 cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
323 case OSSL_FUNC_ASYM_CIPHER_FREECTX:
324 if (cipher->freectx != NULL)
326 cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
329 case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
330 if (cipher->dupctx != NULL)
332 cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
334 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
335 if (cipher->get_ctx_params != NULL)
337 cipher->get_ctx_params
338 = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
341 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
342 if (cipher->gettable_ctx_params != NULL)
344 cipher->gettable_ctx_params
345 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
348 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
349 if (cipher->set_ctx_params != NULL)
351 cipher->set_ctx_params
352 = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
355 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
356 if (cipher->settable_ctx_params != NULL)
358 cipher->settable_ctx_params
359 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
365 || (encfncnt != 0 && encfncnt != 2)
366 || (decfncnt != 0 && decfncnt != 2)
367 || (encfncnt != 2 && decfncnt != 2)
368 || (gparamfncnt != 0 && gparamfncnt != 2)
369 || (sparamfncnt != 0 && sparamfncnt != 2)) {
371 * In order to be a consistent set of functions we must have at least
372 * a set of context functions (newctx and freectx) as well as a pair of
373 * "cipher" functions: (encrypt_init, encrypt) or
374 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
375 * optional, but if one of them is present then the other one must also
376 * be present. The same applies to get_ctx_params and
377 * gettable_ctx_params. The dupctx function is optional.
379 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
385 EVP_ASYM_CIPHER_free(cipher);
389 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
395 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
398 OPENSSL_free(cipher->type_name);
399 ossl_provider_free(cipher->prov);
400 CRYPTO_THREAD_lock_free(cipher->lock);
401 OPENSSL_free(cipher);
404 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
408 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
412 OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
417 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
418 const char *properties)
420 return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
421 evp_asym_cipher_from_algorithm,
422 (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
423 (void (*)(void *))EVP_ASYM_CIPHER_free);
426 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
428 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
431 int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
433 return cipher->name_id;
436 const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
438 return cipher->type_name;
441 const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
443 return cipher->description;
446 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
447 void (*fn)(EVP_ASYM_CIPHER *cipher,
451 evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
452 (void (*)(void *, void *))fn, arg,
453 evp_asym_cipher_from_algorithm,
454 (void (*)(void *))EVP_ASYM_CIPHER_free);
458 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
459 void (*fn)(const char *name, void *data),
462 if (cipher->prov != NULL)
463 return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
468 const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
472 if (cip == NULL || cip->gettable_ctx_params == NULL)
475 provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
476 return cip->gettable_ctx_params(NULL, provctx);
479 const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
483 if (cip == NULL || cip->settable_ctx_params == NULL)
486 provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
487 return cip->settable_ctx_params(NULL, provctx);