2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef HEADER_CRYPTLIB_H
11 # define HEADER_CRYPTLIB_H
16 # ifdef OPENSSL_USE_APPLINK
17 # define BIO_FLAGS_UPLINK_INTERNAL 0x8000
18 # include "ms/uplink.h"
20 # define BIO_FLAGS_UPLINK_INTERNAL 0
23 # include <openssl/crypto.h>
24 # include <openssl/buffer.h>
25 # include <openssl/bio.h>
26 # include <openssl/err.h>
27 # include "internal/nelem.h"
30 # define ossl_assert(x) ((x) != 0)
32 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
33 const char *file, int line)
36 OPENSSL_die(exprstr, file, line);
41 # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
47 * Use this inside a union with the field that needs to be aligned to a
48 * reasonable boundary for the platform. The most pessimistic alignment
49 * of the listed types will be used by the compiler.
51 # define OSSL_UNION_ALIGN \
53 ossl_uintmax_t align_int; \
56 typedef struct ex_callback_st EX_CALLBACK;
58 DEFINE_STACK_OF(EX_CALLBACK)
60 typedef struct app_mem_info_st APP_INFO;
62 typedef struct mem_st MEM;
65 # define OPENSSL_CONF "openssl.cnf"
67 # ifndef OPENSSL_SYS_VMS
68 # define X509_CERT_AREA OPENSSLDIR
69 # define X509_CERT_DIR OPENSSLDIR "/certs"
70 # define X509_CERT_FILE OPENSSLDIR "/cert.pem"
71 # define X509_PRIVATE_DIR OPENSSLDIR "/private"
72 # define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"
74 # define X509_CERT_AREA "OSSL$DATAROOT:[000000]"
75 # define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"
76 # define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"
77 # define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"
78 # define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"
81 # define X509_CERT_DIR_EVP "SSL_CERT_DIR"
82 # define X509_CERT_FILE_EVP "SSL_CERT_FILE"
83 # define CTLOG_FILE_EVP "CTLOG_FILE"
85 /* size of string representations */
86 # define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
87 # define HEX_SIZE(type) (sizeof(type)*2)
89 void OPENSSL_cpuid_setup(void);
90 extern unsigned int OPENSSL_ia32cap_P[];
91 void OPENSSL_showfatal(const char *fmta, ...);
92 int do_ex_data_init(OPENSSL_CTX *ctx);
93 void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx);
94 int openssl_init_fork_handlers(void);
96 char *ossl_safe_getenv(const char *name);
98 extern CRYPTO_RWLOCK *memdbg_lock;
99 int openssl_strerror_r(int errnum, char *buf, size_t buflen);
100 # if !defined(OPENSSL_NO_STDIO)
101 FILE *openssl_fopen(const char *filename, const char *mode);
103 void *openssl_fopen(const char *filename, const char *mode);
106 uint32_t OPENSSL_rdtsc(void);
107 size_t OPENSSL_instrument_bus(unsigned int *, size_t);
108 size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
110 /* ex_data structures */
113 * Each structure type (sometimes called a class), that supports
114 * exdata has a stack of callbacks for each instance.
116 struct ex_callback_st {
117 long argl; /* Arbitrary long */
118 void *argp; /* Arbitrary void * */
119 CRYPTO_EX_new *new_func;
120 CRYPTO_EX_free *free_func;
121 CRYPTO_EX_dup *dup_func;
125 * The state for each class. This could just be a typedef, but
126 * a structure allows future changes.
128 typedef struct ex_callbacks_st {
129 STACK_OF(EX_CALLBACK) *meth;
132 typedef struct ossl_ex_data_global_st {
133 CRYPTO_RWLOCK *ex_data_lock;
134 EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
135 } OSSL_EX_DATA_GLOBAL;
140 # define OPENSSL_CTX_PROVIDER_STORE_RUN_ONCE_INDEX 0
141 # define OPENSSL_CTX_DEFAULT_METHOD_STORE_RUN_ONCE_INDEX 1
142 # define OPENSSL_CTX_METHOD_STORE_RUN_ONCE_INDEX 2
143 # define OPENSSL_CTX_MAX_RUN_ONCE 3
145 # define OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX 0
146 # define OPENSSL_CTX_PROVIDER_STORE_INDEX 1
147 # define OPENSSL_CTX_PROPERTY_DEFN_INDEX 2
148 # define OPENSSL_CTX_PROPERTY_STRING_INDEX 3
149 # define OPENSSL_CTX_NAMEMAP_INDEX 4
150 # define OPENSSL_CTX_DRBG_INDEX 5
151 # define OPENSSL_CTX_DRBG_NONCE_INDEX 6
152 # define OPENSSL_CTX_RAND_CRNGT_INDEX 7
153 # define OPENSSL_CTX_THREAD_EVENT_HANDLER_INDEX 8
154 # define OPENSSL_CTX_FIPS_PROV_INDEX 9
155 # define OPENSSL_CTX_MAX_INDEXES 10
157 typedef struct openssl_ctx_method {
158 void *(*new_func)(OPENSSL_CTX *ctx);
159 void (*free_func)(void *);
160 } OPENSSL_CTX_METHOD;
162 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
164 /* Functions to retrieve pointers to data by index */
165 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */,
166 const OPENSSL_CTX_METHOD * ctx);
168 void openssl_ctx_default_deinit(void);
169 OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx);
170 typedef int (openssl_ctx_run_once_fn)(OPENSSL_CTX *ctx);
171 typedef void (openssl_ctx_onfree_fn)(OPENSSL_CTX *ctx);
173 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
174 openssl_ctx_run_once_fn run_once_fn);
175 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn);
177 OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad);
178 int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
180 int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index,
181 long argl, void *argp,
182 CRYPTO_EX_new *new_func,
183 CRYPTO_EX_dup *dup_func,
184 CRYPTO_EX_free *free_func);
185 int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx);
187 /* Function for simple binary search */
190 # define OSSL_BSEARCH_VALUE_ON_NOMATCH 0x01
191 # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH 0x02
193 const void *ossl_bsearch(const void *key, const void *base, int num,
194 int size, int (*cmp) (const void *, const void *),