private Certificate[] establishCertChain(Certificate userCert,Certificate certToVerify) throws Exception {
  if (userCert != null) {
    PublicKey origPubKey=userCert.getPublicKey();
    PublicKey replyPubKey=certToVerify.getPublicKey();
    if (!origPubKey.equals(replyPubKey)) {
      throw new Exception(rb.getString("Public.keys.in.reply.and.keystore.don.t.match"));
    }
    if (certToVerify.equals(userCert)) {
      throw new Exception(rb.getString("Certificate.reply.and.certificate.in.keystore.are.identical"));
    }
  }
  Hashtable<Principal,Vector<Certificate>> certs=null;
  if (keyStore.size() > 0) {
    certs=new Hashtable<Principal,Vector<Certificate>>(11);
    keystorecerts2Hashtable(keyStore,certs);
  }
  if (trustcacerts) {
    if (caks != null && caks.size() > 0) {
      if (certs == null) {
        certs=new Hashtable<Principal,Vector<Certificate>>(11);
      }
      keystorecerts2Hashtable(caks,certs);
    }
  }
  Vector<Certificate> chain=new Vector<>(2);
  if (buildChain((X509Certificate)certToVerify,chain,certs)) {
    Certificate[] newChain=new Certificate[chain.size()];
    int j=0;
    for (int i=chain.size() - 1; i >= 0; i--) {
      newChain[j]=chain.elementAt(i);
      j++;
    }
    return newChain;
  }
 else {
    throw new Exception(rb.getString("Failed.to.establish.chain.from.reply"));
  }
}

private Certificate [ ] establishCertChain ( Certificate userCert , Certificate certToVerify ) throws Exception { if ( userCert != null ) { PublicKey origPubKey = userCert . getPublicKey ( ) ; PublicKey replyPubKey = certToVerify . getPublicKey ( ) ; if ( ! origPubKey . equals ( replyPubKey ) ) { throw new Exception ( rb . getString ( "Public.keys.in.reply.and.keystore.don.t.match" ) ) ; } if ( certToVerify . equals ( userCert ) ) { throw new Exception ( rb . getString ( "Certificate.reply.and.certificate.in.keystore.are.identical" ) ) ; } } Hashtable < Principal , Vector < Certificate > > certs = null ; if ( keyStore . size ( ) > 0 ) { certs = new Hashtable < Principal , Vector < Certificate > > ( 11 ) ; keystorecerts2Hashtable ( keyStore , certs ) ; } if ( trustcacerts ) { if ( caks != null && caks . size ( ) > 0 ) { if ( certs == null ) { certs = new Hashtable < Principal , Vector < Certificate > > ( 11 ) ; } keystorecerts2Hashtable ( caks , certs ) ; } } Vector < Certificate > chain = new Vector < > ( 2 ) ; if ( buildChain ( ( X509Certificate ) certToVerify , chain , certs ) ) { Certificate [ ] newChain = new Certificate [ chain . size ( ) ] ; int j = 0 ; for ( int i = chain . size ( ) - 1 ; i >= 0 ; i -- ) { newChain [ j ] = chain . elementAt ( i ) ; j ++ ; } return newChain ; } else { throw new Exception ( rb . getString ( "Failed.to.establish.chain.from.reply" ) ) ; } }
establishes a certificate chain ( using trusted certificates in the keystore ) , starting with the user certificate and ending at a self - signed certificate found in the keystore .
