TlsNullCipher.cs 4.0 KB

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