AuthEnvelopedDataParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.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 && ((Asn1TaggedObjectParser)nextObject).TagNo == 0)
  48. {
  49. Asn1SequenceParser originatorInfo = (Asn1SequenceParser) ((Asn1TaggedObjectParser)nextObject).GetObjectParser(Asn1Tags.Sequence, false);
  50. nextObject = null;
  51. return OriginatorInfo.GetInstance(originatorInfo.ToAsn1Object());
  52. }
  53. return null;
  54. }
  55. public Asn1SetParser GetRecipientInfos()
  56. {
  57. if (!originatorInfoCalled)
  58. {
  59. GetOriginatorInfo();
  60. }
  61. if (nextObject == null)
  62. {
  63. nextObject = seq.ReadObject();
  64. }
  65. Asn1SetParser recipientInfos = (Asn1SetParser)nextObject;
  66. nextObject = null;
  67. return recipientInfos;
  68. }
  69. public EncryptedContentInfoParser GetAuthEncryptedContentInfo()
  70. {
  71. if (nextObject == null)
  72. {
  73. nextObject = seq.ReadObject();
  74. }
  75. if (nextObject != null)
  76. {
  77. Asn1SequenceParser o = (Asn1SequenceParser) nextObject;
  78. nextObject = null;
  79. EncryptedContentInfoParser encryptedContentInfoParser = new EncryptedContentInfoParser(o);
  80. isData = CmsObjectIdentifiers.Data.Equals(encryptedContentInfoParser.ContentType);
  81. return encryptedContentInfoParser;
  82. }
  83. return null;
  84. }
  85. public Asn1SetParser GetAuthAttrs()
  86. {
  87. if (nextObject == null)
  88. {
  89. nextObject = seq.ReadObject();
  90. }
  91. if (nextObject is Asn1TaggedObjectParser)
  92. {
  93. IAsn1Convertible o = nextObject;
  94. nextObject = null;
  95. return (Asn1SetParser)((Asn1TaggedObjectParser)o).GetObjectParser(Asn1Tags.Set, false);
  96. }
  97. // "The authAttrs MUST be present if the content type carried in
  98. // EncryptedContentInfo is not id-data."
  99. if (!isData)
  100. throw new Asn1ParsingException("authAttrs must be present with non-data content");
  101. return null;
  102. }
  103. public Asn1OctetString GetMac()
  104. {
  105. if (nextObject == null)
  106. {
  107. nextObject = seq.ReadObject();
  108. }
  109. IAsn1Convertible o = nextObject;
  110. nextObject = null;
  111. return Asn1OctetString.GetInstance(o.ToAsn1Object());
  112. }
  113. public Asn1SetParser GetUnauthAttrs()
  114. {
  115. if (nextObject == null)
  116. {
  117. nextObject = seq.ReadObject();
  118. }
  119. if (nextObject != null)
  120. {
  121. IAsn1Convertible o = nextObject;
  122. nextObject = null;
  123. return (Asn1SetParser)((Asn1TaggedObjectParser)o).GetObjectParser(Asn1Tags.Set, false);
  124. }
  125. return null;
  126. }
  127. }
  128. }
  129. #pragma warning restore
  130. #endif