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