Coverage Report

Created: 2025-11-16 06:40

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