2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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_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);
32 typedef enum test_types_en {
40 static TEST_TYPE test_type;
41 static const char *path_crypto;
42 static const char *path_ssl;
43 static const char *path_atexit;
49 # define SHLIB_INIT NULL
52 typedef void *SHLIB_SYM;
54 static int shlib_load(const char *filename, SHLIB *lib)
56 int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
58 if (filename[strlen(filename) - 1] == ')')
59 dl_flags |= RTLD_MEMBER;
61 *lib = dlopen(filename, dl_flags);
62 return *lib == NULL ? 0 : 1;
65 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
67 *sym = dlsym(lib, symname);
71 static int shlib_close(SHLIB lib)
73 return dlclose(lib) != 0 ? 0 : 1;
83 typedef HINSTANCE SHLIB;
84 typedef void *SHLIB_SYM;
86 static int shlib_load(const char *filename, SHLIB *lib)
88 *lib = LoadLibraryA(filename);
89 return *lib == NULL ? 0 : 1;
92 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
94 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
98 static int shlib_close(SHLIB lib)
100 return FreeLibrary(lib) == 0 ? 0 : 1;
105 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
107 static int atexit_handler_done = 0;
109 static void atexit_handler(void)
111 FILE *atexit_file = fopen(path_atexit, "w");
113 if (atexit_file == NULL)
116 fprintf(atexit_file, "atexit() run\n");
118 atexit_handler_done++;
121 static int test_lib(void)
123 SHLIB ssllib = SHLIB_INIT;
124 SHLIB cryptolib = SHLIB_INIT;
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;
145 if (!shlib_load(path_crypto, &cryptolib)) {
146 fprintf(stderr, "Failed to load libcrypto\n");
149 if (test_type != CRYPTO_FIRST)
154 if (!shlib_load(path_ssl, &ssllib)) {
155 fprintf(stderr, "Failed to load libssl\n");
158 if (test_type != SSL_FIRST)
160 if (!shlib_load(path_crypto, &cryptolib)) {
161 fprintf(stderr, "Failed to load libcrypto\n");
167 if (test_type == NO_ATEXIT) {
168 OPENSSL_init_crypto_t myOPENSSL_init_crypto;
170 if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
171 fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
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");
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");
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());
195 fprintf(stderr, "Failed to create SSL_CTX\n");
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");
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");
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");
226 myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
227 if (!myOPENSSL_atexit(atexit_handler)) {
228 fprintf(stderr, "Failed to register atexit handler\n");
232 if (test_type == DSO_REFTEST) {
234 DSO_dsobyaddr_t myDSO_dsobyaddr;
235 DSO_free_t myDSO_free;
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
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");
251 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
252 myDSO_free = (DSO_free_t)symbols[1].func;
256 /* use known symbol from crypto module */
257 hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
259 fprintf(stderr, "DSO_dsobyaddr() failed\n");
264 # endif /* DSO_DLFCN */
267 if (!shlib_close(cryptolib)) {
268 fprintf(stderr, "Failed to close libcrypto\n");
272 if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
273 if (!shlib_close(ssllib)) {
274 fprintf(stderr, "Failed to close libssl\n");
279 # if defined(OPENSSL_NO_PINSHARED) \
280 && defined(__GLIBC__) \
281 && defined(__GLIBC_PREREQ) \
282 && defined(OPENSSL_SYS_LINUX)
283 # if __GLIBC_PREREQ(2, 3)
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
289 if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
290 fprintf(stderr, "atexit() handler did not run\n");
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.
308 int main(int argc, char *argv[])
313 fprintf(stderr, "Incorrect number of arguments\n");
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;
330 fprintf(stderr, "Unrecognised argument\n");
333 path_crypto = argv[2];
335 path_atexit = argv[4];
336 if (path_crypto == NULL || path_ssl == NULL) {
337 fprintf(stderr, "Invalid libcrypto/libssl path\n");
341 #if defined(DSO_DLFCN) || defined(DSO_WIN32)