Sample code for 30+ languages & platforms
Swift

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 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 dsig = CkoXmlDSig()!
    let sbXml = CkoStringBuilder()!

    success = sbXml.loadFile(path: "c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml", charset: "utf-8")
    if success != true {
        print("Failed to load XML file.")
        return
    }

    success = dsig.loadSignatureSb(sbXmlSig: sbXml)
    if success != true {
        print("\(dsig.lastErrorText!)")
        return
    }

    // Get the KeyInfo XML.
    var xmlKeyInfo: CkoXml? = dsig.getKeyInfo()
    if dsig.lastMethodSuccess != true {
        print("\(dsig.lastErrorText!)")
        return
    }

    print("\(xmlKeyInfo!.getXml()!)")
    print("----")

    // 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>
    var certBase64: String? = xmlKeyInfo!.getChildContent(tagPath: "*:X509Data|*:X509Certificate")
    if xmlKeyInfo!.lastMethodSuccess != true {
        print("No X509Certificate found in the KeyInfo.")
        return
    }

    // Load a certificate object w/ the base64.
    let cert = CkoCert()!
    success = cert.load(fromBase64: certBase64)
    if success != true {
        print("\(cert.lastErrorText!)")
        return
    }

    // Examine the cert..
    print("SubjectDN: \(cert.subjectDN!)")
    print("IssuerDN: \(cert.issuerDN!)")
    print("SerialNumber as Decimal: \(cert.serialDecimal!)")

    xmlKeyInfo = nil

}