From 655f73cecf411737cef9debdfa4c0b8b041656df Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Fri, 11 Sep 2020 08:35:26 +0200 Subject: [PATCH] EVP: Add the internal convenience function evp_keymgmt_util_export() This is purely to allow exporting without having to repeatedly specify the keymgmt and keydata from the EVP_PKEY. Reviewed-by: Nicola Tuveri Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/12853) --- crypto/evp/keymgmt_lib.c | 16 ++++++++++++---- crypto/evp/p_lib.c | 16 +++++++--------- .../man3/evp_keymgmt_util_export_to_provider.pod | 6 ++++++ include/crypto/evp.h | 2 ++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/crypto/evp/keymgmt_lib.c b/crypto/evp/keymgmt_lib.c index 5ef4115f473..763982e58f7 100644 --- a/crypto/evp/keymgmt_lib.c +++ b/crypto/evp/keymgmt_lib.c @@ -77,6 +77,13 @@ EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata) return pkey; } +int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg) +{ + return evp_keymgmt_export(pk->keymgmt, pk->keydata, selection, + export_cb, export_cbarg); +} + void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt) { struct evp_keymgmt_util_try_import_data_st import_data; @@ -139,8 +146,8 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt) * The export function calls the callback (evp_keymgmt_util_try_import), * which does the import for us. If successful, we're done. */ - if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, OSSL_KEYMGMT_SELECT_ALL, - &evp_keymgmt_util_try_import, &import_data)) { + if (!evp_keymgmt_util_export(pk, OSSL_KEYMGMT_SELECT_ALL, + &evp_keymgmt_util_try_import, &import_data)) { /* If there was an error, bail out */ evp_keymgmt_freedata(keymgmt, import_data.keydata); return NULL; @@ -392,8 +399,9 @@ int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection) import_data.keydata = to_keydata; import_data.selection = selection; - if (!evp_keymgmt_export(from->keymgmt, from->keydata, selection, - &evp_keymgmt_util_try_import, &import_data)) { + if (!evp_keymgmt_util_export(from, selection, + &evp_keymgmt_util_try_import, + &import_data)) { evp_keymgmt_freedata(to_keymgmt, alloc_keydata); return 0; } diff --git a/crypto/evp/p_lib.c b/crypto/evp/p_lib.c index 0f5378c4fe6..cb72048f864 100644 --- a/crypto/evp/p_lib.c +++ b/crypto/evp/p_lib.c @@ -527,9 +527,8 @@ int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, raw_key.len = len; raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY; - return evp_keymgmt_export(pkey->keymgmt, pkey->keydata, - OSSL_KEYMGMT_SELECT_PRIVATE_KEY, - get_raw_key_details, &raw_key); + return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, + get_raw_key_details, &raw_key); } if (pkey->ameth == NULL) { @@ -560,9 +559,8 @@ int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, raw_key.len = len; raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY; - return evp_keymgmt_export(pkey->keymgmt, pkey->keydata, - OSSL_KEYMGMT_SELECT_PUBLIC_KEY, - get_raw_key_details, &raw_key); + return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, + get_raw_key_details, &raw_key); } if (pkey->ameth == NULL) { @@ -1115,9 +1113,9 @@ int evp_pkey_get_EC_KEY_curve_nid(const EVP_PKEY *pkey) } else if (EVP_PKEY_is_a(pkey, "EC") || EVP_PKEY_is_a(pkey, "SM2")) { char *curve_name = NULL; - ret = evp_keymgmt_export(pkey->keymgmt, pkey->keydata, - OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, - get_ec_curve_name_cb, &curve_name); + ret = evp_keymgmt_util_export(pkey, + OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, + get_ec_curve_name_cb, &curve_name); if (ret) ret = ec_curve_name2nid(curve_name); OPENSSL_free(curve_name); diff --git a/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod b/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod index 5fdd6259875..bb2ad9ba8eb 100644 --- a/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod +++ b/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod @@ -2,6 +2,7 @@ =head1 NAME +evp_keymgmt_util_export, evp_keymgmt_util_export_to_provider, evp_keymgmt_util_find_operation_cache_index, evp_keymgmt_util_clear_operation_cache, @@ -14,6 +15,8 @@ evp_keymgmt_util_fromdata #include "crypto/evp.h" + int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg); void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); @@ -26,6 +29,9 @@ evp_keymgmt_util_fromdata =head1 DESCRIPTION +evp_keymgmt_util_export() calls L with the +I and I from I. This is a convenience function. + evp_keymgmt_util_export_to_provider() exports cached key material (provider side key material) from the given key I to a provider via a B interface, if this hasn't already been done. diff --git a/include/crypto/evp.h b/include/crypto/evp.h index bdff97f6397..4912760230d 100644 --- a/include/crypto/evp.h +++ b/include/crypto/evp.h @@ -696,6 +696,8 @@ int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt, void *keydata); EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata); +int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg); void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); -- 2.47.3