Java
Chilkat2 Java
Examine KeyInfo Certificate in XML Signature
See more XML Digital Signatures Examples
This example loads signed XML and gets the signing certificate, assuming the certificate is contained in X509Certificate within the KeyInfo.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
import com.chilkatsoft.chilkat2.XmlDSig;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
XmlDSig dsig = new XmlDSig();
StringBuilder sbXml = new StringBuilder();
try {
sbXml.loadFile("c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml", "utf-8");
} catch (ChilkatException e) {
System.out.println("Failed to load XML file.");
return;
}
Xml xmlKeyInfo;
try {
dsig.loadSignatureSb(sbXml);
// Get the KeyInfo XML.
xmlKeyInfo = dsig.getKeyInfo();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(xmlKeyInfo.getXml());
System.out.println("----");
// Assuming the X509Certificate is in the KeyInfo, it will look like this:
// <ds:KeyInfo Id="...">
// <ds:KeyValue>
// ...
// <ds:X509Data>
// <ds:X509Certificate>MIIHAz...</ds:X509Certificate>
// </ds:X509Data>
// </ds:KeyInfo>
String certBase64 = xmlKeyInfo.getChildContent("*:X509Data|*:X509Certificate");
if (certBase64 == null) {
System.out.println("No X509Certificate found in the KeyInfo.");
return;
}
// Load a certificate object w/ the base64.
Cert cert = new Cert();
try {
cert.loadFromBase64(certBase64);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Examine the cert..
System.out.println("SubjectDN: " + cert.getSubjectDN());
System.out.println("IssuerDN: " + cert.getIssuerDN());
System.out.println("SerialNumber as Decimal: " + cert.getSerialDecimal());
xmlKeyInfo.close();
}
}