1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech#ifndef LSQUIC_HANDSHAKE_H
35392f7a3SLiteSpeed Tech#define LSQUIC_HANDSHAKE_H 1
450aadb33SDmitri Tikhonov
550aadb33SDmitri Tikhonov#define aes128_key_len 16
650aadb33SDmitri Tikhonov#define aes128_iv_len 4
75392f7a3SLiteSpeed Tech#define STK_LENGTH   60
85392f7a3SLiteSpeed Tech#define SCID_LENGTH  16
950aadb33SDmitri Tikhonov
105392f7a3SLiteSpeed Techstruct lsquic_server_config;
115392f7a3SLiteSpeed Techstruct sockaddr;
125392f7a3SLiteSpeed Techstruct lsquic_str;
135392f7a3SLiteSpeed Techstruct lsquic_packet_in;
145392f7a3SLiteSpeed Techstruct lsquic_cid;
15a137764bSDmitri Tikhonovstruct lsquic_enc_session;
162f4629f2SDmitri Tikhonovstruct lsquic_engine_public;
1750aadb33SDmitri Tikhonov
185392f7a3SLiteSpeed Tech/* client side, certs and hashs
195392f7a3SLiteSpeed Tech */
205392f7a3SLiteSpeed Techtypedef struct cert_hash_item_st
21c51ce338SDmitri Tikhonov{
225392f7a3SLiteSpeed Tech    struct lsquic_str*   domain; /*with port, such as "xyz.com:8088" as the key */
235392f7a3SLiteSpeed Tech    struct lsquic_str*   crts;
245392f7a3SLiteSpeed Tech    struct lsquic_str*   hashs;
255392f7a3SLiteSpeed Tech    struct lsquic_hash_elem hash_el;
265392f7a3SLiteSpeed Tech    int         count;
275392f7a3SLiteSpeed Tech} cert_hash_item_t;
285392f7a3SLiteSpeed Tech
295392f7a3SLiteSpeed Tech#ifndef NDEBUG
305392f7a3SLiteSpeed Techenum hsk_failure_reason
31a5fa05f9SDmitri Tikhonovlsquic_verify_stk0(const struct lsquic_enc_session *,
32a137764bSDmitri Tikhonov            struct lsquic_server_config *, const struct sockaddr *ip_addr, uint64_t tm,
335392f7a3SLiteSpeed Tech               struct lsquic_str *stk,
345392f7a3SLiteSpeed Tech               unsigned secs_since_stk_generated);
355392f7a3SLiteSpeed Techenum hsk_failure_reason
36a5fa05f9SDmitri Tikhonovlsquic_verify_stk(void *, const struct sockaddr *ip_addr,
375392f7a3SLiteSpeed Tech                                        uint64_t tm, struct lsquic_str *stk);
385392f7a3SLiteSpeed Techstruct cert_hash_item_st* c_find_certs(const struct lsquic_str *domain);
395392f7a3SLiteSpeed Tech#endif
40c51ce338SDmitri Tikhonov
415392f7a3SLiteSpeed Tech#define SNO_LENGTH   56
429626cfc2SDmitri Tikhonov
435392f7a3SLiteSpeed Tech/* EVP_AEAD_CTX from boringssl pre-18d9f28f0df9f95570. */
445392f7a3SLiteSpeed Techstruct old_evp_aead_ctx_st {
455392f7a3SLiteSpeed Tech    void *ptr1;     /* aead */
465392f7a3SLiteSpeed Tech    void *ptr2;     /* aead_state */
475392f7a3SLiteSpeed Tech};
488ca33e0eSDmitri Tikhonov
495392f7a3SLiteSpeed Tech/* Server need refresh SCFG once a day */
505392f7a3SLiteSpeed Tech/* can not use sizeof() to get the size */
515392f7a3SLiteSpeed Techtypedef struct SCFG_info_st
5250aadb33SDmitri Tikhonov{
5350aadb33SDmitri Tikhonov    unsigned char   sscid[SCID_LENGTH];
545392f7a3SLiteSpeed Tech    unsigned char   priv_key[32];
555392f7a3SLiteSpeed Tech    unsigned char   skt_key[16];
565392f7a3SLiteSpeed Tech    uint32_t        aead; /* Fixed, ONLY AESG */
575392f7a3SLiteSpeed Tech    uint32_t        kexs; /* Fixed, ONLY C255 */
585392f7a3SLiteSpeed Tech    uint32_t        pdmd; /* Fixed, ONLY X509 */
595392f7a3SLiteSpeed Tech    uint64_t        orbt; /* Fixed, 0 */
605392f7a3SLiteSpeed Tech    uint64_t        expy;
615392f7a3SLiteSpeed Tech    /* Keep the hole for compatibility with older builds of LSWS: */
625392f7a3SLiteSpeed Tech    struct old_evp_aead_ctx_st unused
635392f7a3SLiteSpeed Tech#if __GNUC__
645392f7a3SLiteSpeed Tech                                      __attribute__((deprecated))
655392f7a3SLiteSpeed Tech#endif
665392f7a3SLiteSpeed Tech                                                                 ;
675392f7a3SLiteSpeed Tech    short           scfg_len;
685392f7a3SLiteSpeed Tech} SCFG_info_t;
6950aadb33SDmitri Tikhonov
705392f7a3SLiteSpeed Techstruct SCFG_st
718ca33e0eSDmitri Tikhonov{
725392f7a3SLiteSpeed Tech    SCFG_info_t info;
735392f7a3SLiteSpeed Tech    unsigned char   scfg[]; /* whoile buffer */
748ca33e0eSDmitri Tikhonov};
755392f7a3SLiteSpeed Techtypedef struct SCFG_st SCFG_t;
765392f7a3SLiteSpeed Tech/* server side need to store STK with expired time */
778ca33e0eSDmitri Tikhonov
785392f7a3SLiteSpeed Techtypedef struct lsquic_server_config
7950aadb33SDmitri Tikhonov{
805392f7a3SLiteSpeed Tech    SCFG_t         *lsc_scfg;   /* This part is stored in SHM */
815392f7a3SLiteSpeed Tech    EVP_AEAD_CTX    lsc_stk_ctx;
825392f7a3SLiteSpeed Tech} lsquic_server_config_t;
8350aadb33SDmitri Tikhonov
845392f7a3SLiteSpeed Tech/* Based on enum HandshakeFailureReason in Chromium */
855392f7a3SLiteSpeed Techenum hsk_failure_reason
865392f7a3SLiteSpeed Tech{
875392f7a3SLiteSpeed Tech    HFR_HANDSHAKE_OK                         =  0,
885392f7a3SLiteSpeed Tech
895392f7a3SLiteSpeed Tech    /* Invalid client nonce in CHLO: */
905392f7a3SLiteSpeed Tech    HFR_CLIENT_NONCE_UNKNOWN                 =  1,  /* Default nonce failure */
915392f7a3SLiteSpeed Tech    HFR_CLIENT_NONCE_INVALID                 =  2,  /* Incorrect nonce length */
925392f7a3SLiteSpeed Tech    HFR_CLIENT_NONCE_NOT_UNIQ                =  3,
935392f7a3SLiteSpeed Tech    HFR_CLIENT_NONCE_INVALID_ORBIT           =  4,
945392f7a3SLiteSpeed Tech    HFR_CLIENT_NONCE_INVALID_TIME            =  5,
955392f7a3SLiteSpeed Tech
965392f7a3SLiteSpeed Tech    /* Invalid server nonce in CHLO: */
975392f7a3SLiteSpeed Tech    HFR_SERVER_NONCE_DECRYPTION              =  8,
985392f7a3SLiteSpeed Tech    HFR_SERVER_NONCE_INVALID                 =  9,
995392f7a3SLiteSpeed Tech    HFR_SERVER_NONCE_NOT_UNIQUE              =  10,
1005392f7a3SLiteSpeed Tech    HFR_SERVER_NONCE_INVALID_TIME            =  11,
1015392f7a3SLiteSpeed Tech    HFR_SERVER_NONCE_REQUIRED                =  20,
1025392f7a3SLiteSpeed Tech
1035392f7a3SLiteSpeed Tech    HFR_CONFIG_INCHOATE_HELLO                =  12, /* Missing SCID tag */
1045392f7a3SLiteSpeed Tech    HFR_CONFIG_UNKNOWN_CONFIG                =  13, /* Could not find server config SCID */
1055392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_INVALID               =  14, /* Missing STK tag */
1065392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_DECRYPTION            =  15,
1075392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_PARSE                 =  16,
1085392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_DIFFERENT_IP_ADDRESS  =  17,
1095392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_CLOCK_SKEW            =  18,
1105392f7a3SLiteSpeed Tech    HFR_SRC_ADDR_TOKEN_EXPIRED               =  19,
1115392f7a3SLiteSpeed Tech    HFR_INVALID_EXPECTED_LEAF_CERTIFICATE    =  21,
11283287402SDmitri Tikhonov};
11350aadb33SDmitri Tikhonov
11490fe3b25SDmitri Tikhonovenum lsquic_version
1157483dee0SDmitri Tikhonovlsquic_sess_resume_version (const unsigned char *, size_t);
11690fe3b25SDmitri Tikhonov
1172f4629f2SDmitri Tikhonovint
1182f4629f2SDmitri Tikhonovlsquic_init_gquic_crypto (struct lsquic_engine_public *enpub);
1192f4629f2SDmitri Tikhonov
1202f4629f2SDmitri Tikhonovvoid
1212f4629f2SDmitri Tikhonovlsquic_cleanup_gquic_crypto (struct lsquic_engine_public *enpub);
1222f4629f2SDmitri Tikhonov
12350aadb33SDmitri Tikhonov#endif
124