1c7da899bSchristos /*
2*b0d17251Schristos * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3c7da899bSchristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8c7da899bSchristos */
9c7da899bSchristos
10c7da899bSchristos #include <stdio.h>
11c7da899bSchristos #include <string.h>
12c7da899bSchristos #include <stdlib.h>
13c7da899bSchristos #include <openssl/opensslv.h>
1413d40330Schristos #include <openssl/ssl.h>
15*b0d17251Schristos #include <openssl/types.h>
16*b0d17251Schristos #include "simpledynamic.h"
17c7da899bSchristos
1813d40330Schristos typedef void DSO;
19c7da899bSchristos
20c7da899bSchristos typedef const SSL_METHOD * (*TLS_method_t)(void);
21c7da899bSchristos typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
22c7da899bSchristos typedef void (*SSL_CTX_free_t)(SSL_CTX *);
23b88c74d5Schristos typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24b88c74d5Schristos typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
25c7da899bSchristos typedef unsigned long (*ERR_get_error_t)(void);
26*b0d17251Schristos typedef unsigned long (*OPENSSL_version_major_t)(void);
27*b0d17251Schristos typedef unsigned long (*OPENSSL_version_minor_t)(void);
28*b0d17251Schristos typedef unsigned long (*OPENSSL_version_patch_t)(void);
29132cc1c4Schristos typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
30132cc1c4Schristos typedef int (*DSO_free_t)(DSO *dso);
31132cc1c4Schristos
3213d40330Schristos typedef enum test_types_en {
3313d40330Schristos CRYPTO_FIRST,
3413d40330Schristos SSL_FIRST,
3513d40330Schristos JUST_CRYPTO,
36b88c74d5Schristos DSO_REFTEST,
37b88c74d5Schristos NO_ATEXIT
3813d40330Schristos } TEST_TYPE;
3913d40330Schristos
4013d40330Schristos static TEST_TYPE test_type;
4113d40330Schristos static const char *path_crypto;
4213d40330Schristos static const char *path_ssl;
43b88c74d5Schristos static const char *path_atexit;
4413d40330Schristos
45*b0d17251Schristos #ifdef SD_INIT
46c7da899bSchristos
47b88c74d5Schristos static int atexit_handler_done = 0;
48b88c74d5Schristos
atexit_handler(void)49b88c74d5Schristos static void atexit_handler(void)
50b88c74d5Schristos {
51b88c74d5Schristos FILE *atexit_file = fopen(path_atexit, "w");
52b88c74d5Schristos
53b88c74d5Schristos if (atexit_file == NULL)
54b88c74d5Schristos return;
55b88c74d5Schristos
56b88c74d5Schristos fprintf(atexit_file, "atexit() run\n");
57b88c74d5Schristos fclose(atexit_file);
58b88c74d5Schristos atexit_handler_done++;
59b88c74d5Schristos }
60b88c74d5Schristos
test_lib(void)6113d40330Schristos static int test_lib(void)
62c7da899bSchristos {
63*b0d17251Schristos SD ssllib = SD_INIT;
64*b0d17251Schristos SD cryptolib = SD_INIT;
65c7da899bSchristos SSL_CTX *ctx;
66c7da899bSchristos union {
67c7da899bSchristos void (*func)(void);
68*b0d17251Schristos SD_SYM sym;
69*b0d17251Schristos } symbols[5];
7013d40330Schristos TLS_method_t myTLS_method;
7113d40330Schristos SSL_CTX_new_t mySSL_CTX_new;
7213d40330Schristos SSL_CTX_free_t mySSL_CTX_free;
7313d40330Schristos ERR_get_error_t myERR_get_error;
74*b0d17251Schristos OPENSSL_version_major_t myOPENSSL_version_major;
75*b0d17251Schristos OPENSSL_version_minor_t myOPENSSL_version_minor;
76*b0d17251Schristos OPENSSL_version_patch_t myOPENSSL_version_patch;
77b88c74d5Schristos OPENSSL_atexit_t myOPENSSL_atexit;
7813d40330Schristos int result = 0;
79c7da899bSchristos
8013d40330Schristos switch (test_type) {
8113d40330Schristos case JUST_CRYPTO:
8213d40330Schristos case DSO_REFTEST:
83b88c74d5Schristos case NO_ATEXIT:
84b88c74d5Schristos case CRYPTO_FIRST:
85*b0d17251Schristos if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
86b88c74d5Schristos fprintf(stderr, "Failed to load libcrypto\n");
8713d40330Schristos goto end;
88b88c74d5Schristos }
89b88c74d5Schristos if (test_type != CRYPTO_FIRST)
90b88c74d5Schristos break;
91b88c74d5Schristos /* Fall through */
92b88c74d5Schristos
93b88c74d5Schristos case SSL_FIRST:
94*b0d17251Schristos if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) {
95b88c74d5Schristos fprintf(stderr, "Failed to load libssl\n");
96b88c74d5Schristos goto end;
97b88c74d5Schristos }
98b88c74d5Schristos if (test_type != SSL_FIRST)
99b88c74d5Schristos break;
100*b0d17251Schristos if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
101b88c74d5Schristos fprintf(stderr, "Failed to load libcrypto\n");
102b88c74d5Schristos goto end;
103b88c74d5Schristos }
10413d40330Schristos break;
105c7da899bSchristos }
106c7da899bSchristos
107b88c74d5Schristos if (test_type == NO_ATEXIT) {
108b88c74d5Schristos OPENSSL_init_crypto_t myOPENSSL_init_crypto;
109b88c74d5Schristos
110*b0d17251Schristos if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
111b88c74d5Schristos fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
11213d40330Schristos goto end;
113b88c74d5Schristos }
114b88c74d5Schristos myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
115b88c74d5Schristos if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
116b88c74d5Schristos fprintf(stderr, "Failed to initialise libcrypto\n");
117b88c74d5Schristos goto end;
118b88c74d5Schristos }
119b88c74d5Schristos }
120b88c74d5Schristos
121b88c74d5Schristos if (test_type != JUST_CRYPTO
122b88c74d5Schristos && test_type != DSO_REFTEST
123b88c74d5Schristos && test_type != NO_ATEXIT) {
124*b0d17251Schristos if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym)
125*b0d17251Schristos || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
126*b0d17251Schristos || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
127b88c74d5Schristos fprintf(stderr, "Failed to load libssl symbols\n");
128b88c74d5Schristos goto end;
129b88c74d5Schristos }
13013d40330Schristos myTLS_method = (TLS_method_t)symbols[0].func;
13113d40330Schristos mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
13213d40330Schristos mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
133b88c74d5Schristos ctx = mySSL_CTX_new(myTLS_method());
134b88c74d5Schristos if (ctx == NULL) {
135b88c74d5Schristos fprintf(stderr, "Failed to create SSL_CTX\n");
13613d40330Schristos goto end;
137b88c74d5Schristos }
13813d40330Schristos mySSL_CTX_free(ctx);
139c7da899bSchristos }
140c7da899bSchristos
141*b0d17251Schristos if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
142*b0d17251Schristos || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym)
143*b0d17251Schristos || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym)
144*b0d17251Schristos || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym)
145*b0d17251Schristos || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) {
146b88c74d5Schristos fprintf(stderr, "Failed to load libcrypto symbols\n");
14713d40330Schristos goto end;
148b88c74d5Schristos }
14913d40330Schristos myERR_get_error = (ERR_get_error_t)symbols[0].func;
150b88c74d5Schristos if (myERR_get_error() != 0) {
151b88c74d5Schristos fprintf(stderr, "Unexpected ERR_get_error() response\n");
15213d40330Schristos goto end;
153b88c74d5Schristos }
154c7da899bSchristos
155*b0d17251Schristos /* Library and header version should be identical in this test */
156*b0d17251Schristos myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func;
157*b0d17251Schristos myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func;
158*b0d17251Schristos myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func;
159*b0d17251Schristos if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR
160*b0d17251Schristos || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR
161*b0d17251Schristos || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) {
162b88c74d5Schristos fprintf(stderr, "Invalid library version number\n");
16313d40330Schristos goto end;
164b88c74d5Schristos }
165b88c74d5Schristos
166*b0d17251Schristos myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
167b88c74d5Schristos if (!myOPENSSL_atexit(atexit_handler)) {
168b88c74d5Schristos fprintf(stderr, "Failed to register atexit handler\n");
16913d40330Schristos goto end;
170b88c74d5Schristos }
171c7da899bSchristos
172132cc1c4Schristos if (test_type == DSO_REFTEST) {
173132cc1c4Schristos # ifdef DSO_DLFCN
17413d40330Schristos DSO_dsobyaddr_t myDSO_dsobyaddr;
17513d40330Schristos DSO_free_t myDSO_free;
17613d40330Schristos
177132cc1c4Schristos /*
178132cc1c4Schristos * This is resembling the code used in ossl_init_base() and
179132cc1c4Schristos * OPENSSL_atexit() to block unloading the library after dlclose().
180132cc1c4Schristos * We are not testing this on Windows, because it is done there in a
181132cc1c4Schristos * completely different way. Especially as a call to DSO_dsobyaddr()
182132cc1c4Schristos * will always return an error, because DSO_pathbyaddr() is not
183132cc1c4Schristos * implemented there.
184132cc1c4Schristos */
185*b0d17251Schristos if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
186*b0d17251Schristos || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
187b88c74d5Schristos fprintf(stderr, "Unable to load DSO symbols\n");
18813d40330Schristos goto end;
189b88c74d5Schristos }
190132cc1c4Schristos
19113d40330Schristos myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
19213d40330Schristos myDSO_free = (DSO_free_t)symbols[1].func;
193132cc1c4Schristos
194132cc1c4Schristos {
195132cc1c4Schristos DSO *hndl;
196132cc1c4Schristos /* use known symbol from crypto module */
197b88c74d5Schristos hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
198b88c74d5Schristos if (hndl == NULL) {
199b88c74d5Schristos fprintf(stderr, "DSO_dsobyaddr() failed\n");
20013d40330Schristos goto end;
201b88c74d5Schristos }
20213d40330Schristos myDSO_free(hndl);
203132cc1c4Schristos }
204132cc1c4Schristos # endif /* DSO_DLFCN */
205132cc1c4Schristos }
206132cc1c4Schristos
207*b0d17251Schristos if (!sd_close(cryptolib)) {
208b88c74d5Schristos fprintf(stderr, "Failed to close libcrypto\n");
20913d40330Schristos goto end;
210c7da899bSchristos }
211*b0d17251Schristos cryptolib = SD_INIT;
212c7da899bSchristos
213b88c74d5Schristos if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
214*b0d17251Schristos if (!sd_close(ssllib)) {
215b88c74d5Schristos fprintf(stderr, "Failed to close libssl\n");
216b88c74d5Schristos goto end;
217b88c74d5Schristos }
218*b0d17251Schristos ssllib = SD_INIT;
219b88c74d5Schristos }
220b88c74d5Schristos
221b88c74d5Schristos # if defined(OPENSSL_NO_PINSHARED) \
222b88c74d5Schristos && defined(__GLIBC__) \
223b88c74d5Schristos && defined(__GLIBC_PREREQ) \
224b88c74d5Schristos && defined(OPENSSL_SYS_LINUX)
225b88c74d5Schristos # if __GLIBC_PREREQ(2, 3)
226b88c74d5Schristos /*
227b88c74d5Schristos * If we didn't pin the so then we are hopefully on a platform that supports
228b88c74d5Schristos * running atexit() on so unload. If not we might crash. We know this is
229b88c74d5Schristos * true on linux since glibc 2.2.3
230b88c74d5Schristos */
231b88c74d5Schristos if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
232b88c74d5Schristos fprintf(stderr, "atexit() handler did not run\n");
233b88c74d5Schristos goto end;
234b88c74d5Schristos }
235b88c74d5Schristos # endif
236b88c74d5Schristos # endif
237b88c74d5Schristos
23813d40330Schristos result = 1;
23913d40330Schristos end:
240*b0d17251Schristos if (cryptolib != SD_INIT)
241*b0d17251Schristos sd_close(cryptolib);
242*b0d17251Schristos if (ssllib != SD_INIT)
243*b0d17251Schristos sd_close(ssllib);
24413d40330Schristos return result;
245c7da899bSchristos }
246c7da899bSchristos #endif
24713d40330Schristos
24813d40330Schristos
249b88c74d5Schristos /*
250b88c74d5Schristos * shlibloadtest should not use the normal test framework because we don't want
251b88c74d5Schristos * it to link against libcrypto (which the framework uses). The point of the
252b88c74d5Schristos * test is to check dynamic loading and unloading of libcrypto/libssl.
253b88c74d5Schristos */
main(int argc,char * argv[])254b88c74d5Schristos int main(int argc, char *argv[])
25513d40330Schristos {
256b88c74d5Schristos const char *p;
257b88c74d5Schristos
258b88c74d5Schristos if (argc != 5) {
259b88c74d5Schristos fprintf(stderr, "Incorrect number of arguments\n");
260b88c74d5Schristos return 1;
261b88c74d5Schristos }
262b88c74d5Schristos
263b88c74d5Schristos p = argv[1];
26413d40330Schristos
26513d40330Schristos if (strcmp(p, "-crypto_first") == 0) {
26613d40330Schristos test_type = CRYPTO_FIRST;
26713d40330Schristos } else if (strcmp(p, "-ssl_first") == 0) {
26813d40330Schristos test_type = SSL_FIRST;
26913d40330Schristos } else if (strcmp(p, "-just_crypto") == 0) {
27013d40330Schristos test_type = JUST_CRYPTO;
27113d40330Schristos } else if (strcmp(p, "-dso_ref") == 0) {
272b88c74d5Schristos test_type = DSO_REFTEST;
273b88c74d5Schristos } else if (strcmp(p, "-no_atexit") == 0) {
274b88c74d5Schristos test_type = NO_ATEXIT;
27513d40330Schristos } else {
276b88c74d5Schristos fprintf(stderr, "Unrecognised argument\n");
277b88c74d5Schristos return 1;
27813d40330Schristos }
279b88c74d5Schristos path_crypto = argv[2];
280b88c74d5Schristos path_ssl = argv[3];
281b88c74d5Schristos path_atexit = argv[4];
282b88c74d5Schristos if (path_crypto == NULL || path_ssl == NULL) {
283b88c74d5Schristos fprintf(stderr, "Invalid libcrypto/libssl path\n");
284b88c74d5Schristos return 1;
285b88c74d5Schristos }
28613d40330Schristos
287*b0d17251Schristos #ifdef SD_INIT
288b88c74d5Schristos if (!test_lib())
28913d40330Schristos return 1;
290b88c74d5Schristos #endif
291b88c74d5Schristos return 0;
29213d40330Schristos }
293