| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Russell Bryant <russell@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! 
 | 
					
						
							|  |  |  |  * \file | 
					
						
							|  |  |  |  * \brief Common OpenSSL support code | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \author Russell Bryant <russell@digium.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_OPENSSL
 | 
					
						
							|  |  |  | #include <openssl/ssl.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/_private.h" /* ast_ssl_init() */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_OPENSSL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ast_mutex_t *ssl_locks; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ssl_num_locks; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static unsigned long ssl_threadid(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-09 08:31:24 +00:00
										 |  |  | 	return (unsigned long)pthread_self(); | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void ssl_lock(int mode, int n, const char *file, int line) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	if (n < 0 || n >= ssl_num_locks) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - " | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 				"ssl_num_locks '%d' - n '%d'\n", | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 				ssl_num_locks, n); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mode & CRYPTO_LOCK) { | 
					
						
							|  |  |  | 		ast_mutex_lock(&ssl_locks[n]); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_mutex_unlock(&ssl_locks[n]); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* HAVE_OPENSSL */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \internal | 
					
						
							|  |  |  |  * \brief Common OpenSSL initialization for all of Asterisk. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int ast_ssl_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef HAVE_OPENSSL
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	unsigned int i; | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	SSL_library_init(); | 
					
						
							|  |  |  | 	SSL_load_error_strings(); | 
					
						
							|  |  |  | 	ERR_load_crypto_strings(); | 
					
						
							|  |  |  | 	ERR_load_BIO_strings(); | 
					
						
							|  |  |  | 	OpenSSL_add_all_algorithms(); | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	/* Make OpenSSL thread-safe. */ | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	CRYPTO_set_id_callback(ssl_threadid); | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	ssl_num_locks = CRYPTO_num_locks(); | 
					
						
							|  |  |  | 	if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (i = 0; i < ssl_num_locks; i++) { | 
					
						
							|  |  |  | 		ast_mutex_init(&ssl_locks[i]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	CRYPTO_set_locking_callback(ssl_lock); | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* HAVE_OPENSSL */
 | 
					
						
							| 
									
										
										
										
											2009-07-08 15:56:28 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-07-08 15:17:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |