Certificate.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  10. {
  11. /**
  12. * Parsing and encoding of a <i>Certificate</i> struct from RFC 4346.
  13. * <p/>
  14. * <pre>
  15. * opaque ASN.1Cert&lt;2^24-1&gt;;
  16. *
  17. * struct {
  18. * ASN.1Cert certificate_list&lt;0..2^24-1&gt;;
  19. * } Certificate;
  20. * </pre>
  21. *
  22. * @see BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509.X509CertificateStructure
  23. */
  24. public class Certificate
  25. {
  26. public static readonly Certificate EmptyChain = new Certificate(new X509CertificateStructure[0]);
  27. /**
  28. * The certificates.
  29. */
  30. protected readonly X509CertificateStructure[] mCertificateList;
  31. public Certificate(X509CertificateStructure[] certificateList)
  32. {
  33. if (certificateList == null)
  34. throw new ArgumentNullException("certificateList");
  35. this.mCertificateList = certificateList;
  36. }
  37. /**
  38. * @return an array of {@link org.bouncycastle.asn1.x509.Certificate} representing a certificate
  39. * chain.
  40. */
  41. public virtual X509CertificateStructure[] GetCertificateList()
  42. {
  43. return CloneCertificateList();
  44. }
  45. public virtual X509CertificateStructure GetCertificateAt(int index)
  46. {
  47. return mCertificateList[index];
  48. }
  49. public virtual int Length
  50. {
  51. get { return mCertificateList.Length; }
  52. }
  53. /**
  54. * @return <code>true</code> if this certificate chain contains no certificates, or
  55. * <code>false</code> otherwise.
  56. */
  57. public virtual bool IsEmpty
  58. {
  59. get { return mCertificateList.Length == 0; }
  60. }
  61. /**
  62. * Encode this {@link Certificate} to a {@link Stream}.
  63. *
  64. * @param output the {@link Stream} to encode to.
  65. * @throws IOException
  66. */
  67. public virtual void Encode(Stream output)
  68. {
  69. IList derEncodings = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(mCertificateList.Length);
  70. int totalLength = 0;
  71. foreach (Asn1Encodable asn1Cert in mCertificateList)
  72. {
  73. byte[] derEncoding = asn1Cert.GetEncoded(Asn1Encodable.Der);
  74. derEncodings.Add(derEncoding);
  75. totalLength += derEncoding.Length + 3;
  76. }
  77. TlsUtilities.CheckUint24(totalLength);
  78. TlsUtilities.WriteUint24(totalLength, output);
  79. foreach (byte[] derEncoding in derEncodings)
  80. {
  81. TlsUtilities.WriteOpaque24(derEncoding, output);
  82. }
  83. }
  84. /**
  85. * Parse a {@link Certificate} from a {@link Stream}.
  86. *
  87. * @param input the {@link Stream} to parse from.
  88. * @return a {@link Certificate} object.
  89. * @throws IOException
  90. */
  91. public static Certificate Parse(Stream input)
  92. {
  93. int totalLength = TlsUtilities.ReadUint24(input);
  94. if (totalLength == 0)
  95. {
  96. return EmptyChain;
  97. }
  98. byte[] certListData = TlsUtilities.ReadFully(totalLength, input);
  99. MemoryStream buf = new MemoryStream(certListData, false);
  100. IList certificate_list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  101. while (buf.Position < buf.Length)
  102. {
  103. byte[] berEncoding = TlsUtilities.ReadOpaque24(buf);
  104. Asn1Object asn1Cert = TlsUtilities.ReadAsn1Object(berEncoding);
  105. certificate_list.Add(X509CertificateStructure.GetInstance(asn1Cert));
  106. }
  107. X509CertificateStructure[] certificateList = new X509CertificateStructure[certificate_list.Count];
  108. for (int i = 0; i < certificate_list.Count; ++i)
  109. {
  110. certificateList[i] = (X509CertificateStructure)certificate_list[i];
  111. }
  112. return new Certificate(certificateList);
  113. }
  114. protected virtual X509CertificateStructure[] CloneCertificateList()
  115. {
  116. return (X509CertificateStructure[])mCertificateList.Clone();
  117. }
  118. }
  119. }
  120. #pragma warning restore
  121. #endif