1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include <string.h> 12e1051a39Sopenharmony_ci#include <stdlib.h> 13e1051a39Sopenharmony_ci#include <openssl/opensslv.h> 14e1051a39Sopenharmony_ci#include <openssl/ssl.h> 15e1051a39Sopenharmony_ci#include <openssl/types.h> 16e1051a39Sopenharmony_ci#include "simpledynamic.h" 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_citypedef void DSO; 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_citypedef const SSL_METHOD * (*TLS_method_t)(void); 21e1051a39Sopenharmony_citypedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth); 22e1051a39Sopenharmony_citypedef void (*SSL_CTX_free_t)(SSL_CTX *); 23e1051a39Sopenharmony_citypedef int (*OPENSSL_init_crypto_t)(uint64_t, void *); 24e1051a39Sopenharmony_citypedef int (*OPENSSL_atexit_t)(void (*handler)(void)); 25e1051a39Sopenharmony_citypedef unsigned long (*ERR_get_error_t)(void); 26e1051a39Sopenharmony_citypedef unsigned long (*OPENSSL_version_major_t)(void); 27e1051a39Sopenharmony_citypedef unsigned long (*OPENSSL_version_minor_t)(void); 28e1051a39Sopenharmony_citypedef unsigned long (*OPENSSL_version_patch_t)(void); 29e1051a39Sopenharmony_citypedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags); 30e1051a39Sopenharmony_citypedef int (*DSO_free_t)(DSO *dso); 31e1051a39Sopenharmony_ci 32e1051a39Sopenharmony_citypedef enum test_types_en { 33e1051a39Sopenharmony_ci CRYPTO_FIRST, 34e1051a39Sopenharmony_ci SSL_FIRST, 35e1051a39Sopenharmony_ci JUST_CRYPTO, 36e1051a39Sopenharmony_ci DSO_REFTEST, 37e1051a39Sopenharmony_ci NO_ATEXIT 38e1051a39Sopenharmony_ci} TEST_TYPE; 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_cistatic TEST_TYPE test_type; 41e1051a39Sopenharmony_cistatic const char *path_crypto; 42e1051a39Sopenharmony_cistatic const char *path_ssl; 43e1051a39Sopenharmony_cistatic const char *path_atexit; 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci#ifdef SD_INIT 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_cistatic int atexit_handler_done = 0; 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_cistatic void atexit_handler(void) 50e1051a39Sopenharmony_ci{ 51e1051a39Sopenharmony_ci FILE *atexit_file = fopen(path_atexit, "w"); 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci if (atexit_file == NULL) 54e1051a39Sopenharmony_ci return; 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ci fprintf(atexit_file, "atexit() run\n"); 57e1051a39Sopenharmony_ci fclose(atexit_file); 58e1051a39Sopenharmony_ci atexit_handler_done++; 59e1051a39Sopenharmony_ci} 60e1051a39Sopenharmony_ci 61e1051a39Sopenharmony_cistatic int test_lib(void) 62e1051a39Sopenharmony_ci{ 63e1051a39Sopenharmony_ci SD ssllib = SD_INIT; 64e1051a39Sopenharmony_ci SD cryptolib = SD_INIT; 65e1051a39Sopenharmony_ci SSL_CTX *ctx; 66e1051a39Sopenharmony_ci union { 67e1051a39Sopenharmony_ci void (*func)(void); 68e1051a39Sopenharmony_ci SD_SYM sym; 69e1051a39Sopenharmony_ci } symbols[5]; 70e1051a39Sopenharmony_ci TLS_method_t myTLS_method; 71e1051a39Sopenharmony_ci SSL_CTX_new_t mySSL_CTX_new; 72e1051a39Sopenharmony_ci SSL_CTX_free_t mySSL_CTX_free; 73e1051a39Sopenharmony_ci ERR_get_error_t myERR_get_error; 74e1051a39Sopenharmony_ci OPENSSL_version_major_t myOPENSSL_version_major; 75e1051a39Sopenharmony_ci OPENSSL_version_minor_t myOPENSSL_version_minor; 76e1051a39Sopenharmony_ci OPENSSL_version_patch_t myOPENSSL_version_patch; 77e1051a39Sopenharmony_ci OPENSSL_atexit_t myOPENSSL_atexit; 78e1051a39Sopenharmony_ci int result = 0; 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci switch (test_type) { 81e1051a39Sopenharmony_ci case JUST_CRYPTO: 82e1051a39Sopenharmony_ci case DSO_REFTEST: 83e1051a39Sopenharmony_ci case NO_ATEXIT: 84e1051a39Sopenharmony_ci case CRYPTO_FIRST: 85e1051a39Sopenharmony_ci if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { 86e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load libcrypto\n"); 87e1051a39Sopenharmony_ci goto end; 88e1051a39Sopenharmony_ci } 89e1051a39Sopenharmony_ci if (test_type != CRYPTO_FIRST) 90e1051a39Sopenharmony_ci break; 91e1051a39Sopenharmony_ci /* Fall through */ 92e1051a39Sopenharmony_ci 93e1051a39Sopenharmony_ci case SSL_FIRST: 94e1051a39Sopenharmony_ci if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) { 95e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load libssl\n"); 96e1051a39Sopenharmony_ci goto end; 97e1051a39Sopenharmony_ci } 98e1051a39Sopenharmony_ci if (test_type != SSL_FIRST) 99e1051a39Sopenharmony_ci break; 100e1051a39Sopenharmony_ci if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { 101e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load libcrypto\n"); 102e1051a39Sopenharmony_ci goto end; 103e1051a39Sopenharmony_ci } 104e1051a39Sopenharmony_ci break; 105e1051a39Sopenharmony_ci } 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_ci if (test_type == NO_ATEXIT) { 108e1051a39Sopenharmony_ci OPENSSL_init_crypto_t myOPENSSL_init_crypto; 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) { 111e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n"); 112e1051a39Sopenharmony_ci goto end; 113e1051a39Sopenharmony_ci } 114e1051a39Sopenharmony_ci myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func; 115e1051a39Sopenharmony_ci if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) { 116e1051a39Sopenharmony_ci fprintf(stderr, "Failed to initialise libcrypto\n"); 117e1051a39Sopenharmony_ci goto end; 118e1051a39Sopenharmony_ci } 119e1051a39Sopenharmony_ci } 120e1051a39Sopenharmony_ci 121e1051a39Sopenharmony_ci if (test_type != JUST_CRYPTO 122e1051a39Sopenharmony_ci && test_type != DSO_REFTEST 123e1051a39Sopenharmony_ci && test_type != NO_ATEXIT) { 124e1051a39Sopenharmony_ci if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym) 125e1051a39Sopenharmony_ci || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym) 126e1051a39Sopenharmony_ci || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) { 127e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load libssl symbols\n"); 128e1051a39Sopenharmony_ci goto end; 129e1051a39Sopenharmony_ci } 130e1051a39Sopenharmony_ci myTLS_method = (TLS_method_t)symbols[0].func; 131e1051a39Sopenharmony_ci mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func; 132e1051a39Sopenharmony_ci mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func; 133e1051a39Sopenharmony_ci ctx = mySSL_CTX_new(myTLS_method()); 134e1051a39Sopenharmony_ci if (ctx == NULL) { 135e1051a39Sopenharmony_ci fprintf(stderr, "Failed to create SSL_CTX\n"); 136e1051a39Sopenharmony_ci goto end; 137e1051a39Sopenharmony_ci } 138e1051a39Sopenharmony_ci mySSL_CTX_free(ctx); 139e1051a39Sopenharmony_ci } 140e1051a39Sopenharmony_ci 141e1051a39Sopenharmony_ci if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym) 142e1051a39Sopenharmony_ci || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym) 143e1051a39Sopenharmony_ci || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym) 144e1051a39Sopenharmony_ci || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym) 145e1051a39Sopenharmony_ci || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) { 146e1051a39Sopenharmony_ci fprintf(stderr, "Failed to load libcrypto symbols\n"); 147e1051a39Sopenharmony_ci goto end; 148e1051a39Sopenharmony_ci } 149e1051a39Sopenharmony_ci myERR_get_error = (ERR_get_error_t)symbols[0].func; 150e1051a39Sopenharmony_ci if (myERR_get_error() != 0) { 151e1051a39Sopenharmony_ci fprintf(stderr, "Unexpected ERR_get_error() response\n"); 152e1051a39Sopenharmony_ci goto end; 153e1051a39Sopenharmony_ci } 154e1051a39Sopenharmony_ci 155e1051a39Sopenharmony_ci /* Library and header version should be identical in this test */ 156e1051a39Sopenharmony_ci myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func; 157e1051a39Sopenharmony_ci myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func; 158e1051a39Sopenharmony_ci myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func; 159e1051a39Sopenharmony_ci if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR 160e1051a39Sopenharmony_ci || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR 161e1051a39Sopenharmony_ci || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) { 162e1051a39Sopenharmony_ci fprintf(stderr, "Invalid library version number\n"); 163e1051a39Sopenharmony_ci goto end; 164e1051a39Sopenharmony_ci } 165e1051a39Sopenharmony_ci 166e1051a39Sopenharmony_ci myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func; 167e1051a39Sopenharmony_ci if (!myOPENSSL_atexit(atexit_handler)) { 168e1051a39Sopenharmony_ci fprintf(stderr, "Failed to register atexit handler\n"); 169e1051a39Sopenharmony_ci goto end; 170e1051a39Sopenharmony_ci } 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ci if (test_type == DSO_REFTEST) { 173e1051a39Sopenharmony_ci# ifdef DSO_DLFCN 174e1051a39Sopenharmony_ci DSO_dsobyaddr_t myDSO_dsobyaddr; 175e1051a39Sopenharmony_ci DSO_free_t myDSO_free; 176e1051a39Sopenharmony_ci 177e1051a39Sopenharmony_ci /* 178e1051a39Sopenharmony_ci * This is resembling the code used in ossl_init_base() and 179e1051a39Sopenharmony_ci * OPENSSL_atexit() to block unloading the library after dlclose(). 180e1051a39Sopenharmony_ci * We are not testing this on Windows, because it is done there in a 181e1051a39Sopenharmony_ci * completely different way. Especially as a call to DSO_dsobyaddr() 182e1051a39Sopenharmony_ci * will always return an error, because DSO_pathbyaddr() is not 183e1051a39Sopenharmony_ci * implemented there. 184e1051a39Sopenharmony_ci */ 185e1051a39Sopenharmony_ci if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym) 186e1051a39Sopenharmony_ci || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) { 187e1051a39Sopenharmony_ci fprintf(stderr, "Unable to load DSO symbols\n"); 188e1051a39Sopenharmony_ci goto end; 189e1051a39Sopenharmony_ci } 190e1051a39Sopenharmony_ci 191e1051a39Sopenharmony_ci myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func; 192e1051a39Sopenharmony_ci myDSO_free = (DSO_free_t)symbols[1].func; 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci { 195e1051a39Sopenharmony_ci DSO *hndl; 196e1051a39Sopenharmony_ci /* use known symbol from crypto module */ 197e1051a39Sopenharmony_ci hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0); 198e1051a39Sopenharmony_ci if (hndl == NULL) { 199e1051a39Sopenharmony_ci fprintf(stderr, "DSO_dsobyaddr() failed\n"); 200e1051a39Sopenharmony_ci goto end; 201e1051a39Sopenharmony_ci } 202e1051a39Sopenharmony_ci myDSO_free(hndl); 203e1051a39Sopenharmony_ci } 204e1051a39Sopenharmony_ci# endif /* DSO_DLFCN */ 205e1051a39Sopenharmony_ci } 206e1051a39Sopenharmony_ci 207e1051a39Sopenharmony_ci if (!sd_close(cryptolib)) { 208e1051a39Sopenharmony_ci fprintf(stderr, "Failed to close libcrypto\n"); 209e1051a39Sopenharmony_ci goto end; 210e1051a39Sopenharmony_ci } 211e1051a39Sopenharmony_ci cryptolib = SD_INIT; 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) { 214e1051a39Sopenharmony_ci if (!sd_close(ssllib)) { 215e1051a39Sopenharmony_ci fprintf(stderr, "Failed to close libssl\n"); 216e1051a39Sopenharmony_ci goto end; 217e1051a39Sopenharmony_ci } 218e1051a39Sopenharmony_ci ssllib = SD_INIT; 219e1051a39Sopenharmony_ci } 220e1051a39Sopenharmony_ci 221e1051a39Sopenharmony_ci# if defined(OPENSSL_NO_PINSHARED) \ 222e1051a39Sopenharmony_ci && defined(__GLIBC__) \ 223e1051a39Sopenharmony_ci && defined(__GLIBC_PREREQ) \ 224e1051a39Sopenharmony_ci && defined(OPENSSL_SYS_LINUX) 225e1051a39Sopenharmony_ci# if __GLIBC_PREREQ(2, 3) 226e1051a39Sopenharmony_ci /* 227e1051a39Sopenharmony_ci * If we didn't pin the so then we are hopefully on a platform that supports 228e1051a39Sopenharmony_ci * running atexit() on so unload. If not we might crash. We know this is 229e1051a39Sopenharmony_ci * true on linux since glibc 2.2.3 230e1051a39Sopenharmony_ci */ 231e1051a39Sopenharmony_ci if (test_type != NO_ATEXIT && atexit_handler_done != 1) { 232e1051a39Sopenharmony_ci fprintf(stderr, "atexit() handler did not run\n"); 233e1051a39Sopenharmony_ci goto end; 234e1051a39Sopenharmony_ci } 235e1051a39Sopenharmony_ci# endif 236e1051a39Sopenharmony_ci# endif 237e1051a39Sopenharmony_ci 238e1051a39Sopenharmony_ci result = 1; 239e1051a39Sopenharmony_ciend: 240e1051a39Sopenharmony_ci if (cryptolib != SD_INIT) 241e1051a39Sopenharmony_ci sd_close(cryptolib); 242e1051a39Sopenharmony_ci if (ssllib != SD_INIT) 243e1051a39Sopenharmony_ci sd_close(ssllib); 244e1051a39Sopenharmony_ci return result; 245e1051a39Sopenharmony_ci} 246e1051a39Sopenharmony_ci#endif 247e1051a39Sopenharmony_ci 248e1051a39Sopenharmony_ci 249e1051a39Sopenharmony_ci/* 250e1051a39Sopenharmony_ci * shlibloadtest should not use the normal test framework because we don't want 251e1051a39Sopenharmony_ci * it to link against libcrypto (which the framework uses). The point of the 252e1051a39Sopenharmony_ci * test is to check dynamic loading and unloading of libcrypto/libssl. 253e1051a39Sopenharmony_ci */ 254e1051a39Sopenharmony_ciint main(int argc, char *argv[]) 255e1051a39Sopenharmony_ci{ 256e1051a39Sopenharmony_ci const char *p; 257e1051a39Sopenharmony_ci 258e1051a39Sopenharmony_ci if (argc != 5) { 259e1051a39Sopenharmony_ci fprintf(stderr, "Incorrect number of arguments\n"); 260e1051a39Sopenharmony_ci return 1; 261e1051a39Sopenharmony_ci } 262e1051a39Sopenharmony_ci 263e1051a39Sopenharmony_ci p = argv[1]; 264e1051a39Sopenharmony_ci 265e1051a39Sopenharmony_ci if (strcmp(p, "-crypto_first") == 0) { 266e1051a39Sopenharmony_ci test_type = CRYPTO_FIRST; 267e1051a39Sopenharmony_ci } else if (strcmp(p, "-ssl_first") == 0) { 268e1051a39Sopenharmony_ci test_type = SSL_FIRST; 269e1051a39Sopenharmony_ci } else if (strcmp(p, "-just_crypto") == 0) { 270e1051a39Sopenharmony_ci test_type = JUST_CRYPTO; 271e1051a39Sopenharmony_ci } else if (strcmp(p, "-dso_ref") == 0) { 272e1051a39Sopenharmony_ci test_type = DSO_REFTEST; 273e1051a39Sopenharmony_ci } else if (strcmp(p, "-no_atexit") == 0) { 274e1051a39Sopenharmony_ci test_type = NO_ATEXIT; 275e1051a39Sopenharmony_ci } else { 276e1051a39Sopenharmony_ci fprintf(stderr, "Unrecognised argument\n"); 277e1051a39Sopenharmony_ci return 1; 278e1051a39Sopenharmony_ci } 279e1051a39Sopenharmony_ci path_crypto = argv[2]; 280e1051a39Sopenharmony_ci path_ssl = argv[3]; 281e1051a39Sopenharmony_ci path_atexit = argv[4]; 282e1051a39Sopenharmony_ci if (path_crypto == NULL || path_ssl == NULL) { 283e1051a39Sopenharmony_ci fprintf(stderr, "Invalid libcrypto/libssl path\n"); 284e1051a39Sopenharmony_ci return 1; 285e1051a39Sopenharmony_ci } 286e1051a39Sopenharmony_ci 287e1051a39Sopenharmony_ci#ifdef SD_INIT 288e1051a39Sopenharmony_ci if (!test_lib()) 289e1051a39Sopenharmony_ci return 1; 290e1051a39Sopenharmony_ci#endif 291e1051a39Sopenharmony_ci return 0; 292e1051a39Sopenharmony_ci} 293