ossl_bsearch(): New generic internal binary search utility function
[openssl.git] / include / internal / cryptlib.h
1 /*
2  * Copyright 1995-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 HEADER_CRYPTLIB_H
11 # define HEADER_CRYPTLIB_H
12
13 # include <stdlib.h>
14 # include <string.h>
15
16 # ifdef OPENSSL_USE_APPLINK
17 #  undef BIO_FLAGS_UPLINK
18 #  define BIO_FLAGS_UPLINK 0x8000
19 #  include "ms/uplink.h"
20 # endif
21
22 # include <openssl/crypto.h>
23 # include <openssl/buffer.h>
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include "internal/nelem.h"
27
28 #ifdef NDEBUG
29 # define ossl_assert(x) ((x) != 0)
30 #else
31 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
32                                               const char *file, int line)
33 {
34     if (!expr)
35         OPENSSL_die(exprstr, file, line);
36
37     return expr;
38 }
39
40 # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
41                                          __FILE__, __LINE__)
42
43 #endif
44
45 /*
46  * Use this inside a union with the field that needs to be aligned to a
47  * reasonable boundary for the platform.  The most pessimistic alignment
48  * of the listed types will be used by the compiler.
49  */
50 # define OSSL_UNION_ALIGN       \
51     double align;               \
52     ossl_uintmax_t align_int;   \
53     void *align_ptr
54
55 typedef struct ex_callback_st EX_CALLBACK;
56
57 DEFINE_STACK_OF(EX_CALLBACK)
58
59 typedef struct app_mem_info_st APP_INFO;
60
61 typedef struct mem_st MEM;
62 DEFINE_LHASH_OF(MEM);
63
64 # define OPENSSL_CONF             "openssl.cnf"
65
66 # ifndef OPENSSL_SYS_VMS
67 #  define X509_CERT_AREA          OPENSSLDIR
68 #  define X509_CERT_DIR           OPENSSLDIR "/certs"
69 #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
70 #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
71 #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
72 # else
73 #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
74 #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
75 #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
76 #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
77 #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
78 # endif
79
80 # define X509_CERT_DIR_EVP        "SSL_CERT_DIR"
81 # define X509_CERT_FILE_EVP       "SSL_CERT_FILE"
82 # define CTLOG_FILE_EVP           "CTLOG_FILE"
83
84 /* size of string representations */
85 # define DECIMAL_SIZE(type)      ((sizeof(type)*8+2)/3+1)
86 # define HEX_SIZE(type)          (sizeof(type)*2)
87
88 void OPENSSL_cpuid_setup(void);
89 extern unsigned int OPENSSL_ia32cap_P[];
90 void OPENSSL_showfatal(const char *fmta, ...);
91 int do_ex_data_init(OPENSSL_CTX *ctx);
92 void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx);
93 int openssl_init_fork_handlers(void);
94
95 char *ossl_safe_getenv(const char *name);
96
97 extern CRYPTO_RWLOCK *memdbg_lock;
98 int openssl_strerror_r(int errnum, char *buf, size_t buflen);
99 # if !defined(OPENSSL_NO_STDIO)
100 FILE *openssl_fopen(const char *filename, const char *mode);
101 # else
102 void *openssl_fopen(const char *filename, const char *mode);
103 # endif
104
105 uint32_t OPENSSL_rdtsc(void);
106 size_t OPENSSL_instrument_bus(unsigned int *, size_t);
107 size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
108
109 /* ex_data structures */
110
111 /*
112  * Each structure type (sometimes called a class), that supports
113  * exdata has a stack of callbacks for each instance.
114  */
115 struct ex_callback_st {
116     long argl;                  /* Arbitrary long */
117     void *argp;                 /* Arbitrary void * */
118     CRYPTO_EX_new *new_func;
119     CRYPTO_EX_free *free_func;
120     CRYPTO_EX_dup *dup_func;
121 };
122
123 /*
124  * The state for each class.  This could just be a typedef, but
125  * a structure allows future changes.
126  */
127 typedef struct ex_callbacks_st {
128     STACK_OF(EX_CALLBACK) *meth;
129 } EX_CALLBACKS;
130
131 typedef struct ossl_ex_data_global_st {
132     CRYPTO_RWLOCK *ex_data_lock;
133     EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
134 } OSSL_EX_DATA_GLOBAL;
135
136
137 /* OPENSSL_CTX */
138
139 # define OPENSSL_CTX_PROVIDER_STORE_RUN_ONCE_INDEX          0
140 # define OPENSSL_CTX_DEFAULT_METHOD_STORE_RUN_ONCE_INDEX    1
141 # define OPENSSL_CTX_METHOD_STORE_RUN_ONCE_INDEX            2
142 # define OPENSSL_CTX_MAX_RUN_ONCE                           3
143
144 # define OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX     0
145 # define OPENSSL_CTX_PROVIDER_STORE_INDEX           1
146 # define OPENSSL_CTX_PROPERTY_DEFN_INDEX            2
147 # define OPENSSL_CTX_PROPERTY_STRING_INDEX          3
148 # define OPENSSL_CTX_MAX_INDEXES                    4
149
150 typedef struct openssl_ctx_method {
151     void *(*new_func)(OPENSSL_CTX *ctx);
152     void (*free_func)(void *);
153 } OPENSSL_CTX_METHOD;
154
155 /* Functions to retrieve pointers to data by index */
156 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */,
157                            const OPENSSL_CTX_METHOD * ctx);
158
159 void openssl_ctx_default_deinit(void);
160 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx);
161 typedef int (openssl_ctx_run_once_fn)(OPENSSL_CTX *ctx);
162 typedef void (openssl_ctx_onfree_fn)(OPENSSL_CTX *ctx);
163
164 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
165                          openssl_ctx_run_once_fn run_once_fn);
166 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn);
167
168 OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad);
169 int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
170                           CRYPTO_EX_DATA *ad);
171 int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index,
172                                long argl, void *argp,
173                                CRYPTO_EX_new *new_func,
174                                CRYPTO_EX_dup *dup_func,
175                                CRYPTO_EX_free *free_func);
176 int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx);
177
178 /* Function for simple binary search */
179
180 /* Flags */
181 # define OSSL_BSEARCH_VALUE_ON_NOMATCH            0x01
182 # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH        0x02
183
184 const void *ossl_bsearch(const void *key, const void *base, int num,
185                          int size, int (*cmp) (const void *, const void *),
186                          int flags);
187
188 #endif