| 1 |
/* |
| 2 |
* Copyright 2006-2025 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 |
/* |
| 11 |
* Low level key APIs (DH etc) are deprecated for public use, but still ok for |
| 12 |
* internal use. |
| 13 |
*/ |
| 14 |
#include "internal/deprecated.h" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#ifndef FIPS_MODULE |
| 19 |
#include <openssl/engine.h> |
| 20 |
#endif |
| 21 |
#include <openssl/evp.h> |
| 22 |
#include <openssl/core_names.h> |
| 23 |
#include <openssl/dh.h> |
| 24 |
#include <openssl/rsa.h> |
| 25 |
#include <openssl/kdf.h> |
| 26 |
#include "internal/cryptlib.h" |
| 27 |
#ifndef FIPS_MODULE |
| 28 |
#include "crypto/asn1.h" |
| 29 |
#endif |
| 30 |
#include "crypto/evp.h" |
| 31 |
#include "crypto/dh.h" |
| 32 |
#include "crypto/ec.h" |
| 33 |
#include "internal/ffc.h" |
| 34 |
#include "internal/numbers.h" |
| 35 |
#include "internal/provider.h" |
| 36 |
#include "evp_local.h" |
| 37 |
|
| 38 |
#ifndef FIPS_MODULE |
| 39 |
|
| 40 |
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
| 41 |
int keytype, int optype, |
| 42 |
int cmd, const char *name, |
| 43 |
const void *data, size_t data_len); |
| 44 |
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
| 45 |
int cmd, const char *name); |
| 46 |
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx); |
| 47 |
|
| 48 |
typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void); |
| 49 |
typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
| 50 |
|
| 51 |
static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
| 52 |
|
| 53 |
/* This array needs to be in order of NIDs */ |
| 54 |
static pmeth_fn standard_methods[] = { |
| 55 |
ossl_rsa_pkey_method, |
| 56 |
#ifndef OPENSSL_NO_DH |
| 57 |
ossl_dh_pkey_method, |
| 58 |
#endif |
| 59 |
#ifndef OPENSSL_NO_DSA |
| 60 |
ossl_dsa_pkey_method, |
| 61 |
#endif |
| 62 |
#ifndef OPENSSL_NO_EC |
| 63 |
ossl_ec_pkey_method, |
| 64 |
#endif |
| 65 |
ossl_rsa_pss_pkey_method, |
| 66 |
#ifndef OPENSSL_NO_DH |
| 67 |
ossl_dhx_pkey_method, |
| 68 |
#endif |
| 69 |
#ifndef OPENSSL_NO_ECX |
| 70 |
ossl_ecx25519_pkey_method, |
| 71 |
ossl_ecx448_pkey_method, |
| 72 |
ossl_ed25519_pkey_method, |
| 73 |
ossl_ed448_pkey_method, |
| 74 |
#endif |
| 75 |
}; |
| 76 |
|
| 77 |
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
| 78 |
|
| 79 |
static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b) |
| 80 |
{ |
| 81 |
return ((*a)->pkey_id - ((**b)())->pkey_id); |
| 82 |
} |
| 83 |
|
| 84 |
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
| 85 |
|
| 86 |
static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
| 87 |
const EVP_PKEY_METHOD *const *b) |
| 88 |
{ |
| 89 |
return ((*a)->pkey_id - (*b)->pkey_id); |
| 90 |
} |
| 91 |
|
| 92 |
static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type) |
| 93 |
{ |
| 94 |
if (app_pkey_methods != NULL) { |
| 95 |
int idx; |
| 96 |
EVP_PKEY_METHOD tmp; |
| 97 |
|
| 98 |
tmp.pkey_id = type; |
| 99 |
idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); |
| 100 |
if (idx >= 0) |
| 101 |
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); |
| 102 |
} |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
| 107 |
{ |
| 108 |
pmeth_fn *ret; |
| 109 |
EVP_PKEY_METHOD tmp; |
| 110 |
const EVP_PKEY_METHOD *t; |
| 111 |
|
| 112 |
if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL) |
| 113 |
return t; |
| 114 |
|
| 115 |
tmp.pkey_id = type; |
| 116 |
t = &tmp; |
| 117 |
ret = OBJ_bsearch_pmeth_func(&t, standard_methods, |
| 118 |
OSSL_NELEM(standard_methods)); |
| 119 |
if (ret == NULL || *ret == NULL) |
| 120 |
return NULL; |
| 121 |
return (**ret)(); |
| 122 |
} |
| 123 |
|
| 124 |
EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
| 125 |
{ |
| 126 |
EVP_PKEY_METHOD *pmeth; |
| 127 |
|
| 128 |
pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
| 129 |
if (pmeth == NULL) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
pmeth->pkey_id = id; |
| 133 |
pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; |
| 134 |
return pmeth; |
| 135 |
} |
| 136 |
#endif /* FIPS_MODULE */ |
| 137 |
|
| 138 |
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx) |
| 139 |
{ |
| 140 |
if (ctx->operation == EVP_PKEY_OP_UNDEFINED) |
| 141 |
return EVP_PKEY_STATE_UNKNOWN; |
| 142 |
|
| 143 |
if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
| 144 |
&& ctx->op.kex.algctx != NULL) |
| 145 |
|| (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
| 146 |
&& ctx->op.sig.algctx != NULL) |
| 147 |
|| (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
| 148 |
&& ctx->op.ciph.algctx != NULL) |
| 149 |
|| (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
| 150 |
&& ctx->op.keymgmt.genctx != NULL) |
| 151 |
|| (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
| 152 |
&& ctx->op.encap.algctx != NULL)) |
| 153 |
return EVP_PKEY_STATE_PROVIDER; |
| 154 |
|
| 155 |
return EVP_PKEY_STATE_LEGACY; |
| 156 |
} |
| 157 |
|
| 158 |
static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx, |
| 159 |
EVP_PKEY *pkey, ENGINE *e, |
| 160 |
const char *keytype, const char *propquery, |
| 161 |
int id) |
| 162 |
|
| 163 |
{ |
| 164 |
EVP_PKEY_CTX *ret = NULL; |
| 165 |
const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL; |
| 166 |
EVP_KEYMGMT *keymgmt = NULL; |
| 167 |
|
| 168 |
/* Code below to be removed when legacy support is dropped. */ |
| 169 |
/* BEGIN legacy */ |
| 170 |
if (id == -1) { |
| 171 |
if (pkey != NULL && !evp_pkey_is_provided(pkey)) { |
| 172 |
id = pkey->type; |
| 173 |
} else { |
| 174 |
if (pkey != NULL) { |
| 175 |
/* Must be provided if we get here */ |
| 176 |
keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt); |
| 177 |
} |
| 178 |
#ifndef FIPS_MODULE |
| 179 |
if (keytype != NULL) { |
| 180 |
id = evp_pkey_name2type(keytype); |
| 181 |
if (id == NID_undef) |
| 182 |
id = -1; |
| 183 |
} |
| 184 |
#endif |
| 185 |
} |
| 186 |
} |
| 187 |
/* If no ID was found here, we can only resort to find a keymgmt */ |
| 188 |
if (id == -1) { |
| 189 |
#ifndef FIPS_MODULE |
| 190 |
/* Using engine with a key without id will not work */ |
| 191 |
if (e != NULL) { |
| 192 |
ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
#endif |
| 196 |
goto common; |
| 197 |
} |
| 198 |
|
| 199 |
#ifndef FIPS_MODULE |
| 200 |
/* |
| 201 |
* Here, we extract what information we can for the purpose of |
| 202 |
* supporting usage with implementations from providers, to make |
| 203 |
* for a smooth transition from legacy stuff to provider based stuff. |
| 204 |
* |
| 205 |
* If an engine is given, this is entirely legacy, and we should not |
| 206 |
* pretend anything else, so we clear the name. |
| 207 |
*/ |
| 208 |
if (e != NULL) |
| 209 |
keytype = NULL; |
| 210 |
if (e == NULL && (pkey == NULL || pkey->foreign == 0)) |
| 211 |
keytype = OBJ_nid2sn(id); |
| 212 |
|
| 213 |
#ifndef OPENSSL_NO_ENGINE |
| 214 |
if (e == NULL && pkey != NULL) |
| 215 |
e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
| 216 |
/* Try to find an ENGINE which implements this method */ |
| 217 |
if (e != NULL) { |
| 218 |
if (!ENGINE_init(e)) { |
| 219 |
ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
} else { |
| 223 |
e = ENGINE_get_pkey_meth_engine(id); |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* If an ENGINE handled this method look it up. Otherwise use internal |
| 228 |
* tables. |
| 229 |
*/ |
| 230 |
if (e != NULL) |
| 231 |
pmeth = ENGINE_get_pkey_meth(e, id); |
| 232 |
else |
| 233 |
#endif /* OPENSSL_NO_ENGINE */ |
| 234 |
if (pkey != NULL && pkey->foreign) |
| 235 |
pmeth = EVP_PKEY_meth_find(id); |
| 236 |
else |
| 237 |
app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id); |
| 238 |
|
| 239 |
/* END legacy */ |
| 240 |
#endif /* FIPS_MODULE */ |
| 241 |
common: |
| 242 |
/* |
| 243 |
* If there's no engine and no app supplied pmeth and there's a name, we try |
| 244 |
* fetching a provider implementation. |
| 245 |
*/ |
| 246 |
if (e == NULL && app_pmeth == NULL && keytype != NULL) { |
| 247 |
/* |
| 248 |
* If |pkey| is given and is provided, we take a reference to its |
| 249 |
* keymgmt. Otherwise, we fetch one for the keytype we got. This |
| 250 |
* is to ensure that operation init functions can access what they |
| 251 |
* need through this single pointer. |
| 252 |
*/ |
| 253 |
if (pkey != NULL && pkey->keymgmt != NULL) { |
| 254 |
if (!EVP_KEYMGMT_up_ref(pkey->keymgmt)) |
| 255 |
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
| 256 |
else |
| 257 |
keymgmt = pkey->keymgmt; |
| 258 |
} else { |
| 259 |
keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery); |
| 260 |
} |
| 261 |
if (keymgmt == NULL) |
| 262 |
return NULL; /* EVP_KEYMGMT_fetch() recorded an error */ |
| 263 |
|
| 264 |
#ifndef FIPS_MODULE |
| 265 |
/* |
| 266 |
* Chase down the legacy NID, as that might be needed for diverse |
| 267 |
* purposes, such as ensure that EVP_PKEY_type() can return sensible |
| 268 |
* values. We go through all keymgmt names, because the keytype |
| 269 |
* that's passed to this function doesn't necessarily translate |
| 270 |
* directly. |
| 271 |
*/ |
| 272 |
if (keymgmt != NULL) { |
| 273 |
int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt); |
| 274 |
|
| 275 |
if (tmp_id != NID_undef) { |
| 276 |
if (id == -1) { |
| 277 |
id = tmp_id; |
| 278 |
} else { |
| 279 |
/* |
| 280 |
* It really really shouldn't differ. If it still does, |
| 281 |
* something is very wrong. |
| 282 |
*/ |
| 283 |
if (!ossl_assert(id == tmp_id)) { |
| 284 |
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
| 285 |
EVP_KEYMGMT_free(keymgmt); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
if (pmeth == NULL && keymgmt == NULL) { |
| 295 |
ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 296 |
} else { |
| 297 |
ret = OPENSSL_zalloc(sizeof(*ret)); |
| 298 |
} |
| 299 |
|
| 300 |
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
| 301 |
if ((ret == NULL || pmeth == NULL) && e != NULL) |
| 302 |
ENGINE_finish(e); |
| 303 |
#endif |
| 304 |
|
| 305 |
if (ret == NULL) { |
| 306 |
EVP_KEYMGMT_free(keymgmt); |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
if (propquery != NULL) { |
| 310 |
ret->propquery = OPENSSL_strdup(propquery); |
| 311 |
if (ret->propquery == NULL) { |
| 312 |
OPENSSL_free(ret); |
| 313 |
EVP_KEYMGMT_free(keymgmt); |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
} |
| 317 |
ret->libctx = libctx; |
| 318 |
ret->keytype = keytype; |
| 319 |
ret->keymgmt = keymgmt; |
| 320 |
ret->legacy_keytype = id; |
| 321 |
ret->engine = e; |
| 322 |
ret->pmeth = pmeth; |
| 323 |
ret->operation = EVP_PKEY_OP_UNDEFINED; |
| 324 |
|
| 325 |
if (pkey != NULL && !EVP_PKEY_up_ref(pkey)) { |
| 326 |
EVP_PKEY_CTX_free(ret); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
ret->pkey = pkey; |
| 331 |
|
| 332 |
if (pmeth != NULL && pmeth->init != NULL) { |
| 333 |
if (pmeth->init(ret) <= 0) { |
| 334 |
ret->pmeth = NULL; |
| 335 |
EVP_PKEY_CTX_free(ret); |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return ret; |
| 341 |
} |
| 342 |
|
| 343 |
/*- All methods below can also be used in FIPS_MODULE */ |
| 344 |
|
| 345 |
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, |
| 346 |
const char *name, |
| 347 |
const char *propquery) |
| 348 |
{ |
| 349 |
return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); |
| 350 |
} |
| 351 |
|
| 352 |
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, |
| 353 |
const char *propquery) |
| 354 |
{ |
| 355 |
return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1); |
| 356 |
} |
| 357 |
|
| 358 |
void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
| 359 |
{ |
| 360 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
| 361 |
if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL) |
| 362 |
ctx->op.sig.signature->freectx(ctx->op.sig.algctx); |
| 363 |
EVP_SIGNATURE_free(ctx->op.sig.signature); |
| 364 |
ctx->op.sig.algctx = NULL; |
| 365 |
ctx->op.sig.signature = NULL; |
| 366 |
} else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 367 |
if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL) |
| 368 |
ctx->op.kex.exchange->freectx(ctx->op.kex.algctx); |
| 369 |
EVP_KEYEXCH_free(ctx->op.kex.exchange); |
| 370 |
ctx->op.kex.algctx = NULL; |
| 371 |
ctx->op.kex.exchange = NULL; |
| 372 |
} else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
| 373 |
if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL) |
| 374 |
ctx->op.encap.kem->freectx(ctx->op.encap.algctx); |
| 375 |
EVP_KEM_free(ctx->op.encap.kem); |
| 376 |
ctx->op.encap.algctx = NULL; |
| 377 |
ctx->op.encap.kem = NULL; |
| 378 |
} else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
| 379 |
if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL) |
| 380 |
ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx); |
| 381 |
EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); |
| 382 |
ctx->op.ciph.algctx = NULL; |
| 383 |
ctx->op.ciph.cipher = NULL; |
| 384 |
} else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
| 385 |
if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL) |
| 386 |
evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
| 391 |
{ |
| 392 |
if (ctx == NULL) |
| 393 |
return; |
| 394 |
if (ctx->pmeth && ctx->pmeth->cleanup) |
| 395 |
ctx->pmeth->cleanup(ctx); |
| 396 |
|
| 397 |
evp_pkey_ctx_free_old_ops(ctx); |
| 398 |
#ifndef FIPS_MODULE |
| 399 |
evp_pkey_ctx_free_all_cached_data(ctx); |
| 400 |
#endif |
| 401 |
EVP_KEYMGMT_free(ctx->keymgmt); |
| 402 |
|
| 403 |
OPENSSL_free(ctx->propquery); |
| 404 |
EVP_PKEY_free(ctx->pkey); |
| 405 |
EVP_PKEY_free(ctx->peerkey); |
| 406 |
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
| 407 |
ENGINE_finish(ctx->engine); |
| 408 |
#endif |
| 409 |
BN_free(ctx->rsa_pubexp); |
| 410 |
OPENSSL_free(ctx); |
| 411 |
} |
| 412 |
|
| 413 |
#ifndef FIPS_MODULE |
| 414 |
|
| 415 |
void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
| 416 |
const EVP_PKEY_METHOD *meth) |
| 417 |
{ |
| 418 |
if (ppkey_id) |
| 419 |
*ppkey_id = meth->pkey_id; |
| 420 |
if (pflags) |
| 421 |
*pflags = meth->flags; |
| 422 |
} |
| 423 |
|
| 424 |
void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) |
| 425 |
{ |
| 426 |
int pkey_id = dst->pkey_id; |
| 427 |
int flags = dst->flags; |
| 428 |
|
| 429 |
*dst = *src; |
| 430 |
|
| 431 |
/* We only copy the function pointers so restore the other values */ |
| 432 |
dst->pkey_id = pkey_id; |
| 433 |
dst->flags = flags; |
| 434 |
} |
| 435 |
|
| 436 |
void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
| 437 |
{ |
| 438 |
if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) |
| 439 |
OPENSSL_free(pmeth); |
| 440 |
} |
| 441 |
|
| 442 |
EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
| 443 |
{ |
| 444 |
return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
| 445 |
} |
| 446 |
|
| 447 |
EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) |
| 448 |
{ |
| 449 |
return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
| 450 |
} |
| 451 |
|
| 452 |
EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
| 453 |
{ |
| 454 |
EVP_PKEY_CTX *rctx; |
| 455 |
|
| 456 |
#ifndef OPENSSL_NO_ENGINE |
| 457 |
/* Make sure it's safe to copy a pkey context using an ENGINE */ |
| 458 |
if (pctx->engine && !ENGINE_init(pctx->engine)) { |
| 459 |
ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
#endif |
| 463 |
rctx = OPENSSL_zalloc(sizeof(*rctx)); |
| 464 |
if (rctx == NULL) |
| 465 |
return NULL; |
| 466 |
|
| 467 |
if (pctx->pkey != NULL && !EVP_PKEY_up_ref(pctx->pkey)) |
| 468 |
goto err; |
| 469 |
|
| 470 |
rctx->pkey = pctx->pkey; |
| 471 |
rctx->operation = pctx->operation; |
| 472 |
rctx->libctx = pctx->libctx; |
| 473 |
rctx->keytype = pctx->keytype; |
| 474 |
rctx->propquery = NULL; |
| 475 |
if (pctx->propquery != NULL) { |
| 476 |
rctx->propquery = OPENSSL_strdup(pctx->propquery); |
| 477 |
if (rctx->propquery == NULL) |
| 478 |
goto err; |
| 479 |
} |
| 480 |
rctx->legacy_keytype = pctx->legacy_keytype; |
| 481 |
|
| 482 |
if (pctx->keymgmt != NULL) { |
| 483 |
if (!EVP_KEYMGMT_up_ref(pctx->keymgmt)) |
| 484 |
goto err; |
| 485 |
rctx->keymgmt = pctx->keymgmt; |
| 486 |
} |
| 487 |
|
| 488 |
if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
| 489 |
if (pctx->op.kex.exchange != NULL) { |
| 490 |
rctx->op.kex.exchange = pctx->op.kex.exchange; |
| 491 |
if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) |
| 492 |
goto err; |
| 493 |
} |
| 494 |
if (pctx->op.kex.algctx != NULL) { |
| 495 |
if (!ossl_assert(pctx->op.kex.exchange != NULL)) |
| 496 |
goto err; |
| 497 |
|
| 498 |
if (pctx->op.kex.exchange->dupctx != NULL) |
| 499 |
rctx->op.kex.algctx |
| 500 |
= pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx); |
| 501 |
|
| 502 |
if (rctx->op.kex.algctx == NULL) { |
| 503 |
EVP_KEYEXCH_free(rctx->op.kex.exchange); |
| 504 |
rctx->op.kex.exchange = NULL; |
| 505 |
goto err; |
| 506 |
} |
| 507 |
return rctx; |
| 508 |
} |
| 509 |
} else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { |
| 510 |
if (pctx->op.sig.signature != NULL) { |
| 511 |
rctx->op.sig.signature = pctx->op.sig.signature; |
| 512 |
if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) |
| 513 |
goto err; |
| 514 |
} |
| 515 |
if (pctx->op.sig.algctx != NULL) { |
| 516 |
if (!ossl_assert(pctx->op.sig.signature != NULL)) |
| 517 |
goto err; |
| 518 |
|
| 519 |
if (pctx->op.sig.signature->dupctx != NULL) |
| 520 |
rctx->op.sig.algctx |
| 521 |
= pctx->op.sig.signature->dupctx(pctx->op.sig.algctx); |
| 522 |
|
| 523 |
if (rctx->op.sig.algctx == NULL) { |
| 524 |
EVP_SIGNATURE_free(rctx->op.sig.signature); |
| 525 |
rctx->op.sig.signature = NULL; |
| 526 |
goto err; |
| 527 |
} |
| 528 |
return rctx; |
| 529 |
} |
| 530 |
} else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
| 531 |
if (pctx->op.ciph.cipher != NULL) { |
| 532 |
rctx->op.ciph.cipher = pctx->op.ciph.cipher; |
| 533 |
if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) |
| 534 |
goto err; |
| 535 |
} |
| 536 |
if (pctx->op.ciph.algctx != NULL) { |
| 537 |
if (!ossl_assert(pctx->op.ciph.cipher != NULL)) |
| 538 |
goto err; |
| 539 |
|
| 540 |
if (pctx->op.ciph.cipher->dupctx != NULL) |
| 541 |
rctx->op.ciph.algctx |
| 542 |
= pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx); |
| 543 |
|
| 544 |
if (rctx->op.ciph.algctx == NULL) { |
| 545 |
EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); |
| 546 |
rctx->op.ciph.cipher = NULL; |
| 547 |
goto err; |
| 548 |
} |
| 549 |
return rctx; |
| 550 |
} |
| 551 |
} else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) { |
| 552 |
if (pctx->op.encap.kem != NULL) { |
| 553 |
rctx->op.encap.kem = pctx->op.encap.kem; |
| 554 |
if (!EVP_KEM_up_ref(rctx->op.encap.kem)) |
| 555 |
goto err; |
| 556 |
} |
| 557 |
if (pctx->op.encap.algctx != NULL) { |
| 558 |
if (!ossl_assert(pctx->op.encap.kem != NULL)) |
| 559 |
goto err; |
| 560 |
|
| 561 |
if (pctx->op.encap.kem->dupctx != NULL) |
| 562 |
rctx->op.encap.algctx |
| 563 |
= pctx->op.encap.kem->dupctx(pctx->op.encap.algctx); |
| 564 |
|
| 565 |
if (rctx->op.encap.algctx == NULL) { |
| 566 |
EVP_KEM_free(rctx->op.encap.kem); |
| 567 |
rctx->op.encap.kem = NULL; |
| 568 |
goto err; |
| 569 |
} |
| 570 |
return rctx; |
| 571 |
} |
| 572 |
} else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) { |
| 573 |
/* Not supported - This would need a gen_dupctx() to work */ |
| 574 |
goto err; |
| 575 |
} |
| 576 |
|
| 577 |
rctx->pmeth = pctx->pmeth; |
| 578 |
#ifndef OPENSSL_NO_ENGINE |
| 579 |
rctx->engine = pctx->engine; |
| 580 |
#endif |
| 581 |
|
| 582 |
if (pctx->peerkey != NULL && !EVP_PKEY_up_ref(pctx->peerkey)) |
| 583 |
goto err; |
| 584 |
|
| 585 |
rctx->peerkey = pctx->peerkey; |
| 586 |
|
| 587 |
if (pctx->pmeth == NULL) { |
| 588 |
if (rctx->operation == EVP_PKEY_OP_UNDEFINED) { |
| 589 |
EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt; |
| 590 |
void *provkey; |
| 591 |
|
| 592 |
if (pctx->pkey == NULL) |
| 593 |
return rctx; |
| 594 |
|
| 595 |
provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx, |
| 596 |
&tmp_keymgmt, pctx->propquery); |
| 597 |
if (provkey == NULL) |
| 598 |
goto err; |
| 599 |
if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) |
| 600 |
goto err; |
| 601 |
EVP_KEYMGMT_free(rctx->keymgmt); |
| 602 |
rctx->keymgmt = tmp_keymgmt; |
| 603 |
return rctx; |
| 604 |
} |
| 605 |
} else if (pctx->pmeth->copy(rctx, pctx) > 0) { |
| 606 |
return rctx; |
| 607 |
} |
| 608 |
err: |
| 609 |
rctx->pmeth = NULL; |
| 610 |
EVP_PKEY_CTX_free(rctx); |
| 611 |
return NULL; |
| 612 |
} |
| 613 |
|
| 614 |
int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
| 615 |
{ |
| 616 |
if (app_pkey_methods == NULL) { |
| 617 |
app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); |
| 618 |
if (app_pkey_methods == NULL) { |
| 619 |
ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
| 620 |
return 0; |
| 621 |
} |
| 622 |
} |
| 623 |
if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
| 624 |
ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
| 625 |
return 0; |
| 626 |
} |
| 627 |
sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
| 628 |
return 1; |
| 629 |
} |
| 630 |
|
| 631 |
void evp_app_cleanup_int(void) |
| 632 |
{ |
| 633 |
if (app_pkey_methods != NULL) |
| 634 |
sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); |
| 635 |
} |
| 636 |
|
| 637 |
int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) |
| 638 |
{ |
| 639 |
const EVP_PKEY_METHOD *ret; |
| 640 |
|
| 641 |
ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); |
| 642 |
|
| 643 |
return ret == NULL ? 0 : 1; |
| 644 |
} |
| 645 |
|
| 646 |
size_t EVP_PKEY_meth_get_count(void) |
| 647 |
{ |
| 648 |
size_t rv = OSSL_NELEM(standard_methods); |
| 649 |
|
| 650 |
if (app_pkey_methods) |
| 651 |
rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); |
| 652 |
return rv; |
| 653 |
} |
| 654 |
|
| 655 |
const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) |
| 656 |
{ |
| 657 |
if (idx < OSSL_NELEM(standard_methods)) |
| 658 |
return (standard_methods[idx])(); |
| 659 |
if (app_pkey_methods == NULL) |
| 660 |
return NULL; |
| 661 |
idx -= OSSL_NELEM(standard_methods); |
| 662 |
if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) |
| 663 |
return NULL; |
| 664 |
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, (int)idx); |
| 665 |
} |
| 666 |
#endif |
| 667 |
|
| 668 |
int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype) |
| 669 |
{ |
| 670 |
#ifndef FIPS_MODULE |
| 671 |
if (evp_pkey_ctx_is_legacy(ctx)) |
| 672 |
return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype)); |
| 673 |
#endif |
| 674 |
return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype); |
| 675 |
} |
| 676 |
|
| 677 |
int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params) |
| 678 |
{ |
| 679 |
switch (evp_pkey_ctx_state(ctx)) { |
| 680 |
case EVP_PKEY_STATE_PROVIDER: |
| 681 |
if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
| 682 |
&& ctx->op.kex.exchange != NULL |
| 683 |
&& ctx->op.kex.exchange->set_ctx_params != NULL) |
| 684 |
return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx, |
| 685 |
params); |
| 686 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
| 687 |
&& ctx->op.sig.signature != NULL |
| 688 |
&& ctx->op.sig.signature->set_ctx_params != NULL) |
| 689 |
return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx, |
| 690 |
params); |
| 691 |
if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
| 692 |
&& ctx->op.ciph.cipher != NULL |
| 693 |
&& ctx->op.ciph.cipher->set_ctx_params != NULL) |
| 694 |
return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx, |
| 695 |
params); |
| 696 |
if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
| 697 |
&& ctx->keymgmt != NULL |
| 698 |
&& ctx->keymgmt->gen_set_params != NULL) |
| 699 |
return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx, |
| 700 |
params); |
| 701 |
if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
| 702 |
&& ctx->op.encap.kem != NULL |
| 703 |
&& ctx->op.encap.kem->set_ctx_params != NULL) |
| 704 |
return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx, |
| 705 |
params); |
| 706 |
break; |
| 707 |
case EVP_PKEY_STATE_UNKNOWN: |
| 708 |
break; |
| 709 |
#ifndef FIPS_MODULE |
| 710 |
case EVP_PKEY_STATE_LEGACY: |
| 711 |
return evp_pkey_ctx_set_params_to_ctrl(ctx, params); |
| 712 |
#endif |
| 713 |
} |
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
| 718 |
{ |
| 719 |
switch (evp_pkey_ctx_state(ctx)) { |
| 720 |
case EVP_PKEY_STATE_PROVIDER: |
| 721 |
if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
| 722 |
&& ctx->op.kex.exchange != NULL |
| 723 |
&& ctx->op.kex.exchange->get_ctx_params != NULL) |
| 724 |
return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx, |
| 725 |
params); |
| 726 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
| 727 |
&& ctx->op.sig.signature != NULL |
| 728 |
&& ctx->op.sig.signature->get_ctx_params != NULL) |
| 729 |
return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx, |
| 730 |
params); |
| 731 |
if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
| 732 |
&& ctx->op.ciph.cipher != NULL |
| 733 |
&& ctx->op.ciph.cipher->get_ctx_params != NULL) |
| 734 |
return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx, |
| 735 |
params); |
| 736 |
if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
| 737 |
&& ctx->op.encap.kem != NULL |
| 738 |
&& ctx->op.encap.kem->get_ctx_params != NULL) |
| 739 |
return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx, |
| 740 |
params); |
| 741 |
if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
| 742 |
&& ctx->keymgmt != NULL |
| 743 |
&& ctx->keymgmt->gen_get_params != NULL) |
| 744 |
return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx, |
| 745 |
params); |
| 746 |
break; |
| 747 |
case EVP_PKEY_STATE_UNKNOWN: |
| 748 |
break; |
| 749 |
#ifndef FIPS_MODULE |
| 750 |
case EVP_PKEY_STATE_LEGACY: |
| 751 |
return evp_pkey_ctx_get_params_to_ctrl(ctx, params); |
| 752 |
#endif |
| 753 |
} |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
#ifndef FIPS_MODULE |
| 758 |
const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx) |
| 759 |
{ |
| 760 |
void *provctx; |
| 761 |
|
| 762 |
if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
| 763 |
&& ctx->op.kex.exchange != NULL |
| 764 |
&& ctx->op.kex.exchange->gettable_ctx_params != NULL) { |
| 765 |
provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
| 766 |
return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx, |
| 767 |
provctx); |
| 768 |
} |
| 769 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
| 770 |
&& ctx->op.sig.signature != NULL |
| 771 |
&& ctx->op.sig.signature->gettable_ctx_params != NULL) { |
| 772 |
provctx = ossl_provider_ctx( |
| 773 |
EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
| 774 |
return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx, |
| 775 |
provctx); |
| 776 |
} |
| 777 |
if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
| 778 |
&& ctx->op.ciph.cipher != NULL |
| 779 |
&& ctx->op.ciph.cipher->gettable_ctx_params != NULL) { |
| 780 |
provctx = ossl_provider_ctx( |
| 781 |
EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
| 782 |
return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx, |
| 783 |
provctx); |
| 784 |
} |
| 785 |
if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
| 786 |
&& ctx->op.encap.kem != NULL |
| 787 |
&& ctx->op.encap.kem->gettable_ctx_params != NULL) { |
| 788 |
provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
| 789 |
return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx, |
| 790 |
provctx); |
| 791 |
} |
| 792 |
if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
| 793 |
&& ctx->keymgmt != NULL |
| 794 |
&& ctx->keymgmt->gen_gettable_params != NULL) { |
| 795 |
provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt)); |
| 796 |
return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx, |
| 797 |
provctx); |
| 798 |
} |
| 799 |
return NULL; |
| 800 |
} |
| 801 |
|
| 802 |
const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx) |
| 803 |
{ |
| 804 |
void *provctx; |
| 805 |
|
| 806 |
if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
| 807 |
&& ctx->op.kex.exchange != NULL |
| 808 |
&& ctx->op.kex.exchange->settable_ctx_params != NULL) { |
| 809 |
provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
| 810 |
return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx, |
| 811 |
provctx); |
| 812 |
} |
| 813 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
| 814 |
&& ctx->op.sig.signature != NULL |
| 815 |
&& ctx->op.sig.signature->settable_ctx_params != NULL) { |
| 816 |
provctx = ossl_provider_ctx( |
| 817 |
EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
| 818 |
return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx, |
| 819 |
provctx); |
| 820 |
} |
| 821 |
if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
| 822 |
&& ctx->op.ciph.cipher != NULL |
| 823 |
&& ctx->op.ciph.cipher->settable_ctx_params != NULL) { |
| 824 |
provctx = ossl_provider_ctx( |
| 825 |
EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
| 826 |
return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx, |
| 827 |
provctx); |
| 828 |
} |
| 829 |
if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
| 830 |
&& ctx->keymgmt != NULL |
| 831 |
&& ctx->keymgmt->gen_settable_params != NULL) { |
| 832 |
provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt)); |
| 833 |
return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx, |
| 834 |
provctx); |
| 835 |
} |
| 836 |
if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
| 837 |
&& ctx->op.encap.kem != NULL |
| 838 |
&& ctx->op.encap.kem->settable_ctx_params != NULL) { |
| 839 |
provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
| 840 |
return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx, |
| 841 |
provctx); |
| 842 |
} |
| 843 |
return NULL; |
| 844 |
} |
| 845 |
|
| 846 |
/* |
| 847 |
* Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). |
| 848 |
* |
| 849 |
* Return 1 on success, 0 or negative for errors. |
| 850 |
* |
| 851 |
* In particular they return -2 if any of the params is not supported. |
| 852 |
* |
| 853 |
* They are not available in FIPS_MODULE as they depend on |
| 854 |
* - EVP_PKEY_CTX_{get,set}_params() |
| 855 |
* - EVP_PKEY_CTX_{gettable,settable}_params() |
| 856 |
* |
| 857 |
*/ |
| 858 |
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
| 859 |
{ |
| 860 |
if (ctx == NULL || params == NULL) |
| 861 |
return 0; |
| 862 |
|
| 863 |
/* |
| 864 |
* We only check for provider side EVP_PKEY_CTX. For #legacy, we |
| 865 |
* depend on the translation that happens in EVP_PKEY_CTX_set_params() |
| 866 |
* call, and that the resulting ctrl call will return -2 if it doesn't |
| 867 |
* known the ctrl command number. |
| 868 |
*/ |
| 869 |
if (evp_pkey_ctx_is_provided(ctx)) { |
| 870 |
const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx); |
| 871 |
const OSSL_PARAM *p; |
| 872 |
|
| 873 |
for (p = params; p->key != NULL; p++) { |
| 874 |
/* Check the ctx actually understands this parameter */ |
| 875 |
if (OSSL_PARAM_locate_const(settable, p->key) == NULL) |
| 876 |
return -2; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
return EVP_PKEY_CTX_set_params(ctx, params); |
| 881 |
} |
| 882 |
|
| 883 |
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
| 884 |
{ |
| 885 |
if (ctx == NULL || params == NULL) |
| 886 |
return 0; |
| 887 |
|
| 888 |
/* |
| 889 |
* We only check for provider side EVP_PKEY_CTX. For #legacy, we |
| 890 |
* depend on the translation that happens in EVP_PKEY_CTX_get_params() |
| 891 |
* call, and that the resulting ctrl call will return -2 if it doesn't |
| 892 |
* known the ctrl command number. |
| 893 |
*/ |
| 894 |
if (evp_pkey_ctx_is_provided(ctx)) { |
| 895 |
const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx); |
| 896 |
const OSSL_PARAM *p; |
| 897 |
|
| 898 |
for (p = params; p->key != NULL; p++) { |
| 899 |
/* Check the ctx actually understands this parameter */ |
| 900 |
if (OSSL_PARAM_locate_const(gettable, p->key) == NULL) |
| 901 |
return -2; |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
return EVP_PKEY_CTX_get_params(ctx, params); |
| 906 |
} |
| 907 |
|
| 908 |
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
| 909 |
{ |
| 910 |
OSSL_PARAM sig_md_params[2], *p = sig_md_params; |
| 911 |
/* 80 should be big enough */ |
| 912 |
char name[80] = ""; |
| 913 |
const EVP_MD *tmp; |
| 914 |
|
| 915 |
if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
| 916 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 917 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 918 |
return -2; |
| 919 |
} |
| 920 |
|
| 921 |
if (ctx->op.sig.algctx == NULL) |
| 922 |
return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
| 923 |
EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); |
| 924 |
|
| 925 |
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
| 926 |
name, |
| 927 |
sizeof(name)); |
| 928 |
*p = OSSL_PARAM_construct_end(); |
| 929 |
|
| 930 |
if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) |
| 931 |
return 0; |
| 932 |
|
| 933 |
tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
| 934 |
if (tmp == NULL) |
| 935 |
return 0; |
| 936 |
|
| 937 |
*md = tmp; |
| 938 |
|
| 939 |
return 1; |
| 940 |
} |
| 941 |
|
| 942 |
static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md, |
| 943 |
int fallback, const char *param, int op, |
| 944 |
int ctrl) |
| 945 |
{ |
| 946 |
OSSL_PARAM md_params[2], *p = md_params; |
| 947 |
const char *name; |
| 948 |
|
| 949 |
if (ctx == NULL || (ctx->operation & op) == 0) { |
| 950 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 951 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 952 |
return -2; |
| 953 |
} |
| 954 |
|
| 955 |
if (fallback) |
| 956 |
return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md)); |
| 957 |
|
| 958 |
if (md == NULL) { |
| 959 |
name = ""; |
| 960 |
} else { |
| 961 |
name = EVP_MD_get0_name(md); |
| 962 |
} |
| 963 |
|
| 964 |
*p++ = OSSL_PARAM_construct_utf8_string(param, |
| 965 |
/* |
| 966 |
* Cast away the const. This is read |
| 967 |
* only so should be safe |
| 968 |
*/ |
| 969 |
(char *)name, 0); |
| 970 |
*p = OSSL_PARAM_construct_end(); |
| 971 |
|
| 972 |
return EVP_PKEY_CTX_set_params(ctx, md_params); |
| 973 |
} |
| 974 |
|
| 975 |
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 976 |
{ |
| 977 |
return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL, |
| 978 |
OSSL_SIGNATURE_PARAM_DIGEST, |
| 979 |
EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD); |
| 980 |
} |
| 981 |
|
| 982 |
int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 983 |
{ |
| 984 |
return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
| 985 |
OSSL_KDF_PARAM_DIGEST, |
| 986 |
EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD); |
| 987 |
} |
| 988 |
|
| 989 |
static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback, |
| 990 |
const char *param, int op, int ctrl, |
| 991 |
const unsigned char *data, |
| 992 |
int datalen) |
| 993 |
{ |
| 994 |
OSSL_PARAM octet_string_params[2], *p = octet_string_params; |
| 995 |
|
| 996 |
if (ctx == NULL || (ctx->operation & op) == 0) { |
| 997 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 998 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 999 |
return -2; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* Code below to be removed when legacy support is dropped. */ |
| 1003 |
if (fallback) |
| 1004 |
return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); |
| 1005 |
/* end of legacy support */ |
| 1006 |
|
| 1007 |
if (datalen < 0) { |
| 1008 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
|
| 1012 |
*p++ = OSSL_PARAM_construct_octet_string(param, |
| 1013 |
/* |
| 1014 |
* Cast away the const. This is read |
| 1015 |
* only so should be safe |
| 1016 |
*/ |
| 1017 |
(unsigned char *)data, |
| 1018 |
(size_t)datalen); |
| 1019 |
*p = OSSL_PARAM_construct_end(); |
| 1020 |
|
| 1021 |
return EVP_PKEY_CTX_set_params(ctx, octet_string_params); |
| 1022 |
} |
| 1023 |
|
| 1024 |
static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback, |
| 1025 |
const char *param, int op, int ctrl, |
| 1026 |
const unsigned char *data, |
| 1027 |
int datalen) |
| 1028 |
{ |
| 1029 |
OSSL_PARAM os_params[2]; |
| 1030 |
const OSSL_PARAM *gettables; |
| 1031 |
unsigned char *info = NULL; |
| 1032 |
size_t info_len = 0; |
| 1033 |
size_t info_alloc = 0; |
| 1034 |
int ret = 0; |
| 1035 |
|
| 1036 |
if (ctx == NULL || (ctx->operation & op) == 0) { |
| 1037 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1038 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 1039 |
return -2; |
| 1040 |
} |
| 1041 |
|
| 1042 |
/* Code below to be removed when legacy support is dropped. */ |
| 1043 |
if (fallback) |
| 1044 |
return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); |
| 1045 |
/* end of legacy support */ |
| 1046 |
|
| 1047 |
if (datalen < 0) { |
| 1048 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
| 1049 |
return 0; |
| 1050 |
} else if (datalen == 0) { |
| 1051 |
return 1; |
| 1052 |
} |
| 1053 |
|
| 1054 |
/* Check for older provider that doesn't support getting this parameter */ |
| 1055 |
gettables = EVP_PKEY_CTX_gettable_params(ctx); |
| 1056 |
if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL) |
| 1057 |
return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl, |
| 1058 |
data, datalen); |
| 1059 |
|
| 1060 |
/* Get the original value length */ |
| 1061 |
os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0); |
| 1062 |
os_params[1] = OSSL_PARAM_construct_end(); |
| 1063 |
|
| 1064 |
if (!EVP_PKEY_CTX_get_params(ctx, os_params)) |
| 1065 |
return 0; |
| 1066 |
|
| 1067 |
/* This should not happen but check to be sure. */ |
| 1068 |
if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED) |
| 1069 |
return 0; |
| 1070 |
|
| 1071 |
info_alloc = os_params[0].return_size + datalen; |
| 1072 |
if (info_alloc == 0) |
| 1073 |
return 0; |
| 1074 |
info = OPENSSL_zalloc(info_alloc); |
| 1075 |
if (info == NULL) |
| 1076 |
return 0; |
| 1077 |
info_len = os_params[0].return_size; |
| 1078 |
|
| 1079 |
os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc); |
| 1080 |
|
| 1081 |
/* if we have data, then go get it */ |
| 1082 |
if (info_len > 0) { |
| 1083 |
if (!EVP_PKEY_CTX_get_params(ctx, os_params)) |
| 1084 |
goto error; |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* Copy the input data */ |
| 1088 |
memcpy(&info[info_len], data, datalen); |
| 1089 |
ret = EVP_PKEY_CTX_set_params(ctx, os_params); |
| 1090 |
|
| 1091 |
error: |
| 1092 |
OPENSSL_clear_free(info, info_alloc); |
| 1093 |
return ret; |
| 1094 |
} |
| 1095 |
|
| 1096 |
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx, |
| 1097 |
const unsigned char *sec, int seclen) |
| 1098 |
{ |
| 1099 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1100 |
OSSL_KDF_PARAM_SECRET, |
| 1101 |
EVP_PKEY_OP_DERIVE, |
| 1102 |
EVP_PKEY_CTRL_TLS_SECRET, |
| 1103 |
sec, seclen); |
| 1104 |
} |
| 1105 |
|
| 1106 |
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx, |
| 1107 |
const unsigned char *seed, int seedlen) |
| 1108 |
{ |
| 1109 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1110 |
OSSL_KDF_PARAM_SEED, |
| 1111 |
EVP_PKEY_OP_DERIVE, |
| 1112 |
EVP_PKEY_CTRL_TLS_SEED, |
| 1113 |
seed, seedlen); |
| 1114 |
} |
| 1115 |
|
| 1116 |
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 1117 |
{ |
| 1118 |
return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
| 1119 |
OSSL_KDF_PARAM_DIGEST, |
| 1120 |
EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD); |
| 1121 |
} |
| 1122 |
|
| 1123 |
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, |
| 1124 |
const unsigned char *salt, int saltlen) |
| 1125 |
{ |
| 1126 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1127 |
OSSL_KDF_PARAM_SALT, |
| 1128 |
EVP_PKEY_OP_DERIVE, |
| 1129 |
EVP_PKEY_CTRL_HKDF_SALT, |
| 1130 |
salt, saltlen); |
| 1131 |
} |
| 1132 |
|
| 1133 |
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, |
| 1134 |
const unsigned char *key, int keylen) |
| 1135 |
{ |
| 1136 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1137 |
OSSL_KDF_PARAM_KEY, |
| 1138 |
EVP_PKEY_OP_DERIVE, |
| 1139 |
EVP_PKEY_CTRL_HKDF_KEY, |
| 1140 |
key, keylen); |
| 1141 |
} |
| 1142 |
|
| 1143 |
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, |
| 1144 |
const unsigned char *info, int infolen) |
| 1145 |
{ |
| 1146 |
return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1147 |
OSSL_KDF_PARAM_INFO, |
| 1148 |
EVP_PKEY_OP_DERIVE, |
| 1149 |
EVP_PKEY_CTRL_HKDF_INFO, |
| 1150 |
info, infolen); |
| 1151 |
} |
| 1152 |
|
| 1153 |
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode) |
| 1154 |
{ |
| 1155 |
OSSL_PARAM int_params[2], *p = int_params; |
| 1156 |
|
| 1157 |
if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 1158 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1159 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 1160 |
return -2; |
| 1161 |
} |
| 1162 |
|
| 1163 |
/* Code below to be removed when legacy support is dropped. */ |
| 1164 |
if (ctx->op.kex.algctx == NULL) |
| 1165 |
return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE, |
| 1166 |
EVP_PKEY_CTRL_HKDF_MODE, mode, NULL); |
| 1167 |
/* end of legacy support */ |
| 1168 |
|
| 1169 |
if (mode < 0) { |
| 1170 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
| 1171 |
return 0; |
| 1172 |
} |
| 1173 |
|
| 1174 |
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
| 1175 |
*p = OSSL_PARAM_construct_end(); |
| 1176 |
|
| 1177 |
return EVP_PKEY_CTX_set_params(ctx, int_params); |
| 1178 |
} |
| 1179 |
|
| 1180 |
int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass, |
| 1181 |
int passlen) |
| 1182 |
{ |
| 1183 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1184 |
OSSL_KDF_PARAM_PASSWORD, |
| 1185 |
EVP_PKEY_OP_DERIVE, |
| 1186 |
EVP_PKEY_CTRL_PASS, |
| 1187 |
(const unsigned char *)pass, passlen); |
| 1188 |
} |
| 1189 |
|
| 1190 |
int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx, |
| 1191 |
const unsigned char *salt, int saltlen) |
| 1192 |
{ |
| 1193 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
| 1194 |
OSSL_KDF_PARAM_SALT, |
| 1195 |
EVP_PKEY_OP_DERIVE, |
| 1196 |
EVP_PKEY_CTRL_SCRYPT_SALT, |
| 1197 |
salt, saltlen); |
| 1198 |
} |
| 1199 |
|
| 1200 |
static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param, |
| 1201 |
int op, int ctrl, uint64_t val) |
| 1202 |
{ |
| 1203 |
OSSL_PARAM uint64_params[2], *p = uint64_params; |
| 1204 |
|
| 1205 |
if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 1206 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1207 |
/* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
| 1208 |
return -2; |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* Code below to be removed when legacy support is dropped. */ |
| 1212 |
if (ctx->op.kex.algctx == NULL) |
| 1213 |
return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val); |
| 1214 |
/* end of legacy support */ |
| 1215 |
|
| 1216 |
*p++ = OSSL_PARAM_construct_uint64(param, &val); |
| 1217 |
*p = OSSL_PARAM_construct_end(); |
| 1218 |
|
| 1219 |
return EVP_PKEY_CTX_set_params(ctx, uint64_params); |
| 1220 |
} |
| 1221 |
|
| 1222 |
int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n) |
| 1223 |
{ |
| 1224 |
return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N, |
| 1225 |
EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N, |
| 1226 |
n); |
| 1227 |
} |
| 1228 |
|
| 1229 |
int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r) |
| 1230 |
{ |
| 1231 |
return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R, |
| 1232 |
EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R, |
| 1233 |
r); |
| 1234 |
} |
| 1235 |
|
| 1236 |
int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p) |
| 1237 |
{ |
| 1238 |
return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P, |
| 1239 |
EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P, |
| 1240 |
p); |
| 1241 |
} |
| 1242 |
|
| 1243 |
int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx, |
| 1244 |
uint64_t maxmem_bytes) |
| 1245 |
{ |
| 1246 |
return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM, |
| 1247 |
EVP_PKEY_OP_DERIVE, |
| 1248 |
EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, |
| 1249 |
maxmem_bytes); |
| 1250 |
} |
| 1251 |
|
| 1252 |
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key, |
| 1253 |
int keylen) |
| 1254 |
{ |
| 1255 |
return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL, |
| 1256 |
OSSL_PKEY_PARAM_PRIV_KEY, |
| 1257 |
EVP_PKEY_OP_KEYGEN, |
| 1258 |
EVP_PKEY_CTRL_SET_MAC_KEY, |
| 1259 |
key, keylen); |
| 1260 |
} |
| 1261 |
|
| 1262 |
int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op) |
| 1263 |
{ |
| 1264 |
OSSL_PARAM params[2], *p = params; |
| 1265 |
|
| 1266 |
if (ctx == NULL || op == NULL) { |
| 1267 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
| 1268 |
return 0; |
| 1269 |
} |
| 1270 |
if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
| 1271 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1272 |
return -2; |
| 1273 |
} |
| 1274 |
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, |
| 1275 |
(char *)op, 0); |
| 1276 |
*p = OSSL_PARAM_construct_end(); |
| 1277 |
return EVP_PKEY_CTX_set_params(ctx, params); |
| 1278 |
} |
| 1279 |
|
| 1280 |
int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len) |
| 1281 |
{ |
| 1282 |
return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
| 1283 |
EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id)); |
| 1284 |
} |
| 1285 |
|
| 1286 |
int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id) |
| 1287 |
{ |
| 1288 |
return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id); |
| 1289 |
} |
| 1290 |
|
| 1291 |
int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len) |
| 1292 |
{ |
| 1293 |
return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
| 1294 |
EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len); |
| 1295 |
} |
| 1296 |
|
| 1297 |
static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype, |
| 1298 |
int cmd, int p1, void *p2) |
| 1299 |
{ |
| 1300 |
int ret = 0; |
| 1301 |
|
| 1302 |
/* |
| 1303 |
* If the method has a |digest_custom| function, we can relax the |
| 1304 |
* operation type check, since this can be called before the operation |
| 1305 |
* is initialized. |
| 1306 |
*/ |
| 1307 |
if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) { |
| 1308 |
if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
| 1309 |
ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET); |
| 1310 |
return -1; |
| 1311 |
} |
| 1312 |
|
| 1313 |
if ((optype != -1) && !(ctx->operation & optype)) { |
| 1314 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
| 1315 |
return -1; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
switch (evp_pkey_ctx_state(ctx)) { |
| 1320 |
case EVP_PKEY_STATE_PROVIDER: |
| 1321 |
return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
| 1322 |
case EVP_PKEY_STATE_UNKNOWN: |
| 1323 |
case EVP_PKEY_STATE_LEGACY: |
| 1324 |
if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { |
| 1325 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1326 |
return -2; |
| 1327 |
} |
| 1328 |
if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) |
| 1329 |
return -1; |
| 1330 |
|
| 1331 |
ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
| 1332 |
|
| 1333 |
if (ret == -2) |
| 1334 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1335 |
break; |
| 1336 |
} |
| 1337 |
return ret; |
| 1338 |
} |
| 1339 |
|
| 1340 |
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
| 1341 |
int cmd, int p1, void *p2) |
| 1342 |
{ |
| 1343 |
int ret = 0; |
| 1344 |
|
| 1345 |
if (ctx == NULL) { |
| 1346 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1347 |
return -2; |
| 1348 |
} |
| 1349 |
/* If unsupported, we don't want that reported here */ |
| 1350 |
ERR_set_mark(); |
| 1351 |
ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype, |
| 1352 |
cmd, NULL, p2, p1); |
| 1353 |
if (ret == -2) { |
| 1354 |
ERR_pop_to_mark(); |
| 1355 |
} else { |
| 1356 |
ERR_clear_last_mark(); |
| 1357 |
/* |
| 1358 |
* If there was an error, there was an error. |
| 1359 |
* If the operation isn't initialized yet, we also return, as |
| 1360 |
* the saved values will be used then anyway. |
| 1361 |
*/ |
| 1362 |
if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
| 1363 |
return ret; |
| 1364 |
} |
| 1365 |
return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2); |
| 1366 |
} |
| 1367 |
|
| 1368 |
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
| 1369 |
int cmd, uint64_t value) |
| 1370 |
{ |
| 1371 |
return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); |
| 1372 |
} |
| 1373 |
|
| 1374 |
static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx, |
| 1375 |
const char *name, const char *value) |
| 1376 |
{ |
| 1377 |
int ret = 0; |
| 1378 |
|
| 1379 |
if (ctx == NULL) { |
| 1380 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1381 |
return -2; |
| 1382 |
} |
| 1383 |
|
| 1384 |
switch (evp_pkey_ctx_state(ctx)) { |
| 1385 |
case EVP_PKEY_STATE_PROVIDER: |
| 1386 |
return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value); |
| 1387 |
case EVP_PKEY_STATE_UNKNOWN: |
| 1388 |
case EVP_PKEY_STATE_LEGACY: |
| 1389 |
if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) { |
| 1390 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1391 |
return -2; |
| 1392 |
} |
| 1393 |
if (strcmp(name, "digest") == 0) |
| 1394 |
ret = EVP_PKEY_CTX_md(ctx, |
| 1395 |
EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
| 1396 |
EVP_PKEY_CTRL_MD, value); |
| 1397 |
else |
| 1398 |
ret = ctx->pmeth->ctrl_str(ctx, name, value); |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
|
| 1402 |
return ret; |
| 1403 |
} |
| 1404 |
|
| 1405 |
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
| 1406 |
const char *name, const char *value) |
| 1407 |
{ |
| 1408 |
int ret = 0; |
| 1409 |
|
| 1410 |
/* If unsupported, we don't want that reported here */ |
| 1411 |
ERR_set_mark(); |
| 1412 |
ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1, |
| 1413 |
name, value, strlen(value) + 1); |
| 1414 |
if (ret == -2) { |
| 1415 |
ERR_pop_to_mark(); |
| 1416 |
} else { |
| 1417 |
ERR_clear_last_mark(); |
| 1418 |
/* |
| 1419 |
* If there was an error, there was an error. |
| 1420 |
* If the operation isn't initialized yet, we also return, as |
| 1421 |
* the saved values will be used then anyway. |
| 1422 |
*/ |
| 1423 |
if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
| 1424 |
return ret; |
| 1425 |
} |
| 1426 |
|
| 1427 |
return evp_pkey_ctx_ctrl_str_int(ctx, name, value); |
| 1428 |
} |
| 1429 |
|
| 1430 |
static int decode_cmd(int cmd, const char *name) |
| 1431 |
{ |
| 1432 |
if (cmd == -1) { |
| 1433 |
/* |
| 1434 |
* The consequence of the assertion not being true is that this |
| 1435 |
* function will return -1, which will cause the calling functions |
| 1436 |
* to signal that the command is unsupported... in non-debug mode. |
| 1437 |
*/ |
| 1438 |
if (ossl_assert(name != NULL)) |
| 1439 |
if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0) |
| 1440 |
cmd = EVP_PKEY_CTRL_SET1_ID; |
| 1441 |
} |
| 1442 |
|
| 1443 |
return cmd; |
| 1444 |
} |
| 1445 |
|
| 1446 |
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
| 1447 |
int keytype, int optype, |
| 1448 |
int cmd, const char *name, |
| 1449 |
const void *data, size_t data_len) |
| 1450 |
{ |
| 1451 |
/* |
| 1452 |
* Check that it's one of the supported commands. The ctrl commands |
| 1453 |
* number cases here must correspond to the cases in the bottom switch |
| 1454 |
* in this function. |
| 1455 |
*/ |
| 1456 |
switch (cmd = decode_cmd(cmd, name)) { |
| 1457 |
case EVP_PKEY_CTRL_SET1_ID: |
| 1458 |
break; |
| 1459 |
default: |
| 1460 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1461 |
return -2; |
| 1462 |
} |
| 1463 |
|
| 1464 |
if (keytype != -1) { |
| 1465 |
switch (evp_pkey_ctx_state(ctx)) { |
| 1466 |
case EVP_PKEY_STATE_PROVIDER: |
| 1467 |
if (ctx->keymgmt == NULL) { |
| 1468 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1469 |
return -2; |
| 1470 |
} |
| 1471 |
if (!EVP_KEYMGMT_is_a(ctx->keymgmt, |
| 1472 |
evp_pkey_type2name(keytype))) { |
| 1473 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
| 1474 |
return -1; |
| 1475 |
} |
| 1476 |
break; |
| 1477 |
case EVP_PKEY_STATE_UNKNOWN: |
| 1478 |
case EVP_PKEY_STATE_LEGACY: |
| 1479 |
if (ctx->pmeth == NULL) { |
| 1480 |
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1481 |
return -2; |
| 1482 |
} |
| 1483 |
if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) { |
| 1484 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
| 1485 |
return -1; |
| 1486 |
} |
| 1487 |
break; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
if (optype != -1 && (ctx->operation & optype) == 0) { |
| 1491 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
| 1492 |
return -1; |
| 1493 |
} |
| 1494 |
|
| 1495 |
switch (cmd) { |
| 1496 |
case EVP_PKEY_CTRL_SET1_ID: |
| 1497 |
evp_pkey_ctx_free_cached_data(ctx, cmd, name); |
| 1498 |
if (name != NULL) { |
| 1499 |
ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name); |
| 1500 |
if (ctx->cached_parameters.dist_id_name == NULL) |
| 1501 |
return 0; |
| 1502 |
} |
| 1503 |
if (data_len > 0) { |
| 1504 |
ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len); |
| 1505 |
if (ctx->cached_parameters.dist_id == NULL) |
| 1506 |
return 0; |
| 1507 |
} |
| 1508 |
ctx->cached_parameters.dist_id_set = 1; |
| 1509 |
ctx->cached_parameters.dist_id_len = data_len; |
| 1510 |
break; |
| 1511 |
} |
| 1512 |
return 1; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
| 1516 |
int cmd, const char *name) |
| 1517 |
{ |
| 1518 |
cmd = decode_cmd(cmd, name); |
| 1519 |
switch (cmd) { |
| 1520 |
case EVP_PKEY_CTRL_SET1_ID: |
| 1521 |
OPENSSL_free(ctx->cached_parameters.dist_id); |
| 1522 |
OPENSSL_free(ctx->cached_parameters.dist_id_name); |
| 1523 |
ctx->cached_parameters.dist_id = NULL; |
| 1524 |
ctx->cached_parameters.dist_id_name = NULL; |
| 1525 |
break; |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx) |
| 1530 |
{ |
| 1531 |
evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL); |
| 1532 |
} |
| 1533 |
|
| 1534 |
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx) |
| 1535 |
{ |
| 1536 |
int ret = 1; |
| 1537 |
|
| 1538 |
if (ret && ctx->cached_parameters.dist_id_set) { |
| 1539 |
const char *name = ctx->cached_parameters.dist_id_name; |
| 1540 |
const void *val = ctx->cached_parameters.dist_id; |
| 1541 |
size_t len = ctx->cached_parameters.dist_id_len; |
| 1542 |
|
| 1543 |
if (name != NULL) |
| 1544 |
ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val); |
| 1545 |
else |
| 1546 |
ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation, |
| 1547 |
EVP_PKEY_CTRL_SET1_ID, |
| 1548 |
(int)len, (void *)val); |
| 1549 |
} |
| 1550 |
|
| 1551 |
return ret; |
| 1552 |
} |
| 1553 |
|
| 1554 |
OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx) |
| 1555 |
{ |
| 1556 |
return ctx->libctx; |
| 1557 |
} |
| 1558 |
|
| 1559 |
const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx) |
| 1560 |
{ |
| 1561 |
return ctx->propquery; |
| 1562 |
} |
| 1563 |
|
| 1564 |
const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx) |
| 1565 |
{ |
| 1566 |
if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
| 1567 |
if (ctx->op.sig.signature != NULL) |
| 1568 |
return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature); |
| 1569 |
} else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
| 1570 |
if (ctx->op.kex.exchange != NULL) |
| 1571 |
return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange); |
| 1572 |
} else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
| 1573 |
if (ctx->op.encap.kem != NULL) |
| 1574 |
return EVP_KEM_get0_provider(ctx->op.encap.kem); |
| 1575 |
} else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
| 1576 |
if (ctx->op.ciph.cipher != NULL) |
| 1577 |
return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher); |
| 1578 |
} else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
| 1579 |
if (ctx->keymgmt != NULL) |
| 1580 |
return EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
| 1581 |
} |
| 1582 |
|
| 1583 |
return NULL; |
| 1584 |
} |
| 1585 |
|
| 1586 |
/* Utility functions to send a string of hex string to a ctrl */ |
| 1587 |
|
| 1588 |
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) |
| 1589 |
{ |
| 1590 |
size_t len; |
| 1591 |
|
| 1592 |
len = strlen(str); |
| 1593 |
if (len > INT_MAX) |
| 1594 |
return -1; |
| 1595 |
return ctx->pmeth->ctrl(ctx, cmd, (int)len, (void *)str); |
| 1596 |
} |
| 1597 |
|
| 1598 |
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) |
| 1599 |
{ |
| 1600 |
unsigned char *bin; |
| 1601 |
long binlen; |
| 1602 |
int rv = -1; |
| 1603 |
|
| 1604 |
bin = OPENSSL_hexstr2buf(hex, &binlen); |
| 1605 |
if (bin == NULL) |
| 1606 |
return 0; |
| 1607 |
if (binlen <= INT_MAX) |
| 1608 |
rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); |
| 1609 |
OPENSSL_free(bin); |
| 1610 |
return rv; |
| 1611 |
} |
| 1612 |
|
| 1613 |
/* Pass a message digest to a ctrl */ |
| 1614 |
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) |
| 1615 |
{ |
| 1616 |
const EVP_MD *m; |
| 1617 |
|
| 1618 |
if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
| 1619 |
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST); |
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); |
| 1623 |
} |
| 1624 |
|
| 1625 |
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
| 1626 |
{ |
| 1627 |
return ctx->operation; |
| 1628 |
} |
| 1629 |
|
| 1630 |
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) |
| 1631 |
{ |
| 1632 |
ctx->keygen_info = dat; |
| 1633 |
ctx->keygen_info_count = datlen; |
| 1634 |
} |
| 1635 |
|
| 1636 |
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
| 1637 |
{ |
| 1638 |
ctx->data = data; |
| 1639 |
} |
| 1640 |
|
| 1641 |
void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
| 1642 |
{ |
| 1643 |
return ctx->data; |
| 1644 |
} |
| 1645 |
|
| 1646 |
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
| 1647 |
{ |
| 1648 |
return ctx->pkey; |
| 1649 |
} |
| 1650 |
|
| 1651 |
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
| 1652 |
{ |
| 1653 |
return ctx->peerkey; |
| 1654 |
} |
| 1655 |
|
| 1656 |
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
| 1657 |
{ |
| 1658 |
ctx->app_data = data; |
| 1659 |
} |
| 1660 |
|
| 1661 |
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) |
| 1662 |
{ |
| 1663 |
return ctx->app_data; |
| 1664 |
} |
| 1665 |
|
| 1666 |
void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, |
| 1667 |
int (*init)(EVP_PKEY_CTX *ctx)) |
| 1668 |
{ |
| 1669 |
pmeth->init = init; |
| 1670 |
} |
| 1671 |
|
| 1672 |
void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, |
| 1673 |
int (*copy)(EVP_PKEY_CTX *dst, |
| 1674 |
const EVP_PKEY_CTX *src)) |
| 1675 |
{ |
| 1676 |
pmeth->copy = copy; |
| 1677 |
} |
| 1678 |
|
| 1679 |
void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, |
| 1680 |
void (*cleanup)(EVP_PKEY_CTX *ctx)) |
| 1681 |
{ |
| 1682 |
pmeth->cleanup = cleanup; |
| 1683 |
} |
| 1684 |
|
| 1685 |
void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, |
| 1686 |
int (*paramgen_init)(EVP_PKEY_CTX *ctx), |
| 1687 |
int (*paramgen)(EVP_PKEY_CTX *ctx, |
| 1688 |
EVP_PKEY *pkey)) |
| 1689 |
{ |
| 1690 |
pmeth->paramgen_init = paramgen_init; |
| 1691 |
pmeth->paramgen = paramgen; |
| 1692 |
} |
| 1693 |
|
| 1694 |
void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, |
| 1695 |
int (*keygen_init)(EVP_PKEY_CTX *ctx), |
| 1696 |
int (*keygen)(EVP_PKEY_CTX *ctx, |
| 1697 |
EVP_PKEY *pkey)) |
| 1698 |
{ |
| 1699 |
pmeth->keygen_init = keygen_init; |
| 1700 |
pmeth->keygen = keygen; |
| 1701 |
} |
| 1702 |
|
| 1703 |
void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, |
| 1704 |
int (*sign_init)(EVP_PKEY_CTX *ctx), |
| 1705 |
int (*sign)(EVP_PKEY_CTX *ctx, |
| 1706 |
unsigned char *sig, size_t *siglen, |
| 1707 |
const unsigned char *tbs, |
| 1708 |
size_t tbslen)) |
| 1709 |
{ |
| 1710 |
pmeth->sign_init = sign_init; |
| 1711 |
pmeth->sign = sign; |
| 1712 |
} |
| 1713 |
|
| 1714 |
void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, |
| 1715 |
int (*verify_init)(EVP_PKEY_CTX *ctx), |
| 1716 |
int (*verify)(EVP_PKEY_CTX *ctx, |
| 1717 |
const unsigned char *sig, |
| 1718 |
size_t siglen, |
| 1719 |
const unsigned char *tbs, |
| 1720 |
size_t tbslen)) |
| 1721 |
{ |
| 1722 |
pmeth->verify_init = verify_init; |
| 1723 |
pmeth->verify = verify; |
| 1724 |
} |
| 1725 |
|
| 1726 |
void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, |
| 1727 |
int (*verify_recover_init)(EVP_PKEY_CTX |
| 1728 |
*ctx), |
| 1729 |
int (*verify_recover)(EVP_PKEY_CTX |
| 1730 |
*ctx, |
| 1731 |
unsigned char |
| 1732 |
*sig, |
| 1733 |
size_t *siglen, |
| 1734 |
const unsigned char *tbs, |
| 1735 |
size_t tbslen)) |
| 1736 |
{ |
| 1737 |
pmeth->verify_recover_init = verify_recover_init; |
| 1738 |
pmeth->verify_recover = verify_recover; |
| 1739 |
} |
| 1740 |
|
| 1741 |
void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, |
| 1742 |
int (*signctx_init)(EVP_PKEY_CTX *ctx, |
| 1743 |
EVP_MD_CTX *mctx), |
| 1744 |
int (*signctx)(EVP_PKEY_CTX *ctx, |
| 1745 |
unsigned char *sig, |
| 1746 |
size_t *siglen, |
| 1747 |
EVP_MD_CTX *mctx)) |
| 1748 |
{ |
| 1749 |
pmeth->signctx_init = signctx_init; |
| 1750 |
pmeth->signctx = signctx; |
| 1751 |
} |
| 1752 |
|
| 1753 |
void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, |
| 1754 |
int (*verifyctx_init)(EVP_PKEY_CTX *ctx, |
| 1755 |
EVP_MD_CTX *mctx), |
| 1756 |
int (*verifyctx)(EVP_PKEY_CTX *ctx, |
| 1757 |
const unsigned char *sig, |
| 1758 |
int siglen, |
| 1759 |
EVP_MD_CTX *mctx)) |
| 1760 |
{ |
| 1761 |
pmeth->verifyctx_init = verifyctx_init; |
| 1762 |
pmeth->verifyctx = verifyctx; |
| 1763 |
} |
| 1764 |
|
| 1765 |
void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, |
| 1766 |
int (*encrypt_init)(EVP_PKEY_CTX *ctx), |
| 1767 |
int (*encryptfn)(EVP_PKEY_CTX *ctx, |
| 1768 |
unsigned char *out, |
| 1769 |
size_t *outlen, |
| 1770 |
const unsigned char *in, |
| 1771 |
size_t inlen)) |
| 1772 |
{ |
| 1773 |
pmeth->encrypt_init = encrypt_init; |
| 1774 |
pmeth->encrypt = encryptfn; |
| 1775 |
} |
| 1776 |
|
| 1777 |
void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, |
| 1778 |
int (*decrypt_init)(EVP_PKEY_CTX *ctx), |
| 1779 |
int (*decrypt)(EVP_PKEY_CTX *ctx, |
| 1780 |
unsigned char *out, |
| 1781 |
size_t *outlen, |
| 1782 |
const unsigned char *in, |
| 1783 |
size_t inlen)) |
| 1784 |
{ |
| 1785 |
pmeth->decrypt_init = decrypt_init; |
| 1786 |
pmeth->decrypt = decrypt; |
| 1787 |
} |
| 1788 |
|
| 1789 |
void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, |
| 1790 |
int (*derive_init)(EVP_PKEY_CTX *ctx), |
| 1791 |
int (*derive)(EVP_PKEY_CTX *ctx, |
| 1792 |
unsigned char *key, |
| 1793 |
size_t *keylen)) |
| 1794 |
{ |
| 1795 |
pmeth->derive_init = derive_init; |
| 1796 |
pmeth->derive = derive; |
| 1797 |
} |
| 1798 |
|
| 1799 |
void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, |
| 1800 |
int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, |
| 1801 |
void *p2), |
| 1802 |
int (*ctrl_str)(EVP_PKEY_CTX *ctx, |
| 1803 |
const char *type, |
| 1804 |
const char *value)) |
| 1805 |
{ |
| 1806 |
pmeth->ctrl = ctrl; |
| 1807 |
pmeth->ctrl_str = ctrl_str; |
| 1808 |
} |
| 1809 |
|
| 1810 |
void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth, |
| 1811 |
int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
| 1812 |
const unsigned char *tbs, size_t tbslen)) |
| 1813 |
{ |
| 1814 |
pmeth->digestsign = digestsign; |
| 1815 |
} |
| 1816 |
|
| 1817 |
void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth, |
| 1818 |
int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig, |
| 1819 |
size_t siglen, const unsigned char *tbs, |
| 1820 |
size_t tbslen)) |
| 1821 |
{ |
| 1822 |
pmeth->digestverify = digestverify; |
| 1823 |
} |
| 1824 |
|
| 1825 |
void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
| 1826 |
int (*check)(EVP_PKEY *pkey)) |
| 1827 |
{ |
| 1828 |
pmeth->check = check; |
| 1829 |
} |
| 1830 |
|
| 1831 |
void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
| 1832 |
int (*check)(EVP_PKEY *pkey)) |
| 1833 |
{ |
| 1834 |
pmeth->public_check = check; |
| 1835 |
} |
| 1836 |
|
| 1837 |
void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, |
| 1838 |
int (*check)(EVP_PKEY *pkey)) |
| 1839 |
{ |
| 1840 |
pmeth->param_check = check; |
| 1841 |
} |
| 1842 |
|
| 1843 |
void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
| 1844 |
int (*digest_custom)(EVP_PKEY_CTX *ctx, |
| 1845 |
EVP_MD_CTX *mctx)) |
| 1846 |
{ |
| 1847 |
pmeth->digest_custom = digest_custom; |
| 1848 |
} |
| 1849 |
|
| 1850 |
void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
| 1851 |
int (**pinit)(EVP_PKEY_CTX *ctx)) |
| 1852 |
{ |
| 1853 |
*pinit = pmeth->init; |
| 1854 |
} |
| 1855 |
|
| 1856 |
void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
| 1857 |
int (**pcopy)(EVP_PKEY_CTX *dst, |
| 1858 |
const EVP_PKEY_CTX *src)) |
| 1859 |
{ |
| 1860 |
*pcopy = pmeth->copy; |
| 1861 |
} |
| 1862 |
|
| 1863 |
void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
| 1864 |
void (**pcleanup)(EVP_PKEY_CTX *ctx)) |
| 1865 |
{ |
| 1866 |
*pcleanup = pmeth->cleanup; |
| 1867 |
} |
| 1868 |
|
| 1869 |
void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
| 1870 |
int (**pparamgen_init)(EVP_PKEY_CTX *ctx), |
| 1871 |
int (**pparamgen)(EVP_PKEY_CTX *ctx, |
| 1872 |
EVP_PKEY *pkey)) |
| 1873 |
{ |
| 1874 |
if (pparamgen_init) |
| 1875 |
*pparamgen_init = pmeth->paramgen_init; |
| 1876 |
if (pparamgen) |
| 1877 |
*pparamgen = pmeth->paramgen; |
| 1878 |
} |
| 1879 |
|
| 1880 |
void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
| 1881 |
int (**pkeygen_init)(EVP_PKEY_CTX *ctx), |
| 1882 |
int (**pkeygen)(EVP_PKEY_CTX *ctx, |
| 1883 |
EVP_PKEY *pkey)) |
| 1884 |
{ |
| 1885 |
if (pkeygen_init) |
| 1886 |
*pkeygen_init = pmeth->keygen_init; |
| 1887 |
if (pkeygen) |
| 1888 |
*pkeygen = pmeth->keygen; |
| 1889 |
} |
| 1890 |
|
| 1891 |
void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
| 1892 |
int (**psign_init)(EVP_PKEY_CTX *ctx), |
| 1893 |
int (**psign)(EVP_PKEY_CTX *ctx, |
| 1894 |
unsigned char *sig, size_t *siglen, |
| 1895 |
const unsigned char *tbs, |
| 1896 |
size_t tbslen)) |
| 1897 |
{ |
| 1898 |
if (psign_init) |
| 1899 |
*psign_init = pmeth->sign_init; |
| 1900 |
if (psign) |
| 1901 |
*psign = pmeth->sign; |
| 1902 |
} |
| 1903 |
|
| 1904 |
void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
| 1905 |
int (**pverify_init)(EVP_PKEY_CTX *ctx), |
| 1906 |
int (**pverify)(EVP_PKEY_CTX *ctx, |
| 1907 |
const unsigned char *sig, |
| 1908 |
size_t siglen, |
| 1909 |
const unsigned char *tbs, |
| 1910 |
size_t tbslen)) |
| 1911 |
{ |
| 1912 |
if (pverify_init) |
| 1913 |
*pverify_init = pmeth->verify_init; |
| 1914 |
if (pverify) |
| 1915 |
*pverify = pmeth->verify; |
| 1916 |
} |
| 1917 |
|
| 1918 |
void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
| 1919 |
int (**pverify_recover_init)(EVP_PKEY_CTX |
| 1920 |
*ctx), |
| 1921 |
int (**pverify_recover)(EVP_PKEY_CTX |
| 1922 |
*ctx, |
| 1923 |
unsigned char |
| 1924 |
*sig, |
| 1925 |
size_t *siglen, |
| 1926 |
const unsigned char *tbs, |
| 1927 |
size_t tbslen)) |
| 1928 |
{ |
| 1929 |
if (pverify_recover_init) |
| 1930 |
*pverify_recover_init = pmeth->verify_recover_init; |
| 1931 |
if (pverify_recover) |
| 1932 |
*pverify_recover = pmeth->verify_recover; |
| 1933 |
} |
| 1934 |
|
| 1935 |
void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
| 1936 |
int (**psignctx_init)(EVP_PKEY_CTX *ctx, |
| 1937 |
EVP_MD_CTX *mctx), |
| 1938 |
int (**psignctx)(EVP_PKEY_CTX *ctx, |
| 1939 |
unsigned char *sig, |
| 1940 |
size_t *siglen, |
| 1941 |
EVP_MD_CTX *mctx)) |
| 1942 |
{ |
| 1943 |
if (psignctx_init) |
| 1944 |
*psignctx_init = pmeth->signctx_init; |
| 1945 |
if (psignctx) |
| 1946 |
*psignctx = pmeth->signctx; |
| 1947 |
} |
| 1948 |
|
| 1949 |
void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
| 1950 |
int (**pverifyctx_init)(EVP_PKEY_CTX *ctx, |
| 1951 |
EVP_MD_CTX *mctx), |
| 1952 |
int (**pverifyctx)(EVP_PKEY_CTX *ctx, |
| 1953 |
const unsigned char *sig, |
| 1954 |
int siglen, |
| 1955 |
EVP_MD_CTX *mctx)) |
| 1956 |
{ |
| 1957 |
if (pverifyctx_init) |
| 1958 |
*pverifyctx_init = pmeth->verifyctx_init; |
| 1959 |
if (pverifyctx) |
| 1960 |
*pverifyctx = pmeth->verifyctx; |
| 1961 |
} |
| 1962 |
|
| 1963 |
void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
| 1964 |
int (**pencrypt_init)(EVP_PKEY_CTX *ctx), |
| 1965 |
int (**pencryptfn)(EVP_PKEY_CTX *ctx, |
| 1966 |
unsigned char *out, |
| 1967 |
size_t *outlen, |
| 1968 |
const unsigned char *in, |
| 1969 |
size_t inlen)) |
| 1970 |
{ |
| 1971 |
if (pencrypt_init) |
| 1972 |
*pencrypt_init = pmeth->encrypt_init; |
| 1973 |
if (pencryptfn) |
| 1974 |
*pencryptfn = pmeth->encrypt; |
| 1975 |
} |
| 1976 |
|
| 1977 |
void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
| 1978 |
int (**pdecrypt_init)(EVP_PKEY_CTX *ctx), |
| 1979 |
int (**pdecrypt)(EVP_PKEY_CTX *ctx, |
| 1980 |
unsigned char *out, |
| 1981 |
size_t *outlen, |
| 1982 |
const unsigned char *in, |
| 1983 |
size_t inlen)) |
| 1984 |
{ |
| 1985 |
if (pdecrypt_init) |
| 1986 |
*pdecrypt_init = pmeth->decrypt_init; |
| 1987 |
if (pdecrypt) |
| 1988 |
*pdecrypt = pmeth->decrypt; |
| 1989 |
} |
| 1990 |
|
| 1991 |
void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
| 1992 |
int (**pderive_init)(EVP_PKEY_CTX *ctx), |
| 1993 |
int (**pderive)(EVP_PKEY_CTX *ctx, |
| 1994 |
unsigned char *key, |
| 1995 |
size_t *keylen)) |
| 1996 |
{ |
| 1997 |
if (pderive_init) |
| 1998 |
*pderive_init = pmeth->derive_init; |
| 1999 |
if (pderive) |
| 2000 |
*pderive = pmeth->derive; |
| 2001 |
} |
| 2002 |
|
| 2003 |
void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
| 2004 |
int (**pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, |
| 2005 |
void *p2), |
| 2006 |
int (**pctrl_str)(EVP_PKEY_CTX *ctx, |
| 2007 |
const char *type, |
| 2008 |
const char *value)) |
| 2009 |
{ |
| 2010 |
if (pctrl) |
| 2011 |
*pctrl = pmeth->ctrl; |
| 2012 |
if (pctrl_str) |
| 2013 |
*pctrl_str = pmeth->ctrl_str; |
| 2014 |
} |
| 2015 |
|
| 2016 |
void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth, |
| 2017 |
int (**digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
| 2018 |
const unsigned char *tbs, size_t tbslen)) |
| 2019 |
{ |
| 2020 |
if (digestsign) |
| 2021 |
*digestsign = pmeth->digestsign; |
| 2022 |
} |
| 2023 |
|
| 2024 |
void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth, |
| 2025 |
int (**digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig, |
| 2026 |
size_t siglen, const unsigned char *tbs, |
| 2027 |
size_t tbslen)) |
| 2028 |
{ |
| 2029 |
if (digestverify) |
| 2030 |
*digestverify = pmeth->digestverify; |
| 2031 |
} |
| 2032 |
|
| 2033 |
void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
| 2034 |
int (**pcheck)(EVP_PKEY *pkey)) |
| 2035 |
{ |
| 2036 |
if (pcheck != NULL) |
| 2037 |
*pcheck = pmeth->check; |
| 2038 |
} |
| 2039 |
|
| 2040 |
void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
| 2041 |
int (**pcheck)(EVP_PKEY *pkey)) |
| 2042 |
{ |
| 2043 |
if (pcheck != NULL) |
| 2044 |
*pcheck = pmeth->public_check; |
| 2045 |
} |
| 2046 |
|
| 2047 |
void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
| 2048 |
int (**pcheck)(EVP_PKEY *pkey)) |
| 2049 |
{ |
| 2050 |
if (pcheck != NULL) |
| 2051 |
*pcheck = pmeth->param_check; |
| 2052 |
} |
| 2053 |
|
| 2054 |
void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth, |
| 2055 |
int (**pdigest_custom)(EVP_PKEY_CTX *ctx, |
| 2056 |
EVP_MD_CTX *mctx)) |
| 2057 |
{ |
| 2058 |
if (pdigest_custom != NULL) |
| 2059 |
*pdigest_custom = pmeth->digest_custom; |
| 2060 |
} |
| 2061 |
|
| 2062 |
#endif /* FIPS_MODULE */ |