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