title: Getting certificate from XMLSignature in Java
body: <p>I'm trying to get the certificate out of XMLSignature, get it's CRL DistributionPoint and verify if it's valid.</p>
  
  <p>I have a digital document and signature file name, and that's how I get XMLSignature:</p>
  
  <pre><code>ZipFile zipFile = new ZipFile(dataFactory.getDataReader().getFileAdoc(adocFileName));
  ZipEntry entry = zipFile.getEntry(signatureFileName);
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setNamespaceAware(true);
  Document doc = dbf.newDocumentBuilder().parse(zipFile.getInputStream(entry));
  NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
  if (nl.getLength() == 0)
  {
      throw new Exception("Cannot find Signature element");
  }
  XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
  DOMValidateContext valContext = new DOMValidateContext(new X509KeySelector(), nl.item(0));
  ZipFileURIDereferencer dereferencer = new ZipFileURIDereferencer(zipFile);
  valContext.setURIDereferencer(dereferencer);
  
  XMLSignature signature = fac.unmarshalXMLSignature(valContext);
  </code></pre>
  
  <p>Now, how do I get Certificate or X509Certificate?</p>
  
  <p>I have tried getting &lt; X509Certificate > part:</p>
  
  <pre><code>NodeList sertificateNodeList = doc.getElementsByTagName("X509Certificate");
  if (sertificateNodeList.getLength() == 0) {
      throw new Exception("Cannot find X509Certificate element");
  }
  String certPart = sertificateNodeList.item(0).getFirstChild().getNodeValue();
  System.out.println(certPart);
  InputStream is = new ByteArrayInputStream(certPart.getBytes());
  
  CertificateFactory cf = CertificateFactory.getInstance("X.509");
  Certificate cert = cf.generateCertificate(is);
  </code></pre>
  
  <p>But that gives me:</p>
  
  <pre>java.security.cert.CertificateParsingException: invalid DER-encoded certificate data</pre>
  
  <p>Maybe I just need to somehow encode that InputStream is?</p>
  
  <p>The signature.xml contains:</p>
  
  <pre><code>&lt;X509Certificate&gt;
  MIIKVTCCCT2gAwIBAgIOY7W3f/J6VnsAAQAInYYwDQYJKoZIhvcNAQEFBQAwgbsxCzAJBgNVBAYT
  AkxUMUAwPgYDVQQKEzdHeXZlbnRvanUgcmVnaXN0cm8gdGFybnliYSBwcmllIExSIFZSTSAtIGku
  ...
  FWxieiI3KtGsVPYZ1/C7QHLv0SRMaCm/+qHuPSWh+L5YIcjBxQbD4bU2Q9soW7QshkRNRJOWSonK
  Rw/cD4gWZDPte3V42qj6SZazsjDrGTFaGBg3
  &lt;/X509Certificate&gt;
  </code></pre>
  
  <p>Thanks!</p>