美文网首页
iOS https 相应配置

iOS https 相应配置

作者: 小明讲啥故事 | 来源:发表于2019-03-12 10:18 被阅读0次

1.通过绕过证书认证的方法进行请求

#pragma mark - https 认证
- (void)certifyHttps:(NSString *)httpsUrl {
    _bHttpsCertifySuccess = NO;        // 默认失败
    
    NSURLRequest *certifyRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:httpsUrl]];
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:certifyRequest delegate:self];
    [conn start];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
    return NO;
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSLog(@"APPdelegate didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        [[challenge sender]  useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        [[challenge sender]  continueWithoutCredentialForAuthenticationChallenge: challenge];
    }
    
    _bHttpsCertifySuccess = YES;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    _bHttpsCertifySuccess = NO;
}

客户端要实现 https 验证

1.系统验证
2.本地证书验证

NSURLConnection支持HTTPS的实现

// Now start the connection
NSURL * httpsURL = [NSURL URLWithString:@"https://www.google.com"];
self.connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:httpsURL] delegate:self];

//回调
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    //1)获取trust object
    SecTrustRef trust = challenge.protectionSpace.serverTrust;
    SecTrustResultType result;

    //2)SecTrustEvaluate对trust进行验证
    OSStatus status = SecTrustEvaluate(trust, &result);
    if (status == errSecSuccess &&
        (result == kSecTrustResultProceed ||
        result == kSecTrustResultUnspecified)) {

        //3)验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
        NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];

    } else {

        //5)验证失败,取消这次验证流程
        [challenge.sender cancelAuthenticationChallenge:challenge];

  }
}

客户端本地证书验证

1.生成证书

openssl x509 -in csc108.crt -out csc108.cer -outform der

2.本地证书验证(安全性高)

- (void)handleZiQianCers {
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"csc108" ofType:@"cer"];; // 证书路径
    NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
    SecCertificateRef certificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)(cerData));
    if (certificate) {
        self.trustedCertificates = @[CFBridgingRelease(certificate)];
    }
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//1 获取trust object
    SecTrustRef trust = challenge.protectionSpace.serverTrust;
    SecTrustResultType result;
    //注意:这里将之前导入的证书设置成下面验证的Trust Object的anchor certificate
    SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)self.trustedCertificates);
    //2 SecTrustEvaluate会查找前面SecTrustSetAnchorCertificates设置的证书或者系统默认提供的证书,对trust进行验证
    OSStatus status = SecTrustEvaluate(trust, &result);
    if (status == errSecSuccess &&
        (result == kSecTrustResultProceed ||
         result == kSecTrustResultUnspecified)) {

            //3 验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
            NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
            [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];

        } else {
            //4 验证失败,取消这次验证流程
            [challenge.sender cancelAuthenticationChallenge:challenge];
        }
}

AFNetworking上配置对HTTPS

NSURL * url = [NSURL URLWithString:@"https://www.google.com"];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

//allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO
//如果是需要验证自建证书,需要设置为YES
securityPolicy.allowInvalidCertificates = YES;

//validatesDomainName 是否需要验证域名,默认为YES;
//假如证书的域名与你请求的域名不一致,需把该项设置为NO;如设成NO的话,即服务器使用其他可信任机构颁发的证书,也可以建立连接,这个非常危险,建议打开。
//置为NO,主要用于这种情况:客户端请求的是子域名,而证书上的是另外一个域名。因为SSL证书上的域名是独立的,假如证书上注册的域名是www.google.com,那么mail.google.com是无法验证通过的;当然,有钱可以注册通配符的域名*.google.com,但这个还是比较贵的。
//如置为NO,建议自己添加对应域名的校验逻辑。
securityPolicy.validatesDomainName = YES;

//validatesCertificateChain 是否验证整个证书链,默认为YES
//设置为YES,会将服务器返回的Trust Object上的证书链与本地导入的证书进行对比,这就意味着,假如你的证书链是这样的:
//GeoTrust Global CA
//    Google Internet Authority G2
//        *.google.com
//那么,除了导入*.google.com之外,还需要导入证书链上所有的CA证书(GeoTrust Global CA, Google Internet Authority G2);
//如是自建证书的时候,可以设置为YES,增强安全性;假如是信任的CA所签发的证书,则建议关闭该验证,因为整个证书链一一比对是完全没有必要(请查看源代码);
securityPolicy.validatesCertificateChain = NO;

manager.securityPolicy = securityPolicy;

补充

NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"csc108" ofType:@"cer"];
NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
security.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];

相关文章

  • iOS https 相应配置

    1.通过绕过证书认证的方法进行请求 客户端要实现 https 验证 1.系统验证2.本地证书验证 NSURLCon...

  • iOS配置HTTPS证书

    iOS配置HTTPS

  • 关于iOS 10的适配及Xcode 8的变化

    一、iOS 10 1、权限 IOS 10使用隐私功能,必须在info.plist添加相应地权限配置,以及相应的Va...

  • Flutter iOS 常用权限申请

    一、 配置文件(ios->Runner->Info.plist)增加相应配置 NSPhotoLibrar...

  • iOS配置HTTPS

    https://github.com/ChenYilong/iOS9AdaptationTips 配置方法 :ht...

  • iOS 配置https

    貌似在iOS9之后,官方默认只能访问https。如果访问的https都是安全的,则不需要做任何配置。 安全的htt...

  • iOS 配置https

    昨天试验了iOS 11 beta6 发现原有的https自建证书不能使用,可能是新版本要对ATS加强验证,之前一直...

  • iOS 配置HTTPS

    关于苹果2017年1月1日之后,要求App Store 商店所有的app 使用https 的计划虽已延迟,但尽早掌...

  • iOS配置HTTPS

    引言 所有iOS的开发者都应该知道,2016年以来苹果在不断地收紧对于HTTP网络连接的限制,强力推行全部使用HT...

  • iOS配置HTTPS

    申请一个SSL证书 这个是我们后台做的操作,然后发给我.cer格式的证书,放到mainbundle就好。 AFNe...

网友评论

      本文标题:iOS https 相应配置

      本文链接:https://www.haomeiwen.com/subject/aqqgpqtx.html