Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

552 строки
22 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. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
  109. #define OPENSSL_HEADER_CRYPTO_INTERNAL_H
  110. #include <openssl/ex_data.h>
  111. #include <openssl/thread.h>
  112. #if defined(OPENSSL_NO_THREADS)
  113. #elif defined(OPENSSL_WINDOWS)
  114. #pragma warning(push, 3)
  115. #include <windows.h>
  116. #pragma warning(pop)
  117. #else
  118. #include <pthread.h>
  119. #endif
  120. #if defined(__cplusplus)
  121. extern "C" {
  122. #endif
  123. /* MSVC's C4701 warning about the use of *potentially*--as opposed to
  124. * *definitely*--uninitialized values sometimes has false positives. Usually
  125. * the false positives can and should be worked around by simplifying the
  126. * control flow. When that is not practical, annotate the function containing
  127. * the code that triggers the warning with
  128. * OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS after its parameters:
  129. *
  130. * void f() OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS {
  131. * ...
  132. * }
  133. *
  134. * Note that MSVC's control flow analysis seems to operate on a whole-function
  135. * basis, so the annotation must be placed on the entire function, not just a
  136. * block within the function. */
  137. #if defined(_MSC_VER)
  138. #define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS \
  139. __pragma(warning(suppress:4701))
  140. #else
  141. #define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS
  142. #endif
  143. /* MSVC will sometimes correctly detect unreachable code and issue a warning,
  144. * which breaks the build since we treat errors as warnings, in some rare cases
  145. * where we want to allow the dead code to continue to exist. In these
  146. * situations, annotate the function containing the unreachable code with
  147. * OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS after its parameters:
  148. *
  149. * void f() OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  150. * ...
  151. * }
  152. *
  153. * Note that MSVC's reachability analysis seems to operate on a whole-function
  154. * basis, so the annotation must be placed on the entire function, not just a
  155. * block within the function. */
  156. #if defined(_MSC_VER)
  157. #define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS \
  158. __pragma(warning(suppress:4702))
  159. #else
  160. #define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS
  161. #endif
  162. #if defined(_MSC_VER)
  163. #define OPENSSL_U64(x) x##UI64
  164. #else
  165. #if defined(OPENSSL_64_BIT)
  166. #define OPENSSL_U64(x) x##UL
  167. #else
  168. #define OPENSSL_U64(x) x##ULL
  169. #endif
  170. #endif /* defined(_MSC_VER) */
  171. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
  172. defined(OPENSSL_AARCH64)
  173. /* OPENSSL_cpuid_setup initializes OPENSSL_ia32cap_P. */
  174. void OPENSSL_cpuid_setup(void);
  175. #endif
  176. #if !defined(inline)
  177. #define inline __inline
  178. #endif
  179. /* Constant-time utility functions.
  180. *
  181. * The following methods return a bitmask of all ones (0xff...f) for true and 0
  182. * for false. This is useful for choosing a value based on the result of a
  183. * conditional in constant time. For example,
  184. *
  185. * if (a < b) {
  186. * c = a;
  187. * } else {
  188. * c = b;
  189. * }
  190. *
  191. * can be written as
  192. *
  193. * unsigned int lt = constant_time_lt(a, b);
  194. * c = constant_time_select(lt, a, b); */
  195. /* constant_time_msb returns the given value with the MSB copied to all the
  196. * other bits. */
  197. static inline unsigned int constant_time_msb(unsigned int a) {
  198. return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
  199. }
  200. /* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */
  201. static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
  202. /* Consider the two cases of the problem:
  203. * msb(a) == msb(b): a < b iff the MSB of a - b is set.
  204. * msb(a) != msb(b): a < b iff the MSB of b is set.
  205. *
  206. * If msb(a) == msb(b) then the following evaluates as:
  207. * msb(a^((a^b)|((a-b)^a))) ==
  208. * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
  209. * msb(a^a^(a-b)) == (rearranging)
  210. * msb(a-b) (because ∀x. x^x == 0)
  211. *
  212. * Else, if msb(a) != msb(b) then the following evaluates as:
  213. * msb(a^((a^b)|((a-b)^a))) ==
  214. * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
  215. * represents a value s.t. msb(𝟙) = 1)
  216. * msb(a^𝟙) == (because ORing with 1 results in 1)
  217. * msb(b)
  218. *
  219. *
  220. * Here is an SMT-LIB verification of this formula:
  221. *
  222. * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
  223. * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
  224. * )
  225. *
  226. * (declare-fun a () (_ BitVec 32))
  227. * (declare-fun b () (_ BitVec 32))
  228. *
  229. * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
  230. * (check-sat)
  231. * (get-model)
  232. */
  233. return constant_time_msb(a^((a^b)|((a-b)^a)));
  234. }
  235. /* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
  236. static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
  237. return (uint8_t)(constant_time_lt(a, b));
  238. }
  239. /* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
  240. static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
  241. return ~constant_time_lt(a, b);
  242. }
  243. /* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
  244. static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
  245. return (uint8_t)(constant_time_ge(a, b));
  246. }
  247. /* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
  248. static inline unsigned int constant_time_is_zero(unsigned int a) {
  249. /* Here is an SMT-LIB verification of this formula:
  250. *
  251. * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
  252. * (bvand (bvnot a) (bvsub a #x00000001))
  253. * )
  254. *
  255. * (declare-fun a () (_ BitVec 32))
  256. *
  257. * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
  258. * (check-sat)
  259. * (get-model)
  260. */
  261. return constant_time_msb(~a & (a - 1));
  262. }
  263. /* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
  264. * mask. */
  265. static inline uint8_t constant_time_is_zero_8(unsigned int a) {
  266. return (uint8_t)(constant_time_is_zero(a));
  267. }
  268. /* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
  269. static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
  270. return constant_time_is_zero(a ^ b);
  271. }
  272. /* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
  273. static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
  274. return (uint8_t)(constant_time_eq(a, b));
  275. }
  276. /* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
  277. static inline unsigned int constant_time_eq_int(int a, int b) {
  278. return constant_time_eq((unsigned)(a), (unsigned)(b));
  279. }
  280. /* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
  281. * mask. */
  282. static inline uint8_t constant_time_eq_int_8(int a, int b) {
  283. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  284. }
  285. /* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
  286. * or all 0s (as returned by the methods above), the select methods return
  287. * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
  288. static inline unsigned int constant_time_select(unsigned int mask,
  289. unsigned int a, unsigned int b) {
  290. return (mask & a) | (~mask & b);
  291. }
  292. /* constant_time_select_8 acts like |constant_time_select| but operates on
  293. * 8-bit values. */
  294. static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
  295. uint8_t b) {
  296. return (uint8_t)(constant_time_select(mask, a, b));
  297. }
  298. /* constant_time_select_int acts like |constant_time_select| but operates on
  299. * ints. */
  300. static inline int constant_time_select_int(unsigned int mask, int a, int b) {
  301. return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
  302. }
  303. /* Thread-safe initialisation. */
  304. #if defined(OPENSSL_NO_THREADS)
  305. typedef uint32_t CRYPTO_once_t;
  306. #define CRYPTO_ONCE_INIT 0
  307. #elif defined(OPENSSL_WINDOWS)
  308. typedef LONG CRYPTO_once_t;
  309. #define CRYPTO_ONCE_INIT 0
  310. #else
  311. typedef pthread_once_t CRYPTO_once_t;
  312. #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
  313. #endif
  314. /* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
  315. * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
  316. * then they will block until |init| completes, but |init| will have only been
  317. * called once.
  318. *
  319. * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
  320. * the value |CRYPTO_ONCE_INIT|. */
  321. OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
  322. /* Reference counting. */
  323. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
  324. !defined(__STDC_NO_ATOMICS__)
  325. /* OSX's atomic support is broken: the compiler sets the right macros but
  326. * stdatomic.h is missing. */
  327. #if !defined(OPENSSL_APPLE)
  328. #define OPENSSL_C11_ATOMIC
  329. #endif
  330. #endif
  331. /* CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates. */
  332. #define CRYPTO_REFCOUNT_MAX 0xffffffff
  333. /* CRYPTO_refcount_inc atomically increments the value at |*count| unless the
  334. * value would overflow. It's safe for multiple threads to concurrently call
  335. * this or |CRYPTO_refcount_dec_and_test_zero| on the same
  336. * |CRYPTO_refcount_t|. */
  337. OPENSSL_EXPORT void CRYPTO_refcount_inc(CRYPTO_refcount_t *count);
  338. /* CRYPTO_refcount_dec_and_test_zero tests the value at |*count|:
  339. * if it's zero, it crashes the address space.
  340. * if it's the maximum value, it returns zero.
  341. * otherwise, it atomically decrements it and returns one iff the resulting
  342. * value is zero.
  343. *
  344. * It's safe for multiple threads to concurrently call this or
  345. * |CRYPTO_refcount_inc| on the same |CRYPTO_refcount_t|. */
  346. OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);
  347. /* Locks.
  348. *
  349. * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
  350. * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
  351. * a global lock. A global lock must be initialised to the value
  352. * |CRYPTO_STATIC_MUTEX_INIT|.
  353. *
  354. * |CRYPTO_MUTEX| can appear in public structures and so is defined in
  355. * thread.h.
  356. *
  357. * The global lock is a different type because there's no static initialiser
  358. * value on Windows for locks, so global locks have to be coupled with a
  359. * |CRYPTO_once_t| to ensure that the lock is setup before use. This is done
  360. * automatically by |CRYPTO_STATIC_MUTEX_lock_*|. */
  361. #if defined(OPENSSL_NO_THREADS)
  362. struct CRYPTO_STATIC_MUTEX {};
  363. #define CRYPTO_STATIC_MUTEX_INIT {}
  364. #elif defined(OPENSSL_WINDOWS)
  365. struct CRYPTO_STATIC_MUTEX {
  366. CRYPTO_once_t once;
  367. CRITICAL_SECTION lock;
  368. };
  369. #define CRYPTO_STATIC_MUTEX_INIT { CRYPTO_ONCE_INIT, { 0 } }
  370. #else
  371. struct CRYPTO_STATIC_MUTEX {
  372. pthread_rwlock_t lock;
  373. };
  374. #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
  375. #endif
  376. /* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
  377. * |CRYPTO_STATIC_MUTEX|. */
  378. OPENSSL_EXPORT void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
  379. /* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
  380. * read lock, but none may have a write lock. (On Windows, read locks are
  381. * actually fully exclusive.) */
  382. OPENSSL_EXPORT void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
  383. /* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
  384. * of lock on it. */
  385. OPENSSL_EXPORT void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
  386. /* CRYPTO_MUTEX_unlock unlocks |lock|. */
  387. OPENSSL_EXPORT void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock);
  388. /* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
  389. OPENSSL_EXPORT void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
  390. /* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
  391. * have a read lock, but none may have a write lock. The |lock| variable does
  392. * not need to be initialised by any function, but must have been statically
  393. * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
  394. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_read(
  395. struct CRYPTO_STATIC_MUTEX *lock);
  396. /* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
  397. * any type of lock on it. The |lock| variable does not need to be initialised
  398. * by any function, but must have been statically initialised with
  399. * |CRYPTO_STATIC_MUTEX_INIT|. */
  400. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_lock_write(
  401. struct CRYPTO_STATIC_MUTEX *lock);
  402. /* CRYPTO_STATIC_MUTEX_unlock unlocks |lock|. */
  403. OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock(
  404. struct CRYPTO_STATIC_MUTEX *lock);
  405. /* Thread local storage. */
  406. /* thread_local_data_t enumerates the types of thread-local data that can be
  407. * stored. */
  408. typedef enum {
  409. OPENSSL_THREAD_LOCAL_ERR = 0,
  410. OPENSSL_THREAD_LOCAL_RAND,
  411. OPENSSL_THREAD_LOCAL_TEST,
  412. NUM_OPENSSL_THREAD_LOCALS,
  413. } thread_local_data_t;
  414. /* thread_local_destructor_t is the type of a destructor function that will be
  415. * called when a thread exits and its thread-local storage needs to be freed. */
  416. typedef void (*thread_local_destructor_t)(void *);
  417. /* CRYPTO_get_thread_local gets the pointer value that is stored for the
  418. * current thread for the given index, or NULL if none has been set. */
  419. OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
  420. /* CRYPTO_set_thread_local sets a pointer value for the current thread at the
  421. * given index. This function should only be called once per thread for a given
  422. * |index|: rather than update the pointer value itself, update the data that
  423. * is pointed to.
  424. *
  425. * The destructor function will be called when a thread exits to free this
  426. * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
  427. * |index| should have the same |destructor| argument. The destructor may be
  428. * called with a NULL argument if a thread that never set a thread-local
  429. * pointer for |index|, exits. The destructor may be called concurrently with
  430. * different arguments.
  431. *
  432. * This function returns one on success or zero on error. If it returns zero
  433. * then |destructor| has been called with |value| already. */
  434. OPENSSL_EXPORT int CRYPTO_set_thread_local(
  435. thread_local_data_t index, void *value,
  436. thread_local_destructor_t destructor);
  437. /* ex_data */
  438. typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
  439. /* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
  440. * supports ex_data. It should defined as a static global within the module
  441. * which defines that type. */
  442. typedef struct {
  443. struct CRYPTO_STATIC_MUTEX lock;
  444. STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
  445. } CRYPTO_EX_DATA_CLASS;
  446. #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL}
  447. /* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes
  448. * it to |*out_index|. Each class of object should provide a wrapper function
  449. * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and
  450. * zero otherwise. */
  451. OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class,
  452. int *out_index, long argl,
  453. void *argp, CRYPTO_EX_new *new_func,
  454. CRYPTO_EX_dup *dup_func,
  455. CRYPTO_EX_free *free_func);
  456. /* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
  457. * of object should provide a wrapper function. */
  458. OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
  459. /* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
  460. * if no such index exists. Each class of object should provide a wrapper
  461. * function. */
  462. OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
  463. /* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA| which is
  464. * embedded inside of |obj| which is of class |ex_data_class|. Returns one on
  465. * success and zero otherwise. */
  466. OPENSSL_EXPORT int CRYPTO_new_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  467. void *obj, CRYPTO_EX_DATA *ad);
  468. /* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
  469. * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
  470. * class. It returns one on success and zero otherwise. */
  471. OPENSSL_EXPORT int CRYPTO_dup_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  472. CRYPTO_EX_DATA *to,
  473. const CRYPTO_EX_DATA *from);
  474. /* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
  475. * object of the given class. */
  476. OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  477. void *obj, CRYPTO_EX_DATA *ad);
  478. #if defined(__cplusplus)
  479. } /* extern C */
  480. #endif
  481. #endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */