BSecurity.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file BSecurity.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  31. #include <pthread.h>
  32. #endif
  33. #include <openssl/crypto.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. #include <security/BSecurity.h>
  37. int bsecurity_initialized = 0;
  38. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  39. pthread_mutex_t *bsecurity_locks;
  40. int bsecurity_num_locks;
  41. #endif
  42. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  43. static unsigned long id_callback (void)
  44. {
  45. ASSERT(bsecurity_initialized)
  46. return (unsigned long)pthread_self();
  47. }
  48. static void locking_callback (int mode, int type, const char *file, int line)
  49. {
  50. ASSERT(bsecurity_initialized)
  51. ASSERT(type >= 0)
  52. ASSERT(type < bsecurity_num_locks)
  53. if ((mode & CRYPTO_LOCK)) {
  54. ASSERT_FORCE(pthread_mutex_lock(&bsecurity_locks[type]) == 0)
  55. } else {
  56. ASSERT_FORCE(pthread_mutex_unlock(&bsecurity_locks[type]) == 0)
  57. }
  58. }
  59. #endif
  60. int BSecurity_GlobalInitThreadSafe (void)
  61. {
  62. ASSERT(!bsecurity_initialized)
  63. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  64. // get number of locks
  65. int num_locks = CRYPTO_num_locks();
  66. ASSERT_FORCE(num_locks >= 0)
  67. // alloc locks array
  68. if (!(bsecurity_locks = BAllocArray(num_locks, sizeof(bsecurity_locks[0])))) {
  69. goto fail0;
  70. }
  71. // init locks
  72. bsecurity_num_locks = 0;
  73. for (int i = 0; i < num_locks; i++) {
  74. if (pthread_mutex_init(&bsecurity_locks[i], NULL) != 0) {
  75. goto fail1;
  76. }
  77. bsecurity_num_locks++;
  78. }
  79. #endif
  80. bsecurity_initialized = 1;
  81. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  82. CRYPTO_set_id_callback(id_callback);
  83. CRYPTO_set_locking_callback(locking_callback);
  84. #endif
  85. return 1;
  86. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  87. fail1:
  88. while (bsecurity_num_locks > 0) {
  89. ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
  90. bsecurity_num_locks--;
  91. }
  92. BFree(bsecurity_locks);
  93. fail0:
  94. return 0;
  95. #endif
  96. }
  97. void BSecurity_GlobalFreeThreadSafe (void)
  98. {
  99. ASSERT(bsecurity_initialized)
  100. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  101. // remove callbacks
  102. CRYPTO_set_locking_callback(NULL);
  103. CRYPTO_set_id_callback(NULL);
  104. // free locks
  105. while (bsecurity_num_locks > 0) {
  106. ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
  107. bsecurity_num_locks--;
  108. }
  109. // free locks array
  110. BFree(bsecurity_locks);
  111. #endif
  112. bsecurity_initialized = 0;
  113. }
  114. void BSecurity_GlobalAssertThreadSafe (int thread_safe)
  115. {
  116. ASSERT(thread_safe == 0 || thread_safe == 1)
  117. ASSERT(!(thread_safe) || bsecurity_initialized)
  118. }