KeyTransRecipientInfoGenerator.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  12. {
  13. public class KeyTransRecipientInfoGenerator
  14. : RecipientInfoGenerator
  15. {
  16. private readonly IKeyWrapper m_keyWrapper;
  17. private IssuerAndSerialNumber m_issuerAndSerialNumber;
  18. private Asn1OctetString m_subjectKeyIdentifier;
  19. public KeyTransRecipientInfoGenerator(X509Certificate recipCert, IKeyWrapper keyWrapper)
  20. : this(new IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber)), keyWrapper)
  21. {
  22. }
  23. public KeyTransRecipientInfoGenerator(IssuerAndSerialNumber issuerAndSerial, IKeyWrapper keyWrapper)
  24. {
  25. m_issuerAndSerialNumber = issuerAndSerial;
  26. m_keyWrapper = keyWrapper;
  27. }
  28. public KeyTransRecipientInfoGenerator(byte[] subjectKeyID, IKeyWrapper keyWrapper)
  29. {
  30. m_subjectKeyIdentifier = new DerOctetString(subjectKeyID);
  31. m_keyWrapper = keyWrapper;
  32. }
  33. public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
  34. {
  35. AlgorithmIdentifier keyEncryptionAlgorithm = AlgorithmDetails;
  36. byte[] encryptedKeyBytes = GenerateWrappedKey(contentEncryptionKey);
  37. RecipientIdentifier recipId;
  38. if (m_issuerAndSerialNumber != null)
  39. {
  40. recipId = new RecipientIdentifier(m_issuerAndSerialNumber);
  41. }
  42. else
  43. {
  44. recipId = new RecipientIdentifier(m_subjectKeyIdentifier);
  45. }
  46. return new RecipientInfo(new KeyTransRecipientInfo(recipId, keyEncryptionAlgorithm,
  47. new DerOctetString(encryptedKeyBytes)));
  48. }
  49. protected virtual AlgorithmIdentifier AlgorithmDetails
  50. {
  51. get { return (AlgorithmIdentifier)m_keyWrapper.AlgorithmDetails; }
  52. }
  53. protected virtual byte[] GenerateWrappedKey(KeyParameter contentEncryptionKey)
  54. {
  55. return m_keyWrapper.Wrap(contentEncryptionKey.GetKey()).Collect();
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif