0

I need to implement authentication challenge on iOS 14, I was given a crt and a private key in form of a pem file.

I do know how to create a SecKey and SecCertificate out of it, but for the challenge I need an SecIdentity:

public init(identity: SecIdentity, certificates certArray: [Any]?, persistence: URLCredential.Persistence)

How to achieve this?

I also tried to generate a .p12 out of those two files, then import that via SecPKCS12Import to get identities and certificate from there, but it still doesn't work. Is there something special I need to consider when trying to do

URLCredential(identity: ..., certificates: [myCertificate], persistence: .none)

This is how I get certificate and identity I send to the server:

    var certificateRef: SecCertificate? = nil

    var items : CFArray?

    let securityError: OSStatus = SecPKCS12Import(decodedData, [kSecImportExportPassphrase as NSString : psswd] as CFDictionary, &items)

    let theArray: CFArray = items!
    if securityError == noErr && CFArrayGetCount(theArray) > 0 {
        let newArray = theArray as [AnyObject] as NSArray
        let dictionary = newArray.object(at: 0)
        let secIdentity = (dictionary as AnyObject)[kSecImportItemIdentity as String] as! SecIdentity
        let securityError = SecIdentityCopyCertificate(secIdentity , &certificateRef)
        if securityError != noErr {
            certificateRef = nil
        }
        return secIdentity
    }
swalkner
  • 16,679
  • 31
  • 123
  • 210
  • See https://stackoverflow.com/questions/26458540/how-to-obtain-secidentityref-from-seccertificateref-and-privatekey – dgatwood May 17 '21 at 21:06
  • Does this answer your question? [how to obtain SecIdentityRef from SecCertificateRef and PrivateKey?](https://stackoverflow.com/questions/26458540/how-to-obtain-secidentityref-from-seccertificateref-and-privatekey) – dgatwood May 17 '21 at 21:07

0 Answers0