xref: /freebsd/crypto/openssl/e_os.h (revision c9cf7b5c)
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
52f579bf8eSKris Kennaway 
5374664626SKris Kennaway /********************************************************************
5474664626SKris Kennaway  The Microsoft section
5574664626SKris Kennaway  ********************************************************************/
565c87c606SMark Murray # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
57f579bf8eSKris Kennaway #  define WIN32
58f579bf8eSKris Kennaway # endif
595c87c606SMark Murray # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
6074664626SKris Kennaway #  define WINDOWS
6174664626SKris Kennaway # endif
625c87c606SMark Murray # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
6374664626SKris Kennaway #  define MSDOS
6474664626SKris Kennaway # endif
6574664626SKris Kennaway 
6674664626SKris Kennaway # ifdef WIN32
67e71b7053SJung-uk Kim #  undef get_last_sys_error
68e71b7053SJung-uk Kim #  undef clear_sys_error
6974664626SKris Kennaway #  define get_last_sys_error()    GetLastError()
7074664626SKris Kennaway #  define clear_sys_error()       SetLastError(0)
7174664626SKris Kennaway #  if !defined(WINNT)
7274664626SKris Kennaway #   define WIN_CONSOLE_BUG
7374664626SKris Kennaway #  endif
7474664626SKris Kennaway # else
7574664626SKris Kennaway # endif
7674664626SKris Kennaway 
775c87c606SMark Murray # if (defined(WINDOWS) || defined(MSDOS))
785c87c606SMark Murray 
795c87c606SMark Murray #  ifdef __DJGPP__
805c87c606SMark Murray #   include <unistd.h>
815c87c606SMark Murray #   include <sys/stat.h>
825c87c606SMark Murray #   define _setmode setmode
835c87c606SMark Murray #   define _O_TEXT O_TEXT
845c87c606SMark Murray #   define _O_BINARY O_BINARY
85e71b7053SJung-uk Kim #   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
86e71b7053SJung-uk Kim #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
873b4e3dcbSSimon L. B. Nielsen #   undef DEVRANDOM
883b4e3dcbSSimon L. B. Nielsen #   define DEVRANDOM "/dev/urandom\x24"
895c87c606SMark Murray #  endif                        /* __DJGPP__ */
9074664626SKris Kennaway 
9174664626SKris Kennaway #  ifndef S_IFDIR
9274664626SKris Kennaway #   define S_IFDIR     _S_IFDIR
9374664626SKris Kennaway #  endif
9474664626SKris Kennaway 
9574664626SKris Kennaway #  ifndef S_IFMT
9674664626SKris Kennaway #   define S_IFMT      _S_IFMT
97f579bf8eSKris Kennaway #  endif
9874664626SKris Kennaway 
995c87c606SMark Murray #  if !defined(WINNT) && !defined(__DJGPP__)
10074664626SKris Kennaway #   define NO_SYSLOG
10174664626SKris Kennaway #  endif
10274664626SKris Kennaway 
10374664626SKris Kennaway #  ifdef WINDOWS
1043b4e3dcbSSimon L. B. Nielsen #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
1053b4e3dcbSSimon L. B. Nielsen        /*
1063b4e3dcbSSimon L. B. Nielsen         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
1073b4e3dcbSSimon L. B. Nielsen         * Most notably we ought to check for availability of each specific
108e71b7053SJung-uk Kim         * routine that was introduced after denoted _WIN32_WINNT with
109e71b7053SJung-uk Kim         * GetProcAddress(). Normally newer functions are masked with higher
110e71b7053SJung-uk Kim         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
111e71b7053SJung-uk Kim         * some module, you'd need to override _WIN32_WINNT definition in
112e71b7053SJung-uk Kim         * the target module in order to "reach for" prototypes, but replace
113e71b7053SJung-uk Kim         * calls to new functions with indirect calls. Alternatively it
114e71b7053SJung-uk Kim         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
115e71b7053SJung-uk Kim         * and check for current OS version instead.
1163b4e3dcbSSimon L. B. Nielsen         */
117e71b7053SJung-uk Kim #    define _WIN32_WINNT 0x0501
1183b4e3dcbSSimon L. B. Nielsen #   endif
119e71b7053SJung-uk Kim #   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
1201f13597dSJung-uk Kim        /*
1211f13597dSJung-uk Kim         * Just like defining _WIN32_WINNT including winsock2.h implies
1221f13597dSJung-uk Kim         * certain "discipline" for maintaining [broad] binary compatibility.
1231f13597dSJung-uk Kim         * As long as structures are invariant among Winsock versions,
1241f13597dSJung-uk Kim         * it's sufficient to check for specific Winsock2 API availability
1251f13597dSJung-uk Kim         * at run-time [DSO_global_lookup is recommended]...
1261f13597dSJung-uk Kim         */
1271f13597dSJung-uk Kim #    include <winsock2.h>
1281f13597dSJung-uk Kim #    include <ws2tcpip.h>
1291f13597dSJung-uk Kim        /* yes, they have to be #included prior to <windows.h> */
1301f13597dSJung-uk Kim #   endif
13174664626SKris Kennaway #   include <windows.h>
132db522d3aSSimon L. B. Nielsen #   include <stdio.h>
13374664626SKris Kennaway #   include <stddef.h>
13474664626SKris Kennaway #   include <errno.h>
1357bded2dbSJung-uk Kim #   if defined(_WIN32_WCE) && !defined(EACCES)
1367bded2dbSJung-uk Kim #    define EACCES   13
1377bded2dbSJung-uk Kim #   endif
13874664626SKris Kennaway #   include <string.h>
1393b4e3dcbSSimon L. B. Nielsen #   ifdef _WIN64
1403b4e3dcbSSimon L. B. Nielsen #    define strlen(s) _strlen31(s)
1413b4e3dcbSSimon L. B. Nielsen /* cut strings to 2GB */
142751d2991SJung-uk Kim static __inline unsigned int _strlen31(const char *str)
1433b4e3dcbSSimon L. B. Nielsen {
1443b4e3dcbSSimon L. B. Nielsen     unsigned int len = 0;
1456f9291ceSJung-uk Kim     while (*str && len < 0x80000000U)
1466f9291ceSJung-uk Kim         str++, len++;
1473b4e3dcbSSimon L. B. Nielsen     return len & 0x7FFFFFFF;
1483b4e3dcbSSimon L. B. Nielsen }
1493b4e3dcbSSimon L. B. Nielsen #   endif
15074664626SKris Kennaway #   include <malloc.h>
1517bded2dbSJung-uk Kim #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
1527bded2dbSJung-uk Kim #    if _MSC_VER>=1300 && _MSC_VER<1600
153db522d3aSSimon L. B. Nielsen #     undef stdin
154db522d3aSSimon L. B. Nielsen #     undef stdout
155db522d3aSSimon L. B. Nielsen #     undef stderr
156db522d3aSSimon L. B. Nielsen FILE *__iob_func();
157db522d3aSSimon L. B. Nielsen #     define stdin  (&__iob_func()[0])
158db522d3aSSimon L. B. Nielsen #     define stdout (&__iob_func()[1])
159db522d3aSSimon L. B. Nielsen #     define stderr (&__iob_func()[2])
1607bded2dbSJung-uk Kim #    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
161db522d3aSSimon L. B. Nielsen #     undef stdin
162db522d3aSSimon L. B. Nielsen #     undef stdout
163db522d3aSSimon L. B. Nielsen #     undef stderr
1646f9291ceSJung-uk Kim          /*
1656f9291ceSJung-uk Kim           * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
1666f9291ceSJung-uk Kim           * or in other words with /MD. Declaring implicit import, i.e. with
1676f9291ceSJung-uk Kim           * _imp_ prefix, works correctly with all compiler options, but
1686f9291ceSJung-uk Kim           * without /MD results in LINK warning LNK4049: 'locally defined
1696f9291ceSJung-uk Kim           * symbol "__iob" imported'.
170db522d3aSSimon L. B. Nielsen           */
171db522d3aSSimon L. B. Nielsen extern FILE *_imp___iob;
172db522d3aSSimon L. B. Nielsen #     define stdin  (&_imp___iob[0])
173db522d3aSSimon L. B. Nielsen #     define stdout (&_imp___iob[1])
174db522d3aSSimon L. B. Nielsen #     define stderr (&_imp___iob[2])
175db522d3aSSimon L. B. Nielsen #    endif
176db522d3aSSimon L. B. Nielsen #   endif
17774664626SKris Kennaway #  endif
17874664626SKris Kennaway #  include <io.h>
17974664626SKris Kennaway #  include <fcntl.h>
18074664626SKris Kennaway 
1815c87c606SMark Murray #  ifdef OPENSSL_SYS_WINCE
1821f13597dSJung-uk Kim #   define OPENSSL_NO_POSIX_IO
1835c87c606SMark Murray #  endif
1845c87c606SMark Murray 
18550ef0093SJacques Vidrine #  define EXIT(n) exit(n)
18674664626SKris Kennaway #  define LIST_SEPARATOR_CHAR ';'
18774664626SKris Kennaway #  ifndef W_OK
18874664626SKris Kennaway #   define W_OK        2
18974664626SKris Kennaway #  endif
19074664626SKris Kennaway #  ifndef R_OK
19174664626SKris Kennaway #   define R_OK        4
19274664626SKris Kennaway #  endif
1935c87c606SMark Murray #  ifdef OPENSSL_SYS_WINCE
1945c87c606SMark Murray #   define DEFAULT_HOME  ""
1955c87c606SMark Murray #  else
19626d191b4SKris Kennaway #   define DEFAULT_HOME  "C:"
1975c87c606SMark Murray #  endif
19874664626SKris Kennaway 
199dc2b908fSJung-uk Kim /* Avoid Visual Studio 13 GetVersion deprecated problems */
200560ede85SJung-uk Kim #  if defined(_MSC_VER) && _MSC_VER>=1800
201560ede85SJung-uk Kim #   define check_winnt() (1)
202dc2b908fSJung-uk Kim #   define check_win_minplat(x) (1)
203560ede85SJung-uk Kim #  else
204560ede85SJung-uk Kim #   define check_winnt() (GetVersion() < 0x80000000)
205dc2b908fSJung-uk Kim #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
206560ede85SJung-uk Kim #  endif
207560ede85SJung-uk Kim 
2081f13597dSJung-uk Kim # else                          /* The non-microsoft world */
20974664626SKris Kennaway 
210e71b7053SJung-uk Kim #  if defined(OPENSSL_SYS_VXWORKS)
211e71b7053SJung-uk Kim #   include <sys/times.h>
212e71b7053SJung-uk Kim #  else
213e71b7053SJung-uk Kim #   include <sys/time.h>
214e71b7053SJung-uk Kim #  endif
215e71b7053SJung-uk Kim 
2165c87c606SMark Murray #  ifdef OPENSSL_SYS_VMS
21774664626SKris Kennaway #   define VMS 1
2186f9291ceSJung-uk Kim   /*
2196f9291ceSJung-uk Kim    * some programs don't include stdlib, so exit() and others give implicit
2206f9291ceSJung-uk Kim    * function warnings
2216f9291ceSJung-uk Kim    */
22274664626SKris Kennaway #   include <stdlib.h>
22374664626SKris Kennaway #   if defined(__DECC)
22474664626SKris Kennaway #    include <unistd.h>
22574664626SKris Kennaway #   else
22674664626SKris Kennaway #    include <unixlib.h>
22774664626SKris Kennaway #   endif
22874664626SKris Kennaway #   define LIST_SEPARATOR_CHAR ','
2295c87c606SMark Murray   /* We don't have any well-defined random devices on VMS, yet... */
2305c87c606SMark Murray #   undef DEVRANDOM
2316f9291ceSJung-uk Kim   /*-
2326f9291ceSJung-uk Kim      We need to do this since VMS has the following coding on status codes:
233f579bf8eSKris Kennaway 
234f579bf8eSKris Kennaway      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
235f579bf8eSKris Kennaway                The important thing to know is that odd numbers are considered
236f579bf8eSKris Kennaway                good, while even ones are considered errors.
237f579bf8eSKris Kennaway      Bits 3-15: actual status number
238f579bf8eSKris Kennaway      Bits 16-27: facility number.  0 is considered "unknown"
239f579bf8eSKris Kennaway      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
240f579bf8eSKris Kennaway                  output the message (which, for random codes, just looks ugly)
241f579bf8eSKris Kennaway 
242f579bf8eSKris Kennaway      So, what we do here is to change 0 to 1 to get the default success status,
243f579bf8eSKris Kennaway      and everything else is shifted up to fit into the status number field, and
244e71b7053SJung-uk Kim      the status is tagged as an error, which is what is wanted here.
245e71b7053SJung-uk Kim 
246e71b7053SJung-uk Kim      Finally, we add the VMS C facility code 0x35a000, because there are some
247e71b7053SJung-uk Kim      programs, such as Perl, that will reinterpret the code back to something
248c9cf7b5cSJung-uk Kim      POSIX.  'man perlvms' explains it further.
249e71b7053SJung-uk Kim 
250e71b7053SJung-uk Kim      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
251e71b7053SJung-uk Kim      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
252e71b7053SJung-uk Kim      status type doesn't seem to bother Perl.
253f579bf8eSKris Kennaway      -- Richard Levitte
254f579bf8eSKris Kennaway   */
255e71b7053SJung-uk Kim #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
2563b4e3dcbSSimon L. B. Nielsen 
257e71b7053SJung-uk Kim #   define DEFAULT_HOME "SYS$LOGIN:"
2583b4e3dcbSSimon L. B. Nielsen 
25974664626SKris Kennaway #  else
26074664626SKris Kennaway      /* !defined VMS */
261f579bf8eSKris Kennaway #   ifdef OPENSSL_UNISTD
26274664626SKris Kennaway #    include OPENSSL_UNISTD
263f579bf8eSKris Kennaway #   else
264f579bf8eSKris Kennaway #    include <unistd.h>
265f579bf8eSKris Kennaway #   endif
266f579bf8eSKris Kennaway #   include <sys/types.h>
2671f13597dSJung-uk Kim #   ifdef OPENSSL_SYS_WIN32_CYGWIN
2681f13597dSJung-uk Kim #    include <io.h>
2691f13597dSJung-uk Kim #    include <fcntl.h>
2701f13597dSJung-uk Kim #   endif
27174664626SKris Kennaway 
27274664626SKris Kennaway #   define LIST_SEPARATOR_CHAR ':'
2735c87c606SMark Murray #   define EXIT(n)             exit(n)
27474664626SKris Kennaway #  endif
27574664626SKris Kennaway 
2765c87c606SMark Murray # endif
2775c87c606SMark Murray 
27874664626SKris Kennaway /***********************************************/
27974664626SKris Kennaway 
2806be8ae07SJacques Vidrine # if defined(OPENSSL_SYS_WINDOWS)
2816be8ae07SJacques Vidrine #  define strcasecmp _stricmp
2826be8ae07SJacques Vidrine #  define strncasecmp _strnicmp
283e71b7053SJung-uk Kim #  if (_MSC_VER >= 1310)
284e71b7053SJung-uk Kim #   define open _open
285e71b7053SJung-uk Kim #   define fdopen _fdopen
286e71b7053SJung-uk Kim #   define close _close
287e71b7053SJung-uk Kim #   ifndef strdup
288e71b7053SJung-uk Kim #    define strdup _strdup
2896be8ae07SJacques Vidrine #   endif
290e71b7053SJung-uk Kim #   define unlink _unlink
291e71b7053SJung-uk Kim #   define fileno _fileno
292e71b7053SJung-uk Kim #  endif
293e71b7053SJung-uk Kim # else
294e71b7053SJung-uk Kim #  include <strings.h>
29574664626SKris Kennaway # endif
2965c87c606SMark Murray 
2975c87c606SMark Murray /* vxworks */
2985c87c606SMark Murray # if defined(OPENSSL_SYS_VXWORKS)
2995c87c606SMark Murray #  include <ioLib.h>
3005c87c606SMark Murray #  include <tickLib.h>
3015c87c606SMark Murray #  include <sysLib.h>
3025c87c606SMark Murray #  include <vxWorks.h>
3035c87c606SMark Murray #  include <sockLib.h>
3045c87c606SMark Murray #  include <taskLib.h>
3055c87c606SMark Murray 
306e71b7053SJung-uk Kim #  define TTY_STRUCT int
307e71b7053SJung-uk Kim #  define sleep(a) taskDelay((a) * sysClkRateGet())
3085c87c606SMark Murray 
3096f9291ceSJung-uk Kim /*
3106f9291ceSJung-uk Kim  * NOTE: these are implemented by helpers in database app! if the database is
311e71b7053SJung-uk Kim  * not linked, we need to implement them elsewhere
3126f9291ceSJung-uk Kim  */
3135c87c606SMark Murray struct hostent *gethostbyname(const char *name);
3145c87c606SMark Murray struct hostent *gethostbyaddr(const char *addr, int length, int type);
3155c87c606SMark Murray struct servent *getservbyname(const char *name, const char *proto);
3165c87c606SMark Murray 
3175c87c606SMark Murray # endif
3185c87c606SMark Murray /* end vxworks */
31974664626SKris Kennaway 
320e71b7053SJung-uk Kim # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
321e71b7053SJung-uk Kim #  define CRYPTO_memcmp memcmp
32274664626SKris Kennaway # endif
32374664626SKris Kennaway 
324c9cf7b5cSJung-uk Kim /* unistd.h defines _POSIX_VERSION */
325c9cf7b5cSJung-uk Kim # if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
326c9cf7b5cSJung-uk Kim      && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
327c9cf7b5cSJung-uk Kim           || defined(__sun) || defined(__hpux) || defined(__sgi)      \
328c9cf7b5cSJung-uk Kim           || defined(__osf__) )
329c9cf7b5cSJung-uk Kim #  define OPENSSL_SECURE_MEMORY  /* secure memory is implemented */
330c9cf7b5cSJung-uk Kim # endif
33174664626SKris Kennaway #endif
332