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