Sample code for 30+ languages & platforms
Swift

Get Certificates within XML Signature

See more XML Digital Signatures Examples

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

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let sbXml = CkoStringBuilder()!

    // Load XML containing one or more signatures.
    success = sbXml.loadFile(path: "qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml", charset: "utf-8")
    if success == false {
        print("Failed to load the XML file..")
        return
    }

    let dsig = CkoXmlDSig()!

    // 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.
    success = dsig.loadSignatureSb(sbXmlSig: sbXml)
    if success != true {
        print("\(dsig.lastErrorText!)")
        return
    }

    // For each signature, verify and also get the certificate(s) contained within each Signature.
    var i: Int = 0
    let saCerts = CkoStringArray()!
    let cert = CkoCert()!

    print("numSignatures = \(dsig.numSignatures.intValue)")

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

        var bVerifyReferenceDigests: Bool = true
        var bVerified: Bool = dsig.verifySignature(verifyReferenceDigests: bVerifyReferenceDigests)
        print("Signature \(i + 1) verified = \(bVerified)")

        // Get the certificates embedded in this signature.
        saCerts.clear()
        success = dsig.getCerts(sa: saCerts)
        if success == true {
            var j: Int = 0
            while j < saCerts.count.intValue {
                success = cert.load(fromBase64: saCerts.getString(index: j))
                if success == true {
                    print("    \(cert.subjectDN!)")
                }

                j = j + 1
            }

        }

        i = i + 1
    }


}