EnvelopedDataHelper.cs 4.2 KB

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