Make the CT code library context aware
[openssl.git] / test / sslprovidertest.c
1 /*
2  * Copyright 2019 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 <string.h>
11 #include <openssl/provider.h>
12
13 #include "ssltestlib.h"
14 #include "testutil.h"
15
16 static char *cert = NULL;
17 static char *privkey = NULL;
18 static char *modulename = NULL;
19 static char *configfile = NULL;
20
21 static OSSL_PROVIDER *defctxlegacy = NULL;
22
23 static int test_different_libctx(void)
24 {
25     SSL_CTX *cctx = NULL, *sctx = NULL;
26     SSL *clientssl = NULL, *serverssl = NULL;
27     int testresult = 0;
28     OPENSSL_CTX *libctx = OPENSSL_CTX_new();
29     OSSL_PROVIDER *prov = NULL;
30
31     /*
32      * Verify that the default and fips providers in the default libctx are not
33      * available
34      */
35     if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
36             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
37         goto end;
38
39     if (!TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
40         goto end;
41
42     prov = OSSL_PROVIDER_load(libctx, modulename);
43     if (!TEST_ptr(prov)
44                /* Check we have the provider available */
45             || !TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
46         goto end;
47     /* Check the default provider is not available */
48     if (strcmp(modulename, "default") != 0
49             && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
50         goto end;
51     TEST_note("%s provider loaded", modulename);
52
53     cctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_client_method());
54     if (!TEST_ptr(cctx))
55         goto end;
56     sctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_server_method());
57     if (!TEST_ptr(sctx))
58         goto end;
59
60     /*
61      * TODO(3.0): Make this work in TLSv1.3. Currently we can only do RSA key
62      * exchange, because we don't have key gen/param gen for EC yet - which
63      * implies TLSv1.2 only
64      */
65     if (!TEST_true(create_ssl_ctx_pair(NULL,
66                                        NULL,
67                                        TLS1_VERSION,
68                                        TLS1_2_VERSION,
69                                        &sctx, &cctx, cert, privkey)))
70         goto end;
71
72     /* Ensure we use a FIPS compatible ciphersuite and sigalg */
73     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA256"))
74             || !TEST_true(SSL_CTX_set1_sigalgs_list(cctx, "RSA+SHA256")))
75         goto end;
76
77     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
78                                       NULL, NULL)))
79         goto end;
80
81     /* This time we expect success */
82     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
83         goto end;
84
85     /*
86      * Verify that the default and fips providers in the default libctx are
87      * still not available
88      */
89     if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
90             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
91         goto end;
92
93     testresult = 1;
94
95  end:
96     SSL_free(serverssl);
97     SSL_free(clientssl);
98     SSL_CTX_free(sctx);
99     SSL_CTX_free(cctx);
100
101     OSSL_PROVIDER_unload(prov);
102     OPENSSL_CTX_free(libctx);
103
104     return testresult;
105 }
106
107 int setup_tests(void)
108 {
109     char *certsdir = NULL;
110
111     if (!test_skip_common_options()) {
112         TEST_error("Error parsing test options\n");
113         return 0;
114     }
115
116     if (!TEST_ptr(certsdir = test_get_argument(0))
117             || !TEST_ptr(modulename = test_get_argument(1))
118             || !TEST_ptr(configfile = test_get_argument(2)))
119         return 0;
120
121     cert = test_mk_file_path(certsdir, "servercert.pem");
122     if (cert == NULL)
123         return 0;
124
125     privkey = test_mk_file_path(certsdir, "serverkey.pem");
126     if (privkey == NULL) {
127         OPENSSL_free(cert);
128         return 0;
129     }
130
131     /*
132      * For tests in this file we want to ensure the default ctx does not have
133      * the default provider loaded into the default ctx. So we load "legacy" to
134      * prevent default from being auto-loaded. This tests that there is no
135      * "leakage", i.e. when using SSL_CTX_new_with_libctx() we expect only the
136      * specific libctx to be used - nothing should fall back to the default
137      * libctx
138      */
139     defctxlegacy = OSSL_PROVIDER_load(NULL, "legacy");
140
141     ADD_TEST(test_different_libctx);
142
143     return 1;
144 }
145
146 void cleanup_tests(void)
147 {
148     OSSL_PROVIDER_unload(defctxlegacy);
149 }