【问题标题】:get pfx file included in a mobilecertificate in iOS获取包含在 iOS 中的移动证书中的 pfx 文件
【发布时间】:2012-07-30 09:04:39
【问题描述】:

我正在尝试使用存储在 iPhone 上的 .mobileconfig 文件中的 .pfx 连接到服务器。

当服务器请求它时

-(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge{

如何使用 .pfx 创建 NSURLCredential?我应该使用

+ (NSURLCredential *)credentialWithIdentity:(SecIdentityRef)identity certificates:(NSArray *)certArray persistence:(NSURLCredentialPersistence)persistence

如果是这样,我如何提取 .pfx 以将其放入数组中。

提前致谢。

【问题讨论】:

    标签: iphone objective-c ios cocoa


    【解决方案1】:

    所以不,没有办法从 mobileconfig 文件中获取证书。 iOS 应用程序使用自己的钥匙串访问和存储。只有电子邮件和其他电话服务(如互联网)可以使用这些证书

    【讨论】:

      【解决方案2】:

      你可以使用我的代码:

       - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge   
      {
          NSString *path = [[NSBundle mainBundle] pathForResource:@"torbix" ofType:@"pfx"];
          NSData *pfxdata = [NSData dataWithContentsOfFile:path];
          CFDataRef inpfxdata = (CFDataRef)pfxdata;
          SecIdentityRef myIdentity;
          SecTrustRef myTrust;
          OSStatus status = extractIdentityAndTrust(inpfxdata, &myIdentity, &myTrust);
          SecCertificateRef myCertificate;
          SecIdentityCopyCertificate(myIdentity, &myCertificate);
          const void *certs[] = { myCertificate };
          CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);
          NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity
                                                                   certificates:(NSArray *)myCertificate
                                                                    persistence:NSURLCredentialPersistencePermanent];
          [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
          CFRelease(myIdentity);
          CFRelease(myCertificate);
          CFRelease(certsArray);
      
      }
      //extractIdentityAndTrust method.
      -(OSStatus) extractIdentityAndTrust:(CFDataRef)inpfxdata identity:(SecIdentityRef *)identity trust:(SecTrustRef *)trust
      {
          OSStatus securityError = errSecSuccess;
          CFStringRef password = CFSTR("password");
          const void *keys[] = { kSecImportExportPassphrase };
          const void *values[] = { password };
          CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
          CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
          securityError = SecPKCS12Import(inpfxdata, options, &items);
          if (securityError == 0) {
              CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
              const void *tempIdentity = NULL;
              tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
              *identity = (SecIdentityRef)tempIdentity;
              const void *tempTrust = NULL;
              tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust);
              *trust = (SecTrustRef)tempTrust;
          }
          if (options) {
              CFRelease(options);
          }
          return securityError;
      }
      

      祝你好运!^-^

      【讨论】:

      • 问题在于路径返回 nil,因为 pfx(我猜)不在 mainbundle 中。它合并到mobileconfig中。我还尝试在没有 mobileconfig 的情况下安装 pfx,但路径仍然返回 nil。你有 pfx 到文档目录吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多