CMSSignedHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1", "ECDSA");
  65. AddEntries(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224", "ECDSA");
  66. AddEntries(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256", "ECDSA");
  67. AddEntries(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384", "ECDSA");
  68. AddEntries(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512", "ECDSA");
  69. AddEntries(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1", "DSA");
  70. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA");
  71. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA");
  72. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA");
  73. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA");
  74. AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA");
  75. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA");
  76. AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA");
  77. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1");
  78. AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1");
  79. encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa.Id, "DSA");
  80. encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption.Id, "RSA");
  81. encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm, "RSA");
  82. encryptionAlgs.Add(X509ObjectIdentifiers.IdEARsa.Id, "RSA");
  83. encryptionAlgs.Add(CmsSignedGenerator.EncryptionRsaPss, "RSAandMGF1");
  84. encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x94.Id, "GOST3410");
  85. encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x2001.Id, "ECGOST3410");
  86. encryptionAlgs.Add("1.3.6.1.4.1.5849.1.6.2", "ECGOST3410");
  87. encryptionAlgs.Add("1.3.6.1.4.1.5849.1.1.5", "GOST3410");
  88. digestAlgs.Add(PkcsObjectIdentifiers.MD2.Id, "MD2");
  89. digestAlgs.Add(PkcsObjectIdentifiers.MD4.Id, "MD4");
  90. digestAlgs.Add(PkcsObjectIdentifiers.MD5.Id, "MD5");
  91. digestAlgs.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1");
  92. digestAlgs.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224");
  93. digestAlgs.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256");
  94. digestAlgs.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384");
  95. digestAlgs.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512");
  96. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128");
  97. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160");
  98. digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256");
  99. digestAlgs.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411");
  100. digestAlgs.Add("1.3.6.1.4.1.5849.1.2.1", "GOST3411");
  101. digestAliases.Add("SHA1", new string[] { "SHA-1" });
  102. digestAliases.Add("SHA224", new string[] { "SHA-224" });
  103. digestAliases.Add("SHA256", new string[] { "SHA-256" });
  104. digestAliases.Add("SHA384", new string[] { "SHA-384" });
  105. digestAliases.Add("SHA512", new string[] { "SHA-512" });
  106. noParams.Add(CmsSignedGenerator.EncryptionDsa);
  107. // noParams.Add(EncryptionECDsa);
  108. noParams.Add(EncryptionECDsaWithSha1);
  109. noParams.Add(EncryptionECDsaWithSha224);
  110. noParams.Add(EncryptionECDsaWithSha256);
  111. noParams.Add(EncryptionECDsaWithSha384);
  112. noParams.Add(EncryptionECDsaWithSha512);
  113. ecAlgorithms.Add(CmsSignedGenerator.DigestSha1, EncryptionECDsaWithSha1);
  114. ecAlgorithms.Add(CmsSignedGenerator.DigestSha224, EncryptionECDsaWithSha224);
  115. ecAlgorithms.Add(CmsSignedGenerator.DigestSha256, EncryptionECDsaWithSha256);
  116. ecAlgorithms.Add(CmsSignedGenerator.DigestSha384, EncryptionECDsaWithSha384);
  117. ecAlgorithms.Add(CmsSignedGenerator.DigestSha512, EncryptionECDsaWithSha512);
  118. }
  119. /**
  120. * Return the digest algorithm using one of the standard JCA string
  121. * representations rather than the algorithm identifier (if possible).
  122. */
  123. internal string GetDigestAlgName(
  124. string digestAlgOid)
  125. {
  126. string algName = (string)digestAlgs[digestAlgOid];
  127. if (algName != null)
  128. {
  129. return algName;
  130. }
  131. return digestAlgOid;
  132. }
  133. internal AlgorithmIdentifier GetEncAlgorithmIdentifier(
  134. DerObjectIdentifier encOid,
  135. Asn1Encodable sigX509Parameters)
  136. {
  137. if (noParams.Contains(encOid.Id))
  138. {
  139. return new AlgorithmIdentifier(encOid);
  140. }
  141. return new AlgorithmIdentifier(encOid, sigX509Parameters);
  142. }
  143. internal string[] GetDigestAliases(
  144. string algName)
  145. {
  146. string[] aliases = (string[]) digestAliases[algName];
  147. return aliases == null ? new String[0] : (string[]) aliases.Clone();
  148. }
  149. /**
  150. * Return the digest encryption algorithm using one of the standard
  151. * JCA string representations rather than the algorithm identifier (if
  152. * possible).
  153. */
  154. internal string GetEncryptionAlgName(
  155. string encryptionAlgOid)
  156. {
  157. string algName = (string) encryptionAlgs[encryptionAlgOid];
  158. if (algName != null)
  159. {
  160. return algName;
  161. }
  162. return encryptionAlgOid;
  163. }
  164. internal IDigest GetDigestInstance(
  165. string algorithm)
  166. {
  167. try
  168. {
  169. return DigestUtilities.GetDigest(algorithm);
  170. }
  171. catch (SecurityUtilityException e)
  172. {
  173. // This is probably superfluous on C#, since no provider infrastructure,
  174. // assuming DigestUtilities already knows all the aliases
  175. foreach (string alias in GetDigestAliases(algorithm))
  176. {
  177. try { return DigestUtilities.GetDigest(alias); }
  178. catch (SecurityUtilityException) {}
  179. }
  180. throw e;
  181. }
  182. }
  183. internal ISigner GetSignatureInstance(
  184. string algorithm)
  185. {
  186. return SignerUtilities.GetSigner(algorithm);
  187. }
  188. internal IX509Store CreateAttributeStore(
  189. string type,
  190. Asn1Set certSet)
  191. {
  192. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  193. if (certSet != null)
  194. {
  195. foreach (Asn1Encodable ae in certSet)
  196. {
  197. try
  198. {
  199. Asn1Object obj = ae.ToAsn1Object();
  200. if (obj is Asn1TaggedObject)
  201. {
  202. Asn1TaggedObject tagged = (Asn1TaggedObject)obj;
  203. if (tagged.TagNo == 2)
  204. {
  205. certs.Add(
  206. new X509V2AttributeCertificate(
  207. Asn1Sequence.GetInstance(tagged, false).GetEncoded()));
  208. }
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. throw new CmsException("can't re-encode attribute certificate!", ex);
  214. }
  215. }
  216. }
  217. try
  218. {
  219. return X509StoreFactory.Create(
  220. "AttributeCertificate/" + type,
  221. new X509CollectionStoreParameters(certs));
  222. }
  223. catch (ArgumentException e)
  224. {
  225. throw new CmsException("can't setup the X509Store", e);
  226. }
  227. }
  228. internal IX509Store CreateCertificateStore(
  229. string type,
  230. Asn1Set certSet)
  231. {
  232. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  233. if (certSet != null)
  234. {
  235. AddCertsFromSet(certs, certSet);
  236. }
  237. try
  238. {
  239. return X509StoreFactory.Create(
  240. "Certificate/" + type,
  241. new X509CollectionStoreParameters(certs));
  242. }
  243. catch (ArgumentException e)
  244. {
  245. throw new CmsException("can't setup the X509Store", e);
  246. }
  247. }
  248. internal IX509Store CreateCrlStore(
  249. string type,
  250. Asn1Set crlSet)
  251. {
  252. IList crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  253. if (crlSet != null)
  254. {
  255. AddCrlsFromSet(crls, crlSet);
  256. }
  257. try
  258. {
  259. return X509StoreFactory.Create(
  260. "CRL/" + type,
  261. new X509CollectionStoreParameters(crls));
  262. }
  263. catch (ArgumentException e)
  264. {
  265. throw new CmsException("can't setup the X509Store", e);
  266. }
  267. }
  268. private void AddCertsFromSet(
  269. IList certs,
  270. Asn1Set certSet)
  271. {
  272. X509CertificateParser cf = new X509CertificateParser();
  273. foreach (Asn1Encodable ae in certSet)
  274. {
  275. try
  276. {
  277. Asn1Object obj = ae.ToAsn1Object();
  278. if (obj is Asn1Sequence)
  279. {
  280. // TODO Build certificate directly from sequence?
  281. certs.Add(cf.ReadCertificate(obj.GetEncoded()));
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. throw new CmsException("can't re-encode certificate!", ex);
  287. }
  288. }
  289. }
  290. private void AddCrlsFromSet(
  291. IList crls,
  292. Asn1Set crlSet)
  293. {
  294. X509CrlParser cf = new X509CrlParser();
  295. foreach (Asn1Encodable ae in crlSet)
  296. {
  297. try
  298. {
  299. // TODO Build CRL directly from ae.ToAsn1Object()?
  300. crls.Add(cf.ReadCrl(ae.GetEncoded()));
  301. }
  302. catch (Exception ex)
  303. {
  304. throw new CmsException("can't re-encode CRL!", ex);
  305. }
  306. }
  307. }
  308. internal AlgorithmIdentifier FixAlgID(
  309. AlgorithmIdentifier algId)
  310. {
  311. if (algId.Parameters == null)
  312. return new AlgorithmIdentifier(algId.Algorithm, DerNull.Instance);
  313. return algId;
  314. }
  315. internal string GetEncOid(
  316. AsymmetricKeyParameter key,
  317. string digestOID)
  318. {
  319. string encOID = null;
  320. if (key is RsaKeyParameters)
  321. {
  322. if (!((RsaKeyParameters)key).IsPrivate)
  323. throw new ArgumentException("Expected RSA private key");
  324. encOID = CmsSignedGenerator.EncryptionRsa;
  325. }
  326. else if (key is DsaPrivateKeyParameters)
  327. {
  328. if (digestOID.Equals(CmsSignedGenerator.DigestSha1))
  329. {
  330. encOID = CmsSignedGenerator.EncryptionDsa;
  331. }
  332. else if (digestOID.Equals(CmsSignedGenerator.DigestSha224))
  333. {
  334. encOID = NistObjectIdentifiers.DsaWithSha224.Id;
  335. }
  336. else if (digestOID.Equals(CmsSignedGenerator.DigestSha256))
  337. {
  338. encOID = NistObjectIdentifiers.DsaWithSha256.Id;
  339. }
  340. else if (digestOID.Equals(CmsSignedGenerator.DigestSha384))
  341. {
  342. encOID = NistObjectIdentifiers.DsaWithSha384.Id;
  343. }
  344. else if (digestOID.Equals(CmsSignedGenerator.DigestSha512))
  345. {
  346. encOID = NistObjectIdentifiers.DsaWithSha512.Id;
  347. }
  348. else
  349. {
  350. throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2");
  351. }
  352. }
  353. else if (key is ECPrivateKeyParameters)
  354. {
  355. ECPrivateKeyParameters ecPrivKey = (ECPrivateKeyParameters)key;
  356. string algName = ecPrivKey.AlgorithmName;
  357. if (algName == "ECGOST3410")
  358. {
  359. encOID = CmsSignedGenerator.EncryptionECGost3410;
  360. }
  361. else
  362. {
  363. // TODO Should we insist on algName being one of "EC" or "ECDSA", as Java does?
  364. encOID = (string)ecAlgorithms[digestOID];
  365. if (encOID == null)
  366. throw new ArgumentException("can't mix ECDSA with anything but SHA family digests");
  367. }
  368. }
  369. else if (key is Gost3410PrivateKeyParameters)
  370. {
  371. encOID = CmsSignedGenerator.EncryptionGost3410;
  372. }
  373. else
  374. {
  375. throw new ArgumentException("Unknown algorithm in CmsSignedGenerator.GetEncOid");
  376. }
  377. return encOID;
  378. }
  379. }
  380. }
  381. #pragma warning restore
  382. #endif