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