TlsNullCipher.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.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. this.m_cryptoParams = cryptoParams;
  19. int key_block_size = clientMac.MacLength + serverMac.MacLength;
  20. byte[] key_block = TlsImplUtilities.CalculateKeyBlock(cryptoParams, key_block_size);
  21. int offset = 0;
  22. clientMac.SetKey(key_block, offset, clientMac.MacLength);
  23. offset += clientMac.MacLength;
  24. serverMac.SetKey(key_block, offset, serverMac.MacLength);
  25. offset += serverMac.MacLength;
  26. if (offset != key_block_size)
  27. throw new TlsFatalAlert(AlertDescription.internal_error);
  28. if (cryptoParams.IsServer)
  29. {
  30. this.m_writeMac = new TlsSuiteHmac(cryptoParams, serverMac);
  31. this.m_readMac = new TlsSuiteHmac(cryptoParams, clientMac);
  32. }
  33. else
  34. {
  35. this.m_writeMac = new TlsSuiteHmac(cryptoParams, clientMac);
  36. this.m_readMac = new TlsSuiteHmac(cryptoParams, serverMac);
  37. }
  38. }
  39. public virtual int GetCiphertextDecodeLimit(int plaintextLimit)
  40. {
  41. return plaintextLimit + m_writeMac.Size;
  42. }
  43. public virtual int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
  44. {
  45. return plaintextLength + m_writeMac.Size;
  46. }
  47. public virtual int GetPlaintextLimit(int ciphertextLimit)
  48. {
  49. return ciphertextLimit - m_writeMac.Size;
  50. }
  51. public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
  52. int headerAllocation, byte[] plaintext, int offset, int len)
  53. {
  54. byte[] mac = m_writeMac.CalculateMac(seqNo, contentType, plaintext, offset, len);
  55. byte[] ciphertext = new byte[headerAllocation + len + mac.Length];
  56. Array.Copy(plaintext, offset, ciphertext, headerAllocation, len);
  57. Array.Copy(mac, 0, ciphertext, headerAllocation + len, mac.Length);
  58. return new TlsEncodeResult(ciphertext, 0, ciphertext.Length, contentType);
  59. }
  60. public virtual TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
  61. byte[] ciphertext, int offset, int len)
  62. {
  63. int macSize = m_readMac.Size;
  64. if (len < macSize)
  65. throw new TlsFatalAlert(AlertDescription.decode_error);
  66. int macInputLen = len - macSize;
  67. byte[] expectedMac = m_readMac.CalculateMac(seqNo, recordType, ciphertext, offset, macInputLen);
  68. bool badMac = !TlsUtilities.ConstantTimeAreEqual(macSize, expectedMac, 0, ciphertext, offset + macInputLen);
  69. if (badMac)
  70. throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  71. return new TlsDecodeResult(ciphertext, offset, macInputLen, recordType);
  72. }
  73. public virtual void RekeyDecoder()
  74. {
  75. throw new TlsFatalAlert(AlertDescription.internal_error);
  76. }
  77. public virtual void RekeyEncoder()
  78. {
  79. throw new TlsFatalAlert(AlertDescription.internal_error);
  80. }
  81. public virtual bool UsesOpaqueRecordType
  82. {
  83. get { return false; }
  84. }
  85. }
  86. }
  87. #pragma warning restore
  88. #endif