WrapperUtilities.cs 5.6 KB

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