code: public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
          throws IOException
      {
          X509Certificate userCert;
          userCert = block.getCertificate(certificateSerialNumber, issuerName);
          if (userCert == null)
              return null;
  
          ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
          certList.add(userCert);
  
          X509Certificate[] pkcsCerts = block.getCertificates();
          if (pkcsCerts == null
              || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
              return certList;
          }
  
          Principal issuer = userCert.getIssuerDN();
          int start = 0;
          while (true) {
              boolean match = false;
              int i = start;
              while (i < pkcsCerts.length) {
                  if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                      // next cert in chain found
                      certList.add(pkcsCerts[i]);
                      // if selected cert is self-signed, we're done
                      // constructing the chain
                      if (pkcsCerts[i].getSubjectDN().equals(
                                              pkcsCerts[i].getIssuerDN())) {
                          start = pkcsCerts.length;
                      } else {
                          issuer = pkcsCerts[i].getIssuerDN();
                          X509Certificate tmpCert = pkcsCerts[start];
                          pkcsCerts[start] = pkcsCerts[i];
                          pkcsCerts[i] = tmpCert;
                          start++;
                      }
                      match = true;
                      break;
                  } else {
                      i++;
                  }
              }
              if (!match)
                  break;
          }
  
          return certList;
      }
docs: /*
  Returns the certificate chain pertaining to this SignerInfo.