Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 1 | /* |
Matt Caswell | b0edda1 | 2018-03-20 13:00:17 +0000 | [diff] [blame] | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the OpenSSL license (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 |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <openssl/opensslv.h> |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 14 | #include <openssl/ssl.h> |
| 15 | #include <openssl/ossl_typ.h> |
Richard Levitte | b71fa7b | 2018-03-23 01:05:23 +0100 | [diff] [blame] | 16 | #include "internal/dso_conf.h" |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 17 | #include "testutil.h" |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 18 | |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 19 | typedef void DSO; |
| 20 | |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 21 | typedef const SSL_METHOD * (*TLS_method_t)(void); |
| 22 | typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth); |
| 23 | typedef void (*SSL_CTX_free_t)(SSL_CTX *); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 24 | typedef unsigned long (*ERR_get_error_t)(void); |
| 25 | typedef unsigned long (*OpenSSL_version_num_t)(void); |
Bernd Edlinger | 26db324 | 2018-03-30 19:17:39 +0200 | [diff] [blame] | 26 | typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags); |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 27 | typedef int (*DSO_free_t)(DSO *dso); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 28 | |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 29 | typedef enum test_types_en { |
| 30 | CRYPTO_FIRST, |
| 31 | SSL_FIRST, |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 32 | JUST_CRYPTO, |
| 33 | DSO_REFTEST |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 34 | } TEST_TYPE; |
| 35 | |
| 36 | static TEST_TYPE test_type; |
| 37 | static const char *path_crypto; |
| 38 | static const char *path_ssl; |
| 39 | |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 40 | #ifdef DSO_DLFCN |
| 41 | |
| 42 | # include <dlfcn.h> |
| 43 | |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 44 | # define SHLIB_INIT NULL |
| 45 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 46 | typedef void *SHLIB; |
| 47 | typedef void *SHLIB_SYM; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 48 | |
Richard Levitte | 62dd335 | 2016-11-03 18:48:23 +0100 | [diff] [blame] | 49 | static int shlib_load(const char *filename, SHLIB *lib) |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 50 | { |
Richard Levitte | 62dd335 | 2016-11-03 18:48:23 +0100 | [diff] [blame] | 51 | *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 52 | return *lib == NULL ? 0 : 1; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym) |
| 56 | { |
| 57 | *sym = dlsym(lib, symname); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 58 | return *sym != NULL; |
| 59 | } |
| 60 | |
| 61 | static int shlib_close(SHLIB lib) |
| 62 | { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 63 | return dlclose(lib) != 0 ? 0 : 1; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 64 | } |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 65 | #endif |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 66 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 67 | #ifdef DSO_WIN32 |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 68 | |
| 69 | # include <windows.h> |
| 70 | |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 71 | # define SHLIB_INIT 0 |
| 72 | |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 73 | typedef HINSTANCE SHLIB; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 74 | typedef void *SHLIB_SYM; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 75 | |
Richard Levitte | 62dd335 | 2016-11-03 18:48:23 +0100 | [diff] [blame] | 76 | static int shlib_load(const char *filename, SHLIB *lib) |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 77 | { |
| 78 | *lib = LoadLibraryA(filename); |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 79 | return *lib == NULL ? 0 : 1; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym) |
| 83 | { |
| 84 | *sym = (SHLIB_SYM)GetProcAddress(lib, symname); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 85 | return *sym != NULL; |
| 86 | } |
| 87 | |
| 88 | static int shlib_close(SHLIB lib) |
| 89 | { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 90 | return FreeLibrary(lib) == 0 ? 0 : 1; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 91 | } |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 94 | |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 95 | #if defined(DSO_DLFCN) || defined(DSO_WIN32) |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 96 | |
| 97 | static int test_lib(void) |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 98 | { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 99 | SHLIB ssllib = SHLIB_INIT; |
| 100 | SHLIB cryptolib = SHLIB_INIT; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 101 | SSL_CTX *ctx; |
| 102 | union { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 103 | void (*func)(void); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 104 | SHLIB_SYM sym; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 105 | } symbols[3]; |
| 106 | TLS_method_t myTLS_method; |
| 107 | SSL_CTX_new_t mySSL_CTX_new; |
| 108 | SSL_CTX_free_t mySSL_CTX_free; |
| 109 | ERR_get_error_t myERR_get_error; |
| 110 | OpenSSL_version_num_t myOpenSSL_version_num; |
| 111 | int result = 0; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 112 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 113 | switch (test_type) { |
| 114 | case JUST_CRYPTO: |
| 115 | if (!TEST_true(shlib_load(path_crypto, &cryptolib))) |
| 116 | goto end; |
| 117 | break; |
| 118 | case CRYPTO_FIRST: |
| 119 | if (!TEST_true(shlib_load(path_crypto, &cryptolib)) |
| 120 | || !TEST_true(shlib_load(path_ssl, &ssllib))) |
| 121 | goto end; |
Bernd Edlinger | 5511101 | 2017-06-16 16:10:11 -0400 | [diff] [blame] | 122 | break; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 123 | case SSL_FIRST: |
| 124 | if (!TEST_true(shlib_load(path_ssl, &ssllib)) |
| 125 | || !TEST_true(shlib_load(path_crypto, &cryptolib))) |
| 126 | goto end; |
| 127 | break; |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 128 | case DSO_REFTEST: |
| 129 | if (!TEST_true(shlib_load(path_crypto, &cryptolib))) |
| 130 | goto end; |
| 131 | break; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 134 | if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 135 | if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym)) |
| 136 | || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)) |
| 137 | || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym))) |
| 138 | goto end; |
| 139 | myTLS_method = (TLS_method_t)symbols[0].func; |
| 140 | mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func; |
| 141 | mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func; |
| 142 | if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method()))) |
| 143 | goto end; |
| 144 | mySSL_CTX_free(ctx); |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 147 | if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)) |
| 148 | || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num", |
| 149 | &symbols[1].sym))) |
| 150 | goto end; |
| 151 | myERR_get_error = (ERR_get_error_t)symbols[0].func; |
| 152 | if (!TEST_int_eq(myERR_get_error(), 0)) |
| 153 | goto end; |
Richard Levitte | 00c8f1b | 2018-03-14 17:31:20 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * The bits that COMPATIBILITY_MASK lets through MUST be the same in |
| 157 | * the library and in the application. |
| 158 | * The bits that are masked away MUST be a larger or equal number in |
| 159 | * the library compared to the application. |
| 160 | */ |
| 161 | # define COMPATIBILITY_MASK 0xfff00000L |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 162 | myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func; |
Richard Levitte | 00c8f1b | 2018-03-14 17:31:20 +0100 | [diff] [blame] | 163 | if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK, |
Richard Levitte | cfaad17 | 2018-03-23 01:05:41 +0100 | [diff] [blame] | 164 | OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)) |
Richard Levitte | 00c8f1b | 2018-03-14 17:31:20 +0100 | [diff] [blame] | 165 | goto end; |
| 166 | if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK, |
Richard Levitte | cfaad17 | 2018-03-23 01:05:41 +0100 | [diff] [blame] | 167 | OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)) |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 168 | goto end; |
| 169 | |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 170 | if (test_type == DSO_REFTEST) { |
| 171 | # ifdef DSO_DLFCN |
Richard Levitte | 84e68a1 | 2018-03-23 14:18:16 +0100 | [diff] [blame] | 172 | DSO_dsobyaddr_t myDSO_dsobyaddr; |
| 173 | DSO_free_t myDSO_free; |
| 174 | |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 175 | /* |
| 176 | * This is resembling the code used in ossl_init_base() and |
| 177 | * OPENSSL_atexit() to block unloading the library after dlclose(). |
| 178 | * We are not testing this on Windows, because it is done there in a |
| 179 | * completely different way. Especially as a call to DSO_dsobyaddr() |
| 180 | * will always return an error, because DSO_pathbyaddr() is not |
| 181 | * implemented there. |
| 182 | */ |
| 183 | if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)) |
| 184 | || !TEST_true(shlib_sym(cryptolib, "DSO_free", |
| 185 | &symbols[1].sym))) |
| 186 | goto end; |
| 187 | |
| 188 | myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func; |
| 189 | myDSO_free = (DSO_free_t)symbols[1].func; |
| 190 | |
| 191 | { |
| 192 | DSO *hndl; |
| 193 | /* use known symbol from crypto module */ |
Bernd Edlinger | 26db324 | 2018-03-30 19:17:39 +0200 | [diff] [blame] | 194 | if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0))) |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 195 | goto end; |
Richard Levitte | cfaad17 | 2018-03-23 01:05:41 +0100 | [diff] [blame] | 196 | myDSO_free(hndl); |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 197 | } |
| 198 | # endif /* DSO_DLFCN */ |
| 199 | } |
| 200 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 201 | switch (test_type) { |
| 202 | case JUST_CRYPTO: |
| 203 | if (!TEST_true(shlib_close(cryptolib))) |
| 204 | goto end; |
| 205 | break; |
| 206 | case CRYPTO_FIRST: |
| 207 | if (!TEST_true(shlib_close(cryptolib)) |
| 208 | || !TEST_true(shlib_close(ssllib))) |
| 209 | goto end; |
Bernd Edlinger | 5511101 | 2017-06-16 16:10:11 -0400 | [diff] [blame] | 210 | break; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 211 | case SSL_FIRST: |
| 212 | if (!TEST_true(shlib_close(ssllib)) |
| 213 | || !TEST_true(shlib_close(cryptolib))) |
| 214 | goto end; |
| 215 | break; |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 216 | case DSO_REFTEST: |
| 217 | if (!TEST_true(shlib_close(cryptolib))) |
| 218 | goto end; |
| 219 | break; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 222 | result = 1; |
| 223 | end: |
| 224 | return result; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 225 | } |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 226 | #endif |
| 227 | |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 228 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 229 | int setup_tests(void) |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 230 | { |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 231 | const char *p = test_get_argument(0); |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 232 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 233 | if (strcmp(p, "-crypto_first") == 0) { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 234 | test_type = CRYPTO_FIRST; |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 235 | } else if (strcmp(p, "-ssl_first") == 0) { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 236 | test_type = SSL_FIRST; |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 237 | } else if (strcmp(p, "-just_crypto") == 0) { |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 238 | test_type = JUST_CRYPTO; |
Matthias Kraft | 4af14b7 | 2018-03-19 13:37:46 -0400 | [diff] [blame] | 239 | } else if (strcmp(p, "-dso_ref") == 0) { |
| 240 | test_type = JUST_CRYPTO; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 241 | } else { |
| 242 | TEST_error("Unrecognised argument"); |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 243 | return 0; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 244 | } |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 245 | if (!TEST_ptr(path_crypto = test_get_argument(1)) |
| 246 | || !TEST_ptr(path_ssl = test_get_argument(2))) |
| 247 | return 0; |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 248 | |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 249 | #if defined(DSO_DLFCN) || defined(DSO_WIN32) |
Rich Salz | bdd07c7 | 2017-05-05 17:39:13 -0400 | [diff] [blame] | 250 | ADD_TEST(test_lib); |
Rich Salz | e0011aa | 2017-05-06 07:59:18 -0400 | [diff] [blame] | 251 | #endif |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 252 | return 1; |
Matt Caswell | b987d74 | 2016-10-18 14:16:35 +0100 | [diff] [blame] | 253 | } |