gpt4 book ai didi

ios - 无法将类型 'SecCertificate' 的值转换为预期的参数类型 'UnsafeMutablePointer!'

转载 作者:行者123 更新时间:2023-11-28 14:42:07 25 4
gpt4 key购买 nike

我正在尝试执行 https 请求,但是当我尝试匹配本地和远程 ssl 证书时,Xcode 向我显示了上述错误。我在下面附上了一张错误图片和代码,如果有任何帮助,我们将不胜感激。

enter image description here

下面是URLSessionDelegate的完整委托(delegate)方法。

  func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {


let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
let certificate: SecCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
let cerPath: String = Bundle.main.path(forResource: "xxxxx", ofType: "der")!
let localCertificateData = NSData(contentsOfFile:cerPath)!
print(localCertificateData.length)
let result = remoteCertificateData.isEqual(localCertificateData as Data)
let certDataRef = localCertificateData as! CFData
var cert: SecCertificate = SecCertificateCreateWithData(nil, certDataRef)!
let certArrayRef = CFArrayCreate(nil, cert, 1, nil)
SecTrustSetAnchorCertificates(serverTrust, certArrayRef)

SecTrustSetAnchorCertificatesOnly(serverTrust, false)
print(result)
var trustResult: SecTrustResultType = SecTrustResultType.invalid
SecTrustEvaluate(serverTrust, &trustResult)
if (trustResult == SecTrustResultType.unspecified ||
trustResult == SecTrustResultType.proceed)
{
let credential:URLCredential = URLCredential(trust: serverTrust)
challenge.sender?.use(credential, for: challenge)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))

} else {

completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
}
}
else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate
{
let path: String = Bundle.main.path(forResource: "client", ofType: "p12")!
let PKCS12Data = NSData(contentsOfFile:path)!
let identityAndTrust:IdentityAndTrust = self.extractIdentity(certData: PKCS12Data);
let urlCredential:URLCredential = URLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as? [AnyObject],
persistence: URLCredential.Persistence.forSession);
completionHandler(URLSession.AuthChallengeDisposition.useCredential, urlCredential);
}
else
{
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil);
}
}

最佳答案

导致错误的行用于创建 CFArray

创建 CFArray 的一种简单方法是使用桥接 Swift ArrayCFArray

尝试更改行:

let certArrayRef = CFArrayCreate(nil, cert, 1, nil)

收件人:

let certArrayRef = [cert] as CFArray

关于ios - 无法将类型 'SecCertificate' 的值转换为预期的参数类型 'UnsafeMutablePointer<UnsafeRawPointer?>!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50483084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com