原文:http://my.oschina.net/vimfung/blog/494687ios
iOS9中新增App Transport Security(简称ATS)特性, 主要使到原来请求的时候用到的HTTP,都转向TLS1.2协议进行传输。这也意味着全部的HTTP协议都强制使用了HTTPS协议进行传输。原文以下:算法
App Transport Securityvim
App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.浏览器 If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's |
若是咱们在iOS9下直接进行HTTP请求是会收到以下错误提示:app
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.less
系统会告诉咱们不能直接使用HTTP进行请求,须要在Info.plist新增一段用于控制ATS的配置:dom
1
2
3
4
5
|
<
key
>NSAppTransportSecurity</
key
>
<
dict
>
<
key
>NSAllowsArbitraryLoads</
key
>
<
true
/>
</
dict
>
|
也即:ide
这段配置中的NSAppTransportSecurity是ATS配置的根节点,配置了节点表示告诉系统要走自定义的ATS设置。而NSAllowsAritraryLoads节点则是控制是否禁用ATS特性,设置YES就是禁用ATS功能。ui
直到前面的配置能够完美的适配iOS9了,可是若是你想遵循苹果给出的标准,让本身的数据更加安全,那么须要继续往下看。
其实ATS并不仅仅针对HTTP进行了限制,而是对HTTPS也有必定的要求,以百度的地址为例,若是在App中请求https://baidu.com的话,是会收到以下的错误信息:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
查阅了一下官方资料(https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/),发现HTTPS的请求须要知足下面的要求:
These are the App Transport Security requirements:
|
根据原文描述,首先必需要基于TLS 1.2版本协议。再来就是链接的加密方式要提供Forward Secrecy(正向保密?这个不清楚是什么,好奇的筒子找资料吧~),文档中罗列出支持的加密算法(以下表)。最后就是证书至少要使用一个SHA256的指纹与任一个2048位或者更高位的RSA密钥,或者是256位或者更高位的ECC密钥。若是不符合其中一项,请求将被中断并返回nil。
支持Forward Secrecy的加密方式
|
咱们再来看刚才的百度的地址,用浏览器打开百度的地址,而后点击连接前面的锁图标,如图:
能够看到它使用了TLS 1.2版本协议,符合第一个约定。而后能够看到使用AES_128_GCM进行加密,并使用ECDHE_RSA做为密钥交换机制的,咱们能够在Forward Secrecy的列表中找到对应两条记录:
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
可是还不能肯定百度是否提供Forward Secrecy,咱们再点开证书信息,查看“签发者名称”和“公共密钥信息”两项,如图:
看到签名算法中写着“带RSA加密的SHA-1”。能够断定该加密算法不包含在上面两项中。所以百度是一个不符合ATS的要求,因此返回了错误。这时候,若是要解决这样的问题,一样须要对ATS进行配置。配置以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
key
>NSAppTransportSecurity</
key
>
<
dict
>
<
key
>NSExceptionDomains</
key
>
<
dict
>
<
key
>baidu.com</
key
>
<
dict
>
<
key
>NSIncludesSubdomains</
key
>
<
true
/>
<
key
>NSExceptionRequiresForwardSecrecy</
key
>
<
false
/>
<
key
>NSExceptionAllowsInsecureHTTPLoads</
key
>
<
true
/>
</
dict
>
</
dict
>
</
dict
>
|
其中NSIncludesSubdomains设置为YES表示百度的子级域名都使用相同设置。NSExceptionRequiresForwardSecrecy为NO因为百度不支持ForwardSecrecy,所以屏蔽掉改功能。最后NSExceptionAllowInsecureHTTPLoads设置为YES,则表示容许访问没有证书或者是自签名、过时、主机名不匹配的证书引起的错误的域名(这里检查过百度的证书貌似没有什么问题,可是仍是须要设置此项才容许访问)。