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