First, what the server needs to do:
1. Be aware that App Transport Security requires TLS 1.2,
2, and it requires the site to use a password that supports the forward secrecy protocol.
3, the certificate is also required to meet the ATS specifications, ATS only trust the certificate issued by the well-known CA, the small company used by the self signed certificate, or will be intercepted by ATS. It is therefore important to carefully check that the servers that interact with your application are compliant with ATS requirements.
Two, the client can use four kinds of programs:
1, all HTTPS 2, the two mixed, individual does not meet 3, all disable HTTPS
4. Disable HTTPS by default, but for special sites, turn on HTTPS
|
1. |
HTTPS only (HTTPS only, ATS is used in all cases) |
If your app is based on a server that supports HTTPS, then you're lucky. Your app doesn't need to make any changes. However, note that app Transport security requires TLS 1.2 and it requires the site to use a password that supports the forward secrecy protocol. The certificate is also required to comply with ATS specifications. It is therefore important to carefully check that the servers that interact with your application are compliant with ATS requirements. |
|
2. |
Mix & Match (Mix) |
It is possible for your application to work with a server that does not meet ATS requirements. In this case, you need to tell the operating system which sites are involved and then indicate in your app's info.plist file which requirements are not met. |
|
3. |
OPT out (ATS disabled) |
If you are creating a Web browser, then you have a bigger problem. Because you cannot know that your users are going to visit that webpage, you cannot indicate whether these pages support ATS requirements and transmit on HTTPS. In this case, there is no other option except to revoke all APP Transport Security. |
|
4. |
Opt out with Exceptions (except for special cases, no ATS) |
When your app revokes app Transport Security, it also defines some exceptions. This is very useful when your app takes data from a lot of servers, but also interacts with an API that you can control. In this case, specifying any load in the app's Info.plist file is allowed, but you have also specified one or more exceptions to indicate which of the app Transport security must be required. |
Using Nsurlconnection to support HTTPS implementations
Now start the connection
Nsurl * Httpsurl = [Nsurl urlwithstring:@ "https://www.google.com"];
Self.connection = [nsurlconnection connectionwithrequest:[nsurlrequest requestwithurl:httpsurl] delegate:self];
Callback
-(void) connection: (Nsurlconnection *) connection Willsendrequestforauthenticationchallenge: ( Nsurlauthenticationchallenge *) Challenge {
1) Get the Trust object
Sectrustref Trust = Challenge.protectionSpace.serverTrust;
Sectrustresulttype result;
2) Sectrustevaluate the trust to verify
Osstatus status = Sectrustevaluate (trust, &result);
if (status = = Errsecsuccess &&
(Result = = Ksectrustresultproceed | |
result = = ksectrustresultunspecified)) {
3) Verify success, generate Nsurlcredential voucher cred, tell challenge sender to use this credential to continue connection
Nsurlcredential *cred = [nsurlcredential credentialfortrust:trust];
[Challenge.sender usecredential:cred Forauthenticationchallenge:challenge];
} else {
5) Verification failure, cancel this verification process
[Challenge.sender Cancelauthenticationchallenge:challenge];
}
}
Using afnetworking to support HTTPS
Afnetworking is one of the most popular third-party open source repositories developed by Ios/osx, the author of which is the very famous IOS/OSX developer mattt Thompson, whose blog nshipster is also ios/ A great place for OSX developers to learn and broaden their technical horizons. Afnetworking has already encapsulated the above logic code, even more perfect, in the Afsecuritypolicy file, interested in reading the code of this module;
Configuring HTTPS support on Afnetworking is simple:
Nsurl * url = [Nsurl urlwithstring:@ "https://www.google.com"];
Afhttprequestoperationmanager * Requestoperationmanager = [[Afhttprequestoperationmanager alloc] InitWithBaseURL:url ];
dispatch_queue_t requestqueue = Dispatch_create_serial_queue_for_name ("Krequestcompletionqueue");
Requestoperationmanager.completionqueue = Requestqueue;
Afsecuritypolicy * securitypolicy = [Afsecuritypolicy policywithpinningmode:afsslpinningmodecertificate];
Allowinvalidcertificates whether to allow invalid certificates (that is, self-built certificates), default to No
If you need to validate your self-built certificate, you need to set it to Yes
Securitypolicy.allowinvalidcertificates = YES;
Validatesdomainname If you need to verify the domain name, the default is yes;
If the domain name of the certificate does not match the domain name you requested, you need to set the entry to no, and if it is set to No, the server can also establish a connection by using a certificate issued by another trusted authority, which is very dangerous and is recommended to be opened.
Set to No, mainly used in this case: the client is requesting a subdomain, and the certificate is a different domain name. Because the domain name on the SSL certificate is independent, if the domain name registered on the certificate is www.google.com, then mail.google.com cannot be verified; Of course, the rich can register the wildcard domain name *. google.com, but this is still more expensive.
If set to No, it is recommended that you add the check logic for the corresponding domain name.
Securitypolicy.validatesdomainname = YES;
Validatescertificatechain whether to validate the entire certificate chain, the default is Yes
Set to Yes, the certificate chain on the trust object returned by the server is compared to the locally imported certificate, which means that if your certificate chain is this:
GeoTrust Global CA
Google Internet Authority G2
// *. google.com
So, in addition to importing *. google.com , you also need to import all the CA certificates on the certificate chain (GeoTrust Global CA, Google Internet Authority G2);
If the certificate is self-built, it can be set to Yes, enhanced security, if it is a certificate issued by a trusted CA, it is recommended to turn off the authentication, because the entire certificate chain one by one is not necessary (see source code);
Securitypolicy.validatescertificatechain = NO;
Requestoperationmanager.securitypolicy = securitypolicy;
This is afnetworking to support the main configuration of HTTPS, Afhttpsessionmanager with the Basic agreement, will not repeat.
The client verifies the HTTPS certificate, has the server certificate and the client self-built certificate two certificate sources, when the server classmate determines.
Related Knowledge Reference:
https://github.com/ChenYilong/iOS9AdaptationTips#1https-only-%E5%8F%AA%E6%9C%89https%E6%89%80%E6%9C%89%E6%83% 85%e5%86%b5%e4%b8%8b%e9%83%bd%e4%bd%bf%e7%94%a8ats
http://mp.weixin.qq.com/s?__biz=MzAxMzE2Mjc2Ng==&mid=211889548&idx=1&sn= 8eb4da781c32a7fd51c3ad552347f322&scene=23&srcid=0922zkxfle07gkn3nflh8b6k#rd
IOS9 HTTPS technology pre-research