TlsEccUtilities.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  8. {
  9. public abstract class TlsEccUtilities
  10. {
  11. /// <exception cref="IOException"/>
  12. public static TlsECConfig CreateNamedECConfig(TlsContext context, int namedGroup)
  13. {
  14. if (NamedGroup.GetCurveBits(namedGroup) < 1)
  15. throw new TlsFatalAlert(AlertDescription.internal_error);
  16. return new TlsECConfig(namedGroup);
  17. }
  18. public static int GetMinimumCurveBits(int cipherSuite)
  19. {
  20. /*
  21. * NOTE: This mechanism was added to support a minimum bit-size requirement mooted in early
  22. * drafts of RFC 8442. This requirement was removed in later drafts, so this mechanism is
  23. * currently somewhat trivial.
  24. */
  25. return IsEccCipherSuite(cipherSuite) ? 1 : 0;
  26. }
  27. public static bool IsEccCipherSuite(int cipherSuite)
  28. {
  29. switch (TlsUtilities.GetKeyExchangeAlgorithm(cipherSuite))
  30. {
  31. case KeyExchangeAlgorithm.ECDH_anon:
  32. case KeyExchangeAlgorithm.ECDH_ECDSA:
  33. case KeyExchangeAlgorithm.ECDH_RSA:
  34. case KeyExchangeAlgorithm.ECDHE_ECDSA:
  35. case KeyExchangeAlgorithm.ECDHE_PSK:
  36. case KeyExchangeAlgorithm.ECDHE_RSA:
  37. return true;
  38. default:
  39. return false;
  40. }
  41. }
  42. /// <exception cref="IOException"/>
  43. public static void CheckPointEncoding(int namedGroup, byte[] encoding)
  44. {
  45. if (TlsUtilities.IsNullOrEmpty(encoding))
  46. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  47. switch (namedGroup)
  48. {
  49. case NamedGroup.x25519:
  50. case NamedGroup.x448:
  51. return;
  52. }
  53. switch (encoding[0])
  54. {
  55. case 0x04: // uncompressed
  56. return;
  57. case 0x00: // infinity
  58. case 0x02: // compressed
  59. case 0x03: // compressed
  60. case 0x06: // hybrid
  61. case 0x07: // hybrid
  62. default:
  63. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  64. }
  65. }
  66. /// <exception cref="IOException"/>
  67. public static TlsECConfig ReceiveECDHConfig(TlsContext context, Stream input)
  68. {
  69. short curveType = TlsUtilities.ReadUint8(input);
  70. if (curveType != ECCurveType.named_curve)
  71. throw new TlsFatalAlert(AlertDescription.handshake_failure);
  72. int namedGroup = TlsUtilities.ReadUint16(input);
  73. if (NamedGroup.RefersToAnECDHCurve(namedGroup))
  74. {
  75. int[] clientSupportedGroups = context.SecurityParameters.ClientSupportedGroups;
  76. if (null == clientSupportedGroups || Arrays.Contains(clientSupportedGroups, namedGroup))
  77. return new TlsECConfig(namedGroup);
  78. }
  79. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  80. }
  81. /// <exception cref="IOException"/>
  82. public static void WriteECConfig(TlsECConfig ecConfig, Stream output)
  83. {
  84. WriteNamedECParameters(ecConfig.NamedGroup, output);
  85. }
  86. /// <exception cref="IOException"/>
  87. public static void WriteNamedECParameters(int namedGroup, Stream output)
  88. {
  89. if (!NamedGroup.RefersToASpecificCurve(namedGroup))
  90. {
  91. /*
  92. * RFC 4492 5.4. All those values of NamedCurve are allowed that refer to a specific
  93. * curve. Values of NamedCurve that indicate support for a class of explicitly defined
  94. * curves are not allowed here [...].
  95. */
  96. throw new TlsFatalAlert(AlertDescription.internal_error);
  97. }
  98. TlsUtilities.WriteUint8(ECCurveType.named_curve, output);
  99. TlsUtilities.CheckUint16(namedGroup);
  100. TlsUtilities.WriteUint16(namedGroup, output);
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif