ECNamedCurveTable.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Anssi;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  15. {
  16. /**
  17. * A general class that reads all X9.62 style EC curve tables.
  18. */
  19. public class ECNamedCurveTable
  20. {
  21. /**
  22. * return a X9ECParameters object representing the passed in named
  23. * curve. The routine returns null if the curve is not present.
  24. *
  25. * @param name the name of the curve requested
  26. * @return an X9ECParameters object or null if the curve is not available.
  27. */
  28. public static X9ECParameters GetByName(string name)
  29. {
  30. X9ECParameters ecP = X962NamedCurves.GetByName(name);
  31. if (ecP == null)
  32. {
  33. ecP = SecNamedCurves.GetByName(name);
  34. }
  35. if (ecP == null)
  36. {
  37. ecP = NistNamedCurves.GetByName(name);
  38. }
  39. if (ecP == null)
  40. {
  41. ecP = TeleTrusTNamedCurves.GetByName(name);
  42. }
  43. if (ecP == null)
  44. {
  45. ecP = AnssiNamedCurves.GetByName(name);
  46. }
  47. if (ecP == null)
  48. {
  49. ecP = FromDomainParameters(ECGost3410NamedCurves.GetByName(name));
  50. }
  51. if (ecP == null)
  52. {
  53. ecP = GMNamedCurves.GetByName(name);
  54. }
  55. return ecP;
  56. }
  57. public static string GetName(DerObjectIdentifier oid)
  58. {
  59. string name = X962NamedCurves.GetName(oid);
  60. if (name == null)
  61. {
  62. name = SecNamedCurves.GetName(oid);
  63. }
  64. if (name == null)
  65. {
  66. name = NistNamedCurves.GetName(oid);
  67. }
  68. if (name == null)
  69. {
  70. name = TeleTrusTNamedCurves.GetName(oid);
  71. }
  72. if (name == null)
  73. {
  74. name = AnssiNamedCurves.GetName(oid);
  75. }
  76. if (name == null)
  77. {
  78. name = ECGost3410NamedCurves.GetName(oid);
  79. }
  80. if (name == null)
  81. {
  82. name = GMNamedCurves.GetName(oid);
  83. }
  84. return name;
  85. }
  86. /**
  87. * return the object identifier signified by the passed in name. Null
  88. * if there is no object identifier associated with name.
  89. *
  90. * @return the object identifier associated with name, if present.
  91. */
  92. public static DerObjectIdentifier GetOid(string name)
  93. {
  94. DerObjectIdentifier oid = X962NamedCurves.GetOid(name);
  95. if (oid == null)
  96. {
  97. oid = SecNamedCurves.GetOid(name);
  98. }
  99. if (oid == null)
  100. {
  101. oid = NistNamedCurves.GetOid(name);
  102. }
  103. if (oid == null)
  104. {
  105. oid = TeleTrusTNamedCurves.GetOid(name);
  106. }
  107. if (oid == null)
  108. {
  109. oid = AnssiNamedCurves.GetOid(name);
  110. }
  111. if (oid == null)
  112. {
  113. oid = ECGost3410NamedCurves.GetOid(name);
  114. }
  115. if (oid == null)
  116. {
  117. oid = GMNamedCurves.GetOid(name);
  118. }
  119. return oid;
  120. }
  121. /**
  122. * return a X9ECParameters object representing the passed in named
  123. * curve.
  124. *
  125. * @param oid the object id of the curve requested
  126. * @return an X9ECParameters object or null if the curve is not available.
  127. */
  128. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  129. {
  130. X9ECParameters ecP = X962NamedCurves.GetByOid(oid);
  131. if (ecP == null)
  132. {
  133. ecP = SecNamedCurves.GetByOid(oid);
  134. }
  135. // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup
  136. if (ecP == null)
  137. {
  138. ecP = TeleTrusTNamedCurves.GetByOid(oid);
  139. }
  140. if (ecP == null)
  141. {
  142. ecP = AnssiNamedCurves.GetByOid(oid);
  143. }
  144. if (ecP == null)
  145. {
  146. ecP = FromDomainParameters(ECGost3410NamedCurves.GetByOid(oid));
  147. }
  148. if (ecP == null)
  149. {
  150. ecP = GMNamedCurves.GetByOid(oid);
  151. }
  152. return ecP;
  153. }
  154. /**
  155. * return an enumeration of the names of the available curves.
  156. *
  157. * @return an enumeration of the names of the available curves.
  158. */
  159. public static IEnumerable Names
  160. {
  161. get
  162. {
  163. IList v = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  164. CollectionUtilities.AddRange(v, X962NamedCurves.Names);
  165. CollectionUtilities.AddRange(v, SecNamedCurves.Names);
  166. CollectionUtilities.AddRange(v, NistNamedCurves.Names);
  167. CollectionUtilities.AddRange(v, TeleTrusTNamedCurves.Names);
  168. CollectionUtilities.AddRange(v, AnssiNamedCurves.Names);
  169. CollectionUtilities.AddRange(v, ECGost3410NamedCurves.Names);
  170. CollectionUtilities.AddRange(v, GMNamedCurves.Names);
  171. return v;
  172. }
  173. }
  174. private static X9ECParameters FromDomainParameters(ECDomainParameters dp)
  175. {
  176. return dp == null ? null : new X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed());
  177. }
  178. }
  179. }
  180. #pragma warning restore
  181. #endif