X509CrlParser.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.X509
  12. {
  13. public class X509CrlParser
  14. {
  15. private static readonly PemParser PemCrlParser = new PemParser("CRL");
  16. private readonly bool lazyAsn1;
  17. private Asn1Set sCrlData;
  18. private int sCrlDataObjectCount;
  19. private Stream currentCrlStream;
  20. public X509CrlParser(bool lazyAsn1 = false)
  21. {
  22. this.lazyAsn1 = lazyAsn1;
  23. }
  24. private X509Crl ReadDerCrl(Asn1InputStream dIn)
  25. {
  26. Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
  27. if (seq.Count > 1 && seq[0] is DerObjectIdentifier)
  28. {
  29. if (seq[0].Equals(PkcsObjectIdentifiers.SignedData))
  30. {
  31. sCrlData = SignedData.GetInstance(
  32. Asn1Sequence.GetInstance((Asn1TaggedObject) seq[1], true)).Crls;
  33. return GetCrl();
  34. }
  35. }
  36. return new X509Crl(CertificateList.GetInstance(seq));
  37. }
  38. private X509Crl ReadPemCrl(Stream inStream)
  39. {
  40. Asn1Sequence seq = PemCrlParser.ReadPemObject(inStream);
  41. return seq == null ? null : new X509Crl(CertificateList.GetInstance(seq));
  42. }
  43. private X509Crl GetCrl()
  44. {
  45. if (sCrlData == null || sCrlDataObjectCount >= sCrlData.Count)
  46. return null;
  47. return new X509Crl(CertificateList.GetInstance(sCrlData[sCrlDataObjectCount++]));
  48. }
  49. /// <summary>
  50. /// Create loading data from byte array.
  51. /// </summary>
  52. /// <param name="input"></param>
  53. public X509Crl ReadCrl(byte[] input)
  54. {
  55. return ReadCrl(new MemoryStream(input, false));
  56. }
  57. /// <summary>
  58. /// Create loading data from byte array.
  59. /// </summary>
  60. /// <param name="input"></param>
  61. public IList<X509Crl> ReadCrls(byte[] input)
  62. {
  63. return ReadCrls(new MemoryStream(input, false));
  64. }
  65. /**
  66. * Generates a certificate revocation list (CRL) object and initializes
  67. * it with the data read from the input stream inStream.
  68. */
  69. public X509Crl ReadCrl(Stream inStream)
  70. {
  71. if (inStream == null)
  72. throw new ArgumentNullException("inStream");
  73. if (!inStream.CanRead)
  74. throw new ArgumentException("inStream must be read-able", "inStream");
  75. if (currentCrlStream == null)
  76. {
  77. currentCrlStream = inStream;
  78. sCrlData = null;
  79. sCrlDataObjectCount = 0;
  80. }
  81. else if (currentCrlStream != inStream) // reset if input stream has changed
  82. {
  83. currentCrlStream = inStream;
  84. sCrlData = null;
  85. sCrlDataObjectCount = 0;
  86. }
  87. try
  88. {
  89. if (sCrlData != null)
  90. {
  91. if (sCrlDataObjectCount != sCrlData.Count)
  92. return GetCrl();
  93. sCrlData = null;
  94. sCrlDataObjectCount = 0;
  95. return null;
  96. }
  97. int tag = inStream.ReadByte();
  98. if (tag < 0)
  99. return null;
  100. if (inStream.CanSeek)
  101. {
  102. inStream.Seek(-1L, SeekOrigin.Current);
  103. }
  104. else
  105. {
  106. PushbackStream pis = new PushbackStream(inStream);
  107. pis.Unread(tag);
  108. inStream = pis;
  109. }
  110. if (tag != 0x30) // assume ascii PEM encoded.
  111. return ReadPemCrl(inStream);
  112. Asn1InputStream asn1 = lazyAsn1
  113. ? new LazyAsn1InputStream(inStream)
  114. : new Asn1InputStream(inStream);
  115. return ReadDerCrl(asn1);
  116. }
  117. catch (CrlException e)
  118. {
  119. throw e;
  120. }
  121. catch (Exception e)
  122. {
  123. throw new CrlException(e.ToString());
  124. }
  125. }
  126. /**
  127. * Returns a (possibly empty) collection view of the CRLs read from
  128. * the given input stream inStream.
  129. *
  130. * The inStream may contain a sequence of DER-encoded CRLs, or
  131. * a PKCS#7 CRL set. This is a PKCS#7 SignedData object, with the
  132. * only significant field being crls. In particular the signature
  133. * and the contents are ignored.
  134. */
  135. public IList<X509Crl> ReadCrls(Stream inStream)
  136. {
  137. return new List<X509Crl>(ParseCrls(inStream));
  138. }
  139. public IEnumerable<X509Crl> ParseCrls(Stream inStream)
  140. {
  141. X509Crl crl;
  142. while ((crl = ReadCrl(inStream)) != null)
  143. {
  144. yield return crl;
  145. }
  146. }
  147. }
  148. }
  149. #pragma warning restore
  150. #endif