ssl.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * \file
  20. * \brief Common OpenSSL support code
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #ifdef HAVE_OPENSSL
  30. #include <openssl/ssl.h>
  31. #include <openssl/err.h>
  32. #endif
  33. #include "asterisk/_private.h" /* ast_ssl_init() */
  34. #include "asterisk/utils.h"
  35. #include "asterisk/lock.h"
  36. #ifdef HAVE_OPENSSL
  37. static ast_mutex_t *ssl_locks;
  38. static int ssl_num_locks;
  39. static unsigned long ssl_threadid(void)
  40. {
  41. return (unsigned long)pthread_self();
  42. }
  43. static void ssl_lock(int mode, int n, const char *file, int line)
  44. {
  45. if (n < 0 || n >= ssl_num_locks) {
  46. ast_log(LOG_ERROR, "OpenSSL is full of LIES!!! - "
  47. "ssl_num_locks '%d' - n '%d'\n",
  48. ssl_num_locks, n);
  49. return;
  50. }
  51. if (mode & CRYPTO_LOCK) {
  52. ast_mutex_lock(&ssl_locks[n]);
  53. } else {
  54. ast_mutex_unlock(&ssl_locks[n]);
  55. }
  56. }
  57. #endif /* HAVE_OPENSSL */
  58. /*!
  59. * \internal
  60. * \brief Common OpenSSL initialization for all of Asterisk.
  61. */
  62. int ast_ssl_init(void)
  63. {
  64. #ifdef HAVE_OPENSSL
  65. unsigned int i;
  66. SSL_library_init();
  67. SSL_load_error_strings();
  68. ERR_load_BIO_strings();
  69. /* Make OpenSSL thread-safe. */
  70. CRYPTO_set_id_callback(ssl_threadid);
  71. ssl_num_locks = CRYPTO_num_locks();
  72. if (!(ssl_locks = ast_calloc(ssl_num_locks, sizeof(ssl_locks[0])))) {
  73. return -1;
  74. }
  75. for (i = 0; i < ssl_num_locks; i++) {
  76. ast_mutex_init(&ssl_locks[i]);
  77. }
  78. CRYPTO_set_locking_callback(ssl_lock);
  79. #endif /* HAVE_OPENSSL */
  80. return 0;
  81. }