ECNamedCurveTable.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.Anssi;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  12. {
  13. /// <summary>A unified elliptic curve registry of the various standard-specific registries.</summary>
  14. public class ECNamedCurveTable
  15. {
  16. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary>
  17. /// <param name="name">The name of the curve.</param>
  18. public static X9ECParameters GetByName(string name)
  19. {
  20. X9ECParameters ecP = X962NamedCurves.GetByName(name);
  21. if (ecP == null)
  22. {
  23. ecP = SecNamedCurves.GetByName(name);
  24. }
  25. if (ecP == null)
  26. {
  27. ecP = NistNamedCurves.GetByName(name);
  28. }
  29. if (ecP == null)
  30. {
  31. ecP = TeleTrusTNamedCurves.GetByName(name);
  32. }
  33. if (ecP == null)
  34. {
  35. ecP = AnssiNamedCurves.GetByName(name);
  36. }
  37. if (ecP == null)
  38. {
  39. ecP = ECGost3410NamedCurves.GetByName(name);
  40. }
  41. if (ecP == null)
  42. {
  43. ecP = GMNamedCurves.GetByName(name);
  44. }
  45. return ecP;
  46. }
  47. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary>
  48. /// <remarks>
  49. /// Allows accessing the <see cref="Math.EC.ECCurve">curve</see> without necessarily triggering the creation of
  50. /// the full <see cref="X9ECParameters"/>.
  51. /// </remarks>
  52. /// <param name="name">The name of the curve.</param>
  53. public static X9ECParametersHolder GetByNameLazy(string name)
  54. {
  55. X9ECParametersHolder holder = X962NamedCurves.GetByNameLazy(name);
  56. if (null == holder)
  57. {
  58. holder = SecNamedCurves.GetByNameLazy(name);
  59. }
  60. if (null == holder)
  61. {
  62. holder = NistNamedCurves.GetByNameLazy(name);
  63. }
  64. if (null == holder)
  65. {
  66. holder = TeleTrusTNamedCurves.GetByNameLazy(name);
  67. }
  68. if (null == holder)
  69. {
  70. holder = AnssiNamedCurves.GetByNameLazy(name);
  71. }
  72. if (null == holder)
  73. {
  74. holder = ECGost3410NamedCurves.GetByNameLazy(name);
  75. }
  76. if (null == holder)
  77. {
  78. holder = GMNamedCurves.GetByNameLazy(name);
  79. }
  80. return holder;
  81. }
  82. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given
  83. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  84. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  85. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  86. {
  87. X9ECParameters ecP = X962NamedCurves.GetByOid(oid);
  88. if (ecP == null)
  89. {
  90. ecP = SecNamedCurves.GetByOid(oid);
  91. }
  92. // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup
  93. if (ecP == null)
  94. {
  95. ecP = TeleTrusTNamedCurves.GetByOid(oid);
  96. }
  97. if (ecP == null)
  98. {
  99. ecP = AnssiNamedCurves.GetByOid(oid);
  100. }
  101. if (ecP == null)
  102. {
  103. ecP = ECGost3410NamedCurves.GetByOid(oid);
  104. }
  105. if (ecP == null)
  106. {
  107. ecP = GMNamedCurves.GetByOid(oid);
  108. }
  109. return ecP;
  110. }
  111. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given
  112. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  113. /// <remarks>
  114. /// Allows accessing the <see cref="Math.EC.ECCurve">curve</see> without necessarily triggering the creation of
  115. /// the full <see cref="X9ECParameters"/>.
  116. /// </remarks>
  117. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  118. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid)
  119. {
  120. X9ECParametersHolder holder = X962NamedCurves.GetByOidLazy(oid);
  121. if (null == holder)
  122. {
  123. holder = SecNamedCurves.GetByOidLazy(oid);
  124. }
  125. // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup
  126. if (null == holder)
  127. {
  128. holder = TeleTrusTNamedCurves.GetByOidLazy(oid);
  129. }
  130. if (null == holder)
  131. {
  132. holder = AnssiNamedCurves.GetByOidLazy(oid);
  133. }
  134. if (null == holder)
  135. {
  136. holder = ECGost3410NamedCurves.GetByOidLazy(oid);
  137. }
  138. if (null == holder)
  139. {
  140. holder = GMNamedCurves.GetByOidLazy(oid);
  141. }
  142. return holder;
  143. }
  144. /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary>
  145. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  146. public static string GetName(DerObjectIdentifier oid)
  147. {
  148. string name = X962NamedCurves.GetName(oid);
  149. if (name == null)
  150. {
  151. name = SecNamedCurves.GetName(oid);
  152. }
  153. if (name == null)
  154. {
  155. name = NistNamedCurves.GetName(oid);
  156. }
  157. if (name == null)
  158. {
  159. name = TeleTrusTNamedCurves.GetName(oid);
  160. }
  161. if (name == null)
  162. {
  163. name = AnssiNamedCurves.GetName(oid);
  164. }
  165. if (name == null)
  166. {
  167. name = ECGost3410NamedCurves.GetName(oid);
  168. }
  169. if (name == null)
  170. {
  171. name = GMNamedCurves.GetName(oid);
  172. }
  173. return name;
  174. }
  175. /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary>
  176. /// <param name="name">The name of the curve.</param>
  177. public static DerObjectIdentifier GetOid(string name)
  178. {
  179. DerObjectIdentifier oid = X962NamedCurves.GetOid(name);
  180. if (oid == null)
  181. {
  182. oid = SecNamedCurves.GetOid(name);
  183. }
  184. if (oid == null)
  185. {
  186. oid = NistNamedCurves.GetOid(name);
  187. }
  188. if (oid == null)
  189. {
  190. oid = TeleTrusTNamedCurves.GetOid(name);
  191. }
  192. if (oid == null)
  193. {
  194. oid = AnssiNamedCurves.GetOid(name);
  195. }
  196. if (oid == null)
  197. {
  198. oid = ECGost3410NamedCurves.GetOid(name);
  199. }
  200. if (oid == null)
  201. {
  202. oid = GMNamedCurves.GetOid(name);
  203. }
  204. return oid;
  205. }
  206. /// <summary>Enumerate the available curve names in all the registries.</summary>
  207. public static IEnumerable<string> Names
  208. {
  209. get
  210. {
  211. var result = new List<string>();
  212. result.AddRange(X962NamedCurves.Names);
  213. result.AddRange(SecNamedCurves.Names);
  214. result.AddRange(NistNamedCurves.Names);
  215. result.AddRange(TeleTrusTNamedCurves.Names);
  216. result.AddRange(AnssiNamedCurves.Names);
  217. result.AddRange(ECGost3410NamedCurves.Names);
  218. result.AddRange(GMNamedCurves.Names);
  219. return result;
  220. }
  221. }
  222. }
  223. }
  224. #pragma warning restore
  225. #endif