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