2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Common OpenSSL support code
23 * \author Russell Bryant <russell@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <openssl/ssl.h>
32 #include <openssl/err.h>
35 #include "asterisk/_private.h" /* ast_ssl_init() */
37 #include "asterisk/utils.h"
38 #include "asterisk/lock.h"
42 static ast_mutex_t *ssl_locks;
44 static int ssl_num_locks;
46 static unsigned long ssl_threadid(void)
48 return (unsigned long)pthread_self();
51 static void ssl_lock(int mode, int n, const char *file, int line)
53 if (n < 0 || n >= ssl_num_locks) {
54 ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
55 "ssl_num_locks '%d' - n '%d'\n",
60 if (mode & CRYPTO_LOCK) {
61 ast_mutex_lock(&ssl_locks[n]);
63 ast_mutex_unlock(&ssl_locks[n]);
67 #endif /* HAVE_OPENSSL */
71 * \brief Common OpenSSL initialization for all of Asterisk.
73 int ast_ssl_init(void)
79 SSL_load_error_strings();
80 ERR_load_crypto_strings();
81 ERR_load_BIO_strings();
82 OpenSSL_add_all_algorithms();
84 /* Make OpenSSL thread-safe. */
86 CRYPTO_set_id_callback(ssl_threadid);
88 ssl_num_locks = CRYPTO_num_locks();
89 if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
92 for (i = 0; i < ssl_num_locks; i++) {
93 ast_mutex_init(&ssl_locks[i]);
95 CRYPTO_set_locking_callback(ssl_lock);
97 #endif /* HAVE_OPENSSL */