lsquic_handshake.h revision 8ca33e0e
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
283287402SDmitri Tikhonov#ifndef LSQUIC_HANDSHAKE_SERVER_H
383287402SDmitri Tikhonov#define LSQUIC_HANDSHAKE_SERVER_H
450aadb33SDmitri Tikhonov
550aadb33SDmitri Tikhonovstruct lsquic_engine_public;
683287402SDmitri Tikhonovstruct lsquic_enc_session;
77f2bd84cSDmitri Tikhonovstruct stack_st_X509;
850aadb33SDmitri Tikhonov
983287402SDmitri Tikhonovtypedef struct lsquic_enc_session lsquic_enc_session_t;
1050aadb33SDmitri Tikhonov
118ca33e0eSDmitri Tikhonov#define MAX_SCFG_LENGTH 512
128ca33e0eSDmitri Tikhonov#define MAX_SPUBS_LENGTH 32
1350aadb33SDmitri Tikhonov#define STK_LENGTH   60
1450aadb33SDmitri Tikhonov#define SNO_LENGTH   56
1550aadb33SDmitri Tikhonov#define SCID_LENGTH  16
1650aadb33SDmitri Tikhonov#define DNONC_LENGTH 32
1750aadb33SDmitri Tikhonov#define aes128_key_len 16
1850aadb33SDmitri Tikhonov#define aes128_iv_len 4
199626cfc2SDmitri Tikhonov#define SRST_LENGTH 16
2050aadb33SDmitri Tikhonov
2150aadb33SDmitri Tikhonovenum handshake_error            /* TODO: rename this enum */
2250aadb33SDmitri Tikhonov{
2350aadb33SDmitri Tikhonov    DATA_NOT_ENOUGH = -2,
2450aadb33SDmitri Tikhonov    DATA_FORMAT_ERROR = -1,
2550aadb33SDmitri Tikhonov    HS_ERROR = -1,
2650aadb33SDmitri Tikhonov    DATA_NO_ERROR = 0,
2750aadb33SDmitri Tikhonov    HS_SHLO = 0,
2850aadb33SDmitri Tikhonov    HS_1RTT = 1,
2950aadb33SDmitri Tikhonov    HS_2RTT = 2,
3050aadb33SDmitri Tikhonov};
3150aadb33SDmitri Tikhonov
32c51ce338SDmitri Tikhonovenum enc_level
33c51ce338SDmitri Tikhonov{
34c51ce338SDmitri Tikhonov    ENC_LEV_UNSET,
35c51ce338SDmitri Tikhonov    ENC_LEV_CLEAR,
36c51ce338SDmitri Tikhonov    ENC_LEV_INIT,
37c51ce338SDmitri Tikhonov    ENC_LEV_FORW,
38c51ce338SDmitri Tikhonov};
39c51ce338SDmitri Tikhonov
409626cfc2SDmitri Tikhonovextern const char *const lsquic_enclev2str[];
419626cfc2SDmitri Tikhonov
428ca33e0eSDmitri Tikhonov/* client */
438ca33e0eSDmitri Tikhonovtypedef struct c_cert_item_st
448ca33e0eSDmitri Tikhonov{
458ca33e0eSDmitri Tikhonov    struct lsquic_str*  crts;
468ca33e0eSDmitri Tikhonov    struct lsquic_str*  hashs;
478ca33e0eSDmitri Tikhonov    int                 count;
488ca33e0eSDmitri Tikhonov} c_cert_item_t;
498ca33e0eSDmitri Tikhonov
5050aadb33SDmitri Tikhonov/* client side need to store 0rtt info per STK */
5150aadb33SDmitri Tikhonovtypedef struct lsquic_session_cache_info_st
5250aadb33SDmitri Tikhonov{
5350aadb33SDmitri Tikhonov    unsigned char   sscid[SCID_LENGTH];
5450aadb33SDmitri Tikhonov    unsigned char   spubs[32];  /* server pub key for next time 0rtt */
5550aadb33SDmitri Tikhonov    uint32_t    ver;  /* one VERSION */
5650aadb33SDmitri Tikhonov    uint32_t    aead;
5750aadb33SDmitri Tikhonov    uint32_t    kexs;
5850aadb33SDmitri Tikhonov    uint32_t    pdmd;
5950aadb33SDmitri Tikhonov    uint64_t    orbt;
6050aadb33SDmitri Tikhonov    uint64_t    expy;
6150aadb33SDmitri Tikhonov    int         scfg_flag; /* 0, no-init, 1, no parse, 2, parsed */
6250aadb33SDmitri Tikhonov    struct lsquic_str    sstk;
6350aadb33SDmitri Tikhonov    struct lsquic_str    scfg;
6450aadb33SDmitri Tikhonov    struct lsquic_str    sni_key;   /* This is only used as key */
6550aadb33SDmitri Tikhonov
6650aadb33SDmitri Tikhonov} lsquic_session_cache_info_t;
6750aadb33SDmitri Tikhonov
688ca33e0eSDmitri Tikhonovstruct lsquic_cert_storage
698ca33e0eSDmitri Tikhonov{
708ca33e0eSDmitri Tikhonov    uint32_t    len;
718ca33e0eSDmitri Tikhonov    uint8_t     data[0];
728ca33e0eSDmitri Tikhonov};
738ca33e0eSDmitri Tikhonov
748ca33e0eSDmitri Tikhonovstruct lsquic_zero_rtt_storage
758ca33e0eSDmitri Tikhonov{
768ca33e0eSDmitri Tikhonov    uint32_t    quic_version_tag;
778ca33e0eSDmitri Tikhonov    uint32_t    serializer_version;
788ca33e0eSDmitri Tikhonov    uint32_t    ver;
798ca33e0eSDmitri Tikhonov    uint32_t    aead;
808ca33e0eSDmitri Tikhonov    uint32_t    kexs;
818ca33e0eSDmitri Tikhonov    uint32_t    pdmd;
828ca33e0eSDmitri Tikhonov    uint64_t    orbt;
838ca33e0eSDmitri Tikhonov    uint64_t    expy;
848ca33e0eSDmitri Tikhonov    uint64_t    sstk_len;
858ca33e0eSDmitri Tikhonov    uint64_t    scfg_len;
868ca33e0eSDmitri Tikhonov    uint64_t    scfg_flag;
878ca33e0eSDmitri Tikhonov    uint8_t     sstk[STK_LENGTH];
888ca33e0eSDmitri Tikhonov    uint8_t     scfg[MAX_SCFG_LENGTH];
898ca33e0eSDmitri Tikhonov    uint8_t     sscid[SCID_LENGTH];
908ca33e0eSDmitri Tikhonov    uint8_t     spubs[MAX_SPUBS_LENGTH];
918ca33e0eSDmitri Tikhonov    uint32_t    cert_count;
928ca33e0eSDmitri Tikhonov    struct lsquic_cert_storage  cert_storage[0];
938ca33e0eSDmitri Tikhonov};
948ca33e0eSDmitri Tikhonov
9550aadb33SDmitri Tikhonov#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY
9650aadb33SDmitri Tikhonov#   ifndef NDEBUG
9750aadb33SDmitri Tikhonov#       define LSQUIC_KEEP_ENC_SESS_HISTORY 1
9850aadb33SDmitri Tikhonov#   else
9950aadb33SDmitri Tikhonov#       define LSQUIC_KEEP_ENC_SESS_HISTORY 0
10050aadb33SDmitri Tikhonov#   endif
10150aadb33SDmitri Tikhonov#endif
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov#if LSQUIC_KEEP_ENC_SESS_HISTORY
10450aadb33SDmitri Tikhonov#define ESHIST_BITS 7
10550aadb33SDmitri Tikhonov#define ESHIST_MASK ((1 << ESHIST_BITS) - 1)
10650aadb33SDmitri Tikhonov#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1)
10750aadb33SDmitri Tikhonov#endif
10850aadb33SDmitri Tikhonov
10983287402SDmitri Tikhonovstruct enc_session_funcs
11050aadb33SDmitri Tikhonov{
11183287402SDmitri Tikhonov    /* Global initialization: call once per implementation */
11283287402SDmitri Tikhonov    int (*esf_global_init)(int flags);
11350aadb33SDmitri Tikhonov
11483287402SDmitri Tikhonov    /* Global cleanup: call once per implementation */
11583287402SDmitri Tikhonov    void (*esf_global_cleanup) (void);
11650aadb33SDmitri Tikhonov
11750aadb33SDmitri Tikhonov#if LSQUIC_KEEP_ENC_SESS_HISTORY
11883287402SDmitri Tikhonov    /* Grab encryption session history */
11983287402SDmitri Tikhonov    void (*esf_get_hist) (const lsquic_enc_session_t *,
12083287402SDmitri Tikhonov                                            char buf[ESHIST_STR_SIZE]);
12150aadb33SDmitri Tikhonov#endif
12250aadb33SDmitri Tikhonov
12383287402SDmitri Tikhonov    /* Destroy enc session */
12483287402SDmitri Tikhonov    void (*esf_destroy)(lsquic_enc_session_t *enc_session);
12550aadb33SDmitri Tikhonov
12683287402SDmitri Tikhonov    /* Return true if handshake has been completed */
12783287402SDmitri Tikhonov    int (*esf_is_hsk_done)(lsquic_enc_session_t *enc_session);
12850aadb33SDmitri Tikhonov
12983287402SDmitri Tikhonov    /* Encrypt buffer */
1309626cfc2SDmitri Tikhonov    enum enc_level (*esf_encrypt)(lsquic_enc_session_t *enc_session,
1319626cfc2SDmitri Tikhonov               enum lsquic_version, uint8_t path_id, uint64_t pack_num,
13250aadb33SDmitri Tikhonov               const unsigned char *header, size_t header_len,
13350aadb33SDmitri Tikhonov               const unsigned char *data, size_t data_len,
13450aadb33SDmitri Tikhonov               unsigned char *buf_out, size_t max_out_len, size_t *out_len,
13550aadb33SDmitri Tikhonov               int is_hello);
13650aadb33SDmitri Tikhonov
137c51ce338SDmitri Tikhonov    /** Decrypt buffer
138c51ce338SDmitri Tikhonov     *
139c51ce338SDmitri Tikhonov     * If decryption is successful, decryption level is returned.  Otherwise,
140c51ce338SDmitri Tikhonov     * the return value is -1.
141c51ce338SDmitri Tikhonov     */
142c51ce338SDmitri Tikhonov    enum enc_level (*esf_decrypt)(lsquic_enc_session_t *enc_session,
143c51ce338SDmitri Tikhonov                   enum lsquic_version,
14483287402SDmitri Tikhonov                   uint8_t path_id, uint64_t pack_num,
14583287402SDmitri Tikhonov                   unsigned char *buf, size_t *header_len, size_t data_len,
14683287402SDmitri Tikhonov                   unsigned char *diversification_nonce,
14783287402SDmitri Tikhonov                   unsigned char *buf_out, size_t max_out_len, size_t *out_len);
14883287402SDmitri Tikhonov
14983287402SDmitri Tikhonov    /* Get value of setting specified by `tag' */
15083287402SDmitri Tikhonov    int (*esf_get_peer_setting) (const lsquic_enc_session_t *, uint32_t tag,
15183287402SDmitri Tikhonov                                                                uint32_t *val);
15283287402SDmitri Tikhonov
15383287402SDmitri Tikhonov    /* Get value of peer option (that from COPT array) */
15483287402SDmitri Tikhonov    int (*esf_get_peer_option) (const lsquic_enc_session_t *enc_session,
15583287402SDmitri Tikhonov                                                                uint32_t tag);
15683287402SDmitri Tikhonov
15783287402SDmitri Tikhonov    /* Create client session */
15883287402SDmitri Tikhonov    lsquic_enc_session_t *
15983287402SDmitri Tikhonov    (*esf_create_client) (const char *domain, lsquic_cid_t cid,
1608ca33e0eSDmitri Tikhonov                            const struct lsquic_engine_public *,
1618ca33e0eSDmitri Tikhonov                            const unsigned char *, size_t);
16250aadb33SDmitri Tikhonov
16383287402SDmitri Tikhonov    /* Generate connection ID */
16483287402SDmitri Tikhonov    lsquic_cid_t (*esf_generate_cid) (void);
16550aadb33SDmitri Tikhonov
16683287402SDmitri Tikhonov    /* -1 error, 0, OK, response in `buf' */
16783287402SDmitri Tikhonov    int
16883287402SDmitri Tikhonov    (*esf_gen_chlo) (lsquic_enc_session_t *, enum lsquic_version,
16983287402SDmitri Tikhonov                                                uint8_t *buf, size_t *len);
17083287402SDmitri Tikhonov
17183287402SDmitri Tikhonov    int
17283287402SDmitri Tikhonov    (*esf_handle_chlo_reply) (lsquic_enc_session_t *,
17383287402SDmitri Tikhonov                                                const uint8_t *data, int len);
174c51ce338SDmitri Tikhonov
175c51ce338SDmitri Tikhonov    size_t
176c51ce338SDmitri Tikhonov    (*esf_mem_used)(lsquic_enc_session_t *);
1779626cfc2SDmitri Tikhonov
1789626cfc2SDmitri Tikhonov    int
1799626cfc2SDmitri Tikhonov    (*esf_verify_reset_token) (lsquic_enc_session_t *, const unsigned char *,
1809626cfc2SDmitri Tikhonov                                                                    size_t);
1817f2bd84cSDmitri Tikhonov
1828ca33e0eSDmitri Tikhonov    int
1838ca33e0eSDmitri Tikhonov    (*esf_did_zero_rtt_succeed) (const lsquic_enc_session_t *);
1848ca33e0eSDmitri Tikhonov
1858ca33e0eSDmitri Tikhonov    int
1868ca33e0eSDmitri Tikhonov    (*esf_is_zero_rtt_enabled) (const lsquic_enc_session_t *);
1878ca33e0eSDmitri Tikhonov
1888ca33e0eSDmitri Tikhonov    c_cert_item_t *
1898ca33e0eSDmitri Tikhonov    (*esf_get_cert_item) (const lsquic_enc_session_t *);
1908ca33e0eSDmitri Tikhonov
1917f2bd84cSDmitri Tikhonov    struct stack_st_X509 *
1927f2bd84cSDmitri Tikhonov    (*esf_get_server_cert_chain) (lsquic_enc_session_t *);
1938ca33e0eSDmitri Tikhonov
1948ca33e0eSDmitri Tikhonov    ssize_t
1958ca33e0eSDmitri Tikhonov    (*esf_get_zero_rtt) (lsquic_enc_session_t *, enum lsquic_version,
1968ca33e0eSDmitri Tikhonov                                                            void *, size_t);
19783287402SDmitri Tikhonov};
19850aadb33SDmitri Tikhonov
19983287402SDmitri Tikhonovextern
20050aadb33SDmitri Tikhonov#ifdef NDEBUG
20183287402SDmitri Tikhonovconst
20250aadb33SDmitri Tikhonov#endif
20383287402SDmitri Tikhonovstruct enc_session_funcs lsquic_enc_session_gquic_1;
20483287402SDmitri Tikhonov
20583287402SDmitri Tikhonov#define select_esf_by_ver(ver) \
20683287402SDmitri Tikhonov    (ver ? &lsquic_enc_session_gquic_1 : &lsquic_enc_session_gquic_1)
20783287402SDmitri Tikhonov
20850aadb33SDmitri Tikhonov#endif
209