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.sipfoundry.sipxconfig.admin.X509CertificateUtils.java

/**
 * Creates a X509 certificate based on the provided file path. Strips any preamble information
 * from the original certificate (if any)
 *
 * @param path/*w ww .ja v a 2 s .  c om*/
 * @return X509 Certificate or null if cannot create
 */
public static X509Certificate getX509Certificate(String path) {
    X509Certificate cert = null;
    LineIterator it = null;
    InputStream is = null;
    try {
        File sslCertFile = new File(path);
        it = FileUtils.lineIterator(sslCertFile);

        StringBuffer rep = new StringBuffer();
        boolean shouldWrite = false;
        while (it.hasNext()) {
            String line = it.nextLine();
            if (line.equals(BEGIN_LINE)) {
                shouldWrite = true;
            }
            if (shouldWrite) {
                rep.append(line);
                rep.append(NEW_LINE);
            }
        }

        is = new ByteArrayInputStream(rep.toString().getBytes());
        CertificateFactory cf = CertificateFactory.getInstance(X509_TYPE);
        cert = (X509Certificate) cf.generateCertificate(is);
    } catch (Exception ex) {
        cert = null;
    } finally {
        LineIterator.closeQuietly(it);
        IOUtils.closeQuietly(is);
    }
    return cert;
}

From source file:fi.vm.kapa.identification.shibboleth.extauthn.util.CertificateUtil.java

public static X509Certificate getCertificate(String pemCertificate) {
    X509Certificate newCert = null;
    try {//  ww  w.j a v  a2  s  .co m
        // Add \r after cert header field, Cert API needs this (Legacy method, Apache2-provided cert)
        pemCertificate = pemCertificate.replaceFirst(X509_PEM_HEADER, X509_PEM_HEADER + "\r");

        // Add X509 header and footer, Cert API needs this (SCS method, SCS-provided certificate)
        if (!pemCertificate.contains(X509_PEM_HEADER)) {
            pemCertificate = X509_PEM_HEADER + "\r" + pemCertificate + "\r" + X509_PEM_FOOTER;
        }

        if (StringUtils.isNotBlank(pemCertificate)) {
            InputStream in = new ByteArrayInputStream(pemCertificate.getBytes());
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            newCert = (X509Certificate) cf.generateCertificate(in);
        }
    } catch (final CertificateException | NullPointerException e) {
        logger.warn("Error getting client certificate from request", e);
    }
    return newCert;
}

From source file:wsattacker.library.signatureFaking.helper.CertificateHandlerTest.java

public static void testCertificateHandler() throws Exception {
    String certificate = FileReader.readFile(DIR + "/test-cert");
    CertificateHandler ch = new CertificateHandler(certificate);
    ch.createFakedCertificate();//from   w ww. j  a  v a2  s  .  c  o  m
    X509CertImpl faked = ch.getFakedCertificate();

    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    X509Certificate original = (X509Certificate) certFactory
            .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(certificate)));

    assertEquals(faked.getIssuerDN().getName(), original.getIssuerDN().getName());
    assertEquals(faked.getSigAlgOID(), original.getSigAlgOID());
    assertEquals(faked.getSubjectDN().getName(), original.getSubjectDN().getName());
    faked.verify(faked.getPublicKey());
}

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

/**
 * Build a certificate from PEM resource.
 * //from   ww w .  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:syncthing.api.SyncthingSSLSocketFactory.java

public static X509Certificate makeCert(String cert) {
    X509Certificate tmpCa = null;
    if (cert != null) {
        InputStream inStream = null;
        try {/*from   ww w  .j a  v a2  s . c  o m*/
            inStream = new ByteArrayInputStream(cert.getBytes("UTF-8"));
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            tmpCa = (X509Certificate) cf.generateCertificate(inStream);
        } catch (CertificateException | UnsupportedEncodingException e) {
            Timber.e("Failed to parse certificate", e);
        } finally {
            IOUtils.closeQuietly(inStream);
        }
    }
    return tmpCa;
}

From source file:se.inera.axel.shs.broker.validation.certificate.PemConverter.java

private static X509Certificate generateX509Certificate(String pemCertificate) throws CertificateException {
    InputStream is = new ByteArrayInputStream((pemCertificate).getBytes());
    BufferedInputStream bis = new BufferedInputStream(is);

    CertificateFactory factory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) factory.generateCertificate(bis);
    return certificate;
}

From source file:Main.java

/**
 * Generate a SSLSocketFactory wich checks the certificate given
 * @param context Context to use/*from w w  w. j  a v  a  2 s  .c  o  m*/
 * @param rResource int with url of the resource to read the certificate
 * @parma password String to use with certificate
 * @return SSLSocketFactory generated to validate this certificate
 */
public static SSLSocketFactory newSslSocketFactory(Context context, int rResource, String password)
        throws CertificateException, NoSuchProviderException, KeyStoreException, NoSuchAlgorithmException,
        IOException, UnrecoverableKeyException, KeyManagementException {

    // Get an instance of the Bouncy Castle KeyStore format
    KeyStore trusted = KeyStore.getInstance("BKS");
    // Get the raw resource, which contains the keystore with
    // your trusted certificates (root and any intermediate certs)
    InputStream is = context.getApplicationContext().getResources().openRawResource(rResource);

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
    X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is);
    String alias = "alias";//cert.getSubjectX500Principal().getName();

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);
    trustStore.setCertificateEntry(alias, cert);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    kmf.init(trustStore, null);
    KeyManager[] keyManagers = kmf.getKeyManagers();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    return sslContext.getSocketFactory();

}

From source file:fi.laverca.util.X509Util.java

/**
 * Convert a DER certificate to X509Certificate
 * @param der Certificate to convert//w ww . j  a v  a  2 s .c om
 * @return Converted certificate as X509Certificate or null if the conversion failed
 */
public static X509Certificate DERtoX509Certificate(final byte[] der) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(der);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(bis);
    } catch (Exception e) {
        log.error(e);
    }
    return null;
}

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  w ww  .ja v  a 2  s .c o m
    return cert;
}

From source file:org.waveprotocol.wave.crypto.CertUtil.java

public static X509Certificate getCertFromBytes(byte[] derCert) throws GeneralSecurityException {
    CertificateFactory fac = CertificateFactory.getInstance("X509");
    ByteArrayInputStream in = new ByteArrayInputStream(derCert);
    X509Certificate cert = (X509Certificate) fac.generateCertificate(in);
    return cert;/*from   w ww. j  a  v a  2s  . co  m*/
}