blob: 5dc42a0397123b2a406fd33b4e7edd51eb8b35ec [file] [log] [blame]
Matt Caswellb987d742016-10-18 14:16:35 +01001/*
Matt Caswell3c2bdd72021-04-08 13:04:41 +01002 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
Matt Caswellb987d742016-10-18 14:16:35 +01003 *
Richard Levitte909f1a22018-12-06 13:05:25 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Matt Caswellb987d742016-10-18 14:16:35 +01005 * 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>
Rich Salzbdd07c72017-05-05 17:39:13 -040014#include <openssl/ssl.h>
Dr. Matthias St. Pierre50cd4762019-09-28 00:45:46 +020015#include <openssl/types.h>
Richard Levitte9800b1a2020-11-25 07:56:08 +010016#include "simpledynamic.h"
Matt Caswellb987d742016-10-18 14:16:35 +010017
Matthias Kraft4af14b72018-03-19 13:37:46 -040018typedef void DSO;
19
Matt Caswellb987d742016-10-18 14:16:35 +010020typedef 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 *);
Matt Caswell88d57bf2018-11-15 17:41:06 +000023typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
Matt Caswellb987d742016-10-18 14:16:35 +010025typedef unsigned long (*ERR_get_error_t)(void);
Richard Levitte3a63dbe2018-09-27 15:56:35 +020026typedef unsigned long (*OPENSSL_version_major_t)(void);
27typedef unsigned long (*OPENSSL_version_minor_t)(void);
28typedef unsigned long (*OPENSSL_version_patch_t)(void);
Bernd Edlinger26db3242018-03-30 19:17:39 +020029typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
Matthias Kraft4af14b72018-03-19 13:37:46 -040030typedef int (*DSO_free_t)(DSO *dso);
Matt Caswellb987d742016-10-18 14:16:35 +010031
Rich Salze0011aa2017-05-06 07:59:18 -040032typedef enum test_types_en {
33 CRYPTO_FIRST,
34 SSL_FIRST,
Matthias Kraft4af14b72018-03-19 13:37:46 -040035 JUST_CRYPTO,
Matt Caswell88d57bf2018-11-15 17:41:06 +000036 DSO_REFTEST,
37 NO_ATEXIT
Rich Salze0011aa2017-05-06 07:59:18 -040038} TEST_TYPE;
39
40static TEST_TYPE test_type;
41static const char *path_crypto;
42static const char *path_ssl;
Matt Caswell88d57bf2018-11-15 17:41:06 +000043static const char *path_atexit;
Rich Salze0011aa2017-05-06 07:59:18 -040044
Richard Levitte9800b1a2020-11-25 07:56:08 +010045#ifdef SD_INIT
Rich Salzbdd07c72017-05-05 17:39:13 -040046
Matt Caswell41999e72018-11-16 14:05:14 +000047static int atexit_handler_done = 0;
48
Matt Caswell88d57bf2018-11-15 17:41:06 +000049static 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);
Matt Caswell41999e72018-11-16 14:05:14 +000058 atexit_handler_done++;
Matt Caswell88d57bf2018-11-15 17:41:06 +000059}
60
Rich Salzbdd07c72017-05-05 17:39:13 -040061static int test_lib(void)
Matt Caswellb987d742016-10-18 14:16:35 +010062{
Richard Levitte9800b1a2020-11-25 07:56:08 +010063 SD ssllib = SD_INIT;
64 SD cryptolib = SD_INIT;
Matt Caswellb987d742016-10-18 14:16:35 +010065 SSL_CTX *ctx;
66 union {
Rich Salzbdd07c72017-05-05 17:39:13 -040067 void (*func)(void);
Richard Levitte9800b1a2020-11-25 07:56:08 +010068 SD_SYM sym;
Matt Caswell88d57bf2018-11-15 17:41:06 +000069 } symbols[5];
Rich Salzbdd07c72017-05-05 17:39:13 -040070 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;
Richard Levitte3a63dbe2018-09-27 15:56:35 +020074 OPENSSL_version_major_t myOPENSSL_version_major;
75 OPENSSL_version_minor_t myOPENSSL_version_minor;
76 OPENSSL_version_patch_t myOPENSSL_version_patch;
Matt Caswell88d57bf2018-11-15 17:41:06 +000077 OPENSSL_atexit_t myOPENSSL_atexit;
Rich Salzbdd07c72017-05-05 17:39:13 -040078 int result = 0;
Matt Caswellb987d742016-10-18 14:16:35 +010079
Rich Salzbdd07c72017-05-05 17:39:13 -040080 switch (test_type) {
81 case JUST_CRYPTO:
Matt Caswelldf5228e2018-11-15 14:50:52 +000082 case DSO_REFTEST:
Matt Caswell88d57bf2018-11-15 17:41:06 +000083 case NO_ATEXIT:
Rich Salzbdd07c72017-05-05 17:39:13 -040084 case CRYPTO_FIRST:
Richard Levitte9800b1a2020-11-25 07:56:08 +010085 if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +000086 fprintf(stderr, "Failed to load libcrypto\n");
Rich Salzbdd07c72017-05-05 17:39:13 -040087 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +000088 }
89 if (test_type != CRYPTO_FIRST)
90 break;
91 /* Fall through */
92
Rich Salzbdd07c72017-05-05 17:39:13 -040093 case SSL_FIRST:
Richard Levitte9800b1a2020-11-25 07:56:08 +010094 if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +000095 fprintf(stderr, "Failed to load libssl\n");
Rich Salzbdd07c72017-05-05 17:39:13 -040096 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +000097 }
98 if (test_type != SSL_FIRST)
99 break;
Richard Levitte9800b1a2020-11-25 07:56:08 +0100100 if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +0000101 fprintf(stderr, "Failed to load libcrypto\n");
102 goto end;
103 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400104 break;
Matt Caswellb987d742016-10-18 14:16:35 +0100105 }
106
Matt Caswell88d57bf2018-11-15 17:41:06 +0000107 if (test_type == NO_ATEXIT) {
108 OPENSSL_init_crypto_t myOPENSSL_init_crypto;
109
Richard Levitte9800b1a2020-11-25 07:56:08 +0100110 if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
Matt Caswell88d57bf2018-11-15 17:41:06 +0000111 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) {
Richard Levitte9800b1a2020-11-25 07:56:08 +0100124 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)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +0000127 fprintf(stderr, "Failed to load libssl symbols\n");
Rich Salzbdd07c72017-05-05 17:39:13 -0400128 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000129 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400130 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;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000133 ctx = mySSL_CTX_new(myTLS_method());
134 if (ctx == NULL) {
135 fprintf(stderr, "Failed to create SSL_CTX\n");
Rich Salzbdd07c72017-05-05 17:39:13 -0400136 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000137 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400138 mySSL_CTX_free(ctx);
Matt Caswellb987d742016-10-18 14:16:35 +0100139 }
140
Richard Levitte9800b1a2020-11-25 07:56:08 +0100141 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)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +0000146 fprintf(stderr, "Failed to load libcrypto symbols\n");
Rich Salzbdd07c72017-05-05 17:39:13 -0400147 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000148 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400149 myERR_get_error = (ERR_get_error_t)symbols[0].func;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000150 if (myERR_get_error() != 0) {
151 fprintf(stderr, "Unexpected ERR_get_error() response\n");
Rich Salzbdd07c72017-05-05 17:39:13 -0400152 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000153 }
Richard Levitte00c8f1b2018-03-14 17:31:20 +0100154
Matt Caswelld0f2f202018-11-15 16:59:41 +0000155 /* Library and header version should be identical in this test */
Richard Levitte3a63dbe2018-09-27 15:56:35 +0200156 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;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000159 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");
Richard Levitte00c8f1b2018-03-14 17:31:20 +0100163 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000164 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400165
Matt Caswell88d57bf2018-11-15 17:41:06 +0000166 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
Matthias Kraft4af14b72018-03-19 13:37:46 -0400172 if (test_type == DSO_REFTEST) {
173# ifdef DSO_DLFCN
Richard Levitte84e68a12018-03-23 14:18:16 +0100174 DSO_dsobyaddr_t myDSO_dsobyaddr;
175 DSO_free_t myDSO_free;
176
Matthias Kraft4af14b72018-03-19 13:37:46 -0400177 /*
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 */
Richard Levitte9800b1a2020-11-25 07:56:08 +0100185 if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
186 || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +0000187 fprintf(stderr, "Unable to load DSO symbols\n");
Matthias Kraft4af14b72018-03-19 13:37:46 -0400188 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000189 }
Matthias Kraft4af14b72018-03-19 13:37:46 -0400190
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 */
Matt Caswelld0f2f202018-11-15 16:59:41 +0000197 hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
198 if (hndl == NULL) {
199 fprintf(stderr, "DSO_dsobyaddr() failed\n");
Matthias Kraft4af14b72018-03-19 13:37:46 -0400200 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000201 }
Richard Levittecfaad172018-03-23 01:05:41 +0100202 myDSO_free(hndl);
Matthias Kraft4af14b72018-03-19 13:37:46 -0400203 }
204# endif /* DSO_DLFCN */
205 }
206
Richard Levitte9800b1a2020-11-25 07:56:08 +0100207 if (!sd_close(cryptolib)) {
Matt Caswell41999e72018-11-16 14:05:14 +0000208 fprintf(stderr, "Failed to close libcrypto\n");
209 goto end;
210 }
Pauli65bf0292021-03-18 10:45:35 +1000211 cryptolib = SD_INIT;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000212
Matt Caswell41999e72018-11-16 14:05:14 +0000213 if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) {
Richard Levitte9800b1a2020-11-25 07:56:08 +0100214 if (!sd_close(ssllib)) {
Matt Caswelld0f2f202018-11-15 16:59:41 +0000215 fprintf(stderr, "Failed to close libssl\n");
Rich Salzbdd07c72017-05-05 17:39:13 -0400216 goto end;
Matt Caswelld0f2f202018-11-15 16:59:41 +0000217 }
Pauli65bf0292021-03-18 10:45:35 +1000218 ssllib = SD_INIT;
Matt Caswellb987d742016-10-18 14:16:35 +0100219 }
220
Matt Caswell41999e72018-11-16 14:05:14 +0000221# 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;
234 }
235# endif
236# endif
237
Rich Salzbdd07c72017-05-05 17:39:13 -0400238 result = 1;
239end:
Pauli65bf0292021-03-18 10:45:35 +1000240 if (cryptolib != SD_INIT)
241 sd_close(cryptolib);
242 if (ssllib != SD_INIT)
243 sd_close(ssllib);
Rich Salzbdd07c72017-05-05 17:39:13 -0400244 return result;
Matt Caswellb987d742016-10-18 14:16:35 +0100245}
Rich Salze0011aa2017-05-06 07:59:18 -0400246#endif
247
Rich Salzbdd07c72017-05-05 17:39:13 -0400248
Matt Caswelld0f2f202018-11-15 16:59:41 +0000249/*
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[])
Matt Caswellb987d742016-10-18 14:16:35 +0100255{
Matt Caswelld0f2f202018-11-15 16:59:41 +0000256 const char *p;
257
Matt Caswell88d57bf2018-11-15 17:41:06 +0000258 if (argc != 5) {
259 fprintf(stderr, "Incorrect number of arguments\n");
Matt Caswelld0f2f202018-11-15 16:59:41 +0000260 return 1;
261 }
262
263 p = argv[1];
Rich Salzbdd07c72017-05-05 17:39:13 -0400264
Pauliad887412017-07-18 11:48:27 +1000265 if (strcmp(p, "-crypto_first") == 0) {
Rich Salzbdd07c72017-05-05 17:39:13 -0400266 test_type = CRYPTO_FIRST;
Pauliad887412017-07-18 11:48:27 +1000267 } else if (strcmp(p, "-ssl_first") == 0) {
Rich Salzbdd07c72017-05-05 17:39:13 -0400268 test_type = SSL_FIRST;
Pauliad887412017-07-18 11:48:27 +1000269 } else if (strcmp(p, "-just_crypto") == 0) {
Rich Salzbdd07c72017-05-05 17:39:13 -0400270 test_type = JUST_CRYPTO;
Matthias Kraft4af14b72018-03-19 13:37:46 -0400271 } else if (strcmp(p, "-dso_ref") == 0) {
Matt Caswelldf5228e2018-11-15 14:50:52 +0000272 test_type = DSO_REFTEST;
Matt Caswell88d57bf2018-11-15 17:41:06 +0000273 } else if (strcmp(p, "-no_atexit") == 0) {
274 test_type = NO_ATEXIT;
Rich Salzbdd07c72017-05-05 17:39:13 -0400275 } else {
Matt Caswell88d57bf2018-11-15 17:41:06 +0000276 fprintf(stderr, "Unrecognised argument\n");
Matt Caswelld0f2f202018-11-15 16:59:41 +0000277 return 1;
Rich Salzbdd07c72017-05-05 17:39:13 -0400278 }
Matt Caswelld0f2f202018-11-15 16:59:41 +0000279 path_crypto = argv[2];
280 path_ssl = argv[3];
Matt Caswell88d57bf2018-11-15 17:41:06 +0000281 path_atexit = argv[4];
Matt Caswelld0f2f202018-11-15 16:59:41 +0000282 if (path_crypto == NULL || path_ssl == NULL) {
283 fprintf(stderr, "Invalid libcrypto/libssl path\n");
284 return 1;
285 }
Rich Salzbdd07c72017-05-05 17:39:13 -0400286
Richard Levitte9800b1a2020-11-25 07:56:08 +0100287#ifdef SD_INIT
Matt Caswelld0f2f202018-11-15 16:59:41 +0000288 if (!test_lib())
289 return 1;
Rich Salze0011aa2017-05-06 07:59:18 -0400290#endif
Matt Caswelld0f2f202018-11-15 16:59:41 +0000291 return 0;
Matt Caswellb987d742016-10-18 14:16:35 +0100292}