在Enterprise iOS app中使用来自.mobileconfig的客户端SSL证书

时间:2015-04-20 17:43:12

标签: ios authentication ssl ssl-certificate enterprise

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

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

我们如何让这个工作?谢谢!

2 个答案:

答案 0 :(得分:8)

<强> 概述:

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

Safari.app和Mail.app可以访问此钥匙串,而iOS应用则无法访问。

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

由于safari可以访问它,因此在连接和验证服务器质询时没有任何问题。

<强> 解决方案:

将导出的P12文件包含在应用程序包中并引用它以查找服务器正在查找的正确客户端证书。这实际上是一种解决方法。硬编码是获取P12文件的可靠方法。

<强> 实现:

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

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] &gt; 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, &amp;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, &amp;p12Items);

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

             SecCertificateRef certRef;
             SecIdentityCopyCertificate(identityApp, &amp;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

希望这有帮助

答案 1 :(得分:0)

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

check in the source code of the driver