Make the PACKET/WPACKET code available to both libcrypto and libssl
[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 int atexit_handler_done = 0;
108
109 static void atexit_handler(void)
110 {
111     FILE *atexit_file = fopen(path_atexit, "w");
112
113     if (atexit_file == NULL)
114         return;
115
116     fprintf(atexit_file, "atexit() run\n");
117     fclose(atexit_file);
118     atexit_handler_done++;
119 }
120
121 static int test_lib(void)
122 {
123     SHLIB ssllib = SHLIB_INIT;
124     SHLIB cryptolib = SHLIB_INIT;
125     SSL_CTX *ctx;
126     union {
127         void (*func)(void);
128         SHLIB_SYM sym;
129     } symbols[5];
130     TLS_method_t myTLS_method;
131     SSL_CTX_new_t mySSL_CTX_new;
132     SSL_CTX_free_t mySSL_CTX_free;
133     ERR_get_error_t myERR_get_error;
134     OPENSSL_version_major_t myOPENSSL_version_major;
135     OPENSSL_version_minor_t myOPENSSL_version_minor;
136     OPENSSL_version_patch_t myOPENSSL_version_patch;
137     OPENSSL_atexit_t myOPENSSL_atexit;
138     int result = 0;
139
140     switch (test_type) {
141     case JUST_CRYPTO:
142     case DSO_REFTEST:
143     case NO_ATEXIT:
144     case CRYPTO_FIRST:
145         if (!shlib_load(path_crypto, &cryptolib)) {
146             fprintf(stderr, "Failed to load libcrypto\n");
147             goto end;
148         }
149         if (test_type != CRYPTO_FIRST)
150             break;
151         /* Fall through */
152
153     case SSL_FIRST:
154         if (!shlib_load(path_ssl, &ssllib)) {
155             fprintf(stderr, "Failed to load libssl\n");
156             goto end;
157         }
158         if (test_type != SSL_FIRST)
159             break;
160         if (!shlib_load(path_crypto, &cryptolib)) {
161             fprintf(stderr, "Failed to load libcrypto\n");
162             goto end;
163         }
164         break;
165     }
166
167     if (test_type == NO_ATEXIT) {
168         OPENSSL_init_crypto_t myOPENSSL_init_crypto;
169
170         if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
171             fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
172             goto end;
173         }
174         myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
175         if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
176             fprintf(stderr, "Failed to initialise libcrypto\n");
177             goto end;
178         }
179     }
180
181     if (test_type != JUST_CRYPTO
182             && test_type != DSO_REFTEST
183             && test_type != NO_ATEXIT) {
184         if (!shlib_sym(ssllib, "TLS_method", &symbols[0].sym)
185                 || !shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
186                 || !shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
187             fprintf(stderr, "Failed to load libssl symbols\n");
188             goto end;
189         }
190         myTLS_method = (TLS_method_t)symbols[0].func;
191         mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
192         mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
193         ctx = mySSL_CTX_new(myTLS_method());
194         if (ctx == NULL) {
195             fprintf(stderr, "Failed to create SSL_CTX\n");
196             goto end;
197         }
198         mySSL_CTX_free(ctx);
199     }
200
201     if (!shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
202            || !shlib_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
203            || !shlib_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
204            || !shlib_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
205            || !shlib_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
206         fprintf(stderr, "Failed to load libcrypto symbols\n");
207         goto end;
208     }
209     myERR_get_error = (ERR_get_error_t)symbols[0].func;
210     if (myERR_get_error() != 0) {
211         fprintf(stderr, "Unexpected ERR_get_error() response\n");
212         goto end;
213     }
214
215     /* Library and header version should be identical in this test */
216     myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
217     myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
218     myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
219     if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
220             || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
221             || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
222         fprintf(stderr, "Invalid library version number\n");
223         goto end;
224     }
225
226     myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
227     if (!myOPENSSL_atexit(atexit_handler)) {
228         fprintf(stderr, "Failed to register atexit handler\n");
229         goto end;
230     }
231
232     if (test_type == DSO_REFTEST) {
233 # ifdef DSO_DLFCN
234         DSO_dsobyaddr_t myDSO_dsobyaddr;
235         DSO_free_t myDSO_free;
236
237         /*
238          * This is resembling the code used in ossl_init_base() and
239          * OPENSSL_atexit() to block unloading the library after dlclose().
240          * We are not testing this on Windows, because it is done there in a
241          * completely different way. Especially as a call to DSO_dsobyaddr()
242          * will always return an error, because DSO_pathbyaddr() is not
243          * implemented there.
244          */
245         if (!shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
246                 || !shlib_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
247             fprintf(stderr, "Unable to load DSO symbols\n");
248             goto end;
249         }
250
251         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
252         myDSO_free = (DSO_free_t)symbols[1].func;
253
254         {
255             DSO *hndl;
256             /* use known symbol from crypto module */
257             hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
258             if (hndl == NULL) {
259                 fprintf(stderr, "DSO_dsobyaddr() failed\n");
260                 goto end;
261             }
262             myDSO_free(hndl);
263         }
264 # endif /* DSO_DLFCN */
265     }
266
267     if (!shlib_close(cryptolib)) {
268         fprintf(stderr, "Failed to close libcrypto\n");
269         goto end;
270     }
271
272     if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
273         if (!shlib_close(ssllib)) {
274             fprintf(stderr, "Failed to close libssl\n");
275             goto end;
276         }
277     }
278
279 # if defined(OPENSSL_NO_PINSHARED) \
280     && defined(__GLIBC__) \
281     && defined(__GLIBC_PREREQ) \
282     && defined(OPENSSL_SYS_LINUX)
283 #  if __GLIBC_PREREQ(2, 3)
284     /*
285      * If we didn't pin the so then we are hopefully on a platform that supports
286      * running atexit() on so unload. If not we might crash. We know this is
287      * true on linux since glibc 2.2.3
288      */
289     if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
290         fprintf(stderr, "atexit() handler did not run\n");
291         goto end;
292     }
293 #  endif
294 # endif
295
296     result = 1;
297 end:
298     return result;
299 }
300 #endif
301
302
303 /*
304  * shlibloadtest should not use the normal test framework because we don't want
305  * it to link against libcrypto (which the framework uses). The point of the
306  * test is to check dynamic loading and unloading of libcrypto/libssl.
307  */
308 int main(int argc, char *argv[])
309 {
310     const char *p;
311
312     if (argc != 5) {
313         fprintf(stderr, "Incorrect number of arguments\n");
314         return 1;
315     }
316
317     p = argv[1];
318
319     if (strcmp(p, "-crypto_first") == 0) {
320         test_type = CRYPTO_FIRST;
321     } else if (strcmp(p, "-ssl_first") == 0) {
322         test_type = SSL_FIRST;
323     } else if (strcmp(p, "-just_crypto") == 0) {
324         test_type = JUST_CRYPTO;
325     } else if (strcmp(p, "-dso_ref") == 0) {
326         test_type = DSO_REFTEST;
327     } else if (strcmp(p, "-no_atexit") == 0) {
328         test_type = NO_ATEXIT;
329     } else {
330         fprintf(stderr, "Unrecognised argument\n");
331         return 1;
332     }
333     path_crypto = argv[2];
334     path_ssl = argv[3];
335     path_atexit = argv[4];
336     if (path_crypto == NULL || path_ssl == NULL) {
337         fprintf(stderr, "Invalid libcrypto/libssl path\n");
338         return 1;
339     }
340
341 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
342     if (!test_lib())
343         return 1;
344 #endif
345     return 0;
346 }