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:nl.surfnet.spring.security.opensaml.util.KeyStoreUtil.java

/**
 * Append a certificate to the given key store
 * @param keyStore/*w  w w .j  ava2  s.com*/
 * @param keyAlias
 * @param pemCert
 */
public static void appendCertificateToKeyStore(KeyStore keyStore, String keyAlias, String pemCert) {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + pemCert + "\n-----END CERTIFICATE-----";
    ByteArrayInputStream certificateInputStream = new ByteArrayInputStream(wrappedCert.getBytes());
    try {
        final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        final Certificate cert = certificateFactory.generateCertificate(certificateInputStream);
        IOUtils.closeQuietly(certificateInputStream);
        keyStore.setCertificateEntry(keyAlias, cert);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.vmware.identity.openidconnect.protocol.PersonUserCertificateGrant.java

public static PersonUserCertificateGrant parse(Map<String, String> parameters) throws ParseException {
    Validate.notNull(parameters, "parameters");

    GrantType grantType = GrantType.parse(ParameterMapUtils.getString(parameters, "grant_type"));
    if (grantType != GRANT_TYPE) {
        throw new ParseException("unexpected grant_type: " + grantType.getValue());
    }//from  w  w  w .ja v a2  s.  c o m

    String personUserCertificateString = ParameterMapUtils.getString(parameters, "person_user_certificate");

    byte[] certBytes = Base64Utils.decodeToBytes(personUserCertificateString);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(certBytes);

    X509Certificate personUserCertificate;
    try {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        personUserCertificate = (X509Certificate) certFactory.generateCertificate(inputStream);
    } catch (CertificateException e) {
        throw new ParseException("failed to parse person_user_certificate parameter", e);
    }

    PersonUserAssertion personUserAssertion = PersonUserAssertion.parse(parameters);

    return new PersonUserCertificateGrant(personUserCertificate, personUserAssertion);
}

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

/**
 * ?X.509?/*from   w w  w.  j av  a2s.  c  om*/
 * 
 * @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:org.xdi.util.security.CertificateHelper.java

public static X509Certificate loadCertificate(byte[] certificate) throws CertificateException {
    CertificateFactory fty = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(certificate));
    X509Certificate x509Certificate = (X509Certificate) fty.generateCertificate(bais);

    return x509Certificate;
}

From source file:org.xdi.oxauth.util.CertUtil.java

public static X509Certificate x509CertificateFromBytes(byte[] cert) {
    try {/*from   w ww  .  jav a 2  s.  c o m*/
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        InputStream bais = new ByteArrayInputStream(cert);

        return (X509Certificate) certFactory.generateCertificate(bais);
    } catch (CertificateException ex) {
        log.error("Failed to parse X.509 certificates from bytes", ex);
    }

    return null;
}

From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLCertReader.java

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

From source file:Main.java

public static List<Certificate> loadCertificates(String[] pemEncodedCerts) throws Exception {

    List<Certificate> certList = new ArrayList<>();
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

    for (String certPem : pemEncodedCerts) {
        InputStream certIn = new ByteArrayInputStream(certPem.getBytes());
        Certificate cert = certFactory.generateCertificate(certIn);
        certList.add(cert);//from  w w  w.j a  v a2 s  .  c o m
    }

    return certList;
}

From source file:org.openanzo.rdf.utils.KeystoreUtils.java

/**
 * /*from   www  .  ja va 2s.  com*/
 * @param keyStoreFile
 * @param keystoreType
 * @param password
 * @param alias
 * @param in
 * @throws AnzoException
 */
public static void addTrustedCert(String keyStoreFile, String keystoreType, String password, String alias,
        InputStream in) throws AnzoException {
    try {

        CertificateFactory cf = CertificateFactory.getInstance("X509");

        X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
        if (cert.getSubjectDN().equals(cert.getIssuerDN())) {
            cert.verify(cert.getPublicKey());
        }
        addTrustedCert(keyStoreFile, keystoreType, password, alias, cert);

    } catch (Exception cce) {
        throw new AnzoException(ExceptionConstants.OSGI.INTERNAL_COMPONENT_ERROR, cce);
    }
}

From source file:Main.java

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;//from   w  w w.  j a  va2s  .  c  o  m
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {//from w  w w.ja  v  a2 s.c  o  m
        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;
}