TlsNullCipher.cs 4.1 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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  7. {
  8. /// <summary>
  9. /// A NULL CipherSuite, with optional MAC.
  10. /// </summary>
  11. public class TlsNullCipher
  12. : TlsCipher
  13. {
  14. protected readonly TlsContext context;
  15. protected readonly TlsMac writeMac;
  16. protected readonly TlsMac readMac;
  17. public TlsNullCipher(TlsContext context)
  18. {
  19. this.context = context;
  20. this.writeMac = null;
  21. this.readMac = null;
  22. }
  23. /// <exception cref="IOException"></exception>
  24. public TlsNullCipher(TlsContext context, IDigest clientWriteDigest, IDigest serverWriteDigest)
  25. {
  26. if ((clientWriteDigest == null) != (serverWriteDigest == null))
  27. throw new TlsFatalAlert(AlertDescription.internal_error);
  28. this.context = context;
  29. TlsMac clientWriteMac = null, serverWriteMac = null;
  30. if (clientWriteDigest != null)
  31. {
  32. int key_block_size = clientWriteDigest.GetDigestSize()
  33. + serverWriteDigest.GetDigestSize();
  34. byte[] key_block = TlsUtilities.CalculateKeyBlock(context, key_block_size);
  35. int offset = 0;
  36. clientWriteMac = new TlsMac(context, clientWriteDigest, key_block, offset,
  37. clientWriteDigest.GetDigestSize());
  38. offset += clientWriteDigest.GetDigestSize();
  39. serverWriteMac = new TlsMac(context, serverWriteDigest, key_block, offset,
  40. serverWriteDigest.GetDigestSize());
  41. offset += serverWriteDigest.GetDigestSize();
  42. if (offset != key_block_size)
  43. {
  44. throw new TlsFatalAlert(AlertDescription.internal_error);
  45. }
  46. }
  47. if (context.IsServer)
  48. {
  49. writeMac = serverWriteMac;
  50. readMac = clientWriteMac;
  51. }
  52. else
  53. {
  54. writeMac = clientWriteMac;
  55. readMac = serverWriteMac;
  56. }
  57. }
  58. public virtual int GetPlaintextLimit(int ciphertextLimit)
  59. {
  60. int result = ciphertextLimit;
  61. if (writeMac != null)
  62. {
  63. result -= writeMac.Size;
  64. }
  65. return result;
  66. }
  67. /// <exception cref="IOException"></exception>
  68. public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len)
  69. {
  70. if (writeMac == null)
  71. {
  72. return Arrays.CopyOfRange(plaintext, offset, offset + len);
  73. }
  74. byte[] mac = writeMac.CalculateMac(seqNo, type, plaintext, offset, len);
  75. byte[] ciphertext = new byte[len + mac.Length];
  76. Array.Copy(plaintext, offset, ciphertext, 0, len);
  77. Array.Copy(mac, 0, ciphertext, len, mac.Length);
  78. return ciphertext;
  79. }
  80. /// <exception cref="IOException"></exception>
  81. public virtual byte[] DecodeCiphertext(long seqNo, byte type, byte[] ciphertext, int offset, int len)
  82. {
  83. if (readMac == null)
  84. {
  85. return Arrays.CopyOfRange(ciphertext, offset, offset + len);
  86. }
  87. int macSize = readMac.Size;
  88. if (len < macSize)
  89. throw new TlsFatalAlert(AlertDescription.decode_error);
  90. int macInputLen = len - macSize;
  91. byte[] receivedMac = Arrays.CopyOfRange(ciphertext, offset + macInputLen, offset + len);
  92. byte[] computedMac = readMac.CalculateMac(seqNo, type, ciphertext, offset, macInputLen);
  93. if (!Arrays.ConstantTimeAreEqual(receivedMac, computedMac))
  94. throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  95. return Arrays.CopyOfRange(ciphertext, offset, offset + macInputLen);
  96. }
  97. }
  98. }
  99. #pragma warning restore
  100. #endif