Pkcs10CertificationRequest.cs 26 KB

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