lsquic_handshake.h revision 9626cfc2
1/* Copyright (c) 2017 - 2018 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;
7
8typedef struct lsquic_enc_session lsquic_enc_session_t;
9
10#define STK_LENGTH   60
11#define SNO_LENGTH   56
12#define SCID_LENGTH  16
13#define DNONC_LENGTH 32
14#define aes128_key_len 16
15#define aes128_iv_len 4
16#define SRST_LENGTH 16
17
18enum handshake_error            /* TODO: rename this enum */
19{
20    DATA_NOT_ENOUGH = -2,
21    DATA_FORMAT_ERROR = -1,
22    HS_ERROR = -1,
23    DATA_NO_ERROR = 0,
24    HS_SHLO = 0,
25    HS_1RTT = 1,
26    HS_2RTT = 2,
27};
28
29enum enc_level
30{
31    ENC_LEV_UNSET,
32    ENC_LEV_CLEAR,
33    ENC_LEV_INIT,
34    ENC_LEV_FORW,
35};
36
37extern const char *const lsquic_enclev2str[];
38
39/* client side need to store 0rtt info per STK */
40typedef struct lsquic_session_cache_info_st
41{
42    unsigned char   sscid[SCID_LENGTH];
43    unsigned char   spubs[32];  /* server pub key for next time 0rtt */
44    uint32_t    ver;  /* one VERSION */
45    uint32_t    aead;
46    uint32_t    kexs;
47    uint32_t    pdmd;
48    uint64_t    orbt;
49    uint64_t    expy;
50    int         scfg_flag; /* 0, no-init, 1, no parse, 2, parsed */
51    struct lsquic_str    sstk;
52    struct lsquic_str    scfg;
53    struct lsquic_str    sni_key;   /* This is only used as key */
54
55} lsquic_session_cache_info_t;
56
57#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY
58#   ifndef NDEBUG
59#       define LSQUIC_KEEP_ENC_SESS_HISTORY 1
60#   else
61#       define LSQUIC_KEEP_ENC_SESS_HISTORY 0
62#   endif
63#endif
64
65#if LSQUIC_KEEP_ENC_SESS_HISTORY
66#define ESHIST_BITS 7
67#define ESHIST_MASK ((1 << ESHIST_BITS) - 1)
68#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1)
69#endif
70
71struct enc_session_funcs
72{
73    /* Global initialization: call once per implementation */
74    int (*esf_global_init)(int flags);
75
76    /* Global cleanup: call once per implementation */
77    void (*esf_global_cleanup) (void);
78
79#if LSQUIC_KEEP_ENC_SESS_HISTORY
80    /* Grab encryption session history */
81    void (*esf_get_hist) (const lsquic_enc_session_t *,
82                                            char buf[ESHIST_STR_SIZE]);
83#endif
84
85    /* Destroy enc session */
86    void (*esf_destroy)(lsquic_enc_session_t *enc_session);
87
88    /* Return true if handshake has been completed */
89    int (*esf_is_hsk_done)(lsquic_enc_session_t *enc_session);
90
91    /* Encrypt buffer */
92    enum enc_level (*esf_encrypt)(lsquic_enc_session_t *enc_session,
93               enum lsquic_version, uint8_t path_id, uint64_t pack_num,
94               const unsigned char *header, size_t header_len,
95               const unsigned char *data, size_t data_len,
96               unsigned char *buf_out, size_t max_out_len, size_t *out_len,
97               int is_hello);
98
99    /** Decrypt buffer
100     *
101     * If decryption is successful, decryption level is returned.  Otherwise,
102     * the return value is -1.
103     */
104    enum enc_level (*esf_decrypt)(lsquic_enc_session_t *enc_session,
105                   enum lsquic_version,
106                   uint8_t path_id, uint64_t pack_num,
107                   unsigned char *buf, size_t *header_len, size_t data_len,
108                   unsigned char *diversification_nonce,
109                   unsigned char *buf_out, size_t max_out_len, size_t *out_len);
110
111    /* Get value of setting specified by `tag' */
112    int (*esf_get_peer_setting) (const lsquic_enc_session_t *, uint32_t tag,
113                                                                uint32_t *val);
114
115    /* Get value of peer option (that from COPT array) */
116    int (*esf_get_peer_option) (const lsquic_enc_session_t *enc_session,
117                                                                uint32_t tag);
118
119    /* Create client session */
120    lsquic_enc_session_t *
121    (*esf_create_client) (const char *domain, lsquic_cid_t cid,
122                                    const struct lsquic_engine_public *);
123
124    /* Generate connection ID */
125    lsquic_cid_t (*esf_generate_cid) (void);
126
127    /* -1 error, 0, OK, response in `buf' */
128    int
129    (*esf_gen_chlo) (lsquic_enc_session_t *, enum lsquic_version,
130                                                uint8_t *buf, size_t *len);
131
132    int
133    (*esf_handle_chlo_reply) (lsquic_enc_session_t *,
134                                                const uint8_t *data, int len);
135
136    size_t
137    (*esf_mem_used)(lsquic_enc_session_t *);
138
139    int
140    (*esf_verify_reset_token) (lsquic_enc_session_t *, const unsigned char *,
141                                                                    size_t);
142};
143
144extern
145#ifdef NDEBUG
146const
147#endif
148struct enc_session_funcs lsquic_enc_session_gquic_1;
149
150#define select_esf_by_ver(ver) \
151    (ver ? &lsquic_enc_session_gquic_1 : &lsquic_enc_session_gquic_1)
152
153/* client side, certs and hashs
154 */
155typedef struct cert_hash_item_st
156{
157    struct lsquic_str*   domain; /*with port, such as "xyz.com:8088" as the key */
158    struct lsquic_str*   crts;
159    struct lsquic_str*   hashs;
160    int         count;
161} cert_hash_item_t;
162
163#endif
164