站内
QQ客服
自定内容
自定内容
请输入内容
新闻搜索
 
 
新闻动态
HTTPS业务场景的解决方案
成都狮龙书廊科技有限责任公司长春分公司    2016-06-17 18:11:46    文字:【】【】【
摘要:HTTPS业务场景的解决方案

HTTPS业务场景的解决方案


1 背景说明

发送HTTPS请求首先要进行SSL/TLS握手,握手过程大致如下:

  1. 客户端发起握手请求,携带随机数、支持算法列表等参数。
  2. 服务端收到请求,选择合适的算法,下发公钥证书和随机数。
  3. 客户端对服务端证书进行校验,并发送随机数信息,该信息使用公钥加密。
  4. 服务端通过私钥获取随机数信息。
  5. 双方根据以上交互的信息生成session ticket,用作该连接后续数据传输的加密密钥。

上述过程中,和HTTPDNS有关的是第3步,客户端需要验证服务端下发的证书,验证过程有以下两个要点:

  1. 客户端用本地保存的根证书解开证书链,确认服务端下发的证书是由可信任的机构颁发的。
  2. 客户端需要检查证书的domain域和扩展域,看是否包含本次请求的host。

如果上述两点都校验通过,就证明当前的服务端是可信任的,否则就是不可信任,应当中断当前连接。

当客户端使用HTTPDNS解析域名时,请求URL中的host会被替换成HTTPDNS解析出来的IP,所以在证书验证的第2步,会出现domain不匹配的情况,导致SSL/TLS握手不成功。

2 解决方案

针对“domain不匹配”问题,可以采用如下方案解决:hook证书校验过程中第2步,将IP直接替换成原来的域名,再执行证书验证。

下面分别列出Android和iOS平台的示例代码。

2.1 Android示例

此示例针对HttpURLConnection接口。

		
  1. try {
  2. String url = "https://140.205.160.59/?sprefer=sypc00";
  3. HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
  4. connection.setRequestProperty("Host", "m.taobao.com");
  5. connection.setHostnameVerifier(new HostnameVerifier() {
  6. /*
  7. * 关于这个接口的说明,官方有文档描述:
  8. * This is an extended verification option that implementers can provide.
  9. * It is to be used during a handshake if the URL's hostname does not match the
  10. * peer's identification hostname.
  11. *
  12. * 使用HTTPDNSURL里设置的hostname不是远程的主机名(如:m.taobao.com),与证书颁发的域不匹配,
  13. * Android HttpsURLConnection提供了回调接口让用户来处理这种定制化场景。
  14. * 在确认HTTPDNS返回的源站IP与Session携带的IP信息一致后,您可以在回调方法中将待验证域名替换为原来的真实域名进行验证。
  15. *
  16. */
  17. @Override
  18. public boolean verify(String hostname, SSLSession session) {
  19. return HttpsURLConnection.getDefaultHostnameVerifier().
  20. verify("m.taobao.com“, session);
  21. return false;
  22. }
  23. });
  24. connection.connect();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. }

2.2 iOS示例

此示例针对NSURLSession/NSURLConnection接口。

		
  1. - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
  2. forDomain:(NSString *)domain
  3. {
  4. /*
  5. * 创建证书校验策略
  6. */
  7. NSMutableArray *policies = [NSMutableArray array];
  8. if (domain) {
  9. [policies addObject:(__bridge_transfer id)SecPolicyCreateSSL(true, (__bridge CFStringRef)domain)];
  10. } else {
  11. [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
  12. }
  13. /*
  14. * 绑定校验策略到服务端的证书上
  15. */
  16. SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);
  17. /*
  18. * 评估当前serverTrust是否可信任,
  19. * 官方建议在result = kSecTrustResultUnspecified 或 kSecTrustResultProceed
  20. * 的情况下serverTrust可以被验证通过,https://developer.apple.com/library/ios/technotes/tn2232/_index.html
  21. * 关于SecTrustResultType的详细信息请参考SecTrust.h
  22. */
  23. SecTrustResultType result;
  24. SecTrustEvaluate(serverTrust, &result);
  25. return (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
  26. }
  27. /*
  28. * NSURLConnection
  29. */
  30. - (void)connection:(NSURLConnection *)connection
  31. willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  32. {
  33. if (!challenge) {
  34. return;
  35. }
  36. /*
  37. * URL里面的host在使用HTTPDNS的情况下被设置成了IP,此处从HTTP Header中获取真实域名
  38. */
  39. NSString* host = [[self.request allHTTPHeaderFields] objectForKey:@"host"];
  40. if (!host) {
  41. host = self.request.URL.host;
  42. }
  43. /*
  44. * 判断challenge的身份验证方法是否是NSURLAuthenticationMethodServerTrust(HTTPS模式下会进行该身份验证流程),
  45. * 在没有配置身份验证方法的情况下进行默认的网络请求流程。
  46. */
  47. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
  48. {
  49. if ([self evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:host]) {
  50. /*
  51. * 验证完以后,需要构造一个NSURLCredential发送给发起方
  52. */
  53. NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  54. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  55. } else {
  56. /*
  57. * 验证失败,取消这次验证流程
  58. */
  59. [[challenge sender] cancelAuthenticationChallenge:challenge];
  60. }
  61. } else {
  62. /*
  63. * 对于其他验证方法直接进行处理流程
  64. */
  65. [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  66. }
  67. }
  68. ////////////////////////////////////////////////////////////
  69. ////////////////////////////////////////////////////////////
  70. ////////////////////////////////////////////////////////////
  71. ////////////////////////////////////////////////////////////
  72. /*
  73. * NSURLSession
  74. */
  75. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
  76. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  77. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler
  78. {
  79. if (!challenge) {
  80. return;
  81. }
  82. NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  83. NSURLCredential *credential = nil;
  84. /*
  85. * 获取原始域名信息。
  86. */
  87. NSString* host = [[self.request allHTTPHeaderFields] objectForKey:@"host"];
  88. if (!host) {
  89. host = self.request.URL.host;
  90. }
  91. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  92. if ([self evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:host]) {
  93. disposition = NSURLSessionAuthChallengeUseCredential;
  94. credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  95. } else {
  96. disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
  97. }
  98. } else {
  99. disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  100. }
  101. // 对于其他的challenges直接使用默认的验证方案
  102. completionHandler(disposition,credential);
  103. }
感谢您的反馈!

浏览 (103) | 评论 (0) | 评分(0) | 支持(0) | 反对(0) | 发布人:毛凌国.中国
将本文加入收藏夹
 
 
自定内容
请输入内容<--狮龙书廊跑马灯文件代码区->
自定内容

 

感谢您注册参加技术达人“秀”—“数字化变革新旗舰5K智能协作终端发布”在线研讨会。

活动日期:2017年6月8日     
北京时间:上午10:00-11:00
活动号:204 914 711
活动密码:170608
 网站编辑@ 2017 年成都狮龙书廊科技有限责任公司。
Cisco、Cisco Systems和Cisco Systems标志是思科系统有限公司和/或其子公司在中国和特定国家和地区的商标或注册商标。
本文中涉及的所有其它商标均属于各自所有者的资产。
自定内容

请输入内容

自定内容

欢迎参加第五届豆瓣阅读征文大赛

详情
豆瓣阅读  发送至 public  
隐藏
发件人:豆瓣阅读  
收件人:
public  
抄 送:
密 送:
时 间:2017-08-11 15:28
时 间:
地 点:
邮件已加密,收件方查看邮件需要密码。 邮件已加密,密码:
  • 回复
  • 转发
查看译文
查看原文
对照显示
正在翻译,请稍候...
自定内容

美橙视频制作中心

独特创意 精妙制作 品质铸造 超高性价比
专业实景影棚
1000平方米实景影棚
超2000平方米外拍基地
一流的影视设备
电影级摄像机、灯光设备、
专业航拍机等
资深制作团队
资深编导、策划、拍摄、
后期制作专业操刀
专业配音
80多位资深配音,支持
普通话、英文、方言等