1 /* 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. 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> 14 15 /* The test is only currently implemented for DSO_DLFCN and DSO_WIN32 */ 16 #if defined(DSO_DLFCN) || defined(DSO_WIN32) 17 18 #define SSL_CTX_NEW "SSL_CTX_new" 19 #define SSL_CTX_FREE "SSL_CTX_free" 20 #define TLS_METHOD "TLS_method" 21 22 #define ERR_GET_ERROR "ERR_get_error" 23 #define OPENSSL_VERSION_NUM_FUNC "OpenSSL_version_num" 24 25 typedef struct ssl_ctx_st SSL_CTX; 26 typedef struct ssl_method_st SSL_METHOD; 27 typedef const SSL_METHOD * (*TLS_method_t)(void); 28 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth); 29 typedef void (*SSL_CTX_free_t)(SSL_CTX *); 30 31 typedef unsigned long (*ERR_get_error_t)(void); 32 typedef unsigned long (*OpenSSL_version_num_t)(void); 33 34 static TLS_method_t TLS_method; 35 static SSL_CTX_new_t SSL_CTX_new; 36 static SSL_CTX_free_t SSL_CTX_free; 37 38 static ERR_get_error_t ERR_get_error; 39 static OpenSSL_version_num_t OpenSSL_version_num; 40 41 #ifdef DSO_DLFCN 42 43 # include <dlfcn.h> 44 45 typedef void * SHLIB; 46 typedef void * SHLIB_SYM; 47 # define SHLIB_INIT NULL 48 49 static int shlib_load(const char *filename, SHLIB *lib) 50 { 51 *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); 52 53 if (*lib == NULL) 54 return 0; 55 56 return 1; 57 } 58 59 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym) 60 { 61 *sym = dlsym(lib, symname); 62 63 return *sym != NULL; 64 } 65 66 static int shlib_close(SHLIB lib) 67 { 68 if (dlclose(lib) != 0) 69 return 0; 70 71 return 1; 72 } 73 74 #elif defined(DSO_WIN32) 75 76 # include <windows.h> 77 78 typedef HINSTANCE SHLIB; 79 typedef void * SHLIB_SYM; 80 # define SHLIB_INIT 0 81 82 static int shlib_load(const char *filename, SHLIB *lib) 83 { 84 *lib = LoadLibraryA(filename); 85 if (*lib == NULL) 86 return 0; 87 88 return 1; 89 } 90 91 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym) 92 { 93 *sym = (SHLIB_SYM)GetProcAddress(lib, symname); 94 95 return *sym != NULL; 96 } 97 98 static int shlib_close(SHLIB lib) 99 { 100 if (FreeLibrary(lib) == 0) 101 return 0; 102 103 return 1; 104 } 105 106 #endif 107 108 # define CRYPTO_FIRST_OPT "-crypto_first" 109 # define SSL_FIRST_OPT "-ssl_first" 110 # define JUST_CRYPTO_OPT "-just_crypto" 111 112 enum test_types_en { 113 CRYPTO_FIRST, 114 SSL_FIRST, 115 JUST_CRYPTO 116 }; 117 118 int main(int argc, char **argv) 119 { 120 SHLIB ssllib = SHLIB_INIT, cryptolib = SHLIB_INIT; 121 SSL_CTX *ctx; 122 union { 123 void (*func) (void); 124 SHLIB_SYM sym; 125 } tls_method_sym, ssl_ctx_new_sym, ssl_ctx_free_sym, err_get_error_sym, 126 openssl_version_num_sym; 127 enum test_types_en test_type; 128 int i; 129 130 if (argc != 4) { 131 printf("Unexpected number of arguments\n"); 132 return 1; 133 } 134 135 if (strcmp(argv[1], CRYPTO_FIRST_OPT) == 0) { 136 test_type = CRYPTO_FIRST; 137 } else if (strcmp(argv[1], SSL_FIRST_OPT) == 0) { 138 test_type = SSL_FIRST; 139 } else if (strcmp(argv[1], JUST_CRYPTO_OPT) == 0) { 140 test_type = JUST_CRYPTO; 141 } else { 142 printf("Unrecognised argument\n"); 143 return 1; 144 } 145 146 for (i = 0; i < 2; i++) { 147 if ((i == 0 && (test_type == CRYPTO_FIRST 148 || test_type == JUST_CRYPTO)) 149 || (i == 1 && test_type == SSL_FIRST)) { 150 if (!shlib_load(argv[2], &cryptolib)) { 151 printf("Unable to load libcrypto\n"); 152 return 1; 153 } 154 } 155 if ((i == 0 && test_type == SSL_FIRST) 156 || (i == 1 && test_type == CRYPTO_FIRST)) { 157 if (!shlib_load(argv[3], &ssllib)) { 158 printf("Unable to load libssl\n"); 159 return 1; 160 } 161 } 162 } 163 164 if (test_type != JUST_CRYPTO) { 165 if (!shlib_sym(ssllib, TLS_METHOD, &tls_method_sym.sym) 166 || !shlib_sym(ssllib, SSL_CTX_NEW, &ssl_ctx_new_sym.sym) 167 || !shlib_sym(ssllib, SSL_CTX_FREE, &ssl_ctx_free_sym.sym)) { 168 printf("Unable to load ssl symbols\n"); 169 return 1; 170 } 171 172 TLS_method = (TLS_method_t)tls_method_sym.func; 173 SSL_CTX_new = (SSL_CTX_new_t)ssl_ctx_new_sym.func; 174 SSL_CTX_free = (SSL_CTX_free_t)ssl_ctx_free_sym.func; 175 176 ctx = SSL_CTX_new(TLS_method()); 177 if (ctx == NULL) { 178 printf("Unable to create SSL_CTX\n"); 179 return 1; 180 } 181 SSL_CTX_free(ctx); 182 } 183 184 if (!shlib_sym(cryptolib, ERR_GET_ERROR, &err_get_error_sym.sym) 185 || !shlib_sym(cryptolib, OPENSSL_VERSION_NUM_FUNC, 186 &openssl_version_num_sym.sym)) { 187 printf("Unable to load crypto symbols\n"); 188 return 1; 189 } 190 191 ERR_get_error = (ERR_get_error_t)err_get_error_sym.func; 192 OpenSSL_version_num = (OpenSSL_version_num_t)openssl_version_num_sym.func; 193 194 if (ERR_get_error() != 0) { 195 printf("Unexpected error in error queue\n"); 196 return 1; 197 } 198 199 /* 200 * The bits that COMPATIBILITY_MASK lets through MUST be the same in 201 * the library and in the application. 202 * The bits that are masked away MUST be a larger or equal number in 203 * the library compared to the application. 204 */ 205 # define COMPATIBILITY_MASK 0xfff00000L 206 if ((OpenSSL_version_num() & COMPATIBILITY_MASK) 207 != (OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK)) { 208 printf("Unexpected library version loaded\n"); 209 return 1; 210 } 211 212 if ((OpenSSL_version_num() & ~COMPATIBILITY_MASK) 213 < (OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK)) { 214 printf("Unexpected library version loaded\n"); 215 return 1; 216 } 217 218 for (i = 0; i < 2; i++) { 219 if ((i == 0 && test_type == CRYPTO_FIRST) 220 || (i == 1 && test_type == SSL_FIRST)) { 221 if (!shlib_close(ssllib)) { 222 printf("Unable to close libssl\n"); 223 return 1; 224 } 225 } 226 if ((i == 0 && (test_type == SSL_FIRST 227 || test_type == JUST_CRYPTO)) 228 || (i == 1 && test_type == CRYPTO_FIRST)) { 229 if (!shlib_close(cryptolib)) { 230 printf("Unable to close libcrypto\n"); 231 return 1; 232 } 233 } 234 } 235 236 printf("Success\n"); 237 return 0; 238 } 239 #else 240 int main(void) 241 { 242 printf("Test not implemented on this platform\n"); 243 return 0; 244 } 245 #endif 246