lsquic_enc_sess.h revision fb73393f
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_ENC_SESS_H
3#define LSQUIC_ENC_SESS_H 1
4
5struct lsquic_alarmset;
6struct lsquic_engine_public;
7struct lsquic_packet_out;
8struct lsquic_packet_in;
9struct stream_wrapper;
10struct ver_neg;
11struct lsquic_conn;
12struct transport_params;
13struct lsquic_cid;
14struct ssl_stream_method_st;
15struct ssl_st;
16struct sockaddr;
17struct conn_cid_elem;
18
19#define DNONC_LENGTH 32
20#define SRST_LENGTH 16
21
22/* From [draft-ietf-quic-tls-14]:
23 *
24 * Data is protected using a number of encryption levels:
25 *
26 * o  Plaintext
27 *
28 * o  Early Data (0-RTT) Keys
29 *
30 * o  Handshake Keys
31 *
32 * o  Application Data (1-RTT) Keys
33 */
34
35/* This enum maps to the list above */
36enum enc_level
37{
38    ENC_LEV_CLEAR,
39    ENC_LEV_EARLY,
40    ENC_LEV_INIT,
41    ENC_LEV_FORW,
42    N_ENC_LEVS
43};
44
45enum handshake_error            /* TODO: rename this enum */
46{
47    DATA_NOT_ENOUGH = -2,
48    DATA_FORMAT_ERROR = -1,
49    HS_ERROR = -1,
50    DATA_NO_ERROR = 0,
51    HS_SHLO = 0,
52    HS_1RTT = 1,
53    HS_SREJ = 2,
54};
55
56#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY
57#   ifndef NDEBUG
58#       define LSQUIC_KEEP_ENC_SESS_HISTORY 1
59#   else
60#       define LSQUIC_KEEP_ENC_SESS_HISTORY 0
61#   endif
62#endif
63
64#if LSQUIC_KEEP_ENC_SESS_HISTORY
65#define ESHIST_BITS 7
66#define ESHIST_MASK ((1 << ESHIST_BITS) - 1)
67#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1)
68#endif
69
70enum enc_packout { ENCPA_OK, ENCPA_NOMEM, ENCPA_BADCRYPT, };
71
72enum dec_packin {
73    DECPI_OK,
74    DECPI_NOMEM,
75    DECPI_TOO_SHORT,
76    DECPI_NOT_YET,
77    DECPI_BADCRYPT,
78    DECPI_VIOLATION,
79};
80
81typedef void enc_session_t;
82
83struct enc_session_funcs_common
84{
85    /* Global initialization: call once per implementation */
86    int (*esf_global_init)(int flags);
87
88    /* Global cleanup: call once per implementation */
89    void (*esf_global_cleanup) (void);
90
91    const char *
92    (*esf_cipher) (enc_session_t *);
93
94    int
95    (*esf_keysize) (enc_session_t *);
96
97    int
98    (*esf_alg_keysize) (enc_session_t *);
99
100    /* Need to pass lconn in encrypt and decrypt methods because enc_session
101     * is allowed to be NULL for gQUIC.
102     */
103    enum enc_packout
104    (*esf_encrypt_packet) (enc_session_t *, const struct lsquic_engine_public *,
105        struct lsquic_conn *, struct lsquic_packet_out *);
106
107    enum dec_packin
108    (*esf_decrypt_packet)(enc_session_t *, struct lsquic_engine_public *,
109        const struct lsquic_conn *, struct lsquic_packet_in *);
110
111    struct stack_st_X509 *
112    (*esf_get_server_cert_chain) (enc_session_t *);
113
114    int
115    (*esf_verify_reset_token) (enc_session_t *, const unsigned char *, size_t);
116
117    int
118    (*esf_did_zero_rtt_succeed) (enc_session_t *);
119
120    int
121    (*esf_is_zero_rtt_enabled) (enc_session_t *);
122
123    void
124    (*esf_set_conn) (enc_session_t *, struct lsquic_conn *);
125
126    unsigned
127    esf_tag_len;
128};
129
130struct enc_session_funcs_gquic
131{
132#if LSQUIC_KEEP_ENC_SESS_HISTORY
133    /* Grab encryption session history */
134    void (*esf_get_hist) (enc_session_t *,
135                                            char buf[ESHIST_STR_SIZE]);
136#endif
137
138    /* Destroy enc session */
139    void (*esf_destroy)(enc_session_t *enc_session);
140
141    /* Return true if handshake has been completed */
142    int (*esf_is_hsk_done)(enc_session_t *enc_session);
143
144    /* Get value of setting specified by `tag' */
145    int (*esf_get_peer_setting) (enc_session_t *, uint32_t tag,
146                                                                uint32_t *val);
147
148    /* Get value of peer option (that from COPT array) */
149    int (*esf_get_peer_option) (enc_session_t *enc_session,
150                                                                uint32_t tag);
151
152    /* Create server session */
153    enc_session_t *
154    (*esf_create_server) (struct lsquic_conn *,
155                        lsquic_cid_t cid, const struct lsquic_engine_public *);
156
157    /* out_len should have init value as the max length of out */
158    enum handshake_error
159    (*esf_handle_chlo) (enc_session_t *enc_session, enum lsquic_version,
160                const uint8_t *in, int in_len, time_t t,
161                const struct sockaddr *ip_addr, const struct sockaddr *local,
162                uint8_t *out, size_t *out_len,
163                uint8_t nonce[DNONC_LENGTH], int *nonce_set);
164
165    void (*esf_hsk_destroy)(void *hsk_ctx);
166
167#ifndef NDEBUG
168    /* Need to expose this function for testing */
169    int (*esf_determine_diversification_key) (enc_session_t *,
170                              uint8_t *diversification_nonce);
171#endif
172
173    const char *
174    (*esf_get_ua) (enc_session_t *);
175
176    int
177    (*esf_have_key_gt_one) (enc_session_t *enc_session);
178
179#ifndef NDEBUG
180    /* Functions that are only relevant in maintest.  We may want to get rid
181     * of them somehow and only use the public API to test.
182     */
183
184    uint8_t
185    (*esf_have_key) (enc_session_t *);
186
187    void
188    (*esf_set_have_key) (enc_session_t *, uint8_t);
189
190    const unsigned char *
191    (*esf_get_enc_key_i) (enc_session_t *);
192
193    const unsigned char *
194    (*esf_get_dec_key_i) (enc_session_t *);
195
196    const unsigned char *
197    (*esf_get_enc_key_nonce_i) (enc_session_t *);
198
199    const unsigned char *
200    (*esf_get_dec_key_nonce_i) (enc_session_t *);
201
202    const unsigned char *
203    (*esf_get_enc_key_nonce_f) (enc_session_t *);
204
205    const unsigned char *
206    (*esf_get_dec_key_nonce_f) (enc_session_t *);
207#endif /* !defined(NDEBUG) */
208
209    /* Create client session */
210    enc_session_t *
211    (*esf_create_client) (struct lsquic_conn *, const char *domain,
212                            lsquic_cid_t cid,
213                                    const struct lsquic_engine_public *,
214                                    const unsigned char *, size_t);
215
216    /* -1 error, 0, OK, response in `buf' */
217    int
218    (*esf_gen_chlo) (enc_session_t *, enum lsquic_version,
219                                                uint8_t *buf, size_t *len);
220
221    int
222    (*esf_handle_chlo_reply) (enc_session_t *,
223                                                const uint8_t *data, int len);
224
225    size_t
226    (*esf_mem_used)(enc_session_t *);
227
228    /* Zero-rtt serialization needs the knowledge of the QUIC version, that's
229     * why there is a separate method for thus.  Plus, we want to be able to
230     * call it after the "handshake is done" callback is called.
231     */
232    void (*esf_maybe_dispatch_zero_rtt) (enc_session_t *,
233            void (*cb)(struct lsquic_conn *, const unsigned char *, size_t));
234
235    void (*esf_reset_cid) (enc_session_t *, const lsquic_cid_t *);
236};
237
238enum iquic_handshake_status {
239    IHS_WANT_READ,
240    IHS_WANT_WRITE,
241    IHS_STOP,
242};
243
244struct crypto_stream_if
245{
246    ssize_t     (*csi_write) (void *stream, const void *buf, size_t len);
247    int         (*csi_flush) (void *stream);
248    ssize_t     (*csi_readf) (void *stream,
249        size_t (*readf)(void *, const unsigned char *, size_t, int), void *ctx);
250    int         (*csi_wantwrite) (void *stream, int is_want);
251    int         (*csi_wantread) (void *stream, int is_want);
252    enum enc_level
253                (*csi_enc_level) (void *stream);
254};
255
256struct enc_session_funcs_iquic
257{
258    enc_session_t *
259    (*esfi_create_client) (const char *domain, struct lsquic_engine_public *,
260                           struct lsquic_conn *, const struct lsquic_cid *,
261                           const struct ver_neg *, void *(crypto_streams)[4],
262                           const struct crypto_stream_if *,
263                           const unsigned char *, size_t,
264                           struct lsquic_alarmset *, unsigned);
265
266    void
267    (*esfi_destroy) (enc_session_t *);
268
269    struct ssl_st *
270    (*esfi_get_ssl) (enc_session_t *);
271
272    struct transport_params *
273    (*esfi_get_peer_transport_params) (enc_session_t *);
274
275    int
276    (*esfi_reset_dcid) (enc_session_t *, const struct lsquic_cid *,
277                                                const struct lsquic_cid *);
278
279    void
280    (*esfi_set_iscid) (enc_session_t *, const struct lsquic_packet_in *);
281
282    int
283    (*esfi_init_server) (enc_session_t *);
284
285    void
286    (*esfi_set_streams) (enc_session_t *, void *(crypto_streams)[4],
287                           const struct crypto_stream_if *);
288
289    enc_session_t *
290    (*esfi_create_server) (struct lsquic_engine_public *, struct lsquic_conn *,
291                                                    const struct lsquic_cid *,
292                           void *(crypto_streams)[4],
293                           const struct crypto_stream_if *,
294                           const struct lsquic_cid *odcid,
295                           const struct lsquic_cid *iscid );
296
297    void
298    (*esfi_shake_stream)(enc_session_t *, struct lsquic_stream *,
299                         const char *);
300
301    void
302    (*esfi_handshake_confirmed)(enc_session_t *);
303
304    int
305    (*esfi_in_init)(enc_session_t *);
306
307    int
308    (*esfi_data_in)(enc_session_t *, enum enc_level,
309                                            const unsigned char *, size_t);
310};
311
312extern
313#ifdef NDEBUG
314const
315#endif
316struct enc_session_funcs_common lsquic_enc_session_common_gquic_1;
317
318extern
319#ifdef NDEBUG
320const
321#endif
322struct enc_session_funcs_common lsquic_enc_session_common_gquic_2;
323
324extern const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1;
325
326extern
327#ifdef NDEBUG
328const
329#endif
330struct enc_session_funcs_gquic lsquic_enc_session_gquic_gquic_1;
331
332extern const struct enc_session_funcs_iquic lsquic_enc_session_iquic_ietf_v1;
333
334#define select_esf_common_by_ver(ver) ( \
335    ver == LSQVER_ID27 ? &lsquic_enc_session_common_ietf_v1 : \
336    ver == LSQVER_ID28 ? &lsquic_enc_session_common_ietf_v1 : \
337    ver == LSQVER_VERNEG ? &lsquic_enc_session_common_ietf_v1 : \
338    ver == LSQVER_050 ? &lsquic_enc_session_common_gquic_2 : \
339    &lsquic_enc_session_common_gquic_1 )
340
341#define select_esf_gquic_by_ver(ver) ( \
342    ver ? &lsquic_enc_session_gquic_gquic_1 : &lsquic_enc_session_gquic_gquic_1)
343
344#define select_esf_iquic_by_ver(ver) ( \
345    ver ? &lsquic_enc_session_iquic_ietf_v1 : &lsquic_enc_session_iquic_ietf_v1)
346
347extern const char *const lsquic_enclev2str[];
348
349extern const struct lsquic_stream_if lsquic_cry_sm_if;
350
351extern const struct lsquic_stream_if lsquic_mini_cry_sm_if;
352
353/* RFC 7301, Section 3.2 */
354#define ALERT_NO_APPLICATION_PROTOCOL 120
355
356enum lsquic_version
357lsquic_zero_rtt_version (const unsigned char *, size_t);
358
359/* This is seems to be true for all of the ciphers used by IETF QUIC.
360 * XXX: Perhaps add a check?
361 */
362#define IQUIC_TAG_LEN 16
363
364#endif
365