2 * Copyright 2016-2018 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 void atexit_handler(void)
107 FILE *atexit_file = fopen(path_atexit, "w");
109 if (atexit_file == NULL)
112 fprintf(atexit_file, "atexit() run\n");
116 static int test_lib(void)
118 SHLIB ssllib = SHLIB_INIT;
119 SHLIB cryptolib = SHLIB_INIT;
125 TLS_method_t myTLS_method;
126 SSL_CTX_new_t mySSL_CTX_new;
127 SSL_CTX_free_t mySSL_CTX_free;
128 ERR_get_error_t myERR_get_error;
129 OpenSSL_version_num_t myOpenSSL_version_num;
130 OPENSSL_atexit_t myOPENSSL_atexit;
138 if (!shlib_load(path_crypto, &cryptolib)) {
139 fprintf(stderr, "Failed to load libcrypto\n");
142 if (test_type != CRYPTO_FIRST)
147 if (!shlib_load(path_ssl, &ssllib)) {
148 fprintf(stderr, "Failed to load libssl\n");
151 if (test_type != SSL_FIRST)
153 if (!shlib_load(path_crypto, &cryptolib)) {
154 fprintf(stderr, "Failed to load libcrypto\n");
160 if (test_type == NO_ATEXIT) {
161 OPENSSL_init_crypto_t myOPENSSL_init_crypto;
163 if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
164 fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
167 myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
168 if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
169 fprintf(stderr, "Failed to initialise libcrypto\n");
174 if (test_type != JUST_CRYPTO
175 && test_type != DSO_REFTEST
176 && test_type != NO_ATEXIT) {
177 if (!shlib_sym(ssllib, "TLS_method", &symbols[0].sym)
178 || !shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
179 || !shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
180 fprintf(stderr, "Failed to load libssl symbols\n");
183 myTLS_method = (TLS_method_t)symbols[0].func;
184 mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
185 mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
186 ctx = mySSL_CTX_new(myTLS_method());
188 fprintf(stderr, "Failed to create SSL_CTX\n");
194 if (!shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
195 || !shlib_sym(cryptolib, "OpenSSL_version_num", &symbols[1].sym)
196 || !shlib_sym(cryptolib, "OPENSSL_atexit", &symbols[2].sym)) {
197 fprintf(stderr, "Failed to load libcrypto symbols\n");
200 myERR_get_error = (ERR_get_error_t)symbols[0].func;
201 if (myERR_get_error() != 0) {
202 fprintf(stderr, "Unexpected ERR_get_error() response\n");
206 myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
207 if (myOpenSSL_version_num() != OPENSSL_VERSION_NUMBER) {
208 fprintf(stderr, "Invalid library version number\n");
212 myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[2].func;
213 if (!myOPENSSL_atexit(atexit_handler)) {
214 fprintf(stderr, "Failed to register atexit handler\n");
218 if (test_type == DSO_REFTEST) {
220 DSO_dsobyaddr_t myDSO_dsobyaddr;
221 DSO_free_t myDSO_free;
224 * This is resembling the code used in ossl_init_base() and
225 * OPENSSL_atexit() to block unloading the library after dlclose().
226 * We are not testing this on Windows, because it is done there in a
227 * completely different way. Especially as a call to DSO_dsobyaddr()
228 * will always return an error, because DSO_pathbyaddr() is not
231 if (!shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
232 || !shlib_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
233 fprintf(stderr, "Unable to load DSO symbols\n");
237 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
238 myDSO_free = (DSO_free_t)symbols[1].func;
242 /* use known symbol from crypto module */
243 hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
245 fprintf(stderr, "DSO_dsobyaddr() failed\n");
250 # endif /* DSO_DLFCN */
258 if (!shlib_close(cryptolib)) {
259 fprintf(stderr, "Failed to close libcrypto\n");
262 if (test_type != CRYPTO_FIRST)
267 if (test_type == CRYPTO_FIRST && !shlib_close(ssllib)) {
268 fprintf(stderr, "Failed to close libssl\n");
271 if (test_type != SSL_FIRST)
274 if (!shlib_close(cryptolib)) {
275 fprintf(stderr, "Failed to close libcrypto\n");
289 * shlibloadtest should not use the normal test framework because we don't want
290 * it to link against libcrypto (which the framework uses). The point of the
291 * test is to check dynamic loading and unloading of libcrypto/libssl.
293 int main(int argc, char *argv[])
298 fprintf(stderr, "Incorrect number of arguments\n");
304 if (strcmp(p, "-crypto_first") == 0) {
305 test_type = CRYPTO_FIRST;
306 } else if (strcmp(p, "-ssl_first") == 0) {
307 test_type = SSL_FIRST;
308 } else if (strcmp(p, "-just_crypto") == 0) {
309 test_type = JUST_CRYPTO;
310 } else if (strcmp(p, "-dso_ref") == 0) {
311 test_type = DSO_REFTEST;
312 } else if (strcmp(p, "-no_atexit") == 0) {
313 test_type = NO_ATEXIT;
315 fprintf(stderr, "Unrecognised argument\n");
318 path_crypto = argv[2];
320 path_atexit = argv[4];
321 if (path_crypto == NULL || path_ssl == NULL) {
322 fprintf(stderr, "Invalid libcrypto/libssl path\n");
326 #if defined(DSO_DLFCN) || defined(DSO_WIN32)