2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
62 #include <openssl/opensslconf.h>
64 #include <openssl/e_os2.h>
65 /* <openssl/e_os2.h> contains what we can justify to make visible
66 * to the outside; this file e_os.h is not part of the exported
73 /* Used to checking reference counts, most while doing perl5 stuff :-) */
76 #define REF_PRINT(a,b) fprintf(stderr,"%08X:%4d:%s\n",(int)b,b->references,a)
80 /* set this to a comma-separated list of 'random' device files to try out.
81 * My default, we will try to read at least one of these files */
82 #define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
85 /* set this to a comma-separated list of 'egd' sockets to try out. These
86 * sockets will be tried in the order listed in case accessing the device files
87 * listed in DEVRANDOM did not return enough entropy. */
88 #define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
91 #if defined(OPENSSL_SYS_VXWORKS)
92 # define NO_SYS_PARAM_H
97 /********************************************************************
99 ********************************************************************/
100 /* The following is used because of the small stack in some
101 * Microsoft operating systems */
102 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYSNAME_WIN32)
103 # define MS_STATIC static
108 #if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
111 #if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
114 #if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
118 #if defined(MSDOS) && !defined(GETPID_IS_MEANINGLESS)
119 # define GETPID_IS_MEANINGLESS
124 #define get_last_sys_error() GetLastError()
125 #define clear_sys_error() SetLastError(0)
127 #define WIN_CONSOLE_BUG
130 #define get_last_sys_error() errno
131 #define clear_sys_error() errno=0
135 #define get_last_socket_error() WSAGetLastError()
136 #define clear_socket_error() WSASetLastError(0)
137 #define readsocket(s,b,n) recv((s),(b),(n),0)
138 #define writesocket(s,b,n) send((s),(b),(n),0)
139 #elif defined(__DJGPP__)
141 #define get_last_socket_error() errno
142 #define clear_socket_error() errno=0
143 #define closesocket(s) close_s(s)
144 #define readsocket(s,b,n) read_s(s,b,n)
145 #define writesocket(s,b,n) send(s,b,n,0)
146 #elif defined(OPENSSL_SYS_VMS)
147 #define get_last_socket_error() errno
148 #define clear_socket_error() errno=0
149 #define ioctlsocket(a,b,c) ioctl(a,b,c)
150 #define closesocket(s) close(s)
151 #define readsocket(s,b,n) recv((s),(b),(n),0)
152 #define writesocket(s,b,n) send((s),(b),(n),0)
153 #elif defined(OPENSSL_SYS_VXWORKS)
154 #define get_last_socket_error() errno
155 #define clear_socket_error() errno=0
156 #define ioctlsocket(a,b,c) ioctl((a),(b),(int)(c))
157 #define closesocket(s) close(s)
158 #define readsocket(s,b,n) read((s),(b),(n))
159 #define writesocket(s,b,n) write((s),(char *)(b),(n))
160 #elif defined(OPENSSL_SYS_NETWARE)
161 #if defined(NETWARE_BSDSOCK)
162 #define get_last_socket_error() errno
163 #define clear_socket_error() errno=0
164 #define closesocket(s) close(s)
165 #define ioctlsocket(a,b,c) ioctl(a,b,c)
166 #if defined(NETWARE_LIBC)
167 #define readsocket(s,b,n) recv((s),(b),(n),0)
168 #define writesocket(s,b,n) send((s),(b),(n),0)
170 #define readsocket(s,b,n) recv((s),(char*)(b),(n),0)
171 #define writesocket(s,b,n) send((s),(char*)(b),(n),0)
174 #define get_last_socket_error() WSAGetLastError()
175 #define clear_socket_error() WSASetLastError(0)
176 #define readsocket(s,b,n) recv((s),(b),(n),0)
177 #define writesocket(s,b,n) send((s),(b),(n),0)
180 #define get_last_socket_error() errno
181 #define clear_socket_error() errno=0
182 #define ioctlsocket(a,b,c) ioctl(a,b,c)
183 #define closesocket(s) close(s)
184 #define readsocket(s,b,n) read((s),(b),(n))
185 #define writesocket(s,b,n) write((s),(b),(n))
188 #ifdef WIN16 /* never the case */
189 # define MS_CALLBACK _far _loadds
196 #ifdef OPENSSL_NO_STDIO
197 # undef OPENSSL_NO_FP_API
198 # define OPENSSL_NO_FP_API
201 #if (defined(WINDOWS) || defined(MSDOS))
205 # include <sys/stat.h>
206 # include <sys/socket.h>
209 # define _setmode setmode
210 # define _O_TEXT O_TEXT
211 # define _O_BINARY O_BINARY
213 # define DEVRANDOM "/dev/urandom\x24"
214 # endif /* __DJGPP__ */
217 # define S_IFDIR _S_IFDIR
221 # define S_IFMT _S_IFMT
224 # if !defined(WINNT) && !defined(__DJGPP__)
230 # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
232 * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
233 * Most notably we ought to check for availability of each specific
234 * routine with GetProcAddress() and/or guard NT-specific calls with
235 * GetVersion() < 0x80000000. One can argue that in latter "or" case
236 * we ought to /DELAYLOAD some .DLLs in order to protect ourselves
237 * against run-time link errors. This doesn't seem to be necessary,
238 * because it turned out that already Windows 95, first non-NT Win32
239 * implementation, is equipped with at least NT 3.51 stubs, dummy
240 * routines with same name, but which do nothing. Meaning that it's
241 * apparently sufficient to guard "vanilla" NT calls with GetVersion
242 * alone, while NT 4.0 and above interfaces ought to be linked with
243 * GetProcAddress at run-time.
245 # define _WIN32_WINNT 0x0400
247 # if !defined(OPENSSL_NO_SOCK) && (defined(_WIN32_WINNT) || defined(_WIN32_WCE))
249 * Just like defining _WIN32_WINNT including winsock2.h implies
250 * certain "discipline" for maintaining [broad] binary compatibility.
251 * As long as structures are invariant among Winsock versions,
252 * it's sufficient to check for specific Winsock2 API availability
253 * at run-time [DSO_global_lookup is recommended]...
255 # include <winsock2.h>
256 # include <ws2tcpip.h>
257 /* yes, they have to be #included prior to <windows.h> */
259 # include <windows.h>
263 # if defined(_WIN32_WCE) && !defined(EACCES)
268 # define strlen(s) _strlen31(s)
269 /* cut strings to 2GB */
270 static __inline unsigned int _strlen31(const char *str)
273 while (*str && len<0x80000000U) str++, len++;
274 return len&0x7FFFFFFF;
278 # if defined(_MSC_VER) && _MSC_VER<=1200 && defined(_MT) && defined(isspace)
279 /* compensate for bug in VC6 ctype.h */
286 # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
287 # if _MSC_VER>=1300 && _MSC_VER<1600
292 # define stdin (&__iob_func()[0])
293 # define stdout (&__iob_func()[1])
294 # define stderr (&__iob_func()[2])
295 # elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
299 /* pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
300 * or in other words with /MD. Declaring implicit import, i.e.
301 * with _imp_ prefix, works correctly with all compiler options,
302 * but without /MD results in LINK warning LNK4049:
303 * 'locally defined symbol "__iob" imported'.
305 extern FILE *_imp___iob;
306 # define stdin (&_imp___iob[0])
307 # define stdout (&_imp___iob[1])
308 # define stderr (&_imp___iob[2])
315 # ifdef OPENSSL_SYS_WINCE
316 # define OPENSSL_NO_POSIX_IO
319 # if defined (__BORLANDC__)
320 # define _setmode setmode
321 # define _O_TEXT O_TEXT
322 # define _O_BINARY O_BINARY
323 # define _int64 __int64
324 # define _kbhit kbhit
327 # define EXIT(n) exit(n)
328 # define LIST_SEPARATOR_CHAR ';'
338 # define OPENSSL_CONF "openssl.cnf"
339 # define SSLEAY_CONF OPENSSL_CONF
340 # define NUL_DEV "nul"
341 # define RFILE ".rnd"
342 # ifdef OPENSSL_SYS_WINCE
343 # define DEFAULT_HOME ""
345 # define DEFAULT_HOME "C:"
348 /* Avoid Windows 8 SDK GetVersion deprecated problems */
349 #if defined(_MSC_VER) && _MSC_VER>=1800
350 # define check_winnt() (1)
352 # define check_winnt() (GetVersion() < 0x80000000)
355 #else /* The non-microsoft world */
357 # ifdef OPENSSL_SYS_VMS
359 /* some programs don't include stdlib, so exit() and others give implicit
365 # include <unixlib.h>
367 # define OPENSSL_CONF "openssl.cnf"
368 # define SSLEAY_CONF OPENSSL_CONF
369 # define RFILE ".rnd"
370 # define LIST_SEPARATOR_CHAR ','
371 # define NUL_DEV "NLA0:"
372 /* We don't have any well-defined random devices on VMS, yet... */
374 /* We need to do this since VMS has the following coding on status codes:
376 Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
377 The important thing to know is that odd numbers are considered
378 good, while even ones are considered errors.
379 Bits 3-15: actual status number
380 Bits 16-27: facility number. 0 is considered "unknown"
381 Bits 28-31: control bits. If bit 28 is set, the shell won't try to
382 output the message (which, for random codes, just looks ugly)
384 So, what we do here is to change 0 to 1 to get the default success status,
385 and everything else is shifted up to fit into the status number field, and
386 the status is tagged as an error, which I believe is what is wanted here.
389 # define EXIT(n) do { int __VMS_EXIT = n; \
390 if (__VMS_EXIT == 0) \
393 __VMS_EXIT = (n << 3) | 2; \
394 __VMS_EXIT |= 0x10000000; \
395 exit(__VMS_EXIT); } while(0)
396 # define NO_SYS_PARAM_H
398 # elif defined(OPENSSL_SYS_NETWARE)
401 # define NO_SYS_TYPES_H
404 # define getpid GetThreadID
405 extern int GetThreadID(void);
406 /* # include <conio.h> */
407 extern int kbhit(void);
412 # define _setmode setmode
413 # define _kbhit kbhit
414 # define _O_TEXT O_TEXT
415 # define _O_BINARY O_BINARY
416 # define OPENSSL_CONF "openssl.cnf"
417 # define SSLEAY_CONF OPENSSL_CONF
418 # define RFILE ".rnd"
419 # define LIST_SEPARATOR_CHAR ';'
420 # define EXIT(n) { if (n) printf("ERROR: %d\n", (int)n); exit(n); }
424 # ifdef OPENSSL_SYS_MPE
425 # define NO_SYS_PARAM_H
427 # ifdef OPENSSL_UNISTD
428 # include OPENSSL_UNISTD
432 # ifndef NO_SYS_TYPES_H
433 # include <sys/types.h>
436 # define pid_t int /* pid_t is missing on NEXTSTEP/OPENSTEP
437 * (unless when compiling with -D_POSIX_SOURCE,
438 * which doesn't work for us) */
440 # ifdef OPENSSL_SYS_WIN32_CYGWIN
445 # define OPENSSL_CONF "openssl.cnf"
446 # define SSLEAY_CONF OPENSSL_CONF
447 # define RFILE ".rnd"
448 # define LIST_SEPARATOR_CHAR ':'
449 # define NUL_DEV "/dev/null"
450 # define EXIT(n) exit(n)
453 # define SSLeay_getpid() getpid()
460 #if defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_DGRAM)
461 # define OPENSSL_NO_DGRAM
465 # if defined(WINDOWS) || defined(MSDOS)
468 # ifdef OPENSSL_NO_SOCK
469 # define SSLeay_Write(a,b,c) (-1)
470 # define SSLeay_Read(a,b,c) (-1)
471 # define SHUTDOWN(fd) close(fd)
472 # define SHUTDOWN2(fd) close(fd)
473 # elif !defined(__DJGPP__)
474 # if defined(_WIN32_WCE) && _WIN32_WCE<410
475 # define getservbyname _masked_declaration_getservbyname
477 # if !defined(IPPROTO_IP)
478 /* winsock[2].h was included already? */
479 # include <winsock.h>
481 # ifdef getservbyname
482 # undef getservbyname
483 /* this is used to be wcecompat/include/winsock_extras.h */
484 struct servent* PASCAL getservbyname(const char*,const char*);
489 * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
490 * the value constitutes an index in per-process table of limited size
491 * and not a real pointer.
493 # define socket(d,t,p) ((int)socket(d,t,p))
494 # define accept(s,f,l) ((int)accept(s,f,l))
496 # define SSLeay_Write(a,b,c) send((a),(b),(c),0)
497 # define SSLeay_Read(a,b,c) recv((a),(b),(c),0)
498 # define SHUTDOWN(fd) { shutdown((fd),0); closesocket(fd); }
499 # define SHUTDOWN2(fd) { shutdown((fd),2); closesocket(fd); }
501 # define SSLeay_Write(a,b,c) write_s(a,b,c,0)
502 # define SSLeay_Read(a,b,c) read_s(a,b,c)
503 # define SHUTDOWN(fd) close_s(fd)
504 # define SHUTDOWN2(fd) close_s(fd)
507 # elif defined(OPENSSL_SYS_NETWARE)
508 /* NetWare uses the WinSock2 interfaces by default, but can be configured for BSD
510 # if defined(NETWARE_BSDSOCK)
511 # include <sys/socket.h>
512 # include <netinet/in.h>
513 # include <sys/time.h>
514 # if defined(NETWARE_CLIB)
515 # include <sys/bsdskt.h>
517 # include <sys/select.h>
519 # define INVALID_SOCKET (int)(~0)
521 # include <novsock2.h>
523 # define SSLeay_Write(a,b,c) send((a),(b),(c),0)
524 # define SSLeay_Read(a,b,c) recv((a),(b),(c),0)
525 # define SHUTDOWN(fd) { shutdown((fd),0); closesocket(fd); }
526 # define SHUTDOWN2(fd) { shutdown((fd),2); closesocket(fd); }
530 # ifndef NO_SYS_PARAM_H
531 # include <sys/param.h>
533 # ifdef OPENSSL_SYS_VXWORKS
535 # elif !defined(OPENSSL_SYS_MPE)
536 # include <sys/time.h> /* Needed under linux for FD_XXX */
540 # if defined(OPENSSL_SYS_VMS_NODECC)
545 # include <sys/socket.h>
547 # ifdef OPENSSL_SYS_VXWORKS
548 # include <streams/un.h>
552 # ifndef UNIX_PATH_MAX
553 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
557 # include <sys/filio.h> /* Added for FIONBIO under unixware */
559 # include <netinet/in.h>
560 # include <arpa/inet.h>
563 # if defined(NeXT) || defined(_NEXT_SOURCE)
564 # include <sys/fcntl.h>
565 # include <sys/types.h>
568 # ifdef OPENSSL_SYS_AIX
569 # include <sys/select.h>
573 # include <sys/select.h>
577 # include <sys/filio.h>
580 # include <sys/ioctl.h>
582 /* ioctl is only in VMS > 7.0 and when socketshr is not used */
583 # if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
584 # include <sys/ioctl.h>
591 # if defined(TCPIP_TYPE_SOCKETSHR)
592 # include <socketshr.h>
596 # define SSLeay_Read(a,b,c) read((a),(b),(c))
597 # define SSLeay_Write(a,b,c) write((a),(b),(c))
598 # define SHUTDOWN(fd) { shutdown((fd),0); closesocket((fd)); }
599 # define SHUTDOWN2(fd) { shutdown((fd),2); closesocket((fd)); }
600 # ifndef INVALID_SOCKET
601 # define INVALID_SOCKET (-1)
602 # endif /* INVALID_SOCKET */
605 /* Some IPv6 implementations are broken, disable them in known bad
608 # if !defined(OPENSSL_USE_IPV6)
609 # if defined(AF_INET6) && !defined(NETWARE_CLIB)
610 # define OPENSSL_USE_IPV6 1
612 # define OPENSSL_USE_IPV6 0
618 #if defined(sun) && !defined(__svr4__) && !defined(__SVR4)
619 /* include headers first, so our defines don't break it */
622 /* bcopy can handle overlapping moves according to SunOS 4.1.4 manpage */
623 # define memmove(s1,s2,n) bcopy((s2),(s1),(n))
624 # define strtoul(s,e,b) ((unsigned long int)strtol((s),(e),(b)))
625 extern char *sys_errlist[]; extern int sys_nerr;
626 # define strerror(errnum) \
627 (((errnum)<0 || (errnum)>=sys_nerr) ? NULL : sys_errlist[errnum])
628 /* Being signed SunOS 4.x memcpy breaks ASN1_OBJECT table lookup */
629 #include "crypto/o_str.h"
630 # define memcmp OPENSSL_memcmp
634 # if defined(MONOLITH) && !defined(OPENSSL_C)
635 # define OPENSSL_EXIT(n) return(n)
637 # define OPENSSL_EXIT(n) do { EXIT(n); return(n); } while(0)
641 /***********************************************/
643 #define DG_GCC_BUG /* gcc < 2.6.3 on DGUX */
646 #define IRIX_CC_BUG /* all version of IRIX I've tested (4.* 5.*) */
648 #ifdef OPENSSL_SYS_SNI
649 #define IRIX_CC_BUG /* CDS++ up to V2.0Bsomething suffered from the same bug.*/
652 #if defined(OPENSSL_SYS_WINDOWS)
653 # define strcasecmp _stricmp
654 # define strncasecmp _strnicmp
655 #elif defined(OPENSSL_SYS_VMS)
656 /* VMS below version 7.0 doesn't have strcasecmp() */
658 # define strcasecmp OPENSSL_strcasecmp
659 # define strncasecmp OPENSSL_strncasecmp
660 # define OPENSSL_IMPLEMENTS_strncasecmp
661 #elif defined(OPENSSL_SYS_OS2) && defined(__EMX__)
662 # define strcasecmp stricmp
663 # define strncasecmp strnicmp
664 #elif defined(OPENSSL_SYS_NETWARE)
666 # if defined(NETWARE_CLIB)
667 # define strcasecmp stricmp
668 # define strncasecmp strnicmp
669 # endif /* NETWARE_CLIB */
672 #if defined(OPENSSL_SYS_OS2) && defined(__EMX__)
679 #if defined(OPENSSL_SYS_VXWORKS)
684 #define TTY_STRUCT int
686 #define sleep(a) taskDelay((a) * sysClkRateGet())
692 #define getpid taskIdSelf
694 /* NOTE: these are implemented by helpers in database app!
695 * if the database is not linked, we need to implement them
697 struct hostent *gethostbyname(const char *name);
698 struct hostent *gethostbyaddr(const char *addr, int length, int type);
699 struct servent *getservbyname(const char *name, const char *proto);
704 #if !defined(inline) && !defined(__cplusplus)
705 # if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
706 /* do nothing, inline works */
707 # elif defined(__GNUC__) && __GNUC__>=2
708 # define inline __inline__
709 # elif defined(_MSC_VER)
711 * Visual Studio: inline is available in C++ only, however
712 * __inline is available for C, see
713 * http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx
715 # define inline __inline