Add support for openssl_ctx_run_once and openssl_ctx_onfree
[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 void crypto_cleanup_all_ex_data_int(void);
92 int openssl_init_fork_handlers(void);
93
94 char *ossl_safe_getenv(const char *name);
95
96 extern CRYPTO_RWLOCK *memdbg_lock;
97 int openssl_strerror_r(int errnum, char *buf, size_t buflen);
98 # if !defined(OPENSSL_NO_STDIO)
99 FILE *openssl_fopen(const char *filename, const char *mode);
100 # else
101 void *openssl_fopen(const char *filename, const char *mode);
102 # endif
103
104 uint32_t OPENSSL_rdtsc(void);
105 size_t OPENSSL_instrument_bus(unsigned int *, size_t);
106 size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
107
108 # define MAX_OPENSSL_CTX_RUN_ONCE           1
109
110 typedef struct openssl_ctx_method {
111     void *(*new_func)(void);
112     void (*free_func)(void *);
113 } OPENSSL_CTX_METHOD;
114 /* For each type of data to store in the context, an index must be created */
115 int openssl_ctx_new_index(const OPENSSL_CTX_METHOD *);
116 /* Functions to retrieve pointers to data by index */
117 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */);
118
119 typedef int (*openssl_ctx_run_once_fn)(OPENSSL_CTX *ctx);
120 typedef void (*openssl_ctx_onfree_fn)(OPENSSL_CTX *ctx);
121
122 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
123                          openssl_ctx_run_once_fn run_once_fn);
124 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn);
125 #endif