title: How to get the Policy Identifier and the Subject Type of Basic Constraints in a X509Certificate of java
body: <p>I have a X509Certificate in java and I want to get the value of the <strong><code>Policy Identifier</code></strong> which there exists in the <strong><code>Certificate Policies</code></strong> field, as depicted in the following picture:</p>
  
  <p><a href="https://i.stack.imgur.com/cUQrH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cUQrH.png" alt="enter image description here"></a></p>
  
  <p>Also, I want to get the value of the <strong><code>Subject Type</code></strong> in <strong><code>Basic Constraints</code></strong> field, as depicted in the following picture:</p>
  
  <p><a href="https://i.stack.imgur.com/CFvZc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CFvZc.png" alt="enter image description here"></a></p>
  
  <p>My code:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      CertificateFactory cf = CertificateFactory.getInstance("X509");
      InputStream in = new FileInputStream(new File("E:\\myCert.crt"));
      X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
      int length = cert.getCertificateExtensionOIDs().size();
      String oid;
      for(int i = 0; i &lt; length; i++){
          oid = cert.getCertificateExtensionOIDs().iterator().next();
          byte[] UID = cert.getExtensionValue(oid);
          DERObject derObject = toDERObject(UID);
          if(derObject instanceof DEROctetString){
              DEROctetString derOctetString = (DEROctetString) derObject;
              derObject = toDERObject(derOctetString.getOctets());
          }
  // here I think, I should use derObject to retrieve cert info but I don't know how!?
  }
  public static DERObject toDERObject(byte[] data) throws IOException {
          ByteArrayInputStream inStream = new ByteArrayInputStream(data);
          ASN1InputStream DIS = new ASN1InputStream(inStream);
          return DIS.readObject();
      }
  </code></pre>