Hi!
With CryptoKit functions i can instantiate certificate keys, but with usage of Security module the code fails. I cannot figure out what is the issue.
Anyone have any idea what needs to be changed?
Thanks!!!, Garfield
This is the code that causes issue:
import Security
import CryptoKit
// base64 encoded
let privateKeyString = "deXnwQoRHddejAMvHsucXdPkE0B5hAIap1VE69ASplo="
let publicKeyString = "p4Rqbh+nmv/FslbJFGOe7JzSP6/ySaTkgW6h5t/+fqC/E8M9SVmamBKdTusddEecWY5KBGZo4x2oeNYyM7EtPA=="
let attributesPrivate: [NSObject : NSObject] = [
kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass: kSecAttrKeyClassPrivate
]
let attributesPublic: [NSObject : NSObject] = [
kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass: kSecAttrKeyClassPublic
]
var errorPrivate: Unmanaged<CFError>?
var errorPublic: Unmanaged<CFError>?
var error: Unmanaged<CFError>?
if #available(iOS 13.0, *) {
let publicKeyX = try! P256.Signing.PublicKey(rawRepresentation: Data(base64Encoded: publicKeyString)!.suffix(65))
//https://developer.apple.com/forums/thread/680554
print(publicKeyX, "P256 PUBLIC")
let privateKeyX = try! P256.Signing.PrivateKey(rawRepresentation: Data(base64Encoded: privateKeyString)!.suffix(65))
print(privateKeyX, "P256 PRIVATE")
} else {
// Fallback on earlier versions
}
let publicKey = SecKeyCreateWithData(Data(base64Encoded: publicKeyString)! as NSData, attributesPublic as NSDictionary, &errorPublic)!
let privateKey = SecKeyCreateWithData(Data(base64Encoded: privateKeyString)! as NSData, attributesPrivate as NSDictionary, &errorPrivate)!