@Test public void testDisplayName() throws Exception { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate clientCert = (X509Certificate) cf.generateCertificate(getClass().getResourceAsStream( "startssl-client.crt")); assertEquals("Andreas Schildbach", X509Utils.getDisplayNameFromCertificate(clientCert, false)); X509Certificate comodoCert = (X509Certificate) cf.generateCertificate(getClass().getResourceAsStream( "comodo-smime.crt")); assertEquals("comodo.com@schildbach.de", X509Utils.getDisplayNameFromCertificate(comodoCert, true)); }
@Nullable public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException { X500Name name = new X500Name(certificate.getSubjectX500Principal().getName()); String commonName = null, org = null, location = null, country = null; for (RDN rdn : name.getRDNs()) { AttributeTypeAndValue pair = rdn.getFirst(); String val = ((ASN1String) pair.getValue()).getString(); ASN1ObjectIdentifier type = pair.getType(); if (type.equals(RFC4519Style.cn)) commonName = val; else if (type.equals(RFC4519Style.o)) org = val; else if (type.equals(RFC4519Style.l)) location = val; else if (type.equals(RFC4519Style.c)) country = val; } final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames(); String altName = null; if (subjectAlternativeNames != null) for (final List<?> subjectAlternativeName : subjectAlternativeNames) if ((Integer) subjectAlternativeName.get(0) == 1) altName = (String) subjectAlternativeName.get(1); if (org != null) { return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org; } else if (commonName != null) { return commonName; } else { return altName; } }
X509Utils { @Nullable public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException { X500Name name = new X500Name(certificate.getSubjectX500Principal().getName()); String commonName = null, org = null, location = null, country = null; for (RDN rdn : name.getRDNs()) { AttributeTypeAndValue pair = rdn.getFirst(); String val = ((ASN1String) pair.getValue()).getString(); ASN1ObjectIdentifier type = pair.getType(); if (type.equals(RFC4519Style.cn)) commonName = val; else if (type.equals(RFC4519Style.o)) org = val; else if (type.equals(RFC4519Style.l)) location = val; else if (type.equals(RFC4519Style.c)) country = val; } final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames(); String altName = null; if (subjectAlternativeNames != null) for (final List<?> subjectAlternativeName : subjectAlternativeNames) if ((Integer) subjectAlternativeName.get(0) == 1) altName = (String) subjectAlternativeName.get(1); if (org != null) { return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org; } else if (commonName != null) { return commonName; } else { return altName; } } }
X509Utils { @Nullable public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException { X500Name name = new X500Name(certificate.getSubjectX500Principal().getName()); String commonName = null, org = null, location = null, country = null; for (RDN rdn : name.getRDNs()) { AttributeTypeAndValue pair = rdn.getFirst(); String val = ((ASN1String) pair.getValue()).getString(); ASN1ObjectIdentifier type = pair.getType(); if (type.equals(RFC4519Style.cn)) commonName = val; else if (type.equals(RFC4519Style.o)) org = val; else if (type.equals(RFC4519Style.l)) location = val; else if (type.equals(RFC4519Style.c)) country = val; } final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames(); String altName = null; if (subjectAlternativeNames != null) for (final List<?> subjectAlternativeName : subjectAlternativeNames) if ((Integer) subjectAlternativeName.get(0) == 1) altName = (String) subjectAlternativeName.get(1); if (org != null) { return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org; } else if (commonName != null) { return commonName; } else { return altName; } }  }
X509Utils { @Nullable public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException { X500Name name = new X500Name(certificate.getSubjectX500Principal().getName()); String commonName = null, org = null, location = null, country = null; for (RDN rdn : name.getRDNs()) { AttributeTypeAndValue pair = rdn.getFirst(); String val = ((ASN1String) pair.getValue()).getString(); ASN1ObjectIdentifier type = pair.getType(); if (type.equals(RFC4519Style.cn)) commonName = val; else if (type.equals(RFC4519Style.o)) org = val; else if (type.equals(RFC4519Style.l)) location = val; else if (type.equals(RFC4519Style.c)) country = val; } final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames(); String altName = null; if (subjectAlternativeNames != null) for (final List<?> subjectAlternativeName : subjectAlternativeNames) if ((Integer) subjectAlternativeName.get(0) == 1) altName = (String) subjectAlternativeName.get(1); if (org != null) { return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org; } else if (commonName != null) { return commonName; } else { return altName; } }  @Nullable static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation); static KeyStore loadKeyStore(String keystoreType, @Nullable String keystorePassword, InputStream is); }
X509Utils { @Nullable public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException { X500Name name = new X500Name(certificate.getSubjectX500Principal().getName()); String commonName = null, org = null, location = null, country = null; for (RDN rdn : name.getRDNs()) { AttributeTypeAndValue pair = rdn.getFirst(); String val = ((ASN1String) pair.getValue()).getString(); ASN1ObjectIdentifier type = pair.getType(); if (type.equals(RFC4519Style.cn)) commonName = val; else if (type.equals(RFC4519Style.o)) org = val; else if (type.equals(RFC4519Style.l)) location = val; else if (type.equals(RFC4519Style.c)) country = val; } final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames(); String altName = null; if (subjectAlternativeNames != null) for (final List<?> subjectAlternativeName : subjectAlternativeNames) if ((Integer) subjectAlternativeName.get(0) == 1) altName = (String) subjectAlternativeName.get(1); if (org != null) { return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org; } else if (commonName != null) { return commonName; } else { return altName; } }  @Nullable static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation); static KeyStore loadKeyStore(String keystoreType, @Nullable String keystorePassword, InputStream is);  }