KEKRecipientInformation.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.Cms;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  11. {
  12. /**
  13. * the RecipientInfo class for a recipient who has been sent a message
  14. * encrypted using a secret key known to the other side.
  15. */
  16. public class KekRecipientInformation
  17. : RecipientInformation
  18. {
  19. private KekRecipientInfo info;
  20. internal KekRecipientInformation(
  21. KekRecipientInfo info,
  22. CmsSecureReadable secureReadable)
  23. : base(info.KeyEncryptionAlgorithm, secureReadable)
  24. {
  25. this.info = info;
  26. this.rid = new RecipientID();
  27. KekIdentifier kekId = info.KekID;
  28. rid.KeyIdentifier = kekId.KeyIdentifier.GetOctets();
  29. }
  30. /**
  31. * decrypt the content and return an input stream.
  32. */
  33. public override CmsTypedStream GetContentStream(
  34. ICipherParameters key)
  35. {
  36. try
  37. {
  38. byte[] encryptedKey = info.EncryptedKey.GetOctets();
  39. IWrapper keyWrapper = WrapperUtilities.GetWrapper(keyEncAlg.Algorithm.Id);
  40. keyWrapper.Init(false, key);
  41. KeyParameter sKey = ParameterUtilities.CreateKeyParameter(
  42. GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
  43. return GetContentFromSessionKey(sKey);
  44. }
  45. catch (SecurityUtilityException e)
  46. {
  47. throw new CmsException("couldn't create cipher.", e);
  48. }
  49. catch (InvalidKeyException e)
  50. {
  51. throw new CmsException("key invalid in message.", e);
  52. }
  53. }
  54. }
  55. }
  56. #pragma warning restore
  57. #endif