1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 199-2015 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * licensing@OpenSSL.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
106 /* socket-related functions used by s_client and s_server */
114 * With IPv6, it looks like Digital has mixed up the proper order of
115 * recursive header file inclusion, resulting in the compiler complaining
116 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
117 * needed to have fileno() declared correctly... So let's define u_int
119 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
121 typedef unsigned int u_int;
128 #include <openssl/ssl.h>
133 # include "../e_os.h"
136 #ifndef OPENSSL_NO_SOCK
138 # if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
142 static struct hostent *GetHostByName(const char *name);
143 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
144 static void ssl_sock_cleanup(void);
146 static int ssl_sock_init(void);
147 static int init_client_ip(int *sock, const unsigned char ip[4], int port,
149 static int init_server(int *sock, int port, int type);
150 static int init_server_long(int *sock, int port, char *ip, int type);
151 static int do_accept(int acc_sock, int *sock, char **host);
152 static int host_ip(const char *str, unsigned char ip[4]);
154 static int init_server_unix(int *sock, const char *path);
155 static int do_accept_unix(int acc_sock, int *sock);
158 # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
159 static int wsa_init_done = 0;
162 # ifdef OPENSSL_SYS_WINDOWS
163 static struct WSAData wsa_state;
164 static int wsa_init_done = 0;
166 # endif /* OPENSSL_SYS_WINDOWS */
168 # ifdef OPENSSL_SYS_WINDOWS
169 static void ssl_sock_cleanup(void)
173 # ifndef OPENSSL_SYS_WINCE
174 WSACancelBlockingCall();
179 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
180 static void sock_cleanup(void)
189 static int ssl_sock_init(void)
192 extern int _watt_do_exit;
196 # elif defined(OPENSSL_SYS_WINDOWS)
197 if (!wsa_init_done) {
201 signal(SIGINT, (void (*)(int))ssl_sock_cleanup);
204 memset(&wsa_state, 0, sizeof(wsa_state));
205 if (WSAStartup(0x0101, &wsa_state) != 0) {
206 err = WSAGetLastError();
207 BIO_printf(bio_err, "unable to start WINSOCK, error code=%d\n",
212 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
217 if (!wsa_init_done) {
220 signal(SIGINT, (void (*)(int))sock_cleanup);
224 wVerReq = MAKEWORD(2, 0);
225 err = WSAStartup(wVerReq, &wsaData);
227 BIO_printf(bio_err, "unable to start WINSOCK2, error code=%d\n",
236 int init_client(int *sock, const char *host, int port, int type)
240 ip[0] = ip[1] = ip[2] = ip[3] = 0;
241 if (!host_ip(host, &(ip[0])))
243 return init_client_ip(sock, ip, port, type);
246 static int init_client_ip(int *sock, const unsigned char ip[4], int port,
250 struct sockaddr_in them;
253 if (!ssl_sock_init())
256 memset((char *)&them, 0, sizeof(them));
257 them.sin_family = AF_INET;
258 them.sin_port = htons((unsigned short)port);
259 addr = (unsigned long)
260 ((unsigned long)ip[0] << 24L) |
261 ((unsigned long)ip[1] << 16L) |
262 ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
263 them.sin_addr.s_addr = htonl(addr);
265 if (type == SOCK_STREAM)
266 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
267 else /* ( type == SOCK_DGRAM) */
268 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
270 if (s == INVALID_SOCKET) {
274 # if defined(SO_KEEPALIVE)
275 if (type == SOCK_STREAM) {
277 i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
286 if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
296 int init_client_unix(int *sock, const char *server)
298 struct sockaddr_un them;
301 if (strlen(server) > (UNIX_PATH_MAX + 1))
303 if (!ssl_sock_init())
306 s = socket(AF_UNIX, SOCK_STREAM, 0);
307 if (s == INVALID_SOCKET) {
312 memset((char *)&them, 0, sizeof(them));
313 them.sun_family = AF_UNIX;
314 strcpy(them.sun_path, server);
316 if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
326 int do_server(int port, int type, int *ret,
327 int (*cb) (char *hostname, int s, int stype,
328 unsigned char *context), unsigned char *context,
333 int accept_socket = 0;
336 if (!init_server(&accept_socket, port, type))
340 *ret = accept_socket;
344 if (type == SOCK_STREAM) {
345 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
346 if (do_accept(accept_socket, &sock, NULL) == 0)
348 if (do_accept(accept_socket, &sock, &name) == 0)
351 SHUTDOWN(accept_socket);
355 sock = accept_socket;
356 i = (*cb) (name, sock, type, context);
358 if (type == SOCK_STREAM)
362 if (i < 0 || naccept == 0) {
363 SHUTDOWN2(accept_socket);
370 int do_server_unix(const char *path, int *ret,
371 int (*cb) (char *hostname, int s, int stype,
372 unsigned char *context), unsigned char *context,
376 int accept_socket = 0;
379 if (!init_server_unix(&accept_socket, path))
383 *ret = accept_socket;
385 if (do_accept_unix(accept_socket, &sock) == 0) {
386 SHUTDOWN(accept_socket);
390 i = (*cb) (NULL, sock, 0, context);
394 if (i < 0 || naccept == 0) {
395 SHUTDOWN2(accept_socket);
405 static int init_server_long(int *sock, int port, char *ip, int type)
408 struct sockaddr_in server;
411 if (!ssl_sock_init())
414 memset((char *)&server, 0, sizeof(server));
415 server.sin_family = AF_INET;
416 server.sin_port = htons((unsigned short)port);
418 server.sin_addr.s_addr = INADDR_ANY;
420 /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
421 # ifndef BIT_FIELD_LIMITS
422 memcpy(&server.sin_addr.s_addr, ip, 4);
424 memcpy(&server.sin_addr, ip, 4);
427 if (type == SOCK_STREAM)
428 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
429 else /* type == SOCK_DGRAM */
430 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
432 if (s == INVALID_SOCKET)
434 # if defined SOL_SOCKET && defined SO_REUSEADDR
437 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
440 if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
441 # ifndef OPENSSL_SYS_WINDOWS
446 /* Make it 128 for linux */
447 if (type == SOCK_STREAM && listen(s, 128) == -1)
452 if ((ret == 0) && (s != -1)) {
458 static int init_server(int *sock, int port, int type)
460 return (init_server_long(sock, port, NULL, type));
464 static int init_server_unix(int *sock, const char *path)
467 struct sockaddr_un server;
470 if (strlen(path) > (UNIX_PATH_MAX + 1))
472 if (!ssl_sock_init())
475 s = socket(AF_UNIX, SOCK_STREAM, 0);
476 if (s == INVALID_SOCKET)
479 memset((char *)&server, 0, sizeof(server));
480 server.sun_family = AF_UNIX;
481 strcpy(server.sun_path, path);
483 if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
484 # ifndef OPENSSL_SYS_WINDOWS
489 /* Make it 128 for linux */
490 if (listen(s, 128) == -1) {
491 # ifndef OPENSSL_SYS_WINDOWS
500 if ((ret == 0) && (s != -1)) {
507 static int do_accept(int acc_sock, int *sock, char **host)
510 struct hostent *h1, *h2;
511 static struct sockaddr_in from;
513 /* struct linger ling; */
515 if (!ssl_sock_init())
518 # ifndef OPENSSL_SYS_WINDOWS
522 memset((char *)&from, 0, sizeof(from));
525 * Note: under VMS with SOCKETSHR the fourth parameter is currently of
526 * type (int *) whereas under other systems it is (void *) if you don't
527 * have a cast it will choke the compiler: if you do have a cast then you
528 * can either go for (int *) or (void *).
530 ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len);
531 if (ret == INVALID_SOCKET) {
532 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
534 i = WSAGetLastError();
535 BIO_printf(bio_err, "accept error %d\n", i);
537 if (errno == EINTR) {
543 fprintf(stderr, "errno=%d ", errno);
551 # ifndef BIT_FIELD_LIMITS
552 /* I should use WSAAsyncGetHostByName() under windows */
553 h1 = gethostbyaddr((char *)&from.sin_addr.s_addr,
554 sizeof(from.sin_addr.s_addr), AF_INET);
556 h1 = gethostbyaddr((char *)&from.sin_addr,
557 sizeof(struct in_addr), AF_INET);
560 BIO_printf(bio_err, "bad gethostbyaddr\n");
564 *host = app_malloc(strlen(h1->h_name) + 1, "copy hostname");
565 BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
567 h2 = GetHostByName(*host);
569 BIO_printf(bio_err, "gethostbyname failure\n");
573 if (h2->h_addrtype != AF_INET) {
574 BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
585 static int do_accept_unix(int acc_sock, int *sock)
589 if (!ssl_sock_init())
593 ret = accept(acc_sock, NULL, NULL);
594 if (ret == INVALID_SOCKET) {
595 if (errno == EINTR) {
601 fprintf(stderr, "errno=%d ", errno);
611 int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
612 unsigned short *port_ptr)
617 p = strchr(str, ':');
619 BIO_printf(bio_err, "no port defined\n");
624 if ((ip != NULL) && !host_ip(str, ip))
626 if (host_ptr != NULL)
629 if (!extract_port(p, port_ptr))
636 static int host_ip(const char *str, unsigned char ip[4])
641 if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
643 for (i = 0; i < 4; i++)
645 BIO_printf(bio_err, "invalid IP address\n");
652 } else { /* do a gethostbyname */
655 if (!ssl_sock_init())
658 he = GetHostByName(str);
660 BIO_printf(bio_err, "gethostbyname failure\n");
663 if (he->h_addrtype != AF_INET) {
664 BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
667 ip[0] = he->h_addr_list[0][0];
668 ip[1] = he->h_addr_list[0][1];
669 ip[2] = he->h_addr_list[0][2];
670 ip[3] = he->h_addr_list[0][3];
677 int extract_port(const char *str, unsigned short *port_ptr)
684 *port_ptr = (unsigned short)i;
686 s = getservbyname(str, "tcp");
688 BIO_printf(bio_err, "getservbyname failure for %s\n", str);
691 *port_ptr = ntohs((unsigned short)s->s_port);
697 static struct ghbn_cache_st {
701 } ghbn_cache[GHBN_NUM];
703 static unsigned long ghbn_hits = 0L;
704 static unsigned long ghbn_miss = 0L;
706 static struct hostent *GetHostByName(const char *name)
710 unsigned long low = (unsigned long)-1;
712 for (i = 0; i < GHBN_NUM; i++) {
713 if (low > ghbn_cache[i].order) {
714 low = ghbn_cache[i].order;
717 if (ghbn_cache[i].order > 0) {
718 if (strncmp(name, ghbn_cache[i].name, 128) == 0)
722 if (i == GHBN_NUM) { /* no hit */
724 ret = gethostbyname(name);
727 /* else add to cache */
728 if (strlen(name) < sizeof ghbn_cache[0].name) {
729 strcpy(ghbn_cache[lowi].name, name);
730 memcpy((char *)&(ghbn_cache[lowi].ent), ret,
731 sizeof(struct hostent));
732 ghbn_cache[lowi].order = ghbn_miss + ghbn_hits;
737 ret = &(ghbn_cache[i].ent);
738 ghbn_cache[i].order = ghbn_miss + ghbn_hits;