@Test public void testGetCNsReal() throws IOException, GeneralSecurityException { String samplesDir = TEST_HOME + "samples/x509"; TrustMaterial tm = new TrustMaterial(samplesDir + "/x509_three_cns_foo_bar_hanako.pem"); X509Certificate c = (X509Certificate) tm.getCertificates().first(); String[] cns = Certificates.getCNs(c); Assert.assertEquals(3, cns.length); Assert.assertEquals("foo.com", cns[0]); Assert.assertEquals("bar.com", cns[1]); Assert.assertEquals("花子.co.jp", cns[2]); tm = new TrustMaterial(samplesDir + "/x509_foo_bar_hanako.pem"); c = (X509Certificate) tm.getCertificates().first(); cns = Certificates.getCNs(c); Assert.assertEquals(1, cns.length); Assert.assertEquals("foo.com", cns[0]); tm = new TrustMaterial(samplesDir + "/x509_wild_co_jp.pem"); c = (X509Certificate) tm.getCertificates().first(); cns = Certificates.getCNs(c); Assert.assertEquals(1, cns.length); Assert.assertEquals("*.co.jp", cns[0]); tm = new TrustMaterial(samplesDir + "/x509_wild_foo_bar_hanako.pem"); c = (X509Certificate) tm.getCertificates().first(); cns = Certificates.getCNs(c); Assert.assertEquals(1, cns.length); Assert.assertEquals("*.foo.com", cns[0]); tm = new TrustMaterial(samplesDir + "/x509_wild_foo.pem"); c = (X509Certificate) tm.getCertificates().first(); cns = Certificates.getCNs(c); Assert.assertEquals(1, cns.length); Assert.assertEquals("*.foo.com", cns[0]); }
public static String[] getCNs(X509Certificate cert) { try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); final LinkedList<String> cnList = new LinkedList<String>(); final LdapName subjectDN = new LdapName(subjectPrincipal); for (final Rdn rds : subjectDN.getRdns()) { final Attributes attributes = rds.toAttributes(); final Attribute cn = attributes.get("cn"); if (cn != null) { try { final Object value = cn.get(); if (value != null) { cnList.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } if (!cnList.isEmpty()) { return cnList.toArray(new String[cnList.size()]); } } catch (InvalidNameException ignore) { } return null; }
Certificates { public static String[] getCNs(X509Certificate cert) { try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); final LinkedList<String> cnList = new LinkedList<String>(); final LdapName subjectDN = new LdapName(subjectPrincipal); for (final Rdn rds : subjectDN.getRdns()) { final Attributes attributes = rds.toAttributes(); final Attribute cn = attributes.get("cn"); if (cn != null) { try { final Object value = cn.get(); if (value != null) { cnList.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } if (!cnList.isEmpty()) { return cnList.toArray(new String[cnList.size()]); } } catch (InvalidNameException ignore) { } return null; } }
Certificates { public static String[] getCNs(X509Certificate cert) { try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); final LinkedList<String> cnList = new LinkedList<String>(); final LdapName subjectDN = new LdapName(subjectPrincipal); for (final Rdn rds : subjectDN.getRdns()) { final Attributes attributes = rds.toAttributes(); final Attribute cn = attributes.get("cn"); if (cn != null) { try { final Object value = cn.get(); if (value != null) { cnList.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } if (!cnList.isEmpty()) { return cnList.toArray(new String[cnList.size()]); } } catch (InvalidNameException ignore) { } return null; }  }
Certificates { public static String[] getCNs(X509Certificate cert) { try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); final LinkedList<String> cnList = new LinkedList<String>(); final LdapName subjectDN = new LdapName(subjectPrincipal); for (final Rdn rds : subjectDN.getRdns()) { final Attributes attributes = rds.toAttributes(); final Attribute cn = attributes.get("cn"); if (cn != null) { try { final Object value = cn.get(); if (value != null) { cnList.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } if (!cnList.isEmpty()) { return cnList.toArray(new String[cnList.size()]); } } catch (InvalidNameException ignore) { } return null; }  static String toPEMString(X509Certificate cert); static String toString(byte[] x509Encoded); static String toString(X509Certificate cert); static String toString(X509Certificate cert, boolean htmlStyle); static List getCRLs(X509Extension cert); static void checkCRL(X509Certificate cert); static BigInteger getFingerprint(X509Certificate x509); static BigInteger getFingerprint(byte[] x509); static String getCN(X509Certificate cert); static String[] getCNs(X509Certificate cert); static String[] getDNSSubjectAlts(X509Certificate cert); static Certificate[] trimChain(Certificate[] chain); static X509Certificate[] x509ifyChain(Certificate[] chain); static void main(String[] args); }
Certificates { public static String[] getCNs(X509Certificate cert) { try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); final LinkedList<String> cnList = new LinkedList<String>(); final LdapName subjectDN = new LdapName(subjectPrincipal); for (final Rdn rds : subjectDN.getRdns()) { final Attributes attributes = rds.toAttributes(); final Attribute cn = attributes.get("cn"); if (cn != null) { try { final Object value = cn.get(); if (value != null) { cnList.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } if (!cnList.isEmpty()) { return cnList.toArray(new String[cnList.size()]); } } catch (InvalidNameException ignore) { } return null; }  static String toPEMString(X509Certificate cert); static String toString(byte[] x509Encoded); static String toString(X509Certificate cert); static String toString(X509Certificate cert, boolean htmlStyle); static List getCRLs(X509Extension cert); static void checkCRL(X509Certificate cert); static BigInteger getFingerprint(X509Certificate x509); static BigInteger getFingerprint(byte[] x509); static String getCN(X509Certificate cert); static String[] getCNs(X509Certificate cert); static String[] getDNSSubjectAlts(X509Certificate cert); static Certificate[] trimChain(Certificate[] chain); static X509Certificate[] x509ifyChain(Certificate[] chain); static void main(String[] args); final static CertificateFactory CF; final static String LINE_ENDING; final static String CRL_EXTENSION; final static String OCSP_EXTENSION; final static SerializableComparator COMPARE_BY_EXPIRY; }