CMSEnvelopedHelper.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  16. {
  17. class CmsEnvelopedHelper
  18. {
  19. internal static readonly CmsEnvelopedHelper Instance = new CmsEnvelopedHelper();
  20. private static readonly IDictionary KeySizes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  21. private static readonly IDictionary BaseCipherNames = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  22. static CmsEnvelopedHelper()
  23. {
  24. KeySizes.Add(CmsEnvelopedGenerator.DesEde3Cbc, 192);
  25. KeySizes.Add(CmsEnvelopedGenerator.Aes128Cbc, 128);
  26. KeySizes.Add(CmsEnvelopedGenerator.Aes192Cbc, 192);
  27. KeySizes.Add(CmsEnvelopedGenerator.Aes256Cbc, 256);
  28. BaseCipherNames.Add(CmsEnvelopedGenerator.DesEde3Cbc, "DESEDE");
  29. BaseCipherNames.Add(CmsEnvelopedGenerator.Aes128Cbc, "AES");
  30. BaseCipherNames.Add(CmsEnvelopedGenerator.Aes192Cbc, "AES");
  31. BaseCipherNames.Add(CmsEnvelopedGenerator.Aes256Cbc, "AES");
  32. }
  33. private string GetAsymmetricEncryptionAlgName(
  34. string encryptionAlgOid)
  35. {
  36. if (Asn1.Pkcs.PkcsObjectIdentifiers.RsaEncryption.Id.Equals(encryptionAlgOid))
  37. {
  38. return "RSA/ECB/PKCS1Padding";
  39. }
  40. return encryptionAlgOid;
  41. }
  42. internal IBufferedCipher CreateAsymmetricCipher(
  43. string encryptionOid)
  44. {
  45. string asymName = GetAsymmetricEncryptionAlgName(encryptionOid);
  46. if (!asymName.Equals(encryptionOid))
  47. {
  48. try
  49. {
  50. return CipherUtilities.GetCipher(asymName);
  51. }
  52. catch (SecurityUtilityException)
  53. {
  54. // Ignore
  55. }
  56. }
  57. return CipherUtilities.GetCipher(encryptionOid);
  58. }
  59. internal IWrapper CreateWrapper(
  60. string encryptionOid)
  61. {
  62. try
  63. {
  64. return WrapperUtilities.GetWrapper(encryptionOid);
  65. }
  66. catch (SecurityUtilityException)
  67. {
  68. return WrapperUtilities.GetWrapper(GetAsymmetricEncryptionAlgName(encryptionOid));
  69. }
  70. }
  71. internal string GetRfc3211WrapperName(
  72. string oid)
  73. {
  74. if (oid == null)
  75. throw new ArgumentNullException("oid");
  76. string alg = (string) BaseCipherNames[oid];
  77. if (alg == null)
  78. throw new ArgumentException("no name for " + oid, "oid");
  79. return alg + "RFC3211Wrap";
  80. }
  81. internal int GetKeySize(
  82. string oid)
  83. {
  84. if (!KeySizes.Contains(oid))
  85. {
  86. throw new ArgumentException("no keysize for " + oid, "oid");
  87. }
  88. return (int) KeySizes[oid];
  89. }
  90. internal static RecipientInformationStore BuildRecipientInformationStore(
  91. Asn1Set recipientInfos, CmsSecureReadable secureReadable)
  92. {
  93. IList infos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  94. for (int i = 0; i != recipientInfos.Count; i++)
  95. {
  96. RecipientInfo info = RecipientInfo.GetInstance(recipientInfos[i]);
  97. ReadRecipientInfo(infos, info, secureReadable);
  98. }
  99. return new RecipientInformationStore(infos);
  100. }
  101. private static void ReadRecipientInfo(
  102. IList infos, RecipientInfo info, CmsSecureReadable secureReadable)
  103. {
  104. Asn1Encodable recipInfo = info.Info;
  105. if (recipInfo is KeyTransRecipientInfo)
  106. {
  107. infos.Add(new KeyTransRecipientInformation((KeyTransRecipientInfo)recipInfo, secureReadable));
  108. }
  109. else if (recipInfo is KekRecipientInfo)
  110. {
  111. infos.Add(new KekRecipientInformation((KekRecipientInfo)recipInfo, secureReadable));
  112. }
  113. else if (recipInfo is KeyAgreeRecipientInfo)
  114. {
  115. KeyAgreeRecipientInformation.ReadRecipientInfo(infos, (KeyAgreeRecipientInfo)recipInfo, secureReadable);
  116. }
  117. else if (recipInfo is PasswordRecipientInfo)
  118. {
  119. infos.Add(new PasswordRecipientInformation((PasswordRecipientInfo)recipInfo, secureReadable));
  120. }
  121. }
  122. internal class CmsAuthenticatedSecureReadable : CmsSecureReadable
  123. {
  124. private AlgorithmIdentifier algorithm;
  125. private IMac mac;
  126. private CmsReadable readable;
  127. internal CmsAuthenticatedSecureReadable(AlgorithmIdentifier algorithm, CmsReadable readable)
  128. {
  129. this.algorithm = algorithm;
  130. this.readable = readable;
  131. }
  132. public AlgorithmIdentifier Algorithm
  133. {
  134. get { return this.algorithm; }
  135. }
  136. public object CryptoObject
  137. {
  138. get { return this.mac; }
  139. }
  140. public CmsReadable GetReadable(KeyParameter sKey)
  141. {
  142. string macAlg = this.algorithm.Algorithm.Id;
  143. // Asn1Object sParams = this.algorithm.Parameters.ToAsn1Object();
  144. try
  145. {
  146. this.mac = MacUtilities.GetMac(macAlg);
  147. // FIXME Support for MAC algorithm parameters similar to cipher parameters
  148. // ASN1Object sParams = (ASN1Object)macAlg.getParameters();
  149. //
  150. // if (sParams != null && !(sParams instanceof ASN1Null))
  151. // {
  152. // AlgorithmParameters params = CMSEnvelopedHelper.INSTANCE.createAlgorithmParameters(macAlg.getObjectId().getId(), provider);
  153. //
  154. // params.init(sParams.getEncoded(), "ASN.1");
  155. //
  156. // mac.init(sKey, params.getParameterSpec(IvParameterSpec.class));
  157. // }
  158. // else
  159. {
  160. mac.Init(sKey);
  161. }
  162. // Asn1Object asn1Params = asn1Enc == null ? null : asn1Enc.ToAsn1Object();
  163. //
  164. // ICipherParameters cipherParameters = sKey;
  165. //
  166. // if (asn1Params != null && !(asn1Params is Asn1Null))
  167. // {
  168. // cipherParameters = ParameterUtilities.GetCipherParameters(
  169. // macAlg.Algorithm, cipherParameters, asn1Params);
  170. // }
  171. // else
  172. // {
  173. // string alg = macAlg.Algorithm.Id;
  174. // if (alg.Equals(CmsEnvelopedDataGenerator.DesEde3Cbc)
  175. // || alg.Equals(CmsEnvelopedDataGenerator.IdeaCbc)
  176. // || alg.Equals(CmsEnvelopedDataGenerator.Cast5Cbc))
  177. // {
  178. // cipherParameters = new ParametersWithIV(cipherParameters, new byte[8]);
  179. // }
  180. // }
  181. //
  182. // mac.Init(cipherParameters);
  183. }
  184. catch (SecurityUtilityException e)
  185. {
  186. throw new CmsException("couldn't create cipher.", e);
  187. }
  188. catch (InvalidKeyException e)
  189. {
  190. throw new CmsException("key invalid in message.", e);
  191. }
  192. catch (IOException e)
  193. {
  194. throw new CmsException("error decoding algorithm parameters.", e);
  195. }
  196. try
  197. {
  198. return new CmsProcessableInputStream(
  199. new TeeInputStream(
  200. readable.GetInputStream(),
  201. new MacSink(this.mac)));
  202. }
  203. catch (IOException e)
  204. {
  205. throw new CmsException("error reading content.", e);
  206. }
  207. }
  208. }
  209. internal class CmsEnvelopedSecureReadable : CmsSecureReadable
  210. {
  211. private AlgorithmIdentifier algorithm;
  212. private IBufferedCipher cipher;
  213. private CmsReadable readable;
  214. internal CmsEnvelopedSecureReadable(AlgorithmIdentifier algorithm, CmsReadable readable)
  215. {
  216. this.algorithm = algorithm;
  217. this.readable = readable;
  218. }
  219. public AlgorithmIdentifier Algorithm
  220. {
  221. get { return this.algorithm; }
  222. }
  223. public object CryptoObject
  224. {
  225. get { return this.cipher; }
  226. }
  227. public CmsReadable GetReadable(KeyParameter sKey)
  228. {
  229. try
  230. {
  231. this.cipher = CipherUtilities.GetCipher(this.algorithm.Algorithm);
  232. Asn1Encodable asn1Enc = this.algorithm.Parameters;
  233. Asn1Object asn1Params = asn1Enc == null ? null : asn1Enc.ToAsn1Object();
  234. ICipherParameters cipherParameters = sKey;
  235. if (asn1Params != null && !(asn1Params is Asn1Null))
  236. {
  237. cipherParameters = ParameterUtilities.GetCipherParameters(
  238. this.algorithm.Algorithm, cipherParameters, asn1Params);
  239. }
  240. else
  241. {
  242. string alg = this.algorithm.Algorithm.Id;
  243. if (alg.Equals(CmsEnvelopedDataGenerator.DesEde3Cbc)
  244. || alg.Equals(CmsEnvelopedDataGenerator.IdeaCbc)
  245. || alg.Equals(CmsEnvelopedDataGenerator.Cast5Cbc))
  246. {
  247. cipherParameters = new ParametersWithIV(cipherParameters, new byte[8]);
  248. }
  249. }
  250. cipher.Init(false, cipherParameters);
  251. }
  252. catch (SecurityUtilityException e)
  253. {
  254. throw new CmsException("couldn't create cipher.", e);
  255. }
  256. catch (InvalidKeyException e)
  257. {
  258. throw new CmsException("key invalid in message.", e);
  259. }
  260. catch (IOException e)
  261. {
  262. throw new CmsException("error decoding algorithm parameters.", e);
  263. }
  264. try
  265. {
  266. return new CmsProcessableInputStream(
  267. new CipherStream(readable.GetInputStream(), cipher, null));
  268. }
  269. catch (IOException e)
  270. {
  271. throw new CmsException("error reading content.", e);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. #pragma warning restore
  278. #endif