lsquic_enc_sess.h revision a137764b
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef LSQUIC_ENC_SESS_H 3#define LSQUIC_ENC_SESS_H 1 4 5struct lsquic_alarmset; 6struct lsquic_engine_public; 7struct lsquic_packet_out; 8struct lsquic_packet_in; 9struct stream_wrapper; 10struct ver_neg; 11struct lsquic_conn; 12struct transport_params; 13struct lsquic_cid; 14struct ssl_stream_method_st; 15struct ssl_st; 16struct sockaddr; 17struct conn_cid_elem; 18 19#define DNONC_LENGTH 32 20#define SRST_LENGTH 16 21 22/* From [draft-ietf-quic-tls-14]: 23 * 24 * Data is protected using a number of encryption levels: 25 * 26 * o Plaintext 27 * 28 * o Early Data (0-RTT) Keys 29 * 30 * o Handshake Keys 31 * 32 * o Application Data (1-RTT) Keys 33 */ 34 35/* This enum maps to the list above */ 36enum enc_level 37{ 38 ENC_LEV_CLEAR, 39 ENC_LEV_EARLY, 40 ENC_LEV_INIT, 41 ENC_LEV_FORW, 42 N_ENC_LEVS 43}; 44 45enum handshake_error /* TODO: rename this enum */ 46{ 47 DATA_NOT_ENOUGH = -2, 48 DATA_FORMAT_ERROR = -1, 49 HS_ERROR = -1, 50 DATA_NO_ERROR = 0, 51 HS_SHLO = 0, 52 HS_1RTT = 1, 53 HS_SREJ = 2, 54 HS_DELAYED = 3, 55 HS_PK_OFFLOAD = 4, 56}; 57 58#ifndef LSQUIC_KEEP_ENC_SESS_HISTORY 59# ifndef NDEBUG 60# define LSQUIC_KEEP_ENC_SESS_HISTORY 1 61# else 62# define LSQUIC_KEEP_ENC_SESS_HISTORY 0 63# endif 64#endif 65 66#if LSQUIC_KEEP_ENC_SESS_HISTORY 67#define ESHIST_BITS 7 68#define ESHIST_MASK ((1 << ESHIST_BITS) - 1) 69#define ESHIST_STR_SIZE ((1 << ESHIST_BITS) + 1) 70#endif 71 72enum enc_packout { ENCPA_OK, ENCPA_NOMEM, ENCPA_BADCRYPT, }; 73 74enum dec_packin { 75 DECPI_OK, 76 DECPI_NOMEM, 77 DECPI_TOO_SHORT, 78 DECPI_NOT_YET, 79 DECPI_BADCRYPT, 80 DECPI_VIOLATION, 81}; 82 83typedef void enc_session_t; 84 85struct enc_session_funcs_common 86{ 87 /* Global initialization: call once per implementation */ 88 int (*esf_global_init)(int flags); 89 90 /* Global cleanup: call once per implementation */ 91 void (*esf_global_cleanup) (void); 92 93 const char * 94 (*esf_cipher) (enc_session_t *); 95 96 int 97 (*esf_keysize) (enc_session_t *); 98 99 int 100 (*esf_alg_keysize) (enc_session_t *); 101 102 /* Need to pass lconn in encrypt and decrypt methods because enc_session 103 * is allowed to be NULL for gQUIC. 104 */ 105 enum enc_packout 106 (*esf_encrypt_packet) (enc_session_t *, const struct lsquic_engine_public *, 107 struct lsquic_conn *, struct lsquic_packet_out *); 108 109 enum dec_packin 110 (*esf_decrypt_packet)(enc_session_t *, struct lsquic_engine_public *, 111 const struct lsquic_conn *, struct lsquic_packet_in *); 112 113 struct stack_st_X509 * 114 (*esf_get_server_cert_chain) (enc_session_t *); 115 116 int 117 (*esf_verify_reset_token) (enc_session_t *, const unsigned char *, size_t); 118 119 int 120 (*esf_did_zero_rtt_succeed) (enc_session_t *); 121 122 int 123 (*esf_is_zero_rtt_enabled) (enc_session_t *); 124 125 void 126 (*esf_set_conn) (enc_session_t *, struct lsquic_conn *); 127 128 unsigned 129 esf_tag_len; 130}; 131 132struct enc_session_funcs_gquic 133{ 134#if LSQUIC_KEEP_ENC_SESS_HISTORY 135 /* Grab encryption session history */ 136 void (*esf_get_hist) (enc_session_t *, 137 char buf[ESHIST_STR_SIZE]); 138#endif 139 140 /* Destroy enc session */ 141 void (*esf_destroy)(enc_session_t *enc_session); 142 143 /* Return true if handshake has been completed */ 144 int (*esf_is_hsk_done)(enc_session_t *enc_session); 145 146 /* Get value of setting specified by `tag' */ 147 int (*esf_get_peer_setting) (enc_session_t *, uint32_t tag, 148 uint32_t *val); 149 150 /* Get value of peer option (that from COPT array) */ 151 int (*esf_get_peer_option) (enc_session_t *enc_session, 152 uint32_t tag); 153 154 /* Create server session */ 155 enc_session_t * 156 (*esf_create_server) (struct lsquic_conn *, 157 lsquic_cid_t cid, const struct lsquic_engine_public *); 158 159 /* out_len should have init value as the max length of out */ 160 enum handshake_error 161 (*esf_handle_chlo) (enc_session_t *enc_session, enum lsquic_version, 162 const uint8_t *in, int in_len, time_t t, 163 const struct sockaddr *ip_addr, const struct sockaddr *local, 164 uint8_t *out, size_t *out_len, 165 uint8_t nonce[DNONC_LENGTH], int *nonce_set); 166 167 void (*esf_hsk_destroy)(void *hsk_ctx); 168 169#ifndef NDEBUG 170 /* Need to expose this function for testing */ 171 int (*esf_determine_diversification_key) (enc_session_t *, 172 uint8_t *diversification_nonce, int is_client); 173#endif 174 175 const char * 176 (*esf_get_ua) (enc_session_t *); 177 178 int 179 (*esf_have_key_gt_one) (enc_session_t *enc_session); 180 181#ifndef NDEBUG 182 /* Functions that are only relevant in maintest. We may want to get rid 183 * of them somehow and only use the public API to test. 184 */ 185 186 uint8_t 187 (*esf_have_key) (enc_session_t *); 188 189 void 190 (*esf_set_have_key) (enc_session_t *, uint8_t); 191 192 const unsigned char * 193 (*esf_get_enc_key_i) (enc_session_t *); 194 195 const unsigned char * 196 (*esf_get_dec_key_i) (enc_session_t *); 197 198 const unsigned char * 199 (*esf_get_enc_key_nonce_i) (enc_session_t *); 200 201 const unsigned char * 202 (*esf_get_dec_key_nonce_i) (enc_session_t *); 203 204 const unsigned char * 205 (*esf_get_enc_key_nonce_f) (enc_session_t *); 206 207 const unsigned char * 208 (*esf_get_dec_key_nonce_f) (enc_session_t *); 209#endif /* !defined(NDEBUG) */ 210 211#if LSQUIC_ENABLE_HANDSHAKE_DISABLE 212 void 213 (*esf_set_handshake_completed) (enc_session_t *); 214#endif 215 216 /* Create client session */ 217 enc_session_t * 218 (*esf_create_client) (struct lsquic_conn *, const char *domain, 219 lsquic_cid_t cid, 220 const struct lsquic_engine_public *, 221 const unsigned char *, size_t); 222 223 /* -1 error, 0, OK, response in `buf' */ 224 int 225 (*esf_gen_chlo) (enc_session_t *, enum lsquic_version, 226 uint8_t *buf, size_t *len); 227 228 int 229 (*esf_handle_chlo_reply) (enc_session_t *, 230 const uint8_t *data, int len); 231 232 size_t 233 (*esf_mem_used)(enc_session_t *); 234 235 /* Zero-rtt serialization needs the knowledge of the QUIC version, that's 236 * why there is a separate method for thus. Plus, we want to be able to 237 * call it after the "handshake is done" callback is called. 238 */ 239 void (*esf_maybe_dispatch_zero_rtt) (enc_session_t *, 240 void (*cb)(struct lsquic_conn *, const unsigned char *, size_t)); 241 242 void (*esf_reset_cid) (enc_session_t *, const lsquic_cid_t *); 243}; 244 245enum iquic_handshake_status { 246 IHS_WANT_READ, 247 IHS_WANT_WRITE, 248 IHS_STOP, 249}; 250 251struct crypto_stream_if 252{ 253 ssize_t (*csi_write) (void *stream, const void *buf, size_t len); 254 int (*csi_flush) (void *stream); 255 ssize_t (*csi_readf) (void *stream, 256 size_t (*readf)(void *, const unsigned char *, size_t, int), void *ctx); 257 int (*csi_wantwrite) (void *stream, int is_want); 258 int (*csi_wantread) (void *stream, int is_want); 259 enum enc_level 260 (*csi_enc_level) (void *stream); 261}; 262 263struct enc_session_funcs_iquic 264{ 265 enc_session_t * 266 (*esfi_create_client) (const char *domain, struct lsquic_engine_public *, 267 struct lsquic_conn *, const struct lsquic_cid *, 268 const struct ver_neg *, void *(crypto_streams)[4], 269 const struct crypto_stream_if *, 270 const unsigned char *, size_t, 271 struct lsquic_alarmset *, unsigned); 272 273 void 274 (*esfi_destroy) (enc_session_t *); 275 276 struct ssl_st * 277 (*esfi_get_ssl) (enc_session_t *); 278 279 struct transport_params * 280 (*esfi_get_peer_transport_params) (enc_session_t *); 281 282 int 283 (*esfi_reset_dcid) (enc_session_t *, const struct lsquic_cid *, 284 const struct lsquic_cid *); 285 286 int 287 (*esfi_init_server) (enc_session_t *); 288 289 void 290 (*esfi_set_streams) (enc_session_t *, void *(crypto_streams)[4], 291 const struct crypto_stream_if *); 292 293 enc_session_t * 294 (*esfi_create_server) (struct lsquic_engine_public *, struct lsquic_conn *, 295 const struct lsquic_cid *, 296 void *(crypto_streams)[4], 297 const struct crypto_stream_if *, 298 const struct lsquic_cid *odcid); 299 300 void 301 (*esfi_shake_stream)(enc_session_t *, struct lsquic_stream *, 302 const char *); 303 304 void 305 (*esfi_1rtt_acked)(enc_session_t *); 306}; 307 308extern 309#ifdef NDEBUG 310const 311#endif 312struct enc_session_funcs_common lsquic_enc_session_common_gquic_1; 313extern const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1; 314 315extern 316#ifdef NDEBUG 317const 318#endif 319struct enc_session_funcs_gquic lsquic_enc_session_gquic_gquic_1; 320 321extern const struct enc_session_funcs_iquic lsquic_enc_session_iquic_ietf_v1; 322 323#define select_esf_common_by_ver(ver) ( \ 324 ver == LSQVER_ID23 ? &lsquic_enc_session_common_ietf_v1 : \ 325 ver == LSQVER_ID24 ? &lsquic_enc_session_common_ietf_v1 : \ 326 ver == LSQVER_VERNEG ? &lsquic_enc_session_common_ietf_v1 : \ 327 &lsquic_enc_session_common_gquic_1 ) 328 329#define select_esf_gquic_by_ver(ver) ( \ 330 ver ? &lsquic_enc_session_gquic_gquic_1 : &lsquic_enc_session_gquic_gquic_1) 331 332#define select_esf_iquic_by_ver(ver) ( \ 333 ver ? &lsquic_enc_session_iquic_ietf_v1 : &lsquic_enc_session_iquic_ietf_v1) 334 335extern const char *const lsquic_enclev2str[]; 336 337extern const struct lsquic_stream_if lsquic_cry_sm_if; 338 339extern const struct lsquic_stream_if lsquic_mini_cry_sm_if; 340 341/* RFC 7301, Section 3.2 */ 342#define ALERT_NO_APPLICATION_PROTOCOL 120 343 344enum lsquic_version 345lsquic_zero_rtt_version (const unsigned char *, size_t); 346 347/* This is seems to be true for all of the ciphers used by IETF QUIC. 348 * XXX: Perhaps add a check? 349 */ 350#define IQUIC_TAG_LEN 16 351 352#endif 353