quic_srtm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 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_INTERNAL_QUIC_SRTM_H
  10. # define OSSL_INTERNAL_QUIC_SRTM_H
  11. # pragma once
  12. # include "internal/e_os.h"
  13. # include "internal/time.h"
  14. # include "internal/quic_types.h"
  15. # include "internal/quic_wire.h"
  16. # include "internal/quic_predef.h"
  17. # ifndef OPENSSL_NO_QUIC
  18. /*
  19. * QUIC Stateless Reset Token Manager
  20. * ==================================
  21. *
  22. * The stateless reset token manager is responsible for mapping stateless reset
  23. * tokens to connections. It is used to identify stateless reset tokens in
  24. * incoming packets. In this regard it can be considered an alternate "routing"
  25. * mechanism for incoming packets, and is somewhat analogous with the LCIDM,
  26. * except that it uses SRTs to route rather than DCIDs.
  27. *
  28. * The SRTM specifically stores a bidirectional mapping of the form
  29. *
  30. * (opaque pointer, sequence number) [1] <-> [0..n] SRT
  31. *
  32. * The (opaque pointer, sequence number) tuple is used to refer to an entry (for
  33. * example for the purposes of removing it later when it is no longer needed).
  34. * Likewise, an entry can be looked up using SRT to get the opaque pointer and
  35. * sequence number.
  36. *
  37. * It is important to note that the same SRT may exist multiple times and map to
  38. * multiple (opaque pointer, sequence number) tuples, for example, if we
  39. * initiate multiple connections to the same peer using the same local QUIC_PORT
  40. * and the peer decides to behave bizarrely and issue the same SRT for both
  41. * connections. It should not do this, but we have to be resilient against
  42. * byzantine peer behaviour. Thus we are capable of storing multiple identical
  43. * SRTs for different (opaque pointer, sequence number) keys.
  44. *
  45. * The SRTM supports arbitrary insertion, arbitrary deletion of specific keys
  46. * identified by a (opaque pointer, sequence number) key, and mass deletion of
  47. * all entries under a specific opaque pointer. It supports lookup by SRT to
  48. * identify zero or more corresponding (opaque pointer, sequence number) tuples.
  49. *
  50. * The opaque pointer may be used for any purpose but is intended to represent a
  51. * connection identity and must therefore be consistent (usefully comparable).
  52. */
  53. /* Creates a new empty SRTM instance. */
  54. QUIC_SRTM *ossl_quic_srtm_new(OSSL_LIB_CTX *libctx, const char *propq);
  55. /* Frees a SRTM instance. No-op if srtm is NULL. */
  56. void ossl_quic_srtm_free(QUIC_SRTM *srtm);
  57. /*
  58. * Add a (opaque, seq_num) -> SRT entry to the SRTM. This operation fails if a
  59. * SRT entry already exists with the same (opaque, seq_num) tuple. The token is
  60. * copied. Returns 1 on success or 0 on failure.
  61. */
  62. int ossl_quic_srtm_add(QUIC_SRTM *srtm, void *opaque, uint64_t seq_num,
  63. const QUIC_STATELESS_RESET_TOKEN *token);
  64. /*
  65. * Removes an entry by identifying it via its (opaque, seq_num) tuple.
  66. * Returns 1 if the entry was found and removed, and 0 if it was not found.
  67. */
  68. int ossl_quic_srtm_remove(QUIC_SRTM *srtm, void *opaque, uint64_t seq_num);
  69. /*
  70. * Removes all entries (opaque, *) with the given opaque pointer.
  71. *
  72. * Returns 1 on success and 0 on failure. If no entries with the given opaque
  73. * pointer were found, this is considered a success condition.
  74. */
  75. int ossl_quic_srtm_cull(QUIC_SRTM *strm, void *opaque);
  76. /*
  77. * Looks up a SRT to find the corresponding opaque pointer and sequence number.
  78. * An output field pointer can be set to NULL if it is not required.
  79. *
  80. * This function is designed to avoid exposing timing channels on token values
  81. * or the contents of the SRT mapping.
  82. *
  83. * If there are several identical SRTs, idx can be used to get the nth entry.
  84. * Call this function with idx set to 0 first, and keep calling it after
  85. * incrementing idx until it returns 0.
  86. *
  87. * Returns 1 if an entry was found and 0 otherwise.
  88. */
  89. int ossl_quic_srtm_lookup(QUIC_SRTM *srtm,
  90. const QUIC_STATELESS_RESET_TOKEN *token,
  91. size_t idx,
  92. void **opaque, uint64_t *seq_num);
  93. /* Verify internal invariants and assert if they are not met. */
  94. void ossl_quic_srtm_check(const QUIC_SRTM *srtm);
  95. # endif
  96. #endif