ASIHTTPRequest實現https雙向認證請求

來源:互聯網
上載者:User

標籤:des   blog   http   java   os   檔案   

什麼是雙向認證呢?簡而言之,就是伺服器端對請求它的用戶端要進行身分識別驗證,用戶端對自己所請求的伺服器也會做身分識別驗證。服務端一旦驗證到請求自己的用戶端為不可信任的,服務端就拒絕繼續通訊。用戶端如果探索服務端為不可信任的,那麼也中止通訊。

 

        雙向認證的演算法理論是RSA,(點擊此處瞭解RSA演算法原理)。 雙向認證具體又是通過安全性憑證的方式來實現的,安全性憑證可用openssl或java程式來產生,用於雙向認證的安全性憑證中儲存了金鑰組,憑證授權單位信 息,簽名資訊,簽名演算法,頒發對象,有效期間等資訊。雙向認證中安全性憑證分為伺服器端認證和用戶端認證,用伺服器端認證中的私密金鑰對用戶端認證進行簽名,並把 簽名資訊寫到用戶端認證中,就得到了被服務端信任的認證。當用戶端請求該服務端時,服務端為拿到用戶端認證資訊,然後取出認證中的簽名資訊,用伺服器端證 書的公開金鑰驗證,如果發現這個用戶端認證確實是伺服器端認證簽名頒發的,那麼通訊就可以繼續進行,否則中斷。

        上面簡單介紹了一下雙向認證和安全性憑證,那麼我們現在開始正題。

        首先,我們用java產生一個伺服器端認證庫myserverdomain和用戶端認證庫wenfeng.xu,取出伺服器端的認證庫中的認證為用戶端證 書庫簽名並產生PKCS12格式的認證檔案wenfeng.xu.pfx。然後我們將伺服器端認證配置在應用伺服器中,並啟用用戶端認證。以jetty為 例,以下為配置方法:

 

   啟動應用伺服器,並用與產生服務端認證一致的網域名稱訪問應用(注意這點非常重要,ASIHTTPRequest如果不這麼做是會報錯的,這個網域名稱可以隨便 取,只要更改系統的host配置,讓網域名稱指向服務端ip就行了)。如果你用瀏覽器訪問已啟動的應用,如果看到以下資訊,就可以開始oc用戶端的編碼了。

 

 

      在引入了ASIHTTPRequest架構的項目中建立測試類別Https.m

 

 

C代碼  
  1. @implementation Https  
  2.   
  3. + (void)testClientCertificate {  
  4.     NSURL *httpsUrl = [NSURL URLWithString:@"https://www.myserverdomain.com:8443/smvcj"];//訪問路徑  
  5.       
  6.     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpsUrl];  
  7.       
  8.     SecIdentityRef identity = NULL;  
  9.     SecTrustRef trust = NULL;  
  10.       
  11.     //綁定認證,認證放在Resources檔案夾中  
  12.     NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wenfeng.xu" ofType:@"pfx"]];//認證檔案名稱和檔案類型  
  13.     [Https extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];  
  14.       
  15.     request = [ASIHTTPRequest requestWithURL:httpsUrl];  
  16.       
  17.     [request setClientCertificateIdentity:identity];//設定訪問路徑  
  18.     [request setValidatesSecureCertificate:NO];//是否驗證伺服器端認證,如果此項為yes那麼伺服器端認證必須為合法的認證機構頒發的,而不能是自己用openssl 或java產生的認證  
  19.       
  20.       
  21.     [request startSynchronous];  
  22.       
  23.     NSError *error = [request error];  
  24.     if (!error) {  
  25.         NSString *response = [request responseString];  
  26.         NSLog(@"response is : %@",response);  
  27.     } else {  
  28.         NSLog(@"Failed to save to data store: %@", [error localizedDescription]);  
  29.         NSLog(@"%@",[error userInfo]);  
  30.     }  
  31. }  
  32.   
  33. + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {  
  34.   
  35.       
  36.     OSStatus securityError = errSecSuccess;  
  37.       
  38.     CFStringRef password = CFSTR("[email protected]"); //認證密碼  
  39.     const void *keys[] =   { kSecImportExportPassphrase };  
  40.     const void *values[] = { password };  
  41.       
  42.     CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys,values, 1,NULL, NULL);  
  43.       
  44.     CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);  
  45.     //securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);  
  46.     securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,optionsDictionary,&items);  
  47.       
  48.     if (securityError == 0) {  
  49.         CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);  
  50.         const void *tempIdentity = NULL;  
  51.         tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);  
  52.         *outIdentity = (SecIdentityRef)tempIdentity;  
  53.         const void *tempTrust = NULL;  
  54.         tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);  
  55.         *outTrust = (SecTrustRef)tempTrust;  
  56.     } else {  
  57.         NSLog(@"Failed with error code %d",(int)securityError);  
  58.         return NO;  
  59.     }  
  60.     return YES;  
  61. }  
  62.   
  63. @end  

 

 

    在項目中調用  testClientCertificate方法,發現會報以下錯誤

    

C代碼  
  1. 2014-01-04 15:49:51.194 Mac[661:303] CFNetwork SSLHandshake failed (-9807)  
  2. 2014-01-04 15:49:51.203 Mac[661:303] Failed to save to data store: A connection failure occurred: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)  
  3. 2014-01-04 15:49:51.204 Mac[661:303] {  
  4.     NSLocalizedDescription = "A connection failure occurred: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)";  
  5.     NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-9807 \"The operation couldn\U2019t be completed. (OSStatus error -9807.)\" (errSSLXCertChainInvalid: Invalid certificate chain )";  
  6. }  

 

 

  怎麼會這樣?分析最後一句“Invalid certificate chain” 意思是無效的憑證鏈結。因為每一個認證中都有一個憑證鏈結,來表示這個認證的階層。報這個錯是因為這個用戶端認證的最頂層是我們自己建立的認證,而不是合 法的認證機構頒發的。每個作業系統預設會把一些公認的認證機構頒發的密鑰憑證存在系統信認的根憑證庫中,以便信任由這些公認的認證機構簽名給其它使用者的證 書。那麼如何在測試環境中避免這個錯?我們只要修改ASIHTTPRequest架構中的相關配置就行了,開啟ASIHTTPRequest.m檔案,查 找“https”關健字,找到

Java代碼  
  1. NSMutableDictionary *sslProperties = [NSMutableDictionary dictionaryWithCapacity:1];  

 將其注掉,然後換成以下代碼

Java代碼  
  1. NSMutableDictionary *sslProperties =[[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  2.                                               [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,  
  3.                                               [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,  
  4.                                               [NSNumber numberWithBool:NO],  kCFStreamSSLValidatesCertificateChain,  
  5.                                               kCFNull,kCFStreamSSLPeerName,  
  6.                                               nil];  

 解決我們的錯誤的關鍵代碼是

     [NSNumber numberWithBool:NO],  kCFStreamSSLValidatesCertificateChain  表示不校正憑證鏈結。

儲存一下再運行就可以正常訪問應用了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.