lsquic_chsk_stream.c revision de46bf2f
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * Stream/crypto handshake adapter for the client side.
4 *
5 * The client composes CHLO, writes it to the stream, and wait for the
6 * server response, which it processes.
7 */
8
9#include <assert.h>
10#include <errno.h>
11#include <stdarg.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/queue.h>
15
16#include "lsquic_int_types.h"
17#include "lsquic.h"
18
19#include "lsquic_str.h"
20#include "lsquic_enc_sess.h"
21#include "lsquic_chsk_stream.h"
22#include "lsquic_ver_neg.h"
23#include "lsquic_hash.h"
24#include "lsquic_conn.h"
25#include "lsquic_mm.h"
26#include "lsquic_sizes.h"
27#include "lsquic_full_conn.h"
28
29#define LSQUIC_LOGGER_MODULE LSQLM_HSK_ADAPTER
30#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(c_hsk->lconn)
31#include "lsquic_logger.h"
32
33
34static lsquic_stream_ctx_t *
35hsk_client_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
36{
37    struct client_hsk_ctx *const c_hsk = stream_if_ctx;
38
39    LSQ_DEBUG("stream created");
40
41    lsquic_stream_wantwrite(stream, 1);
42
43    return (void *) c_hsk;
44}
45
46
47static void
48hsk_client_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh)
49{
50    struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh;
51    ssize_t nread;
52    int s;
53    enum lsquic_hsk_status status;
54
55    if (!c_hsk->buf_in)
56    {
57        c_hsk->buf_in  = lsquic_mm_get_16k(c_hsk->mm);
58        if (!c_hsk->buf_in)
59        {
60            LSQ_WARN("could not get buffer: %s", strerror(errno));
61            lsquic_stream_wantread(stream, 0);
62            lsquic_conn_close(c_hsk->lconn);
63            return;
64        }
65        c_hsk->buf_sz  = 16 * 1024;
66        c_hsk->buf_off = 0;
67    }
68
69    nread = lsquic_stream_read(stream, c_hsk->buf_in + c_hsk->buf_off,
70                                            c_hsk->buf_sz - c_hsk->buf_off);
71    if (nread <= 0)
72    {
73        if (nread < 0)
74            LSQ_INFO("Could not read from handshake stream: %s",
75                                                            strerror(errno));
76        else
77            LSQ_INFO("Handshake stream closed (odd)");
78        lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in);
79        c_hsk->buf_in = NULL;
80        lsquic_stream_wantread(stream, 0);
81        lsquic_conn_close(c_hsk->lconn);
82        return;
83    }
84    c_hsk->buf_off += nread;
85
86    s = c_hsk->lconn->cn_esf.g->esf_handle_chlo_reply(c_hsk->lconn->cn_enc_session,
87                                        c_hsk->buf_in, c_hsk->buf_off);
88    LSQ_DEBUG("lsquic_enc_session_handle_chlo_reply returned %d", s);
89    switch (s)
90    {
91    case DATA_NOT_ENOUGH:
92        if (c_hsk->buf_off < c_hsk->buf_sz)
93            LSQ_INFO("not enough server response has arrived, continue "
94                                                                "buffering");
95        else
96        {
97            LSQ_INFO("read in %u bytes of server response, and it is still "
98                        "not enough: giving up", c_hsk->buf_off);
99            lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in);
100            c_hsk->buf_in = NULL;
101            lsquic_stream_wantread(stream, 0);
102            c_hsk->lconn->cn_if->ci_hsk_done(c_hsk->lconn, LSQ_HSK_FAIL);
103            lsquic_conn_close(c_hsk->lconn);
104        }
105        break;
106    case DATA_NO_ERROR:
107        lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in);
108        c_hsk->buf_in = NULL;
109        lsquic_stream_wantread(stream, 0);
110        if (c_hsk->lconn->cn_esf.g->esf_is_hsk_done(c_hsk->lconn->cn_enc_session))
111        {
112            LSQ_DEBUG("handshake is successful, inform connection");
113            status = (c_hsk->lconn->cn_esf_c->esf_did_zero_rtt_succeed(
114                c_hsk->lconn->cn_enc_session)) ? LSQ_HSK_0RTT_OK : LSQ_HSK_OK;
115            c_hsk->lconn->cn_if->ci_hsk_done(c_hsk->lconn, status);
116        }
117        else
118        {
119            LSQ_DEBUG("handshake not yet complete, will generate another "
120                                                                    "message");
121            lsquic_stream_wantwrite(stream, 1);
122        }
123        break;
124    case HS_SREJ:
125        LSQ_DEBUG("got HS_SREJ");
126        c_hsk->buf_off = 0;
127        lsquic_stream_wantread(stream, 0);
128        if (0 == lsquic_gquic_full_conn_srej(c_hsk->lconn))
129            lsquic_stream_wantwrite(stream, 1);
130        break;
131    default:
132        LSQ_WARN("lsquic_enc_session_handle_chlo_reply returned unknown value %d", s);
133        /* fallthru */
134    case DATA_FORMAT_ERROR:
135        LSQ_INFO("lsquic_enc_session_handle_chlo_reply returned an error");
136        c_hsk->buf_in = NULL;
137        lsquic_stream_wantread(stream, 0);
138        c_hsk->lconn->cn_if->ci_hsk_done(c_hsk->lconn, LSQ_HSK_FAIL);
139        lsquic_conn_close(c_hsk->lconn);
140        break;
141    }
142}
143
144
145/* In this function, we assume that we can write the whole message in one
146 * shot.  Otherwise, this is an error.
147 */
148static void
149hsk_client_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh)
150{
151    struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh;
152    unsigned char *buf;
153    size_t len;
154    ssize_t nw;
155
156    lsquic_stream_wantwrite(stream, 0);
157
158    buf = lsquic_mm_get_4k(c_hsk->mm);
159    if (!buf)
160    {
161        LSQ_WARN("cannot allocate buffer: %s", strerror(errno));
162        lsquic_conn_close(c_hsk->lconn);
163        return;
164    }
165    len = 4 * 1024;
166
167    if (0 != c_hsk->lconn->cn_esf.g->esf_gen_chlo(c_hsk->lconn->cn_enc_session,
168                                            c_hsk->ver_neg->vn_ver, buf, &len))
169    {
170        LSQ_WARN("cannot create CHLO message");
171        lsquic_mm_put_4k(c_hsk->mm, buf);
172        lsquic_conn_close(c_hsk->lconn);
173        return;
174    }
175
176    nw = lsquic_stream_write(stream, buf, len);
177    lsquic_mm_put_4k(c_hsk->mm, buf);
178
179    if (nw < 0)
180        LSQ_INFO("error writing to stream: %s", strerror(errno));
181    else if ((size_t) nw == len)
182    {
183        LSQ_INFO("wrote %zd bytes of CHLO to stream", nw);
184        lsquic_stream_flush(stream);
185        lsquic_stream_wantread(stream, 1);
186    }
187    else
188        LSQ_INFO("could only write %zd bytes to stream instead of %zd",
189                                                                    nw, len);
190}
191
192
193static void
194hsk_client_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *sh)
195{
196    struct client_hsk_ctx *const c_hsk = (struct client_hsk_ctx *) sh;
197    if (c_hsk->buf_in)
198        lsquic_mm_put_16k(c_hsk->mm, c_hsk->buf_in);
199    LSQ_DEBUG("stream closed");
200}
201
202
203const struct lsquic_stream_if lsquic_client_hsk_stream_if =
204{
205    .on_new_stream = hsk_client_on_new_stream,
206    .on_read       = hsk_client_on_read,
207    .on_write      = hsk_client_on_write,
208    .on_close      = hsk_client_on_close,
209};
210