Sample code for 30+ languages & platforms
React Native

Get Certificates within XML Signature

See more XML Digital Signatures Examples

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

Chilkat React Native Downloads

React Native
import { Cert, StringArray, StringBuilder, XmlDSig } from '@chilkat/react-native'

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

  const 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 {
    console.log('Failed to load the XML file..');
    return;
  }

  const 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 {
    console.log(dsig.lastErrorText);
    return;
  }

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

  console.log(`numSignatures = ${dsig.numSignatures}`);

  while (i < dsig.numSignatures) {
    // Select the Nth signature by setting the Selector property.
    dsig.selector = i;

    const bVerifyReferenceDigests = true;
    let bVerified = true;
    try {
      dsig.verifySignature(bVerifyReferenceDigests);
    } catch {
      bVerified = false;
    }
    console.log(`Signature ${i + 1} verified = ${bVerified}`);

    // Get the certificates embedded in this signature.
    saCerts.clear();
    try {
      dsig.getCerts(saCerts);
      let j = 0;
      while (j < saCerts.count) {
        try {
          cert.loadFromBase64(saCerts.getString(j));
          console.log(`    ${cert.subjectDN}`);
        } catch {
          // cert.loadFromBase64() failed; continue anyway.
        }

        j++;
      }
    } catch {
      // dsig.getCerts() failed; continue anyway.
    }

    i++;
  }
}