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