tls_locking.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * $Id$
  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. * tls locking and atomic ops related init functions
  20. *
  21. * History:
  22. * --------
  23. * 2007-01-22 created by andrei
  24. */
  25. /*!
  26. * \file
  27. * \brief SIP-router TLS support :: Locking
  28. * \ingroup tls
  29. * Module: \ref tls
  30. */
  31. #include <stdlib.h> /* abort() */
  32. #include <openssl/crypto.h>
  33. #include "../../dprint.h"
  34. #include "../../locking.h"
  35. static int n_static_locks=0;
  36. static gen_lock_set_t* static_locks=0;
  37. /* "dynamic" locks */
  38. struct CRYPTO_dynlock_value{
  39. gen_lock_t lock;
  40. };
  41. static struct CRYPTO_dynlock_value* dyn_create_f(const char* file, int line)
  42. {
  43. struct CRYPTO_dynlock_value* l;
  44. l=shm_malloc(sizeof(struct CRYPTO_dynlock_value));
  45. if (l==0){
  46. LOG(L_CRIT, "ERROR: tls: dyn_create_f locking callback out of shm."
  47. " memory (called from %s:%d)\n", file, line);
  48. goto error;
  49. }
  50. if (lock_init(&l->lock)==0){
  51. LOG(L_CRIT, "ERROR: tls: dyn_create_f locking callback: lock "
  52. "initialization failed (called from %s:%d)\n", file, line);
  53. shm_free(l);
  54. goto error;
  55. }
  56. return l;
  57. error:
  58. return 0;
  59. }
  60. static void dyn_lock_f(int mode, struct CRYPTO_dynlock_value* l,
  61. const char* file, int line)
  62. {
  63. if (l==0){
  64. LOG(L_CRIT, "BUG: tls: dyn_lock_f locking callback: null lock"
  65. " (called from %s:%d)\n", file, line);
  66. /* try to continue */
  67. return;
  68. }
  69. if (mode & CRYPTO_LOCK){
  70. lock_get(&l->lock);
  71. }else{
  72. lock_release(&l->lock);
  73. }
  74. }
  75. static void dyn_destroy_f(struct CRYPTO_dynlock_value *l,
  76. const char* file, int line)
  77. {
  78. if (l==0){
  79. LOG(L_CRIT, "BUG: tls: dyn_destroy_f locking callback: null lock"
  80. " (called from %s:%d)\n", file, line);
  81. return;
  82. }
  83. lock_destroy(&l->lock);
  84. shm_free(l);
  85. }
  86. /* normal locking callback */
  87. static void locking_f(int mode, int n, const char* file, int line)
  88. {
  89. if (n<0 || n>=n_static_locks){
  90. LOG(L_CRIT, "BUG: tls: locking_f (callback): invalid lock number: "
  91. " %d (range 0 - %d), called from %s:%d\n",
  92. n, n_static_locks, file, line);
  93. abort(); /* quick crash :-) */
  94. }
  95. if (mode & CRYPTO_LOCK){
  96. lock_set_get(static_locks, n);
  97. }else{
  98. lock_set_release(static_locks, n);
  99. }
  100. }
  101. void tls_destroy_locks()
  102. {
  103. if (static_locks){
  104. lock_set_destroy(static_locks);
  105. lock_set_dealloc(static_locks);
  106. static_locks=0;
  107. n_static_locks=0;
  108. }
  109. }
  110. unsigned long sr_ssl_id_f()
  111. {
  112. return my_pid();
  113. }
  114. /* returns -1 on error, 0 on success */
  115. int tls_init_locks()
  116. {
  117. /* init "static" tls locks */
  118. n_static_locks=CRYPTO_num_locks();
  119. if (n_static_locks<0){
  120. LOG(L_CRIT, "BUG: tls: tls_init_locking: bad CRYPTO_num_locks %d\n",
  121. n_static_locks);
  122. n_static_locks=0;
  123. }
  124. if (n_static_locks){
  125. static_locks=lock_set_alloc(n_static_locks);
  126. if (static_locks==0){
  127. LOG(L_CRIT, "ERROR: tls_init_locking: could not allocate lockset"
  128. " with %d locks\n", n_static_locks);
  129. goto error;
  130. }
  131. if (lock_set_init(static_locks)==0){
  132. LOG(L_CRIT, "ERROR: tls_init_locking: lock_set_init failed "
  133. "(%d locks)\n", n_static_locks);
  134. lock_set_dealloc(static_locks);
  135. static_locks=0;
  136. n_static_locks=0;
  137. goto error;
  138. }
  139. CRYPTO_set_locking_callback(locking_f);
  140. }
  141. /* set "dynamic" locks callbacks */
  142. CRYPTO_set_dynlock_create_callback(dyn_create_f);
  143. CRYPTO_set_dynlock_lock_callback(dyn_lock_f);
  144. CRYPTO_set_dynlock_destroy_callback(dyn_destroy_f);
  145. /* starting with v1.0.0 openssl does not use anymore getpid(), but address
  146. * of errno which can point to same virtual address in a multi-process
  147. * application
  148. * - for refrence http://www.openssl.org/docs/crypto/threads.html
  149. */
  150. CRYPTO_set_id_callback(sr_ssl_id_f);
  151. /* atomic add -- since for now we don't have atomic_add
  152. * (only atomic_inc), fallback to the default use-locks mode
  153. * CRYPTO_set_add_lock_callback(atomic_add_f);
  154. */
  155. return 0;
  156. error:
  157. tls_destroy_locks();
  158. return -1;
  159. }