private boolean buildChain(X509Certificate certToVerify,Vector<Certificate> chain,Hashtable<Principal,Vector<Certificate>> certs){
  Principal issuer=certToVerify.getIssuerDN();
  if (isSelfSigned(certToVerify)) {
    chain.addElement(certToVerify);
    return true;
  }
  Vector<Certificate> vec=certs.get(issuer);
  if (vec == null) {
    return false;
  }
  for (Enumeration<Certificate> issuerCerts=vec.elements(); issuerCerts.hasMoreElements(); ) {
    X509Certificate issuerCert=(X509Certificate)issuerCerts.nextElement();
    PublicKey issuerPubKey=issuerCert.getPublicKey();
    try {
      certToVerify.verify(issuerPubKey);
    }
 catch (    Exception e) {
      continue;
    }
    if (buildChain(issuerCert,chain,certs)) {
      chain.addElement(certToVerify);
      return true;
    }
  }
  return false;
}

private boolean buildChain ( X509Certificate certToVerify , Vector < Certificate > chain , Hashtable < Principal , Vector < Certificate > > certs ) { Principal issuer = certToVerify . getIssuerDN ( ) ; if ( isSelfSigned ( certToVerify ) ) { chain . addElement ( certToVerify ) ; return true ; } Vector < Certificate > vec = certs . get ( issuer ) ; if ( vec == null ) { return false ; } for ( Enumeration < Certificate > issuerCerts = vec . elements ( ) ; issuerCerts . hasMoreElements ( ) ; ) { X509Certificate issuerCert = ( X509Certificate ) issuerCerts . nextElement ( ) ; PublicKey issuerPubKey = issuerCert . getPublicKey ( ) ; try { certToVerify . verify ( issuerPubKey ) ; } catch ( Exception e ) { continue ; } if ( buildChain ( issuerCert , chain , certs ) ) { chain . addElement ( certToVerify ) ; return true ; } } return false ; }
recursively tries to establish chain from pool of trusted certs .
