TlsNullCipher.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl
  6. {
  7. /// <summary>The NULL cipher.</summary>
  8. public class TlsNullCipher
  9. : TlsCipher
  10. {
  11. protected readonly TlsCryptoParameters m_cryptoParams;
  12. protected readonly TlsSuiteHmac m_readMac, m_writeMac;
  13. /// <exception cref="IOException"/>
  14. public TlsNullCipher(TlsCryptoParameters cryptoParams, TlsHmac clientMac, TlsHmac serverMac)
  15. {
  16. if (TlsImplUtilities.IsTlsV13(cryptoParams))
  17. throw new TlsFatalAlert(AlertDescription.internal_error);
  18. m_cryptoParams = cryptoParams;
  19. int keyBlockSize = clientMac.MacLength + serverMac.MacLength;
  20. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  21. Span<byte> keyBlock = keyBlockSize <= 512
  22. ? stackalloc byte[keyBlockSize]
  23. : new byte[keyBlockSize];
  24. TlsImplUtilities.CalculateKeyBlock(cryptoParams, keyBlock);
  25. clientMac.SetKey(keyBlock[..clientMac.MacLength]); keyBlock = keyBlock[clientMac.MacLength..];
  26. serverMac.SetKey(keyBlock[..serverMac.MacLength]); keyBlock = keyBlock[serverMac.MacLength..];
  27. if (!keyBlock.IsEmpty)
  28. throw new TlsFatalAlert(AlertDescription.internal_error);
  29. #else
  30. byte[] keyBlock = TlsImplUtilities.CalculateKeyBlock(cryptoParams, keyBlockSize);
  31. int pos = 0;
  32. clientMac.SetKey(keyBlock, pos, clientMac.MacLength);
  33. pos += clientMac.MacLength;
  34. serverMac.SetKey(keyBlock, pos, serverMac.MacLength);
  35. pos += serverMac.MacLength;
  36. if (pos != keyBlockSize)
  37. throw new TlsFatalAlert(AlertDescription.internal_error);
  38. #endif
  39. if (cryptoParams.IsServer)
  40. {
  41. this.m_writeMac = new TlsSuiteHmac(cryptoParams, serverMac);
  42. this.m_readMac = new TlsSuiteHmac(cryptoParams, clientMac);
  43. }
  44. else
  45. {
  46. this.m_writeMac = new TlsSuiteHmac(cryptoParams, clientMac);
  47. this.m_readMac = new TlsSuiteHmac(cryptoParams, serverMac);
  48. }
  49. }
  50. public virtual int GetCiphertextDecodeLimit(int plaintextLimit)
  51. {
  52. return plaintextLimit + m_writeMac.Size;
  53. }
  54. public virtual int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
  55. {
  56. return plaintextLength + m_writeMac.Size;
  57. }
  58. public virtual int GetPlaintextLimit(int ciphertextLimit)
  59. {
  60. return ciphertextLimit - m_writeMac.Size;
  61. }
  62. public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
  63. int headerAllocation, byte[] plaintext, int offset, int len)
  64. {
  65. byte[] mac = m_writeMac.CalculateMac(seqNo, contentType, plaintext, offset, len);
  66. byte[] ciphertext = new byte[headerAllocation + len + mac.Length];
  67. Array.Copy(plaintext, offset, ciphertext, headerAllocation, len);
  68. Array.Copy(mac, 0, ciphertext, headerAllocation + len, mac.Length);
  69. return new TlsEncodeResult(ciphertext, 0, ciphertext.Length, contentType);
  70. }
  71. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  72. public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
  73. int headerAllocation, ReadOnlySpan<byte> plaintext)
  74. {
  75. byte[] mac = m_writeMac.CalculateMac(seqNo, contentType, plaintext);
  76. byte[] ciphertext = new byte[headerAllocation + plaintext.Length + mac.Length];
  77. plaintext.CopyTo(ciphertext.AsSpan(headerAllocation));
  78. mac.CopyTo(ciphertext.AsSpan(headerAllocation + plaintext.Length));
  79. return new TlsEncodeResult(ciphertext, 0, ciphertext.Length, contentType);
  80. }
  81. #endif
  82. public virtual TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
  83. byte[] ciphertext, int offset, int len)
  84. {
  85. int macSize = m_readMac.Size;
  86. if (len < macSize)
  87. throw new TlsFatalAlert(AlertDescription.decode_error);
  88. int macInputLen = len - macSize;
  89. byte[] expectedMac = m_readMac.CalculateMac(seqNo, recordType, ciphertext, offset, macInputLen);
  90. bool badMac = !TlsUtilities.ConstantTimeAreEqual(macSize, expectedMac, 0, ciphertext, offset + macInputLen);
  91. if (badMac)
  92. throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  93. return new TlsDecodeResult(ciphertext, offset, macInputLen, recordType);
  94. }
  95. public virtual void RekeyDecoder()
  96. {
  97. throw new TlsFatalAlert(AlertDescription.internal_error);
  98. }
  99. public virtual void RekeyEncoder()
  100. {
  101. throw new TlsFatalAlert(AlertDescription.internal_error);
  102. }
  103. public virtual bool UsesOpaqueRecordType
  104. {
  105. get { return false; }
  106. }
  107. }
  108. }
  109. #pragma warning restore
  110. #endif