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 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);
30 typedef enum test_types_en {
37 static TEST_TYPE test_type;
38 static const char *path_crypto;
39 static const char *path_ssl;
45 # define SHLIB_INIT NULL
48 typedef void *SHLIB_SYM;
50 static int shlib_load(const char *filename, SHLIB *lib)
52 int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
54 if (filename[strlen(filename) - 1] == ')')
55 dl_flags |= RTLD_MEMBER;
57 *lib = dlopen(filename, dl_flags);
58 return *lib == NULL ? 0 : 1;
61 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
63 *sym = dlsym(lib, symname);
67 static int shlib_close(SHLIB lib)
69 return dlclose(lib) != 0 ? 0 : 1;
79 typedef HINSTANCE SHLIB;
80 typedef void *SHLIB_SYM;
82 static int shlib_load(const char *filename, SHLIB *lib)
84 *lib = LoadLibraryA(filename);
85 return *lib == NULL ? 0 : 1;
88 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
90 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
94 static int shlib_close(SHLIB lib)
96 return FreeLibrary(lib) == 0 ? 0 : 1;
101 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
103 static int test_lib(void)
105 SHLIB ssllib = SHLIB_INIT;
106 SHLIB cryptolib = SHLIB_INIT;
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;
125 if (!shlib_load(path_crypto, &cryptolib)) {
126 fprintf(stderr, "Failed to load libcrypto\n");
129 if (test_type != CRYPTO_FIRST)
134 if (!shlib_load(path_ssl, &ssllib)) {
135 fprintf(stderr, "Failed to load libssl\n");
138 if (test_type != SSL_FIRST)
140 if (!shlib_load(path_crypto, &cryptolib)) {
141 fprintf(stderr, "Failed to load libcrypto\n");
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");
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());
159 fprintf(stderr, "Failed to create SSL_CTX\n");
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");
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");
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");
189 if (test_type == DSO_REFTEST) {
191 DSO_dsobyaddr_t myDSO_dsobyaddr;
192 DSO_free_t myDSO_free;
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
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");
208 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
209 myDSO_free = (DSO_free_t)symbols[1].func;
213 /* use known symbol from crypto module */
214 hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
216 fprintf(stderr, "DSO_dsobyaddr() failed\n");
221 # endif /* DSO_DLFCN */
228 if (!shlib_close(cryptolib)) {
229 fprintf(stderr, "Failed to close libcrypto\n");
232 if (test_type != CRYPTO_FIRST)
237 if (test_type == CRYPTO_FIRST && !shlib_close(ssllib)) {
238 fprintf(stderr, "Failed to close libssl\n");
241 if (test_type != SSL_FIRST)
244 if (!shlib_close(cryptolib)) {
245 fprintf(stderr, "Failed to close libcrypto\n");
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.
263 int main(int argc, char *argv[])
268 fprintf(stderr, "Incorrect number of arguments");
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;
283 fprintf(stderr, "Unrecognised argument");
286 path_crypto = argv[2];
288 if (path_crypto == NULL || path_ssl == NULL) {
289 fprintf(stderr, "Invalid libcrypto/libssl path\n");
293 #if defined(DSO_DLFCN) || defined(DSO_WIN32)