在Enterprise iOS应用程序中使用.mobileconfig中的客户端SSL证书

前端之家收集整理的这篇文章主要介绍了在Enterprise iOS应用程序中使用.mobileconfig中的客户端SSL证书前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们试图在企业iOS应用程序中使用客户端SSL证书进行用户身份验证.

>我们可以在服务器上生成客户端ssl证书
>用户可以通过.mobileconfig安装
> Safari中的Web服务器的认证与安装的证书一起使用.
>从iOS应用程序内部发出http请求失败(不使用证书).

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

解决方法

概述:

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

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

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

随着safari访问它,连接和认证服务器的挑战没有任何麻烦.

解:

将导出的P12文件与App捆绑在一起,并引用它来查找服务器正在查找的正确的客户端证书.它实际上是一种解决方法.硬编码是获取P12文件的可靠方法.

执行:

在NSURLConenction委托中的方法是willSendRequestForAuthenticationChallenge.您需要考虑NSURLAuthenticationMethodClientCertificate挑战类型,以处理服务器挑战.这是我们实现了从嵌入式P12文件提取正确的证书身份的魔力的地方.代码如下

  1. - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  2. if ([challenge prevIoUsFailureCount] > 0) {
  3. //this will cause an authentication failure
  4. [[challenge sender] cancelAuthenticationChallenge:challenge];
  5. NSLog(@"Bad Username Or Password");
  6. return;
  7. }
  8.  
  9.  
  10.  
  11. //this is checking the server certificate
  12. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  13. SecTrustResultType result;
  14. //This takes the serverTrust object and checkes it against your keychain
  15. SecTrustEvaluate(challenge.protectionSpace.serverTrust,&result);
  16.  
  17. //if we want to ignore invalid server for certificates,we just accept the server
  18. if (kSPAllowInvalidServerCertificates) {
  19. [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
  20. return;
  21. } else if(result == kSecTrustResultProceed || result == kSecTrustResultConfirm || result == kSecTrustResultUnspecified) {
  22. //When testing this against a trusted server I got kSecTrustResultUnspecified every time. But the other two match the description of a trusted server
  23. [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
  24. return;
  25. }
  26. } else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodClientCertificate) {
  27. //this handles authenticating the client certificate
  28.  
  29. /*
  30. What we need to do here is get the certificate and an an identity so we can do this:
  31. NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:myCerts persistence:NSURLCredentialPersistencePermanent];
  32. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  33.  
  34. It's easy to load the certificate using the code in -installCertificate
  35. It's more difficult to get the identity.
  36. We can get it from a .p12 file,but you need a passphrase:
  37. */
  38.  
  39. NSString *p12Path = [[BundleManager bundleForCurrentSkin] pathForResource:kP12FileName ofType:@"p12"];
  40. NSData *p12Data = [[NSData alloc] initWithContentsOfFile:p12Path];
  41.  
  42. CFStringRef password = CFSTR("PASSWORD");
  43. const void *keys[] = { kSecImportExportPassphrase };
  44. const void *values[] = { password };
  45. CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL,keys,values,1,NULL,NULL);
  46. CFArrayRef p12Items;
  47.  
  48. OSStatus result = SecPKCS12Import((CFDataRef)p12Data,optionsDictionary,&p12Items);
  49.  
  50. if(result == noErr) {
  51. CFDictionaryRef identityDict = CFArrayGetValueAtIndex(p12Items,0);
  52. SecIdentityRef identityApp =(SecIdentityRef)CFDictionaryGetValue(identityDict,kSecImportItemIdentity);
  53.  
  54. SecCertificateRef certRef;
  55. SecIdentityCopyCertificate(identityApp,&certRef);
  56.  
  57. SecCertificateRef certArray[1] = { certRef };
  58. CFArrayRef myCerts = CFArrayCreate(NULL,(void *)certArray,NULL);
  59. CFRelease(certRef);
  60.  
  61. NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identityApp certificates:(NSArray *)myCerts persistence:NSURLCredentialPersistencePermanent];
  62. CFRelease(myCerts);
  63.  
  64. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  65. }
  66. } else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodDefault || [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM) {
  67. // For normal authentication based on username and password. This could be NTLM or Default.
  68.  
  69. DAVCredentials *cred = _parentSession.credentials;
  70. NSURLCredential *credential = [NSURLCredential credentialWithUser:cred.username password:cred.password persistence:NSURLCredentialPersistenceForSession];
  71. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  72. } else {
  73. //If everything fails,we cancel the challenge.
  74. [[challenge sender] cancelAuthenticationChallenge:challenge];
  75. }
  76. }

参考:
Ref1,Ref2,Ref3

希望这可以帮助

猜你在找的iOS相关文章