Skip to content

Commit

Permalink
Adapt existing SERIALIZER implementations to the redesigned interface
Browse files Browse the repository at this point in the history
Reviewed-by: Matt Caswell <[email protected]>
Reviewed-by: Shane Lontis <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from openssl#11006)
  • Loading branch information
levitte committed Feb 7, 2020
1 parent 72ec964 commit 32b0645
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 181 deletions.
12 changes: 6 additions & 6 deletions providers/defltprov.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,19 @@ static const OSSL_ALGORITHM deflt_serializer[] = {
dh_priv_text_serializer_functions },
{ "DH", "default=yes,format=text,type=public",
dh_pub_text_serializer_functions },
{ "DH", "default=yes,format=text,type=domainparams",
{ "DH", "default=yes,format=text,type=parameters",
dh_param_text_serializer_functions },
{ "DH", "default=yes,format=der,type=private",
dh_priv_der_serializer_functions },
{ "DH", "default=yes,format=der,type=public",
dh_pub_der_serializer_functions },
{ "DH", "default=yes,format=der,type=domainparams",
{ "DH", "default=yes,format=der,type=parameters",
dh_param_der_serializer_functions },
{ "DH", "default=yes,format=pem,type=private",
dh_priv_pem_serializer_functions },
{ "DH", "default=yes,format=pem,type=public",
dh_pub_pem_serializer_functions },
{ "DH", "default=yes,format=pem,type=domainparams",
{ "DH", "default=yes,format=pem,type=parameters",
dh_param_pem_serializer_functions },
#endif

Expand All @@ -438,19 +438,19 @@ static const OSSL_ALGORITHM deflt_serializer[] = {
dsa_priv_text_serializer_functions },
{ "DSA", "default=yes,format=text,type=public",
dsa_pub_text_serializer_functions },
{ "DSA", "default=yes,format=text,type=domainparams",
{ "DSA", "default=yes,format=text,type=parameters",
dsa_param_text_serializer_functions },
{ "DSA", "default=yes,format=der,type=private",
dsa_priv_der_serializer_functions },
{ "DSA", "default=yes,format=der,type=public",
dsa_pub_der_serializer_functions },
{ "DSA", "default=yes,format=der,type=domainparams",
{ "DSA", "default=yes,format=der,type=parameters",
dsa_param_der_serializer_functions },
{ "DSA", "default=yes,format=pem,type=private",
dsa_priv_pem_serializer_functions },
{ "DSA", "default=yes,format=pem,type=public",
dsa_pub_pem_serializer_functions },
{ "DSA", "default=yes,format=pem,type=domainparams",
{ "DSA", "default=yes,format=pem,type=parameters",
dsa_param_pem_serializer_functions },
#endif

Expand Down
26 changes: 23 additions & 3 deletions providers/implementations/serializers/serializer_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,32 @@ static X509_PUBKEY *ossl_prov_pubkey_from_obj(const void *obj, int obj_nid,
return xpk;
}

OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_importkey(const OSSL_DISPATCH *fns)
OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
{
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORTKEY)
return OSSL_get_OP_keymgmt_importkey(fns);
if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
return OSSL_get_OP_keymgmt_new(fns);

return NULL;
}

OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
{
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
return OSSL_get_OP_keymgmt_free(fns);

return NULL;
}

OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
{
/* Pilfer the keymgmt dispatch table */
for (; fns->function_id != 0; fns++)
if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
return OSSL_get_OP_keymgmt_import(fns);

return NULL;
}
Expand Down
16 changes: 13 additions & 3 deletions providers/implementations/serializers/serializer_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@
#include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
#include "serializer_local.h"

OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dh_importkey(void)
OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
{
return ossl_prov_get_importkey(dh_keymgmt_functions);
return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
}

OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
{
return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
}

OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
{
return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
}

int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
Expand Down Expand Up @@ -51,7 +61,7 @@ int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
}

p = DH_get0_p(dh);
g = DH_get0_p(dh);
g = DH_get0_g(dh);
if (p == NULL || g == NULL)
goto null_err;

Expand Down
55 changes: 35 additions & 20 deletions providers/implementations/serializers/serializer_dh_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ static void dh_param_freectx(void *ctx)
static int dh_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_param_der(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_der(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand All @@ -66,17 +71,22 @@ static int dh_param_der(void *ctx, void *dh, BIO *out,

/* Public key : PEM */
static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_param_pem(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_pem(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand All @@ -88,17 +98,22 @@ static int dh_param_pem(void *ctx, void *dh, BIO *out,
}

static int dh_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_param_print(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_print(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand Down
62 changes: 37 additions & 25 deletions providers/implementations/serializers/serializer_dh_priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,22 @@ static int dh_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])

/* Private key : DER */
static int dh_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
struct dh_priv_ctx_st *ctx = vctx;
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx->provctx, params);
if (dh_import != NULL) {
DH *dh;

ok = dh_priv_der(ctx, dh, out, cb, cbarg);
DH_free(dh);
if ((dh = dh_new(ctx->provctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_priv_der(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand All @@ -147,24 +151,28 @@ static int dh_priv_der(void *vctx, void *dh, BIO *out,

/* Private key : PEM */
static int dh_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
struct dh_priv_ctx_st *ctx = vctx;
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params);
if (dh_import != NULL) {
DH *dh;

ok = dh_pem_priv(ctx->provctx, dh, out, cb, cbarg);
DH_free(dh);
if ((dh = dh_new(ctx->provctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_pem_priv(ctx->provctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}

static int dh_pem_priv(void *vctx, void *dh, BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
struct dh_priv_ctx_st *ctx = vctx;
int ret;
Expand Down Expand Up @@ -192,25 +200,29 @@ static void dh_print_freectx(void *ctx)
{
}

static int dh_priv_print_data(void *provctx, const OSSL_PARAM params[],
BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
static int dh_priv_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
struct dh_priv_ctx_st *ctx = vctx;
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(provctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_priv_print(provctx, dh, out, cb, cbarg);
DH_free(dh);
if ((dh = dh_new(ctx->provctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_priv_print(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}

static int dh_priv_print(void *ctx, void *dh, BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
return ossl_prov_print_dh(out, dh, dh_print_priv);
}
Expand Down
51 changes: 33 additions & 18 deletions providers/implementations/serializers/serializer_dh_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ static void dh_pub_freectx(void *ctx)
static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_pub_der(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_pub_der(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand All @@ -70,15 +75,20 @@ static int dh_pub_der(void *ctx, void *dh, BIO *out,
static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_pub_pem(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_pub_pem(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand All @@ -95,15 +105,20 @@ static int dh_pub_pem(void *ctx, void *dh, BIO *out,
static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
ossl_prov_get_dh_importkey();
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;

if (dh_importkey != NULL) {
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
if (dh_import != NULL) {
DH *dh;

ok = dh_pub_print(ctx, dh, out, cb, cbarg);
DH_free(dh);
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
&& dh_pub_print(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
Expand Down
Loading

0 comments on commit 32b0645

Please sign in to comment.