try {
boolean trusted = false;
X509Certificate chainCert;
while (in.available() > 0 && !trusted) {
// The length of the next certificate (we dont need this as rthe
// DERInputStream does the work
int certlen = (in.read() & 0xFF) << 16 | (in.read() & 0xFF) << 8 | (in.read() & 0xFF);
// Now read the certificate
DERInputStream der = new DERInputStream(in);
ASN1Sequence certificate = (ASN1Sequence) der.readObject();
// Get the x509 certificate structure
chainCert = new X509Certificate(X509CertificateStructure.getInstance(certificate));
if (x509 == null)
x509 = chainCert;
// Verify if this part of the chain is trusted