KeyTransRecipientInfoGenerator.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  13. {
  14. public class KeyTransRecipientInfoGenerator : RecipientInfoGenerator
  15. {
  16. private static readonly CmsEnvelopedHelper Helper = CmsEnvelopedHelper.Instance;
  17. private Asn1OctetString subjectKeyIdentifier;
  18. private IKeyWrapper keyWrapper;
  19. // Derived fields
  20. private SubjectPublicKeyInfo info;
  21. private IssuerAndSerialNumber issuerAndSerialNumber;
  22. private SecureRandom random;
  23. public KeyTransRecipientInfoGenerator(X509Certificate recipCert, IKeyWrapper keyWrapper)
  24. : this(new Asn1.Cms.IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber)), keyWrapper)
  25. {
  26. }
  27. public KeyTransRecipientInfoGenerator(IssuerAndSerialNumber issuerAndSerial, IKeyWrapper keyWrapper)
  28. {
  29. this.issuerAndSerialNumber = issuerAndSerial;
  30. this.keyWrapper = keyWrapper;
  31. }
  32. public KeyTransRecipientInfoGenerator(byte[] subjectKeyID, IKeyWrapper keyWrapper)
  33. {
  34. this.subjectKeyIdentifier = new DerOctetString(subjectKeyIdentifier);
  35. this.keyWrapper = keyWrapper;
  36. }
  37. public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
  38. {
  39. AlgorithmIdentifier keyEncryptionAlgorithm = this.AlgorithmDetails;
  40. this.random = random;
  41. byte[] encryptedKeyBytes = GenerateWrappedKey(contentEncryptionKey);
  42. RecipientIdentifier recipId;
  43. if (issuerAndSerialNumber != null)
  44. {
  45. recipId = new RecipientIdentifier(issuerAndSerialNumber);
  46. }
  47. else
  48. {
  49. recipId = new RecipientIdentifier(subjectKeyIdentifier);
  50. }
  51. return new RecipientInfo(new KeyTransRecipientInfo(recipId, keyEncryptionAlgorithm,
  52. new DerOctetString(encryptedKeyBytes)));
  53. }
  54. protected virtual AlgorithmIdentifier AlgorithmDetails
  55. {
  56. get
  57. {
  58. if (this.keyWrapper != null)
  59. {
  60. return (AlgorithmIdentifier)keyWrapper.AlgorithmDetails;
  61. }
  62. return info.AlgorithmID;
  63. }
  64. }
  65. protected virtual byte[] GenerateWrappedKey(KeyParameter contentEncryptionKey)
  66. {
  67. return keyWrapper.Wrap(contentEncryptionKey.GetKey()).Collect();
  68. }
  69. }
  70. }
  71. #pragma warning restore
  72. #endif