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