eng_devcrypto: add cipher CTX copy function
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/opensslv.h>
14 #include <openssl/ssl.h>
15 #include <openssl/ossl_typ.h>
16 #include "internal/dso_conf.h"
17 #include "testutil.h"
18
19 typedef void DSO;
20
21 typedef const SSL_METHOD * (*TLS_method_t)(void);
22 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
23 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
24 typedef unsigned long (*ERR_get_error_t)(void);
25 typedef unsigned long (*OpenSSL_version_num_t)(void);
26 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
27 typedef int (*DSO_free_t)(DSO *dso);
28
29 typedef enum test_types_en {
30     CRYPTO_FIRST,
31     SSL_FIRST,
32     JUST_CRYPTO,
33     DSO_REFTEST
34 } TEST_TYPE;
35
36 static TEST_TYPE test_type;
37 static const char *path_crypto;
38 static const char *path_ssl;
39
40 #ifdef DSO_DLFCN
41
42 # include <dlfcn.h>
43
44 # define SHLIB_INIT NULL
45
46 typedef void *SHLIB;
47 typedef void *SHLIB_SYM;
48
49 static int shlib_load(const char *filename, SHLIB *lib)
50 {
51     int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
52 #ifdef _AIX
53     if (filename[strlen(filename) - 1] == ')')
54         dl_flags |= RTLD_MEMBER;
55 #endif
56     *lib = dlopen(filename, dl_flags);
57     return *lib == NULL ? 0 : 1;
58 }
59
60 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
61 {
62     *sym = dlsym(lib, symname);
63     return *sym != NULL;
64 }
65
66 static int shlib_close(SHLIB lib)
67 {
68     return dlclose(lib) != 0 ? 0 : 1;
69 }
70 #endif
71
72 #ifdef DSO_WIN32
73
74 # include <windows.h>
75
76 # define SHLIB_INIT 0
77
78 typedef HINSTANCE SHLIB;
79 typedef void *SHLIB_SYM;
80
81 static int shlib_load(const char *filename, SHLIB *lib)
82 {
83     *lib = LoadLibraryA(filename);
84     return *lib == NULL ? 0 : 1;
85 }
86
87 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
88 {
89     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
90     return *sym != NULL;
91 }
92
93 static int shlib_close(SHLIB lib)
94 {
95     return FreeLibrary(lib) == 0 ? 0 : 1;
96 }
97 #endif
98
99
100 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
101
102 static int test_lib(void)
103 {
104     SHLIB ssllib = SHLIB_INIT;
105     SHLIB cryptolib = SHLIB_INIT;
106     SSL_CTX *ctx;
107     union {
108         void (*func)(void);
109         SHLIB_SYM sym;
110     } symbols[3];
111     TLS_method_t myTLS_method;
112     SSL_CTX_new_t mySSL_CTX_new;
113     SSL_CTX_free_t mySSL_CTX_free;
114     ERR_get_error_t myERR_get_error;
115     OpenSSL_version_num_t myOpenSSL_version_num;
116     int result = 0;
117
118     switch (test_type) {
119     case JUST_CRYPTO:
120         if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
121             goto end;
122         break;
123     case CRYPTO_FIRST:
124         if (!TEST_true(shlib_load(path_crypto, &cryptolib))
125                 || !TEST_true(shlib_load(path_ssl, &ssllib)))
126             goto end;
127         break;
128     case SSL_FIRST:
129         if (!TEST_true(shlib_load(path_ssl, &ssllib))
130                 || !TEST_true(shlib_load(path_crypto, &cryptolib)))
131             goto end;
132         break;
133     case DSO_REFTEST:
134         if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
135             goto end;
136         break;
137     }
138
139     if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
140         if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
141                 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
142                 || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
143             goto end;
144         myTLS_method = (TLS_method_t)symbols[0].func;
145         mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
146         mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
147         if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
148             goto end;
149         mySSL_CTX_free(ctx);
150     }
151
152     if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
153             || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
154                                     &symbols[1].sym)))
155         goto end;
156     myERR_get_error = (ERR_get_error_t)symbols[0].func;
157     if (!TEST_int_eq(myERR_get_error(), 0))
158         goto end;
159
160     /*
161      * The bits that COMPATIBILITY_MASK lets through MUST be the same in
162      * the library and in the application.
163      * The bits that are masked away MUST be a larger or equal number in
164      * the library compared to the application.
165      */
166 # define COMPATIBILITY_MASK 0xfff00000L
167     myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
168     if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
169                      OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK))
170         goto end;
171     if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
172                      OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK))
173         goto end;
174
175     if (test_type == DSO_REFTEST) {
176 # ifdef DSO_DLFCN
177         DSO_dsobyaddr_t myDSO_dsobyaddr;
178         DSO_free_t myDSO_free;
179
180         /*
181          * This is resembling the code used in ossl_init_base() and
182          * OPENSSL_atexit() to block unloading the library after dlclose().
183          * We are not testing this on Windows, because it is done there in a
184          * completely different way. Especially as a call to DSO_dsobyaddr()
185          * will always return an error, because DSO_pathbyaddr() is not
186          * implemented there.
187          */
188         if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
189                 || !TEST_true(shlib_sym(cryptolib, "DSO_free",
190                                         &symbols[1].sym)))
191             goto end;
192
193         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
194         myDSO_free = (DSO_free_t)symbols[1].func;
195
196         {
197             DSO *hndl;
198             /* use known symbol from crypto module */
199             if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
200                 goto end;
201             myDSO_free(hndl);
202         }
203 # endif /* DSO_DLFCN */
204     }
205
206     switch (test_type) {
207     case JUST_CRYPTO:
208         if (!TEST_true(shlib_close(cryptolib)))
209             goto end;
210         break;
211     case CRYPTO_FIRST:
212         if (!TEST_true(shlib_close(cryptolib))
213                 || !TEST_true(shlib_close(ssllib)))
214             goto end;
215         break;
216     case SSL_FIRST:
217         if (!TEST_true(shlib_close(ssllib))
218                 || !TEST_true(shlib_close(cryptolib)))
219             goto end;
220         break;
221     case DSO_REFTEST:
222         if (!TEST_true(shlib_close(cryptolib)))
223             goto end;
224         break;
225     }
226
227     result = 1;
228 end:
229     return result;
230 }
231 #endif
232
233
234 int setup_tests(void)
235 {
236     const char *p = test_get_argument(0);
237
238     if (strcmp(p, "-crypto_first") == 0) {
239         test_type = CRYPTO_FIRST;
240     } else if (strcmp(p, "-ssl_first") == 0) {
241         test_type = SSL_FIRST;
242     } else if (strcmp(p, "-just_crypto") == 0) {
243         test_type = JUST_CRYPTO;
244     } else if (strcmp(p, "-dso_ref") == 0) {
245         test_type = JUST_CRYPTO;
246     } else {
247         TEST_error("Unrecognised argument");
248         return 0;
249     }
250     if (!TEST_ptr(path_crypto = test_get_argument(1))
251             || !TEST_ptr(path_ssl = test_get_argument(2)))
252         return 0;
253
254 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
255     ADD_TEST(test_lib);
256 #endif
257     return 1;
258 }