BSecurity.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file BSecurity.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  24. #include <pthread.h>
  25. #endif
  26. #include <openssl/crypto.h>
  27. #include <misc/debug.h>
  28. #include <misc/balloc.h>
  29. #include <security/BSecurity.h>
  30. int bsecurity_initialized = 0;
  31. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  32. pthread_mutex_t *bsecurity_locks;
  33. int bsecurity_num_locks;
  34. #endif
  35. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  36. static unsigned long id_callback (void)
  37. {
  38. ASSERT(bsecurity_initialized)
  39. return (unsigned long)pthread_self();
  40. }
  41. static void locking_callback (int mode, int type, const char *file, int line)
  42. {
  43. ASSERT(bsecurity_initialized)
  44. ASSERT(type >= 0)
  45. ASSERT(type < bsecurity_num_locks)
  46. if ((mode & CRYPTO_LOCK)) {
  47. ASSERT_FORCE(pthread_mutex_lock(&bsecurity_locks[type]) == 0)
  48. } else {
  49. ASSERT_FORCE(pthread_mutex_unlock(&bsecurity_locks[type]) == 0)
  50. }
  51. }
  52. #endif
  53. int BSecurity_GlobalInitThreadSafe (void)
  54. {
  55. ASSERT(!bsecurity_initialized)
  56. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  57. // get number of locks
  58. int num_locks = CRYPTO_num_locks();
  59. ASSERT_FORCE(num_locks >= 0)
  60. // alloc locks array
  61. if (!(bsecurity_locks = BAllocArray(num_locks, sizeof(bsecurity_locks[0])))) {
  62. goto fail0;
  63. }
  64. // init locks
  65. bsecurity_num_locks = 0;
  66. for (int i = 0; i < num_locks; i++) {
  67. if (pthread_mutex_init(&bsecurity_locks[i], NULL) != 0) {
  68. goto fail1;
  69. }
  70. bsecurity_num_locks++;
  71. }
  72. #endif
  73. bsecurity_initialized = 1;
  74. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  75. CRYPTO_set_id_callback(id_callback);
  76. CRYPTO_set_locking_callback(locking_callback);
  77. #endif
  78. return 1;
  79. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  80. fail1:
  81. while (bsecurity_num_locks > 0) {
  82. ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
  83. bsecurity_num_locks--;
  84. }
  85. BFree(bsecurity_locks);
  86. fail0:
  87. return 0;
  88. #endif
  89. }
  90. void BSecurity_GlobalFreeThreadSafe (void)
  91. {
  92. ASSERT(bsecurity_initialized)
  93. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  94. // remove callbacks
  95. CRYPTO_set_locking_callback(NULL);
  96. CRYPTO_set_id_callback(NULL);
  97. // free locks
  98. while (bsecurity_num_locks > 0) {
  99. ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
  100. bsecurity_num_locks--;
  101. }
  102. // free locks array
  103. BFree(bsecurity_locks);
  104. #endif
  105. bsecurity_initialized = 0;
  106. }
  107. void BSecurity_GlobalAssertThreadSafe (int thread_safe)
  108. {
  109. ASSERT(thread_safe == 0 || thread_safe == 1)
  110. ASSERT(!(thread_safe) || bsecurity_initialized)
  111. }