TlsStreamCipher.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. using Org.BouncyCastle.Crypto.Parameters;
  5. using Org.BouncyCastle.Crypto.Tls;
  6. using Org.BouncyCastle.Utilities;
  7. namespace Org.BouncyCastle.Crypto.Tls
  8. {
  9. public class TlsStreamCipher
  10. : TlsCipher
  11. {
  12. protected readonly TlsContext context;
  13. protected readonly IStreamCipher encryptCipher;
  14. protected readonly IStreamCipher decryptCipher;
  15. protected readonly TlsMac writeMac;
  16. protected readonly TlsMac readMac;
  17. protected readonly bool usesNonce;
  18. /// <exception cref="IOException"></exception>
  19. public TlsStreamCipher(TlsContext context, IStreamCipher clientWriteCipher,
  20. IStreamCipher serverWriteCipher, IDigest clientWriteDigest, IDigest serverWriteDigest,
  21. int cipherKeySize, bool usesNonce)
  22. {
  23. bool isServer = context.IsServer;
  24. this.context = context;
  25. this.usesNonce = usesNonce;
  26. this.encryptCipher = clientWriteCipher;
  27. this.decryptCipher = serverWriteCipher;
  28. int key_block_size = (2 * cipherKeySize) + clientWriteDigest.GetDigestSize()
  29. + serverWriteDigest.GetDigestSize();
  30. byte[] key_block = TlsUtilities.CalculateKeyBlock(context, key_block_size);
  31. int offset = 0;
  32. // Init MACs
  33. TlsMac clientWriteMac = new TlsMac(context, clientWriteDigest, key_block, offset,
  34. clientWriteDigest.GetDigestSize());
  35. offset += clientWriteDigest.GetDigestSize();
  36. TlsMac serverWriteMac = new TlsMac(context, serverWriteDigest, key_block, offset,
  37. serverWriteDigest.GetDigestSize());
  38. offset += serverWriteDigest.GetDigestSize();
  39. // Build keys
  40. KeyParameter clientWriteKey = new KeyParameter(key_block, offset, cipherKeySize);
  41. offset += cipherKeySize;
  42. KeyParameter serverWriteKey = new KeyParameter(key_block, offset, cipherKeySize);
  43. offset += cipherKeySize;
  44. if (offset != key_block_size)
  45. {
  46. throw new TlsFatalAlert(AlertDescription.internal_error);
  47. }
  48. ICipherParameters encryptParams, decryptParams;
  49. if (isServer)
  50. {
  51. this.writeMac = serverWriteMac;
  52. this.readMac = clientWriteMac;
  53. this.encryptCipher = serverWriteCipher;
  54. this.decryptCipher = clientWriteCipher;
  55. encryptParams = serverWriteKey;
  56. decryptParams = clientWriteKey;
  57. }
  58. else
  59. {
  60. this.writeMac = clientWriteMac;
  61. this.readMac = serverWriteMac;
  62. this.encryptCipher = clientWriteCipher;
  63. this.decryptCipher = serverWriteCipher;
  64. encryptParams = clientWriteKey;
  65. decryptParams = serverWriteKey;
  66. }
  67. if (usesNonce)
  68. {
  69. byte[] dummyNonce = new byte[8];
  70. encryptParams = new ParametersWithIV(encryptParams, dummyNonce);
  71. decryptParams = new ParametersWithIV(decryptParams, dummyNonce);
  72. }
  73. this.encryptCipher.Init(true, encryptParams);
  74. this.decryptCipher.Init(false, decryptParams);
  75. }
  76. public virtual int GetPlaintextLimit(int ciphertextLimit)
  77. {
  78. return ciphertextLimit - writeMac.Size;
  79. }
  80. public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len)
  81. {
  82. if (usesNonce)
  83. {
  84. UpdateIV(encryptCipher, true, seqNo);
  85. }
  86. byte[] outBuf = new byte[len + writeMac.Size];
  87. encryptCipher.ProcessBytes(plaintext, offset, len, outBuf, 0);
  88. byte[] mac = writeMac.CalculateMac(seqNo, type, plaintext, offset, len);
  89. encryptCipher.ProcessBytes(mac, 0, mac.Length, outBuf, len);
  90. return outBuf;
  91. }
  92. /// <exception cref="IOException"></exception>
  93. public virtual byte[] DecodeCiphertext(long seqNo, byte type, byte[] ciphertext, int offset, int len)
  94. {
  95. if (usesNonce)
  96. {
  97. UpdateIV(decryptCipher, false, seqNo);
  98. }
  99. int macSize = readMac.Size;
  100. if (len < macSize)
  101. throw new TlsFatalAlert(AlertDescription.decode_error);
  102. int plaintextLength = len - macSize;
  103. byte[] deciphered = new byte[len];
  104. decryptCipher.ProcessBytes(ciphertext, offset, len, deciphered, 0);
  105. CheckMac(seqNo, type, deciphered, plaintextLength, len, deciphered, 0, plaintextLength);
  106. return Arrays.CopyOfRange(deciphered, 0, plaintextLength);
  107. }
  108. /// <exception cref="IOException"></exception>
  109. protected virtual void CheckMac(long seqNo, byte type, byte[] recBuf, int recStart, int recEnd, byte[] calcBuf, int calcOff, int calcLen)
  110. {
  111. byte[] receivedMac = Arrays.CopyOfRange(recBuf, recStart, recEnd);
  112. byte[] computedMac = readMac.CalculateMac(seqNo, type, calcBuf, calcOff, calcLen);
  113. if (!Arrays.ConstantTimeAreEqual(receivedMac, computedMac))
  114. throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  115. }
  116. protected virtual void UpdateIV(IStreamCipher cipher, bool forEncryption, long seqNo)
  117. {
  118. byte[] nonce = new byte[8];
  119. TlsUtilities.WriteUint64(seqNo, nonce, 0);
  120. cipher.Init(forEncryption, new ParametersWithIV(null, nonce));
  121. }
  122. }
  123. }
  124. #endif