EncryptedData.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. int version = ((DerInteger) seq[0]).Value.IntValue;
  51. if (version != 0)
  52. {
  53. throw new ArgumentException("sequence not version 0");
  54. }
  55. this.data = (Asn1Sequence) seq[1];
  56. }
  57. public EncryptedData(
  58. DerObjectIdentifier contentType,
  59. AlgorithmIdentifier encryptionAlgorithm,
  60. Asn1Encodable content)
  61. {
  62. data = new BerSequence(
  63. contentType,
  64. encryptionAlgorithm.ToAsn1Object(),
  65. new BerTaggedObject(false, 0, content));
  66. }
  67. public DerObjectIdentifier ContentType
  68. {
  69. get { return (DerObjectIdentifier) data[0]; }
  70. }
  71. public AlgorithmIdentifier EncryptionAlgorithm
  72. {
  73. get { return AlgorithmIdentifier.GetInstance(data[1]); }
  74. }
  75. public Asn1OctetString Content
  76. {
  77. get
  78. {
  79. if (data.Count == 3)
  80. {
  81. DerTaggedObject o = (DerTaggedObject) data[2];
  82. return Asn1OctetString.GetInstance(o, false);
  83. }
  84. return null;
  85. }
  86. }
  87. public override Asn1Object ToAsn1Object()
  88. {
  89. return new BerSequence(new DerInteger(0), data);
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif