Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoder key export fixes #21519

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion crypto/encode_decode/decoder_pkey.c
Expand Up @@ -155,7 +155,11 @@ static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,

import_data.keymgmt = keymgmt;
import_data.keydata = NULL;
import_data.selection = data->selection;
if (data->selection == 0)
/* import/export functions do not tolerate 0 selection */
import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
else
import_data.selection = data->selection;

/*
* No need to check for errors here, the value of
Expand Down
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/decode_der2key.c
Expand Up @@ -317,10 +317,14 @@ static int der2key_export_object(void *vctx,
void *keydata;

if (reference_sz == sizeof(keydata) && export != NULL) {
int selection = ctx->selection;

if (selection == 0)
selection = OSSL_KEYMGMT_SELECT_ALL;
/* The contents of the reference is the address to our object */
keydata = *(void **)reference;

return export(keydata, ctx->selection, export_cb, export_cbarg);
return export(keydata, selection, export_cb, export_cbarg);
}
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/decode_msblob2key.c
Expand Up @@ -209,10 +209,14 @@ msblob2key_export_object(void *vctx,
void *keydata;

if (reference_sz == sizeof(keydata) && export != NULL) {
int selection = ctx->selection;

if (selection == 0)
selection = OSSL_KEYMGMT_SELECT_ALL;
/* The contents of the reference is the address to our object */
keydata = *(void **)reference;

return export(keydata, ctx->selection, export_cb, export_cbarg);
return export(keydata, selection, export_cb, export_cbarg);
}
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion providers/implementations/encode_decode/decode_pvk2key.c
Expand Up @@ -179,10 +179,14 @@ static int pvk2key_export_object(void *vctx,
void *keydata;

if (reference_sz == sizeof(keydata) && export != NULL) {
int selection = ctx->selection;

if (selection == 0)
selection = OSSL_KEYMGMT_SELECT_ALL;
/* The contents of the reference is the address to our object */
keydata = *(void **)reference;

return export(keydata, ctx->selection, export_cb, export_cbarg);
return export(keydata, selection, export_cb, export_cbarg);
}
return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/keymgmt/dh_kmgmt.c
Expand Up @@ -222,6 +222,9 @@ static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
if (!ossl_prov_is_running() || dh == NULL)
return 0;

if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
return 0;

tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/keymgmt/dsa_kmgmt.c
Expand Up @@ -223,6 +223,9 @@ static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
if (!ossl_prov_is_running() || dsa == NULL)
return 0;

if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
return 0;

tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/keymgmt/ecx_kmgmt.c
Expand Up @@ -241,6 +241,9 @@ static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
if (!ossl_prov_is_running() || key == NULL)
return 0;

if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
return 0;

tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
Expand Down
3 changes: 3 additions & 0 deletions providers/implementations/keymgmt/mac_legacy_kmgmt.c
Expand Up @@ -275,6 +275,9 @@ static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
if (!ossl_prov_is_running() || key == NULL)
return 0;

if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
return 0;

tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
Expand Down
16 changes: 14 additions & 2 deletions test/endecode_test.c
Expand Up @@ -162,6 +162,7 @@ static int test_encode_decode(const char *file, const int line,
void *encoded = NULL;
long encoded_len = 0;
EVP_PKEY *pkey2 = NULL;
EVP_PKEY *pkey3 = NULL;
void *encoded2 = NULL;
long encoded2_len = 0;
int ok = 0;
Expand Down Expand Up @@ -189,15 +190,25 @@ static int test_encode_decode(const char *file, const int line,
output_type, output_structure,
(flags & FLAG_DECODE_WITH_TYPE ? type : NULL),
selection, pass))
|| ((output_structure == NULL
|| strcmp(output_structure, "type-specific") != 0)
&& !TEST_true(decode_cb(file, line, (void **)&pkey3, encoded, encoded_len,
output_type, output_structure,
(flags & FLAG_DECODE_WITH_TYPE ? type : NULL),
0, pass)))
|| !TEST_true(encode_cb(file, line, &encoded2, &encoded2_len, pkey2, selection,
output_type, output_structure, pass, pcipher)))
goto end;

if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) {
if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey2), 1))
if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey2), 1)
|| (pkey3 != NULL
&& !TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey3), 1)))
goto end;
} else {
if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1)
|| (pkey3 != NULL
&& !TEST_int_eq(EVP_PKEY_eq(pkey, pkey3), 1)))
goto end;
}

Expand All @@ -222,6 +233,7 @@ static int test_encode_decode(const char *file, const int line,
OPENSSL_free(encoded);
OPENSSL_free(encoded2);
EVP_PKEY_free(pkey2);
EVP_PKEY_free(pkey3);
return ok;
}

Expand Down