| 1 |
/* |
| 2 |
* Copyright 2019-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 <openssl/core_names.h> |
| 11 |
#include <openssl/crypto.h> |
| 12 |
#include <openssl/evp.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include "internal/cryptlib.h" |
| 15 |
#include "internal/refcount.h" |
| 16 |
#include "internal/provider.h" |
| 17 |
#include "internal/core.h" |
| 18 |
#include "internal/numbers.h" /* includes SIZE_MAX */ |
| 19 |
#include "crypto/evp.h" |
| 20 |
#include "evp_local.h" |
| 21 |
|
| 22 |
static void evp_keyexch_free(void *data) |
| 23 |
{ |
| 24 |
EVP_KEYEXCH_free(data); |
| 25 |
} |
| 26 |
|
| 27 |
static int evp_keyexch_up_ref(void *data) |
| 28 |
{ |
| 29 |
return EVP_KEYEXCH_up_ref(data); |
| 30 |
} |
| 31 |
|
| 32 |
static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov) |
| 33 |
{ |
| 34 |
EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH)); |
| 35 |
|
| 36 |
if (exchange == NULL) |
| 37 |
return NULL; |
| 38 |
|
| 39 |
if (!CRYPTO_NEW_REF(&exchange->refcnt, 1) |
| 40 |
|| !ossl_provider_up_ref(prov)) { |
| 41 |
CRYPTO_FREE_REF(&exchange->refcnt); |
| 42 |
OPENSSL_free(exchange); |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
exchange->prov = prov; |
| 46 |
|
| 47 |
return exchange; |
| 48 |
} |
| 49 |
|
| 50 |
static void *evp_keyexch_from_algorithm(int name_id, |
| 51 |
const OSSL_ALGORITHM *algodef, |
| 52 |
OSSL_PROVIDER *prov) |
| 53 |
{ |
| 54 |
const OSSL_DISPATCH *fns = algodef->implementation; |
| 55 |
EVP_KEYEXCH *exchange = NULL; |
| 56 |
int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0, derive_found = 0; |
| 57 |
|
| 58 |
if ((exchange = evp_keyexch_new(prov)) == NULL) { |
| 59 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 60 |
goto err; |
| 61 |
} |
| 62 |
|
| 63 |
exchange->name_id = name_id; |
| 64 |
if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
| 65 |
goto err; |
| 66 |
exchange->description = algodef->algorithm_description; |
| 67 |
|
| 68 |
for (; fns->function_id != 0; fns++) { |
| 69 |
switch (fns->function_id) { |
| 70 |
case OSSL_FUNC_KEYEXCH_NEWCTX: |
| 71 |
if (exchange->newctx != NULL) |
| 72 |
break; |
| 73 |
exchange->newctx = OSSL_FUNC_keyexch_newctx(fns); |
| 74 |
fncnt++; |
| 75 |
break; |
| 76 |
case OSSL_FUNC_KEYEXCH_INIT: |
| 77 |
if (exchange->init != NULL) |
| 78 |
break; |
| 79 |
exchange->init = OSSL_FUNC_keyexch_init(fns); |
| 80 |
fncnt++; |
| 81 |
break; |
| 82 |
case OSSL_FUNC_KEYEXCH_SET_PEER: |
| 83 |
if (exchange->set_peer != NULL) |
| 84 |
break; |
| 85 |
exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns); |
| 86 |
break; |
| 87 |
case OSSL_FUNC_KEYEXCH_DERIVE: |
| 88 |
if (exchange->derive != NULL) |
| 89 |
break; |
| 90 |
exchange->derive = OSSL_FUNC_keyexch_derive(fns); |
| 91 |
derive_found = 1; |
| 92 |
break; |
| 93 |
case OSSL_FUNC_KEYEXCH_FREECTX: |
| 94 |
if (exchange->freectx != NULL) |
| 95 |
break; |
| 96 |
exchange->freectx = OSSL_FUNC_keyexch_freectx(fns); |
| 97 |
fncnt++; |
| 98 |
break; |
| 99 |
case OSSL_FUNC_KEYEXCH_DUPCTX: |
| 100 |
if (exchange->dupctx != NULL) |
| 101 |
break; |
| 102 |
exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns); |
| 103 |
break; |
| 104 |
case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS: |
| 105 |
if (exchange->get_ctx_params != NULL) |
| 106 |
break; |
| 107 |
exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns); |
| 108 |
gparamfncnt++; |
| 109 |
break; |
| 110 |
case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS: |
| 111 |
if (exchange->gettable_ctx_params != NULL) |
| 112 |
break; |
| 113 |
exchange->gettable_ctx_params |
| 114 |
= OSSL_FUNC_keyexch_gettable_ctx_params(fns); |
| 115 |
gparamfncnt++; |
| 116 |
break; |
| 117 |
case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS: |
| 118 |
if (exchange->set_ctx_params != NULL) |
| 119 |
break; |
| 120 |
exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns); |
| 121 |
sparamfncnt++; |
| 122 |
break; |
| 123 |
case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS: |
| 124 |
if (exchange->settable_ctx_params != NULL) |
| 125 |
break; |
| 126 |
exchange->settable_ctx_params |
| 127 |
= OSSL_FUNC_keyexch_settable_ctx_params(fns); |
| 128 |
sparamfncnt++; |
| 129 |
break; |
| 130 |
case OSSL_FUNC_KEYEXCH_DERIVE_SKEY: |
| 131 |
if (exchange->derive_skey != NULL) |
| 132 |
break; |
| 133 |
exchange->derive_skey = OSSL_FUNC_keyexch_derive_skey(fns); |
| 134 |
derive_found = 1; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
fncnt += derive_found; |
| 139 |
if (fncnt != 4 |
| 140 |
|| (gparamfncnt != 0 && gparamfncnt != 2) |
| 141 |
|| (sparamfncnt != 0 && sparamfncnt != 2)) { |
| 142 |
/* |
| 143 |
* In order to be a consistent set of functions we must have at least |
| 144 |
* a complete set of "exchange" functions: init, derive, newctx, |
| 145 |
* and freectx. The set_ctx_params and settable_ctx_params functions are |
| 146 |
* optional, but if one of them is present then the other one must also |
| 147 |
* be present. Same goes for get_ctx_params and gettable_ctx_params. |
| 148 |
* The dupctx and set_peer functions are optional. |
| 149 |
*/ |
| 150 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
| 151 |
goto err; |
| 152 |
} |
| 153 |
|
| 154 |
return exchange; |
| 155 |
|
| 156 |
err: |
| 157 |
EVP_KEYEXCH_free(exchange); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange) |
| 162 |
{ |
| 163 |
int i; |
| 164 |
|
| 165 |
if (exchange == NULL) |
| 166 |
return; |
| 167 |
CRYPTO_DOWN_REF(&exchange->refcnt, &i); |
| 168 |
if (i > 0) |
| 169 |
return; |
| 170 |
OPENSSL_free(exchange->type_name); |
| 171 |
ossl_provider_free(exchange->prov); |
| 172 |
CRYPTO_FREE_REF(&exchange->refcnt); |
| 173 |
OPENSSL_free(exchange); |
| 174 |
} |
| 175 |
|
| 176 |
int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange) |
| 177 |
{ |
| 178 |
int ref = 0; |
| 179 |
|
| 180 |
CRYPTO_UP_REF(&exchange->refcnt, &ref); |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange) |
| 185 |
{ |
| 186 |
return exchange->prov; |
| 187 |
} |
| 188 |
|
| 189 |
EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
| 190 |
const char *properties) |
| 191 |
{ |
| 192 |
return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties, |
| 193 |
evp_keyexch_from_algorithm, |
| 194 |
evp_keyexch_up_ref, |
| 195 |
evp_keyexch_free); |
| 196 |
} |
| 197 |
|
| 198 |
EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov, |
| 199 |
const char *algorithm, |
| 200 |
const char *properties) |
| 201 |
{ |
| 202 |
return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH, |
| 203 |
algorithm, properties, |
| 204 |
evp_keyexch_from_algorithm, |
| 205 |
evp_keyexch_up_ref, |
| 206 |
evp_keyexch_free); |
| 207 |
} |
| 208 |
|
| 209 |
int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) |
| 210 |
{ |
| 211 |
return EVP_PKEY_derive_init_ex(ctx, NULL); |
| 212 |
} |
| 213 |
|
| 214 |
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
| 215 |
{ |
| 216 |
int ret; |
| 217 |
void *provkey = NULL; |
| 218 |
EVP_KEYEXCH *exchange = NULL; |
| 219 |
EVP_KEYMGMT *tmp_keymgmt = NULL; |
| 220 |
const OSSL_PROVIDER *tmp_prov = NULL; |
| 221 |
const char *supported_exch = NULL; |
| 222 |
int iter; |
| 223 |
|
| 224 |
if (ctx == NULL) { |
| 225 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 226 |
return -2; |
| 227 |
} |
| 228 |
|
| 229 |
evp_pkey_ctx_free_old_ops(ctx); |
| 230 |
ctx->operation = EVP_PKEY_OP_DERIVE; |
| 231 |
|
| 232 |
ERR_set_mark(); |
| 233 |
|
| 234 |
if (evp_pkey_ctx_is_legacy(ctx)) |
| 235 |
goto legacy; |
| 236 |
|
| 237 |
/* |
| 238 |
* Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create |
| 239 |
* a blank one. |
| 240 |
*/ |
| 241 |
if (ctx->pkey == NULL) { |
| 242 |
EVP_PKEY *pkey = EVP_PKEY_new(); |
| 243 |
|
| 244 |
if (pkey == NULL |
| 245 |
|| !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt) |
| 246 |
|| (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) { |
| 247 |
ERR_clear_last_mark(); |
| 248 |
EVP_PKEY_free(pkey); |
| 249 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 250 |
goto err; |
| 251 |
} |
| 252 |
ctx->pkey = pkey; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* Try to derive the supported exch from |ctx->keymgmt|. |
| 257 |
*/ |
| 258 |
if (!ossl_assert(ctx->pkey->keymgmt == NULL |
| 259 |
|| ctx->pkey->keymgmt == ctx->keymgmt)) { |
| 260 |
ERR_clear_last_mark(); |
| 261 |
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
| 262 |
goto err; |
| 263 |
} |
| 264 |
supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
| 265 |
OSSL_OP_KEYEXCH); |
| 266 |
if (supported_exch == NULL) { |
| 267 |
ERR_clear_last_mark(); |
| 268 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 269 |
goto err; |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* We perform two iterations: |
| 274 |
* |
| 275 |
* 1. Do the normal exchange fetch, using the fetching data given by |
| 276 |
* the EVP_PKEY_CTX. |
| 277 |
* 2. Do the provider specific exchange fetch, from the same provider |
| 278 |
* as |ctx->keymgmt| |
| 279 |
* |
| 280 |
* We then try to fetch the keymgmt from the same provider as the |
| 281 |
* exchange, and try to export |ctx->pkey| to that keymgmt (when |
| 282 |
* this keymgmt happens to be the same as |ctx->keymgmt|, the export |
| 283 |
* is a no-op, but we call it anyway to not complicate the code even |
| 284 |
* more). |
| 285 |
* If the export call succeeds (returns a non-NULL provider key pointer), |
| 286 |
* we're done and can perform the operation itself. If not, we perform |
| 287 |
* the second iteration, or jump to legacy. |
| 288 |
*/ |
| 289 |
for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
| 290 |
EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
| 291 |
|
| 292 |
/* |
| 293 |
* If we're on the second iteration, free the results from the first. |
| 294 |
* They are NULL on the first iteration, so no need to check what |
| 295 |
* iteration we're on. |
| 296 |
*/ |
| 297 |
EVP_KEYEXCH_free(exchange); |
| 298 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 299 |
|
| 300 |
switch (iter) { |
| 301 |
case 1: |
| 302 |
exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery); |
| 303 |
if (exchange != NULL) |
| 304 |
tmp_prov = EVP_KEYEXCH_get0_provider(exchange); |
| 305 |
break; |
| 306 |
case 2: |
| 307 |
tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
| 308 |
exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
| 309 |
supported_exch, ctx->propquery); |
| 310 |
if (exchange == NULL) |
| 311 |
goto legacy; |
| 312 |
break; |
| 313 |
} |
| 314 |
if (exchange == NULL) |
| 315 |
continue; |
| 316 |
|
| 317 |
/* |
| 318 |
* Ensure that the key is provided, either natively, or as a cached |
| 319 |
* export. We start by fetching the keymgmt with the same name as |
| 320 |
* |ctx->keymgmt|, but from the provider of the exchange method, using |
| 321 |
* the same property query as when fetching the exchange method. |
| 322 |
* With the keymgmt we found (if we did), we try to export |ctx->pkey| |
| 323 |
* to it (evp_pkey_export_to_provider() is smart enough to only actually |
| 324 |
* export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
| 325 |
*/ |
| 326 |
tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
| 327 |
EVP_KEYMGMT_get0_name(ctx->keymgmt), |
| 328 |
ctx->propquery); |
| 329 |
if (tmp_keymgmt != NULL) |
| 330 |
provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
| 331 |
&tmp_keymgmt, ctx->propquery); |
| 332 |
if (tmp_keymgmt == NULL) |
| 333 |
EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
| 334 |
} |
| 335 |
|
| 336 |
if (provkey == NULL) { |
| 337 |
EVP_KEYEXCH_free(exchange); |
| 338 |
goto legacy; |
| 339 |
} |
| 340 |
|
| 341 |
ERR_pop_to_mark(); |
| 342 |
|
| 343 |
/* No more legacy from here down to legacy: */ |
| 344 |
|
| 345 |
/* A Coverity false positive with up_ref/down_ref and free */ |
| 346 |
/* coverity[use_after_free] */ |
| 347 |
ctx->op.kex.exchange = exchange; |
| 348 |
/* A Coverity false positive with up_ref/down_ref and free */ |
| 349 |
/* coverity[deref_arg] */ |
| 350 |
ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov)); |
| 351 |
if (ctx->op.kex.algctx == NULL) { |
| 352 |
/* The provider key can stay in the cache */ |
| 353 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 354 |
goto err; |
| 355 |
} |
| 356 |
ret = exchange->init(ctx->op.kex.algctx, provkey, params); |
| 357 |
|
| 358 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 359 |
return ret ? 1 : 0; |
| 360 |
err: |
| 361 |
evp_pkey_ctx_free_old_ops(ctx); |
| 362 |
ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 363 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 364 |
return 0; |
| 365 |
|
| 366 |
legacy: |
| 367 |
/* |
| 368 |
* If we don't have the full support we need with provided methods, |
| 369 |
* let's go see if legacy does. |
| 370 |
*/ |
| 371 |
ERR_pop_to_mark(); |
| 372 |
|
| 373 |
#ifdef FIPS_MODULE |
| 374 |
return 0; |
| 375 |
#else |
| 376 |
if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
| 377 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 378 |
return -2; |
| 379 |
} |
| 380 |
|
| 381 |
if (ctx->pmeth->derive_init == NULL) |
| 382 |
return 1; |
| 383 |
ret = ctx->pmeth->derive_init(ctx); |
| 384 |
if (ret <= 0) |
| 385 |
ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 386 |
EVP_KEYMGMT_free(tmp_keymgmt); |
| 387 |
return ret; |
| 388 |
#endif |
| 389 |
} |
| 390 |
|
| 391 |
int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer, |
| 392 |
int validate_peer) |
| 393 |
{ |
| 394 |
int ret = 0, check; |
| 395 |
void *provkey = NULL; |
| 396 |
EVP_PKEY_CTX *check_ctx = NULL; |
| 397 |
EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL; |
| 398 |
|
| 399 |
if (ctx == NULL) { |
| 400 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
|
| 404 |
if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL) |
| 405 |
goto legacy; |
| 406 |
|
| 407 |
if (ctx->op.kex.exchange->set_peer == NULL) { |
| 408 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 409 |
return -2; |
| 410 |
} |
| 411 |
|
| 412 |
if (validate_peer) { |
| 413 |
check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery); |
| 414 |
if (check_ctx == NULL) |
| 415 |
return -1; |
| 416 |
check = EVP_PKEY_public_check(check_ctx); |
| 417 |
EVP_PKEY_CTX_free(check_ctx); |
| 418 |
if (check <= 0) |
| 419 |
return -1; |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* Ensure that the |peer| is provided, either natively, or as a cached |
| 424 |
* export. We start by fetching the keymgmt with the same name as |
| 425 |
* |ctx->keymgmt|, but from the provider of the exchange method, using |
| 426 |
* the same property query as when fetching the exchange method. |
| 427 |
* With the keymgmt we found (if we did), we try to export |peer| |
| 428 |
* to it (evp_pkey_export_to_provider() is smart enough to only actually |
| 429 |
* export it if |tmp_keymgmt| is different from |peer|'s keymgmt) |
| 430 |
*/ |
| 431 |
tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *) |
| 432 |
EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange), |
| 433 |
EVP_KEYMGMT_get0_name(ctx->keymgmt), |
| 434 |
ctx->propquery); |
| 435 |
if (tmp_keymgmt != NULL) |
| 436 |
/* A Coverity issue with up_ref/down_ref and free */ |
| 437 |
/* coverity[pass_freed_arg] */ |
| 438 |
provkey = evp_pkey_export_to_provider(peer, ctx->libctx, |
| 439 |
&tmp_keymgmt, ctx->propquery); |
| 440 |
EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
| 441 |
|
| 442 |
/* |
| 443 |
* If making the key provided wasn't possible, legacy may be able to pick |
| 444 |
* it up |
| 445 |
*/ |
| 446 |
if (provkey == NULL) |
| 447 |
goto legacy; |
| 448 |
ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey); |
| 449 |
if (ret <= 0) |
| 450 |
return ret; |
| 451 |
goto common; |
| 452 |
|
| 453 |
legacy: |
| 454 |
#ifdef FIPS_MODULE |
| 455 |
return ret; |
| 456 |
#else |
| 457 |
if (ctx->pmeth == NULL |
| 458 |
|| !(ctx->pmeth->derive != NULL |
| 459 |
|| ctx->pmeth->encrypt != NULL |
| 460 |
|| ctx->pmeth->decrypt != NULL) |
| 461 |
|| ctx->pmeth->ctrl == NULL) { |
| 462 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 463 |
return -2; |
| 464 |
} |
| 465 |
if (ctx->operation != EVP_PKEY_OP_DERIVE |
| 466 |
&& ctx->operation != EVP_PKEY_OP_ENCRYPT |
| 467 |
&& ctx->operation != EVP_PKEY_OP_DECRYPT) { |
| 468 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
| 469 |
return -1; |
| 470 |
} |
| 471 |
|
| 472 |
ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer); |
| 473 |
|
| 474 |
if (ret <= 0) |
| 475 |
return ret; |
| 476 |
|
| 477 |
if (ret == 2) |
| 478 |
return 1; |
| 479 |
|
| 480 |
if (ctx->pkey == NULL) { |
| 481 |
ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
| 482 |
return -1; |
| 483 |
} |
| 484 |
|
| 485 |
if (ctx->pkey->type != peer->type) { |
| 486 |
ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 487 |
return -1; |
| 488 |
} |
| 489 |
|
| 490 |
/* |
| 491 |
* For clarity. The error is if parameters in peer are |
| 492 |
* present (!missing) but don't match. EVP_PKEY_parameters_eq may return |
| 493 |
* 1 (match), 0 (don't match) and -2 (comparison is not defined). -1 |
| 494 |
* (different key types) is impossible here because it is checked earlier. |
| 495 |
* -2 is OK for us here, as well as 1, so we can check for 0 only. |
| 496 |
*/ |
| 497 |
if (!EVP_PKEY_missing_parameters(peer) && !EVP_PKEY_parameters_eq(ctx->pkey, peer)) { |
| 498 |
ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS); |
| 499 |
return -1; |
| 500 |
} |
| 501 |
|
| 502 |
ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer); |
| 503 |
if (ret <= 0) |
| 504 |
return ret; |
| 505 |
#endif |
| 506 |
|
| 507 |
common: |
| 508 |
if (!EVP_PKEY_up_ref(peer)) |
| 509 |
return -1; |
| 510 |
|
| 511 |
EVP_PKEY_free(ctx->peerkey); |
| 512 |
ctx->peerkey = peer; |
| 513 |
|
| 514 |
return 1; |
| 515 |
} |
| 516 |
|
| 517 |
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) |
| 518 |
{ |
| 519 |
return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1); |
| 520 |
} |
| 521 |
|
| 522 |
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen) |
| 523 |
{ |
| 524 |
int ret; |
| 525 |
|
| 526 |
if (ctx == NULL || pkeylen == NULL) { |
| 527 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 528 |
return -1; |
| 529 |
} |
| 530 |
|
| 531 |
if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 532 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
| 533 |
return -1; |
| 534 |
} |
| 535 |
|
| 536 |
if (ctx->op.kex.algctx == NULL) |
| 537 |
goto legacy; |
| 538 |
|
| 539 |
ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen, |
| 540 |
key != NULL ? *pkeylen : 0); |
| 541 |
|
| 542 |
return ret; |
| 543 |
legacy: |
| 544 |
if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
| 545 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 546 |
return -2; |
| 547 |
} |
| 548 |
|
| 549 |
M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) return ctx->pmeth->derive(ctx, key, pkeylen); |
| 550 |
} |
| 551 |
|
| 552 |
EVP_SKEY *EVP_PKEY_derive_SKEY(EVP_PKEY_CTX *ctx, EVP_SKEYMGMT *mgmt, |
| 553 |
const char *key_type, const char *propquery, |
| 554 |
size_t keylen, const OSSL_PARAM params[]) |
| 555 |
{ |
| 556 |
EVP_SKEYMGMT *skeymgmt = NULL; |
| 557 |
EVP_SKEY *ret = NULL; |
| 558 |
|
| 559 |
if (ctx == NULL || key_type == NULL) { |
| 560 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 561 |
return NULL; |
| 562 |
} |
| 563 |
|
| 564 |
if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 565 |
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
| 566 |
return NULL; |
| 567 |
} |
| 568 |
|
| 569 |
if (ctx->op.kex.algctx == NULL) { |
| 570 |
ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
if (mgmt != NULL) { |
| 575 |
skeymgmt = mgmt; |
| 576 |
} else { |
| 577 |
skeymgmt = evp_skeymgmt_fetch_from_prov(ctx->op.kex.exchange->prov, |
| 578 |
key_type, propquery); |
| 579 |
if (skeymgmt == NULL) { |
| 580 |
/* |
| 581 |
* The provider does not support skeymgmt, let's try to fallback |
| 582 |
* to a provider that supports it |
| 583 |
*/ |
| 584 |
skeymgmt = EVP_SKEYMGMT_fetch(ctx->libctx, key_type, propquery); |
| 585 |
} |
| 586 |
if (skeymgmt == NULL) { |
| 587 |
ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED); |
| 588 |
return NULL; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/* Fallback to raw derive + import if necessary */ |
| 593 |
if (skeymgmt->prov != ctx->op.kex.exchange->prov || ctx->op.kex.exchange->derive_skey == NULL) { |
| 594 |
size_t tmplen = keylen; |
| 595 |
unsigned char *key = NULL; |
| 596 |
OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 597 |
|
| 598 |
if (ctx->op.kex.exchange->derive == NULL) { |
| 599 |
ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED); |
| 600 |
goto cleanup; |
| 601 |
} |
| 602 |
|
| 603 |
key = OPENSSL_zalloc(keylen); |
| 604 |
if (key == NULL) { |
| 605 |
ERR_raise(ERR_R_EVP_LIB, ERR_R_CRYPTO_LIB); |
| 606 |
goto cleanup; |
| 607 |
} |
| 608 |
|
| 609 |
if (!ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, &tmplen, |
| 610 |
tmplen)) { |
| 611 |
OPENSSL_free(key); |
| 612 |
goto cleanup; |
| 613 |
} |
| 614 |
|
| 615 |
if (keylen != tmplen) { |
| 616 |
OPENSSL_free(key); |
| 617 |
ERR_raise(ERR_R_EVP_LIB, ERR_R_INTERNAL_ERROR); |
| 618 |
goto cleanup; |
| 619 |
} |
| 620 |
import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
| 621 |
key, keylen); |
| 622 |
|
| 623 |
ret = EVP_SKEY_import_SKEYMGMT(ctx->libctx, skeymgmt, |
| 624 |
OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params); |
| 625 |
OPENSSL_clear_free(key, keylen); |
| 626 |
goto cleanup; |
| 627 |
} |
| 628 |
|
| 629 |
ret = evp_skey_alloc(skeymgmt); |
| 630 |
if (ret == NULL) |
| 631 |
goto cleanup; |
| 632 |
|
| 633 |
ret->keydata = ctx->op.kex.exchange->derive_skey(ctx->op.kex.algctx, key_type, |
| 634 |
ossl_provider_ctx(skeymgmt->prov), |
| 635 |
skeymgmt->import, keylen, params); |
| 636 |
|
| 637 |
if (ret->keydata == NULL) { |
| 638 |
EVP_SKEY_free(ret); |
| 639 |
ret = NULL; |
| 640 |
goto cleanup; |
| 641 |
} |
| 642 |
cleanup: |
| 643 |
if (mgmt != skeymgmt) |
| 644 |
EVP_SKEYMGMT_free(skeymgmt); |
| 645 |
return ret; |
| 646 |
} |
| 647 |
|
| 648 |
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch) |
| 649 |
{ |
| 650 |
return keyexch->name_id; |
| 651 |
} |
| 652 |
|
| 653 |
const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch) |
| 654 |
{ |
| 655 |
return keyexch->type_name; |
| 656 |
} |
| 657 |
|
| 658 |
const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch) |
| 659 |
{ |
| 660 |
return keyexch->description; |
| 661 |
} |
| 662 |
|
| 663 |
int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name) |
| 664 |
{ |
| 665 |
return keyexch != NULL |
| 666 |
&& evp_is_a(keyexch->prov, keyexch->name_id, NULL, name); |
| 667 |
} |
| 668 |
|
| 669 |
void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx, |
| 670 |
void (*fn)(EVP_KEYEXCH *keyexch, void *arg), |
| 671 |
void *arg) |
| 672 |
{ |
| 673 |
evp_generic_do_all(libctx, OSSL_OP_KEYEXCH, |
| 674 |
(void (*)(void *, void *))fn, arg, |
| 675 |
evp_keyexch_from_algorithm, |
| 676 |
evp_keyexch_up_ref, |
| 677 |
evp_keyexch_free); |
| 678 |
} |
| 679 |
|
| 680 |
int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch, |
| 681 |
void (*fn)(const char *name, void *data), |
| 682 |
void *data) |
| 683 |
{ |
| 684 |
if (keyexch->prov != NULL) |
| 685 |
return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data); |
| 686 |
|
| 687 |
return 1; |
| 688 |
} |
| 689 |
|
| 690 |
const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch) |
| 691 |
{ |
| 692 |
void *provctx; |
| 693 |
|
| 694 |
if (keyexch == NULL || keyexch->gettable_ctx_params == NULL) |
| 695 |
return NULL; |
| 696 |
|
| 697 |
provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
| 698 |
return keyexch->gettable_ctx_params(NULL, provctx); |
| 699 |
} |
| 700 |
|
| 701 |
const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch) |
| 702 |
{ |
| 703 |
void *provctx; |
| 704 |
|
| 705 |
if (keyexch == NULL || keyexch->settable_ctx_params == NULL) |
| 706 |
return NULL; |
| 707 |
provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
| 708 |
return keyexch->settable_ctx_params(NULL, provctx); |
| 709 |
} |