KeyTransRecipientInfoGenerator.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. internal class KeyTransRecipientInfoGenerator : RecipientInfoGenerator
  15. {
  16. private static readonly CmsEnvelopedHelper Helper = CmsEnvelopedHelper.Instance;
  17. private TbsCertificateStructure recipientTbsCert;
  18. private AsymmetricKeyParameter recipientPublicKey;
  19. private Asn1OctetString subjectKeyIdentifier;
  20. // Derived fields
  21. private SubjectPublicKeyInfo info;
  22. internal KeyTransRecipientInfoGenerator()
  23. {
  24. }
  25. internal X509Certificate RecipientCert
  26. {
  27. set
  28. {
  29. this.recipientTbsCert = CmsUtilities.GetTbsCertificateStructure(value);
  30. this.recipientPublicKey = value.GetPublicKey();
  31. this.info = recipientTbsCert.SubjectPublicKeyInfo;
  32. }
  33. }
  34. internal AsymmetricKeyParameter RecipientPublicKey
  35. {
  36. set
  37. {
  38. this.recipientPublicKey = value;
  39. try
  40. {
  41. info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(
  42. recipientPublicKey);
  43. }
  44. catch (IOException)
  45. {
  46. throw new ArgumentException("can't extract key algorithm from this key");
  47. }
  48. }
  49. }
  50. internal Asn1OctetString SubjectKeyIdentifier
  51. {
  52. set { this.subjectKeyIdentifier = value; }
  53. }
  54. public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
  55. {
  56. byte[] keyBytes = contentEncryptionKey.GetKey();
  57. AlgorithmIdentifier keyEncryptionAlgorithm = info.AlgorithmID;
  58. IWrapper keyWrapper = Helper.CreateWrapper(keyEncryptionAlgorithm.Algorithm.Id);
  59. keyWrapper.Init(true, new ParametersWithRandom(recipientPublicKey, random));
  60. byte[] encryptedKeyBytes = keyWrapper.Wrap(keyBytes, 0, keyBytes.Length);
  61. RecipientIdentifier recipId;
  62. if (recipientTbsCert != null)
  63. {
  64. IssuerAndSerialNumber issuerAndSerial = new IssuerAndSerialNumber(
  65. recipientTbsCert.Issuer, recipientTbsCert.SerialNumber.Value);
  66. recipId = new RecipientIdentifier(issuerAndSerial);
  67. }
  68. else
  69. {
  70. recipId = new RecipientIdentifier(subjectKeyIdentifier);
  71. }
  72. return new RecipientInfo(new KeyTransRecipientInfo(recipId, keyEncryptionAlgorithm,
  73. new DerOctetString(encryptedKeyBytes)));
  74. }
  75. }
  76. }
  77. #pragma warning restore
  78. #endif