/*
- * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
#include "internal/cryptlib.h"
#include "internal/nelem.h"
#include "crypto/evp.h"
-#include "crypto/asn1.h"
#include "internal/core.h"
#include "internal/provider.h"
#include "evp_local.h"
*/
static int match_type(const EVP_KEYMGMT *keymgmt1, const EVP_KEYMGMT *keymgmt2)
{
- const OSSL_PROVIDER *prov2 = EVP_KEYMGMT_provider(keymgmt2);
- const char *name2 = evp_first_name(prov2, EVP_KEYMGMT_number(keymgmt2));
+ const char *name2 = EVP_KEYMGMT_get0_name(keymgmt2);
return EVP_KEYMGMT_is_a(keymgmt1, name2);
}
int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg)
{
struct evp_keymgmt_util_try_import_data_st *data = arg;
+ int delete_on_error = 0;
/* Just in time creation of keydata */
- if (data->keydata == NULL
- && (data->keydata = evp_keymgmt_newdata(data->keymgmt)) == NULL) {
- ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
- return 0;
+ if (data->keydata == NULL) {
+ if ((data->keydata = evp_keymgmt_newdata(data->keymgmt)) == NULL) {
+ ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ delete_on_error = 1;
}
/*
if (params[0].key == NULL)
return 1;
- return evp_keymgmt_import(data->keymgmt, data->keydata, data->selection,
- params);
+ if (evp_keymgmt_import(data->keymgmt, data->keydata, data->selection,
+ params))
+ return 1;
+ if (delete_on_error) {
+ evp_keymgmt_freedata(data->keymgmt, data->keydata);
+ data->keydata = NULL;
+ }
+ return 0;
}
int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
OSSL_CALLBACK *export_cb, void *export_cbarg)
{
+ if (pk == NULL || export_cb == NULL)
+ return 0;
return evp_keymgmt_export(pk->keymgmt, pk->keydata, selection,
export_cb, export_cbarg);
}
if (pk->keydata == NULL)
return NULL;
- /* If |keymgmt| matches the "origin" |keymgmt|, no more to do */
- if (pk->keymgmt == keymgmt)
+ /*
+ * If |keymgmt| matches the "origin" |keymgmt|, there is no more to do.
+ * The "origin" is determined by the |keymgmt| pointers being identical
+ * or when the provider and the name ID match. The latter case handles the
+ * situation where the fetch cache is flushed and a "new" key manager is
+ * created.
+ */
+ if (pk->keymgmt == keymgmt
+ || (pk->keymgmt->name_id == keymgmt->name_id
+ && pk->keymgmt->prov == keymgmt->prov))
return pk->keydata;
if (!CRYPTO_THREAD_read_lock(pk->lock))
CRYPTO_THREAD_unlock(pk->lock);
/* If the "origin" |keymgmt| doesn't support exporting, give up */
- /*
- * TODO(3.0) consider an evp_keymgmt_export() return value that indicates
- * that the method is unsupported.
- */
if (pk->keymgmt->export == NULL)
return NULL;
* which does the import for us. If successful, we're done.
*/
if (!evp_keymgmt_util_export(pk, OSSL_KEYMGMT_SELECT_ALL,
- &evp_keymgmt_util_try_import, &import_data)) {
+ &evp_keymgmt_util_try_import, &import_data))
/* If there was an error, bail out */
- evp_keymgmt_freedata(keymgmt, import_data.keydata);
return NULL;
- }
if (!CRYPTO_THREAD_write_lock(pk->lock)) {
evp_keymgmt_freedata(keymgmt, import_data.keydata);
/* Add the new export to the operation cache */
if (!evp_keymgmt_util_cache_keydata(pk, keymgmt, import_data.keydata)) {
+ CRYPTO_THREAD_unlock(pk->lock);
evp_keymgmt_freedata(keymgmt, import_data.keydata);
return NULL;
}
/*
* Cache information about the provider "origin" key.
*
- * This services functions like EVP_PKEY_size, EVP_PKEY_bits, etc
+ * This services functions like EVP_PKEY_get_size, EVP_PKEY_get_bits, etc
*/
if (pk->keydata != NULL) {
int bits = 0;
* but also to determine if we should attempt a cross export
* the other way. There's no point doing it both ways.
*/
- int ok = 1;
+ int ok = 0;
/* Complex case, where the keymgmt differ */
if (keymgmt1 != NULL
OPENSSL_strlcpy(mdname, result, mdname_sz);
return rv;
}
+
+/*
+ * If |keymgmt| has the method function |query_operation_name|, use it to get
+ * the name of a supported operation identity. Otherwise, return the keytype,
+ * assuming that it works as a default operation name.
+ */
+const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
+ int op_id)
+{
+ const char *name = NULL;
+
+ if (keymgmt != NULL) {
+ if (keymgmt->query_operation_name != NULL)
+ name = keymgmt->query_operation_name(op_id);
+ if (name == NULL)
+ name = EVP_KEYMGMT_get0_name(keymgmt);
+ }
+ return name;
+}