1f3cd074jkim/*
21661cedjkim * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3038f65ejkim *
4f3cd074jkim * Licensed under the OpenSSL license (the "License").  You may not use
5f3cd074jkim * this file except in compliance with the License.  You can obtain a copy
6f3cd074jkim * in the file LICENSE in the source distribution or at
7f3cd074jkim * https://www.openssl.org/source/license.html
82e467dckris */
92e467dckris
101661cedjkim#ifndef OSSL_E_OS_H
111661cedjkim# define OSSL_E_OS_H
122e467dckris
13f3cd074jkim# include <limits.h>
14038f65ejkim# include <openssl/opensslconf.h>
152e467dckris
16038f65ejkim# include <openssl/e_os2.h>
17f3cd074jkim# include <openssl/crypto.h>
18f3cd074jkim# include "internal/nelem.h"
19f3cd074jkim
20038f65ejkim/*
21038f65ejkim * <openssl/e_os2.h> contains what we can justify to make visible to the
22038f65ejkim * outside; this file e_os.h is not part of the exported interface.
23038f65ejkim */
242e467dckris
25038f65ejkim# ifndef DEVRANDOM
26038f65ejkim/*
27f3cd074jkim * set this to a comma-separated list of 'random' device files to try out. By
28038f65ejkim * default, we will try to read at least one of these files
29038f65ejkim */
30a36e0f5jkim#  define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
31388b5f1jkim#  if defined(__linux) && !defined(__ANDROID__)
32388b5f1jkim#   ifndef DEVRANDOM_WAIT
33388b5f1jkim#    define DEVRANDOM_WAIT   "/dev/random"
34388b5f1jkim#   endif
35388b5f1jkim/*
36388b5f1jkim * Linux kernels 4.8 and later changes how their random device works and there
37388b5f1jkim * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
38388b5f1jkim * should be used instead.
39388b5f1jkim */
40388b5f1jkim#   ifndef DEVRANDOM_SAFE_KERNEL
41388b5f1jkim#    define DEVRANDOM_SAFE_KERNEL        4, 8
42388b5f1jkim#   endif
43388b5f1jkim/*
44388b5f1jkim * Some operating systems do not permit select(2) on their random devices,
451661cedjkim * defining this to zero will force the use of read(2) to extract one byte
46388b5f1jkim * from /dev/random.
47388b5f1jkim */
48388b5f1jkim#   ifndef DEVRANDM_WAIT_USE_SELECT
49388b5f1jkim#    define DEVRANDM_WAIT_USE_SELECT     1
50388b5f1jkim#   endif
51388b5f1jkim/*
52388b5f1jkim * Define the shared memory identifier used to indicate if the operating
53388b5f1jkim * system has properly seeded the DEVRANDOM source.
54388b5f1jkim */
55388b5f1jkim#   ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
56388b5f1jkim#    define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
57388b5f1jkim#   endif
58388b5f1jkim
59f3cd074jkim#  endif
60038f65ejkim# endif
61f3cd074jkim# if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
62038f65ejkim/*
63f3cd074jkim * set this to a comma-separated list of 'egd' sockets to try out. These
64038f65ejkim * sockets will be tried in the order listed in case accessing the device
65f3cd074jkim * files listed in DEVRANDOM did not return enough randomness.
66038f65ejkim */
67a36e0f5jkim#  define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
68038f65ejkim# endif
692e467dckris
70f3cd074jkim# if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
710aed2eenectar#  define NO_CHMOD
720aed2eenectar#  define NO_SYSLOG
73038f65ejkim# endif
74038f65ejkim
75f3cd074jkim# define get_last_sys_error()    errno
76f3cd074jkim# define clear_sys_error()       errno=0
77cfa887fjkim# define set_sys_error(e)        errno=(e)
7854c77f9kris
792e467dckris/********************************************************************
802e467dckris The Microsoft section
812e467dckris ********************************************************************/
82038f65ejkim# if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
8354c77f9kris#  define WIN32
84038f65ejkim# endif
85038f65ejkim# if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
86aad1d64markm#  define WINDOWS
87038f65ejkim# endif
88038f65ejkim# if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
89aad1d64markm#  define MSDOS
90038f65ejkim# endif
912e467dckris
92038f65ejkim# ifdef WIN32
93f3cd074jkim#  undef get_last_sys_error
94f3cd074jkim#  undef clear_sys_error
95cfa887fjkim#  undef set_sys_error
96038f65ejkim#  define get_last_sys_error()    GetLastError()
97038f65ejkim#  define clear_sys_error()       SetLastError(0)
98cfa887fjkim#  define set_sys_error(e)        SetLastError(e)
99038f65ejkim#  if !defined(WINNT)
100038f65ejkim#   define WIN_CONSOLE_BUG
101038f65ejkim#  endif
102038f65ejkim# else
103038f65ejkim# endif
1042e467dckris
105038f65ejkim# if (defined(WINDOWS) || defined(MSDOS))
106aad1d64markm
107aad1d64markm#  ifdef __DJGPP__
108038f65ejkim#   include <unistd.h>
109038f65ejkim#   include <sys/stat.h>
110038f65ejkim#   define _setmode setmode
111038f65ejkim#   define _O_TEXT O_TEXT
112038f65ejkim#   define _O_BINARY O_BINARY
113f3cd074jkim#   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
114f3cd074jkim#   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
115038f65ejkim#   undef DEVRANDOM
116038f65ejkim#   define DEVRANDOM "/dev/urandom\x24"
117038f65ejkim#  endif                        /* __DJGPP__ */
1182e467dckris
11954c77f9kris#  ifndef S_IFDIR
120038f65ejkim#   define S_IFDIR     _S_IFDIR
12154c77f9kris#  endif
1222e467dckris
12354c77f9kris#  ifndef S_IFMT
124038f65ejkim#   define S_IFMT      _S_IFMT
12554c77f9kris#  endif
1262e467dckris
127aad1d64markm#  if !defined(WINNT) && !defined(__DJGPP__)
128038f65ejkim#   define NO_SYSLOG
12954c77f9kris#  endif
1302e467dckris
1312e467dckris#  ifdef WINDOWS
132038f65ejkim#   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
133fb3c70esimon       /*
134038f65ejkim        * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
135038f65ejkim        * Most notably we ought to check for availability of each specific
136f3cd074jkim        * routine that was introduced after denoted _WIN32_WINNT with
137f3cd074jkim        * GetProcAddress(). Normally newer functions are masked with higher
138f3cd074jkim        * _WIN32_WINNT in SDK headers. So that if you wish to use them in
139f3cd074jkim        * some module, you'd need to override _WIN32_WINNT definition in
140f3cd074jkim        * the target module in order to "reach for" prototypes, but replace
141f3cd074jkim        * calls to new functions with indirect calls. Alternatively it
142f3cd074jkim        * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
143f3cd074jkim        * and check for current OS version instead.
144038f65ejkim        */
145f3cd074jkim#    define _WIN32_WINNT 0x0501
146038f65ejkim#   endif
147f3cd074jkim#   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
148532b408jkim       /*
149532b408jkim        * Just like defining _WIN32_WINNT including winsock2.h implies
150532b408jkim        * certain "discipline" for maintaining [broad] binary compatibility.
151532b408jkim        * As long as structures are invariant among Winsock versions,
152532b408jkim        * it's sufficient to check for specific Winsock2 API availability
153532b408jkim        * at run-time [DSO_global_lookup is recommended]...
154532b408jkim        */
155038f65ejkim#    include <winsock2.h>
156038f65ejkim#    include <ws2tcpip.h>
157532b408jkim       /* yes, they have to be #included prior to <windows.h> */
158038f65ejkim#   endif
159038f65ejkim#   include <windows.h>
160038f65ejkim#   include <stdio.h>
161038f65ejkim#   include <stddef.h>
162038f65ejkim#   include <errno.h>
16364cb0c9jkim#   if defined(_WIN32_WCE) && !defined(EACCES)
16464cb0c9jkim#    define EACCES   13
16564cb0c9jkim#   endif
166038f65ejkim#   include <string.h>
167038f65ejkim#   ifdef _WIN64
168038f65ejkim#    define strlen(s) _strlen31(s)
169fb3c70esimon/* cut strings to 2GB */
170a350427jkimstatic __inline unsigned int _strlen31(const char *str)
171038f65ejkim{
172038f65ejkim    unsigned int len = 0;
173038f65ejkim    while (*str && len < 0x80000000U)
174038f65ejkim        str++, len++;
175038f65ejkim    return len & 0x7FFFFFFF;
176038f65ejkim}
177038f65ejkim#   endif
178038f65ejkim#   include <malloc.h>
17964cb0c9jkim#   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
18064cb0c9jkim#    if _MSC_VER>=1300 && _MSC_VER<1600
181038f65ejkim#     undef stdin
182038f65ejkim#     undef stdout
183038f65ejkim#     undef stderr
184038f65ejkimFILE *__iob_func();
185038f65ejkim#     define stdin  (&__iob_func()[0])
186038f65ejkim#     define stdout (&__iob_func()[1])
187038f65ejkim#     define stderr (&__iob_func()[2])
18864cb0c9jkim#    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
189038f65ejkim#     undef stdin
190038f65ejkim#     undef stdout
191038f65ejkim#     undef stderr
192038f65ejkim         /*
193038f65ejkim          * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
194038f65ejkim          * or in other words with /MD. Declaring implicit import, i.e. with
195038f65ejkim          * _imp_ prefix, works correctly with all compiler options, but
196038f65ejkim          * without /MD results in LINK warning LNK4049: 'locally defined
197038f65ejkim          * symbol "__iob" imported'.
198fc5b6d5simon          */
199038f65ejkimextern FILE *_imp___iob;
200038f65ejkim#     define stdin  (&_imp___iob[0])
201038f65ejkim#     define stdout (&_imp___iob[1])
202038f65ejkim#     define stderr (&_imp___iob[2])
203fc5b6d5simon#    endif
204038f65ejkim#   endif
2052e467dckris#  endif
2062e467dckris#  include <io.h>
2072e467dckris#  include <fcntl.h>
2082e467dckris
209aad1d64markm#  ifdef OPENSSL_SYS_WINCE
210038f65ejkim#   define OPENSSL_NO_POSIX_IO
211aad1d64markm#  endif
212aad1d64markm
213532b408jkim#  define EXIT(n) exit(n)
2142e467dckris#  define LIST_SEPARATOR_CHAR ';'
21554c77f9kris#  ifndef W_OK
216038f65ejkim#   define W_OK        2
21754c77f9kris#  endif
21854c77f9kris#  ifndef R_OK
219038f65ejkim#   define R_OK        4
22054c77f9kris#  endif
221aad1d64markm#  ifdef OPENSSL_SYS_WINCE
222038f65ejkim#   define DEFAULT_HOME  ""
223aad1d64markm#  else
224038f65ejkim#   define DEFAULT_HOME  "C:"
225aad1d64markm#  endif
2262e467dckris
22763414eejkim/* Avoid Visual Studio 13 GetVersion deprecated problems */
228038f65ejkim#  if defined(_MSC_VER) && _MSC_VER>=1800
229038f65ejkim#   define check_winnt() (1)
230038f65ejkim#   define check_win_minplat(x) (1)
231038f65ejkim#  else
232038f65ejkim#   define check_winnt() (GetVersion() < 0x80000000)
233038f65ejkim#   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
234038f65ejkim#  endif
2359a02b27jkim
236038f65ejkim# else                          /* The non-microsoft world */
2372e467dckris
238f3cd074jkim#  if defined(OPENSSL_SYS_VXWORKS)
239f3cd074jkim#   include <sys/times.h>
240f3cd074jkim#  else
241f3cd074jkim#   include <sys/time.h>
242f3cd074jkim#  endif
243f3cd074jkim
244aad1d64markm#  ifdef OPENSSL_SYS_VMS
245038f65ejkim#   define VMS 1
246038f65ejkim  /*
247038f65ejkim   * some programs don't include stdlib, so exit() and others give implicit
248038f65ejkim   * function warnings
249038f65ejkim   */
250038f65ejkim#   include <stdlib.h>
251038f65ejkim#   if defined(__DECC)
252038f65ejkim#    include <unistd.h>
253038f65ejkim#   else
254038f65ejkim#    include <unixlib.h>
255038f65ejkim#   endif
256038f65ejkim#   define LIST_SEPARATOR_CHAR ','
257aad1d64markm  /* We don't have any well-defined random devices on VMS, yet... */
258038f65ejkim#   undef DEVRANDOM
259038f65ejkim  /*-
260038f65ejkim     We need to do this since VMS has the following coding on status codes:
26154c77f9kris
26254c77f9kris     Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
26354c77f9kris               The important thing to know is that odd numbers are considered
264038f65ejkim               good, while even ones are considered errors.
26554c77f9kris     Bits 3-15: actual status number
26654c77f9kris     Bits 16-27: facility number.  0 is considered "unknown"
26754c77f9kris     Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
26854c77f9kris                 output the message (which, for random codes, just looks ugly)
26954c77f9kris
27054c77f9kris     So, what we do here is to change 0 to 1 to get the default success status,
27154c77f9kris     and everything else is shifted up to fit into the status number field, and
272f3cd074jkim     the status is tagged as an error, which is what is wanted here.
273f3cd074jkim
274f3cd074jkim     Finally, we add the VMS C facility code 0x35a000, because there are some
275f3cd074jkim     programs, such as Perl, that will reinterpret the code back to something
276f4095d4jkim     POSIX.  'man perlvms' explains it further.
277f3cd074jkim
278f3cd074jkim     NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
279f3cd074jkim     codes (status type = 1).  I couldn't disagree more.  Fortunately, the
280f3cd074jkim     status type doesn't seem to bother Perl.
28154c77f9kris     -- Richard Levitte
28254c77f9kris  */
283f3cd074jkim#   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
284f3cd074jkim
285f3cd074jkim#   define DEFAULT_HOME "SYS$LOGIN:"
286fb3c70esimon
2872e467dckris#  else
2882e467dckris     /* !defined VMS */
289038f65ejkim#   ifdef OPENSSL_UNISTD
290038f65ejkim#    include OPENSSL_UNISTD
291038f65ejkim#   else
292038f65ejkim#    include <unistd.h>
293038f65ejkim#   endif
294f3cd074jkim#   include <sys/types.h>
295038f65ejkim#   ifdef OPENSSL_SYS_WIN32_CYGWIN
296038f65ejkim#    include <io.h>
297038f65ejkim#    include <fcntl.h>
298038f65ejkim#   endif
299038f65ejkim
300038f65ejkim#   define LIST_SEPARATOR_CHAR ':'
301038f65ejkim#   define EXIT(n)             exit(n)
3022e467dckris#  endif
3032e467dckris
304aad1d64markm# endif
305aad1d64markm
3062e467dckris/***********************************************/
3072e467dckris
308038f65ejkim# if defined(OPENSSL_SYS_WINDOWS)
30978a5957nectar#  define strcasecmp _stricmp
31078a5957nectar#  define strncasecmp _strnicmp
31129e7d35jkim#  if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
312f3cd074jkim#   define open _open
313f3cd074jkim#   define fdopen _fdopen
314f3cd074jkim#   define close _close
315f3cd074jkim#   ifndef strdup
316f3cd074jkim#    define strdup _strdup
317f3cd074jkim#   endif
318f3cd074jkim#   define unlink _unlink
319f3cd074jkim#   define fileno _fileno
320f3cd074jkim#  endif
321f3cd074jkim# else
322f3cd074jkim#  include <strings.h>
323038f65ejkim# endif
324aad1d64markm
325aad1d64markm/* vxworks */
326038f65ejkim# if defined(OPENSSL_SYS_VXWORKS)
327038f65ejkim#  include <ioLib.h>
328038f65ejkim#  include <tickLib.h>
329038f65ejkim#  include <sysLib.h>
330038f65ejkim#  include <vxWorks.h>
331038f65ejkim#  include <sockLib.h>
332038f65ejkim#  include <taskLib.h>
333aad1d64markm
334f3cd074jkim#  define TTY_STRUCT int
335f3cd074jkim#  define sleep(a) taskDelay((a) * sysClkRateGet())
336aad1d64markm
337038f65ejkim/*
338038f65ejkim * NOTE: these are implemented by helpers in database app! if the database is
339f3cd074jkim * not linked, we need to implement them elsewhere
340038f65ejkim */
341aad1d64markmstruct hostent *gethostbyname(const char *name);
342aad1d64markmstruct hostent *gethostbyaddr(const char *addr, int length, int type);
343aad1d64markmstruct servent *getservbyname(const char *name, const char *proto);
344aad1d64markm
345038f65ejkim# endif
346aad1d64markm/* end vxworks */
3472e467dckris
348f4095d4jkim# ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
349f4095d4jkim#  define CRYPTO_memcmp memcmp
350f4095d4jkim# endif
3512e467dckris
352f4095d4jkim/* unistd.h defines _POSIX_VERSION */
353f4095d4jkim# if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
354f4095d4jkim     && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
355f4095d4jkim          || defined(__sun) || defined(__hpux) || defined(__sgi)      \
356f4095d4jkim          || defined(__osf__) )
357f4095d4jkim#  define OPENSSL_SECURE_MEMORY  /* secure memory is implemented */
358f4095d4jkim# endif
3592e467dckris#endif
360