]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/shlibloadtest.c
Move ossl_asn1_string_to_time_t() to libtestutil
[thirdparty/openssl.git] / test / shlibloadtest.c
CommitLineData
b987d748 1/*
3c2bdd7d 2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
b987d748 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b987d748
MC
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
8 */
9
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13#include <openssl/opensslv.h>
bdd07c78 14#include <openssl/ssl.h>
50cd4768 15#include <openssl/types.h>
9800b1a0 16#include "simpledynamic.h"
b987d748 17
4af14b7b
MK
18typedef void DSO;
19
b987d748
MC
20typedef const SSL_METHOD * (*TLS_method_t)(void);
21typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
22typedef void (*SSL_CTX_free_t)(SSL_CTX *);
88d57bf8
MC
23typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
b987d748 25typedef unsigned long (*ERR_get_error_t)(void);
3a63dbef
RL
26typedef unsigned long (*OPENSSL_version_major_t)(void);
27typedef unsigned long (*OPENSSL_version_minor_t)(void);
28typedef unsigned long (*OPENSSL_version_patch_t)(void);
26db3246 29typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
4af14b7b 30typedef int (*DSO_free_t)(DSO *dso);
b987d748 31
e0011aa8
RS
32typedef enum test_types_en {
33 CRYPTO_FIRST,
34 SSL_FIRST,
4af14b7b 35 JUST_CRYPTO,
88d57bf8
MC
36 DSO_REFTEST,
37 NO_ATEXIT
e0011aa8
RS
38} TEST_TYPE;
39
40static TEST_TYPE test_type;
41static const char *path_crypto;
42static const char *path_ssl;
88d57bf8 43static const char *path_atexit;
e0011aa8 44
9800b1a0 45#ifdef SD_INIT
bdd07c78 46
41999e7d
MC
47static int atexit_handler_done = 0;
48
88d57bf8
MC
49static void atexit_handler(void)
50{
51 FILE *atexit_file = fopen(path_atexit, "w");
52
53 if (atexit_file == NULL)
54 return;
55
56 fprintf(atexit_file, "atexit() run\n");
57 fclose(atexit_file);
41999e7d 58 atexit_handler_done++;
88d57bf8
MC
59}
60
bdd07c78 61static int test_lib(void)
b987d748 62{
9800b1a0
RL
63 SD ssllib = SD_INIT;
64 SD cryptolib = SD_INIT;
b987d748
MC
65 SSL_CTX *ctx;
66 union {
bdd07c78 67 void (*func)(void);
9800b1a0 68 SD_SYM sym;
88d57bf8 69 } symbols[5];
bdd07c78
RS
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;
3a63dbef
RL
74 OPENSSL_version_major_t myOPENSSL_version_major;
75 OPENSSL_version_minor_t myOPENSSL_version_minor;
76 OPENSSL_version_patch_t myOPENSSL_version_patch;
88d57bf8 77 OPENSSL_atexit_t myOPENSSL_atexit;
bdd07c78
RS
78 int result = 0;
79
80 switch (test_type) {
81 case JUST_CRYPTO:
df5228e3 82 case DSO_REFTEST:
88d57bf8 83 case NO_ATEXIT:
bdd07c78 84 case CRYPTO_FIRST:
9800b1a0 85 if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
d0f2f202 86 fprintf(stderr, "Failed to load libcrypto\n");
bdd07c78 87 goto end;
d0f2f202
MC
88 }
89 if (test_type != CRYPTO_FIRST)
90 break;
91 /* Fall through */
92
bdd07c78 93 case SSL_FIRST:
9800b1a0 94 if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) {
d0f2f202
MC
95 fprintf(stderr, "Failed to load libssl\n");
96 goto end;
97 }
98 if (test_type != SSL_FIRST)
99 break;
9800b1a0 100 if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
d0f2f202 101 fprintf(stderr, "Failed to load libcrypto\n");
bdd07c78 102 goto end;
d0f2f202 103 }
bdd07c78 104 break;
b987d748
MC
105 }
106
88d57bf8
MC
107 if (test_type == NO_ATEXIT) {
108 OPENSSL_init_crypto_t myOPENSSL_init_crypto;
109
9800b1a0 110 if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
88d57bf8
MC
111 fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
112 goto end;
113 }
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");
117 goto end;
118 }
119 }
120
121 if (test_type != JUST_CRYPTO
122 && test_type != DSO_REFTEST
123 && test_type != NO_ATEXIT) {
9800b1a0
RL
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)) {
d0f2f202 127 fprintf(stderr, "Failed to load libssl symbols\n");
bdd07c78 128 goto end;
d0f2f202 129 }
bdd07c78
RS
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;
d0f2f202
MC
133 ctx = mySSL_CTX_new(myTLS_method());
134 if (ctx == NULL) {
135 fprintf(stderr, "Failed to create SSL_CTX\n");
bdd07c78 136 goto end;
d0f2f202 137 }
bdd07c78 138 mySSL_CTX_free(ctx);
b987d748
MC
139 }
140
9800b1a0
RL
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)) {
d0f2f202 146 fprintf(stderr, "Failed to load libcrypto symbols\n");
bdd07c78 147 goto end;
d0f2f202 148 }
bdd07c78 149 myERR_get_error = (ERR_get_error_t)symbols[0].func;
d0f2f202
MC
150 if (myERR_get_error() != 0) {
151 fprintf(stderr, "Unexpected ERR_get_error() response\n");
bdd07c78 152 goto end;
d0f2f202 153 }
00c8f1b0 154
d0f2f202 155 /* Library and header version should be identical in this test */
3a63dbef
RL
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;
d0f2f202
MC
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");
bdd07c78 163 goto end;
d0f2f202 164 }
bdd07c78 165
88d57bf8
MC
166 myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func;
167 if (!myOPENSSL_atexit(atexit_handler)) {
168 fprintf(stderr, "Failed to register atexit handler\n");
169 goto end;
170 }
171
4af14b7b
MK
172 if (test_type == DSO_REFTEST) {
173# ifdef DSO_DLFCN
84e68a1b
RL
174 DSO_dsobyaddr_t myDSO_dsobyaddr;
175 DSO_free_t myDSO_free;
176
4af14b7b
MK
177 /*
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
183 * implemented there.
184 */
9800b1a0
RL
185 if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
186 || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
d0f2f202 187 fprintf(stderr, "Unable to load DSO symbols\n");
4af14b7b 188 goto end;
d0f2f202 189 }
4af14b7b
MK
190
191 myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
192 myDSO_free = (DSO_free_t)symbols[1].func;
193
194 {
195 DSO *hndl;
196 /* use known symbol from crypto module */
d0f2f202
MC
197 hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
198 if (hndl == NULL) {
199 fprintf(stderr, "DSO_dsobyaddr() failed\n");
4af14b7b 200 goto end;
d0f2f202 201 }
cfaad171 202 myDSO_free(hndl);
4af14b7b
MK
203 }
204# endif /* DSO_DLFCN */
205 }
206
9800b1a0 207 if (!sd_close(cryptolib)) {
41999e7d
MC
208 fprintf(stderr, "Failed to close libcrypto\n");
209 goto end;
210 }
65bf029d 211 cryptolib = SD_INIT;
d0f2f202 212
41999e7d 213 if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
9800b1a0 214 if (!sd_close(ssllib)) {
d0f2f202
MC
215 fprintf(stderr, "Failed to close libssl\n");
216 goto end;
217 }
65bf029d 218 ssllib = SD_INIT;
41999e7d 219 }
d0f2f202 220
41999e7d
MC
221# if defined(OPENSSL_NO_PINSHARED) \
222 && defined(__GLIBC__) \
223 && defined(__GLIBC_PREREQ) \
224 && defined(OPENSSL_SYS_LINUX)
225# if __GLIBC_PREREQ(2, 3)
226 /*
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
230 */
231 if (test_type != NO_ATEXIT && atexit_handler_done != 1) {
232 fprintf(stderr, "atexit() handler did not run\n");
233 goto end;
b987d748 234 }
41999e7d
MC
235# endif
236# endif
b987d748 237
bdd07c78
RS
238 result = 1;
239end:
65bf029d
P
240 if (cryptolib != SD_INIT)
241 sd_close(cryptolib);
242 if (ssllib != SD_INIT)
243 sd_close(ssllib);
bdd07c78
RS
244 return result;
245}
e0011aa8
RS
246#endif
247
b987d748 248
d0f2f202
MC
249/*
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.
253 */
254int main(int argc, char *argv[])
bdd07c78 255{
d0f2f202
MC
256 const char *p;
257
88d57bf8
MC
258 if (argc != 5) {
259 fprintf(stderr, "Incorrect number of arguments\n");
d0f2f202
MC
260 return 1;
261 }
262
263 p = argv[1];
b987d748 264
ad887416 265 if (strcmp(p, "-crypto_first") == 0) {
bdd07c78 266 test_type = CRYPTO_FIRST;
ad887416 267 } else if (strcmp(p, "-ssl_first") == 0) {
bdd07c78 268 test_type = SSL_FIRST;
ad887416 269 } else if (strcmp(p, "-just_crypto") == 0) {
bdd07c78 270 test_type = JUST_CRYPTO;
4af14b7b 271 } else if (strcmp(p, "-dso_ref") == 0) {
df5228e3 272 test_type = DSO_REFTEST;
88d57bf8
MC
273 } else if (strcmp(p, "-no_atexit") == 0) {
274 test_type = NO_ATEXIT;
bdd07c78 275 } else {
88d57bf8 276 fprintf(stderr, "Unrecognised argument\n");
d0f2f202
MC
277 return 1;
278 }
279 path_crypto = argv[2];
280 path_ssl = argv[3];
88d57bf8 281 path_atexit = argv[4];
d0f2f202
MC
282 if (path_crypto == NULL || path_ssl == NULL) {
283 fprintf(stderr, "Invalid libcrypto/libssl path\n");
284 return 1;
b987d748
MC
285 }
286
9800b1a0 287#ifdef SD_INIT
d0f2f202
MC
288 if (!test_lib())
289 return 1;
e0011aa8 290#endif
d0f2f202 291 return 0;
b987d748 292}