Example usage for java.security.cert CertificateFactory generateCertificate

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

Introduction

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

Prototype

public final Certificate generateCertificate(InputStream inStream) throws CertificateException 

Source Link

Document

Generates a certificate object and initializes it with the data read from the input stream inStream .

Usage

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  w w .j  a  v  a 2  s  .  c  om*/
    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.panbox.core.crypto.CryptCore.java

/**
 * Creates an X509 Certificate for a given byte array
 * //from ww  w . j  av a2 s .com
 * @param certBytes
 * @return
 */
public static X509Certificate createCertificateFromBytes(byte[] certBytes) {
    X509Certificate cert = null;
    CertificateFactory certFactory;
    try {
        certFactory = CertificateFactory.getInstance("X.509");

        InputStream in = new ByteArrayInputStream(certBytes);
        cert = (X509Certificate) certFactory.generateCertificate(in);

    } catch (CertificateException e) {
        logger.warn("Excpetion caught in CryptCore." + "createCertificateFromBytes, returning null", e);
    }

    return cert;
}

From source file:com.raspberry.library.util.AppUtils.java

/**
 * Judge whether an app is dubuggable//  w ww  .  j a  va2s .co  m
 *
 * @param ctx
 * @return
 */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable) {
                break;
            }
        }

    } catch (NameNotFoundException e) {
    } catch (CertificateException e) {
    }
    return debuggable;
}

From source file:org.apache.zeppelin.realm.jwt.KnoxJwtRealm.java

public static RSAPublicKey parseRSAPublicKey(String pem) throws IOException, ServletException {
    final String pemHeader = "-----BEGIN CERTIFICATE-----\n";
    final String pemFooter = "\n-----END CERTIFICATE-----";
    String fullPem = pemHeader + pem + pemFooter;
    PublicKey key = null;/*from   w w  w.  ja  va 2s . com*/
    try {
        CertificateFactory fact = CertificateFactory.getInstance("X.509");
        ByteArrayInputStream is = new ByteArrayInputStream(
                FileUtils.readFileToString(new File(pem)).getBytes("UTF8"));
        X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
        key = cer.getPublicKey();
    } catch (CertificateException ce) {
        String message = null;
        if (pem.startsWith(pemHeader)) {
            message = "CertificateException - be sure not to include PEM header "
                    + "and footer in the PEM configuration element.";
        } else {
            message = "CertificateException - PEM may be corrupt";
        }
        throw new ServletException(message, ce);
    } catch (UnsupportedEncodingException uee) {
        throw new ServletException(uee);
    } catch (IOException e) {
        throw new IOException(e);
    }
    return (RSAPublicKey) key;
}

From source file:at.asitplus.regkassen.core.base.util.CashBoxUtils.java

/**
 * extract certificates from DEP Export Format String representation
 *
 * @param base64EncodedCertificate BASE64 encoded DER-encoded-certificate
 * @return java object for X509Certificate
 * @throws CertificateException/*  w ww  .  ja v a2s.co m*/
 */
public static X509Certificate parseCertificate(String base64EncodedCertificate) throws CertificateException {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream bIn = new ByteArrayInputStream(
            CashBoxUtils.base64Decode(base64EncodedCertificate, false));
    return (X509Certificate) certificateFactory.generateCertificate(bIn);
}

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

/**
 * Creates a Certificate from a PEM encoded String
 *
 * @param certificateString/* w w  w  .ja  v a2 s.  c  o m*/
 * @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:de.brendamour.jpasskit.signing.PKSigningUtil.java

/**
 * Load a DEAR Certificate from an <code>InputStream</code>.
 * /*w  ww  . ja v  a2 s  .c om*/
 * The caller is responsible for closing the stream after this method returns successfully or fails.
 * 
 * @param certificateInputStream
 *            <code>InputStream</code> containing the certificate.
 * @return Loaded certificate.
 * @throws IOException
 * @throws CertificateException
 */
public static X509Certificate loadDERCertificate(final InputStream certificateInputStream)
        throws IOException, CertificateException {
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509",
                BouncyCastleProvider.PROVIDER_NAME);
        Certificate certificate = certificateFactory.generateCertificate(certificateInputStream);
        if (certificate instanceof X509Certificate) {
            return (X509Certificate) certificate;
        }
        throw new IOException("The key from the input stream could not be decrypted");
    } catch (IOException ex) {
        throw new IOException("The key from the input stream could not be decrypted", ex);
    } catch (NoSuchProviderException ex) {
        throw new IOException("The key from the input stream could not be decrypted", ex);
    }
}

From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java

static X509Certificate loadLicenseCaCertificate() throws CertificateException {
    final CertificateFactory cf = CertificateFactory.getInstance("X.509");
    final InputStream stream = BlackDuckHubUpdateSite.class
            .getResourceAsStream("/blackduck-hub-root-cacert.pem");
    try {//from w w w . j a  v  a 2  s .c o  m
        return stream != null ? (X509Certificate) cf.generateCertificate(stream) : null;
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.http.JsseListener.java

/**
 * Return the chain of X509 certificates used to negotiate the SSL Session.
 * <p>/*from   w w w .  j  a  v a2 s  .c  om*/
 * Note: in order to do this we must convert a javax.security.cert.X509Certificate[], as used by
 * JSSE to a java.security.cert.X509Certificate[],as required by the Servlet specs.
 * 
 * @param sslSession the javax.net.ssl.SSLSession to use as the source of the cert chain.
 * @return the chain of java.security.cert.X509Certificates used to negotiate the SSL
 *         connection. <br>
 *         Will be null if the chain is missing or empty.
 */
private static X509Certificate[] getCertChain(SSLSession sslSession) {
    try {
        javax.security.cert.X509Certificate javaxCerts[] = sslSession.getPeerCertificateChain();
        if (javaxCerts == null || javaxCerts.length == 0)
            return null;

        int length = javaxCerts.length;
        X509Certificate[] javaCerts = new X509Certificate[length];

        java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory.getInstance("X.509");
        for (int i = 0; i < length; i++) {
            byte bytes[] = javaxCerts[i].getEncoded();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            javaCerts[i] = (X509Certificate) cf.generateCertificate(stream);
        }

        return javaCerts;
    } catch (SSLPeerUnverifiedException pue) {
        return null;
    } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
        return null;
    }
}

From source file:se.leap.bitmaskclient.ConfigHelper.java

public static X509Certificate parseX509CertificateFromString(String certificate_string) {
    java.security.cert.Certificate certificate = null;
    CertificateFactory cf;
    try {//  w  w w .j  a  va2  s .c  om
        cf = CertificateFactory.getInstance("X.509");

        certificate_string = certificate_string.replaceFirst("-----BEGIN CERTIFICATE-----", "")
                .replaceFirst("-----END CERTIFICATE-----", "").trim();
        byte[] cert_bytes = Base64.decode(certificate_string, Base64.DEFAULT);
        InputStream caInput = new ByteArrayInputStream(cert_bytes);
        try {
            certificate = cf.generateCertificate(caInput);
            System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN());
        } finally {
            caInput.close();
        }
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        return null;
    } catch (IllegalArgumentException e) {
        return null;
    }

    return (X509Certificate) certificate;
}