You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

271 lines
11 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_THREAD_H
  57. #define OPENSSL_HEADER_THREAD_H
  58. #include <openssl/base.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. /* Functions to support multithreading.
  63. *
  64. * OpenSSL can safely be used in multi-threaded applications provided that at
  65. * least two callback functions are set with |CRYPTO_set_locking_callback| and
  66. * |CRYPTO_THREADID_set_callback|.
  67. *
  68. * The locking callback performs mutual exclusion. Rather than using a single
  69. * lock for all, shared data-structures, OpenSSL requires that the locking
  70. * callback support a fixed (at run-time) number of different locks, given by
  71. * |CRYPTO_num_locks|.
  72. *
  73. * The thread ID callback is called to record the currently executing thread's
  74. * identifier in a |CRYPTO_THREADID| structure. If this callback is not
  75. * provided then the address of |errno| is used as the thread identifier. This
  76. * is sufficient only if the system has a thread-local |errno| value. */
  77. /* CRYPTO_num_locks returns the number of static locks that the callback
  78. * function passed to |CRYPTO_set_locking_callback| must be able to handle. */
  79. int CRYPTO_num_locks(void);
  80. /* CRYPTO_set_locking_callback sets a callback function that implements locking
  81. * on behalf of OpenSSL. The callback is called whenever OpenSSL needs to lock
  82. * or unlock a lock, and locks are specified as a number between zero and
  83. * |CRYPTO_num_locks()-1|.
  84. *
  85. * The mode argument to the callback is a bitwise-OR of either CRYPTO_LOCK or
  86. * CRYPTO_UNLOCK, to denote the action, and CRYPTO_READ or CRYPTO_WRITE, to
  87. * indicate the type of lock. The |file| and |line| arguments give the location
  88. * in the OpenSSL source where the locking action originated. */
  89. void CRYPTO_set_locking_callback(void (*func)(int mode, int lock_num,
  90. const char *file, int line));
  91. /* CRYPTO_set_add_lock_callback sets an optional callback which is used when
  92. * OpenSSL needs to add a fixed amount to an integer. For example, this is used
  93. * when maintaining reference counts. Normally the reference counts are
  94. * maintained by performing the addition under a lock but, if this callback
  95. * has been set, the application is free to implement the operation using
  96. * faster methods (i.e. atomic operations).
  97. *
  98. * The callback is given a pointer to the integer to be altered (|num|), the
  99. * amount to add to the integer (|amount|, which may be negative), the number
  100. * of the lock which would have been taken to protect the operation and the
  101. * position in the OpenSSL code where the operation originated. */
  102. void CRYPTO_set_add_lock_callback(int (*func)(int *num, int amount,
  103. int lock_num, const char *file,
  104. int line));
  105. /* CRYPTO_get_lock_name returns the name of the lock given by |lock_num|. This
  106. * can be used in a locking callback for debugging purposes. */
  107. const char *CRYPTO_get_lock_name(int lock_num);
  108. /* CRYPTO_THREADID identifies a thread in a multithreaded program. This
  109. * structure should not be used directly. Rather applications should use
  110. * |CRYPTO_THREADID_set_numeric| and |CRYPTO_THREADID_set_pointer|. */
  111. typedef struct crypto_threadid_st {
  112. void *ptr;
  113. unsigned long val;
  114. } CRYPTO_THREADID;
  115. /* CRYPTO_THREADID_set_callback sets a callback function that stores an
  116. * identifier of the currently executing thread into |threadid|. The
  117. * CRYPTO_THREADID structure should not be accessed directly. Rather one of
  118. * |CRYPTO_THREADID_set_numeric| or |CRYPTO_THREADID_set_pointer| should be
  119. * used depending on whether thread IDs are numbers or pointers on the host
  120. * system. */
  121. int CRYPTO_THREADID_set_callback(
  122. void (*threadid_func)(CRYPTO_THREADID *threadid));
  123. void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
  124. void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
  125. /* Private functions: */
  126. /* CRYPTO_get_locking_callback returns the callback, if any, that was most
  127. * recently set using |CRYPTO_set_locking_callback|. */
  128. void (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
  129. const char *file, int line);
  130. /* CRYPTO_get_add_lock_callback returns the callback, if any, that was most
  131. * recently set using |CRYPTO_set_add_lock_callback|. */
  132. int (*CRYPTO_get_add_lock_callback(void))(int *num, int amount, int lock_num,
  133. const char *file, int line);
  134. /* CRYPTO_lock locks or unlocks the lock specified by |lock_num| (one of
  135. * |CRYPTO_LOCK_*|). Don't call this directly, rather use one of the
  136. * CRYPTO_[rw]_(un)lock macros. */
  137. void CRYPTO_lock(int mode, int lock_num, const char *file, int line);
  138. /* CRYPTO_add_lock adds |amount| to |*pointer|, protected by the lock specified
  139. * by |lock_num|. It returns the new value of |*pointer|. Don't call this
  140. * function directly, rather use the |CRYPTO_add_lock| macro.
  141. *
  142. * TODO(fork): rename to CRYPTO_add_locked. */
  143. int CRYPTO_add_lock(int *pointer, int amount, int lock_num, const char *file,
  144. int line);
  145. /* CRYPTO_THREADID_current stores the current thread identifier in |id|. */
  146. void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
  147. /* CRYPTO_THREADID_cmp returns < 0, 0 or > 0 if |a| is less than, equal to or
  148. * greater than |b|, respectively. */
  149. int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b);
  150. /* CRYPTO_THREADID_cpy sets |*dest| equal to |*src|. */
  151. void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src);
  152. /* CRYPTO_THREADID_hash returns a hash of the numeric value of |id|. */
  153. uint32_t CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
  154. /* These are the locks used by OpenSSL. These values should match up with the
  155. * table in thread.c. */
  156. #define CRYPTO_LOCK_ERR 1
  157. #define CRYPTO_LOCK_EX_DATA 2
  158. #define CRYPTO_LOCK_X509 3
  159. #define CRYPTO_LOCK_X509_INFO 4
  160. #define CRYPTO_LOCK_X509_PKEY 5
  161. #define CRYPTO_LOCK_X509_CRL 6
  162. #define CRYPTO_LOCK_X509_REQ 7
  163. #define CRYPTO_LOCK_DSA 8
  164. #define CRYPTO_LOCK_RSA 9
  165. #define CRYPTO_LOCK_EVP_PKEY 10
  166. #define CRYPTO_LOCK_X509_STORE 11
  167. #define CRYPTO_LOCK_SSL_CTX 12
  168. #define CRYPTO_LOCK_SSL_CERT 13
  169. #define CRYPTO_LOCK_SSL_SESSION 14
  170. #define CRYPTO_LOCK_SSL_SESS_CERT 15
  171. #define CRYPTO_LOCK_SSL 16
  172. #define CRYPTO_LOCK_SSL_METHOD 17
  173. #define CRYPTO_LOCK_RAND 18
  174. #define CRYPTO_LOCK_RAND2 19
  175. #define CRYPTO_LOCK_MALLOC 20
  176. #define CRYPTO_LOCK_BIO 21
  177. #define CRYPTO_LOCK_GETHOSTBYNAME 22
  178. #define CRYPTO_LOCK_GETSERVBYNAME 23
  179. #define CRYPTO_LOCK_READDIR 24
  180. #define CRYPTO_LOCK_RSA_BLINDING 25
  181. #define CRYPTO_LOCK_DH 26
  182. #define CRYPTO_LOCK_MALLOC2 27
  183. #define CRYPTO_LOCK_DSO 28
  184. #define CRYPTO_LOCK_DYNLOCK 29
  185. #define CRYPTO_LOCK_ENGINE 30
  186. #define CRYPTO_LOCK_UI 31
  187. #define CRYPTO_LOCK_ECDSA 32
  188. #define CRYPTO_LOCK_EC 33
  189. #define CRYPTO_LOCK_ECDH 34
  190. #define CRYPTO_LOCK_BN 35
  191. #define CRYPTO_LOCK_EC_PRE_COMP 36
  192. #define CRYPTO_LOCK_STORE 37
  193. #define CRYPTO_LOCK_COMP 38
  194. #define CRYPTO_LOCK_FIPS 39
  195. #define CRYPTO_LOCK_FIPS2 40
  196. #define CRYPTO_LOCK_OBJ 40
  197. #define CRYPTO_NUM_LOCKS 42
  198. #define CRYPTO_LOCK 1
  199. #define CRYPTO_UNLOCK 2
  200. #define CRYPTO_READ 4
  201. #define CRYPTO_WRITE 8
  202. #define CRYPTO_w_lock(lock_num) \
  203. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  204. #define CRYPTO_w_unlock(lock_num) \
  205. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  206. #define CRYPTO_r_lock(lock_num) \
  207. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  208. #define CRYPTO_r_unlock(lock_num) \
  209. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  210. #define CRYPTO_add(addr, amount, lock_num) \
  211. CRYPTO_add_lock(addr, amount, lock_num, __FILE__, __LINE__)
  212. /* Private functions.
  213. *
  214. * Some old code calls these functions and so no-op implementations are
  215. * provided.
  216. *
  217. * TODO(fork): cleanup callers and remove. */
  218. void CRYPTO_set_id_callback(unsigned long (*func)(void));
  219. typedef struct {
  220. int references;
  221. struct CRYPTO_dynlock_value *data;
  222. } CRYPTO_dynlock;
  223. void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(
  224. *dyn_create_function)(const char *file, int line));
  225. void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(
  226. int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
  227. void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(
  228. struct CRYPTO_dynlock_value *l, const char *file, int line));
  229. #if defined(__cplusplus)
  230. } /* extern C */
  231. #endif
  232. #endif /* OPENSSL_HEADER_THREAD_H */