code: public static Object[] verifyCertificates(Certificate certs[], KeyStore keystore, Collection crls, Calendar calendar) {
          if (calendar == null)
              calendar = new GregorianCalendar();
          for (int k = 0; k < certs.length; ++k) {
              X509Certificate cert = (X509Certificate)certs[k];
              String err = verifyCertificate(cert, crls, calendar);
              if (err != null)
                  return new Object[]{cert, err};
              try {
                  for (Enumeration aliases = keystore.aliases(); aliases.hasMoreElements();) {
                      try {
                          String alias = (String)aliases.nextElement();
                          if (!keystore.isCertificateEntry(alias))
                              continue;
                          X509Certificate certStoreX509 = (X509Certificate)keystore.getCertificate(alias);
                          if (verifyCertificate(certStoreX509, crls, calendar) != null)
                              continue;
                          try {
                              cert.verify(certStoreX509.getPublicKey());
                              return null;
                          }
                          catch (Exception e) {
                              continue;
                          }
                      }
                      catch (Exception ex) {
                      }
                  }
              }
              catch (Exception e) {
              }
              int j;
              for (j = 0; j < certs.length; ++j) {
                  if (j == k)
                      continue;
                  X509Certificate certNext = (X509Certificate)certs[j];
                  try {
                      cert.verify(certNext.getPublicKey());
                      break;
                  }
                  catch (Exception e) {
                  }
              }
              if (j == certs.length)
                  return new Object[]{cert, "Cannot be verified against the KeyStore or the certificate chain"};
          }
          return new Object[]{null, "Invalid state. Possible circular certificate chain"};
      }
docs: Verifies a certificate chain against a KeyStore.
  @param certs the certificate chain
  @param keystore the <CODE>KeyStore</CODE>
  @param crls the certificate revocation list or <CODE>null</CODE>
  @param calendar the date or <CODE>null</CODE> for the current date
  @return <CODE>null</CODE> if the certificate chain could be validated or a
  <CODE>Object[]{cert,error}</CODE> where <CODE>cert</CODE> is the
  failed certificate and <CODE>error</CODE> is the error message