【问题标题】:Using Client SSL Certificate from .mobileconfig within Enterprise iOS app在企业 iOS 应用程序中使用来自 .mobileconfig 的客户端 SSL 证书
【发布时间】:2015-06-27 14:03:31
【问题描述】:

我们正在尝试在企业 iOS 应用中使用客户端 SSL 证书进行用户身份验证。

  • 我们可以在服务器端生成客户端ssl证书
  • 用户可以通过 .mobileconfig 安装它
  • Safari 中的 Web 服务器身份验证使用已安装的证书。
  • 从 iOS 应用程序内部发出 http 请求失败(未使用证书)。

我们如何让它发挥作用?谢谢!

【问题讨论】:

  • 您是否使用 https 的自签名证书保护您的网站和服务?或者您是否尝试使用证书作为身份验证手段而不是使用用户名和密码?
  • 我们想使用证书来验证用户身份(而不是使用用户名/密码)。
  • 您用于 Web 应用 NTLM 或 Kerberos 身份验证的现有解决方案是什么?

标签: ios authentication ssl ssl-certificate enterprise


【解决方案1】:

概述:

您已在设备钥匙串上安装了客户端 SSL 证书。

Safari.app 和 Mail.app 可以访问此钥匙串,而 iOS 应用没有。

原因是我们开发的应用是沙盒的,在非越狱设备上没有任何访问权限。

由于 safari 可以访问它,因此它可以毫无问题地连接和验证服务器质询。

解决方案:

将导出的 P12 文件包含在 App bundle 中,并参考它以找到服务器正在寻找的正确客户端证书。这实际上是一种解决方法。硬编码是获取 P12 文件的可靠方法。

实施:

有问题的方法是willSendRequestForAuthenticationChallenge in NSURLConenction delegate。您需要考虑NSURLAuthenticationMethodClientCertificate 质询类型才能处理服务器质询。这就是我们实现从嵌入式 P12 文件中提取正确证书身份的魔术的地方。代码如下

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] > 0) {
       //this will cause an authentication failure
       [[challenge sender] cancelAuthenticationChallenge:challenge];
       NSLog(@"Bad Username Or Password");        
       return;
    }



     //this is checking the server certificate
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            SecTrustResultType result;
            //This takes the serverTrust object and checkes it against your keychain
            SecTrustEvaluate(challenge.protectionSpace.serverTrust, &result);

            //if we want to ignore invalid server for certificates, we just accept the server
            if (kSPAllowInvalidServerCertificates) {
                [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
                return;
            } else if(result == kSecTrustResultProceed || result == kSecTrustResultConfirm ||  result == kSecTrustResultUnspecified) {
                //When testing this against a trusted server I got kSecTrustResultUnspecified every time. But the other two match the description of a trusted server
                [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
                return;
            }
        } else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodClientCertificate) {
        //this handles authenticating the client certificate

       /* 
 What we need to do here is get the certificate and an an identity so we can do this:
   NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:myCerts persistence:NSURLCredentialPersistencePermanent];
   [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

   It's easy to load the certificate using the code in -installCertificate
   It's more difficult to get the identity.
   We can get it from a .p12 file, but you need a passphrase:
   */

   NSString *p12Path = [[BundleManager bundleForCurrentSkin] pathForResource:kP12FileName ofType:@"p12"];
   NSData *p12Data = [[NSData alloc] initWithContentsOfFile:p12Path];

   CFStringRef password = CFSTR("PASSWORD");
   const void *keys[] = { kSecImportExportPassphrase };
   const void *values[] = { password };
   CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
   CFArrayRef p12Items;

   OSStatus result = SecPKCS12Import((CFDataRef)p12Data, optionsDictionary, &p12Items);

   if(result == noErr) {
             CFDictionaryRef identityDict = CFArrayGetValueAtIndex(p12Items, 0);
             SecIdentityRef identityApp =(SecIdentityRef)CFDictionaryGetValue(identityDict,kSecImportItemIdentity);

             SecCertificateRef certRef;
             SecIdentityCopyCertificate(identityApp, &certRef);

             SecCertificateRef certArray[1] = { certRef };
             CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, 1, NULL);
             CFRelease(certRef);

             NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identityApp certificates:(NSArray *)myCerts persistence:NSURLCredentialPersistencePermanent];
             CFRelease(myCerts);

             [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
         }
    } else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodDefault || [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM) {
   // For normal authentication based on username and password. This could be NTLM or Default.

        DAVCredentials *cred = _parentSession.credentials;
        NSURLCredential *credential = [NSURLCredential credentialWithUser:cred.username password:cred.password persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    } else {
        //If everything fails, we cancel the challenge.
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

参考: Ref1Ref2Ref3

希望对你有帮助

【讨论】:

  • @Christopher Stott - 试试这个并告诉我。如果您仍然遇到问题,请随时发表评论。
  • 我在其他地方读到有访问设备证书的私有方法。因为我正在构建一个通过企业商店分发的应用程序,所以我不在乎 App Store 被拒绝。
【解决方案2】:

我创建了一个包来使用 TLS 套接字/iOS/Obj-C。我将它连接到节点服务器,但它可能与其他人一起使用。更重要的是,鉴于最近的 iOS 13 TLS 限制,我有关于正确创建证书的重要资源的链接。我希望它可以有所帮助:

https://github.com/eamonwhiter73/IOSObjCWebSockets/tree/master

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    相关资源
    最近更新 更多