EncryptedData.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Pkcs
  7. {
  8. /**
  9. * The EncryptedData object.
  10. * <pre>
  11. * EncryptedData ::= Sequence {
  12. * version Version,
  13. * encryptedContentInfo EncryptedContentInfo
  14. * }
  15. *
  16. *
  17. * EncryptedContentInfo ::= Sequence {
  18. * contentType ContentType,
  19. * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
  20. * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
  21. * }
  22. *
  23. * EncryptedContent ::= OCTET STRING
  24. * </pre>
  25. */
  26. public class EncryptedData
  27. : Asn1Encodable
  28. {
  29. private readonly Asn1Sequence data;
  30. // private readonly DerObjectIdentifier bagId;
  31. // private readonly Asn1Object bagValue;
  32. public static EncryptedData GetInstance(
  33. object obj)
  34. {
  35. if (obj is EncryptedData)
  36. {
  37. return (EncryptedData) obj;
  38. }
  39. if (obj is Asn1Sequence)
  40. {
  41. return new EncryptedData((Asn1Sequence) obj);
  42. }
  43. throw new ArgumentException("Unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  44. }
  45. private EncryptedData(
  46. Asn1Sequence seq)
  47. {
  48. if (seq.Count != 2)
  49. throw new ArgumentException("Wrong number of elements in sequence", "seq");
  50. DerInteger version = (DerInteger)seq[0];
  51. if (!version.HasValue(0))
  52. throw new ArgumentException("sequence not version 0");
  53. this.data = (Asn1Sequence) seq[1];
  54. }
  55. public EncryptedData(
  56. DerObjectIdentifier contentType,
  57. AlgorithmIdentifier encryptionAlgorithm,
  58. Asn1Encodable content)
  59. {
  60. data = new BerSequence(
  61. contentType,
  62. encryptionAlgorithm.ToAsn1Object(),
  63. new BerTaggedObject(false, 0, content));
  64. }
  65. public DerObjectIdentifier ContentType
  66. {
  67. get { return (DerObjectIdentifier) data[0]; }
  68. }
  69. public AlgorithmIdentifier EncryptionAlgorithm
  70. {
  71. get { return AlgorithmIdentifier.GetInstance(data[1]); }
  72. }
  73. public Asn1OctetString Content
  74. {
  75. get
  76. {
  77. if (data.Count == 3)
  78. {
  79. DerTaggedObject o = (DerTaggedObject) data[2];
  80. return Asn1OctetString.GetInstance(o, false);
  81. }
  82. return null;
  83. }
  84. }
  85. public override Asn1Object ToAsn1Object()
  86. {
  87. return new BerSequence(new DerInteger(0), data);
  88. }
  89. }
  90. }
  91. #pragma warning restore
  92. #endif