123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Collections;
- using System.IO;
- using Org.BouncyCastle.Asn1;
- using Org.BouncyCastle.Asn1.X509;
- using Org.BouncyCastle.Utilities;
- namespace Org.BouncyCastle.Crypto.Tls
- {
-
- public class Certificate
- {
- public static readonly Certificate EmptyChain = new Certificate(new X509CertificateStructure[0]);
-
- protected readonly X509CertificateStructure[] mCertificateList;
- public Certificate(X509CertificateStructure[] certificateList)
- {
- if (certificateList == null)
- throw new ArgumentNullException("certificateList");
- this.mCertificateList = certificateList;
- }
-
- public virtual X509CertificateStructure[] GetCertificateList()
- {
- return CloneCertificateList();
- }
- public virtual X509CertificateStructure GetCertificateAt(int index)
- {
- return mCertificateList[index];
- }
- public virtual int Length
- {
- get { return mCertificateList.Length; }
- }
-
- public virtual bool IsEmpty
- {
- get { return mCertificateList.Length == 0; }
- }
-
- public virtual void Encode(Stream output)
- {
- IList derEncodings = Org.BouncyCastle.Utilities.Platform.CreateArrayList(mCertificateList.Length);
- int totalLength = 0;
- foreach (Asn1Encodable asn1Cert in mCertificateList)
- {
- byte[] derEncoding = asn1Cert.GetEncoded(Asn1Encodable.Der);
- derEncodings.Add(derEncoding);
- totalLength += derEncoding.Length + 3;
- }
- TlsUtilities.CheckUint24(totalLength);
- TlsUtilities.WriteUint24(totalLength, output);
- foreach (byte[] derEncoding in derEncodings)
- {
- TlsUtilities.WriteOpaque24(derEncoding, output);
- }
- }
-
- public static Certificate Parse(Stream input)
- {
- int totalLength = TlsUtilities.ReadUint24(input);
- if (totalLength == 0)
- {
- return EmptyChain;
- }
- byte[] certListData = TlsUtilities.ReadFully(totalLength, input);
- MemoryStream buf = new MemoryStream(certListData, false);
- IList certificate_list = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- while (buf.Position < buf.Length)
- {
- byte[] derEncoding = TlsUtilities.ReadOpaque24(buf);
- Asn1Object asn1Cert = TlsUtilities.ReadDerObject(derEncoding);
- certificate_list.Add(X509CertificateStructure.GetInstance(asn1Cert));
- }
- X509CertificateStructure[] certificateList = new X509CertificateStructure[certificate_list.Count];
- for (int i = 0; i < certificate_list.Count; ++i)
- {
- certificateList[i] = (X509CertificateStructure)certificate_list[i];
- }
- return new Certificate(certificateList);
- }
- protected virtual X509CertificateStructure[] CloneCertificateList()
- {
- return (X509CertificateStructure[])mCertificateList.Clone();
- }
- }
- }
- #endif
|