2 * Copyright 2016 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
;
46 typedef void * SHLIB_SYM
;
47 # define SHLIB_INIT NULL
49 static int shlib_load(const char *filename
, SHLIB
*lib
)
51 *lib
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
59 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
61 *sym
= dlsym(lib
, symname
);
66 static int shlib_close(SHLIB lib
)
68 if (dlclose(lib
) != 0)
74 #elif defined(DSO_WIN32)
78 typedef HINSTANCE SHLIB
;
79 typedef void * SHLIB_SYM
;
82 static int shlib_load(const char *filename
, SHLIB
*lib
)
84 *lib
= LoadLibraryA(filename
);
91 static int shlib_sym(SHLIB lib
, const char *symname
, SHLIB_SYM
*sym
)
93 *sym
= (SHLIB_SYM
)GetProcAddress(lib
, symname
);
98 static int shlib_close(SHLIB lib
)
100 if (FreeLibrary(lib
) == 0)
108 # define CRYPTO_FIRST_OPT "-crypto_first"
109 # define SSL_FIRST_OPT "-ssl_first"
110 # define JUST_CRYPTO_OPT "-just_crypto"
118 int main(int argc
, char **argv
)
120 SHLIB ssllib
= SHLIB_INIT
, cryptolib
= SHLIB_INIT
;
125 } tls_method_sym
, ssl_ctx_new_sym
, ssl_ctx_free_sym
, err_get_error_sym
,
126 openssl_version_num_sym
;
127 enum test_types_en test_type
;
131 printf("Unexpected number of arguments\n");
135 if (strcmp(argv
[1], CRYPTO_FIRST_OPT
) == 0) {
136 test_type
= CRYPTO_FIRST
;
137 } else if (strcmp(argv
[1], SSL_FIRST_OPT
) == 0) {
138 test_type
= SSL_FIRST
;
139 } else if (strcmp(argv
[1], JUST_CRYPTO_OPT
) == 0) {
140 test_type
= JUST_CRYPTO
;
142 printf("Unrecognised argument\n");
146 for (i
= 0; i
< 2; i
++) {
147 if ((i
== 0 && (test_type
== CRYPTO_FIRST
148 || test_type
== JUST_CRYPTO
))
149 || (i
== 1 && test_type
== SSL_FIRST
)) {
150 if (!shlib_load(argv
[2], &cryptolib
)) {
151 printf("Unable to load libcrypto\n");
155 if ((i
== 0 && test_type
== SSL_FIRST
)
156 || (i
== 1 && test_type
== CRYPTO_FIRST
)) {
157 if (!shlib_load(argv
[3], &ssllib
)) {
158 printf("Unable to load libssl\n");
164 if (test_type
!= JUST_CRYPTO
) {
165 if (!shlib_sym(ssllib
, TLS_METHOD
, &tls_method_sym
.sym
)
166 || !shlib_sym(ssllib
, SSL_CTX_NEW
, &ssl_ctx_new_sym
.sym
)
167 || !shlib_sym(ssllib
, SSL_CTX_FREE
, &ssl_ctx_free_sym
.sym
)) {
168 printf("Unable to load ssl symbols\n");
172 TLS_method
= (TLS_method_t
)tls_method_sym
.func
;
173 SSL_CTX_new
= (SSL_CTX_new_t
)ssl_ctx_new_sym
.func
;
174 SSL_CTX_free
= (SSL_CTX_free_t
)ssl_ctx_free_sym
.func
;
176 ctx
= SSL_CTX_new(TLS_method());
178 printf("Unable to create SSL_CTX\n");
184 if (!shlib_sym(cryptolib
, ERR_GET_ERROR
, &err_get_error_sym
.sym
)
185 || !shlib_sym(cryptolib
, OPENSSL_VERSION_NUM_FUNC
,
186 &openssl_version_num_sym
.sym
)) {
187 printf("Unable to load crypto symbols\n");
191 ERR_get_error
= (ERR_get_error_t
)err_get_error_sym
.func
;
192 OpenSSL_version_num
= (OpenSSL_version_num_t
)openssl_version_num_sym
.func
;
194 if (ERR_get_error() != 0) {
195 printf("Unexpected error in error queue\n");
199 if (OpenSSL_version_num() != OPENSSL_VERSION_NUMBER
) {
200 printf("Unexpected library version loaded\n");
204 for (i
= 0; i
< 2; i
++) {
205 if ((i
== 0 && test_type
== CRYPTO_FIRST
)
206 || (i
== 1 && test_type
== SSL_FIRST
)) {
207 if (!shlib_close(ssllib
)) {
208 printf("Unable to close libssl\n");
212 if ((i
== 0 && (test_type
== SSL_FIRST
213 || test_type
== JUST_CRYPTO
))
214 || (i
== 1 && test_type
== CRYPTO_FIRST
)) {
215 if (!shlib_close(cryptolib
)) {
216 printf("Unable to close libcrypto\n");
228 printf("Test not implemented on this platform\n");