Hi I’m making an attempt to create identification however I get error – Can not discover ‘SecIdentityCreateWithCertificate’ in scope whereas calling loadIdentity operate.
enter picture description right here
I’m not certain what I’m doing fallacious right here. I must move identification in https request.
I’m utilizing Xcode 15.3 and macOS Sonoma 14.5
import Basis
import Safety
import UIKit
class URLSessionPinningDelegate: NSObject, URLSessionDelegate {
var identification: SecIdentity
init(identification: SecIdentity) {
self.identification = identification
}
func urlSession(_ session: URLSession, didReceive problem: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let credential = URLCredential(identification: self.identification, certificates: nil, persistence: .forSession)
completionHandler(.useCredential, credential)
}
}
func loadIdentity(certPath: String, keyPath: String) -> SecIdentity? {
guard let certData = strive? Information(contentsOf: URL(fileURLWithPath: certPath)) else {
print("Unable to load certificates")
return nil
}
guard let cert = SecCertificateCreateWithData(nil, certData as CFData) else {
print("Unable to create certificates")
return nil
}
guard let keyData = strive? Information(contentsOf: URL(fileURLWithPath: keyPath)) else {
print("Unable to load non-public key")
return nil
}
let keyDict: [NSString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits: 2048,
kSecReturnPersistentRef: true
]
var error: Unmanaged?
guard let privateKey = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, &error) else {
print("Unable to create non-public key")
return nil
}
var identification: SecIdentity?
let standing = SecIdentityCreateWithCertificate(cert, privateKey, &identification)
guard standing == errSecSuccess else {
print("Unable to create identification")
return nil
}
return identification
}
@objc(AzureProvisionWithCertificate)
class AzureProvisionWithCertificate: NSObject { @objc(provisionAndUploadFile:withRegistrationId:withKey:withCertificate:withProvisionHost:withFileNameWithFolder:withModelId:withResolver:withRejecter:)
func provisionAndUploadFile(scopeId:String, registrationId:String, key:String, certificates:String, provisionHost:String, fileNameWithFolder:String, modelId:String, resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
let certPath = "/path/to/your/device-cert.pem"
let keyPath = "/path/to/your/device-key.pem"
guard let identification = loadIdentity(certPath: certificates, keyPath: key) else {
print("Unable to load identification")
return
}
let session = URLSession(configuration: .default, delegate: URLSessionPinningDelegate(identification: identification), delegateQueue: nil)
guard let url = URL(string: "https://international.azure-devices-provisioning.web/[scopeId]/registrations/[registrationId]/register?api-version=2021-06-01") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
//Https request code right here
}
}