CMSSignedGenerator.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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.BC;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Bsi;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Eac;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  20. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  21. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  22. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  23. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  24. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  25. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  26. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  27. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  28. {
  29. public class DefaultSignatureAlgorithmIdentifierFinder
  30. {
  31. private static readonly IDictionary algorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  32. private static readonly ISet noParams = new HashSet();
  33. private static readonly IDictionary _params = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  34. private static readonly ISet pkcs15RsaEncryption = new HashSet();
  35. private static readonly IDictionary digestOids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  36. private static readonly IDictionary digestBuilders = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  37. private static readonly DerObjectIdentifier ENCRYPTION_RSA = PkcsObjectIdentifiers.RsaEncryption;
  38. private static readonly DerObjectIdentifier ENCRYPTION_DSA = X9ObjectIdentifiers.IdDsaWithSha1;
  39. private static readonly DerObjectIdentifier ENCRYPTION_ECDSA = X9ObjectIdentifiers.ECDsaWithSha1;
  40. private static readonly DerObjectIdentifier ENCRYPTION_RSA_PSS = PkcsObjectIdentifiers.IdRsassaPss;
  41. private static readonly DerObjectIdentifier ENCRYPTION_GOST3410 = CryptoProObjectIdentifiers.GostR3410x94;
  42. private static readonly DerObjectIdentifier ENCRYPTION_ECGOST3410 = CryptoProObjectIdentifiers.GostR3410x2001;
  43. private static readonly DerObjectIdentifier ENCRYPTION_ECGOST3410_2012_256 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
  44. private static readonly DerObjectIdentifier ENCRYPTION_ECGOST3410_2012_512 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
  45. static DefaultSignatureAlgorithmIdentifierFinder()
  46. {
  47. algorithms["MD2WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.MD2WithRsaEncryption;
  48. algorithms["MD2WITHRSA"] = PkcsObjectIdentifiers.MD2WithRsaEncryption;
  49. algorithms["MD5WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.MD5WithRsaEncryption;
  50. algorithms["MD5WITHRSA"] = PkcsObjectIdentifiers.MD5WithRsaEncryption;
  51. algorithms["SHA1WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  52. algorithms["SHA-1WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  53. algorithms["SHA1WITHRSA"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  54. algorithms["SHA-1WITHRSA"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
  55. algorithms["SHA224WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  56. algorithms["SHA-224WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  57. algorithms["SHA224WITHRSA"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  58. algorithms["SHA-224WITHRSA"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
  59. algorithms["SHA256WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  60. algorithms["SHA-256WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  61. algorithms["SHA256WITHRSA"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  62. algorithms["SHA-256WITHRSA"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
  63. algorithms["SHA384WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  64. algorithms["SHA-384WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  65. algorithms["SHA384WITHRSA"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  66. algorithms["SHA-384WITHRSA"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
  67. algorithms["SHA512WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  68. algorithms["SHA-512WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  69. algorithms["SHA512WITHRSA"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  70. algorithms["SHA-512WITHRSA"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
  71. algorithms["SHA512(224)WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512_224WithRSAEncryption;
  72. algorithms["SHA-512(224)WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512_224WithRSAEncryption;
  73. algorithms["SHA512(224)WITHRSA"] = PkcsObjectIdentifiers.Sha512_224WithRSAEncryption;
  74. algorithms["SHA-512(224)WITHRSA"] = PkcsObjectIdentifiers.Sha512_224WithRSAEncryption;
  75. algorithms["SHA512(256)WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512_256WithRSAEncryption;
  76. algorithms["SHA-512(256)WITHRSAENCRYPTION"] = PkcsObjectIdentifiers.Sha512_256WithRSAEncryption;
  77. algorithms["SHA512(256)WITHRSA"] = PkcsObjectIdentifiers.Sha512_256WithRSAEncryption;
  78. algorithms["SHA-512(256)WITHRSA"] = PkcsObjectIdentifiers.Sha512_256WithRSAEncryption;
  79. algorithms["SHA1WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  80. algorithms["SHA224WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  81. algorithms["SHA256WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  82. algorithms["SHA384WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  83. algorithms["SHA512WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  84. algorithms["SHA3-224WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  85. algorithms["SHA3-256WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  86. algorithms["SHA3-384WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  87. algorithms["SHA3-512WITHRSAANDMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
  88. algorithms["RIPEMD160WITHRSAENCRYPTION"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160;
  89. algorithms["RIPEMD160WITHRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160;
  90. algorithms["RIPEMD128WITHRSAENCRYPTION"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128;
  91. algorithms["RIPEMD128WITHRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128;
  92. algorithms["RIPEMD256WITHRSAENCRYPTION"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256;
  93. algorithms["RIPEMD256WITHRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256;
  94. algorithms["SHA1WITHDSA"] = X9ObjectIdentifiers.IdDsaWithSha1;
  95. algorithms["SHA-1WITHDSA"] = X9ObjectIdentifiers.IdDsaWithSha1;
  96. algorithms["DSAWITHSHA1"] = X9ObjectIdentifiers.IdDsaWithSha1;
  97. algorithms["SHA224WITHDSA"] = NistObjectIdentifiers.DsaWithSha224;
  98. algorithms["SHA256WITHDSA"] = NistObjectIdentifiers.DsaWithSha256;
  99. algorithms["SHA384WITHDSA"] = NistObjectIdentifiers.DsaWithSha384;
  100. algorithms["SHA512WITHDSA"] = NistObjectIdentifiers.DsaWithSha512;
  101. algorithms["SHA3-224WITHDSA"] = NistObjectIdentifiers.IdDsaWithSha3_224;
  102. algorithms["SHA3-256WITHDSA"] = NistObjectIdentifiers.IdDsaWithSha3_256;
  103. algorithms["SHA3-384WITHDSA"] = NistObjectIdentifiers.IdDsaWithSha3_384;
  104. algorithms["SHA3-512WITHDSA"] = NistObjectIdentifiers.IdDsaWithSha3_512;
  105. algorithms["SHA3-224WITHECDSA"] = NistObjectIdentifiers.IdEcdsaWithSha3_224;
  106. algorithms["SHA3-256WITHECDSA"] = NistObjectIdentifiers.IdEcdsaWithSha3_256;
  107. algorithms["SHA3-384WITHECDSA"] = NistObjectIdentifiers.IdEcdsaWithSha3_384;
  108. algorithms["SHA3-512WITHECDSA"] = NistObjectIdentifiers.IdEcdsaWithSha3_512;
  109. algorithms["SHA3-224WITHRSA"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224;
  110. algorithms["SHA3-256WITHRSA"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256;
  111. algorithms["SHA3-384WITHRSA"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384;
  112. algorithms["SHA3-512WITHRSA"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512;
  113. algorithms["SHA3-224WITHRSAENCRYPTION"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224;
  114. algorithms["SHA3-256WITHRSAENCRYPTION"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256;
  115. algorithms["SHA3-384WITHRSAENCRYPTION"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384;
  116. algorithms["SHA3-512WITHRSAENCRYPTION"] = NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512;
  117. algorithms["SHA1WITHECDSA"] = X9ObjectIdentifiers.ECDsaWithSha1;
  118. algorithms["ECDSAWITHSHA1"] = X9ObjectIdentifiers.ECDsaWithSha1;
  119. algorithms["SHA224WITHECDSA"] = X9ObjectIdentifiers.ECDsaWithSha224;
  120. algorithms["SHA256WITHECDSA"] = X9ObjectIdentifiers.ECDsaWithSha256;
  121. algorithms["SHA384WITHECDSA"] = X9ObjectIdentifiers.ECDsaWithSha384;
  122. algorithms["SHA512WITHECDSA"] = X9ObjectIdentifiers.ECDsaWithSha512;
  123. algorithms["GOST3411WITHGOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94;
  124. algorithms["GOST3411WITHGOST3410-94"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94;
  125. algorithms["GOST3411WITHECGOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
  126. algorithms["GOST3411WITHECGOST3410-2001"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
  127. algorithms["GOST3411WITHGOST3410-2001"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
  128. algorithms["GOST3411WITHECGOST3410-2012-256"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256;
  129. algorithms["GOST3411WITHECGOST3410-2012-512"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512;
  130. algorithms["GOST3411WITHGOST3410-2012-256"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256;
  131. algorithms["GOST3411WITHGOST3410-2012-512"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512;
  132. algorithms["GOST3411-2012-256WITHECGOST3410-2012-256"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256;
  133. algorithms["GOST3411-2012-512WITHECGOST3410-2012-512"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512;
  134. algorithms["GOST3411-2012-256WITHGOST3410-2012-256"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256;
  135. algorithms["GOST3411-2012-512WITHGOST3410-2012-512"] = RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512;
  136. algorithms["SHA1WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA1;
  137. algorithms["SHA224WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA224;
  138. algorithms["SHA256WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA256;
  139. algorithms["SHA384WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA384;
  140. algorithms["SHA512WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA512;
  141. algorithms["RIPEMD160WITHPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_RIPEMD160;
  142. algorithms["SHA1WITHCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_1;
  143. algorithms["SHA224WITHCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_224;
  144. algorithms["SHA256WITHCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_256;
  145. algorithms["SHA384WITHCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_384;
  146. algorithms["SHA512WITHCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_512;
  147. algorithms["SHA3-512WITHSPHINCS256"] = BCObjectIdentifiers.sphincs256_with_SHA3_512;
  148. algorithms["SHA512WITHSPHINCS256"] = BCObjectIdentifiers.sphincs256_with_SHA512;
  149. algorithms["SHA256WITHSM2"] = GMObjectIdentifiers.sm2sign_with_sha256;
  150. algorithms["SM3WITHSM2"] = GMObjectIdentifiers.sm2sign_with_sm3;
  151. algorithms["SHA256WITHXMSS"] = BCObjectIdentifiers.xmss_with_SHA256;
  152. algorithms["SHA512WITHXMSS"] = BCObjectIdentifiers.xmss_with_SHA512;
  153. algorithms["SHAKE128WITHXMSS"] = BCObjectIdentifiers.xmss_with_SHAKE128;
  154. algorithms["SHAKE256WITHXMSS"] = BCObjectIdentifiers.xmss_with_SHAKE256;
  155. algorithms["SHA256WITHXMSSMT"] = BCObjectIdentifiers.xmss_mt_with_SHA256;
  156. algorithms["SHA512WITHXMSSMT"] = BCObjectIdentifiers.xmss_mt_with_SHA512;
  157. algorithms["SHAKE128WITHXMSSMT"] = BCObjectIdentifiers.xmss_mt_with_SHAKE128;
  158. algorithms["SHAKE256WITHXMSSMT"] = BCObjectIdentifiers.xmss_mt_with_SHAKE256;
  159. //
  160. // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
  161. // The parameters field SHALL be NULL for RSA based signature algorithms.
  162. //
  163. noParams.Add((object)X9ObjectIdentifiers.ECDsaWithSha1);
  164. noParams.Add((object)X9ObjectIdentifiers.ECDsaWithSha224);
  165. noParams.Add((object)X9ObjectIdentifiers.ECDsaWithSha256);
  166. noParams.Add((object)X9ObjectIdentifiers.ECDsaWithSha384);
  167. noParams.Add((object)X9ObjectIdentifiers.ECDsaWithSha512);
  168. noParams.Add((object)X9ObjectIdentifiers.IdDsaWithSha1);
  169. noParams.Add((object)NistObjectIdentifiers.DsaWithSha224);
  170. noParams.Add((object)NistObjectIdentifiers.DsaWithSha256);
  171. noParams.Add((object)NistObjectIdentifiers.DsaWithSha384);
  172. noParams.Add((object)NistObjectIdentifiers.DsaWithSha512);
  173. noParams.Add((object)NistObjectIdentifiers.IdDsaWithSha3_224);
  174. noParams.Add((object)NistObjectIdentifiers.IdDsaWithSha3_256);
  175. noParams.Add((object)NistObjectIdentifiers.IdDsaWithSha3_384);
  176. noParams.Add((object)NistObjectIdentifiers.IdDsaWithSha3_512);
  177. noParams.Add((object)NistObjectIdentifiers.IdEcdsaWithSha3_224);
  178. noParams.Add((object)NistObjectIdentifiers.IdEcdsaWithSha3_256);
  179. noParams.Add((object)NistObjectIdentifiers.IdEcdsaWithSha3_384);
  180. noParams.Add((object)NistObjectIdentifiers.IdEcdsaWithSha3_512);
  181. //
  182. // RFC 4491
  183. //
  184. noParams.Add((object)CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
  185. noParams.Add((object)CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
  186. noParams.Add((object)RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256);
  187. noParams.Add((object)RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512);
  188. //
  189. // SPHINCS-256
  190. //
  191. noParams.Add((object)BCObjectIdentifiers.sphincs256_with_SHA512);
  192. noParams.Add((object)BCObjectIdentifiers.sphincs256_with_SHA3_512);
  193. //
  194. // XMSS
  195. //
  196. noParams.Add((object)BCObjectIdentifiers.xmss_with_SHA256);
  197. noParams.Add((object)BCObjectIdentifiers.xmss_with_SHA512);
  198. noParams.Add((object)BCObjectIdentifiers.xmss_with_SHAKE128);
  199. noParams.Add((object)BCObjectIdentifiers.xmss_with_SHAKE256);
  200. noParams.Add((object)BCObjectIdentifiers.xmss_mt_with_SHA256);
  201. noParams.Add((object)BCObjectIdentifiers.xmss_mt_with_SHA512);
  202. noParams.Add((object)BCObjectIdentifiers.xmss_mt_with_SHAKE128);
  203. noParams.Add((object)BCObjectIdentifiers.xmss_mt_with_SHAKE256);
  204. //
  205. // SM2
  206. //
  207. noParams.Add((object)GMObjectIdentifiers.sm2sign_with_sha256);
  208. noParams.Add((object)GMObjectIdentifiers.sm2sign_with_sm3);
  209. //
  210. // PKCS 1.5 encrypted algorithms
  211. //
  212. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha1WithRsaEncryption);
  213. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha224WithRsaEncryption);
  214. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha256WithRsaEncryption);
  215. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha384WithRsaEncryption);
  216. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha512WithRsaEncryption);
  217. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha512_224WithRSAEncryption);
  218. pkcs15RsaEncryption.Add((object)PkcsObjectIdentifiers.Sha512_256WithRSAEncryption);
  219. pkcs15RsaEncryption.Add((object)TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
  220. pkcs15RsaEncryption.Add((object)TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
  221. pkcs15RsaEncryption.Add((object)TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
  222. pkcs15RsaEncryption.Add((object)NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224);
  223. pkcs15RsaEncryption.Add((object)NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256);
  224. pkcs15RsaEncryption.Add((object)NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384);
  225. pkcs15RsaEncryption.Add((object)NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512);
  226. //
  227. // explicit params
  228. //
  229. AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  230. _params["SHA1WITHRSAANDMGF1"] = CreatePssParams(sha1AlgId, 20);
  231. AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
  232. _params["SHA224WITHRSAANDMGF1"] = CreatePssParams(sha224AlgId, 28);
  233. AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
  234. _params["SHA256WITHRSAANDMGF1"] = CreatePssParams(sha256AlgId, 32);
  235. AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
  236. _params["SHA384WITHRSAANDMGF1"] = CreatePssParams(sha384AlgId, 48);
  237. AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
  238. _params["SHA512WITHRSAANDMGF1"] = CreatePssParams(sha512AlgId, 64);
  239. AlgorithmIdentifier sha3_224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha3_224, DerNull.Instance);
  240. _params["SHA3-224WITHRSAANDMGF1"] = CreatePssParams(sha3_224AlgId, 28);
  241. AlgorithmIdentifier sha3_256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha3_256, DerNull.Instance);
  242. _params["SHA3-256WITHRSAANDMGF1"] = CreatePssParams(sha3_256AlgId, 32);
  243. AlgorithmIdentifier sha3_384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha3_384, DerNull.Instance);
  244. _params["SHA3-384WITHRSAANDMGF1"] = CreatePssParams(sha3_384AlgId, 48);
  245. AlgorithmIdentifier sha3_512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha3_512, DerNull.Instance);
  246. _params["SHA3-512WITHRSAANDMGF1"] = CreatePssParams(sha3_512AlgId, 64);
  247. //
  248. // digests
  249. //
  250. digestOids[PkcsObjectIdentifiers.Sha224WithRsaEncryption] = NistObjectIdentifiers.IdSha224;
  251. digestOids[PkcsObjectIdentifiers.Sha256WithRsaEncryption] = NistObjectIdentifiers.IdSha256;
  252. digestOids[PkcsObjectIdentifiers.Sha384WithRsaEncryption] = NistObjectIdentifiers.IdSha384;
  253. digestOids[PkcsObjectIdentifiers.Sha512WithRsaEncryption] = NistObjectIdentifiers.IdSha512;
  254. digestOids[PkcsObjectIdentifiers.Sha512_224WithRSAEncryption] = NistObjectIdentifiers.IdSha512_224;
  255. digestOids[PkcsObjectIdentifiers.Sha512_256WithRSAEncryption] = NistObjectIdentifiers.IdSha512_256;
  256. digestOids[NistObjectIdentifiers.DsaWithSha224] = NistObjectIdentifiers.IdSha224;
  257. digestOids[NistObjectIdentifiers.DsaWithSha256] = NistObjectIdentifiers.IdSha256;
  258. digestOids[NistObjectIdentifiers.DsaWithSha384] = NistObjectIdentifiers.IdSha384;
  259. digestOids[NistObjectIdentifiers.DsaWithSha512] = NistObjectIdentifiers.IdSha512;
  260. digestOids[NistObjectIdentifiers.IdDsaWithSha3_224] = NistObjectIdentifiers.IdSha3_224;
  261. digestOids[NistObjectIdentifiers.IdDsaWithSha3_256] = NistObjectIdentifiers.IdSha3_256;
  262. digestOids[NistObjectIdentifiers.IdDsaWithSha3_384] = NistObjectIdentifiers.IdSha3_384;
  263. digestOids[NistObjectIdentifiers.IdDsaWithSha3_512] = NistObjectIdentifiers.IdSha3_512;
  264. digestOids[NistObjectIdentifiers.IdEcdsaWithSha3_224] = NistObjectIdentifiers.IdSha3_224;
  265. digestOids[NistObjectIdentifiers.IdEcdsaWithSha3_256] = NistObjectIdentifiers.IdSha3_256;
  266. digestOids[NistObjectIdentifiers.IdEcdsaWithSha3_384] = NistObjectIdentifiers.IdSha3_384;
  267. digestOids[NistObjectIdentifiers.IdEcdsaWithSha3_512] = NistObjectIdentifiers.IdSha3_512;
  268. digestOids[NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224] = NistObjectIdentifiers.IdSha3_224;
  269. digestOids[NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256] = NistObjectIdentifiers.IdSha3_256;
  270. digestOids[NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384] = NistObjectIdentifiers.IdSha3_384;
  271. digestOids[NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512] = NistObjectIdentifiers.IdSha3_512;
  272. digestOids[PkcsObjectIdentifiers.MD2WithRsaEncryption] = PkcsObjectIdentifiers.MD2;
  273. digestOids[PkcsObjectIdentifiers.MD4WithRsaEncryption] = PkcsObjectIdentifiers.MD4;
  274. digestOids[PkcsObjectIdentifiers.MD5WithRsaEncryption] = PkcsObjectIdentifiers.MD5;
  275. digestOids[PkcsObjectIdentifiers.Sha1WithRsaEncryption] = OiwObjectIdentifiers.IdSha1;
  276. digestOids[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128] = TeleTrusTObjectIdentifiers.RipeMD128;
  277. digestOids[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160] = TeleTrusTObjectIdentifiers.RipeMD160;
  278. digestOids[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256] = TeleTrusTObjectIdentifiers.RipeMD256;
  279. digestOids[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94] = CryptoProObjectIdentifiers.GostR3411;
  280. digestOids[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001] = CryptoProObjectIdentifiers.GostR3411;
  281. digestOids[RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256;
  282. digestOids[RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512;
  283. digestOids[GMObjectIdentifiers.sm2sign_with_sha256] = NistObjectIdentifiers.IdSha256;
  284. digestOids[GMObjectIdentifiers.sm2sign_with_sm3] = GMObjectIdentifiers.sm3;
  285. }
  286. private static AlgorithmIdentifier Generate(string signatureAlgorithm)
  287. {
  288. AlgorithmIdentifier sigAlgId;
  289. AlgorithmIdentifier encAlgId;
  290. AlgorithmIdentifier digAlgId;
  291. string algorithmName = Strings.ToUpperCase(signatureAlgorithm);
  292. DerObjectIdentifier sigOID = (DerObjectIdentifier)algorithms[algorithmName];
  293. if (sigOID == null)
  294. {
  295. throw new ArgumentException("Unknown signature type requested: " + algorithmName);
  296. }
  297. if (noParams.Contains(sigOID))
  298. {
  299. sigAlgId = new AlgorithmIdentifier(sigOID);
  300. }
  301. else if (_params.Contains(algorithmName))
  302. {
  303. sigAlgId = new AlgorithmIdentifier(sigOID, (Asn1Encodable)_params[algorithmName]);
  304. }
  305. else
  306. {
  307. sigAlgId = new AlgorithmIdentifier(sigOID, DerNull.Instance);
  308. }
  309. if (pkcs15RsaEncryption.Contains(sigOID))
  310. {
  311. encAlgId = new AlgorithmIdentifier(PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance);
  312. }
  313. else
  314. {
  315. encAlgId = sigAlgId;
  316. }
  317. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  318. {
  319. digAlgId = ((RsassaPssParameters)sigAlgId.Parameters).HashAlgorithm;
  320. }
  321. else
  322. {
  323. digAlgId = new AlgorithmIdentifier((DerObjectIdentifier)digestOids[sigOID], DerNull.Instance);
  324. }
  325. return sigAlgId;
  326. }
  327. private static RsassaPssParameters CreatePssParams(AlgorithmIdentifier hashAlgId, int saltSize)
  328. {
  329. return new RsassaPssParameters(
  330. hashAlgId,
  331. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
  332. new DerInteger(saltSize),
  333. new DerInteger(1));
  334. }
  335. public AlgorithmIdentifier Find(string sigAlgName)
  336. {
  337. return Generate(sigAlgName);
  338. }
  339. }
  340. public class DefaultDigestAlgorithmIdentifierFinder
  341. {
  342. private static readonly IDictionary digestOids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  343. private static readonly IDictionary digestNameToOids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  344. static DefaultDigestAlgorithmIdentifierFinder()
  345. {
  346. //
  347. // digests
  348. //
  349. digestOids.Add(OiwObjectIdentifiers.MD4WithRsaEncryption, PkcsObjectIdentifiers.MD4);
  350. digestOids.Add(OiwObjectIdentifiers.MD4WithRsa, PkcsObjectIdentifiers.MD4);
  351. digestOids.Add(OiwObjectIdentifiers.MD5WithRsa, PkcsObjectIdentifiers.MD5);
  352. digestOids.Add(OiwObjectIdentifiers.Sha1WithRsa, OiwObjectIdentifiers.IdSha1);
  353. digestOids.Add(OiwObjectIdentifiers.DsaWithSha1, OiwObjectIdentifiers.IdSha1);
  354. digestOids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, NistObjectIdentifiers.IdSha224);
  355. digestOids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, NistObjectIdentifiers.IdSha256);
  356. digestOids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, NistObjectIdentifiers.IdSha384);
  357. digestOids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, NistObjectIdentifiers.IdSha512);
  358. digestOids.Add(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, NistObjectIdentifiers.IdSha512_224);
  359. digestOids.Add(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, NistObjectIdentifiers.IdSha512_256);
  360. digestOids.Add(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224, NistObjectIdentifiers.IdSha3_224);
  361. digestOids.Add(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256, NistObjectIdentifiers.IdSha3_256);
  362. digestOids.Add(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384, NistObjectIdentifiers.IdSha3_384);
  363. digestOids.Add(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512, NistObjectIdentifiers.IdSha3_512);
  364. digestOids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, PkcsObjectIdentifiers.MD2);
  365. digestOids.Add(PkcsObjectIdentifiers.MD4WithRsaEncryption, PkcsObjectIdentifiers.MD4);
  366. digestOids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, PkcsObjectIdentifiers.MD5);
  367. digestOids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, OiwObjectIdentifiers.IdSha1);
  368. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha1, OiwObjectIdentifiers.IdSha1);
  369. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha224, NistObjectIdentifiers.IdSha224);
  370. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha256, NistObjectIdentifiers.IdSha256);
  371. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha384, NistObjectIdentifiers.IdSha384);
  372. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha512, NistObjectIdentifiers.IdSha512);
  373. digestOids.Add(X9ObjectIdentifiers.IdDsaWithSha1, OiwObjectIdentifiers.IdSha1);
  374. digestOids.Add(NistObjectIdentifiers.DsaWithSha224, NistObjectIdentifiers.IdSha224);
  375. digestOids.Add(NistObjectIdentifiers.DsaWithSha256, NistObjectIdentifiers.IdSha256);
  376. digestOids.Add(NistObjectIdentifiers.DsaWithSha384, NistObjectIdentifiers.IdSha384);
  377. digestOids.Add(NistObjectIdentifiers.DsaWithSha512, NistObjectIdentifiers.IdSha512);
  378. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128, TeleTrusTObjectIdentifiers.RipeMD128);
  379. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160, TeleTrusTObjectIdentifiers.RipeMD160);
  380. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256, TeleTrusTObjectIdentifiers.RipeMD256);
  381. digestOids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, CryptoProObjectIdentifiers.GostR3411);
  382. digestOids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, CryptoProObjectIdentifiers.GostR3411);
  383. digestNameToOids.Add("SHA-1", OiwObjectIdentifiers.IdSha1);
  384. digestNameToOids.Add("SHA-224", NistObjectIdentifiers.IdSha224);
  385. digestNameToOids.Add("SHA-256", NistObjectIdentifiers.IdSha256);
  386. digestNameToOids.Add("SHA-384", NistObjectIdentifiers.IdSha384);
  387. digestNameToOids.Add("SHA-512", NistObjectIdentifiers.IdSha512);
  388. digestNameToOids.Add("SHA-512/224", NistObjectIdentifiers.IdSha512_224);
  389. digestNameToOids.Add("SHA-512(224)", NistObjectIdentifiers.IdSha512_224);
  390. digestNameToOids.Add("SHA-512/256", NistObjectIdentifiers.IdSha512_256);
  391. digestNameToOids.Add("SHA-512(256)", NistObjectIdentifiers.IdSha512_256);
  392. digestNameToOids.Add("SHA1", OiwObjectIdentifiers.IdSha1);
  393. digestNameToOids.Add("SHA224", NistObjectIdentifiers.IdSha224);
  394. digestNameToOids.Add("SHA256", NistObjectIdentifiers.IdSha256);
  395. digestNameToOids.Add("SHA384", NistObjectIdentifiers.IdSha384);
  396. digestNameToOids.Add("SHA512", NistObjectIdentifiers.IdSha512);
  397. digestNameToOids.Add("SHA512/224", NistObjectIdentifiers.IdSha512_224);
  398. digestNameToOids.Add("SHA512(224)", NistObjectIdentifiers.IdSha512_224);
  399. digestNameToOids.Add("SHA512/256", NistObjectIdentifiers.IdSha512_256);
  400. digestNameToOids.Add("SHA512(256)", NistObjectIdentifiers.IdSha512_256);
  401. digestNameToOids.Add("SHA3-224", NistObjectIdentifiers.IdSha3_224);
  402. digestNameToOids.Add("SHA3-256", NistObjectIdentifiers.IdSha3_256);
  403. digestNameToOids.Add("SHA3-384", NistObjectIdentifiers.IdSha3_384);
  404. digestNameToOids.Add("SHA3-512", NistObjectIdentifiers.IdSha3_512);
  405. digestNameToOids.Add("SHAKE-128", NistObjectIdentifiers.IdShake128);
  406. digestNameToOids.Add("SHAKE-256", NistObjectIdentifiers.IdShake256);
  407. digestNameToOids.Add("GOST3411", CryptoProObjectIdentifiers.GostR3411);
  408. digestNameToOids.Add("MD2", PkcsObjectIdentifiers.MD2);
  409. digestNameToOids.Add("MD4", PkcsObjectIdentifiers.MD4);
  410. digestNameToOids.Add("MD5", PkcsObjectIdentifiers.MD5);
  411. digestNameToOids.Add("RIPEMD128", TeleTrusTObjectIdentifiers.RipeMD128);
  412. digestNameToOids.Add("RIPEMD160", TeleTrusTObjectIdentifiers.RipeMD160);
  413. digestNameToOids.Add("RIPEMD256", TeleTrusTObjectIdentifiers.RipeMD256);
  414. }
  415. public AlgorithmIdentifier find(AlgorithmIdentifier sigAlgId)
  416. {
  417. AlgorithmIdentifier digAlgId;
  418. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  419. {
  420. digAlgId = RsassaPssParameters.GetInstance(sigAlgId.Parameters).HashAlgorithm;
  421. }
  422. else
  423. {
  424. digAlgId = new AlgorithmIdentifier((DerObjectIdentifier)digestOids[sigAlgId.Algorithm], DerNull.Instance);
  425. }
  426. return digAlgId;
  427. }
  428. public AlgorithmIdentifier find(string digAlgName)
  429. {
  430. return new AlgorithmIdentifier((DerObjectIdentifier)digestNameToOids[digAlgName], DerNull.Instance);
  431. }
  432. }
  433. public class CmsSignedGenerator
  434. {
  435. /**
  436. * Default type for the signed data.
  437. */
  438. public static readonly string Data = CmsObjectIdentifiers.Data.Id;
  439. public static readonly string DigestSha1 = OiwObjectIdentifiers.IdSha1.Id;
  440. public static readonly string DigestSha224 = NistObjectIdentifiers.IdSha224.Id;
  441. public static readonly string DigestSha256 = NistObjectIdentifiers.IdSha256.Id;
  442. public static readonly string DigestSha384 = NistObjectIdentifiers.IdSha384.Id;
  443. public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id;
  444. public static readonly string DigestSha512_224 = NistObjectIdentifiers.IdSha512_224.Id;
  445. public static readonly string DigestSha512_256 = NistObjectIdentifiers.IdSha512_256.Id;
  446. public static readonly string DigestMD5 = PkcsObjectIdentifiers.MD5.Id;
  447. public static readonly string DigestGost3411 = CryptoProObjectIdentifiers.GostR3411.Id;
  448. public static readonly string DigestRipeMD128 = TeleTrusTObjectIdentifiers.RipeMD128.Id;
  449. public static readonly string DigestRipeMD160 = TeleTrusTObjectIdentifiers.RipeMD160.Id;
  450. public static readonly string DigestRipeMD256 = TeleTrusTObjectIdentifiers.RipeMD256.Id;
  451. public static readonly string EncryptionRsa = PkcsObjectIdentifiers.RsaEncryption.Id;
  452. public static readonly string EncryptionDsa = X9ObjectIdentifiers.IdDsaWithSha1.Id;
  453. public static readonly string EncryptionECDsa = X9ObjectIdentifiers.ECDsaWithSha1.Id;
  454. public static readonly string EncryptionRsaPss = PkcsObjectIdentifiers.IdRsassaPss.Id;
  455. public static readonly string EncryptionGost3410 = CryptoProObjectIdentifiers.GostR3410x94.Id;
  456. public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id;
  457. internal IList _certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  458. internal IList _crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  459. internal IList _signers = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  460. internal IDictionary _digests = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  461. internal bool _useDerForCerts = false;
  462. internal bool _useDerForCrls = false;
  463. protected readonly SecureRandom rand;
  464. protected CmsSignedGenerator()
  465. : this(new SecureRandom())
  466. {
  467. }
  468. /// <summary>Constructor allowing specific source of randomness</summary>
  469. /// <param name="rand">Instance of <c>SecureRandom</c> to use.</param>
  470. protected CmsSignedGenerator(
  471. SecureRandom rand)
  472. {
  473. this.rand = rand;
  474. }
  475. internal protected virtual IDictionary GetBaseParameters(
  476. DerObjectIdentifier contentType,
  477. AlgorithmIdentifier digAlgId,
  478. byte[] hash)
  479. {
  480. IDictionary param = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  481. if (contentType != null)
  482. {
  483. param[CmsAttributeTableParameter.ContentType] = contentType;
  484. }
  485. param[CmsAttributeTableParameter.DigestAlgorithmIdentifier] = digAlgId;
  486. param[CmsAttributeTableParameter.Digest] = hash.Clone();
  487. return param;
  488. }
  489. internal protected virtual Asn1Set GetAttributeSet(
  490. Asn1.Cms.AttributeTable attr)
  491. {
  492. return attr == null
  493. ? null
  494. : new DerSet(attr.ToAsn1EncodableVector());
  495. }
  496. public void AddCertificates(
  497. IX509Store certStore)
  498. {
  499. CollectionUtilities.AddRange(_certs, CmsUtilities.GetCertificatesFromStore(certStore));
  500. }
  501. public void AddCrls(
  502. IX509Store crlStore)
  503. {
  504. CollectionUtilities.AddRange(_crls, CmsUtilities.GetCrlsFromStore(crlStore));
  505. }
  506. /**
  507. * Add the attribute certificates contained in the passed in store to the
  508. * generator.
  509. *
  510. * @param store a store of Version 2 attribute certificates
  511. * @throws CmsException if an error occurse processing the store.
  512. */
  513. public void AddAttributeCertificates(
  514. IX509Store store)
  515. {
  516. try
  517. {
  518. foreach (IX509AttributeCertificate attrCert in store.GetMatches(null))
  519. {
  520. _certs.Add(new DerTaggedObject(false, 2,
  521. AttributeCertificate.GetInstance(Asn1Object.FromByteArray(attrCert.GetEncoded()))));
  522. }
  523. }
  524. catch (Exception e)
  525. {
  526. throw new CmsException("error processing attribute certs", e);
  527. }
  528. }
  529. /**
  530. * Add a store of precalculated signers to the generator.
  531. *
  532. * @param signerStore store of signers
  533. */
  534. public void AddSigners(
  535. SignerInformationStore signerStore)
  536. {
  537. foreach (SignerInformation o in signerStore.GetSigners())
  538. {
  539. _signers.Add(o);
  540. AddSignerCallback(o);
  541. }
  542. }
  543. /**
  544. * Return a map of oids and byte arrays representing the digests calculated on the content during
  545. * the last generate.
  546. *
  547. * @return a map of oids (as string objects) and byte[] representing digests.
  548. */
  549. public IDictionary GetGeneratedDigests()
  550. {
  551. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(_digests);
  552. }
  553. public bool UseDerForCerts
  554. {
  555. get { return _useDerForCerts; }
  556. set { this._useDerForCerts = value; }
  557. }
  558. public bool UseDerForCrls
  559. {
  560. get { return _useDerForCrls; }
  561. set { this._useDerForCrls = value; }
  562. }
  563. internal virtual void AddSignerCallback(
  564. SignerInformation si)
  565. {
  566. }
  567. internal static SignerIdentifier GetSignerIdentifier(X509Certificate cert)
  568. {
  569. return new SignerIdentifier(CmsUtilities.GetIssuerAndSerialNumber(cert));
  570. }
  571. internal static SignerIdentifier GetSignerIdentifier(byte[] subjectKeyIdentifier)
  572. {
  573. return new SignerIdentifier(new DerOctetString(subjectKeyIdentifier));
  574. }
  575. }
  576. }
  577. #pragma warning restore
  578. #endif