DtlsProtocol.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Utilities;
  7. namespace BestHTTP.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, IDictionary clientExtensions,
  36. IDictionary serverExtensions, short alertDescription)
  37. {
  38. short maxFragmentLength = TlsExtensionsUtilities.GetMaxFragmentLengthExtension(serverExtensions);
  39. if (maxFragmentLength >= 0)
  40. {
  41. if (!MaxFragmentLength.IsValid(maxFragmentLength)
  42. || (!resumedSession && maxFragmentLength != TlsExtensionsUtilities
  43. .GetMaxFragmentLengthExtension(clientExtensions)))
  44. {
  45. throw new TlsFatalAlert(alertDescription);
  46. }
  47. }
  48. return maxFragmentLength;
  49. }
  50. /// <exception cref="IOException"/>
  51. internal static byte[] GenerateCertificate(TlsContext context, Certificate certificate, Stream endPointHash)
  52. {
  53. MemoryStream buf = new MemoryStream();
  54. certificate.Encode(context, buf, endPointHash);
  55. return buf.ToArray();
  56. }
  57. /// <exception cref="IOException"/>
  58. internal static byte[] GenerateSupplementalData(IList supplementalData)
  59. {
  60. MemoryStream buf = new MemoryStream();
  61. TlsProtocol.WriteSupplementalData(buf, supplementalData);
  62. return buf.ToArray();
  63. }
  64. /// <exception cref="IOException"/>
  65. internal static void SendCertificateMessage(TlsContext context, DtlsReliableHandshake handshake,
  66. Certificate certificate, Stream endPointHash)
  67. {
  68. SecurityParameters securityParameters = context.SecurityParameters;
  69. if (null != securityParameters.LocalCertificate)
  70. throw new TlsFatalAlert(AlertDescription.internal_error);
  71. if (null == certificate)
  72. {
  73. certificate = Certificate.EmptyChain;
  74. }
  75. byte[] certificateBody = GenerateCertificate(context, certificate, endPointHash);
  76. handshake.SendMessage(HandshakeType.certificate, certificateBody);
  77. securityParameters.m_localCertificate = certificate;
  78. }
  79. /// <exception cref="IOException"/>
  80. internal static int ValidateSelectedCipherSuite(int selectedCipherSuite, short alertDescription)
  81. {
  82. switch (TlsUtilities.GetEncryptionAlgorithm(selectedCipherSuite))
  83. {
  84. case EncryptionAlgorithm.RC4_40:
  85. case EncryptionAlgorithm.RC4_128:
  86. case -1:
  87. throw new TlsFatalAlert(alertDescription);
  88. default:
  89. return selectedCipherSuite;
  90. }
  91. }
  92. }
  93. }
  94. #pragma warning restore
  95. #endif