libasteriskssl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009-2018, 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. * \file
  20. * \brief Common OpenSSL support code
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/_private.h" /* ast_ssl_init() */
  27. #ifdef HAVE_OPENSSL
  28. #include <openssl/opensslv.h> /* for OPENSSL_VERSION_NUMBER */
  29. #endif
  30. #if defined(HAVE_OPENSSL) && \
  31. (defined(LIBRESSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100000L)
  32. #include <dlfcn.h> /* for dlerror, dlsym, RTLD_NEXT */
  33. #include <openssl/crypto.h> /* for CRYPTO_num_locks, CRYPTO_set_id_call... */
  34. #include <openssl/err.h> /* for ERR_free_strings */
  35. #include <openssl/ssl.h> /* for SSL_library_init, SSL_load_error_str... */
  36. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  37. #include <pthread.h> /* for pthread_self */
  38. #endif
  39. #include "asterisk/lock.h" /* for ast_mutex_t, ast_mutex_init, ast_mut... */
  40. #include "asterisk/logger.h" /* for ast_debug, ast_log, LOG_ERROR */
  41. #include "asterisk/utils.h" /* for ast_calloc */
  42. #define get_OpenSSL_function(func) do { real_##func = dlsym(RTLD_NEXT, __stringify(func)); } while(0)
  43. static int startup_complete;
  44. static ast_mutex_t *ssl_locks;
  45. static int ssl_num_locks;
  46. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  47. static unsigned long ssl_threadid(void)
  48. {
  49. return (unsigned long) pthread_self();
  50. }
  51. #endif
  52. static void ssl_lock(int mode, int n, const char *file, int line)
  53. {
  54. if (n < 0 || n >= ssl_num_locks) {
  55. ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
  56. "ssl_num_locks '%d' - n '%d'\n",
  57. ssl_num_locks, n);
  58. return;
  59. }
  60. if (mode & 0x1) {
  61. ast_mutex_lock(&ssl_locks[n]);
  62. } else {
  63. ast_mutex_unlock(&ssl_locks[n]);
  64. }
  65. }
  66. int SSL_library_init(void)
  67. {
  68. #if defined(AST_DEVMODE)
  69. if (startup_complete) {
  70. ast_debug(1, "Called after startup... ignoring!\n");
  71. }
  72. #endif
  73. return 1;
  74. }
  75. void SSL_load_error_strings(void)
  76. {
  77. #if defined(AST_DEVMODE)
  78. if (startup_complete) {
  79. ast_debug(1, "Called after startup... ignoring!\n");
  80. }
  81. #endif
  82. }
  83. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  84. void CRYPTO_set_id_callback(unsigned long (*func)(void))
  85. {
  86. #if defined(AST_DEVMODE)
  87. if (startup_complete) {
  88. ast_debug(1, "Called after startup... ignoring!\n");
  89. }
  90. #endif
  91. }
  92. #endif
  93. void CRYPTO_set_locking_callback(void (*func)(int mode,int type, const char *file, int line))
  94. {
  95. #if defined(AST_DEVMODE)
  96. if (startup_complete) {
  97. ast_debug(1, "Called after startup... ignoring!\n");
  98. }
  99. #endif
  100. }
  101. void ERR_free_strings(void)
  102. {
  103. /* we can't allow this to be called, ever */
  104. }
  105. /*!
  106. * \internal
  107. * \brief Common OpenSSL initialization for all of Asterisk.
  108. *
  109. * Not needed for OpenSSL versions >= 1.1.0
  110. */
  111. int ast_ssl_init(void)
  112. {
  113. unsigned int i;
  114. int (*real_SSL_library_init)(void);
  115. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  116. void (*real_CRYPTO_set_id_callback)(unsigned long (*)(void));
  117. #endif
  118. void (*real_CRYPTO_set_locking_callback)(void (*)(int, int, const char *, int));
  119. void (*real_SSL_load_error_strings)(void);
  120. const char *errstr;
  121. /* clear any previous dynamic linker errors */
  122. dlerror();
  123. get_OpenSSL_function(SSL_library_init);
  124. if ((errstr = dlerror()) != NULL) {
  125. ast_debug(1, "unable to get real address of SSL_library_init: %s\n", errstr);
  126. /* there is no way to continue in this situation... SSL will
  127. * likely be broken in this process
  128. */
  129. return -1;
  130. } else {
  131. real_SSL_library_init();
  132. }
  133. /* Make OpenSSL usage thread-safe. */
  134. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  135. dlerror();
  136. get_OpenSSL_function(CRYPTO_set_id_callback);
  137. if ((errstr = dlerror()) != NULL) {
  138. ast_debug(1, "unable to get real address of CRYPTO_set_id_callback: %s\n", errstr);
  139. /* there is no way to continue in this situation... SSL will
  140. * likely be broken in this process
  141. */
  142. return -1;
  143. } else {
  144. real_CRYPTO_set_id_callback(ssl_threadid);
  145. }
  146. #endif
  147. dlerror();
  148. get_OpenSSL_function(CRYPTO_set_locking_callback);
  149. if ((errstr = dlerror()) != NULL) {
  150. ast_debug(1, "unable to get real address of CRYPTO_set_locking_callback: %s\n", errstr);
  151. /* there is no way to continue in this situation... SSL will
  152. * likely be broken in this process
  153. */
  154. return -1;
  155. } else {
  156. ssl_num_locks = CRYPTO_num_locks();
  157. if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
  158. return -1;
  159. }
  160. for (i = 0; i < ssl_num_locks; i++) {
  161. ast_mutex_init(&ssl_locks[i]);
  162. }
  163. real_CRYPTO_set_locking_callback(ssl_lock);
  164. }
  165. /* after this point, we don't check for errors from the dlsym() calls,
  166. * under the assumption that if the ones above were successful, all
  167. * the rest will be too. this assumption holds as long as OpenSSL still
  168. * provides all of these functions.
  169. */
  170. get_OpenSSL_function(SSL_load_error_strings);
  171. real_SSL_load_error_strings();
  172. startup_complete = 1;
  173. return 0;
  174. }
  175. #else
  176. int ast_ssl_init(void)
  177. {
  178. return 0;
  179. }
  180. #endif