00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 205532 $")
00029
00030 #ifdef HAVE_OPENSSL
00031 #include <openssl/ssl.h>
00032 #include <openssl/err.h>
00033 #endif
00034
00035 #include "asterisk/_private.h"
00036
00037 #include "asterisk/utils.h"
00038 #include "asterisk/lock.h"
00039
00040 #ifdef HAVE_OPENSSL
00041
00042 static ast_mutex_t *ssl_locks;
00043
00044 static int ssl_num_locks;
00045
00046 static unsigned long ssl_threadid(void)
00047 {
00048 return (unsigned long)pthread_self();
00049 }
00050
00051 static void ssl_lock(int mode, int n, const char *file, int line)
00052 {
00053 if (n < 0 || n >= ssl_num_locks) {
00054 ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
00055 "ssl_num_locks '%d' - n '%d'\n",
00056 ssl_num_locks, n);
00057 return;
00058 }
00059
00060 if (mode & CRYPTO_LOCK) {
00061 ast_mutex_lock(&ssl_locks[n]);
00062 } else {
00063 ast_mutex_unlock(&ssl_locks[n]);
00064 }
00065 }
00066
00067 #endif
00068
00069
00070
00071
00072
00073 int ast_ssl_init(void)
00074 {
00075 #ifdef HAVE_OPENSSL
00076 unsigned int i;
00077
00078 SSL_library_init();
00079 SSL_load_error_strings();
00080 ERR_load_crypto_strings();
00081 ERR_load_BIO_strings();
00082 OpenSSL_add_all_algorithms();
00083
00084
00085
00086 CRYPTO_set_id_callback(ssl_threadid);
00087
00088 ssl_num_locks = CRYPTO_num_locks();
00089 if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
00090 return -1;
00091 }
00092 for (i = 0; i < ssl_num_locks; i++) {
00093 ast_mutex_init(&ssl_locks[i]);
00094 }
00095 CRYPTO_set_locking_callback(ssl_lock);
00096
00097 #endif
00098 return 0;
00099 }
00100