X509_LOOKUP_store: new X509_LOOKUP_METHOD that works by OSSL_STORE URI
[openssl.git] / include / openssl / safestack.h
1 /*
2  * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OPENSSL_SAFESTACK_H
11 # define OPENSSL_SAFESTACK_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # if !OPENSSL_API_3
16 #  define HEADER_SAFESTACK_H
17 # endif
18
19 # include <openssl/stack.h>
20 # include <openssl/e_os2.h>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 # define STACK_OF(type) struct stack_st_##type
27
28 # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
29     STACK_OF(t1); \
30     typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
31     typedef void (*sk_##t1##_freefunc)(t3 *a); \
32     typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
33     static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
34     { \
35         return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
36     } \
37     static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
38     { \
39         return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \
40     } \
41     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
42     { \
43         return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
44     } \
45     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
46     { \
47         return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
48     } \
49     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
50     { \
51         return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
52     } \
53     static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
54     { \
55         return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \
56     } \
57     static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
58     { \
59         OPENSSL_sk_free((OPENSSL_STACK *)sk); \
60     } \
61     static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
62     { \
63         OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
64     } \
65     static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
66     { \
67         return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
68     } \
69     static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
70     { \
71         return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
72                                            (const void *)ptr); \
73     } \
74     static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
75     { \
76         return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
77     } \
78     static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
79     { \
80         return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
81     } \
82     static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
83     { \
84         return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
85     } \
86     static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
87     { \
88         return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
89     } \
90     static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
91     { \
92         OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
93     } \
94     static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
95     { \
96         return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
97     } \
98     static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
99     { \
100         return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
101     } \
102     static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
103     { \
104         return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
105     } \
106     static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
107     { \
108         return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
109     } \
110     static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
111     { \
112         OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
113     } \
114     static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
115     { \
116         return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
117     } \
118     static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
119     { \
120         return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
121     } \
122     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
123                                                     sk_##t1##_copyfunc copyfunc, \
124                                                     sk_##t1##_freefunc freefunc) \
125     { \
126         return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
127                                             (OPENSSL_sk_copyfunc)copyfunc, \
128                                             (OPENSSL_sk_freefunc)freefunc); \
129     } \
130     static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
131     { \
132         return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
133     }
134
135 # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
136 # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
137 # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
138             SKM_DEFINE_STACK_OF(t1, const t2, t2)
139 # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
140
141 /*-
142  * Strings are special: normally an lhash entry will point to a single
143  * (somewhat) mutable object. In the case of strings:
144  *
145  * a) Instead of a single char, there is an array of chars, NUL-terminated.
146  * b) The string may have be immutable.
147  *
148  * So, they need their own declarations. Especially important for
149  * type-checking tools, such as Deputy.
150  *
151  * In practice, however, it appears to be hard to have a const
152  * string. For now, I'm settling for dealing with the fact it is a
153  * string at all.
154  */
155 typedef char *OPENSSL_STRING;
156 typedef const char *OPENSSL_CSTRING;
157
158 /*-
159  * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
160  * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
161  * above, instead of a single char each entry is a NUL-terminated array of
162  * chars. So, we have to implement STRING specially for STACK_OF. This is
163  * dealt with in the autogenerated macros below.
164  */
165 DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
166 DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char)
167
168 /*
169  * Similarly, we sometimes use a block of characters, NOT nul-terminated.
170  * These should also be distinguished from "normal" stacks.
171  */
172 typedef void *OPENSSL_BLOCK;
173 DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
174
175 /*
176  * If called without higher optimization (min. -xO3) the Oracle Developer
177  * Studio compiler generates code for the defined (static inline) functions
178  * above.
179  * This would later lead to the linker complaining about missing symbols when
180  * this header file is included but the resulting object is not linked against
181  * the Crypto library (openssl#6912).
182  */
183 # ifdef __SUNPRO_C
184 #  pragma weak OPENSSL_sk_num
185 #  pragma weak OPENSSL_sk_value
186 #  pragma weak OPENSSL_sk_new
187 #  pragma weak OPENSSL_sk_new_null
188 #  pragma weak OPENSSL_sk_new_reserve
189 #  pragma weak OPENSSL_sk_reserve
190 #  pragma weak OPENSSL_sk_free
191 #  pragma weak OPENSSL_sk_zero
192 #  pragma weak OPENSSL_sk_delete
193 #  pragma weak OPENSSL_sk_delete_ptr
194 #  pragma weak OPENSSL_sk_push
195 #  pragma weak OPENSSL_sk_unshift
196 #  pragma weak OPENSSL_sk_pop
197 #  pragma weak OPENSSL_sk_shift
198 #  pragma weak OPENSSL_sk_pop_free
199 #  pragma weak OPENSSL_sk_insert
200 #  pragma weak OPENSSL_sk_set
201 #  pragma weak OPENSSL_sk_find
202 #  pragma weak OPENSSL_sk_find_ex
203 #  pragma weak OPENSSL_sk_sort
204 #  pragma weak OPENSSL_sk_is_sorted
205 #  pragma weak OPENSSL_sk_dup
206 #  pragma weak OPENSSL_sk_deep_copy
207 #  pragma weak OPENSSL_sk_set_cmp_func
208 # endif /* __SUNPRO_C */
209
210 # ifdef  __cplusplus
211 }
212 # endif
213 #endif