PasswordRecipientInformation.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Parameters;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  12. {
  13. /**
  14. * the RecipientInfo class for a recipient who has been sent a message
  15. * encrypted using a password.
  16. */
  17. public class PasswordRecipientInformation
  18. : RecipientInformation
  19. {
  20. private readonly PasswordRecipientInfo info;
  21. internal PasswordRecipientInformation(
  22. PasswordRecipientInfo info,
  23. CmsSecureReadable secureReadable)
  24. : base(info.KeyEncryptionAlgorithm, secureReadable)
  25. {
  26. this.info = info;
  27. this.rid = new RecipientID();
  28. }
  29. /**
  30. * return the object identifier for the key derivation algorithm, or null
  31. * if there is none present.
  32. *
  33. * @return OID for key derivation algorithm, if present.
  34. */
  35. public virtual AlgorithmIdentifier KeyDerivationAlgorithm
  36. {
  37. get { return info.KeyDerivationAlgorithm; }
  38. }
  39. /**
  40. * decrypt the content and return an input stream.
  41. */
  42. public override CmsTypedStream GetContentStream(
  43. ICipherParameters key)
  44. {
  45. try
  46. {
  47. AlgorithmIdentifier kekAlg = AlgorithmIdentifier.GetInstance(info.KeyEncryptionAlgorithm);
  48. Asn1Sequence kekAlgParams = (Asn1Sequence)kekAlg.Parameters;
  49. byte[] encryptedKey = info.EncryptedKey.GetOctets();
  50. string kekAlgName = DerObjectIdentifier.GetInstance(kekAlgParams[0]).Id;
  51. string cName = CmsEnvelopedHelper.Instance.GetRfc3211WrapperName(kekAlgName);
  52. IWrapper keyWrapper = WrapperUtilities.GetWrapper(cName);
  53. byte[] iv = Asn1OctetString.GetInstance(kekAlgParams[1]).GetOctets();
  54. ICipherParameters parameters = ((CmsPbeKey)key).GetEncoded(kekAlgName);
  55. parameters = new ParametersWithIV(parameters, iv);
  56. keyWrapper.Init(false, parameters);
  57. KeyParameter sKey = ParameterUtilities.CreateKeyParameter(
  58. GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
  59. return GetContentFromSessionKey(sKey);
  60. }
  61. catch (SecurityUtilityException e)
  62. {
  63. throw new CmsException("couldn't create cipher.", e);
  64. }
  65. catch (InvalidKeyException e)
  66. {
  67. throw new CmsException("key invalid in message.", e);
  68. }
  69. }
  70. }
  71. }
  72. #pragma warning restore
  73. #endif