AgreementUtilities.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.Collections;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Security
  13. {
  14. /// <remarks>
  15. /// Utility class for creating IBasicAgreement objects from their names/Oids
  16. /// </remarks>
  17. public sealed class AgreementUtilities
  18. {
  19. private AgreementUtilities()
  20. {
  21. }
  22. private static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  23. //private static readonly IDictionary oids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  24. static AgreementUtilities()
  25. {
  26. algorithms[X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme.Id] = "ECCDHWITHSHA1KDF";
  27. algorithms[X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme.Id] = "ECDHWITHSHA1KDF";
  28. algorithms[X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme.Id] = "ECMQVWITHSHA1KDF";
  29. algorithms[EdECObjectIdentifiers.id_X25519.Id] = "X25519";
  30. algorithms[EdECObjectIdentifiers.id_X448.Id] = "X448";
  31. }
  32. public static IBasicAgreement GetBasicAgreement(
  33. DerObjectIdentifier oid)
  34. {
  35. return GetBasicAgreement(oid.Id);
  36. }
  37. public static IBasicAgreement GetBasicAgreement(
  38. string algorithm)
  39. {
  40. string mechanism = GetMechanism(algorithm);
  41. if (mechanism == "DH" || mechanism == "DIFFIEHELLMAN")
  42. return new DHBasicAgreement();
  43. if (mechanism == "ECDH")
  44. return new ECDHBasicAgreement();
  45. if (mechanism == "ECDHC" || mechanism == "ECCDH")
  46. return new ECDHCBasicAgreement();
  47. if (mechanism == "ECMQV")
  48. return new ECMqvBasicAgreement();
  49. throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised.");
  50. }
  51. public static IBasicAgreement GetBasicAgreementWithKdf(
  52. DerObjectIdentifier oid,
  53. string wrapAlgorithm)
  54. {
  55. return GetBasicAgreementWithKdf(oid.Id, wrapAlgorithm);
  56. }
  57. public static IBasicAgreement GetBasicAgreementWithKdf(
  58. string agreeAlgorithm,
  59. string wrapAlgorithm)
  60. {
  61. string mechanism = GetMechanism(agreeAlgorithm);
  62. // 'DHWITHSHA1KDF' retained for backward compatibility
  63. if (mechanism == "DHWITHSHA1KDF" || mechanism == "ECDHWITHSHA1KDF")
  64. return new ECDHWithKdfBasicAgreement(
  65. wrapAlgorithm,
  66. new ECDHKekGenerator(
  67. new Sha1Digest()));
  68. if (mechanism == "ECMQVWITHSHA1KDF")
  69. return new ECMqvWithKdfBasicAgreement(
  70. wrapAlgorithm,
  71. new ECDHKekGenerator(
  72. new Sha1Digest()));
  73. throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised.");
  74. }
  75. public static IRawAgreement GetRawAgreement(
  76. DerObjectIdentifier oid)
  77. {
  78. return GetRawAgreement(oid.Id);
  79. }
  80. public static IRawAgreement GetRawAgreement(
  81. string algorithm)
  82. {
  83. string mechanism = GetMechanism(algorithm);
  84. if (mechanism == "X25519")
  85. return new X25519Agreement();
  86. if (mechanism == "X448")
  87. return new X448Agreement();
  88. throw new SecurityUtilityException("Raw Agreement " + algorithm + " not recognised.");
  89. }
  90. public static string GetAlgorithmName(
  91. DerObjectIdentifier oid)
  92. {
  93. return (string)algorithms[oid.Id];
  94. }
  95. private static string GetMechanism(string algorithm)
  96. {
  97. string upper = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  98. string mechanism = (string)algorithms[upper];
  99. return mechanism == null ? upper : mechanism;
  100. }
  101. }
  102. }
  103. #pragma warning restore
  104. #endif