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