lsquic_handshake.h revision 90fe3b25
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_zero_rtt_storage
69{
70    uint32_t    quic_version_tag;
71    uint32_t    serializer_version;
72    uint32_t    ver;
73    uint32_t    aead;
74    uint32_t    kexs;
75    uint32_t    pdmd;
76    uint64_t    orbt;
77    uint64_t    expy;
78    uint64_t    sstk_len;
79    uint64_t    scfg_len;
80    uint64_t    scfg_flag;
81    uint8_t     sstk[STK_LENGTH];
82    uint8_t     scfg[MAX_SCFG_LENGTH];
83    uint8_t     sscid[SCID_LENGTH];
84    uint8_t     spubs[MAX_SPUBS_LENGTH];
85    uint32_t    cert_count;
86/*
87 *  uint32_t    cert_len;
88 *  uint8_t     cert_data[0];
89 */
90};
91
92#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY
93#   ifndef NDEBUG
94#       define LSQUIC_KEEP_ENC_SESS_HISTORY 1
95#   else
96#       define LSQUIC_KEEP_ENC_SESS_HISTORY 0
97#   endif
98#endif
99
100#if LSQUIC_KEEP_ENC_SESS_HISTORY
101#define ESHIST_BITS 7
102#define ESHIST_MASK ((1 << ESHIST_BITS) - 1)
103#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1)
104#endif
105
106struct enc_session_funcs
107{
108    /* Global initialization: call once per implementation */
109    int (*esf_global_init)(int flags);
110
111    /* Global cleanup: call once per implementation */
112    void (*esf_global_cleanup) (void);
113
114#if LSQUIC_KEEP_ENC_SESS_HISTORY
115    /* Grab encryption session history */
116    void (*esf_get_hist) (const lsquic_enc_session_t *,
117                                            char buf[ESHIST_STR_SIZE]);
118#endif
119
120    /* Destroy enc session */
121    void (*esf_destroy)(lsquic_enc_session_t *enc_session);
122
123    /* Return true if handshake has been completed */
124    int (*esf_is_hsk_done)(lsquic_enc_session_t *enc_session);
125
126    /* Encrypt buffer */
127    enum enc_level (*esf_encrypt)(lsquic_enc_session_t *enc_session,
128               enum lsquic_version, uint8_t path_id, uint64_t pack_num,
129               const unsigned char *header, size_t header_len,
130               const unsigned char *data, size_t data_len,
131               unsigned char *buf_out, size_t max_out_len, size_t *out_len,
132               int is_hello);
133
134    /** Decrypt buffer
135     *
136     * If decryption is successful, decryption level is returned.  Otherwise,
137     * the return value is -1.
138     */
139    enum enc_level (*esf_decrypt)(lsquic_enc_session_t *enc_session,
140                   enum lsquic_version,
141                   uint8_t path_id, uint64_t pack_num,
142                   unsigned char *buf, size_t *header_len, size_t data_len,
143                   unsigned char *diversification_nonce,
144                   unsigned char *buf_out, size_t max_out_len, size_t *out_len);
145
146    /* Get value of setting specified by `tag' */
147    int (*esf_get_peer_setting) (const lsquic_enc_session_t *, uint32_t tag,
148                                                                uint32_t *val);
149
150    /* Get value of peer option (that from COPT array) */
151    int (*esf_get_peer_option) (const lsquic_enc_session_t *enc_session,
152                                                                uint32_t tag);
153
154    /* Create client session */
155    lsquic_enc_session_t *
156    (*esf_create_client) (const char *domain, lsquic_cid_t cid,
157                            const struct lsquic_engine_public *,
158                            const unsigned char *, size_t);
159
160    /* Generate connection ID */
161    lsquic_cid_t (*esf_generate_cid) (void);
162
163    /* -1 error, 0, OK, response in `buf' */
164    int
165    (*esf_gen_chlo) (lsquic_enc_session_t *, enum lsquic_version,
166                                                uint8_t *buf, size_t *len);
167
168    int
169    (*esf_handle_chlo_reply) (lsquic_enc_session_t *,
170                                                const uint8_t *data, int len);
171
172    size_t
173    (*esf_mem_used)(lsquic_enc_session_t *);
174
175    int
176    (*esf_verify_reset_token) (lsquic_enc_session_t *, const unsigned char *,
177                                                                    size_t);
178
179    int
180    (*esf_did_zero_rtt_succeed) (const lsquic_enc_session_t *);
181
182    int
183    (*esf_is_zero_rtt_enabled) (const lsquic_enc_session_t *);
184
185    c_cert_item_t *
186    (*esf_get_cert_item) (const lsquic_enc_session_t *);
187
188    struct stack_st_X509 *
189    (*esf_get_server_cert_chain) (lsquic_enc_session_t *);
190
191    ssize_t
192    (*esf_get_zero_rtt) (lsquic_enc_session_t *, enum lsquic_version,
193                                                            void *, size_t);
194};
195
196extern
197#ifdef NDEBUG
198const
199#endif
200struct enc_session_funcs lsquic_enc_session_gquic_1;
201
202#define select_esf_by_ver(ver) \
203    (ver ? &lsquic_enc_session_gquic_1 : &lsquic_enc_session_gquic_1)
204
205enum lsquic_version
206lsquic_zero_rtt_version (const unsigned char *, size_t);
207
208#endif
209