Sample code for 30+ languages & platforms
Dart

Get Certificates within XML Signature

See more XML Digital Signatures Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final sbXml = CkStringBuilder();

  // Load XML containing one or more signatures.
  try {
    sbXml.loadFile('qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml', 'utf-8');
  } on ChilkatException {
    print('Failed to load the XML file..');
    return;
  }

  final dsig = CkXmlDSig();

  // 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);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // For each signature, verify and also get the certificate(s) contained within each Signature.
  var i = 0;
  final saCerts = CkStringArray();
  final cert = CkCert();

  print('numSignatures = ${dsig.numSignatures}');

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

    final bVerifyReferenceDigests = true;
    var bVerified = true;
    try {
      dsig.verifySignature(bVerifyReferenceDigests);
    } on ChilkatException {
      bVerified = false;
    }
    print('Signature ${i + 1} verified = $bVerified');

    // Get the certificates embedded in this signature.
    saCerts.clear();
    try {
      dsig.getCerts(saCerts);
      var j = 0;
      while (j < saCerts.count) {
        try {
          cert.loadFromBase64(saCerts.getString(j));
          print('    ${cert.subjectDN}');
        } on ChilkatException {
          // cert.loadFromBase64() failed; continue anyway.
        }

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

    i++;
  }
}