SignerUtilities.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Asn1;
  6. using Org.BouncyCastle.Asn1.CryptoPro;
  7. using Org.BouncyCastle.Asn1.Nist;
  8. using Org.BouncyCastle.Asn1.Pkcs;
  9. using Org.BouncyCastle.Asn1.TeleTrust;
  10. using Org.BouncyCastle.Asn1.X509;
  11. using Org.BouncyCastle.Asn1.X9;
  12. using Org.BouncyCastle.Security;
  13. using Org.BouncyCastle.Crypto.Digests;
  14. using Org.BouncyCastle.Crypto;
  15. using Org.BouncyCastle.Crypto.Engines;
  16. using Org.BouncyCastle.Crypto.Signers;
  17. using Org.BouncyCastle.Utilities;
  18. namespace Org.BouncyCastle.Security
  19. {
  20. /// <summary>
  21. /// Signer Utility class contains methods that can not be specifically grouped into other classes.
  22. /// </summary>
  23. public sealed class SignerUtilities
  24. {
  25. private SignerUtilities()
  26. {
  27. }
  28. internal static readonly IDictionary algorithms = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  29. internal static readonly IDictionary oids = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  30. static SignerUtilities()
  31. {
  32. algorithms["MD2WITHRSA"] = "MD2withRSA";
  33. algorithms["MD2WITHRSAENCRYPTION"] = "MD2withRSA";
  34. algorithms[PkcsObjectIdentifiers.MD2WithRsaEncryption.Id] = "MD2withRSA";
  35. algorithms["MD4WITHRSA"] = "MD4withRSA";
  36. algorithms["MD4WITHRSAENCRYPTION"] = "MD4withRSA";
  37. algorithms[PkcsObjectIdentifiers.MD4WithRsaEncryption.Id] = "MD4withRSA";
  38. algorithms["MD5WITHRSA"] = "MD5withRSA";
  39. algorithms["MD5WITHRSAENCRYPTION"] = "MD5withRSA";
  40. algorithms[PkcsObjectIdentifiers.MD5WithRsaEncryption.Id] = "MD5withRSA";
  41. algorithms["SHA1WITHRSA"] = "SHA-1withRSA";
  42. algorithms["SHA1WITHRSAENCRYPTION"] = "SHA-1withRSA";
  43. algorithms[PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id] = "SHA-1withRSA";
  44. algorithms["SHA-1WITHRSA"] = "SHA-1withRSA";
  45. algorithms["SHA224WITHRSA"] = "SHA-224withRSA";
  46. algorithms["SHA224WITHRSAENCRYPTION"] = "SHA-224withRSA";
  47. algorithms[PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id] = "SHA-224withRSA";
  48. algorithms["SHA-224WITHRSA"] = "SHA-224withRSA";
  49. algorithms["SHA256WITHRSA"] = "SHA-256withRSA";
  50. algorithms["SHA256WITHRSAENCRYPTION"] = "SHA-256withRSA";
  51. algorithms[PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id] = "SHA-256withRSA";
  52. algorithms["SHA-256WITHRSA"] = "SHA-256withRSA";
  53. algorithms["SHA384WITHRSA"] = "SHA-384withRSA";
  54. algorithms["SHA384WITHRSAENCRYPTION"] = "SHA-384withRSA";
  55. algorithms[PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id] = "SHA-384withRSA";
  56. algorithms["SHA-384WITHRSA"] = "SHA-384withRSA";
  57. algorithms["SHA512WITHRSA"] = "SHA-512withRSA";
  58. algorithms["SHA512WITHRSAENCRYPTION"] = "SHA-512withRSA";
  59. algorithms[PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id] = "SHA-512withRSA";
  60. algorithms["SHA-512WITHRSA"] = "SHA-512withRSA";
  61. algorithms["PSSWITHRSA"] = "PSSwithRSA";
  62. algorithms["RSASSA-PSS"] = "PSSwithRSA";
  63. algorithms[PkcsObjectIdentifiers.IdRsassaPss.Id] = "PSSwithRSA";
  64. algorithms["RSAPSS"] = "PSSwithRSA";
  65. algorithms["SHA1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
  66. algorithms["SHA-1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
  67. algorithms["SHA1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
  68. algorithms["SHA-1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
  69. algorithms["SHA224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
  70. algorithms["SHA-224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
  71. algorithms["SHA224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
  72. algorithms["SHA-224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
  73. algorithms["SHA256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
  74. algorithms["SHA-256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
  75. algorithms["SHA256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
  76. algorithms["SHA-256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
  77. algorithms["SHA384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
  78. algorithms["SHA-384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
  79. algorithms["SHA384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
  80. algorithms["SHA-384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
  81. algorithms["SHA512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
  82. algorithms["SHA-512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
  83. algorithms["SHA512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
  84. algorithms["SHA-512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
  85. algorithms["RIPEMD128WITHRSA"] = "RIPEMD128withRSA";
  86. algorithms["RIPEMD128WITHRSAENCRYPTION"] = "RIPEMD128withRSA";
  87. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128.Id] = "RIPEMD128withRSA";
  88. algorithms["RIPEMD160WITHRSA"] = "RIPEMD160withRSA";
  89. algorithms["RIPEMD160WITHRSAENCRYPTION"] = "RIPEMD160withRSA";
  90. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160.Id] = "RIPEMD160withRSA";
  91. algorithms["RIPEMD256WITHRSA"] = "RIPEMD256withRSA";
  92. algorithms["RIPEMD256WITHRSAENCRYPTION"] = "RIPEMD256withRSA";
  93. algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256.Id] = "RIPEMD256withRSA";
  94. algorithms["NONEWITHRSA"] = "RSA";
  95. algorithms["RSAWITHNONE"] = "RSA";
  96. algorithms["RAWRSA"] = "RSA";
  97. algorithms["RAWRSAPSS"] = "RAWRSASSA-PSS";
  98. algorithms["NONEWITHRSAPSS"] = "RAWRSASSA-PSS";
  99. algorithms["NONEWITHRSASSA-PSS"] = "RAWRSASSA-PSS";
  100. algorithms["NONEWITHDSA"] = "NONEwithDSA";
  101. algorithms["DSAWITHNONE"] = "NONEwithDSA";
  102. algorithms["RAWDSA"] = "NONEwithDSA";
  103. algorithms["DSA"] = "SHA-1withDSA";
  104. algorithms["DSAWITHSHA1"] = "SHA-1withDSA";
  105. algorithms["DSAWITHSHA-1"] = "SHA-1withDSA";
  106. algorithms["SHA/DSA"] = "SHA-1withDSA";
  107. algorithms["SHA1/DSA"] = "SHA-1withDSA";
  108. algorithms["SHA-1/DSA"] = "SHA-1withDSA";
  109. algorithms["SHA1WITHDSA"] = "SHA-1withDSA";
  110. algorithms["SHA-1WITHDSA"] = "SHA-1withDSA";
  111. algorithms[X9ObjectIdentifiers.IdDsaWithSha1.Id] = "SHA-1withDSA";
  112. algorithms["DSAWITHSHA224"] = "SHA-224withDSA";
  113. algorithms["DSAWITHSHA-224"] = "SHA-224withDSA";
  114. algorithms["SHA224/DSA"] = "SHA-224withDSA";
  115. algorithms["SHA-224/DSA"] = "SHA-224withDSA";
  116. algorithms["SHA224WITHDSA"] = "SHA-224withDSA";
  117. algorithms["SHA-224WITHDSA"] = "SHA-224withDSA";
  118. algorithms[NistObjectIdentifiers.DsaWithSha224.Id] = "SHA-224withDSA";
  119. algorithms["DSAWITHSHA256"] = "SHA-256withDSA";
  120. algorithms["DSAWITHSHA-256"] = "SHA-256withDSA";
  121. algorithms["SHA256/DSA"] = "SHA-256withDSA";
  122. algorithms["SHA-256/DSA"] = "SHA-256withDSA";
  123. algorithms["SHA256WITHDSA"] = "SHA-256withDSA";
  124. algorithms["SHA-256WITHDSA"] = "SHA-256withDSA";
  125. algorithms[NistObjectIdentifiers.DsaWithSha256.Id] = "SHA-256withDSA";
  126. algorithms["DSAWITHSHA384"] = "SHA-384withDSA";
  127. algorithms["DSAWITHSHA-384"] = "SHA-384withDSA";
  128. algorithms["SHA384/DSA"] = "SHA-384withDSA";
  129. algorithms["SHA-384/DSA"] = "SHA-384withDSA";
  130. algorithms["SHA384WITHDSA"] = "SHA-384withDSA";
  131. algorithms["SHA-384WITHDSA"] = "SHA-384withDSA";
  132. algorithms[NistObjectIdentifiers.DsaWithSha384.Id] = "SHA-384withDSA";
  133. algorithms["DSAWITHSHA512"] = "SHA-512withDSA";
  134. algorithms["DSAWITHSHA-512"] = "SHA-512withDSA";
  135. algorithms["SHA512/DSA"] = "SHA-512withDSA";
  136. algorithms["SHA-512/DSA"] = "SHA-512withDSA";
  137. algorithms["SHA512WITHDSA"] = "SHA-512withDSA";
  138. algorithms["SHA-512WITHDSA"] = "SHA-512withDSA";
  139. algorithms[NistObjectIdentifiers.DsaWithSha512.Id] = "SHA-512withDSA";
  140. algorithms["NONEWITHECDSA"] = "NONEwithECDSA";
  141. algorithms["ECDSAWITHNONE"] = "NONEwithECDSA";
  142. algorithms["ECDSA"] = "SHA-1withECDSA";
  143. algorithms["SHA1/ECDSA"] = "SHA-1withECDSA";
  144. algorithms["SHA-1/ECDSA"] = "SHA-1withECDSA";
  145. algorithms["ECDSAWITHSHA1"] = "SHA-1withECDSA";
  146. algorithms["ECDSAWITHSHA-1"] = "SHA-1withECDSA";
  147. algorithms["SHA1WITHECDSA"] = "SHA-1withECDSA";
  148. algorithms["SHA-1WITHECDSA"] = "SHA-1withECDSA";
  149. algorithms[X9ObjectIdentifiers.ECDsaWithSha1.Id] = "SHA-1withECDSA";
  150. algorithms[TeleTrusTObjectIdentifiers.ECSignWithSha1.Id] = "SHA-1withECDSA";
  151. algorithms["SHA224/ECDSA"] = "SHA-224withECDSA";
  152. algorithms["SHA-224/ECDSA"] = "SHA-224withECDSA";
  153. algorithms["ECDSAWITHSHA224"] = "SHA-224withECDSA";
  154. algorithms["ECDSAWITHSHA-224"] = "SHA-224withECDSA";
  155. algorithms["SHA224WITHECDSA"] = "SHA-224withECDSA";
  156. algorithms["SHA-224WITHECDSA"] = "SHA-224withECDSA";
  157. algorithms[X9ObjectIdentifiers.ECDsaWithSha224.Id] = "SHA-224withECDSA";
  158. algorithms["SHA256/ECDSA"] = "SHA-256withECDSA";
  159. algorithms["SHA-256/ECDSA"] = "SHA-256withECDSA";
  160. algorithms["ECDSAWITHSHA256"] = "SHA-256withECDSA";
  161. algorithms["ECDSAWITHSHA-256"] = "SHA-256withECDSA";
  162. algorithms["SHA256WITHECDSA"] = "SHA-256withECDSA";
  163. algorithms["SHA-256WITHECDSA"] = "SHA-256withECDSA";
  164. algorithms[X9ObjectIdentifiers.ECDsaWithSha256.Id] = "SHA-256withECDSA";
  165. algorithms["SHA384/ECDSA"] = "SHA-384withECDSA";
  166. algorithms["SHA-384/ECDSA"] = "SHA-384withECDSA";
  167. algorithms["ECDSAWITHSHA384"] = "SHA-384withECDSA";
  168. algorithms["ECDSAWITHSHA-384"] = "SHA-384withECDSA";
  169. algorithms["SHA384WITHECDSA"] = "SHA-384withECDSA";
  170. algorithms["SHA-384WITHECDSA"] = "SHA-384withECDSA";
  171. algorithms[X9ObjectIdentifiers.ECDsaWithSha384.Id] = "SHA-384withECDSA";
  172. algorithms["SHA512/ECDSA"] = "SHA-512withECDSA";
  173. algorithms["SHA-512/ECDSA"] = "SHA-512withECDSA";
  174. algorithms["ECDSAWITHSHA512"] = "SHA-512withECDSA";
  175. algorithms["ECDSAWITHSHA-512"] = "SHA-512withECDSA";
  176. algorithms["SHA512WITHECDSA"] = "SHA-512withECDSA";
  177. algorithms["SHA-512WITHECDSA"] = "SHA-512withECDSA";
  178. algorithms[X9ObjectIdentifiers.ECDsaWithSha512.Id] = "SHA-512withECDSA";
  179. algorithms["RIPEMD160/ECDSA"] = "RIPEMD160withECDSA";
  180. algorithms["ECDSAWITHRIPEMD160"] = "RIPEMD160withECDSA";
  181. algorithms["RIPEMD160WITHECDSA"] = "RIPEMD160withECDSA";
  182. algorithms[TeleTrusTObjectIdentifiers.ECSignWithRipeMD160.Id] = "RIPEMD160withECDSA";
  183. algorithms["GOST-3410"] = "GOST3410";
  184. algorithms["GOST-3410-94"] = "GOST3410";
  185. algorithms["GOST3411WITHGOST3410"] = "GOST3410";
  186. algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94.Id] = "GOST3410";
  187. algorithms["ECGOST-3410"] = "ECGOST3410";
  188. algorithms["ECGOST-3410-2001"] = "ECGOST3410";
  189. algorithms["GOST3411WITHECGOST3410"] = "ECGOST3410";
  190. algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001.Id] = "ECGOST3410";
  191. oids["MD2withRSA"] = PkcsObjectIdentifiers.MD2WithRsaEncryption;
  192. oids["MD4withRSA"] = PkcsObjectIdentifiers.MD4WithRsaEncryption;
  193. oids["MD5withRSA"] = PkcsObjectIdentifiers.MD5WithRsaEncryption;
  194. oids["SHA-1withRSA"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  195. oids["SHA-224withRSA"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  196. oids["SHA-256withRSA"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  197. oids["SHA-384withRSA"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  198. oids["SHA-512withRSA"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  199. oids["PSSwithRSA"] = PkcsObjectIdentifiers.IdRsassaPss;
  200. oids["SHA-1withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  201. oids["SHA-224withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  202. oids["SHA-256withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  203. oids["SHA-384withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  204. oids["SHA-512withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  205. oids["RIPEMD128withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128;
  206. oids["RIPEMD160withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160;
  207. oids["RIPEMD256withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256;
  208. oids["SHA-1withDSA"] = X9ObjectIdentifiers.IdDsaWithSha1;
  209. oids["SHA-1withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha1;
  210. oids["SHA-224withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha224;
  211. oids["SHA-256withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha256;
  212. oids["SHA-384withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha384;
  213. oids["SHA-512withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha512;
  214. oids["GOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94;
  215. oids["ECGOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
  216. }
  217. /// <summary>
  218. /// Returns an ObjectIdentifier for a given encoding.
  219. /// </summary>
  220. /// <param name="mechanism">A string representation of the encoding.</param>
  221. /// <returns>A DerObjectIdentifier, null if the OID is not available.</returns>
  222. // TODO Don't really want to support this
  223. public static DerObjectIdentifier GetObjectIdentifier(
  224. string mechanism)
  225. {
  226. if (mechanism == null)
  227. throw new ArgumentNullException("mechanism");
  228. mechanism = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(mechanism);
  229. string aliased = (string) algorithms[mechanism];
  230. if (aliased != null)
  231. mechanism = aliased;
  232. return (DerObjectIdentifier) oids[mechanism];
  233. }
  234. public static ICollection Algorithms
  235. {
  236. get { return oids.Keys; }
  237. }
  238. public static Asn1Encodable GetDefaultX509Parameters(
  239. DerObjectIdentifier id)
  240. {
  241. return GetDefaultX509Parameters(id.Id);
  242. }
  243. public static Asn1Encodable GetDefaultX509Parameters(
  244. string algorithm)
  245. {
  246. if (algorithm == null)
  247. throw new ArgumentNullException("algorithm");
  248. algorithm = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  249. string mechanism = (string) algorithms[algorithm];
  250. if (mechanism == null)
  251. mechanism = algorithm;
  252. if (mechanism == "PSSwithRSA")
  253. {
  254. // TODO The Sha1Digest here is a default. In JCE version, the actual digest
  255. // to be used can be overridden by subsequent parameter settings.
  256. return GetPssX509Parameters("SHA-1");
  257. }
  258. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "withRSAandMGF1"))
  259. {
  260. string digestName = mechanism.Substring(0, mechanism.Length - "withRSAandMGF1".Length);
  261. return GetPssX509Parameters(digestName);
  262. }
  263. return DerNull.Instance;
  264. }
  265. private static Asn1Encodable GetPssX509Parameters(
  266. string digestName)
  267. {
  268. AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
  269. DigestUtilities.GetObjectIdentifier(digestName), DerNull.Instance);
  270. // TODO Is it possible for the MGF hash alg to be different from the PSS one?
  271. AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
  272. PkcsObjectIdentifiers.IdMgf1, hashAlgorithm);
  273. int saltLen = DigestUtilities.GetDigest(digestName).GetDigestSize();
  274. return new RsassaPssParameters(hashAlgorithm, maskGenAlgorithm,
  275. new DerInteger(saltLen), new DerInteger(1));
  276. }
  277. public static ISigner GetSigner(
  278. DerObjectIdentifier id)
  279. {
  280. return GetSigner(id.Id);
  281. }
  282. public static ISigner GetSigner(
  283. string algorithm)
  284. {
  285. if (algorithm == null)
  286. throw new ArgumentNullException("algorithm");
  287. algorithm = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  288. string mechanism = (string) algorithms[algorithm];
  289. if (mechanism == null)
  290. mechanism = algorithm;
  291. if (mechanism.Equals("RSA"))
  292. {
  293. return (new RsaDigestSigner(new NullDigest(), (AlgorithmIdentifier)null));
  294. }
  295. if (mechanism.Equals("MD2withRSA"))
  296. {
  297. return (new RsaDigestSigner(new MD2Digest()));
  298. }
  299. if (mechanism.Equals("MD4withRSA"))
  300. {
  301. return (new RsaDigestSigner(new MD4Digest()));
  302. }
  303. if (mechanism.Equals("MD5withRSA"))
  304. {
  305. return (new RsaDigestSigner(new MD5Digest()));
  306. }
  307. if (mechanism.Equals("SHA-1withRSA"))
  308. {
  309. return (new RsaDigestSigner(new Sha1Digest()));
  310. }
  311. if (mechanism.Equals("SHA-224withRSA"))
  312. {
  313. return (new RsaDigestSigner(new Sha224Digest()));
  314. }
  315. if (mechanism.Equals("SHA-256withRSA"))
  316. {
  317. return (new RsaDigestSigner(new Sha256Digest()));
  318. }
  319. if (mechanism.Equals("SHA-384withRSA"))
  320. {
  321. return (new RsaDigestSigner(new Sha384Digest()));
  322. }
  323. if (mechanism.Equals("SHA-512withRSA"))
  324. {
  325. return (new RsaDigestSigner(new Sha512Digest()));
  326. }
  327. if (mechanism.Equals("RIPEMD128withRSA"))
  328. {
  329. return (new RsaDigestSigner(new RipeMD128Digest()));
  330. }
  331. if (mechanism.Equals("RIPEMD160withRSA"))
  332. {
  333. return (new RsaDigestSigner(new RipeMD160Digest()));
  334. }
  335. if (mechanism.Equals("RIPEMD256withRSA"))
  336. {
  337. return (new RsaDigestSigner(new RipeMD256Digest()));
  338. }
  339. if (mechanism.Equals("RAWRSASSA-PSS"))
  340. {
  341. // TODO Add support for other parameter settings
  342. return PssSigner.CreateRawSigner(new RsaBlindedEngine(), new Sha1Digest());
  343. }
  344. if (mechanism.Equals("PSSwithRSA"))
  345. {
  346. // TODO The Sha1Digest here is a default. In JCE version, the actual digest
  347. // to be used can be overridden by subsequent parameter settings.
  348. return (new PssSigner(new RsaBlindedEngine(), new Sha1Digest()));
  349. }
  350. if (mechanism.Equals("SHA-1withRSAandMGF1"))
  351. {
  352. return (new PssSigner(new RsaBlindedEngine(), new Sha1Digest()));
  353. }
  354. if (mechanism.Equals("SHA-224withRSAandMGF1"))
  355. {
  356. return (new PssSigner(new RsaBlindedEngine(), new Sha224Digest()));
  357. }
  358. if (mechanism.Equals("SHA-256withRSAandMGF1"))
  359. {
  360. return (new PssSigner(new RsaBlindedEngine(), new Sha256Digest()));
  361. }
  362. if (mechanism.Equals("SHA-384withRSAandMGF1"))
  363. {
  364. return (new PssSigner(new RsaBlindedEngine(), new Sha384Digest()));
  365. }
  366. if (mechanism.Equals("SHA-512withRSAandMGF1"))
  367. {
  368. return (new PssSigner(new RsaBlindedEngine(), new Sha512Digest()));
  369. }
  370. if (mechanism.Equals("NONEwithDSA"))
  371. {
  372. return (new DsaDigestSigner(new DsaSigner(), new NullDigest()));
  373. }
  374. if (mechanism.Equals("SHA-1withDSA"))
  375. {
  376. return (new DsaDigestSigner(new DsaSigner(), new Sha1Digest()));
  377. }
  378. if (mechanism.Equals("SHA-224withDSA"))
  379. {
  380. return (new DsaDigestSigner(new DsaSigner(), new Sha224Digest()));
  381. }
  382. if (mechanism.Equals("SHA-256withDSA"))
  383. {
  384. return (new DsaDigestSigner(new DsaSigner(), new Sha256Digest()));
  385. }
  386. if (mechanism.Equals("SHA-384withDSA"))
  387. {
  388. return (new DsaDigestSigner(new DsaSigner(), new Sha384Digest()));
  389. }
  390. if (mechanism.Equals("SHA-512withDSA"))
  391. {
  392. return (new DsaDigestSigner(new DsaSigner(), new Sha512Digest()));
  393. }
  394. if (mechanism.Equals("NONEwithECDSA"))
  395. {
  396. return (new DsaDigestSigner(new ECDsaSigner(), new NullDigest()));
  397. }
  398. if (mechanism.Equals("SHA-1withECDSA"))
  399. {
  400. return (new DsaDigestSigner(new ECDsaSigner(), new Sha1Digest()));
  401. }
  402. if (mechanism.Equals("SHA-224withECDSA"))
  403. {
  404. return (new DsaDigestSigner(new ECDsaSigner(), new Sha224Digest()));
  405. }
  406. if (mechanism.Equals("SHA-256withECDSA"))
  407. {
  408. return (new DsaDigestSigner(new ECDsaSigner(), new Sha256Digest()));
  409. }
  410. if (mechanism.Equals("SHA-384withECDSA"))
  411. {
  412. return (new DsaDigestSigner(new ECDsaSigner(), new Sha384Digest()));
  413. }
  414. if (mechanism.Equals("SHA-512withECDSA"))
  415. {
  416. return (new DsaDigestSigner(new ECDsaSigner(), new Sha512Digest()));
  417. }
  418. if (mechanism.Equals("RIPEMD160withECDSA"))
  419. {
  420. return (new DsaDigestSigner(new ECDsaSigner(), new RipeMD160Digest()));
  421. }
  422. if (mechanism.Equals("SHA1WITHECNR"))
  423. {
  424. return (new DsaDigestSigner(new ECNRSigner(), new Sha1Digest()));
  425. }
  426. if (mechanism.Equals("SHA224WITHECNR"))
  427. {
  428. return (new DsaDigestSigner(new ECNRSigner(), new Sha224Digest()));
  429. }
  430. if (mechanism.Equals("SHA256WITHECNR"))
  431. {
  432. return (new DsaDigestSigner(new ECNRSigner(), new Sha256Digest()));
  433. }
  434. if (mechanism.Equals("SHA384WITHECNR"))
  435. {
  436. return (new DsaDigestSigner(new ECNRSigner(), new Sha384Digest()));
  437. }
  438. if (mechanism.Equals("SHA512WITHECNR"))
  439. {
  440. return (new DsaDigestSigner(new ECNRSigner(), new Sha512Digest()));
  441. }
  442. if (mechanism.Equals("GOST3410"))
  443. {
  444. return new Gost3410DigestSigner(new Gost3410Signer(), new Gost3411Digest());
  445. }
  446. if (mechanism.Equals("ECGOST3410"))
  447. {
  448. return new Gost3410DigestSigner(new ECGost3410Signer(), new Gost3411Digest());
  449. }
  450. if (mechanism.Equals("SHA1WITHRSA/ISO9796-2"))
  451. {
  452. return new Iso9796d2Signer(new RsaBlindedEngine(), new Sha1Digest(), true);
  453. }
  454. if (mechanism.Equals("MD5WITHRSA/ISO9796-2"))
  455. {
  456. return new Iso9796d2Signer(new RsaBlindedEngine(), new MD5Digest(), true);
  457. }
  458. if (mechanism.Equals("RIPEMD160WITHRSA/ISO9796-2"))
  459. {
  460. return new Iso9796d2Signer(new RsaBlindedEngine(), new RipeMD160Digest(), true);
  461. }
  462. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "/X9.31"))
  463. {
  464. string x931 = mechanism.Substring(0, mechanism.Length - "/X9.31".Length);
  465. int withPos = Org.BouncyCastle.Utilities.Platform.IndexOf(x931, "WITH");
  466. if (withPos > 0)
  467. {
  468. int endPos = withPos + "WITH".Length;
  469. string digestName = x931.Substring(0, withPos);
  470. IDigest digest = DigestUtilities.GetDigest(digestName);
  471. string cipherName = x931.Substring(endPos, x931.Length - endPos);
  472. if (cipherName.Equals("RSA"))
  473. {
  474. IAsymmetricBlockCipher cipher = new RsaBlindedEngine();
  475. return new X931Signer(cipher, digest);
  476. }
  477. }
  478. }
  479. throw new SecurityUtilityException("Signer " + algorithm + " not recognised.");
  480. }
  481. public static string GetEncodingName(
  482. DerObjectIdentifier oid)
  483. {
  484. return (string) algorithms[oid.Id];
  485. }
  486. }
  487. }
  488. #endif