2014-02-19 60 views
2

我明顯錯過了啊哈!因爲我一直在使用CA或自簽名證書進行研究,以獲得對安全URL https的訪問權限,而且我仍然無法完全理解它,所以我通常只是在與其他人的代碼和解決方案混淆,試圖獲得我的工作,我顯然缺乏基本的理解,所以希望網站的居民可以提供幫助。SSL證書混淆

本質上,我有一個應用程序與使用自簽名證書的https服務器進行通信。

我認爲是訪問服務器所需的證書和密鑰都存儲在存儲在應用程序根包中的p12中。然後,我通過這個代碼,我在網上

NSString *p12Path = [[NSBundle mainBundle] pathForResource:p12Name ofType:@"p12"]; 
NSData *p12Data = [[NSData alloc] initWithContentsOfFile:p12Path]; 
NSError *error = nil; 
NSData *data = [[SFHFKeychainUtils getPasswordForUsername:[[UIDevice currentDevice]name] andServiceName:serviceName error:&error] dataUsingEncoding:NSUTF8StringEncoding]; 
[self transform:data]; 
NSString *pass = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
CFStringRef password = (__bridge CFStringRef)pass; 
const void *keys[] = { kSecImportExportPassphrase }; 
const void *values[] = { password }; 
CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); 
CFArrayRef p12Items; 

OSStatus result = SecPKCS12Import((__bridge CFDataRef)p12Data, optionsDictionary,&p12Items); 

if(result == noErr) 
{ 
    CFDictionaryRef identityDict = CFArrayGetValueAtIndex(p12Items, 0); 
    SecIdentityRef identityApp =(SecIdentityRef)CFDictionaryGetValue(identityDict,kSecImportItemIdentity); 

    SecCertificateRef certRef; 
    SecIdentityCopyCertificate(identityApp,&certRef); 

    SecCertificateRef certArray[1] = { certRef }; 
    CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, 1, NULL); 
    CFRelease(certRef); 

    NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identityApp certificates:(__bridge NSArray *)myCerts persistence:NSURLCredentialPersistencePermanent]; 
    CFRelease(myCerts); 

    [[RKObjectManager sharedManager].HTTPClient setDefaultCredential:credential]; 
} 

而且,還有這似乎工作就發現這個P12添加到手機或應用鑰匙扣,但我必須有這個啓用

_httpClient = [RKObjectManager sharedManager].HTTPClient; 
[_httpClient setAllowsInvalidSSLCertificate:YES]; 

否則沒有連接,現在我看到各種職位說,你需要將此設置爲是允許自簽名證書,但同時我看到其他職位說,它應該只用於開發,否則這使得使用https完全多餘。顯然,多餘的安全性是不好的,所以我把它設置爲no,並且它沒有連接。

所以真的有人有任何鏈接或可以騰出一些時間自己填補我的知識這些東西如何工作的差距?這將是非常感激,並會救我從老闆得到的悲傷。

回答

1

這需要由AFNetworking處理並被稱爲「SSL固定」。查看文檔here瞭解如何啓用該功能並提供證書的詳細信息。還有有用的信息here

+0

感謝您的回答和鏈接,我仍然慢慢地對此有所瞭解,所以希望更多的人發表評論以幫助我,並可能其他人更好地理解手頭的概念以及如何爲其編寫代碼。 – Genhain