cryptlib.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright 1995-2020 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. #ifndef OSSL_INTERNAL_CRYPTLIB_H
  10. # define OSSL_INTERNAL_CRYPTLIB_H
  11. # include <stdlib.h>
  12. # include <string.h>
  13. # ifdef OPENSSL_USE_APPLINK
  14. # define BIO_FLAGS_UPLINK_INTERNAL 0x8000
  15. # include "ms/uplink.h"
  16. # else
  17. # define BIO_FLAGS_UPLINK_INTERNAL 0
  18. # endif
  19. # include <openssl/crypto.h>
  20. # include <openssl/buffer.h>
  21. # include <openssl/bio.h>
  22. # include <openssl/asn1.h>
  23. # include <openssl/err.h>
  24. # include "internal/nelem.h"
  25. #ifdef NDEBUG
  26. # define ossl_assert(x) ((x) != 0)
  27. #else
  28. __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
  29. const char *file, int line)
  30. {
  31. if (!expr)
  32. OPENSSL_die(exprstr, file, line);
  33. return expr;
  34. }
  35. # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
  36. __FILE__, __LINE__)
  37. #endif
  38. /*
  39. * Use this inside a union with the field that needs to be aligned to a
  40. * reasonable boundary for the platform. The most pessimistic alignment
  41. * of the listed types will be used by the compiler.
  42. */
  43. # define OSSL_UNION_ALIGN \
  44. double align; \
  45. ossl_uintmax_t align_int; \
  46. void *align_ptr
  47. typedef struct ex_callback_st EX_CALLBACK;
  48. DEFINE_STACK_OF(EX_CALLBACK)
  49. typedef struct mem_st MEM;
  50. DEFINE_LHASH_OF(MEM);
  51. # define OPENSSL_CONF "openssl.cnf"
  52. # ifndef OPENSSL_SYS_VMS
  53. # define X509_CERT_AREA OPENSSLDIR
  54. # define X509_CERT_DIR OPENSSLDIR "/certs"
  55. # define X509_CERT_FILE OPENSSLDIR "/cert.pem"
  56. # define X509_PRIVATE_DIR OPENSSLDIR "/private"
  57. # define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"
  58. # else
  59. # define X509_CERT_AREA "OSSL$DATAROOT:[000000]"
  60. # define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"
  61. # define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"
  62. # define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"
  63. # define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"
  64. # endif
  65. # define X509_CERT_DIR_EVP "SSL_CERT_DIR"
  66. # define X509_CERT_FILE_EVP "SSL_CERT_FILE"
  67. # define CTLOG_FILE_EVP "CTLOG_FILE"
  68. /* size of string representations */
  69. # define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
  70. # define HEX_SIZE(type) (sizeof(type)*2)
  71. void OPENSSL_cpuid_setup(void);
  72. #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  73. defined(__x86_64) || defined(__x86_64__) || \
  74. defined(_M_AMD64) || defined(_M_X64)
  75. extern unsigned int OPENSSL_ia32cap_P[];
  76. #endif
  77. void OPENSSL_showfatal(const char *fmta, ...);
  78. int do_ex_data_init(OSSL_LIB_CTX *ctx);
  79. void crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx);
  80. int openssl_init_fork_handlers(void);
  81. int openssl_get_fork_id(void);
  82. char *ossl_safe_getenv(const char *name);
  83. extern CRYPTO_RWLOCK *memdbg_lock;
  84. int openssl_strerror_r(int errnum, char *buf, size_t buflen);
  85. # if !defined(OPENSSL_NO_STDIO)
  86. FILE *openssl_fopen(const char *filename, const char *mode);
  87. # else
  88. void *openssl_fopen(const char *filename, const char *mode);
  89. # endif
  90. uint32_t OPENSSL_rdtsc(void);
  91. size_t OPENSSL_instrument_bus(unsigned int *, size_t);
  92. size_t OPENSSL_instrument_bus2(unsigned int *, size_t, size_t);
  93. /* ex_data structures */
  94. /*
  95. * Each structure type (sometimes called a class), that supports
  96. * exdata has a stack of callbacks for each instance.
  97. */
  98. struct ex_callback_st {
  99. long argl; /* Arbitrary long */
  100. void *argp; /* Arbitrary void * */
  101. CRYPTO_EX_new *new_func;
  102. CRYPTO_EX_free *free_func;
  103. CRYPTO_EX_dup *dup_func;
  104. };
  105. /*
  106. * The state for each class. This could just be a typedef, but
  107. * a structure allows future changes.
  108. */
  109. typedef struct ex_callbacks_st {
  110. STACK_OF(EX_CALLBACK) *meth;
  111. } EX_CALLBACKS;
  112. typedef struct ossl_ex_data_global_st {
  113. CRYPTO_RWLOCK *ex_data_lock;
  114. EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
  115. } OSSL_EX_DATA_GLOBAL;
  116. /* OSSL_LIB_CTX */
  117. # define OSSL_LIB_CTX_PROVIDER_STORE_RUN_ONCE_INDEX 0
  118. # define OSSL_LIB_CTX_DEFAULT_METHOD_STORE_RUN_ONCE_INDEX 1
  119. # define OSSL_LIB_CTX_METHOD_STORE_RUN_ONCE_INDEX 2
  120. # define OSSL_LIB_CTX_MAX_RUN_ONCE 3
  121. # define OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX 0
  122. # define OSSL_LIB_CTX_PROVIDER_STORE_INDEX 1
  123. # define OSSL_LIB_CTX_PROPERTY_DEFN_INDEX 2
  124. # define OSSL_LIB_CTX_PROPERTY_STRING_INDEX 3
  125. # define OSSL_LIB_CTX_NAMEMAP_INDEX 4
  126. # define OSSL_LIB_CTX_DRBG_INDEX 5
  127. # define OSSL_LIB_CTX_DRBG_NONCE_INDEX 6
  128. # define OSSL_LIB_CTX_RAND_CRNGT_INDEX 7
  129. # define OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX 8
  130. # define OSSL_LIB_CTX_FIPS_PROV_INDEX 9
  131. # define OSSL_LIB_CTX_ENCODER_STORE_INDEX 10
  132. # define OSSL_LIB_CTX_DECODER_STORE_INDEX 11
  133. # define OSSL_LIB_CTX_SELF_TEST_CB_INDEX 12
  134. # define OSSL_LIB_CTX_BIO_PROV_INDEX 13
  135. # define OSSL_LIB_CTX_GLOBAL_PROPERTIES 14
  136. # define OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX 15
  137. # define OSSL_LIB_CTX_MAX_INDEXES 16
  138. typedef struct ossl_lib_ctx_method {
  139. void *(*new_func)(OSSL_LIB_CTX *ctx);
  140. void (*free_func)(void *);
  141. } OSSL_LIB_CTX_METHOD;
  142. OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx);
  143. int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx);
  144. int ossl_lib_ctx_is_global_default(OSSL_LIB_CTX *ctx);
  145. /* Functions to retrieve pointers to data by index */
  146. void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *, int /* index */,
  147. const OSSL_LIB_CTX_METHOD * ctx);
  148. void ossl_lib_ctx_default_deinit(void);
  149. OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx);
  150. typedef int (ossl_lib_ctx_run_once_fn)(OSSL_LIB_CTX *ctx);
  151. typedef void (ossl_lib_ctx_onfree_fn)(OSSL_LIB_CTX *ctx);
  152. int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
  153. ossl_lib_ctx_run_once_fn run_once_fn);
  154. int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
  155. OSSL_LIB_CTX *crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad);
  156. int crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
  157. CRYPTO_EX_DATA *ad);
  158. int crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
  159. long argl, void *argp,
  160. CRYPTO_EX_new *new_func,
  161. CRYPTO_EX_dup *dup_func,
  162. CRYPTO_EX_free *free_func);
  163. int crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx);
  164. /* Function for simple binary search */
  165. /* Flags */
  166. # define OSSL_BSEARCH_VALUE_ON_NOMATCH 0x01
  167. # define OSSL_BSEARCH_FIRST_VALUE_ON_MATCH 0x02
  168. const void *ossl_bsearch(const void *key, const void *base, int num,
  169. int size, int (*cmp) (const void *, const void *),
  170. int flags);
  171. /* system-specific variants defining ossl_sleep() */
  172. #ifdef OPENSSL_SYS_UNIX
  173. # include <unistd.h>
  174. static ossl_inline void ossl_sleep(unsigned long millis)
  175. {
  176. # ifdef OPENSSL_SYS_VXWORKS
  177. struct timespec ts;
  178. ts.tv_sec = (long int) (millis / 1000);
  179. ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
  180. nanosleep(&ts, NULL);
  181. # elif defined(__TANDEM) && !defined(_REENTRANT)
  182. # include <cextdecs.h(PROCESS_DELAY_)>
  183. /* HPNS does not support usleep for non threaded apps */
  184. PROCESS_DELAY_(millis * 1000);
  185. # else
  186. usleep(millis * 1000);
  187. # endif
  188. }
  189. #elif defined(_WIN32)
  190. # include <windows.h>
  191. static ossl_inline void ossl_sleep(unsigned long millis)
  192. {
  193. Sleep(millis);
  194. }
  195. #else
  196. /* Fallback to a busy wait */
  197. static ossl_inline void ossl_sleep(unsigned long millis)
  198. {
  199. struct timeval start, now;
  200. unsigned long elapsedms;
  201. gettimeofday(&start, NULL);
  202. do {
  203. gettimeofday(&now, NULL);
  204. elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
  205. + now.tv_usec - start.tv_usec) / 1000;
  206. } while (elapsedms < millis);
  207. }
  208. #endif /* defined OPENSSL_SYS_UNIX */
  209. char *sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, const char *sep,
  210. size_t max_len);
  211. char *ipaddr_to_asc(unsigned char *p, int len);
  212. char *openssl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep);
  213. unsigned char *openssl_hexstr2buf_sep(const char *str, long *buflen,
  214. const char sep);
  215. static ossl_inline int ossl_ends_with_dirsep(const char *path)
  216. {
  217. if (*path != '\0')
  218. path += strlen(path) - 1;
  219. # if defined __VMS
  220. if (*path == ']' || *path == '>' || *path == ':')
  221. return 1;
  222. # elif defined _WIN32
  223. if (*path == '\\')
  224. return 1;
  225. # endif
  226. return *path == '/';
  227. }
  228. static ossl_inline int ossl_is_absolute_path(const char *path)
  229. {
  230. # if defined __VMS
  231. if (strchr(path, ':') != NULL
  232. || ((path[0] == '[' || path[0] == '<')
  233. && path[1] != '.' && path[1] != '-'
  234. && path[1] != ']' && path[1] != '>'))
  235. return 1;
  236. # elif defined _WIN32
  237. if (path[0] == '\\'
  238. || (path[0] != '\0' && path[1] == ':'))
  239. return 1;
  240. # endif
  241. return path[0] == '/';
  242. }
  243. #endif