A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
13 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /*
  2. * Copyright 1995-2021 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. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  21. # define NO_CHMOD
  22. # define NO_SYSLOG
  23. # endif
  24. # define get_last_sys_error() errno
  25. # define clear_sys_error() errno=0
  26. # define set_sys_error(e) errno=(e)
  27. /********************************************************************
  28. The Microsoft section
  29. ********************************************************************/
  30. # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
  31. # define WIN32
  32. # endif
  33. # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
  34. # define WINDOWS
  35. # endif
  36. # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
  37. # define MSDOS
  38. # endif
  39. # ifdef WIN32
  40. # undef get_last_sys_error
  41. # undef clear_sys_error
  42. # undef set_sys_error
  43. # define get_last_sys_error() GetLastError()
  44. # define clear_sys_error() SetLastError(0)
  45. # define set_sys_error(e) SetLastError(e)
  46. # if !defined(WINNT)
  47. # define WIN_CONSOLE_BUG
  48. # endif
  49. # else
  50. # endif
  51. # if (defined(WINDOWS) || defined(MSDOS))
  52. # ifdef __DJGPP__
  53. # include <unistd.h>
  54. # include <sys/stat.h>
  55. # define _setmode setmode
  56. # define _O_TEXT O_TEXT
  57. # define _O_BINARY O_BINARY
  58. # undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
  59. # undef DEVRANDOM
  60. # define DEVRANDOM "/dev/urandom\x24"
  61. # endif /* __DJGPP__ */
  62. # ifndef S_IFDIR
  63. # define S_IFDIR _S_IFDIR
  64. # endif
  65. # ifndef S_IFMT
  66. # define S_IFMT _S_IFMT
  67. # endif
  68. # if !defined(WINNT) && !defined(__DJGPP__)
  69. # define NO_SYSLOG
  70. # endif
  71. # ifdef WINDOWS
  72. # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
  73. /*
  74. * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
  75. * Most notably we ought to check for availability of each specific
  76. * routine that was introduced after denoted _WIN32_WINNT with
  77. * GetProcAddress(). Normally newer functions are masked with higher
  78. * _WIN32_WINNT in SDK headers. So that if you wish to use them in
  79. * some module, you'd need to override _WIN32_WINNT definition in
  80. * the target module in order to "reach for" prototypes, but replace
  81. * calls to new functions with indirect calls. Alternatively it
  82. * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
  83. * and check for current OS version instead.
  84. */
  85. # define _WIN32_WINNT 0x0501
  86. # endif
  87. # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
  88. /*
  89. * Just like defining _WIN32_WINNT including winsock2.h implies
  90. * certain "discipline" for maintaining [broad] binary compatibility.
  91. * As long as structures are invariant among Winsock versions,
  92. * it's sufficient to check for specific Winsock2 API availability
  93. * at run-time [DSO_global_lookup is recommended]...
  94. */
  95. # include <winsock2.h>
  96. # include <ws2tcpip.h>
  97. /*
  98. * Clang-based C++Builder 10.3.3 toolchains cannot find C inline
  99. * definitions at link-time. This header defines WspiapiLoad() as an
  100. * __inline function. https://quality.embarcadero.com/browse/RSP-33806
  101. */
  102. # if !defined(__BORLANDC__) || !defined(__clang__)
  103. # include <wspiapi.h>
  104. # endif
  105. /* yes, they have to be #included prior to <windows.h> */
  106. # endif
  107. # include <windows.h>
  108. # include <stdio.h>
  109. # include <stddef.h>
  110. # include <errno.h>
  111. # if defined(_WIN32_WCE) && !defined(EACCES)
  112. # define EACCES 13
  113. # endif
  114. # include <string.h>
  115. # ifdef _WIN64
  116. # define strlen(s) _strlen31(s)
  117. /* cut strings to 2GB */
  118. static __inline unsigned int _strlen31(const char *str)
  119. {
  120. unsigned int len = 0;
  121. while (*str && len < 0x80000000U)
  122. str++, len++;
  123. return len & 0x7FFFFFFF;
  124. }
  125. # endif
  126. # include <malloc.h>
  127. # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
  128. # if _MSC_VER>=1300 && _MSC_VER<1600
  129. # undef stdin
  130. # undef stdout
  131. # undef stderr
  132. FILE *__iob_func();
  133. # define stdin (&__iob_func()[0])
  134. # define stdout (&__iob_func()[1])
  135. # define stderr (&__iob_func()[2])
  136. # elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
  137. # undef stdin
  138. # undef stdout
  139. # undef stderr
  140. /*
  141. * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
  142. * or in other words with /MD. Declaring implicit import, i.e. with
  143. * _imp_ prefix, works correctly with all compiler options, but
  144. * without /MD results in LINK warning LNK4049: 'locally defined
  145. * symbol "__iob" imported'.
  146. */
  147. extern FILE *_imp___iob;
  148. # define stdin (&_imp___iob[0])
  149. # define stdout (&_imp___iob[1])
  150. # define stderr (&_imp___iob[2])
  151. # endif
  152. # endif
  153. # endif
  154. # include <io.h>
  155. # include <fcntl.h>
  156. # ifdef OPENSSL_SYS_WINCE
  157. # define OPENSSL_NO_POSIX_IO
  158. # endif
  159. # define EXIT(n) exit(n)
  160. # define LIST_SEPARATOR_CHAR ';'
  161. # ifndef W_OK
  162. # define W_OK 2
  163. # endif
  164. # ifndef R_OK
  165. # define R_OK 4
  166. # endif
  167. # ifdef OPENSSL_SYS_WINCE
  168. # define DEFAULT_HOME ""
  169. # else
  170. # define DEFAULT_HOME "C:"
  171. # endif
  172. /* Avoid Visual Studio 13 GetVersion deprecated problems */
  173. # if defined(_MSC_VER) && _MSC_VER>=1800
  174. # define check_winnt() (1)
  175. # define check_win_minplat(x) (1)
  176. # else
  177. # define check_winnt() (GetVersion() < 0x80000000)
  178. # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
  179. # endif
  180. # else /* The non-microsoft world */
  181. # if defined(OPENSSL_SYS_VXWORKS)
  182. # include <time.h>
  183. # else
  184. # include <sys/time.h>
  185. # endif
  186. # ifdef OPENSSL_SYS_VMS
  187. # define VMS 1
  188. /*
  189. * some programs don't include stdlib, so exit() and others give implicit
  190. * function warnings
  191. */
  192. # include <stdlib.h>
  193. # if defined(__DECC)
  194. # include <unistd.h>
  195. # else
  196. # include <unixlib.h>
  197. # endif
  198. # define LIST_SEPARATOR_CHAR ','
  199. /* We don't have any well-defined random devices on VMS, yet... */
  200. # undef DEVRANDOM
  201. /*-
  202. We need to do this since VMS has the following coding on status codes:
  203. Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
  204. The important thing to know is that odd numbers are considered
  205. good, while even ones are considered errors.
  206. Bits 3-15: actual status number
  207. Bits 16-27: facility number. 0 is considered "unknown"
  208. Bits 28-31: control bits. If bit 28 is set, the shell won't try to
  209. output the message (which, for random codes, just looks ugly)
  210. So, what we do here is to change 0 to 1 to get the default success status,
  211. and everything else is shifted up to fit into the status number field, and
  212. the status is tagged as an error, which is what is wanted here.
  213. Finally, we add the VMS C facility code 0x35a000, because there are some
  214. programs, such as Perl, that will reinterpret the code back to something
  215. POSIX. 'man perlvms' explains it further.
  216. NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
  217. codes (status type = 1). I couldn't disagree more. Fortunately, the
  218. status type doesn't seem to bother Perl.
  219. -- Richard Levitte
  220. */
  221. # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
  222. # define DEFAULT_HOME "SYS$LOGIN:"
  223. # else
  224. /* !defined VMS */
  225. # include <unistd.h>
  226. # include <sys/types.h>
  227. # ifdef OPENSSL_SYS_WIN32_CYGWIN
  228. # include <io.h>
  229. # include <fcntl.h>
  230. # endif
  231. # define LIST_SEPARATOR_CHAR ':'
  232. # define EXIT(n) exit(n)
  233. # endif
  234. # endif
  235. /***********************************************/
  236. # if defined(OPENSSL_SYS_WINDOWS)
  237. # define strcasecmp _stricmp
  238. # define strncasecmp _strnicmp
  239. # if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
  240. # define open _open
  241. # define fdopen _fdopen
  242. # define close _close
  243. # ifndef strdup
  244. # define strdup _strdup
  245. # endif
  246. # define unlink _unlink
  247. # define fileno _fileno
  248. # endif
  249. # else
  250. # include <strings.h>
  251. # endif
  252. /* vxworks */
  253. # if defined(OPENSSL_SYS_VXWORKS)
  254. # include <ioLib.h>
  255. # include <tickLib.h>
  256. # include <sysLib.h>
  257. # include <vxWorks.h>
  258. # include <sockLib.h>
  259. # include <taskLib.h>
  260. # define TTY_STRUCT int
  261. # define sleep(a) taskDelay((a) * sysClkRateGet())
  262. /*
  263. * NOTE: these are implemented by helpers in database app! if the database is
  264. * not linked, we need to implement them elsewhere
  265. */
  266. struct hostent *gethostbyname(const char *name);
  267. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  268. struct servent *getservbyname(const char *name, const char *proto);
  269. # endif
  270. /* end vxworks */
  271. /* system-specific variants defining ossl_sleep() */
  272. #ifdef OPENSSL_SYS_UNIX
  273. # include <unistd.h>
  274. static ossl_inline void ossl_sleep(unsigned long millis)
  275. {
  276. # ifdef OPENSSL_SYS_VXWORKS
  277. struct timespec ts;
  278. ts.tv_sec = (long int) (millis / 1000);
  279. ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
  280. nanosleep(&ts, NULL);
  281. # elif defined(__TANDEM)
  282. # if !defined(_REENTRANT)
  283. # include <cextdecs.h(PROCESS_DELAY_)>
  284. /* HPNS does not support usleep for non threaded apps */
  285. PROCESS_DELAY_(millis * 1000);
  286. # elif defined(_SPT_MODEL_)
  287. # include <spthread.h>
  288. # include <spt_extensions.h>
  289. usleep(millis * 1000);
  290. # else
  291. usleep(millis * 1000);
  292. # endif
  293. # else
  294. usleep(millis * 1000);
  295. # endif
  296. }
  297. #elif defined(_WIN32)
  298. # include <windows.h>
  299. static ossl_inline void ossl_sleep(unsigned long millis)
  300. {
  301. Sleep(millis);
  302. }
  303. #else
  304. /* Fallback to a busy wait */
  305. static ossl_inline void ossl_sleep(unsigned long millis)
  306. {
  307. struct timeval start, now;
  308. unsigned long elapsedms;
  309. gettimeofday(&start, NULL);
  310. do {
  311. gettimeofday(&now, NULL);
  312. elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
  313. + now.tv_usec - start.tv_usec) / 1000;
  314. } while (elapsedms < millis);
  315. }
  316. #endif /* defined OPENSSL_SYS_UNIX */
  317. /* ----------------------------- HP NonStop -------------------------------- */
  318. /* Required to support platform variant without getpid() and pid_t. */
  319. # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
  320. # include <strings.h>
  321. # include <netdb.h>
  322. # define getservbyname(name,proto) getservbyname((char*)name,proto)
  323. # define gethostbyname(name) gethostbyname((char*)name)
  324. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  325. # ifdef NO_GETPID
  326. inline int nssgetpid();
  327. # ifndef NSSGETPID_MACRO
  328. # define NSSGETPID_MACRO
  329. # include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
  330. # include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
  331. inline int nssgetpid()
  332. {
  333. short phandle[10]={0};
  334. union pseudo_pid {
  335. struct {
  336. short cpu;
  337. short pin;
  338. } cpu_pin ;
  339. int ppid;
  340. } ppid = { 0 };
  341. PROCESSHANDLE_GETMINE_(phandle);
  342. PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
  343. return ppid.ppid;
  344. }
  345. # define getpid(a) nssgetpid(a)
  346. # endif /* NSSGETPID_MACRO */
  347. # endif /* NO_GETPID */
  348. /*# define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
  349. /*# define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
  350. /*# define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
  351. /*# define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
  352. /*# define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
  353. # if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
  354. /*
  355. * HPNS SPT threads
  356. */
  357. # define SPT_THREAD_SIGNAL 1
  358. # define SPT_THREAD_AWARE 1
  359. # include <spthread.h>
  360. # undef close
  361. # define close spt_close
  362. /*
  363. # define get_last_socket_error() errno
  364. # define clear_socket_error() errno=0
  365. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  366. # define closesocket(s) close(s)
  367. # define readsocket(s,b,n) read((s),(char*)(b),(n))
  368. # define writesocket(s,b,n) write((s),(char*)(b),(n)
  369. */
  370. # define accept(a,b,c) accept(a,(struct sockaddr *)b,c)
  371. # define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
  372. # endif
  373. # endif
  374. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  375. # define CRYPTO_memcmp memcmp
  376. # endif
  377. # ifndef OPENSSL_NO_SECURE_MEMORY
  378. /* unistd.h defines _POSIX_VERSION */
  379. # if (defined(OPENSSL_SYS_UNIX) \
  380. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  381. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  382. || defined(__osf__) )) \
  383. || defined(_WIN32)
  384. /* secure memory is implemented */
  385. # else
  386. # define OPENSSL_NO_SECURE_MEMORY
  387. # endif
  388. # endif
  389. #endif