VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/apps/lib/s_socket.c@ 109052

Last change on this file since 109052 was 109052, checked in by vboxsync, 4 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/*
2 * Copyright 1995-2025 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/* socket-related functions used by s_client and s_server */
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <errno.h>
15#include <signal.h>
16#include <openssl/opensslconf.h>
17
18/*
19 * With IPv6, it looks like Digital has mixed up the proper order of
20 * recursive header file inclusion, resulting in the compiler complaining
21 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
22 * needed to have fileno() declared correctly... So let's define u_int
23 */
24#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
25# define __U_INT
26typedef unsigned int u_int;
27#endif
28
29#ifdef _WIN32
30# include <process.h>
31
32/* MSVC renamed some POSIX functions to have an underscore prefix. */
33# ifdef _MSC_VER
34# define getpid _getpid
35# endif
36#endif
37
38#ifndef OPENSSL_NO_SOCK
39
40# include "apps.h"
41# include "s_apps.h"
42# include "internal/sockets.h"
43
44# include <openssl/bio.h>
45# include <openssl/err.h>
46
47/* Keep track of our peer's address for the cookie callback */
48BIO_ADDR *ourpeer = NULL;
49
50/*
51 * init_client - helper routine to set up socket communication
52 * @sock: pointer to storage of resulting socket.
53 * @host: the hostname or path (for AF_UNIX) to connect to.
54 * @port: the port to connect to (ignored for AF_UNIX).
55 * @bindhost: source host or path (for AF_UNIX).
56 * @bindport: source port (ignored for AF_UNIX).
57 * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
58 * AF_UNSPEC
59 * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
60 * @protocol: socket protocol, e.g. IPPROTO_TCP or IPPROTO_UDP (or 0 for any)
61 * @tfo: flag to enable TCP Fast Open
62 * @doconn: whether we should call BIO_connect() on the socket
63 * @ba_ret: BIO_ADDR for the remote peer, to be freed by caller
64 *
65 * This will create a socket and use it to connect to a host:port, or if
66 * family == AF_UNIX, to the path found in host.
67 *
68 * If the host has more than one address, it will try them one by one until
69 * a successful connection is established. The resulting socket will be
70 * found in *sock on success, it will be given INVALID_SOCKET otherwise.
71 *
72 * Returns 1 on success, 0 on failure.
73 */
74int init_client(int *sock, const char *host, const char *port,
75 const char *bindhost, const char *bindport,
76 int family, int type, int protocol, int tfo, int doconn,
77 BIO_ADDR **ba_ret)
78{
79 BIO_ADDRINFO *res = NULL;
80 BIO_ADDRINFO *bindaddr = NULL;
81 const BIO_ADDRINFO *ai = NULL;
82 const BIO_ADDRINFO *bi = NULL;
83 int found = 0;
84 int ret;
85 int options = 0;
86
87 if (BIO_sock_init() != 1)
88 return 0;
89
90 ret = BIO_lookup_ex(host, port, BIO_LOOKUP_CLIENT, family, type, protocol,
91 &res);
92 if (ret == 0) {
93 ERR_print_errors(bio_err);
94 return 0;
95 }
96
97 if (bindhost != NULL || bindport != NULL) {
98 ret = BIO_lookup_ex(bindhost, bindport, BIO_LOOKUP_CLIENT,
99 family, type, protocol, &bindaddr);
100 if (ret == 0) {
101 ERR_print_errors (bio_err);
102 goto out;
103 }
104 }
105
106 ret = 0;
107 for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
108 /* Admittedly, these checks are quite paranoid, we should not get
109 * anything in the BIO_ADDRINFO chain that we haven't
110 * asked for. */
111 OPENSSL_assert((family == AF_UNSPEC
112 || family == BIO_ADDRINFO_family(ai))
113 && (type == 0 || type == BIO_ADDRINFO_socktype(ai))
114 && (protocol == 0
115 || protocol == BIO_ADDRINFO_protocol(ai)));
116
117 if (bindaddr != NULL) {
118 for (bi = bindaddr; bi != NULL; bi = BIO_ADDRINFO_next(bi)) {
119 if (BIO_ADDRINFO_family(bi) == BIO_ADDRINFO_family(ai))
120 break;
121 }
122 if (bi == NULL)
123 continue;
124 ++found;
125 }
126
127 *sock = BIO_socket(BIO_ADDRINFO_family(ai), BIO_ADDRINFO_socktype(ai),
128 BIO_ADDRINFO_protocol(ai), 0);
129 if (*sock == INVALID_SOCKET) {
130 /* Maybe the kernel doesn't support the socket family, even if
131 * BIO_lookup() added it in the returned result...
132 */
133 continue;
134 }
135
136 if (bi != NULL) {
137 if (!BIO_bind(*sock, BIO_ADDRINFO_address(bi),
138 BIO_SOCK_REUSEADDR)) {
139 BIO_closesocket(*sock);
140 *sock = INVALID_SOCKET;
141 break;
142 }
143 }
144
145#ifndef OPENSSL_NO_SCTP
146 if (protocol == IPPROTO_SCTP) {
147 /*
148 * For SCTP we have to set various options on the socket prior to
149 * connecting. This is done automatically by BIO_new_dgram_sctp().
150 * We don't actually need the created BIO though so we free it again
151 * immediately.
152 */
153 BIO *tmpbio = BIO_new_dgram_sctp(*sock, BIO_NOCLOSE);
154
155 if (tmpbio == NULL) {
156 ERR_print_errors(bio_err);
157 return 0;
158 }
159 BIO_free(tmpbio);
160 }
161#endif
162 if (BIO_ADDRINFO_protocol(ai) == IPPROTO_TCP) {
163 options |= BIO_SOCK_NODELAY;
164 if (tfo)
165 options |= BIO_SOCK_TFO;
166 }
167
168 if (doconn && !BIO_connect(*sock, BIO_ADDRINFO_address(ai), options)) {
169 BIO_closesocket(*sock);
170 *sock = INVALID_SOCKET;
171 continue;
172 }
173
174 /* Save the address */
175 if (tfo || !doconn)
176 *ba_ret = BIO_ADDR_dup(BIO_ADDRINFO_address(ai));
177
178 /* Success, don't try any more addresses */
179 break;
180 }
181
182 if (*sock == INVALID_SOCKET) {
183 if (bindaddr != NULL && !found) {
184 BIO_printf(bio_err, "Can't bind %saddress for %s%s%s\n",
185#ifdef AF_INET6
186 BIO_ADDRINFO_family(res) == AF_INET6 ? "IPv6 " :
187#endif
188 BIO_ADDRINFO_family(res) == AF_INET ? "IPv4 " :
189 BIO_ADDRINFO_family(res) == AF_UNIX ? "unix " : "",
190 bindhost != NULL ? bindhost : "",
191 bindport != NULL ? ":" : "",
192 bindport != NULL ? bindport : "");
193 ERR_clear_error();
194 ret = 0;
195 }
196 ERR_print_errors(bio_err);
197 } else {
198 char *hostname = NULL;
199
200 hostname = BIO_ADDR_hostname_string(BIO_ADDRINFO_address(ai), 1);
201 if (hostname != NULL) {
202 BIO_printf(bio_err, "Connecting to %s\n", hostname);
203 OPENSSL_free(hostname);
204 }
205 /* Remove any stale errors from previous connection attempts */
206 ERR_clear_error();
207 ret = 1;
208 }
209out:
210 if (bindaddr != NULL) {
211 BIO_ADDRINFO_free (bindaddr);
212 }
213 BIO_ADDRINFO_free(res);
214 return ret;
215}
216
217void get_sock_info_address(int asock, char **hostname, char **service)
218{
219 union BIO_sock_info_u info;
220
221 if (hostname != NULL)
222 *hostname = NULL;
223 if (service != NULL)
224 *service = NULL;
225
226 if ((info.addr = BIO_ADDR_new()) != NULL
227 && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)) {
228 if (hostname != NULL)
229 *hostname = BIO_ADDR_hostname_string(info.addr, 1);
230 if (service != NULL)
231 *service = BIO_ADDR_service_string(info.addr, 1);
232 }
233 BIO_ADDR_free(info.addr);
234}
235
236int report_server_accept(BIO *out, int asock, int with_address, int with_pid)
237{
238 int success = 1;
239
240 if (BIO_printf(out, "ACCEPT") <= 0)
241 return 0;
242 if (with_address) {
243 char *hostname, *service;
244
245 get_sock_info_address(asock, &hostname, &service);
246 success = hostname != NULL && service != NULL;
247 if (success)
248 success = BIO_printf(out,
249 strchr(hostname, ':') == NULL
250 ? /* IPv4 */ " %s:%s"
251 : /* IPv6 */ " [%s]:%s",
252 hostname, service) > 0;
253 else
254 (void)BIO_printf(out, "unknown:error\n");
255 OPENSSL_free(hostname);
256 OPENSSL_free(service);
257 }
258 if (with_pid)
259 success *= BIO_printf(out, " PID=%d", getpid()) > 0;
260 success *= BIO_printf(out, "\n") > 0;
261 (void)BIO_flush(out);
262
263 return success;
264}
265
266/*
267 * do_server - helper routine to perform a server operation
268 * @accept_sock: pointer to storage of resulting socket.
269 * @host: the hostname or path (for AF_UNIX) to connect to.
270 * @port: the port to connect to (ignored for AF_UNIX).
271 * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
272 * AF_UNSPEC
273 * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
274 * @cb: pointer to a function that receives the accepted socket and
275 * should perform the communication with the connecting client.
276 * @context: pointer to memory that's passed verbatim to the cb function.
277 * @naccept: number of times an incoming connect should be accepted. If -1,
278 * unlimited number.
279 *
280 * This will create a socket and use it to listen to a host:port, or if
281 * family == AF_UNIX, to the path found in host, then start accepting
282 * incoming connections and run cb on the resulting socket.
283 *
284 * 0 on failure, something other on success.
285 */
286int do_server(int *accept_sock, const char *host, const char *port,
287 int family, int type, int protocol, do_server_cb cb,
288 unsigned char *context, int naccept, BIO *bio_s_out,
289 int tfo)
290{
291 int asock = 0;
292 int sock;
293 int i;
294 BIO_ADDRINFO *res = NULL;
295 const BIO_ADDRINFO *next;
296 int sock_family, sock_type, sock_protocol, sock_port;
297 const BIO_ADDR *sock_address;
298 int sock_family_fallback = AF_UNSPEC;
299 const BIO_ADDR *sock_address_fallback = NULL;
300 int sock_options = BIO_SOCK_REUSEADDR;
301 int ret = 0;
302
303 if (BIO_sock_init() != 1)
304 return 0;
305
306 if (!BIO_lookup_ex(host, port, BIO_LOOKUP_SERVER, family, type, protocol,
307 &res)) {
308 ERR_print_errors(bio_err);
309 return 0;
310 }
311
312 /* Admittedly, these checks are quite paranoid, we should not get
313 * anything in the BIO_ADDRINFO chain that we haven't asked for */
314 OPENSSL_assert((family == AF_UNSPEC || family == BIO_ADDRINFO_family(res))
315 && (type == 0 || type == BIO_ADDRINFO_socktype(res))
316 && (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res)));
317
318 sock_family = BIO_ADDRINFO_family(res);
319 sock_type = BIO_ADDRINFO_socktype(res);
320 sock_protocol = BIO_ADDRINFO_protocol(res);
321 sock_address = BIO_ADDRINFO_address(res);
322 next = BIO_ADDRINFO_next(res);
323 if (tfo && sock_type == SOCK_STREAM)
324 sock_options |= BIO_SOCK_TFO;
325#ifdef AF_INET6
326 if (sock_family == AF_INET6)
327 sock_options |= BIO_SOCK_V6_ONLY;
328 if (next != NULL
329 && BIO_ADDRINFO_socktype(next) == sock_type
330 && BIO_ADDRINFO_protocol(next) == sock_protocol) {
331 if (sock_family == AF_INET
332 && BIO_ADDRINFO_family(next) == AF_INET6) {
333 /* In case AF_INET6 is returned but not supported by the
334 * kernel, retry with the first detected address family */
335 sock_family_fallback = sock_family;
336 sock_address_fallback = sock_address;
337 sock_family = AF_INET6;
338 sock_address = BIO_ADDRINFO_address(next);
339 } else if (sock_family == AF_INET6
340 && BIO_ADDRINFO_family(next) == AF_INET) {
341 sock_options &= ~BIO_SOCK_V6_ONLY;
342 }
343 }
344#endif
345
346 asock = BIO_socket(sock_family, sock_type, sock_protocol, 0);
347 if (asock == INVALID_SOCKET && sock_family_fallback != AF_UNSPEC) {
348 asock = BIO_socket(sock_family_fallback, sock_type, sock_protocol, 0);
349 sock_address = sock_address_fallback;
350 }
351 if (asock == INVALID_SOCKET
352 || !BIO_listen(asock, sock_address, sock_options)) {
353 BIO_ADDRINFO_free(res);
354 ERR_print_errors(bio_err);
355 if (asock != INVALID_SOCKET)
356 BIO_closesocket(asock);
357 goto end;
358 }
359
360#ifndef OPENSSL_NO_SCTP
361 if (protocol == IPPROTO_SCTP) {
362 /*
363 * For SCTP we have to set various options on the socket prior to
364 * accepting. This is done automatically by BIO_new_dgram_sctp().
365 * We don't actually need the created BIO though so we free it again
366 * immediately.
367 */
368 BIO *tmpbio = BIO_new_dgram_sctp(asock, BIO_NOCLOSE);
369
370 if (tmpbio == NULL) {
371 BIO_closesocket(asock);
372 ERR_print_errors(bio_err);
373 goto end;
374 }
375 BIO_free(tmpbio);
376 }
377#endif
378
379 sock_port = BIO_ADDR_rawport(sock_address);
380
381 BIO_ADDRINFO_free(res);
382 res = NULL;
383
384 if (!report_server_accept(bio_s_out, asock, sock_port == 0, 0)) {
385 BIO_closesocket(asock);
386 ERR_print_errors(bio_err);
387 goto end;
388 }
389
390 if (accept_sock != NULL)
391 *accept_sock = asock;
392 for (;;) {
393 char sink[64];
394 struct timeval timeout;
395 fd_set readfds;
396
397 if (type == SOCK_STREAM) {
398 BIO_ADDR_free(ourpeer);
399 ourpeer = BIO_ADDR_new();
400 if (ourpeer == NULL) {
401 BIO_closesocket(asock);
402 ERR_print_errors(bio_err);
403 goto end;
404 }
405 do {
406 sock = BIO_accept_ex(asock, ourpeer, 0);
407 } while (sock < 0 && BIO_sock_should_retry(sock));
408 if (sock < 0) {
409 ERR_print_errors(bio_err);
410 BIO_closesocket(asock);
411 break;
412 }
413
414 if (naccept != -1)
415 naccept--;
416 if (naccept == 0)
417 BIO_closesocket(asock);
418
419 BIO_set_tcp_ndelay(sock, 1);
420 i = (*cb)(sock, type, protocol, context);
421
422 /*
423 * If we ended with an alert being sent, but still with data in the
424 * network buffer to be read, then calling BIO_closesocket() will
425 * result in a TCP-RST being sent. On some platforms (notably
426 * Windows) then this will result in the peer immediately abandoning
427 * the connection including any buffered alert data before it has
428 * had a chance to be read. Shutting down the sending side first,
429 * and then closing the socket sends TCP-FIN first followed by
430 * TCP-RST. This seems to allow the peer to read the alert data.
431 */
432 shutdown(sock, 1); /* SHUT_WR */
433 /*
434 * We just said we have nothing else to say, but it doesn't mean
435 * that the other side has nothing. It's even recommended to
436 * consume incoming data. [In testing context this ensures that
437 * alerts are passed on...]
438 */
439 timeout.tv_sec = 0;
440 timeout.tv_usec = 500000; /* some extreme round-trip */
441 do {
442 FD_ZERO(&readfds);
443 openssl_fdset(sock, &readfds);
444 } while (select(sock + 1, &readfds, NULL, NULL, &timeout) > 0
445 && readsocket(sock, sink, sizeof(sink)) > 0);
446
447 BIO_closesocket(sock);
448 } else {
449 if (naccept != -1)
450 naccept--;
451
452 i = (*cb)(asock, type, protocol, context);
453 }
454
455 if (i < 0 || naccept == 0) {
456 BIO_closesocket(asock);
457 ret = i;
458 break;
459 }
460 }
461 end:
462# ifdef AF_UNIX
463 if (family == AF_UNIX)
464 unlink(host);
465# endif
466 BIO_ADDR_free(ourpeer);
467 ourpeer = NULL;
468 return ret;
469}
470
471void do_ssl_shutdown(SSL *ssl)
472{
473 int ret;
474
475 do {
476 /* We only do unidirectional shutdown */
477 ret = SSL_shutdown(ssl);
478 if (ret < 0) {
479 switch (SSL_get_error(ssl, ret)) {
480 case SSL_ERROR_WANT_READ:
481 case SSL_ERROR_WANT_WRITE:
482 case SSL_ERROR_WANT_ASYNC:
483 case SSL_ERROR_WANT_ASYNC_JOB:
484 /* We just do busy waiting. Nothing clever */
485 continue;
486 }
487 ret = 0;
488 }
489 } while (ret < 0);
490}
491
492#endif /* OPENSSL_NO_SOCK */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette