xref: /freebsd/crypto/openssl/test/user_property_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <openssl/core.h>
11*e0c4386eSCy Schubert #include <openssl/core_dispatch.h>
12*e0c4386eSCy Schubert #include <openssl/core_names.h>
13*e0c4386eSCy Schubert #include <openssl/provider.h>
14*e0c4386eSCy Schubert #include <openssl/crypto.h>
15*e0c4386eSCy Schubert #include <openssl/evp.h>
16*e0c4386eSCy Schubert #include "testutil.h"
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #define MYPROPERTIES "foo.bar=yes"
19*e0c4386eSCy Schubert 
20*e0c4386eSCy Schubert static OSSL_FUNC_provider_query_operation_fn testprov_query;
21*e0c4386eSCy Schubert static OSSL_FUNC_digest_get_params_fn tmpmd_get_params;
22*e0c4386eSCy Schubert static OSSL_FUNC_digest_digest_fn tmpmd_digest;
23*e0c4386eSCy Schubert 
tmpmd_get_params(OSSL_PARAM params[])24*e0c4386eSCy Schubert static int tmpmd_get_params(OSSL_PARAM params[])
25*e0c4386eSCy Schubert {
26*e0c4386eSCy Schubert     OSSL_PARAM *p = NULL;
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert     p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE);
29*e0c4386eSCy Schubert     if (p != NULL && !OSSL_PARAM_set_size_t(p, 1))
30*e0c4386eSCy Schubert         return 0;
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert     p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
33*e0c4386eSCy Schubert     if (p != NULL && !OSSL_PARAM_set_size_t(p, 1))
34*e0c4386eSCy Schubert         return 0;
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert     return 1;
37*e0c4386eSCy Schubert }
38*e0c4386eSCy Schubert 
tmpmd_digest(void * provctx,const unsigned char * in,size_t inl,unsigned char * out,size_t * outl,size_t outsz)39*e0c4386eSCy Schubert static int tmpmd_digest(void *provctx, const unsigned char *in, size_t inl,
40*e0c4386eSCy Schubert                  unsigned char *out, size_t *outl, size_t outsz)
41*e0c4386eSCy Schubert {
42*e0c4386eSCy Schubert     return 0;
43*e0c4386eSCy Schubert }
44*e0c4386eSCy Schubert 
45*e0c4386eSCy Schubert static const OSSL_DISPATCH testprovmd_functions[] = {
46*e0c4386eSCy Schubert     { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))tmpmd_get_params },
47*e0c4386eSCy Schubert     { OSSL_FUNC_DIGEST_DIGEST, (void (*)(void))tmpmd_digest },
48*e0c4386eSCy Schubert     { 0, NULL }
49*e0c4386eSCy Schubert };
50*e0c4386eSCy Schubert 
51*e0c4386eSCy Schubert static const OSSL_ALGORITHM testprov_digests[] = {
52*e0c4386eSCy Schubert     { "testprovmd", MYPROPERTIES, testprovmd_functions },
53*e0c4386eSCy Schubert     { NULL, NULL, NULL }
54*e0c4386eSCy Schubert };
55*e0c4386eSCy Schubert 
testprov_query(void * provctx,int operation_id,int * no_cache)56*e0c4386eSCy Schubert static const OSSL_ALGORITHM *testprov_query(void *provctx,
57*e0c4386eSCy Schubert                                           int operation_id,
58*e0c4386eSCy Schubert                                           int *no_cache)
59*e0c4386eSCy Schubert {
60*e0c4386eSCy Schubert     *no_cache = 0;
61*e0c4386eSCy Schubert     return operation_id == OSSL_OP_DIGEST ? testprov_digests : NULL;
62*e0c4386eSCy Schubert }
63*e0c4386eSCy Schubert 
64*e0c4386eSCy Schubert static const OSSL_DISPATCH testprov_dispatch_table[] = {
65*e0c4386eSCy Schubert     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))testprov_query },
66*e0c4386eSCy Schubert     { 0, NULL }
67*e0c4386eSCy Schubert };
68*e0c4386eSCy Schubert 
testprov_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)69*e0c4386eSCy Schubert static int testprov_provider_init(const OSSL_CORE_HANDLE *handle,
70*e0c4386eSCy Schubert                                   const OSSL_DISPATCH *in,
71*e0c4386eSCy Schubert                                   const OSSL_DISPATCH **out,
72*e0c4386eSCy Schubert                                   void **provctx)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert     *provctx = (void *)handle;
75*e0c4386eSCy Schubert     *out = testprov_dispatch_table;
76*e0c4386eSCy Schubert     return 1;
77*e0c4386eSCy Schubert }
78*e0c4386eSCy Schubert 
79*e0c4386eSCy Schubert enum {
80*e0c4386eSCy Schubert     DEFAULT_PROPS_FIRST = 0,
81*e0c4386eSCy Schubert     DEFAULT_PROPS_AFTER_LOAD,
82*e0c4386eSCy Schubert     DEFAULT_PROPS_AFTER_FETCH,
83*e0c4386eSCy Schubert     DEFAULT_PROPS_FINAL
84*e0c4386eSCy Schubert };
85*e0c4386eSCy Schubert 
test_default_props_and_providers(int propsorder)86*e0c4386eSCy Schubert static int test_default_props_and_providers(int propsorder)
87*e0c4386eSCy Schubert {
88*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx;
89*e0c4386eSCy Schubert     OSSL_PROVIDER *testprov = NULL;
90*e0c4386eSCy Schubert     EVP_MD *testprovmd = NULL;
91*e0c4386eSCy Schubert     int res = 0;
92*e0c4386eSCy Schubert 
93*e0c4386eSCy Schubert     if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())
94*e0c4386eSCy Schubert             || !TEST_true(OSSL_PROVIDER_add_builtin(libctx, "testprov",
95*e0c4386eSCy Schubert                                                     testprov_provider_init)))
96*e0c4386eSCy Schubert         goto err;
97*e0c4386eSCy Schubert 
98*e0c4386eSCy Schubert     if (propsorder == DEFAULT_PROPS_FIRST
99*e0c4386eSCy Schubert             && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
100*e0c4386eSCy Schubert         goto err;
101*e0c4386eSCy Schubert 
102*e0c4386eSCy Schubert     if (!TEST_ptr(testprov = OSSL_PROVIDER_load(libctx, "testprov")))
103*e0c4386eSCy Schubert         goto err;
104*e0c4386eSCy Schubert 
105*e0c4386eSCy Schubert     if (propsorder == DEFAULT_PROPS_AFTER_LOAD
106*e0c4386eSCy Schubert             && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
107*e0c4386eSCy Schubert         goto err;
108*e0c4386eSCy Schubert 
109*e0c4386eSCy Schubert     if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL)))
110*e0c4386eSCy Schubert         goto err;
111*e0c4386eSCy Schubert 
112*e0c4386eSCy Schubert     if (propsorder == DEFAULT_PROPS_AFTER_FETCH) {
113*e0c4386eSCy Schubert         if (!TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
114*e0c4386eSCy Schubert             goto err;
115*e0c4386eSCy Schubert         EVP_MD_free(testprovmd);
116*e0c4386eSCy Schubert         if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL)))
117*e0c4386eSCy Schubert             goto err;
118*e0c4386eSCy Schubert     }
119*e0c4386eSCy Schubert 
120*e0c4386eSCy Schubert     res = 1;
121*e0c4386eSCy Schubert  err:
122*e0c4386eSCy Schubert     EVP_MD_free(testprovmd);
123*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(testprov);
124*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
125*e0c4386eSCy Schubert     return res;
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert 
setup_tests(void)128*e0c4386eSCy Schubert int setup_tests(void)
129*e0c4386eSCy Schubert {
130*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_default_props_and_providers, DEFAULT_PROPS_FINAL);
131*e0c4386eSCy Schubert     return 1;
132*e0c4386eSCy Schubert }
133