EncryptedData.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  6. {
  7. public class EncryptedData
  8. : Asn1Encodable
  9. {
  10. private readonly DerInteger version;
  11. private readonly EncryptedContentInfo encryptedContentInfo;
  12. private readonly Asn1Set unprotectedAttrs;
  13. public static EncryptedData GetInstance(
  14. object obj)
  15. {
  16. if (obj is EncryptedData)
  17. return (EncryptedData) obj;
  18. if (obj is Asn1Sequence)
  19. return new EncryptedData((Asn1Sequence) obj);
  20. throw new ArgumentException("Invalid EncryptedData: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  21. }
  22. public EncryptedData(
  23. EncryptedContentInfo encInfo)
  24. : this(encInfo, null)
  25. {
  26. }
  27. public EncryptedData(
  28. EncryptedContentInfo encInfo,
  29. Asn1Set unprotectedAttrs)
  30. {
  31. if (encInfo == null)
  32. throw new ArgumentNullException("encInfo");
  33. this.version = new DerInteger((unprotectedAttrs == null) ? 0 : 2);
  34. this.encryptedContentInfo = encInfo;
  35. this.unprotectedAttrs = unprotectedAttrs;
  36. }
  37. private EncryptedData(
  38. Asn1Sequence seq)
  39. {
  40. if (seq == null)
  41. throw new ArgumentNullException("seq");
  42. if (seq.Count < 2 || seq.Count > 3)
  43. throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
  44. this.version = DerInteger.GetInstance(seq[0]);
  45. this.encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[1]);
  46. if (seq.Count > 2)
  47. {
  48. this.unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[2], false);
  49. }
  50. }
  51. public virtual DerInteger Version
  52. {
  53. get { return version; }
  54. }
  55. public virtual EncryptedContentInfo EncryptedContentInfo
  56. {
  57. get { return encryptedContentInfo; }
  58. }
  59. public virtual Asn1Set UnprotectedAttrs
  60. {
  61. get { return unprotectedAttrs; }
  62. }
  63. /**
  64. * <pre>
  65. * EncryptedData ::= SEQUENCE {
  66. * version CMSVersion,
  67. * encryptedContentInfo EncryptedContentInfo,
  68. * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL }
  69. * </pre>
  70. * @return a basic ASN.1 object representation.
  71. */
  72. public override Asn1Object ToAsn1Object()
  73. {
  74. Asn1EncodableVector v = new Asn1EncodableVector(version, encryptedContentInfo);
  75. if (unprotectedAttrs != null)
  76. {
  77. v.Add(new BerTaggedObject(false, 1, unprotectedAttrs));
  78. }
  79. return new BerSequence(v);
  80. }
  81. }
  82. }
  83. #pragma warning restore
  84. #endif