Example usage for java.security.cert X509Certificate getSubjectX500Principal

List of usage examples for java.security.cert X509Certificate getSubjectX500Principal

Introduction

In this page you can find the example usage for java.security.cert X509Certificate getSubjectX500Principal.

Prototype

public X500Principal getSubjectX500Principal() 

Source Link

Document

Returns the subject (subject distinguished name) value from the certificate as an X500Principal .

Usage

From source file:Main.java

protected static X500Principal getSubjectPrincipal(X509Certificate cert) {
    return cert.getSubjectX500Principal();
}

From source file:Main.java

public static boolean isTrustAnchor(X509Certificate certificate) throws IOException {
    boolean trust_anchor = certificate.getSubjectX500Principal().equals(certificate.getIssuerX500Principal())
            && certificate.getBasicConstraints() >= 0;
    if (trust_anchor) {
        try {/* w  ww . ja v a  2 s .  co m*/
            certificate.verify(certificate.getPublicKey());
        } catch (Exception e) {
            throw new IOException(e);
        }
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {//from  www. j  a v  a 2  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;
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/*w w w .jav a 2 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;
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/*from  ww  w .j  a  va2  s .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:Main.java

/**
 * whether current app is debuggable//  w w  w  .  j a v a  2 s .com
 */
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

/**
 * Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
 *//*from  w w  w  .j  a v a2s.com*/
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:Main.java

private static boolean isDebugCertificateCheck(final Context context) {
    final X500Principal DEBUG_CERTIFICATE_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
    boolean debuggable = false;

    try {//from   w ww.j  a v a  2 s  .c o m
        Signature[] signatures = getSignatures(context);

        for (int i = 0; i < signatures.length; i++) {
            X509Certificate certificate = generateX509CertificateFromSignature(signatures[i]);
            debuggable = certificate.getSubjectX500Principal().equals(DEBUG_CERTIFICATE_DN);
            if (debuggable) {
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        // package not found - debuggable = false
    } catch (CertificateException e) {
        // certificate factory non-instantiable - debuggable = false
    }
    return debuggable;
}

From source file:Main.java

private static boolean isDebugCertificateCheck(final Context context) {
    final X500Principal DEBUG_CERTIFICATE_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
    boolean debuggable = false;

    try {//from  w  ww .j a  va2s .co  m
        Signature[] signatures = getSignatures(context);

        for (int i = 0; i < signatures.length; i++) {
            X509Certificate certificate = generateX509CertificateFromSignature(signatures[i]);
            debuggable = certificate.getSubjectX500Principal().equals(DEBUG_CERTIFICATE_DN);
            if (debuggable) {
                return true;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        // package not found - debuggable = false
    } catch (CertificateException e) {
        // certificate factory non-instantiable - debuggable = false
    }
    return false;
}

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

/**
 * Parse the given RND type from the given certificate's subject
 * @param cert Certificate/*  w  w w.j a v  a 2  s .  c o m*/
 * @param rdnType RND type
 * @return parsed value as String
 */
public static String parseSubjectName(final X509Certificate cert, final String rdnType) {
    String dn = cert.getSubjectX500Principal().getName();

    String name = null;
    try {
        LdapName ldapDn = new LdapName(dn);
        List<Rdn> rdns = ldapDn.getRdns();
        for (Rdn r : rdns) {
            if (rdnType.equals(r.getType())) {
                name = r.getValue().toString();
            }
        }
    } catch (InvalidNameException e) {
        log.error(e);
    }

    return name;
}