xref: /freebsd-src/crypto/openssl/include/internal/cryptlib.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
8e71b7053SJung-uk Kim  */
9e71b7053SJung-uk Kim 
1017f01e99SJung-uk Kim #ifndef OSSL_INTERNAL_CRYPTLIB_H
1117f01e99SJung-uk Kim # define OSSL_INTERNAL_CRYPTLIB_H
12*b077aed3SPierre Pronchery # pragma once
13e71b7053SJung-uk Kim 
14e71b7053SJung-uk Kim # include <stdlib.h>
15e71b7053SJung-uk Kim # include <string.h>
16e71b7053SJung-uk Kim 
17e71b7053SJung-uk Kim # ifdef OPENSSL_USE_APPLINK
18*b077aed3SPierre Pronchery #  define BIO_FLAGS_UPLINK_INTERNAL 0x8000
19e71b7053SJung-uk Kim #  include "ms/uplink.h"
20*b077aed3SPierre Pronchery # else
21*b077aed3SPierre Pronchery #  define BIO_FLAGS_UPLINK_INTERNAL 0
22e71b7053SJung-uk Kim # endif
23e71b7053SJung-uk Kim 
24e71b7053SJung-uk Kim # include <openssl/crypto.h>
25e71b7053SJung-uk Kim # include <openssl/buffer.h>
26e71b7053SJung-uk Kim # include <openssl/bio.h>
27*b077aed3SPierre Pronchery # include <openssl/asn1.h>
28e71b7053SJung-uk Kim # include <openssl/err.h>
29e71b7053SJung-uk Kim # include "internal/nelem.h"
30e71b7053SJung-uk Kim 
31e71b7053SJung-uk Kim #ifdef NDEBUG
32e71b7053SJung-uk Kim # define ossl_assert(x) ((x) != 0)
33e71b7053SJung-uk Kim #else
ossl_assert_int(int expr,const char * exprstr,const char * file,int line)34e71b7053SJung-uk Kim __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
35e71b7053SJung-uk Kim                                               const char *file, int line)
36e71b7053SJung-uk Kim {
37e71b7053SJung-uk Kim     if (!expr)
38e71b7053SJung-uk Kim         OPENSSL_die(exprstr, file, line);
39e71b7053SJung-uk Kim 
40e71b7053SJung-uk Kim     return expr;
41e71b7053SJung-uk Kim }
42e71b7053SJung-uk Kim 
43e71b7053SJung-uk Kim # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
44e71b7053SJung-uk Kim                                          __FILE__, __LINE__)
45e71b7053SJung-uk Kim 
46e71b7053SJung-uk Kim #endif
47e71b7053SJung-uk Kim 
48*b077aed3SPierre Pronchery /*
49*b077aed3SPierre Pronchery  * Use this inside a union with the field that needs to be aligned to a
50*b077aed3SPierre Pronchery  * reasonable boundary for the platform.  The most pessimistic alignment
51*b077aed3SPierre Pronchery  * of the listed types will be used by the compiler.
52*b077aed3SPierre Pronchery  */
53*b077aed3SPierre Pronchery # define OSSL_UNION_ALIGN       \
54*b077aed3SPierre Pronchery     double align;               \
55*b077aed3SPierre Pronchery     ossl_uintmax_t align_int;   \
56*b077aed3SPierre Pronchery     void *align_ptr
57*b077aed3SPierre Pronchery 
58e71b7053SJung-uk Kim typedef struct ex_callback_st EX_CALLBACK;
59e71b7053SJung-uk Kim DEFINE_STACK_OF(EX_CALLBACK)
60e71b7053SJung-uk Kim 
61e71b7053SJung-uk Kim typedef struct mem_st MEM;
62e71b7053SJung-uk Kim DEFINE_LHASH_OF(MEM);
63e71b7053SJung-uk Kim 
64e71b7053SJung-uk Kim # define OPENSSL_CONF             "openssl.cnf"
65e71b7053SJung-uk Kim 
66e71b7053SJung-uk Kim # ifndef OPENSSL_SYS_VMS
67e71b7053SJung-uk Kim #  define X509_CERT_AREA          OPENSSLDIR
68e71b7053SJung-uk Kim #  define X509_CERT_DIR           OPENSSLDIR "/certs"
69e71b7053SJung-uk Kim #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
70e71b7053SJung-uk Kim #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
71e71b7053SJung-uk Kim #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
72e71b7053SJung-uk Kim # else
73e71b7053SJung-uk Kim #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
74e71b7053SJung-uk Kim #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
75e71b7053SJung-uk Kim #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
76e71b7053SJung-uk Kim #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
77e71b7053SJung-uk Kim #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
78e71b7053SJung-uk Kim # endif
79e71b7053SJung-uk Kim 
80e71b7053SJung-uk Kim # define X509_CERT_DIR_EVP        "SSL_CERT_DIR"
81e71b7053SJung-uk Kim # define X509_CERT_FILE_EVP       "SSL_CERT_FILE"
82e71b7053SJung-uk Kim # define CTLOG_FILE_EVP           "CTLOG_FILE"
83e71b7053SJung-uk Kim 
84e71b7053SJung-uk Kim /* size of string representations */
85e71b7053SJung-uk Kim # define DECIMAL_SIZE(type)      ((sizeof(type)*8+2)/3+1)
86e71b7053SJung-uk Kim # define HEX_SIZE(type)          (sizeof(type)*2)
87e71b7053SJung-uk Kim 
88e71b7053SJung-uk Kim void OPENSSL_cpuid_setup(void);
89*b077aed3SPierre Pronchery #if defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
90*b077aed3SPierre Pronchery     defined(__x86_64) || defined(__x86_64__) || \
91*b077aed3SPierre Pronchery     defined(_M_AMD64) || defined(_M_X64)
92e71b7053SJung-uk Kim extern unsigned int OPENSSL_ia32cap_P[];
93*b077aed3SPierre Pronchery #endif
94e71b7053SJung-uk Kim void OPENSSL_showfatal(const char *fmta, ...);
95*b077aed3SPierre Pronchery int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx);
96*b077aed3SPierre Pronchery void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx);
97e71b7053SJung-uk Kim int openssl_init_fork_handlers(void);
98da327cd2SJung-uk Kim int openssl_get_fork_id(void);
99e71b7053SJung-uk Kim 
100c9cf7b5cSJung-uk Kim char *ossl_safe_getenv(const char *name);
101c9cf7b5cSJung-uk Kim 
102e71b7053SJung-uk Kim extern CRYPTO_RWLOCK *memdbg_lock;
103e71b7053SJung-uk Kim int openssl_strerror_r(int errnum, char *buf, size_t buflen);
104e71b7053SJung-uk Kim # if !defined(OPENSSL_NO_STDIO)
105e71b7053SJung-uk Kim FILE *openssl_fopen(const char *filename, const char *mode);
106e71b7053SJung-uk Kim # else
107e71b7053SJung-uk Kim void *openssl_fopen(const char *filename, const char *mode);
108e71b7053SJung-uk Kim # endif
109e71b7053SJung-uk Kim 
110e71b7053SJung-uk Kim uint32_t OPENSSL_rdtsc(void);
1116935a639SJung-uk Kim size_t OPENSSL_instrument_bus(unsigned int *, size_t);
1126935a639SJung-uk Kim size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
113e71b7053SJung-uk Kim 
114*b077aed3SPierre Pronchery /* ex_data structures */
115*b077aed3SPierre Pronchery 
116*b077aed3SPierre Pronchery /*
117*b077aed3SPierre Pronchery  * Each structure type (sometimes called a class), that supports
118*b077aed3SPierre Pronchery  * exdata has a stack of callbacks for each instance.
119*b077aed3SPierre Pronchery  */
120*b077aed3SPierre Pronchery struct ex_callback_st {
121*b077aed3SPierre Pronchery     long argl;                  /* Arbitrary long */
122*b077aed3SPierre Pronchery     void *argp;                 /* Arbitrary void * */
123*b077aed3SPierre Pronchery     int priority;               /* Priority ordering for freeing */
124*b077aed3SPierre Pronchery     CRYPTO_EX_new *new_func;
125*b077aed3SPierre Pronchery     CRYPTO_EX_free *free_func;
126*b077aed3SPierre Pronchery     CRYPTO_EX_dup *dup_func;
127*b077aed3SPierre Pronchery };
128*b077aed3SPierre Pronchery 
129*b077aed3SPierre Pronchery /*
130*b077aed3SPierre Pronchery  * The state for each class.  This could just be a typedef, but
131*b077aed3SPierre Pronchery  * a structure allows future changes.
132*b077aed3SPierre Pronchery  */
133*b077aed3SPierre Pronchery typedef struct ex_callbacks_st {
134*b077aed3SPierre Pronchery     STACK_OF(EX_CALLBACK) *meth;
135*b077aed3SPierre Pronchery } EX_CALLBACKS;
136*b077aed3SPierre Pronchery 
137*b077aed3SPierre Pronchery typedef struct ossl_ex_data_global_st {
138*b077aed3SPierre Pronchery     CRYPTO_RWLOCK *ex_data_lock;
139*b077aed3SPierre Pronchery     EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
140*b077aed3SPierre Pronchery } OSSL_EX_DATA_GLOBAL;
141*b077aed3SPierre Pronchery 
142*b077aed3SPierre Pronchery 
143*b077aed3SPierre Pronchery /* OSSL_LIB_CTX */
144*b077aed3SPierre Pronchery 
145*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_PROVIDER_STORE_RUN_ONCE_INDEX          0
146*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_DEFAULT_METHOD_STORE_RUN_ONCE_INDEX    1
147*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_METHOD_STORE_RUN_ONCE_INDEX            2
148*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_MAX_RUN_ONCE                           3
149*b077aed3SPierre Pronchery 
150*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX         0
151*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_PROVIDER_STORE_INDEX           1
152*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_PROPERTY_DEFN_INDEX            2
153*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_PROPERTY_STRING_INDEX          3
154*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_NAMEMAP_INDEX                  4
155*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_DRBG_INDEX                     5
156*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_DRBG_NONCE_INDEX               6
157*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_RAND_CRNGT_INDEX               7
158*b077aed3SPierre Pronchery # ifdef FIPS_MODULE
159*b077aed3SPierre Pronchery #  define OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX    8
160*b077aed3SPierre Pronchery # endif
161*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_FIPS_PROV_INDEX                9
162*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_ENCODER_STORE_INDEX           10
163*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_DECODER_STORE_INDEX           11
164*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_SELF_TEST_CB_INDEX            12
165*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_BIO_PROV_INDEX                13
166*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_GLOBAL_PROPERTIES             14
167*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX      15
168*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_PROVIDER_CONF_INDEX           16
169*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_BIO_CORE_INDEX                17
170*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_CHILD_PROVIDER_INDEX          18
171*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_MAX_INDEXES                   19
172*b077aed3SPierre Pronchery 
173*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_METHOD_LOW_PRIORITY          -1
174*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY       0
175*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_METHOD_PRIORITY_1             1
176*b077aed3SPierre Pronchery # define OSSL_LIB_CTX_METHOD_PRIORITY_2             2
177*b077aed3SPierre Pronchery 
178*b077aed3SPierre Pronchery typedef struct ossl_lib_ctx_method {
179*b077aed3SPierre Pronchery     int priority;
180*b077aed3SPierre Pronchery     void *(*new_func)(OSSL_LIB_CTX *ctx);
181*b077aed3SPierre Pronchery     void (*free_func)(void *);
182*b077aed3SPierre Pronchery } OSSL_LIB_CTX_METHOD;
183*b077aed3SPierre Pronchery 
184*b077aed3SPierre Pronchery OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx);
185*b077aed3SPierre Pronchery int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx);
186*b077aed3SPierre Pronchery int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx);
187*b077aed3SPierre Pronchery 
188*b077aed3SPierre Pronchery /* Functions to retrieve pointers to data by index */
189*b077aed3SPierre Pronchery void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *, int /* index */,
190*b077aed3SPierre Pronchery                             const OSSL_LIB_CTX_METHOD * ctx);
191*b077aed3SPierre Pronchery 
192*b077aed3SPierre Pronchery void ossl_lib_ctx_default_deinit(void);
193*b077aed3SPierre Pronchery OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx);
194*b077aed3SPierre Pronchery typedef int (ossl_lib_ctx_run_once_fn)(OSSL_LIB_CTX *ctx);
195*b077aed3SPierre Pronchery typedef void (ossl_lib_ctx_onfree_fn)(OSSL_LIB_CTX *ctx);
196*b077aed3SPierre Pronchery 
197*b077aed3SPierre Pronchery int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
198*b077aed3SPierre Pronchery                           ossl_lib_ctx_run_once_fn run_once_fn);
199*b077aed3SPierre Pronchery int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
200*b077aed3SPierre Pronchery const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx);
201*b077aed3SPierre Pronchery 
202*b077aed3SPierre Pronchery void ossl_release_default_drbg_ctx(void);
203*b077aed3SPierre Pronchery 
204*b077aed3SPierre Pronchery OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad);
205*b077aed3SPierre Pronchery int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
206*b077aed3SPierre Pronchery                                CRYPTO_EX_DATA *ad);
207*b077aed3SPierre Pronchery int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
208*b077aed3SPierre Pronchery                                     long argl, void *argp,
209*b077aed3SPierre Pronchery                                     CRYPTO_EX_new *new_func,
210*b077aed3SPierre Pronchery                                     CRYPTO_EX_dup *dup_func,
211*b077aed3SPierre Pronchery                                     CRYPTO_EX_free *free_func,
212*b077aed3SPierre Pronchery                                     int priority);
213*b077aed3SPierre Pronchery int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx);
214*b077aed3SPierre Pronchery 
215*b077aed3SPierre Pronchery /* Function for simple binary search */
216*b077aed3SPierre Pronchery 
217*b077aed3SPierre Pronchery /* Flags */
218*b077aed3SPierre Pronchery # define OSSL_BSEARCH_VALUE_ON_NOMATCH            0x01
219*b077aed3SPierre Pronchery # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH        0x02
220*b077aed3SPierre Pronchery 
221*b077aed3SPierre Pronchery const void *ossl_bsearch(const void *key, const void *base, int num,
222*b077aed3SPierre Pronchery                          int size, int (*cmp) (const void *, const void *),
223*b077aed3SPierre Pronchery                          int flags);
224*b077aed3SPierre Pronchery 
225*b077aed3SPierre Pronchery char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
226*b077aed3SPierre Pronchery                                    const char *sep, size_t max_len);
227*b077aed3SPierre Pronchery char *ossl_ipaddr_to_asc(unsigned char *p, int len);
228*b077aed3SPierre Pronchery 
229*b077aed3SPierre Pronchery char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep);
230*b077aed3SPierre Pronchery unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
231*b077aed3SPierre Pronchery                                    const char sep);
232*b077aed3SPierre Pronchery 
ossl_ends_with_dirsep(const char * path)233*b077aed3SPierre Pronchery static ossl_inline int ossl_ends_with_dirsep(const char *path)
234*b077aed3SPierre Pronchery {
235*b077aed3SPierre Pronchery     if (*path != '\0')
236*b077aed3SPierre Pronchery         path += strlen(path) - 1;
237*b077aed3SPierre Pronchery # if defined __VMS
238*b077aed3SPierre Pronchery     if (*path == ']' || *path == '>' || *path == ':')
239*b077aed3SPierre Pronchery         return 1;
240*b077aed3SPierre Pronchery # elif defined _WIN32
241*b077aed3SPierre Pronchery     if (*path == '\\')
242*b077aed3SPierre Pronchery         return 1;
243*b077aed3SPierre Pronchery # endif
244*b077aed3SPierre Pronchery     return *path == '/';
245*b077aed3SPierre Pronchery }
246*b077aed3SPierre Pronchery 
ossl_is_absolute_path(const char * path)247*b077aed3SPierre Pronchery static ossl_inline int ossl_is_absolute_path(const char *path)
248*b077aed3SPierre Pronchery {
249*b077aed3SPierre Pronchery # if defined __VMS
250*b077aed3SPierre Pronchery     if (strchr(path, ':') != NULL
251*b077aed3SPierre Pronchery         || ((path[0] == '[' || path[0] == '<')
252*b077aed3SPierre Pronchery             && path[1] != '.' && path[1] != '-'
253*b077aed3SPierre Pronchery             && path[1] != ']' && path[1] != '>'))
254*b077aed3SPierre Pronchery         return 1;
255*b077aed3SPierre Pronchery # elif defined _WIN32
256*b077aed3SPierre Pronchery     if (path[0] == '\\'
257*b077aed3SPierre Pronchery         || (path[0] != '\0' && path[1] == ':'))
258*b077aed3SPierre Pronchery         return 1;
259*b077aed3SPierre Pronchery # endif
260*b077aed3SPierre Pronchery     return path[0] == '/';
261*b077aed3SPierre Pronchery }
262*b077aed3SPierre Pronchery 
263e71b7053SJung-uk Kim #endif
264