Sat Apr 26 2014 22:02:54

Asterisk developer's documentation


libasteriskssl.c File Reference

Common OpenSSL support code. More...

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

Go to the source code of this file.

Defines

#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)
void ERR_load_BIO_strings (void)
void ERR_load_crypto_strings (void)
void ERR_load_SSL_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 <russell@digium.com>

Definition in file libasteriskssl.c.


Define Documentation

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

Definition at line 48 of file libasteriskssl.c.

Referenced by ast_ssl_init().


Function Documentation

int ast_ssl_init ( void  )

Provided by ssl.c

Definition at line 152 of file libasteriskssl.c.

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

Referenced by main().

{
#ifdef HAVE_OPENSSL
   unsigned int i;
   int (*real_SSL_library_init)(void);
   void (*real_CRYPTO_set_id_callback)(unsigned long (*)(void));
   void (*real_CRYPTO_set_locking_callback)(void (*)(int, int, const char *, int));
   void (*real_SSL_load_error_strings)(void);
   void (*real_ERR_load_SSL_strings)(void);
   void (*real_ERR_load_BIO_strings)(void);
   const char *errstr;

   /* clear any previous dynamic linker errors */
   dlerror();
   get_OpenSSL_function(SSL_library_init);
   if ((errstr = dlerror()) != NULL) {
      ast_debug(1, "unable to get real address of SSL_library_init: %s\n", errstr);
      /* there is no way to continue in this situation... SSL will
       * likely be broken in this process
       */
      return -1;
   } else {
      real_SSL_library_init();
   }

   /* Make OpenSSL usage thread-safe. */

   dlerror();
   get_OpenSSL_function(CRYPTO_set_id_callback);
   if ((errstr = dlerror()) != NULL) {
      ast_debug(1, "unable to get real address of CRYPTO_set_id_callback: %s\n", errstr);
      /* there is no way to continue in this situation... SSL will
       * likely be broken in this process
       */
      return -1;
   } else {
      real_CRYPTO_set_id_callback(ssl_threadid);
   }

   dlerror();
   get_OpenSSL_function(CRYPTO_set_locking_callback);
   if ((errstr = dlerror()) != NULL) {
      ast_debug(1, "unable to get real address of CRYPTO_set_locking_callback: %s\n", errstr);
      /* there is no way to continue in this situation... SSL will
       * likely be broken in this process
       */
      return -1;
   } else {
      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]);
      }
      real_CRYPTO_set_locking_callback(ssl_lock);
   }

   /* after this point, we don't check for errors from the dlsym() calls,
    * under the assumption that if the ones above were successful, all
    * the rest will be too. this assumption holds as long as OpenSSL still
    * provides all of these functions.
    */

   get_OpenSSL_function(SSL_load_error_strings);
   real_SSL_load_error_strings();

   get_OpenSSL_function(ERR_load_SSL_strings);
   real_ERR_load_SSL_strings();

   get_OpenSSL_function(ERR_load_BIO_strings);
   real_ERR_load_BIO_strings();

   startup_complete = 1;

#endif /* HAVE_OPENSSL */
   return 0;
}
void CRYPTO_set_id_callback ( unsigned long(*)(void)  func)

Definition at line 123 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
void CRYPTO_set_locking_callback ( void(*)(int mode, int type, const char *file, int line)  func)

Definition at line 132 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
void ERR_free_strings ( void  )

Definition at line 141 of file libasteriskssl.c.

{
   /* we can't allow this to be called, ever */
}
void ERR_load_BIO_strings ( void  )

Definition at line 114 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
void ERR_load_crypto_strings ( void  )

Definition at line 105 of file libasteriskssl.c.

References ast_debug.

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
void ERR_load_SSL_strings ( void  )

Definition at line 96 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
int SSL_library_init ( void  )

Definition at line 77 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
   return 1;
}
void SSL_load_error_strings ( void  )

Definition at line 87 of file libasteriskssl.c.

References ast_debug.

Referenced by ast_ssl_init().

{
#if defined(AST_DEVMODE)
   if (startup_complete) {
      ast_debug(1, "Called after startup... ignoring!\n");
   }
#endif
}
static void ssl_lock ( int  mode,
int  n,
const char *  file,
int  line 
) [static]

Definition at line 61 of file libasteriskssl.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, and LOG_ERROR.

Referenced by ast_ssl_init().

{
   if (n < 0 || n >= ssl_num_locks) {
      ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
            "ssl_num_locks '%d' - n '%d'\n",
            ssl_num_locks, n);
      return;
   }

   if (mode & CRYPTO_LOCK) {
      ast_mutex_lock(&ssl_locks[n]);
   } else {
      ast_mutex_unlock(&ssl_locks[n]);
   }
}
static unsigned long ssl_threadid ( void  ) [static]

Definition at line 56 of file libasteriskssl.c.

Referenced by ast_ssl_init().

{
   return (unsigned long) pthread_self();
}

Variable Documentation

Definition at line 52 of file libasteriskssl.c.

int ssl_num_locks [static]

Definition at line 54 of file libasteriskssl.c.

Referenced by ast_ssl_init().

int startup_complete [static]

Definition at line 50 of file libasteriskssl.c.