123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- #ifndef OSSL_QUIC_WIRE_PKT_H
- # define OSSL_QUIC_WIRE_PKT_H
- # include <openssl/ssl.h>
- # include "internal/packet_quic.h"
- # include "internal/quic_types.h"
- # ifndef OPENSSL_NO_QUIC
- # define QUIC_VERSION_NONE ((uint32_t)0)
- # define QUIC_VERSION_1 ((uint32_t)1)
- # define QUIC_PKT_TYPE_INITIAL 1
- # define QUIC_PKT_TYPE_0RTT 2
- # define QUIC_PKT_TYPE_HANDSHAKE 3
- # define QUIC_PKT_TYPE_RETRY 4
- # define QUIC_PKT_TYPE_1RTT 5
- # define QUIC_PKT_TYPE_VERSION_NEG 6
- static ossl_inline ossl_unused uint32_t
- ossl_quic_pkt_type_to_enc_level(uint32_t pkt_type)
- {
- switch (pkt_type) {
- case QUIC_PKT_TYPE_INITIAL:
- return QUIC_ENC_LEVEL_INITIAL;
- case QUIC_PKT_TYPE_HANDSHAKE:
- return QUIC_ENC_LEVEL_HANDSHAKE;
- case QUIC_PKT_TYPE_0RTT:
- return QUIC_ENC_LEVEL_0RTT;
- case QUIC_PKT_TYPE_1RTT:
- return QUIC_ENC_LEVEL_1RTT;
- default:
- return QUIC_ENC_LEVEL_NUM;
- }
- }
- static ossl_inline ossl_unused uint32_t
- ossl_quic_enc_level_to_pkt_type(uint32_t enc_level)
- {
- switch (enc_level) {
- case QUIC_ENC_LEVEL_INITIAL:
- return QUIC_PKT_TYPE_INITIAL;
- case QUIC_ENC_LEVEL_HANDSHAKE:
- return QUIC_PKT_TYPE_HANDSHAKE;
- case QUIC_ENC_LEVEL_0RTT:
- return QUIC_PKT_TYPE_0RTT;
- case QUIC_ENC_LEVEL_1RTT:
- return QUIC_PKT_TYPE_1RTT;
- default:
- return UINT32_MAX;
- }
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_is_encrypted(uint32_t pkt_type)
- {
- switch (pkt_type) {
- case QUIC_PKT_TYPE_RETRY:
- case QUIC_PKT_TYPE_VERSION_NEG:
- return 0;
- default:
- return 1;
- }
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_has_pn(uint32_t pkt_type)
- {
-
- return ossl_quic_pkt_type_is_encrypted(pkt_type);
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_can_share_dgram(uint32_t pkt_type)
- {
-
- return ossl_quic_pkt_type_is_encrypted(pkt_type);
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_must_be_last(uint32_t pkt_type)
- {
-
- return !ossl_quic_pkt_type_can_share_dgram(pkt_type)
- || pkt_type == QUIC_PKT_TYPE_1RTT;
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_has_version(uint32_t pkt_type)
- {
- return pkt_type != QUIC_PKT_TYPE_1RTT && pkt_type != QUIC_PKT_TYPE_VERSION_NEG;
- }
- static ossl_inline ossl_unused int
- ossl_quic_pkt_type_has_scid(uint32_t pkt_type)
- {
- return pkt_type != QUIC_PKT_TYPE_1RTT;
- }
- # define QUIC_MIN_VALID_PKT_LEN_CRYPTO 21
- # define QUIC_MIN_VALID_PKT_LEN_VERSION_NEG 7
- # define QUIC_MIN_VALID_PKT_LEN QUIC_MIN_VALID_PKT_LEN_VERSION_NEG
- typedef struct quic_pkt_hdr_ptrs_st QUIC_PKT_HDR_PTRS;
- typedef struct quic_hdr_protector_st {
- OSSL_LIB_CTX *libctx;
- const char *propq;
- EVP_CIPHER_CTX *cipher_ctx;
- EVP_CIPHER *cipher;
- uint32_t cipher_id;
- } QUIC_HDR_PROTECTOR;
- # define QUIC_HDR_PROT_CIPHER_AES_128 1
- # define QUIC_HDR_PROT_CIPHER_AES_256 2
- # define QUIC_HDR_PROT_CIPHER_CHACHA 3
- int ossl_quic_hdr_protector_init(QUIC_HDR_PROTECTOR *hpr,
- OSSL_LIB_CTX *libctx,
- const char *propq,
- uint32_t cipher_id,
- const unsigned char *quic_hp_key,
- size_t quic_hp_key_len);
- void ossl_quic_hdr_protector_cleanup(QUIC_HDR_PROTECTOR *hpr);
- int ossl_quic_hdr_protector_decrypt(QUIC_HDR_PROTECTOR *hpr,
- QUIC_PKT_HDR_PTRS *ptrs);
- int ossl_quic_hdr_protector_encrypt(QUIC_HDR_PROTECTOR *hpr,
- QUIC_PKT_HDR_PTRS *ptrs);
- int ossl_quic_hdr_protector_decrypt_fields(QUIC_HDR_PROTECTOR *hpr,
- const unsigned char *sample,
- size_t sample_len,
- unsigned char *first_byte,
- unsigned char *pn_bytes);
- int ossl_quic_hdr_protector_encrypt_fields(QUIC_HDR_PROTECTOR *hpr,
- const unsigned char *sample,
- size_t sample_len,
- unsigned char *first_byte,
- unsigned char *pn_bytes);
- typedef struct quic_pkt_hdr_st {
-
- unsigned int type :8;
-
- unsigned int spin_bit :1;
-
- unsigned int key_phase :1;
-
- unsigned int pn_len :4;
-
- unsigned int partial :1;
-
- unsigned int fixed :1;
-
- unsigned int unused :4;
-
- unsigned int reserved :2;
-
- uint32_t version;
-
- QUIC_CONN_ID dst_conn_id;
-
- QUIC_CONN_ID src_conn_id;
-
- unsigned char pn[4];
-
- const unsigned char *token;
- size_t token_len;
-
- size_t len;
-
- const unsigned char *data;
- } QUIC_PKT_HDR;
- struct quic_pkt_hdr_ptrs_st {
- unsigned char *raw_start;
- unsigned char *raw_sample;
- size_t raw_sample_len;
-
- unsigned char *raw_pn;
- };
- int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
- size_t short_conn_id_len,
- int partial,
- int nodata,
- QUIC_PKT_HDR *hdr,
- QUIC_PKT_HDR_PTRS *ptrs);
- int ossl_quic_wire_encode_pkt_hdr(WPACKET *pkt,
- size_t short_conn_id_len,
- const QUIC_PKT_HDR *hdr,
- QUIC_PKT_HDR_PTRS *ptrs);
- int ossl_quic_wire_get_pkt_hdr_dst_conn_id(const unsigned char *buf,
- size_t buf_len,
- size_t short_conn_id_len,
- QUIC_CONN_ID *dst_conn_id);
- int ossl_quic_wire_get_encoded_pkt_hdr_len(size_t short_conn_id_len,
- const QUIC_PKT_HDR *hdr);
- int ossl_quic_wire_decode_pkt_hdr_pn(const unsigned char *enc_pn,
- size_t enc_pn_len,
- QUIC_PN largest_pn,
- QUIC_PN *res_pn);
- int ossl_quic_wire_determine_pn_len(QUIC_PN pn, QUIC_PN largest_acked);
- int ossl_quic_wire_encode_pkt_hdr_pn(QUIC_PN pn,
- unsigned char *enc_pn,
- size_t enc_pn_len);
- # define QUIC_RETRY_INTEGRITY_TAG_LEN 16
- int ossl_quic_validate_retry_integrity_tag(OSSL_LIB_CTX *libctx,
- const char *propq,
- const QUIC_PKT_HDR *hdr,
- const QUIC_CONN_ID *client_initial_dcid);
- int ossl_quic_calculate_retry_integrity_tag(OSSL_LIB_CTX *libctx,
- const char *propq,
- const QUIC_PKT_HDR *hdr,
- const QUIC_CONN_ID *client_initial_dcid,
- unsigned char *tag);
- # endif
- #endif
|