Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
libasteriskssl.c File Reference

Common OpenSSL support code. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include <openssl/opensslv.h>
#include <dlfcn.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <pthread.h>
#include "asterisk/lock.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
Include dependency graph for libasteriskssl.c:

Go to the source code of this file.

Macros

#define get_OpenSSL_function(func)   do { real_##func = dlsym(RTLD_NEXT, __stringify(func)); } while(0)
 

Functions

int ast_ssl_init (void)
 
void CRYPTO_set_id_callback (unsigned long(*func)(void))
 
void CRYPTO_set_locking_callback (void(*func)(int mode, int type, const char *file, int line))
 
void ERR_free_strings (void)
 
int SSL_library_init (void)
 
void SSL_load_error_strings (void)
 
static void ssl_lock (int mode, int n, const char *file, int line)
 
static unsigned long ssl_threadid (void)
 

Variables

static ast_mutex_tssl_locks
 
static int ssl_num_locks
 
static int startup_complete
 

Detailed Description

Common OpenSSL support code.

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

Definition in file libasteriskssl.c.

Macro Definition Documentation

◆ get_OpenSSL_function

#define get_OpenSSL_function (   func)    do { real_##func = dlsym(RTLD_NEXT, __stringify(func)); } while(0)

Definition at line 49 of file libasteriskssl.c.

Referenced by ast_ssl_init().

Function Documentation

◆ ast_ssl_init()

int ast_ssl_init ( void  )

Provided by ssl.c

Definition at line 130 of file libasteriskssl.c.

References ast_calloc, ast_debug, ast_mutex_init, CRYPTO_set_id_callback(), CRYPTO_set_locking_callback(), get_OpenSSL_function, NULL, SSL_library_init(), SSL_load_error_strings(), ssl_lock(), ssl_num_locks, ssl_threadid(), and startup_complete.

Referenced by asterisk_daemon().

131 {
132  unsigned int i;
133  int (*real_SSL_library_init)(void);
134 #if OPENSSL_VERSION_NUMBER < 0x10000000L
135  void (*real_CRYPTO_set_id_callback)(unsigned long (*)(void));
136 #endif
137  void (*real_CRYPTO_set_locking_callback)(void (*)(int, int, const char *, int));
138  void (*real_SSL_load_error_strings)(void);
139  const char *errstr;
140 
141  /* clear any previous dynamic linker errors */
142  dlerror();
144  if ((errstr = dlerror()) != NULL) {
145  ast_debug(1, "unable to get real address of SSL_library_init: %s\n", errstr);
146  /* there is no way to continue in this situation... SSL will
147  * likely be broken in this process
148  */
149  return -1;
150  } else {
151  real_SSL_library_init();
152  }
153 
154  /* Make OpenSSL usage thread-safe. */
155 
156 #if OPENSSL_VERSION_NUMBER < 0x10000000L
157  dlerror();
159  if ((errstr = dlerror()) != NULL) {
160  ast_debug(1, "unable to get real address of CRYPTO_set_id_callback: %s\n", errstr);
161  /* there is no way to continue in this situation... SSL will
162  * likely be broken in this process
163  */
164  return -1;
165  } else {
166  real_CRYPTO_set_id_callback(ssl_threadid);
167  }
168 #endif
169 
170  dlerror();
172  if ((errstr = dlerror()) != NULL) {
173  ast_debug(1, "unable to get real address of CRYPTO_set_locking_callback: %s\n", errstr);
174  /* there is no way to continue in this situation... SSL will
175  * likely be broken in this process
176  */
177  return -1;
178  } else {
179  ssl_num_locks = CRYPTO_num_locks();
180  if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
181  return -1;
182  }
183  for (i = 0; i < ssl_num_locks; i++) {
185  }
186  real_CRYPTO_set_locking_callback(ssl_lock);
187  }
188 
189  /* after this point, we don't check for errors from the dlsym() calls,
190  * under the assumption that if the ones above were successful, all
191  * the rest will be too. this assumption holds as long as OpenSSL still
192  * provides all of these functions.
193  */
194 
196  real_SSL_load_error_strings();
197 
198  startup_complete = 1;
199 
200  return 0;
201 }
static int ssl_num_locks
#define NULL
Definition: resample.c:96
void SSL_load_error_strings(void)
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static unsigned long ssl_threadid(void)
void CRYPTO_set_id_callback(unsigned long(*func)(void))
static ast_mutex_t * ssl_locks
static void ssl_lock(int mode, int n, const char *file, int line)
int SSL_library_init(void)
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define ast_mutex_init(pmutex)
Definition: lock.h:184
void CRYPTO_set_locking_callback(void(*func)(int mode, int type, const char *file, int line))
#define get_OpenSSL_function(func)
static int startup_complete

◆ CRYPTO_set_id_callback()

void CRYPTO_set_id_callback ( unsigned long(*)(void)  func)

Definition at line 100 of file libasteriskssl.c.

References ast_debug, and startup_complete.

Referenced by ast_ssl_init().

101 {
102 #if defined(AST_DEVMODE)
103  if (startup_complete) {
104  ast_debug(1, "Called after startup... ignoring!\n");
105  }
106 #endif
107 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int startup_complete

◆ CRYPTO_set_locking_callback()

void CRYPTO_set_locking_callback ( void(*)(int mode, int type, const char *file, int line)  func)

Definition at line 110 of file libasteriskssl.c.

References ast_debug, and startup_complete.

Referenced by ast_ssl_init().

111 {
112 #if defined(AST_DEVMODE)
113  if (startup_complete) {
114  ast_debug(1, "Called after startup... ignoring!\n");
115  }
116 #endif
117 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int startup_complete

◆ ERR_free_strings()

void ERR_free_strings ( void  )

Definition at line 119 of file libasteriskssl.c.

120 {
121  /* we can't allow this to be called, ever */
122 }

◆ SSL_library_init()

int SSL_library_init ( void  )

Definition at line 80 of file libasteriskssl.c.

References ast_debug, and startup_complete.

Referenced by ast_ssl_init().

81 {
82 #if defined(AST_DEVMODE)
83  if (startup_complete) {
84  ast_debug(1, "Called after startup... ignoring!\n");
85  }
86 #endif
87  return 1;
88 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int startup_complete

◆ SSL_load_error_strings()

void SSL_load_error_strings ( void  )

Definition at line 90 of file libasteriskssl.c.

References ast_debug, and startup_complete.

Referenced by ast_ssl_init().

91 {
92 #if defined(AST_DEVMODE)
93  if (startup_complete) {
94  ast_debug(1, "Called after startup... ignoring!\n");
95  }
96 #endif
97 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int startup_complete

◆ ssl_lock()

static void ssl_lock ( int  mode,
int  n,
const char *  file,
int  line 
)
static

Definition at line 64 of file libasteriskssl.c.

References ast_log, ast_mutex_lock, ast_mutex_unlock, LOG_ERROR, and ssl_num_locks.

Referenced by ast_ssl_init().

65 {
66  if (n < 0 || n >= ssl_num_locks) {
67  ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
68  "ssl_num_locks '%d' - n '%d'\n",
69  ssl_num_locks, n);
70  return;
71  }
72 
73  if (mode & 0x1) {
75  } else {
77  }
78 }
static int ssl_num_locks
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_log
Definition: astobj2.c:42
static ast_mutex_t * ssl_locks
#define LOG_ERROR
Definition: logger.h:285
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ ssl_threadid()

static unsigned long ssl_threadid ( void  )
static

Definition at line 58 of file libasteriskssl.c.

Referenced by ast_ssl_init().

59 {
60  return (unsigned long) pthread_self();
61 }

Variable Documentation

◆ ssl_locks

ast_mutex_t* ssl_locks
static

Definition at line 53 of file libasteriskssl.c.

◆ ssl_num_locks

int ssl_num_locks
static

Definition at line 55 of file libasteriskssl.c.

Referenced by ast_ssl_init(), and ssl_lock().

◆ startup_complete

int startup_complete
static