Mon Oct 8 12:39:05 2012

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 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00033 
00034 #ifdef HAVE_OPENSSL
00035 #include <openssl/ssl.h>
00036 #include <openssl/err.h>
00037 #endif
00038 
00039 #include "asterisk/_private.h" /* ast_ssl_init() */
00040 
00041 #include "asterisk/utils.h"
00042 #include "asterisk/lock.h"
00043 
00044 #ifdef HAVE_OPENSSL
00045 
00046 static ast_mutex_t *ssl_locks;
00047 
00048 static int ssl_num_locks;
00049 
00050 static unsigned long ssl_threadid(void)
00051 {
00052    return (unsigned long)pthread_self();
00053 }
00054 
00055 static void ssl_lock(int mode, int n, const char *file, int line)
00056 {
00057    if (n < 0 || n >= ssl_num_locks) {
00058       ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
00059             "ssl_num_locks '%d' - n '%d'\n",
00060             ssl_num_locks, n);
00061       return;
00062    }
00063 
00064    if (mode & CRYPTO_LOCK) {
00065       ast_mutex_lock(&ssl_locks[n]);
00066    } else {
00067       ast_mutex_unlock(&ssl_locks[n]);
00068    }
00069 }
00070 
00071 #endif /* HAVE_OPENSSL */
00072 
00073 /*!
00074  * \internal
00075  * \brief Common OpenSSL initialization for all of Asterisk.
00076  */
00077 int ast_ssl_init(void)
00078 {
00079 #ifdef HAVE_OPENSSL
00080    unsigned int i;
00081 
00082    SSL_library_init();
00083    SSL_load_error_strings();
00084    ERR_load_crypto_strings();
00085    ERR_load_BIO_strings();
00086    OpenSSL_add_all_algorithms();
00087 
00088    /* Make OpenSSL thread-safe. */
00089 
00090    CRYPTO_set_id_callback(ssl_threadid);
00091 
00092    ssl_num_locks = CRYPTO_num_locks();
00093    if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
00094       return -1;
00095    }
00096    for (i = 0; i < ssl_num_locks; i++) {
00097       ast_mutex_init(&ssl_locks[i]);
00098    }
00099    CRYPTO_set_locking_callback(ssl_lock);
00100 
00101 #endif /* HAVE_OPENSSL */
00102    return 0;
00103 }
00104 

Generated on Mon Oct 8 12:39:05 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7