e_os.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright 1995-2018 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 HEADER_E_OS_H
  10. # define HEADER_E_OS_H
  11. # include <limits.h>
  12. # include <openssl/opensslconf.h>
  13. # include <openssl/e_os2.h>
  14. # include <openssl/crypto.h>
  15. # include "internal/nelem.h"
  16. /*
  17. * <openssl/e_os2.h> contains what we can justify to make visible to the
  18. * outside; this file e_os.h is not part of the exported interface.
  19. */
  20. # ifndef DEVRANDOM
  21. /*
  22. * set this to a comma-separated list of 'random' device files to try out. By
  23. * default, we will try to read at least one of these files
  24. */
  25. # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
  26. # ifdef __linux
  27. # define DEVRANDOM_WAIT "/dev/random"
  28. # endif
  29. # endif
  30. # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
  31. /*
  32. * set this to a comma-separated list of 'egd' sockets to try out. These
  33. * sockets will be tried in the order listed in case accessing the device
  34. * files listed in DEVRANDOM did not return enough randomness.
  35. */
  36. # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
  37. # endif
  38. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  39. # define NO_CHMOD
  40. # define NO_SYSLOG
  41. # endif
  42. # define get_last_sys_error() errno
  43. # define clear_sys_error() errno=0
  44. # define set_sys_error(e) errno=(e)
  45. /********************************************************************
  46. The Microsoft section
  47. ********************************************************************/
  48. # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
  49. # define WIN32
  50. # endif
  51. # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
  52. # define WINDOWS
  53. # endif
  54. # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
  55. # define MSDOS
  56. # endif
  57. # ifdef WIN32
  58. # undef get_last_sys_error
  59. # undef clear_sys_error
  60. # undef set_sys_error
  61. # define get_last_sys_error() GetLastError()
  62. # define clear_sys_error() SetLastError(0)
  63. # define set_sys_error(e) SetLastError(e)
  64. # if !defined(WINNT)
  65. # define WIN_CONSOLE_BUG
  66. # endif
  67. # else
  68. # endif
  69. # if (defined(WINDOWS) || defined(MSDOS))
  70. # ifdef __DJGPP__
  71. # include <unistd.h>
  72. # include <sys/stat.h>
  73. # define _setmode setmode
  74. # define _O_TEXT O_TEXT
  75. # define _O_BINARY O_BINARY
  76. # define HAS_LFN_SUPPORT(name) (pathconf((name), _PC_NAME_MAX) > 12)
  77. # undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
  78. # undef DEVRANDOM
  79. # define DEVRANDOM "/dev/urandom\x24"
  80. # endif /* __DJGPP__ */
  81. # ifndef S_IFDIR
  82. # define S_IFDIR _S_IFDIR
  83. # endif
  84. # ifndef S_IFMT
  85. # define S_IFMT _S_IFMT
  86. # endif
  87. # if !defined(WINNT) && !defined(__DJGPP__)
  88. # define NO_SYSLOG
  89. # endif
  90. # ifdef WINDOWS
  91. # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
  92. /*
  93. * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
  94. * Most notably we ought to check for availability of each specific
  95. * routine that was introduced after denoted _WIN32_WINNT with
  96. * GetProcAddress(). Normally newer functions are masked with higher
  97. * _WIN32_WINNT in SDK headers. So that if you wish to use them in
  98. * some module, you'd need to override _WIN32_WINNT definition in
  99. * the target module in order to "reach for" prototypes, but replace
  100. * calls to new functions with indirect calls. Alternatively it
  101. * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
  102. * and check for current OS version instead.
  103. */
  104. # define _WIN32_WINNT 0x0501
  105. # endif
  106. # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
  107. /*
  108. * Just like defining _WIN32_WINNT including winsock2.h implies
  109. * certain "discipline" for maintaining [broad] binary compatibility.
  110. * As long as structures are invariant among Winsock versions,
  111. * it's sufficient to check for specific Winsock2 API availability
  112. * at run-time [DSO_global_lookup is recommended]...
  113. */
  114. # include <winsock2.h>
  115. # include <ws2tcpip.h>
  116. /* yes, they have to be #included prior to <windows.h> */
  117. # endif
  118. # include <windows.h>
  119. # include <stdio.h>
  120. # include <stddef.h>
  121. # include <errno.h>
  122. # if defined(_WIN32_WCE) && !defined(EACCES)
  123. # define EACCES 13
  124. # endif
  125. # include <string.h>
  126. # ifdef _WIN64
  127. # define strlen(s) _strlen31(s)
  128. /* cut strings to 2GB */
  129. static __inline unsigned int _strlen31(const char *str)
  130. {
  131. unsigned int len = 0;
  132. while (*str && len < 0x80000000U)
  133. str++, len++;
  134. return len & 0x7FFFFFFF;
  135. }
  136. # endif
  137. # include <malloc.h>
  138. # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
  139. # if _MSC_VER>=1300 && _MSC_VER<1600
  140. # undef stdin
  141. # undef stdout
  142. # undef stderr
  143. FILE *__iob_func();
  144. # define stdin (&__iob_func()[0])
  145. # define stdout (&__iob_func()[1])
  146. # define stderr (&__iob_func()[2])
  147. # elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
  148. # undef stdin
  149. # undef stdout
  150. # undef stderr
  151. /*
  152. * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
  153. * or in other words with /MD. Declaring implicit import, i.e. with
  154. * _imp_ prefix, works correctly with all compiler options, but
  155. * without /MD results in LINK warning LNK4049: 'locally defined
  156. * symbol "__iob" imported'.
  157. */
  158. extern FILE *_imp___iob;
  159. # define stdin (&_imp___iob[0])
  160. # define stdout (&_imp___iob[1])
  161. # define stderr (&_imp___iob[2])
  162. # endif
  163. # endif
  164. # endif
  165. # include <io.h>
  166. # include <fcntl.h>
  167. # ifdef OPENSSL_SYS_WINCE
  168. # define OPENSSL_NO_POSIX_IO
  169. # endif
  170. # define EXIT(n) exit(n)
  171. # define LIST_SEPARATOR_CHAR ';'
  172. # ifndef W_OK
  173. # define W_OK 2
  174. # endif
  175. # ifndef R_OK
  176. # define R_OK 4
  177. # endif
  178. # ifdef OPENSSL_SYS_WINCE
  179. # define DEFAULT_HOME ""
  180. # else
  181. # define DEFAULT_HOME "C:"
  182. # endif
  183. /* Avoid Visual Studio 13 GetVersion deprecated problems */
  184. # if defined(_MSC_VER) && _MSC_VER>=1800
  185. # define check_winnt() (1)
  186. # define check_win_minplat(x) (1)
  187. # else
  188. # define check_winnt() (GetVersion() < 0x80000000)
  189. # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
  190. # endif
  191. # else /* The non-microsoft world */
  192. # if defined(OPENSSL_SYS_VXWORKS)
  193. # include <time.h>
  194. # else
  195. # include <sys/time.h>
  196. # endif
  197. # ifdef OPENSSL_SYS_VMS
  198. # define VMS 1
  199. /*
  200. * some programs don't include stdlib, so exit() and others give implicit
  201. * function warnings
  202. */
  203. # include <stdlib.h>
  204. # if defined(__DECC)
  205. # include <unistd.h>
  206. # else
  207. # include <unixlib.h>
  208. # endif
  209. # define LIST_SEPARATOR_CHAR ','
  210. /* We don't have any well-defined random devices on VMS, yet... */
  211. # undef DEVRANDOM
  212. /*-
  213. We need to do this since VMS has the following coding on status codes:
  214. Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
  215. The important thing to know is that odd numbers are considered
  216. good, while even ones are considered errors.
  217. Bits 3-15: actual status number
  218. Bits 16-27: facility number. 0 is considered "unknown"
  219. Bits 28-31: control bits. If bit 28 is set, the shell won't try to
  220. output the message (which, for random codes, just looks ugly)
  221. So, what we do here is to change 0 to 1 to get the default success status,
  222. and everything else is shifted up to fit into the status number field, and
  223. the status is tagged as an error, which is what is wanted here.
  224. Finally, we add the VMS C facility code 0x35a000, because there are some
  225. programs, such as Perl, that will reinterpret the code back to something
  226. POSIX. 'man perlvms' explains it further.
  227. NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
  228. codes (status type = 1). I couldn't disagree more. Fortunately, the
  229. status type doesn't seem to bother Perl.
  230. -- Richard Levitte
  231. */
  232. # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
  233. # define DEFAULT_HOME "SYS$LOGIN:"
  234. # else
  235. /* !defined VMS */
  236. # ifdef OPENSSL_UNISTD
  237. # include OPENSSL_UNISTD
  238. # else
  239. # include <unistd.h>
  240. # endif
  241. # include <sys/types.h>
  242. # ifdef OPENSSL_SYS_WIN32_CYGWIN
  243. # include <io.h>
  244. # include <fcntl.h>
  245. # endif
  246. # define LIST_SEPARATOR_CHAR ':'
  247. # define EXIT(n) exit(n)
  248. # endif
  249. # endif
  250. /***********************************************/
  251. # if defined(OPENSSL_SYS_WINDOWS)
  252. # define strcasecmp _stricmp
  253. # define strncasecmp _strnicmp
  254. # if (_MSC_VER >= 1310)
  255. # define open _open
  256. # define fdopen _fdopen
  257. # define close _close
  258. # ifndef strdup
  259. # define strdup _strdup
  260. # endif
  261. # define unlink _unlink
  262. # define fileno _fileno
  263. # endif
  264. # else
  265. # include <strings.h>
  266. # endif
  267. /* vxworks */
  268. # if defined(OPENSSL_SYS_VXWORKS)
  269. # include <ioLib.h>
  270. # include <tickLib.h>
  271. # include <sysLib.h>
  272. # include <vxWorks.h>
  273. # include <sockLib.h>
  274. # include <taskLib.h>
  275. # define TTY_STRUCT int
  276. # define sleep(a) taskDelay((a) * sysClkRateGet())
  277. /*
  278. * NOTE: these are implemented by helpers in database app! if the database is
  279. * not linked, we need to implement them elsewhere
  280. */
  281. struct hostent *gethostbyname(const char *name);
  282. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  283. struct servent *getservbyname(const char *name, const char *proto);
  284. # endif
  285. /* end vxworks */
  286. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  287. # define CRYPTO_memcmp memcmp
  288. # endif
  289. /* unistd.h defines _POSIX_VERSION */
  290. # if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
  291. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  292. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  293. || defined(__osf__) )
  294. # define OPENSSL_SECURE_MEMORY /* secure memory is implemented */
  295. # endif
  296. #endif