#include <openssl/evp.h>
#include <openssl/err.h>
#include "internal/refcount.h"
-#include "internal/evp_int.h"
+#include "crypto/evp.h"
#include "internal/provider.h"
-#include "evp_locl.h"
+#include "internal/numbers.h" /* includes SIZE_MAX */
+#include "evp_local.h"
static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
{
return exchange;
}
-static void *evp_keyexch_from_dispatch(const char *name,
+static void *evp_keyexch_from_dispatch(int name_id,
const OSSL_DISPATCH *fns,
OSSL_PROVIDER *prov)
{
EVP_KEYEXCH *exchange = NULL;
- int fncnt = 0;
+ int fncnt = 0, paramfncnt = 0;
- if ((exchange = evp_keyexch_new(prov)) == NULL
- || (exchange->name = OPENSSL_strdup(name)) == NULL) {
- EVP_KEYEXCH_free(exchange);
- EVPerr(0, ERR_R_MALLOC_FAILURE);
- return NULL;
+ if ((exchange = evp_keyexch_new(prov)) == NULL) {
+ ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+ goto err;
}
+ exchange->name_id = name_id;
+
for (; fns->function_id != 0; fns++) {
switch (fns->function_id) {
case OSSL_FUNC_KEYEXCH_NEWCTX:
break;
exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
break;
- case OSSL_FUNC_KEYEXCH_SET_PARAMS:
- if (exchange->set_params != NULL)
+ case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
+ if (exchange->set_ctx_params != NULL)
break;
- exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
+ exchange->set_ctx_params = OSSL_get_OP_keyexch_set_ctx_params(fns);
+ paramfncnt++;
+ break;
+ case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
+ if (exchange->settable_ctx_params != NULL)
+ break;
+ exchange->settable_ctx_params
+ = OSSL_get_OP_keyexch_settable_ctx_params(fns);
+ paramfncnt++;
break;
}
}
- if (fncnt != 4) {
+ if (fncnt != 4 || (paramfncnt != 0 && paramfncnt != 2)) {
/*
* In order to be a consistent set of functions we must have at least
* a complete set of "exchange" functions: init, derive, newctx,
- * and freectx. The dupctx, set_peer and set_params functions are
- * optional.
+ * and freectx. The set_ctx_params and settable_ctx_params functions are
+ * optional, but if one of them is present then the other one must also
+ * be present. The dupctx and set_peer functions are optional.
*/
- EVP_KEYEXCH_free(exchange);
EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
EVP_R_INVALID_PROVIDER_FUNCTIONS);
- return NULL;
+ goto err;
}
return exchange;
+
+ err:
+ EVP_KEYEXCH_free(exchange);
+ return NULL;
}
void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
if (i > 0)
return;
ossl_provider_free(exchange->prov);
- OPENSSL_free(exchange->name);
CRYPTO_THREAD_lock_free(exchange->lock);
OPENSSL_free(exchange);
}
return 1;
}
+OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
+{
+ return exchange->prov;
+}
+
EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
const char *properties)
{
(void (*)(void *))EVP_KEYEXCH_free);
}
-int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
+int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
{
int ret;
- OSSL_PARAM *param = NULL;
- size_t paramsz = 0;
+ void *provkey = NULL;
+ EVP_KEYEXCH *exchange = NULL;
+ if (ctx == NULL) {
+ EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
+ return -2;
+ }
+
+ evp_pkey_ctx_free_old_ops(ctx);
ctx->operation = EVP_PKEY_OP_DERIVE;
- if (ctx->engine != NULL)
+ if (ctx->engine != NULL || ctx->algorithm == NULL)
goto legacy;
- if (exchange != NULL) {
- if (!EVP_KEYEXCH_up_ref(exchange))
- goto err;
- } else {
- int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
+ /*
+ * Because we cleared out old ops, we shouldn't need to worry about
+ * checking if exchange is already there. Keymgmt is a different
+ * matter, as it isn't tied to a specific EVP_PKEY op.
+ */
+ exchange = EVP_KEYEXCH_fetch(ctx->libctx, ctx->algorithm, ctx->propquery);
+ if (exchange != NULL && ctx->keymgmt == NULL) {
+ int name_id = EVP_KEYEXCH_number(exchange);
+ ctx->keymgmt =
+ evp_keymgmt_fetch_by_number(ctx->libctx, name_id, ctx->propquery);
+ }
+
+ if (ctx->keymgmt == NULL
+ || exchange == NULL
+ || (EVP_KEYMGMT_provider(ctx->keymgmt)
+ != EVP_KEYEXCH_provider(exchange))) {
/*
- * TODO(3.0): Check for legacy handling. Remove this once all all
- * algorithms are moved to providers.
+ * We don't have the full support we need with provided methods,
+ * let's go see if legacy does. Also, we don't need to free
+ * ctx->keymgmt here, as it's not necessarily tied to this
+ * operation. It will be freed by EVP_PKEY_CTX_free().
*/
- if (ctx->pkey != NULL) {
- switch (ctx->pkey->type) {
- case EVP_PKEY_DH:
- break;
- default:
- goto legacy;
- }
- exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
- } else {
- goto legacy;
- }
-
- if (exchange == NULL) {
- EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
- goto err;
- }
+ EVP_KEYEXCH_free(exchange);
+ goto legacy;
}
- if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
- ctx->exchange->freectx(ctx->exchprovctx);
- EVP_KEYEXCH_free(ctx->exchange);
- ctx->exchange = exchange;
+
+ ctx->op.kex.exchange = exchange;
+
if (ctx->pkey != NULL) {
- param = evp_pkey_to_param(ctx->pkey, ¶msz);
- if (param == NULL) {
- EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
+ provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
+ if (provkey == NULL) {
+ EVPerr(0, EVP_R_INITIALIZATION_ERROR);
goto err;
}
}
- ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
- if (ctx->exchprovctx == NULL) {
- OPENSSL_secure_clear_free(param, paramsz);
- EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
+ ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
+ if (ctx->op.kex.exchprovctx == NULL) {
+ /* The provider key can stay in the cache */
+ EVPerr(0, EVP_R_INITIALIZATION_ERROR);
goto err;
}
- ret = exchange->init(ctx->exchprovctx, param);
- /*
- * TODO(3.0): Really we should detect whether to call OPENSSL_free or
- * OPENSSL_secure_clear_free based on the presence of a private key or not.
- * Since we always expect a private key to be present we just call
- * OPENSSL_secure_clear_free for now.
- */
- OPENSSL_secure_clear_free(param, paramsz);
+ ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
return ret ? 1 : 0;
err:
legacy:
if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
- EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
- EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
+ EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
return -2;
}
return ret;
}
-int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
-{
- return EVP_PKEY_derive_init_ex(ctx, NULL);
-}
-
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
{
int ret;
- OSSL_PARAM *param = NULL;
+ void *provkey = NULL;
if (ctx == NULL) {
EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
return -2;
}
- if (ctx->exchprovctx == NULL)
+ if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
goto legacy;
- if (ctx->operation != EVP_PKEY_OP_DERIVE) {
- EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
- EVP_R_OPERATON_NOT_INITIALIZED);
- return -1;
- }
-
- if (ctx->exchange->set_peer == NULL) {
+ if (ctx->op.kex.exchange->set_peer == NULL) {
EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
return -2;
}
- param = evp_pkey_to_param(peer, NULL);
- if (param == NULL) {
+ provkey = evp_keymgmt_export_to_provider(peer, ctx->keymgmt, 0);
+ if (provkey == NULL) {
EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
return 0;
}
- ret = ctx->exchange->set_peer(ctx->exchprovctx, param);
- /*
- * TODO(3.0): Really we should detect whether to call OPENSSL_free or
- * OPENSSL_secure_clear_free based on the presence of a private key or not.
- * Since we always expect a public key to be present we just call
- * OPENSSL_free for now.
- */
- OPENSSL_free(param);
-
- return ret;
+ return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
legacy:
if (ctx->pmeth == NULL
return -2;
}
- if (ctx->operation != EVP_PKEY_OP_DERIVE) {
+ if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
return -1;
}
- if (ctx->exchprovctx == NULL)
+ if (ctx->op.kex.exchprovctx == NULL)
goto legacy;
- ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
+ ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
+ SIZE_MAX);
return ret;
legacy:
M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
return ctx->pmeth->derive(ctx, key, pkeylen);
}
+
+int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
+{
+ return keyexch->name_id;
+}
+
+int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
+{
+ return evp_is_a(keyexch->prov, keyexch->name_id, name);
+}
+
+void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX *libctx,
+ void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
+ void *arg)
+{
+ evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
+ (void (*)(void *, void *))fn, arg,
+ evp_keyexch_from_dispatch,
+ (void (*)(void *))EVP_KEYEXCH_free);
+}
+
+void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
+ void (*fn)(const char *name, void *data),
+ void *data)
+{
+ if (keyexch->prov != NULL)
+ evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
+}