X509CertificateParser.cs 4.4 KB

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