/src/openssl/crypto/bio/bss_conn.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2022 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 | | #include <stdio.h> |
11 | | #include <errno.h> |
12 | | |
13 | | #include "bio_local.h" |
14 | | #include "internal/bio_tfo.h" |
15 | | #include "internal/ktls.h" |
16 | | |
17 | | #ifndef OPENSSL_NO_SOCK |
18 | | |
19 | | typedef struct bio_connect_st { |
20 | | int state; |
21 | | int connect_family; |
22 | | char *param_hostname; |
23 | | char *param_service; |
24 | | int connect_mode; |
25 | | # ifndef OPENSSL_NO_KTLS |
26 | | unsigned char record_type; |
27 | | # endif |
28 | | int tfo_first; |
29 | | |
30 | | BIO_ADDRINFO *addr_first; |
31 | | const BIO_ADDRINFO *addr_iter; |
32 | | /* |
33 | | * int socket; this will be kept in bio->num so that it is compatible |
34 | | * with the bss_sock bio |
35 | | */ |
36 | | /* |
37 | | * called when the connection is initially made callback(BIO,state,ret); |
38 | | * The callback should return 'ret'. state is for compatibility with the |
39 | | * ssl info_callback |
40 | | */ |
41 | | BIO_info_cb *info_callback; |
42 | | } BIO_CONNECT; |
43 | | |
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 *); |
52 | | |
53 | | static int conn_state(BIO *b, BIO_CONNECT *c); |
54 | | static void conn_close_socket(BIO *data); |
55 | | BIO_CONNECT *BIO_CONNECT_new(void); |
56 | | void BIO_CONNECT_free(BIO_CONNECT *a); |
57 | | |
58 | 0 | #define BIO_CONN_S_BEFORE 1 |
59 | 0 | #define BIO_CONN_S_GET_ADDR 2 |
60 | 0 | #define BIO_CONN_S_CREATE_SOCKET 3 |
61 | 0 | #define BIO_CONN_S_CONNECT 4 |
62 | 0 | #define BIO_CONN_S_OK 5 |
63 | 0 | #define BIO_CONN_S_BLOCKED_CONNECT 6 |
64 | 0 | #define BIO_CONN_S_CONNECT_ERROR 7 |
65 | | |
66 | | static const BIO_METHOD methods_connectp = { |
67 | | BIO_TYPE_CONNECT, |
68 | | "socket connect", |
69 | | bwrite_conv, |
70 | | conn_write, |
71 | | bread_conv, |
72 | | conn_read, |
73 | | conn_puts, |
74 | | conn_gets, |
75 | | conn_ctrl, |
76 | | conn_new, |
77 | | conn_free, |
78 | | conn_callback_ctrl, |
79 | | }; |
80 | | |
81 | | static int conn_state(BIO *b, BIO_CONNECT *c) |
82 | 0 | { |
83 | 0 | int ret = -1, i; |
84 | 0 | BIO_info_cb *cb = NULL; |
85 | |
|
86 | 0 | if (c->info_callback != NULL) |
87 | 0 | cb = c->info_callback; |
88 | |
|
89 | 0 | for (;;) { |
90 | 0 | switch (c->state) { |
91 | 0 | case BIO_CONN_S_BEFORE: |
92 | 0 | if (c->param_hostname == NULL && c->param_service == NULL) { |
93 | 0 | ERR_raise_data(ERR_LIB_BIO, |
94 | 0 | BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED, |
95 | 0 | "hostname=%s service=%s", |
96 | 0 | c->param_hostname, c->param_service); |
97 | 0 | goto exit_loop; |
98 | 0 | } |
99 | 0 | c->state = BIO_CONN_S_GET_ADDR; |
100 | 0 | break; |
101 | | |
102 | 0 | case BIO_CONN_S_GET_ADDR: |
103 | 0 | { |
104 | 0 | int family = AF_UNSPEC; |
105 | 0 | switch (c->connect_family) { |
106 | 0 | case BIO_FAMILY_IPV6: |
107 | 0 | if (1) { /* This is a trick we use to avoid bit rot. |
108 | | * at least the "else" part will always be |
109 | | * compiled. |
110 | | */ |
111 | 0 | #if OPENSSL_USE_IPV6 |
112 | 0 | family = AF_INET6; |
113 | 0 | } else { |
114 | 0 | #endif |
115 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); |
116 | 0 | goto exit_loop; |
117 | 0 | } |
118 | 0 | break; |
119 | 0 | case BIO_FAMILY_IPV4: |
120 | 0 | family = AF_INET; |
121 | 0 | break; |
122 | 0 | case BIO_FAMILY_IPANY: |
123 | 0 | family = AF_UNSPEC; |
124 | 0 | break; |
125 | 0 | default: |
126 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); |
127 | 0 | goto exit_loop; |
128 | 0 | } |
129 | 0 | if (BIO_lookup(c->param_hostname, c->param_service, |
130 | 0 | BIO_LOOKUP_CLIENT, |
131 | 0 | family, SOCK_STREAM, &c->addr_first) == 0) |
132 | 0 | goto exit_loop; |
133 | 0 | } |
134 | 0 | if (c->addr_first == NULL) { |
135 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING); |
136 | 0 | goto exit_loop; |
137 | 0 | } |
138 | 0 | c->addr_iter = c->addr_first; |
139 | 0 | c->state = BIO_CONN_S_CREATE_SOCKET; |
140 | 0 | break; |
141 | | |
142 | 0 | case BIO_CONN_S_CREATE_SOCKET: |
143 | 0 | ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter), |
144 | 0 | BIO_ADDRINFO_socktype(c->addr_iter), |
145 | 0 | BIO_ADDRINFO_protocol(c->addr_iter), 0); |
146 | 0 | if (ret == (int)INVALID_SOCKET) { |
147 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
148 | 0 | "calling socket(%s, %s)", |
149 | 0 | c->param_hostname, c->param_service); |
150 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); |
151 | 0 | goto exit_loop; |
152 | 0 | } |
153 | 0 | b->num = ret; |
154 | 0 | c->state = BIO_CONN_S_CONNECT; |
155 | 0 | break; |
156 | | |
157 | 0 | case BIO_CONN_S_CONNECT: |
158 | 0 | BIO_clear_retry_flags(b); |
159 | 0 | ERR_set_mark(); |
160 | 0 | ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter), |
161 | 0 | BIO_SOCK_KEEPALIVE | c->connect_mode); |
162 | 0 | b->retry_reason = 0; |
163 | 0 | if (ret == 0) { |
164 | 0 | if (BIO_sock_should_retry(ret)) { |
165 | 0 | BIO_set_retry_special(b); |
166 | 0 | c->state = BIO_CONN_S_BLOCKED_CONNECT; |
167 | 0 | b->retry_reason = BIO_RR_CONNECT; |
168 | 0 | ERR_pop_to_mark(); |
169 | 0 | } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) |
170 | 0 | != NULL) { |
171 | | /* |
172 | | * if there are more addresses to try, do that first |
173 | | */ |
174 | 0 | BIO_closesocket(b->num); |
175 | 0 | c->state = BIO_CONN_S_CREATE_SOCKET; |
176 | 0 | ERR_pop_to_mark(); |
177 | 0 | break; |
178 | 0 | } else { |
179 | 0 | ERR_clear_last_mark(); |
180 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
181 | 0 | "calling connect(%s, %s)", |
182 | 0 | c->param_hostname, c->param_service); |
183 | 0 | c->state = BIO_CONN_S_CONNECT_ERROR; |
184 | 0 | break; |
185 | 0 | } |
186 | 0 | goto exit_loop; |
187 | 0 | } else { |
188 | 0 | ERR_clear_last_mark(); |
189 | 0 | c->state = BIO_CONN_S_OK; |
190 | 0 | } |
191 | 0 | break; |
192 | | |
193 | 0 | case BIO_CONN_S_BLOCKED_CONNECT: |
194 | | /* wait for socket being writable, before querying BIO_sock_error */ |
195 | 0 | if (BIO_socket_wait(b->num, 0, time(NULL)) == 0) |
196 | 0 | break; |
197 | 0 | i = BIO_sock_error(b->num); |
198 | 0 | if (i != 0) { |
199 | 0 | BIO_clear_retry_flags(b); |
200 | 0 | if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) { |
201 | | /* |
202 | | * if there are more addresses to try, do that first |
203 | | */ |
204 | 0 | BIO_closesocket(b->num); |
205 | 0 | c->state = BIO_CONN_S_CREATE_SOCKET; |
206 | 0 | break; |
207 | 0 | } |
208 | 0 | ERR_raise_data(ERR_LIB_SYS, i, |
209 | 0 | "calling connect(%s, %s)", |
210 | 0 | c->param_hostname, c->param_service); |
211 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR); |
212 | 0 | ret = 0; |
213 | 0 | goto exit_loop; |
214 | 0 | } else { |
215 | 0 | c->state = BIO_CONN_S_OK; |
216 | | # ifndef OPENSSL_NO_KTLS |
217 | | /* |
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. |
222 | | */ |
223 | | ktls_enable(b->num); |
224 | | # endif |
225 | 0 | } |
226 | 0 | break; |
227 | | |
228 | 0 | case BIO_CONN_S_CONNECT_ERROR: |
229 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); |
230 | 0 | ret = 0; |
231 | 0 | goto exit_loop; |
232 | | |
233 | 0 | case BIO_CONN_S_OK: |
234 | 0 | ret = 1; |
235 | 0 | goto exit_loop; |
236 | 0 | default: |
237 | | /* abort(); */ |
238 | 0 | goto exit_loop; |
239 | 0 | } |
240 | | |
241 | 0 | if (cb != NULL) { |
242 | 0 | if ((ret = cb((BIO *)b, c->state, ret)) == 0) |
243 | 0 | goto end; |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | | /* Loop does not exit */ |
248 | 0 | exit_loop: |
249 | 0 | if (cb != NULL) |
250 | 0 | ret = cb((BIO *)b, c->state, ret); |
251 | 0 | end: |
252 | 0 | return ret; |
253 | 0 | } |
254 | | |
255 | | BIO_CONNECT *BIO_CONNECT_new(void) |
256 | 0 | { |
257 | 0 | BIO_CONNECT *ret; |
258 | |
|
259 | 0 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) |
260 | 0 | return NULL; |
261 | 0 | ret->state = BIO_CONN_S_BEFORE; |
262 | 0 | ret->connect_family = BIO_FAMILY_IPANY; |
263 | 0 | return ret; |
264 | 0 | } |
265 | | |
266 | | void BIO_CONNECT_free(BIO_CONNECT *a) |
267 | 0 | { |
268 | 0 | if (a == NULL) |
269 | 0 | return; |
270 | 0 | OPENSSL_free(a->param_hostname); |
271 | 0 | OPENSSL_free(a->param_service); |
272 | 0 | BIO_ADDRINFO_free(a->addr_first); |
273 | 0 | OPENSSL_free(a); |
274 | 0 | } |
275 | | |
276 | | const BIO_METHOD *BIO_s_connect(void) |
277 | 0 | { |
278 | 0 | return &methods_connectp; |
279 | 0 | } |
280 | | |
281 | | static int conn_new(BIO *bi) |
282 | 0 | { |
283 | 0 | bi->init = 0; |
284 | 0 | bi->num = (int)INVALID_SOCKET; |
285 | 0 | bi->flags = 0; |
286 | 0 | if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) |
287 | 0 | return 0; |
288 | 0 | else |
289 | 0 | return 1; |
290 | 0 | } |
291 | | |
292 | | static void conn_close_socket(BIO *bio) |
293 | 0 | { |
294 | 0 | BIO_CONNECT *c; |
295 | |
|
296 | 0 | c = (BIO_CONNECT *)bio->ptr; |
297 | 0 | if (bio->num != (int)INVALID_SOCKET) { |
298 | | /* Only do a shutdown if things were established */ |
299 | 0 | if (c->state == BIO_CONN_S_OK) |
300 | 0 | shutdown(bio->num, 2); |
301 | 0 | BIO_closesocket(bio->num); |
302 | 0 | bio->num = (int)INVALID_SOCKET; |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | | static int conn_free(BIO *a) |
307 | 0 | { |
308 | 0 | BIO_CONNECT *data; |
309 | |
|
310 | 0 | if (a == NULL) |
311 | 0 | return 0; |
312 | 0 | data = (BIO_CONNECT *)a->ptr; |
313 | |
|
314 | 0 | if (a->shutdown) { |
315 | 0 | conn_close_socket(a); |
316 | 0 | BIO_CONNECT_free(data); |
317 | 0 | a->ptr = NULL; |
318 | 0 | a->flags = 0; |
319 | 0 | a->init = 0; |
320 | 0 | } |
321 | 0 | return 1; |
322 | 0 | } |
323 | | |
324 | | static int conn_read(BIO *b, char *out, int outl) |
325 | 0 | { |
326 | 0 | int ret = 0; |
327 | 0 | BIO_CONNECT *data; |
328 | |
|
329 | 0 | data = (BIO_CONNECT *)b->ptr; |
330 | 0 | if (data->state != BIO_CONN_S_OK) { |
331 | 0 | ret = conn_state(b, data); |
332 | 0 | if (ret <= 0) |
333 | 0 | return ret; |
334 | 0 | } |
335 | | |
336 | 0 | if (out != NULL) { |
337 | 0 | clear_socket_error(); |
338 | | # ifndef OPENSSL_NO_KTLS |
339 | | if (BIO_get_ktls_recv(b)) |
340 | | ret = ktls_read_record(b->num, out, outl); |
341 | | else |
342 | | # endif |
343 | 0 | ret = readsocket(b->num, out, outl); |
344 | 0 | BIO_clear_retry_flags(b); |
345 | 0 | if (ret <= 0) { |
346 | 0 | if (BIO_sock_should_retry(ret)) |
347 | 0 | BIO_set_retry_read(b); |
348 | 0 | else if (ret == 0) |
349 | 0 | b->flags |= BIO_FLAGS_IN_EOF; |
350 | 0 | } |
351 | 0 | } |
352 | 0 | return ret; |
353 | 0 | } |
354 | | |
355 | | static int conn_write(BIO *b, const char *in, int inl) |
356 | 0 | { |
357 | 0 | int ret; |
358 | 0 | BIO_CONNECT *data; |
359 | |
|
360 | 0 | data = (BIO_CONNECT *)b->ptr; |
361 | 0 | if (data->state != BIO_CONN_S_OK) { |
362 | 0 | ret = conn_state(b, data); |
363 | 0 | if (ret <= 0) |
364 | 0 | return ret; |
365 | 0 | } |
366 | | |
367 | 0 | 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); |
371 | | if (ret >= 0) { |
372 | | ret = inl; |
373 | | BIO_clear_ktls_ctrl_msg_flag(b); |
374 | | } |
375 | | } else |
376 | | # endif |
377 | | # if defined(OSSL_TFO_SENDTO) |
378 | | if (data->tfo_first) { |
379 | | int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter); |
380 | | |
381 | | ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO, |
382 | | BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen); |
383 | | data->tfo_first = 0; |
384 | | } else |
385 | | # endif |
386 | 0 | ret = writesocket(b->num, in, inl); |
387 | 0 | BIO_clear_retry_flags(b); |
388 | 0 | if (ret <= 0) { |
389 | 0 | if (BIO_sock_should_retry(ret)) |
390 | 0 | BIO_set_retry_write(b); |
391 | 0 | } |
392 | 0 | return ret; |
393 | 0 | } |
394 | | |
395 | | static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) |
396 | 0 | { |
397 | 0 | BIO *dbio; |
398 | 0 | int *ip; |
399 | 0 | const char **pptr = NULL; |
400 | 0 | long ret = 1; |
401 | 0 | BIO_CONNECT *data; |
402 | | # ifndef OPENSSL_NO_KTLS |
403 | | ktls_crypto_info_t *crypto_info; |
404 | | # endif |
405 | |
|
406 | 0 | data = (BIO_CONNECT *)b->ptr; |
407 | |
|
408 | 0 | switch (cmd) { |
409 | 0 | case BIO_CTRL_RESET: |
410 | 0 | ret = 0; |
411 | 0 | data->state = BIO_CONN_S_BEFORE; |
412 | 0 | conn_close_socket(b); |
413 | 0 | BIO_ADDRINFO_free(data->addr_first); |
414 | 0 | data->addr_first = NULL; |
415 | 0 | b->flags = 0; |
416 | 0 | break; |
417 | 0 | case BIO_C_DO_STATE_MACHINE: |
418 | | /* use this one to start the connection */ |
419 | 0 | if (data->state != BIO_CONN_S_OK) |
420 | 0 | ret = (long)conn_state(b, data); |
421 | 0 | else |
422 | 0 | ret = 1; |
423 | 0 | break; |
424 | 0 | case BIO_C_GET_CONNECT: |
425 | 0 | if (ptr != NULL) { |
426 | 0 | pptr = (const char **)ptr; |
427 | 0 | if (num == 0) { |
428 | 0 | *pptr = data->param_hostname; |
429 | 0 | } else if (num == 1) { |
430 | 0 | *pptr = data->param_service; |
431 | 0 | } else if (num == 2) { |
432 | 0 | *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter); |
433 | 0 | } else if (num == 3) { |
434 | 0 | switch (BIO_ADDRINFO_family(data->addr_iter)) { |
435 | 0 | # if OPENSSL_USE_IPV6 |
436 | 0 | case AF_INET6: |
437 | 0 | ret = BIO_FAMILY_IPV6; |
438 | 0 | break; |
439 | 0 | # endif |
440 | 0 | case AF_INET: |
441 | 0 | ret = BIO_FAMILY_IPV4; |
442 | 0 | break; |
443 | 0 | case 0: |
444 | 0 | ret = data->connect_family; |
445 | 0 | break; |
446 | 0 | default: |
447 | 0 | ret = -1; |
448 | 0 | break; |
449 | 0 | } |
450 | 0 | } else if (num == 4) { |
451 | 0 | ret = data->connect_mode; |
452 | 0 | } else { |
453 | 0 | ret = 0; |
454 | 0 | } |
455 | 0 | } else { |
456 | 0 | ret = 0; |
457 | 0 | } |
458 | 0 | break; |
459 | 0 | case BIO_C_SET_CONNECT: |
460 | 0 | if (ptr != NULL) { |
461 | 0 | b->init = 1; |
462 | 0 | if (num == 0) { /* BIO_set_conn_hostname */ |
463 | 0 | 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 |
467 | | */ |
468 | |
|
469 | 0 | OPENSSL_free(data->param_hostname); |
470 | 0 | data->param_hostname = NULL; |
471 | 0 | ret = BIO_parse_hostserv(ptr, |
472 | 0 | &data->param_hostname, |
473 | 0 | &data->param_service, |
474 | 0 | BIO_PARSE_PRIO_HOST); |
475 | 0 | if (hold_service != data->param_service) |
476 | 0 | OPENSSL_free(hold_service); |
477 | 0 | } else if (num == 1) { /* BIO_set_conn_port */ |
478 | 0 | OPENSSL_free(data->param_service); |
479 | 0 | if ((data->param_service = OPENSSL_strdup(ptr)) == NULL) |
480 | 0 | ret = 0; |
481 | 0 | } else if (num == 2) { /* BIO_set_conn_address */ |
482 | 0 | const BIO_ADDR *addr = (const BIO_ADDR *)ptr; |
483 | 0 | char *host = BIO_ADDR_hostname_string(addr, 1); |
484 | 0 | char *service = BIO_ADDR_service_string(addr, 1); |
485 | |
|
486 | 0 | ret = host != NULL && service != NULL; |
487 | 0 | if (ret) { |
488 | 0 | OPENSSL_free(data->param_hostname); |
489 | 0 | data->param_hostname = host; |
490 | 0 | OPENSSL_free(data->param_service); |
491 | 0 | data->param_service = service; |
492 | 0 | BIO_ADDRINFO_free(data->addr_first); |
493 | 0 | data->addr_first = NULL; |
494 | 0 | data->addr_iter = NULL; |
495 | 0 | } else { |
496 | 0 | OPENSSL_free(host); |
497 | 0 | OPENSSL_free(service); |
498 | 0 | } |
499 | 0 | } else if (num == 3) { /* BIO_set_conn_ip_family */ |
500 | 0 | data->connect_family = *(int *)ptr; |
501 | 0 | } else { |
502 | 0 | ret = 0; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | break; |
506 | 0 | case BIO_C_SET_NBIO: |
507 | 0 | if (num != 0) |
508 | 0 | data->connect_mode |= BIO_SOCK_NONBLOCK; |
509 | 0 | else |
510 | 0 | data->connect_mode &= ~BIO_SOCK_NONBLOCK; |
511 | 0 | break; |
512 | | #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO) |
513 | | case BIO_C_SET_TFO: |
514 | | if (num != 0) { |
515 | | data->connect_mode |= BIO_SOCK_TFO; |
516 | | data->tfo_first = 1; |
517 | | } else { |
518 | | data->connect_mode &= ~BIO_SOCK_TFO; |
519 | | data->tfo_first = 0; |
520 | | } |
521 | | break; |
522 | | #endif |
523 | 0 | case BIO_C_SET_CONNECT_MODE: |
524 | 0 | data->connect_mode = (int)num; |
525 | 0 | if (num & BIO_SOCK_TFO) |
526 | 0 | data->tfo_first = 1; |
527 | 0 | else |
528 | 0 | data->tfo_first = 0; |
529 | 0 | break; |
530 | 0 | case BIO_C_GET_FD: |
531 | 0 | if (b->init) { |
532 | 0 | ip = (int *)ptr; |
533 | 0 | if (ip != NULL) |
534 | 0 | *ip = b->num; |
535 | 0 | ret = b->num; |
536 | 0 | } else |
537 | 0 | ret = -1; |
538 | 0 | break; |
539 | 0 | case BIO_CTRL_GET_CLOSE: |
540 | 0 | ret = b->shutdown; |
541 | 0 | break; |
542 | 0 | case BIO_CTRL_SET_CLOSE: |
543 | 0 | b->shutdown = (int)num; |
544 | 0 | break; |
545 | 0 | case BIO_CTRL_PENDING: |
546 | 0 | case BIO_CTRL_WPENDING: |
547 | 0 | ret = 0; |
548 | 0 | break; |
549 | 0 | case BIO_CTRL_FLUSH: |
550 | 0 | break; |
551 | 0 | case BIO_CTRL_DUP: |
552 | 0 | { |
553 | 0 | dbio = (BIO *)ptr; |
554 | 0 | if (data->param_hostname) |
555 | 0 | BIO_set_conn_hostname(dbio, data->param_hostname); |
556 | 0 | if (data->param_service) |
557 | 0 | BIO_set_conn_port(dbio, data->param_service); |
558 | 0 | BIO_set_conn_ip_family(dbio, data->connect_family); |
559 | 0 | BIO_set_conn_mode(dbio, data->connect_mode); |
560 | | /* |
561 | | * FIXME: the cast of the function seems unlikely to be a good |
562 | | * idea |
563 | | */ |
564 | 0 | (void)BIO_set_info_callback(dbio, data->info_callback); |
565 | 0 | } |
566 | 0 | break; |
567 | 0 | case BIO_CTRL_SET_CALLBACK: |
568 | 0 | ret = 0; /* use callback ctrl */ |
569 | 0 | break; |
570 | 0 | case BIO_CTRL_GET_CALLBACK: |
571 | 0 | { |
572 | 0 | BIO_info_cb **fptr; |
573 | |
|
574 | 0 | fptr = (BIO_info_cb **)ptr; |
575 | 0 | *fptr = data->info_callback; |
576 | 0 | } |
577 | 0 | break; |
578 | 0 | case BIO_CTRL_EOF: |
579 | 0 | ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; |
580 | 0 | break; |
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); |
585 | | if (ret) |
586 | | BIO_set_ktls_flag(b, num); |
587 | | break; |
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; |
595 | | ret = 0; |
596 | | break; |
597 | | case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG: |
598 | | BIO_clear_ktls_ctrl_msg_flag(b); |
599 | | ret = 0; |
600 | | break; |
601 | | case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE: |
602 | | ret = ktls_enable_tx_zerocopy_sendfile(b->num); |
603 | | if (ret) |
604 | | BIO_set_ktls_zerocopy_sendfile_flag(b); |
605 | | break; |
606 | | # endif |
607 | 0 | default: |
608 | 0 | ret = 0; |
609 | 0 | break; |
610 | 0 | } |
611 | 0 | return ret; |
612 | 0 | } |
613 | | |
614 | | static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
615 | 0 | { |
616 | 0 | long ret = 1; |
617 | 0 | BIO_CONNECT *data; |
618 | |
|
619 | 0 | data = (BIO_CONNECT *)b->ptr; |
620 | |
|
621 | 0 | switch (cmd) { |
622 | 0 | case BIO_CTRL_SET_CALLBACK: |
623 | 0 | { |
624 | 0 | data->info_callback = fp; |
625 | 0 | } |
626 | 0 | break; |
627 | 0 | default: |
628 | 0 | ret = 0; |
629 | 0 | break; |
630 | 0 | } |
631 | 0 | return ret; |
632 | 0 | } |
633 | | |
634 | | static int conn_puts(BIO *bp, const char *str) |
635 | 0 | { |
636 | 0 | int n, ret; |
637 | |
|
638 | 0 | n = strlen(str); |
639 | 0 | ret = conn_write(bp, str, n); |
640 | 0 | return ret; |
641 | 0 | } |
642 | | |
643 | | int conn_gets(BIO *bio, char *buf, int size) |
644 | 0 | { |
645 | 0 | BIO_CONNECT *data; |
646 | 0 | char *ptr = buf; |
647 | 0 | int ret = 0; |
648 | |
|
649 | 0 | if (buf == NULL) { |
650 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
651 | 0 | return -1; |
652 | 0 | } |
653 | 0 | if (size <= 0) { |
654 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); |
655 | 0 | return -1; |
656 | 0 | } |
657 | 0 | *buf = '\0'; |
658 | |
|
659 | 0 | if (bio == NULL || bio->ptr == NULL) { |
660 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
661 | 0 | return -1; |
662 | 0 | } |
663 | 0 | data = (BIO_CONNECT *)bio->ptr; |
664 | 0 | if (data->state != BIO_CONN_S_OK) { |
665 | 0 | ret = conn_state(bio, data); |
666 | 0 | if (ret <= 0) |
667 | 0 | return ret; |
668 | 0 | } |
669 | | |
670 | 0 | clear_socket_error(); |
671 | 0 | while (size-- > 1) { |
672 | | # ifndef OPENSSL_NO_KTLS |
673 | | if (BIO_get_ktls_recv(bio)) |
674 | | ret = ktls_read_record(bio->num, ptr, 1); |
675 | | else |
676 | | # endif |
677 | 0 | ret = readsocket(bio->num, ptr, 1); |
678 | 0 | BIO_clear_retry_flags(bio); |
679 | 0 | if (ret <= 0) { |
680 | 0 | if (BIO_sock_should_retry(ret)) |
681 | 0 | BIO_set_retry_read(bio); |
682 | 0 | else if (ret == 0) |
683 | 0 | bio->flags |= BIO_FLAGS_IN_EOF; |
684 | 0 | break; |
685 | 0 | } |
686 | 0 | if (*ptr++ == '\n') |
687 | 0 | break; |
688 | 0 | } |
689 | 0 | *ptr = '\0'; |
690 | 0 | return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret; |
691 | 0 | } |
692 | | |
693 | | BIO *BIO_new_connect(const char *str) |
694 | 0 | { |
695 | 0 | BIO *ret; |
696 | |
|
697 | 0 | ret = BIO_new(BIO_s_connect()); |
698 | 0 | if (ret == NULL) |
699 | 0 | return NULL; |
700 | 0 | if (BIO_set_conn_hostname(ret, str)) |
701 | 0 | return ret; |
702 | 0 | BIO_free(ret); |
703 | 0 | return NULL; |
704 | 0 | } |
705 | | |
706 | | #endif |