EnvelopedDataParser.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  5. {
  6. /**
  7. * Produce an object suitable for an Asn1OutputStream.
  8. * <pre>
  9. * EnvelopedData ::= SEQUENCE {
  10. * version CMSVersion,
  11. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  12. * recipientInfos RecipientInfos,
  13. * encryptedContentInfo EncryptedContentInfo,
  14. * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
  15. * }
  16. * </pre>
  17. */
  18. public class EnvelopedDataParser
  19. {
  20. private Asn1SequenceParser _seq;
  21. private DerInteger _version;
  22. private IAsn1Convertible _nextObject;
  23. private bool _originatorInfoCalled;
  24. public EnvelopedDataParser(
  25. Asn1SequenceParser seq)
  26. {
  27. this._seq = seq;
  28. this._version = (DerInteger)seq.ReadObject();
  29. }
  30. public DerInteger Version
  31. {
  32. get { return _version; }
  33. }
  34. public OriginatorInfo GetOriginatorInfo()
  35. {
  36. _originatorInfoCalled = true;
  37. if (_nextObject == null)
  38. {
  39. _nextObject = _seq.ReadObject();
  40. }
  41. if (_nextObject is Asn1TaggedObjectParser o)
  42. {
  43. if (o.HasContextTag(0))
  44. {
  45. Asn1SequenceParser originatorInfo = (Asn1SequenceParser)o.ParseBaseUniversal(false, Asn1Tags.Sequence);
  46. _nextObject = null;
  47. return OriginatorInfo.GetInstance(originatorInfo.ToAsn1Object());
  48. }
  49. }
  50. return null;
  51. }
  52. public Asn1SetParser GetRecipientInfos()
  53. {
  54. if (!_originatorInfoCalled)
  55. {
  56. GetOriginatorInfo();
  57. }
  58. if (_nextObject == null)
  59. {
  60. _nextObject = _seq.ReadObject();
  61. }
  62. Asn1SetParser recipientInfos = (Asn1SetParser)_nextObject;
  63. _nextObject = null;
  64. return recipientInfos;
  65. }
  66. public EncryptedContentInfoParser GetEncryptedContentInfo()
  67. {
  68. if (_nextObject == null)
  69. {
  70. _nextObject = _seq.ReadObject();
  71. }
  72. if (_nextObject != null)
  73. {
  74. Asn1SequenceParser o = (Asn1SequenceParser) _nextObject;
  75. _nextObject = null;
  76. return new EncryptedContentInfoParser(o);
  77. }
  78. return null;
  79. }
  80. public Asn1SetParser GetUnprotectedAttrs()
  81. {
  82. if (_nextObject == null)
  83. {
  84. _nextObject = _seq.ReadObject();
  85. }
  86. if (_nextObject != null)
  87. {
  88. Asn1TaggedObjectParser o = (Asn1TaggedObjectParser)_nextObject;
  89. _nextObject = null;
  90. return (Asn1SetParser)Asn1Utilities.ParseContextBaseUniversal(o, 1, false, Asn1Tags.SetOf);
  91. }
  92. return null;
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif