2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "bio_local.h"
14 #include "internal/bio_tfo.h"
15 #include "internal/ktls.h"
17 #ifndef OPENSSL_NO_SOCK
19 typedef struct bio_connect_st {
25 # ifndef OPENSSL_NO_KTLS
26 unsigned char record_type;
30 BIO_ADDRINFO *addr_first;
31 const BIO_ADDRINFO *addr_iter;
33 * int socket; this will be kept in bio->num so that it is compatible
34 * with the bss_sock bio
37 * called when the connection is initially made callback(BIO,state,ret);
38 * The callback should return 'ret'. state is for compatibility with the
41 BIO_info_cb *info_callback;
44 static int conn_write(BIO *h, const char *buf, int num);
45 static int conn_read(BIO *h, char *buf, int size);
46 static int conn_puts(BIO *h, const char *str);
47 static int conn_gets(BIO *h, char *buf, int size);
48 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
49 static int conn_new(BIO *h);
50 static int conn_free(BIO *data);
51 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
53 static int conn_state(BIO *b, BIO_CONNECT *c);
54 static void conn_close_socket(BIO *data);
55 static BIO_CONNECT *BIO_CONNECT_new(void);
56 static void BIO_CONNECT_free(BIO_CONNECT *a);
58 #define BIO_CONN_S_BEFORE 1
59 #define BIO_CONN_S_GET_ADDR 2
60 #define BIO_CONN_S_CREATE_SOCKET 3
61 #define BIO_CONN_S_CONNECT 4
62 #define BIO_CONN_S_OK 5
63 #define BIO_CONN_S_BLOCKED_CONNECT 6
64 #define BIO_CONN_S_CONNECT_ERROR 7
66 static const BIO_METHOD methods_connectp = {
81 static int conn_state(BIO *b, BIO_CONNECT *c)
84 BIO_info_cb *cb = NULL;
86 if (c->info_callback != NULL)
87 cb = c->info_callback;
91 case BIO_CONN_S_BEFORE:
92 if (c->param_hostname == NULL && c->param_service == NULL) {
93 ERR_raise_data(ERR_LIB_BIO,
94 BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
95 "hostname=%s service=%s",
96 c->param_hostname, c->param_service);
99 c->state = BIO_CONN_S_GET_ADDR;
102 case BIO_CONN_S_GET_ADDR:
104 int family = AF_UNSPEC;
105 switch (c->connect_family) {
106 case BIO_FAMILY_IPV6:
107 if (1) { /* This is a trick we use to avoid bit rot.
108 * at least the "else" part will always be
115 ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
119 case BIO_FAMILY_IPV4:
122 case BIO_FAMILY_IPANY:
126 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
129 if (BIO_lookup(c->param_hostname, c->param_service,
131 family, SOCK_STREAM, &c->addr_first) == 0)
134 if (c->addr_first == NULL) {
135 ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
138 c->addr_iter = c->addr_first;
139 c->state = BIO_CONN_S_CREATE_SOCKET;
142 case BIO_CONN_S_CREATE_SOCKET:
143 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
144 BIO_ADDRINFO_socktype(c->addr_iter),
145 BIO_ADDRINFO_protocol(c->addr_iter), 0);
146 if (ret == (int)INVALID_SOCKET) {
147 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
148 "calling socket(%s, %s)",
149 c->param_hostname, c->param_service);
150 ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
154 c->state = BIO_CONN_S_CONNECT;
157 case BIO_CONN_S_CONNECT:
158 BIO_clear_retry_flags(b);
160 ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
161 BIO_SOCK_KEEPALIVE | c->connect_mode);
164 if (BIO_sock_should_retry(ret)) {
165 BIO_set_retry_special(b);
166 c->state = BIO_CONN_S_BLOCKED_CONNECT;
167 b->retry_reason = BIO_RR_CONNECT;
169 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
172 * if there are more addresses to try, do that first
174 BIO_closesocket(b->num);
175 c->state = BIO_CONN_S_CREATE_SOCKET;
179 ERR_clear_last_mark();
180 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
181 "calling connect(%s, %s)",
182 c->param_hostname, c->param_service);
183 c->state = BIO_CONN_S_CONNECT_ERROR;
188 ERR_clear_last_mark();
189 c->state = BIO_CONN_S_OK;
193 case BIO_CONN_S_BLOCKED_CONNECT:
194 /* wait for socket being writable, before querying BIO_sock_error */
195 if (BIO_socket_wait(b->num, 0, time(NULL)) == 0)
197 i = BIO_sock_error(b->num);
199 BIO_clear_retry_flags(b);
200 if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
202 * if there are more addresses to try, do that first
204 BIO_closesocket(b->num);
205 c->state = BIO_CONN_S_CREATE_SOCKET;
208 ERR_raise_data(ERR_LIB_SYS, i,
209 "calling connect(%s, %s)",
210 c->param_hostname, c->param_service);
211 ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
215 c->state = BIO_CONN_S_OK;
216 # ifndef OPENSSL_NO_KTLS
218 * The new socket is created successfully regardless of ktls_enable.
219 * ktls_enable doesn't change any functionality of the socket, except
220 * changing the setsockopt to enable the processing of ktls_start.
221 * Thus, it is not a problem to call it for non-TLS sockets.
228 case BIO_CONN_S_CONNECT_ERROR:
229 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
242 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
247 /* Loop does not exit */
250 ret = cb((BIO *)b, c->state, ret);
255 static BIO_CONNECT *BIO_CONNECT_new(void)
259 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
261 ret->state = BIO_CONN_S_BEFORE;
262 ret->connect_family = BIO_FAMILY_IPANY;
266 static void BIO_CONNECT_free(BIO_CONNECT *a)
270 OPENSSL_free(a->param_hostname);
271 OPENSSL_free(a->param_service);
272 BIO_ADDRINFO_free(a->addr_first);
276 const BIO_METHOD *BIO_s_connect(void)
278 return &methods_connectp;
281 static int conn_new(BIO *bi)
284 bi->num = (int)INVALID_SOCKET;
286 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
292 static void conn_close_socket(BIO *bio)
296 c = (BIO_CONNECT *)bio->ptr;
297 if (bio->num != (int)INVALID_SOCKET) {
298 /* Only do a shutdown if things were established */
299 if (c->state == BIO_CONN_S_OK)
300 shutdown(bio->num, 2);
301 BIO_closesocket(bio->num);
302 bio->num = (int)INVALID_SOCKET;
306 static int conn_free(BIO *a)
312 data = (BIO_CONNECT *)a->ptr;
315 conn_close_socket(a);
316 BIO_CONNECT_free(data);
324 static int conn_read(BIO *b, char *out, int outl)
329 data = (BIO_CONNECT *)b->ptr;
330 if (data->state != BIO_CONN_S_OK) {
331 ret = conn_state(b, data);
337 clear_socket_error();
338 # ifndef OPENSSL_NO_KTLS
339 if (BIO_get_ktls_recv(b))
340 ret = ktls_read_record(b->num, out, outl);
343 ret = readsocket(b->num, out, outl);
344 BIO_clear_retry_flags(b);
346 if (BIO_sock_should_retry(ret))
347 BIO_set_retry_read(b);
349 b->flags |= BIO_FLAGS_IN_EOF;
355 static int conn_write(BIO *b, const char *in, int inl)
360 data = (BIO_CONNECT *)b->ptr;
361 if (data->state != BIO_CONN_S_OK) {
362 ret = conn_state(b, data);
367 clear_socket_error();
368 # ifndef OPENSSL_NO_KTLS
369 if (BIO_should_ktls_ctrl_msg_flag(b)) {
370 ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
373 BIO_clear_ktls_ctrl_msg_flag(b);
377 # if defined(OSSL_TFO_SENDTO)
378 if (data->tfo_first) {
379 int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter);
381 ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
382 BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen);
386 ret = writesocket(b->num, in, inl);
387 BIO_clear_retry_flags(b);
389 if (BIO_sock_should_retry(ret))
390 BIO_set_retry_write(b);
395 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
399 const char **pptr = NULL;
402 # ifndef OPENSSL_NO_KTLS
403 ktls_crypto_info_t *crypto_info;
406 data = (BIO_CONNECT *)b->ptr;
411 data->state = BIO_CONN_S_BEFORE;
412 conn_close_socket(b);
413 BIO_ADDRINFO_free(data->addr_first);
414 data->addr_first = NULL;
417 case BIO_C_DO_STATE_MACHINE:
418 /* use this one to start the connection */
419 if (data->state != BIO_CONN_S_OK)
420 ret = (long)conn_state(b, data);
424 case BIO_C_GET_CONNECT:
426 pptr = (const char **)ptr;
428 *pptr = data->param_hostname;
429 } else if (num == 1) {
430 *pptr = data->param_service;
431 } else if (num == 2) {
432 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
433 } else if (num == 3) {
434 switch (BIO_ADDRINFO_family(data->addr_iter)) {
435 # if OPENSSL_USE_IPV6
437 ret = BIO_FAMILY_IPV6;
441 ret = BIO_FAMILY_IPV4;
444 ret = data->connect_family;
450 } else if (num == 4) {
451 ret = data->connect_mode;
459 case BIO_C_SET_CONNECT:
462 if (num == 0) { /* BIO_set_conn_hostname */
463 char *hold_service = data->param_service;
464 /* We affect the hostname regardless. However, the input
465 * string might contain a host:service spec, so we must
466 * parse it, which might or might not affect the service
469 OPENSSL_free(data->param_hostname);
470 data->param_hostname = NULL;
471 ret = BIO_parse_hostserv(ptr,
472 &data->param_hostname,
473 &data->param_service,
474 BIO_PARSE_PRIO_HOST);
475 if (hold_service != data->param_service)
476 OPENSSL_free(hold_service);
477 } else if (num == 1) { /* BIO_set_conn_port */
478 OPENSSL_free(data->param_service);
479 if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
481 } else if (num == 2) { /* BIO_set_conn_address */
482 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
483 char *host = BIO_ADDR_hostname_string(addr, 1);
484 char *service = BIO_ADDR_service_string(addr, 1);
486 ret = host != NULL && service != NULL;
488 OPENSSL_free(data->param_hostname);
489 data->param_hostname = host;
490 OPENSSL_free(data->param_service);
491 data->param_service = service;
492 BIO_ADDRINFO_free(data->addr_first);
493 data->addr_first = NULL;
494 data->addr_iter = NULL;
497 OPENSSL_free(service);
499 } else if (num == 3) { /* BIO_set_conn_ip_family */
500 data->connect_family = *(int *)ptr;
508 data->connect_mode |= BIO_SOCK_NONBLOCK;
510 data->connect_mode &= ~BIO_SOCK_NONBLOCK;
512 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
515 data->connect_mode |= BIO_SOCK_TFO;
518 data->connect_mode &= ~BIO_SOCK_TFO;
523 case BIO_C_SET_CONNECT_MODE:
524 data->connect_mode = (int)num;
525 if (num & BIO_SOCK_TFO)
539 case BIO_CTRL_GET_CLOSE:
542 case BIO_CTRL_SET_CLOSE:
543 b->shutdown = (int)num;
545 case BIO_CTRL_PENDING:
546 case BIO_CTRL_WPENDING:
554 if (data->param_hostname)
555 BIO_set_conn_hostname(dbio, data->param_hostname);
556 if (data->param_service)
557 BIO_set_conn_port(dbio, data->param_service);
558 BIO_set_conn_ip_family(dbio, data->connect_family);
559 BIO_set_conn_mode(dbio, data->connect_mode);
561 * FIXME: the cast of the function seems unlikely to be a good
564 (void)BIO_set_info_callback(dbio, data->info_callback);
567 case BIO_CTRL_SET_CALLBACK:
568 ret = 0; /* use callback ctrl */
570 case BIO_CTRL_GET_CALLBACK:
574 fptr = (BIO_info_cb **)ptr;
575 *fptr = data->info_callback;
579 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
581 # ifndef OPENSSL_NO_KTLS
582 case BIO_CTRL_SET_KTLS:
583 crypto_info = (ktls_crypto_info_t *)ptr;
584 ret = ktls_start(b->num, crypto_info, num);
586 BIO_set_ktls_flag(b, num);
588 case BIO_CTRL_GET_KTLS_SEND:
589 return BIO_should_ktls_flag(b, 1) != 0;
590 case BIO_CTRL_GET_KTLS_RECV:
591 return BIO_should_ktls_flag(b, 0) != 0;
592 case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
593 BIO_set_ktls_ctrl_msg_flag(b);
594 data->record_type = num;
597 case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
598 BIO_clear_ktls_ctrl_msg_flag(b);
601 case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
602 ret = ktls_enable_tx_zerocopy_sendfile(b->num);
604 BIO_set_ktls_zerocopy_sendfile_flag(b);
614 static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
619 data = (BIO_CONNECT *)b->ptr;
622 case BIO_CTRL_SET_CALLBACK:
624 data->info_callback = fp;
634 static int conn_puts(BIO *bp, const char *str)
639 ret = conn_write(bp, str, n);
643 int conn_gets(BIO *bio, char *buf, int size)
650 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
654 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
659 if (bio == NULL || bio->ptr == NULL) {
660 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
663 data = (BIO_CONNECT *)bio->ptr;
664 if (data->state != BIO_CONN_S_OK) {
665 ret = conn_state(bio, data);
670 clear_socket_error();
672 # ifndef OPENSSL_NO_KTLS
673 if (BIO_get_ktls_recv(bio))
674 ret = ktls_read_record(bio->num, ptr, 1);
677 ret = readsocket(bio->num, ptr, 1);
678 BIO_clear_retry_flags(bio);
680 if (BIO_sock_should_retry(ret))
681 BIO_set_retry_read(bio);
683 bio->flags |= BIO_FLAGS_IN_EOF;
690 return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret;
693 BIO *BIO_new_connect(const char *str)
697 ret = BIO_new(BIO_s_connect());
700 if (BIO_set_conn_hostname(ret, str))