Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


ssl.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*!
20  * \file
21  * \brief Common OpenSSL support code
22  *
23  * \author Russell Bryant <russell@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 373061 $")
33 
34 #ifdef HAVE_OPENSSL
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37 #endif
38 
39 #include "asterisk/_private.h" /* ast_ssl_init() */
40 
41 #include "asterisk/utils.h"
42 #include "asterisk/lock.h"
43 
44 #ifdef HAVE_OPENSSL
45 
47 
48 static int ssl_num_locks;
49 
50 static unsigned long ssl_threadid(void)
51 {
52  return (unsigned long)pthread_self();
53 }
54 
55 static void ssl_lock(int mode, int n, const char *file, int line)
56 {
57  if (n < 0 || n >= ssl_num_locks) {
58  ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
59  "ssl_num_locks '%d' - n '%d'\n",
60  ssl_num_locks, n);
61  return;
62  }
63 
64  if (mode & CRYPTO_LOCK) {
65  ast_mutex_lock(&ssl_locks[n]);
66  } else {
67  ast_mutex_unlock(&ssl_locks[n]);
68  }
69 }
70 
71 #endif /* HAVE_OPENSSL */
72 
73 /*!
74  * \internal
75  * \brief Common OpenSSL initialization for all of Asterisk.
76  */
77 int ast_ssl_init(void)
78 {
79 #ifdef HAVE_OPENSSL
80  unsigned int i;
81 
82  SSL_library_init();
83  SSL_load_error_strings();
84  ERR_load_BIO_strings();
85 
86  /* Make OpenSSL thread-safe. */
87 
88  CRYPTO_set_id_callback(ssl_threadid);
89 
90  ssl_num_locks = CRYPTO_num_locks();
91  if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
92  return -1;
93  }
94  for (i = 0; i < ssl_num_locks; i++) {
95  ast_mutex_init(&ssl_locks[i]);
96  }
97  CRYPTO_set_locking_callback(ssl_lock);
98 
99 #endif /* HAVE_OPENSSL */
100  return 0;
101 }
102 
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
static void ssl_lock(int mode, int n, const char *file, int line)
Definition: ssl.c:55
static int ssl_num_locks
Definition: ssl.c:48
#define ast_mutex_lock(a)
Definition: lock.h:155
Utility functions.
static unsigned long ssl_threadid(void)
Definition: ssl.c:50
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
Prototypes for public functions only of internal interest,.
static ast_mutex_t * ssl_locks
Definition: ssl.c:46
int ast_ssl_init(void)
Definition: ssl.c:77
#define ast_calloc(a, b)
Definition: astmm.h:82
#define ast_mutex_init(pmutex)
Definition: lock.h:152
Structure for mutex and tracking information.
Definition: lock.h:121
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define ast_mutex_unlock(a)
Definition: lock.h:156