SignedDataParser.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. /**
  9. * <pre>
  10. * SignedData ::= SEQUENCE {
  11. * version CMSVersion,
  12. * digestAlgorithms DigestAlgorithmIdentifiers,
  13. * encapContentInfo EncapsulatedContentInfo,
  14. * certificates [0] IMPLICIT CertificateSet OPTIONAL,
  15. * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  16. * signerInfos SignerInfos
  17. * }
  18. * </pre>
  19. */
  20. public class SignedDataParser
  21. {
  22. private Asn1SequenceParser _seq;
  23. private DerInteger _version;
  24. private object _nextObject;
  25. private bool _certsCalled;
  26. private bool _crlsCalled;
  27. public static SignedDataParser GetInstance(
  28. object o)
  29. {
  30. if (o is Asn1Sequence)
  31. return new SignedDataParser(((Asn1Sequence)o).Parser);
  32. if (o is Asn1SequenceParser)
  33. return new SignedDataParser((Asn1SequenceParser)o);
  34. throw new IOException("unknown object encountered: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(o));
  35. }
  36. public SignedDataParser(
  37. Asn1SequenceParser seq)
  38. {
  39. this._seq = seq;
  40. this._version = (DerInteger)seq.ReadObject();
  41. }
  42. public DerInteger Version
  43. {
  44. get { return _version; }
  45. }
  46. public Asn1SetParser GetDigestAlgorithms()
  47. {
  48. return (Asn1SetParser)_seq.ReadObject();
  49. }
  50. public ContentInfoParser GetEncapContentInfo()
  51. {
  52. return new ContentInfoParser((Asn1SequenceParser)_seq.ReadObject());
  53. }
  54. public Asn1SetParser GetCertificates()
  55. {
  56. _certsCalled = true;
  57. _nextObject = _seq.ReadObject();
  58. if (_nextObject is Asn1TaggedObjectParser o)
  59. {
  60. if (o.HasContextTag(0))
  61. {
  62. Asn1SetParser certs = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf);
  63. _nextObject = null;
  64. return certs;
  65. }
  66. }
  67. return null;
  68. }
  69. public Asn1SetParser GetCrls()
  70. {
  71. if (!_certsCalled)
  72. throw new IOException("GetCerts() has not been called.");
  73. _crlsCalled = true;
  74. if (_nextObject == null)
  75. {
  76. _nextObject = _seq.ReadObject();
  77. }
  78. if (_nextObject is Asn1TaggedObjectParser o)
  79. {
  80. if (o.HasContextTag(1))
  81. {
  82. Asn1SetParser crls = (Asn1SetParser)o.ParseBaseUniversal(false, Asn1Tags.SetOf);
  83. _nextObject = null;
  84. return crls;
  85. }
  86. }
  87. return null;
  88. }
  89. public Asn1SetParser GetSignerInfos()
  90. {
  91. if (!_certsCalled || !_crlsCalled)
  92. throw new IOException("GetCerts() and/or GetCrls() has not been called.");
  93. if (_nextObject == null)
  94. {
  95. _nextObject = _seq.ReadObject();
  96. }
  97. return (Asn1SetParser)_nextObject;
  98. }
  99. }
  100. }
  101. #pragma warning restore
  102. #endif