| 1 |
/* |
| 2 |
* Copyright 2006-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 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" |
| 19 |
|
| 20 |
static void evp_asym_cipher_free(void *data) |
| 21 |
{ |
| 22 |
EVP_ASYM_CIPHER_free(data); |
| 23 |
} |
| 24 |
|
| 25 |
static int evp_asym_cipher_up_ref(void *data) |
| 26 |
{ |
| 27 |
return EVP_ASYM_CIPHER_up_ref(data); |
| 28 |
} |
| 29 |
|
| 30 |
static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation, |
| 31 |
const OSSL_PARAM params[]) |
| 32 |
{ |
| 33 |
int ret = 0; |
| 34 |
void *provkey = NULL; |
| 35 |
EVP_ASYM_CIPHER *cipher = NULL; |
| 36 |
const char *desc; |
| 37 |
EVP_KEYMGMT *tmp_keymgmt = NULL; |
| 38 |
const OSSL_PROVIDER *tmp_prov = NULL; |
| 39 |
const char *supported_ciph = NULL; |
| 40 |
int iter; |
| 41 |
|
| 42 |
if (ctx == NULL) { |
| 43 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 44 |
return -2; |
| 45 |
} |
| 46 |
|
| 47 |
evp_pkey_ctx_free_old_ops(ctx); |
| 48 |
ctx->operation = operation; |
| 49 |
|
| 50 |
ERR_set_mark(); |
| 51 |
|
| 52 |
if (evp_pkey_ctx_is_legacy(ctx)) |
| 53 |
goto legacy; |
| 54 |
|
| 55 |
if (ctx->pkey == NULL) { |
| 56 |
ERR_clear_last_mark(); |
| 57 |
ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
| 58 |
goto err; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Try to derive the supported asym cipher from |ctx->keymgmt|. |
| 63 |
*/ |
| 64 |
if (!ossl_assert(ctx->pkey->keymgmt == NULL |
| 65 |
|| ctx->pkey->keymgmt == ctx->keymgmt)) { |
| 66 |
ERR_clear_last_mark(); |
| 67 |
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
| 68 |
goto err; |
| 69 |
} |
| 70 |
supported_ciph |
| 71 |
= evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
| 72 |
OSSL_OP_ASYM_CIPHER); |
| 73 |
if (supported_ciph == NULL) { |
| 74 |
ERR_clear_last_mark(); |
| 75 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 76 |
goto err; |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* We perform two iterations: |
| 81 |
* |
| 82 |
* 1. Do the normal asym cipher fetch, using the fetching data given by |
| 83 |
* the EVP_PKEY_CTX. |
| 84 |
* 2. Do the provider specific asym cipher fetch, from the same provider |
| 85 |
* as |ctx->keymgmt| |
| 86 |
* |
| 87 |
* We then try to fetch the keymgmt from the same provider as the |
| 88 |
* asym cipher, and try to export |ctx->pkey| to that keymgmt (when |
| 89 |
* this keymgmt happens to be the same as |ctx->keymgmt|, the export |
| 90 |
* is a no-op, but we call it anyway to not complicate the code even |
| 91 |
* more). |
| 92 |
* If the export call succeeds (returns a non-NULL provider key pointer), |
| 93 |
* we're done and can perform the operation itself. If not, we perform |
| 94 |
* the second iteration, or jump to legacy. |
| 95 |
*/ |
| 96 |
for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
| 97 |
EVP_KEYMGMT *tmp_keymgmt_tofree; |
| 98 |
|
| 99 |
/* |
| 100 |
* If we're on the second iteration, free the results from the first. |
| 101 |
* They are NULL on the first iteration, so no need to check what |
| 102 |
* iteration we're on. |
| 103 |
*/ |
| 104 |
EVP_ASYM_CIPHER_free(cipher); |
| 105 |
cipher = NULL; |
| 106 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 107 |
tmp_keymgmt = NULL; |
| 108 |
|
| 109 |
switch (iter) { |
| 110 |
case 1: |
| 111 |
cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, |
| 112 |
ctx->propquery); |
| 113 |
if (cipher != NULL) |
| 114 |
tmp_prov = EVP_ASYM_CIPHER_get0_provider(cipher); |
| 115 |
break; |
| 116 |
case 2: |
| 117 |
tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
| 118 |
cipher = evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
| 119 |
supported_ciph, ctx->propquery); |
| 120 |
if (cipher == NULL) |
| 121 |
goto legacy; |
| 122 |
break; |
| 123 |
} |
| 124 |
if (cipher == NULL) |
| 125 |
continue; |
| 126 |
|
| 127 |
/* |
| 128 |
* Ensure that the key is provided, either natively, or as a cached |
| 129 |
* export. We start by fetching the keymgmt with the same name as |
| 130 |
* |ctx->pkey|, but from the provider of the asym cipher method, using |
| 131 |
* the same property query as when fetching the asym cipher method. |
| 132 |
* With the keymgmt we found (if we did), we try to export |ctx->pkey| |
| 133 |
* to it (evp_pkey_export_to_provider() is smart enough to only actually |
| 134 |
* export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
| 135 |
*/ |
| 136 |
tmp_keymgmt_tofree = tmp_keymgmt |
| 137 |
= evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
| 138 |
EVP_KEYMGMT_get0_name(ctx->keymgmt), |
| 139 |
ctx->propquery); |
| 140 |
if (tmp_keymgmt != NULL) |
| 141 |
provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
| 142 |
&tmp_keymgmt, ctx->propquery); |
| 143 |
if (tmp_keymgmt == NULL) |
| 144 |
EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
| 145 |
} |
| 146 |
|
| 147 |
if (provkey == NULL) { |
| 148 |
EVP_ASYM_CIPHER_free(cipher); |
| 149 |
goto legacy; |
| 150 |
} |
| 151 |
|
| 152 |
ERR_pop_to_mark(); |
| 153 |
|
| 154 |
/* No more legacy from here down to legacy: */ |
| 155 |
|
| 156 |
ctx->op.ciph.cipher = cipher; |
| 157 |
ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov)); |
| 158 |
if (ctx->op.ciph.algctx == NULL) { |
| 159 |
/* The provider key can stay in the cache */ |
| 160 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 161 |
goto err; |
| 162 |
} |
| 163 |
|
| 164 |
desc = cipher->description != NULL ? cipher->description : ""; |
| 165 |
switch (operation) { |
| 166 |
case EVP_PKEY_OP_ENCRYPT: |
| 167 |
if (cipher->encrypt_init == NULL) { |
| 168 |
ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED, |
| 169 |
"%s encrypt_init:%s", cipher->type_name, desc); |
| 170 |
ret = -2; |
| 171 |
goto err; |
| 172 |
} |
| 173 |
ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params); |
| 174 |
break; |
| 175 |
case EVP_PKEY_OP_DECRYPT: |
| 176 |
if (cipher->decrypt_init == NULL) { |
| 177 |
ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED, |
| 178 |
"%s decrypt_init:%s", cipher->type_name, desc); |
| 179 |
ret = -2; |
| 180 |
goto err; |
| 181 |
} |
| 182 |
ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params); |
| 183 |
break; |
| 184 |
default: |
| 185 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 186 |
goto err; |
| 187 |
} |
| 188 |
|
| 189 |
if (ret <= 0) |
| 190 |
goto err; |
| 191 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 192 |
return 1; |
| 193 |
|
| 194 |
legacy: |
| 195 |
/* |
| 196 |
* If we don't have the full support we need with provided methods, |
| 197 |
* let's go see if legacy does. |
| 198 |
*/ |
| 199 |
ERR_pop_to_mark(); |
| 200 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 201 |
tmp_keymgmt = NULL; |
| 202 |
|
| 203 |
if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) { |
| 204 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 205 |
return -2; |
| 206 |
} |
| 207 |
switch (ctx->operation) { |
| 208 |
case EVP_PKEY_OP_ENCRYPT: |
| 209 |
if (ctx->pmeth->encrypt_init == NULL) |
| 210 |
return 1; |
| 211 |
ret = ctx->pmeth->encrypt_init(ctx); |
| 212 |
break; |
| 213 |
case EVP_PKEY_OP_DECRYPT: |
| 214 |
if (ctx->pmeth->decrypt_init == NULL) |
| 215 |
return 1; |
| 216 |
ret = ctx->pmeth->decrypt_init(ctx); |
| 217 |
break; |
| 218 |
default: |
| 219 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 220 |
ret = -1; |
| 221 |
} |
| 222 |
|
| 223 |
err: |
| 224 |
if (ret <= 0) { |
| 225 |
evp_pkey_ctx_free_old_ops(ctx); |
| 226 |
ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 227 |
} |
| 228 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 229 |
return ret; |
| 230 |
} |
| 231 |
|
| 232 |
int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) |
| 233 |
{ |
| 234 |
return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL); |
| 235 |
} |
| 236 |
|
| 237 |
int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
| 238 |
{ |
| 239 |
return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params); |
| 240 |
} |
| 241 |
|
| 242 |
int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, |
| 243 |
unsigned char *out, size_t *outlen, |
| 244 |
const unsigned char *in, size_t inlen) |
| 245 |
{ |
| 246 |
EVP_ASYM_CIPHER *cipher; |
| 247 |
const char *desc; |
| 248 |
int ret; |
| 249 |
|
| 250 |
if (ctx == NULL) { |
| 251 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 252 |
return -2; |
| 253 |
} |
| 254 |
|
| 255 |
if (ctx->operation != EVP_PKEY_OP_ENCRYPT) { |
| 256 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
| 257 |
return -1; |
| 258 |
} |
| 259 |
|
| 260 |
if (ctx->op.ciph.algctx == NULL) |
| 261 |
goto legacy; |
| 262 |
|
| 263 |
cipher = ctx->op.ciph.cipher; |
| 264 |
desc = cipher->description != NULL ? cipher->description : ""; |
| 265 |
ERR_set_mark(); |
| 266 |
ret = cipher->encrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen); |
| 267 |
if (ret <= 0 && ERR_count_to_mark() == 0) |
| 268 |
ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE, |
| 269 |
"%s encrypt:%s", cipher->type_name, desc); |
| 270 |
ERR_clear_last_mark(); |
| 271 |
return ret; |
| 272 |
|
| 273 |
legacy: |
| 274 |
if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) { |
| 275 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 276 |
return -2; |
| 277 |
} |
| 278 |
M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT) return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen); |
| 279 |
} |
| 280 |
|
| 281 |
int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) |
| 282 |
{ |
| 283 |
return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL); |
| 284 |
} |
| 285 |
|
| 286 |
int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
| 287 |
{ |
| 288 |
return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params); |
| 289 |
} |
| 290 |
|
| 291 |
int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, |
| 292 |
unsigned char *out, size_t *outlen, |
| 293 |
const unsigned char *in, size_t inlen) |
| 294 |
{ |
| 295 |
EVP_ASYM_CIPHER *cipher; |
| 296 |
const char *desc; |
| 297 |
int ret; |
| 298 |
|
| 299 |
if (ctx == NULL) { |
| 300 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 301 |
return -2; |
| 302 |
} |
| 303 |
|
| 304 |
if (ctx->operation != EVP_PKEY_OP_DECRYPT) { |
| 305 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
| 306 |
return -1; |
| 307 |
} |
| 308 |
|
| 309 |
if (ctx->op.ciph.algctx == NULL) |
| 310 |
goto legacy; |
| 311 |
|
| 312 |
cipher = ctx->op.ciph.cipher; |
| 313 |
desc = cipher->description != NULL ? cipher->description : ""; |
| 314 |
ERR_set_mark(); |
| 315 |
ret = cipher->decrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen); |
| 316 |
if (ret <= 0 && ERR_count_to_mark() == 0) |
| 317 |
ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE, |
| 318 |
"%s decrypt:%s", cipher->type_name, desc); |
| 319 |
ERR_clear_last_mark(); |
| 320 |
|
| 321 |
return ret; |
| 322 |
|
| 323 |
legacy: |
| 324 |
if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) { |
| 325 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 326 |
return -2; |
| 327 |
} |
| 328 |
M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT) return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen); |
| 329 |
} |
| 330 |
|
| 331 |
/* decrypt to new buffer of dynamic size, checking any pre-determined size */ |
| 332 |
int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp, |
| 333 |
size_t *outlenp, size_t expected_outlen, |
| 334 |
const unsigned char *in, size_t inlen) |
| 335 |
{ |
| 336 |
if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0 |
| 337 |
|| (*outp = OPENSSL_malloc(*outlenp)) == NULL) |
| 338 |
return -1; |
| 339 |
if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0 |
| 340 |
|| *outlenp == 0 |
| 341 |
|| (expected_outlen != 0 && *outlenp != expected_outlen)) { |
| 342 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 343 |
OPENSSL_clear_free(*outp, *outlenp); |
| 344 |
*outp = NULL; |
| 345 |
return 0; |
| 346 |
} |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov) |
| 351 |
{ |
| 352 |
EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER)); |
| 353 |
|
| 354 |
if (cipher == NULL) |
| 355 |
return NULL; |
| 356 |
|
| 357 |
if (!CRYPTO_NEW_REF(&cipher->refcnt, 1) |
| 358 |
|| !ossl_provider_up_ref(prov)) { |
| 359 |
CRYPTO_FREE_REF(&cipher->refcnt); |
| 360 |
OPENSSL_free(cipher); |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
cipher->prov = prov; |
| 364 |
|
| 365 |
return cipher; |
| 366 |
} |
| 367 |
|
| 368 |
static void *evp_asym_cipher_from_algorithm(int name_id, |
| 369 |
const OSSL_ALGORITHM *algodef, |
| 370 |
OSSL_PROVIDER *prov) |
| 371 |
{ |
| 372 |
const OSSL_DISPATCH *fns = algodef->implementation; |
| 373 |
EVP_ASYM_CIPHER *cipher = NULL; |
| 374 |
int ctxfncnt = 0, encfncnt = 0, decfncnt = 0; |
| 375 |
int gparamfncnt = 0, sparamfncnt = 0; |
| 376 |
|
| 377 |
if ((cipher = evp_asym_cipher_new(prov)) == NULL) { |
| 378 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 379 |
goto err; |
| 380 |
} |
| 381 |
|
| 382 |
cipher->name_id = name_id; |
| 383 |
if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
| 384 |
goto err; |
| 385 |
cipher->description = algodef->algorithm_description; |
| 386 |
|
| 387 |
for (; fns->function_id != 0; fns++) { |
| 388 |
switch (fns->function_id) { |
| 389 |
case OSSL_FUNC_ASYM_CIPHER_NEWCTX: |
| 390 |
if (cipher->newctx != NULL) |
| 391 |
break; |
| 392 |
cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns); |
| 393 |
ctxfncnt++; |
| 394 |
break; |
| 395 |
case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT: |
| 396 |
if (cipher->encrypt_init != NULL) |
| 397 |
break; |
| 398 |
cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns); |
| 399 |
encfncnt++; |
| 400 |
break; |
| 401 |
case OSSL_FUNC_ASYM_CIPHER_ENCRYPT: |
| 402 |
if (cipher->encrypt != NULL) |
| 403 |
break; |
| 404 |
cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns); |
| 405 |
encfncnt++; |
| 406 |
break; |
| 407 |
case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT: |
| 408 |
if (cipher->decrypt_init != NULL) |
| 409 |
break; |
| 410 |
cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns); |
| 411 |
decfncnt++; |
| 412 |
break; |
| 413 |
case OSSL_FUNC_ASYM_CIPHER_DECRYPT: |
| 414 |
if (cipher->decrypt != NULL) |
| 415 |
break; |
| 416 |
cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns); |
| 417 |
decfncnt++; |
| 418 |
break; |
| 419 |
case OSSL_FUNC_ASYM_CIPHER_FREECTX: |
| 420 |
if (cipher->freectx != NULL) |
| 421 |
break; |
| 422 |
cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns); |
| 423 |
ctxfncnt++; |
| 424 |
break; |
| 425 |
case OSSL_FUNC_ASYM_CIPHER_DUPCTX: |
| 426 |
if (cipher->dupctx != NULL) |
| 427 |
break; |
| 428 |
cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns); |
| 429 |
break; |
| 430 |
case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS: |
| 431 |
if (cipher->get_ctx_params != NULL) |
| 432 |
break; |
| 433 |
cipher->get_ctx_params |
| 434 |
= OSSL_FUNC_asym_cipher_get_ctx_params(fns); |
| 435 |
gparamfncnt++; |
| 436 |
break; |
| 437 |
case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS: |
| 438 |
if (cipher->gettable_ctx_params != NULL) |
| 439 |
break; |
| 440 |
cipher->gettable_ctx_params |
| 441 |
= OSSL_FUNC_asym_cipher_gettable_ctx_params(fns); |
| 442 |
gparamfncnt++; |
| 443 |
break; |
| 444 |
case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS: |
| 445 |
if (cipher->set_ctx_params != NULL) |
| 446 |
break; |
| 447 |
cipher->set_ctx_params |
| 448 |
= OSSL_FUNC_asym_cipher_set_ctx_params(fns); |
| 449 |
sparamfncnt++; |
| 450 |
break; |
| 451 |
case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS: |
| 452 |
if (cipher->settable_ctx_params != NULL) |
| 453 |
break; |
| 454 |
cipher->settable_ctx_params |
| 455 |
= OSSL_FUNC_asym_cipher_settable_ctx_params(fns); |
| 456 |
sparamfncnt++; |
| 457 |
break; |
| 458 |
} |
| 459 |
} |
| 460 |
if (ctxfncnt != 2 |
| 461 |
|| (encfncnt != 0 && encfncnt != 2) |
| 462 |
|| (decfncnt != 0 && decfncnt != 2) |
| 463 |
|| (encfncnt != 2 && decfncnt != 2) |
| 464 |
|| (gparamfncnt != 0 && gparamfncnt != 2) |
| 465 |
|| (sparamfncnt != 0 && sparamfncnt != 2)) { |
| 466 |
/* |
| 467 |
* In order to be a consistent set of functions we must have at least |
| 468 |
* a set of context functions (newctx and freectx) as well as a pair of |
| 469 |
* "cipher" functions: (encrypt_init, encrypt) or |
| 470 |
* (decrypt_init decrypt). set_ctx_params and settable_ctx_params are |
| 471 |
* optional, but if one of them is present then the other one must also |
| 472 |
* be present. The same applies to get_ctx_params and |
| 473 |
* gettable_ctx_params. The dupctx function is optional. |
| 474 |
*/ |
| 475 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
| 476 |
goto err; |
| 477 |
} |
| 478 |
|
| 479 |
return cipher; |
| 480 |
err: |
| 481 |
EVP_ASYM_CIPHER_free(cipher); |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
|
| 485 |
void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher) |
| 486 |
{ |
| 487 |
int i; |
| 488 |
|
| 489 |
if (cipher == NULL) |
| 490 |
return; |
| 491 |
CRYPTO_DOWN_REF(&cipher->refcnt, &i); |
| 492 |
if (i > 0) |
| 493 |
return; |
| 494 |
OPENSSL_free(cipher->type_name); |
| 495 |
ossl_provider_free(cipher->prov); |
| 496 |
CRYPTO_FREE_REF(&cipher->refcnt); |
| 497 |
OPENSSL_free(cipher); |
| 498 |
} |
| 499 |
|
| 500 |
int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher) |
| 501 |
{ |
| 502 |
int ref = 0; |
| 503 |
|
| 504 |
CRYPTO_UP_REF(&cipher->refcnt, &ref); |
| 505 |
return 1; |
| 506 |
} |
| 507 |
|
| 508 |
OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher) |
| 509 |
{ |
| 510 |
return cipher->prov; |
| 511 |
} |
| 512 |
|
| 513 |
EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
| 514 |
const char *properties) |
| 515 |
{ |
| 516 |
return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties, |
| 517 |
evp_asym_cipher_from_algorithm, |
| 518 |
evp_asym_cipher_up_ref, |
| 519 |
evp_asym_cipher_free); |
| 520 |
} |
| 521 |
|
| 522 |
EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov, |
| 523 |
const char *algorithm, |
| 524 |
const char *properties) |
| 525 |
{ |
| 526 |
return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER, |
| 527 |
algorithm, properties, |
| 528 |
evp_asym_cipher_from_algorithm, |
| 529 |
evp_asym_cipher_up_ref, |
| 530 |
evp_asym_cipher_free); |
| 531 |
} |
| 532 |
|
| 533 |
int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name) |
| 534 |
{ |
| 535 |
return evp_is_a(cipher->prov, cipher->name_id, NULL, name); |
| 536 |
} |
| 537 |
|
| 538 |
int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher) |
| 539 |
{ |
| 540 |
return cipher->name_id; |
| 541 |
} |
| 542 |
|
| 543 |
const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher) |
| 544 |
{ |
| 545 |
return cipher->type_name; |
| 546 |
} |
| 547 |
|
| 548 |
const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher) |
| 549 |
{ |
| 550 |
return cipher->description; |
| 551 |
} |
| 552 |
|
| 553 |
void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, |
| 554 |
void (*fn)(EVP_ASYM_CIPHER *cipher, |
| 555 |
void *arg), |
| 556 |
void *arg) |
| 557 |
{ |
| 558 |
evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER, |
| 559 |
(void (*)(void *, void *))fn, arg, |
| 560 |
evp_asym_cipher_from_algorithm, |
| 561 |
evp_asym_cipher_up_ref, |
| 562 |
evp_asym_cipher_free); |
| 563 |
} |
| 564 |
|
| 565 |
int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher, |
| 566 |
void (*fn)(const char *name, void *data), |
| 567 |
void *data) |
| 568 |
{ |
| 569 |
if (cipher->prov != NULL) |
| 570 |
return evp_names_do_all(cipher->prov, cipher->name_id, fn, data); |
| 571 |
|
| 572 |
return 1; |
| 573 |
} |
| 574 |
|
| 575 |
const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip) |
| 576 |
{ |
| 577 |
void *provctx; |
| 578 |
|
| 579 |
if (cip == NULL || cip->gettable_ctx_params == NULL) |
| 580 |
return NULL; |
| 581 |
|
| 582 |
provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
| 583 |
return cip->gettable_ctx_params(NULL, provctx); |
| 584 |
} |
| 585 |
|
| 586 |
const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip) |
| 587 |
{ |
| 588 |
void *provctx; |
| 589 |
|
| 590 |
if (cip == NULL || cip->settable_ctx_params == NULL) |
| 591 |
return NULL; |
| 592 |
|
| 593 |
provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
| 594 |
return cip->settable_ctx_params(NULL, provctx); |
| 595 |
} |