diff options
author | Shane Lontis <shane.lontis@oracle.com> | 2019-05-27 21:52:37 +1000 |
---|---|---|
committer | Shane Lontis <shane.lontis@oracle.com> | 2019-05-27 21:55:10 +1000 |
commit | 6aa2e59e1c52761cc5ad60170106118d7c1aa090 (patch) | |
tree | 08a30dc60ca00a1c70ae5ed18d6c4888036e0e56 /test/evp_pkey_dparams_test.c | |
parent | 324954640e7fcb2b4a26cb5ae7923a6e5e79ee14 (diff) |
Add d2i_KeyParams/i2d_KeyParams API's.
Convert EVP_PKEY Parameters to/from binary.
This wraps the low level i2d/d2i calls for DH,DSA and EC key parameters
in a similar way to Public and Private Keys.
The API's can be used by applications (including openssl apps) that only
want to use EVP_PKEY without needing to access low level key API's.
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/8903)
Diffstat (limited to 'test/evp_pkey_dparams_test.c')
-rw-r--r-- | test/evp_pkey_dparams_test.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/evp_pkey_dparams_test.c b/test/evp_pkey_dparams_test.c new file mode 100644 index 0000000000..30da6ed8b1 --- /dev/null +++ b/test/evp_pkey_dparams_test.c @@ -0,0 +1,73 @@ +/* + * Copyright 2019 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 + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "internal/nelem.h" +#include <openssl/crypto.h> +#include <openssl/bio.h> +#include <openssl/bn.h> +#include <openssl/rand.h> +#include <openssl/err.h> +#include <openssl/evp.h> +#include <openssl/ec.h> +#include "testutil.h" + +static int pkey_param_types[] = { +#ifndef OPENSSL_NO_DH + EVP_PKEY_DH, +#endif +#ifndef OPENSSL_NO_DSA + EVP_PKEY_DSA, +#endif +#ifndef OPENSSL_NO_EC + EVP_PKEY_EC +#endif +}; + +#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC) + +static int params_bio_test(int id) +{ + int ret; + BIO *mem_bio = NULL; + EVP_PKEY_CTX *ctx = NULL; + EVP_PKEY *params_key = NULL, *out_key = NULL; + int type = pkey_param_types[id]; + + ret = + TEST_ptr(mem_bio = BIO_new(BIO_s_mem())) + && TEST_ptr(ctx = EVP_PKEY_CTX_new_id(type, NULL)) + && TEST_int_gt(EVP_PKEY_paramgen_init(ctx), 0) + && (type != EVP_PKEY_EC + || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_secp256k1) > 0) + && TEST_int_gt(EVP_PKEY_paramgen(ctx, ¶ms_key), 0) + && TEST_int_gt(i2d_KeyParams_bio(mem_bio, params_key), 0) + && TEST_ptr(d2i_KeyParams_bio(type, &out_key, mem_bio)) + && TEST_int_gt(EVP_PKEY_cmp_parameters(out_key, params_key), 0); + + BIO_free(mem_bio); + EVP_PKEY_CTX_free(ctx); + EVP_PKEY_free(params_key); + EVP_PKEY_free(out_key); + return ret; +} +#endif + +int setup_tests(void) +{ +#if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC) + TEST_note("No DH/DSA/EC support"); +#else + ADD_ALL_TESTS(params_bio_test, OSSL_NELEM(pkey_param_types)); +#endif + return 1; +} |