TlsStreamCipher.cs 5.8 KB

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