HandshakeMessageOutput.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. internal sealed class HandshakeMessageOutput
  9. : MemoryStream
  10. {
  11. internal static int GetLength(int bodyLength)
  12. {
  13. return 4 + bodyLength;
  14. }
  15. /// <exception cref="IOException"/>
  16. internal static void Send(TlsProtocol protocol, short handshakeType, byte[] body)
  17. {
  18. HandshakeMessageOutput message = new HandshakeMessageOutput(handshakeType, body.Length);
  19. message.Write(body, 0, body.Length);
  20. message.Send(protocol);
  21. }
  22. /// <exception cref="IOException"/>
  23. internal HandshakeMessageOutput(short handshakeType)
  24. : this(handshakeType, 60)
  25. {
  26. }
  27. /// <exception cref="IOException"/>
  28. internal HandshakeMessageOutput(short handshakeType, int bodyLength)
  29. : base(GetLength(bodyLength))
  30. {
  31. TlsUtilities.CheckUint8(handshakeType);
  32. TlsUtilities.WriteUint8(handshakeType, this);
  33. // Reserve space for length
  34. Seek(3L, SeekOrigin.Current);
  35. }
  36. /// <exception cref="IOException"/>
  37. internal void Send(TlsProtocol protocol)
  38. {
  39. // Patch actual length back in
  40. int bodyLength = (int)Length - 4;
  41. TlsUtilities.CheckUint24(bodyLength);
  42. Seek(1L, SeekOrigin.Begin);
  43. TlsUtilities.WriteUint24(bodyLength, this);
  44. #if PORTABLE || NETFX_CORE
  45. byte[] buf = ToArray();
  46. int count = buf.Length;
  47. #else
  48. byte[] buf = GetBuffer();
  49. int count = (int)Length;
  50. #endif
  51. protocol.WriteHandshakeMessage(buf, 0, count);
  52. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(this);
  53. }
  54. internal void PrepareClientHello(TlsHandshakeHash handshakeHash, int bindersSize)
  55. {
  56. // Patch actual length back in
  57. int bodyLength = (int)Length - 4 + bindersSize;
  58. TlsUtilities.CheckUint24(bodyLength);
  59. Seek(1L, SeekOrigin.Begin);
  60. TlsUtilities.WriteUint24(bodyLength, this);
  61. #if PORTABLE || NETFX_CORE
  62. byte[] buf = ToArray();
  63. int count = buf.Length;
  64. #else
  65. byte[] buf = GetBuffer();
  66. int count = (int)Length;
  67. #endif
  68. handshakeHash.Update(buf, 0, count);
  69. Seek(0L, SeekOrigin.End);
  70. }
  71. internal void SendClientHello(TlsClientProtocol clientProtocol, TlsHandshakeHash handshakeHash, int bindersSize)
  72. {
  73. #if PORTABLE || NETFX_CORE
  74. byte[] buf = ToArray();
  75. int count = buf.Length;
  76. #else
  77. byte[] buf = GetBuffer();
  78. int count = (int)Length;
  79. #endif
  80. if (bindersSize > 0)
  81. {
  82. handshakeHash.Update(buf, count - bindersSize, bindersSize);
  83. }
  84. clientProtocol.WriteHandshakeMessage(buf, 0, count);
  85. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(this);
  86. }
  87. }
  88. }
  89. #pragma warning restore
  90. #endif