【问题标题】:Cannot convert value of type 'SecCertificate' to expected argument type 'UnsafeMutablePointer<UnsafeRawPointer?>!'无法将“SecCertificate”类型的值转换为预期的参数类型“UnsafeMutablePointer<UnsafeRawPointer?>!”
【发布时间】:2018-05-23 08:08:47
【问题描述】:

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

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

  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);
    }
}

【问题讨论】:

    标签: ios swift xcode nsurlsession unsafe-pointers


    【解决方案1】:

    导致错误的行是用于创建CFArray

    创建CFArray 的一种简单方法是将Swift Array 桥接到CFArray

    换行试试:

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

    收件人:

    let certArrayRef = [cert] as CFArray
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 2016-07-02
      相关资源
      最近更新 更多