Swift 2.0和XCode 7中的服务器身份验证已损坏

是我

我刚刚将代码更新为Swift 2.0,以使用Xcode7。我的应用程序执行NSURLAuthenticationMethodServerTrust并进行NSURLAuthenticationMethodClientCertificate身份验证。

问题是NSURLAuthenticationMethodServerTrust身份验证在我的模拟器上停止工作-但仍可以在装有iOS 8.3的测试设备上工作。除了我以前的不是Swift 2.0的项目,它仍然可以正常工作。

错误: NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

从NSURLSession检索到错误:

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fcf75053070 {NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fcf73700d00>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorCodeKey=-9802, NSUnderlyingError=0x7fcf735284b0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://mywebapi/dosomething, NSErrorFailingURLStringKey=https://mywebapi/dosomething, _kCFStreamErrorDomainKey=3} [GetOneTimeTokenController.swift:76]

我仍将iOS 8.0部署为目标。

这是我处理身份验证挑战的方法(使用自签名证书):

if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate {

        let urlCredential:NSURLCredential = NSURLCredential(
            identity: identityAndTrust.identityRef,
            certificates: identityAndTrust.certArray as [AnyObject],
            persistence: NSURLCredentialPersistence.ForSession);

        completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, urlCredential);

    } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {

        completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(trust: challenge.protectionSpace.serverTrust!));

    } else {

        challenge.sender?.continueWithoutCredentialForAuthenticationChallenge(challenge)
        Logger.sharedInstance.logMessage("Unexpected Authentication Challange", .Error);

    }
卡洛斯

您没有绕过TLS层。请在下面找到使用自签名证书对我有用的答案。

以下是与自签名SSL证书配合使用的代码更改

  func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {

    if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {


    let serverTrust:SecTrustRef = challenge.protectionSpace.serverTrust!
    let certificate: SecCertificateRef = SecTrustGetCertificateAtIndex(serverTrust, 0)!
    let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
    let cerPath: String = NSBundle.mainBundle().pathForResource("xyz.com", ofType: "cer")!
    let localCertificateData = NSData(contentsOfFile:cerPath)!


        if (remoteCertificateData.isEqualToData(localCertificateData) == true) {
            let credential:NSURLCredential = NSURLCredential(forTrust: serverTrust)

            challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)


            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))

        } else {

            completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
        }
    }
    else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate
    {

        let path: String = NSBundle.mainBundle().pathForResource("client", ofType: "p12")!
        let PKCS12Data = NSData(contentsOfFile:path)!


        let identityAndTrust:IdentityAndTrust = self.extractIdentity(PKCS12Data);



            let urlCredential:NSURLCredential = NSURLCredential(
                identity: identityAndTrust.identityRef,
                certificates: identityAndTrust.certArray as? [AnyObject],
                persistence: NSURLCredentialPersistence.ForSession);

            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, urlCredential);




    }
    else
    {
        completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil);
    }
}

 struct IdentityAndTrust {

    var identityRef:SecIdentityRef
    var trust:SecTrustRef
    var certArray:AnyObject
}

func extractIdentity(certData:NSData) -> IdentityAndTrust {
    var identityAndTrust:IdentityAndTrust!
    var securityError:OSStatus = errSecSuccess

    let path: String = NSBundle.mainBundle().pathForResource("client", ofType: "p12")!
    let PKCS12Data = NSData(contentsOfFile:path)!
    let key : NSString = kSecImportExportPassphrase as NSString
    let options : NSDictionary = [key : "xyz"]
    //create variable for holding security information
    //var privateKeyRef: SecKeyRef? = nil

    var items : CFArray?

     securityError = SecPKCS12Import(PKCS12Data, options, &items)

    if securityError == errSecSuccess {
        let certItems:CFArray = items as CFArray!;
        let certItemsArray:Array = certItems as Array
        let dict:AnyObject? = certItemsArray.first;
        if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {

            // grab the identity
            let identityPointer:AnyObject? = certEntry["identity"];
            let secIdentityRef:SecIdentityRef = identityPointer as! SecIdentityRef!;
            print("\(identityPointer)  :::: \(secIdentityRef)")
            // grab the trust
            let trustPointer:AnyObject? = certEntry["trust"];
            let trustRef:SecTrustRef = trustPointer as! SecTrustRef;
            print("\(trustPointer)  :::: \(trustRef)")
            // grab the cert
            let chainPointer:AnyObject? = certEntry["chain"];
            identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray:  chainPointer!);
        }
    }
    return identityAndTrust;
}

在info.plist文件中完成的更改

     <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>amazonaws.com.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
        </dict>
        <key>amazonaws.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
        </dict>
        <key>xyz.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
</dict>
</plist>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PromiseKit无法使用Swift 2和Xcode 7进行编译

来自分类Dev

Web服务器上的身份验证和授权?

来自分类Dev

AWS Multifactor身份验证和闪亮服务器

来自分类Dev

使用Xcode 7和Swift 2编写项目时升级到Xcode 7.1是否安全?

来自分类Dev

WSO2身份服务器身份验证管理员API身份验证失败

来自分类Dev

WSO2身份服务器身份验证管理员API身份验证失败

来自分类Dev

与NodeJS后端服务器的AngularJS服务器会话和身份验证

来自分类Dev

XCode 7和Swift的困惑

来自分类Dev

在令牌验证期间,在 spring security oauth2 中从资源服务器发送到身份验证服务器的确切内容

来自分类Dev

Ionic 2登录组件和身份验证服务

来自分类Dev

代理后面的WSO2身份服务器X509身份验证

来自分类Dev

尝试在Swift中使用Json令牌从服务器获取身份验证

来自分类Dev

使用Swift 2 Xcode 7更改视图

来自分类Dev

Xcode 7 Swift 2后台页面加载

来自分类Dev

带有Xcode 7,Swift 2的TouchID

来自分类Dev

无法使SWRevealViewController工作(Swift 2,Xcode 7)

来自分类Dev

在身份服务器中使用cookie和令牌进行共享身份验证

来自分类Dev

Swift和XCode 7 Beta 2未显示正在播放的歌曲的元数据

来自分类Dev

使用Swift 2和PHP将JSON编码为Xcode 7

来自分类Dev

在Spring Boot OAuth2授权服务器中使用Active Directory身份验证

来自分类Dev

可以将ADFS作为OAuth2提供程序/身份验证服务器吗?

来自分类Dev

Docker容器上的解析服务器的自定义身份验证(OAuth2)

来自分类Dev

在iOS中使用NSURLSessions对服务器API进行oAuth2身份验证

来自分类Dev

使用ServiceStack的OWIN OAuth2资源服务器身份验证

来自分类Dev

Angular2:对节点服务器的 PUT 请求不起作用 - 身份验证丢失

来自分类Dev

Firebase / Swift 2-如何获取经过身份验证的用户密码和电子邮件

来自分类Dev

Symfony 2 Swiftmailer + Amazon SES:无法使用2个可能的身份验证器在用户名“ XXXXXXXXXXXXXXXX”的SMTP服务器上进行身份验证

来自分类Dev

使用OAuth 2.0和Google API进行服务器到服务器身份验证的示例

来自分类Dev

使用OAuth 2.0和Google API进行服务器到服务器身份验证的示例

Related 相关文章

  1. 1

    PromiseKit无法使用Swift 2和Xcode 7进行编译

  2. 2

    Web服务器上的身份验证和授权?

  3. 3

    AWS Multifactor身份验证和闪亮服务器

  4. 4

    使用Xcode 7和Swift 2编写项目时升级到Xcode 7.1是否安全?

  5. 5

    WSO2身份服务器身份验证管理员API身份验证失败

  6. 6

    WSO2身份服务器身份验证管理员API身份验证失败

  7. 7

    与NodeJS后端服务器的AngularJS服务器会话和身份验证

  8. 8

    XCode 7和Swift的困惑

  9. 9

    在令牌验证期间,在 spring security oauth2 中从资源服务器发送到身份验证服务器的确切内容

  10. 10

    Ionic 2登录组件和身份验证服务

  11. 11

    代理后面的WSO2身份服务器X509身份验证

  12. 12

    尝试在Swift中使用Json令牌从服务器获取身份验证

  13. 13

    使用Swift 2 Xcode 7更改视图

  14. 14

    Xcode 7 Swift 2后台页面加载

  15. 15

    带有Xcode 7,Swift 2的TouchID

  16. 16

    无法使SWRevealViewController工作(Swift 2,Xcode 7)

  17. 17

    在身份服务器中使用cookie和令牌进行共享身份验证

  18. 18

    Swift和XCode 7 Beta 2未显示正在播放的歌曲的元数据

  19. 19

    使用Swift 2和PHP将JSON编码为Xcode 7

  20. 20

    在Spring Boot OAuth2授权服务器中使用Active Directory身份验证

  21. 21

    可以将ADFS作为OAuth2提供程序/身份验证服务器吗?

  22. 22

    Docker容器上的解析服务器的自定义身份验证(OAuth2)

  23. 23

    在iOS中使用NSURLSessions对服务器API进行oAuth2身份验证

  24. 24

    使用ServiceStack的OWIN OAuth2资源服务器身份验证

  25. 25

    Angular2:对节点服务器的 PUT 请求不起作用 - 身份验证丢失

  26. 26

    Firebase / Swift 2-如何获取经过身份验证的用户密码和电子邮件

  27. 27

    Symfony 2 Swiftmailer + Amazon SES:无法使用2个可能的身份验证器在用户名“ XXXXXXXXXXXXXXXX”的SMTP服务器上进行身份验证

  28. 28

    使用OAuth 2.0和Google API进行服务器到服务器身份验证的示例

  29. 29

    使用OAuth 2.0和Google API进行服务器到服务器身份验证的示例

热门标签

归档