Browse Source

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 <nic.tuv@gmail.com>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12853)
master
Richard Levitte 2 years ago
parent
commit
655f73cecf
4 changed files with 27 additions and 13 deletions
  1. +12
    -4
      crypto/evp/keymgmt_lib.c
  2. +7
    -9
      crypto/evp/p_lib.c
  3. +6
    -0
      doc/internal/man3/evp_keymgmt_util_export_to_provider.pod
  4. +2
    -0
      include/crypto/evp.h

+ 12
- 4
crypto/evp/keymgmt_lib.c View File

@ -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;
}


+ 7
- 9
crypto/evp/p_lib.c View File

@ -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);


+ 6
- 0
doc/internal/man3/evp_keymgmt_util_export_to_provider.pod View File

@ -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<evp_keymgmt_export(3)> with the
I<keymgmt> and I<keydata> from I<pk>. This is a convenience function.
evp_keymgmt_util_export_to_provider() exports cached key material
(provider side key material) from the given key I<pk> to a provider
via a B<EVP_KEYMGMT> interface, if this hasn't already been done.


+ 2
- 0
include/crypto/evp.h View File

@ -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);


Loading…
Cancel
Save