quic_types.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_QUIC_TYPES_H
  10. # define OSSL_QUIC_TYPES_H
  11. # include <openssl/ssl.h>
  12. # include <internal/ssl.h>
  13. # include <assert.h>
  14. # include <string.h>
  15. # ifndef OPENSSL_NO_QUIC
  16. /* QUIC encryption levels. */
  17. enum {
  18. QUIC_ENC_LEVEL_INITIAL = 0,
  19. QUIC_ENC_LEVEL_HANDSHAKE,
  20. QUIC_ENC_LEVEL_0RTT,
  21. QUIC_ENC_LEVEL_1RTT,
  22. QUIC_ENC_LEVEL_NUM /* Must be the ultimate entry */
  23. };
  24. /* QUIC packet number spaces. */
  25. enum {
  26. QUIC_PN_SPACE_INITIAL = 0,
  27. QUIC_PN_SPACE_HANDSHAKE,
  28. /* New entries must go here, so that QUIC_PN_SPACE_APP is the penultimate */
  29. QUIC_PN_SPACE_APP,
  30. QUIC_PN_SPACE_NUM /* Must be the ultimate entry */
  31. };
  32. static ossl_unused ossl_inline uint32_t
  33. ossl_quic_enc_level_to_pn_space(uint32_t enc_level)
  34. {
  35. switch (enc_level) {
  36. case QUIC_ENC_LEVEL_INITIAL:
  37. return QUIC_PN_SPACE_INITIAL;
  38. case QUIC_ENC_LEVEL_HANDSHAKE:
  39. return QUIC_PN_SPACE_HANDSHAKE;
  40. case QUIC_ENC_LEVEL_0RTT:
  41. case QUIC_ENC_LEVEL_1RTT:
  42. return QUIC_PN_SPACE_APP;
  43. default:
  44. assert(0);
  45. return QUIC_PN_SPACE_APP;
  46. }
  47. }
  48. /* QUIC packet number representation. */
  49. typedef uint64_t QUIC_PN;
  50. # define QUIC_PN_INVALID UINT64_MAX
  51. static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_max(QUIC_PN a, QUIC_PN b)
  52. {
  53. return a > b ? a : b;
  54. }
  55. static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_min(QUIC_PN a, QUIC_PN b)
  56. {
  57. return a < b ? a : b;
  58. }
  59. static ossl_unused ossl_inline int ossl_quic_pn_valid(QUIC_PN pn)
  60. {
  61. return pn < (((QUIC_PN)1) << 62);
  62. }
  63. /* QUIC connection ID representation. */
  64. # define QUIC_MAX_CONN_ID_LEN 20
  65. # define QUIC_MIN_ODCID_LEN 8 /* RFC 9000 s. 7.2 */
  66. typedef struct quic_conn_id_st {
  67. unsigned char id_len, id[QUIC_MAX_CONN_ID_LEN];
  68. } QUIC_CONN_ID;
  69. static ossl_unused ossl_inline int ossl_quic_conn_id_eq(const QUIC_CONN_ID *a,
  70. const QUIC_CONN_ID *b)
  71. {
  72. if (a->id_len != b->id_len || a->id_len > QUIC_MAX_CONN_ID_LEN)
  73. return 0;
  74. return memcmp(a->id, b->id, a->id_len) == 0;
  75. }
  76. /*
  77. * Generates a random CID of the given length. libctx may be NULL.
  78. * Returns 1 on success or 0 on failure.
  79. */
  80. int ossl_quic_gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len,
  81. QUIC_CONN_ID *cid);
  82. # define QUIC_MIN_INITIAL_DGRAM_LEN 1200
  83. # define QUIC_DEFAULT_ACK_DELAY_EXP 3
  84. # define QUIC_MAX_ACK_DELAY_EXP 20
  85. # define QUIC_DEFAULT_MAX_ACK_DELAY 25
  86. # define QUIC_MIN_ACTIVE_CONN_ID_LIMIT 2
  87. /* Arbitrary choice of default idle timeout (not an RFC value). */
  88. # define QUIC_DEFAULT_IDLE_TIMEOUT 30000
  89. # define QUIC_STATELESS_RESET_TOKEN_LEN 16
  90. typedef struct {
  91. unsigned char token[QUIC_STATELESS_RESET_TOKEN_LEN];
  92. } QUIC_STATELESS_RESET_TOKEN;
  93. /*
  94. * An encoded preferred_addr transport parameter cannot be shorter or longer
  95. * than these lengths in bytes.
  96. */
  97. # define QUIC_MIN_ENCODED_PREFERRED_ADDR_LEN 41
  98. # define QUIC_MAX_ENCODED_PREFERRED_ADDR_LEN 61
  99. # endif
  100. #endif