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