Example usage for java.security.cert CertificateFactory getInstance

List of usage examples for java.security.cert CertificateFactory getInstance

Introduction

In this page you can find the example usage for java.security.cert CertificateFactory getInstance.

Prototype

public static final CertificateFactory getInstance(String type) throws CertificateException 

Source Link

Document

Returns a certificate factory object that implements the specified certificate type.

Usage

From source file:com.vmware.certificate.VMCAClient.java

/**
 * Creates a Certificate from a PEM encoded String
 *
 * @param certificateString//from   w  w w .j  av  a2 s .  c om
 * @return
 * @throws Exception
 */
public static X509Certificate getCertificateFromString(String certificateString) throws Exception {
    InputStream is = new ByteArrayInputStream(certificateString.getBytes());
    CertificateFactory cf = CertificateFactory.getInstance("X509");
    X509Certificate c = (X509Certificate) cf.generateCertificate(is);
    return c;
}

From source file:org.apache.kerby.pkix.PkiLoader.java

public List<Certificate> loadCerts(InputStream inputStream) throws IOException {
    CertificateFactory certFactory = null;
    try {/*  w  w w  . ja v  a 2s . c o  m*/
        certFactory = CertificateFactory.getInstance("X.509");
        Collection<? extends Certificate> certs = certFactory.generateCertificates(inputStream);
        return new ArrayList<>(certs);
    } catch (CertificateException e) {
        throw new IOException("Failed to load certificates", e);
    }
}

From source file:Main.java

/**
 * Creates <code>TrustAnchor</code> instance
 * constructed using self signed test certificate
 *
 * @return <code>TrustAnchor</code> instance
 *//*from w ww  . ja  v a2  s .  co  m*/
public static TrustAnchor getTrustAnchor() {
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance(certType);
    } catch (CertificateException e) {
        // requested cert type is not available in the
        // default provider package or any of the other provider packages
        // that were searched
        throw new RuntimeException(e);
    }
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(new ByteArrayInputStream(getEncodedX509Certificate()));
        X509Certificate c1 = (X509Certificate) cf.generateCertificate(bis);

        return new TrustAnchor(c1, null);
    } catch (Exception e) {
        // all failures are fatal
        throw new RuntimeException(e);
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ign) {
            }
        }
    }
}

From source file:org.globus.gsi.util.CertificateLoadUtil.java

/**
 * Returns appropriate <code>CertificateFactory</code>. If <I>provider</I>
 * was set a provider-specific <code>CertificateFactory</code> will be used.
 * Otherwise, a default <code>CertificateFactory</code> will be used.
 *
 * @return <code>CertificateFactory</code>
 *///from   www.  j  a  va2 s  .  c  o  m
protected static CertificateFactory getCertificateFactory() throws GeneralSecurityException {
    if (provider == null) {
        return CertificateFactory.getInstance("X.509");
    } else {
        return CertificateFactory.getInstance("X.509", provider);
    }
}

From source file:org.apache.syncope.console.wicket.markup.html.form.preview.BinaryCertPreviewer.java

@Override
public Component preview() {
    final Label commonNameLabel = new Label("certCommonName", new Model<String>());
    final ByteArrayInputStream certificateStream = new ByteArrayInputStream(uploadedBytes);
    try {//  w  w w.ja  v a 2  s  .c  o  m
        final X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X.509")
                .generateCertificate(certificateStream);

        final StringBuilder commonNameBuilder = new StringBuilder("cn=");

        final LdapName ldapName = new LdapName(certificate.getIssuerDN().getName());

        for (Rdn rdn : ldapName.getRdns()) {
            if ("CN".equalsIgnoreCase(rdn.getType())) {
                commonNameBuilder
                        .append(rdn.getValue() == null ? StringUtils.EMPTY : rdn.getValue().toString());
            }
        }
        commonNameLabel.setDefaultModelObject(commonNameBuilder.toString());
    } catch (Exception e) {
        LOG.error("Error evaluating certificate file", e);
        throw new IllegalArgumentException("Error evaluating certificate file", e);
    } finally {
        IOUtils.closeQuietly(certificateStream);
    }
    return this.add(commonNameLabel);
}

From source file:com.aqnote.shared.cryptology.cert.util.X509CertFileUtil.java

/**
 * ?X.509?/* w  w  w. ja  v  a2s .  com*/
 * 
 * @param crtPath ?
 * @return
 * @throws CertificateException
 * @throws IOException
 */
public static X509Certificate readX509Certificate(String crtPath) throws CertificateException, IOException {

    InputStream inStream = null;

    inStream = new FileInputStream(crtPath);

    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);

    inStream.close();

    return cert;
}

From source file:io.fabric8.utils.cxf.WebClients.java

public static KeyStore createTrustStore(String caCertData, File caCertFile) throws Exception {
    try (InputStream pemInputStream = getInputStreamFromDataOrFile(caCertData, caCertFile)) {
        CertificateFactory certFactory = CertificateFactory.getInstance("X509");
        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(pemInputStream);

        KeyStore trustStore = KeyStore.getInstance("JKS");
        trustStore.load(null);/*from www  . ja v  a  2 s. c  om*/

        String alias = cert.getSubjectX500Principal().getName();
        trustStore.setCertificateEntry(alias, cert);

        return trustStore;
    }
}

From source file:org.eclipse.che.ide.ext.datasource.server.ssl.TrustStoreObject.java

public void addNewServerCACert(String alias, Iterator<FileItem> uploadedFilesIterator) throws Exception {
    Certificate[] certs = null;/*from www .  j  av  a 2  s  .  c o m*/
    while (uploadedFilesIterator.hasNext()) {
        FileItem fileItem = uploadedFilesIterator.next();
        if (!fileItem.isFormField()) {
            if ("certFile".equals(fileItem.getFieldName())) {
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                certs = cf.generateCertificates(fileItem.getInputStream()).toArray(new Certificate[] {});
            }
        }
    }

    if (certs == null) {
        throw new WebApplicationException(
                Response.ok("<pre>Can't find input file.</pre>", MediaType.TEXT_HTML).build());
    }

    keystore.setCertificateEntry(alias, certs[0]);
    save();
}

From source file:de.rub.nds.burp.utilities.attacks.signatureFaking.helper.CertificateHandler.java

public CertificateHandler(final String cert) throws CertificateHandlerException {
    try {/*from w  w  w  . j a va 2 s .c o m*/
        certFactory = CertificateFactory.getInstance("X.509");
        certificate = (X509Certificate) certFactory
                .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(cert)));
        originalPublicKey = certificate.getPublicKey();
    } catch (CertificateException e) {
        throw new CertificateHandlerException(e);
    }
}

From source file:nl.surfnet.spring.security.opensaml.CertificateStoreImpl.java

private void appendToKeyStore(String keyAlias, String pemCert) throws Exception {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + pemCert + "\n-----END CERTIFICATE-----";
    ByteArrayInputStream certificateInputStream = new ByteArrayInputStream(wrappedCert.getBytes());
    final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    final Certificate cert = certificateFactory.generateCertificate(certificateInputStream);
    IOUtils.closeQuietly(certificateInputStream);
    keyStore.setCertificateEntry(keyAlias, cert);
}