CMSSignedHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Eac;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Esf;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  16. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  17. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  18. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  19. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  20. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  21. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  22. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  23. {
  24. internal class CmsSignedHelper
  25. {
  26. internal static readonly CmsSignedHelper Instance = new CmsSignedHelper();
  27. private static readonly string EncryptionECDsaWithSha1 = X9ObjectIdentifiers.ECDsaWithSha1.Id;
  28. private static readonly string EncryptionECDsaWithSha224 = X9ObjectIdentifiers.ECDsaWithSha224.Id;
  29. private static readonly string EncryptionECDsaWithSha256 = X9ObjectIdentifiers.ECDsaWithSha256.Id;
  30. private static readonly string EncryptionECDsaWithSha384 = X9ObjectIdentifiers.ECDsaWithSha384.Id;
  31. private static readonly string EncryptionECDsaWithSha512 = X9ObjectIdentifiers.ECDsaWithSha512.Id;
  32. private static readonly IDictionary<string, string> m_encryptionAlgs = new Dictionary<string, string>();
  33. private static readonly IDictionary<string, string> m_digestAlgs = new Dictionary<string, string>();
  34. private static readonly IDictionary<string, string[]> m_digestAliases = new Dictionary<string, string[]>();
  35. private static readonly HashSet<string> noParams = new HashSet<string>();
  36. private static readonly IDictionary<string, string> m_ecAlgorithms = new Dictionary<string, string>();
  37. private static void AddEntries(DerObjectIdentifier oid, string digest, string encryption)
  38. {
  39. string alias = oid.Id;
  40. m_digestAlgs.Add(alias, digest);
  41. m_encryptionAlgs.Add(alias, encryption);
  42. }
  43. static CmsSignedHelper()
  44. {
  45. AddEntries(NistObjectIdentifiers.DsaWithSha224, "SHA224", "DSA");
  46. AddEntries(NistObjectIdentifiers.DsaWithSha256, "SHA256", "DSA");
  47. AddEntries(NistObjectIdentifiers.DsaWithSha384, "SHA384", "DSA");
  48. AddEntries(NistObjectIdentifiers.DsaWithSha512, "SHA512", "DSA");
  49. AddEntries(OiwObjectIdentifiers.DsaWithSha1, "SHA1", "DSA");
  50. AddEntries(OiwObjectIdentifiers.MD4WithRsa, "MD4", "RSA");
  51. AddEntries(OiwObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
  52. AddEntries(OiwObjectIdentifiers.MD5WithRsa, "MD5", "RSA");
  53. AddEntries(OiwObjectIdentifiers.Sha1WithRsa, "SHA1", "RSA");
  54. AddEntries(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2", "RSA");
  55. AddEntries(PkcsObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
  56. AddEntries(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5", "RSA");
  57. AddEntries(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1", "RSA");
  58. AddEntries(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224", "RSA");
  59. AddEntries(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256", "RSA");
  60. AddEntries(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384", "RSA");
  61. AddEntries(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512", "RSA");
  62. AddEntries(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)", "RSA");
  63. AddEntries(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)", "RSA");
  64. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224, "SHA3-224", "RSA");
  65. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256, "SHA3-256", "RSA");
  66. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384, "SHA3-384", "RSA");
  67. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512, "SHA3-512", "RSA");
  68. AddEntries(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1", "ECDSA");
  69. AddEntries(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224", "ECDSA");
  70. AddEntries(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256", "ECDSA");
  71. AddEntries(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384", "ECDSA");
  72. AddEntries(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512", "ECDSA");
  73. AddEntries(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1", "DSA");
  74. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA");
  75. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA");
  76. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA");
  77. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA");
  78. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA");
  79. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA");
  80. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA");
  81. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1");
  82. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1");
  83. AddEntries(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411", "GOST3410");
  84. AddEntries(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411", "ECGOST3410");
  85. AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256, "GOST3411-2012-256", "ECGOST3410");
  86. AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512, "GOST3411-2012-512", "ECGOST3410");
  87. m_encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa.Id, "DSA");
  88. m_encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption.Id, "RSA");
  89. m_encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm.Id, "RSA");
  90. m_encryptionAlgs.Add(X509ObjectIdentifiers.IdEARsa.Id, "RSA");
  91. m_encryptionAlgs.Add(CmsSignedGenerator.EncryptionRsaPss, "RSAandMGF1");
  92. m_encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x94.Id, "GOST3410");
  93. m_encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x2001.Id, "ECGOST3410");
  94. m_encryptionAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256.Id, "ECGOST3410");
  95. m_encryptionAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512.Id, "ECGOST3410");
  96. m_encryptionAlgs.Add("1.3.6.1.4.1.5849.1.6.2", "ECGOST3410");
  97. m_encryptionAlgs.Add("1.3.6.1.4.1.5849.1.1.5", "GOST3410");
  98. m_digestAlgs.Add(PkcsObjectIdentifiers.MD2.Id, "MD2");
  99. m_digestAlgs.Add(PkcsObjectIdentifiers.MD4.Id, "MD4");
  100. m_digestAlgs.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
  101. m_digestAlgs.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
  102. m_digestAlgs.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
  103. m_digestAlgs.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
  104. m_digestAlgs.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
  105. m_digestAlgs.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
  106. m_digestAlgs.Add(NistObjectIdentifiers.IdSha512_224.Id, "SHA512(224)");
  107. m_digestAlgs.Add(NistObjectIdentifiers.IdSha512_256.Id, "SHA512(256)");
  108. m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_224.Id, "SHA3-224");
  109. m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_256.Id, "SHA3-256");
  110. m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_384.Id, "SHA3-384");
  111. m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_512.Id, "SHA3-512");
  112. m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
  113. m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
  114. m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
  115. m_digestAlgs.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
  116. m_digestAlgs.Add("1.3.6.1.4.1.5849.1.2.1", "GOST3411");
  117. m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id, "GOST3411-2012-256");
  118. m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id, "GOST3411-2012-512");
  119. m_digestAliases.Add("SHA1", new string[]{ "SHA-1" });
  120. m_digestAliases.Add("SHA224", new string[]{ "SHA-224" });
  121. m_digestAliases.Add("SHA256", new string[]{ "SHA-256" });
  122. m_digestAliases.Add("SHA384", new string[]{ "SHA-384" });
  123. m_digestAliases.Add("SHA512", new string[]{ "SHA-512" });
  124. noParams.Add(CmsSignedGenerator.EncryptionDsa);
  125. //noParams.Add(EncryptionECDsa);
  126. noParams.Add(EncryptionECDsaWithSha1);
  127. noParams.Add(EncryptionECDsaWithSha224);
  128. noParams.Add(EncryptionECDsaWithSha256);
  129. noParams.Add(EncryptionECDsaWithSha384);
  130. noParams.Add(EncryptionECDsaWithSha512);
  131. m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha1, EncryptionECDsaWithSha1);
  132. m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha224, EncryptionECDsaWithSha224);
  133. m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha256, EncryptionECDsaWithSha256);
  134. m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha384, EncryptionECDsaWithSha384);
  135. m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha512, EncryptionECDsaWithSha512);
  136. }
  137. /**
  138. * Return the digest algorithm using one of the standard JCA string
  139. * representations rather than the algorithm identifier (if possible).
  140. */
  141. internal string GetDigestAlgName(string digestAlgOid)
  142. {
  143. return m_digestAlgs.TryGetValue(digestAlgOid, out var algName) ? algName : digestAlgOid;
  144. }
  145. internal AlgorithmIdentifier GetEncAlgorithmIdentifier(DerObjectIdentifier encOid,
  146. Asn1Encodable sigX509Parameters)
  147. {
  148. if (noParams.Contains(encOid.Id))
  149. {
  150. return new AlgorithmIdentifier(encOid);
  151. }
  152. return new AlgorithmIdentifier(encOid, sigX509Parameters);
  153. }
  154. internal string[] GetDigestAliases(string algName)
  155. {
  156. return m_digestAliases.TryGetValue(algName, out var aliases) ? (string[])aliases.Clone() : new string[0];
  157. }
  158. /**
  159. * Return the digest encryption algorithm using one of the standard
  160. * JCA string representations rather than the algorithm identifier (if
  161. * possible).
  162. */
  163. internal string GetEncryptionAlgName(string encryptionAlgOid)
  164. {
  165. return m_encryptionAlgs.TryGetValue(encryptionAlgOid, out var algName) ? algName : encryptionAlgOid;
  166. }
  167. internal IDigest GetDigestInstance(
  168. string algorithm)
  169. {
  170. try
  171. {
  172. return DigestUtilities.GetDigest(algorithm);
  173. }
  174. catch (SecurityUtilityException e)
  175. {
  176. // This is probably superfluous on C#, since no provider infrastructure,
  177. // assuming DigestUtilities already knows all the aliases
  178. foreach (string alias in GetDigestAliases(algorithm))
  179. {
  180. try { return DigestUtilities.GetDigest(alias); }
  181. catch (SecurityUtilityException) {}
  182. }
  183. throw e;
  184. }
  185. }
  186. internal ISigner GetSignatureInstance(
  187. string algorithm)
  188. {
  189. return SignerUtilities.GetSigner(algorithm);
  190. }
  191. internal AlgorithmIdentifier FixAlgID(
  192. AlgorithmIdentifier algId)
  193. {
  194. if (algId.Parameters == null)
  195. return new AlgorithmIdentifier(algId.Algorithm, DerNull.Instance);
  196. return algId;
  197. }
  198. internal string GetEncOid(
  199. AsymmetricKeyParameter key,
  200. string digestOID)
  201. {
  202. string encOID = null;
  203. if (key is RsaKeyParameters rsaKeyParameters)
  204. {
  205. if (!rsaKeyParameters.IsPrivate)
  206. throw new ArgumentException("Expected RSA private key");
  207. encOID = CmsSignedGenerator.EncryptionRsa;
  208. }
  209. else if (key is DsaPrivateKeyParameters)
  210. {
  211. if (digestOID.Equals(CmsSignedGenerator.DigestSha1))
  212. {
  213. encOID = CmsSignedGenerator.EncryptionDsa;
  214. }
  215. else if (digestOID.Equals(CmsSignedGenerator.DigestSha224))
  216. {
  217. encOID = NistObjectIdentifiers.DsaWithSha224.Id;
  218. }
  219. else if (digestOID.Equals(CmsSignedGenerator.DigestSha256))
  220. {
  221. encOID = NistObjectIdentifiers.DsaWithSha256.Id;
  222. }
  223. else if (digestOID.Equals(CmsSignedGenerator.DigestSha384))
  224. {
  225. encOID = NistObjectIdentifiers.DsaWithSha384.Id;
  226. }
  227. else if (digestOID.Equals(CmsSignedGenerator.DigestSha512))
  228. {
  229. encOID = NistObjectIdentifiers.DsaWithSha512.Id;
  230. }
  231. else
  232. {
  233. throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2");
  234. }
  235. }
  236. else if (key is ECPrivateKeyParameters ecPrivKey)
  237. {
  238. string algName = ecPrivKey.AlgorithmName;
  239. if (algName == "ECGOST3410")
  240. {
  241. encOID = CmsSignedGenerator.EncryptionECGost3410;
  242. }
  243. else if (ecPrivKey.Parameters is ECGost3410Parameters ecGost3410Parameters)
  244. {
  245. var digestParamSet = ecGost3410Parameters.DigestParamSet;
  246. if (digestParamSet.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256))
  247. {
  248. encOID = CmsSignedGenerator.EncryptionECGost3410_2012_256;
  249. }
  250. else if (digestParamSet.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512))
  251. {
  252. encOID = CmsSignedGenerator.EncryptionECGost3410_2012_512;
  253. }
  254. else
  255. {
  256. throw new ArgumentException("can't determine GOST3410 algorithm");
  257. }
  258. }
  259. else
  260. {
  261. // TODO Should we insist on algName being one of "EC" or "ECDSA", as Java does?
  262. if (!m_ecAlgorithms.TryGetValue(digestOID, out encOID))
  263. throw new ArgumentException("can't mix ECDSA with anything but SHA family digests");
  264. }
  265. }
  266. else if (key is Gost3410PrivateKeyParameters)
  267. {
  268. encOID = CmsSignedGenerator.EncryptionGost3410;
  269. }
  270. else
  271. {
  272. throw new ArgumentException("Unknown algorithm in CmsSignedGenerator.GetEncOid");
  273. }
  274. return encOID;
  275. }
  276. internal IStore<X509V2AttributeCertificate> GetAttributeCertificates(Asn1Set attrCertSet)
  277. {
  278. var contents = new List<X509V2AttributeCertificate>();
  279. if (attrCertSet != null)
  280. {
  281. foreach (Asn1Encodable ae in attrCertSet)
  282. {
  283. if (ae != null && ae.ToAsn1Object() is Asn1TaggedObject t)
  284. {
  285. if (t.HasContextTag(2))
  286. {
  287. Asn1Sequence s = Asn1Sequence.GetInstance(t, false);
  288. contents.Add(new X509V2AttributeCertificate(AttributeCertificate.GetInstance(s)));
  289. }
  290. }
  291. }
  292. }
  293. return CollectionUtilities.CreateStore(contents);
  294. }
  295. internal IStore<X509Certificate> GetCertificates(Asn1Set certSet)
  296. {
  297. var contents = new List<X509Certificate>();
  298. if (certSet != null)
  299. {
  300. foreach (Asn1Encodable ae in certSet)
  301. {
  302. if (ae != null && ae.ToAsn1Object() is Asn1Sequence s)
  303. {
  304. contents.Add(new X509Certificate(X509CertificateStructure.GetInstance(s)));
  305. }
  306. }
  307. }
  308. return CollectionUtilities.CreateStore(contents);
  309. }
  310. internal IStore<X509Crl> GetCrls(Asn1Set crlSet)
  311. {
  312. var contents = new List<X509Crl>();
  313. if (crlSet != null)
  314. {
  315. foreach (Asn1Encodable ae in crlSet)
  316. {
  317. if (ae != null && ae.ToAsn1Object() is Asn1Sequence s)
  318. {
  319. contents.Add(new X509Crl(CertificateList.GetInstance(s)));
  320. }
  321. }
  322. }
  323. return CollectionUtilities.CreateStore(contents);
  324. }
  325. internal IStore<Asn1Encodable> GetOtherRevInfos(Asn1Set crlSet, DerObjectIdentifier otherRevInfoFormat)
  326. {
  327. var contents = new List<Asn1Encodable>();
  328. if (crlSet != null && otherRevInfoFormat != null)
  329. {
  330. foreach (Asn1Encodable ae in crlSet)
  331. {
  332. if (ae != null && ae.ToAsn1Object() is Asn1TaggedObject taggedObject)
  333. {
  334. if (taggedObject.HasContextTag(1))
  335. {
  336. var otherRevocationInfo = OtherRevocationInfoFormat.GetInstance(taggedObject, false);
  337. if (otherRevInfoFormat.Equals(otherRevocationInfo.InfoFormat))
  338. {
  339. contents.Add(otherRevocationInfo.Info);
  340. }
  341. }
  342. }
  343. }
  344. }
  345. return CollectionUtilities.CreateStore(contents);
  346. }
  347. }
  348. }
  349. #pragma warning restore
  350. #endif