Latest update.
This commit is contained in:
+3
-3
@@ -657,12 +657,12 @@ int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
|
||||
size_t sz;
|
||||
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
||||
|
||||
if (ctx == NULL || ctx->digest == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
|
||||
if (ctx == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->digest->prov == NULL)
|
||||
if (ctx->digest != NULL && ctx->digest->prov == NULL)
|
||||
goto legacy;
|
||||
|
||||
switch (cmd) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Generated by util/mkerr.pl DO NOT EDIT
|
||||
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2020 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
|
||||
@@ -64,12 +64,16 @@ static const ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
"expecting a poly1305 key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_SIPHASH_KEY),
|
||||
"expecting a siphash key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_FETCH_FAILED), "fetch failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_FINAL_ERROR), "final error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_FIPS_MODE_NOT_SUPPORTED),
|
||||
"fips mode not supported"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_GET_RAW_KEY_FAILED), "get raw key failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ILLEGAL_SCRYPT_PARAMETERS),
|
||||
"illegal scrypt parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS),
|
||||
"inaccessible domain parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INACCESSIBLE_KEY), "inaccessible key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INITIALIZATION_ERROR),
|
||||
"initialization error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INPUT_NOT_INITIALIZED),
|
||||
@@ -87,6 +91,8 @@ static const ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_SALT_LENGTH),
|
||||
"invalid salt length"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_KEYGEN_FAILURE), "keygen failure"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_KEYMGMT_EXPORT_FAILURE),
|
||||
"keymgmt export failure"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_KEY_SETUP_FAILED), "key setup failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_MEMORY_LIMIT_EXCEEDED),
|
||||
"memory limit exceeded"},
|
||||
@@ -102,6 +108,7 @@ static const ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_CIPHER_SET), "no cipher set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_DEFAULT_DIGEST), "no default digest"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_DIGEST_SET), "no digest set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_IMPORT_FUNCTION), "no import function"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_KEYMGMT_AVAILABLE),
|
||||
"no keymgmt available"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_KEYMGMT_PRESENT), "no keymgmt present"},
|
||||
@@ -128,6 +135,7 @@ static const ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_TOO_MANY_RECORDS), "too many records"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_CIPHER), "unknown cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_DIGEST), "unknown digest"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_KEY_TYPE), "unknown key type"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_OPTION), "unknown option"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_PBE_ALGORITHM),
|
||||
"unknown pbe algorithm"},
|
||||
|
||||
+48
-6
@@ -294,9 +294,26 @@ void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
|
||||
int (*up_ref_method)(void *),
|
||||
void (*free_method)(void *))
|
||||
{
|
||||
return inner_evp_generic_fetch(libctx,
|
||||
operation_id, 0, name, properties,
|
||||
new_method, up_ref_method, free_method);
|
||||
void *ret = inner_evp_generic_fetch(libctx,
|
||||
operation_id, 0, name, properties,
|
||||
new_method, up_ref_method, free_method);
|
||||
|
||||
if (ret == NULL) {
|
||||
int code = EVP_R_FETCH_FAILED;
|
||||
|
||||
#ifdef FIPS_MODE
|
||||
ERR_raise(ERR_LIB_EVP, code);
|
||||
#else
|
||||
ERR_raise_data(ERR_LIB_EVP, code,
|
||||
"%s, Algorithm (%s), Properties (%s)",
|
||||
(openssl_ctx_is_default(libctx)
|
||||
? "Default library context"
|
||||
: "Non-default library context"),
|
||||
name = NULL ? "<null>" : name,
|
||||
properties == NULL ? "<null>" : properties);
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -314,9 +331,34 @@ void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
|
||||
int (*up_ref_method)(void *),
|
||||
void (*free_method)(void *))
|
||||
{
|
||||
return inner_evp_generic_fetch(libctx,
|
||||
operation_id, name_id, NULL, properties,
|
||||
new_method, up_ref_method, free_method);
|
||||
void *ret = inner_evp_generic_fetch(libctx,
|
||||
operation_id, name_id, NULL,
|
||||
properties, new_method, up_ref_method,
|
||||
free_method);
|
||||
|
||||
if (ret == NULL) {
|
||||
int code = EVP_R_FETCH_FAILED;
|
||||
|
||||
#ifdef FIPS_MODE
|
||||
ERR_raise(ERR_LIB_EVP, code);
|
||||
#else
|
||||
{
|
||||
OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
|
||||
const char *name = (namemap == NULL)
|
||||
? NULL
|
||||
: ossl_namemap_num2name(namemap, name_id, 0);
|
||||
|
||||
ERR_raise_data(ERR_LIB_EVP, code,
|
||||
"%s, Algorithm (%s), Properties (%s)",
|
||||
(openssl_ctx_is_default(libctx)
|
||||
? "Default library context"
|
||||
: "Non-default library context"),
|
||||
name = NULL ? "<null>" : name,
|
||||
properties == NULL ? "<null>" : properties);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
|
||||
|
||||
@@ -81,6 +81,16 @@ struct evp_keymgmt_st {
|
||||
OSSL_OP_keymgmt_set_params_fn *set_params;
|
||||
OSSL_OP_keymgmt_settable_params_fn *settable_params;
|
||||
|
||||
/* Generation, a complex constructor */
|
||||
OSSL_OP_keymgmt_gen_init_fn *gen_init;
|
||||
OSSL_OP_keymgmt_gen_set_template_fn *gen_set_template;
|
||||
OSSL_OP_keymgmt_gen_set_params_fn *gen_set_params;
|
||||
OSSL_OP_keymgmt_gen_settable_params_fn *gen_settable_params;
|
||||
OSSL_OP_keymgmt_gen_get_params_fn *gen_get_params;
|
||||
OSSL_OP_keymgmt_gen_gettable_params_fn *gen_gettable_params;
|
||||
OSSL_OP_keymgmt_gen_fn *gen;
|
||||
OSSL_OP_keymgmt_gen_cleanup_fn *gen_cleanup;
|
||||
|
||||
/* Key object checking */
|
||||
OSSL_OP_keymgmt_query_operation_name_fn *query_operation_name;
|
||||
OSSL_OP_keymgmt_has_fn *has;
|
||||
@@ -129,9 +139,11 @@ struct evp_signature_st {
|
||||
OSSL_OP_signature_digest_sign_init_fn *digest_sign_init;
|
||||
OSSL_OP_signature_digest_sign_update_fn *digest_sign_update;
|
||||
OSSL_OP_signature_digest_sign_final_fn *digest_sign_final;
|
||||
OSSL_OP_signature_digest_sign_fn *digest_sign;
|
||||
OSSL_OP_signature_digest_verify_init_fn *digest_verify_init;
|
||||
OSSL_OP_signature_digest_verify_update_fn *digest_verify_update;
|
||||
OSSL_OP_signature_digest_verify_final_fn *digest_verify_final;
|
||||
OSSL_OP_signature_digest_verify_fn *digest_verify;
|
||||
OSSL_OP_signature_freectx_fn *freectx;
|
||||
OSSL_OP_signature_dupctx_fn *dupctx;
|
||||
OSSL_OP_signature_get_ctx_params_fn *get_ctx_params;
|
||||
|
||||
@@ -197,7 +197,7 @@ int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
|
||||
*/
|
||||
ERR_set_mark();
|
||||
|
||||
if (ctx->engine != NULL || ctx->keytype == NULL)
|
||||
if (ctx->keymgmt == NULL)
|
||||
goto legacy;
|
||||
|
||||
/*
|
||||
|
||||
+105
-43
@@ -39,13 +39,26 @@ static int try_import(const OSSL_PARAM params[], void *arg)
|
||||
{
|
||||
struct import_data_st *data = arg;
|
||||
|
||||
/*
|
||||
* It's fine if there was no data to transfer, we just end up with an
|
||||
* empty destination key.
|
||||
*/
|
||||
if (params[0].key == NULL)
|
||||
return 1;
|
||||
|
||||
/* Just in time creation of keydata, if needed */
|
||||
if (data->keydata == NULL
|
||||
&& (data->keydata = evp_keymgmt_newdata(data->keymgmt)) == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return evp_keymgmt_import(data->keymgmt, data->keydata, data->selection,
|
||||
params);
|
||||
}
|
||||
|
||||
void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
|
||||
{
|
||||
void *keydata = NULL;
|
||||
struct import_data_st import_data;
|
||||
size_t i = 0;
|
||||
|
||||
@@ -54,7 +67,7 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
|
||||
return NULL;
|
||||
|
||||
/* If we have an unassigned key, give up */
|
||||
if (pk->keymgmt == NULL)
|
||||
if (pk->keydata == NULL)
|
||||
return NULL;
|
||||
|
||||
/* If |keymgmt| matches the "origin" |keymgmt|, no more to do */
|
||||
@@ -91,10 +104,6 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
|
||||
if (!ossl_assert(match_type(pk->keymgmt, keymgmt)))
|
||||
return NULL;
|
||||
|
||||
/* Create space to import data into */
|
||||
if ((keydata = evp_keymgmt_newdata(keymgmt)) == NULL)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* We look at the already cached provider keys, and import from the
|
||||
* first that supports it (i.e. use its export function), and export
|
||||
@@ -102,7 +111,7 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
|
||||
*/
|
||||
|
||||
/* Setup for the export callback */
|
||||
import_data.keydata = keydata;
|
||||
import_data.keydata = NULL; /* try_import will create it */
|
||||
import_data.keymgmt = keymgmt;
|
||||
import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
|
||||
|
||||
@@ -113,17 +122,17 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
|
||||
if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, OSSL_KEYMGMT_SELECT_ALL,
|
||||
&try_import, &import_data)) {
|
||||
/* If there was an error, bail out */
|
||||
evp_keymgmt_freedata(keymgmt, keydata);
|
||||
evp_keymgmt_freedata(keymgmt, import_data.keydata);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Add the new export to the operation cache */
|
||||
if (!evp_keymgmt_util_cache_keydata(pk, i, keymgmt, keydata)) {
|
||||
evp_keymgmt_freedata(keymgmt, keydata);
|
||||
if (!evp_keymgmt_util_cache_keydata(pk, i, keymgmt, import_data.keydata)) {
|
||||
evp_keymgmt_freedata(keymgmt, import_data.keydata);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return keydata;
|
||||
return import_data.keydata;
|
||||
}
|
||||
|
||||
void evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk)
|
||||
@@ -175,7 +184,7 @@ void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk)
|
||||
*
|
||||
* This services functions like EVP_PKEY_size, EVP_PKEY_bits, etc
|
||||
*/
|
||||
if (pk->keymgmt != NULL) {
|
||||
if (pk->keydata != NULL) {
|
||||
int bits = 0;
|
||||
int security_bits = 0;
|
||||
int size = 0;
|
||||
@@ -197,17 +206,15 @@ void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk)
|
||||
void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
|
||||
int selection, const OSSL_PARAM params[])
|
||||
{
|
||||
void *keydata = evp_keymgmt_newdata(keymgmt);
|
||||
void *keydata = NULL;
|
||||
|
||||
if ((keydata = evp_keymgmt_newdata(keymgmt)) == NULL
|
||||
|| !evp_keymgmt_import(keymgmt, keydata, selection, params)
|
||||
|| !EVP_PKEY_set_type_by_keymgmt(target, keymgmt)) {
|
||||
evp_keymgmt_freedata(keymgmt, keydata);
|
||||
keydata = NULL;
|
||||
}
|
||||
if (keydata != NULL) {
|
||||
if (!evp_keymgmt_import(keymgmt, keydata, selection, params)
|
||||
|| !EVP_KEYMGMT_up_ref(keymgmt)) {
|
||||
evp_keymgmt_freedata(keymgmt, keydata);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
evp_keymgmt_util_clear_operation_cache(target);
|
||||
target->keymgmt = keymgmt;
|
||||
target->keydata = keydata;
|
||||
evp_keymgmt_util_cache_keyinfo(target);
|
||||
}
|
||||
@@ -254,7 +261,17 @@ int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
|
||||
keydata2 = pk2->keydata;
|
||||
|
||||
if (keymgmt1 != keymgmt2) {
|
||||
void *tmp_keydata = NULL;
|
||||
/*
|
||||
* The condition for a successful cross export is that the
|
||||
* keydata to be exported is NULL (typed, but otherwise empty
|
||||
* EVP_PKEY), or that it was possible to export it with
|
||||
* evp_keymgmt_util_export_to_provider().
|
||||
*
|
||||
* We use |ok| to determine if it's ok to cross export one way,
|
||||
* 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;
|
||||
|
||||
/* Complex case, where the keymgmt differ */
|
||||
if (keymgmt1 != NULL
|
||||
@@ -270,17 +287,35 @@ int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
|
||||
*/
|
||||
if (keymgmt2 != NULL
|
||||
&& keymgmt2->match != NULL) {
|
||||
tmp_keydata = evp_keymgmt_util_export_to_provider(pk1, keymgmt2);
|
||||
if (tmp_keydata != NULL) {
|
||||
void *tmp_keydata = NULL;
|
||||
|
||||
ok = 1;
|
||||
if (keydata1 != NULL) {
|
||||
tmp_keydata =
|
||||
evp_keymgmt_util_export_to_provider(pk1, keymgmt2);
|
||||
ok = (tmp_keydata != NULL);
|
||||
}
|
||||
if (ok) {
|
||||
keymgmt1 = keymgmt2;
|
||||
keydata1 = tmp_keydata;
|
||||
}
|
||||
}
|
||||
if (tmp_keydata == NULL
|
||||
/*
|
||||
* If we've successfully cross exported one way, there's no point
|
||||
* doing it the other way, hence the |!ok| check.
|
||||
*/
|
||||
if (!ok
|
||||
&& keymgmt1 != NULL
|
||||
&& keymgmt1->match != NULL) {
|
||||
tmp_keydata = evp_keymgmt_util_export_to_provider(pk2, keymgmt1);
|
||||
if (tmp_keydata != NULL) {
|
||||
void *tmp_keydata = NULL;
|
||||
|
||||
ok = 1;
|
||||
if (keydata2 != NULL) {
|
||||
tmp_keydata =
|
||||
evp_keymgmt_util_export_to_provider(pk2, keymgmt1);
|
||||
ok = (tmp_keydata != NULL);
|
||||
}
|
||||
if (ok) {
|
||||
keymgmt2 = keymgmt1;
|
||||
keydata2 = tmp_keydata;
|
||||
}
|
||||
@@ -291,6 +326,13 @@ int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
|
||||
if (keymgmt1 != keymgmt2)
|
||||
return -2;
|
||||
|
||||
/* If both keydata are NULL, then they're the same key */
|
||||
if (keydata1 == NULL && keydata2 == NULL)
|
||||
return 1;
|
||||
/* If only one of the keydata is NULL, then they're different keys */
|
||||
if (keydata1 == NULL || keydata2 == NULL)
|
||||
return 0;
|
||||
/* If both keydata are non-NULL, we let the backend decide */
|
||||
return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
|
||||
}
|
||||
|
||||
@@ -301,23 +343,21 @@ int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection)
|
||||
void *to_keydata = to->keydata, *alloc_keydata = NULL;
|
||||
|
||||
/* An unassigned key can't be copied */
|
||||
if (from == NULL || from->keymgmt == NULL)
|
||||
if (from == NULL || from->keydata == NULL)
|
||||
return 0;
|
||||
|
||||
/* If |from| doesn't support copying, we fail */
|
||||
if (from->keymgmt->copy == NULL)
|
||||
return 0;
|
||||
|
||||
/* If |to| doesn't have a provider side "origin" yet, create one */
|
||||
if (to_keymgmt == NULL) {
|
||||
to_keydata = alloc_keydata = evp_keymgmt_newdata(from->keymgmt);
|
||||
if (to_keydata == NULL)
|
||||
if (to_keymgmt == from->keymgmt && to_keymgmt->copy != NULL) {
|
||||
/* Make sure there's somewhere to copy to */
|
||||
if (to_keydata == NULL
|
||||
&& (to_keydata = evp_keymgmt_newdata(to_keymgmt)) == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
to_keymgmt = from->keymgmt;
|
||||
}
|
||||
}
|
||||
|
||||
if (to_keymgmt == from->keymgmt) {
|
||||
/* |to| and |from| have the same keymgmt, just copy and be done */
|
||||
/*
|
||||
* |to| and |from| have the same keymgmt, and the copy function is
|
||||
* implemented, so just copy and be done
|
||||
*/
|
||||
if (!evp_keymgmt_copy(to_keymgmt, to_keydata, from->keydata,
|
||||
selection))
|
||||
return 0;
|
||||
@@ -333,20 +373,42 @@ int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection)
|
||||
evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* In this case to_keydata was previously unallocated, try_import()
|
||||
* may have created it for us.
|
||||
*/
|
||||
to_keydata = import_data.keydata;
|
||||
} else {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (to->keymgmt == NULL
|
||||
&& !EVP_KEYMGMT_up_ref(to_keymgmt)) {
|
||||
&& !EVP_PKEY_set_type_by_keymgmt(to, to_keymgmt)) {
|
||||
evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
|
||||
return 0;
|
||||
}
|
||||
evp_keymgmt_util_clear_operation_cache(to);
|
||||
to->keymgmt = to_keymgmt;
|
||||
to->keydata = to_keydata;
|
||||
evp_keymgmt_util_cache_keyinfo(to);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
|
||||
void *genctx, OSSL_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
void *keydata = NULL;
|
||||
|
||||
if ((keydata = evp_keymgmt_gen(keymgmt, genctx, cb, cbarg)) == NULL
|
||||
|| !EVP_PKEY_set_type_by_keymgmt(target, keymgmt)) {
|
||||
evp_keymgmt_freedata(keymgmt, keydata);
|
||||
keydata = NULL;
|
||||
}
|
||||
if (keydata != NULL) {
|
||||
target->keydata = keydata;
|
||||
evp_keymgmt_util_cache_keyinfo(target);
|
||||
}
|
||||
|
||||
return keydata;
|
||||
}
|
||||
+120
-3
@@ -38,7 +38,9 @@ static void *keymgmt_from_dispatch(int name_id,
|
||||
OSSL_PROVIDER *prov)
|
||||
{
|
||||
EVP_KEYMGMT *keymgmt = NULL;
|
||||
int setparamfncnt = 0, getparamfncnt = 0, importfncnt = 0, exportfncnt = 0;
|
||||
int setparamfncnt = 0, getparamfncnt = 0;
|
||||
int setgenparamfncnt = 0, getgenparamfncnt = 0;
|
||||
int importfncnt = 0, exportfncnt = 0;
|
||||
|
||||
if ((keymgmt = keymgmt_new()) == NULL) {
|
||||
EVP_KEYMGMT_free(keymgmt);
|
||||
@@ -52,6 +54,51 @@ static void *keymgmt_from_dispatch(int name_id,
|
||||
if (keymgmt->new == NULL)
|
||||
keymgmt->new = OSSL_get_OP_keymgmt_new(fns);
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_INIT:
|
||||
if (keymgmt->gen_init == NULL)
|
||||
keymgmt->gen_init = OSSL_get_OP_keymgmt_gen_init(fns);
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE:
|
||||
if (keymgmt->gen_set_template == NULL)
|
||||
keymgmt->gen_set_template =
|
||||
OSSL_get_OP_keymgmt_gen_set_template(fns);
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS:
|
||||
if (keymgmt->gen_set_params == NULL) {
|
||||
setgenparamfncnt++;
|
||||
keymgmt->gen_set_params =
|
||||
OSSL_get_OP_keymgmt_gen_set_params(fns);
|
||||
}
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS:
|
||||
if (keymgmt->gen_settable_params == NULL) {
|
||||
setgenparamfncnt++;
|
||||
keymgmt->gen_settable_params =
|
||||
OSSL_get_OP_keymgmt_gen_settable_params(fns);
|
||||
}
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS:
|
||||
if (keymgmt->gen_get_params == NULL) {
|
||||
getgenparamfncnt++;
|
||||
keymgmt->gen_get_params =
|
||||
OSSL_get_OP_keymgmt_gen_get_params(fns);
|
||||
}
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS:
|
||||
if (keymgmt->gen_gettable_params == NULL) {
|
||||
getgenparamfncnt++;
|
||||
keymgmt->gen_gettable_params =
|
||||
OSSL_get_OP_keymgmt_gen_gettable_params(fns);
|
||||
}
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN:
|
||||
if (keymgmt->gen == NULL)
|
||||
keymgmt->gen = OSSL_get_OP_keymgmt_gen(fns);
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_GEN_CLEANUP:
|
||||
if (keymgmt->gen_cleanup == NULL)
|
||||
keymgmt->gen_cleanup = OSSL_get_OP_keymgmt_gen_cleanup(fns);
|
||||
break;
|
||||
case OSSL_FUNC_KEYMGMT_FREE:
|
||||
if (keymgmt->free == NULL)
|
||||
keymgmt->free = OSSL_get_OP_keymgmt_free(fns);
|
||||
@@ -134,12 +181,17 @@ static void *keymgmt_from_dispatch(int name_id,
|
||||
* export if you can't import or export.
|
||||
*/
|
||||
if (keymgmt->free == NULL
|
||||
|| keymgmt->new == NULL
|
||||
|| (keymgmt->new == NULL && keymgmt->gen == NULL)
|
||||
|| keymgmt->has == NULL
|
||||
|| (getparamfncnt != 0 && getparamfncnt != 2)
|
||||
|| (setparamfncnt != 0 && setparamfncnt != 2)
|
||||
|| (setgenparamfncnt != 0 && setgenparamfncnt != 2)
|
||||
|| (getgenparamfncnt != 0 && getgenparamfncnt != 2)
|
||||
|| (importfncnt != 0 && importfncnt != 2)
|
||||
|| (exportfncnt != 0 && exportfncnt != 2)) {
|
||||
|| (exportfncnt != 0 && exportfncnt != 2)
|
||||
|| (keymgmt->gen != NULL
|
||||
&& (keymgmt->gen_init == NULL
|
||||
|| keymgmt->gen_cleanup == NULL))) {
|
||||
EVP_KEYMGMT_free(keymgmt);
|
||||
EVPerr(0, EVP_R_INVALID_PROVIDER_FUNCTIONS);
|
||||
return NULL;
|
||||
@@ -249,6 +301,71 @@ void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
|
||||
keymgmt->free(keydata);
|
||||
}
|
||||
|
||||
void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection)
|
||||
{
|
||||
void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
|
||||
|
||||
if (keymgmt->gen_init == NULL)
|
||||
return NULL;
|
||||
return keymgmt->gen_init(provctx, selection);
|
||||
}
|
||||
|
||||
int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
|
||||
void *template)
|
||||
{
|
||||
if (keymgmt->gen_set_template == NULL)
|
||||
return 0;
|
||||
return keymgmt->gen_set_template(genctx, template);
|
||||
}
|
||||
|
||||
int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
|
||||
const OSSL_PARAM params[])
|
||||
{
|
||||
if (keymgmt->gen_set_params == NULL)
|
||||
return 0;
|
||||
return keymgmt->gen_set_params(genctx, params);
|
||||
}
|
||||
|
||||
const OSSL_PARAM *evp_keymgmt_gen_settable_params(const EVP_KEYMGMT *keymgmt)
|
||||
{
|
||||
void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
|
||||
|
||||
if (keymgmt->gen_settable_params == NULL)
|
||||
return NULL;
|
||||
return keymgmt->gen_settable_params(provctx);
|
||||
}
|
||||
|
||||
int evp_keymgmt_gen_get_params(const EVP_KEYMGMT *keymgmt, void *genctx,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
if (keymgmt->gen_get_params == NULL)
|
||||
return 0;
|
||||
return keymgmt->gen_get_params(genctx, params);
|
||||
}
|
||||
|
||||
const OSSL_PARAM *evp_keymgmt_gen_gettable_params(const EVP_KEYMGMT *keymgmt)
|
||||
{
|
||||
void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
|
||||
|
||||
if (keymgmt->gen_gettable_params == NULL)
|
||||
return NULL;
|
||||
return keymgmt->gen_gettable_params(provctx);
|
||||
}
|
||||
|
||||
void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
|
||||
OSSL_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
if (keymgmt->gen == NULL)
|
||||
return NULL;
|
||||
return keymgmt->gen(genctx, cb, cbarg);
|
||||
}
|
||||
|
||||
void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
|
||||
{
|
||||
if (keymgmt->gen != NULL)
|
||||
keymgmt->gen_cleanup(genctx);
|
||||
}
|
||||
|
||||
int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
|
||||
+101
-17
@@ -24,10 +24,22 @@ static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we get the "NULL" md then the name comes back as "UNDEF". We want to use
|
||||
* NULL for this.
|
||||
*/
|
||||
static const char *canon_mdname(const char *mdname)
|
||||
{
|
||||
if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
|
||||
return NULL;
|
||||
|
||||
return mdname;
|
||||
}
|
||||
|
||||
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, const char *mdname,
|
||||
const char *props, ENGINE *e, EVP_PKEY *pkey,
|
||||
int ver)
|
||||
OPENSSL_CTX *libctx, int ver)
|
||||
{
|
||||
EVP_PKEY_CTX *locpctx = NULL;
|
||||
EVP_SIGNATURE *signature = NULL;
|
||||
@@ -47,8 +59,12 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
ctx->provctx = NULL;
|
||||
}
|
||||
|
||||
if (ctx->pctx == NULL)
|
||||
ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
|
||||
if (ctx->pctx == NULL) {
|
||||
if (libctx != NULL)
|
||||
ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
|
||||
else
|
||||
ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
|
||||
}
|
||||
if (ctx->pctx == NULL)
|
||||
return 0;
|
||||
|
||||
@@ -61,7 +77,7 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
*/
|
||||
ERR_set_mark();
|
||||
|
||||
if (locpctx->keytype == NULL)
|
||||
if (locpctx->engine != NULL || locpctx->keytype == NULL)
|
||||
goto legacy;
|
||||
|
||||
/*
|
||||
@@ -134,12 +150,12 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
if (type != NULL) {
|
||||
ctx->reqdigest = type;
|
||||
if (mdname == NULL)
|
||||
mdname = EVP_MD_name(type);
|
||||
mdname = canon_mdname(EVP_MD_name(type));
|
||||
} else {
|
||||
if (mdname == NULL
|
||||
&& EVP_PKEY_get_default_digest_name(locpctx->pkey, locmdname,
|
||||
sizeof(locmdname)))
|
||||
mdname = locmdname;
|
||||
mdname = canon_mdname(locmdname);
|
||||
|
||||
if (mdname != NULL) {
|
||||
/*
|
||||
@@ -184,6 +200,9 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
*/
|
||||
ERR_pop_to_mark();
|
||||
|
||||
if (type == NULL && mdname != NULL)
|
||||
type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
|
||||
|
||||
if (ctx->pctx->pmeth == NULL) {
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return 0;
|
||||
@@ -238,35 +257,38 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
* This indicates the current algorithm requires
|
||||
* special treatment before hashing the tbs-message.
|
||||
*/
|
||||
ctx->pctx->flag_call_digest_custom = 0;
|
||||
if (ctx->pctx->pmeth->digest_custom != NULL)
|
||||
return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
|
||||
ctx->pctx->flag_call_digest_custom = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const char *mdname, const char *props, EVP_PKEY *pkey)
|
||||
const char *mdname, const char *props, EVP_PKEY *pkey,
|
||||
OPENSSL_CTX *libctx)
|
||||
{
|
||||
return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, 0);
|
||||
return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, libctx,
|
||||
0);
|
||||
}
|
||||
|
||||
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
|
||||
{
|
||||
return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, 0);
|
||||
return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 0);
|
||||
}
|
||||
|
||||
int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const char *mdname, const char *props,
|
||||
EVP_PKEY *pkey)
|
||||
EVP_PKEY *pkey, OPENSSL_CTX *libctx)
|
||||
{
|
||||
return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, 1);
|
||||
return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, libctx, 1);
|
||||
}
|
||||
|
||||
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
|
||||
{
|
||||
return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, 1);
|
||||
return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 1);
|
||||
}
|
||||
#endif /* FIPS_MDOE */
|
||||
|
||||
@@ -280,10 +302,21 @@ int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
|
||||
|| pctx->op.sig.signature == NULL)
|
||||
goto legacy;
|
||||
|
||||
if (pctx->op.sig.signature->digest_sign_update == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
|
||||
data, dsize);
|
||||
|
||||
legacy:
|
||||
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
||||
if (pctx->flag_call_digest_custom
|
||||
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
||||
return 0;
|
||||
pctx->flag_call_digest_custom = 0;
|
||||
|
||||
return EVP_DigestUpdate(ctx, data, dsize);
|
||||
}
|
||||
|
||||
@@ -297,10 +330,21 @@ int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
|
||||
|| pctx->op.sig.signature == NULL)
|
||||
goto legacy;
|
||||
|
||||
if (pctx->op.sig.signature->digest_verify_update == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
|
||||
data, dsize);
|
||||
|
||||
legacy:
|
||||
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
||||
if (pctx->flag_call_digest_custom
|
||||
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
||||
return 0;
|
||||
pctx->flag_call_digest_custom = 0;
|
||||
|
||||
return EVP_DigestUpdate(ctx, data, dsize);
|
||||
}
|
||||
|
||||
@@ -326,6 +370,12 @@ int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
||||
if (pctx->flag_call_digest_custom
|
||||
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
||||
return 0;
|
||||
pctx->flag_call_digest_custom = 0;
|
||||
|
||||
if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
|
||||
if (sigret == NULL)
|
||||
return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
|
||||
@@ -391,8 +441,22 @@ int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
|
||||
const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
|
||||
return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
|
||||
EVP_PKEY_CTX *pctx = ctx->pctx;
|
||||
|
||||
if (pctx != NULL
|
||||
&& pctx->operation == EVP_PKEY_OP_SIGNCTX
|
||||
&& pctx->op.sig.sigprovctx != NULL
|
||||
&& pctx->op.sig.signature != NULL) {
|
||||
if (pctx->op.sig.signature->digest_sign != NULL)
|
||||
return pctx->op.sig.signature->digest_sign(pctx->op.sig.sigprovctx,
|
||||
sigret, siglen, SIZE_MAX,
|
||||
tbs, tbslen);
|
||||
} else {
|
||||
/* legacy */
|
||||
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
|
||||
return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
|
||||
}
|
||||
|
||||
if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
|
||||
return 0;
|
||||
return EVP_DigestSignFinal(ctx, sigret, siglen);
|
||||
@@ -422,6 +486,12 @@ int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
||||
if (pctx->flag_call_digest_custom
|
||||
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
||||
return 0;
|
||||
pctx->flag_call_digest_custom = 0;
|
||||
|
||||
if (pctx->pmeth->verifyctx != NULL)
|
||||
vctx = 1;
|
||||
else
|
||||
@@ -454,8 +524,22 @@ int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
|
||||
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
|
||||
size_t siglen, const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
|
||||
return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
|
||||
EVP_PKEY_CTX *pctx = ctx->pctx;
|
||||
|
||||
if (pctx != NULL
|
||||
&& pctx->operation == EVP_PKEY_OP_VERIFYCTX
|
||||
&& pctx->op.sig.sigprovctx != NULL
|
||||
&& pctx->op.sig.signature != NULL) {
|
||||
if (pctx->op.sig.signature->digest_verify != NULL)
|
||||
return pctx->op.sig.signature->digest_verify(pctx->op.sig.sigprovctx,
|
||||
sigret, siglen,
|
||||
tbs, tbslen);
|
||||
} else {
|
||||
/* legacy */
|
||||
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
|
||||
return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
|
||||
}
|
||||
|
||||
if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
|
||||
return -1;
|
||||
return EVP_DigestVerifyFinal(ctx, sigret, siglen);
|
||||
|
||||
+666
-203
@@ -24,6 +24,7 @@
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/cmac.h>
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/params.h>
|
||||
@@ -32,13 +33,24 @@
|
||||
|
||||
#include "crypto/asn1.h"
|
||||
#include "crypto/evp.h"
|
||||
#include "internal/evp.h"
|
||||
#include "internal/provider.h"
|
||||
#include "evp_local.h"
|
||||
|
||||
#include "crypto/ec.h"
|
||||
|
||||
/* TODO remove this when the EVP_PKEY_is_a() #legacy support hack is removed */
|
||||
#include "e_os.h" /* strcasecmp on Windows */
|
||||
|
||||
static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
|
||||
int len, EVP_KEYMGMT *keymgmt);
|
||||
static void evp_pkey_free_it(EVP_PKEY *key);
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
|
||||
/* The type of parameters selected in key parameter functions */
|
||||
# define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
|
||||
|
||||
int EVP_PKEY_bits(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (pkey != NULL) {
|
||||
@@ -84,6 +96,16 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
|
||||
{
|
||||
return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
|
||||
}
|
||||
|
||||
void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
|
||||
{
|
||||
return CRYPTO_get_ex_data(&key->ex_data, idx);
|
||||
}
|
||||
|
||||
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
||||
{
|
||||
/*
|
||||
@@ -92,16 +114,35 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
||||
*/
|
||||
|
||||
/*
|
||||
* Only check that type match this early when both keys are legacy.
|
||||
* If either of them is provided, we let evp_keymgmt_util_copy()
|
||||
* do this check, after having exported either of them that isn't
|
||||
* provided.
|
||||
* If |to| is a legacy key and |from| isn't, we must downgrade |from|.
|
||||
* If that fails, this function fails.
|
||||
*/
|
||||
if (to->keymgmt == NULL && from->keymgmt == NULL) {
|
||||
if (to->type == EVP_PKEY_NONE) {
|
||||
if (to->type != EVP_PKEY_NONE && from->keymgmt != NULL)
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)from))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Make sure |to| is typed. Content is less important at this early
|
||||
* stage.
|
||||
*
|
||||
* 1. If |to| is untyped, assign |from|'s key type to it.
|
||||
* 2. If |to| contains a legacy key, compare its |type| to |from|'s.
|
||||
* (|from| was already downgraded above)
|
||||
*
|
||||
* If |to| is a provided key, there's nothing more to do here, functions
|
||||
* like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
|
||||
* further down help us find out if they are the same or not.
|
||||
*/
|
||||
if (to->type == EVP_PKEY_NONE && to->keymgmt == NULL) {
|
||||
if (from->type != EVP_PKEY_NONE) {
|
||||
if (EVP_PKEY_set_type(to, from->type) == 0)
|
||||
return 0;
|
||||
} else if (to->type != from->type) {
|
||||
} else {
|
||||
if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
|
||||
return 0;
|
||||
}
|
||||
} else if (to->type != EVP_PKEY_NONE) {
|
||||
if (to->type != from->type) {
|
||||
EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
|
||||
goto err;
|
||||
}
|
||||
@@ -119,34 +160,9 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* If |from| is provided, we upgrade |to| to be provided as well.
|
||||
* This drops the legacy key from |to|.
|
||||
* evp_pkey_upgrade_to_provider() checks if |to| is already provided,
|
||||
* we don't need to do that here.
|
||||
*
|
||||
* TODO(3.0) We should investigate if that's too aggressive and make
|
||||
* this scenario unsupported instead.
|
||||
*/
|
||||
if (from->keymgmt != NULL) {
|
||||
EVP_KEYMGMT *tmp_keymgmt = from->keymgmt;
|
||||
|
||||
/*
|
||||
* The returned pointer is known to be cached, so we don't have to
|
||||
* save it. However, if it's NULL, something went wrong and we can't
|
||||
* copy.
|
||||
*/
|
||||
if (evp_pkey_upgrade_to_provider(to, NULL,
|
||||
&tmp_keymgmt, NULL) == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* For purely provided keys, we just call the keymgmt utility */
|
||||
if (to->keymgmt != NULL && from->keymgmt != NULL)
|
||||
return evp_keymgmt_util_copy(to, (EVP_PKEY *)from,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
return evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
|
||||
|
||||
/*
|
||||
* If |to| is provided, we know that |from| is legacy at this point.
|
||||
@@ -159,12 +175,16 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
||||
evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
|
||||
NULL);
|
||||
|
||||
/*
|
||||
* If we get a NULL, it could be an internal error, or it could be
|
||||
* that there's a key mismatch. We're pretending the latter...
|
||||
*/
|
||||
if (from_keydata == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
|
||||
return 0;
|
||||
}
|
||||
return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
SELECT_PARAMETERS);
|
||||
}
|
||||
|
||||
/* Both keys are legacy */
|
||||
@@ -178,8 +198,7 @@ int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (pkey != NULL) {
|
||||
if (pkey->keymgmt != NULL)
|
||||
return !evp_keymgmt_util_has((EVP_PKEY *)pkey,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
|
||||
else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
|
||||
return pkey->ameth->param_missing(pkey);
|
||||
}
|
||||
@@ -206,20 +225,25 @@ static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
|
||||
return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
|
||||
|
||||
/*
|
||||
* Here, we know that we have a mixture of legacy and provided keys.
|
||||
* Try cross export and compare the resulting key data.
|
||||
* At this point, one of them is provided, the other not. This allows
|
||||
* us to compare types using legacy NIDs.
|
||||
*/
|
||||
if ((a->type != EVP_PKEY_NONE
|
||||
&& !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
|
||||
|| (b->type != EVP_PKEY_NONE
|
||||
&& !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type))))
|
||||
return -1; /* not the same key type */
|
||||
|
||||
/*
|
||||
* We've determined that they both are the same keytype, so the next
|
||||
* step is to do a bit of cross export to ensure we have keydata for
|
||||
* both keys in the same keymgmt.
|
||||
*/
|
||||
keymgmt1 = a->keymgmt;
|
||||
keydata1 = a->keydata;
|
||||
keymgmt2 = b->keymgmt;
|
||||
keydata2 = b->keydata;
|
||||
|
||||
if ((keymgmt1 == NULL
|
||||
&& !EVP_KEYMGMT_is_a(keymgmt2, OBJ_nid2sn(a->type)))
|
||||
|| (keymgmt2 == NULL
|
||||
&& !EVP_KEYMGMT_is_a(keymgmt1, OBJ_nid2sn(b->type))))
|
||||
return -1; /* not the same key type */
|
||||
|
||||
if (keymgmt2 != NULL && keymgmt2->match != NULL) {
|
||||
tmp_keydata =
|
||||
evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
|
||||
@@ -252,7 +276,7 @@ int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
|
||||
*/
|
||||
|
||||
if (a->keymgmt != NULL || b->keymgmt != NULL)
|
||||
return evp_pkey_cmp_any(a, b, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
|
||||
|
||||
/* All legacy keys */
|
||||
if (a->type != b->type)
|
||||
@@ -270,9 +294,8 @@ int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
||||
*/
|
||||
|
||||
if (a->keymgmt != NULL || b->keymgmt != NULL)
|
||||
return evp_pkey_cmp_any(a, b,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
|
||||
| OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
|
||||
return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
|
||||
| OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
|
||||
|
||||
/* All legacy keys */
|
||||
if (a->type != b->type)
|
||||
@@ -294,57 +317,6 @@ int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
|
||||
* is NULL just return 1 or 0 if the algorithm exists.
|
||||
*/
|
||||
|
||||
static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
|
||||
int len)
|
||||
{
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
ENGINE **eptr = (e == NULL) ? &e : NULL;
|
||||
|
||||
if (pkey) {
|
||||
if (pkey->pkey.ptr)
|
||||
evp_pkey_free_it(pkey);
|
||||
/*
|
||||
* If key type matches and a method exists then this lookup has
|
||||
* succeeded once so just indicate success.
|
||||
*/
|
||||
if ((type == pkey->save_type) && pkey->ameth)
|
||||
return 1;
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
/* If we have ENGINEs release them */
|
||||
ENGINE_finish(pkey->engine);
|
||||
pkey->engine = NULL;
|
||||
ENGINE_finish(pkey->pmeth_engine);
|
||||
pkey->pmeth_engine = NULL;
|
||||
# endif
|
||||
}
|
||||
if (str)
|
||||
ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
|
||||
else
|
||||
ameth = EVP_PKEY_asn1_find(eptr, type);
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
if (pkey == NULL && eptr != NULL)
|
||||
ENGINE_finish(e);
|
||||
# endif
|
||||
if (ameth == NULL) {
|
||||
EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
|
||||
return 0;
|
||||
}
|
||||
if (pkey) {
|
||||
pkey->ameth = ameth;
|
||||
pkey->engine = e;
|
||||
|
||||
pkey->type = pkey->ameth->pkey_id;
|
||||
pkey->save_type = type;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
|
||||
const unsigned char *priv,
|
||||
size_t len)
|
||||
@@ -352,7 +324,7 @@ EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
|
||||
EVP_PKEY *ret = EVP_PKEY_new();
|
||||
|
||||
if (ret == NULL
|
||||
|| !pkey_set_type(ret, e, type, NULL, -1)) {
|
||||
|| !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
|
||||
/* EVPerr already called */
|
||||
goto err;
|
||||
}
|
||||
@@ -382,7 +354,7 @@ EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
|
||||
EVP_PKEY *ret = EVP_PKEY_new();
|
||||
|
||||
if (ret == NULL
|
||||
|| !pkey_set_type(ret, e, type, NULL, -1)) {
|
||||
|| !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
|
||||
/* EVPerr already called */
|
||||
goto err;
|
||||
}
|
||||
@@ -408,6 +380,7 @@ EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
|
||||
int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
|
||||
size_t *len)
|
||||
{
|
||||
/* TODO(3.0) Do we need to do anything about provider side keys? */
|
||||
if (pkey->ameth->get_priv_key == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
@@ -425,6 +398,7 @@ int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
|
||||
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
|
||||
size_t *len)
|
||||
{
|
||||
/* TODO(3.0) Do we need to do anything about provider side keys? */
|
||||
if (pkey->ameth->get_pub_key == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
@@ -457,8 +431,8 @@ EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
|
||||
size_t paramsn = 0;
|
||||
|
||||
if (ret == NULL
|
||||
|| cmctx == NULL
|
||||
|| !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
|
||||
|| cmctx == NULL
|
||||
|| !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1, NULL)) {
|
||||
/* EVPerr already called */
|
||||
goto err;
|
||||
}
|
||||
@@ -499,12 +473,12 @@ EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
|
||||
|
||||
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
|
||||
{
|
||||
return pkey_set_type(pkey, NULL, type, NULL, -1);
|
||||
return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
|
||||
}
|
||||
|
||||
int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
|
||||
{
|
||||
return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
|
||||
return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
|
||||
}
|
||||
|
||||
int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
|
||||
@@ -573,6 +547,10 @@ int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
|
||||
|
||||
void *EVP_PKEY_get0(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.ptr;
|
||||
}
|
||||
|
||||
@@ -628,6 +606,10 @@ int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
|
||||
|
||||
RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
|
||||
return NULL;
|
||||
}
|
||||
if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
|
||||
return NULL;
|
||||
@@ -655,6 +637,10 @@ int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
|
||||
|
||||
DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
|
||||
return NULL;
|
||||
}
|
||||
if (pkey->type != EVP_PKEY_DSA) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
|
||||
return NULL;
|
||||
@@ -683,6 +669,10 @@ int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
|
||||
|
||||
EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
|
||||
return NULL;
|
||||
}
|
||||
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
|
||||
return NULL;
|
||||
@@ -713,6 +703,10 @@ int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
|
||||
|
||||
DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
|
||||
return NULL;
|
||||
}
|
||||
if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
|
||||
return NULL;
|
||||
@@ -755,6 +749,119 @@ int EVP_PKEY_base_id(const EVP_PKEY *pkey)
|
||||
return EVP_PKEY_type(pkey->type);
|
||||
}
|
||||
|
||||
int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
|
||||
{
|
||||
#ifndef FIPS_MODE
|
||||
if (pkey->keymgmt == NULL) {
|
||||
/*
|
||||
* These hard coded cases are pure hackery to get around the fact
|
||||
* that names in crypto/objects/objects.txt are a mess. There is
|
||||
* no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
|
||||
* fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
|
||||
* the NID of which is used for EVP_PKEY_RSA. Strangely enough,
|
||||
* "DSA" is accurate... but still, better be safe and hard-code
|
||||
* names that we know.
|
||||
* TODO Clean this away along with all other #legacy support.
|
||||
*/
|
||||
int type;
|
||||
|
||||
if (strcasecmp(name, "RSA") == 0)
|
||||
type = EVP_PKEY_RSA;
|
||||
#ifndef OPENSSL_NO_EC
|
||||
else if (strcasecmp(name, "EC") == 0)
|
||||
type = EVP_PKEY_EC;
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
else if (strcasecmp(name, "DSA") == 0)
|
||||
type = EVP_PKEY_DSA;
|
||||
#endif
|
||||
else
|
||||
type = EVP_PKEY_type(OBJ_sn2nid(name));
|
||||
return EVP_PKEY_type(pkey->type) == type;
|
||||
}
|
||||
#endif
|
||||
return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
|
||||
}
|
||||
|
||||
int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
|
||||
{
|
||||
if (pkey->keymgmt == NULL) {
|
||||
switch (EVP_PKEY_base_id(pkey)) {
|
||||
case EVP_PKEY_RSA:
|
||||
return 1;
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
case EVP_PKEY_DSA:
|
||||
return 1;
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
case EVP_PKEY_ED25519:
|
||||
case EVP_PKEY_ED448:
|
||||
return 1;
|
||||
case EVP_PKEY_EC: /* Including SM2 */
|
||||
return EC_KEY_can_sign(pkey->pkey.ec);
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
|
||||
OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
|
||||
const char *supported_sig =
|
||||
pkey->keymgmt->query_operation_name != NULL
|
||||
? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
|
||||
: evp_first_name(prov, pkey->keymgmt->name_id);
|
||||
EVP_SIGNATURE *signature = NULL;
|
||||
|
||||
signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
|
||||
if (signature != NULL) {
|
||||
EVP_SIGNATURE_free(signature);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
/*
|
||||
* TODO rewrite when we have proper data extraction functions
|
||||
* Note: an octet pointer would be desirable!
|
||||
*/
|
||||
static OSSL_CALLBACK get_ec_curve_name_cb;
|
||||
static int get_ec_curve_name_cb(const OSSL_PARAM params[], void *arg)
|
||||
{
|
||||
const OSSL_PARAM *p = NULL;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL)
|
||||
return OSSL_PARAM_get_utf8_string(p, arg, 0);
|
||||
|
||||
/* If there is no curve name, this is not an EC key */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int evp_pkey_get_EC_KEY_curve_nid(const EVP_PKEY *pkey)
|
||||
{
|
||||
int ret = NID_undef;
|
||||
|
||||
if (pkey->keymgmt == NULL) {
|
||||
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
|
||||
EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
|
||||
|
||||
ret = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
|
||||
}
|
||||
} 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);
|
||||
if (ret)
|
||||
ret = ec_curve_name2nid(curve_name);
|
||||
OPENSSL_free(curve_name);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
|
||||
{
|
||||
@@ -993,10 +1100,192 @@ EVP_PKEY *EVP_PKEY_new(void)
|
||||
ret->lock = CRYPTO_THREAD_lock_new();
|
||||
if (ret->lock == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(ret);
|
||||
return NULL;
|
||||
goto err;
|
||||
}
|
||||
#ifndef FIPS_MODE
|
||||
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
|
||||
EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
|
||||
err:
|
||||
CRYPTO_THREAD_lock_free(ret->lock);
|
||||
OPENSSL_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup a public key management method.
|
||||
*
|
||||
* For legacy keys, either |type| or |str| is expected to have the type
|
||||
* information. In this case, the setup consists of finding an ASN1 method
|
||||
* and potentially an ENGINE, and setting those fields in |pkey|.
|
||||
*
|
||||
* For provider side keys, |keymgmt| is expected to be non-NULL. In this
|
||||
* case, the setup consists of setting the |keymgmt| field in |pkey|.
|
||||
*
|
||||
* If pkey is NULL just return 1 or 0 if the key management method exists.
|
||||
*/
|
||||
|
||||
static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
|
||||
int len, EVP_KEYMGMT *keymgmt)
|
||||
{
|
||||
#ifndef FIPS_MODE
|
||||
const EVP_PKEY_ASN1_METHOD *ameth = NULL;
|
||||
ENGINE **eptr = (e == NULL) ? &e : NULL;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The setups can't set both legacy and provider side methods.
|
||||
* It is forbidden
|
||||
*/
|
||||
if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
|
||||
|| !ossl_assert(e == NULL || keymgmt == NULL)) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pkey != NULL) {
|
||||
int free_it = 0;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
free_it = free_it || pkey->pkey.ptr != NULL;
|
||||
#endif
|
||||
free_it = free_it || pkey->keydata != NULL;
|
||||
if (free_it)
|
||||
evp_pkey_free_it(pkey);
|
||||
#ifndef FIPS_MODE
|
||||
/*
|
||||
* If key type matches and a method exists then this lookup has
|
||||
* succeeded once so just indicate success.
|
||||
*/
|
||||
if (pkey->type != EVP_PKEY_NONE
|
||||
&& type == pkey->save_type
|
||||
&& pkey->ameth != NULL)
|
||||
return 1;
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
/* If we have ENGINEs release them */
|
||||
ENGINE_finish(pkey->engine);
|
||||
pkey->engine = NULL;
|
||||
ENGINE_finish(pkey->pmeth_engine);
|
||||
pkey->pmeth_engine = NULL;
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
#ifndef FIPS_MODE
|
||||
if (str != NULL)
|
||||
ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
|
||||
else if (type != EVP_PKEY_NONE)
|
||||
ameth = EVP_PKEY_asn1_find(eptr, type);
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
if (pkey == NULL && eptr != NULL)
|
||||
ENGINE_finish(e);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
{
|
||||
int check = 1;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
check = check && ameth == NULL;
|
||||
#endif
|
||||
check = check && keymgmt == NULL;
|
||||
if (check) {
|
||||
EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (pkey != NULL) {
|
||||
if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkey->keymgmt = keymgmt;
|
||||
|
||||
pkey->save_type = type;
|
||||
pkey->type = type;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/*
|
||||
* If the internal "origin" key is provider side, don't save |ameth|.
|
||||
* The main reason is that |ameth| is one factor to detect that the
|
||||
* internal "origin" key is a legacy one.
|
||||
*/
|
||||
if (keymgmt == NULL)
|
||||
pkey->ameth = ameth;
|
||||
pkey->engine = e;
|
||||
|
||||
/*
|
||||
* The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes,
|
||||
* depending on if we're setting this key to contain a legacy or
|
||||
* a provider side "origin" key. For a legacy key, we assign it
|
||||
* to the |type| field, but for a provider side key, we assign it
|
||||
* to the |save_type| field, because |type| is supposed to be set
|
||||
* to EVP_PKEY_NONE in that case.
|
||||
*/
|
||||
if (keymgmt != NULL)
|
||||
pkey->save_type = ameth->pkey_id;
|
||||
else if (pkey->ameth != NULL)
|
||||
pkey->type = ameth->pkey_id;
|
||||
#endif
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
static void find_ameth(const char *name, void *data)
|
||||
{
|
||||
const char **str = data;
|
||||
|
||||
/*
|
||||
* The error messages from pkey_set_type() are uninteresting here,
|
||||
* and misleading.
|
||||
*/
|
||||
ERR_set_mark();
|
||||
|
||||
if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
|
||||
NULL)) {
|
||||
if (str[0] == NULL)
|
||||
str[0] = name;
|
||||
else if (str[1] == NULL)
|
||||
str[1] = name;
|
||||
}
|
||||
|
||||
ERR_pop_to_mark();
|
||||
}
|
||||
#endif
|
||||
|
||||
int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
|
||||
{
|
||||
#ifndef FIPS_MODE
|
||||
# define EVP_PKEY_TYPE_STR str[0]
|
||||
# define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
|
||||
/*
|
||||
* Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
|
||||
* Ideally, only one should be found. If two (or more) are found, the
|
||||
* match is ambiguous. This should never happen, but...
|
||||
*/
|
||||
const char *str[2] = { NULL, NULL };
|
||||
|
||||
EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str);
|
||||
if (str[1] != NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
# define EVP_PKEY_TYPE_STR NULL
|
||||
# define EVP_PKEY_TYPE_STRLEN -1
|
||||
#endif
|
||||
return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
|
||||
EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
|
||||
keymgmt);
|
||||
|
||||
#undef EVP_PKEY_TYPE_STR
|
||||
#undef EVP_PKEY_TYPE_STRLEN
|
||||
}
|
||||
|
||||
int EVP_PKEY_up_ref(EVP_PKEY *pkey)
|
||||
@@ -1012,13 +1301,12 @@ int EVP_PKEY_up_ref(EVP_PKEY *pkey)
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
static void evp_pkey_free_legacy(EVP_PKEY *x)
|
||||
void evp_pkey_free_legacy(EVP_PKEY *x)
|
||||
{
|
||||
if (x->ameth != NULL) {
|
||||
if (x->ameth->pkey_free != NULL)
|
||||
x->ameth->pkey_free(x);
|
||||
x->pkey.ptr = NULL;
|
||||
x->ameth = NULL;
|
||||
}
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
ENGINE_finish(x->engine);
|
||||
@@ -1026,7 +1314,7 @@ static void evp_pkey_free_legacy(EVP_PKEY *x)
|
||||
ENGINE_finish(x->pmeth_engine);
|
||||
x->pmeth_engine = NULL;
|
||||
# endif
|
||||
x->type = x->save_type = EVP_PKEY_NONE;
|
||||
x->type = EVP_PKEY_NONE;
|
||||
}
|
||||
#endif /* FIPS_MODE */
|
||||
|
||||
@@ -1060,6 +1348,9 @@ void EVP_PKEY_free(EVP_PKEY *x)
|
||||
return;
|
||||
REF_ASSERT_ISNT(i < 0);
|
||||
evp_pkey_free_it(x);
|
||||
#ifndef FIPS_MODE
|
||||
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
|
||||
#endif
|
||||
CRYPTO_THREAD_lock_free(x->lock);
|
||||
#ifndef FIPS_MODE
|
||||
sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
|
||||
@@ -1069,13 +1360,16 @@ void EVP_PKEY_free(EVP_PKEY *x)
|
||||
|
||||
int EVP_PKEY_size(const EVP_PKEY *pkey)
|
||||
{
|
||||
int size = 0;
|
||||
|
||||
if (pkey != NULL) {
|
||||
if (pkey->ameth == NULL)
|
||||
return pkey->cache.size;
|
||||
else if (pkey->ameth->pkey_size != NULL)
|
||||
return pkey->ameth->pkey_size(pkey);
|
||||
size = pkey->cache.size;
|
||||
#ifndef FIPS_MODE
|
||||
if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
|
||||
size = pkey->ameth->pkey_size(pkey);
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
@@ -1085,10 +1379,20 @@ void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
EVP_KEYMGMT *allocated_keymgmt = NULL;
|
||||
EVP_KEYMGMT *tmp_keymgmt = NULL;
|
||||
void *keydata = NULL;
|
||||
int check;
|
||||
|
||||
if (pk == NULL)
|
||||
return NULL;
|
||||
|
||||
/* No key data => nothing to export */
|
||||
check = 1;
|
||||
#ifndef FIPS_MODE
|
||||
check = check && pk->pkey.ptr == NULL;
|
||||
#endif
|
||||
check = check && pk->keydata == NULL;
|
||||
if (check)
|
||||
return NULL;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
if (pk->pkey.ptr != NULL) {
|
||||
/*
|
||||
@@ -1105,13 +1409,15 @@ void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
*keymgmt = NULL;
|
||||
}
|
||||
|
||||
/* If no keymgmt was given or found, get a default keymgmt */
|
||||
/*
|
||||
* If no keymgmt was given or found, get a default keymgmt. We do so by
|
||||
* letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
|
||||
*/
|
||||
if (tmp_keymgmt == NULL) {
|
||||
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
|
||||
|
||||
if (ctx != NULL && ctx->keytype != NULL)
|
||||
tmp_keymgmt = allocated_keymgmt =
|
||||
EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
|
||||
tmp_keymgmt = ctx->keymgmt;
|
||||
ctx->keymgmt = NULL;
|
||||
EVP_PKEY_CTX_free(ctx);
|
||||
}
|
||||
|
||||
@@ -1159,7 +1465,7 @@ void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
|
||||
goto end;
|
||||
|
||||
if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
|
||||
if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) {
|
||||
evp_keymgmt_freedata(tmp_keymgmt, keydata);
|
||||
keydata = NULL;
|
||||
goto end;
|
||||
@@ -1212,98 +1518,255 @@ void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/*
|
||||
* This differs from exporting in that it releases the legacy key and assigns
|
||||
* the export keymgmt and keydata to the "origin" provider side key instead
|
||||
* of the operation cache.
|
||||
*/
|
||||
void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
|
||||
EVP_KEYMGMT **keymgmt,
|
||||
const char *propquery)
|
||||
int evp_pkey_downgrade(EVP_PKEY *pk)
|
||||
{
|
||||
EVP_KEYMGMT *allocated_keymgmt = NULL;
|
||||
EVP_KEYMGMT *tmp_keymgmt = NULL;
|
||||
void *keydata = NULL;
|
||||
EVP_KEYMGMT *keymgmt = pk->keymgmt;
|
||||
void *keydata = pk->keydata;
|
||||
int type = pk->save_type;
|
||||
const char *keytype = NULL;
|
||||
|
||||
if (pk == NULL)
|
||||
return NULL;
|
||||
/* If this isn't a provider side key, we're done */
|
||||
if (keymgmt == NULL)
|
||||
return 1;
|
||||
|
||||
/* Get the key type name for error reporting */
|
||||
if (type != EVP_PKEY_NONE)
|
||||
keytype = OBJ_nid2sn(type);
|
||||
else
|
||||
keytype =
|
||||
evp_first_name(EVP_KEYMGMT_provider(keymgmt), keymgmt->name_id);
|
||||
|
||||
/*
|
||||
* If this key is already "upgraded", this function shouldn't have been
|
||||
* called.
|
||||
* |save_type| was set when any of the EVP_PKEY_set_type functions
|
||||
* was called. It was set to EVP_PKEY_NONE if the key type wasn't
|
||||
* recognised to be any of the legacy key types, and the downgrade
|
||||
* isn't possible.
|
||||
*/
|
||||
if (!ossl_assert(pk->keymgmt == NULL))
|
||||
return NULL;
|
||||
|
||||
if (keymgmt != NULL) {
|
||||
tmp_keymgmt = *keymgmt;
|
||||
*keymgmt = NULL;
|
||||
if (type == EVP_PKEY_NONE) {
|
||||
ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_KEY_TYPE,
|
||||
"key type = %s, can't downgrade", keytype);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If the key isn't a legacy one, bail out, but with proper values */
|
||||
if (pk->pkey.ptr == NULL) {
|
||||
tmp_keymgmt = pk->keymgmt;
|
||||
keydata = pk->keydata;
|
||||
} else {
|
||||
/* If the legacy key doesn't have an export function, give up */
|
||||
if (pk->ameth->export_to == NULL)
|
||||
return NULL;
|
||||
|
||||
/* If no keymgmt was given, get a default keymgmt */
|
||||
if (tmp_keymgmt == NULL) {
|
||||
EVP_PKEY_CTX *ctx =
|
||||
EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
|
||||
|
||||
if (ctx != NULL && ctx->keytype != NULL)
|
||||
tmp_keymgmt = allocated_keymgmt =
|
||||
EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
|
||||
EVP_PKEY_CTX_free(ctx);
|
||||
}
|
||||
|
||||
/* If we still don't have a keymgmt, give up */
|
||||
if (tmp_keymgmt == NULL)
|
||||
goto end;
|
||||
|
||||
/* Make sure that the keymgmt key type matches the legacy NID */
|
||||
if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
|
||||
goto end;
|
||||
|
||||
if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
|
||||
goto end;
|
||||
|
||||
if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)
|
||||
|| !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
|
||||
evp_keymgmt_freedata(tmp_keymgmt, keydata);
|
||||
keydata = NULL;
|
||||
goto end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear the operation cache, all the legacy data, as well as the
|
||||
* dirty counters
|
||||
*/
|
||||
evp_pkey_free_legacy(pk);
|
||||
pk->dirty_cnt_copy = 0;
|
||||
|
||||
evp_keymgmt_util_clear_operation_cache(pk);
|
||||
pk->keymgmt = tmp_keymgmt;
|
||||
pk->keydata = keydata;
|
||||
evp_keymgmt_util_cache_keyinfo(pk);
|
||||
}
|
||||
|
||||
end:
|
||||
/*
|
||||
* If nothing was upgraded, |tmp_keymgmt| might point at a freed
|
||||
* EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
|
||||
* the caller either way in that case.
|
||||
* To be able to downgrade, we steal the provider side "origin" keymgmt
|
||||
* and keydata. We've already grabbed the pointers, so all we need to
|
||||
* do is clear those pointers in |pk| and then call evp_pkey_free_it().
|
||||
* That way, we can restore |pk| if we need to.
|
||||
*/
|
||||
if (keydata == NULL)
|
||||
tmp_keymgmt = NULL;
|
||||
pk->keymgmt = NULL;
|
||||
pk->keydata = NULL;
|
||||
evp_pkey_free_it(pk);
|
||||
if (EVP_PKEY_set_type(pk, type)) {
|
||||
/* If the key is typed but empty, we're done */
|
||||
if (keydata == NULL) {
|
||||
/* We're dropping the EVP_KEYMGMT */
|
||||
EVP_KEYMGMT_free(keymgmt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (keymgmt != NULL)
|
||||
*keymgmt = tmp_keymgmt;
|
||||
if (pk->ameth->import_from == NULL) {
|
||||
ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
|
||||
"key type = %s", keytype);
|
||||
} else if (evp_keymgmt_export(keymgmt, keydata,
|
||||
OSSL_KEYMGMT_SELECT_ALL,
|
||||
pk->ameth->import_from, pk)) {
|
||||
/*
|
||||
* Save the provider side data in the operation cache, so they'll
|
||||
* find it again. evp_pkey_free_it() cleared the cache, so it's
|
||||
* safe to assume slot zero is free.
|
||||
* Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
|
||||
* reference count.
|
||||
*/
|
||||
evp_keymgmt_util_cache_keydata(pk, 0, keymgmt, keydata);
|
||||
|
||||
EVP_KEYMGMT_free(allocated_keymgmt);
|
||||
return keydata;
|
||||
/* Synchronize the dirty count */
|
||||
pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
|
||||
|
||||
/* evp_keymgmt_export() increased the refcount... */
|
||||
EVP_KEYMGMT_free(keymgmt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
|
||||
"key type = %s", keytype);
|
||||
}
|
||||
|
||||
/*
|
||||
* Something went wrong. This could for example happen if the keymgmt
|
||||
* turns out to be an HSM implementation that refuses to let go of some
|
||||
* of the key data, typically the private bits. In this case, we restore
|
||||
* the provider side internal "origin" and leave it at that.
|
||||
*/
|
||||
if (!ossl_assert(EVP_PKEY_set_type_by_keymgmt(pk, keymgmt))) {
|
||||
/* This should not be impossible */
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
/* EVP_PKEY_set_type_by_keymgmt() increased the refcount... */
|
||||
EVP_KEYMGMT_free(keymgmt);
|
||||
pk->keydata = keydata;
|
||||
evp_keymgmt_util_cache_keyinfo(pk);
|
||||
return 0; /* No downgrade, but at least the key is restored */
|
||||
}
|
||||
#endif /* FIPS_MODE */
|
||||
|
||||
const OSSL_PARAM *EVP_PKEY_gettable_params(EVP_PKEY *pkey)
|
||||
{
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL)
|
||||
return 0;
|
||||
return evp_keymgmt_gettable_params(pkey->keymgmt);
|
||||
}
|
||||
|
||||
/*
|
||||
* For the following methods param->return_size is set to a value
|
||||
* larger than can be returned by the call to evp_keymgmt_get_params().
|
||||
* If it is still this value then the parameter was ignored - and in this
|
||||
* case it returns an error..
|
||||
*/
|
||||
|
||||
int EVP_PKEY_get_bn_param(EVP_PKEY *pkey, const char *key_name, BIGNUM **bn)
|
||||
{
|
||||
int ret = 0;
|
||||
OSSL_PARAM params[2];
|
||||
unsigned char buffer[2048];
|
||||
/*
|
||||
* Use -1 as the terminator here instead of sizeof(buffer) + 1 since
|
||||
* -1 is less likely to be a valid value.
|
||||
*/
|
||||
const size_t not_set = (size_t)-1;
|
||||
unsigned char *buf = NULL;
|
||||
size_t buf_sz = 0;
|
||||
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL
|
||||
|| key_name == NULL
|
||||
|| bn == NULL)
|
||||
return 0;
|
||||
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
|
||||
/* If the return_size is still not_set then we know it was not found */
|
||||
params[0].return_size = not_set;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) {
|
||||
if (params[0].return_size == not_set
|
||||
|| params[0].return_size == 0)
|
||||
return 0;
|
||||
buf_sz = params[0].return_size;
|
||||
/*
|
||||
* If it failed because the buffer was too small then allocate the
|
||||
* required buffer size and retry.
|
||||
*/
|
||||
buf = OPENSSL_zalloc(buf_sz);
|
||||
if (buf == NULL)
|
||||
return 0;
|
||||
params[0].data = buf;
|
||||
params[0].data_size = buf_sz;
|
||||
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
|
||||
goto err;
|
||||
}
|
||||
/* Fail if the param was not found */
|
||||
if (params[0].return_size == not_set)
|
||||
goto err;
|
||||
ret = OSSL_PARAM_get_BN(params, bn);
|
||||
err:
|
||||
OPENSSL_free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_PKEY_get_octet_string_param(EVP_PKEY *pkey, const char *key_name,
|
||||
unsigned char *buf, size_t max_buf_sz,
|
||||
size_t *out_sz)
|
||||
{
|
||||
OSSL_PARAM params[2];
|
||||
const size_t not_set = max_buf_sz + 1;
|
||||
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL
|
||||
|| key_name == NULL)
|
||||
return 0;
|
||||
|
||||
params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
|
||||
params[0].return_size = not_set;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
|
||||
return 0;
|
||||
if (params[0].return_size == not_set)
|
||||
return 0;
|
||||
if (out_sz != NULL)
|
||||
*out_sz = params[0].return_size;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_PKEY_get_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
|
||||
char *str, size_t max_buf_sz,
|
||||
size_t *out_sz)
|
||||
{
|
||||
OSSL_PARAM params[2];
|
||||
const size_t not_set = max_buf_sz + 1;
|
||||
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL
|
||||
|| key_name == NULL)
|
||||
return 0;
|
||||
|
||||
params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
|
||||
params[0].return_size = not_set;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
|
||||
return 0;
|
||||
if (params[0].return_size == not_set)
|
||||
return 0;
|
||||
if (out_sz != NULL)
|
||||
*out_sz = params[0].return_size;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_PKEY_get_int_param(EVP_PKEY *pkey, const char *key_name, int *out)
|
||||
{
|
||||
OSSL_PARAM params[2];
|
||||
const size_t not_set = sizeof(int) + 1;
|
||||
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL
|
||||
|| key_name == NULL)
|
||||
return 0;
|
||||
|
||||
params[0] = OSSL_PARAM_construct_int(key_name, out);
|
||||
params[0].return_size = not_set;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
|
||||
return 0;
|
||||
if (params[0].return_size == not_set)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_PKEY_get_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t *out)
|
||||
{
|
||||
OSSL_PARAM params[2];
|
||||
const size_t not_set = sizeof(size_t) + 1;
|
||||
|
||||
if (pkey == NULL
|
||||
|| pkey->keymgmt == NULL
|
||||
|| pkey->keydata == NULL
|
||||
|| key_name == NULL)
|
||||
return 0;
|
||||
|
||||
params[0] = OSSL_PARAM_construct_size_t(key_name, out);
|
||||
params[0].return_size = not_set;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
|
||||
return 0;
|
||||
if (params[0].return_size == not_set)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
+79
-9
@@ -51,17 +51,31 @@ static int pkey_mac_init(EVP_PKEY_CTX *ctx)
|
||||
MAC_PKEY_CTX *hctx;
|
||||
/* We're being smart and using the same base NIDs for PKEY and for MAC */
|
||||
int nid = ctx->pmeth->pkey_id;
|
||||
EVP_MAC *mac = EVP_MAC_fetch(NULL, OBJ_nid2sn(nid), NULL);
|
||||
EVP_MAC *mac;
|
||||
|
||||
ERR_set_mark();
|
||||
mac = EVP_MAC_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propquery);
|
||||
ERR_pop_to_mark();
|
||||
|
||||
/*
|
||||
* mac == NULL may actually be ok in some situations. In an
|
||||
* EVP_PKEY_new_mac_key() call a temporary EVP_PKEY_CTX is created with
|
||||
* default libctx. We don't actually need the underlying MAC to be present
|
||||
* to successfully set the key in that case. The resulting EVP_PKEY could
|
||||
* then be used in some other libctx where the MAC *is* present
|
||||
*/
|
||||
|
||||
if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
|
||||
EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hctx->ctx = EVP_MAC_CTX_new(mac);
|
||||
if (hctx->ctx == NULL) {
|
||||
OPENSSL_free(hctx);
|
||||
return 0;
|
||||
if (mac != NULL) {
|
||||
hctx->ctx = EVP_MAC_CTX_new(mac);
|
||||
if (hctx->ctx == NULL) {
|
||||
OPENSSL_free(hctx);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (nid == EVP_PKEY_CMAC) {
|
||||
@@ -83,6 +97,13 @@ static int pkey_mac_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
|
||||
MAC_PKEY_CTX *sctx, *dctx;
|
||||
|
||||
sctx = EVP_PKEY_CTX_get_data(src);
|
||||
|
||||
if (sctx->ctx == NULL) {
|
||||
/* This actually means the fetch failed during the init call */
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sctx->ctx->data == NULL)
|
||||
return 0;
|
||||
|
||||
@@ -142,7 +163,7 @@ static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
|
||||
MAC_PKEY_CTX *hctx = ctx == NULL ? NULL : EVP_PKEY_CTX_get_data(ctx);
|
||||
|
||||
if (hctx != NULL) {
|
||||
EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
|
||||
EVP_MAC *mac = hctx->ctx != NULL ? EVP_MAC_CTX_mac(hctx->ctx) : NULL;
|
||||
|
||||
switch (hctx->type) {
|
||||
case MAC_TYPE_RAW:
|
||||
@@ -177,8 +198,15 @@ static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||
break;
|
||||
case MAC_TYPE_MAC:
|
||||
{
|
||||
EVP_MAC_CTX *cmkey = EVP_MAC_CTX_dup(hctx->ctx);
|
||||
EVP_MAC_CTX *cmkey;
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/* This actually means the fetch failed during the init call */
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cmkey = EVP_MAC_CTX_dup(hctx->ctx);
|
||||
if (cmkey == NULL)
|
||||
return 0;
|
||||
if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(hctx->ctx)))
|
||||
@@ -220,6 +248,12 @@ static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
|
||||
hctx->type == MAC_TYPE_RAW
|
||||
&& (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/* This actually means the fetch failed during the init call */
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (set_key) {
|
||||
if (!EVP_MAC_is_a(EVP_MAC_CTX_mac(hctx->ctx),
|
||||
OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
|
||||
@@ -285,6 +319,14 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
ciphname, 0);
|
||||
params[params_n] = OSSL_PARAM_construct_end();
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/*
|
||||
* This actually means the fetch failed during the init call
|
||||
*/
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!EVP_MAC_CTX_set_params(hctx->ctx, params)
|
||||
|| !EVP_MAC_init(hctx->ctx))
|
||||
return 0;
|
||||
@@ -306,8 +348,7 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
|
||||
if (ctx->pkey == NULL)
|
||||
return 0;
|
||||
new_mac_ctx = EVP_MAC_CTX_dup((EVP_MAC_CTX *)ctx->pkey
|
||||
->pkey.ptr);
|
||||
new_mac_ctx = EVP_MAC_CTX_dup(ctx->pkey->pkey.ptr);
|
||||
if (new_mac_ctx == NULL)
|
||||
return 0;
|
||||
EVP_MAC_CTX_free(hctx->ctx);
|
||||
@@ -337,6 +378,14 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
params[0] =
|
||||
OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/*
|
||||
* This actually means the fetch failed during the init call
|
||||
*/
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!EVP_MAC_CTX_set_params(hctx->ctx, params))
|
||||
return 0;
|
||||
|
||||
@@ -373,6 +422,14 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
p2, p1);
|
||||
params[params_n] = OSSL_PARAM_construct_end();
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/*
|
||||
* This actually means the fetch failed during the init call
|
||||
*/
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EVP_MAC_CTX_set_params(hctx->ctx, params);
|
||||
}
|
||||
break;
|
||||
@@ -385,6 +442,12 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
case EVP_PKEY_CTRL_DIGESTINIT:
|
||||
switch (hctx->type) {
|
||||
case MAC_TYPE_RAW:
|
||||
if (hctx->ctx == NULL) {
|
||||
/* This actually means the fetch failed during the init call */
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Ensure that we have attached the implementation */
|
||||
if (!EVP_MAC_init(hctx->ctx))
|
||||
return 0;
|
||||
@@ -456,6 +519,13 @@ static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
type, value, strlen(value) + 1, NULL))
|
||||
return 0;
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
|
||||
if (hctx->ctx == NULL) {
|
||||
/* This actually means the fetch failed during the init call */
|
||||
EVPerr(0, EVP_R_FETCH_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
|
||||
OPENSSL_free(params[0].data);
|
||||
return ok;
|
||||
|
||||
+33
-16
@@ -35,19 +35,24 @@ int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
|
||||
return evp_keymgmt_validate(keymgmt, key,
|
||||
OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
|
||||
|
||||
if (pkey->type == EVP_PKEY_NONE)
|
||||
goto not_supported;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/* legacy */
|
||||
/* call customized public key check function first */
|
||||
if (ctx->pmeth->public_check != NULL)
|
||||
return ctx->pmeth->public_check(pkey);
|
||||
|
||||
/* use default public key check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)
|
||||
goto not_supported;
|
||||
|
||||
return pkey->ameth->pkey_public_check(pkey);
|
||||
#endif
|
||||
not_supported:
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
|
||||
@@ -68,19 +73,24 @@ int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
|
||||
return evp_keymgmt_validate(keymgmt, key,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
|
||||
if (pkey->type == EVP_PKEY_NONE)
|
||||
goto not_supported;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/* legacy */
|
||||
/* call customized param check function first */
|
||||
if (ctx->pmeth->param_check != NULL)
|
||||
return ctx->pmeth->param_check(pkey);
|
||||
|
||||
/* legacy */
|
||||
/* use default param check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)
|
||||
goto not_supported;
|
||||
|
||||
return pkey->ameth->pkey_param_check(pkey);
|
||||
#endif
|
||||
not_supported:
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
|
||||
@@ -101,6 +111,7 @@ int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
|
||||
return evp_keymgmt_validate(keymgmt, key,
|
||||
OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
|
||||
/* not supported for legacy keys */
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -121,6 +132,7 @@ int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
|
||||
if (key != NULL && keymgmt != NULL)
|
||||
return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
|
||||
/* not supported for legacy keys */
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -141,18 +153,23 @@ int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
|
||||
if (key != NULL && keymgmt != NULL)
|
||||
return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);
|
||||
|
||||
if (pkey->type == EVP_PKEY_NONE)
|
||||
goto not_supported;
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/* legacy */
|
||||
/* call customized check function first */
|
||||
if (ctx->pmeth->check != NULL)
|
||||
return ctx->pmeth->check(pkey);
|
||||
|
||||
/* use default check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)
|
||||
goto not_supported;
|
||||
|
||||
return pkey->ameth->pkey_check(pkey);
|
||||
#endif
|
||||
not_supported:
|
||||
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation)
|
||||
*/
|
||||
ERR_set_mark();
|
||||
|
||||
if (ctx->keytype == NULL || ctx->engine != NULL)
|
||||
if (ctx->engine != NULL || ctx->keytype == NULL)
|
||||
goto legacy;
|
||||
|
||||
/*
|
||||
|
||||
+238
-76
@@ -9,7 +9,10 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/core.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/core.h"
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/evp.h>
|
||||
#include "crypto/bn.h"
|
||||
@@ -17,102 +20,262 @@
|
||||
#include "crypto/evp.h"
|
||||
#include "evp_local.h"
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
|
||||
#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_EC)
|
||||
# define TMP_SM2_HACK
|
||||
#endif
|
||||
|
||||
/* TODO(3.0) remove when provider SM2 key generation is implemented */
|
||||
#ifdef TMP_SM2_HACK
|
||||
# include <openssl/ec.h>
|
||||
# include <openssl/serializer.h>
|
||||
# include "internal/sizes.h"
|
||||
#endif
|
||||
|
||||
static int gen_init(EVP_PKEY_CTX *ctx, int operation)
|
||||
{
|
||||
int ret;
|
||||
if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
int ret = 0;
|
||||
|
||||
if (ctx == NULL)
|
||||
goto not_supported;
|
||||
|
||||
evp_pkey_ctx_free_old_ops(ctx);
|
||||
ctx->operation = operation;
|
||||
|
||||
if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
|
||||
goto legacy;
|
||||
|
||||
/* TODO remove when provider SM2 key generation is implemented */
|
||||
#ifdef TMP_SM2_HACK
|
||||
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id == EVP_PKEY_SM2)
|
||||
goto legacy;
|
||||
#endif
|
||||
|
||||
switch (operation) {
|
||||
case EVP_PKEY_OP_PARAMGEN:
|
||||
ctx->op.keymgmt.genctx =
|
||||
evp_keymgmt_gen_init(ctx->keymgmt,
|
||||
OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
|
||||
break;
|
||||
case EVP_PKEY_OP_KEYGEN:
|
||||
ctx->op.keymgmt.genctx =
|
||||
evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
|
||||
break;
|
||||
}
|
||||
ctx->operation = EVP_PKEY_OP_PARAMGEN;
|
||||
if (!ctx->pmeth->paramgen_init)
|
||||
return 1;
|
||||
ret = ctx->pmeth->paramgen_init(ctx);
|
||||
|
||||
if (ctx->op.keymgmt.genctx == NULL)
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
||||
else
|
||||
ret = 1;
|
||||
goto end;
|
||||
|
||||
legacy:
|
||||
#ifdef FIPS_MODE
|
||||
goto not_supported;
|
||||
#else
|
||||
if (ctx->pmeth == NULL
|
||||
|| (operation == EVP_PKEY_OP_PARAMGEN
|
||||
&& ctx->pmeth->paramgen == NULL)
|
||||
|| (operation == EVP_PKEY_OP_KEYGEN
|
||||
&& ctx->pmeth->keygen == NULL))
|
||||
goto not_supported;
|
||||
|
||||
ret = 1;
|
||||
switch (operation) {
|
||||
case EVP_PKEY_OP_PARAMGEN:
|
||||
if (ctx->pmeth->paramgen_init != NULL)
|
||||
ret = ctx->pmeth->paramgen_init(ctx);
|
||||
break;
|
||||
case EVP_PKEY_OP_KEYGEN:
|
||||
if (ctx->pmeth->keygen_init != NULL)
|
||||
ret = ctx->pmeth->keygen_init(ctx);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
end:
|
||||
if (ret <= 0)
|
||||
ctx->operation = EVP_PKEY_OP_UNDEFINED;
|
||||
return ret;
|
||||
|
||||
not_supported:
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
ret = -2;
|
||||
goto end;
|
||||
}
|
||||
|
||||
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
||||
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
int ret;
|
||||
if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ppkey == NULL)
|
||||
return -1;
|
||||
|
||||
if (*ppkey == NULL)
|
||||
*ppkey = EVP_PKEY_new();
|
||||
|
||||
if (*ppkey == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ctx->pmeth->paramgen(ctx, *ppkey);
|
||||
if (ret <= 0) {
|
||||
EVP_PKEY_free(*ppkey);
|
||||
*ppkey = NULL;
|
||||
}
|
||||
return ret;
|
||||
return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
|
||||
}
|
||||
|
||||
int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
int ret;
|
||||
if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
|
||||
EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
ctx->operation = EVP_PKEY_OP_KEYGEN;
|
||||
if (!ctx->pmeth->keygen_init)
|
||||
return 1;
|
||||
ret = ctx->pmeth->keygen_init(ctx);
|
||||
if (ret <= 0)
|
||||
ctx->operation = EVP_PKEY_OP_UNDEFINED;
|
||||
return ret;
|
||||
return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
|
||||
}
|
||||
|
||||
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
||||
static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
|
||||
{
|
||||
int ret;
|
||||
EVP_PKEY_CTX *ctx = arg;
|
||||
const OSSL_PARAM *param = NULL;
|
||||
int p = -1, n = -1;
|
||||
|
||||
if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
|
||||
EVPerr(EVP_F_EVP_PKEY_KEYGEN,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
|
||||
EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
if (ctx->pkey_gencb == NULL)
|
||||
return 1; /* No callback? That's fine */
|
||||
|
||||
if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
|
||||
== NULL
|
||||
|| !OSSL_PARAM_get_int(param, &p))
|
||||
return 0;
|
||||
if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
|
||||
== NULL
|
||||
|| !OSSL_PARAM_get_int(param, &n))
|
||||
return 0;
|
||||
|
||||
ctx->keygen_info[0] = p;
|
||||
ctx->keygen_info[1] = n;
|
||||
|
||||
return ctx->pkey_gencb(ctx);
|
||||
}
|
||||
|
||||
int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
||||
{
|
||||
int ret = 0;
|
||||
OSSL_CALLBACK cb;
|
||||
EVP_PKEY *allocated_pkey = NULL;
|
||||
|
||||
if (ppkey == NULL)
|
||||
return -1;
|
||||
|
||||
if (*ppkey == NULL)
|
||||
*ppkey = EVP_PKEY_new();
|
||||
if (*ppkey == NULL)
|
||||
return -1;
|
||||
if (ctx == NULL)
|
||||
goto not_supported;
|
||||
|
||||
ret = ctx->pmeth->keygen(ctx, *ppkey);
|
||||
if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
|
||||
goto not_initialized;
|
||||
|
||||
if (*ppkey == NULL)
|
||||
*ppkey = allocated_pkey = EVP_PKEY_new();
|
||||
|
||||
if (*ppkey == NULL) {
|
||||
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->op.keymgmt.genctx == NULL)
|
||||
goto legacy;
|
||||
|
||||
ret = 1;
|
||||
if (ctx->pkey != NULL) {
|
||||
EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
|
||||
void *keydata =
|
||||
evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
|
||||
&tmp_keymgmt, ctx->propquery);
|
||||
|
||||
if (keydata == NULL)
|
||||
goto not_supported;
|
||||
ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
|
||||
ctx->op.keymgmt.genctx, keydata);
|
||||
}
|
||||
|
||||
/*
|
||||
* the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
|
||||
* so we so not need to save it, just check it.
|
||||
*/
|
||||
ret = ret
|
||||
&& (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
|
||||
ossl_callback_to_pkey_gencb, ctx)
|
||||
!= NULL);
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
/* In case |*ppkey| was originally a legacy key */
|
||||
if (ret)
|
||||
evp_pkey_free_legacy(*ppkey);
|
||||
#endif
|
||||
|
||||
/* TODO remove when SM2 key have been cleanly separated from EC keys */
|
||||
#ifdef TMP_SM2_HACK
|
||||
/*
|
||||
* Legacy SM2 keys are implemented as EC_KEY with a twist. The legacy
|
||||
* key generation detects the SM2 curve and "magically" changes the pkey
|
||||
* id accordingly.
|
||||
* Since we don't have SM2 in the provider implementation, we need to
|
||||
* downgrade the generated provider side key to a legacy one under the
|
||||
* same conditions.
|
||||
*
|
||||
* THIS IS AN UGLY BUT TEMPORARY HACK
|
||||
*/
|
||||
{
|
||||
char curve_name[OSSL_MAX_NAME_SIZE] = "";
|
||||
|
||||
if (EVP_PKEY_CTX_get_ec_paramgen_curve_name(ctx, curve_name,
|
||||
sizeof(curve_name)) < 1
|
||||
|| strcmp(curve_name, "SM2") != 0)
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!evp_pkey_downgrade(*ppkey)
|
||||
|| !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2))
|
||||
ret = 0;
|
||||
#endif
|
||||
goto end;
|
||||
|
||||
legacy:
|
||||
#ifdef FIPS_MODE
|
||||
goto not_supported;
|
||||
#else
|
||||
if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
|
||||
goto not_accessible;
|
||||
switch (ctx->operation) {
|
||||
case EVP_PKEY_OP_PARAMGEN:
|
||||
ret = ctx->pmeth->paramgen(ctx, *ppkey);
|
||||
break;
|
||||
case EVP_PKEY_OP_KEYGEN:
|
||||
ret = ctx->pmeth->keygen(ctx, *ppkey);
|
||||
break;
|
||||
default:
|
||||
goto not_supported;
|
||||
}
|
||||
#endif
|
||||
|
||||
end:
|
||||
if (ret <= 0) {
|
||||
EVP_PKEY_free(*ppkey);
|
||||
*ppkey = NULL;
|
||||
if (allocated_pkey != NULL)
|
||||
*ppkey = NULL;
|
||||
EVP_PKEY_free(allocated_pkey);
|
||||
}
|
||||
return ret;
|
||||
|
||||
not_supported:
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
ret = -2;
|
||||
goto end;
|
||||
not_initialized:
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
|
||||
ret = -1;
|
||||
goto end;
|
||||
#ifndef FIPS_MODE
|
||||
not_accessible:
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
|
||||
ret = -1;
|
||||
goto end;
|
||||
#endif
|
||||
}
|
||||
|
||||
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
||||
{
|
||||
if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
return EVP_PKEY_gen(ctx, ppkey);
|
||||
}
|
||||
|
||||
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
||||
{
|
||||
if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
return EVP_PKEY_gen(ctx, ppkey);
|
||||
}
|
||||
|
||||
void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
|
||||
@@ -152,6 +315,8 @@ int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
|
||||
return ctx->keygen_info[idx];
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
|
||||
EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
||||
const unsigned char *key, int keylen)
|
||||
{
|
||||
@@ -181,13 +346,10 @@ static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
|
||||
goto not_supported;
|
||||
|
||||
evp_pkey_ctx_free_old_ops(ctx);
|
||||
ctx->operation = operation;
|
||||
if (ctx->keymgmt == NULL)
|
||||
ctx->keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype,
|
||||
ctx->propquery);
|
||||
if (ctx->keymgmt == NULL)
|
||||
goto not_supported;
|
||||
|
||||
ctx->operation = operation;
|
||||
return 1;
|
||||
|
||||
not_supported:
|
||||
|
||||
+98
-24
@@ -1,4 +1,3 @@
|
||||
|
||||
/*
|
||||
* Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
@@ -138,12 +137,13 @@ EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
|
||||
|
||||
static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
|
||||
EVP_PKEY *pkey, ENGINE *e,
|
||||
const char *name, const char *propquery,
|
||||
const char *keytype, const char *propquery,
|
||||
int id)
|
||||
|
||||
{
|
||||
EVP_PKEY_CTX *ret;
|
||||
const EVP_PKEY_METHOD *pmeth = NULL;
|
||||
EVP_KEYMGMT *keymgmt = NULL;
|
||||
|
||||
/*
|
||||
* When using providers, the context is bound to the algo implementation
|
||||
@@ -156,15 +156,11 @@ static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
|
||||
* If the key doesn't contain anything legacy, then it must be provided,
|
||||
* so we extract the necessary information and use that.
|
||||
*/
|
||||
if (pkey != NULL && pkey->ameth == NULL) {
|
||||
if (pkey != NULL && pkey->type == EVP_PKEY_NONE) {
|
||||
/* If we have an engine, something went wrong somewhere... */
|
||||
if (!ossl_assert(e == NULL))
|
||||
return NULL;
|
||||
name = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
|
||||
/*
|
||||
* TODO: I wonder if the EVP_PKEY should have the name and propquery
|
||||
* that were used when building it.... /RL
|
||||
*/
|
||||
keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
|
||||
goto common;
|
||||
}
|
||||
#ifndef FIPS_MODE
|
||||
@@ -187,10 +183,10 @@ static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
|
||||
* since that can only happen internally, it's safe to make an
|
||||
* assertion.
|
||||
*/
|
||||
if (!ossl_assert(e == NULL || name == NULL))
|
||||
if (!ossl_assert(e == NULL || keytype == NULL))
|
||||
return NULL;
|
||||
if (e == NULL)
|
||||
name = OBJ_nid2sn(id);
|
||||
keytype = OBJ_nid2sn(id);
|
||||
|
||||
# ifndef OPENSSL_NO_ENGINE
|
||||
if (e == NULL && pkey != NULL)
|
||||
@@ -225,6 +221,17 @@ static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
|
||||
/* END legacy */
|
||||
#endif /* FIPS_MODE */
|
||||
common:
|
||||
/*
|
||||
* If there's no engine and there's a name, we try fetching a provider
|
||||
* implementation.
|
||||
*/
|
||||
if (e == NULL && keytype != NULL) {
|
||||
/* This could fail so ignore errors */
|
||||
ERR_set_mark();
|
||||
keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
|
||||
ERR_pop_to_mark();
|
||||
}
|
||||
|
||||
ret = OPENSSL_zalloc(sizeof(*ret));
|
||||
if (ret == NULL) {
|
||||
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
|
||||
@@ -234,8 +241,9 @@ static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
|
||||
return NULL;
|
||||
}
|
||||
ret->libctx = libctx;
|
||||
ret->keytype = name;
|
||||
ret->propquery = propquery;
|
||||
ret->keytype = keytype;
|
||||
ret->keymgmt = keymgmt;
|
||||
ret->engine = e;
|
||||
ret->pmeth = pmeth;
|
||||
ret->operation = EVP_PKEY_OP_UNDEFINED;
|
||||
@@ -292,6 +300,9 @@ void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
|
||||
EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
|
||||
ctx->op.ciph.ciphprovctx = NULL;
|
||||
ctx->op.ciph.cipher = NULL;
|
||||
} else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
|
||||
if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
|
||||
evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -569,6 +580,12 @@ int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
|
||||
&& ctx->op.ciph.cipher->set_ctx_params != NULL)
|
||||
return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
|
||||
params);
|
||||
if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
|
||||
&& ctx->op.keymgmt.genctx != NULL
|
||||
&& ctx->keymgmt != NULL
|
||||
&& ctx->keymgmt->gen_set_params != NULL)
|
||||
return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
|
||||
params);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -593,6 +610,12 @@ int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
|
||||
&& ctx->op.ciph.cipher->get_ctx_params != NULL)
|
||||
return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
|
||||
params);
|
||||
if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
|
||||
&& ctx->op.keymgmt.genctx != NULL
|
||||
&& ctx->keymgmt != NULL
|
||||
&& ctx->keymgmt->gen_get_params != NULL)
|
||||
return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
|
||||
params);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -629,6 +652,10 @@ const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
|
||||
&& ctx->op.ciph.cipher != NULL
|
||||
&& ctx->op.ciph.cipher->settable_ctx_params != NULL)
|
||||
return ctx->op.ciph.cipher->settable_ctx_params();
|
||||
if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
|
||||
&& ctx->keymgmt != NULL
|
||||
&& ctx->keymgmt->gen_settable_params != NULL)
|
||||
return evp_keymgmt_gen_settable_params(ctx->keymgmt);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -792,6 +819,8 @@ static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
# ifndef OPENSSL_NO_EC
|
||||
if (keytype == EVP_PKEY_EC) {
|
||||
switch (cmd) {
|
||||
case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
|
||||
return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
|
||||
case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
|
||||
if (p1 == -2) {
|
||||
return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
|
||||
@@ -822,6 +851,30 @@ static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
}
|
||||
}
|
||||
# endif
|
||||
if (keytype == EVP_PKEY_RSA) {
|
||||
switch (cmd) {
|
||||
case EVP_PKEY_CTRL_RSA_OAEP_MD:
|
||||
return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
|
||||
return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_MGF1_MD:
|
||||
return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
|
||||
return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
|
||||
case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
|
||||
return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
|
||||
case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
|
||||
return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
|
||||
case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
|
||||
return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
|
||||
return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* keytype == -1 is used when several key types share the same structure,
|
||||
* or for generic controls that are the same across multiple key types.
|
||||
*/
|
||||
if (keytype == -1) {
|
||||
switch (cmd) {
|
||||
case EVP_PKEY_CTRL_MD:
|
||||
@@ -832,18 +885,8 @@ static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
|
||||
case EVP_PKEY_CTRL_GET_RSA_PADDING:
|
||||
return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_OAEP_MD:
|
||||
return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
|
||||
return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_MGF1_MD:
|
||||
return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
|
||||
return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
|
||||
case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
|
||||
return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
|
||||
case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
|
||||
return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
|
||||
case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
|
||||
return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
|
||||
case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
|
||||
@@ -854,7 +897,8 @@ static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
case EVP_PKEY_CTRL_CMS_DECRYPT:
|
||||
case EVP_PKEY_CTRL_CMS_ENCRYPT:
|
||||
# endif
|
||||
if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
|
||||
/* TODO (3.0) Temporary hack, this should probe */
|
||||
if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
|
||||
return 1;
|
||||
ERR_raise(ERR_LIB_EVP,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
@@ -878,7 +922,9 @@ int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
|| (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
|
||||
&& ctx->op.sig.sigprovctx != NULL)
|
||||
|| (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
|
||||
&& ctx->op.ciph.ciphprovctx != NULL))
|
||||
&& ctx->op.ciph.ciphprovctx != NULL)
|
||||
|| (EVP_PKEY_CTX_IS_GEN_OP(ctx)
|
||||
&& ctx->op.keymgmt.genctx != NULL))
|
||||
return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
|
||||
|
||||
if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
|
||||
@@ -920,6 +966,24 @@ int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
|
||||
const char *value)
|
||||
{
|
||||
|
||||
/* Special cases that we intercept */
|
||||
# ifndef OPENSSL_NO_EC
|
||||
/*
|
||||
* We don't support encoding settings for providers, i.e. the only
|
||||
* possible encoding is "named_curve", so we simply fail when something
|
||||
* else is given, and otherwise just pretend all is fine.
|
||||
*/
|
||||
if (strcmp(name, "ec_param_enc") == 0) {
|
||||
if (strcmp(value, "named_curve") == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
# endif
|
||||
|
||||
if (strcmp(name, "rsa_padding_mode") == 0)
|
||||
name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
|
||||
else if (strcmp(name, "rsa_mgf1_md") == 0)
|
||||
@@ -930,11 +994,19 @@ static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
|
||||
name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
|
||||
else if (strcmp(name, "rsa_pss_saltlen") == 0)
|
||||
name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
|
||||
else if (strcmp(name, "rsa_keygen_bits") == 0)
|
||||
name = OSSL_PKEY_PARAM_RSA_BITS;
|
||||
else if (strcmp(name, "rsa_keygen_pubexp") == 0)
|
||||
name = OSSL_PKEY_PARAM_RSA_E;
|
||||
else if (strcmp(name, "rsa_keygen_primes") == 0)
|
||||
name = OSSL_PKEY_PARAM_RSA_PRIMES;
|
||||
# ifndef OPENSSL_NO_DH
|
||||
else if (strcmp(name, "dh_pad") == 0)
|
||||
name = OSSL_EXCHANGE_PARAM_PAD;
|
||||
# endif
|
||||
# ifndef OPENSSL_NO_EC
|
||||
else if (strcmp(name, "ec_paramgen_curve") == 0)
|
||||
name = OSSL_PKEY_PARAM_EC_NAME;
|
||||
else if (strcmp(name, "ecdh_cofactor_mode") == 0)
|
||||
name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
|
||||
else if (strcmp(name, "ecdh_kdf_md") == 0)
|
||||
@@ -979,7 +1051,9 @@ int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
|| (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
|
||||
&& ctx->op.sig.sigprovctx != NULL)
|
||||
|| (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
|
||||
&& ctx->op.ciph.ciphprovctx != NULL))
|
||||
&& ctx->op.ciph.ciphprovctx != NULL)
|
||||
|| (EVP_PKEY_CTX_IS_GEN_OP(ctx)
|
||||
&& ctx->op.keymgmt.genctx != NULL))
|
||||
return legacy_ctrl_str_to_param(ctx, name, value);
|
||||
|
||||
if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
|
||||
|
||||
+27
-7
@@ -105,7 +105,6 @@ static void *evp_signature_from_dispatch(int name_id,
|
||||
break;
|
||||
signature->digest_sign_init
|
||||
= OSSL_get_OP_signature_digest_sign_init(fns);
|
||||
digsignfncnt++;
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
|
||||
if (signature->digest_sign_update != NULL)
|
||||
@@ -121,12 +120,17 @@ static void *evp_signature_from_dispatch(int name_id,
|
||||
= OSSL_get_OP_signature_digest_sign_final(fns);
|
||||
digsignfncnt++;
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_DIGEST_SIGN:
|
||||
if (signature->digest_sign != NULL)
|
||||
break;
|
||||
signature->digest_sign
|
||||
= OSSL_get_OP_signature_digest_sign(fns);
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
|
||||
if (signature->digest_verify_init != NULL)
|
||||
break;
|
||||
signature->digest_verify_init
|
||||
= OSSL_get_OP_signature_digest_verify_init(fns);
|
||||
digverifyfncnt++;
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
|
||||
if (signature->digest_verify_update != NULL)
|
||||
@@ -142,6 +146,12 @@ static void *evp_signature_from_dispatch(int name_id,
|
||||
= OSSL_get_OP_signature_digest_verify_final(fns);
|
||||
digverifyfncnt++;
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY:
|
||||
if (signature->digest_verify != NULL)
|
||||
break;
|
||||
signature->digest_verify
|
||||
= OSSL_get_OP_signature_digest_verify(fns);
|
||||
break;
|
||||
case OSSL_FUNC_SIGNATURE_FREECTX:
|
||||
if (signature->freectx != NULL)
|
||||
break;
|
||||
@@ -216,12 +226,20 @@ static void *evp_signature_from_dispatch(int name_id,
|
||||
&& verifyfncnt == 0
|
||||
&& verifyrecfncnt == 0
|
||||
&& digsignfncnt == 0
|
||||
&& digverifyfncnt == 0)
|
||||
&& digverifyfncnt == 0
|
||||
&& signature->digest_sign == NULL
|
||||
&& signature->digest_verify == NULL)
|
||||
|| (signfncnt != 0 && signfncnt != 2)
|
||||
|| (verifyfncnt != 0 && verifyfncnt != 2)
|
||||
|| (verifyrecfncnt != 0 && verifyrecfncnt != 2)
|
||||
|| (digsignfncnt != 0 && digsignfncnt != 3)
|
||||
|| (digverifyfncnt != 0 && digverifyfncnt != 3)
|
||||
|| (digsignfncnt != 0 && digsignfncnt != 2)
|
||||
|| (digsignfncnt == 2 && signature->digest_sign_init == NULL)
|
||||
|| (digverifyfncnt != 0 && digverifyfncnt != 2)
|
||||
|| (digverifyfncnt == 2 && signature->digest_verify_init == NULL)
|
||||
|| (signature->digest_sign != NULL
|
||||
&& signature->digest_sign_init == NULL)
|
||||
|| (signature->digest_verify != NULL
|
||||
&& signature->digest_verify_init == NULL)
|
||||
|| (gparamfncnt != 0 && gparamfncnt != 2)
|
||||
|| (sparamfncnt != 0 && sparamfncnt != 2)
|
||||
|| (gmdparamfncnt != 0 && gmdparamfncnt != 2)
|
||||
@@ -234,7 +252,9 @@ static void *evp_signature_from_dispatch(int name_id,
|
||||
* (verify_init verify) or
|
||||
* (verify_recover_init, verify_recover) or
|
||||
* (digest_sign_init, digest_sign_update, digest_sign_final) or
|
||||
* (digest_verify_init, digest_verify_update, digest_verify_final).
|
||||
* (digest_verify_init, digest_verify_update, digest_verify_final) or
|
||||
* (digest_sign_init, digest_sign) or
|
||||
* (digest_verify_init, digest_verify).
|
||||
*
|
||||
* set_ctx_params and settable_ctx_params are optional, but if one of
|
||||
* them is present then the other one must also be present. The same
|
||||
@@ -339,7 +359,7 @@ static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
|
||||
*/
|
||||
ERR_set_mark();
|
||||
|
||||
if (ctx->keytype == NULL)
|
||||
if (ctx->keymgmt == NULL)
|
||||
goto legacy;
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user