]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/cryptlib.h
Add the function openssl_ctx_get_concrete()
[thirdparty/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_NAMEMAP_INDEX 4
149 # define OPENSSL_CTX_DRBG_INDEX 5
150 # define OPENSSL_CTX_DRBG_NONCE_INDEX 6
151 # define OPENSSL_CTX_RAND_CRNGT_INDEX 7
152 # define OPENSSL_CTX_MAX_INDEXES 8
153
154 typedef struct openssl_ctx_method {
155 void *(*new_func)(OPENSSL_CTX *ctx);
156 void (*free_func)(void *);
157 } OPENSSL_CTX_METHOD;
158
159 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
160
161 /* Functions to retrieve pointers to data by index */
162 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */,
163 const OPENSSL_CTX_METHOD * ctx);
164
165 void openssl_ctx_default_deinit(void);
166 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx);
167 typedef int (openssl_ctx_run_once_fn)(OPENSSL_CTX *ctx);
168 typedef void (openssl_ctx_onfree_fn)(OPENSSL_CTX *ctx);
169
170 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
171 openssl_ctx_run_once_fn run_once_fn);
172 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn);
173
174 OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad);
175 int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
176 CRYPTO_EX_DATA *ad);
177 int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index,
178 long argl, void *argp,
179 CRYPTO_EX_new *new_func,
180 CRYPTO_EX_dup *dup_func,
181 CRYPTO_EX_free *free_func);
182 int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx);
183
184 /* Function for simple binary search */
185
186 /* Flags */
187 # define OSSL_BSEARCH_VALUE_ON_NOMATCH 0x01
188 # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH 0x02
189
190 const void *ossl_bsearch(const void *key, const void *base, int num,
191 int size, int (*cmp) (const void *, const void *),
192 int flags);
193
194 #endif