Coverage Report

Created: 2024-05-21 06:52

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