Org.BouncyCastle.Pkix.PkixCertPath.SortCerts C# (CSharp) Method

SortCerts() private static method

private static SortCerts ( IList certs ) : IList
certs IList
return IList
		private static IList SortCerts(
			IList certs)
		{
			if (certs.Count < 2)
				return certs;

			X509Name issuer = ((X509Certificate)certs[0]).IssuerDN;
			bool okay = true;

			for (int i = 1; i != certs.Count; i++) 
			{
				X509Certificate cert = (X509Certificate)certs[i];

				if (issuer.Equivalent(cert.SubjectDN, true))
				{
					issuer = ((X509Certificate)certs[i]).IssuerDN;
				}
				else
				{
					okay = false;
					break;
				}
			}

			if (okay)
				return certs;

			// find end-entity cert
			IList retList = new ArrayList(certs.Count);
			IList orig = new ArrayList(certs);

			for (int i = 0; i < certs.Count; i++)
			{
				X509Certificate cert = (X509Certificate)certs[i];
				bool           found = false;

				X509Name subject = cert.SubjectDN;
				foreach (X509Certificate c in certs)
				{
					if (c.IssuerDN.Equivalent(subject, true))
					{
						found = true;
						break;
					}
				}

				if (!found)
				{
					retList.Add(cert);
					certs.RemoveAt(i);
				}
			}

			// can only have one end entity cert - something's wrong, give up.
			if (retList.Count > 1)
				return orig;

			for (int i = 0; i != retList.Count; i++)
			{
				issuer = ((X509Certificate)retList[i]).IssuerDN;

				for (int j = 0; j < certs.Count; j++)
				{
					X509Certificate c = (X509Certificate)certs[j];
					if (issuer.Equivalent(c.SubjectDN, true))
					{
						retList.Add(c);
						certs.RemoveAt(j);
						break;
					}
				}
			}

			// make sure all certificates are accounted for.
			if (certs.Count > 0)
				return orig;

			return retList;
		}

Usage Example

Beispiel #1
0
        public PkixCertPath(Stream inStream, string encoding)
        {
            string text = encoding.ToUpper();
            IList  list;

            try
            {
                if (text.Equals("PkiPath".ToUpper()))
                {
                    Asn1InputStream asn1InputStream = new Asn1InputStream(inStream);
                    Asn1Object      asn1Object      = asn1InputStream.ReadObject();
                    if (!(asn1Object is Asn1Sequence))
                    {
                        throw new CertificateException("input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath");
                    }
                    list = Platform.CreateArrayList();
                    using (IEnumerator enumerator = ((Asn1Sequence)asn1Object).GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            Asn1Encodable asn1Encodable = (Asn1Encodable)enumerator.Current;
                            byte[]        encoded       = asn1Encodable.GetEncoded("DER");
                            Stream        inStream2     = new MemoryStream(encoded, false);
                            list.Insert(0, new X509CertificateParser().ReadCertificate(inStream2));
                        }
                        goto IL_EF;
                    }
                }
                if (!text.Equals("PKCS7") && !text.Equals("PEM"))
                {
                    throw new CertificateException("unsupported encoding: " + encoding);
                }
                list = Platform.CreateArrayList(new X509CertificateParser().ReadCertificates(inStream));
                IL_EF :;
            }
            catch (IOException ex)
            {
                throw new CertificateException("IOException throw while decoding CertPath:\n" + ex.ToString());
            }
            this.certificates = PkixCertPath.SortCerts(list);
        }
All Usage Examples Of Org.BouncyCastle.Pkix.PkixCertPath::SortCerts