Generate error queue entry on FFC_CHECK_BAD_LN_PAIR for DH and DSA
[openssl.git] / include / openssl / safestack.h
1 /*
2  * Copyright 1999-2020 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 # ifndef OPENSSL_NO_DEPRECATED_3_0
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_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
136 # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
137 # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
138 # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
139             SKM_DEFINE_STACK_OF(t1, const t2, t2)
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 # define DEFINE_STACK_OF_STRING() \
159         DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
160 # define DEFINE_STACK_OF_CSTRING() \
161         DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char)
162
163 /*
164  * If we're building OpenSSL, or we have no-deprecated configured,
165  * then we don't define the inline functions (see |SKM_DEFINE_STACK_OF|,
166  * above), we just declare the stack datatypes. Otherwise, for compatibility
167  * and to not remove the API's, we define the functions.  We have the
168  * trailing semicolon so that uses of this never need it.
169  */
170 #if defined(OPENSSL_BUILDING_OPENSSL) || defined(OPENSSL_NO_DEPRECATED_3_0)
171 # define DEFINE_OR_DECLARE_STACK_OF(s) STACK_OF(s);
172 # define DEFINE_OR_DECLARE_STACK_OF_STRING() STACK_OF(OPENSSL_STRING);
173 # define DEFINE_OR_DECLARE_STACK_OF_CSTRING() STACK_OF(OPENSSL_CSTRING);
174 #else
175 # define DEFINE_OR_DECLARE_STACK_OF(s) DEFINE_STACK_OF(s)
176 # define DEFINE_OR_DECLARE_STACK_OF_STRING() DEFINE_STACK_OF_STRING()
177 # define DEFINE_OR_DECLARE_STACK_OF_CSTRING() DEFINE_STACK_OF_CSTRING()
178 #endif
179
180 /*-
181  * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
182  * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
183  * above, instead of a single char each entry is a NUL-terminated array of
184  * chars. So, we have to implement STRING specially for STACK_OF. This is
185  * dealt with in the autogenerated macros below.
186  */
187 DEFINE_OR_DECLARE_STACK_OF_STRING()
188 DEFINE_OR_DECLARE_STACK_OF_CSTRING()
189
190 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
191 /*
192  * This is not used by OpenSSL.  A block of bytes,  NOT nul-terminated.
193  * These should also be distinguished from "normal" stacks.
194  */
195 typedef void *OPENSSL_BLOCK;
196 DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
197 #endif
198
199 /*
200  * If called without higher optimization (min. -xO3) the Oracle Developer
201  * Studio compiler generates code for the defined (static inline) functions
202  * above.
203  * This would later lead to the linker complaining about missing symbols when
204  * this header file is included but the resulting object is not linked against
205  * the Crypto library (openssl#6912).
206  */
207 # ifdef __SUNPRO_C
208 #  pragma weak OPENSSL_sk_num
209 #  pragma weak OPENSSL_sk_value
210 #  pragma weak OPENSSL_sk_new
211 #  pragma weak OPENSSL_sk_new_null
212 #  pragma weak OPENSSL_sk_new_reserve
213 #  pragma weak OPENSSL_sk_reserve
214 #  pragma weak OPENSSL_sk_free
215 #  pragma weak OPENSSL_sk_zero
216 #  pragma weak OPENSSL_sk_delete
217 #  pragma weak OPENSSL_sk_delete_ptr
218 #  pragma weak OPENSSL_sk_push
219 #  pragma weak OPENSSL_sk_unshift
220 #  pragma weak OPENSSL_sk_pop
221 #  pragma weak OPENSSL_sk_shift
222 #  pragma weak OPENSSL_sk_pop_free
223 #  pragma weak OPENSSL_sk_insert
224 #  pragma weak OPENSSL_sk_set
225 #  pragma weak OPENSSL_sk_find
226 #  pragma weak OPENSSL_sk_find_ex
227 #  pragma weak OPENSSL_sk_sort
228 #  pragma weak OPENSSL_sk_is_sorted
229 #  pragma weak OPENSSL_sk_dup
230 #  pragma weak OPENSSL_sk_deep_copy
231 #  pragma weak OPENSSL_sk_set_cmp_func
232 # endif /* __SUNPRO_C */
233
234 # ifdef  __cplusplus
235 }
236 # endif
237 #endif