e_os.h 11 KB

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