PasswordRecipientInfo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 PasswordRecipientInfo
  9. : Asn1Encodable
  10. {
  11. private readonly DerInteger version;
  12. private readonly AlgorithmIdentifier keyDerivationAlgorithm;
  13. private readonly AlgorithmIdentifier keyEncryptionAlgorithm;
  14. private readonly Asn1OctetString encryptedKey;
  15. public PasswordRecipientInfo(
  16. AlgorithmIdentifier keyEncryptionAlgorithm,
  17. Asn1OctetString encryptedKey)
  18. {
  19. this.version = new DerInteger(0);
  20. this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
  21. this.encryptedKey = encryptedKey;
  22. }
  23. public PasswordRecipientInfo(
  24. AlgorithmIdentifier keyDerivationAlgorithm,
  25. AlgorithmIdentifier keyEncryptionAlgorithm,
  26. Asn1OctetString encryptedKey)
  27. {
  28. this.version = new DerInteger(0);
  29. this.keyDerivationAlgorithm = keyDerivationAlgorithm;
  30. this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
  31. this.encryptedKey = encryptedKey;
  32. }
  33. public PasswordRecipientInfo(
  34. Asn1Sequence seq)
  35. {
  36. version = (DerInteger) seq[0];
  37. if (seq[1] is Asn1TaggedObject)
  38. {
  39. keyDerivationAlgorithm = AlgorithmIdentifier.GetInstance((Asn1TaggedObject) seq[1], false);
  40. keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]);
  41. encryptedKey = (Asn1OctetString) seq[3];
  42. }
  43. else
  44. {
  45. keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
  46. encryptedKey = (Asn1OctetString) seq[2];
  47. }
  48. }
  49. /**
  50. * return a PasswordRecipientInfo object from a tagged object.
  51. *
  52. * @param obj the tagged object holding the object we want.
  53. * @param explicitly true if the object is meant to be explicitly
  54. * tagged false otherwise.
  55. * @exception ArgumentException if the object held by the
  56. * tagged object cannot be converted.
  57. */
  58. public static PasswordRecipientInfo GetInstance(
  59. Asn1TaggedObject obj,
  60. bool explicitly)
  61. {
  62. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  63. }
  64. /**
  65. * return a PasswordRecipientInfo object from the given object.
  66. *
  67. * @param obj the object we want converted.
  68. * @exception ArgumentException if the object cannot be converted.
  69. */
  70. public static PasswordRecipientInfo GetInstance(
  71. object obj)
  72. {
  73. if (obj == null || obj is PasswordRecipientInfo)
  74. return (PasswordRecipientInfo) obj;
  75. if (obj is Asn1Sequence)
  76. return new PasswordRecipientInfo((Asn1Sequence) obj);
  77. throw new ArgumentException("Invalid PasswordRecipientInfo: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  78. }
  79. public DerInteger Version
  80. {
  81. get { return version; }
  82. }
  83. public AlgorithmIdentifier KeyDerivationAlgorithm
  84. {
  85. get { return keyDerivationAlgorithm; }
  86. }
  87. public AlgorithmIdentifier KeyEncryptionAlgorithm
  88. {
  89. get { return keyEncryptionAlgorithm; }
  90. }
  91. public Asn1OctetString EncryptedKey
  92. {
  93. get { return encryptedKey; }
  94. }
  95. /**
  96. * Produce an object suitable for an Asn1OutputStream.
  97. * <pre>
  98. * PasswordRecipientInfo ::= Sequence {
  99. * version CMSVersion, -- Always set to 0
  100. * keyDerivationAlgorithm [0] KeyDerivationAlgorithmIdentifier
  101. * OPTIONAL,
  102. * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
  103. * encryptedKey EncryptedKey }
  104. * </pre>
  105. */
  106. public override Asn1Object ToAsn1Object()
  107. {
  108. Asn1EncodableVector v = new Asn1EncodableVector(version);
  109. v.AddOptionalTagged(false, 0, keyDerivationAlgorithm);
  110. v.Add(keyEncryptionAlgorithm, encryptedKey);
  111. return new DerSequence(v);
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif