X509AttrCertParser.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  13. {
  14. public class X509AttrCertParser
  15. {
  16. private static readonly PemParser PemAttrCertParser = new PemParser("ATTRIBUTE CERTIFICATE");
  17. private Asn1Set sData;
  18. private int sDataObjectCount;
  19. private Stream currentStream;
  20. private IX509AttributeCertificate ReadDerCertificate(
  21. Asn1InputStream dIn)
  22. {
  23. Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
  24. if (seq.Count > 1 && seq[0] is DerObjectIdentifier)
  25. {
  26. if (seq[0].Equals(PkcsObjectIdentifiers.SignedData))
  27. {
  28. sData = SignedData.GetInstance(
  29. Asn1Sequence.GetInstance((Asn1TaggedObject) seq[1], true)).Certificates;
  30. return GetCertificate();
  31. }
  32. }
  33. // return new X509V2AttributeCertificate(seq.getEncoded());
  34. return new X509V2AttributeCertificate(AttributeCertificate.GetInstance(seq));
  35. }
  36. private IX509AttributeCertificate GetCertificate()
  37. {
  38. if (sData != null)
  39. {
  40. while (sDataObjectCount < sData.Count)
  41. {
  42. object obj = sData[sDataObjectCount++];
  43. if (obj is Asn1TaggedObject && ((Asn1TaggedObject)obj).TagNo == 2)
  44. {
  45. //return new X509V2AttributeCertificate(
  46. // Asn1Sequence.GetInstance((Asn1TaggedObject)obj, false).GetEncoded());
  47. return new X509V2AttributeCertificate(
  48. AttributeCertificate.GetInstance(
  49. Asn1Sequence.GetInstance((Asn1TaggedObject)obj, false)));
  50. }
  51. }
  52. }
  53. return null;
  54. }
  55. private IX509AttributeCertificate ReadPemCertificate(
  56. Stream inStream)
  57. {
  58. Asn1Sequence seq = PemAttrCertParser.ReadPemObject(inStream);
  59. return seq == null
  60. ? null
  61. //: new X509V2AttributeCertificate(seq.getEncoded());
  62. : new X509V2AttributeCertificate(AttributeCertificate.GetInstance(seq));
  63. }
  64. /// <summary>
  65. /// Create loading data from byte array.
  66. /// </summary>
  67. /// <param name="input"></param>
  68. public IX509AttributeCertificate ReadAttrCert(
  69. byte[] input)
  70. {
  71. return ReadAttrCert(new MemoryStream(input, false));
  72. }
  73. /// <summary>
  74. /// Create loading data from byte array.
  75. /// </summary>
  76. /// <param name="input"></param>
  77. public ICollection ReadAttrCerts(
  78. byte[] input)
  79. {
  80. return ReadAttrCerts(new MemoryStream(input, false));
  81. }
  82. /**
  83. * Generates a certificate object and initializes it with the data
  84. * read from the input stream inStream.
  85. */
  86. public IX509AttributeCertificate ReadAttrCert(
  87. Stream inStream)
  88. {
  89. if (inStream == null)
  90. throw new ArgumentNullException("inStream");
  91. if (!inStream.CanRead)
  92. throw new ArgumentException("inStream must be read-able", "inStream");
  93. if (currentStream == null)
  94. {
  95. currentStream = inStream;
  96. sData = null;
  97. sDataObjectCount = 0;
  98. }
  99. else if (currentStream != inStream) // reset if input stream has changed
  100. {
  101. currentStream = inStream;
  102. sData = null;
  103. sDataObjectCount = 0;
  104. }
  105. try
  106. {
  107. if (sData != null)
  108. {
  109. if (sDataObjectCount != sData.Count)
  110. {
  111. return GetCertificate();
  112. }
  113. sData = null;
  114. sDataObjectCount = 0;
  115. return null;
  116. }
  117. PushbackStream pis = new PushbackStream(inStream);
  118. int tag = pis.ReadByte();
  119. if (tag < 0)
  120. return null;
  121. pis.Unread(tag);
  122. if (tag != 0x30) // assume ascii PEM encoded.
  123. {
  124. return ReadPemCertificate(pis);
  125. }
  126. return ReadDerCertificate(new Asn1InputStream(pis));
  127. }
  128. catch (Exception e)
  129. {
  130. throw new CertificateException(e.ToString());
  131. }
  132. }
  133. /**
  134. * Returns a (possibly empty) collection view of the certificates
  135. * read from the given input stream inStream.
  136. */
  137. public ICollection ReadAttrCerts(
  138. Stream inStream)
  139. {
  140. IX509AttributeCertificate attrCert;
  141. IList attrCerts = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  142. while ((attrCert = ReadAttrCert(inStream)) != null)
  143. {
  144. attrCerts.Add(attrCert);
  145. }
  146. return attrCerts;
  147. }
  148. }
  149. }
  150. #pragma warning restore
  151. #endif