NISTNamedCurves.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist
  11. {
  12. /**
  13. * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-3
  14. */
  15. public sealed class NistNamedCurves
  16. {
  17. private NistNamedCurves()
  18. {
  19. }
  20. private static readonly IDictionary objIds = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  21. private static readonly IDictionary names = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  22. private static void DefineCurveAlias(
  23. string name,
  24. DerObjectIdentifier oid)
  25. {
  26. objIds.Add(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name), oid);
  27. names.Add(oid, name);
  28. }
  29. static NistNamedCurves()
  30. {
  31. DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2);
  32. DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1);
  33. DefineCurveAlias("B-283", SecObjectIdentifiers.SecT283r1);
  34. DefineCurveAlias("B-409", SecObjectIdentifiers.SecT409r1);
  35. DefineCurveAlias("B-571", SecObjectIdentifiers.SecT571r1);
  36. DefineCurveAlias("K-163", SecObjectIdentifiers.SecT163k1);
  37. DefineCurveAlias("K-233", SecObjectIdentifiers.SecT233k1);
  38. DefineCurveAlias("K-283", SecObjectIdentifiers.SecT283k1);
  39. DefineCurveAlias("K-409", SecObjectIdentifiers.SecT409k1);
  40. DefineCurveAlias("K-571", SecObjectIdentifiers.SecT571k1);
  41. DefineCurveAlias("P-192", SecObjectIdentifiers.SecP192r1);
  42. DefineCurveAlias("P-224", SecObjectIdentifiers.SecP224r1);
  43. DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1);
  44. DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1);
  45. DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);
  46. }
  47. public static X9ECParameters GetByName(
  48. string name)
  49. {
  50. DerObjectIdentifier oid = GetOid(name);
  51. return oid == null ? null : GetByOid(oid);
  52. }
  53. /**
  54. * return the X9ECParameters object for the named curve represented by
  55. * the passed in object identifier. Null if the curve isn't present.
  56. *
  57. * @param oid an object identifier representing a named curve, if present.
  58. */
  59. public static X9ECParameters GetByOid(
  60. DerObjectIdentifier oid)
  61. {
  62. return SecNamedCurves.GetByOid(oid);
  63. }
  64. /**
  65. * return the object identifier signified by the passed in name. Null
  66. * if there is no object identifier associated with name.
  67. *
  68. * @return the object identifier associated with name, if present.
  69. */
  70. public static DerObjectIdentifier GetOid(
  71. string name)
  72. {
  73. return (DerObjectIdentifier) objIds[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  74. }
  75. /**
  76. * return the named curve name represented by the given object identifier.
  77. */
  78. public static string GetName(
  79. DerObjectIdentifier oid)
  80. {
  81. return (string) names[oid];
  82. }
  83. /**
  84. * returns an enumeration containing the name strings for curves
  85. * contained in this structure.
  86. */
  87. public static IEnumerable Names
  88. {
  89. get { return new EnumerableProxy(names.Values); }
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif