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
链接: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
- + (void)testClientCertificate {
- NSURL *httpsUrl = [NSURL URLWithString:@"https://xxxxxx.xx.xx"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpsUrl];
- SecIdentityRef identity = NULL;
- SecTrustRef trust = NULL;
- //绑定证书,证书放在Resources文件夹中
- NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];
- [HttpsTestViewController extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];
- request = [ASIHTTPRequest requestWithURL:httpsUrl];
- [request setClientCertificateIdentity:identity];
- [request setValidatesSecureCertificate:NO];
- [request startSynchronous];
- error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- NSLog(@"response is : %@",response);
- } else {
- NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
- NSLog(@"%@",[error userInfo]);
- }
- }
- + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {
- OSStatus securityError = errSecSuccess;
- CFStringRef password = CFSTR("xxxxxx"); //证书密码
- const void *keys[] = { kSecImportExportPassphrase };
- const void *values[] = { password };
- CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys,values, 1,NULL, NULL);
- CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
- //securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);
- securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,optionsDictionary,&items);
- if (securityError == 0) {
- CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
- const void *tempIdentity = NULL;
- tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
- *outIdentity = (SecIdentityRef)tempIdentity;
- const void *tempTrust = NULL;
- tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
- *outTrust = (SecTrustRef)tempTrust;
- } else {
- NSLog(@"Failed with error code %d",(int)securityError);
- return NO;
- }
- return YES;
- }
- + (void)testClientCertificate {
- NSURL *httpsUrl = [NSURL URLWithString:@"https://xxxxxx.xx.xx"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpsUrl];
- SecIdentityRef identity = NULL;
- SecTrustRef trust = NULL;
- //绑定证书,证书放在Resources文件夹中
- NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];
- [HttpsTestViewController extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];
- request = [ASIHTTPRequest requestWithURL:httpsUrl];
- [request setClientCertificateIdentity:identity];
- [request setValidatesSecureCertificate:NO];
- [request startSynchronous];
- error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- NSLog(@"response is : %@",response);
- } else {
- NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
- NSLog(@"%@",[error userInfo]);
- }
- }
- + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {
- OSStatus securityError = errSecSuccess;
- CFStringRef password = CFSTR("xxxxxx"); //证书密码
- const void *keys[] = { kSecImportExportPassphrase };
- const void *values[] = { password };
- CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys,values, 1,NULL, NULL);
- CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
- //securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);
- securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,optionsDictionary,&items);
- if (securityError == 0) {
- CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
- const void *tempIdentity = NULL;
- tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
- *outIdentity = (SecIdentityRef)tempIdentity;
- const void *tempTrust = NULL;
- tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
- *outTrust = (SecTrustRef)tempTrust;
- } else {
- NSLog(@"Failed with error code %d",(int)securityError);
- return NO;
- }
- return YES;
- }
项目中,要添加Security.framework。
原文转自http://sparrow82.iteye.com/blog/1087640
- iphone下使用服务器信任证书,访问https服务器(ASIHTTPRequest)
- OkHttp使用Https访问服务器时信任所有证书
- iPhone HTTPS 访问非信任站点(使用非信任证书,untrusted certificate)
- ios-信任服务器证书
- https下的证书信任
- ASIHTTPRequest下访问https
- webView绕过信任证书访问https
- Tomcat服务器配置https认证(使用keytool生成证书)
- 服务器使用https,自签名证书
- Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
- Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- httpclient 无信任证书使用https
- 引发类型为“Antlr.Runtime.MismatchedTokenException”的异常
- 关于Qt中的调试问题
- 有6种不同颜色的球,每种球有无数个。现在取5个球,求取出5、4、3、2种不同颜色球的概率分别为多少----网易游戏
- 数对之差的最大值
- Eclipse上GIT插件EGIT使用手册
- iphone下使用服务器信任证书,访问https服务器(ASIHTTPRequest)
- 团队任务等待系统
- 【提升Windows 7系统性能的一些建议】
- linux的解压命令大全
- 内存分配的原理__进程分配内存有两种方式,分别由两个系统调用完成:brk和mmap(不考虑共享内存)
- 获取指定表名的所有列,按逗号分割好,很适用
- 开学前倒数第九周(6.24-6.30)
- Windows套接字错误代码说明
- python中的类