HeartbeatMessage.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  8. {
  9. public sealed class HeartbeatMessage
  10. {
  11. public static HeartbeatMessage Create(TlsContext context, short type, byte[] payload)
  12. {
  13. return Create(context, type, payload, 16);
  14. }
  15. public static HeartbeatMessage Create(TlsContext context, short type, byte[] payload, int paddingLength)
  16. {
  17. byte[] padding = context.NonceGenerator.GenerateNonce(paddingLength);
  18. return new HeartbeatMessage(type, payload, padding);
  19. }
  20. private readonly short m_type;
  21. private readonly byte[] m_payload;
  22. private readonly byte[] m_padding;
  23. public HeartbeatMessage(short type, byte[] payload, byte[] padding)
  24. {
  25. if (!HeartbeatMessageType.IsValid(type))
  26. throw new ArgumentException("not a valid HeartbeatMessageType value", "type");
  27. if (null == payload || payload.Length >= (1 << 16))
  28. throw new ArgumentException("must have length < 2^16", "payload");
  29. if (null == padding || padding.Length < 16)
  30. throw new ArgumentException("must have length >= 16", "padding");
  31. this.m_type = type;
  32. this.m_payload = payload;
  33. this.m_padding = padding;
  34. }
  35. public int PaddingLength
  36. {
  37. /*
  38. * RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
  39. */
  40. get { return m_padding.Length; }
  41. }
  42. public byte[] Payload
  43. {
  44. get { return m_payload; }
  45. }
  46. public short Type
  47. {
  48. get { return m_type; }
  49. }
  50. /// <summary>Encode this <see cref="HeartbeatMessage"/> to a <see cref="Stream"/>.</summary>
  51. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  52. /// <exception cref="IOException"/>
  53. public void Encode(Stream output)
  54. {
  55. TlsUtilities.WriteUint8(m_type, output);
  56. TlsUtilities.CheckUint16(m_payload.Length);
  57. TlsUtilities.WriteUint16(m_payload.Length, output);
  58. output.Write(m_payload, 0, m_payload.Length);
  59. output.Write(m_padding, 0, m_padding.Length);
  60. }
  61. /// <summary>Parse a <see cref="HeartbeatMessage"/> from a <see cref="Stream"/>.</summary>
  62. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  63. /// <returns>a <see cref="HeartbeatMessage"/> object.</returns>
  64. /// <exception cref="IOException"/>
  65. public static HeartbeatMessage Parse(Stream input)
  66. {
  67. short type = TlsUtilities.ReadUint8(input);
  68. if (!HeartbeatMessageType.IsValid(type))
  69. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  70. int payload_length = TlsUtilities.ReadUint16(input);
  71. byte[] payloadBuffer = Streams.ReadAll(input);
  72. byte[] payload = GetPayload(payloadBuffer, payload_length);
  73. if (null == payload)
  74. {
  75. /*
  76. * RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the received
  77. * HeartbeatMessage MUST be discarded silently.
  78. */
  79. return null;
  80. }
  81. byte[] padding = GetPadding(payloadBuffer, payload_length);
  82. return new HeartbeatMessage(type, payload, padding);
  83. }
  84. private static byte[] GetPayload(byte[] payloadBuffer, int payloadLength)
  85. {
  86. /*
  87. * RFC 6520 4. The padding_length MUST be at least 16.
  88. */
  89. int maxPayloadLength = payloadBuffer.Length - 16;
  90. if (payloadLength > maxPayloadLength)
  91. return null;
  92. return Arrays.CopyOf(payloadBuffer, payloadLength);
  93. }
  94. private static byte[] GetPadding(byte[] payloadBuffer, int payloadLength)
  95. {
  96. return TlsUtilities.CopyOfRangeExact(payloadBuffer, payloadLength, payloadBuffer.Length);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif