https ssl加密

AFNetworking 单向校验证书

+ (AFSecurityPolicy*)customPrivateSecurityPolicy {

    // 先导入证书

    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"private" ofType:@"cer"];//证书的路径

    NSData *certData = [NSData dataWithContentsOfFile:cerPath];

   // AFSSLPinningModeCertificate 使用证书验证模式

    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 = NO;

    securityPolicy.pinnedCertificates = @[certData];

    return securityPolicy;

}

测试:

 1.获得请求管理者

AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];

2.返回结果类型:

manage.responseSerializer = [AFHTTPResponseSerializer serializer];

 3. 加上这行代码,https ssl 验证。

 [manage setSecurityPolicy:[self customPrivateSecurityPolicy]];

重点在于处理NSURLConnection的didReceiveAuthenticationChallenge代理方法,对CA文件进行验证,并建立信任连接。//https 证书验证

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

    ///NO 系统进行管理  YES 调用connection: didReceiveAuthenticationChallenge 进行验证

    return YES;

}

// NSURLConnection 手动验证 https 证书

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    // 获取der格式CA证书路径

    NSString *certPath = [[NSBundle mainBundle]  pathForResource:@"CustomCA" ofType:@"der"];

    // 提取二进制内容

    NSData *derCA = [NSData dataWithContentsOfFile:certPath];

    // 根据二进制内容提取证书信息

    SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)derCA);

    // 形成钥匙链

    NSArray * chain = [NSArray arrayWithObject:(__bridge id)(caRef)];

    CFArrayRef caChainArrayRef = CFBridgingRetain(chain);

    // 取出服务器证书

    SecTrustRef trust = [[challenge protectionSpace] serverTrust];

    SecTrustResultType trustResult = kSecTrustResultInvalid;

    // 设置为我们自有的CA证书钥匙连

    int err = SecTrustSetAnchorCertificates(trust, caChainArrayRef);

    if (err == noErr) {

        // 用CA证书验证服务器证书

        err = SecTrustEvaluate(trust, &trustResult);

    }

    // 检查结果 kSecTrustResultConfirm 当值是这个时应询问用户许可,但这个值在iOS7.0后废弃了,系统支持到7.0 可以不管理这个值

    BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));

#warning 使用根证书验证存在服务器证书如果不是我们使用的GlobalSignRootCA签发的这个子证书签发的(另一个子证书签发)也能校验过

    if (trusted) {

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

    } else {

        //验证证书不通过

        //当执行到这的时候应该去排查一下具体为什么没过,可以看一下是否换证书了,是否是用GlobalSignRootCA或其直接签发的子证书签发的

        //现在的验证的全部是这个机构签发的证书。这个机构换证书的几率很小,因为验证书是用根证书验的,签发机构要换 就要把所有证书换掉 一般不会这么做,

        [challenge.sender cancelAuthenticationChallenge:challenge];

    }

}

你可能感兴趣的:(https ssl加密)