Sample code for 30+ languages & platforms
Java

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringArray;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.XmlDSig;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        StringBuilder sbXml = new StringBuilder();

        // Load XML containing one or more signatures.
        try {
            sbXml.loadFile("qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml", "utf-8");
        } catch (ChilkatException e) {
            System.out.println("Failed to load the XML file..");
            return;
        }

        XmlDSig dsig = new XmlDSig();

        // First load the XML containing the signatures to be verified.
        // Note that this particular Signature already contains the RSA public key that will be used
        // for verification.
        try {
            dsig.loadSignatureSb(sbXml);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // For each signature, verify and also get the certificate(s) contained within each Signature.
        int i = 0;
        StringArray saCerts = new StringArray();
        Cert cert = new Cert();

        System.out.println("numSignatures = " + dsig.getNumSignatures());

        while (i < dsig.getNumSignatures()) {
            // Select the Nth signature by setting the Selector property.
            dsig.setSelector(i);

            boolean bVerifyReferenceDigests = true;
            boolean bVerified;
            try {
                dsig.verifySignature(bVerifyReferenceDigests);
                bVerified = true;
            } catch (ChilkatException e) {
                bVerified = false;
            }
            System.out.println("Signature " + (i + 1) + " verified = " + bVerified);

            // Get the certificates embedded in this signature.
            saCerts.clear();
            try {
                dsig.getCerts(saCerts);
                success = true;
            } catch (ChilkatException e) {
                success = false;
            }
            if (success) {
                int j = 0;
                while (j < saCerts.getCount()) {
                    try {
                        cert.loadFromBase64(saCerts.getString(j));
                        success = true;
                    } catch (ChilkatException e) {
                        success = false;
                    }
                    if (success) {
                        System.out.println("    " + cert.getSubjectDN());
                    }

                    j = j + 1;
                }
            }

            i = i + 1;
        }
    }
}