CMSSignedGenerator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  20. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  21. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  22. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  23. {
  24. public class DefaultDigestAlgorithmIdentifierFinder
  25. {
  26. private static readonly IDictionary digestOids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  27. private static readonly IDictionary digestNameToOids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  28. static DefaultDigestAlgorithmIdentifierFinder()
  29. {
  30. //
  31. // digests
  32. //
  33. digestOids.Add(OiwObjectIdentifiers.MD4WithRsaEncryption, PkcsObjectIdentifiers.MD4);
  34. digestOids.Add(OiwObjectIdentifiers.MD4WithRsa, PkcsObjectIdentifiers.MD4);
  35. digestOids.Add(OiwObjectIdentifiers.MD5WithRsa, PkcsObjectIdentifiers.MD5);
  36. digestOids.Add(OiwObjectIdentifiers.Sha1WithRsa, OiwObjectIdentifiers.IdSha1);
  37. digestOids.Add(OiwObjectIdentifiers.DsaWithSha1, OiwObjectIdentifiers.IdSha1);
  38. digestOids.Add(PkcsObjectIdentifiers.Sha224WithRsaEncryption, NistObjectIdentifiers.IdSha224);
  39. digestOids.Add(PkcsObjectIdentifiers.Sha256WithRsaEncryption, NistObjectIdentifiers.IdSha256);
  40. digestOids.Add(PkcsObjectIdentifiers.Sha384WithRsaEncryption, NistObjectIdentifiers.IdSha384);
  41. digestOids.Add(PkcsObjectIdentifiers.Sha512WithRsaEncryption, NistObjectIdentifiers.IdSha512);
  42. digestOids.Add(PkcsObjectIdentifiers.MD2WithRsaEncryption, PkcsObjectIdentifiers.MD2);
  43. digestOids.Add(PkcsObjectIdentifiers.MD4WithRsaEncryption, PkcsObjectIdentifiers.MD4);
  44. digestOids.Add(PkcsObjectIdentifiers.MD5WithRsaEncryption, PkcsObjectIdentifiers.MD5);
  45. digestOids.Add(PkcsObjectIdentifiers.Sha1WithRsaEncryption, OiwObjectIdentifiers.IdSha1);
  46. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha1, OiwObjectIdentifiers.IdSha1);
  47. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha224, NistObjectIdentifiers.IdSha224);
  48. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha256, NistObjectIdentifiers.IdSha256);
  49. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha384, NistObjectIdentifiers.IdSha384);
  50. digestOids.Add(X9ObjectIdentifiers.ECDsaWithSha512, NistObjectIdentifiers.IdSha512);
  51. digestOids.Add(X9ObjectIdentifiers.IdDsaWithSha1, OiwObjectIdentifiers.IdSha1);
  52. digestOids.Add(NistObjectIdentifiers.DsaWithSha224, NistObjectIdentifiers.IdSha224);
  53. digestOids.Add(NistObjectIdentifiers.DsaWithSha256, NistObjectIdentifiers.IdSha256);
  54. digestOids.Add(NistObjectIdentifiers.DsaWithSha384, NistObjectIdentifiers.IdSha384);
  55. digestOids.Add(NistObjectIdentifiers.DsaWithSha512, NistObjectIdentifiers.IdSha512);
  56. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128, TeleTrusTObjectIdentifiers.RipeMD128);
  57. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160, TeleTrusTObjectIdentifiers.RipeMD160);
  58. digestOids.Add(TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256, TeleTrusTObjectIdentifiers.RipeMD256);
  59. digestOids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, CryptoProObjectIdentifiers.GostR3411);
  60. digestOids.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, CryptoProObjectIdentifiers.GostR3411);
  61. digestNameToOids.Add("SHA-1", OiwObjectIdentifiers.IdSha1);
  62. digestNameToOids.Add("SHA-224", NistObjectIdentifiers.IdSha224);
  63. digestNameToOids.Add("SHA-256", NistObjectIdentifiers.IdSha256);
  64. digestNameToOids.Add("SHA-384", NistObjectIdentifiers.IdSha384);
  65. digestNameToOids.Add("SHA-512", NistObjectIdentifiers.IdSha512);
  66. digestNameToOids.Add("SHA1", OiwObjectIdentifiers.IdSha1);
  67. digestNameToOids.Add("SHA224", NistObjectIdentifiers.IdSha224);
  68. digestNameToOids.Add("SHA256", NistObjectIdentifiers.IdSha256);
  69. digestNameToOids.Add("SHA384", NistObjectIdentifiers.IdSha384);
  70. digestNameToOids.Add("SHA512", NistObjectIdentifiers.IdSha512);
  71. digestNameToOids.Add("SHA3-224", NistObjectIdentifiers.IdSha3_224);
  72. digestNameToOids.Add("SHA3-256", NistObjectIdentifiers.IdSha3_256);
  73. digestNameToOids.Add("SHA3-384", NistObjectIdentifiers.IdSha3_384);
  74. digestNameToOids.Add("SHA3-512", NistObjectIdentifiers.IdSha3_512);
  75. digestNameToOids.Add("SHAKE-128", NistObjectIdentifiers.IdShake128);
  76. digestNameToOids.Add("SHAKE-256", NistObjectIdentifiers.IdShake256);
  77. digestNameToOids.Add("GOST3411", CryptoProObjectIdentifiers.GostR3411);
  78. digestNameToOids.Add("MD2", PkcsObjectIdentifiers.MD2);
  79. digestNameToOids.Add("MD4", PkcsObjectIdentifiers.MD4);
  80. digestNameToOids.Add("MD5", PkcsObjectIdentifiers.MD5);
  81. digestNameToOids.Add("RIPEMD128", TeleTrusTObjectIdentifiers.RipeMD128);
  82. digestNameToOids.Add("RIPEMD160", TeleTrusTObjectIdentifiers.RipeMD160);
  83. digestNameToOids.Add("RIPEMD256", TeleTrusTObjectIdentifiers.RipeMD256);
  84. }
  85. public AlgorithmIdentifier find(AlgorithmIdentifier sigAlgId)
  86. {
  87. AlgorithmIdentifier digAlgId;
  88. if (sigAlgId.Algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss))
  89. {
  90. digAlgId = RsassaPssParameters.GetInstance(sigAlgId.Parameters).HashAlgorithm;
  91. }
  92. else
  93. {
  94. digAlgId = new AlgorithmIdentifier((DerObjectIdentifier)digestOids[sigAlgId.Algorithm], DerNull.Instance);
  95. }
  96. return digAlgId;
  97. }
  98. public AlgorithmIdentifier find(String digAlgName)
  99. {
  100. return new AlgorithmIdentifier((DerObjectIdentifier)digestNameToOids[digAlgName], DerNull.Instance);
  101. }
  102. }
  103. public class CmsSignedGenerator
  104. {
  105. /**
  106. * Default type for the signed data.
  107. */
  108. public static readonly string Data = CmsObjectIdentifiers.Data.Id;
  109. public static readonly string DigestSha1 = OiwObjectIdentifiers.IdSha1.Id;
  110. public static readonly string DigestSha224 = NistObjectIdentifiers.IdSha224.Id;
  111. public static readonly string DigestSha256 = NistObjectIdentifiers.IdSha256.Id;
  112. public static readonly string DigestSha384 = NistObjectIdentifiers.IdSha384.Id;
  113. public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id;
  114. public static readonly string DigestMD5 = PkcsObjectIdentifiers.MD5.Id;
  115. public static readonly string DigestGost3411 = CryptoProObjectIdentifiers.GostR3411.Id;
  116. public static readonly string DigestRipeMD128 = TeleTrusTObjectIdentifiers.RipeMD128.Id;
  117. public static readonly string DigestRipeMD160 = TeleTrusTObjectIdentifiers.RipeMD160.Id;
  118. public static readonly string DigestRipeMD256 = TeleTrusTObjectIdentifiers.RipeMD256.Id;
  119. public static readonly string EncryptionRsa = PkcsObjectIdentifiers.RsaEncryption.Id;
  120. public static readonly string EncryptionDsa = X9ObjectIdentifiers.IdDsaWithSha1.Id;
  121. public static readonly string EncryptionECDsa = X9ObjectIdentifiers.ECDsaWithSha1.Id;
  122. public static readonly string EncryptionRsaPss = PkcsObjectIdentifiers.IdRsassaPss.Id;
  123. public static readonly string EncryptionGost3410 = CryptoProObjectIdentifiers.GostR3410x94.Id;
  124. public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id;
  125. internal IList _certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  126. internal IList _crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  127. internal IList _signers = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  128. internal IDictionary _digests = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  129. internal bool _useDerForCerts = false;
  130. internal bool _useDerForCrls = false;
  131. protected readonly SecureRandom rand;
  132. protected CmsSignedGenerator()
  133. : this(new SecureRandom())
  134. {
  135. }
  136. /// <summary>Constructor allowing specific source of randomness</summary>
  137. /// <param name="rand">Instance of <c>SecureRandom</c> to use.</param>
  138. protected CmsSignedGenerator(
  139. SecureRandom rand)
  140. {
  141. this.rand = rand;
  142. }
  143. internal protected virtual IDictionary GetBaseParameters(
  144. DerObjectIdentifier contentType,
  145. AlgorithmIdentifier digAlgId,
  146. byte[] hash)
  147. {
  148. IDictionary param = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  149. if (contentType != null)
  150. {
  151. param[CmsAttributeTableParameter.ContentType] = contentType;
  152. }
  153. param[CmsAttributeTableParameter.DigestAlgorithmIdentifier] = digAlgId;
  154. param[CmsAttributeTableParameter.Digest] = hash.Clone();
  155. return param;
  156. }
  157. internal protected virtual Asn1Set GetAttributeSet(
  158. Asn1.Cms.AttributeTable attr)
  159. {
  160. return attr == null
  161. ? null
  162. : new DerSet(attr.ToAsn1EncodableVector());
  163. }
  164. public void AddCertificates(
  165. IX509Store certStore)
  166. {
  167. CollectionUtilities.AddRange(_certs, CmsUtilities.GetCertificatesFromStore(certStore));
  168. }
  169. public void AddCrls(
  170. IX509Store crlStore)
  171. {
  172. CollectionUtilities.AddRange(_crls, CmsUtilities.GetCrlsFromStore(crlStore));
  173. }
  174. /**
  175. * Add the attribute certificates contained in the passed in store to the
  176. * generator.
  177. *
  178. * @param store a store of Version 2 attribute certificates
  179. * @throws CmsException if an error occurse processing the store.
  180. */
  181. public void AddAttributeCertificates(
  182. IX509Store store)
  183. {
  184. try
  185. {
  186. foreach (IX509AttributeCertificate attrCert in store.GetMatches(null))
  187. {
  188. _certs.Add(new DerTaggedObject(false, 2,
  189. AttributeCertificate.GetInstance(Asn1Object.FromByteArray(attrCert.GetEncoded()))));
  190. }
  191. }
  192. catch (Exception e)
  193. {
  194. throw new CmsException("error processing attribute certs", e);
  195. }
  196. }
  197. /**
  198. * Add a store of precalculated signers to the generator.
  199. *
  200. * @param signerStore store of signers
  201. */
  202. public void AddSigners(
  203. SignerInformationStore signerStore)
  204. {
  205. foreach (SignerInformation o in signerStore.GetSigners())
  206. {
  207. _signers.Add(o);
  208. AddSignerCallback(o);
  209. }
  210. }
  211. /**
  212. * Return a map of oids and byte arrays representing the digests calculated on the content during
  213. * the last generate.
  214. *
  215. * @return a map of oids (as String objects) and byte[] representing digests.
  216. */
  217. public IDictionary GetGeneratedDigests()
  218. {
  219. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(_digests);
  220. }
  221. public bool UseDerForCerts
  222. {
  223. get { return _useDerForCerts; }
  224. set { this._useDerForCerts = value; }
  225. }
  226. public bool UseDerForCrls
  227. {
  228. get { return _useDerForCrls; }
  229. set { this._useDerForCrls = value; }
  230. }
  231. internal virtual void AddSignerCallback(
  232. SignerInformation si)
  233. {
  234. }
  235. internal static SignerIdentifier GetSignerIdentifier(X509Certificate cert)
  236. {
  237. return new SignerIdentifier(CmsUtilities.GetIssuerAndSerialNumber(cert));
  238. }
  239. internal static SignerIdentifier GetSignerIdentifier(byte[] subjectKeyIdentifier)
  240. {
  241. return new SignerIdentifier(new DerOctetString(subjectKeyIdentifier));
  242. }
  243. }
  244. }
  245. #pragma warning restore
  246. #endif