KEKRecipientInfo.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class KekRecipientInfo
  9. : Asn1Encodable
  10. {
  11. private DerInteger version;
  12. private KekIdentifier kekID;
  13. private AlgorithmIdentifier keyEncryptionAlgorithm;
  14. private Asn1OctetString encryptedKey;
  15. public KekRecipientInfo(
  16. KekIdentifier kekID,
  17. AlgorithmIdentifier keyEncryptionAlgorithm,
  18. Asn1OctetString encryptedKey)
  19. {
  20. this.version = new DerInteger(4);
  21. this.kekID = kekID;
  22. this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
  23. this.encryptedKey = encryptedKey;
  24. }
  25. public KekRecipientInfo(
  26. Asn1Sequence seq)
  27. {
  28. version = (DerInteger) seq[0];
  29. kekID = KekIdentifier.GetInstance(seq[1]);
  30. keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]);
  31. encryptedKey = (Asn1OctetString) seq[3];
  32. }
  33. /**
  34. * return a KekRecipientInfo object from a tagged object.
  35. *
  36. * @param obj the tagged object holding the object we want.
  37. * @param explicitly true if the object is meant to be explicitly
  38. * tagged false otherwise.
  39. * @exception ArgumentException if the object held by the
  40. * tagged object cannot be converted.
  41. */
  42. public static KekRecipientInfo GetInstance(
  43. Asn1TaggedObject obj,
  44. bool explicitly)
  45. {
  46. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  47. }
  48. /**
  49. * return a KekRecipientInfo object from the given object.
  50. *
  51. * @param obj the object we want converted.
  52. * @exception ArgumentException if the object cannot be converted.
  53. */
  54. public static KekRecipientInfo GetInstance(
  55. object obj)
  56. {
  57. if (obj == null || obj is KekRecipientInfo)
  58. return (KekRecipientInfo)obj;
  59. if(obj is Asn1Sequence)
  60. return new KekRecipientInfo((Asn1Sequence)obj);
  61. throw new ArgumentException("Invalid KekRecipientInfo: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  62. }
  63. public DerInteger Version
  64. {
  65. get { return version; }
  66. }
  67. public KekIdentifier KekID
  68. {
  69. get { return kekID; }
  70. }
  71. public AlgorithmIdentifier KeyEncryptionAlgorithm
  72. {
  73. get { return keyEncryptionAlgorithm; }
  74. }
  75. public Asn1OctetString EncryptedKey
  76. {
  77. get { return encryptedKey; }
  78. }
  79. /**
  80. * Produce an object suitable for an Asn1OutputStream.
  81. * <pre>
  82. * KekRecipientInfo ::= Sequence {
  83. * version CMSVersion, -- always set to 4
  84. * kekID KekIdentifier,
  85. * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
  86. * encryptedKey EncryptedKey
  87. * }
  88. * </pre>
  89. */
  90. public override Asn1Object ToAsn1Object()
  91. {
  92. return new DerSequence(version, kekID, keyEncryptionAlgorithm, encryptedKey);
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif