b1a817258f46a76ed361e6d463719952ffcef97e
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016-2018 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 <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
18 typedef void DSO;
19
20 typedef const SSL_METHOD * (*TLS_method_t)(void);
21 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
22 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
23 typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24 typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
25 typedef unsigned long (*ERR_get_error_t)(void);
26 typedef unsigned long (*OPENSSL_version_major_t)(void);
27 typedef unsigned long (*OPENSSL_version_minor_t)(void);
28 typedef unsigned long (*OPENSSL_version_patch_t)(void);
29 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
30 typedef int (*DSO_free_t)(DSO *dso);
31
32 typedef enum test_types_en {
33     CRYPTO_FIRST,
34     SSL_FIRST,
35     JUST_CRYPTO,
36     DSO_REFTEST,
37     NO_ATEXIT
38 } TEST_TYPE;
39
40 static TEST_TYPE test_type;
41 static const char *path_crypto;
42 static const char *path_ssl;
43 static const char *path_atexit;
44
45 #ifdef DSO_DLFCN
46
47 # include <dlfcn.h>
48
49 # define SHLIB_INIT NULL
50
51 typedef void *SHLIB;
52 typedef void *SHLIB_SYM;
53
54 static int shlib_load(const char *filename, SHLIB *lib)
55 {
56     int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
57 #ifdef _AIX
58     if (filename[strlen(filename) - 1] == ')')
59         dl_flags |= RTLD_MEMBER;
60 #endif
61     *lib = dlopen(filename, dl_flags);
62     return *lib == NULL ? 0 : 1;
63 }
64
65 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
66 {
67     *sym = dlsym(lib, symname);
68     return *sym != NULL;
69 }
70
71 static int shlib_close(SHLIB lib)
72 {
73     return dlclose(lib) != 0 ? 0 : 1;
74 }
75 #endif
76
77 #ifdef DSO_WIN32
78
79 # include <windows.h>
80
81 # define SHLIB_INIT 0
82
83 typedef HINSTANCE SHLIB;
84 typedef void *SHLIB_SYM;
85
86 static int shlib_load(const char *filename, SHLIB *lib)
87 {
88     *lib = LoadLibraryA(filename);
89     return *lib == NULL ? 0 : 1;
90 }
91
92 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
93 {
94     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
95     return *sym != NULL;
96 }
97
98 static int shlib_close(SHLIB lib)
99 {
100     return FreeLibrary(lib) == 0 ? 0 : 1;
101 }
102 #endif
103
104
105 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
106
107 static void atexit_handler(void)
108 {
109     FILE *atexit_file = fopen(path_atexit, "w");
110
111     if (atexit_file == NULL)
112         return;
113
114     fprintf(atexit_file, "atexit() run\n");
115     fclose(atexit_file);
116 }
117
118 static int test_lib(void)
119 {
120     SHLIB ssllib = SHLIB_INIT;
121     SHLIB cryptolib = SHLIB_INIT;
122     SSL_CTX *ctx;
123     union {
124         void (*func)(void);
125         SHLIB_SYM sym;
126     } symbols[5];
127     TLS_method_t myTLS_method;
128     SSL_CTX_new_t mySSL_CTX_new;
129     SSL_CTX_free_t mySSL_CTX_free;
130     ERR_get_error_t myERR_get_error;
131     OPENSSL_version_major_t myOPENSSL_version_major;
132     OPENSSL_version_minor_t myOPENSSL_version_minor;
133     OPENSSL_version_patch_t myOPENSSL_version_patch;
134     OPENSSL_atexit_t myOPENSSL_atexit;
135     int result = 0;
136
137     switch (test_type) {
138     case JUST_CRYPTO:
139     case DSO_REFTEST:
140     case NO_ATEXIT:
141     case CRYPTO_FIRST:
142         if (!shlib_load(path_crypto, &cryptolib)) {
143             fprintf(stderr, "Failed to load libcrypto\n");
144             goto end;
145         }
146         if (test_type != CRYPTO_FIRST)
147             break;
148         /* Fall through */
149
150     case SSL_FIRST:
151         if (!shlib_load(path_ssl, &ssllib)) {
152             fprintf(stderr, "Failed to load libssl\n");
153             goto end;
154         }
155         if (test_type != SSL_FIRST)
156             break;
157         if (!shlib_load(path_crypto, &cryptolib)) {
158             fprintf(stderr, "Failed to load libcrypto\n");
159             goto end;
160         }
161         break;
162     }
163
164     if (test_type == NO_ATEXIT) {
165         OPENSSL_init_crypto_t myOPENSSL_init_crypto;
166
167         if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
168             fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
169             goto end;
170         }
171         myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
172         if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
173             fprintf(stderr, "Failed to initialise libcrypto\n");
174             goto end;
175         }
176     }
177
178     if (test_type != JUST_CRYPTO
179             && test_type != DSO_REFTEST
180             && test_type != NO_ATEXIT) {
181         if (!shlib_sym(ssllib, "TLS_method", &symbols[0].sym)
182                 || !shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
183                 || !shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
184             fprintf(stderr, "Failed to load libssl symbols\n");
185             goto end;
186         }
187         myTLS_method = (TLS_method_t)symbols[0].func;
188         mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
189         mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
190         ctx = mySSL_CTX_new(myTLS_method());
191         if (ctx == NULL) {
192             fprintf(stderr, "Failed to create SSL_CTX\n");
193             goto end;
194         }
195         mySSL_CTX_free(ctx);
196     }
197
198     if (!shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
199            || !shlib_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
200            || !shlib_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
201            || !shlib_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
202            || !shlib_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
203         fprintf(stderr, "Failed to load libcrypto symbols\n");
204         goto end;
205     }
206     myERR_get_error = (ERR_get_error_t)symbols[0].func;
207     if (myERR_get_error() != 0) {
208         fprintf(stderr, "Unexpected ERR_get_error() response\n");
209         goto end;
210     }
211
212     /* Library and header version should be identical in this test */
213     myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
214     myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
215     myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
216     if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
217             || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
218             || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
219         fprintf(stderr, "Invalid library version number\n");
220         goto end;
221     }
222
223     myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
224     if (!myOPENSSL_atexit(atexit_handler)) {
225         fprintf(stderr, "Failed to register atexit handler\n");
226         goto end;
227     }
228
229     if (test_type == DSO_REFTEST) {
230 # ifdef DSO_DLFCN
231         DSO_dsobyaddr_t myDSO_dsobyaddr;
232         DSO_free_t myDSO_free;
233
234         /*
235          * This is resembling the code used in ossl_init_base() and
236          * OPENSSL_atexit() to block unloading the library after dlclose().
237          * We are not testing this on Windows, because it is done there in a
238          * completely different way. Especially as a call to DSO_dsobyaddr()
239          * will always return an error, because DSO_pathbyaddr() is not
240          * implemented there.
241          */
242         if (!shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
243                 || !shlib_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
244             fprintf(stderr, "Unable to load DSO symbols\n");
245             goto end;
246         }
247
248         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
249         myDSO_free = (DSO_free_t)symbols[1].func;
250
251         {
252             DSO *hndl;
253             /* use known symbol from crypto module */
254             hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
255             if (hndl == NULL) {
256                 fprintf(stderr, "DSO_dsobyaddr() failed\n");
257                 goto end;
258             }
259             myDSO_free(hndl);
260         }
261 # endif /* DSO_DLFCN */
262     }
263
264     switch (test_type) {
265     case JUST_CRYPTO:
266     case DSO_REFTEST:
267     case NO_ATEXIT:
268     case CRYPTO_FIRST:
269         if (!shlib_close(cryptolib)) {
270             fprintf(stderr, "Failed to close libcrypto\n");
271             goto end;
272         }
273         if (test_type != CRYPTO_FIRST)
274             break;
275         /* Fall through */
276
277     case SSL_FIRST:
278         if (test_type == CRYPTO_FIRST && !shlib_close(ssllib)) {
279             fprintf(stderr, "Failed to close libssl\n");
280             goto end;
281         }
282         if (test_type != SSL_FIRST)
283             break;
284
285         if (!shlib_close(cryptolib)) {
286             fprintf(stderr, "Failed to close libcrypto\n");
287             goto end;
288         }
289         break;
290     }
291
292     result = 1;
293 end:
294     return result;
295 }
296 #endif
297
298
299 /*
300  * shlibloadtest should not use the normal test framework because we don't want
301  * it to link against libcrypto (which the framework uses). The point of the
302  * test is to check dynamic loading and unloading of libcrypto/libssl.
303  */
304 int main(int argc, char *argv[])
305 {
306     const char *p;
307
308     if (argc != 5) {
309         fprintf(stderr, "Incorrect number of arguments\n");
310         return 1;
311     }
312
313     p = argv[1];
314
315     if (strcmp(p, "-crypto_first") == 0) {
316         test_type = CRYPTO_FIRST;
317     } else if (strcmp(p, "-ssl_first") == 0) {
318         test_type = SSL_FIRST;
319     } else if (strcmp(p, "-just_crypto") == 0) {
320         test_type = JUST_CRYPTO;
321     } else if (strcmp(p, "-dso_ref") == 0) {
322         test_type = DSO_REFTEST;
323     } else if (strcmp(p, "-no_atexit") == 0) {
324         test_type = NO_ATEXIT;
325     } else {
326         fprintf(stderr, "Unrecognised argument\n");
327         return 1;
328     }
329     path_crypto = argv[2];
330     path_ssl = argv[3];
331     path_atexit = argv[4];
332     if (path_crypto == NULL || path_ssl == NULL) {
333         fprintf(stderr, "Invalid libcrypto/libssl path\n");
334         return 1;
335     }
336
337 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
338     if (!test_lib())
339         return 1;
340 #endif
341     return 0;
342 }