xref: /freebsd/crypto/openssl/e_os.h (revision 6935a639)
1e71b7053SJung-uk Kim /*
2e71b7053SJung-uk Kim  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4e71b7053SJung-uk Kim  * Licensed under the OpenSSL license (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
1074664626SKris Kennaway #ifndef HEADER_E_OS_H
1174664626SKris Kennaway # define HEADER_E_OS_H
1274664626SKris Kennaway 
13e71b7053SJung-uk Kim # include <limits.h>
1474664626SKris Kennaway # include <openssl/opensslconf.h>
1574664626SKris Kennaway 
1674664626SKris Kennaway # include <openssl/e_os2.h>
17e71b7053SJung-uk Kim # include <openssl/crypto.h>
18e71b7053SJung-uk Kim # include "internal/nelem.h"
19e71b7053SJung-uk Kim 
206f9291ceSJung-uk Kim /*
216f9291ceSJung-uk Kim  * <openssl/e_os2.h> contains what we can justify to make visible to the
226f9291ceSJung-uk Kim  * outside; this file e_os.h is not part of the exported interface.
236f9291ceSJung-uk Kim  */
2474664626SKris Kennaway 
2574664626SKris Kennaway # ifndef DEVRANDOM
266f9291ceSJung-uk Kim /*
27e71b7053SJung-uk Kim  * set this to a comma-separated list of 'random' device files to try out. By
286f9291ceSJung-uk Kim  * default, we will try to read at least one of these files
296f9291ceSJung-uk Kim  */
30e71b7053SJung-uk Kim #  if defined(__s390__)
31e71b7053SJung-uk Kim #   define DEVRANDOM "/dev/prandom","/dev/urandom","/dev/hwrng","/dev/random"
32e71b7053SJung-uk Kim #  else
335c87c606SMark Murray #   define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
345c87c606SMark Murray #  endif
35e71b7053SJung-uk Kim # endif
36e71b7053SJung-uk Kim # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
376f9291ceSJung-uk Kim /*
38e71b7053SJung-uk Kim  * set this to a comma-separated list of 'egd' sockets to try out. These
396f9291ceSJung-uk Kim  * sockets will be tried in the order listed in case accessing the device
40e71b7053SJung-uk Kim  * files listed in DEVRANDOM did not return enough randomness.
416f9291ceSJung-uk Kim  */
425c87c606SMark Murray #  define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
4374664626SKris Kennaway # endif
4474664626SKris Kennaway 
45e71b7053SJung-uk Kim # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
46c1803d78SJacques Vidrine #  define NO_CHMOD
47c1803d78SJacques Vidrine #  define NO_SYSLOG
48c1803d78SJacques Vidrine # endif
49c1803d78SJacques Vidrine 
50e71b7053SJung-uk Kim # define get_last_sys_error()    errno
51e71b7053SJung-uk Kim # define clear_sys_error()       errno=0
526935a639SJung-uk Kim # define set_sys_error(e)        errno=(e)
53f579bf8eSKris Kennaway 
5474664626SKris Kennaway /********************************************************************
5574664626SKris Kennaway  The Microsoft section
5674664626SKris Kennaway  ********************************************************************/
575c87c606SMark Murray # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
58f579bf8eSKris Kennaway #  define WIN32
59f579bf8eSKris Kennaway # endif
605c87c606SMark Murray # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
6174664626SKris Kennaway #  define WINDOWS
6274664626SKris Kennaway # endif
635c87c606SMark Murray # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
6474664626SKris Kennaway #  define MSDOS
6574664626SKris Kennaway # endif
6674664626SKris Kennaway 
6774664626SKris Kennaway # ifdef WIN32
68e71b7053SJung-uk Kim #  undef get_last_sys_error
69e71b7053SJung-uk Kim #  undef clear_sys_error
706935a639SJung-uk Kim #  undef set_sys_error
7174664626SKris Kennaway #  define get_last_sys_error()    GetLastError()
7274664626SKris Kennaway #  define clear_sys_error()       SetLastError(0)
736935a639SJung-uk Kim #  define set_sys_error(e)        SetLastError(e)
7474664626SKris Kennaway #  if !defined(WINNT)
7574664626SKris Kennaway #   define WIN_CONSOLE_BUG
7674664626SKris Kennaway #  endif
7774664626SKris Kennaway # else
7874664626SKris Kennaway # endif
7974664626SKris Kennaway 
805c87c606SMark Murray # if (defined(WINDOWS) || defined(MSDOS))
815c87c606SMark Murray 
825c87c606SMark Murray #  ifdef __DJGPP__
835c87c606SMark Murray #   include <unistd.h>
845c87c606SMark Murray #   include <sys/stat.h>
855c87c606SMark Murray #   define _setmode setmode
865c87c606SMark Murray #   define _O_TEXT O_TEXT
875c87c606SMark Murray #   define _O_BINARY O_BINARY
88e71b7053SJung-uk Kim #   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
89e71b7053SJung-uk Kim #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
903b4e3dcbSSimon L. B. Nielsen #   undef DEVRANDOM
913b4e3dcbSSimon L. B. Nielsen #   define DEVRANDOM "/dev/urandom\x24"
925c87c606SMark Murray #  endif                        /* __DJGPP__ */
9374664626SKris Kennaway 
9474664626SKris Kennaway #  ifndef S_IFDIR
9574664626SKris Kennaway #   define S_IFDIR     _S_IFDIR
9674664626SKris Kennaway #  endif
9774664626SKris Kennaway 
9874664626SKris Kennaway #  ifndef S_IFMT
9974664626SKris Kennaway #   define S_IFMT      _S_IFMT
100f579bf8eSKris Kennaway #  endif
10174664626SKris Kennaway 
1025c87c606SMark Murray #  if !defined(WINNT) && !defined(__DJGPP__)
10374664626SKris Kennaway #   define NO_SYSLOG
10474664626SKris Kennaway #  endif
10574664626SKris Kennaway 
10674664626SKris Kennaway #  ifdef WINDOWS
1073b4e3dcbSSimon L. B. Nielsen #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
1083b4e3dcbSSimon L. B. Nielsen        /*
1093b4e3dcbSSimon L. B. Nielsen         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
1103b4e3dcbSSimon L. B. Nielsen         * Most notably we ought to check for availability of each specific
111e71b7053SJung-uk Kim         * routine that was introduced after denoted _WIN32_WINNT with
112e71b7053SJung-uk Kim         * GetProcAddress(). Normally newer functions are masked with higher
113e71b7053SJung-uk Kim         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
114e71b7053SJung-uk Kim         * some module, you'd need to override _WIN32_WINNT definition in
115e71b7053SJung-uk Kim         * the target module in order to "reach for" prototypes, but replace
116e71b7053SJung-uk Kim         * calls to new functions with indirect calls. Alternatively it
117e71b7053SJung-uk Kim         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
118e71b7053SJung-uk Kim         * and check for current OS version instead.
1193b4e3dcbSSimon L. B. Nielsen         */
120e71b7053SJung-uk Kim #    define _WIN32_WINNT 0x0501
1213b4e3dcbSSimon L. B. Nielsen #   endif
122e71b7053SJung-uk Kim #   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
1231f13597dSJung-uk Kim        /*
1241f13597dSJung-uk Kim         * Just like defining _WIN32_WINNT including winsock2.h implies
1251f13597dSJung-uk Kim         * certain "discipline" for maintaining [broad] binary compatibility.
1261f13597dSJung-uk Kim         * As long as structures are invariant among Winsock versions,
1271f13597dSJung-uk Kim         * it's sufficient to check for specific Winsock2 API availability
1281f13597dSJung-uk Kim         * at run-time [DSO_global_lookup is recommended]...
1291f13597dSJung-uk Kim         */
1301f13597dSJung-uk Kim #    include <winsock2.h>
1311f13597dSJung-uk Kim #    include <ws2tcpip.h>
1321f13597dSJung-uk Kim        /* yes, they have to be #included prior to <windows.h> */
1331f13597dSJung-uk Kim #   endif
13474664626SKris Kennaway #   include <windows.h>
135db522d3aSSimon L. B. Nielsen #   include <stdio.h>
13674664626SKris Kennaway #   include <stddef.h>
13774664626SKris Kennaway #   include <errno.h>
1387bded2dbSJung-uk Kim #   if defined(_WIN32_WCE) && !defined(EACCES)
1397bded2dbSJung-uk Kim #    define EACCES   13
1407bded2dbSJung-uk Kim #   endif
14174664626SKris Kennaway #   include <string.h>
1423b4e3dcbSSimon L. B. Nielsen #   ifdef _WIN64
1433b4e3dcbSSimon L. B. Nielsen #    define strlen(s) _strlen31(s)
1443b4e3dcbSSimon L. B. Nielsen /* cut strings to 2GB */
145751d2991SJung-uk Kim static __inline unsigned int _strlen31(const char *str)
1463b4e3dcbSSimon L. B. Nielsen {
1473b4e3dcbSSimon L. B. Nielsen     unsigned int len = 0;
1486f9291ceSJung-uk Kim     while (*str && len < 0x80000000U)
1496f9291ceSJung-uk Kim         str++, len++;
1503b4e3dcbSSimon L. B. Nielsen     return len & 0x7FFFFFFF;
1513b4e3dcbSSimon L. B. Nielsen }
1523b4e3dcbSSimon L. B. Nielsen #   endif
15374664626SKris Kennaway #   include <malloc.h>
1547bded2dbSJung-uk Kim #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
1557bded2dbSJung-uk Kim #    if _MSC_VER>=1300 && _MSC_VER<1600
156db522d3aSSimon L. B. Nielsen #     undef stdin
157db522d3aSSimon L. B. Nielsen #     undef stdout
158db522d3aSSimon L. B. Nielsen #     undef stderr
159db522d3aSSimon L. B. Nielsen FILE *__iob_func();
160db522d3aSSimon L. B. Nielsen #     define stdin  (&__iob_func()[0])
161db522d3aSSimon L. B. Nielsen #     define stdout (&__iob_func()[1])
162db522d3aSSimon L. B. Nielsen #     define stderr (&__iob_func()[2])
1637bded2dbSJung-uk Kim #    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
164db522d3aSSimon L. B. Nielsen #     undef stdin
165db522d3aSSimon L. B. Nielsen #     undef stdout
166db522d3aSSimon L. B. Nielsen #     undef stderr
1676f9291ceSJung-uk Kim          /*
1686f9291ceSJung-uk Kim           * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
1696f9291ceSJung-uk Kim           * or in other words with /MD. Declaring implicit import, i.e. with
1706f9291ceSJung-uk Kim           * _imp_ prefix, works correctly with all compiler options, but
1716f9291ceSJung-uk Kim           * without /MD results in LINK warning LNK4049: 'locally defined
1726f9291ceSJung-uk Kim           * symbol "__iob" imported'.
173db522d3aSSimon L. B. Nielsen           */
174db522d3aSSimon L. B. Nielsen extern FILE *_imp___iob;
175db522d3aSSimon L. B. Nielsen #     define stdin  (&_imp___iob[0])
176db522d3aSSimon L. B. Nielsen #     define stdout (&_imp___iob[1])
177db522d3aSSimon L. B. Nielsen #     define stderr (&_imp___iob[2])
178db522d3aSSimon L. B. Nielsen #    endif
179db522d3aSSimon L. B. Nielsen #   endif
18074664626SKris Kennaway #  endif
18174664626SKris Kennaway #  include <io.h>
18274664626SKris Kennaway #  include <fcntl.h>
18374664626SKris Kennaway 
1845c87c606SMark Murray #  ifdef OPENSSL_SYS_WINCE
1851f13597dSJung-uk Kim #   define OPENSSL_NO_POSIX_IO
1865c87c606SMark Murray #  endif
1875c87c606SMark Murray 
18850ef0093SJacques Vidrine #  define EXIT(n) exit(n)
18974664626SKris Kennaway #  define LIST_SEPARATOR_CHAR ';'
19074664626SKris Kennaway #  ifndef W_OK
19174664626SKris Kennaway #   define W_OK        2
19274664626SKris Kennaway #  endif
19374664626SKris Kennaway #  ifndef R_OK
19474664626SKris Kennaway #   define R_OK        4
19574664626SKris Kennaway #  endif
1965c87c606SMark Murray #  ifdef OPENSSL_SYS_WINCE
1975c87c606SMark Murray #   define DEFAULT_HOME  ""
1985c87c606SMark Murray #  else
19926d191b4SKris Kennaway #   define DEFAULT_HOME  "C:"
2005c87c606SMark Murray #  endif
20174664626SKris Kennaway 
202dc2b908fSJung-uk Kim /* Avoid Visual Studio 13 GetVersion deprecated problems */
203560ede85SJung-uk Kim #  if defined(_MSC_VER) && _MSC_VER>=1800
204560ede85SJung-uk Kim #   define check_winnt() (1)
205dc2b908fSJung-uk Kim #   define check_win_minplat(x) (1)
206560ede85SJung-uk Kim #  else
207560ede85SJung-uk Kim #   define check_winnt() (GetVersion() < 0x80000000)
208dc2b908fSJung-uk Kim #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
209560ede85SJung-uk Kim #  endif
210560ede85SJung-uk Kim 
2111f13597dSJung-uk Kim # else                          /* The non-microsoft world */
21274664626SKris Kennaway 
213e71b7053SJung-uk Kim #  if defined(OPENSSL_SYS_VXWORKS)
214e71b7053SJung-uk Kim #   include <sys/times.h>
215e71b7053SJung-uk Kim #  else
216e71b7053SJung-uk Kim #   include <sys/time.h>
217e71b7053SJung-uk Kim #  endif
218e71b7053SJung-uk Kim 
2195c87c606SMark Murray #  ifdef OPENSSL_SYS_VMS
22074664626SKris Kennaway #   define VMS 1
2216f9291ceSJung-uk Kim   /*
2226f9291ceSJung-uk Kim    * some programs don't include stdlib, so exit() and others give implicit
2236f9291ceSJung-uk Kim    * function warnings
2246f9291ceSJung-uk Kim    */
22574664626SKris Kennaway #   include <stdlib.h>
22674664626SKris Kennaway #   if defined(__DECC)
22774664626SKris Kennaway #    include <unistd.h>
22874664626SKris Kennaway #   else
22974664626SKris Kennaway #    include <unixlib.h>
23074664626SKris Kennaway #   endif
23174664626SKris Kennaway #   define LIST_SEPARATOR_CHAR ','
2325c87c606SMark Murray   /* We don't have any well-defined random devices on VMS, yet... */
2335c87c606SMark Murray #   undef DEVRANDOM
2346f9291ceSJung-uk Kim   /*-
2356f9291ceSJung-uk Kim      We need to do this since VMS has the following coding on status codes:
236f579bf8eSKris Kennaway 
237f579bf8eSKris Kennaway      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
238f579bf8eSKris Kennaway                The important thing to know is that odd numbers are considered
239f579bf8eSKris Kennaway                good, while even ones are considered errors.
240f579bf8eSKris Kennaway      Bits 3-15: actual status number
241f579bf8eSKris Kennaway      Bits 16-27: facility number.  0 is considered "unknown"
242f579bf8eSKris Kennaway      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
243f579bf8eSKris Kennaway                  output the message (which, for random codes, just looks ugly)
244f579bf8eSKris Kennaway 
245f579bf8eSKris Kennaway      So, what we do here is to change 0 to 1 to get the default success status,
246f579bf8eSKris Kennaway      and everything else is shifted up to fit into the status number field, and
247e71b7053SJung-uk Kim      the status is tagged as an error, which is what is wanted here.
248e71b7053SJung-uk Kim 
249e71b7053SJung-uk Kim      Finally, we add the VMS C facility code 0x35a000, because there are some
250e71b7053SJung-uk Kim      programs, such as Perl, that will reinterpret the code back to something
251c9cf7b5cSJung-uk Kim      POSIX.  'man perlvms' explains it further.
252e71b7053SJung-uk Kim 
253e71b7053SJung-uk Kim      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
254e71b7053SJung-uk Kim      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
255e71b7053SJung-uk Kim      status type doesn't seem to bother Perl.
256f579bf8eSKris Kennaway      -- Richard Levitte
257f579bf8eSKris Kennaway   */
258e71b7053SJung-uk Kim #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
2593b4e3dcbSSimon L. B. Nielsen 
260e71b7053SJung-uk Kim #   define DEFAULT_HOME "SYS$LOGIN:"
2613b4e3dcbSSimon L. B. Nielsen 
26274664626SKris Kennaway #  else
26374664626SKris Kennaway      /* !defined VMS */
264f579bf8eSKris Kennaway #   ifdef OPENSSL_UNISTD
26574664626SKris Kennaway #    include OPENSSL_UNISTD
266f579bf8eSKris Kennaway #   else
267f579bf8eSKris Kennaway #    include <unistd.h>
268f579bf8eSKris Kennaway #   endif
269f579bf8eSKris Kennaway #   include <sys/types.h>
2701f13597dSJung-uk Kim #   ifdef OPENSSL_SYS_WIN32_CYGWIN
2711f13597dSJung-uk Kim #    include <io.h>
2721f13597dSJung-uk Kim #    include <fcntl.h>
2731f13597dSJung-uk Kim #   endif
27474664626SKris Kennaway 
27574664626SKris Kennaway #   define LIST_SEPARATOR_CHAR ':'
2765c87c606SMark Murray #   define EXIT(n)             exit(n)
27774664626SKris Kennaway #  endif
27874664626SKris Kennaway 
2795c87c606SMark Murray # endif
2805c87c606SMark Murray 
28174664626SKris Kennaway /***********************************************/
28274664626SKris Kennaway 
2836be8ae07SJacques Vidrine # if defined(OPENSSL_SYS_WINDOWS)
2846be8ae07SJacques Vidrine #  define strcasecmp _stricmp
2856be8ae07SJacques Vidrine #  define strncasecmp _strnicmp
286e71b7053SJung-uk Kim #  if (_MSC_VER >= 1310)
287e71b7053SJung-uk Kim #   define open _open
288e71b7053SJung-uk Kim #   define fdopen _fdopen
289e71b7053SJung-uk Kim #   define close _close
290e71b7053SJung-uk Kim #   ifndef strdup
291e71b7053SJung-uk Kim #    define strdup _strdup
2926be8ae07SJacques Vidrine #   endif
293e71b7053SJung-uk Kim #   define unlink _unlink
294e71b7053SJung-uk Kim #   define fileno _fileno
295e71b7053SJung-uk Kim #  endif
296e71b7053SJung-uk Kim # else
297e71b7053SJung-uk Kim #  include <strings.h>
29874664626SKris Kennaway # endif
2995c87c606SMark Murray 
3005c87c606SMark Murray /* vxworks */
3015c87c606SMark Murray # if defined(OPENSSL_SYS_VXWORKS)
3025c87c606SMark Murray #  include <ioLib.h>
3035c87c606SMark Murray #  include <tickLib.h>
3045c87c606SMark Murray #  include <sysLib.h>
3055c87c606SMark Murray #  include <vxWorks.h>
3065c87c606SMark Murray #  include <sockLib.h>
3075c87c606SMark Murray #  include <taskLib.h>
3085c87c606SMark Murray 
309e71b7053SJung-uk Kim #  define TTY_STRUCT int
310e71b7053SJung-uk Kim #  define sleep(a) taskDelay((a) * sysClkRateGet())
3115c87c606SMark Murray 
3126f9291ceSJung-uk Kim /*
3136f9291ceSJung-uk Kim  * NOTE: these are implemented by helpers in database app! if the database is
314e71b7053SJung-uk Kim  * not linked, we need to implement them elsewhere
3156f9291ceSJung-uk Kim  */
3165c87c606SMark Murray struct hostent *gethostbyname(const char *name);
3175c87c606SMark Murray struct hostent *gethostbyaddr(const char *addr, int length, int type);
3185c87c606SMark Murray struct servent *getservbyname(const char *name, const char *proto);
3195c87c606SMark Murray 
3205c87c606SMark Murray # endif
3215c87c606SMark Murray /* end vxworks */
32274664626SKris Kennaway 
323e71b7053SJung-uk Kim # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
324e71b7053SJung-uk Kim #  define CRYPTO_memcmp memcmp
32574664626SKris Kennaway # endif
32674664626SKris Kennaway 
327c9cf7b5cSJung-uk Kim /* unistd.h defines _POSIX_VERSION */
328c9cf7b5cSJung-uk Kim # if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
329c9cf7b5cSJung-uk Kim      && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
330c9cf7b5cSJung-uk Kim           || defined(__sun) || defined(__hpux) || defined(__sgi)      \
331c9cf7b5cSJung-uk Kim           || defined(__osf__) )
332c9cf7b5cSJung-uk Kim #  define OPENSSL_SECURE_MEMORY  /* secure memory is implemented */
333c9cf7b5cSJung-uk Kim # endif
33474664626SKris Kennaway #endif
335