Example usage for javax.security.cert X509Certificate getEncoded

List of usage examples for javax.security.cert X509Certificate getEncoded

Introduction

In this page you can find the example usage for javax.security.cert X509Certificate getEncoded.

Prototype

public abstract byte[] getEncoded() throws CertificateEncodingException;

Source Link

Document

Returns the encoded form of this certificate.

Usage

From source file:org.panlab.tgw.restclient.PtmInfoParser.java

private static void processCertificate(String alias, X509Certificate x509, URL url) {
    try {/*from w w  w .  j  av  a  2  s  .c  o  m*/
        String store = System.getProperty("javax.net.ssl.trustStore");
        String password = System.getProperty("javax.net.ssl.trustStorePassword");

        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(new FileInputStream(store), password.toCharArray());

        Enumeration<String> en = keystore.aliases();
        while (en.hasMoreElements()) {
            log.info(en.nextElement());
        }

        if (!keystore.containsAlias(alias)) {
            ByteArrayInputStream bais = new ByteArrayInputStream(x509.getEncoded());
            Certificate cert = CertificateFactory.getInstance("x509").generateCertificate(bais);
            keystore.setCertificateEntry(alias, cert);

            storeNewPTM(alias, url, x509.getSubjectDN().toString().replace(", ", ","));

            en = keystore.aliases();
            while (en.hasMoreElements()) {
                log.info(en.nextElement());
            }
            keystore.store(new FileOutputStream(store), password.toCharArray());

            TrustManagerFactory.getInstance("PKIX").init(keystore);
        }

    } catch (Exception error) {
        log.error(error.getMessage());
    }
}