Adjust ssl_test_new for SHA1 security level
[openssl.git] / test / provider_test.c
1 /*
2  * Copyright 2019-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 <stddef.h>
11 #include <openssl/provider.h>
12 #include "testutil.h"
13
14 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
15
16 static char buf[256];
17 static OSSL_PARAM greeting_request[] = {
18     { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf) },
19     { NULL, 0, NULL, 0, 0 }
20 };
21
22 static int test_provider(OSSL_LIB_CTX **libctx, const char *name)
23 {
24     OSSL_PROVIDER *prov = NULL;
25     const char *greeting = NULL;
26     char expected_greeting[256];
27     int ok = 0;
28     long err;
29
30     BIO_snprintf(expected_greeting, sizeof(expected_greeting),
31                  "Hello OpenSSL %.20s, greetings from %s!",
32                  OPENSSL_VERSION_STR, name);
33
34     if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name))
35             || !TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
36             || !TEST_ptr(greeting = greeting_request[0].data)
37             || !TEST_size_t_gt(greeting_request[0].data_size, 0)
38             || !TEST_str_eq(greeting, expected_greeting)
39             || !TEST_true(OSSL_PROVIDER_unload(prov)))
40         goto err;
41
42     prov = NULL;
43
44     /*
45      * We must free the libctx to force the provider to really be unloaded from
46      * memory
47      */
48     OSSL_LIB_CTX_free(*libctx);
49     *libctx = NULL;
50
51     /* Make sure we got the error we were expecting */
52     err = ERR_peek_last_error();
53     if (!TEST_int_gt(err, 0)
54             || !TEST_int_eq(ERR_GET_REASON(err), 1))
55         goto err;
56
57     /* We print out all the data to make sure it can still be accessed */
58     ERR_print_errors_fp(stderr);
59     ok = 1;
60  err:
61     OSSL_PROVIDER_unload(prov);
62     OSSL_LIB_CTX_free(*libctx);
63     *libctx = NULL;
64     return ok;
65 }
66
67 static int test_builtin_provider(void)
68 {
69     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
70     const char *name = "p_test_builtin";
71     int ok;
72
73     ok =
74         TEST_ptr(libctx)
75         && TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
76                                                PROVIDER_INIT_FUNCTION_NAME))
77         && test_provider(&libctx, name);
78
79     OSSL_LIB_CTX_free(libctx);
80
81     return ok;
82 }
83
84 #ifndef NO_PROVIDER_MODULE
85 static int test_loaded_provider(void)
86 {
87     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
88     const char *name = "p_test";
89
90     if (!TEST_ptr(libctx))
91         return 0;
92
93     /* test_provider will free libctx as part of the test */
94     return test_provider(&libctx, name);
95 }
96 #endif
97
98 int setup_tests(void)
99 {
100     ADD_TEST(test_builtin_provider);
101 #ifndef NO_PROVIDER_MODULE
102     ADD_TEST(test_loaded_provider);
103 #endif
104     return 1;
105 }
106