2 * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/opensslv.h>
14 #include <openssl/ssl.h>
15 #include <openssl/ossl_typ.h>
16 #include "internal/dso_conf.h"
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_num_t
)(void);
27 typedef DSO
* (*DSO_dsobyaddr_t
)(void (*addr
)(void), int flags
);
28 typedef int (*DSO_free_t
)(DSO
*dso
);
30 typedef enum test_types_en
{
38 static TEST_TYPE test_type
;
39 static const char *path_crypto
;
40 static const char *path_ssl
;
41 static const char *path_atexit
;
47 # define SHLIB_INIT NULL
50 typedef void *SHLIB_SYM
;
52 static int shlib_load(const char *filename
, SHLIB
*lib
)
54 int dl_flags
= (RTLD_GLOBAL
|RTLD_LAZY
);
56 if (filename
[strlen(filename
) - 1] == ')')
57 dl_flags
|= RTLD_MEMBER
;
59 *lib
= dlopen(filename
, dl_flags
);
60 return *lib
== NULL
? 0 : 1;
63 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
65 *sym
= dlsym(lib
, symname
);
69 static int shlib_close(SHLIB lib
)
71 return dlclose(lib
) != 0 ? 0 : 1;
81 typedef HINSTANCE SHLIB
;
82 typedef void *SHLIB_SYM
;
84 static int shlib_load(const char *filename
, SHLIB
*lib
)
86 *lib
= LoadLibraryA(filename
);
87 return *lib
== NULL
? 0 : 1;
90 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
92 *sym
= (SHLIB_SYM
)GetProcAddress(lib
, symname
);
96 static int shlib_close(SHLIB lib
)
98 return FreeLibrary(lib
) == 0 ? 0 : 1;
103 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
105 static int atexit_handler_done
= 0;
107 static void atexit_handler(void)
109 FILE *atexit_file
= fopen(path_atexit
, "w");
111 if (atexit_file
== NULL
)
114 fprintf(atexit_file
, "atexit() run\n");
116 atexit_handler_done
++;
119 static int test_lib(void)
121 SHLIB ssllib
= SHLIB_INIT
;
122 SHLIB cryptolib
= SHLIB_INIT
;
128 TLS_method_t myTLS_method
;
129 SSL_CTX_new_t mySSL_CTX_new
;
130 SSL_CTX_free_t mySSL_CTX_free
;
131 ERR_get_error_t myERR_get_error
;
132 OpenSSL_version_num_t myOpenSSL_version_num
;
133 OPENSSL_atexit_t myOPENSSL_atexit
;
141 if (!shlib_load(path_crypto
, &cryptolib
)) {
142 fprintf(stderr
, "Failed to load libcrypto\n");
145 if (test_type
!= CRYPTO_FIRST
)
150 if (!shlib_load(path_ssl
, &ssllib
)) {
151 fprintf(stderr
, "Failed to load libssl\n");
154 if (test_type
!= SSL_FIRST
)
156 if (!shlib_load(path_crypto
, &cryptolib
)) {
157 fprintf(stderr
, "Failed to load libcrypto\n");
163 if (test_type
== NO_ATEXIT
) {
164 OPENSSL_init_crypto_t myOPENSSL_init_crypto
;
166 if (!shlib_sym(cryptolib
, "OPENSSL_init_crypto", &symbols
[0].sym
)) {
167 fprintf(stderr
, "Failed to load OPENSSL_init_crypto symbol\n");
170 myOPENSSL_init_crypto
= (OPENSSL_init_crypto_t
)symbols
[0].func
;
171 if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT
, NULL
)) {
172 fprintf(stderr
, "Failed to initialise libcrypto\n");
177 if (test_type
!= JUST_CRYPTO
178 && test_type
!= DSO_REFTEST
179 && test_type
!= NO_ATEXIT
) {
180 if (!shlib_sym(ssllib
, "TLS_method", &symbols
[0].sym
)
181 || !shlib_sym(ssllib
, "SSL_CTX_new", &symbols
[1].sym
)
182 || !shlib_sym(ssllib
, "SSL_CTX_free", &symbols
[2].sym
)) {
183 fprintf(stderr
, "Failed to load libssl symbols\n");
186 myTLS_method
= (TLS_method_t
)symbols
[0].func
;
187 mySSL_CTX_new
= (SSL_CTX_new_t
)symbols
[1].func
;
188 mySSL_CTX_free
= (SSL_CTX_free_t
)symbols
[2].func
;
189 ctx
= mySSL_CTX_new(myTLS_method());
191 fprintf(stderr
, "Failed to create SSL_CTX\n");
197 if (!shlib_sym(cryptolib
, "ERR_get_error", &symbols
[0].sym
)
198 || !shlib_sym(cryptolib
, "OpenSSL_version_num", &symbols
[1].sym
)
199 || !shlib_sym(cryptolib
, "OPENSSL_atexit", &symbols
[2].sym
)) {
200 fprintf(stderr
, "Failed to load libcrypto symbols\n");
203 myERR_get_error
= (ERR_get_error_t
)symbols
[0].func
;
204 if (myERR_get_error() != 0) {
205 fprintf(stderr
, "Unexpected ERR_get_error() response\n");
209 myOpenSSL_version_num
= (OpenSSL_version_num_t
)symbols
[1].func
;
210 if (myOpenSSL_version_num() != OPENSSL_VERSION_NUMBER
) {
211 fprintf(stderr
, "Invalid library version number\n");
215 myOPENSSL_atexit
= (OPENSSL_atexit_t
)symbols
[2].func
;
216 if (!myOPENSSL_atexit(atexit_handler
)) {
217 fprintf(stderr
, "Failed to register atexit handler\n");
221 if (test_type
== DSO_REFTEST
) {
223 DSO_dsobyaddr_t myDSO_dsobyaddr
;
224 DSO_free_t myDSO_free
;
227 * This is resembling the code used in ossl_init_base() and
228 * OPENSSL_atexit() to block unloading the library after dlclose().
229 * We are not testing this on Windows, because it is done there in a
230 * completely different way. Especially as a call to DSO_dsobyaddr()
231 * will always return an error, because DSO_pathbyaddr() is not
234 if (!shlib_sym(cryptolib
, "DSO_dsobyaddr", &symbols
[0].sym
)
235 || !shlib_sym(cryptolib
, "DSO_free", &symbols
[1].sym
)) {
236 fprintf(stderr
, "Unable to load DSO symbols\n");
240 myDSO_dsobyaddr
= (DSO_dsobyaddr_t
)symbols
[0].func
;
241 myDSO_free
= (DSO_free_t
)symbols
[1].func
;
245 /* use known symbol from crypto module */
246 hndl
= myDSO_dsobyaddr((void (*)(void))myERR_get_error
, 0);
248 fprintf(stderr
, "DSO_dsobyaddr() failed\n");
253 # endif /* DSO_DLFCN */
256 if (!shlib_close(cryptolib
)) {
257 fprintf(stderr
, "Failed to close libcrypto\n");
261 if (test_type
== CRYPTO_FIRST
|| test_type
== SSL_FIRST
) {
262 if (!shlib_close(ssllib
)) {
263 fprintf(stderr
, "Failed to close libssl\n");
268 # if defined(OPENSSL_NO_PINSHARED) \
269 && defined(__GLIBC__) \
270 && defined(__GLIBC_PREREQ) \
271 && defined(OPENSSL_SYS_LINUX)
272 # if __GLIBC_PREREQ(2, 3)
274 * If we didn't pin the so then we are hopefully on a platform that supports
275 * running atexit() on so unload. If not we might crash. We know this is
276 * true on linux since glibc 2.2.3
278 if (test_type
!= NO_ATEXIT
&& atexit_handler_done
!= 1) {
279 fprintf(stderr
, "atexit() handler did not run\n");
293 * shlibloadtest should not use the normal test framework because we don't want
294 * it to link against libcrypto (which the framework uses). The point of the
295 * test is to check dynamic loading and unloading of libcrypto/libssl.
297 int main(int argc
, char *argv
[])
302 fprintf(stderr
, "Incorrect number of arguments\n");
308 if (strcmp(p
, "-crypto_first") == 0) {
309 test_type
= CRYPTO_FIRST
;
310 } else if (strcmp(p
, "-ssl_first") == 0) {
311 test_type
= SSL_FIRST
;
312 } else if (strcmp(p
, "-just_crypto") == 0) {
313 test_type
= JUST_CRYPTO
;
314 } else if (strcmp(p
, "-dso_ref") == 0) {
315 test_type
= DSO_REFTEST
;
316 } else if (strcmp(p
, "-no_atexit") == 0) {
317 test_type
= NO_ATEXIT
;
319 fprintf(stderr
, "Unrecognised argument\n");
322 path_crypto
= argv
[2];
324 path_atexit
= argv
[4];
325 if (path_crypto
== NULL
|| path_ssl
== NULL
) {
326 fprintf(stderr
, "Invalid libcrypto/libssl path\n");
330 #if defined(DSO_DLFCN) || defined(DSO_WIN32)