12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2021 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef SHRPX_QUIC_H
262c593315Sopenharmony_ci#define SHRPX_QUIC_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "shrpx.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <stdint.h>
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#include <functional>
332c593315Sopenharmony_ci
342c593315Sopenharmony_ci#include <ngtcp2/ngtcp2.h>
352c593315Sopenharmony_ci
362c593315Sopenharmony_ci#include "network.h"
372c593315Sopenharmony_ci
382c593315Sopenharmony_ciusing namespace nghttp2;
392c593315Sopenharmony_ci
402c593315Sopenharmony_cinamespace std {
412c593315Sopenharmony_citemplate <> struct hash<ngtcp2_cid> {
422c593315Sopenharmony_ci  std::size_t operator()(const ngtcp2_cid &cid) const noexcept {
432c593315Sopenharmony_ci    // FNV-1a 64bits variant
442c593315Sopenharmony_ci    constexpr uint64_t basis = 0xCBF29CE484222325ULL;
452c593315Sopenharmony_ci    const uint8_t *p = cid.data, *end = cid.data + cid.datalen;
462c593315Sopenharmony_ci    uint64_t h = basis;
472c593315Sopenharmony_ci
482c593315Sopenharmony_ci    for (; p != end;) {
492c593315Sopenharmony_ci      h ^= *p++;
502c593315Sopenharmony_ci      h *= basis;
512c593315Sopenharmony_ci    }
522c593315Sopenharmony_ci
532c593315Sopenharmony_ci    return static_cast<size_t>(h);
542c593315Sopenharmony_ci  }
552c593315Sopenharmony_ci};
562c593315Sopenharmony_ci} // namespace std
572c593315Sopenharmony_ci
582c593315Sopenharmony_cibool operator==(const ngtcp2_cid &lhs, const ngtcp2_cid &rhs);
592c593315Sopenharmony_ci
602c593315Sopenharmony_cinamespace shrpx {
612c593315Sopenharmony_ci
622c593315Sopenharmony_cistruct UpstreamAddr;
632c593315Sopenharmony_cistruct QUICKeyingMaterials;
642c593315Sopenharmony_cistruct QUICKeyingMaterial;
652c593315Sopenharmony_ci
662c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_SCIDLEN = 20;
672c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_SERVER_IDLEN = 4;
682c593315Sopenharmony_ci// SHRPX_QUIC_CID_PREFIXLEN includes SHRPX_QUIC_SERVER_IDLEN.
692c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_CID_PREFIXLEN = 8;
702c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_CID_PREFIX_OFFSET = 1;
712c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_DECRYPTED_DCIDLEN = 16;
722c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_CID_ENCRYPTION_KEYLEN = 16;
732c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_MAX_UDP_PAYLOAD_SIZE = 1472;
742c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_CONN_CLOSE_PKTLEN = 256;
752c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_STATELESS_RESET_BURST = 100;
762c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_SECRET_RESERVEDLEN = 4;
772c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_SECRETLEN = 32;
782c593315Sopenharmony_ciconstexpr size_t SHRPX_QUIC_SALTLEN = 32;
792c593315Sopenharmony_ciconstexpr uint8_t SHRPX_QUIC_DCID_KM_ID_MASK = 0xc0;
802c593315Sopenharmony_ci
812c593315Sopenharmony_cingtcp2_tstamp quic_timestamp();
822c593315Sopenharmony_ci
832c593315Sopenharmony_ciint quic_send_packet(const UpstreamAddr *faddr, const sockaddr *remote_sa,
842c593315Sopenharmony_ci                     size_t remote_salen, const sockaddr *local_sa,
852c593315Sopenharmony_ci                     size_t local_salen, const ngtcp2_pkt_info &pi,
862c593315Sopenharmony_ci                     const uint8_t *data, size_t datalen, size_t gso_size);
872c593315Sopenharmony_ci
882c593315Sopenharmony_ciint generate_quic_retry_connection_id(ngtcp2_cid &cid, size_t cidlen,
892c593315Sopenharmony_ci                                      const uint8_t *server_id, uint8_t km_id,
902c593315Sopenharmony_ci                                      const uint8_t *key);
912c593315Sopenharmony_ci
922c593315Sopenharmony_ciint generate_quic_connection_id(ngtcp2_cid &cid, size_t cidlen,
932c593315Sopenharmony_ci                                const uint8_t *cid_prefix, uint8_t km_id,
942c593315Sopenharmony_ci                                const uint8_t *key);
952c593315Sopenharmony_ci
962c593315Sopenharmony_ciint encrypt_quic_connection_id(uint8_t *dest, const uint8_t *src,
972c593315Sopenharmony_ci                               const uint8_t *key);
982c593315Sopenharmony_ci
992c593315Sopenharmony_ciint decrypt_quic_connection_id(uint8_t *dest, const uint8_t *src,
1002c593315Sopenharmony_ci                               const uint8_t *key);
1012c593315Sopenharmony_ci
1022c593315Sopenharmony_ciint generate_quic_hashed_connection_id(ngtcp2_cid &dest,
1032c593315Sopenharmony_ci                                       const Address &remote_addr,
1042c593315Sopenharmony_ci                                       const Address &local_addr,
1052c593315Sopenharmony_ci                                       const ngtcp2_cid &cid);
1062c593315Sopenharmony_ci
1072c593315Sopenharmony_ciint generate_quic_stateless_reset_token(uint8_t *token, const ngtcp2_cid &cid,
1082c593315Sopenharmony_ci                                        const uint8_t *secret,
1092c593315Sopenharmony_ci                                        size_t secretlen);
1102c593315Sopenharmony_ci
1112c593315Sopenharmony_ciint generate_retry_token(uint8_t *token, size_t &tokenlen, uint32_t version,
1122c593315Sopenharmony_ci                         const sockaddr *sa, socklen_t salen,
1132c593315Sopenharmony_ci                         const ngtcp2_cid &retry_scid, const ngtcp2_cid &odcid,
1142c593315Sopenharmony_ci                         const uint8_t *secret, size_t secretlen);
1152c593315Sopenharmony_ci
1162c593315Sopenharmony_ciint verify_retry_token(ngtcp2_cid &odcid, const uint8_t *token, size_t tokenlen,
1172c593315Sopenharmony_ci                       uint32_t version, const ngtcp2_cid &dcid,
1182c593315Sopenharmony_ci                       const sockaddr *sa, socklen_t salen,
1192c593315Sopenharmony_ci                       const uint8_t *secret, size_t secretlen);
1202c593315Sopenharmony_ci
1212c593315Sopenharmony_ciint generate_token(uint8_t *token, size_t &tokenlen, const sockaddr *sa,
1222c593315Sopenharmony_ci                   size_t salen, const uint8_t *secret, size_t secretlen);
1232c593315Sopenharmony_ci
1242c593315Sopenharmony_ciint verify_token(const uint8_t *token, size_t tokenlen, const sockaddr *sa,
1252c593315Sopenharmony_ci                 socklen_t salen, const uint8_t *secret, size_t secretlen);
1262c593315Sopenharmony_ci
1272c593315Sopenharmony_ciint generate_quic_connection_id_encryption_key(uint8_t *key, size_t keylen,
1282c593315Sopenharmony_ci                                               const uint8_t *secret,
1292c593315Sopenharmony_ci                                               size_t secretlen,
1302c593315Sopenharmony_ci                                               const uint8_t *salt,
1312c593315Sopenharmony_ci                                               size_t saltlen);
1322c593315Sopenharmony_ci
1332c593315Sopenharmony_ciconst QUICKeyingMaterial *
1342c593315Sopenharmony_ciselect_quic_keying_material(const QUICKeyingMaterials &qkms, uint8_t km_id);
1352c593315Sopenharmony_ci
1362c593315Sopenharmony_ci} // namespace shrpx
1372c593315Sopenharmony_ci
1382c593315Sopenharmony_ci#endif // SHRPX_QUIC_H
139