CMSAuthenticatedDataStreamGenerator.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  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. /**
  18. * General class for generating a CMS authenticated-data message stream.
  19. * <p>
  20. * A simple example of usage.
  21. * <pre>
  22. * CMSAuthenticatedDataStreamGenerator edGen = new CMSAuthenticatedDataStreamGenerator();
  23. *
  24. * edGen.addKeyTransRecipient(cert);
  25. *
  26. * ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  27. *
  28. * OutputStream out = edGen.open(
  29. * bOut, CMSAuthenticatedDataGenerator.AES128_CBC, "BC");*
  30. * out.write(data);
  31. *
  32. * out.close();
  33. * </pre>
  34. * </p>
  35. */
  36. public class CmsAuthenticatedDataStreamGenerator
  37. : CmsAuthenticatedGenerator
  38. {
  39. // TODO Add support
  40. // private object _originatorInfo = null;
  41. // private object _unprotectedAttributes = null;
  42. private int _bufferSize;
  43. private bool _berEncodeRecipientSet;
  44. /**
  45. * base constructor
  46. */
  47. public CmsAuthenticatedDataStreamGenerator()
  48. {
  49. }
  50. /**
  51. * constructor allowing specific source of randomness
  52. * @param rand instance of SecureRandom to use
  53. */
  54. public CmsAuthenticatedDataStreamGenerator(
  55. SecureRandom rand)
  56. : base(rand)
  57. {
  58. }
  59. /**
  60. * Set the underlying string size for encapsulated data
  61. *
  62. * @param bufferSize length of octet strings to buffer the data.
  63. */
  64. public void SetBufferSize(
  65. int bufferSize)
  66. {
  67. _bufferSize = bufferSize;
  68. }
  69. /**
  70. * Use a BER Set to store the recipient information
  71. */
  72. public void SetBerEncodeRecipients(
  73. bool berEncodeRecipientSet)
  74. {
  75. _berEncodeRecipientSet = berEncodeRecipientSet;
  76. }
  77. /**
  78. * generate an enveloped object that contains an CMS Enveloped Data
  79. * object using the given provider and the passed in key generator.
  80. * @throws java.io.IOException
  81. */
  82. private Stream Open(
  83. Stream outStr,
  84. string macOid,
  85. CipherKeyGenerator keyGen)
  86. {
  87. // FIXME Will this work for macs?
  88. byte[] encKeyBytes = keyGen.GenerateKey();
  89. KeyParameter encKey = ParameterUtilities.CreateKeyParameter(macOid, encKeyBytes);
  90. Asn1Encodable asn1Params = GenerateAsn1Parameters(macOid, encKeyBytes);
  91. ICipherParameters cipherParameters;
  92. AlgorithmIdentifier macAlgId = GetAlgorithmIdentifier(
  93. macOid, encKey, asn1Params, out cipherParameters);
  94. Asn1EncodableVector recipientInfos = new Asn1EncodableVector();
  95. foreach (RecipientInfoGenerator rig in recipientInfoGenerators)
  96. {
  97. try
  98. {
  99. recipientInfos.Add(rig.Generate(encKey, rand));
  100. }
  101. catch (InvalidKeyException e)
  102. {
  103. throw new CmsException("key inappropriate for algorithm.", e);
  104. }
  105. catch (GeneralSecurityException e)
  106. {
  107. throw new CmsException("error making encrypted content.", e);
  108. }
  109. }
  110. // FIXME Only passing key at the moment
  111. // return Open(outStr, macAlgId, cipherParameters, recipientInfos);
  112. return Open(outStr, macAlgId, encKey, recipientInfos);
  113. }
  114. protected Stream Open(
  115. Stream outStr,
  116. AlgorithmIdentifier macAlgId,
  117. ICipherParameters cipherParameters,
  118. Asn1EncodableVector recipientInfos)
  119. {
  120. try
  121. {
  122. //
  123. // ContentInfo
  124. //
  125. BerSequenceGenerator cGen = new BerSequenceGenerator(outStr);
  126. cGen.AddObject(CmsObjectIdentifiers.AuthenticatedData);
  127. //
  128. // Authenticated Data
  129. //
  130. BerSequenceGenerator authGen = new BerSequenceGenerator(
  131. cGen.GetRawOutputStream(), 0, true);
  132. authGen.AddObject(new DerInteger(AuthenticatedData.CalculateVersion(null)));
  133. Stream authRaw = authGen.GetRawOutputStream();
  134. Asn1Generator recipGen = _berEncodeRecipientSet
  135. ? (Asn1Generator) new BerSetGenerator(authRaw)
  136. : new DerSetGenerator(authRaw);
  137. foreach (Asn1Encodable ae in recipientInfos)
  138. {
  139. recipGen.AddObject(ae);
  140. }
  141. recipGen.Close();
  142. authGen.AddObject(macAlgId);
  143. BerSequenceGenerator eiGen = new BerSequenceGenerator(authRaw);
  144. eiGen.AddObject(CmsObjectIdentifiers.Data);
  145. Stream octetOutputStream = CmsUtilities.CreateBerOctetOutputStream(
  146. eiGen.GetRawOutputStream(), 0, true, _bufferSize);
  147. IMac mac = MacUtilities.GetMac(macAlgId.Algorithm);
  148. // TODO Confirm no ParametersWithRandom needed
  149. mac.Init(cipherParameters);
  150. Stream mOut = new TeeOutputStream(octetOutputStream, new MacSink(mac));
  151. return new CmsAuthenticatedDataOutputStream(mOut, mac, cGen, authGen, eiGen);
  152. }
  153. catch (SecurityUtilityException e)
  154. {
  155. throw new CmsException("couldn't create cipher.", e);
  156. }
  157. catch (InvalidKeyException e)
  158. {
  159. throw new CmsException("key invalid in message.", e);
  160. }
  161. catch (IOException e)
  162. {
  163. throw new CmsException("exception decoding algorithm parameters.", e);
  164. }
  165. }
  166. /**
  167. * generate an enveloped object that contains an CMS Enveloped Data object
  168. */
  169. public Stream Open(
  170. Stream outStr,
  171. string encryptionOid)
  172. {
  173. CipherKeyGenerator keyGen = GeneratorUtilities.GetKeyGenerator(encryptionOid);
  174. keyGen.Init(new KeyGenerationParameters(rand, keyGen.DefaultStrength));
  175. return Open(outStr, encryptionOid, keyGen);
  176. }
  177. /**
  178. * generate an enveloped object that contains an CMS Enveloped Data object
  179. */
  180. public Stream Open(
  181. Stream outStr,
  182. string encryptionOid,
  183. int keySize)
  184. {
  185. CipherKeyGenerator keyGen = GeneratorUtilities.GetKeyGenerator(encryptionOid);
  186. keyGen.Init(new KeyGenerationParameters(rand, keySize));
  187. return Open(outStr, encryptionOid, keyGen);
  188. }
  189. private class CmsAuthenticatedDataOutputStream
  190. : BaseOutputStream
  191. {
  192. private readonly Stream macStream;
  193. private readonly IMac mac;
  194. private readonly BerSequenceGenerator cGen;
  195. private readonly BerSequenceGenerator authGen;
  196. private readonly BerSequenceGenerator eiGen;
  197. public CmsAuthenticatedDataOutputStream(
  198. Stream macStream,
  199. IMac mac,
  200. BerSequenceGenerator cGen,
  201. BerSequenceGenerator authGen,
  202. BerSequenceGenerator eiGen)
  203. {
  204. this.macStream = macStream;
  205. this.mac = mac;
  206. this.cGen = cGen;
  207. this.authGen = authGen;
  208. this.eiGen = eiGen;
  209. }
  210. public override void WriteByte(
  211. byte b)
  212. {
  213. macStream.WriteByte(b);
  214. }
  215. public override void Write(
  216. byte[] bytes,
  217. int off,
  218. int len)
  219. {
  220. macStream.Write(bytes, off, len);
  221. }
  222. #if PORTABLE || NETFX_CORE
  223. protected override void Dispose(bool disposing)
  224. {
  225. if (disposing)
  226. {
  227. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(macStream);
  228. // TODO Parent context(s) should really be be closed explicitly
  229. eiGen.Close();
  230. // [TODO] auth attributes go here
  231. byte[] macOctets = MacUtilities.DoFinal(mac);
  232. authGen.AddObject(new DerOctetString(macOctets));
  233. // [TODO] unauth attributes go here
  234. authGen.Close();
  235. cGen.Close();
  236. }
  237. base.Dispose(disposing);
  238. }
  239. #else
  240. public override void Close()
  241. {
  242. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(macStream);
  243. // TODO Parent context(s) should really be be closed explicitly
  244. eiGen.Close();
  245. // [TODO] auth attributes go here
  246. byte[] macOctets = MacUtilities.DoFinal(mac);
  247. authGen.AddObject(new DerOctetString(macOctets));
  248. // [TODO] unauth attributes go here
  249. authGen.Close();
  250. cGen.Close();
  251. base.Close();
  252. }
  253. #endif
  254. }
  255. }
  256. }
  257. #pragma warning restore
  258. #endif