Iphone 在iOS中获取包含在mobilecertificate中的pfx文件

Iphone 在iOS中获取包含在mobilecertificate中的pfx文件,iphone,objective-c,ios,cocoa,Iphone,Objective C,Ios,Cocoa,我正在尝试使用存储在iPhone上.mobileconfig文件中的.pfx连接到服务器 当服务器在中请求它时 -(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge{ 如何使用.pfx创建NSURLCredential?我应该用吗 + (NSURLCredential *)credentialWithIde

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

当服务器在中请求它时

-(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge{
如何使用.pfx创建NSURLCredential?我应该用吗

+ (NSURLCredential *)credentialWithIdentity:(SecIdentityRef)identity certificates:(NSArray *)certArray persistence:(NSURLCredentialPersistence)persistence
如果是这样,我如何提取.pfx以将其放入数组中

提前感谢。

您可以使用我的代码:

 - (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;
}

祝你好运^-^

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

问题在于path返回零,因为pfx(我猜)不在主捆绑包中。它与mobileconfig合并。我还尝试在没有mobileconfig的情况下安装pfx,但路径仍然返回nil。你有pfx文件目录吗?