2 * Copyright 2016-2021 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/types.h>
16 #include "simpledynamic.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
;
47 static int atexit_handler_done
= 0;
49 static void atexit_handler(void)
51 FILE *atexit_file
= fopen(path_atexit
, "w");
53 if (atexit_file
== NULL
)
56 fprintf(atexit_file
, "atexit() run\n");
58 atexit_handler_done
++;
61 static int test_lib(void)
64 SD cryptolib
= SD_INIT
;
70 TLS_method_t myTLS_method
;
71 SSL_CTX_new_t mySSL_CTX_new
;
72 SSL_CTX_free_t mySSL_CTX_free
;
73 ERR_get_error_t myERR_get_error
;
74 OPENSSL_version_major_t myOPENSSL_version_major
;
75 OPENSSL_version_minor_t myOPENSSL_version_minor
;
76 OPENSSL_version_patch_t myOPENSSL_version_patch
;
77 OPENSSL_atexit_t myOPENSSL_atexit
;
85 if (!sd_load(path_crypto
, &cryptolib
, SD_SHLIB
)) {
86 fprintf(stderr
, "Failed to load libcrypto\n");
89 if (test_type
!= CRYPTO_FIRST
)
94 if (!sd_load(path_ssl
, &ssllib
, SD_SHLIB
)) {
95 fprintf(stderr
, "Failed to load libssl\n");
98 if (test_type
!= SSL_FIRST
)
100 if (!sd_load(path_crypto
, &cryptolib
, SD_SHLIB
)) {
101 fprintf(stderr
, "Failed to load libcrypto\n");
107 if (test_type
== NO_ATEXIT
) {
108 OPENSSL_init_crypto_t myOPENSSL_init_crypto
;
110 if (!sd_sym(cryptolib
, "OPENSSL_init_crypto", &symbols
[0].sym
)) {
111 fprintf(stderr
, "Failed to load OPENSSL_init_crypto symbol\n");
114 myOPENSSL_init_crypto
= (OPENSSL_init_crypto_t
)symbols
[0].func
;
115 if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT
, NULL
)) {
116 fprintf(stderr
, "Failed to initialise libcrypto\n");
121 if (test_type
!= JUST_CRYPTO
122 && test_type
!= DSO_REFTEST
123 && test_type
!= NO_ATEXIT
) {
124 if (!sd_sym(ssllib
, "TLS_method", &symbols
[0].sym
)
125 || !sd_sym(ssllib
, "SSL_CTX_new", &symbols
[1].sym
)
126 || !sd_sym(ssllib
, "SSL_CTX_free", &symbols
[2].sym
)) {
127 fprintf(stderr
, "Failed to load libssl symbols\n");
130 myTLS_method
= (TLS_method_t
)symbols
[0].func
;
131 mySSL_CTX_new
= (SSL_CTX_new_t
)symbols
[1].func
;
132 mySSL_CTX_free
= (SSL_CTX_free_t
)symbols
[2].func
;
133 ctx
= mySSL_CTX_new(myTLS_method());
135 fprintf(stderr
, "Failed to create SSL_CTX\n");
141 if (!sd_sym(cryptolib
, "ERR_get_error", &symbols
[0].sym
)
142 || !sd_sym(cryptolib
, "OPENSSL_version_major", &symbols
[1].sym
)
143 || !sd_sym(cryptolib
, "OPENSSL_version_minor", &symbols
[2].sym
)
144 || !sd_sym(cryptolib
, "OPENSSL_version_patch", &symbols
[3].sym
)
145 || !sd_sym(cryptolib
, "OPENSSL_atexit", &symbols
[4].sym
)) {
146 fprintf(stderr
, "Failed to load libcrypto symbols\n");
149 myERR_get_error
= (ERR_get_error_t
)symbols
[0].func
;
150 if (myERR_get_error() != 0) {
151 fprintf(stderr
, "Unexpected ERR_get_error() response\n");
155 /* Library and header version should be identical in this test */
156 myOPENSSL_version_major
= (OPENSSL_version_major_t
)symbols
[1].func
;
157 myOPENSSL_version_minor
= (OPENSSL_version_minor_t
)symbols
[2].func
;
158 myOPENSSL_version_patch
= (OPENSSL_version_patch_t
)symbols
[3].func
;
159 if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
160 || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
161 || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH
) {
162 fprintf(stderr
, "Invalid library version number\n");
166 myOPENSSL_atexit
= (OPENSSL_atexit_t
)symbols
[4].func
;
167 if (!myOPENSSL_atexit(atexit_handler
)) {
168 fprintf(stderr
, "Failed to register atexit handler\n");
172 if (test_type
== DSO_REFTEST
) {
174 DSO_dsobyaddr_t myDSO_dsobyaddr
;
175 DSO_free_t myDSO_free
;
178 * This is resembling the code used in ossl_init_base() and
179 * OPENSSL_atexit() to block unloading the library after dlclose().
180 * We are not testing this on Windows, because it is done there in a
181 * completely different way. Especially as a call to DSO_dsobyaddr()
182 * will always return an error, because DSO_pathbyaddr() is not
185 if (!sd_sym(cryptolib
, "DSO_dsobyaddr", &symbols
[0].sym
)
186 || !sd_sym(cryptolib
, "DSO_free", &symbols
[1].sym
)) {
187 fprintf(stderr
, "Unable to load DSO symbols\n");
191 myDSO_dsobyaddr
= (DSO_dsobyaddr_t
)symbols
[0].func
;
192 myDSO_free
= (DSO_free_t
)symbols
[1].func
;
196 /* use known symbol from crypto module */
197 hndl
= myDSO_dsobyaddr((void (*)(void))myERR_get_error
, 0);
199 fprintf(stderr
, "DSO_dsobyaddr() failed\n");
204 # endif /* DSO_DLFCN */
207 if (!sd_close(cryptolib
)) {
208 fprintf(stderr
, "Failed to close libcrypto\n");
213 if (test_type
== CRYPTO_FIRST
|| test_type
== SSL_FIRST
) {
214 if (!sd_close(ssllib
)) {
215 fprintf(stderr
, "Failed to close libssl\n");
221 # if defined(OPENSSL_NO_PINSHARED) \
222 && defined(__GLIBC__) \
223 && defined(__GLIBC_PREREQ) \
224 && defined(OPENSSL_SYS_LINUX)
225 # if __GLIBC_PREREQ(2, 3)
227 * If we didn't pin the so then we are hopefully on a platform that supports
228 * running atexit() on so unload. If not we might crash. We know this is
229 * true on linux since glibc 2.2.3
231 if (test_type
!= NO_ATEXIT
&& atexit_handler_done
!= 1) {
232 fprintf(stderr
, "atexit() handler did not run\n");
240 if (cryptolib
!= SD_INIT
)
242 if (ssllib
!= SD_INIT
)
250 * shlibloadtest should not use the normal test framework because we don't want
251 * it to link against libcrypto (which the framework uses). The point of the
252 * test is to check dynamic loading and unloading of libcrypto/libssl.
254 int main(int argc
, char *argv
[])
259 fprintf(stderr
, "Incorrect number of arguments\n");
265 if (strcmp(p
, "-crypto_first") == 0) {
266 test_type
= CRYPTO_FIRST
;
267 } else if (strcmp(p
, "-ssl_first") == 0) {
268 test_type
= SSL_FIRST
;
269 } else if (strcmp(p
, "-just_crypto") == 0) {
270 test_type
= JUST_CRYPTO
;
271 } else if (strcmp(p
, "-dso_ref") == 0) {
272 test_type
= DSO_REFTEST
;
273 } else if (strcmp(p
, "-no_atexit") == 0) {
274 test_type
= NO_ATEXIT
;
276 fprintf(stderr
, "Unrecognised argument\n");
279 path_crypto
= argv
[2];
281 path_atexit
= argv
[4];
282 if (path_crypto
== NULL
|| path_ssl
== NULL
) {
283 fprintf(stderr
, "Invalid libcrypto/libssl path\n");