Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

486 рядки
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. #ifndef OPENSSL_HEADER_STACK_H
  57. #define OPENSSL_HEADER_STACK_H
  58. #include <openssl/base.h>
  59. #include <openssl/type_check.h>
  60. #if defined(__cplusplus)
  61. extern "C" {
  62. #endif
  63. // A stack, in OpenSSL, is an array of pointers. They are the most commonly
  64. // used collection object.
  65. //
  66. // This file defines macros for type safe use of the stack functions. A stack
  67. // of a specific type of object has type |STACK_OF(type)|. This can be defined
  68. // (once) with |DEFINE_STACK_OF(type)| and declared where needed with
  69. // |DECLARE_STACK_OF(type)|. For example:
  70. //
  71. // typedef struct foo_st {
  72. // int bar;
  73. // } FOO;
  74. //
  75. // DEFINE_STACK_OF(FOO);
  76. //
  77. // Although note that the stack will contain /pointers/ to |FOO|.
  78. //
  79. // A macro will be defined for each of the sk_* functions below. For
  80. // STACK_OF(FOO), the macros would be sk_FOO_new, sk_FOO_pop etc.
  81. // stack_cmp_func is a comparison function that returns a value < 0, 0 or > 0
  82. // if |*a| is less than, equal to or greater than |*b|, respectively. Note the
  83. // extra indirection - the function is given a pointer to a pointer to the
  84. // element. This differs from the usual qsort/bsearch comparison function.
  85. typedef int (*stack_cmp_func)(const void **a, const void **b);
  86. // stack_st contains an array of pointers. It is not designed to be used
  87. // directly, rather the wrapper macros should be used.
  88. typedef struct stack_st {
  89. // num contains the number of valid pointers in |data|.
  90. size_t num;
  91. void **data;
  92. // sorted is non-zero if the values pointed to by |data| are in ascending
  93. // order, based on |comp|.
  94. int sorted;
  95. // num_alloc contains the number of pointers allocated in the buffer pointed
  96. // to by |data|, which may be larger than |num|.
  97. size_t num_alloc;
  98. // comp is an optional comparison function.
  99. stack_cmp_func comp;
  100. } _STACK;
  101. #define STACK_OF(type) struct stack_st_##type
  102. #define DECLARE_STACK_OF(type) STACK_OF(type);
  103. // These are the raw stack functions, you shouldn't be using them. Rather you
  104. // should be using the type stack macros implemented above.
  105. // sk_new creates a new, empty stack with the given comparison function, which
  106. // may be zero. It returns the new stack or NULL on allocation failure.
  107. OPENSSL_EXPORT _STACK *sk_new(stack_cmp_func comp);
  108. // sk_new_null creates a new, empty stack. It returns the new stack or NULL on
  109. // allocation failure.
  110. OPENSSL_EXPORT _STACK *sk_new_null(void);
  111. // sk_num returns the number of elements in |s|.
  112. OPENSSL_EXPORT size_t sk_num(const _STACK *sk);
  113. // sk_zero resets |sk| to the empty state but does nothing to free the
  114. // individual elements themselves.
  115. OPENSSL_EXPORT void sk_zero(_STACK *sk);
  116. // sk_value returns the |i|th pointer in |sk|, or NULL if |i| is out of
  117. // range.
  118. OPENSSL_EXPORT void *sk_value(const _STACK *sk, size_t i);
  119. // sk_set sets the |i|th pointer in |sk| to |p| and returns |p|. If |i| is out
  120. // of range, it returns NULL.
  121. OPENSSL_EXPORT void *sk_set(_STACK *sk, size_t i, void *p);
  122. // sk_free frees the given stack and array of pointers, but does nothing to
  123. // free the individual elements. Also see |sk_pop_free|.
  124. OPENSSL_EXPORT void sk_free(_STACK *sk);
  125. // sk_pop_free calls |free_func| on each element in the stack and then frees
  126. // the stack itself.
  127. OPENSSL_EXPORT void sk_pop_free(_STACK *sk, void (*free_func)(void *));
  128. // sk_insert inserts |p| into the stack at index |where|, moving existing
  129. // elements if needed. It returns the length of the new stack, or zero on
  130. // error.
  131. OPENSSL_EXPORT size_t sk_insert(_STACK *sk, void *p, size_t where);
  132. // sk_delete removes the pointer at index |where|, moving other elements down
  133. // if needed. It returns the removed pointer, or NULL if |where| is out of
  134. // range.
  135. OPENSSL_EXPORT void *sk_delete(_STACK *sk, size_t where);
  136. // sk_delete_ptr removes, at most, one instance of |p| from the stack based on
  137. // pointer equality. If an instance of |p| is found then |p| is returned,
  138. // otherwise it returns NULL.
  139. OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, void *p);
  140. // sk_find returns the first value in the stack equal to |p|. If a comparison
  141. // function has been set on the stack, then equality is defined by it and the
  142. // stack will be sorted if need be so that a binary search can be used.
  143. // Otherwise pointer equality is used. If a matching element is found, its
  144. // index is written to |*out_index| (if |out_index| is not NULL) and one is
  145. // returned. Otherwise zero is returned.
  146. OPENSSL_EXPORT int sk_find(_STACK *sk, size_t *out_index, void *p);
  147. // sk_shift removes and returns the first element in the stack, or returns NULL
  148. // if the stack is empty.
  149. OPENSSL_EXPORT void *sk_shift(_STACK *sk);
  150. // sk_push appends |p| to the stack and returns the length of the new stack, or
  151. // 0 on allocation failure.
  152. OPENSSL_EXPORT size_t sk_push(_STACK *sk, void *p);
  153. // sk_pop returns and removes the last element on the stack, or NULL if the
  154. // stack is empty.
  155. OPENSSL_EXPORT void *sk_pop(_STACK *sk);
  156. // sk_dup performs a shallow copy of a stack and returns the new stack, or NULL
  157. // on error.
  158. OPENSSL_EXPORT _STACK *sk_dup(const _STACK *sk);
  159. // sk_sort sorts the elements of |sk| into ascending order based on the
  160. // comparison function. The stack maintains a |sorted| flag and sorting an
  161. // already sorted stack is a no-op.
  162. OPENSSL_EXPORT void sk_sort(_STACK *sk);
  163. // sk_is_sorted returns one if |sk| is known to be sorted and zero
  164. // otherwise.
  165. OPENSSL_EXPORT int sk_is_sorted(const _STACK *sk);
  166. // sk_set_cmp_func sets the comparison function to be used by |sk| and returns
  167. // the previous one.
  168. OPENSSL_EXPORT stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp);
  169. // sk_deep_copy performs a copy of |sk| and of each of the non-NULL elements in
  170. // |sk| by using |copy_func|. If an error occurs, |free_func| is used to free
  171. // any copies already made and NULL is returned.
  172. OPENSSL_EXPORT _STACK *sk_deep_copy(const _STACK *sk,
  173. void *(*copy_func)(void *),
  174. void (*free_func)(void *));
  175. // Defining stack types.
  176. //
  177. // This set of macros is used to emit the typed functions that act on a
  178. // |STACK_OF(T)|.
  179. #if !defined(BORINGSSL_NO_CXX)
  180. extern "C++" {
  181. namespace bssl {
  182. namespace internal {
  183. template <typename T>
  184. struct StackTraits {};
  185. }
  186. }
  187. }
  188. #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const) \
  189. extern "C++" { \
  190. namespace bssl { \
  191. namespace internal { \
  192. template <> \
  193. struct StackTraits<STACK_OF(name)> { \
  194. static constexpr bool kIsStack = true; \
  195. using Type = type; \
  196. static constexpr bool kIsConst = is_const; \
  197. }; \
  198. } \
  199. } \
  200. }
  201. #else
  202. #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const)
  203. #endif
  204. // Stack functions must be tagged unused to support file-local stack types.
  205. // Clang's -Wunused-function only allows unused static inline functions if they
  206. // are defined in a header.
  207. #define BORINGSSL_DEFINE_STACK_OF_IMPL(name, ptrtype, constptrtype) \
  208. DECLARE_STACK_OF(name); \
  209. \
  210. typedef int (*stack_##name##_cmp_func)(constptrtype *a, constptrtype *b); \
  211. \
  212. static inline OPENSSL_UNUSED STACK_OF(name) * \
  213. sk_##name##_new(stack_##name##_cmp_func comp) { \
  214. return (STACK_OF(name) *)sk_new((stack_cmp_func)comp); \
  215. } \
  216. \
  217. static inline OPENSSL_UNUSED STACK_OF(name) *sk_##name##_new_null(void) { \
  218. return (STACK_OF(name) *)sk_new_null(); \
  219. } \
  220. \
  221. static inline OPENSSL_UNUSED size_t sk_##name##_num( \
  222. const STACK_OF(name) *sk) { \
  223. return sk_num((const _STACK *)sk); \
  224. } \
  225. \
  226. static inline OPENSSL_UNUSED void sk_##name##_zero(STACK_OF(name) *sk) { \
  227. sk_zero((_STACK *)sk); \
  228. } \
  229. \
  230. static inline OPENSSL_UNUSED ptrtype sk_##name##_value( \
  231. const STACK_OF(name) *sk, size_t i) { \
  232. return (ptrtype)sk_value((const _STACK *)sk, i); \
  233. } \
  234. \
  235. static inline OPENSSL_UNUSED ptrtype sk_##name##_set(STACK_OF(name) *sk, \
  236. size_t i, ptrtype p) { \
  237. return (ptrtype)sk_set((_STACK *)sk, i, (void *)p); \
  238. } \
  239. \
  240. static inline OPENSSL_UNUSED void sk_##name##_free(STACK_OF(name) *sk) { \
  241. sk_free((_STACK *)sk); \
  242. } \
  243. \
  244. static inline OPENSSL_UNUSED void sk_##name##_pop_free( \
  245. STACK_OF(name) *sk, void (*free_func)(ptrtype p)) { \
  246. sk_pop_free((_STACK *)sk, (void (*)(void *))free_func); \
  247. } \
  248. \
  249. static inline OPENSSL_UNUSED size_t sk_##name##_insert( \
  250. STACK_OF(name) *sk, ptrtype p, size_t where) { \
  251. return sk_insert((_STACK *)sk, (void *)p, where); \
  252. } \
  253. \
  254. static inline OPENSSL_UNUSED ptrtype sk_##name##_delete(STACK_OF(name) *sk, \
  255. size_t where) { \
  256. return (ptrtype)sk_delete((_STACK *)sk, where); \
  257. } \
  258. \
  259. static inline OPENSSL_UNUSED ptrtype sk_##name##_delete_ptr( \
  260. STACK_OF(name) *sk, ptrtype p) { \
  261. return (ptrtype)sk_delete_ptr((_STACK *)sk, (void *)p); \
  262. } \
  263. \
  264. static inline OPENSSL_UNUSED int sk_##name##_find( \
  265. STACK_OF(name) *sk, size_t *out_index, ptrtype p) { \
  266. return sk_find((_STACK *)sk, out_index, (void *)p); \
  267. } \
  268. \
  269. static inline OPENSSL_UNUSED ptrtype sk_##name##_shift(STACK_OF(name) *sk) { \
  270. return (ptrtype)sk_shift((_STACK *)sk); \
  271. } \
  272. \
  273. static inline OPENSSL_UNUSED size_t sk_##name##_push(STACK_OF(name) *sk, \
  274. ptrtype p) { \
  275. return sk_push((_STACK *)sk, (void *)p); \
  276. } \
  277. \
  278. static inline OPENSSL_UNUSED ptrtype sk_##name##_pop(STACK_OF(name) *sk) { \
  279. return (ptrtype)sk_pop((_STACK *)sk); \
  280. } \
  281. \
  282. static inline OPENSSL_UNUSED STACK_OF(name) * \
  283. sk_##name##_dup(const STACK_OF(name) *sk) { \
  284. return (STACK_OF(name) *)sk_dup((const _STACK *)sk); \
  285. } \
  286. \
  287. static inline OPENSSL_UNUSED void sk_##name##_sort(STACK_OF(name) *sk) { \
  288. sk_sort((_STACK *)sk); \
  289. } \
  290. \
  291. static inline OPENSSL_UNUSED int sk_##name##_is_sorted( \
  292. const STACK_OF(name) *sk) { \
  293. return sk_is_sorted((const _STACK *)sk); \
  294. } \
  295. \
  296. static inline OPENSSL_UNUSED stack_##name##_cmp_func \
  297. sk_##name##_set_cmp_func(STACK_OF(name) *sk, \
  298. stack_##name##_cmp_func comp) { \
  299. return (stack_##name##_cmp_func)sk_set_cmp_func((_STACK *)sk, \
  300. (stack_cmp_func)comp); \
  301. } \
  302. \
  303. static inline OPENSSL_UNUSED STACK_OF(name) * \
  304. sk_##name##_deep_copy(const STACK_OF(name) *sk, \
  305. ptrtype(*copy_func)(ptrtype), \
  306. void (*free_func)(ptrtype)) { \
  307. return (STACK_OF(name) *)sk_deep_copy((_STACK *)sk, \
  308. (void *(*)(void *))copy_func, \
  309. (void (*)(void *))free_func); \
  310. }
  311. // DEFINE_STACK_OF defines |STACK_OF(type)| to be a stack whose elements are
  312. // |type| *.
  313. #define DEFINE_STACK_OF(type) \
  314. BORINGSSL_DEFINE_STACK_OF_IMPL(type, type *, const type *) \
  315. BORINGSSL_DEFINE_STACK_TRAITS(type, type, false)
  316. // DEFINE_CONST_STACK_OF defines |STACK_OF(type)| to be a stack whose elements
  317. // are const |type| *.
  318. #define DEFINE_CONST_STACK_OF(type) \
  319. BORINGSSL_DEFINE_STACK_OF_IMPL(type, const type *, const type *) \
  320. BORINGSSL_DEFINE_STACK_TRAITS(type, const type, true)
  321. // DEFINE_SPECIAL_STACK_OF defines |STACK_OF(type)| to be a stack whose elements
  322. // are |type|, where |type| must be a typedef for a pointer.
  323. #define DEFINE_SPECIAL_STACK_OF(type) \
  324. OPENSSL_COMPILE_ASSERT(sizeof(type) == sizeof(void *), \
  325. special_stack_of_non_pointer_##type); \
  326. BORINGSSL_DEFINE_STACK_OF_IMPL(type, type, const type)
  327. typedef char *OPENSSL_STRING;
  328. DEFINE_STACK_OF(void)
  329. DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING)
  330. #if defined(__cplusplus)
  331. } // extern C
  332. #endif
  333. #if !defined(BORINGSSL_NO_CXX)
  334. extern "C++" {
  335. #include <type_traits>
  336. namespace bssl {
  337. namespace internal {
  338. // Stacks defined with |DEFINE_CONST_STACK_OF| are freed with |sk_free|.
  339. template <typename Stack>
  340. struct DeleterImpl<
  341. Stack, typename std::enable_if<StackTraits<Stack>::kIsConst>::type> {
  342. static void Free(Stack *sk) { sk_free(reinterpret_cast<_STACK *>(sk)); }
  343. };
  344. // Stacks defined with |DEFINE_STACK_OF| are freed with |sk_pop_free| and the
  345. // corresponding type's deleter.
  346. template <typename Stack>
  347. struct DeleterImpl<
  348. Stack, typename std::enable_if<!StackTraits<Stack>::kIsConst>::type> {
  349. static void Free(Stack *sk) {
  350. sk_pop_free(
  351. reinterpret_cast<_STACK *>(sk),
  352. reinterpret_cast<void (*)(void *)>(
  353. DeleterImpl<typename StackTraits<Stack>::Type>::Free));
  354. }
  355. };
  356. template <typename Stack>
  357. class StackIteratorImpl {
  358. public:
  359. using Type = typename StackTraits<Stack>::Type;
  360. // Iterators must be default-constructable.
  361. StackIteratorImpl() : sk_(nullptr), idx_(0) {}
  362. StackIteratorImpl(const Stack *sk, size_t idx) : sk_(sk), idx_(idx) {}
  363. bool operator==(StackIteratorImpl other) const {
  364. return sk_ == other.sk_ && idx_ == other.idx_;
  365. }
  366. bool operator!=(StackIteratorImpl other) const {
  367. return !(*this == other);
  368. }
  369. Type *operator*() const {
  370. return reinterpret_cast<Type *>(
  371. sk_value(reinterpret_cast<const _STACK *>(sk_), idx_));
  372. }
  373. StackIteratorImpl &operator++(/* prefix */) {
  374. idx_++;
  375. return *this;
  376. }
  377. StackIteratorImpl operator++(int /* postfix */) {
  378. StackIteratorImpl copy(*this);
  379. ++(*this);
  380. return copy;
  381. }
  382. private:
  383. const Stack *sk_;
  384. size_t idx_;
  385. };
  386. template <typename Stack>
  387. using StackIterator = typename std::enable_if<StackTraits<Stack>::kIsStack,
  388. StackIteratorImpl<Stack>>::type;
  389. } // namespace internal
  390. // PushToStack pushes |elem| to |sk|. It returns true on success and false on
  391. // allocation failure.
  392. template <typename Stack>
  393. static inline
  394. typename std::enable_if<!internal::StackTraits<Stack>::kIsConst, bool>::type
  395. PushToStack(Stack *sk,
  396. UniquePtr<typename internal::StackTraits<Stack>::Type> elem) {
  397. if (!sk_push(reinterpret_cast<_STACK *>(sk), elem.get())) {
  398. return false;
  399. }
  400. // sk_push takes ownership on success.
  401. elem.release();
  402. return true;
  403. }
  404. } // namespace bssl
  405. // Define begin() and end() for stack types so C++ range for loops work.
  406. template <typename Stack>
  407. static inline bssl::internal::StackIterator<Stack> begin(const Stack *sk) {
  408. return bssl::internal::StackIterator<Stack>(sk, 0);
  409. }
  410. template <typename Stack>
  411. static inline bssl::internal::StackIterator<Stack> end(const Stack *sk) {
  412. return bssl::internal::StackIterator<Stack>(
  413. sk, sk_num(reinterpret_cast<const _STACK *>(sk)));
  414. }
  415. } // extern C++
  416. #endif
  417. #endif // OPENSSL_HEADER_STACK_H