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:Main.java

public static X509Certificate fromString(String certificate) throws CertificateException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    return (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certificate.getBytes()));
}

From source file:Main.java

static X509Certificate loadPEMCertificate(String fname) throws Exception {
    try (FileInputStream fis = new FileInputStream(fname)) {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(fis);
    }//from  w ww. j ava2  s .  c o m
}

From source file:Main.java

public static X509Certificate getCertificateFromBlob(byte[] encoded) throws IOException {
    try {//w w  w .  ja  v a  2 s . c  o  m
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(encoded));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse);
    }
}

From source file:Main.java

/**
 * Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
 *//*from   w  w  w.  j  a  v a  2s .  c o  m*/
public static SSLSocketFactory newSslSocketFactoryForCa(InputStream certChain) throws Exception {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(new BufferedInputStream(certChain));
    X500Principal principal = cert.getSubjectX500Principal();
    ks.setCertificateEntry(principal.getName("RFC2253"), cert);
    //    ks.setCertificateEntry("ca", cert);

    // Set up trust manager factory to use our key store.
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(ks);
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, trustManagerFactory.getTrustManagers(), null);
    return context.getSocketFactory();
}

From source file:mx.bigdata.cfdi.security.KeyLoader.java

public static X509Certificate loadX509Certificate(InputStream in) throws Exception {
    CertificateFactory factory = CertificateFactory.getInstance("X.509");
    return (X509Certificate) factory.generateCertificate(in);
}

From source file:Main.java

public static X509Certificate base64StringToCertificate(String certificateString)
        throws CertificateException, IOException {

    if (certificateString == null) {
        throw new IllegalArgumentException("certificateString cannot be null");
    }//from ww w. j a v  a  2  s  .c o m

    byte[] encodedCert = Base64.decode(certificateString, Base64.DEFAULT);
    InputStream inStream = new ByteArrayInputStream(encodedCert);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
    inStream.close();

    return cert;
}

From source file:Main.java

/**
 * populate X509Certificate from a certificate file at the certFilePath
 *//*from w  w w  .java  2 s. c o m*/
public static X509Certificate getX509(String certFilePath) throws CertificateException, FileNotFoundException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in = new FileInputStream(certFilePath);
    return (X509Certificate) cf.generateCertificate(in);
}

From source file:Main.java

public static X509Certificate unserializeCert(String cert) throws CertificateException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream in = new ByteArrayInputStream(cert.getBytes());
    return (X509Certificate) cf.generateCertificate(in);
}

From source file:org.wso2.carbon.identity.authenticator.inbound.saml2sso.util.Utils.java

/**
 * TODO: ideally this method must be in identity.commons. However the one in identity.commons uses
 * TODO: java.util.Base64 which doesn't work here. Only the OpenSAML Base64 decoder works. Until then duplicating
 * TODO: this method.//from w w  w  .j a v a2  s  .  c o m
 * Decode X509 certificate.
 *
 * @param encodedCert Base64 encoded certificate
 * @return Decoded <code>Certificate</code>
 * @throws java.security.cert.CertificateException Error when decoding certificate
 */
public static Certificate decodeCertificate(String encodedCert) throws CertificateException {

    if (StringUtils.isNotBlank(encodedCert)) {
        byte[] bytes = Base64.decode(encodedCert);
        if (bytes == null || bytes.length == 0) {
            throw new CertificateException("Encoded certificate is invalid: " + encodedCert);
        }
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(bytes));
        return cert;
    } else {
        throw new CertificateException("Encoded certificate is empty: " + encodedCert);
    }
}

From source file:com.cloud.utils.security.CertificateHelper.java

public static Certificate buildCertificate(String content) throws CertificateException {
    assert (content != null);

    BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(content.getBytes()));
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    return cf.generateCertificate(bis);
}