From 87e60f09aa8b253c38d457c3560680ba839a6cf2 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Wed, 9 Jun 2021 15:50:37 +0100 Subject: Add a test for the newly added OBJ upcalls Reviewed-by: Richard Levitte Reviewed-by: Tim Hudson Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/15681) --- test/build.info | 6 ++- test/recipes/04-test_upcalls.t | 19 +++++++ test/upcallstest.c | 112 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 test/recipes/04-test_upcalls.t create mode 100644 test/upcallstest.c diff --git a/test/build.info b/test/build.info index bc543bd761..eeb2845638 100644 --- a/test/build.info +++ b/test/build.info @@ -56,7 +56,7 @@ IF[{- !$disabled{tests} -}] sysdefaulttest errtest ssl_ctx_test gosttest \ context_internal_test aesgcmtest params_test evp_pkey_dparams_test \ keymgmt_internal_test hexstr_test provider_status_test defltfips_test \ - bio_readbuffer_test user_property_test pkcs7_test + bio_readbuffer_test user_property_test pkcs7_test upcallstest IF[{- !$disabled{'deprecated-3.0'} -}] PROGRAMS{noinst}=enginetest @@ -133,6 +133,10 @@ IF[{- !$disabled{tests} -}] INCLUDE[tls13ccstest]=../include ../apps/include DEPEND[tls13ccstest]=../libcrypto ../libssl libtestutil.a + SOURCE[upcallstest]=upcallstest.c + INCLUDE[upcallstest]=../include ../apps/include + DEPEND[upcallstest]=../libcrypto libtestutil.a + SOURCE[user_property_test]=user_property_test.c INCLUDE[user_property_test]=../include ../apps/include DEPEND[user_property_test]=../libcrypto libtestutil.a diff --git a/test/recipes/04-test_upcalls.t b/test/recipes/04-test_upcalls.t new file mode 100644 index 0000000000..6e5dbd329f --- /dev/null +++ b/test/recipes/04-test_upcalls.t @@ -0,0 +1,19 @@ +#! /usr/bin/env perl +# Copyright 2015-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 + + +use strict; +use warnings; + +use OpenSSL::Test::Simple; +use OpenSSL::Test; +use OpenSSL::Test::Utils; + +setup("test_upcalls"); + +simple_test("test_upcalls", "upcallstest"); diff --git a/test/upcallstest.c b/test/upcallstest.c new file mode 100644 index 0000000000..01e4e95237 --- /dev/null +++ b/test/upcallstest.c @@ -0,0 +1,112 @@ +/* + * Copyright 2021 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 +#include +#include +#include "testutil.h" + +static const OSSL_ALGORITHM *obj_query(void *provctx, int operation_id, + int *no_cache) +{ + *no_cache = 0; + return NULL; +} + +static const OSSL_DISPATCH obj_dispatch_table[] = { + { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))obj_query }, + { 0, NULL } +}; + +static OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid = NULL; +static OSSL_FUNC_core_obj_create_fn *c_obj_create = NULL; + +#define SIG_OID "1.3.6.1.4.1.16604.998877.1" +#define SIG_SN "my-sig" +#define SIG_LN "my-sig-long" +#define DIGEST_OID "1.3.6.1.4.1.16604.998877.2" +#define DIGEST_SN "my-digest" +#define DIGEST_LN "my-digest-long" +#define SIGALG_OID "1.3.6.1.4.1.16604.998877.3" +#define SIGALG_SN "my-sigalg" +#define SIGALG_LN "my-sigalg-long" + +static int obj_provider_init(const OSSL_CORE_HANDLE *handle, + const OSSL_DISPATCH *in, + const OSSL_DISPATCH **out, + void **provctx) +{ + *provctx = (void *)handle; + *out = obj_dispatch_table; + + for (; in->function_id != 0; in++) { + switch (in->function_id) { + case OSSL_FUNC_CORE_OBJ_ADD_SIGID: + c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in); + break; + case OSSL_FUNC_CORE_OBJ_CREATE: + c_obj_create = OSSL_FUNC_core_obj_create(in); + break; + break; + default: + /* Just ignore anything we don't understand */ + break; + } + } + + if (!c_obj_create(handle, DIGEST_OID, DIGEST_SN, DIGEST_LN) + || !c_obj_create(handle, SIG_OID, SIG_SN, SIG_LN) + || !c_obj_create(handle, SIGALG_OID, SIGALG_SN, SIGALG_LN)) + return 0; + + if (!c_obj_add_sigid(handle, SIGALG_OID, DIGEST_SN, SIG_LN)) + return 0; + + return 1; +} + +static int obj_create_test(void) +{ + OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); + OSSL_PROVIDER *objprov = NULL; + int sigalgnid, digestnid, signid; + int testresult = 0; + + if (!TEST_ptr(libctx)) + goto err; + + if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov", + obj_provider_init)) + || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov"))) + goto err; + + /* Check that the provider created the OIDs/NIDs we expected */ + sigalgnid = OBJ_txt2nid(SIGALG_OID); + if (!TEST_int_ne(sigalgnid, NID_undef) + || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid)) + || !TEST_int_ne(digestnid, NID_undef) + || !TEST_int_ne(signid, NID_undef) + || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN)) + || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN))) + goto err; + + testresult = 1; + err: + OSSL_PROVIDER_unload(objprov); + OSSL_LIB_CTX_free(libctx); + return testresult; +} + +int setup_tests(void) +{ + + ADD_TEST(obj_create_test); + + return 1; +} -- cgit v1.2.3