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:be.fedict.eid.idp.model.CryptoUtil.java

public static X509Certificate getCertificate(byte[] certificateBytes) throws CertificateException {

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificateBytes));
}

From source file:org.commonjava.maven.galley.transport.htcli.internal.SSLUtils.java

public static KeyStore readKeyAndCert(final String pemContent, final String keyPass)
        throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException,
        InvalidKeySpecException {
    final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null);/*www  . ja  v a 2 s .c  om*/

    final CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    final KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    final List<String> lines = readLines(pemContent);

    String currentHeader = null;
    final StringBuilder current = new StringBuilder();
    final Map<String, String> entries = new LinkedHashMap<String, String>();
    for (final String line : lines) {
        if (line == null) {
            continue;
        }

        if (line.startsWith("-----BEGIN")) {
            currentHeader = line.trim();
            current.setLength(0);
        } else if (line.startsWith("-----END")) {
            entries.put(currentHeader, current.toString());
        } else {
            current.append(line.trim());
        }
    }

    final List<Certificate> certs = new ArrayList<Certificate>();
    for (int pass = 0; pass < 2; pass++) {
        for (final Map.Entry<String, String> entry : entries.entrySet()) {
            final String header = entry.getKey();
            final byte[] data = decodeBase64(entry.getValue());

            if (pass > 0 && header.contains("BEGIN PRIVATE KEY")) {
                final KeySpec spec = new PKCS8EncodedKeySpec(data);
                final PrivateKey key = keyFactory.generatePrivate(spec);
                ks.setKeyEntry("key", key, keyPass.toCharArray(), certs.toArray(new Certificate[] {}));
            } else if (pass < 1 && header.contains("BEGIN CERTIFICATE")) {
                final Certificate c = certFactory.generateCertificate(new ByteArrayInputStream(data));

                ks.setCertificateEntry("certificate", c);
                certs.add(c);
            }
        }
    }

    return ks;
}

From source file:com.vmware.identity.rest.core.util.CertificateHelper.java

/**
 * Converts a PEM formatted string to a {@code X509Certificate} instance.
 *
 * @param pem PEM formatted string// w w w .j av a 2 s .  com
 * @return {@link X509Certificate}
 * @throws CertificateException If conversion fails
 */
public static X509Certificate convertToX509(String pem) throws CertificateException {
    if (pem == null) {
        return null;
    }

    CertificateFactory certFactory = CertificateFactory.getInstance(X509_CERT_TYPE);
    InputStream inputStream = new ByteArrayInputStream(pem.getBytes());
    return (X509Certificate) certFactory.generateCertificate(inputStream);
}

From source file:fr.mby.saml2.sp.impl.helper.SecurityHelper.java

/**
 * Build a certificate from PEM resource.
 * //from   w  ww .ja v  a 2  s .com
 * @param certificate
 *            the PEM resource
 * @param type
 *            the certificate type
 * @return the java.security.cert.Certificate
 * @throws CertificateException
 * @throws IOException
 */
public static Certificate buildCertificate(final Resource certificate, final String type)
        throws CertificateException, IOException {
    Certificate result = null;

    final CertificateFactory certFactory = CertificateFactory.getInstance(type);
    result = certFactory.generateCertificate(certificate.getInputStream());

    return result;
}

From source file:com.aqnote.shared.cryptology.cert.tool.X509CertTool.java

public static X509Certificate coverString2Cert(String base64CrtFile) throws CertificateException, IOException {

    byte[] certENcoded = getCertEncoded(base64CrtFile);
    InputStream istream = StreamUtil.bytes2Stream(certENcoded);

    CertificateFactory cf = CertificateFactory.getInstance(CERT_TYPE_X509);
    X509Certificate cert = (X509Certificate) cf.generateCertificate(istream);
    istream.close();//from ww w  .j  av a 2  s . c  om
    return cert;
}

From source file:com.linkage.crm.csb.sign.CtSignature.java

/**
 * .//from  w  ww.  j  a v  a2  s. co m
 * 
 * @param pubKeyFile String 
 * @return Signature 
 */
public static Signature createSignatureForVerify(String pubKeyFile) {
    try {
        CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
        FileInputStream fin = new FileInputStream(pubKeyFile);
        X509Certificate certificate = (X509Certificate) certificatefactory.generateCertificate(fin);
        PublicKey pub = certificate.getPublicKey();
        Signature dsa = Signature.getInstance("SHA1withDSA");
        dsa.initVerify(pub);
        return dsa;
    } catch (Exception ex) {
        logger.error("errors appeared while trying to verify a signature", ex);
        return null;
    }
}

From source file:com.thoughtworks.go.security.Registration.java

public static Registration fromJson(String json) {
    Map map = new Gson().fromJson(json, Map.class);
    List<Certificate> chain = new ArrayList<>();
    try {/*ww  w  .  ja va2s .  c om*/
        PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        PrivateKey privateKey = kf.generatePrivate(spec);
        String agentCertificate = (String) map.get("agentCertificate");
        PemReader certReader = new PemReader(new StringReader(agentCertificate));
        while (true) {
            PemObject obj = certReader.readPemObject();
            if (obj == null) {
                break;
            }
            chain.add(CertificateFactory.getInstance("X.509")
                    .generateCertificate(new ByteArrayInputStream(obj.getContent())));
        }
        return new Registration(privateKey, chain.toArray(new Certificate[chain.size()]));
    } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
        throw bomb(e);
    }
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static Certificate[] convertByteArrayToCertificate(byte[] sslCertificate) throws CertificateException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Collection c = cf.generateCertificates(new ByteArrayInputStream(sslCertificate));
    Certificate[] certs;//from w ww  .j a v  a 2s .  com
    certs = new Certificate[c.toArray().length];
    if (c.size() == 1) {
        InputStream certstream = new ByteArrayInputStream(sslCertificate);
        Certificate cert = cf.generateCertificate(certstream);
        certs[0] = cert;
    } else {
        certs = (Certificate[]) c.toArray();
    }

    return certs;
}

From source file:org.apache.ambari.server.security.encryption.CertificateUtils.java

/**
 * Get RSA public key from X.509 certificate string (full crt file content, including header and footer)
 * @param certificateString certificate string
 * @return RSA public key//from  w  ww  .ja  va2s  . co m
 * @throws CertificateException
 * @throws UnsupportedEncodingException
 */
public static RSAPublicKey getPublicKeyFromString(String certificateString)
        throws CertificateException, UnsupportedEncodingException {

    CertificateFactory fact = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream is = new ByteArrayInputStream(certificateString.getBytes("UTF8"));

    X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
    return (RSAPublicKey) cer.getPublicKey();
}

From source file:com.thoughtworks.go.security.RegistrationJSONizer.java

public static Registration fromJson(String json) {
    Map map = GSON.fromJson(json, Map.class);

    if (map.isEmpty()) {
        return Registration.createNullPrivateKeyEntry();
    }/*from   w  w  w  .j  a  v a 2s .co m*/

    List<X509Certificate> chain = new ArrayList<>();
    try {
        PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        PrivateKey privateKey = kf.generatePrivate(spec);
        String agentCertificate = (String) map.get("agentCertificate");
        PemReader certReader = new PemReader(new StringReader(agentCertificate));
        while (true) {
            PemObject obj = certReader.readPemObject();
            if (obj == null) {
                break;
            }
            chain.add((X509Certificate) CertificateFactory.getInstance("X.509")
                    .generateCertificate(new ByteArrayInputStream(obj.getContent())));
        }
        return new Registration(privateKey, chain.toArray(new X509Certificate[0]));
    } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
        throw bomb(e);
    }
}