packet_quic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_INTERNAL_PACKET_QUIC_H
  10. # define OSSL_INTERNAL_PACKET_QUIC_H
  11. # pragma once
  12. # include "internal/packet.h"
  13. # include "internal/quic_vlint.h"
  14. # ifndef OPENSSL_NO_QUIC
  15. /*
  16. * Decodes a QUIC variable-length integer in |pkt| and stores the result in
  17. * |data|.
  18. */
  19. __owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,
  20. uint64_t *data)
  21. {
  22. size_t enclen;
  23. if (PACKET_remaining(pkt) < 1)
  24. return 0;
  25. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  26. if (PACKET_remaining(pkt) < enclen)
  27. return 0;
  28. *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
  29. packet_forward(pkt, enclen);
  30. return 1;
  31. }
  32. /*
  33. * Decodes a QUIC variable-length integer in |pkt| and stores the result in
  34. * |data|. Unlike PACKET_get_quic_vlint, this does not advance the current
  35. * position. If was_minimal is non-NULL, *was_minimal is set to 1 if the integer
  36. * was encoded using the minimal possible number of bytes and 0 otherwise.
  37. */
  38. __owur static ossl_inline int PACKET_peek_quic_vlint_ex(PACKET *pkt,
  39. uint64_t *data,
  40. int *was_minimal)
  41. {
  42. size_t enclen;
  43. if (PACKET_remaining(pkt) < 1)
  44. return 0;
  45. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  46. if (PACKET_remaining(pkt) < enclen)
  47. return 0;
  48. *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
  49. if (was_minimal != NULL)
  50. *was_minimal = (enclen == ossl_quic_vlint_encode_len(*data));
  51. return 1;
  52. }
  53. __owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,
  54. uint64_t *data)
  55. {
  56. return PACKET_peek_quic_vlint_ex(pkt, data, NULL);
  57. }
  58. /*
  59. * Skips over a QUIC variable-length integer in |pkt| without decoding it.
  60. */
  61. __owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)
  62. {
  63. size_t enclen;
  64. if (PACKET_remaining(pkt) < 1)
  65. return 0;
  66. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  67. if (PACKET_remaining(pkt) < enclen)
  68. return 0;
  69. packet_forward(pkt, enclen);
  70. return 1;
  71. }
  72. /*
  73. * Reads a variable-length vector prefixed with a QUIC variable-length integer
  74. * denoting the length, and stores the contents in |subpkt|. |pkt| can equal
  75. * |subpkt|. Data is not copied: the |subpkt| packet will share its underlying
  76. * buffer with the original |pkt|, so data wrapped by |pkt| must outlive the
  77. * |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.
  78. */
  79. __owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,
  80. PACKET *subpkt)
  81. {
  82. uint64_t length;
  83. const unsigned char *data;
  84. PACKET tmp = *pkt;
  85. if (!PACKET_get_quic_vlint(&tmp, &length) ||
  86. length > SIZE_MAX ||
  87. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  88. return 0;
  89. }
  90. *pkt = tmp;
  91. subpkt->curr = data;
  92. subpkt->remaining = (size_t)length;
  93. return 1;
  94. }
  95. /*
  96. * Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte
  97. * representation is used.
  98. */
  99. __owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);
  100. /*
  101. * Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len
  102. * specifies the upper bound for the sub-packet size at the time the sub-packet
  103. * is closed, which determines the encoding size for the variable-length
  104. * integer header. max_len can be a precise figure or a worst-case bound
  105. * if a precise figure is not available.
  106. */
  107. __owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);
  108. /*
  109. * Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a
  110. * QUIC variable-length integer. The pointer to the payload buffer is output and
  111. * must be filled by the caller. This function assures optimal selection of
  112. * variable-length integer encoding length.
  113. */
  114. __owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,
  115. unsigned char **bytes);
  116. /*
  117. * Write a QUIC variable-length integer to the packet.
  118. */
  119. __owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);
  120. # endif /* OPENSSL_NO_QUIC */
  121. #endif /* OSSL_INTERNAL_PACKET_QUIC_H */