… what is supposed to do, is encrypt text with imported certificate as .pem.
I’m not familiar with
RSAUtils.swift(it’s not an Apple thing AFAICT) but it seems likely that these tags values relate to the keychain (probably used by
kSecAttrApplicationTag). Which is weird because you don’t need to mess with the keychain in order to encrypt data with a certificate. What you need to do is:
Convert the certificate from PEM format into DER format
Create a certificate object (SecCertificate) from that
Extract the public key from that
Encrypt using that public key
I’ll break down each step in turn.
The difficulty of step 1 depends on the source of your certificate. If you’re dealing with one fixed certificate, you can just do the conversion on your Mac (using Keychain Access) and then add the
.cerfile to your app’s bundle. OTOH, if you have to deal with some arbitrary PEM, you’ll have to write (or acquire) a PEM parser.
To create a certificate object from DER data, call
To extract a public key from a certificate:
Create a trust object using
Evaluate that trust object using
SecTrustEvaluate(you can ignore the result, but step 3 won’t work unless you’ve done an evaluation)
Get the public key via
To see how to encrypt using a public key, take a look at the CryptoCompatibility sample code.
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"