Three things…
What version of Swift are you using here? Judging by your use of NSBundle (rather than
Bundle
) I presume you’re using Swift 2 of some form. Is that right?
You don’t need to create your own CFData here; Swift will automatically go from NSData to CFData like this:
let cert = SecCertificateCreateWithData(nil, data)
If you’re using
Data
(in Swift 3) you’ll need to bounce to NSData.
let cert = SecCertificateCreateWithData(nil, data as NSData)
The most common reason for
SecCertificateCreateWithData
to return nil is that the data isn’t a valid certificate. A common problem is that folks try to pass in a PEM format certificate, whereas
SecCertificateCreateWithData
requires DER. If you open the certificate with a text editor, do you see Base64? Or do you see binary goo? If you see Base64, you’ll need to convert the certificate to binary (DER) form before passing to to
SecCertificateCreateWithData
. For a single certificate that you include in your bundle, you can just pre-convert it using
Keychain Access on the Mac.
OTOH, if it’s already in binary form, there’s something broken within the certificate itself. If you post a hex dump of that binary data I can take a look.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"