lsquic_handshake.h revision 8ca33e0e
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_HANDSHAKE_SERVER_H
3#define LSQUIC_HANDSHAKE_SERVER_H
4
5struct lsquic_engine_public;
6struct lsquic_enc_session;
7struct stack_st_X509;
8
9typedef struct lsquic_enc_session lsquic_enc_session_t;
10
11#define MAX_SCFG_LENGTH 512
12#define MAX_SPUBS_LENGTH 32
13#define STK_LENGTH   60
14#define SNO_LENGTH   56
15#define SCID_LENGTH  16
16#define DNONC_LENGTH 32
17#define aes128_key_len 16
18#define aes128_iv_len 4
19#define SRST_LENGTH 16
20
21enum handshake_error            /* TODO: rename this enum */
22{
23    DATA_NOT_ENOUGH = -2,
24    DATA_FORMAT_ERROR = -1,
25    HS_ERROR = -1,
26    DATA_NO_ERROR = 0,
27    HS_SHLO = 0,
28    HS_1RTT = 1,
29    HS_2RTT = 2,
30};
31
32enum enc_level
33{
34    ENC_LEV_UNSET,
35    ENC_LEV_CLEAR,
36    ENC_LEV_INIT,
37    ENC_LEV_FORW,
38};
39
40extern const char *const lsquic_enclev2str[];
41
42/* client */
43typedef struct c_cert_item_st
44{
45    struct lsquic_str*  crts;
46    struct lsquic_str*  hashs;
47    int                 count;
48} c_cert_item_t;
49
50/* client side need to store 0rtt info per STK */
51typedef struct lsquic_session_cache_info_st
52{
53    unsigned char   sscid[SCID_LENGTH];
54    unsigned char   spubs[32];  /* server pub key for next time 0rtt */
55    uint32_t    ver;  /* one VERSION */
56    uint32_t    aead;
57    uint32_t    kexs;
58    uint32_t    pdmd;
59    uint64_t    orbt;
60    uint64_t    expy;
61    int         scfg_flag; /* 0, no-init, 1, no parse, 2, parsed */
62    struct lsquic_str    sstk;
63    struct lsquic_str    scfg;
64    struct lsquic_str    sni_key;   /* This is only used as key */
65
66} lsquic_session_cache_info_t;
67
68struct lsquic_cert_storage
69{
70    uint32_t    len;
71    uint8_t     data[0];
72};
73
74struct lsquic_zero_rtt_storage
75{
76    uint32_t    quic_version_tag;
77    uint32_t    serializer_version;
78    uint32_t    ver;
79    uint32_t    aead;
80    uint32_t    kexs;
81    uint32_t    pdmd;
82    uint64_t    orbt;
83    uint64_t    expy;
84    uint64_t    sstk_len;
85    uint64_t    scfg_len;
86    uint64_t    scfg_flag;
87    uint8_t     sstk[STK_LENGTH];
88    uint8_t     scfg[MAX_SCFG_LENGTH];
89    uint8_t     sscid[SCID_LENGTH];
90    uint8_t     spubs[MAX_SPUBS_LENGTH];
91    uint32_t    cert_count;
92    struct lsquic_cert_storage  cert_storage[0];
93};
94
95#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY
96#   ifndef NDEBUG
97#       define LSQUIC_KEEP_ENC_SESS_HISTORY 1
98#   else
99#       define LSQUIC_KEEP_ENC_SESS_HISTORY 0
100#   endif
101#endif
102
103#if LSQUIC_KEEP_ENC_SESS_HISTORY
104#define ESHIST_BITS 7
105#define ESHIST_MASK ((1 << ESHIST_BITS) - 1)
106#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1)
107#endif
108
109struct enc_session_funcs
110{
111    /* Global initialization: call once per implementation */
112    int (*esf_global_init)(int flags);
113
114    /* Global cleanup: call once per implementation */
115    void (*esf_global_cleanup) (void);
116
117#if LSQUIC_KEEP_ENC_SESS_HISTORY
118    /* Grab encryption session history */
119    void (*esf_get_hist) (const lsquic_enc_session_t *,
120                                            char buf[ESHIST_STR_SIZE]);
121#endif
122
123    /* Destroy enc session */
124    void (*esf_destroy)(lsquic_enc_session_t *enc_session);
125
126    /* Return true if handshake has been completed */
127    int (*esf_is_hsk_done)(lsquic_enc_session_t *enc_session);
128
129    /* Encrypt buffer */
130    enum enc_level (*esf_encrypt)(lsquic_enc_session_t *enc_session,
131               enum lsquic_version, uint8_t path_id, uint64_t pack_num,
132               const unsigned char *header, size_t header_len,
133               const unsigned char *data, size_t data_len,
134               unsigned char *buf_out, size_t max_out_len, size_t *out_len,
135               int is_hello);
136
137    /** Decrypt buffer
138     *
139     * If decryption is successful, decryption level is returned.  Otherwise,
140     * the return value is -1.
141     */
142    enum enc_level (*esf_decrypt)(lsquic_enc_session_t *enc_session,
143                   enum lsquic_version,
144                   uint8_t path_id, uint64_t pack_num,
145                   unsigned char *buf, size_t *header_len, size_t data_len,
146                   unsigned char *diversification_nonce,
147                   unsigned char *buf_out, size_t max_out_len, size_t *out_len);
148
149    /* Get value of setting specified by `tag' */
150    int (*esf_get_peer_setting) (const lsquic_enc_session_t *, uint32_t tag,
151                                                                uint32_t *val);
152
153    /* Get value of peer option (that from COPT array) */
154    int (*esf_get_peer_option) (const lsquic_enc_session_t *enc_session,
155                                                                uint32_t tag);
156
157    /* Create client session */
158    lsquic_enc_session_t *
159    (*esf_create_client) (const char *domain, lsquic_cid_t cid,
160                            const struct lsquic_engine_public *,
161                            const unsigned char *, size_t);
162
163    /* Generate connection ID */
164    lsquic_cid_t (*esf_generate_cid) (void);
165
166    /* -1 error, 0, OK, response in `buf' */
167    int
168    (*esf_gen_chlo) (lsquic_enc_session_t *, enum lsquic_version,
169                                                uint8_t *buf, size_t *len);
170
171    int
172    (*esf_handle_chlo_reply) (lsquic_enc_session_t *,
173                                                const uint8_t *data, int len);
174
175    size_t
176    (*esf_mem_used)(lsquic_enc_session_t *);
177
178    int
179    (*esf_verify_reset_token) (lsquic_enc_session_t *, const unsigned char *,
180                                                                    size_t);
181
182    int
183    (*esf_did_zero_rtt_succeed) (const lsquic_enc_session_t *);
184
185    int
186    (*esf_is_zero_rtt_enabled) (const lsquic_enc_session_t *);
187
188    c_cert_item_t *
189    (*esf_get_cert_item) (const lsquic_enc_session_t *);
190
191    struct stack_st_X509 *
192    (*esf_get_server_cert_chain) (lsquic_enc_session_t *);
193
194    ssize_t
195    (*esf_get_zero_rtt) (lsquic_enc_session_t *, enum lsquic_version,
196                                                            void *, size_t);
197};
198
199extern
200#ifdef NDEBUG
201const
202#endif
203struct enc_session_funcs lsquic_enc_session_gquic_1;
204
205#define select_esf_by_ver(ver) \
206    (ver ? &lsquic_enc_session_gquic_1 : &lsquic_enc_session_gquic_1)
207
208#endif
209