WrapperUtilities.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Kisa;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ntt;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  14. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Security
  15. {
  16. /// <remarks>
  17. /// Utility class for creating IWrapper objects from their names/Oids
  18. /// </remarks>
  19. public static class WrapperUtilities
  20. {
  21. private enum WrapAlgorithm { AESWRAP, CAMELLIAWRAP, DESEDEWRAP, RC2WRAP, SEEDWRAP,
  22. DESEDERFC3211WRAP, AESRFC3211WRAP, CAMELLIARFC3211WRAP };
  23. private static readonly IDictionary<string, string> Algorithms =
  24. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  25. static WrapperUtilities()
  26. {
  27. // Signal to obfuscation tools not to change enum constants
  28. Enums.GetArbitraryValue<WrapAlgorithm>().ToString();
  29. Algorithms[NistObjectIdentifiers.IdAes128Wrap.Id] = "AESWRAP";
  30. Algorithms[NistObjectIdentifiers.IdAes192Wrap.Id] = "AESWRAP";
  31. Algorithms[NistObjectIdentifiers.IdAes256Wrap.Id] = "AESWRAP";
  32. Algorithms[NttObjectIdentifiers.IdCamellia128Wrap.Id] = "CAMELLIAWRAP";
  33. Algorithms[NttObjectIdentifiers.IdCamellia192Wrap.Id] = "CAMELLIAWRAP";
  34. Algorithms[NttObjectIdentifiers.IdCamellia256Wrap.Id] = "CAMELLIAWRAP";
  35. Algorithms[PkcsObjectIdentifiers.IdAlgCms3DesWrap.Id] = "DESEDEWRAP";
  36. Algorithms["TDEAWRAP"] = "DESEDEWRAP";
  37. Algorithms[PkcsObjectIdentifiers.IdAlgCmsRC2Wrap.Id] = "RC2WRAP";
  38. Algorithms[KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap.Id] = "SEEDWRAP";
  39. }
  40. public static IWrapper GetWrapper(DerObjectIdentifier oid)
  41. {
  42. return GetWrapper(oid.Id);
  43. }
  44. public static IWrapper GetWrapper(string algorithm)
  45. {
  46. string mechanism = CollectionUtilities.GetValueOrKey(Algorithms, algorithm).ToUpperInvariant();
  47. try
  48. {
  49. WrapAlgorithm wrapAlgorithm = Enums.GetEnumValue<WrapAlgorithm>(mechanism);
  50. switch (wrapAlgorithm)
  51. {
  52. case WrapAlgorithm.AESWRAP: return new AesWrapEngine();
  53. case WrapAlgorithm.CAMELLIAWRAP: return new CamelliaWrapEngine();
  54. case WrapAlgorithm.DESEDEWRAP: return new DesEdeWrapEngine();
  55. case WrapAlgorithm.RC2WRAP: return new RC2WrapEngine();
  56. case WrapAlgorithm.SEEDWRAP: return new SeedWrapEngine();
  57. case WrapAlgorithm.DESEDERFC3211WRAP: return new Rfc3211WrapEngine(new DesEdeEngine());
  58. case WrapAlgorithm.AESRFC3211WRAP: return new Rfc3211WrapEngine(AesUtilities.CreateEngine());
  59. case WrapAlgorithm.CAMELLIARFC3211WRAP: return new Rfc3211WrapEngine(new CamelliaEngine());
  60. }
  61. }
  62. catch (ArgumentException)
  63. {
  64. }
  65. // Create an IBufferedCipher and use it as IWrapper (via BufferedCipherWrapper)
  66. IBufferedCipher blockCipher = CipherUtilities.GetCipher(algorithm);
  67. if (blockCipher != null)
  68. return new BufferedCipherWrapper(blockCipher);
  69. throw new SecurityUtilityException("Wrapper " + algorithm + " not recognised.");
  70. }
  71. public static string GetAlgorithmName(DerObjectIdentifier oid)
  72. {
  73. return CollectionUtilities.GetValueOrNull(Algorithms, oid.Id);
  74. }
  75. private class BufferedCipherWrapper
  76. : IWrapper
  77. {
  78. private readonly IBufferedCipher cipher;
  79. private bool forWrapping;
  80. public BufferedCipherWrapper(
  81. IBufferedCipher cipher)
  82. {
  83. this.cipher = cipher;
  84. }
  85. public string AlgorithmName
  86. {
  87. get { return cipher.AlgorithmName; }
  88. }
  89. public void Init(
  90. bool forWrapping,
  91. ICipherParameters parameters)
  92. {
  93. this.forWrapping = forWrapping;
  94. cipher.Init(forWrapping, parameters);
  95. }
  96. public byte[] Wrap(
  97. byte[] input,
  98. int inOff,
  99. int length)
  100. {
  101. if (!forWrapping)
  102. throw new InvalidOperationException("Not initialised for wrapping");
  103. return cipher.DoFinal(input, inOff, length);
  104. }
  105. public byte[] Unwrap(
  106. byte[] input,
  107. int inOff,
  108. int length)
  109. {
  110. if (forWrapping)
  111. throw new InvalidOperationException("Not initialised for unwrapping");
  112. return cipher.DoFinal(input, inOff, length);
  113. }
  114. }
  115. }
  116. }
  117. #pragma warning restore
  118. #endif