2010-11-12 126 views
1

從我在MPMoviewPlayerController的文檔中看到的,NSURLCredentialStorage可以設置爲NSURLConnection身份驗證挑戰的替代方案(這對於從URL加載資源但將NSURLConnection抽象出來的較高級類很有用不提供處理認證挑戰的代表)。這似乎與在瀏覽器中安裝證書類似,並且在某個地址需要時自動選擇所需的證書。NSURLCredentialStorage和客戶端證書身份驗證

爲了測試這一點,我建立了自己的證書頒發機構,服務器證書和客戶端證書(.p12文件)。我已經建立了一個https服務器,並使用瀏覽器從另一臺機器成功測試了客戶端證書。

現在我需要將證書集成到我的iPhone應用程序中。

我已經捆綁了P12文件和應用程序啓動我這樣做:

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"clientside" ofType:@"p12"]; 
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; 
CFDataRef inPKCS12Data = (CFDataRef)PKCS12Data;  
SecIdentityRef identity; 
SecTrustRef trust; 
extractIdentityAndTrust(inPKCS12Data, &identity, &trust); 

SecCertificateRef certificate = NULL; 
SecIdentityCopyCertificate (identity, &certificate); 

const void *certs[] = {certificate}; 
CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL); 

NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:(NSArray*)certArray persistence:NSURLCredentialPersistencePermanent]; 

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] 
             initWithHost: @"myhostname" 
             port: 443 
             protocol: @"https" 
             realm: nil 
             authenticationMethod: NSURLAuthenticationMethodClientCertificate]; 

[[NSURLCredentialStorage sharedCredentialStorage] 
setDefaultCredential: credential 
forProtectionSpace: protectionSpace]; 

的extractIdentityAndTrust功能簡單複製形式的「證書,密鑰和信託服務任務的iOS」文檔頁面(我只是用自己的密碼替換了證書密碼)。

從我可以告訴身份和證書加載很好。如果我在控制檯中打印NSURLCredential對象,我會得到如下結果:<NSURLCredential: 0x140ea0>: (null)(我不知道這是否是一個好兆頭)。

但是,當我執行setDefaultCredential時,應用程序崩潰而沒有提供太多的信息。

堆棧跟蹤看起來是這樣的:

#0 0x350a41c8 in CFHash 
#1 0x3515180c in __CFBasicHashStandardHashKey 
#2 0x351534e4 in ___CFBasicHashFindBucket_Linear 
#3 0x350a4000 in CFBasicHashFindBucket 
#4 0x350a3ecc in CFDictionaryGetValue 
#5 0x344a0df2 in URLCredentialStorage::setDefaultCredentialForProtectionSpace 
#6 0x3446a5aa in CFURLCredentialStorageSetDefaultCredentialForProtectionSpace 
#7 0x339ec79e in -[NSURLCredentialStorage setDefaultCredential:forProtectionSpace:] 
#8 0x00002612 in -[AVPlayerExampleViewController awakeFromNib] at AVPlayerExampleViewController.m:84 

我看來像有什麼在憑證不成立。

關於如何解決這個問題的任何想法?

編輯:

只是爲了測試我從郵件中打開該文件安裝在iPhone上的證書。我可以通過Safari通過https訪問頁面,但不能通過我的應用程序訪問。

唯一可行的是,如果我打開一個NSURLConnection到受保護域上的任何資源,迴應didReceiveAuthenticationChallenge(我在上面的代碼中完全加載我的憑證,只是直接發送給N​​SURLAuthenticationChallenge的發件人)和然後使用更高級的課程訪問我真正需要的東西。

+0

我有一個問題:「extractIdentityAndTrust」在哪裏聲明?你的方法是?還是從圖書館收錄?我收到一個鏈接錯誤,說它找不到該方法。 – mtmurdock 2011-04-06 18:05:16

+0

這是我的代碼中定義的方法。如果你在iOS文檔中查找這個名字,你會發現這個方法的代碼示例。 – 2011-04-07 06:41:29

回答

4

看來這是一個已知的問題,NSURLCredentialStorage只適用於用戶名密碼& NSURLCredentials(證書不支持)。不幸的是,文檔沒有提到這一點。

+0

證書不工作?你從哪裏讀到的?你能提供一個鏈接嗎? – mtmurdock 2011-04-07 14:25:11

+0

是的,請讓我們知道你的信息。謝謝! – n8gray 2011-09-14 17:50:48

+1

這是在AdvancedURLConnections示例代碼的自述文件中記錄的。當前鏈接(蘋果公司將與他們的雙週刊「打破所有文檔鏈接」腳本):http://developer.apple.com/library/ios/#samplecode/AdvancedURLConnections/Listings/Read_Me_About_AdvancedURLConnections_txt.html#//apple_ref/ doc/uid/DTS40009558-Read_Me_About_AdvancedURLConnections_txt -DontLinkElementID_24 – n8gray 2011-09-15 21:31:21

相關問題