cryptothread.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Name: cryptothread.c
  2. *
  3. * This function contains the code necessary for using OpenSSL in a thread-safe
  4. * manner.
  5. *
  6. * Slitheen - a decoy routing system for censorship resistance
  7. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, version 3.
  12. *
  13. * This program 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
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Additional permission under GNU GPL version 3 section 7
  22. *
  23. * If you modify this Program, or any covered work, by linking or combining
  24. * it with the OpenSSL library (or a modified version of that library),
  25. * containing parts covered by the terms of the OpenSSL Licence and the
  26. * SSLeay license, the licensors of this Program grant you additional
  27. * permission to convey the resulting work. Corresponding Source for a
  28. * non-source form of such a combination shall include the source code
  29. * for the parts of the OpenSSL library used as well as that of the covered
  30. * work.
  31. */
  32. #include <pthread.h>
  33. #include <openssl/crypto.h>
  34. #include "cryptothread.h"
  35. static pthread_mutex_t *crypto_locks;
  36. static long *lock_count;
  37. static void pthreads_thread_id(CRYPTO_THREADID *tid);
  38. static void pthreads_locking_callback(int mode, int type, const char *file, int line);
  39. void init_crypto_locks(void){
  40. crypto_locks = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
  41. if(!crypto_locks)
  42. exit(1);
  43. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  44. if(!lock_count)
  45. exit(1);
  46. for (int i = 0; i < CRYPTO_num_locks(); i++) {
  47. lock_count[i] = 0;
  48. pthread_mutex_init(&(crypto_locks[i]), NULL);
  49. }
  50. CRYPTO_THREADID_set_callback(pthreads_thread_id);
  51. CRYPTO_set_locking_callback(pthreads_locking_callback);
  52. }
  53. void crypto_locks_cleanup(void){
  54. int i;
  55. CRYPTO_set_locking_callback(NULL);
  56. for (i = 0; i < CRYPTO_num_locks(); i++) {
  57. pthread_mutex_destroy(&(crypto_locks[i]));
  58. }
  59. OPENSSL_free(crypto_locks);
  60. OPENSSL_free(lock_count);
  61. }
  62. /** If the mode is CRYPTO_LOCK, the lock indicated by type will be acquired, otherwise it will be released */
  63. static void pthreads_locking_callback(int mode, int type, const char *file, int line){
  64. if(mode & CRYPTO_LOCK){
  65. pthread_mutex_lock(&(crypto_locks[type]));
  66. lock_count[type]++;
  67. } else {
  68. pthread_mutex_unlock(&(crypto_locks[type]));
  69. }
  70. }
  71. static void pthreads_thread_id(CRYPTO_THREADID *tid){
  72. CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
  73. }