Sample code for 30+ languages & platforms
Swift

Load Certificate from PFX (PKCS#12)

See more Certificates Examples

Loads a digital certificate (and private key, if available) from a PFX file.(also known as PKCS#12)

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let cert = CkoCert()!

    // Load from the PFX file
    var pfxFilename: String? = "/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx"
    var pfxPassword: String? = "test"

    // A PFX typically contains certificates in the chain of authentication.
    // The Chilkat cert object will choose the certificate w/
    // private key farthest from the root authority cert.
    // To access all the certificates in a PFX, use the 
    // Chilkat certificate store object instead.
    success = cert.loadPfxFile(path: pfxFilename, password: pfxPassword)
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    // Get some information about the digital certificate, 
    // then get the private key...

    // DN = "Distinguished Name"
    print("SubjectDN:\(cert.subjectDN!)")

    print("Common Name:\(cert.subjectCN!)")
    print("Issuer Common Name:\(cert.issuerCN!)")

    print("Serial Number:\(cert.serialNumber!)")

    let privKey = CkoPrivateKey()!
    success = cert.getPrivateKey(privKey: privKey)
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    // The private key object may be used in any Chilkat methods
    // (in other objects/classes) that expect a private key argument.

    // In this case, save the private key to a PKCS8 Encrypted PEM format file:
    var pemPassword: String? = "secret"
    var pemPath: String? = "/Users/chilkat/testData/pem/chilkat_privKey.pem"
    success = privKey.savePkcs8EncryptedPemFile(password: pemPassword, path: pemPath)
    if success == false {
        print("\(privKey.lastErrorText!)")
        return
    }

    print("Private key saved to PKCS8 Encrypted PEM...")

}