X509CertificateParser.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  14. * class for dealing with X509 certificates.
  15. * <p>
  16. * At the moment this will deal with "-----BEGIN CERTIFICATE-----" to "-----END CERTIFICATE-----"
  17. * base 64 encoded certs, as well as the BER binaries of certificates and some classes of PKCS#7
  18. * objects.</p>
  19. */
  20. public class X509CertificateParser
  21. {
  22. private static readonly PemParser PemCertParser = new PemParser("CERTIFICATE");
  23. private Asn1Set sData;
  24. private int sDataObjectCount;
  25. private Stream currentStream;
  26. private X509Certificate ReadDerCertificate(Asn1InputStream dIn)
  27. {
  28. Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
  29. if (seq.Count > 1 && seq[0] is DerObjectIdentifier)
  30. {
  31. if (seq[0].Equals(PkcsObjectIdentifiers.SignedData))
  32. {
  33. sData = SignedData.GetInstance(
  34. Asn1Sequence.GetInstance((Asn1TaggedObject) seq[1], true)).Certificates;
  35. return GetCertificate();
  36. }
  37. }
  38. return new X509Certificate(X509CertificateStructure.GetInstance(seq));
  39. }
  40. private X509Certificate ReadPemCertificate(Stream inStream)
  41. {
  42. Asn1Sequence seq = PemCertParser.ReadPemObject(inStream);
  43. return seq == null ? null : new X509Certificate(X509CertificateStructure.GetInstance(seq));
  44. }
  45. private X509Certificate GetCertificate()
  46. {
  47. if (sData != null)
  48. {
  49. while (sDataObjectCount < sData.Count)
  50. {
  51. object obj = sData[sDataObjectCount++];
  52. if (obj is Asn1Sequence)
  53. return new X509Certificate(X509CertificateStructure.GetInstance(obj));
  54. }
  55. }
  56. return null;
  57. }
  58. /// <summary>
  59. /// Create loading data from byte array.
  60. /// </summary>
  61. /// <param name="input"></param>
  62. public X509Certificate ReadCertificate(byte[] input)
  63. {
  64. return ReadCertificate(new MemoryStream(input, false));
  65. }
  66. /// <summary>
  67. /// Create loading data from byte array.
  68. /// </summary>
  69. /// <param name="input"></param>
  70. public IList<X509Certificate> ReadCertificates(byte[] input)
  71. {
  72. return ReadCertificates(new MemoryStream(input, false));
  73. }
  74. /**
  75. * Generates a certificate object and initializes it with the data
  76. * read from the input stream inStream.
  77. */
  78. public X509Certificate ReadCertificate(Stream inStream)
  79. {
  80. if (inStream == null)
  81. throw new ArgumentNullException("inStream");
  82. if (!inStream.CanRead)
  83. throw new ArgumentException("inStream must be read-able", "inStream");
  84. if (currentStream == null)
  85. {
  86. currentStream = inStream;
  87. sData = null;
  88. sDataObjectCount = 0;
  89. }
  90. else if (currentStream != inStream) // reset if input stream has changed
  91. {
  92. currentStream = inStream;
  93. sData = null;
  94. sDataObjectCount = 0;
  95. }
  96. try
  97. {
  98. if (sData != null)
  99. {
  100. if (sDataObjectCount != sData.Count)
  101. return GetCertificate();
  102. sData = null;
  103. sDataObjectCount = 0;
  104. return null;
  105. }
  106. int tag = inStream.ReadByte();
  107. if (tag < 0)
  108. return null;
  109. if (inStream.CanSeek)
  110. {
  111. inStream.Seek(-1L, SeekOrigin.Current);
  112. }
  113. else
  114. {
  115. PushbackStream pis = new PushbackStream(inStream);
  116. pis.Unread(tag);
  117. inStream = pis;
  118. }
  119. if (tag != 0x30) // assume ascii PEM encoded.
  120. return ReadPemCertificate(inStream);
  121. return ReadDerCertificate(new Asn1InputStream(inStream));
  122. }
  123. catch (Exception e)
  124. {
  125. throw new CertificateException("Failed to read certificate", e);
  126. }
  127. }
  128. /**
  129. * Returns a (possibly empty) collection view of the certificates
  130. * read from the given input stream inStream.
  131. */
  132. public IList<X509Certificate> ReadCertificates(Stream inStream)
  133. {
  134. return new List<X509Certificate>(ParseCertificates(inStream));
  135. }
  136. public IEnumerable<X509Certificate> ParseCertificates(Stream inStream)
  137. {
  138. X509Certificate cert;
  139. while ((cert = ReadCertificate(inStream)) != null)
  140. {
  141. yield return cert;
  142. }
  143. }
  144. }
  145. }
  146. #pragma warning restore
  147. #endif