Ssl3Utilities.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Tls.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  9. {
  10. internal abstract class Ssl3Utilities
  11. {
  12. private static readonly byte[] SSL_CLIENT = {0x43, 0x4C, 0x4E, 0x54};
  13. private static readonly byte[] SSL_SERVER = {0x53, 0x52, 0x56, 0x52};
  14. private const byte IPAD_BYTE = (byte)0x36;
  15. private const byte OPAD_BYTE = (byte)0x5C;
  16. private static readonly byte[] IPAD = GenPad(IPAD_BYTE, 48);
  17. private static readonly byte[] OPAD = GenPad(OPAD_BYTE, 48);
  18. internal static byte[] CalculateVerifyData(TlsHandshakeHash handshakeHash, bool isServer)
  19. {
  20. TlsHash prf = handshakeHash.ForkPrfHash();
  21. byte[] sslSender = isServer ? SSL_SERVER : SSL_CLIENT;
  22. prf.Update(sslSender, 0, sslSender.Length);
  23. return prf.CalculateHash();
  24. }
  25. internal static void CompleteCombinedHash(TlsContext context, TlsHash md5, TlsHash sha1)
  26. {
  27. TlsSecret masterSecret = context.SecurityParameters.MasterSecret;
  28. byte[] master_secret = context.Crypto.AdoptSecret(masterSecret).Extract();
  29. CompleteHash(master_secret, md5, 48);
  30. CompleteHash(master_secret, sha1, 40);
  31. }
  32. private static void CompleteHash(byte[] master_secret, TlsHash hash, int padLength)
  33. {
  34. hash.Update(master_secret, 0, master_secret.Length);
  35. hash.Update(IPAD, 0, padLength);
  36. byte[] tmp = hash.CalculateHash();
  37. hash.Update(master_secret, 0, master_secret.Length);
  38. hash.Update(OPAD, 0, padLength);
  39. hash.Update(tmp, 0, tmp.Length);
  40. }
  41. private static byte[] GenPad(byte b, int count)
  42. {
  43. byte[] padding = new byte[count];
  44. Arrays.Fill(padding, b);
  45. return padding;
  46. }
  47. /// <exception cref="IOException"/>
  48. internal static byte[] ReadEncryptedPms(Stream input)
  49. {
  50. return Streams.ReadAll(input);
  51. }
  52. /// <exception cref="IOException"/>
  53. internal static void WriteEncryptedPms(byte[] encryptedPms, Stream output)
  54. {
  55. output.Write(encryptedPms, 0, encryptedPms.Length);
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif