2012-07-30 23 views
2

我試圖使用存儲在我的i​​Phone上的.mobileconfig文件中的.pfx連接到服務器。獲取包含在iOS的mobilecertificate中的pfx文件

當服務器在

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

提出要求我怎樣才能創建NSURLCredential與.PFX?我應該使用

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

如果是這樣的話,我該如何提取.pfx才能將它放入數組中。

在此先感謝。

回答

1

因此,沒有,有沒有辦法讓從mobileconfig文件中的證書! iOS應用程序使用自己的鑰匙串訪問和存儲。只有電子郵件和其他電話服務,如互聯網可以使用這些證書

1

U可以使用我的代碼:

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

好運^ -^

+0

問題是,該路徑返回零,因爲pfx是(我猜)不是在mainbundle。它合併到mobileconfig中。我還試圖在沒有mobileconfig的情況下安裝pfx,路徑仍然返回nil。你有pfx文件目錄嗎? – user1447414 2012-07-30 11:03:44

相關問題