EnvelopedDataHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Nist;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  18. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  19. {
  20. internal class EnvelopedDataHelper
  21. {
  22. private static readonly IDictionary BaseCipherNames = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  23. private static readonly IDictionary MacAlgNames = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  24. //private static readonly IDictionary PrfDigests = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  25. static EnvelopedDataHelper()
  26. {
  27. //PrfDigests.Add(PkcsObjectIdentifiers.IdHmacWithSha1, "SHA-1");
  28. //PrfDigests.Add(PkcsObjectIdentifiers.IdHmacWithSha224, "SHA-224");
  29. //PrfDigests.Add(PkcsObjectIdentifiers.IdHmacWithSha256, "SHA-256");
  30. //PrfDigests.Add(PkcsObjectIdentifiers.IdHmacWithSha384, "SHA-384");
  31. //PrfDigests.Add(PkcsObjectIdentifiers.IdHmacWithSha512, "SHA-512");
  32. BaseCipherNames.Add(PkcsObjectIdentifiers.DesEde3Cbc, "DESEDE");
  33. BaseCipherNames.Add(NistObjectIdentifiers.IdAes128Cbc, "AES");
  34. BaseCipherNames.Add(NistObjectIdentifiers.IdAes192Cbc, "AES");
  35. BaseCipherNames.Add(NistObjectIdentifiers.IdAes256Cbc, "AES");
  36. MacAlgNames.Add(PkcsObjectIdentifiers.DesEde3Cbc, "DESEDEMac");
  37. MacAlgNames.Add(NistObjectIdentifiers.IdAes128Cbc, "AESMac");
  38. MacAlgNames.Add(NistObjectIdentifiers.IdAes192Cbc, "AESMac");
  39. MacAlgNames.Add(NistObjectIdentifiers.IdAes256Cbc, "AESMac");
  40. MacAlgNames.Add(PkcsObjectIdentifiers.RC2Cbc, "RC2Mac");
  41. }
  42. //internal static IDigest GetPrf(AlgorithmIdentifier algID)
  43. //{
  44. // string digestName = (string)PrfDigests[algID];
  45. // return DigestUtilities.GetDigest(digestName);
  46. //}
  47. //internal static IWrapper CreateRfc3211Wrapper(DerObjectIdentifier algorithm)
  48. //{
  49. // if (NistObjectIdentifiers.IdAes128Cbc.Equals(algorithm)
  50. // || NistObjectIdentifiers.IdAes192Cbc.Equals(algorithm)
  51. // || NistObjectIdentifiers.IdAes256Cbc.Equals(algorithm))
  52. // {
  53. // return new Rfc3211WrapEngine(new AesEngine());
  54. // }
  55. // else if (PkcsObjectIdentifiers.DesEde3Cbc.Equals(algorithm))
  56. // {
  57. // return new Rfc3211WrapEngine(new DesEdeEngine());
  58. // }
  59. // else if (OiwObjectIdentifiers.DesCbc.Equals(algorithm))
  60. // {
  61. // return new Rfc3211WrapEngine(new DesEngine());
  62. // }
  63. // else if (PkcsObjectIdentifiers.RC2Cbc.Equals(algorithm))
  64. // {
  65. // return new Rfc3211WrapEngine(new RC2Engine());
  66. // }
  67. // else
  68. // {
  69. // throw new CmsException("cannot recognise wrapper: " + algorithm);
  70. // }
  71. //}
  72. public static object CreateContentCipher(bool forEncryption, ICipherParameters encKey,
  73. AlgorithmIdentifier encryptionAlgID)
  74. {
  75. return CipherFactory.CreateContentCipher(forEncryption, encKey, encryptionAlgID);
  76. }
  77. public AlgorithmIdentifier GenerateEncryptionAlgID(DerObjectIdentifier encryptionOID, KeyParameter encKey, SecureRandom random)
  78. {
  79. return AlgorithmIdentifierFactory.GenerateEncryptionAlgID(encryptionOID, encKey.GetKey().Length * 8, random);
  80. }
  81. public CipherKeyGenerator CreateKeyGenerator(DerObjectIdentifier algorithm, SecureRandom random)
  82. {
  83. return CipherKeyGeneratorFactory.CreateKeyGenerator(algorithm, random);
  84. }
  85. }
  86. }
  87. #pragma warning restore
  88. #endif