iphone下使用服务器信任证书,访问https服务器(ASIHTTPRequest)

来源:互联网 发布:python gui qt pdf 编辑:程序博客网 时间:2024/05/22 18:01
参考ASIHHPRequest开源项目中的ClientCertificateTests.m源码。 
链接:https://github.com/pokeb/asi-http-request/blob/master/Classes/Tests/ClientCertificateTests.m 
以及:http://developer.apple.com/library/mac/#documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html
 
  1. + (void)testClientCertificate {   
  2.     NSURL *httpsUrl = [NSURL URLWithString:@"https://xxxxxx.xx.xx"];   
  3.   
  4.     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpsUrl];   
  5.        
  6.     SecIdentityRef identity = NULL;   
  7.     SecTrustRef trust = NULL;   
  8.            
  9.         //绑定证书,证书放在Resources文件夹中   
  10.     NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];   
  11.     [HttpsTestViewController extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];   
  12.        
  13.     request = [ASIHTTPRequest requestWithURL:httpsUrl];   
  14.        
  15.     [request setClientCertificateIdentity:identity];   
  16.     [request setValidatesSecureCertificate:NO];   
  17.     [request startSynchronous];   
  18.        
  19.     error = [request error];   
  20.     if (!error) {   
  21.         NSString *response = [request responseString];   
  22.         NSLog(@"response is : %@",response);   
  23.     } else {   
  24.         NSLog(@"Failed to save to data store: %@", [error localizedDescription]);   
  25.         NSLog(@"%@",[error userInfo]);   
  26.     }   
  27. }   
  28.   
  29. + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {   
  30.     OSStatus securityError = errSecSuccess;   
  31.        
  32.     CFStringRef password = CFSTR("xxxxxx"); //证书密码   
  33.     const void *keys[] =   { kSecImportExportPassphrase };   
  34.         const void *values[] = { password };   
  35.        
  36.     CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys,values, 1,NULL, NULL);    
  37.        
  38.     CFArrayRef items = CFArrayCreate(NULL, 00, NULL);   
  39.     //securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);   
  40.     securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,optionsDictionary,&items);    
  41.        
  42.     if (securityError == 0) {    
  43.         CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);   
  44.         const void *tempIdentity = NULL;   
  45.         tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);   
  46.         *outIdentity = (SecIdentityRef)tempIdentity;   
  47.         const void *tempTrust = NULL;   
  48.         tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);   
  49.         *outTrust = (SecTrustRef)tempTrust;   
  50.     } else {   
  51.         NSLog(@"Failed with error code %d",(int)securityError);   
  52.         return NO;   
  53.     }   
  54.     return YES;   
  55. }  
[java] view plaincopy
  1. + (void)testClientCertificate {  
  2.     NSURL *httpsUrl = [NSURL URLWithString:@"https://xxxxxx.xx.xx"];  
  3.   
  4.     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpsUrl];  
  5.       
  6.     SecIdentityRef identity = NULL;  
  7.     SecTrustRef trust = NULL;  
  8.           
  9.         //绑定证书,证书放在Resources文件夹中  
  10.     NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];  
  11.     [HttpsTestViewController extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];  
  12.       
  13.     request = [ASIHTTPRequest requestWithURL:httpsUrl];  
  14.       
  15.     [request setClientCertificateIdentity:identity];  
  16.     [request setValidatesSecureCertificate:NO];  
  17.     [request startSynchronous];  
  18.       
  19.     error = [request error];  
  20.     if (!error) {  
  21.         NSString *response = [request responseString];  
  22.         NSLog(@"response is : %@",response);  
  23.     } else {  
  24.         NSLog(@"Failed to save to data store: %@", [error localizedDescription]);  
  25.         NSLog(@"%@",[error userInfo]);  
  26.     }  
  27. }  
  28.   
  29. + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {  
  30.     OSStatus securityError = errSecSuccess;  
  31.       
  32.     CFStringRef password = CFSTR("xxxxxx"); //证书密码  
  33.     const void *keys[] =   { kSecImportExportPassphrase };  
  34.         const void *values[] = { password };  
  35.       
  36.     CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys,values, 1,NULL, NULL);   
  37.       
  38.     CFArrayRef items = CFArrayCreate(NULL, 00, NULL);  
  39.     //securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);  
  40.     securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,optionsDictionary,&items);   
  41.       
  42.     if (securityError == 0) {   
  43.         CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);  
  44.         const void *tempIdentity = NULL;  
  45.         tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);  
  46.         *outIdentity = (SecIdentityRef)tempIdentity;  
  47.         const void *tempTrust = NULL;  
  48.         tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);  
  49.         *outTrust = (SecTrustRef)tempTrust;  
  50.     } else {  
  51.         NSLog(@"Failed with error code %d",(int)securityError);  
  52.         return NO;  
  53.     }  
  54.     return YES;  
  55. }  


项目中,要添加Security.framework。

 

原文转自http://sparrow82.iteye.com/blog/1087640

原创粉丝点击