iOS使用自签名证书实现HTTPS请求

iOS使用自签名证书实现HTTPS请求
由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求,否则无法上架,因此研究了一下在iOS中使用HTTPS请求的实现。相信大家对HTTPS都或多或少有些了解,这里我就不再介绍了,主要功能就是将传输的报文进行加密,提高安全性。

1、证书准备

证书分为两种,一种是花钱向认证的机构购买的证书,服务端如果使用的是这类证书的话,那一般客户端不需要做什么,用HTTPS进行请求就行了,苹果内置了那些受信任的根证书的。另一种是自己制作的证书,使用这类证书的话是不受信任的(当然也不用花钱买),因此需要我们在代码中将该证书设置为信任证书。

我这边使用的是xca来制作了根证书,制作流程请参考http://www.2cto.com/Article/201411/347512.html

由于xca无法导出.jsk的后缀,因此我们只要制作完根证书后以.p12的格式导出就行了,之后的证书制作由命令行来完成。自制一个批处理文件,添加如下命令:

set ip=%1%
md %ip%
keytool -importkeystore -srckeystore ca.p12 -srcstoretype PKCS12 -srcstorepass 123456 -destkeystore ca.jks -deststoretype JKS -deststorepass 123456
keytool -genkeypair -alias server-%ip% -keyalg RSA -keystore ca.jks -storepass 123456 -keypass 123456 -validity 3650 -dname "CN=%ip%, OU=ly, O=hik, L=hz, ST=zj, C=cn"
keytool -certreq -alias server-%ip% -storepass 123456 -file %ip%\server-%ip%.certreq -keystore ca.jks
keytool -gencert -alias ca -storepass 123456 -infile %ip%\server-%ip%.certreq -outfile %ip%\server-%ip%.cer -validity 3650 -keystore ca.jks
keytool -importcert -trustcacerts -storepass 123456 -alias server-%ip% -file %ip%\server-%ip%.cer -keystore ca.jks
keytool -delete -keystore ca.jks -alias ca -storepass 123456

将上面加粗的ca.p12改成你导出的.p12文件的名称,123456改为你创建证书的密码。

然后在文件夹空白处按住ctrl+shift点击右键,选择在此处打开命令窗口,在命令窗口中输入“start.bat ip/域名”来执行批处理文件,其中start.bat是添加了上述命令的批处理文件,ip/域名即你服务器的ip或者域名。执行成功后会生成一个.jks文件和一个以你的ip或域名命名的文件夹,文件夹中有一个.cer的证书,这边的.jks文件将在服务端使用.cer文件将在客户端使用,到这里证书的准备工作就完成了。

2、服务端配置

由于我不做服务端好多年,只会使用Tomcat,所以这边只讲下Tomcat的配置方法,使用其他服务器的同学请自行查找设置方法。

打开tomcat/conf目录下的server.xml文件将HTTPS的配置打开,并进行如下配置:

<Connector URIEncoding="UTF-8" protocol="org.apache.coyote.http11.Http11NioProtocol" port="8443" maxThreads="200" scheme="https" secure="true" SSLEnabled="true" sslProtocol="TLSv1.2" sslEnabledProtocols="TLSv1.2" keystoreFile="${catalina.base}/ca/ca.jks" keystorePass="123456" clientAuth="false" SSLVerifyClient="off" netZone="你的ip或域名"/>

keystoreFile是你.jks文件放置的目录,keystorePass是你制作证书时设置的密码,netZone填写你的ip或域名。注意苹果要求协议要TLSv1.2以上。

3、iOS端配置

首先把前面生成的.cer文件添加到项目中,注意在添加的时候选择要添加的targets。

1.使用NSURLSession进行请求

1 NSString *urlString = @"https://xxxxxxx";
2 NSURL *url = [NSURL URLWithString:urlString];
3 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
4 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
5 NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
6 [task resume];
View Code

需要实现NSURLSessionDataDelegate中的URLSession:didReceiveChallenge:completionHandler:方法来进行证书的校验

 1 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 2  completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
 3     NSLog(@"证书认证");
 4     if ([[[challenge protectionSpace] authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust]) {
 5         do
 6         {
 7             SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
 8             NSCAssert(serverTrust != nil, @"serverTrust is nil");
 9             if(nil == serverTrust)
10                 break; /* failed */
11             /**
12              *  导入多张CA证书(Certification Authority,支持SSL证书以及自签名的CA),请替换掉你的证书名称
13              */
14             NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"cer"];//自签名证书
15             NSData* caCert = [NSData dataWithContentsOfFile:cerPath];
16 
17             NSCAssert(caCert != nil, @"caCert is nil");
18             if(nil == caCert)
19                 break; /* failed */
20             
21             SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);
22             NSCAssert(caRef != nil, @"caRef is nil");
23             if(nil == caRef)
24                 break; /* failed */
25             
26             //可以添加多张证书
27             NSArray *caArray = @[(__bridge id)(caRef)];
28             
29             NSCAssert(caArray != nil, @"caArray is nil");
30             if(nil == caArray)
31                 break; /* failed */
32             
33             //将读取的证书设置为服务端帧数的根证书
34             OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);
35             NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");
36             if(!(errSecSuccess == status))
37                 break; /* failed */
38             
39             SecTrustResultType result = -1;
40             //通过本地导入的证书来验证服务器的证书是否可信
41             status = SecTrustEvaluate(serverTrust, &result);
42             if(!(errSecSuccess == status))
43                 break; /* failed */
44             NSLog(@"stutas:%d",(int)status);
45             NSLog(@"Result: %d", result);
46             
47             BOOL allowConnect = (result == kSecTrustResultUnspecified) || (result == kSecTrustResultProceed);
48             if (allowConnect) {
49                 NSLog(@"success");
50             }else {
51                 NSLog(@"error");
52             }
53 
54             /* kSecTrustResultUnspecified and kSecTrustResultProceed are success */
55             if(! allowConnect)
56             {
57                 break; /* failed */
58             }
59             
60 #if 0
61             /* Treat kSecTrustResultConfirm and kSecTrustResultRecoverableTrustFailure as success */
62             /*   since the user will likely tap-through to see the dancing bunnies */
63             if(result == kSecTrustResultDeny || result == kSecTrustResultFatalTrustFailure || result == kSecTrustResultOtherError)
64                 break; /* failed to trust cert (good in this case) */
65 #endif
66             
67             // The only good exit point
68             NSLog(@"信任该证书");
69             
70             NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
71             completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
72             return [[challenge sender] useCredential: credential
73                           forAuthenticationChallenge: challenge];
74             
75         }
76         while(0);
77     }
78     
79     // Bad dog
80     NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
81     completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,credential);
82     return [[challenge sender] cancelAuthenticationChallenge: challenge];
83 }
View Code

此时即可成功请求到服务端。 注:调用SecTrustSetAnchorCertificates设置可信任证书列表后就只会在设置的列表中进行验证,会屏蔽掉系统原本的信任列表,要使系统的继续起作用只要调用SecTrustSetAnchorCertificates方法,第二个参数设置成NO即可。 2.使用AFNetworking进行请求 AFNetworking首先需要配置AFSecurityPolicy类,AFSecurityPolicy类封装了证书校验的过程。

 1 /**
 2  AFSecurityPolicy分三种验证模式:
 3  AFSSLPinningModeNone:只是验证证书是否在信任列表中
 4  AFSSLPinningModeCertificate:该模式会验证证书是否在信任列表中,然后再对比服务端证书和客户端证书是否一致
 5  AFSSLPinningModePublicKey:只验证服务端证书与客户端证书的公钥是否一致
 6 */
 7 
 8 AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
 9 securityPolicy.allowInvalidCertificates = YES;//是否允许使用自签名证书
10 securityPolicy.validatesDomainName = NO;//是否需要验证域名,默认YES
11 
12 AFHTTPSessionManager *_manager = [AFHTTPSessionManager manager];
13 _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
14 _manager.securityPolicy = securityPolicy;
15 //设置超时
16 [_manager.requestSerializer willChangeValueForKey:@"timeoutinterval"];
17 _manager.requestSerializer.timeoutInterval = 20.f;
18 [_manager.requestSerializer didChangeValueForKey:@"timeoutinterval"];
19  _manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringCacheData;
20 _manager.responseSerializer.acceptableContentTypes  = [NSSet setWithObjects:@"application/xml",@"text/xml",@"text/plain",@"application/json",nil];
21  
22 __weak typeof(self) weakSelf = self;
23 [_manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *_credential) {
24         
25     SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
26     /**
27      *  导入多张CA证书
28      */
29     NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"cer"];//自签名证书
30     NSData* caCert = [NSData dataWithContentsOfFile:cerPath];
31     NSArray *cerArray = @[caCert];
32     weakSelf.manager.securityPolicy.pinnedCertificates = cerArray;
33         
34     SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);
35     NSCAssert(caRef != nil, @"caRef is nil");
36         
37     NSArray *caArray = @[(__bridge id)(caRef)];
38     NSCAssert(caArray != nil, @"caArray is nil");
39         
40     OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);
41     SecTrustSetAnchorCertificatesOnly(serverTrust,NO);
42     NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");
43         
44     NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
45     __autoreleasing NSURLCredential *credential = nil;
46     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
47         if ([weakSelf.manager.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
48             credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
49             if (credential) {
50                 disposition = NSURLSessionAuthChallengeUseCredential;
51             } else {
52                 disposition = NSURLSessionAuthChallengePerformDefaultHandling;
53             }
54         } else {
55             disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
56         }
57     } else {
58         disposition = NSURLSessionAuthChallengePerformDefaultHandling;
59     }
60         
61     return disposition;
62 }];
View Code

上述代码通过给AFHTTPSessionManager重新设置证书验证回调来自己验证证书,然后将自己的证书加入到可信任的证书列表中,即可通过证书的校验。

由于服务端使用.jks是一个证书库,客户端获取到的证书可能不止一本,我这边获取到了两本,具体获取到基本可通过SecTrustGetCertificateCount方法获取证书个数,AFNetworking在evaluateServerTrust:forDomain:方法中,AFSSLPinningMode的类型为AFSSLPinningModeCertificate和AFSSLPinningModePublicKey的时候都有校验服务端的证书个数与客户端信任的证书数量是否一样,如果不一样的话无法请求成功,所以这边我就修改他的源码,当有一个校验成功时即算成功。

当类型为AFSSLPinningModeCertificate时

1 return trustedCertificateCount == [serverCertificates count] - 1;
View Code

为AFSSLPinningModePublicKey时

1 return trustedPublicKeyCount > 0 && ((self.validatesCertificateChain) || (!self.validatesCertificateChain && trustedPublicKeyCount >= 1));
View Code

去掉了第二块中的trustedPublicKeyCount == [serverCertificates count]的条件。

这边使用的AFNetworking的版本为2.5.3,如果其他版本有不同之处请自行根据实际情况修改。

转载:https://github.com/fengling2300/networkTest

posted @ 2016-11-15 13:31  花佑御风  阅读(437)  评论(0编辑  收藏  举报