我是相当新的使用SSL通道消费web服务。经过相当不错的搜索后,我发现了一种使用NSURLConnection委托API执行SSL/HTTPS身份验证的方法。以下是代码片段进行实际验证的事情:

iOS和SSL:无法验证自签名的服务器证书

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

[self printLogToConsole:@"Authenticating...."];

[self printLogToConsole:[NSString stringWithFormat:@"\n%@\n", [challenge description]]];

NSLog(@"\n\nserverTrust: %@\n", [[challenge protectionSpace] serverTrust]);

/* Extract the server certificate for trust validation

*/

NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

assert(protectionSpace);

SecTrustRef trust = [protectionSpace serverTrust];

assert(trust);

CFRetain(trust); // Make sure this thing stays around until we're done with it

NSURLCredential *credential = [NSURLCredential credentialForTrust:trust];

/* On iOS

* we need to convert it to 'der' certificate. It can be done easily through Terminal as follows:

* $ openssl x509 -in certificate.pem -outform der -out rootcert.der

*/

NSString *path = [[NSBundle mainBundle] pathForResource:@"rootcert" ofType:@"der"];

assert(path);

NSData *data = [NSData dataWithContentsOfFile:path];

assert(data);

/* Set up the array of certificates, we will authenticate against and create credentials */

SecCertificateRef rtCertificate = SecCertificateCreateWithData(NULL, CFBridgingRetain(data));

const void *array[1] = { rtCertificate };

trustedCerts = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks);

CFRelease(rtCertificate); // for completeness, really does not matter

/* Build up the trust anchor using our root cert */

int err;

SecTrustResultType trustResult = 0;

err = SecTrustSetAnchorCertificates(trust, trustedCerts);

if (err == noErr) {

err = SecTrustEvaluate(trust, &trustResult);

}

CFRelease(trust); // OK, now we're done with it

[self printLogToConsole:[NSString stringWithFormat:@"trustResult: %d\n", trustResult]];

/* http://developer.apple.com/library/mac/#qa/qa1360/_index.html

*/

BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified));

// Return based on whether we decided to trust or not

if (trusted) {

[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

[self printLogToConsole:@"Success! Trust validation successful."];

} else {

[self printLogToConsole:@"Failed! Trust evaluation failed for service root certificate.\n"];

[[challenge sender] cancelAuthenticationChallenge:challenge];

}

}

但我发现了以下错误:

2012-06-11 17:10:12.541 SecureLogin[3424:f803] Error during connection: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x682c790 {NSErrorFailingURLKey=https://staging.esecure.url/authentication/signin/merchants, NSErrorFailingURLStringKey=https://staging.esecure.url/authentication/signin/merchants}

我使用我从服务器获得的相同证书并将其转换为'der'格式。我正在构建适用于iOS 5.x的应用程序。 我不确定我是否错过了某些东西。让我知道你的建议。

谢谢。

编辑 这里检查证书后输出的外观:

让我知道如果有什么不对。

谢谢。

+0

不,我没有使用ASIHTTPRequest APIs。我正在使用NSURLConnection API,此时不能更改为其他第三方库。 我正在使用使用OpenSSL创建的服务器使用自签名证书。 –

+0

[NSURLConnection问题]的可能重复(http:// stackoverflow。com/questions/3766755/problem-with-nsurlconnection) –

ios 自架验证服务器,iOS和SSL:无法验证自签名的服务器证书相关推荐

  1. ios 自架验证服务器,iOS 13-Sign In with Apple(苹果登录)APP+后端验证

    关于Sign In with Apple的介绍就不讲了,直接进入正题吧! Certificates,IDs & Profiels->Keys->+号 1585892549402.j ...

  2. 苹果网页无法与服务器建立安全连接,iOS 9.3:发生SSL错误,无法建立与服务器的安全连接...

    我的自签名证书出现以下错误 错误域= NSURLErrorDomain代码= -1200"发生SSL错误,无法建立与服务器的安全连接. 在测试我的一个演示应用程序的Web服务时 iOS 9. ...

  3. iphone开发 IOS 组织架构图

    转载自 :http://blog.csdn.net/mashi321323/article/details/18267719 登录 | 注册 mashi321323的专栏 目录视图 摘要视图 订阅 1 ...

  4. 服务器ios文件,ios 文件到服务器

    ios 文件到服务器 内容精选 换一换 安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具,将文件上传到云服务器.例如QQ.exe.在本地主机和Windows云服务器上分别安装数据传 ...

  5. ios文件app访问samba服务器,ios链接samba服务器

    ios链接samba服务器 内容精选 换一换 通过此链接可以拉起华为云会议App并且加入会议.App已启动未登录场景下,调用该链接会弹出主界面并且匿名入会:App已启动已登录场景下,调用该链接会弹出主 ...

  6. 梦幻西游手游什么服务器稳定,《梦幻西游手游》ios玩哪个区好 ios区服选择推荐...

    导 读 梦幻西游三维版ios服即将在12月12日10点开启全新体验,届时大家都可以进入游戏中体验,很多玩家都会在纠结选哪个服好?玩过梦幻各种版本的游戏都知道有几个服都是官方默认的体验服,早进入到其中可 ...

  7. ios添加邮件收件服务器,iOS 系统邮件的基础使用

    尽管有好多人的主力邮件是 Gmail,客户端的首选也是 Gmail,但是在国内的互联网环境中不可避免的要使用到本土的一些邮件服务.对于这些轻量级的服务我选择用系统自带的邮件应用进行统一的管理.本文主要 ...

  8. 苹果在线签名服务器搭建,苹果iOS企业签名 、apple 超级签名网站服务器选用

    苹果iOS企业签名运行流程 1.用户手机安装预留的描述文件,获取本机udid后,向服务器返回用户的udid 2.服务器收到UDID后,将UDID添加到开发者账号下,下载此udid签名用的证书描述文件. ...

  9. ios怎么用spotify_在iOS中通过Spotify进行身份验证

    ios怎么用spotify Authenticating through an API like Spotify in order to access and modify a user's info ...

最新文章

  1. EE4J项目情况汇总,微软加入Jakarta EE工作组
  2. 【已解决】R read.table()报错:incomplete final line found by readTableHeader
  3. Android Studio添加aar
  4. input 对伪元素(:before :after)的支持情况
  5. python制作软件excel_利用Python制作一个 截图+Excel操作浏览器小工具
  6. python全局解释器锁 tensorflow_Python即将出局?Julia和Swift能取而代之吗?
  7. 图像增强︱window7+opencv3.2+keras/theano简单应用(函数解读)
  8. ASP.NET profile之 找不到存储过程'dbo.aspnet_CheckSchemaVersion'
  9. 小白 C 入门并发疯学习路线(书单)
  10. php 禁止转换,php实现十进制、三十六进制转换的函数
  11. zabbix开启网页报警声音方法:网页也可以有报警声音(46)
  12. python表格控件_python--excel操作插件openpyxl
  13. 为什么要学编写通达信指标公式
  14. 数据库原理与应用实验九 视图的使用
  15. linux进程线程-alarm闹钟函数
  16. 产品读书《游戏改变世界:游戏化如何让现实变得更美好》
  17. 企查查如何在线查询失信企业?
  18. W3school:CSS基础:CSS注释、颜色(颜色、RGB、HEX、HSL)、背景(背景、背景图像、背景重复、背景附着、简写背景属性)
  19. chinaren同学录的字数倒记数
  20. caffe 制作数据集

热门文章

  1. c语言未定义标识符using,c++未定义标识符怎么办?
  2. 常用js库和框架(jsPlumb)
  3. 如何给视频添加动态文字水印?
  4. efi文件错误服务器崩溃,验证磁盘提示EFI分区错误需要修复。系统启动时禁止符号!...
  5. 基于rnn神经网络的写唐诗机器人
  6. 如何写出一份高逼格的简历?
  7. virbr0怎么关闭_虚拟机中除去虚拟网桥virbr0 | 学步园
  8. mysql删除注册表mysqld要删除吗_彻底删除Mysql方法
  9. Centos7下彻底删除Mysql, 重新安装Mysql
  10. 域服务器和客户端怎么传文件,ad域服务器同步客户端文件