ECKeyParameters.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  10. {
  11. public abstract class ECKeyParameters
  12. : AsymmetricKeyParameter
  13. {
  14. // NB: Use a Dictionary so we can lookup the upper case version
  15. private static readonly Dictionary<string, string> Algorithms =
  16. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  17. {
  18. { "EC", "EC" },
  19. { "ECDSA", "ECDSA" },
  20. { "ECDH", "ECDH" },
  21. { "ECDHC", "ECDHC" },
  22. { "ECGOST3410", "ECGOST3410" },
  23. { "ECMQV", "ECMQV" },
  24. };
  25. private readonly string algorithm;
  26. private readonly ECDomainParameters parameters;
  27. private readonly DerObjectIdentifier publicKeyParamSet;
  28. protected ECKeyParameters(
  29. string algorithm,
  30. bool isPrivate,
  31. ECDomainParameters parameters)
  32. : base(isPrivate)
  33. {
  34. if (algorithm == null)
  35. throw new ArgumentNullException("algorithm");
  36. if (parameters == null)
  37. throw new ArgumentNullException("parameters");
  38. this.algorithm = VerifyAlgorithmName(algorithm);
  39. this.parameters = parameters;
  40. }
  41. protected ECKeyParameters(
  42. string algorithm,
  43. bool isPrivate,
  44. DerObjectIdentifier publicKeyParamSet)
  45. : base(isPrivate)
  46. {
  47. if (algorithm == null)
  48. throw new ArgumentNullException("algorithm");
  49. if (publicKeyParamSet == null)
  50. throw new ArgumentNullException("publicKeyParamSet");
  51. this.algorithm = VerifyAlgorithmName(algorithm);
  52. this.parameters = LookupParameters(publicKeyParamSet);
  53. this.publicKeyParamSet = publicKeyParamSet;
  54. }
  55. public string AlgorithmName
  56. {
  57. get { return algorithm; }
  58. }
  59. public ECDomainParameters Parameters
  60. {
  61. get { return parameters; }
  62. }
  63. public DerObjectIdentifier PublicKeyParamSet
  64. {
  65. get { return publicKeyParamSet; }
  66. }
  67. public override bool Equals(
  68. object obj)
  69. {
  70. if (obj == this)
  71. return true;
  72. ECDomainParameters other = obj as ECDomainParameters;
  73. if (other == null)
  74. return false;
  75. return Equals(other);
  76. }
  77. protected bool Equals(
  78. ECKeyParameters other)
  79. {
  80. return parameters.Equals(other.parameters) && base.Equals(other);
  81. }
  82. public override int GetHashCode()
  83. {
  84. return parameters.GetHashCode() ^ base.GetHashCode();
  85. }
  86. internal ECKeyGenerationParameters CreateKeyGenerationParameters(
  87. SecureRandom random)
  88. {
  89. if (publicKeyParamSet != null)
  90. {
  91. return new ECKeyGenerationParameters(publicKeyParamSet, random);
  92. }
  93. return new ECKeyGenerationParameters(parameters, random);
  94. }
  95. internal static string VerifyAlgorithmName(string algorithm)
  96. {
  97. if (!Algorithms.TryGetValue(algorithm, out var upper))
  98. throw new ArgumentException("unrecognised algorithm: " + algorithm, nameof(algorithm));
  99. return upper;
  100. }
  101. internal static ECDomainParameters LookupParameters(
  102. DerObjectIdentifier publicKeyParamSet)
  103. {
  104. if (publicKeyParamSet == null)
  105. throw new ArgumentNullException("publicKeyParamSet");
  106. X9ECParameters x9 = ECKeyPairGenerator.FindECCurveByOid(publicKeyParamSet);
  107. if (x9 == null)
  108. throw new ArgumentException("OID is not a valid public key parameter set", "publicKeyParamSet");
  109. return new ECDomainParameters(x9);
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif