1 | /*
|
---|
2 | * Copyright 1995-2022 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 | # ifndef IN_RING0 /* bird */
|
---|
102 | # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
|
---|
103 | /*
|
---|
104 | * Just like defining _WIN32_WINNT including winsock2.h implies
|
---|
105 | * certain "discipline" for maintaining [broad] binary compatibility.
|
---|
106 | * As long as structures are invariant among Winsock versions,
|
---|
107 | * it's sufficient to check for specific Winsock2 API availability
|
---|
108 | * at run-time [DSO_global_lookup is recommended]...
|
---|
109 | */
|
---|
110 | # ifdef VBOX
|
---|
111 | # include <iprt/win/winsock2.h>
|
---|
112 | # include <iprt/win/ws2tcpip.h>
|
---|
113 | # endif
|
---|
114 | # include <winsock2.h>
|
---|
115 | # include <ws2tcpip.h>
|
---|
116 | /*
|
---|
117 | * Clang-based C++Builder 10.3.3 toolchains cannot find C inline
|
---|
118 | * definitions at link-time. This header defines WspiapiLoad() as an
|
---|
119 | * __inline function. https://quality.embarcadero.com/browse/RSP-33806
|
---|
120 | */
|
---|
121 | # if !defined(__BORLANDC__) || !defined(__clang__)
|
---|
122 | # include <wspiapi.h>
|
---|
123 | # endif
|
---|
124 | /* yes, they have to be #included prior to <windows.h> */
|
---|
125 | # endif
|
---|
126 | # ifdef VBOX
|
---|
127 | # include <iprt/win/windows.h>
|
---|
128 | # else
|
---|
129 | # include <windows.h>
|
---|
130 | # endif
|
---|
131 | # endif /* bird */
|
---|
132 | # include <stdio.h>
|
---|
133 | # include <stddef.h>
|
---|
134 | # include <errno.h>
|
---|
135 | # if defined(_WIN32_WCE) && !defined(EACCES)
|
---|
136 | # define EACCES 13
|
---|
137 | # endif
|
---|
138 | # include <string.h>
|
---|
139 | # ifdef _WIN64
|
---|
140 | # define strlen(s) _strlen31(s)
|
---|
141 | /* cut strings to 2GB */
|
---|
142 | # ifndef VBOX
|
---|
143 | static __inline unsigned int _strlen31(const char *str)
|
---|
144 | # else
|
---|
145 | static __inline size_t _strlen31(const char *str)
|
---|
146 | # endif
|
---|
147 | {
|
---|
148 | unsigned int len = 0;
|
---|
149 | while (*str && len < 0x80000000U)
|
---|
150 | str++, len++;
|
---|
151 | return len & 0x7FFFFFFF;
|
---|
152 | }
|
---|
153 | # endif
|
---|
154 | # include <malloc.h>
|
---|
155 | # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
|
---|
156 | # if _MSC_VER>=1300 && _MSC_VER<1600
|
---|
157 | # undef stdin
|
---|
158 | # undef stdout
|
---|
159 | # undef stderr
|
---|
160 | FILE * __cdecl __iob_func(); /* vbox/bird: added __cdecl */
|
---|
161 | # define stdin (&__iob_func()[0])
|
---|
162 | # define stdout (&__iob_func()[1])
|
---|
163 | # define stderr (&__iob_func()[2])
|
---|
164 | # endif
|
---|
165 | # endif
|
---|
166 | # endif
|
---|
167 | # include <io.h>
|
---|
168 | # include <fcntl.h>
|
---|
169 |
|
---|
170 | # ifdef OPENSSL_SYS_WINCE
|
---|
171 | # define OPENSSL_NO_POSIX_IO
|
---|
172 | # endif
|
---|
173 |
|
---|
174 | # define EXIT(n) exit(n)
|
---|
175 | # define LIST_SEPARATOR_CHAR ';'
|
---|
176 | # ifndef W_OK
|
---|
177 | # define W_OK 2
|
---|
178 | # endif
|
---|
179 | # ifndef R_OK
|
---|
180 | # define R_OK 4
|
---|
181 | # endif
|
---|
182 | # ifdef OPENSSL_SYS_WINCE
|
---|
183 | # define DEFAULT_HOME ""
|
---|
184 | # else
|
---|
185 | # define DEFAULT_HOME "C:"
|
---|
186 | # endif
|
---|
187 |
|
---|
188 | /* Avoid Visual Studio 13 GetVersion deprecated problems */
|
---|
189 | # if defined(_MSC_VER) && _MSC_VER>=1800
|
---|
190 | # define check_winnt() (1)
|
---|
191 | # define check_win_minplat(x) (1)
|
---|
192 | # else
|
---|
193 | # define check_winnt() (GetVersion() < 0x80000000)
|
---|
194 | # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
|
---|
195 | # endif
|
---|
196 |
|
---|
197 | # else /* The non-microsoft world */
|
---|
198 |
|
---|
199 | # if defined(OPENSSL_SYS_VXWORKS)
|
---|
200 | # include <time.h>
|
---|
201 | # else
|
---|
202 | # include <sys/time.h>
|
---|
203 | # endif
|
---|
204 |
|
---|
205 | # ifdef OPENSSL_SYS_VMS
|
---|
206 | # define VMS 1
|
---|
207 | /*
|
---|
208 | * some programs don't include stdlib, so exit() and others give implicit
|
---|
209 | * function warnings
|
---|
210 | */
|
---|
211 | # include <stdlib.h>
|
---|
212 | # if defined(__DECC)
|
---|
213 | # include <unistd.h>
|
---|
214 | # else
|
---|
215 | # include <unixlib.h>
|
---|
216 | # endif
|
---|
217 | # define LIST_SEPARATOR_CHAR ','
|
---|
218 | /* We don't have any well-defined random devices on VMS, yet... */
|
---|
219 | # undef DEVRANDOM
|
---|
220 | /*-
|
---|
221 | We need to do this since VMS has the following coding on status codes:
|
---|
222 |
|
---|
223 | Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
|
---|
224 | The important thing to know is that odd numbers are considered
|
---|
225 | good, while even ones are considered errors.
|
---|
226 | Bits 3-15: actual status number
|
---|
227 | Bits 16-27: facility number. 0 is considered "unknown"
|
---|
228 | Bits 28-31: control bits. If bit 28 is set, the shell won't try to
|
---|
229 | output the message (which, for random codes, just looks ugly)
|
---|
230 |
|
---|
231 | So, what we do here is to change 0 to 1 to get the default success status,
|
---|
232 | and everything else is shifted up to fit into the status number field, and
|
---|
233 | the status is tagged as an error, which is what is wanted here.
|
---|
234 |
|
---|
235 | Finally, we add the VMS C facility code 0x35a000, because there are some
|
---|
236 | programs, such as Perl, that will reinterpret the code back to something
|
---|
237 | POSIX. 'man perlvms' explains it further.
|
---|
238 |
|
---|
239 | NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
|
---|
240 | codes (status type = 1). I couldn't disagree more. Fortunately, the
|
---|
241 | status type doesn't seem to bother Perl.
|
---|
242 | -- Richard Levitte
|
---|
243 | */
|
---|
244 | # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
|
---|
245 |
|
---|
246 | # define DEFAULT_HOME "SYS$LOGIN:"
|
---|
247 |
|
---|
248 | # else
|
---|
249 | /* !defined VMS */
|
---|
250 | # include <unistd.h>
|
---|
251 | # include <sys/types.h>
|
---|
252 | # ifdef OPENSSL_SYS_WIN32_CYGWIN
|
---|
253 | # include <io.h>
|
---|
254 | # include <fcntl.h>
|
---|
255 | # endif
|
---|
256 |
|
---|
257 | # define LIST_SEPARATOR_CHAR ':'
|
---|
258 | # define EXIT(n) exit(n)
|
---|
259 | # endif
|
---|
260 |
|
---|
261 | # endif
|
---|
262 |
|
---|
263 | /***********************************************/
|
---|
264 |
|
---|
265 | # if defined(OPENSSL_SYS_WINDOWS)
|
---|
266 | # if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
|
---|
267 | # define open _open
|
---|
268 | # define fdopen _fdopen
|
---|
269 | # define close _close
|
---|
270 | # ifndef strdup
|
---|
271 | # define strdup _strdup
|
---|
272 | # endif
|
---|
273 | # define unlink _unlink
|
---|
274 | # define fileno _fileno
|
---|
275 | # endif
|
---|
276 | # else
|
---|
277 | # include <strings.h>
|
---|
278 | # endif
|
---|
279 |
|
---|
280 | /* vxworks */
|
---|
281 | # if defined(OPENSSL_SYS_VXWORKS)
|
---|
282 | # include <ioLib.h>
|
---|
283 | # include <tickLib.h>
|
---|
284 | # include <sysLib.h>
|
---|
285 | # include <vxWorks.h>
|
---|
286 | # include <sockLib.h>
|
---|
287 | # include <taskLib.h>
|
---|
288 |
|
---|
289 | # define TTY_STRUCT int
|
---|
290 | # define sleep(a) taskDelay((a) * sysClkRateGet())
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * NOTE: these are implemented by helpers in database app! if the database is
|
---|
294 | * not linked, we need to implement them elsewhere
|
---|
295 | */
|
---|
296 | struct hostent *gethostbyname(const char *name);
|
---|
297 | struct hostent *gethostbyaddr(const char *addr, int length, int type);
|
---|
298 | struct servent *getservbyname(const char *name, const char *proto);
|
---|
299 |
|
---|
300 | # endif
|
---|
301 | /* end vxworks */
|
---|
302 |
|
---|
303 | /* system-specific variants defining ossl_sleep() */
|
---|
304 | #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
|
---|
305 | # include <unistd.h>
|
---|
306 | static ossl_inline void ossl_sleep(unsigned long millis)
|
---|
307 | {
|
---|
308 | # ifdef OPENSSL_SYS_VXWORKS
|
---|
309 | struct timespec ts;
|
---|
310 | ts.tv_sec = (long int) (millis / 1000);
|
---|
311 | ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
|
---|
312 | nanosleep(&ts, NULL);
|
---|
313 | # elif defined(__TANDEM)
|
---|
314 | # if !defined(_REENTRANT)
|
---|
315 | # include <cextdecs.h(PROCESS_DELAY_)>
|
---|
316 | /* HPNS does not support usleep for non threaded apps */
|
---|
317 | PROCESS_DELAY_(millis * 1000);
|
---|
318 | # elif defined(_SPT_MODEL_)
|
---|
319 | # include <spthread.h>
|
---|
320 | # include <spt_extensions.h>
|
---|
321 | usleep(millis * 1000);
|
---|
322 | # else
|
---|
323 | usleep(millis * 1000);
|
---|
324 | # endif
|
---|
325 | # else
|
---|
326 | usleep(millis * 1000);
|
---|
327 | # endif
|
---|
328 | }
|
---|
329 | #elif defined(_WIN32)
|
---|
330 | # include <windows.h>
|
---|
331 | static ossl_inline void ossl_sleep(unsigned long millis)
|
---|
332 | {
|
---|
333 | Sleep(millis);
|
---|
334 | }
|
---|
335 | #else
|
---|
336 | /* Fallback to a busy wait */
|
---|
337 | static ossl_inline void ossl_sleep(unsigned long millis)
|
---|
338 | {
|
---|
339 | struct timeval start, now;
|
---|
340 | unsigned long elapsedms;
|
---|
341 |
|
---|
342 | gettimeofday(&start, NULL);
|
---|
343 | do {
|
---|
344 | gettimeofday(&now, NULL);
|
---|
345 | elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
|
---|
346 | + now.tv_usec - start.tv_usec) / 1000;
|
---|
347 | } while (elapsedms < millis);
|
---|
348 | }
|
---|
349 | #endif /* defined OPENSSL_SYS_UNIX */
|
---|
350 |
|
---|
351 | /* ----------------------------- HP NonStop -------------------------------- */
|
---|
352 | /* Required to support platform variant without getpid() and pid_t. */
|
---|
353 | # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
|
---|
354 | # include <strings.h>
|
---|
355 | # include <netdb.h>
|
---|
356 | # define getservbyname(name,proto) getservbyname((char*)name,proto)
|
---|
357 | # define gethostbyname(name) gethostbyname((char*)name)
|
---|
358 | # define ioctlsocket(a,b,c) ioctl(a,b,c)
|
---|
359 | # ifdef NO_GETPID
|
---|
360 | inline int nssgetpid();
|
---|
361 | # ifndef NSSGETPID_MACRO
|
---|
362 | # define NSSGETPID_MACRO
|
---|
363 | # include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
|
---|
364 | # include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
|
---|
365 | inline int nssgetpid()
|
---|
366 | {
|
---|
367 | short phandle[10]={0};
|
---|
368 | union pseudo_pid {
|
---|
369 | struct {
|
---|
370 | short cpu;
|
---|
371 | short pin;
|
---|
372 | } cpu_pin ;
|
---|
373 | int ppid;
|
---|
374 | } ppid = { 0 };
|
---|
375 | PROCESSHANDLE_GETMINE_(phandle);
|
---|
376 | PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
|
---|
377 | return ppid.ppid;
|
---|
378 | }
|
---|
379 | # define getpid(a) nssgetpid(a)
|
---|
380 | # endif /* NSSGETPID_MACRO */
|
---|
381 | # endif /* NO_GETPID */
|
---|
382 | /*# define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
|
---|
383 | /*# define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
|
---|
384 | /*# define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
|
---|
385 | /*# define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
|
---|
386 | /*# define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
|
---|
387 | # if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
|
---|
388 | /*
|
---|
389 | * HPNS SPT threads
|
---|
390 | */
|
---|
391 | # define SPT_THREAD_SIGNAL 1
|
---|
392 | # define SPT_THREAD_AWARE 1
|
---|
393 | # include <spthread.h>
|
---|
394 | # undef close
|
---|
395 | # define close spt_close
|
---|
396 | /*
|
---|
397 | # define get_last_socket_error() errno
|
---|
398 | # define clear_socket_error() errno=0
|
---|
399 | # define ioctlsocket(a,b,c) ioctl(a,b,c)
|
---|
400 | # define closesocket(s) close(s)
|
---|
401 | # define readsocket(s,b,n) read((s),(char*)(b),(n))
|
---|
402 | # define writesocket(s,b,n) write((s),(char*)(b),(n)
|
---|
403 | */
|
---|
404 | # define accept(a,b,c) accept(a,(struct sockaddr *)b,c)
|
---|
405 | # define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
|
---|
406 | # endif
|
---|
407 | # endif
|
---|
408 |
|
---|
409 | # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
---|
410 | # define CRYPTO_memcmp memcmp
|
---|
411 | # endif
|
---|
412 |
|
---|
413 | # ifndef OPENSSL_NO_SECURE_MEMORY
|
---|
414 | /* unistd.h defines _POSIX_VERSION */
|
---|
415 | # if (defined(OPENSSL_SYS_UNIX) \
|
---|
416 | && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
|
---|
417 | || defined(__sun) || defined(__hpux) || defined(__sgi) \
|
---|
418 | || defined(__osf__) )) \
|
---|
419 | || defined(_WIN32)
|
---|
420 | /* secure memory is implemented */
|
---|
421 | # else
|
---|
422 | # define OPENSSL_NO_SECURE_MEMORY
|
---|
423 | # endif
|
---|
424 | # endif
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
|
---|
428 | * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
|
---|
429 | * There are also equivalent functions on Windows.
|
---|
430 | * There is no locale_t on NONSTOP.
|
---|
431 | */
|
---|
432 | # if defined(OPENSSL_SYS_WINDOWS)
|
---|
433 | # define locale_t _locale_t
|
---|
434 | # define freelocale _free_locale
|
---|
435 | # define strcasecmp_l _stricmp_l
|
---|
436 | # define strncasecmp_l _strnicmp_l
|
---|
437 | # define strcasecmp _stricmp
|
---|
438 | # define strncasecmp _strnicmp
|
---|
439 | # elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
|
---|
440 | || defined(OPENSSL_SYS_TANDEM)
|
---|
441 | # ifndef OPENSSL_NO_LOCALE
|
---|
442 | # define OPENSSL_NO_LOCALE
|
---|
443 | # endif
|
---|
444 | # endif
|
---|
445 |
|
---|
446 | #endif
|
---|