1 | /*
|
---|
2 | * Copyright 1995-2024 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 | int connect_sock_type;
|
---|
23 | char *param_hostname;
|
---|
24 | char *param_service;
|
---|
25 | int connect_mode;
|
---|
26 | # ifndef OPENSSL_NO_KTLS
|
---|
27 | unsigned char record_type;
|
---|
28 | # endif
|
---|
29 | int tfo_first;
|
---|
30 |
|
---|
31 | BIO_ADDRINFO *addr_first;
|
---|
32 | const BIO_ADDRINFO *addr_iter;
|
---|
33 | /*
|
---|
34 | * int socket; this will be kept in bio->num so that it is compatible
|
---|
35 | * with the bss_sock bio
|
---|
36 | */
|
---|
37 | /*
|
---|
38 | * called when the connection is initially made callback(BIO,state,ret);
|
---|
39 | * The callback should return 'ret'. state is for compatibility with the
|
---|
40 | * ssl info_callback
|
---|
41 | */
|
---|
42 | BIO_info_cb *info_callback;
|
---|
43 | /*
|
---|
44 | * Used when connect_sock_type is SOCK_DGRAM. Owned by us; we forward
|
---|
45 | * read/write(mmsg) calls to this if present.
|
---|
46 | */
|
---|
47 | BIO *dgram_bio;
|
---|
48 | } BIO_CONNECT;
|
---|
49 |
|
---|
50 | static int conn_write(BIO *h, const char *buf, int num);
|
---|
51 | static int conn_read(BIO *h, char *buf, int size);
|
---|
52 | static int conn_puts(BIO *h, const char *str);
|
---|
53 | static int conn_gets(BIO *h, char *buf, int size);
|
---|
54 | static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
---|
55 | static int conn_new(BIO *h);
|
---|
56 | static int conn_free(BIO *data);
|
---|
57 | static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
|
---|
58 | static int conn_sendmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
|
---|
59 | uint64_t f, size_t *mp);
|
---|
60 | static int conn_recvmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
|
---|
61 | uint64_t f, size_t *mp);
|
---|
62 |
|
---|
63 | static int conn_state(BIO *b, BIO_CONNECT *c);
|
---|
64 | static void conn_close_socket(BIO *data);
|
---|
65 | static BIO_CONNECT *BIO_CONNECT_new(void);
|
---|
66 | static void BIO_CONNECT_free(BIO_CONNECT *a);
|
---|
67 |
|
---|
68 | #define BIO_CONN_S_BEFORE 1
|
---|
69 | #define BIO_CONN_S_GET_ADDR 2
|
---|
70 | #define BIO_CONN_S_CREATE_SOCKET 3
|
---|
71 | #define BIO_CONN_S_CONNECT 4
|
---|
72 | #define BIO_CONN_S_OK 5
|
---|
73 | #define BIO_CONN_S_BLOCKED_CONNECT 6
|
---|
74 | #define BIO_CONN_S_CONNECT_ERROR 7
|
---|
75 |
|
---|
76 | static const BIO_METHOD methods_connectp = {
|
---|
77 | BIO_TYPE_CONNECT,
|
---|
78 | "socket connect",
|
---|
79 | bwrite_conv,
|
---|
80 | conn_write,
|
---|
81 | bread_conv,
|
---|
82 | conn_read,
|
---|
83 | conn_puts,
|
---|
84 | conn_gets,
|
---|
85 | conn_ctrl,
|
---|
86 | conn_new,
|
---|
87 | conn_free,
|
---|
88 | conn_callback_ctrl,
|
---|
89 | conn_sendmmsg,
|
---|
90 | conn_recvmmsg,
|
---|
91 | };
|
---|
92 |
|
---|
93 | static int conn_create_dgram_bio(BIO *b, BIO_CONNECT *c)
|
---|
94 | {
|
---|
95 | if (c->connect_sock_type != SOCK_DGRAM)
|
---|
96 | return 1;
|
---|
97 |
|
---|
98 | #ifndef OPENSSL_NO_DGRAM
|
---|
99 | c->dgram_bio = BIO_new_dgram(b->num, 0);
|
---|
100 | if (c->dgram_bio == NULL)
|
---|
101 | goto err;
|
---|
102 |
|
---|
103 | return 1;
|
---|
104 |
|
---|
105 | err:
|
---|
106 | #endif
|
---|
107 | c->state = BIO_CONN_S_CONNECT_ERROR;
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static int conn_state(BIO *b, BIO_CONNECT *c)
|
---|
112 | {
|
---|
113 | int ret = -1, i, opts;
|
---|
114 | BIO_info_cb *cb = NULL;
|
---|
115 |
|
---|
116 | if (c->info_callback != NULL)
|
---|
117 | cb = c->info_callback;
|
---|
118 |
|
---|
119 | for (;;) {
|
---|
120 | switch (c->state) {
|
---|
121 | case BIO_CONN_S_BEFORE:
|
---|
122 | if (c->param_hostname == NULL && c->param_service == NULL) {
|
---|
123 | ERR_raise_data(ERR_LIB_BIO,
|
---|
124 | BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
|
---|
125 | "hostname=%s service=%s",
|
---|
126 | c->param_hostname, c->param_service);
|
---|
127 | goto exit_loop;
|
---|
128 | }
|
---|
129 | c->state = BIO_CONN_S_GET_ADDR;
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case BIO_CONN_S_GET_ADDR:
|
---|
133 | {
|
---|
134 | int family = AF_UNSPEC;
|
---|
135 | switch (c->connect_family) {
|
---|
136 | case BIO_FAMILY_IPV6:
|
---|
137 | if (1) { /* This is a trick we use to avoid bit rot.
|
---|
138 | * at least the "else" part will always be
|
---|
139 | * compiled.
|
---|
140 | */
|
---|
141 | #if OPENSSL_USE_IPV6
|
---|
142 | family = AF_INET6;
|
---|
143 | } else {
|
---|
144 | #endif
|
---|
145 | ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
|
---|
146 | goto exit_loop;
|
---|
147 | }
|
---|
148 | break;
|
---|
149 | case BIO_FAMILY_IPV4:
|
---|
150 | family = AF_INET;
|
---|
151 | break;
|
---|
152 | case BIO_FAMILY_IPANY:
|
---|
153 | family = AF_UNSPEC;
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
|
---|
157 | goto exit_loop;
|
---|
158 | }
|
---|
159 | if (BIO_lookup(c->param_hostname, c->param_service,
|
---|
160 | BIO_LOOKUP_CLIENT,
|
---|
161 | family, c->connect_sock_type,
|
---|
162 | &c->addr_first) == 0)
|
---|
163 | goto exit_loop;
|
---|
164 | }
|
---|
165 | if (c->addr_first == NULL) {
|
---|
166 | ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
|
---|
167 | goto exit_loop;
|
---|
168 | }
|
---|
169 | c->addr_iter = c->addr_first;
|
---|
170 | c->state = BIO_CONN_S_CREATE_SOCKET;
|
---|
171 | break;
|
---|
172 |
|
---|
173 | case BIO_CONN_S_CREATE_SOCKET:
|
---|
174 | ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
|
---|
175 | BIO_ADDRINFO_socktype(c->addr_iter),
|
---|
176 | BIO_ADDRINFO_protocol(c->addr_iter), 0);
|
---|
177 | if (ret == (int)INVALID_SOCKET) {
|
---|
178 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
|
---|
179 | "calling socket(%s, %s)",
|
---|
180 | c->param_hostname, c->param_service);
|
---|
181 | ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
|
---|
182 | goto exit_loop;
|
---|
183 | }
|
---|
184 | b->num = ret;
|
---|
185 | c->state = BIO_CONN_S_CONNECT;
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case BIO_CONN_S_CONNECT:
|
---|
189 | BIO_clear_retry_flags(b);
|
---|
190 | ERR_set_mark();
|
---|
191 |
|
---|
192 | opts = c->connect_mode;
|
---|
193 | if (BIO_ADDRINFO_socktype(c->addr_iter) == SOCK_STREAM)
|
---|
194 | opts |= BIO_SOCK_KEEPALIVE;
|
---|
195 |
|
---|
196 | ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter), opts);
|
---|
197 | b->retry_reason = 0;
|
---|
198 | if (ret == 0) {
|
---|
199 | if (BIO_sock_should_retry(ret)) {
|
---|
200 | BIO_set_retry_special(b);
|
---|
201 | c->state = BIO_CONN_S_BLOCKED_CONNECT;
|
---|
202 | b->retry_reason = BIO_RR_CONNECT;
|
---|
203 | ERR_pop_to_mark();
|
---|
204 | } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
|
---|
205 | != NULL) {
|
---|
206 | /*
|
---|
207 | * if there are more addresses to try, do that first
|
---|
208 | */
|
---|
209 | BIO_closesocket(b->num);
|
---|
210 | c->state = BIO_CONN_S_CREATE_SOCKET;
|
---|
211 | ERR_pop_to_mark();
|
---|
212 | break;
|
---|
213 | } else {
|
---|
214 | ERR_clear_last_mark();
|
---|
215 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
|
---|
216 | "calling connect(%s, %s)",
|
---|
217 | c->param_hostname, c->param_service);
|
---|
218 | c->state = BIO_CONN_S_CONNECT_ERROR;
|
---|
219 | break;
|
---|
220 | }
|
---|
221 | goto exit_loop;
|
---|
222 | } else {
|
---|
223 | ERR_clear_last_mark();
|
---|
224 | if (!conn_create_dgram_bio(b, c))
|
---|
225 | break;
|
---|
226 | c->state = BIO_CONN_S_OK;
|
---|
227 | }
|
---|
228 | break;
|
---|
229 |
|
---|
230 | case BIO_CONN_S_BLOCKED_CONNECT:
|
---|
231 | /* wait for socket being writable, before querying BIO_sock_error */
|
---|
232 | if (BIO_socket_wait(b->num, 0, time(NULL)) == 0)
|
---|
233 | break;
|
---|
234 | i = BIO_sock_error(b->num);
|
---|
235 | if (i != 0) {
|
---|
236 | BIO_clear_retry_flags(b);
|
---|
237 | if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
|
---|
238 | /*
|
---|
239 | * if there are more addresses to try, do that first
|
---|
240 | */
|
---|
241 | BIO_closesocket(b->num);
|
---|
242 | c->state = BIO_CONN_S_CREATE_SOCKET;
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | ERR_raise_data(ERR_LIB_SYS, i,
|
---|
246 | "calling connect(%s, %s)",
|
---|
247 | c->param_hostname, c->param_service);
|
---|
248 | ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
|
---|
249 | ret = 0;
|
---|
250 | goto exit_loop;
|
---|
251 | } else {
|
---|
252 | if (!conn_create_dgram_bio(b, c))
|
---|
253 | break;
|
---|
254 | c->state = BIO_CONN_S_OK;
|
---|
255 | # ifndef OPENSSL_NO_KTLS
|
---|
256 | /*
|
---|
257 | * The new socket is created successfully regardless of ktls_enable.
|
---|
258 | * ktls_enable doesn't change any functionality of the socket, except
|
---|
259 | * changing the setsockopt to enable the processing of ktls_start.
|
---|
260 | * Thus, it is not a problem to call it for non-TLS sockets.
|
---|
261 | */
|
---|
262 | ktls_enable(b->num);
|
---|
263 | # endif
|
---|
264 | }
|
---|
265 | break;
|
---|
266 |
|
---|
267 | case BIO_CONN_S_CONNECT_ERROR:
|
---|
268 | ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
|
---|
269 | ret = 0;
|
---|
270 | goto exit_loop;
|
---|
271 |
|
---|
272 | case BIO_CONN_S_OK:
|
---|
273 | ret = 1;
|
---|
274 | goto exit_loop;
|
---|
275 | default:
|
---|
276 | /* abort(); */
|
---|
277 | goto exit_loop;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (cb != NULL) {
|
---|
281 | if ((ret = cb((BIO *)b, c->state, ret)) == 0)
|
---|
282 | goto end;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Loop does not exit */
|
---|
287 | exit_loop:
|
---|
288 | if (cb != NULL)
|
---|
289 | ret = cb((BIO *)b, c->state, ret);
|
---|
290 | end:
|
---|
291 | return ret;
|
---|
292 | }
|
---|
293 |
|
---|
294 | static BIO_CONNECT *BIO_CONNECT_new(void)
|
---|
295 | {
|
---|
296 | BIO_CONNECT *ret;
|
---|
297 |
|
---|
298 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
|
---|
299 | return NULL;
|
---|
300 | ret->state = BIO_CONN_S_BEFORE;
|
---|
301 | ret->connect_family = BIO_FAMILY_IPANY;
|
---|
302 | ret->connect_sock_type = SOCK_STREAM;
|
---|
303 | return ret;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static void BIO_CONNECT_free(BIO_CONNECT *a)
|
---|
307 | {
|
---|
308 | if (a == NULL)
|
---|
309 | return;
|
---|
310 | OPENSSL_free(a->param_hostname);
|
---|
311 | OPENSSL_free(a->param_service);
|
---|
312 | BIO_ADDRINFO_free(a->addr_first);
|
---|
313 | OPENSSL_free(a);
|
---|
314 | }
|
---|
315 |
|
---|
316 | const BIO_METHOD *BIO_s_connect(void)
|
---|
317 | {
|
---|
318 | return &methods_connectp;
|
---|
319 | }
|
---|
320 |
|
---|
321 | static int conn_new(BIO *bi)
|
---|
322 | {
|
---|
323 | bi->init = 0;
|
---|
324 | bi->num = (int)INVALID_SOCKET;
|
---|
325 | bi->flags = 0;
|
---|
326 | if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
|
---|
327 | return 0;
|
---|
328 | else
|
---|
329 | return 1;
|
---|
330 | }
|
---|
331 |
|
---|
332 | static void conn_close_socket(BIO *bio)
|
---|
333 | {
|
---|
334 | BIO_CONNECT *c;
|
---|
335 |
|
---|
336 | c = (BIO_CONNECT *)bio->ptr;
|
---|
337 | if (bio->num != (int)INVALID_SOCKET) {
|
---|
338 | /* Only do a shutdown if things were established */
|
---|
339 | if (c->state == BIO_CONN_S_OK)
|
---|
340 | shutdown(bio->num, 2);
|
---|
341 | BIO_closesocket(bio->num);
|
---|
342 | bio->num = (int)INVALID_SOCKET;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | static int conn_free(BIO *a)
|
---|
347 | {
|
---|
348 | BIO_CONNECT *data;
|
---|
349 |
|
---|
350 | if (a == NULL)
|
---|
351 | return 0;
|
---|
352 | data = (BIO_CONNECT *)a->ptr;
|
---|
353 |
|
---|
354 | BIO_free(data->dgram_bio);
|
---|
355 |
|
---|
356 | if (a->shutdown) {
|
---|
357 | conn_close_socket(a);
|
---|
358 | BIO_CONNECT_free(data);
|
---|
359 | a->ptr = NULL;
|
---|
360 | a->flags = 0;
|
---|
361 | a->init = 0;
|
---|
362 | }
|
---|
363 | return 1;
|
---|
364 | }
|
---|
365 |
|
---|
366 | static int conn_read(BIO *b, char *out, int outl)
|
---|
367 | {
|
---|
368 | int ret = 0;
|
---|
369 | BIO_CONNECT *data;
|
---|
370 |
|
---|
371 | data = (BIO_CONNECT *)b->ptr;
|
---|
372 | if (data->state != BIO_CONN_S_OK) {
|
---|
373 | ret = conn_state(b, data);
|
---|
374 | if (ret <= 0)
|
---|
375 | return ret;
|
---|
376 | }
|
---|
377 |
|
---|
378 | if (data->dgram_bio != NULL) {
|
---|
379 | BIO_clear_retry_flags(b);
|
---|
380 | ret = BIO_read(data->dgram_bio, out, outl);
|
---|
381 | BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
|
---|
382 | return ret;
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (out != NULL) {
|
---|
386 | clear_socket_error();
|
---|
387 | # ifndef OPENSSL_NO_KTLS
|
---|
388 | if (BIO_get_ktls_recv(b))
|
---|
389 | ret = ktls_read_record(b->num, out, outl);
|
---|
390 | else
|
---|
391 | # endif
|
---|
392 | ret = readsocket(b->num, out, outl);
|
---|
393 | BIO_clear_retry_flags(b);
|
---|
394 | if (ret <= 0) {
|
---|
395 | if (BIO_sock_should_retry(ret))
|
---|
396 | BIO_set_retry_read(b);
|
---|
397 | else if (ret == 0)
|
---|
398 | b->flags |= BIO_FLAGS_IN_EOF;
|
---|
399 | }
|
---|
400 | }
|
---|
401 | return ret;
|
---|
402 | }
|
---|
403 |
|
---|
404 | static int conn_write(BIO *b, const char *in, int inl)
|
---|
405 | {
|
---|
406 | int ret;
|
---|
407 | BIO_CONNECT *data;
|
---|
408 |
|
---|
409 | data = (BIO_CONNECT *)b->ptr;
|
---|
410 | if (data->state != BIO_CONN_S_OK) {
|
---|
411 | ret = conn_state(b, data);
|
---|
412 | if (ret <= 0)
|
---|
413 | return ret;
|
---|
414 | }
|
---|
415 |
|
---|
416 | if (data->dgram_bio != NULL) {
|
---|
417 | BIO_clear_retry_flags(b);
|
---|
418 | ret = BIO_write(data->dgram_bio, in, inl);
|
---|
419 | BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
|
---|
420 | return ret;
|
---|
421 | }
|
---|
422 |
|
---|
423 | clear_socket_error();
|
---|
424 | # ifndef OPENSSL_NO_KTLS
|
---|
425 | if (BIO_should_ktls_ctrl_msg_flag(b)) {
|
---|
426 | ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
|
---|
427 | if (ret >= 0) {
|
---|
428 | ret = inl;
|
---|
429 | BIO_clear_ktls_ctrl_msg_flag(b);
|
---|
430 | }
|
---|
431 | } else
|
---|
432 | # endif
|
---|
433 | # if defined(OSSL_TFO_SENDTO)
|
---|
434 | if (data->tfo_first) {
|
---|
435 | int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter);
|
---|
436 |
|
---|
437 | ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
|
---|
438 | BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen);
|
---|
439 | data->tfo_first = 0;
|
---|
440 | } else
|
---|
441 | # endif
|
---|
442 | ret = writesocket(b->num, in, inl);
|
---|
443 | BIO_clear_retry_flags(b);
|
---|
444 | if (ret <= 0) {
|
---|
445 | if (BIO_sock_should_retry(ret))
|
---|
446 | BIO_set_retry_write(b);
|
---|
447 | }
|
---|
448 | return ret;
|
---|
449 | }
|
---|
450 |
|
---|
451 | static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
452 | {
|
---|
453 | BIO *dbio;
|
---|
454 | int *ip;
|
---|
455 | const char **pptr = NULL;
|
---|
456 | long ret = 1;
|
---|
457 | BIO_CONNECT *data;
|
---|
458 | const BIO_ADDR *dg_addr;
|
---|
459 | # ifndef OPENSSL_NO_KTLS
|
---|
460 | ktls_crypto_info_t *crypto_info;
|
---|
461 | # endif
|
---|
462 |
|
---|
463 | data = (BIO_CONNECT *)b->ptr;
|
---|
464 |
|
---|
465 | switch (cmd) {
|
---|
466 | case BIO_CTRL_RESET:
|
---|
467 | ret = 0;
|
---|
468 | data->state = BIO_CONN_S_BEFORE;
|
---|
469 | conn_close_socket(b);
|
---|
470 | BIO_ADDRINFO_free(data->addr_first);
|
---|
471 | data->addr_first = NULL;
|
---|
472 | b->flags = 0;
|
---|
473 | break;
|
---|
474 | case BIO_C_DO_STATE_MACHINE:
|
---|
475 | /* use this one to start the connection */
|
---|
476 | if (data->state != BIO_CONN_S_OK)
|
---|
477 | ret = (long)conn_state(b, data);
|
---|
478 | else
|
---|
479 | ret = 1;
|
---|
480 | break;
|
---|
481 | case BIO_C_GET_CONNECT:
|
---|
482 | if (ptr != NULL) {
|
---|
483 | pptr = (const char **)ptr;
|
---|
484 | if (num == 0) {
|
---|
485 | *pptr = data->param_hostname;
|
---|
486 | } else if (num == 1) {
|
---|
487 | *pptr = data->param_service;
|
---|
488 | } else if (num == 2) {
|
---|
489 | *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
|
---|
490 | } else if (num == 3) {
|
---|
491 | switch (BIO_ADDRINFO_family(data->addr_iter)) {
|
---|
492 | # if OPENSSL_USE_IPV6
|
---|
493 | case AF_INET6:
|
---|
494 | ret = BIO_FAMILY_IPV6;
|
---|
495 | break;
|
---|
496 | # endif
|
---|
497 | case AF_INET:
|
---|
498 | ret = BIO_FAMILY_IPV4;
|
---|
499 | break;
|
---|
500 | case 0:
|
---|
501 | ret = data->connect_family;
|
---|
502 | break;
|
---|
503 | default:
|
---|
504 | ret = -1;
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | } else if (num == 4) {
|
---|
508 | ret = data->connect_mode;
|
---|
509 | } else {
|
---|
510 | ret = 0;
|
---|
511 | }
|
---|
512 | } else {
|
---|
513 | ret = 0;
|
---|
514 | }
|
---|
515 | break;
|
---|
516 | case BIO_C_SET_CONNECT:
|
---|
517 | if (ptr != NULL) {
|
---|
518 | b->init = 1;
|
---|
519 | if (num == 0) { /* BIO_set_conn_hostname */
|
---|
520 | char *hold_service = data->param_service;
|
---|
521 | /* We affect the hostname regardless. However, the input
|
---|
522 | * string might contain a host:service spec, so we must
|
---|
523 | * parse it, which might or might not affect the service
|
---|
524 | */
|
---|
525 |
|
---|
526 | OPENSSL_free(data->param_hostname);
|
---|
527 | data->param_hostname = NULL;
|
---|
528 | ret = BIO_parse_hostserv(ptr,
|
---|
529 | &data->param_hostname,
|
---|
530 | &data->param_service,
|
---|
531 | BIO_PARSE_PRIO_HOST);
|
---|
532 | if (hold_service != data->param_service)
|
---|
533 | OPENSSL_free(hold_service);
|
---|
534 | } else if (num == 1) { /* BIO_set_conn_port */
|
---|
535 | OPENSSL_free(data->param_service);
|
---|
536 | if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
|
---|
537 | ret = 0;
|
---|
538 | } else if (num == 2) { /* BIO_set_conn_address */
|
---|
539 | const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
|
---|
540 | char *host = BIO_ADDR_hostname_string(addr, 1);
|
---|
541 | char *service = BIO_ADDR_service_string(addr, 1);
|
---|
542 |
|
---|
543 | ret = host != NULL && service != NULL;
|
---|
544 | if (ret) {
|
---|
545 | OPENSSL_free(data->param_hostname);
|
---|
546 | data->param_hostname = host;
|
---|
547 | OPENSSL_free(data->param_service);
|
---|
548 | data->param_service = service;
|
---|
549 | BIO_ADDRINFO_free(data->addr_first);
|
---|
550 | data->addr_first = NULL;
|
---|
551 | data->addr_iter = NULL;
|
---|
552 | } else {
|
---|
553 | OPENSSL_free(host);
|
---|
554 | OPENSSL_free(service);
|
---|
555 | }
|
---|
556 | } else if (num == 3) { /* BIO_set_conn_ip_family */
|
---|
557 | data->connect_family = *(int *)ptr;
|
---|
558 | } else {
|
---|
559 | ret = 0;
|
---|
560 | }
|
---|
561 | }
|
---|
562 | break;
|
---|
563 | case BIO_C_SET_SOCK_TYPE:
|
---|
564 | if ((num != SOCK_STREAM && num != SOCK_DGRAM)
|
---|
565 | || data->state >= BIO_CONN_S_GET_ADDR) {
|
---|
566 | ret = 0;
|
---|
567 | break;
|
---|
568 | }
|
---|
569 |
|
---|
570 | data->connect_sock_type = (int)num;
|
---|
571 | ret = 1;
|
---|
572 | break;
|
---|
573 | case BIO_C_GET_SOCK_TYPE:
|
---|
574 | ret = data->connect_sock_type;
|
---|
575 | break;
|
---|
576 | case BIO_C_GET_DGRAM_BIO:
|
---|
577 | if (data->dgram_bio != NULL) {
|
---|
578 | *(BIO **)ptr = data->dgram_bio;
|
---|
579 | ret = 1;
|
---|
580 | } else {
|
---|
581 | ret = 0;
|
---|
582 | }
|
---|
583 | break;
|
---|
584 | case BIO_CTRL_DGRAM_GET_PEER:
|
---|
585 | case BIO_CTRL_DGRAM_DETECT_PEER_ADDR:
|
---|
586 | if (data->state != BIO_CONN_S_OK)
|
---|
587 | conn_state(b, data); /* best effort */
|
---|
588 |
|
---|
589 | if (data->state >= BIO_CONN_S_CREATE_SOCKET
|
---|
590 | && data->addr_iter != NULL
|
---|
591 | && (dg_addr = BIO_ADDRINFO_address(data->addr_iter)) != NULL) {
|
---|
592 |
|
---|
593 | ret = BIO_ADDR_sockaddr_size(dg_addr);
|
---|
594 | if (num == 0 || num > ret)
|
---|
595 | num = ret;
|
---|
596 |
|
---|
597 | memcpy(ptr, dg_addr, num);
|
---|
598 | ret = num;
|
---|
599 | } else {
|
---|
600 | ret = 0;
|
---|
601 | }
|
---|
602 |
|
---|
603 | break;
|
---|
604 | case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
|
---|
605 | case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
|
---|
606 | {
|
---|
607 | BIO_POLL_DESCRIPTOR *pd = ptr;
|
---|
608 |
|
---|
609 | if (data->state != BIO_CONN_S_OK)
|
---|
610 | conn_state(b, data); /* best effort */
|
---|
611 |
|
---|
612 | if (data->state >= BIO_CONN_S_CREATE_SOCKET) {
|
---|
613 | pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
|
---|
614 | pd->value.fd = b->num;
|
---|
615 | } else {
|
---|
616 | ret = 0;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | break;
|
---|
620 | case BIO_C_SET_NBIO:
|
---|
621 | if (num != 0)
|
---|
622 | data->connect_mode |= BIO_SOCK_NONBLOCK;
|
---|
623 | else
|
---|
624 | data->connect_mode &= ~BIO_SOCK_NONBLOCK;
|
---|
625 |
|
---|
626 | if (data->dgram_bio != NULL)
|
---|
627 | ret = BIO_set_nbio(data->dgram_bio, num);
|
---|
628 |
|
---|
629 | break;
|
---|
630 | #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
|
---|
631 | case BIO_C_SET_TFO:
|
---|
632 | if (num != 0) {
|
---|
633 | data->connect_mode |= BIO_SOCK_TFO;
|
---|
634 | data->tfo_first = 1;
|
---|
635 | } else {
|
---|
636 | data->connect_mode &= ~BIO_SOCK_TFO;
|
---|
637 | data->tfo_first = 0;
|
---|
638 | }
|
---|
639 | break;
|
---|
640 | #endif
|
---|
641 | case BIO_C_SET_CONNECT_MODE:
|
---|
642 | data->connect_mode = (int)num;
|
---|
643 | if (num & BIO_SOCK_TFO)
|
---|
644 | data->tfo_first = 1;
|
---|
645 | else
|
---|
646 | data->tfo_first = 0;
|
---|
647 | break;
|
---|
648 | case BIO_C_GET_FD:
|
---|
649 | if (b->init) {
|
---|
650 | ip = (int *)ptr;
|
---|
651 | if (ip != NULL)
|
---|
652 | *ip = b->num;
|
---|
653 | ret = b->num;
|
---|
654 | } else
|
---|
655 | ret = -1;
|
---|
656 | break;
|
---|
657 | case BIO_CTRL_GET_CLOSE:
|
---|
658 | ret = b->shutdown;
|
---|
659 | break;
|
---|
660 | case BIO_CTRL_SET_CLOSE:
|
---|
661 | b->shutdown = (int)num;
|
---|
662 | break;
|
---|
663 | case BIO_CTRL_PENDING:
|
---|
664 | case BIO_CTRL_WPENDING:
|
---|
665 | ret = 0;
|
---|
666 | break;
|
---|
667 | case BIO_CTRL_FLUSH:
|
---|
668 | break;
|
---|
669 | case BIO_CTRL_DUP:
|
---|
670 | {
|
---|
671 | dbio = (BIO *)ptr;
|
---|
672 | if (data->param_hostname)
|
---|
673 | BIO_set_conn_hostname(dbio, data->param_hostname);
|
---|
674 | if (data->param_service)
|
---|
675 | BIO_set_conn_port(dbio, data->param_service);
|
---|
676 | BIO_set_conn_ip_family(dbio, data->connect_family);
|
---|
677 | BIO_set_conn_mode(dbio, data->connect_mode);
|
---|
678 | /*
|
---|
679 | * FIXME: the cast of the function seems unlikely to be a good
|
---|
680 | * idea
|
---|
681 | */
|
---|
682 | (void)BIO_set_info_callback(dbio, data->info_callback);
|
---|
683 | }
|
---|
684 | break;
|
---|
685 | case BIO_CTRL_SET_CALLBACK:
|
---|
686 | ret = 0; /* use callback ctrl */
|
---|
687 | break;
|
---|
688 | case BIO_CTRL_GET_CALLBACK:
|
---|
689 | {
|
---|
690 | BIO_info_cb **fptr;
|
---|
691 |
|
---|
692 | fptr = (BIO_info_cb **)ptr;
|
---|
693 | *fptr = data->info_callback;
|
---|
694 | }
|
---|
695 | break;
|
---|
696 | case BIO_CTRL_EOF:
|
---|
697 | ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
|
---|
698 | break;
|
---|
699 | # ifndef OPENSSL_NO_KTLS
|
---|
700 | case BIO_CTRL_SET_KTLS:
|
---|
701 | crypto_info = (ktls_crypto_info_t *)ptr;
|
---|
702 | ret = ktls_start(b->num, crypto_info, num);
|
---|
703 | if (ret)
|
---|
704 | BIO_set_ktls_flag(b, num);
|
---|
705 | break;
|
---|
706 | case BIO_CTRL_GET_KTLS_SEND:
|
---|
707 | return BIO_should_ktls_flag(b, 1) != 0;
|
---|
708 | case BIO_CTRL_GET_KTLS_RECV:
|
---|
709 | return BIO_should_ktls_flag(b, 0) != 0;
|
---|
710 | case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
|
---|
711 | BIO_set_ktls_ctrl_msg_flag(b);
|
---|
712 | data->record_type = num;
|
---|
713 | ret = 0;
|
---|
714 | break;
|
---|
715 | case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
|
---|
716 | BIO_clear_ktls_ctrl_msg_flag(b);
|
---|
717 | ret = 0;
|
---|
718 | break;
|
---|
719 | case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
|
---|
720 | ret = ktls_enable_tx_zerocopy_sendfile(b->num);
|
---|
721 | if (ret)
|
---|
722 | BIO_set_ktls_zerocopy_sendfile_flag(b);
|
---|
723 | break;
|
---|
724 | # endif
|
---|
725 | default:
|
---|
726 | ret = 0;
|
---|
727 | break;
|
---|
728 | }
|
---|
729 | return ret;
|
---|
730 | }
|
---|
731 |
|
---|
732 | static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
---|
733 | {
|
---|
734 | long ret = 1;
|
---|
735 | BIO_CONNECT *data;
|
---|
736 |
|
---|
737 | data = (BIO_CONNECT *)b->ptr;
|
---|
738 |
|
---|
739 | switch (cmd) {
|
---|
740 | case BIO_CTRL_SET_CALLBACK:
|
---|
741 | {
|
---|
742 | data->info_callback = fp;
|
---|
743 | }
|
---|
744 | break;
|
---|
745 | default:
|
---|
746 | ret = 0;
|
---|
747 | break;
|
---|
748 | }
|
---|
749 | return ret;
|
---|
750 | }
|
---|
751 |
|
---|
752 | static int conn_puts(BIO *bp, const char *str)
|
---|
753 | {
|
---|
754 | int n, ret;
|
---|
755 |
|
---|
756 | n = strlen(str);
|
---|
757 | ret = conn_write(bp, str, n);
|
---|
758 | return ret;
|
---|
759 | }
|
---|
760 |
|
---|
761 | int conn_gets(BIO *bio, char *buf, int size)
|
---|
762 | {
|
---|
763 | BIO_CONNECT *data;
|
---|
764 | char *ptr = buf;
|
---|
765 | int ret = 0;
|
---|
766 |
|
---|
767 | if (buf == NULL) {
|
---|
768 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
|
---|
769 | return -1;
|
---|
770 | }
|
---|
771 | if (size <= 0) {
|
---|
772 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
|
---|
773 | return -1;
|
---|
774 | }
|
---|
775 | *buf = '\0';
|
---|
776 |
|
---|
777 | if (bio == NULL || bio->ptr == NULL) {
|
---|
778 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
|
---|
779 | return -1;
|
---|
780 | }
|
---|
781 | data = (BIO_CONNECT *)bio->ptr;
|
---|
782 | if (data->state != BIO_CONN_S_OK) {
|
---|
783 | ret = conn_state(bio, data);
|
---|
784 | if (ret <= 0)
|
---|
785 | return ret;
|
---|
786 | }
|
---|
787 |
|
---|
788 | if (data->dgram_bio != NULL) {
|
---|
789 | ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
---|
790 | return -1;
|
---|
791 | }
|
---|
792 |
|
---|
793 | clear_socket_error();
|
---|
794 | while (size-- > 1) {
|
---|
795 | # ifndef OPENSSL_NO_KTLS
|
---|
796 | if (BIO_get_ktls_recv(bio))
|
---|
797 | ret = ktls_read_record(bio->num, ptr, 1);
|
---|
798 | else
|
---|
799 | # endif
|
---|
800 | ret = readsocket(bio->num, ptr, 1);
|
---|
801 | BIO_clear_retry_flags(bio);
|
---|
802 | if (ret <= 0) {
|
---|
803 | if (BIO_sock_should_retry(ret))
|
---|
804 | BIO_set_retry_read(bio);
|
---|
805 | else if (ret == 0)
|
---|
806 | bio->flags |= BIO_FLAGS_IN_EOF;
|
---|
807 | break;
|
---|
808 | }
|
---|
809 | if (*ptr++ == '\n')
|
---|
810 | break;
|
---|
811 | }
|
---|
812 | *ptr = '\0';
|
---|
813 | return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret;
|
---|
814 | }
|
---|
815 |
|
---|
816 | static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
|
---|
817 | uint64_t flags, size_t *msgs_processed)
|
---|
818 | {
|
---|
819 | int ret;
|
---|
820 | BIO_CONNECT *data;
|
---|
821 |
|
---|
822 | if (bio == NULL) {
|
---|
823 | *msgs_processed = 0;
|
---|
824 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
|
---|
825 | return 0;
|
---|
826 | }
|
---|
827 |
|
---|
828 | data = (BIO_CONNECT *)bio->ptr;
|
---|
829 | if (data->state != BIO_CONN_S_OK) {
|
---|
830 | ret = conn_state(bio, data);
|
---|
831 | if (ret <= 0) {
|
---|
832 | *msgs_processed = 0;
|
---|
833 | return 0;
|
---|
834 | }
|
---|
835 | }
|
---|
836 |
|
---|
837 | if (data->dgram_bio == NULL) {
|
---|
838 | *msgs_processed = 0;
|
---|
839 | ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
---|
840 | return 0;
|
---|
841 | }
|
---|
842 |
|
---|
843 | return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs,
|
---|
844 | flags, msgs_processed);
|
---|
845 | }
|
---|
846 |
|
---|
847 | static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
|
---|
848 | uint64_t flags, size_t *msgs_processed)
|
---|
849 | {
|
---|
850 | int ret;
|
---|
851 | BIO_CONNECT *data;
|
---|
852 |
|
---|
853 | if (bio == NULL) {
|
---|
854 | *msgs_processed = 0;
|
---|
855 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
|
---|
856 | return 0;
|
---|
857 | }
|
---|
858 |
|
---|
859 | data = (BIO_CONNECT *)bio->ptr;
|
---|
860 | if (data->state != BIO_CONN_S_OK) {
|
---|
861 | ret = conn_state(bio, data);
|
---|
862 | if (ret <= 0) {
|
---|
863 | *msgs_processed = 0;
|
---|
864 | return 0;
|
---|
865 | }
|
---|
866 | }
|
---|
867 |
|
---|
868 | if (data->dgram_bio == NULL) {
|
---|
869 | *msgs_processed = 0;
|
---|
870 | ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
---|
871 | return 0;
|
---|
872 | }
|
---|
873 |
|
---|
874 | return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs,
|
---|
875 | flags, msgs_processed);
|
---|
876 | }
|
---|
877 |
|
---|
878 | BIO *BIO_new_connect(const char *str)
|
---|
879 | {
|
---|
880 | BIO *ret;
|
---|
881 |
|
---|
882 | ret = BIO_new(BIO_s_connect());
|
---|
883 | if (ret == NULL)
|
---|
884 | return NULL;
|
---|
885 | if (BIO_set_conn_hostname(ret, str))
|
---|
886 | return ret;
|
---|
887 | BIO_free(ret);
|
---|
888 | return NULL;
|
---|
889 | }
|
---|
890 |
|
---|
891 | #endif
|
---|