Thu Apr 28 2011 16:56:49

Asterisk developer's documentation


ssl.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! 
00020  * \file
00021  * \brief Common OpenSSL support code
00022  *
00023  * \author Russell Bryant <russell@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 205535 $")
00029 
00030 #ifdef HAVE_OPENSSL
00031 #include <openssl/ssl.h>
00032 #include <openssl/err.h>
00033 #endif
00034 
00035 #include "asterisk/_private.h" /* ast_ssl_init() */
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 /* HAVE_OPENSSL */
00068 
00069 /*!
00070  * \internal
00071  * \brief Common OpenSSL initialization for all of Asterisk.
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    /* Make OpenSSL thread-safe. */
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 /* HAVE_OPENSSL */
00098    return 0;
00099 }
00100