2
0

tls_locking.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * TLS module
  3. *
  4. * Copyright (C) 2007 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*!
  19. * \file
  20. * \brief Kamailio TLS support :: Locking
  21. * \ingroup tls
  22. * Module: \ref tls
  23. */
  24. #include <stdlib.h> /* abort() */
  25. #include <openssl/crypto.h>
  26. #include "../../dprint.h"
  27. #include "../../locking.h"
  28. static int n_static_locks=0;
  29. static gen_lock_set_t* static_locks=0;
  30. /* "dynamic" locks */
  31. struct CRYPTO_dynlock_value{
  32. gen_lock_t lock;
  33. };
  34. static struct CRYPTO_dynlock_value* dyn_create_f(const char* file, int line)
  35. {
  36. struct CRYPTO_dynlock_value* l;
  37. l=shm_malloc(sizeof(struct CRYPTO_dynlock_value));
  38. if (l==0){
  39. LOG(L_CRIT, "ERROR: tls: dyn_create_f locking callback out of shm."
  40. " memory (called from %s:%d)\n", file, line);
  41. goto error;
  42. }
  43. if (lock_init(&l->lock)==0){
  44. LOG(L_CRIT, "ERROR: tls: dyn_create_f locking callback: lock "
  45. "initialization failed (called from %s:%d)\n", file, line);
  46. shm_free(l);
  47. goto error;
  48. }
  49. return l;
  50. error:
  51. return 0;
  52. }
  53. static void dyn_lock_f(int mode, struct CRYPTO_dynlock_value* l,
  54. const char* file, int line)
  55. {
  56. if (l==0){
  57. LOG(L_CRIT, "BUG: tls: dyn_lock_f locking callback: null lock"
  58. " (called from %s:%d)\n", file, line);
  59. /* try to continue */
  60. return;
  61. }
  62. if (mode & CRYPTO_LOCK){
  63. lock_get(&l->lock);
  64. }else{
  65. lock_release(&l->lock);
  66. }
  67. }
  68. static void dyn_destroy_f(struct CRYPTO_dynlock_value *l,
  69. const char* file, int line)
  70. {
  71. if (l==0){
  72. LOG(L_CRIT, "BUG: tls: dyn_destroy_f locking callback: null lock"
  73. " (called from %s:%d)\n", file, line);
  74. return;
  75. }
  76. lock_destroy(&l->lock);
  77. shm_free(l);
  78. }
  79. /* normal locking callback */
  80. static void locking_f(int mode, int n, const char* file, int line)
  81. {
  82. if (n<0 || n>=n_static_locks){
  83. LOG(L_CRIT, "BUG: tls: locking_f (callback): invalid lock number: "
  84. " %d (range 0 - %d), called from %s:%d\n",
  85. n, n_static_locks, file, line);
  86. abort(); /* quick crash :-) */
  87. }
  88. if (mode & CRYPTO_LOCK){
  89. lock_set_get(static_locks, n);
  90. }else{
  91. lock_set_release(static_locks, n);
  92. }
  93. }
  94. void tls_destroy_locks()
  95. {
  96. if (static_locks){
  97. lock_set_destroy(static_locks);
  98. lock_set_dealloc(static_locks);
  99. static_locks=0;
  100. n_static_locks=0;
  101. }
  102. }
  103. unsigned long sr_ssl_id_f()
  104. {
  105. return my_pid();
  106. }
  107. /* returns -1 on error, 0 on success */
  108. int tls_init_locks()
  109. {
  110. /* init "static" tls locks */
  111. n_static_locks=CRYPTO_num_locks();
  112. if (n_static_locks<0){
  113. LOG(L_CRIT, "BUG: tls: tls_init_locking: bad CRYPTO_num_locks %d\n",
  114. n_static_locks);
  115. n_static_locks=0;
  116. }
  117. if (n_static_locks){
  118. static_locks=lock_set_alloc(n_static_locks);
  119. if (static_locks==0){
  120. LOG(L_CRIT, "ERROR: tls_init_locking: could not allocate lockset"
  121. " with %d locks\n", n_static_locks);
  122. goto error;
  123. }
  124. if (lock_set_init(static_locks)==0){
  125. LOG(L_CRIT, "ERROR: tls_init_locking: lock_set_init failed "
  126. "(%d locks)\n", n_static_locks);
  127. lock_set_dealloc(static_locks);
  128. static_locks=0;
  129. n_static_locks=0;
  130. goto error;
  131. }
  132. CRYPTO_set_locking_callback(locking_f);
  133. }
  134. /* set "dynamic" locks callbacks */
  135. CRYPTO_set_dynlock_create_callback(dyn_create_f);
  136. CRYPTO_set_dynlock_lock_callback(dyn_lock_f);
  137. CRYPTO_set_dynlock_destroy_callback(dyn_destroy_f);
  138. /* starting with v1.0.0 openssl does not use anymore getpid(), but address
  139. * of errno which can point to same virtual address in a multi-process
  140. * application
  141. * - for refrence http://www.openssl.org/docs/crypto/threads.html
  142. */
  143. CRYPTO_set_id_callback(sr_ssl_id_f);
  144. /* atomic add -- since for now we don't have atomic_add
  145. * (only atomic_inc), fallback to the default use-locks mode
  146. * CRYPTO_set_add_lock_callback(atomic_add_f);
  147. */
  148. return 0;
  149. error:
  150. tls_destroy_locks();
  151. return -1;
  152. }