Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

523 rader
21 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_WINDOWS)
  113. #include <pthread.h>
  114. #else
  115. #pragma warning(push, 3)
  116. #include <windows.h>
  117. #pragma warning(pop)
  118. #endif
  119. #if defined(__cplusplus)
  120. extern "C" {
  121. #endif
  122. /* MSVC's C4701 warning about the use of *potentially*--as opposed to
  123. * *definitely*--uninitialized values sometimes has false positives. Usually
  124. * the false positives can and should be worked around by simplifying the
  125. * control flow. When that is not practical, annotate the function containing
  126. * the code that triggers the warning with
  127. * OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS after its parameters:
  128. *
  129. * void f() OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS {
  130. * ...
  131. * }
  132. *
  133. * Note that MSVC's control flow analysis seems to operate on a whole-function
  134. * basis, so the annotation must be placed on the entire function, not just a
  135. * block within the function. */
  136. #if defined(_MSC_VER)
  137. #define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS \
  138. __pragma(warning(suppress:4701))
  139. #else
  140. #define OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS
  141. #endif
  142. /* MSVC will sometimes correctly detect unreachable code and issue a warning,
  143. * which breaks the build since we treat errors as warnings, in some rare cases
  144. * where we want to allow the dead code to continue to exist. In these
  145. * situations, annotate the function containing the unreachable code with
  146. * OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS after its parameters:
  147. *
  148. * void f() OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  149. * ...
  150. * }
  151. *
  152. * Note that MSVC's reachability analysis seems to operate on a whole-function
  153. * basis, so the annotation must be placed on the entire function, not just a
  154. * block within the function. */
  155. #if defined(_MSC_VER)
  156. #define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS \
  157. __pragma(warning(suppress:4702))
  158. #else
  159. #define OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS
  160. #endif
  161. #if defined(_MSC_VER)
  162. #define OPENSSL_U64(x) x##UI64
  163. #else
  164. #if defined(OPENSSL_64_BIT)
  165. #define OPENSSL_U64(x) x##UL
  166. #else
  167. #define OPENSSL_U64(x) x##ULL
  168. #endif
  169. #endif /* defined(_MSC_VER) */
  170. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
  171. defined(OPENSSL_AARCH64)
  172. /* OPENSSL_cpuid_setup initializes OPENSSL_ia32cap_P. */
  173. void OPENSSL_cpuid_setup(void);
  174. #endif
  175. #if !defined(inline)
  176. #define inline __inline
  177. #endif
  178. /* Constant-time utility functions.
  179. *
  180. * The following methods return a bitmask of all ones (0xff...f) for true and 0
  181. * for false. This is useful for choosing a value based on the result of a
  182. * conditional in constant time. For example,
  183. *
  184. * if (a < b) {
  185. * c = a;
  186. * } else {
  187. * c = b;
  188. * }
  189. *
  190. * can be written as
  191. *
  192. * unsigned int lt = constant_time_lt(a, b);
  193. * c = constant_time_select(lt, a, b); */
  194. /* constant_time_msb returns the given value with the MSB copied to all the
  195. * other bits. */
  196. static inline unsigned int constant_time_msb(unsigned int a) {
  197. return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
  198. }
  199. /* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */
  200. static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
  201. /* Consider the two cases of the problem:
  202. * msb(a) == msb(b): a < b iff the MSB of a - b is set.
  203. * msb(a) != msb(b): a < b iff the MSB of b is set.
  204. *
  205. * If msb(a) == msb(b) then the following evaluates as:
  206. * msb(a^((a^b)|((a-b)^a))) ==
  207. * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
  208. * msb(a^a^(a-b)) == (rearranging)
  209. * msb(a-b) (because ∀x. x^x == 0)
  210. *
  211. * Else, if msb(a) != msb(b) then the following evaluates as:
  212. * msb(a^((a^b)|((a-b)^a))) ==
  213. * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
  214. * represents a value s.t. msb(𝟙) = 1)
  215. * msb(a^𝟙) == (because ORing with 1 results in 1)
  216. * msb(b)
  217. *
  218. *
  219. * Here is an SMT-LIB verification of this formula:
  220. *
  221. * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
  222. * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
  223. * )
  224. *
  225. * (declare-fun a () (_ BitVec 32))
  226. * (declare-fun b () (_ BitVec 32))
  227. *
  228. * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
  229. * (check-sat)
  230. * (get-model)
  231. */
  232. return constant_time_msb(a^((a^b)|((a-b)^a)));
  233. }
  234. /* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
  235. static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
  236. return (uint8_t)(constant_time_lt(a, b));
  237. }
  238. /* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
  239. static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
  240. return ~constant_time_lt(a, b);
  241. }
  242. /* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
  243. static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
  244. return (uint8_t)(constant_time_ge(a, b));
  245. }
  246. /* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
  247. static inline unsigned int constant_time_is_zero(unsigned int a) {
  248. /* Here is an SMT-LIB verification of this formula:
  249. *
  250. * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
  251. * (bvand (bvnot a) (bvsub a #x00000001))
  252. * )
  253. *
  254. * (declare-fun a () (_ BitVec 32))
  255. *
  256. * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
  257. * (check-sat)
  258. * (get-model)
  259. */
  260. return constant_time_msb(~a & (a - 1));
  261. }
  262. /* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
  263. * mask. */
  264. static inline uint8_t constant_time_is_zero_8(unsigned int a) {
  265. return (uint8_t)(constant_time_is_zero(a));
  266. }
  267. /* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
  268. static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
  269. return constant_time_is_zero(a ^ b);
  270. }
  271. /* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
  272. static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
  273. return (uint8_t)(constant_time_eq(a, b));
  274. }
  275. /* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
  276. static inline unsigned int constant_time_eq_int(int a, int b) {
  277. return constant_time_eq((unsigned)(a), (unsigned)(b));
  278. }
  279. /* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
  280. * mask. */
  281. static inline uint8_t constant_time_eq_int_8(int a, int b) {
  282. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  283. }
  284. /* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
  285. * or all 0s (as returned by the methods above), the select methods return
  286. * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
  287. static inline unsigned int constant_time_select(unsigned int mask,
  288. unsigned int a, unsigned int b) {
  289. return (mask & a) | (~mask & b);
  290. }
  291. /* constant_time_select_8 acts like |constant_time_select| but operates on
  292. * 8-bit values. */
  293. static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
  294. uint8_t b) {
  295. return (uint8_t)(constant_time_select(mask, a, b));
  296. }
  297. /* constant_time_select_int acts like |constant_time_select| but operates on
  298. * ints. */
  299. static inline int constant_time_select_int(unsigned int mask, int a, int b) {
  300. return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
  301. }
  302. /* Thread-safe initialisation. */
  303. #if !defined(OPENSSL_WINDOWS)
  304. typedef pthread_once_t CRYPTO_once_t;
  305. #define CRYPTO_ONCE_INIT PTHREAD_ONCE_INIT
  306. #else
  307. typedef int32_t CRYPTO_once_t;
  308. #define CRYPTO_ONCE_INIT 0
  309. #endif
  310. /* CRYPTO_once calls |init| exactly once per process. This is thread-safe: if
  311. * concurrent threads call |CRYPTO_once| with the same |CRYPTO_once_t| argument
  312. * then they will block until |init| completes, but |init| will have only been
  313. * called once.
  314. *
  315. * The |once| argument must be a |CRYPTO_once_t| that has been initialised with
  316. * the value |CRYPTO_ONCE_INIT|. */
  317. OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));
  318. /* Locks.
  319. *
  320. * Two types of locks are defined: |CRYPTO_MUTEX|, which can be used in
  321. * structures as normal, and |struct CRYPTO_STATIC_MUTEX|, which can be used as
  322. * a global lock. A global lock must be initialised to the value
  323. * |CRYPTO_STATIC_MUTEX_INIT|.
  324. *
  325. * |CRYPTO_MUTEX| can appear in public structures and so is defined in
  326. * thread.h.
  327. *
  328. * The global lock is a different type because there's no static initialiser
  329. * value on Windows for locks, so global locks have to be coupled with a
  330. * |CRYPTO_once_t| to ensure that the lock is setup before use. This is done
  331. * automatically by |CRYPTO_STATIC_MUTEX_lock_*|. */
  332. #if !defined(OPENSSL_WINDOWS)
  333. struct CRYPTO_STATIC_MUTEX {
  334. pthread_rwlock_t lock;
  335. };
  336. #if !defined(PTHREAD_RWLOCK_INITIALIZER) && defined(__native_client__) && \
  337. defined(_NEWLIB_VERSION)
  338. /* newlib under NaCl is missing PTHREAD_RWLOCK_INITIALIZER. See
  339. * https://code.google.com/p/nativeclient/issues/detail?id=4160. Remove this
  340. * when that bug is fixed. */
  341. #define CRYPTO_STATIC_MUTEX_INIT \
  342. { { PTHREAD_MUTEX_INITIALIZER, 0, 0, NACL_PTHREAD_ILLEGAL_THREAD_ID, \
  343. PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER } }
  344. #else
  345. #define CRYPTO_STATIC_MUTEX_INIT { PTHREAD_RWLOCK_INITIALIZER }
  346. #endif
  347. #else
  348. struct CRYPTO_STATIC_MUTEX {
  349. CRYPTO_once_t once;
  350. CRITICAL_SECTION lock;
  351. };
  352. #define CRYPTO_STATIC_MUTEX_INIT { CRYPTO_ONCE_INIT, { 0 } }
  353. #endif
  354. /* CRYPTO_MUTEX_init initialises |lock|. If |lock| is a static variable, use a
  355. * |CRYPTO_STATIC_MUTEX|. */
  356. void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock);
  357. /* CRYPTO_MUTEX_lock_read locks |lock| such that other threads may also have a
  358. * read lock, but none may have a write lock. (On Windows, read locks are
  359. * actually fully exclusive.) */
  360. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock);
  361. /* CRYPTO_MUTEX_lock_write locks |lock| such that no other thread has any type
  362. * of lock on it. */
  363. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock);
  364. /* CRYPTO_MUTEX_unlock unlocks |lock|. */
  365. void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock);
  366. /* CRYPTO_MUTEX_cleanup releases all resources held by |lock|. */
  367. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock);
  368. /* CRYPTO_STATIC_MUTEX_lock_read locks |lock| such that other threads may also
  369. * have a read lock, but none may have a write lock. The |lock| variable does
  370. * not need to be initialised by any function, but must have been statically
  371. * initialised with |CRYPTO_STATIC_MUTEX_INIT|. */
  372. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock);
  373. /* CRYPTO_STATIC_MUTEX_lock_write locks |lock| such that no other thread has
  374. * any type of lock on it. The |lock| variable does not need to be initialised
  375. * by any function, but must have been statically initialised with
  376. * |CRYPTO_STATIC_MUTEX_INIT|. */
  377. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock);
  378. /* CRYPTO_STATIC_MUTEX_unlock unlocks |lock|. */
  379. void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock);
  380. /* Thread local storage. */
  381. /* thread_local_data_t enumerates the types of thread-local data that can be
  382. * stored. */
  383. typedef enum {
  384. OPENSSL_THREAD_LOCAL_ERR = 0,
  385. OPENSSL_THREAD_LOCAL_RAND,
  386. OPENSSL_THREAD_LOCAL_TEST,
  387. NUM_OPENSSL_THREAD_LOCALS,
  388. } thread_local_data_t;
  389. /* thread_local_destructor_t is the type of a destructor function that will be
  390. * called when a thread exits and its thread-local storage needs to be freed. */
  391. typedef void (*thread_local_destructor_t)(void *);
  392. /* CRYPTO_get_thread_local gets the pointer value that is stored for the
  393. * current thread for the given index, or NULL if none has been set. */
  394. OPENSSL_EXPORT void *CRYPTO_get_thread_local(thread_local_data_t value);
  395. /* CRYPTO_set_thread_local sets a pointer value for the current thread at the
  396. * given index. This function should only be called once per thread for a given
  397. * |index|: rather than update the pointer value itself, update the data that
  398. * is pointed to.
  399. *
  400. * The destructor function will be called when a thread exits to free this
  401. * thread-local data. All calls to |CRYPTO_set_thread_local| with the same
  402. * |index| should have the same |destructor| argument. The destructor may be
  403. * called with a NULL argument if a thread that never set a thread-local
  404. * pointer for |index|, exits. The destructor may be called concurrently with
  405. * different arguments.
  406. *
  407. * This function returns one on success or zero on error. If it returns zero
  408. * then |destructor| has been called with |value| already. */
  409. OPENSSL_EXPORT int CRYPTO_set_thread_local(
  410. thread_local_data_t index, void *value,
  411. thread_local_destructor_t destructor);
  412. /* ex_data */
  413. typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
  414. /* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
  415. * supports ex_data. It should defined as a static global within the module
  416. * which defines that type. */
  417. typedef struct {
  418. struct CRYPTO_STATIC_MUTEX lock;
  419. STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
  420. } CRYPTO_EX_DATA_CLASS;
  421. #define CRYPTO_EX_DATA_CLASS_INIT {CRYPTO_STATIC_MUTEX_INIT, NULL}
  422. /* CRYPTO_get_ex_new_index allocates a new index for |ex_data_class| and writes
  423. * it to |*out_index|. Each class of object should provide a wrapper function
  424. * that uses the correct |CRYPTO_EX_DATA_CLASS|. It returns one on success and
  425. * zero otherwise. */
  426. OPENSSL_EXPORT int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class,
  427. int *out_index, long argl,
  428. void *argp, CRYPTO_EX_new *new_func,
  429. CRYPTO_EX_dup *dup_func,
  430. CRYPTO_EX_free *free_func);
  431. /* CRYPTO_set_ex_data sets an extra data pointer on a given object. Each class
  432. * of object should provide a wrapper function. */
  433. OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
  434. /* CRYPTO_get_ex_data returns an extra data pointer for a given object, or NULL
  435. * if no such index exists. Each class of object should provide a wrapper
  436. * function. */
  437. OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
  438. /* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA| which is
  439. * embedded inside of |obj| which is of class |ex_data_class|. Returns one on
  440. * success and zero otherwise. */
  441. OPENSSL_EXPORT int CRYPTO_new_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  442. void *obj, CRYPTO_EX_DATA *ad);
  443. /* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
  444. * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
  445. * class. It returns one on success and zero otherwise. */
  446. OPENSSL_EXPORT int CRYPTO_dup_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  447. CRYPTO_EX_DATA *to,
  448. const CRYPTO_EX_DATA *from);
  449. /* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
  450. * object of the given class. */
  451. OPENSSL_EXPORT void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
  452. void *obj, CRYPTO_EX_DATA *ad);
  453. #if defined(__cplusplus)
  454. } /* extern C */
  455. #endif
  456. #endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */