113d40330Schristos /*
2*b0d17251Schristos * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
313d40330Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
513d40330Schristos * this file except in compliance with the License. You can obtain a copy
613d40330Schristos * in the file LICENSE in the source distribution or at
713d40330Schristos * https://www.openssl.org/source/license.html
813d40330Schristos */
913d40330Schristos
107d004720Schristos #ifndef OSSL_INTERNAL_CRYPTLIB_H
117d004720Schristos # define OSSL_INTERNAL_CRYPTLIB_H
12*b0d17251Schristos # pragma once
1313d40330Schristos
1413d40330Schristos # include <stdlib.h>
1513d40330Schristos # include <string.h>
1613d40330Schristos
1713d40330Schristos # ifdef OPENSSL_USE_APPLINK
18*b0d17251Schristos # define BIO_FLAGS_UPLINK_INTERNAL 0x8000
1913d40330Schristos # include "ms/uplink.h"
20*b0d17251Schristos # else
21*b0d17251Schristos # define BIO_FLAGS_UPLINK_INTERNAL 0
2213d40330Schristos # endif
2313d40330Schristos
2413d40330Schristos # include <openssl/crypto.h>
2513d40330Schristos # include <openssl/buffer.h>
2613d40330Schristos # include <openssl/bio.h>
27*b0d17251Schristos # include <openssl/asn1.h>
2813d40330Schristos # include <openssl/err.h>
2913d40330Schristos # include "internal/nelem.h"
3013d40330Schristos
3113d40330Schristos #ifdef NDEBUG
3213d40330Schristos # define ossl_assert(x) ((x) != 0)
3313d40330Schristos #else
ossl_assert_int(int expr,const char * exprstr,const char * file,int line)3413d40330Schristos __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
3513d40330Schristos const char *file, int line)
3613d40330Schristos {
3713d40330Schristos if (!expr)
3813d40330Schristos OPENSSL_die(exprstr, file, line);
3913d40330Schristos
4013d40330Schristos return expr;
4113d40330Schristos }
4213d40330Schristos
4313d40330Schristos # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
4413d40330Schristos __FILE__, __LINE__)
4513d40330Schristos
4613d40330Schristos #endif
4713d40330Schristos
48*b0d17251Schristos /*
49*b0d17251Schristos * Use this inside a union with the field that needs to be aligned to a
50*b0d17251Schristos * reasonable boundary for the platform. The most pessimistic alignment
51*b0d17251Schristos * of the listed types will be used by the compiler.
52*b0d17251Schristos */
53*b0d17251Schristos # define OSSL_UNION_ALIGN \
54*b0d17251Schristos double align; \
55*b0d17251Schristos ossl_uintmax_t align_int; \
56*b0d17251Schristos void *align_ptr
57*b0d17251Schristos
5813d40330Schristos typedef struct ex_callback_st EX_CALLBACK;
5913d40330Schristos DEFINE_STACK_OF(EX_CALLBACK)
6013d40330Schristos
6113d40330Schristos typedef struct mem_st MEM;
6213d40330Schristos DEFINE_LHASH_OF(MEM);
6313d40330Schristos
6413d40330Schristos # define OPENSSL_CONF "openssl.cnf"
6513d40330Schristos
6613d40330Schristos # ifndef OPENSSL_SYS_VMS
6713d40330Schristos # define X509_CERT_AREA OPENSSLDIR
6813d40330Schristos # define X509_CERT_DIR OPENSSLDIR "/certs"
6913d40330Schristos # define X509_CERT_FILE OPENSSLDIR "/cert.pem"
7013d40330Schristos # define X509_PRIVATE_DIR OPENSSLDIR "/private"
7113d40330Schristos # define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"
7213d40330Schristos # else
7313d40330Schristos # define X509_CERT_AREA "OSSL$DATAROOT:[000000]"
7413d40330Schristos # define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"
7513d40330Schristos # define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"
7613d40330Schristos # define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"
7713d40330Schristos # define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"
7813d40330Schristos # endif
7913d40330Schristos
8013d40330Schristos # define X509_CERT_DIR_EVP "SSL_CERT_DIR"
8113d40330Schristos # define X509_CERT_FILE_EVP "SSL_CERT_FILE"
8213d40330Schristos # define CTLOG_FILE_EVP "CTLOG_FILE"
8313d40330Schristos
8413d40330Schristos /* size of string representations */
8513d40330Schristos # define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
8613d40330Schristos # define HEX_SIZE(type) (sizeof(type)*2)
8713d40330Schristos
8813d40330Schristos void OPENSSL_cpuid_setup(void);
89*b0d17251Schristos #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
90*b0d17251Schristos defined(__x86_64) || defined(__x86_64__) || \
91*b0d17251Schristos defined(_M_AMD64) || defined(_M_X64)
9213d40330Schristos extern unsigned int OPENSSL_ia32cap_P[];
93*b0d17251Schristos #endif
9413d40330Schristos void OPENSSL_showfatal(const char *fmta, ...);
95*b0d17251Schristos int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx);
96*b0d17251Schristos void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx);
9713d40330Schristos int openssl_init_fork_handlers(void);
984ce06407Schristos int openssl_get_fork_id(void);
9913d40330Schristos
100f4f044c4Schristos char *ossl_safe_getenv(const char *name);
101f4f044c4Schristos
10213d40330Schristos extern CRYPTO_RWLOCK *memdbg_lock;
10313d40330Schristos int openssl_strerror_r(int errnum, char *buf, size_t buflen);
10413d40330Schristos # if !defined(OPENSSL_NO_STDIO)
10513d40330Schristos FILE *openssl_fopen(const char *filename, const char *mode);
10613d40330Schristos # else
10713d40330Schristos void *openssl_fopen(const char *filename, const char *mode);
10813d40330Schristos # endif
10913d40330Schristos
11013d40330Schristos uint32_t OPENSSL_rdtsc(void);
111b88c74d5Schristos size_t OPENSSL_instrument_bus(unsigned int *, size_t);
112b88c74d5Schristos size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
11313d40330Schristos
114*b0d17251Schristos /* ex_data structures */
115*b0d17251Schristos
116*b0d17251Schristos /*
117*b0d17251Schristos * Each structure type (sometimes called a class), that supports
118*b0d17251Schristos * exdata has a stack of callbacks for each instance.
119*b0d17251Schristos */
120*b0d17251Schristos struct ex_callback_st {
121*b0d17251Schristos long argl; /* Arbitrary long */
122*b0d17251Schristos void *argp; /* Arbitrary void * */
123*b0d17251Schristos int priority; /* Priority ordering for freeing */
124*b0d17251Schristos CRYPTO_EX_new *new_func;
125*b0d17251Schristos CRYPTO_EX_free *free_func;
126*b0d17251Schristos CRYPTO_EX_dup *dup_func;
127*b0d17251Schristos };
128*b0d17251Schristos
129*b0d17251Schristos /*
130*b0d17251Schristos * The state for each class. This could just be a typedef, but
131*b0d17251Schristos * a structure allows future changes.
132*b0d17251Schristos */
133*b0d17251Schristos typedef struct ex_callbacks_st {
134*b0d17251Schristos STACK_OF(EX_CALLBACK) *meth;
135*b0d17251Schristos } EX_CALLBACKS;
136*b0d17251Schristos
137*b0d17251Schristos typedef struct ossl_ex_data_global_st {
138*b0d17251Schristos CRYPTO_RWLOCK *ex_data_lock;
139*b0d17251Schristos EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
140*b0d17251Schristos } OSSL_EX_DATA_GLOBAL;
141*b0d17251Schristos
142*b0d17251Schristos
143*b0d17251Schristos /* OSSL_LIB_CTX */
144*b0d17251Schristos
145*b0d17251Schristos # define OSSL_LIB_CTX_PROVIDER_STORE_RUN_ONCE_INDEX 0
146*b0d17251Schristos # define OSSL_LIB_CTX_DEFAULT_METHOD_STORE_RUN_ONCE_INDEX 1
147*b0d17251Schristos # define OSSL_LIB_CTX_METHOD_STORE_RUN_ONCE_INDEX 2
148*b0d17251Schristos # define OSSL_LIB_CTX_MAX_RUN_ONCE 3
149*b0d17251Schristos
150*b0d17251Schristos # define OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX 0
151*b0d17251Schristos # define OSSL_LIB_CTX_PROVIDER_STORE_INDEX 1
152*b0d17251Schristos # define OSSL_LIB_CTX_PROPERTY_DEFN_INDEX 2
153*b0d17251Schristos # define OSSL_LIB_CTX_PROPERTY_STRING_INDEX 3
154*b0d17251Schristos # define OSSL_LIB_CTX_NAMEMAP_INDEX 4
155*b0d17251Schristos # define OSSL_LIB_CTX_DRBG_INDEX 5
156*b0d17251Schristos # define OSSL_LIB_CTX_DRBG_NONCE_INDEX 6
157*b0d17251Schristos # define OSSL_LIB_CTX_RAND_CRNGT_INDEX 7
158*b0d17251Schristos # ifdef FIPS_MODULE
159*b0d17251Schristos # define OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX 8
160*b0d17251Schristos # endif
161*b0d17251Schristos # define OSSL_LIB_CTX_FIPS_PROV_INDEX 9
162*b0d17251Schristos # define OSSL_LIB_CTX_ENCODER_STORE_INDEX 10
163*b0d17251Schristos # define OSSL_LIB_CTX_DECODER_STORE_INDEX 11
164*b0d17251Schristos # define OSSL_LIB_CTX_SELF_TEST_CB_INDEX 12
165*b0d17251Schristos # define OSSL_LIB_CTX_BIO_PROV_INDEX 13
166*b0d17251Schristos # define OSSL_LIB_CTX_GLOBAL_PROPERTIES 14
167*b0d17251Schristos # define OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX 15
168*b0d17251Schristos # define OSSL_LIB_CTX_PROVIDER_CONF_INDEX 16
169*b0d17251Schristos # define OSSL_LIB_CTX_BIO_CORE_INDEX 17
170*b0d17251Schristos # define OSSL_LIB_CTX_CHILD_PROVIDER_INDEX 18
171*b0d17251Schristos # define OSSL_LIB_CTX_MAX_INDEXES 19
172*b0d17251Schristos
173*b0d17251Schristos # define OSSL_LIB_CTX_METHOD_LOW_PRIORITY -1
174*b0d17251Schristos # define OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY 0
175*b0d17251Schristos # define OSSL_LIB_CTX_METHOD_PRIORITY_1 1
176*b0d17251Schristos # define OSSL_LIB_CTX_METHOD_PRIORITY_2 2
177*b0d17251Schristos
178*b0d17251Schristos typedef struct ossl_lib_ctx_method {
179*b0d17251Schristos int priority;
180*b0d17251Schristos void *(*new_func)(OSSL_LIB_CTX *ctx);
181*b0d17251Schristos void (*free_func)(void *);
182*b0d17251Schristos } OSSL_LIB_CTX_METHOD;
183*b0d17251Schristos
184*b0d17251Schristos OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx);
185*b0d17251Schristos int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx);
186*b0d17251Schristos int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx);
187*b0d17251Schristos
188*b0d17251Schristos /* Functions to retrieve pointers to data by index */
189*b0d17251Schristos void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *, int /* index */,
190*b0d17251Schristos const OSSL_LIB_CTX_METHOD * ctx);
191*b0d17251Schristos
192*b0d17251Schristos void ossl_lib_ctx_default_deinit(void);
193*b0d17251Schristos OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx);
194*b0d17251Schristos typedef int (ossl_lib_ctx_run_once_fn)(OSSL_LIB_CTX *ctx);
195*b0d17251Schristos typedef void (ossl_lib_ctx_onfree_fn)(OSSL_LIB_CTX *ctx);
196*b0d17251Schristos
197*b0d17251Schristos int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
198*b0d17251Schristos ossl_lib_ctx_run_once_fn run_once_fn);
199*b0d17251Schristos int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
200*b0d17251Schristos const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx);
201*b0d17251Schristos
202*b0d17251Schristos void ossl_release_default_drbg_ctx(void);
203*b0d17251Schristos
204*b0d17251Schristos OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad);
205*b0d17251Schristos int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
206*b0d17251Schristos CRYPTO_EX_DATA *ad);
207*b0d17251Schristos int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
208*b0d17251Schristos long argl, void *argp,
209*b0d17251Schristos CRYPTO_EX_new *new_func,
210*b0d17251Schristos CRYPTO_EX_dup *dup_func,
211*b0d17251Schristos CRYPTO_EX_free *free_func,
212*b0d17251Schristos int priority);
213*b0d17251Schristos int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx);
214*b0d17251Schristos
215*b0d17251Schristos /* Function for simple binary search */
216*b0d17251Schristos
217*b0d17251Schristos /* Flags */
218*b0d17251Schristos # define OSSL_BSEARCH_VALUE_ON_NOMATCH 0x01
219*b0d17251Schristos # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH 0x02
220*b0d17251Schristos
221*b0d17251Schristos const void *ossl_bsearch(const void *key, const void *base, int num,
222*b0d17251Schristos int size, int (*cmp) (const void *, const void *),
223*b0d17251Schristos int flags);
224*b0d17251Schristos
225*b0d17251Schristos char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
226*b0d17251Schristos const char *sep, size_t max_len);
227*b0d17251Schristos char *ossl_ipaddr_to_asc(unsigned char *p, int len);
228*b0d17251Schristos
229*b0d17251Schristos char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep);
230*b0d17251Schristos unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
231*b0d17251Schristos const char sep);
232*b0d17251Schristos
ossl_ends_with_dirsep(const char * path)233*b0d17251Schristos static ossl_inline int ossl_ends_with_dirsep(const char *path)
234*b0d17251Schristos {
235*b0d17251Schristos if (*path != '\0')
236*b0d17251Schristos path += strlen(path) - 1;
237*b0d17251Schristos # if defined __VMS
238*b0d17251Schristos if (*path == ']' || *path == '>' || *path == ':')
239*b0d17251Schristos return 1;
240*b0d17251Schristos # elif defined _WIN32
241*b0d17251Schristos if (*path == '\\')
242*b0d17251Schristos return 1;
243*b0d17251Schristos # endif
244*b0d17251Schristos return *path == '/';
245*b0d17251Schristos }
246*b0d17251Schristos
ossl_is_absolute_path(const char * path)247*b0d17251Schristos static ossl_inline int ossl_is_absolute_path(const char *path)
248*b0d17251Schristos {
249*b0d17251Schristos # if defined __VMS
250*b0d17251Schristos if (strchr(path, ':') != NULL
251*b0d17251Schristos || ((path[0] == '[' || path[0] == '<')
252*b0d17251Schristos && path[1] != '.' && path[1] != '-'
253*b0d17251Schristos && path[1] != ']' && path[1] != '>'))
254*b0d17251Schristos return 1;
255*b0d17251Schristos # elif defined _WIN32
256*b0d17251Schristos if (path[0] == '\\'
257*b0d17251Schristos || (path[0] != '\0' && path[1] == ':'))
258*b0d17251Schristos return 1;
259*b0d17251Schristos # endif
260*b0d17251Schristos return path[0] == '/';
261*b0d17251Schristos }
262*b0d17251Schristos
26313d40330Schristos #endif
264