AgreementUtilities.cs 3.9 KB

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