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

/**
 * whether current app is debuggable/*from  ww w .  j ava2s .  c o  m*/
 */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = packageInfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable) {
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    return debuggable;
}

From source file:Main.java

/**
 *
 * @param f/*from  www  .  j  av a2  s  .  co  m*/
 * @throws Exception
 */
private static X509Certificate readCertificate(File f) throws CertificateException, IOException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    // Use BufferedInputStream (which supports mark and reset) so that each
    // generateCertificate call consumes one certificate.
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
    X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
    in.close();
    return cert;
}

From source file:Main.java

/**
 * load the certificates as X.509 certificates
 *//*from   w  ww  .  j a  va  2  s  .  c o  m*/
private static Collection<? extends Certificate> loadCertificates(final java.io.InputStream is)
        throws IOException, CertificateException {

    final CertificateFactory factory = CertificateFactory.getInstance("X.509");
    return factory.generateCertificates(is);
}

From source file:Main.java

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

    if (certificateString == null) {
        throw new IllegalArgumentException("certificateString cannot be null");
    }/* www. j  a  v a  2  s .  com*/

    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: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 boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/*from  w ww.j a  v  a 2  s  .  co 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;
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/*w  ww. j  a v a  2s  .  c  om*/
        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");//No i18n
            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:fi.laverca.util.X509Util.java

/**
 * Convert a DER certificate to X509Certificate
 * @param der Certificate to convert//from   www.  ja  v a 2  s .  com
 * @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:org.haox.pki.Pkix.java

public static List<Certificate> getCerts(InputStream inputStream) throws IOException, CertificateException {
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    Collection<? extends Certificate> certs = (Collection<? extends Certificate>) certFactory
            .generateCertificates(inputStream);

    return new ArrayList<Certificate>(certs);
}

From source file:com.google.u2f.TestUtils.java

public static X509Certificate parseCertificate(byte[] encodedDerCertificate) {
    try {//from  ww w  .j  ava2 s . co m
        return (X509Certificate) CertificateFactory.getInstance("X.509")
                .generateCertificate(new ByteArrayInputStream(encodedDerCertificate));
    } catch (CertificateException e) {
        throw new RuntimeException(e);
    }
}