summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2019-09-04 12:46:02 +0100
committerMatt Caswell <matt@openssl.org>2019-09-09 14:00:00 +0100
commit9c45222ddc36124b8826d98dc0794f3eef1e5f0b (patch)
treeab9140d515d73f044944d4998244b047282ada0d /test
parent21fb7067228e39633755aeba251e925634e64870 (diff)
Revise EVP_PKEY param handling
We add new functions for getting parameters and discovering the gettable and settable parameters. We also make EVP_PKEY_CTX_get_signature_md() a function and implement it in terms of the new functions. This enables applications to discover the set of parameters that are supported for a given algorithm implementation. Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/9753)
Diffstat (limited to 'test')
-rw-r--r--test/evp_extra_test.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 1ad62d2f79..7b7c632dd1 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -19,6 +19,8 @@
#include <openssl/pem.h>
#include <openssl/kdf.h>
#include <openssl/provider.h>
+#include <openssl/core_names.h>
+#include <openssl/dsa.h>
#include "testutil.h"
#include "internal/nelem.h"
#include "internal/evp_int.h"
@@ -1393,6 +1395,117 @@ static int test_EVP_CIPHER_fetch(int tst)
return ret;
}
+/* Test getting and setting parameters on an EVP_PKEY_CTX */
+static int test_EVP_PKEY_CTX_get_set_params(void)
+{
+ EVP_PKEY_CTX *ctx = NULL;
+ EVP_SIGNATURE *dsaimpl = NULL;
+ const OSSL_PARAM *params;
+ OSSL_PARAM ourparams[2], *param = ourparams;
+ DSA *dsa = NULL;
+ BIGNUM *p = NULL, *q = NULL, *g = NULL;
+ EVP_PKEY *pkey = NULL;
+ int ret = 0;
+ const EVP_MD *md;
+ size_t mdsize = SHA512_DIGEST_LENGTH;
+
+ /*
+ * Setup the parameters for our DSA object. For our purposes they don't have
+ * to actually be *valid* parameters. We just need to set something. We
+ * don't even need a pub_key/priv_key.
+ */
+ dsa = DSA_new();
+ p = BN_new();
+ q = BN_new();
+ g = BN_new();
+ if (!TEST_ptr(dsa)
+ || !TEST_ptr(p)
+ || !TEST_ptr(q)
+ || !TEST_ptr(g)
+ || !DSA_set0_pqg(dsa, p, q, g))
+ goto err;
+ p = q = g = NULL;
+
+ pkey = EVP_PKEY_new();
+ if (!TEST_ptr(pkey)
+ || !TEST_true(EVP_PKEY_assign_DSA(pkey, dsa)))
+ goto err;
+
+ dsa = NULL;
+
+ /* Initialise a sign operation */
+ ctx = EVP_PKEY_CTX_new(pkey, NULL);
+ dsaimpl = EVP_SIGNATURE_fetch(NULL, "DSA", NULL);
+ if (!TEST_ptr(ctx)
+ || !TEST_ptr(dsaimpl)
+ || !TEST_int_gt(EVP_PKEY_sign_init_ex(ctx, dsaimpl), 0))
+ goto err;
+
+ /*
+ * We should be able to query the parameters now. The default DSA
+ * implementation supports exactly one parameter - so we expect to see that
+ * returned and no more.
+ */
+ params = EVP_PKEY_CTX_settable_params(ctx);
+ if (!TEST_ptr(params)
+ || !TEST_int_eq(strcmp(params[0].key,
+ OSSL_SIGNATURE_PARAM_DIGEST_SIZE), 0)
+ || !TEST_int_eq(strcmp(params[1].key, OSSL_SIGNATURE_PARAM_DIGEST),
+ 0)
+ /* The final key should be NULL */
+ || !TEST_ptr_null(params[2].key))
+ goto err;
+
+ /* Gettable params are the same as the settable ones */
+ params = EVP_PKEY_CTX_gettable_params(ctx);
+ if (!TEST_ptr(params)
+ || !TEST_int_eq(strcmp(params[0].key,
+ OSSL_SIGNATURE_PARAM_DIGEST_SIZE), 0)
+ || !TEST_int_eq(strcmp(params[1].key, OSSL_SIGNATURE_PARAM_DIGEST),
+ 0)
+ /* The final key should be NULL */
+ || !TEST_ptr_null(params[2].key))
+ goto err;
+
+ /*
+ * Test getting and setting params via EVP_PKEY_CTX_set_params() and
+ * EVP_PKEY_CTX_get_params()
+ */
+ *param++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
+ &mdsize);
+ *param++ = OSSL_PARAM_construct_end();
+
+ if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
+ goto err;
+
+ mdsize = 0;
+ if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
+ || !TEST_size_t_eq(mdsize, SHA512_DIGEST_LENGTH))
+ goto err;
+
+ /*
+ * Test the TEST_PKEY_CTX_set_signature_md() and
+ * TEST_PKEY_CTX_get_signature_md() functions
+ */
+ if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
+ || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
+ || !TEST_ptr_eq(md, EVP_sha256()))
+ goto err;
+
+ ret = 1;
+
+ err:
+ EVP_PKEY_CTX_free(ctx);
+ EVP_SIGNATURE_free(dsaimpl);
+ EVP_PKEY_free(pkey);
+ DSA_free(dsa);
+ BN_free(p);
+ BN_free(q);
+ BN_free(g);
+
+ return ret;
+}
+
int setup_tests(void)
{
ADD_TEST(test_EVP_DigestSignInit);
@@ -1429,5 +1542,6 @@ int setup_tests(void)
ADD_ALL_TESTS(test_EVP_MD_fetch, 5);
ADD_ALL_TESTS(test_EVP_CIPHER_fetch, 5);
#endif
+ ADD_TEST(test_EVP_PKEY_CTX_get_set_params);
return 1;
}