diff options
author | Richard Levitte <levitte@openssl.org> | 2020-08-16 21:25:08 +0200 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2020-08-21 09:23:58 +0200 |
commit | ece9304c96f71277ca95696d9bc49fdec51e9f17 (patch) | |
tree | 7038f8760e1538754bc67371cb5a466a83935dad /providers/implementations/encode_decode/encoder_dsa_pub.c | |
parent | f650993f1de3dbb5eda9009ad0c4895a7b1b7fe2 (diff) | |
download | openssl-new-ece9304c96f71277ca95696d9bc49fdec51e9f17.tar.gz |
Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
Fixes #12455
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12660)
Diffstat (limited to 'providers/implementations/encode_decode/encoder_dsa_pub.c')
-rw-r--r-- | providers/implementations/encode_decode/encoder_dsa_pub.c | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/providers/implementations/encode_decode/encoder_dsa_pub.c b/providers/implementations/encode_decode/encoder_dsa_pub.c new file mode 100644 index 0000000000..ca7dd4bf1e --- /dev/null +++ b/providers/implementations/encode_decode/encoder_dsa_pub.c @@ -0,0 +1,203 @@ +/* + * Copyright 2019-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 + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * DSA low level APIs are deprecated for public use, but still ok for + * internal use. + */ +#include "internal/deprecated.h" + +#include <openssl/core_dispatch.h> +#include <openssl/err.h> +#include <openssl/pem.h> +#include <openssl/dsa.h> +#include <openssl/types.h> +#include <openssl/params.h> +#include "prov/bio.h" +#include "prov/implementations.h" +#include "prov/provider_ctx.h" +#include "encoder_local.h" + +static OSSL_FUNC_encoder_newctx_fn dsa_pub_newctx; +static OSSL_FUNC_encoder_freectx_fn dsa_pub_freectx; +static OSSL_FUNC_encoder_encode_data_fn dsa_pub_der_data; +static OSSL_FUNC_encoder_encode_object_fn dsa_pub_der; +static OSSL_FUNC_encoder_encode_data_fn dsa_pub_pem_data; +static OSSL_FUNC_encoder_encode_object_fn dsa_pub_pem; + +static OSSL_FUNC_encoder_encode_data_fn dsa_pub_print_data; +static OSSL_FUNC_encoder_encode_object_fn dsa_pub_print; + +/* Public key : context */ + +/* + * There's no specific implementation context, so we use the provider context + */ +static void *dsa_pub_newctx(void *provctx) +{ + return provctx; +} + +static void dsa_pub_freectx(void *ctx) +{ +} + +/* Public key : DER */ +static int dsa_pub_der_data(void *ctx, const OSSL_PARAM params[], + OSSL_CORE_BIO *out, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); + OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); + OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); + int ok = 0; + + if (dsa_import != NULL) { + DSA *dsa; + + /* ctx == provctx */ + if ((dsa = dsa_new(ctx)) != NULL + && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) + && dsa_pub_der(ctx, dsa, out, cb, cbarg)) + ok = 1; + dsa_free(dsa); + } + return ok; +} + +static int dsa_pub_der(void *ctx, void *dsa, OSSL_CORE_BIO *cout, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + /* + * TODO(v3.0) implement setting save_parameters, see dsa_pub_encode() + * in crypto/dsa/dsa_ameth.c + */ + int save_parameters = 1; + BIO *out = bio_new_from_core_bio(ctx, cout); + int ret; + + if (out == NULL) + return 0; + + ret = + save_parameters + ? ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA, + ossl_prov_prepare_all_dsa_params, + ossl_prov_dsa_pub_to_der) + : ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA, + ossl_prov_prepare_dsa_params, + ossl_prov_dsa_pub_to_der); + + BIO_free(out); + + return ret; +} + +/* Public key : PEM */ +static int dsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], + OSSL_CORE_BIO *out, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); + OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); + OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); + int ok = 0; + + if (dsa_import != NULL) { + DSA *dsa; + + /* ctx == provctx */ + if ((dsa = dsa_new(ctx)) != NULL + && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) + && dsa_pub_pem(ctx, dsa, out, cb, cbarg)) + ok = 1; + dsa_free(dsa); + } + return ok; +} + +static int dsa_pub_pem(void *ctx, void *dsa, OSSL_CORE_BIO *cout, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + BIO *out = bio_new_from_core_bio(ctx, cout); + int ret; + + if (out == NULL) + return 0; + + ret = ossl_prov_write_pub_pem_from_obj(out, dsa, EVP_PKEY_DSA, + ossl_prov_prepare_dsa_params, + ossl_prov_dsa_pub_to_der); + + BIO_free(out); + + return ret; +} + +static int dsa_pub_print_data(void *ctx, const OSSL_PARAM params[], + OSSL_CORE_BIO *out, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); + OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); + OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); + int ok = 0; + + if (dsa_import != NULL) { + DSA *dsa; + + /* ctx == provctx */ + if ((dsa = dsa_new(ctx)) != NULL + && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) + && dsa_pub_print(ctx, dsa, out, cb, cbarg)) + ok = 1; + dsa_free(dsa); + } + return ok; +} + +static int dsa_pub_print(void *ctx, void *dsa, OSSL_CORE_BIO *cout, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) +{ + BIO *out = bio_new_from_core_bio(ctx, cout); + int ret; + + if (out == NULL) + return 0; + + ret = ossl_prov_print_dsa(out, dsa, 0); + BIO_free(out); + + return ret; +} + +const OSSL_DISPATCH dsa_pub_der_encoder_functions[] = { + { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_pub_newctx }, + { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_pub_freectx }, + { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dsa_pub_der_data }, + { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_pub_der }, + { 0, NULL } +}; + +const OSSL_DISPATCH dsa_pub_pem_encoder_functions[] = { + { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_pub_newctx }, + { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_pub_freectx }, + { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dsa_pub_pem_data }, + { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_pub_pem }, + { 0, NULL } +}; + +const OSSL_DISPATCH dsa_pub_text_encoder_functions[] = { + { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_pub_newctx }, + { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_pub_freectx }, + { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_pub_print }, + { OSSL_FUNC_ENCODER_ENCODE_DATA, + (void (*)(void))dsa_pub_print_data }, + { 0, NULL } +}; |