title: how to extract CN from X509Certificate in Java - without using Bouncy Castle?
body: <p>I want to preferably use only what is bundled with java security package.</p>
  
  <p>From this <a href="https://stackoverflow.com/a/7933650/398348">answer</a>, I tried:</p>
  
  <pre><code>static void parseCert(String filename) throws FileNotFoundException, CertificateException, IOException, InvalidNameException {
      FileInputStream fis = new FileInputStream(filename);
      BufferedInputStream bis = new BufferedInputStream(fis);
  
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
  
      while (bis.available() &gt; 0) {
          X509Certificate cert = (X509Certificate) cf.generateCertificate(bis);
          String dn = cert.getIssuerX500Principal().getName();
          System.out.println("DN is: " + dn);
          LdapName ln = new LdapName(dn);
  
          for (Rdn rdn : ln.getRdns()) {
              if (rdn.getType().equalsIgnoreCase("CN")) {
                  System.out.println("CN is: " + rdn.getValue());
                  break;
              }
          }
      }
  }
  </code></pre>
  
  <p>Output is</p>
  
  <blockquote>
    <p>DN is: CN=LAME_IssuingCA O\=PIG C\=US</p>
    
    <p>CN is: LAME_IssuingCA O=PIG C=US</p>
  </blockquote>
  
  <p>Isn't this incorrect (O and C are part of CN??)</p>