SignedDataParser.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.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: " + BestHTTP.SecureProtocol.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 && ((Asn1TaggedObjectParser)_nextObject).TagNo == 0)
  59. {
  60. Asn1SetParser certs = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(Asn1Tags.Set, false);
  61. _nextObject = null;
  62. return certs;
  63. }
  64. return null;
  65. }
  66. public Asn1SetParser GetCrls()
  67. {
  68. if (!_certsCalled)
  69. throw new IOException("GetCerts() has not been called.");
  70. _crlsCalled = true;
  71. if (_nextObject == null)
  72. {
  73. _nextObject = _seq.ReadObject();
  74. }
  75. if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 1)
  76. {
  77. Asn1SetParser crls = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(Asn1Tags.Set, false);
  78. _nextObject = null;
  79. return crls;
  80. }
  81. return null;
  82. }
  83. public Asn1SetParser GetSignerInfos()
  84. {
  85. if (!_certsCalled || !_crlsCalled)
  86. throw new IOException("GetCerts() and/or GetCrls() has not been called.");
  87. if (_nextObject == null)
  88. {
  89. _nextObject = _seq.ReadObject();
  90. }
  91. return (Asn1SetParser)_nextObject;
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif