2020-07-09 05:19:13 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-07-09 05:19:13 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/core_names.h>
|
CORE: Define provider-native abstract objects
This is placed as CORE because the core of libcrypto is the authority
for what is possible to do and what's required to make these abstract
objects work.
In essence, an abstract object is an OSSL_PARAM array with well
defined parameter keys and values:
- an object type, which is a number indicating what kind of
libcrypto structure the object in question can be used with. The
currently possible numbers are defined in <openssl/core_object.h>.
- an object data type, which is a string that indicates more closely
what the contents of the object are.
- the object data, an octet string. The exact encoding used depends
on the context in which it's used. For example, the decoder
sub-system accepts any encoding, as long as there is a decoder
implementation that takes that as input. If central code is to
handle the data directly, DER encoding is assumed. (*)
- an object reference, also an octet string. This octet string is
not the object contents, just a mere reference to a provider-native
object. (**)
- an object description, which is a human readable text string that
can be displayed if some software desires to do so.
The intent is that certain provider-native operations (called X
here) are able to return any sort of object that belong with other
operations, or an object that has no provider support otherwise.
(*) A future extension might be to be able to specify encoding.
(**) The possible mechanisms for dealing with object references are:
- An object loading function in the target operation. The exact
target operation is determined by the object type (for example,
OSSL_OBJECT_PKEY implies that the target operation is a KEYMGMT)
and the implementation to be fetched by its object data type (for
an OSSL_OBJECT_PKEY, that's the KEYMGMT keytype to be fetched).
This loading function is only useful for this if the implementations
that are involved (X and KEYMGMT, for example) are from the same
provider.
- An object exporter function in the operation X implementation.
That exporter function can be used to export the object data in
OSSL_PARAM form that can be imported by a target operation's
import function. This can be used when it's not possible to fetch
the target operation implementation from the same provider.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12512)
2020-07-22 21:34:25 +08:00
|
|
|
#include <openssl/core_object.h>
|
2020-10-26 20:17:42 +08:00
|
|
|
#include <openssl/provider.h>
|
2020-07-09 05:19:13 +08:00
|
|
|
#include <openssl/evp.h>
|
2020-07-10 21:13:55 +08:00
|
|
|
#include <openssl/ui.h>
|
2020-08-17 03:25:08 +08:00
|
|
|
#include <openssl/decoder.h>
|
2020-07-09 05:19:13 +08:00
|
|
|
#include <openssl/safestack.h>
|
2020-10-28 17:13:24 +08:00
|
|
|
#include <openssl/trace.h>
|
2020-07-09 05:19:13 +08:00
|
|
|
#include "crypto/evp.h"
|
2020-08-02 18:46:00 +08:00
|
|
|
#include "crypto/decoder.h"
|
2020-08-17 03:25:08 +08:00
|
|
|
#include "encoder_local.h"
|
2020-10-26 20:17:42 +08:00
|
|
|
#include "e_os.h" /* strcasecmp on Windows */
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
|
|
|
|
const unsigned char *kstr,
|
|
|
|
size_t klen)
|
2020-07-10 21:13:55 +08:00
|
|
|
{
|
2020-08-02 18:14:19 +08:00
|
|
|
return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
|
2020-07-10 21:13:55 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
|
|
|
|
const UI_METHOD *ui_method,
|
|
|
|
void *ui_data)
|
2020-07-10 21:13:55 +08:00
|
|
|
{
|
2020-08-02 18:14:19 +08:00
|
|
|
return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
|
2020-07-10 21:13:55 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
|
|
|
|
pem_password_cb *cb, void *cbarg)
|
2020-07-10 21:13:55 +08:00
|
|
|
{
|
2020-08-02 18:14:19 +08:00
|
|
|
return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
|
2020-07-10 21:13:55 +08:00
|
|
|
}
|
|
|
|
|
2020-08-02 20:29:33 +08:00
|
|
|
int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx,
|
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb,
|
|
|
|
void *cbarg)
|
|
|
|
{
|
|
|
|
return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
|
|
|
|
}
|
|
|
|
|
2020-07-09 05:19:13 +08:00
|
|
|
/*
|
2021-02-11 23:57:37 +08:00
|
|
|
* Support for OSSL_DECODER_CTX_new_for_pkey:
|
2020-07-28 00:39:58 +08:00
|
|
|
* The construct data, and collecting keymgmt information for it
|
2020-07-09 05:19:13 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEFINE_STACK_OF(EVP_KEYMGMT)
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
struct decoder_pkey_data_st {
|
2020-12-11 01:33:16 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
|
|
|
char *propq;
|
|
|
|
|
2020-07-09 05:19:13 +08:00
|
|
|
char *object_type; /* recorded object data type, may be NULL */
|
|
|
|
void **object; /* Where the result should end up */
|
|
|
|
};
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,
|
|
|
|
const OSSL_PARAM *params,
|
|
|
|
void *construct_data)
|
2020-07-09 05:19:13 +08:00
|
|
|
{
|
2021-02-11 23:57:37 +08:00
|
|
|
struct decoder_pkey_data_st *data = construct_data;
|
2020-09-14 17:35:07 +08:00
|
|
|
OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
|
|
|
|
void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
|
2020-12-11 01:33:16 +08:00
|
|
|
EVP_KEYMGMT *keymgmt = NULL;
|
2020-07-09 05:19:13 +08:00
|
|
|
/*
|
|
|
|
* |object_ref| points to a provider reference to an object, its exact
|
|
|
|
* contents entirely opaque to us, but may be passed to any provider
|
|
|
|
* function that expects this (such as OSSL_FUNC_keymgmt_load().
|
|
|
|
*
|
|
|
|
* This pointer is considered volatile, i.e. whatever it points at
|
|
|
|
* is assumed to be freed as soon as this function returns.
|
|
|
|
*/
|
|
|
|
void *object_ref = NULL;
|
|
|
|
size_t object_ref_sz = 0;
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
|
CORE: Define provider-native abstract objects
This is placed as CORE because the core of libcrypto is the authority
for what is possible to do and what's required to make these abstract
objects work.
In essence, an abstract object is an OSSL_PARAM array with well
defined parameter keys and values:
- an object type, which is a number indicating what kind of
libcrypto structure the object in question can be used with. The
currently possible numbers are defined in <openssl/core_object.h>.
- an object data type, which is a string that indicates more closely
what the contents of the object are.
- the object data, an octet string. The exact encoding used depends
on the context in which it's used. For example, the decoder
sub-system accepts any encoding, as long as there is a decoder
implementation that takes that as input. If central code is to
handle the data directly, DER encoding is assumed. (*)
- an object reference, also an octet string. This octet string is
not the object contents, just a mere reference to a provider-native
object. (**)
- an object description, which is a human readable text string that
can be displayed if some software desires to do so.
The intent is that certain provider-native operations (called X
here) are able to return any sort of object that belong with other
operations, or an object that has no provider support otherwise.
(*) A future extension might be to be able to specify encoding.
(**) The possible mechanisms for dealing with object references are:
- An object loading function in the target operation. The exact
target operation is determined by the object type (for example,
OSSL_OBJECT_PKEY implies that the target operation is a KEYMGMT)
and the implementation to be fetched by its object data type (for
an OSSL_OBJECT_PKEY, that's the KEYMGMT keytype to be fetched).
This loading function is only useful for this if the implementations
that are involved (X and KEYMGMT, for example) are from the same
provider.
- An object exporter function in the operation X implementation.
That exporter function can be used to export the object data in
OSSL_PARAM form that can be imported by a target operation's
import function. This can be used when it's not possible to fetch
the target operation implementation from the same provider.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12512)
2020-07-22 21:34:25 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
|
2020-07-09 05:19:13 +08:00
|
|
|
if (p != NULL) {
|
|
|
|
char *object_type = NULL;
|
|
|
|
|
|
|
|
if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
|
|
|
|
return 0;
|
|
|
|
OPENSSL_free(data->object_type);
|
|
|
|
data->object_type = object_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For stuff that should end up in an EVP_PKEY, we only accept an object
|
|
|
|
* reference for the moment. This enforces that the key data itself
|
|
|
|
* remains with the provider.
|
|
|
|
*/
|
CORE: Define provider-native abstract objects
This is placed as CORE because the core of libcrypto is the authority
for what is possible to do and what's required to make these abstract
objects work.
In essence, an abstract object is an OSSL_PARAM array with well
defined parameter keys and values:
- an object type, which is a number indicating what kind of
libcrypto structure the object in question can be used with. The
currently possible numbers are defined in <openssl/core_object.h>.
- an object data type, which is a string that indicates more closely
what the contents of the object are.
- the object data, an octet string. The exact encoding used depends
on the context in which it's used. For example, the decoder
sub-system accepts any encoding, as long as there is a decoder
implementation that takes that as input. If central code is to
handle the data directly, DER encoding is assumed. (*)
- an object reference, also an octet string. This octet string is
not the object contents, just a mere reference to a provider-native
object. (**)
- an object description, which is a human readable text string that
can be displayed if some software desires to do so.
The intent is that certain provider-native operations (called X
here) are able to return any sort of object that belong with other
operations, or an object that has no provider support otherwise.
(*) A future extension might be to be able to specify encoding.
(**) The possible mechanisms for dealing with object references are:
- An object loading function in the target operation. The exact
target operation is determined by the object type (for example,
OSSL_OBJECT_PKEY implies that the target operation is a KEYMGMT)
and the implementation to be fetched by its object data type (for
an OSSL_OBJECT_PKEY, that's the KEYMGMT keytype to be fetched).
This loading function is only useful for this if the implementations
that are involved (X and KEYMGMT, for example) are from the same
provider.
- An object exporter function in the operation X implementation.
That exporter function can be used to export the object data in
OSSL_PARAM form that can be imported by a target operation's
import function. This can be used when it's not possible to fetch
the target operation implementation from the same provider.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12512)
2020-07-22 21:34:25 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
|
2020-07-09 05:19:13 +08:00
|
|
|
if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
|
|
|
|
return 0;
|
|
|
|
object_ref = p->data;
|
|
|
|
object_ref_sz = p->data_size;
|
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
keymgmt = EVP_KEYMGMT_fetch(data->libctx, data->object_type, data->propq);
|
|
|
|
|
|
|
|
if (keymgmt != NULL) {
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
void *keydata = NULL;
|
|
|
|
const OSSL_PROVIDER *keymgmt_prov = EVP_KEYMGMT_provider(keymgmt);
|
|
|
|
const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_provider(decoder);
|
2020-07-09 05:19:13 +08:00
|
|
|
|
|
|
|
/*
|
2020-12-11 01:33:16 +08:00
|
|
|
* If the EVP_KEYMGMT and the OSSL_DECODER are from the
|
|
|
|
* same provider, we assume that the KEYMGMT has a key loading
|
|
|
|
* function that can handle the provider reference we hold.
|
2020-07-09 05:19:13 +08:00
|
|
|
*
|
2020-12-11 01:33:16 +08:00
|
|
|
* Otherwise, we export from the decoder and import the
|
|
|
|
* result in the keymgmt.
|
2020-07-09 05:19:13 +08:00
|
|
|
*/
|
2020-12-11 01:33:16 +08:00
|
|
|
if (keymgmt_prov == decoder_prov) {
|
|
|
|
keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
|
|
|
|
} else {
|
|
|
|
struct evp_keymgmt_util_try_import_data_st import_data;
|
|
|
|
|
|
|
|
import_data.keymgmt = keymgmt;
|
|
|
|
import_data.keydata = NULL;
|
|
|
|
import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
|
|
|
/*
|
2020-12-11 01:33:16 +08:00
|
|
|
* No need to check for errors here, the value of
|
|
|
|
* |import_data.keydata| is as much an indicator.
|
2020-07-09 05:19:13 +08:00
|
|
|
*/
|
2020-12-11 01:33:16 +08:00
|
|
|
(void)decoder->export_object(decoderctx,
|
|
|
|
object_ref, object_ref_sz,
|
|
|
|
&evp_keymgmt_util_try_import,
|
|
|
|
&import_data);
|
|
|
|
keydata = import_data.keydata;
|
|
|
|
import_data.keydata = NULL;
|
2020-07-09 05:19:13 +08:00
|
|
|
}
|
2020-12-11 01:33:16 +08:00
|
|
|
|
|
|
|
if (keydata != NULL
|
|
|
|
&& (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
|
|
|
|
evp_keymgmt_freedata(keymgmt, keydata);
|
|
|
|
|
|
|
|
*data->object = pkey;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* evp_keymgmt_util_make_pkey() increments the reference count when
|
|
|
|
* assigning the EVP_PKEY, so we can free the keymgmt here.
|
|
|
|
*/
|
|
|
|
EVP_KEYMGMT_free(keymgmt);
|
2020-07-09 05:19:13 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We successfully looked through, |*ctx->object| determines if we
|
|
|
|
* actually found something.
|
|
|
|
*/
|
|
|
|
return (*data->object != NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
static void decoder_clean_pkey_construct_arg(void *construct_data)
|
2020-07-09 05:19:13 +08:00
|
|
|
{
|
2021-02-11 23:57:37 +08:00
|
|
|
struct decoder_pkey_data_st *data = construct_data;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-07-28 03:51:44 +08:00
|
|
|
if (data != NULL) {
|
2020-12-11 01:33:16 +08:00
|
|
|
OPENSSL_free(data->propq);
|
2020-07-28 03:51:44 +08:00
|
|
|
OPENSSL_free(data->object_type);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
}
|
2020-07-09 05:19:13 +08:00
|
|
|
}
|
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
static void collect_name(const char *name, void *arg)
|
|
|
|
{
|
|
|
|
STACK_OF(OPENSSL_CSTRING) *names = arg;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
sk_OPENSSL_CSTRING_push(names, name);
|
|
|
|
}
|
2020-07-09 05:19:13 +08:00
|
|
|
|
|
|
|
static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
|
|
|
|
{
|
2020-12-11 01:33:16 +08:00
|
|
|
STACK_OF(EVP_KEYMGMT) *keymgmts = arg;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
|
|
|
if (!EVP_KEYMGMT_up_ref(keymgmt) /* ref++ */)
|
|
|
|
return;
|
2020-12-11 01:33:16 +08:00
|
|
|
if (sk_EVP_KEYMGMT_push(keymgmts, keymgmt) <= 0) {
|
2020-09-14 17:35:07 +08:00
|
|
|
EVP_KEYMGMT_free(keymgmt); /* ref-- */
|
2020-07-09 05:19:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:17:42 +08:00
|
|
|
/*
|
|
|
|
* The input structure check is only done on the initial decoder
|
|
|
|
* implementations.
|
|
|
|
*/
|
2020-12-11 01:33:16 +08:00
|
|
|
static int decoder_check_input_structure(OSSL_DECODER_CTX *ctx,
|
|
|
|
OSSL_DECODER_INSTANCE *di)
|
2020-10-26 20:17:42 +08:00
|
|
|
{
|
|
|
|
int di_is_was_set = 0;
|
|
|
|
const char *di_is =
|
|
|
|
OSSL_DECODER_INSTANCE_get_input_structure(di, &di_is_was_set);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If caller didn't give an input structure name, the decoder is accepted
|
|
|
|
* unconditionally with regards to the input structure.
|
|
|
|
*/
|
|
|
|
if (ctx->input_structure == NULL)
|
|
|
|
return 1;
|
|
|
|
/*
|
|
|
|
* If the caller did give an input structure name, the decoder must have
|
|
|
|
* a matching input structure to be accepted.
|
|
|
|
*/
|
2020-12-11 01:33:16 +08:00
|
|
|
if (di_is != NULL && strcasecmp(ctx->input_structure, di_is) == 0)
|
2020-10-26 20:17:42 +08:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
struct collect_decoder_data_st {
|
|
|
|
STACK_OF(OPENSSL_CSTRING) *names;
|
|
|
|
OSSL_DECODER_CTX *ctx;
|
|
|
|
|
2021-03-03 01:15:32 +08:00
|
|
|
unsigned int error_occurred:1;
|
2020-12-11 01:33:16 +08:00
|
|
|
};
|
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
static void collect_decoder(OSSL_DECODER *decoder, void *arg)
|
2020-08-04 03:04:05 +08:00
|
|
|
{
|
2020-12-11 01:33:16 +08:00
|
|
|
struct collect_decoder_data_st *data = arg;
|
2020-08-04 03:04:05 +08:00
|
|
|
size_t i, end_i;
|
2020-10-26 20:17:42 +08:00
|
|
|
const OSSL_PROVIDER *prov = OSSL_DECODER_provider(decoder);
|
|
|
|
void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
|
2020-08-04 03:04:05 +08:00
|
|
|
|
2021-03-03 01:15:32 +08:00
|
|
|
if (data->error_occurred)
|
2020-08-04 03:04:05 +08:00
|
|
|
return;
|
|
|
|
|
2021-03-03 01:15:32 +08:00
|
|
|
data->error_occurred = 1; /* Assume the worst */
|
2020-08-11 13:17:17 +08:00
|
|
|
if (data->names == NULL)
|
|
|
|
return;
|
2020-08-04 03:04:05 +08:00
|
|
|
|
|
|
|
end_i = sk_OPENSSL_CSTRING_num(data->names);
|
|
|
|
for (i = 0; i < end_i; i++) {
|
|
|
|
const char *name = sk_OPENSSL_CSTRING_value(data->names, i);
|
2020-11-23 00:50:53 +08:00
|
|
|
void *decoderctx = NULL;
|
2020-10-26 20:17:42 +08:00
|
|
|
OSSL_DECODER_INSTANCE *di = NULL;
|
|
|
|
|
|
|
|
if (OSSL_DECODER_is_a(decoder, name)
|
|
|
|
/*
|
|
|
|
* Either the caller didn't give a selection, or if they did,
|
|
|
|
* the decoder must tell us if it supports that selection to
|
|
|
|
* be accepted. If the decoder doesn't have |does_selection|,
|
|
|
|
* it's seen as taking anything.
|
|
|
|
*/
|
|
|
|
&& (decoder->does_selection == NULL
|
|
|
|
|| decoder->does_selection(provctx, data->ctx->selection))
|
|
|
|
&& (decoderctx = decoder->newctx(provctx)) != NULL
|
|
|
|
&& (di = ossl_decoder_instance_new(decoder, decoderctx)) != NULL) {
|
|
|
|
/* If successful so far, don't free these directly */
|
|
|
|
decoderctx = NULL;
|
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
if (decoder_check_input_structure(data->ctx, di)
|
2020-10-26 20:17:42 +08:00
|
|
|
&& ossl_decoder_ctx_add_decoder_inst(data->ctx, di))
|
|
|
|
di = NULL; /* If successfully added, don't free it */
|
|
|
|
}
|
2020-08-04 03:04:05 +08:00
|
|
|
|
2020-10-26 20:17:42 +08:00
|
|
|
/* Free what can be freed */
|
|
|
|
ossl_decoder_instance_free(di);
|
|
|
|
decoder->freectx(decoderctx);
|
2020-08-04 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2021-03-03 01:15:32 +08:00
|
|
|
data->error_occurred = 0; /* All is good now */
|
2020-08-04 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
|
|
|
|
EVP_PKEY **pkey, const char *keytype,
|
|
|
|
OSSL_LIB_CTX *libctx,
|
|
|
|
const char *propquery)
|
2020-07-09 05:19:13 +08:00
|
|
|
{
|
2021-02-11 23:57:37 +08:00
|
|
|
struct decoder_pkey_data_st *process_data = NULL;
|
2020-12-11 01:33:16 +08:00
|
|
|
STACK_OF(EVP_KEYMGMT) *keymgmts = NULL;
|
|
|
|
STACK_OF(OPENSSL_CSTRING) *names = NULL;
|
2020-08-02 18:46:00 +08:00
|
|
|
int ok = 0;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL
|
|
|
|
|| (propquery != NULL
|
|
|
|
&& (process_data->propq = OPENSSL_strdup(propquery)) == NULL)
|
|
|
|
|| (keymgmts = sk_EVP_KEYMGMT_new_null()) == NULL
|
|
|
|
|| (names = sk_OPENSSL_CSTRING_new_null()) == NULL) {
|
2020-08-17 03:25:08 +08:00
|
|
|
ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
|
2020-07-09 05:19:13 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
process_data->object = (void **)pkey;
|
|
|
|
process_data->libctx = libctx;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
/* First, find all keymgmts to form goals */
|
|
|
|
EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, keymgmts);
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-08-04 03:04:05 +08:00
|
|
|
/* Then, we collect all the keymgmt names */
|
2020-12-11 01:33:16 +08:00
|
|
|
while (sk_EVP_KEYMGMT_num(keymgmts) > 0) {
|
|
|
|
EVP_KEYMGMT *keymgmt = sk_EVP_KEYMGMT_shift(keymgmts);
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
/*
|
|
|
|
* If the key type is given by the caller, we only use the matching
|
|
|
|
* KEYMGMTs, otherwise we use them all.
|
|
|
|
*/
|
2021-02-20 01:03:43 +08:00
|
|
|
if (keytype == NULL || EVP_KEYMGMT_is_a(keymgmt, keytype)) {
|
|
|
|
if (!EVP_KEYMGMT_names_do_all(keymgmt, collect_name, names)) {
|
|
|
|
ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
EVP_KEYMGMT_free(keymgmt);
|
2020-08-04 03:04:05 +08:00
|
|
|
}
|
2020-12-11 01:33:16 +08:00
|
|
|
sk_EVP_KEYMGMT_free(keymgmts);
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-08-04 03:04:05 +08:00
|
|
|
/*
|
2020-08-17 03:25:08 +08:00
|
|
|
* Finally, find all decoders that have any keymgmt of the collected
|
2020-08-04 03:04:05 +08:00
|
|
|
* keymgmt names
|
|
|
|
*/
|
2020-12-11 01:33:16 +08:00
|
|
|
{
|
|
|
|
struct collect_decoder_data_st collect_decoder_data = { NULL, };
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
collect_decoder_data.names = names;
|
|
|
|
collect_decoder_data.ctx = ctx;
|
|
|
|
OSSL_DECODER_do_all_provided(libctx,
|
|
|
|
collect_decoder, &collect_decoder_data);
|
|
|
|
sk_OPENSSL_CSTRING_free(names);
|
|
|
|
|
2021-03-03 01:15:32 +08:00
|
|
|
if (collect_decoder_data.error_occurred)
|
2020-12-11 01:33:16 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-09-14 17:35:07 +08:00
|
|
|
if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
|
2021-02-11 23:57:37 +08:00
|
|
|
if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
|
2020-12-11 01:33:16 +08:00
|
|
|
|| !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
|
2020-09-14 17:35:07 +08:00
|
|
|
|| !OSSL_DECODER_CTX_set_cleanup(ctx,
|
2021-02-11 23:57:37 +08:00
|
|
|
decoder_clean_pkey_construct_arg))
|
2020-09-14 17:35:07 +08:00
|
|
|
goto err;
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-12-11 01:33:16 +08:00
|
|
|
process_data = NULL; /* Avoid it being freed */
|
2020-09-14 17:35:07 +08:00
|
|
|
}
|
2020-07-09 05:19:13 +08:00
|
|
|
|
2020-08-02 18:46:00 +08:00
|
|
|
ok = 1;
|
2020-07-09 05:19:13 +08:00
|
|
|
err:
|
2021-02-11 23:57:37 +08:00
|
|
|
decoder_clean_pkey_construct_arg(process_data);
|
2020-08-02 18:46:00 +08:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:21:51 +08:00
|
|
|
OSSL_DECODER_CTX *
|
2021-02-11 23:57:37 +08:00
|
|
|
OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
|
|
|
|
const char *input_type,
|
|
|
|
const char *input_structure,
|
|
|
|
const char *keytype, int selection,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propquery)
|
2020-08-02 18:46:00 +08:00
|
|
|
{
|
|
|
|
OSSL_DECODER_CTX *ctx = NULL;
|
|
|
|
|
|
|
|
if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-28 17:13:24 +08:00
|
|
|
|
|
|
|
OSSL_TRACE_BEGIN(DECODER) {
|
|
|
|
BIO_printf(trc_out,
|
|
|
|
"(ctx %p) Looking for %s decoders with selection %d\n",
|
|
|
|
(void *)ctx, keytype, selection);
|
|
|
|
BIO_printf(trc_out, " input type: %s, input structure: %s\n",
|
|
|
|
input_type, input_structure);
|
|
|
|
} OSSL_TRACE_END(DECODER);
|
|
|
|
|
2020-08-02 18:46:00 +08:00
|
|
|
if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
|
2020-10-26 20:17:42 +08:00
|
|
|
&& OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
|
|
|
|
&& OSSL_DECODER_CTX_set_selection(ctx, selection)
|
2021-02-11 23:57:37 +08:00
|
|
|
&& ossl_decoder_ctx_setup_for_pkey(ctx, pkey, keytype,
|
|
|
|
libctx, propquery)
|
2020-10-28 17:13:24 +08:00
|
|
|
&& OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)) {
|
|
|
|
OSSL_TRACE_BEGIN(DECODER) {
|
|
|
|
BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
|
|
|
|
(void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
|
|
|
|
} OSSL_TRACE_END(DECODER);
|
2020-08-02 18:46:00 +08:00
|
|
|
return ctx;
|
2020-10-28 17:13:24 +08:00
|
|
|
}
|
2020-08-02 18:46:00 +08:00
|
|
|
|
|
|
|
OSSL_DECODER_CTX_free(ctx);
|
|
|
|
return NULL;
|
2020-07-09 05:19:13 +08:00
|
|
|
}
|