Pkcs10CertificationRequest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  20. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
  21. {
  22. /// <remarks>
  23. /// A class for verifying and creating Pkcs10 Certification requests.
  24. /// </remarks>
  25. /// <code>
  26. /// CertificationRequest ::= Sequence {
  27. /// certificationRequestInfo CertificationRequestInfo,
  28. /// signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
  29. /// signature BIT STRING
  30. /// }
  31. ///
  32. /// CertificationRequestInfo ::= Sequence {
  33. /// version Integer { v1(0) } (v1,...),
  34. /// subject Name,
  35. /// subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
  36. /// attributes [0] Attributes{{ CRIAttributes }}
  37. /// }
  38. ///
  39. /// Attributes { ATTRIBUTE:IOSet } ::= Set OF Attr{{ IOSet }}
  40. ///
  41. /// Attr { ATTRIBUTE:IOSet } ::= Sequence {
  42. /// type ATTRIBUTE.&amp;id({IOSet}),
  43. /// values Set SIZE(1..MAX) OF ATTRIBUTE.&amp;Type({IOSet}{\@type})
  44. /// }
  45. /// </code>
  46. /// see <a href="http://www.rsasecurity.com/rsalabs/node.asp?id=2132"/>
  47. public class Pkcs10CertificationRequest
  48. : CertificationRequest
  49. {
  50. protected static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  51. protected static readonly IDictionary exParams = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  52. protected static readonly IDictionary keyAlgorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  53. protected static readonly IDictionary oids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  54. protected static readonly ISet noParams = new HashSet();
  55. static Pkcs10CertificationRequest()
  56. {
  57. algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  58. algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
  59. algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  60. algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  61. algorithms.Add("RSAWITHMD5", PkcsObjectIdentifiers.MD5WithRsaEncryption);
  62. algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  63. algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  64. algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  65. algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  66. algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  67. algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  68. algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  69. algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  70. algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  71. algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  72. algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  73. algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  74. algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  75. algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  76. algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
  77. algorithms.Add("RSAWITHSHA1", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  78. algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  79. algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  80. algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  81. algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  82. algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  83. algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  84. algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
  85. algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
  86. algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
  87. algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
  88. algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384);
  89. algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512);
  90. algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
  91. algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
  92. algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
  93. algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
  94. algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
  95. algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
  96. algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  97. algorithms.Add("GOST3410WITHGOST3411", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  98. algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  99. algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  100. algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  101. //
  102. // reverse mappings
  103. //
  104. oids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1WITHRSA");
  105. oids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224WITHRSA");
  106. oids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256WITHRSA");
  107. oids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384WITHRSA");
  108. oids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512WITHRSA");
  109. oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411WITHGOST3410");
  110. oids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411WITHECGOST3410");
  111. oids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5WITHRSA");
  112. oids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2WITHRSA");
  113. oids.Add(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1WITHDSA");
  114. oids.Add(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1WITHECDSA");
  115. oids.Add(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224WITHECDSA");
  116. oids.Add(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256WITHECDSA");
  117. oids.Add(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384WITHECDSA");
  118. oids.Add(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512WITHECDSA");
  119. oids.Add(OiwObjectIdentifiers.MD5WithRsa, "MD5WITHRSA");
  120. oids.Add(OiwObjectIdentifiers.Sha1WithRsa, "SHA1WITHRSA");
  121. oids.Add(OiwObjectIdentifiers.DsaWithSha1, "SHA1WITHDSA");
  122. oids.Add(NistObjectIdentifiers.DsaWithSha224, "SHA224WITHDSA");
  123. oids.Add(NistObjectIdentifiers.DsaWithSha256, "SHA256WITHDSA");
  124. //
  125. // key types
  126. //
  127. keyAlgorithms.Add(PkcsObjectIdentifiers.RsaEncryption, "RSA");
  128. keyAlgorithms.Add(X9ObjectIdentifiers.IdDsa, "DSA");
  129. //
  130. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  131. // The parameters field SHALL be NULL for RSA based signature algorithms.
  132. //
  133. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
  134. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
  135. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
  136. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
  137. noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
  138. noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
  139. noParams.Add(NistObjectIdentifiers.DsaWithSha224);
  140. noParams.Add(NistObjectIdentifiers.DsaWithSha256);
  141. //
  142. // RFC 4491
  143. //
  144. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  145. noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  146. //
  147. // explicit params
  148. //
  149. AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  150. exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));
  151. AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
  152. exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));
  153. AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
  154. exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));
  155. AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
  156. exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));
  157. AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
  158. exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
  159. }
  160. private static RsassaPssParameters CreatePssParams(
  161. AlgorithmIdentifier hashAlgId,
  162. int saltSize)
  163. {
  164. return new RsassaPssParameters(
  165. hashAlgId,
  166. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
  167. new DerInteger(saltSize),
  168. new DerInteger(1));
  169. }
  170. protected Pkcs10CertificationRequest()
  171. {
  172. }
  173. public Pkcs10CertificationRequest(
  174. byte[] encoded)
  175. : base((Asn1Sequence) Asn1Object.FromByteArray(encoded))
  176. {
  177. }
  178. public Pkcs10CertificationRequest(
  179. Asn1Sequence seq)
  180. : base(seq)
  181. {
  182. }
  183. public Pkcs10CertificationRequest(
  184. Stream input)
  185. : base((Asn1Sequence) Asn1Object.FromStream(input))
  186. {
  187. }
  188. /// <summary>
  189. /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
  190. /// </summary>
  191. ///<param name="signatureAlgorithm">Name of Sig Alg.</param>
  192. /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
  193. /// <param name="publicKey">Public Key to be included in cert reqest.</param>
  194. /// <param name="attributes">ASN1Set of Attributes.</param>
  195. /// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
  196. public Pkcs10CertificationRequest(
  197. string signatureAlgorithm,
  198. X509Name subject,
  199. AsymmetricKeyParameter publicKey,
  200. Asn1Set attributes,
  201. AsymmetricKeyParameter signingKey)
  202. : this(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes)
  203. {
  204. }
  205. /// <summary>
  206. /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
  207. /// </summary>
  208. ///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
  209. /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
  210. /// <param name="publicKey">Public Key to be included in cert reqest.</param>
  211. /// <param name="attributes">ASN1Set of Attributes.</param>
  212. /// <param name="signingKey">Ignored.</param>
  213. [Obsolete("Use constructor without 'signingKey' parameter (ignored here)")]
  214. public Pkcs10CertificationRequest(
  215. ISignatureFactory signatureFactory,
  216. X509Name subject,
  217. AsymmetricKeyParameter publicKey,
  218. Asn1Set attributes,
  219. AsymmetricKeyParameter signingKey)
  220. : this(signatureFactory, subject, publicKey, attributes)
  221. {
  222. }
  223. /// <summary>
  224. /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
  225. /// </summary>
  226. ///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
  227. /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
  228. /// <param name="publicKey">Public Key to be included in cert reqest.</param>
  229. /// <param name="attributes">ASN1Set of Attributes.</param>
  230. public Pkcs10CertificationRequest(
  231. ISignatureFactory signatureFactory,
  232. X509Name subject,
  233. AsymmetricKeyParameter publicKey,
  234. Asn1Set attributes)
  235. {
  236. if (signatureFactory == null)
  237. throw new ArgumentNullException("signatureFactory");
  238. if (subject == null)
  239. throw new ArgumentNullException("subject");
  240. if (publicKey == null)
  241. throw new ArgumentNullException("publicKey");
  242. if (publicKey.IsPrivate)
  243. throw new ArgumentException("expected public key", "publicKey");
  244. Init(signatureFactory, subject, publicKey, attributes);
  245. }
  246. private void Init(
  247. ISignatureFactory signatureFactory,
  248. X509Name subject,
  249. AsymmetricKeyParameter publicKey,
  250. Asn1Set attributes)
  251. {
  252. this.sigAlgId = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
  253. SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
  254. this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
  255. IStreamCalculator streamCalculator = signatureFactory.CreateCalculator();
  256. byte[] reqInfoData = reqInfo.GetDerEncoded();
  257. streamCalculator.Stream.Write(reqInfoData, 0, reqInfoData.Length);
  258. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  259. // Generate Signature.
  260. sigBits = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
  261. }
  262. // internal Pkcs10CertificationRequest(
  263. // Asn1InputStream seqStream)
  264. // {
  265. // Asn1Sequence seq = (Asn1Sequence) seqStream.ReadObject();
  266. // try
  267. // {
  268. // this.reqInfo = CertificationRequestInfo.GetInstance(seq[0]);
  269. // this.sigAlgId = AlgorithmIdentifier.GetInstance(seq[1]);
  270. // this.sigBits = (DerBitString) seq[2];
  271. // }
  272. // catch (Exception ex)
  273. // {
  274. // throw new ArgumentException("Create From Asn1Sequence: " + ex.Message);
  275. // }
  276. // }
  277. /// <summary>
  278. /// Get the public key.
  279. /// </summary>
  280. /// <returns>The public key.</returns>
  281. public AsymmetricKeyParameter GetPublicKey()
  282. {
  283. return PublicKeyFactory.CreateKey(reqInfo.SubjectPublicKeyInfo);
  284. }
  285. /// <summary>
  286. /// Verify Pkcs10 Cert Request is valid.
  287. /// </summary>
  288. /// <returns>true = valid.</returns>
  289. public bool Verify()
  290. {
  291. return Verify(this.GetPublicKey());
  292. }
  293. public bool Verify(
  294. AsymmetricKeyParameter publicKey)
  295. {
  296. return Verify(new Asn1VerifierFactoryProvider(publicKey));
  297. }
  298. public bool Verify(
  299. IVerifierFactoryProvider verifierProvider)
  300. {
  301. return Verify(verifierProvider.CreateVerifierFactory(sigAlgId));
  302. }
  303. public bool Verify(
  304. IVerifierFactory verifier)
  305. {
  306. try
  307. {
  308. byte[] b = reqInfo.GetDerEncoded();
  309. IStreamCalculator streamCalculator = verifier.CreateCalculator();
  310. streamCalculator.Stream.Write(b, 0, b.Length);
  311. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  312. return ((IVerifier)streamCalculator.GetResult()).IsVerified(sigBits.GetOctets());
  313. }
  314. catch (Exception e)
  315. {
  316. throw new SignatureException("exception encoding TBS cert request", e);
  317. }
  318. }
  319. // /// <summary>
  320. // /// Get the Der Encoded Pkcs10 Certification Request.
  321. // /// </summary>
  322. // /// <returns>A byte array.</returns>
  323. // public byte[] GetEncoded()
  324. // {
  325. // return new CertificationRequest(reqInfo, sigAlgId, sigBits).GetDerEncoded();
  326. // }
  327. // TODO Figure out how to set parameters on an ISigner
  328. private void SetSignatureParameters(
  329. ISigner signature,
  330. Asn1Encodable asn1Params)
  331. {
  332. if (asn1Params != null && !(asn1Params is Asn1Null))
  333. {
  334. // AlgorithmParameters sigParams = AlgorithmParameters.GetInstance(signature.getAlgorithm());
  335. //
  336. // try
  337. // {
  338. // sigParams.init(asn1Params.ToAsn1Object().GetDerEncoded());
  339. // }
  340. // catch (IOException e)
  341. // {
  342. // throw new SignatureException("IOException decoding parameters: " + e.Message);
  343. // }
  344. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EndsWith(signature.AlgorithmName, "MGF1"))
  345. {
  346. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("signature algorithm with MGF1");
  347. // try
  348. // {
  349. // signature.setParameter(sigParams.getParameterSpec(PSSParameterSpec.class));
  350. // }
  351. // catch (GeneralSecurityException e)
  352. // {
  353. // throw new SignatureException("Exception extracting parameters: " + e.getMessage());
  354. // }
  355. }
  356. }
  357. }
  358. internal static string GetSignatureName(
  359. AlgorithmIdentifier sigAlgId)
  360. {
  361. Asn1Encodable asn1Params = sigAlgId.Parameters;
  362. if (asn1Params != null && !(asn1Params is Asn1Null))
  363. {
  364. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  365. {
  366. RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(asn1Params);
  367. return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
  368. }
  369. }
  370. return sigAlgId.Algorithm.Id;
  371. }
  372. private static string GetDigestAlgName(
  373. DerObjectIdentifier digestAlgOID)
  374. {
  375. if (PkcsObjectIdentifiers.MD5.Equals(digestAlgOID))
  376. {
  377. return "MD5";
  378. }
  379. else if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOID))
  380. {
  381. return "SHA1";
  382. }
  383. else if (NistObjectIdentifiers.IdSha224.Equals(digestAlgOID))
  384. {
  385. return "SHA224";
  386. }
  387. else if (NistObjectIdentifiers.IdSha256.Equals(digestAlgOID))
  388. {
  389. return "SHA256";
  390. }
  391. else if (NistObjectIdentifiers.IdSha384.Equals(digestAlgOID))
  392. {
  393. return "SHA384";
  394. }
  395. else if (NistObjectIdentifiers.IdSha512.Equals(digestAlgOID))
  396. {
  397. return "SHA512";
  398. }
  399. else if (TeleTrusTObjectIdentifiers.RipeMD128.Equals(digestAlgOID))
  400. {
  401. return "RIPEMD128";
  402. }
  403. else if (TeleTrusTObjectIdentifiers.RipeMD160.Equals(digestAlgOID))
  404. {
  405. return "RIPEMD160";
  406. }
  407. else if (TeleTrusTObjectIdentifiers.RipeMD256.Equals(digestAlgOID))
  408. {
  409. return "RIPEMD256";
  410. }
  411. else if (CryptoProObjectIdentifiers.GostR3411.Equals(digestAlgOID))
  412. {
  413. return "GOST3411";
  414. }
  415. else
  416. {
  417. return digestAlgOID.Id;
  418. }
  419. }
  420. }
  421. }
  422. #pragma warning restore
  423. #endif