| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "asterisk/_private.h" |
| 29 |
|
| 30 |
#ifdef HAVE_OPENSSL |
| 31 |
#include <openssl/opensslv.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined(HAVE_OPENSSL) && \ |
| 35 |
(defined(LIBRESSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 36 |
|
| 37 |
#include <dlfcn.h> |
| 38 |
#include <openssl/crypto.h> |
| 39 |
#include <openssl/err.h> |
| 40 |
#include <openssl/ssl.h> |
| 41 |
#if OPENSSL_VERSION_NUMBER < 0x10000000L |
| 42 |
#include <pthread.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include "asterisk/lock.h" |
| 46 |
#include "asterisk/logger.h" |
| 47 |
#include "asterisk/utils.h" |
| 48 |
|
| 49 |
#define get_OpenSSL_function(func) do { real_##func = dlsym(RTLD_NEXT, __stringify(func)); } while(0) |
| 50 |
|
| 51 |
static int startup_complete; |
| 52 |
|
| 53 |
static ast_mutex_t *ssl_locks; |
| 54 |
|
| 55 |
static int ssl_num_locks; |
| 56 |
|
| 57 |
#if OPENSSL_VERSION_NUMBER < 0x10000000L |
| 58 |
static unsigned long ssl_threadid(void) |
| 59 |
{ |
| 60 |
return (unsigned long) pthread_self(); |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
static void ssl_lock(int mode, int n, const char *file, int line) |
| 65 |
{ |
| 66 |
if (n < 0 || n >= ssl_num_locks) { |
| 67 |
ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - " |
| 68 |
"ssl_num_locks '%d' - n '%d'\n", |
| 69 |
ssl_num_locks, n); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (mode & 0x1) { |
| 74 |
ast_mutex_lock(&ssl_locks[n]); |
| 75 |
} else { |
| 76 |
ast_mutex_unlock(&ssl_locks[n]); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
int SSL_library_init(void) |
| 81 |
{ |
| 82 |
#if defined(AST_DEVMODE) |
| 83 |
if (startup_complete) { |
| 84 |
ast_debug(1, "Called after startup... ignoring!\n"); |
| 85 |
} |
| 86 |
#endif |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
void SSL_load_error_strings(void) |
| 91 |
{ |
| 92 |
#if defined(AST_DEVMODE) |
| 93 |
if (startup_complete) { |
| 94 |
ast_debug(1, "Called after startup... ignoring!\n"); |
| 95 |
} |
| 96 |
#endif |
| 97 |
} |
| 98 |
|
| 99 |
#if OPENSSL_VERSION_NUMBER < 0x10000000L |
| 100 |
void CRYPTO_set_id_callback(unsigned long (*func)(void)) |
| 101 |
{ |
| 102 |
#if defined(AST_DEVMODE) |
| 103 |
if (startup_complete) { |
| 104 |
ast_debug(1, "Called after startup... ignoring!\n"); |
| 105 |
} |
| 106 |
#endif |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
void CRYPTO_set_locking_callback(void (*func)(int mode,int type, const char *file, int line)) |
| 111 |
{ |
| 112 |
#if defined(AST_DEVMODE) |
| 113 |
if (startup_complete) { |
| 114 |
ast_debug(1, "Called after startup... ignoring!\n"); |
| 115 |
} |
| 116 |
#endif |
| 117 |
} |
| 118 |
|
| 119 |
void ERR_free_strings(void) |
| 120 |
{ |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
int ast_ssl_init(void) |
| 131 |
{ |
| 132 |
unsigned int i; |
| 133 |
int (*real_SSL_library_init)(void); |
| 134 |
#if OPENSSL_VERSION_NUMBER < 0x10000000L |
| 135 |
void (*real_CRYPTO_set_id_callback)(unsigned long (*)(void)); |
| 136 |
#endif |
| 137 |
void (*real_CRYPTO_set_locking_callback)(void (*)(int, int, const char *, int)); |
| 138 |
void (*real_SSL_load_error_strings)(void); |
| 139 |
const char *errstr; |
| 140 |
|
| 141 |
|
| 142 |
dlerror(); |
| 143 |
get_OpenSSL_function(SSL_library_init); |
| 144 |
if ((errstr = dlerror()) != NULL) { |
| 145 |
ast_debug(1, "unable to get real address of SSL_library_init: %s\n", errstr); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
return -1; |
| 150 |
} else { |
| 151 |
real_SSL_library_init(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#if OPENSSL_VERSION_NUMBER < 0x10000000L |
| 157 |
dlerror(); |
| 158 |
get_OpenSSL_function(CRYPTO_set_id_callback); |
| 159 |
if ((errstr = dlerror()) != NULL) { |
| 160 |
ast_debug(1, "unable to get real address of CRYPTO_set_id_callback: %s\n", errstr); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
return -1; |
| 165 |
} else { |
| 166 |
real_CRYPTO_set_id_callback(ssl_threadid); |
| 167 |
} |
| 168 |
#endif |
| 169 |
|
| 170 |
dlerror(); |
| 171 |
get_OpenSSL_function(CRYPTO_set_locking_callback); |
| 172 |
if ((errstr = dlerror()) != NULL) { |
| 173 |
ast_debug(1, "unable to get real address of CRYPTO_set_locking_callback: %s\n", errstr); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
return -1; |
| 178 |
} else { |
| 179 |
ssl_num_locks = CRYPTO_num_locks(); |
| 180 |
if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) { |
| 181 |
return -1; |
| 182 |
} |
| 183 |
for (i = 0; i < ssl_num_locks; i++) { |
| 184 |
ast_mutex_init(&ssl_locks[i]); |
| 185 |
} |
| 186 |
real_CRYPTO_set_locking_callback(ssl_lock); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
get_OpenSSL_function(SSL_load_error_strings); |
| 196 |
real_SSL_load_error_strings(); |
| 197 |
|
| 198 |
startup_complete = 1; |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
#else |
| 204 |
|
| 205 |
int ast_ssl_init(void) |
| 206 |
{ |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
#endif |