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.
 
 
 
 
 
 

261 line
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 <sys/types.h>
  59. #include <openssl/base.h>
  60. #if defined(__cplusplus)
  61. extern "C" {
  62. #endif
  63. #if defined(OPENSSL_WINDOWS)
  64. /* CRYPTO_MUTEX can appear in public header files so we really don't want to
  65. * pull in windows.h. It's statically asserted that this structure is large
  66. * enough to contain a Windows CRITICAL_SECTION by thread_win.c. */
  67. typedef union crypto_mutex_st {
  68. double alignment;
  69. uint8_t padding[4*sizeof(void*) + 2*sizeof(int)];
  70. } CRYPTO_MUTEX;
  71. #elif defined(__MACH__) && defined(__APPLE__)
  72. typedef pthread_rwlock_t CRYPTO_MUTEX;
  73. #else
  74. /* It is reasonable to include pthread.h on non-Windows systems, however the
  75. * |pthread_rwlock_t| that we need is hidden under feature flags, and we can't
  76. * ensure that we'll be able to get it. It's statically asserted that this
  77. * structure is large enough to contain a |pthread_rwlock_t| by
  78. * thread_pthread.c. */
  79. typedef union crypto_mutex_st {
  80. double alignment;
  81. uint8_t padding[3*sizeof(int) + 5*sizeof(unsigned) + 16 + 8];
  82. } CRYPTO_MUTEX;
  83. #endif
  84. /* Functions to support multithreading.
  85. *
  86. * OpenSSL can safely be used in multi-threaded applications provided that at
  87. * least |CRYPTO_set_locking_callback| is set.
  88. *
  89. * The locking callback performs mutual exclusion. Rather than using a single
  90. * lock for all, shared data-structures, OpenSSL requires that the locking
  91. * callback support a fixed (at run-time) number of different locks, given by
  92. * |CRYPTO_num_locks|. */
  93. /* CRYPTO_num_locks returns the number of static locks that the callback
  94. * function passed to |CRYPTO_set_locking_callback| must be able to handle. */
  95. OPENSSL_EXPORT int CRYPTO_num_locks(void);
  96. /* CRYPTO_set_locking_callback sets a callback function that implements locking
  97. * on behalf of OpenSSL. The callback is called whenever OpenSSL needs to lock
  98. * or unlock a lock, and locks are specified as a number between zero and
  99. * |CRYPTO_num_locks()-1|.
  100. *
  101. * The mode argument to the callback is a bitwise-OR of either CRYPTO_LOCK or
  102. * CRYPTO_UNLOCK, to denote the action, and CRYPTO_READ or CRYPTO_WRITE, to
  103. * indicate the type of lock. The |file| and |line| arguments give the location
  104. * in the OpenSSL source where the locking action originated. */
  105. OPENSSL_EXPORT void CRYPTO_set_locking_callback(
  106. void (*func)(int mode, int lock_num, const char *file, int line));
  107. /* CRYPTO_set_add_lock_callback sets an optional callback which is used when
  108. * OpenSSL needs to add a fixed amount to an integer. For example, this is used
  109. * when maintaining reference counts. Normally the reference counts are
  110. * maintained by performing the addition under a lock but, if this callback
  111. * has been set, the application is free to implement the operation using
  112. * faster methods (i.e. atomic operations).
  113. *
  114. * The callback is given a pointer to the integer to be altered (|num|), the
  115. * amount to add to the integer (|amount|, which may be negative), the number
  116. * of the lock which would have been taken to protect the operation and the
  117. * position in the OpenSSL code where the operation originated. */
  118. OPENSSL_EXPORT void CRYPTO_set_add_lock_callback(int (*func)(
  119. int *num, int amount, int lock_num, const char *file, int line));
  120. /* CRYPTO_get_lock_name returns the name of the lock given by |lock_num|. This
  121. * can be used in a locking callback for debugging purposes. */
  122. OPENSSL_EXPORT const char *CRYPTO_get_lock_name(int lock_num);
  123. /* Deprecated functions */
  124. /* CRYPTO_THREADID_set_callback does nothing. */
  125. OPENSSL_EXPORT int CRYPTO_THREADID_set_callback(
  126. void (*threadid_func)(CRYPTO_THREADID *threadid));
  127. /* CRYPTO_THREADID_set_numeric does nothing. */
  128. OPENSSL_EXPORT void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id,
  129. unsigned long val);
  130. /* CRYPTO_THREADID_set_pointer does nothing. */
  131. OPENSSL_EXPORT void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
  132. /* CRYPTO_THREADID_current does nothing. */
  133. OPENSSL_EXPORT void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
  134. /* Private functions: */
  135. /* CRYPTO_get_locking_callback returns the callback, if any, that was most
  136. * recently set using |CRYPTO_set_locking_callback|. */
  137. void (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
  138. const char *file, int line);
  139. /* CRYPTO_get_add_lock_callback returns the callback, if any, that was most
  140. * recently set using |CRYPTO_set_add_lock_callback|. */
  141. int (*CRYPTO_get_add_lock_callback(void))(int *num, int amount, int lock_num,
  142. const char *file, int line);
  143. /* CRYPTO_lock locks or unlocks the lock specified by |lock_num| (one of
  144. * |CRYPTO_LOCK_*|). Don't call this directly, rather use one of the
  145. * CRYPTO_[rw]_(un)lock macros. */
  146. OPENSSL_EXPORT void CRYPTO_lock(int mode, int lock_num, const char *file,
  147. int line);
  148. /* CRYPTO_add_lock adds |amount| to |*pointer|, protected by the lock specified
  149. * by |lock_num|. It returns the new value of |*pointer|. Don't call this
  150. * function directly, rather use the |CRYPTO_add| macro. */
  151. OPENSSL_EXPORT int CRYPTO_add_lock(int *pointer, int amount, int lock_num,
  152. const char *file, int line);
  153. /* Lock IDs start from 1. CRYPTO_LOCK_INVALID_LOCK is an unused placeholder
  154. * used to ensure no lock has ID 0. */
  155. #define CRYPTO_LOCK_LIST \
  156. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_INVALID_LOCK), \
  157. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_BIO), \
  158. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_DH), \
  159. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_DSA), \
  160. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_EC), \
  161. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_EC_PRE_COMP), \
  162. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_ERR), \
  163. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_EVP_PKEY), \
  164. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_EX_DATA), \
  165. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_OBJ), \
  166. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_RAND), \
  167. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_READDIR), \
  168. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_RSA), \
  169. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_RSA_BLINDING), \
  170. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_SSL_CTX), \
  171. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_SSL_SESSION), \
  172. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509), \
  173. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509_INFO), \
  174. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509_PKEY), \
  175. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509_CRL), \
  176. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509_REQ), \
  177. CRYPTO_LOCK_ITEM(CRYPTO_LOCK_X509_STORE), \
  178. #define CRYPTO_LOCK_ITEM(x) x
  179. enum {
  180. CRYPTO_LOCK_LIST
  181. };
  182. #undef CRYPTO_LOCK_ITEM
  183. #define CRYPTO_LOCK 1
  184. #define CRYPTO_UNLOCK 2
  185. #define CRYPTO_READ 4
  186. #define CRYPTO_WRITE 8
  187. #define CRYPTO_w_lock(lock_num) \
  188. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  189. #define CRYPTO_w_unlock(lock_num) \
  190. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  191. #define CRYPTO_r_lock(lock_num) \
  192. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  193. #define CRYPTO_r_unlock(lock_num) \
  194. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  195. #define CRYPTO_add(addr, amount, lock_num) \
  196. CRYPTO_add_lock(addr, amount, lock_num, __FILE__, __LINE__)
  197. /* Private functions.
  198. *
  199. * Some old code calls these functions and so no-op implementations are
  200. * provided.
  201. *
  202. * TODO(fork): cleanup callers and remove. */
  203. OPENSSL_EXPORT void CRYPTO_set_id_callback(unsigned long (*func)(void));
  204. typedef struct {
  205. int references;
  206. struct CRYPTO_dynlock_value *data;
  207. } CRYPTO_dynlock;
  208. OPENSSL_EXPORT void CRYPTO_set_dynlock_create_callback(
  209. struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file,
  210. int line));
  211. OPENSSL_EXPORT void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(
  212. int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
  213. OPENSSL_EXPORT void CRYPTO_set_dynlock_destroy_callback(
  214. void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l,
  215. const char *file, int line));
  216. #if defined(__cplusplus)
  217. } /* extern C */
  218. #endif
  219. #endif /* OPENSSL_HEADER_THREAD_H */