CMSSignedHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Eac;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Iana;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Misc;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  19. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  20. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  21. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  22. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  23. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  24. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  25. {
  26. internal class CmsSignedHelper
  27. {
  28. internal static readonly CmsSignedHelper Instance = new CmsSignedHelper();
  29. private static readonly string EncryptionECDsaWithSha1 = X9ObjectIdentifiers.ECDsaWithSha1.Id;
  30. private static readonly string EncryptionECDsaWithSha224 = X9ObjectIdentifiers.ECDsaWithSha224.Id;
  31. private static readonly string EncryptionECDsaWithSha256 = X9ObjectIdentifiers.ECDsaWithSha256.Id;
  32. private static readonly string EncryptionECDsaWithSha384 = X9ObjectIdentifiers.ECDsaWithSha384.Id;
  33. private static readonly string EncryptionECDsaWithSha512 = X9ObjectIdentifiers.ECDsaWithSha512.Id;
  34. private static readonly IDictionary encryptionAlgs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  35. private static readonly IDictionary digestAlgs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  36. private static readonly IDictionary digestAliases = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  37. private static readonly ISet noParams = new HashSet();
  38. private static readonly IDictionary ecAlgorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  39. private static void AddEntries(DerObjectIdentifier oid, string digest, string encryption)
  40. {
  41. string alias = oid.Id;
  42. digestAlgs.Add(alias, digest);
  43. encryptionAlgs.Add(alias, encryption);
  44. }
  45. static CmsSignedHelper()
  46. {
  47. AddEntries(NistObjectIdentifiers.DsaWithSha224, "SHA224", "DSA");
  48. AddEntries(NistObjectIdentifiers.DsaWithSha256, "SHA256", "DSA");
  49. AddEntries(NistObjectIdentifiers.DsaWithSha384, "SHA384", "DSA");
  50. AddEntries(NistObjectIdentifiers.DsaWithSha512, "SHA512", "DSA");
  51. AddEntries(OiwObjectIdentifiers.DsaWithSha1, "SHA1", "DSA");
  52. AddEntries(OiwObjectIdentifiers.MD4WithRsa, "MD4", "RSA");
  53. AddEntries(OiwObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
  54. AddEntries(OiwObjectIdentifiers.MD5WithRsa, "MD5", "RSA");
  55. AddEntries(OiwObjectIdentifiers.Sha1WithRsa, "SHA1", "RSA");
  56. AddEntries(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2", "RSA");
  57. AddEntries(PkcsObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
  58. AddEntries(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5", "RSA");
  59. AddEntries(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1", "RSA");
  60. AddEntries(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224", "RSA");
  61. AddEntries(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256", "RSA");
  62. AddEntries(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384", "RSA");
  63. AddEntries(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512", "RSA");
  64. AddEntries(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)", "RSA");
  65. AddEntries(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)", "RSA");
  66. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224, "SHA3-224", "RSA");
  67. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256, "SHA3-256", "RSA");
  68. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384, "SHA3-384", "RSA");
  69. AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512, "SHA3-512", "RSA");
  70. AddEntries(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1", "ECDSA");
  71. AddEntries(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224", "ECDSA");
  72. AddEntries(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256", "ECDSA");
  73. AddEntries(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384", "ECDSA");
  74. AddEntries(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512", "ECDSA");
  75. AddEntries(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1", "DSA");
  76. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA");
  77. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA");
  78. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA");
  79. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA");
  80. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA");
  81. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA");
  82. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA");
  83. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1");
  84. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1");
  85. encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa.Id, "DSA");
  86. encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption.Id, "RSA");
  87. encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm.Id, "RSA");
  88. encryptionAlgs.Add(X509ObjectIdentifiers.IdEARsa.Id, "RSA");
  89. encryptionAlgs.Add(CmsSignedGenerator.EncryptionRsaPss, "RSAandMGF1");
  90. encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x94.Id, "GOST3410");
  91. encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x2001.Id, "ECGOST3410");
  92. encryptionAlgs.Add("1.3.6.1.4.1.5849.1.6.2", "ECGOST3410");
  93. encryptionAlgs.Add("1.3.6.1.4.1.5849.1.1.5", "GOST3410");
  94. digestAlgs.Add(PkcsObjectIdentifiers.MD2.Id, "MD2");
  95. digestAlgs.Add(PkcsObjectIdentifiers.MD4.Id, "MD4");
  96. digestAlgs.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
  97. digestAlgs.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
  98. digestAlgs.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
  99. digestAlgs.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
  100. digestAlgs.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
  101. digestAlgs.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
  102. digestAlgs.Add(NistObjectIdentifiers.IdSha512_224.Id, "SHA512(224)");
  103. digestAlgs.Add(NistObjectIdentifiers.IdSha512_256.Id, "SHA512(256)");
  104. digestAlgs.Add(NistObjectIdentifiers.IdSha3_224.Id, "SHA3-224");
  105. digestAlgs.Add(NistObjectIdentifiers.IdSha3_256.Id, "SHA3-256");
  106. digestAlgs.Add(NistObjectIdentifiers.IdSha3_384.Id, "SHA3-384");
  107. digestAlgs.Add(NistObjectIdentifiers.IdSha3_512.Id, "SHA3-512");
  108. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
  109. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
  110. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
  111. digestAlgs.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
  112. digestAlgs.Add("1.3.6.1.4.1.5849.1.2.1", "GOST3411");
  113. digestAliases.Add("SHA1", new string[] { "SHA-1" });
  114. digestAliases.Add("SHA224", new string[] { "SHA-224" });
  115. digestAliases.Add("SHA256", new string[] { "SHA-256" });
  116. digestAliases.Add("SHA384", new string[] { "SHA-384" });
  117. digestAliases.Add("SHA512", new string[] { "SHA-512" });
  118. noParams.Add(CmsSignedGenerator.EncryptionDsa);
  119. // noParams.Add(EncryptionECDsa);
  120. noParams.Add(EncryptionECDsaWithSha1);
  121. noParams.Add(EncryptionECDsaWithSha224);
  122. noParams.Add(EncryptionECDsaWithSha256);
  123. noParams.Add(EncryptionECDsaWithSha384);
  124. noParams.Add(EncryptionECDsaWithSha512);
  125. ecAlgorithms.Add(CmsSignedGenerator.DigestSha1, EncryptionECDsaWithSha1);
  126. ecAlgorithms.Add(CmsSignedGenerator.DigestSha224, EncryptionECDsaWithSha224);
  127. ecAlgorithms.Add(CmsSignedGenerator.DigestSha256, EncryptionECDsaWithSha256);
  128. ecAlgorithms.Add(CmsSignedGenerator.DigestSha384, EncryptionECDsaWithSha384);
  129. ecAlgorithms.Add(CmsSignedGenerator.DigestSha512, EncryptionECDsaWithSha512);
  130. }
  131. /**
  132. * Return the digest algorithm using one of the standard JCA string
  133. * representations rather than the algorithm identifier (if possible).
  134. */
  135. internal string GetDigestAlgName(
  136. string digestAlgOid)
  137. {
  138. string algName = (string)digestAlgs[digestAlgOid];
  139. if (algName != null)
  140. {
  141. return algName;
  142. }
  143. return digestAlgOid;
  144. }
  145. internal AlgorithmIdentifier GetEncAlgorithmIdentifier(
  146. DerObjectIdentifier encOid,
  147. Asn1Encodable sigX509Parameters)
  148. {
  149. if (noParams.Contains(encOid.Id))
  150. {
  151. return new AlgorithmIdentifier(encOid);
  152. }
  153. return new AlgorithmIdentifier(encOid, sigX509Parameters);
  154. }
  155. internal string[] GetDigestAliases(
  156. string algName)
  157. {
  158. string[] aliases = (string[]) digestAliases[algName];
  159. return aliases == null ? new string[0] : (string[]) aliases.Clone();
  160. }
  161. /**
  162. * Return the digest encryption algorithm using one of the standard
  163. * JCA string representations rather than the algorithm identifier (if
  164. * possible).
  165. */
  166. internal string GetEncryptionAlgName(
  167. string encryptionAlgOid)
  168. {
  169. string algName = (string) encryptionAlgs[encryptionAlgOid];
  170. if (algName != null)
  171. {
  172. return algName;
  173. }
  174. return encryptionAlgOid;
  175. }
  176. internal IDigest GetDigestInstance(
  177. string algorithm)
  178. {
  179. try
  180. {
  181. return DigestUtilities.GetDigest(algorithm);
  182. }
  183. catch (SecurityUtilityException e)
  184. {
  185. // This is probably superfluous on C#, since no provider infrastructure,
  186. // assuming DigestUtilities already knows all the aliases
  187. foreach (string alias in GetDigestAliases(algorithm))
  188. {
  189. try { return DigestUtilities.GetDigest(alias); }
  190. catch (SecurityUtilityException) {}
  191. }
  192. throw e;
  193. }
  194. }
  195. internal ISigner GetSignatureInstance(
  196. string algorithm)
  197. {
  198. return SignerUtilities.GetSigner(algorithm);
  199. }
  200. internal IX509Store CreateAttributeStore(
  201. string type,
  202. Asn1Set certSet)
  203. {
  204. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  205. if (certSet != null)
  206. {
  207. foreach (Asn1Encodable ae in certSet)
  208. {
  209. try
  210. {
  211. Asn1Object obj = ae.ToAsn1Object();
  212. if (obj is Asn1TaggedObject)
  213. {
  214. Asn1TaggedObject tagged = (Asn1TaggedObject)obj;
  215. if (tagged.TagNo == 2)
  216. {
  217. certs.Add(
  218. new X509V2AttributeCertificate(
  219. Asn1Sequence.GetInstance(tagged, false).GetEncoded()));
  220. }
  221. }
  222. }
  223. catch (Exception ex)
  224. {
  225. throw new CmsException("can't re-encode attribute certificate!", ex);
  226. }
  227. }
  228. }
  229. try
  230. {
  231. return X509StoreFactory.Create(
  232. "AttributeCertificate/" + type,
  233. new X509CollectionStoreParameters(certs));
  234. }
  235. catch (ArgumentException e)
  236. {
  237. throw new CmsException("can't setup the X509Store", e);
  238. }
  239. }
  240. internal IX509Store CreateCertificateStore(
  241. string type,
  242. Asn1Set certSet)
  243. {
  244. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  245. if (certSet != null)
  246. {
  247. AddCertsFromSet(certs, certSet);
  248. }
  249. try
  250. {
  251. return X509StoreFactory.Create(
  252. "Certificate/" + type,
  253. new X509CollectionStoreParameters(certs));
  254. }
  255. catch (ArgumentException e)
  256. {
  257. throw new CmsException("can't setup the X509Store", e);
  258. }
  259. }
  260. internal IX509Store CreateCrlStore(
  261. string type,
  262. Asn1Set crlSet)
  263. {
  264. IList crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  265. if (crlSet != null)
  266. {
  267. AddCrlsFromSet(crls, crlSet);
  268. }
  269. try
  270. {
  271. return X509StoreFactory.Create(
  272. "CRL/" + type,
  273. new X509CollectionStoreParameters(crls));
  274. }
  275. catch (ArgumentException e)
  276. {
  277. throw new CmsException("can't setup the X509Store", e);
  278. }
  279. }
  280. private void AddCertsFromSet(
  281. IList certs,
  282. Asn1Set certSet)
  283. {
  284. X509CertificateParser cf = new X509CertificateParser();
  285. foreach (Asn1Encodable ae in certSet)
  286. {
  287. try
  288. {
  289. Asn1Object obj = ae.ToAsn1Object();
  290. if (obj is Asn1Sequence)
  291. {
  292. // TODO Build certificate directly from sequence?
  293. certs.Add(cf.ReadCertificate(obj.GetEncoded()));
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. throw new CmsException("can't re-encode certificate!", ex);
  299. }
  300. }
  301. }
  302. private void AddCrlsFromSet(
  303. IList crls,
  304. Asn1Set crlSet)
  305. {
  306. X509CrlParser cf = new X509CrlParser();
  307. foreach (Asn1Encodable ae in crlSet)
  308. {
  309. try
  310. {
  311. // TODO Build CRL directly from ae.ToAsn1Object()?
  312. crls.Add(cf.ReadCrl(ae.GetEncoded()));
  313. }
  314. catch (Exception ex)
  315. {
  316. throw new CmsException("can't re-encode CRL!", ex);
  317. }
  318. }
  319. }
  320. internal AlgorithmIdentifier FixAlgID(
  321. AlgorithmIdentifier algId)
  322. {
  323. if (algId.Parameters == null)
  324. return new AlgorithmIdentifier(algId.Algorithm, DerNull.Instance);
  325. return algId;
  326. }
  327. internal string GetEncOid(
  328. AsymmetricKeyParameter key,
  329. string digestOID)
  330. {
  331. string encOID = null;
  332. if (key is RsaKeyParameters)
  333. {
  334. if (!((RsaKeyParameters)key).IsPrivate)
  335. throw new ArgumentException("Expected RSA private key");
  336. encOID = CmsSignedGenerator.EncryptionRsa;
  337. }
  338. else if (key is DsaPrivateKeyParameters)
  339. {
  340. if (digestOID.Equals(CmsSignedGenerator.DigestSha1))
  341. {
  342. encOID = CmsSignedGenerator.EncryptionDsa;
  343. }
  344. else if (digestOID.Equals(CmsSignedGenerator.DigestSha224))
  345. {
  346. encOID = NistObjectIdentifiers.DsaWithSha224.Id;
  347. }
  348. else if (digestOID.Equals(CmsSignedGenerator.DigestSha256))
  349. {
  350. encOID = NistObjectIdentifiers.DsaWithSha256.Id;
  351. }
  352. else if (digestOID.Equals(CmsSignedGenerator.DigestSha384))
  353. {
  354. encOID = NistObjectIdentifiers.DsaWithSha384.Id;
  355. }
  356. else if (digestOID.Equals(CmsSignedGenerator.DigestSha512))
  357. {
  358. encOID = NistObjectIdentifiers.DsaWithSha512.Id;
  359. }
  360. else
  361. {
  362. throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2");
  363. }
  364. }
  365. else if (key is ECPrivateKeyParameters)
  366. {
  367. ECPrivateKeyParameters ecPrivKey = (ECPrivateKeyParameters)key;
  368. string algName = ecPrivKey.AlgorithmName;
  369. if (algName == "ECGOST3410")
  370. {
  371. encOID = CmsSignedGenerator.EncryptionECGost3410;
  372. }
  373. else
  374. {
  375. // TODO Should we insist on algName being one of "EC" or "ECDSA", as Java does?
  376. encOID = (string)ecAlgorithms[digestOID];
  377. if (encOID == null)
  378. throw new ArgumentException("can't mix ECDSA with anything but SHA family digests");
  379. }
  380. }
  381. else if (key is Gost3410PrivateKeyParameters)
  382. {
  383. encOID = CmsSignedGenerator.EncryptionGost3410;
  384. }
  385. else
  386. {
  387. throw new ArgumentException("Unknown algorithm in CmsSignedGenerator.GetEncOid");
  388. }
  389. return encOID;
  390. }
  391. public IX509Store GetCertificates(Asn1Set certificates)
  392. {
  393. IList certList = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  394. if (certificates != null)
  395. {
  396. foreach (Asn1Encodable enc in certificates)
  397. {
  398. certList.Add(X509CertificateStructure.GetInstance(enc));
  399. }
  400. }
  401. return new X509CollectionStore(certList);
  402. }
  403. }
  404. }
  405. #pragma warning restore
  406. #endif