Skip to content

Instantly share code, notes, and snippets.

@IhorYachmenov
Last active October 8, 2020 11:01
Show Gist options
  • Save IhorYachmenov/3de282a602b72edc930c378b7ebc26bc to your computer and use it in GitHub Desktop.
Save IhorYachmenov/3de282a602b72edc930c378b7ebc26bc to your computer and use it in GitHub Desktop.
StackOverFlow Example
func StackOverFrowReadPCKS12() {
if let url = Bundle.main.url(forResource: "client", withExtension: "p12") {
let PKCS12Data = NSData(contentsOf: url)
let inPKCS12Data = CFDataCreate(kCFAllocatorDefault, PKCS12Data!.bytes.assumingMemoryBound(to: UInt8.self), (PKCS12Data?.length)!)
let keys: [CFString] = [kSecImportExportPassphrase]
let values: [CFTypeRef] = []
let keysPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: keys.count)
keysPointer.initialize(to: keys)
let valuesPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: values.count)
valuesPointer.initialize(to: values)
var items = CFArrayCreate(kCFAllocatorDefault, UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 0), 0, nil)
let securityError = SecPKCS12Import(inPKCS12Data!,[
kSecImportExportPassphrase: "******"
] as CFDictionary, &items)
if (securityError == 0) {
print("Certificate installed Successfully")
} else {
print("Certificate installation failed")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment