AuthEnvelopedDataParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. *
  9. * <pre>
  10. * AuthEnvelopedData ::= SEQUENCE {
  11. * version CMSVersion,
  12. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  13. * recipientInfos RecipientInfos,
  14. * authEncryptedContentInfo EncryptedContentInfo,
  15. * authAttrs [1] IMPLICIT AuthAttributes OPTIONAL,
  16. * mac MessageAuthenticationCode,
  17. * unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }
  18. * </pre>
  19. */
  20. public class AuthEnvelopedDataParser
  21. {
  22. private Asn1SequenceParser seq;
  23. private DerInteger version;
  24. private IAsn1Convertible nextObject;
  25. private bool originatorInfoCalled;
  26. private bool isData;
  27. public AuthEnvelopedDataParser(
  28. Asn1SequenceParser seq)
  29. {
  30. this.seq = seq;
  31. // "It MUST be set to 0."
  32. this.version = (DerInteger)seq.ReadObject();
  33. if (!version.HasValue(0))
  34. throw new Asn1ParsingException("AuthEnvelopedData version number must be 0");
  35. }
  36. public DerInteger Version
  37. {
  38. get { return version; }
  39. }
  40. public OriginatorInfo GetOriginatorInfo()
  41. {
  42. originatorInfoCalled = true;
  43. if (nextObject == null)
  44. {
  45. nextObject = seq.ReadObject();
  46. }
  47. if (nextObject is Asn1TaggedObjectParser o)
  48. {
  49. if (o.HasContextTag(0))
  50. {
  51. Asn1SequenceParser originatorInfo = (Asn1SequenceParser)o.ParseBaseUniversal(false, Asn1Tags.Sequence);
  52. nextObject = null;
  53. return OriginatorInfo.GetInstance(originatorInfo.ToAsn1Object());
  54. }
  55. }
  56. return null;
  57. }
  58. public Asn1SetParser GetRecipientInfos()
  59. {
  60. if (!originatorInfoCalled)
  61. {
  62. GetOriginatorInfo();
  63. }
  64. if (nextObject == null)
  65. {
  66. nextObject = seq.ReadObject();
  67. }
  68. Asn1SetParser recipientInfos = (Asn1SetParser)nextObject;
  69. nextObject = null;
  70. return recipientInfos;
  71. }
  72. public EncryptedContentInfoParser GetAuthEncryptedContentInfo()
  73. {
  74. if (nextObject == null)
  75. {
  76. nextObject = seq.ReadObject();
  77. }
  78. if (nextObject != null)
  79. {
  80. Asn1SequenceParser o = (Asn1SequenceParser) nextObject;
  81. nextObject = null;
  82. EncryptedContentInfoParser encryptedContentInfoParser = new EncryptedContentInfoParser(o);
  83. isData = CmsObjectIdentifiers.Data.Equals(encryptedContentInfoParser.ContentType);
  84. return encryptedContentInfoParser;
  85. }
  86. return null;
  87. }
  88. public Asn1SetParser GetAuthAttrs()
  89. {
  90. if (nextObject == null)
  91. {
  92. nextObject = seq.ReadObject();
  93. }
  94. if (nextObject is Asn1TaggedObjectParser o)
  95. {
  96. nextObject = null;
  97. return (Asn1SetParser)Asn1Utilities.ParseContextBaseUniversal(o, 1, false, Asn1Tags.SetOf);
  98. }
  99. // "The authAttrs MUST be present if the content type carried in EncryptedContentInfo is not id-data."
  100. if (!isData)
  101. throw new Asn1ParsingException("authAttrs must be present with non-data content");
  102. return null;
  103. }
  104. public Asn1OctetString GetMac()
  105. {
  106. if (nextObject == null)
  107. {
  108. nextObject = seq.ReadObject();
  109. }
  110. IAsn1Convertible o = nextObject;
  111. nextObject = null;
  112. return Asn1OctetString.GetInstance(o.ToAsn1Object());
  113. }
  114. public Asn1SetParser GetUnauthAttrs()
  115. {
  116. if (nextObject == null)
  117. {
  118. nextObject = seq.ReadObject();
  119. }
  120. if (nextObject != null)
  121. {
  122. Asn1TaggedObjectParser o = (Asn1TaggedObjectParser)nextObject;
  123. nextObject = null;
  124. return (Asn1SetParser)Asn1Utilities.ParseContextBaseUniversal(o, 2, false, Asn1Tags.SetOf);
  125. }
  126. return null;
  127. }
  128. }
  129. }
  130. #pragma warning restore
  131. #endif