Pkcs10CertificationRequest.cs 25 KB

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