BSecurity.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  23. #include <pthread.h>
  24. #endif
  25. #include <openssl/crypto.h>
  26. #include <misc/debug.h>
  27. #include <misc/balloc.h>
  28. #include <security/BSecurity.h>
  29. int bsecurity_initialized = 0;
  30. int bsecurity_use_threads;
  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. ASSERT(bsecurity_use_threads)
  40. return (unsigned long)pthread_self();
  41. }
  42. static void locking_callback (int mode, int type, const char *file, int line)
  43. {
  44. ASSERT(bsecurity_initialized)
  45. ASSERT(bsecurity_use_threads)
  46. ASSERT(type >= 0)
  47. ASSERT(type < bsecurity_num_locks)
  48. if ((mode & CRYPTO_LOCK)) {
  49. ASSERT_FORCE(pthread_mutex_lock(&bsecurity_locks[type]) == 0)
  50. } else {
  51. ASSERT_FORCE(pthread_mutex_unlock(&bsecurity_locks[type]) == 0)
  52. }
  53. }
  54. #endif
  55. int BSecurity_GlobalInit (int use_threads)
  56. {
  57. ASSERT(use_threads == 0 || use_threads == 1)
  58. ASSERT(!bsecurity_initialized)
  59. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  60. if (use_threads) {
  61. // get number of locks
  62. int num_locks = CRYPTO_num_locks();
  63. ASSERT_FORCE(num_locks >= 0)
  64. // alloc locks array
  65. if (!(bsecurity_locks = BAllocArray(num_locks, sizeof(bsecurity_locks[0])))) {
  66. goto fail0;
  67. }
  68. // init locks
  69. bsecurity_num_locks = 0;
  70. for (int i = 0; i < num_locks; i++) {
  71. if (pthread_mutex_init(&bsecurity_locks[i], NULL) != 0) {
  72. goto fail1;
  73. }
  74. bsecurity_num_locks++;
  75. }
  76. }
  77. #endif
  78. bsecurity_initialized = 1;
  79. bsecurity_use_threads = use_threads;
  80. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  81. if (use_threads) {
  82. CRYPTO_set_id_callback(id_callback);
  83. CRYPTO_set_locking_callback(locking_callback);
  84. }
  85. #endif
  86. return 1;
  87. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  88. fail1:
  89. while (bsecurity_num_locks > 0) {
  90. ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
  91. bsecurity_num_locks--;
  92. }
  93. BFree(bsecurity_locks);
  94. fail0:
  95. return 0;
  96. #endif
  97. }
  98. void BSecurity_GlobalAssert (int need_threads)
  99. {
  100. ASSERT(need_threads == 0 || need_threads == 1)
  101. ASSERT(bsecurity_initialized)
  102. ASSERT(!(need_threads) || bsecurity_use_threads)
  103. }