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