DtlsProtocol.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  9. {
  10. public abstract class DtlsProtocol
  11. {
  12. protected readonly SecureRandom mSecureRandom;
  13. protected DtlsProtocol(SecureRandom secureRandom)
  14. {
  15. if (secureRandom == null)
  16. throw new ArgumentNullException("secureRandom");
  17. this.mSecureRandom = secureRandom;
  18. }
  19. /// <exception cref="IOException"/>
  20. protected virtual void ProcessFinished(byte[] body, byte[] expected_verify_data)
  21. {
  22. MemoryStream buf = new MemoryStream(body, false);
  23. byte[] verify_data = TlsUtilities.ReadFully(expected_verify_data.Length, buf);
  24. TlsProtocol.AssertEmpty(buf);
  25. if (!Arrays.ConstantTimeAreEqual(expected_verify_data, verify_data))
  26. throw new TlsFatalAlert(AlertDescription.handshake_failure);
  27. }
  28. /// <exception cref="IOException"/>
  29. internal static void ApplyMaxFragmentLengthExtension(DtlsRecordLayer recordLayer, short maxFragmentLength)
  30. {
  31. if (maxFragmentLength >= 0)
  32. {
  33. if (!MaxFragmentLength.IsValid((byte)maxFragmentLength))
  34. throw new TlsFatalAlert(AlertDescription.internal_error);
  35. int plainTextLimit = 1 << (8 + maxFragmentLength);
  36. recordLayer.SetPlaintextLimit(plainTextLimit);
  37. }
  38. }
  39. /// <exception cref="IOException"/>
  40. protected static short EvaluateMaxFragmentLengthExtension(bool resumedSession, IDictionary clientExtensions,
  41. IDictionary serverExtensions, byte alertDescription)
  42. {
  43. short maxFragmentLength = TlsExtensionsUtilities.GetMaxFragmentLengthExtension(serverExtensions);
  44. if (maxFragmentLength >= 0)
  45. {
  46. if (!MaxFragmentLength.IsValid((byte)maxFragmentLength)
  47. || (!resumedSession && maxFragmentLength != TlsExtensionsUtilities
  48. .GetMaxFragmentLengthExtension(clientExtensions)))
  49. {
  50. throw new TlsFatalAlert(alertDescription);
  51. }
  52. }
  53. return maxFragmentLength;
  54. }
  55. /// <exception cref="IOException"/>
  56. protected static byte[] GenerateCertificate(Certificate certificate)
  57. {
  58. MemoryStream buf = new MemoryStream();
  59. certificate.Encode(buf);
  60. return buf.ToArray();
  61. }
  62. /// <exception cref="IOException"/>
  63. protected static byte[] GenerateSupplementalData(IList supplementalData)
  64. {
  65. MemoryStream buf = new MemoryStream();
  66. TlsProtocol.WriteSupplementalData(buf, supplementalData);
  67. return buf.ToArray();
  68. }
  69. /// <exception cref="IOException"/>
  70. protected static void ValidateSelectedCipherSuite(int selectedCipherSuite, byte alertDescription)
  71. {
  72. switch (TlsUtilities.GetEncryptionAlgorithm(selectedCipherSuite))
  73. {
  74. case EncryptionAlgorithm.RC4_40:
  75. case EncryptionAlgorithm.RC4_128:
  76. throw new TlsFatalAlert(alertDescription);
  77. }
  78. }
  79. }
  80. }
  81. #pragma warning restore
  82. #endif