Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | #ifndef OPENSSL_HEADER_STACK_H |
| 58 | #define OPENSSL_HEADER_STACK_H |
| 59 | |
| 60 | #include <openssl/base.h> |
| 61 | |
| 62 | #include <openssl/type_check.h> |
| 63 | |
| 64 | #if defined(__cplusplus) |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /* A stack, in OpenSSL, is an array of pointers. They are the most commonly |
| 70 | * used collection object. |
| 71 | * |
| 72 | * This file defines macros for type safe use of the stack functions. A stack |
| 73 | * of a specific type of object has type |STACK_OF(type)|. This can be defined |
| 74 | * (once) with |DEFINE_STACK_OF(type)| and declared where needed with |
| 75 | * |DECLARE_STACK_OF(type)|. For example: |
| 76 | * |
| 77 | * struct foo { |
| 78 | * int bar; |
| 79 | * }; |
| 80 | * |
| 81 | * DEFINE_STACK_OF(struct foo); |
| 82 | * |
| 83 | * Although note that the stack will contain /pointers/ to |foo|. |
| 84 | * |
| 85 | * A macro will be defined for each of the sk_* functions below. For |
| 86 | * STACK_OF(foo), the macros would be sk_foo_new, sk_foo_pop etc. */ |
| 87 | |
| 88 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 89 | /* stack_cmp_func is a comparison function that returns a value < 0, 0 or > 0 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | * if |*a| is less than, equal to or greater than |*b|, respectively. Note the |
| 91 | * extra indirection - the function is given a pointer to a pointer to the |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 92 | * element. This differs from the usual qsort/bsearch comparison function. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 93 | typedef int (*stack_cmp_func)(const void **a, const void **b); |
| 94 | |
| 95 | /* stack_st contains an array of pointers. It is not designed to be used |
| 96 | * directly, rather the wrapper macros should be used. */ |
| 97 | typedef struct stack_st { |
| 98 | /* num contains the number of valid pointers in |data|. */ |
| 99 | size_t num; |
| 100 | void **data; |
| 101 | /* sorted is non-zero if the values pointed to by |data| are in ascending |
| 102 | * order, based on |comp|. */ |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 103 | int sorted; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | /* num_alloc contains the number of pointers allocated in the buffer pointed |
| 105 | * to by |data|, which may be larger than |num|. */ |
| 106 | size_t num_alloc; |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 107 | /* comp is an optional comparison function. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 108 | stack_cmp_func comp; |
| 109 | } _STACK; |
| 110 | |
| 111 | |
| 112 | #define STACK_OF(type) struct stack_st_##type |
| 113 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | #define DECLARE_STACK_OF(type) STACK_OF(type); |
| 115 | |
| 116 | /* The make_macros.sh script in this directory parses the following lines and |
| 117 | * generates the stack_macros.h file that contains macros for the following |
| 118 | * types of stacks: |
| 119 | * |
| 120 | * STACK_OF:ACCESS_DESCRIPTION |
| 121 | * STACK_OF:ASN1_ADB_TABLE |
| 122 | * STACK_OF:ASN1_GENERALSTRING |
| 123 | * STACK_OF:ASN1_INTEGER |
| 124 | * STACK_OF:ASN1_OBJECT |
| 125 | * STACK_OF:ASN1_STRING_TABLE |
| 126 | * STACK_OF:ASN1_TYPE |
| 127 | * STACK_OF:ASN1_VALUE |
| 128 | * STACK_OF:BIO |
| 129 | * STACK_OF:BY_DIR_ENTRY |
| 130 | * STACK_OF:BY_DIR_HASH |
| 131 | * STACK_OF:CONF_VALUE |
Adam Langley | 864c887 | 2016-11-07 14:25:20 -0800 | [diff] [blame] | 132 | * STACK_OF:CRYPTO_BUFFER |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | * STACK_OF:CRYPTO_EX_DATA_FUNCS |
| 134 | * STACK_OF:DIST_POINT |
| 135 | * STACK_OF:GENERAL_NAME |
| 136 | * STACK_OF:GENERAL_NAMES |
| 137 | * STACK_OF:GENERAL_SUBTREE |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | * STACK_OF:POLICYINFO |
| 139 | * STACK_OF:POLICYQUALINFO |
| 140 | * STACK_OF:POLICY_MAPPING |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | * STACK_OF:SSL_COMP |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 142 | * STACK_OF:SSL_CUSTOM_EXTENSION |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | * STACK_OF:STACK_OF_X509_NAME_ENTRY |
| 144 | * STACK_OF:SXNETID |
| 145 | * STACK_OF:X509 |
| 146 | * STACK_OF:X509V3_EXT_METHOD |
| 147 | * STACK_OF:X509_ALGOR |
| 148 | * STACK_OF:X509_ATTRIBUTE |
| 149 | * STACK_OF:X509_CRL |
| 150 | * STACK_OF:X509_EXTENSION |
| 151 | * STACK_OF:X509_INFO |
| 152 | * STACK_OF:X509_LOOKUP |
| 153 | * STACK_OF:X509_NAME |
| 154 | * STACK_OF:X509_NAME_ENTRY |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | * STACK_OF:X509_OBJECT |
| 156 | * STACK_OF:X509_POLICY_DATA |
| 157 | * STACK_OF:X509_POLICY_NODE |
| 158 | * STACK_OF:X509_PURPOSE |
| 159 | * STACK_OF:X509_REVOKED |
| 160 | * STACK_OF:X509_TRUST |
| 161 | * STACK_OF:X509_VERIFY_PARAM |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 162 | * STACK_OF:void |
| 163 | * |
David Benjamin | bc44c08 | 2015-01-01 15:02:11 -0500 | [diff] [blame] | 164 | * Some stacks contain only const structures, so the stack should return const |
| 165 | * pointers to retain type-checking. |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 166 | * |
David Benjamin | bc44c08 | 2015-01-01 15:02:11 -0500 | [diff] [blame] | 167 | * CONST_STACK_OF:SRTP_PROTECTION_PROFILE |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 168 | * CONST_STACK_OF:SSL_CIPHER */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | /* Some stacks are special because, although we would like STACK_OF(char *), |
| 172 | * that would actually be a stack of pointers to char*, but we just want to |
| 173 | * point to the string directly. In this case we call them "special" and use |
| 174 | * |DEFINE_SPECIAL_STACK_OF(type)| */ |
| 175 | #define DEFINE_SPECIAL_STACK_OF(type, inner) \ |
| 176 | STACK_OF(type) { _STACK special_stack; }; \ |
| 177 | OPENSSL_COMPILE_ASSERT(sizeof(type) == sizeof(void *), \ |
| 178 | special_stack_of_non_pointer_##type); |
| 179 | |
| 180 | typedef char *OPENSSL_STRING; |
| 181 | |
| 182 | DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 183 | |
| 184 | /* The make_macros.sh script in this directory parses the following lines and |
| 185 | * generates the stack_macros.h file that contains macros for the following |
| 186 | * types of stacks: |
| 187 | * |
David Benjamin | dec9301 | 2016-08-16 01:45:33 -0400 | [diff] [blame] | 188 | * SPECIAL_STACK_OF:OPENSSL_STRING */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | |
| 190 | #define IN_STACK_H |
| 191 | #include <openssl/stack_macros.h> |
| 192 | #undef IN_STACK_H |
| 193 | |
| 194 | |
| 195 | /* These are the raw stack functions, you shouldn't be using them. Rather you |
| 196 | * should be using the type stack macros implemented above. */ |
| 197 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 198 | /* sk_new creates a new, empty stack with the given comparison function, which |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | * may be zero. It returns the new stack or NULL on allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 200 | OPENSSL_EXPORT _STACK *sk_new(stack_cmp_func comp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
| 202 | /* sk_new_null creates a new, empty stack. It returns the new stack or NULL on |
| 203 | * allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 204 | OPENSSL_EXPORT _STACK *sk_new_null(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
| 206 | /* sk_num returns the number of elements in |s|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 207 | OPENSSL_EXPORT size_t sk_num(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | |
| 209 | /* sk_zero resets |sk| to the empty state but does nothing to free the |
| 210 | * individual elements themselves. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 211 | OPENSSL_EXPORT void sk_zero(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 212 | |
| 213 | /* sk_value returns the |i|th pointer in |sk|, or NULL if |i| is out of |
| 214 | * range. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 215 | OPENSSL_EXPORT void *sk_value(const _STACK *sk, size_t i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
| 217 | /* sk_set sets the |i|th pointer in |sk| to |p| and returns |p|. If |i| is out |
| 218 | * of range, it returns NULL. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 219 | OPENSSL_EXPORT void *sk_set(_STACK *sk, size_t i, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
| 221 | /* sk_free frees the given stack and array of pointers, but does nothing to |
| 222 | * free the individual elements. Also see |sk_pop_free|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 223 | OPENSSL_EXPORT void sk_free(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | |
| 225 | /* sk_pop_free calls |free_func| on each element in the stack and then frees |
| 226 | * the stack itself. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 227 | OPENSSL_EXPORT void sk_pop_free(_STACK *sk, void (*free_func)(void *)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
| 229 | /* sk_insert inserts |p| into the stack at index |where|, moving existing |
| 230 | * elements if needed. It returns the length of the new stack, or zero on |
| 231 | * error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 232 | OPENSSL_EXPORT size_t sk_insert(_STACK *sk, void *p, size_t where); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
| 234 | /* sk_delete removes the pointer at index |where|, moving other elements down |
| 235 | * if needed. It returns the removed pointer, or NULL if |where| is out of |
| 236 | * range. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 237 | OPENSSL_EXPORT void *sk_delete(_STACK *sk, size_t where); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
| 239 | /* sk_delete_ptr removes, at most, one instance of |p| from the stack based on |
| 240 | * pointer equality. If an instance of |p| is found then |p| is returned, |
| 241 | * otherwise it returns NULL. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 242 | OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 244 | /* sk_find returns the first value in the stack equal to |p|. If a comparison |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | * function has been set on the stack, then equality is defined by it and the |
| 246 | * stack will be sorted if need be so that a binary search can be used. |
| 247 | * Otherwise pointer equality is used. If a matching element is found, its |
| 248 | * index is written to |*out_index| (if |out_index| is not NULL) and one is |
| 249 | * returned. Otherwise zero is returned. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 250 | OPENSSL_EXPORT int sk_find(_STACK *sk, size_t *out_index, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
| 252 | /* sk_shift removes and returns the first element in the stack, or returns NULL |
| 253 | * if the stack is empty. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 254 | OPENSSL_EXPORT void *sk_shift(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | |
| 256 | /* sk_push appends |p| to the stack and returns the length of the new stack, or |
| 257 | * 0 on allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 258 | OPENSSL_EXPORT size_t sk_push(_STACK *sk, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
| 260 | /* sk_pop returns and removes the last element on the stack, or NULL if the |
| 261 | * stack is empty. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 262 | OPENSSL_EXPORT void *sk_pop(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
| 264 | /* sk_dup performs a shallow copy of a stack and returns the new stack, or NULL |
| 265 | * on error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 266 | OPENSSL_EXPORT _STACK *sk_dup(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
| 268 | /* sk_sort sorts the elements of |sk| into ascending order based on the |
| 269 | * comparison function. The stack maintains a |sorted| flag and sorting an |
| 270 | * already sorted stack is a no-op. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 271 | OPENSSL_EXPORT void sk_sort(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
| 273 | /* sk_is_sorted returns one if |sk| is known to be sorted and zero |
| 274 | * otherwise. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 275 | OPENSSL_EXPORT int sk_is_sorted(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | |
| 277 | /* sk_set_cmp_func sets the comparison function to be used by |sk| and returns |
| 278 | * the previous one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 279 | OPENSSL_EXPORT stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
Adam Langley | a1048a7 | 2015-02-11 14:27:21 -0800 | [diff] [blame] | 281 | /* sk_deep_copy performs a copy of |sk| and of each of the non-NULL elements in |
| 282 | * |sk| by using |copy_func|. If an error occurs, |free_func| is used to free |
| 283 | * any copies already made and NULL is returned. */ |
| 284 | OPENSSL_EXPORT _STACK *sk_deep_copy(const _STACK *sk, |
| 285 | void *(*copy_func)(void *), |
| 286 | void (*free_func)(void *)); |
| 287 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
| 289 | #if defined(__cplusplus) |
| 290 | } /* extern C */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | #endif |
| 292 | |
| 293 | #endif /* OPENSSL_HEADER_STACK_H */ |