DtlsProtocol.cs 4.1 KB

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