1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <stdlib.h>
13*e0c4386eSCy Schubert #include <openssl/opensslv.h>
14*e0c4386eSCy Schubert #include <openssl/ssl.h>
15*e0c4386eSCy Schubert #include <openssl/types.h>
16*e0c4386eSCy Schubert #include "simpledynamic.h"
17*e0c4386eSCy Schubert
18*e0c4386eSCy Schubert typedef void DSO;
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert typedef const SSL_METHOD * (*TLS_method_t)(void);
21*e0c4386eSCy Schubert typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
22*e0c4386eSCy Schubert typedef void (*SSL_CTX_free_t)(SSL_CTX *);
23*e0c4386eSCy Schubert typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24*e0c4386eSCy Schubert typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
25*e0c4386eSCy Schubert typedef unsigned long (*ERR_get_error_t)(void);
26*e0c4386eSCy Schubert typedef unsigned long (*OPENSSL_version_major_t)(void);
27*e0c4386eSCy Schubert typedef unsigned long (*OPENSSL_version_minor_t)(void);
28*e0c4386eSCy Schubert typedef unsigned long (*OPENSSL_version_patch_t)(void);
29*e0c4386eSCy Schubert typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
30*e0c4386eSCy Schubert typedef int (*DSO_free_t)(DSO *dso);
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert typedef enum test_types_en {
33*e0c4386eSCy Schubert CRYPTO_FIRST,
34*e0c4386eSCy Schubert SSL_FIRST,
35*e0c4386eSCy Schubert JUST_CRYPTO,
36*e0c4386eSCy Schubert DSO_REFTEST,
37*e0c4386eSCy Schubert NO_ATEXIT
38*e0c4386eSCy Schubert } TEST_TYPE;
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert static TEST_TYPE test_type;
41*e0c4386eSCy Schubert static const char *path_crypto;
42*e0c4386eSCy Schubert static const char *path_ssl;
43*e0c4386eSCy Schubert static const char *path_atexit;
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert #ifdef SD_INIT
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert static int atexit_handler_done = 0;
48*e0c4386eSCy Schubert
atexit_handler(void)49*e0c4386eSCy Schubert static void atexit_handler(void)
50*e0c4386eSCy Schubert {
51*e0c4386eSCy Schubert FILE *atexit_file = fopen(path_atexit, "w");
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert if (atexit_file == NULL)
54*e0c4386eSCy Schubert return;
55*e0c4386eSCy Schubert
56*e0c4386eSCy Schubert fprintf(atexit_file, "atexit() run\n");
57*e0c4386eSCy Schubert fclose(atexit_file);
58*e0c4386eSCy Schubert atexit_handler_done++;
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert
test_lib(void)61*e0c4386eSCy Schubert static int test_lib(void)
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert SD ssllib = SD_INIT;
64*e0c4386eSCy Schubert SD cryptolib = SD_INIT;
65*e0c4386eSCy Schubert SSL_CTX *ctx;
66*e0c4386eSCy Schubert union {
67*e0c4386eSCy Schubert void (*func)(void);
68*e0c4386eSCy Schubert SD_SYM sym;
69*e0c4386eSCy Schubert } symbols[5];
70*e0c4386eSCy Schubert TLS_method_t myTLS_method;
71*e0c4386eSCy Schubert SSL_CTX_new_t mySSL_CTX_new;
72*e0c4386eSCy Schubert SSL_CTX_free_t mySSL_CTX_free;
73*e0c4386eSCy Schubert ERR_get_error_t myERR_get_error;
74*e0c4386eSCy Schubert OPENSSL_version_major_t myOPENSSL_version_major;
75*e0c4386eSCy Schubert OPENSSL_version_minor_t myOPENSSL_version_minor;
76*e0c4386eSCy Schubert OPENSSL_version_patch_t myOPENSSL_version_patch;
77*e0c4386eSCy Schubert OPENSSL_atexit_t myOPENSSL_atexit;
78*e0c4386eSCy Schubert int result = 0;
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert switch (test_type) {
81*e0c4386eSCy Schubert case JUST_CRYPTO:
82*e0c4386eSCy Schubert case DSO_REFTEST:
83*e0c4386eSCy Schubert case NO_ATEXIT:
84*e0c4386eSCy Schubert case CRYPTO_FIRST:
85*e0c4386eSCy Schubert if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
86*e0c4386eSCy Schubert fprintf(stderr, "Failed to load libcrypto\n");
87*e0c4386eSCy Schubert goto end;
88*e0c4386eSCy Schubert }
89*e0c4386eSCy Schubert if (test_type != CRYPTO_FIRST)
90*e0c4386eSCy Schubert break;
91*e0c4386eSCy Schubert /* Fall through */
92*e0c4386eSCy Schubert
93*e0c4386eSCy Schubert case SSL_FIRST:
94*e0c4386eSCy Schubert if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) {
95*e0c4386eSCy Schubert fprintf(stderr, "Failed to load libssl\n");
96*e0c4386eSCy Schubert goto end;
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert if (test_type != SSL_FIRST)
99*e0c4386eSCy Schubert break;
100*e0c4386eSCy Schubert if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
101*e0c4386eSCy Schubert fprintf(stderr, "Failed to load libcrypto\n");
102*e0c4386eSCy Schubert goto end;
103*e0c4386eSCy Schubert }
104*e0c4386eSCy Schubert break;
105*e0c4386eSCy Schubert }
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert if (test_type == NO_ATEXIT) {
108*e0c4386eSCy Schubert OPENSSL_init_crypto_t myOPENSSL_init_crypto;
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
111*e0c4386eSCy Schubert fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
112*e0c4386eSCy Schubert goto end;
113*e0c4386eSCy Schubert }
114*e0c4386eSCy Schubert myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
115*e0c4386eSCy Schubert if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
116*e0c4386eSCy Schubert fprintf(stderr, "Failed to initialise libcrypto\n");
117*e0c4386eSCy Schubert goto end;
118*e0c4386eSCy Schubert }
119*e0c4386eSCy Schubert }
120*e0c4386eSCy Schubert
121*e0c4386eSCy Schubert if (test_type != JUST_CRYPTO
122*e0c4386eSCy Schubert && test_type != DSO_REFTEST
123*e0c4386eSCy Schubert && test_type != NO_ATEXIT) {
124*e0c4386eSCy Schubert if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym)
125*e0c4386eSCy Schubert || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
126*e0c4386eSCy Schubert || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
127*e0c4386eSCy Schubert fprintf(stderr, "Failed to load libssl symbols\n");
128*e0c4386eSCy Schubert goto end;
129*e0c4386eSCy Schubert }
130*e0c4386eSCy Schubert myTLS_method = (TLS_method_t)symbols[0].func;
131*e0c4386eSCy Schubert mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
132*e0c4386eSCy Schubert mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
133*e0c4386eSCy Schubert ctx = mySSL_CTX_new(myTLS_method());
134*e0c4386eSCy Schubert if (ctx == NULL) {
135*e0c4386eSCy Schubert fprintf(stderr, "Failed to create SSL_CTX\n");
136*e0c4386eSCy Schubert goto end;
137*e0c4386eSCy Schubert }
138*e0c4386eSCy Schubert mySSL_CTX_free(ctx);
139*e0c4386eSCy Schubert }
140*e0c4386eSCy Schubert
141*e0c4386eSCy Schubert if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
142*e0c4386eSCy Schubert || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
143*e0c4386eSCy Schubert || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
144*e0c4386eSCy Schubert || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
145*e0c4386eSCy Schubert || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
146*e0c4386eSCy Schubert fprintf(stderr, "Failed to load libcrypto symbols\n");
147*e0c4386eSCy Schubert goto end;
148*e0c4386eSCy Schubert }
149*e0c4386eSCy Schubert myERR_get_error = (ERR_get_error_t)symbols[0].func;
150*e0c4386eSCy Schubert if (myERR_get_error() != 0) {
151*e0c4386eSCy Schubert fprintf(stderr, "Unexpected ERR_get_error() response\n");
152*e0c4386eSCy Schubert goto end;
153*e0c4386eSCy Schubert }
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert /* Library and header version should be identical in this test */
156*e0c4386eSCy Schubert myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
157*e0c4386eSCy Schubert myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
158*e0c4386eSCy Schubert myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
159*e0c4386eSCy Schubert if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
160*e0c4386eSCy Schubert || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
161*e0c4386eSCy Schubert || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
162*e0c4386eSCy Schubert fprintf(stderr, "Invalid library version number\n");
163*e0c4386eSCy Schubert goto end;
164*e0c4386eSCy Schubert }
165*e0c4386eSCy Schubert
166*e0c4386eSCy Schubert myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
167*e0c4386eSCy Schubert if (!myOPENSSL_atexit(atexit_handler)) {
168*e0c4386eSCy Schubert fprintf(stderr, "Failed to register atexit handler\n");
169*e0c4386eSCy Schubert goto end;
170*e0c4386eSCy Schubert }
171*e0c4386eSCy Schubert
172*e0c4386eSCy Schubert if (test_type == DSO_REFTEST) {
173*e0c4386eSCy Schubert # ifdef DSO_DLFCN
174*e0c4386eSCy Schubert DSO_dsobyaddr_t myDSO_dsobyaddr;
175*e0c4386eSCy Schubert DSO_free_t myDSO_free;
176*e0c4386eSCy Schubert
177*e0c4386eSCy Schubert /*
178*e0c4386eSCy Schubert * This is resembling the code used in ossl_init_base() and
179*e0c4386eSCy Schubert * OPENSSL_atexit() to block unloading the library after dlclose().
180*e0c4386eSCy Schubert * We are not testing this on Windows, because it is done there in a
181*e0c4386eSCy Schubert * completely different way. Especially as a call to DSO_dsobyaddr()
182*e0c4386eSCy Schubert * will always return an error, because DSO_pathbyaddr() is not
183*e0c4386eSCy Schubert * implemented there.
184*e0c4386eSCy Schubert */
185*e0c4386eSCy Schubert if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
186*e0c4386eSCy Schubert || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
187*e0c4386eSCy Schubert fprintf(stderr, "Unable to load DSO symbols\n");
188*e0c4386eSCy Schubert goto end;
189*e0c4386eSCy Schubert }
190*e0c4386eSCy Schubert
191*e0c4386eSCy Schubert myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
192*e0c4386eSCy Schubert myDSO_free = (DSO_free_t)symbols[1].func;
193*e0c4386eSCy Schubert
194*e0c4386eSCy Schubert {
195*e0c4386eSCy Schubert DSO *hndl;
196*e0c4386eSCy Schubert /* use known symbol from crypto module */
197*e0c4386eSCy Schubert hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
198*e0c4386eSCy Schubert if (hndl == NULL) {
199*e0c4386eSCy Schubert fprintf(stderr, "DSO_dsobyaddr() failed\n");
200*e0c4386eSCy Schubert goto end;
201*e0c4386eSCy Schubert }
202*e0c4386eSCy Schubert myDSO_free(hndl);
203*e0c4386eSCy Schubert }
204*e0c4386eSCy Schubert # endif /* DSO_DLFCN */
205*e0c4386eSCy Schubert }
206*e0c4386eSCy Schubert
207*e0c4386eSCy Schubert if (!sd_close(cryptolib)) {
208*e0c4386eSCy Schubert fprintf(stderr, "Failed to close libcrypto\n");
209*e0c4386eSCy Schubert goto end;
210*e0c4386eSCy Schubert }
211*e0c4386eSCy Schubert cryptolib = SD_INIT;
212*e0c4386eSCy Schubert
213*e0c4386eSCy Schubert if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
214*e0c4386eSCy Schubert if (!sd_close(ssllib)) {
215*e0c4386eSCy Schubert fprintf(stderr, "Failed to close libssl\n");
216*e0c4386eSCy Schubert goto end;
217*e0c4386eSCy Schubert }
218*e0c4386eSCy Schubert ssllib = SD_INIT;
219*e0c4386eSCy Schubert }
220*e0c4386eSCy Schubert
221*e0c4386eSCy Schubert # if defined(OPENSSL_NO_PINSHARED) \
222*e0c4386eSCy Schubert && defined(__GLIBC__) \
223*e0c4386eSCy Schubert && defined(__GLIBC_PREREQ) \
224*e0c4386eSCy Schubert && defined(OPENSSL_SYS_LINUX)
225*e0c4386eSCy Schubert # if __GLIBC_PREREQ(2, 3)
226*e0c4386eSCy Schubert /*
227*e0c4386eSCy Schubert * If we didn't pin the so then we are hopefully on a platform that supports
228*e0c4386eSCy Schubert * running atexit() on so unload. If not we might crash. We know this is
229*e0c4386eSCy Schubert * true on linux since glibc 2.2.3
230*e0c4386eSCy Schubert */
231*e0c4386eSCy Schubert if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
232*e0c4386eSCy Schubert fprintf(stderr, "atexit() handler did not run\n");
233*e0c4386eSCy Schubert goto end;
234*e0c4386eSCy Schubert }
235*e0c4386eSCy Schubert # endif
236*e0c4386eSCy Schubert # endif
237*e0c4386eSCy Schubert
238*e0c4386eSCy Schubert result = 1;
239*e0c4386eSCy Schubert end:
240*e0c4386eSCy Schubert if (cryptolib != SD_INIT)
241*e0c4386eSCy Schubert sd_close(cryptolib);
242*e0c4386eSCy Schubert if (ssllib != SD_INIT)
243*e0c4386eSCy Schubert sd_close(ssllib);
244*e0c4386eSCy Schubert return result;
245*e0c4386eSCy Schubert }
246*e0c4386eSCy Schubert #endif
247*e0c4386eSCy Schubert
248*e0c4386eSCy Schubert
249*e0c4386eSCy Schubert /*
250*e0c4386eSCy Schubert * shlibloadtest should not use the normal test framework because we don't want
251*e0c4386eSCy Schubert * it to link against libcrypto (which the framework uses). The point of the
252*e0c4386eSCy Schubert * test is to check dynamic loading and unloading of libcrypto/libssl.
253*e0c4386eSCy Schubert */
main(int argc,char * argv[])254*e0c4386eSCy Schubert int main(int argc, char *argv[])
255*e0c4386eSCy Schubert {
256*e0c4386eSCy Schubert const char *p;
257*e0c4386eSCy Schubert
258*e0c4386eSCy Schubert if (argc != 5) {
259*e0c4386eSCy Schubert fprintf(stderr, "Incorrect number of arguments\n");
260*e0c4386eSCy Schubert return 1;
261*e0c4386eSCy Schubert }
262*e0c4386eSCy Schubert
263*e0c4386eSCy Schubert p = argv[1];
264*e0c4386eSCy Schubert
265*e0c4386eSCy Schubert if (strcmp(p, "-crypto_first") == 0) {
266*e0c4386eSCy Schubert test_type = CRYPTO_FIRST;
267*e0c4386eSCy Schubert } else if (strcmp(p, "-ssl_first") == 0) {
268*e0c4386eSCy Schubert test_type = SSL_FIRST;
269*e0c4386eSCy Schubert } else if (strcmp(p, "-just_crypto") == 0) {
270*e0c4386eSCy Schubert test_type = JUST_CRYPTO;
271*e0c4386eSCy Schubert } else if (strcmp(p, "-dso_ref") == 0) {
272*e0c4386eSCy Schubert test_type = DSO_REFTEST;
273*e0c4386eSCy Schubert } else if (strcmp(p, "-no_atexit") == 0) {
274*e0c4386eSCy Schubert test_type = NO_ATEXIT;
275*e0c4386eSCy Schubert } else {
276*e0c4386eSCy Schubert fprintf(stderr, "Unrecognised argument\n");
277*e0c4386eSCy Schubert return 1;
278*e0c4386eSCy Schubert }
279*e0c4386eSCy Schubert path_crypto = argv[2];
280*e0c4386eSCy Schubert path_ssl = argv[3];
281*e0c4386eSCy Schubert path_atexit = argv[4];
282*e0c4386eSCy Schubert if (path_crypto == NULL || path_ssl == NULL) {
283*e0c4386eSCy Schubert fprintf(stderr, "Invalid libcrypto/libssl path\n");
284*e0c4386eSCy Schubert return 1;
285*e0c4386eSCy Schubert }
286*e0c4386eSCy Schubert
287*e0c4386eSCy Schubert #ifdef SD_INIT
288*e0c4386eSCy Schubert if (!test_lib())
289*e0c4386eSCy Schubert return 1;
290*e0c4386eSCy Schubert #endif
291*e0c4386eSCy Schubert return 0;
292*e0c4386eSCy Schubert }
293