summaryrefslogtreecommitdiffstats
path: root/crypto/evp/asymcipher.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/evp/asymcipher.c')
-rw-r--r--crypto/evp/asymcipher.c70
1 files changed, 36 insertions, 34 deletions
diff --git a/crypto/evp/asymcipher.c b/crypto/evp/asymcipher.c
index 3150bfa94b..41aea6b1d8 100644
--- a/crypto/evp/asymcipher.c
+++ b/crypto/evp/asymcipher.c
@@ -40,55 +40,53 @@ static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
goto legacy;
/*
- * Ensure that the key is provided, either natively, or as a cached export.
- * If not, go legacy
+ * Try to derive the supported asym cipher from |ctx->keymgmt|.
*/
- tmp_keymgmt = ctx->keymgmt;
- provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
- &tmp_keymgmt, ctx->propquery);
- if (provkey == NULL)
- goto legacy;
- if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
+ if (!ossl_assert(ctx->pkey->keymgmt == NULL
+ || ctx->pkey->keymgmt == ctx->keymgmt)) {
+ ERR_clear_last_mark();
+ ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
+ goto err;
+ }
+ supported_ciph
+ = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
+ OSSL_OP_ASYM_CIPHER);
+ if (supported_ciph == NULL) {
ERR_clear_last_mark();
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
goto err;
}
- EVP_KEYMGMT_free(ctx->keymgmt);
- ctx->keymgmt = tmp_keymgmt;
-
- if (ctx->keymgmt->query_operation_name != NULL)
- supported_ciph =
- ctx->keymgmt->query_operation_name(OSSL_OP_ASYM_CIPHER);
-
- /*
- * If we didn't get a supported ciph, assume there is one with the
- * same name as the key type.
- */
- if (supported_ciph == NULL)
- supported_ciph = ctx->keytype;
/*
* Because we cleared out old ops, we shouldn't need to worry about
* checking if cipher is already there.
*/
- cipher =
- EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, ctx->propquery);
+ cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph,
+ ctx->propquery);
- if (cipher == NULL
- || (EVP_KEYMGMT_get0_provider(ctx->keymgmt)
- != EVP_ASYM_CIPHER_get0_provider(cipher))) {
- /*
- * 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().
- */
- EVP_ASYM_CIPHER_free(cipher);
+ if (cipher == NULL)
goto legacy;
- }
/*
- * If we don't have the full support we need with provided methods,
- * let's go see if legacy does.
+ * Ensure that the key is provided, either natively, or as a cached export.
+ * We start by fetching the keymgmt with the same name as |ctx->pkey|,
+ * but from the provider of the asym cipher method, using the same property
+ * query as when fetching the asym cipher method.
+ * With the keymgmt we found (if we did), we try to export |ctx->pkey|
+ * to it (evp_pkey_export_to_provider() is smart enough to only actually
+
+ * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
*/
+ tmp_keymgmt
+ = evp_keymgmt_fetch_from_prov(EVP_ASYM_CIPHER_get0_provider(cipher),
+ EVP_KEYMGMT_get0_name(ctx->keymgmt),
+ ctx->propquery);
+ if (tmp_keymgmt != NULL)
+ provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
+ &tmp_keymgmt, ctx->propquery);
+ if (provkey == NULL)
+ goto legacy;
+
ERR_pop_to_mark();
/* No more legacy from here down to legacy: */
@@ -125,6 +123,7 @@ static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
if (ret <= 0)
goto err;
+ EVP_KEYMGMT_free(tmp_keymgmt);
return 1;
legacy:
@@ -133,6 +132,8 @@ static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
* let's go see if legacy does.
*/
ERR_pop_to_mark();
+ EVP_KEYMGMT_free(tmp_keymgmt);
+ tmp_keymgmt = NULL;
if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
@@ -159,6 +160,7 @@ static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
evp_pkey_ctx_free_old_ops(ctx);
ctx->operation = EVP_PKEY_OP_UNDEFINED;
}
+ EVP_KEYMGMT_free(tmp_keymgmt);
return ret;
}