X509CertPairParser.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  12. {
  13. public class X509CertPairParser
  14. {
  15. private Stream currentStream;
  16. private X509CertificatePair ReadDerCrossCertificatePair(
  17. Stream inStream)
  18. {
  19. Asn1InputStream dIn = new Asn1InputStream(inStream);//, ProviderUtil.getReadLimit(in));
  20. Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
  21. CertificatePair pair = CertificatePair.GetInstance(seq);
  22. return new X509CertificatePair(pair);
  23. }
  24. /// <summary>
  25. /// Create loading data from byte array.
  26. /// </summary>
  27. /// <param name="input"></param>
  28. public X509CertificatePair ReadCertPair(
  29. byte[] input)
  30. {
  31. return ReadCertPair(new MemoryStream(input, false));
  32. }
  33. /// <summary>
  34. /// Create loading data from byte array.
  35. /// </summary>
  36. /// <param name="input"></param>
  37. public ICollection ReadCertPairs(
  38. byte[] input)
  39. {
  40. return ReadCertPairs(new MemoryStream(input, false));
  41. }
  42. public X509CertificatePair ReadCertPair(
  43. Stream inStream)
  44. {
  45. if (inStream == null)
  46. throw new ArgumentNullException("inStream");
  47. if (!inStream.CanRead)
  48. throw new ArgumentException("inStream must be read-able", "inStream");
  49. if (currentStream == null)
  50. {
  51. currentStream = inStream;
  52. }
  53. else if (currentStream != inStream) // reset if input stream has changed
  54. {
  55. currentStream = inStream;
  56. }
  57. try
  58. {
  59. PushbackStream pis = new PushbackStream(inStream);
  60. int tag = pis.ReadByte();
  61. if (tag < 0)
  62. return null;
  63. pis.Unread(tag);
  64. return ReadDerCrossCertificatePair(pis);
  65. }
  66. catch (Exception e)
  67. {
  68. throw new CertificateException(e.ToString());
  69. }
  70. }
  71. public ICollection ReadCertPairs(
  72. Stream inStream)
  73. {
  74. X509CertificatePair certPair;
  75. IList certPairs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  76. while ((certPair = ReadCertPair(inStream)) != null)
  77. {
  78. certPairs.Add(certPair);
  79. }
  80. return certPairs;
  81. }
  82. }
  83. }
  84. #pragma warning restore
  85. #endif