| 1 | /** |
| 2 | * PHP doesn't support user land threads but we still have to initialize |
| 3 | * the library. |
| 4 | * |
| 5 | * Use following link for additional details: |
| 6 | * 'https://www.openssl.org/docs/man1.0.1/crypto/threads.html' |
| 7 | * 'https://wiki.openssl.org/index.php/Library_Initialization' |
| 8 | |
| 9 | */ |
| 10 | |
| 11 | #include <assert.h> |
| 12 | #include <openssl/crypto.h> |
| 13 | #include <openssl/err.h> |
| 14 | #include <openssl/ssl.h> |
| 15 | |
| 16 | void __attribute__((constructor)) TCLink_OpenSSLInit(void); |
| 17 | void __attribute__((destructor)) TCLink_OpenSSLCleanup(void); |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * Initialize the OpenSSL library. |
| 22 | * Also sets up static callback functions required for multi-thread safety. |
| 23 | */ |
| 24 | void |
| 25 | TCLink_OpenSSLInit(void) |
| 26 | { |
| 27 | int ret; |
| 28 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 29 | SSL_load_error_strings(); |
| 30 | ret = SSL_library_init(); |
| 31 | assert(ret == 1); |
| 32 | #else |
| 33 | ret = OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | |
| 34 | OPENSSL_INIT_ADD_ALL_CIPHERS | |
| 35 | OPENSSL_INIT_ADD_ALL_DIGESTS | |
| 36 | OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_ASYNC | |
| 37 | #ifdef OPENSSL_INIT_NO_ATEXIT |
| 38 | OPENSSL_INIT_NO_ATEXIT | |
| 39 | #endif |
| 40 | #ifdef OPENSSL_INIT_ATFORK |
| 41 | OPENSSL_INIT_ATFORK | |
| 42 | #endif |
| 43 | OPENSSL_INIT_LOAD_SSL_STRINGS, |
| 44 | NULL); |
| 45 | assert(ret == 1); |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * De-initializes the OpenSSL library. |
| 52 | * Performs cleanup required for global data structures. |
| 53 | */ |
| 54 | void |
| 55 | TCLink_OpenSSLCleanup(void) |
| 56 | { |
| 57 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 58 | EVP_cleanup(); |
| 59 | CRYPTO_cleanup_all_ex_data(); |
| 60 | ERR_free_strings(); |
| 61 | #else |
| 62 | OPENSSL_cleanup(); |
| 63 | #endif |
| 64 | } |