title: Retrieve Subject alternative names of X.509 certificate in java
body: <p>I have tried using the solution provided in this <a href="https://stackoverflow.com/questions/11961913/read-alternative-name-from-certificate">link</a>.</p>
  <p>I am getting following error when i tried reading subject alternative names of X.509 Certificate</p>
  <blockquote>
  <p>java.lang.NoSuchMethodError: org.bouncycastle.asn1.ASN1InputStream.readObject()Lorg/bouncycastle/asn1/DERObject;</p>
  </blockquote>
  <p>At below line of code</p>
  <blockquote>
  <p>ASN1InputStream decoder = new ASN1InputStream((byte[]) item.toArray());</p>
  <p>DEREncodable encoded = decoder.readObject();</p>
  </blockquote>
  <p>.der file is used to create certificate as follows.</p>
  <pre><code>X509Certificate cert=null;
  fis = new FileInputStream(file.getAbsoluteFile());     //.der file
  bis = new BufferedInputStream(fis);
  
  CertificateFactory cf = CertificateFactory.getInstance(&quot;X.509&quot;);
  while (bis.available() &gt; 0) {
                      try{
                      cert = cf.generateCertificate(bis);
                      }
                       catch (CertificateException e) {
                        e.printStackTrace();
                    }
   List list=getSubjectAlternativeNames((X509Certificate) cert);
  </code></pre>
  <p>Below is the solution i got from the link mentioned above.</p>
  <pre><code>   public static List&lt;String&gt; getSubjectAlternativeNames(X509Certificate certificate) {
      List&lt;String&gt; identities = new ArrayList&lt;String&gt;();
      try {
          Collection&lt;List&lt;?&gt;&gt; altNames = certificate.getSubjectAlternativeNames();
          // Check that the certificate includes the SubjectAltName extension
          if (altNames == null)
              return Collections.emptyList();
          // Use the type OtherName to search for the certified server name
          for (List item : altNames) {
              Integer type = (Integer) item.get(0);
              if (type == 0)
                  // Type OtherName found so return the associated value
                  try {
                      // Value is encoded using ASN.1 so decode it to get the server's identity
                      ASN1InputStream decoder = new ASN1InputStream((byte[]) item.toArray()[1]);
                      DEREncodable encoded = decoder.readObject();
                      encoded = ((DERSequence) encoded).getObjectAt(1);
                      encoded = ((DERTaggedObject) encoded).getObject();
                      encoded = ((DERTaggedObject) encoded).getObject();
                      String identity = ((DERUTF8String) encoded).getString();
                      // Add the decoded server name to the list of identities
                      identities.add(identity);
                  }
              catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
                 // log.error(&quot;Error decoding subjectAltName&quot; + e.getLocalizedMessage(),e);
              }
              catch (Exception e) {
                 // log.error(&quot;Error decoding subjectAltName&quot; + e.getLocalizedMessage(),e);
                  e.printStackTrace();
              }
              // Other types are not good for XMPP so ignore them
              //log.warn(&quot;SubjectAltName of invalid type found: &quot; + certificate);
          }
      }
      catch (CertificateParsingException e) {
          e.printStackTrace();
         // log.error(&quot;Error parsing SubjectAltName in certificate: &quot; + certificate + &quot;\r\nerror:&quot; + e.getLocalizedMessage(),e);
      }
      return identities;
  }
  </code></pre>
  <p>Is it that i have not used proper .jar file?</p>
  <p>.jar i have used is  --&gt; bcprov-jdk16-1.45.jar</p>
  <p>Suggest me where i have gone wrong.</p>