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>
15 /* The test is only currently implemented for DSO_DLFCN and DSO_WIN32 */
16 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
18 #define SSL_CTX_NEW "SSL_CTX_new"
19 #define SSL_CTX_FREE "SSL_CTX_free"
20 #define TLS_METHOD "TLS_method"
22 #define ERR_GET_ERROR "ERR_get_error"
23 #define OPENSSL_VERSION_NUM_FUNC "OpenSSL_version_num"
25 typedef struct ssl_ctx_st SSL_CTX;
26 typedef struct ssl_method_st SSL_METHOD;
27 typedef const SSL_METHOD * (*TLS_method_t)(void);
28 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
29 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
31 typedef unsigned long (*ERR_get_error_t)(void);
32 typedef unsigned long (*OpenSSL_version_num_t)(void);
34 static TLS_method_t TLS_method;
35 static SSL_CTX_new_t SSL_CTX_new;
36 static SSL_CTX_free_t SSL_CTX_free;
38 static ERR_get_error_t ERR_get_error;
39 static OpenSSL_version_num_t OpenSSL_version_num;
43 # define DSO_DSOBYADDR "DSO_dsobyaddr"
44 # define DSO_FREE "DSO_free"
47 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
48 typedef int (*DSO_free_t)(DSO *dso);
50 static DSO_dsobyaddr_t DSO_dsobyaddr;
51 static DSO_free_t DSO_free;
56 typedef void * SHLIB_SYM;
57 # define SHLIB_INIT NULL
59 static int shlib_load(const char *filename, SHLIB *lib)
61 *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
69 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
71 *sym = dlsym(lib, symname);
76 static int shlib_close(SHLIB lib)
78 if (dlclose(lib) != 0)
84 #elif defined(DSO_WIN32)
88 typedef HINSTANCE SHLIB;
89 typedef void * SHLIB_SYM;
92 static int shlib_load(const char *filename, SHLIB *lib)
94 *lib = LoadLibraryA(filename);
101 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
103 *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
108 static int shlib_close(SHLIB lib)
110 if (FreeLibrary(lib) == 0)
118 # define CRYPTO_FIRST_OPT "-crypto_first"
119 # define SSL_FIRST_OPT "-ssl_first"
120 # define JUST_CRYPTO_OPT "-just_crypto"
121 # define DSO_REFTEST_OPT "-dso_ref"
130 int main(int argc, char **argv)
132 SHLIB ssllib = SHLIB_INIT, cryptolib = SHLIB_INIT;
137 } tls_method_sym, ssl_ctx_new_sym, ssl_ctx_free_sym, err_get_error_sym,
138 openssl_version_num_sym, dso_dsobyaddr_sym, dso_free_sym;
139 enum test_types_en test_type;
143 printf("Unexpected number of arguments\n");
147 if (strcmp(argv[1], CRYPTO_FIRST_OPT) == 0) {
148 test_type = CRYPTO_FIRST;
149 } else if (strcmp(argv[1], SSL_FIRST_OPT) == 0) {
150 test_type = SSL_FIRST;
151 } else if (strcmp(argv[1], JUST_CRYPTO_OPT) == 0) {
152 test_type = JUST_CRYPTO;
153 } else if (strcmp(argv[1], DSO_REFTEST_OPT) == 0) {
154 test_type = DSO_REFTEST;
156 printf("Unrecognised argument\n");
160 for (i = 0; i < 2; i++) {
161 if ((i == 0 && (test_type == CRYPTO_FIRST
162 || test_type == JUST_CRYPTO
163 || test_type == DSO_REFTEST))
164 || (i == 1 && test_type == SSL_FIRST)) {
165 if (!shlib_load(argv[2], &cryptolib)) {
166 printf("Unable to load libcrypto\n");
170 if ((i == 0 && test_type == SSL_FIRST)
171 || (i == 1 && test_type == CRYPTO_FIRST)) {
172 if (!shlib_load(argv[3], &ssllib)) {
173 printf("Unable to load libssl\n");
179 if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
180 if (!shlib_sym(ssllib, TLS_METHOD, &tls_method_sym.sym)
181 || !shlib_sym(ssllib, SSL_CTX_NEW, &ssl_ctx_new_sym.sym)
182 || !shlib_sym(ssllib, SSL_CTX_FREE, &ssl_ctx_free_sym.sym)) {
183 printf("Unable to load ssl symbols\n");
187 TLS_method = (TLS_method_t)tls_method_sym.func;
188 SSL_CTX_new = (SSL_CTX_new_t)ssl_ctx_new_sym.func;
189 SSL_CTX_free = (SSL_CTX_free_t)ssl_ctx_free_sym.func;
191 ctx = SSL_CTX_new(TLS_method());
193 printf("Unable to create SSL_CTX\n");
199 if (!shlib_sym(cryptolib, ERR_GET_ERROR, &err_get_error_sym.sym)
200 || !shlib_sym(cryptolib, OPENSSL_VERSION_NUM_FUNC,
201 &openssl_version_num_sym.sym)) {
202 printf("Unable to load crypto symbols\n");
206 ERR_get_error = (ERR_get_error_t)err_get_error_sym.func;
207 OpenSSL_version_num = (OpenSSL_version_num_t)openssl_version_num_sym.func;
209 if (ERR_get_error() != 0) {
210 printf("Unexpected error in error queue\n");
215 * The bits that COMPATIBILITY_MASK lets through MUST be the same in
216 * the library and in the application.
217 * The bits that are masked away MUST be a larger or equal number in
218 * the library compared to the application.
220 # define COMPATIBILITY_MASK 0xfff00000L
221 if ((OpenSSL_version_num() & COMPATIBILITY_MASK)
222 != (OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)) {
223 printf("Unexpected library version loaded\n");
227 if ((OpenSSL_version_num() & ~COMPATIBILITY_MASK)
228 < (OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)) {
229 printf("Unexpected library version loaded\n");
233 if (test_type == DSO_REFTEST) {
236 * This is resembling the code used in ossl_init_base() and
237 * OPENSSL_atexit() to block unloading the library after dlclose().
238 * We are not testing this on Windows, because it is done there in a
239 * completely different way. Especially as a call to DSO_dsobyaddr()
240 * will always return an error, because DSO_pathbyaddr() is not
243 if (!shlib_sym(cryptolib, DSO_DSOBYADDR, &dso_dsobyaddr_sym.sym)
244 || !shlib_sym(cryptolib, DSO_FREE, &dso_free_sym.sym)) {
245 printf("Unable to load crypto dso symbols\n");
249 DSO_dsobyaddr = (DSO_dsobyaddr_t)dso_dsobyaddr_sym.func;
250 DSO_free = (DSO_free_t)dso_free_sym.func;
254 /* use known symbol from crypto module */
255 if ((hndl = DSO_dsobyaddr((void (*)(void))ERR_get_error, 0)) != NULL) {
258 printf("Unable to obtain DSO reference from crypto symbol\n");
262 # endif /* DSO_DLFCN */
265 for (i = 0; i < 2; i++) {
266 if ((i == 0 && test_type == CRYPTO_FIRST)
267 || (i == 1 && test_type == SSL_FIRST)) {
268 if (!shlib_close(ssllib)) {
269 printf("Unable to close libssl\n");
273 if ((i == 0 && (test_type == SSL_FIRST
274 || test_type == JUST_CRYPTO
275 || test_type == DSO_REFTEST))
276 || (i == 1 && test_type == CRYPTO_FIRST)) {
277 if (!shlib_close(cryptolib)) {
278 printf("Unable to close libcrypto\n");
290 printf("Test not implemented on this platform\n");