'Generate X.509 certificate (and public/private key) in Swift iOS
I want to generate a public/private key along with a certificate. I want to send this certificate to the server for future validation. I am able to generate the public and private key using SecKeyCreateRandomKey and SecKeyCopyPublicKey. I have also created an external representation of the key using SecKeyCopyExternalRepresentation, which is in DER format. I am stuck on how to create a self signed X.509 certificate from this.
Something similar to this on Android: keyStore.getCertificate()
let tag = "com.example.keys.test".data(using: .utf8)!
let attributes: [String: Any] =
[kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits as String: 2048,
kSecPrivateKeyAttrs as String:
[kSecAttrIsPermanent as String: true,
kSecAttrApplicationTag as String: tag]
]
var error: Unmanaged<CFError>?
let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error)
let publicKey = SecKeyCopyPublicKey(privateKey!)
let publicKeyData = SecKeyCopyExternalRepresentation(publicKey!, &error)! as Data
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
