Asn1Signature.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.CryptoPro;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  16. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators
  17. {
  18. internal class X509Utilities
  19. {
  20. private static readonly IDictionary<string, DerObjectIdentifier> m_algorithms =
  21. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  22. private static readonly IDictionary<string, Asn1Encodable> m_exParams =
  23. new Dictionary<string, Asn1Encodable>(StringComparer.OrdinalIgnoreCase);
  24. private static readonly HashSet<DerObjectIdentifier> noParams = new HashSet<DerObjectIdentifier>();
  25. static X509Utilities()
  26. {
  27. m_algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  28. m_algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  29. m_algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  30. m_algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  31. m_algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  32. m_algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  33. m_algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  34. m_algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  35. m_algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  36. m_algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  37. m_algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  38. m_algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  39. m_algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  40. m_algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  41. m_algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  42. m_algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  43. m_algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  44. m_algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  45. m_algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  46. m_algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  47. m_algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  48. m_algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  49. m_algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  50. m_algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  51. m_algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  52. m_algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  53. m_algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  54. m_algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  55. m_algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  56. m_algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  57. m_algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  58. m_algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  59. m_algorithms.Add("SHA3-224WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224);
  60. m_algorithms.Add("SHA3-256WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256);
  61. m_algorithms.Add("SHA3-384WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384);
  62. m_algorithms.Add("SHA3-512WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512);
  63. m_algorithms.Add("SHA3-224WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224);
  64. m_algorithms.Add("SHA3-256WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256);
  65. m_algorithms.Add("SHA3-384WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384);
  66. m_algorithms.Add("SHA3-512WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512);
  67. m_algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  68. m_algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  69. m_algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  70. m_algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  71. m_algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  72. m_algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  73. m_algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  74. m_algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  75. m_algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  76. m_algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  77. m_algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  78. m_algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
  79. m_algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
  80. m_algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
  81. m_algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
  82. m_algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
  83. m_algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
  84. m_algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
  85. m_algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
  86. m_algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
  87. m_algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
  88. m_algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
  89. m_algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
  90. m_algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  91. m_algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  92. m_algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  93. m_algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  94. m_algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  95. m_algorithms.Add("GOST3411-2012-256WITHECGOST3410", RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256);
  96. m_algorithms.Add("GOST3411-2012-256WITHECGOST3410-2012-256", RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256);
  97. m_algorithms.Add("GOST3411-2012-512WITHECGOST3410", RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512);
  98. m_algorithms.Add("GOST3411-2012-512WITHECGOST3410-2012-512", RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512);
  99. //
  100. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  101. // The parameters field SHALL be NULL for RSA based signature algorithms.
  102. //
  103. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
  104. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
  105. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
  106. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
  107. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
  108. noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
  109. noParams.Add(OiwObjectIdentifiers.DsaWithSha1);
  110. noParams.Add(NistObjectIdentifiers.DsaWithSha224);
  111. noParams.Add(NistObjectIdentifiers.DsaWithSha256);
  112. noParams.Add(NistObjectIdentifiers.DsaWithSha384);
  113. noParams.Add(NistObjectIdentifiers.DsaWithSha512);
  114. //
  115. // RFC 4491
  116. //
  117. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  118. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  119. //
  120. // explicit params
  121. //
  122. AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  123. m_exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
  124. AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
  125. m_exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
  126. AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
  127. m_exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
  128. AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
  129. m_exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
  130. AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
  131. m_exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
  132. }
  133. /**
  134. * Return the digest algorithm using one of the standard JCA string
  135. * representations rather than the algorithm identifier (if possible).
  136. */
  137. private static string GetDigestAlgName(
  138. DerObjectIdentifier digestAlgOID)
  139. {
  140. if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
  141. {
  142. return "MD5";
  143. }
  144. else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
  145. {
  146. return "SHA1";
  147. }
  148. else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
  149. {
  150. return "SHA224";
  151. }
  152. else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
  153. {
  154. return "SHA256";
  155. }
  156. else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
  157. {
  158. return "SHA384";
  159. }
  160. else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
  161. {
  162. return "SHA512";
  163. }
  164. else if (NistObjectIdentifiers.IdSha512_224.Equals(digestAlgOID))
  165. {
  166. return "SHA512(224)";
  167. }
  168. else if (NistObjectIdentifiers.IdSha512_256.Equals(digestAlgOID))
  169. {
  170. return "SHA512(256)";
  171. }
  172. else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
  173. {
  174. return "RIPEMD128";
  175. }
  176. else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
  177. {
  178. return "RIPEMD160";
  179. }
  180. else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
  181. {
  182. return "RIPEMD256";
  183. }
  184. else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
  185. {
  186. return "GOST3411";
  187. }
  188. else if (RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Equals(digestAlgOID))
  189. {
  190. return "GOST3411-2012-256";
  191. }
  192. else if (RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Equals(digestAlgOID))
  193. {
  194. return "GOST3411-2012-512";
  195. }
  196. else
  197. {
  198. return digestAlgOID.Id;
  199. }
  200. }
  201. internal static string GetSignatureName(AlgorithmIdentifier sigAlgId)
  202. {
  203. Asn1Encodable parameters = sigAlgId.Parameters;
  204. if (parameters != null && !DerNull.Instance.Equals(parameters))
  205. {
  206. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  207. {
  208. RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
  209. return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
  210. }
  211. if (sigAlgId.Algorithm.Equals(X9ObjectIdentifiers.ECDsaWithSha2))
  212. {
  213. Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
  214. return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
  215. }
  216. }
  217. return sigAlgId.Algorithm.Id;
  218. }
  219. private static RsassaPssParameters CreatePssParams(
  220. AlgorithmIdentifier hashAlgId,
  221. int saltSize)
  222. {
  223. return new RsassaPssParameters(
  224. hashAlgId,
  225. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
  226. new DerInteger(saltSize),
  227. new DerInteger(1));
  228. }
  229. internal static DerObjectIdentifier GetAlgorithmOid(string algorithmName)
  230. {
  231. if (m_algorithms.TryGetValue(algorithmName, out var oid))
  232. return oid;
  233. return new DerObjectIdentifier(algorithmName);
  234. }
  235. internal static AlgorithmIdentifier GetSigAlgID(DerObjectIdentifier sigOid, string algorithmName)
  236. {
  237. if (noParams.Contains(sigOid))
  238. return new AlgorithmIdentifier(sigOid);
  239. if (m_exParams.TryGetValue(algorithmName, out var explicitParameters))
  240. return new AlgorithmIdentifier(sigOid, explicitParameters);
  241. return new AlgorithmIdentifier(sigOid, DerNull.Instance);
  242. }
  243. internal static IEnumerable<string> GetAlgNames()
  244. {
  245. return CollectionUtilities.Proxy(m_algorithms.Keys);
  246. }
  247. }
  248. /// <summary>
  249. /// Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
  250. /// signature algorithm details.
  251. /// </summary>
  252. public class Asn1SignatureFactory
  253. : ISignatureFactory
  254. {
  255. private readonly AlgorithmIdentifier algID;
  256. private readonly string algorithm;
  257. private readonly AsymmetricKeyParameter privateKey;
  258. private readonly SecureRandom random;
  259. /// <summary>
  260. /// Base constructor.
  261. /// </summary>
  262. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  263. /// <param name="privateKey">The private key to be used in the signing operation.</param>
  264. public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey)
  265. : this(algorithm, privateKey, null)
  266. {
  267. }
  268. /// <summary>
  269. /// Constructor which also specifies a source of randomness to be used if one is required.
  270. /// </summary>
  271. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  272. /// <param name="privateKey">The private key to be used in the signing operation.</param>
  273. /// <param name="random">The source of randomness to be used in signature calculation.</param>
  274. public Asn1SignatureFactory(string algorithm, AsymmetricKeyParameter privateKey, SecureRandom random)
  275. {
  276. if (algorithm == null)
  277. throw new ArgumentNullException("algorithm");
  278. if (privateKey == null)
  279. throw new ArgumentNullException("privateKey");
  280. if (!privateKey.IsPrivate)
  281. throw new ArgumentException("Key for signing must be private", "privateKey");
  282. DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
  283. this.algorithm = algorithm;
  284. this.privateKey = privateKey;
  285. this.random = random;
  286. this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
  287. }
  288. public object AlgorithmDetails
  289. {
  290. get { return this.algID; }
  291. }
  292. public IStreamCalculator<IBlockResult> CreateCalculator()
  293. {
  294. ISigner signer = SignerUtilities.InitSigner(algorithm, true, privateKey, random);
  295. return new DefaultSignatureCalculator(signer);
  296. }
  297. /// <summary>
  298. /// Allows enumeration of the signature names supported by the verifier provider.
  299. /// </summary>
  300. public static IEnumerable<string> SignatureAlgNames
  301. {
  302. get { return X509Utilities.GetAlgNames(); }
  303. }
  304. }
  305. /// <summary>
  306. /// Verifier class for signature verification in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
  307. /// signature algorithm details.
  308. /// </summary>
  309. public class Asn1VerifierFactory
  310. : IVerifierFactory
  311. {
  312. private readonly AlgorithmIdentifier algID;
  313. private readonly AsymmetricKeyParameter publicKey;
  314. /// <summary>
  315. /// Base constructor.
  316. /// </summary>
  317. /// <param name="algorithm">The name of the signature algorithm to use.</param>
  318. /// <param name="publicKey">The public key to be used in the verification operation.</param>
  319. public Asn1VerifierFactory(string algorithm, AsymmetricKeyParameter publicKey)
  320. {
  321. if (algorithm == null)
  322. throw new ArgumentNullException("algorithm");
  323. if (publicKey == null)
  324. throw new ArgumentNullException("publicKey");
  325. if (publicKey.IsPrivate)
  326. throw new ArgumentException("Key for verifying must be public", "publicKey");
  327. DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
  328. this.publicKey = publicKey;
  329. this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
  330. }
  331. public Asn1VerifierFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
  332. {
  333. this.publicKey = publicKey;
  334. this.algID = algorithm;
  335. }
  336. public object AlgorithmDetails
  337. {
  338. get { return this.algID; }
  339. }
  340. public IStreamCalculator<IVerifier> CreateCalculator()
  341. {
  342. ISigner verifier = SignerUtilities.InitSigner(X509Utilities.GetSignatureName(algID), false, publicKey, null);
  343. return new DefaultVerifierCalculator(verifier);
  344. }
  345. }
  346. /// <summary>
  347. /// Provider class which supports dynamic creation of signature verifiers.
  348. /// </summary>
  349. public class Asn1VerifierFactoryProvider: IVerifierFactoryProvider
  350. {
  351. private readonly AsymmetricKeyParameter publicKey;
  352. /// <summary>
  353. /// Base constructor - specify the public key to be used in verification.
  354. /// </summary>
  355. /// <param name="publicKey">The public key to be used in creating verifiers provided by this object.</param>
  356. public Asn1VerifierFactoryProvider(AsymmetricKeyParameter publicKey)
  357. {
  358. this.publicKey = publicKey;
  359. }
  360. public IVerifierFactory CreateVerifierFactory(object algorithmDetails)
  361. {
  362. return new Asn1VerifierFactory((AlgorithmIdentifier)algorithmDetails, publicKey);
  363. }
  364. /// <summary>
  365. /// Allows enumeration of the signature names supported by the verifier provider.
  366. /// </summary>
  367. public IEnumerable<string> SignatureAlgNames
  368. {
  369. get { return X509Utilities.GetAlgNames(); }
  370. }
  371. }
  372. }
  373. #pragma warning restore
  374. #endif