summaryrefslogtreecommitdiffstats
path: root/ssl/quic/quic_obj.c
blob: 85b439243873e4c8d8934a7091b19e5bca11336f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include "quic_obj_local.h"
#include "quic_local.h"
#include "internal/ssl_unwrap.h"

static int obj_update_cache(QUIC_OBJ *obj);

int ossl_quic_obj_init(QUIC_OBJ *obj,
                       SSL_CTX *ctx,
                       int type,
                       SSL *parent_obj,
                       QUIC_ENGINE *engine,
                       QUIC_PORT *port)
{
    int is_event_leader = (engine != NULL);
    int is_port_leader  = (port != NULL);

    if (!ossl_assert(obj != NULL && !obj->init_done && SSL_TYPE_IS_QUIC(type)
                     && (parent_obj == NULL || IS_QUIC(parent_obj))))
        return 0;

    /* Event leader is always the root object. */
    if (!ossl_assert(!is_event_leader || parent_obj == NULL))
        return 0;

    if (!ossl_ssl_init(&obj->ssl, ctx, ctx->method, type))
        goto err;

    obj->parent_obj         = (QUIC_OBJ *)parent_obj;
    obj->is_event_leader    = is_event_leader;
    obj->is_port_leader     = is_port_leader;
    obj->engine             = engine;
    obj->port               = port;
    obj->req_blocking_mode  = QUIC_BLOCKING_MODE_INHERIT;
    if (!obj_update_cache(obj))
        goto err;

    obj->init_done          = 1;
    return 1;

err:
    obj->is_event_leader = 0;
    obj->is_port_leader  = 0;
    return 0;
}

static int obj_update_cache(QUIC_OBJ *obj)
{
    QUIC_OBJ *p;

    for (p = obj; p != NULL && !p->is_event_leader;
         p = p->parent_obj)
        if (!ossl_assert(p == obj || p->init_done))
            return 0;

    if (!ossl_assert(p != NULL))
        return 0;

    /*
     * Offset of ->ssl is guaranteed to be 0 but the NULL check makes ubsan
     * happy.
     */
    obj->cached_event_leader    = p;
    obj->engine                 = p->engine;

    for (p = obj; p != NULL && !p->is_port_leader;
         p = p->parent_obj);

    obj->cached_port_leader     = p;
    obj->port                   = (p != NULL) ? p->port : NULL;
    return 1;
}

SSL_CONNECTION *ossl_quic_obj_get0_handshake_layer(QUIC_OBJ *obj)
{
    assert(obj != NULL && obj->init_done);

    if (obj->ssl.type != SSL_TYPE_QUIC_CONNECTION)
        return NULL;

    return SSL_CONNECTION_FROM_SSL_ONLY(((QUIC_CONNECTION *)obj)->tls);
}

/* (Returns a cached result.) */
int ossl_quic_obj_can_support_blocking(const QUIC_OBJ *obj)
{
    QUIC_REACTOR *rtor;

    assert(obj != NULL);
    rtor = ossl_quic_obj_get0_reactor(obj);

    return ossl_quic_reactor_can_poll_r(rtor)
        || ossl_quic_reactor_can_poll_w(rtor);
}

int ossl_quic_obj_desires_blocking(const QUIC_OBJ *obj)
{
    unsigned int req_blocking_mode;

    assert(obj != NULL);
    for (; (req_blocking_mode = obj->req_blocking_mode) == QUIC_BLOCKING_MODE_INHERIT
           && obj->parent_obj != NULL; obj = obj->parent_obj);

    return req_blocking_mode != QUIC_BLOCKING_MODE_NONBLOCKING;
}

int ossl_quic_obj_blocking(const QUIC_OBJ *obj)
{
    assert(obj != NULL);

    if (!ossl_quic_obj_desires_blocking(obj))
        return 0;

    ossl_quic_engine_update_poll_descriptors(ossl_quic_obj_get0_engine(obj),
                                             /*force=*/0);
    return ossl_quic_obj_can_support_blocking(obj);
}

void ossl_quic_obj_set_blocking_mode(QUIC_OBJ *obj, unsigned int mode)
{
    assert(obj != NULL);

    obj->req_blocking_mode = mode;
}