ios9 识别自签名证书能用吗

 我来答
ID千里一醉
2016-03-21 · TA获得超过3229个赞
知道小有建树答主
回答量:963
采纳率:0%
帮助的人:673万
展开全部

公司的服务器被人DDOS攻击了,后台改用自签证的证书,全部请求改用HTTPS. iOS的网络请求也需要全部改. 坑爹的是项目中有两套请求方案. 1,使用原始苹果自带的NSURLConnection 2, 是我接手以后改用的afnetworking. 要全部改的话一下子没那么多时间改的过来. 就只好两个方案都实现下HTTPS

  • afnetworking 使用字签证证书访问HTTPS
    • 把服务器给你的自签证的证书放入bundle一般是.cer文件
    • 创建afnnetworking 安全策略对象,并设置发起请求manager的安全策略属性.设置了安全策略属性,afnnetworking会自动扫描bundl里的证书.
    • 最坑的是 iOS9新出的App Transport Security 也就是要我们把所有请求从 HTTP改成HTTPS的家伙, 它竟然不认自签证的证书. 苹果大爷难道真是土豪惯了,以为我们开发者都会买ca的证书吗. 解决办法就是那里还是要设置在Info.plist中添加NSAppTransportSecurity类型Dictionary在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型 Boolean,值设为YES
  //创建安全策略对象
   AFSecurityPolicy * security = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]; //设置证书
   security.allowInvalidCertificates = YES;  // 由于是自签证证书 afnnetworking 会认为是无效的 设置为允许
   security.validatesDomainName = NO; //验证证书绑定的域
   [[AFHTTPRequestOperationManager manager]setSecurityPolicy:security]; //

   //普通一样发起请求就可以
   [[AFHTTPRequestOperationManager manager] POST:@"/test" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

   }];
  • NSURLConnection 使用自签证证书支持HTTPS,只需要在实现NSURLConnection的代理方法即可. 这个解决方法转自GitHub JacksonTian 他原文中有多了行释放代码 CFRelease(trust); ,多了这行代码在短时间重复请求,一个URL的时候trust 会提前释放导致crash,没有细究原因,直接注释掉了.
#pragma Support NSURLCONNECTION HTTPS

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{

   return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   static CFArrayRef certs;
   if (!certs) {
       //创建证书data
       NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HTTPS" ofType:@"cer"]];

       SecCertificateRef rootcert = SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(certData));
       const void *array[1] = { rootcert };
       certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks);
//        CFRelease(rootcert);    // for completeness, really does not matter
   }

   SecTrustRef trust = [[challenge protectionSpace] serverTrust];
   int err;
   SecTrustResultType trustResult = 0;
   err = SecTrustSetAnchorCertificates(trust, certs);
   if (err == noErr) {
       err = SecTrustEvaluate(trust,&trustResult);
   }
//    BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed)||(trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified));
   BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));

   if (trusted) {

       [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
   }
   else{

       [challenge.sender cancelAuthenticationChallenge:challenge];
   }

//    CFRelease(trust);

}

**后面通过测试, 不实现上面的委托, NSURLConnection 也可以直接连接HTTPS服务器, 这种是绕过了证书的. afnetworking 不设置安全策略就无法访问HTTPS服务器.注:afnnetworking版本使用的是2.5.4 ,之前的版本关于安全策略的API有bug 在afn的issue中看到**

百事牛
2024-10-28 广告
作为深圳奥凯丰科技有限公司的一员,我们了解到,当Word文件设置了编辑密码且用户忘记密码时,常规方式无法直接解除限制。此时,推荐使用我们的奥凯丰WORD解密大师。这款软件具备解除限制功能,用户只需将Word文件添加到软件中,并点击开始,即可... 点击进入详情页
本回答由百事牛提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式