基于iOS,Android的服务器证书失效检测

1.前言

在目前的iOS,Android手机上,当手机应用进行SSL通信时,手机端默认是不会进行服务器证书是否失效的监测。

在iOS上,系统是会定期获取所访问服务器的证书信息然后出存在本地。

在Android端,系统是不会进行任何服务器证书的监测。

2.影响

如果应用在与服务器进行SSL通信时不进行任何的证书有效性检测会造成用户信息泄漏等安全问题。

3.解决方法

服务器证书有效性检测有两种方法,CRL检测和OCSP检测。

OCSP检测主要的好处是时效性更有效率。本文主要从OCSP角度介绍实现方法。

3.1 iOS端

在iOS端,对应不同的通信方法有不同的服务器有效性检测方法

WKWebView,NSURLSession,NSURLConnection等可以通过通用的方法解决。

核心代码如下:

 1 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
 2 {
 3     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
 4         if ([[[UtilCrlOcsp alloc] init] isServerTrustProceedOrUnspecifiedWithAuthenticationChallenge:challenge]) {
 5             completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
 6         } else {
 7             completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
 8         }
 9     }
10 }
11 
12 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
13 {
14     [self updateTextViewMessage:error.description];
15 }
16 
17 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
18 {
19     [self updateTextViewMessage:response.description];
20 }
21 
22 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
23 {
24     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
25         if ([[[UtilCrlOcsp alloc] init] isServerTrustProceedOrUnspecifiedWithAuthenticationChallenge:challenge]) {
26             [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
27         } else {
28             [challenge.sender cancelAuthenticationChallenge:challenge];
29         }
30     }
31 }

 

而对于WebView,CFNetwork等的通信方法需要实现一些其它的方法。

具体可以参考苹果官网的介绍。

 

3.2 Android端的实现

在Android端,利用CRL相关的API可以很快的检测证书有效性。

但是对于OCSP的实现却没有任何相关函数支持,所以需要从零进行开发。

开发时可以参考bouncycastle的相关内容进行开发,本文主要介绍OCSP的实现过程

 1             if (true || basicResponse.isSignatureValid(new JcaContentVerifierProviderBuilder().setProvider("BC").build(rootCert.getPublicKey()))) {
 2                 SingleResp[] responses = basicResponse.getResponses();
 3 
 4                 byte[] reqNonce = getNonce(request.getExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce));
 5                 byte[] resNonce = getNonce(basicResponse.getExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce));
 6 
 7                 // validate the nonce if it is present
 8                 if (reqNonce == null || resNonce == null || Arrays.equals(reqNonce, resNonce)) {
 9 
10                     for (int i = 0; i != responses.length;) {
11                         putLog("OCSP certificate number " + responses[i].getCertID().getSerialNumber());
12                         if (responses[i].getCertStatus() == CertificateStatus.GOOD) {
13                             putLog("---OCSP status GOOD");
14                             return    true;
15                         }
16                         else if (responses[i].getCertStatus() instanceof RevokedStatus) {
17                             putLog("---OCSP status Revoked");
18                             return    false;
19                         }
20                         else {
21                             putLog("---OCSP status " + responses[i].getCertStatus());
22                             return    false;
23                         }
24                     }
25                 }

4.总结

本文只是简单的介绍了基于OCSP的证书有效性检测,具体到真正的项目中该如何应用还需要coder自己的思考。

 

posted on 2016-01-06 21:39  dinghing  阅读(2320)  评论(0编辑  收藏  举报

导航