/** * @param inputStream * @return * @throws CertificateException */ public static X509Certificate[] fromPEM(InputStream inputStream) throws CertificateException { /* This strips off the header and tail automatically, so we don't really have to do anything. */ return getCertFactory().generateCertificates(inputStream).toArray(new X509Certificate[0]); }
/** * Encode a certificate in PKCS 7 format. * @param certs * @param out * @throws CertificateException * @throws IOException */ public static void toPKCS7(X509Certificate[] certs, OutputStream out) throws CertificateException, IOException { ArrayList<X509Certificate> certArrayList = new ArrayList<X509Certificate>(Arrays.asList(certs)); CertPath cp = getCertFactory().generateCertPath(certArrayList); // Encode certificate in PKCS7 format Base64 encoder = new Base64(64); //chunk at 64 char String pkcsCert = new String(encoder.encode(cp.getEncoded("PKCS7"))).trim(); pkcsCert = pkcsCert.replaceAll("\\r", "");// removes extra linefeed. PrintStream printStream = new PrintStream(out); printStream.println(BEGIN_PKCS7); printStream.println(pkcsCert); printStream.println(END_PKCS7); printStream.flush(); printStream.close(); }
certData.getBytes()); try { X509Certificate cert = (X509Certificate) getCertFactory() .generateCertificate(inputStream); c.add(cert);