Asn1KeyWrapper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.Nist;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Encodings;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  14. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators
  15. {
  16. public class Asn1KeyWrapper
  17. : IKeyWrapper
  18. {
  19. private string algorithm;
  20. private IKeyWrapper wrapper;
  21. public Asn1KeyWrapper(string algorithm, X509Certificate cert)
  22. {
  23. this.algorithm = algorithm;
  24. wrapper = KeyWrapperUtil.WrapperForName(algorithm, cert.GetPublicKey());
  25. }
  26. public Asn1KeyWrapper(DerObjectIdentifier algorithm, X509Certificate cert)
  27. : this(algorithm, cert.GetPublicKey())
  28. {
  29. }
  30. public Asn1KeyWrapper(DerObjectIdentifier algorithm, ICipherParameters key)
  31. : this(algorithm, null, key)
  32. {
  33. }
  34. public Asn1KeyWrapper(DerObjectIdentifier algorithm, Asn1Encodable parameters, X509Certificate cert)
  35. :this(algorithm, parameters, cert.GetPublicKey())
  36. {
  37. }
  38. public Asn1KeyWrapper(DerObjectIdentifier algorithm, Asn1Encodable parameters, ICipherParameters key)
  39. {
  40. this.algorithm = algorithm.Id;
  41. if (algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
  42. {
  43. RsaesOaepParameters oaepParams = RsaesOaepParameters.GetInstance(parameters);
  44. WrapperProvider provider;
  45. if (oaepParams.MaskGenAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdMgf1))
  46. {
  47. AlgorithmIdentifier digAlg = AlgorithmIdentifier.GetInstance(oaepParams.MaskGenAlgorithm.Parameters);
  48. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, digAlg.Algorithm);
  49. }
  50. else
  51. {
  52. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, oaepParams.MaskGenAlgorithm.Algorithm);
  53. }
  54. wrapper = (IKeyWrapper)provider.CreateWrapper(true, key);
  55. }
  56. else if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption))
  57. {
  58. wrapper = (IKeyWrapper)new RsaPkcs1Wrapper(true, key);
  59. }
  60. else
  61. {
  62. throw new ArgumentException("unknown algorithm: " + algorithm.Id);
  63. }
  64. }
  65. public object AlgorithmDetails
  66. {
  67. get { return wrapper.AlgorithmDetails; }
  68. }
  69. public IBlockResult Wrap(byte[] keyData)
  70. {
  71. return wrapper.Wrap(keyData);
  72. }
  73. }
  74. public class Asn1KeyUnwrapper
  75. : IKeyUnwrapper
  76. {
  77. private string algorithm;
  78. private IKeyUnwrapper wrapper;
  79. public Asn1KeyUnwrapper(string algorithm, ICipherParameters key)
  80. {
  81. this.algorithm = algorithm;
  82. wrapper = KeyWrapperUtil.UnwrapperForName(algorithm, key);
  83. }
  84. public Asn1KeyUnwrapper(DerObjectIdentifier algorithm, ICipherParameters key)
  85. : this(algorithm, null, key)
  86. {
  87. }
  88. public Asn1KeyUnwrapper(DerObjectIdentifier algorithm, Asn1Encodable parameters, ICipherParameters key)
  89. {
  90. this.algorithm = algorithm.Id;
  91. if (algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
  92. {
  93. RsaesOaepParameters oaepParams = RsaesOaepParameters.GetInstance(parameters);
  94. WrapperProvider provider;
  95. if (oaepParams.MaskGenAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdMgf1))
  96. {
  97. AlgorithmIdentifier digAlg = AlgorithmIdentifier.GetInstance(oaepParams.MaskGenAlgorithm.Parameters);
  98. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, digAlg.Algorithm);
  99. }
  100. else
  101. {
  102. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, oaepParams.MaskGenAlgorithm.Algorithm);
  103. }
  104. wrapper = (IKeyUnwrapper)provider.CreateWrapper(false, key);
  105. }
  106. else if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption))
  107. {
  108. RsaesOaepParameters oaepParams = RsaesOaepParameters.GetInstance(parameters);
  109. WrapperProvider provider;
  110. if (oaepParams.MaskGenAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdMgf1))
  111. {
  112. AlgorithmIdentifier digAlg = AlgorithmIdentifier.GetInstance(oaepParams.MaskGenAlgorithm.Parameters);
  113. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, digAlg.Algorithm);
  114. }
  115. else
  116. {
  117. provider = new RsaOaepWrapperProvider(oaepParams.HashAlgorithm.Algorithm, oaepParams.MaskGenAlgorithm.Algorithm);
  118. }
  119. wrapper = (IKeyUnwrapper)new RsaPkcs1Wrapper(false, key);
  120. }
  121. else
  122. {
  123. throw new ArgumentException("unknown algorithm: " + algorithm.Id);
  124. }
  125. }
  126. public object AlgorithmDetails
  127. {
  128. get { return wrapper.AlgorithmDetails; }
  129. }
  130. public IBlockResult Unwrap(byte[] keyData, int offSet, int length)
  131. {
  132. return wrapper.Unwrap(keyData, offSet, length);
  133. }
  134. }
  135. internal class KeyWrapperUtil
  136. {
  137. //
  138. // Provider
  139. //
  140. private static readonly Dictionary<string, WrapperProvider> m_providerMap =
  141. new Dictionary<string, WrapperProvider>(StringComparer.OrdinalIgnoreCase);
  142. static KeyWrapperUtil()
  143. {
  144. m_providerMap.Add("RSA/ECB/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1));
  145. m_providerMap.Add("RSA/NONE/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1));
  146. m_providerMap.Add("RSA/NONE/OAEPWITHSHA1ANDMGF1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1));
  147. m_providerMap.Add("RSA/NONE/OAEPWITHSHA224ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha224));
  148. m_providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256));
  149. m_providerMap.Add("RSA/NONE/OAEPWITHSHA384ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha384));
  150. m_providerMap.Add("RSA/NONE/OAEPWITHSHA512ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha512));
  151. m_providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1WITHSHA1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256, OiwObjectIdentifiers.IdSha1));
  152. }
  153. public static IKeyWrapper WrapperForName(string algorithm, ICipherParameters parameters)
  154. {
  155. if (!m_providerMap.TryGetValue(algorithm, out var provider))
  156. throw new ArgumentException("could not resolve " + algorithm + " to a KeyWrapper");
  157. return (IKeyWrapper)provider.CreateWrapper(true, parameters);
  158. }
  159. public static IKeyUnwrapper UnwrapperForName(string algorithm, ICipherParameters parameters)
  160. {
  161. if (!m_providerMap.TryGetValue(algorithm, out var provider))
  162. throw new ArgumentException("could not resolve " + algorithm + " to a KeyUnwrapper");
  163. return (IKeyUnwrapper)provider.CreateWrapper(false, parameters);
  164. }
  165. }
  166. internal interface WrapperProvider
  167. {
  168. object CreateWrapper(bool forWrapping, ICipherParameters parameters);
  169. }
  170. internal class RsaPkcs1Wrapper : IKeyWrapper, IKeyUnwrapper
  171. {
  172. private readonly AlgorithmIdentifier algId;
  173. private readonly IAsymmetricBlockCipher engine;
  174. public RsaPkcs1Wrapper(bool forWrapping, ICipherParameters parameters)
  175. {
  176. this.algId = new AlgorithmIdentifier(
  177. PkcsObjectIdentifiers.RsaEncryption,
  178. DerNull.Instance);
  179. this.engine = new Pkcs1Encoding(new RsaBlindedEngine());
  180. this.engine.Init(forWrapping, parameters);
  181. }
  182. public object AlgorithmDetails
  183. {
  184. get { return algId; }
  185. }
  186. public IBlockResult Unwrap(byte[] cipherText, int offset, int length)
  187. {
  188. return new SimpleBlockResult(engine.ProcessBlock(cipherText, offset, length));
  189. }
  190. public IBlockResult Wrap(byte[] keyData)
  191. {
  192. return new SimpleBlockResult(engine.ProcessBlock(keyData, 0, keyData.Length));
  193. }
  194. }
  195. internal class RsaPkcs1WrapperProvider
  196. : WrapperProvider
  197. {
  198. internal RsaPkcs1WrapperProvider()
  199. {
  200. }
  201. object WrapperProvider.CreateWrapper(bool forWrapping, ICipherParameters parameters)
  202. {
  203. return new RsaPkcs1Wrapper(forWrapping, parameters);
  204. }
  205. }
  206. internal class RsaOaepWrapper : IKeyWrapper, IKeyUnwrapper
  207. {
  208. private readonly AlgorithmIdentifier algId;
  209. private readonly IAsymmetricBlockCipher engine;
  210. public RsaOaepWrapper(bool forWrapping, ICipherParameters parameters, DerObjectIdentifier digestOid)
  211. : this(forWrapping, parameters, digestOid, digestOid)
  212. {
  213. }
  214. public RsaOaepWrapper(bool forWrapping, ICipherParameters parameters, DerObjectIdentifier digestOid, DerObjectIdentifier mgfOid)
  215. {
  216. AlgorithmIdentifier digestAlgId = new AlgorithmIdentifier(digestOid, DerNull.Instance);
  217. if (mgfOid.Equals(NistObjectIdentifiers.IdShake128) || mgfOid.Equals(NistObjectIdentifiers.IdShake256))
  218. {
  219. this.algId = new AlgorithmIdentifier(
  220. PkcsObjectIdentifiers.IdRsaesOaep,
  221. new RsaesOaepParameters(
  222. digestAlgId,
  223. new AlgorithmIdentifier(mgfOid),
  224. RsaesOaepParameters.DefaultPSourceAlgorithm));
  225. }
  226. else
  227. {
  228. this.algId = new AlgorithmIdentifier(
  229. PkcsObjectIdentifiers.IdRsaesOaep,
  230. new RsaesOaepParameters(
  231. digestAlgId,
  232. new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, new AlgorithmIdentifier(mgfOid, DerNull.Instance)),
  233. RsaesOaepParameters.DefaultPSourceAlgorithm));
  234. }
  235. this.engine = new OaepEncoding(new RsaBlindedEngine(), DigestUtilities.GetDigest(digestOid), DigestUtilities.GetDigest(mgfOid), null);
  236. this.engine.Init(forWrapping, parameters);
  237. }
  238. public object AlgorithmDetails
  239. {
  240. get { return algId; }
  241. }
  242. public IBlockResult Unwrap(byte[] cipherText, int offset, int length)
  243. {
  244. return new SimpleBlockResult(engine.ProcessBlock(cipherText, offset, length));
  245. }
  246. public IBlockResult Wrap(byte[] keyData)
  247. {
  248. return new SimpleBlockResult(engine.ProcessBlock(keyData, 0, keyData.Length));
  249. }
  250. }
  251. internal class RsaOaepWrapperProvider
  252. : WrapperProvider
  253. {
  254. private readonly DerObjectIdentifier digestOid;
  255. private readonly DerObjectIdentifier mgfOid;
  256. internal RsaOaepWrapperProvider(DerObjectIdentifier digestOid)
  257. {
  258. this.digestOid = digestOid;
  259. this.mgfOid = digestOid;
  260. }
  261. internal RsaOaepWrapperProvider(DerObjectIdentifier digestOid, DerObjectIdentifier mgfOid)
  262. {
  263. this.digestOid = digestOid;
  264. this.mgfOid = mgfOid;
  265. }
  266. object WrapperProvider.CreateWrapper(bool forWrapping, ICipherParameters parameters)
  267. {
  268. return new RsaOaepWrapper(forWrapping, parameters, digestOid, mgfOid);
  269. }
  270. }
  271. }
  272. #pragma warning restore
  273. #endif