summaryrefslogtreecommitdiffstats
path: root/crypto/encode_decode
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2020-09-14 11:35:07 +0200
committerRichard Levitte <levitte@openssl.org>2020-09-20 17:31:54 +0200
commit48b62fb33aa0c5bce52b939fcd94780736491a5d (patch)
treec9faa14517c8073ae72737d5f2f6d1a118d15079 /crypto/encode_decode
parentae12eac074be92e14c11a36b90e1c95eca3723f1 (diff)
DECODER: Some cleanups, and aligning with OSSL_ENCODER
Mostly source nits, but also removing a couple of OSSL_DECODER_PARAM macros that are never used or even make sense. Also, some function names weren't quite consistent. They were made a bit more consistent in the OSSL_ENCODER API, now we bring that back to OSSL_DECODER. Reviewed-by: Shane Lontis <shane.lontis@oracle.com> (Merged from https://github.com/openssl/openssl/pull/12873)
Diffstat (limited to 'crypto/encode_decode')
-rw-r--r--crypto/encode_decode/decoder_lib.c60
-rw-r--r--crypto/encode_decode/decoder_meth.c32
-rw-r--r--crypto/encode_decode/decoder_pkey.c24
3 files changed, 57 insertions, 59 deletions
diff --git a/crypto/encode_decode/decoder_lib.c b/crypto/encode_decode/decoder_lib.c
index 9eeff20f7c..0bc772e43b 100644
--- a/crypto/encode_decode/decoder_lib.c
+++ b/crypto/encode_decode/decoder_lib.c
@@ -159,8 +159,7 @@ int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
return (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
}
-int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx,
- OSSL_DECODER *decoder)
+int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
{
OSSL_DECODER_INSTANCE *decoder_inst = NULL;
const OSSL_PROVIDER *prov = NULL;
@@ -246,7 +245,8 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
for (i = w_prev_start; i < w_prev_end; i++) {
OSSL_DECODER_INSTANCE *decoder_inst =
sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
- const char *name = decoder_inst->input_type;
+ const char *input_type =
+ OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
OSSL_DECODER *decoder = NULL;
/*
@@ -256,12 +256,11 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
* on top of this one, so we don't.
*/
if (ctx->start_input_type != NULL
- && strcasecmp(ctx->start_input_type,
- decoder_inst->input_type) != 0)
+ && strcasecmp(ctx->start_input_type, input_type) != 0)
continue;
ERR_set_mark();
- decoder = OSSL_DECODER_fetch(libctx, name, propq);
+ decoder = OSSL_DECODER_fetch(libctx, input_type, propq);
ERR_pop_to_mark();
if (decoder != NULL) {
@@ -308,7 +307,7 @@ int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
return 1;
}
-int OSSL_DECODER_CTX_num_decoders(OSSL_DECODER_CTX *ctx)
+int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
{
if (ctx == NULL || ctx->decoder_insts == NULL)
return 0;
@@ -375,6 +374,9 @@ int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
void *reference, size_t reference_sz,
OSSL_CALLBACK *export_cb, void *export_cbarg)
{
+ OSSL_DECODER *decoder = NULL;
+ void *decoderctx = NULL;
+
if (!(ossl_assert(decoder_inst != NULL)
&& ossl_assert(reference != NULL)
&& ossl_assert(export_cb != NULL)
@@ -383,25 +385,36 @@ int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
return 0;
}
- return decoder_inst->decoder->export_object(decoder_inst->decoderctx,
- reference, reference_sz,
- export_cb, export_cbarg);
+ decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
+ decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
+ return decoder->export_object(decoderctx, reference, reference_sz,
+ export_cb, export_cbarg);
}
-OSSL_DECODER *OSSL_DECODER_INSTANCE_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
+OSSL_DECODER *
+OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
{
if (decoder_inst == NULL)
return NULL;
return decoder_inst->decoder;
}
-void *OSSL_DECODER_INSTANCE_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
+void *
+OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
{
if (decoder_inst == NULL)
return NULL;
return decoder_inst->decoderctx;
}
+const char *
+OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
+{
+ if (decoder_inst == NULL)
+ return NULL;
+ return decoder_inst->input_type;
+}
+
static int decoder_process(const OSSL_PARAM params[], void *arg)
{
struct decoder_process_data_st *data = arg;
@@ -422,7 +435,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
/* First iteration, where we prepare for what is to come */
data->current_decoder_inst_index =
- OSSL_DECODER_CTX_num_decoders(ctx);
+ OSSL_DECODER_CTX_get_num_decoders(ctx);
bio = data->bio;
} else {
@@ -431,7 +444,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
decoder_inst =
sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
data->current_decoder_inst_index);
- decoder = OSSL_DECODER_INSTANCE_decoder(decoder_inst);
+ decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
if (ctx->construct != NULL
&& ctx->construct(decoder_inst, params, ctx->construct_data)) {
@@ -473,7 +486,11 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
OSSL_DECODER_INSTANCE *new_decoder_inst =
sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
OSSL_DECODER *new_decoder =
- OSSL_DECODER_INSTANCE_decoder(new_decoder_inst);
+ OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
+ void *new_decoderctx =
+ OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
+ const char *new_input_type =
+ OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
/*
* If |decoder| is NULL, it means we've just started, and the caller
@@ -481,18 +498,16 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
* that's the case, we do this extra check.
*/
if (decoder == NULL && ctx->start_input_type != NULL
- && strcasecmp(ctx->start_input_type,
- new_decoder_inst->input_type) != 0)
+ && strcasecmp(ctx->start_input_type, new_input_type) != 0)
continue;
/*
* If we have a previous decoder, we check that the input type
* of the next to be used matches the type of this previous one.
- * decoder_inst->input_type is a cache of the parameter "input-type"
- * value for that decoder.
+ * input_type is a cache of the parameter "input-type" value for
+ * that decoder.
*/
- if (decoder != NULL
- && !OSSL_DECODER_is_a(decoder, new_decoder_inst->input_type))
+ if (decoder != NULL && !OSSL_DECODER_is_a(decoder, new_input_type))
continue;
/*
@@ -511,8 +526,7 @@ static int decoder_process(const OSSL_PARAM params[], void *arg)
/* Recurse */
new_data.current_decoder_inst_index = i;
- ok = new_decoder->decode(new_decoder_inst->decoderctx,
- (OSSL_CORE_BIO *)bio,
+ ok = new_decoder->decode(new_decoderctx, (OSSL_CORE_BIO *)bio,
decoder_process, &new_data,
ossl_pw_passphrase_callback_dec,
&new_data.ctx->pwdata);
diff --git a/crypto/encode_decode/decoder_meth.c b/crypto/encode_decode/decoder_meth.c
index 41406df90f..37c6ab2b57 100644
--- a/crypto/encode_decode/decoder_meth.c
+++ b/crypto/encode_decode/decoder_meth.c
@@ -484,10 +484,8 @@ OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
{
OSSL_DECODER_CTX *ctx;
- if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
+ if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
- return NULL;
- }
return ctx;
}
@@ -506,42 +504,30 @@ int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
if (ctx->decoder_insts == NULL)
return 1;
- l = (size_t)sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
+ l = OSSL_DECODER_CTX_get_num_decoders(ctx);
for (i = 0; i < l; i++) {
OSSL_DECODER_INSTANCE *decoder_inst =
sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
+ OSSL_DECODER *decoder =
+ OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
+ OSSL_DECODER *decoderctx =
+ OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
- if (decoder_inst->decoderctx == NULL
- || decoder_inst->decoder->set_ctx_params == NULL)
+ if (decoderctx == NULL || decoder->set_ctx_params == NULL)
continue;
- if (!decoder_inst->decoder->set_ctx_params(decoder_inst->decoderctx,
- params))
+ if (!decoder->set_ctx_params(decoderctx, params))
return 0;
}
return 1;
}
-static void
-OSSL_DECODER_INSTANCE_free(OSSL_DECODER_INSTANCE *decoder_inst)
-{
- if (decoder_inst != NULL) {
- if (decoder_inst->decoder->freectx != NULL)
- decoder_inst->decoder->freectx(decoder_inst->decoderctx);
- decoder_inst->decoderctx = NULL;
- OSSL_DECODER_free(decoder_inst->decoder);
- decoder_inst->decoder = NULL;
- OPENSSL_free(decoder_inst);
- decoder_inst = NULL;
- }
-}
-
void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
{
if (ctx != NULL) {
if (ctx->cleanup != NULL)
ctx->cleanup(ctx->construct_data);
sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
- OSSL_DECODER_INSTANCE_free);
+ ossl_decoder_instance_free);
ossl_pw_clear_passphrase_data(&ctx->pwdata);
OPENSSL_free(ctx);
}
diff --git a/crypto/encode_decode/decoder_pkey.c b/crypto/encode_decode/decoder_pkey.c
index dfc7cccab1..2e07d0d7cc 100644
--- a/crypto/encode_decode/decoder_pkey.c
+++ b/crypto/encode_decode/decoder_pkey.c
@@ -62,9 +62,8 @@ static int decoder_construct_EVP_PKEY(OSSL_DECODER_INSTANCE *decoder_inst,
void *construct_data)
{
struct decoder_EVP_PKEY_data_st *data = construct_data;
- OSSL_DECODER *decoder =
- OSSL_DECODER_INSTANCE_decoder(decoder_inst);
- void *decoderctx = OSSL_DECODER_INSTANCE_decoder_ctx(decoder_inst);
+ OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
+ void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
size_t i, end_i;
/*
* |object_ref| points to a provider reference to an object, its exact
@@ -207,7 +206,7 @@ static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
if (!EVP_KEYMGMT_up_ref(keymgmt) /* ref++ */)
return;
if (sk_EVP_KEYMGMT_push(data->process_data->keymgmts, keymgmt) <= 0) {
- EVP_KEYMGMT_free(keymgmt); /* ref-- */
+ EVP_KEYMGMT_free(keymgmt); /* ref-- */
return;
}
@@ -301,17 +300,16 @@ int ossl_decoder_ctx_setup_for_EVP_PKEY(OSSL_DECODER_CTX *ctx,
if (data->error_occured)
goto err;
- /* If we found no decoders to match the keymgmts, we err */
- if (OSSL_DECODER_CTX_num_decoders(ctx) == 0)
- goto err;
+ if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
+ if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_EVP_PKEY)
+ || !OSSL_DECODER_CTX_set_construct_data(ctx, data->process_data)
+ || !OSSL_DECODER_CTX_set_cleanup(ctx,
+ decoder_clean_EVP_PKEY_construct_arg))
+ goto err;
- if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_EVP_PKEY)
- || !OSSL_DECODER_CTX_set_construct_data(ctx, data->process_data)
- || !OSSL_DECODER_CTX_set_cleanup(ctx,
- decoder_clean_EVP_PKEY_construct_arg))
- goto err;
+ data->process_data = NULL; /* Avoid it being freed */
+ }
- data->process_data = NULL;
ok = 1;
err:
if (data != NULL) {