2 * Copyright 1995-2021 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/ktls.h"
16 #ifndef OPENSSL_NO_SOCK
18 typedef struct bio_connect_st {
24 # ifndef OPENSSL_NO_KTLS
25 unsigned char record_type;
28 BIO_ADDRINFO *addr_first;
29 const BIO_ADDRINFO *addr_iter;
31 * int socket; this will be kept in bio->num so that it is compatible
32 * with the bss_sock bio
35 * called when the connection is initially made callback(BIO,state,ret);
36 * The callback should return 'ret'. state is for compatibility with the
39 BIO_info_cb *info_callback;
42 static int conn_write(BIO *h, const char *buf, int num);
43 static int conn_read(BIO *h, char *buf, int size);
44 static int conn_puts(BIO *h, const char *str);
45 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
46 static int conn_new(BIO *h);
47 static int conn_free(BIO *data);
48 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
50 static int conn_state(BIO *b, BIO_CONNECT *c);
51 static void conn_close_socket(BIO *data);
52 BIO_CONNECT *BIO_CONNECT_new(void);
53 void BIO_CONNECT_free(BIO_CONNECT *a);
55 #define BIO_CONN_S_BEFORE 1
56 #define BIO_CONN_S_GET_ADDR 2
57 #define BIO_CONN_S_CREATE_SOCKET 3
58 #define BIO_CONN_S_CONNECT 4
59 #define BIO_CONN_S_OK 5
60 #define BIO_CONN_S_BLOCKED_CONNECT 6
61 #define BIO_CONN_S_CONNECT_ERROR 7
63 static const BIO_METHOD methods_connectp = {
66 /* TODO: Convert to new style write function */
69 /* TODO: Convert to new style read function */
73 NULL, /* conn_gets, */
80 static int conn_state(BIO *b, BIO_CONNECT *c)
83 BIO_info_cb *cb = NULL;
85 if (c->info_callback != NULL)
86 cb = c->info_callback;
90 case BIO_CONN_S_BEFORE:
91 if (c->param_hostname == NULL && c->param_service == NULL) {
92 ERR_raise_data(ERR_LIB_BIO,
93 BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
94 "hostname=%s service=%s",
95 c->param_hostname, c->param_service);
98 c->state = BIO_CONN_S_GET_ADDR;
101 case BIO_CONN_S_GET_ADDR:
103 int family = AF_UNSPEC;
104 switch (c->connect_family) {
105 case BIO_FAMILY_IPV6:
106 if (1) { /* This is a trick we use to avoid bit rot.
107 * at least the "else" part will always be
114 ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
118 case BIO_FAMILY_IPV4:
121 case BIO_FAMILY_IPANY:
125 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
128 if (BIO_lookup(c->param_hostname, c->param_service,
130 family, SOCK_STREAM, &c->addr_first) == 0)
133 if (c->addr_first == NULL) {
134 ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
137 c->addr_iter = c->addr_first;
138 c->state = BIO_CONN_S_CREATE_SOCKET;
141 case BIO_CONN_S_CREATE_SOCKET:
142 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
143 BIO_ADDRINFO_socktype(c->addr_iter),
144 BIO_ADDRINFO_protocol(c->addr_iter), 0);
145 if (ret == (int)INVALID_SOCKET) {
146 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
147 "calling socket(%s, %s)",
148 c->param_hostname, c->param_service);
149 ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
153 c->state = BIO_CONN_S_CONNECT;
156 case BIO_CONN_S_CONNECT:
157 BIO_clear_retry_flags(b);
159 ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
160 BIO_SOCK_KEEPALIVE | c->connect_mode);
163 if (BIO_sock_should_retry(ret)) {
164 BIO_set_retry_special(b);
165 c->state = BIO_CONN_S_BLOCKED_CONNECT;
166 b->retry_reason = BIO_RR_CONNECT;
168 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
171 * if there are more addresses to try, do that first
173 BIO_closesocket(b->num);
174 c->state = BIO_CONN_S_CREATE_SOCKET;
178 ERR_clear_last_mark();
179 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
180 "calling connect(%s, %s)",
181 c->param_hostname, c->param_service);
182 c->state = BIO_CONN_S_CONNECT_ERROR;
187 ERR_clear_last_mark();
188 c->state = BIO_CONN_S_OK;
192 case BIO_CONN_S_BLOCKED_CONNECT:
193 i = BIO_sock_error(b->num);
195 BIO_clear_retry_flags(b);
196 if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
198 * if there are more addresses to try, do that first
200 BIO_closesocket(b->num);
201 c->state = BIO_CONN_S_CREATE_SOCKET;
204 ERR_raise_data(ERR_LIB_SYS, i,
205 "calling connect(%s, %s)",
206 c->param_hostname, c->param_service);
207 ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
211 c->state = BIO_CONN_S_OK;
214 case BIO_CONN_S_CONNECT_ERROR:
215 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
228 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
233 /* Loop does not exit */
236 ret = cb((BIO *)b, c->state, ret);
241 BIO_CONNECT *BIO_CONNECT_new(void)
245 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
246 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
249 ret->state = BIO_CONN_S_BEFORE;
250 ret->connect_family = BIO_FAMILY_IPANY;
254 void BIO_CONNECT_free(BIO_CONNECT *a)
258 OPENSSL_free(a->param_hostname);
259 OPENSSL_free(a->param_service);
260 BIO_ADDRINFO_free(a->addr_first);
264 const BIO_METHOD *BIO_s_connect(void)
266 return &methods_connectp;
269 static int conn_new(BIO *bi)
272 bi->num = (int)INVALID_SOCKET;
274 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
280 static void conn_close_socket(BIO *bio)
284 c = (BIO_CONNECT *)bio->ptr;
285 if (bio->num != (int)INVALID_SOCKET) {
286 /* Only do a shutdown if things were established */
287 if (c->state == BIO_CONN_S_OK)
288 shutdown(bio->num, 2);
289 BIO_closesocket(bio->num);
290 bio->num = (int)INVALID_SOCKET;
294 static int conn_free(BIO *a)
300 data = (BIO_CONNECT *)a->ptr;
303 conn_close_socket(a);
304 BIO_CONNECT_free(data);
312 static int conn_read(BIO *b, char *out, int outl)
317 data = (BIO_CONNECT *)b->ptr;
318 if (data->state != BIO_CONN_S_OK) {
319 ret = conn_state(b, data);
325 clear_socket_error();
326 # ifndef OPENSSL_NO_KTLS
327 if (BIO_get_ktls_recv(b))
328 ret = ktls_read_record(b->num, out, outl);
331 ret = readsocket(b->num, out, outl);
332 BIO_clear_retry_flags(b);
334 if (BIO_sock_should_retry(ret))
335 BIO_set_retry_read(b);
337 b->flags |= BIO_FLAGS_IN_EOF;
343 static int conn_write(BIO *b, const char *in, int inl)
348 data = (BIO_CONNECT *)b->ptr;
349 if (data->state != BIO_CONN_S_OK) {
350 ret = conn_state(b, data);
355 clear_socket_error();
356 # ifndef OPENSSL_NO_KTLS
357 if (BIO_should_ktls_ctrl_msg_flag(b)) {
358 ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
361 BIO_clear_ktls_ctrl_msg_flag(b);
365 ret = writesocket(b->num, in, inl);
366 BIO_clear_retry_flags(b);
368 if (BIO_sock_should_retry(ret))
369 BIO_set_retry_write(b);
374 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
378 const char **pptr = NULL;
381 # ifndef OPENSSL_NO_KTLS
382 ktls_crypto_info_t *crypto_info;
385 data = (BIO_CONNECT *)b->ptr;
390 data->state = BIO_CONN_S_BEFORE;
391 conn_close_socket(b);
392 BIO_ADDRINFO_free(data->addr_first);
393 data->addr_first = NULL;
396 case BIO_C_DO_STATE_MACHINE:
397 /* use this one to start the connection */
398 if (data->state != BIO_CONN_S_OK)
399 ret = (long)conn_state(b, data);
403 case BIO_C_GET_CONNECT:
405 pptr = (const char **)ptr;
407 *pptr = data->param_hostname;
408 } else if (num == 1) {
409 *pptr = data->param_service;
410 } else if (num == 2) {
411 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
412 } else if (num == 3) {
413 switch (BIO_ADDRINFO_family(data->addr_iter)) {
416 ret = BIO_FAMILY_IPV6;
420 ret = BIO_FAMILY_IPV4;
423 ret = data->connect_family;
436 case BIO_C_SET_CONNECT:
439 if (num == 0) { /* BIO_set_conn_hostname */
440 char *hold_service = data->param_service;
441 /* We affect the hostname regardless. However, the input
442 * string might contain a host:service spec, so we must
443 * parse it, which might or might not affect the service
446 OPENSSL_free(data->param_hostname);
447 data->param_hostname = NULL;
448 ret = BIO_parse_hostserv(ptr,
449 &data->param_hostname,
450 &data->param_service,
451 BIO_PARSE_PRIO_HOST);
452 if (hold_service != data->param_service)
453 OPENSSL_free(hold_service);
454 } else if (num == 1) { /* BIO_set_conn_port */
455 OPENSSL_free(data->param_service);
456 if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
458 } else if (num == 2) { /* BIO_set_conn_address */
459 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
460 char *host = BIO_ADDR_hostname_string(addr, 1);
461 char *service = BIO_ADDR_service_string(addr, 1);
463 ret = host != NULL && service != NULL;
465 OPENSSL_free(data->param_hostname);
466 data->param_hostname = host;
467 OPENSSL_free(data->param_service);
468 data->param_service = service;
469 BIO_ADDRINFO_free(data->addr_first);
470 data->addr_first = NULL;
471 data->addr_iter = NULL;
474 OPENSSL_free(service);
476 } else if (num == 3) { /* BIO_set_conn_ip_family */
477 data->connect_family = *(int *)ptr;
485 data->connect_mode |= BIO_SOCK_NONBLOCK;
487 data->connect_mode &= ~BIO_SOCK_NONBLOCK;
489 case BIO_C_SET_CONNECT_MODE:
490 data->connect_mode = (int)num;
501 case BIO_CTRL_GET_CLOSE:
504 case BIO_CTRL_SET_CLOSE:
505 b->shutdown = (int)num;
507 case BIO_CTRL_PENDING:
508 case BIO_CTRL_WPENDING:
516 if (data->param_hostname)
517 BIO_set_conn_hostname(dbio, data->param_hostname);
518 if (data->param_service)
519 BIO_set_conn_port(dbio, data->param_service);
520 BIO_set_conn_ip_family(dbio, data->connect_family);
521 BIO_set_conn_mode(dbio, data->connect_mode);
523 * FIXME: the cast of the function seems unlikely to be a good
526 (void)BIO_set_info_callback(dbio, data->info_callback);
529 case BIO_CTRL_SET_CALLBACK:
530 ret = 0; /* use callback ctrl */
532 case BIO_CTRL_GET_CALLBACK:
536 fptr = (BIO_info_cb **)ptr;
537 *fptr = data->info_callback;
541 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
543 # ifndef OPENSSL_NO_KTLS
544 case BIO_CTRL_SET_KTLS:
545 crypto_info = (ktls_crypto_info_t *)ptr;
546 ret = ktls_start(b->num, crypto_info, num);
548 BIO_set_ktls_flag(b, num);
550 case BIO_CTRL_GET_KTLS_SEND:
551 return BIO_should_ktls_flag(b, 1) != 0;
552 case BIO_CTRL_GET_KTLS_RECV:
553 return BIO_should_ktls_flag(b, 0) != 0;
554 case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
555 BIO_set_ktls_ctrl_msg_flag(b);
556 data->record_type = num;
559 case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
560 BIO_clear_ktls_ctrl_msg_flag(b);
571 static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
576 data = (BIO_CONNECT *)b->ptr;
579 case BIO_CTRL_SET_CALLBACK:
581 data->info_callback = fp;
591 static int conn_puts(BIO *bp, const char *str)
596 ret = conn_write(bp, str, n);
600 BIO *BIO_new_connect(const char *str)
604 ret = BIO_new(BIO_s_connect());
607 if (BIO_set_conn_hostname(ret, str))