内容简介:我想使用iOS中代码附带的自签名证书创建到我的服务器的SSL连接.这样我就不必担心更复杂的中间人攻击,其中有人可以访问高级“可信”的证书颁发机构.使用我认为是Apple的标准方式,我遇到了问题.通过找到的程序生成证书和iOS代码:
我想使用iOS中代码附带的自签名证书创建到我的服务器的SSL连接.这样我就不必担心更复杂的中间人攻击,其中有人可以访问高级“可信”的证书颁发机构.使用我认为是Apple的标准方式,我遇到了问题.
通过找到的程序生成证书 here
# Create root CA & private key openssl req -newkey rsa:4096 -sha512 -days 9999 -x509 -nodes -out root.pem.cer # Create a certificate signing request openssl req -newkey rsa:4096 -sha512 -nodes -out ssl.csr -keyout ssl.key # Create an OpenSSL Configuration file from http://svasey.org/projects/software-usage-notes/ssl_en.html vim openssl.conf # Create the indexes touch certindex echo 000a > certserial echo 000a > crlnumber # Generate SSL certificate openssl ca -batch -config openssl.conf -notext -in ssl.csr -out ssl.pem.cer # Create Certificate Revocation List openssl ca -config openssl.conf -gencrl -keyfile privkey.pem -cert root.pem.cer -out root.crl.pem openssl crl -inform PEM -in root.crl.pem -outform DER -out root.crl && rm root.crl.pem
和iOS代码:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
// Load anchor cert.. also tried this with both certs and it doesn't seem to matter
NSString *path = [[NSBundle mainBundle] pathForResource:@"root.der" ofType:@"crt"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
SecCertificateRef anchorCert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)data);
CFMutableArrayRef anchorCerts = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(anchorCerts, anchorCert);
// Set anchor cert
SecTrustRef trust = [protectionSpace serverTrust];
SecTrustSetAnchorCertificates(trust, anchorCerts);
SecTrustSetAnchorCertificatesOnly(trust, YES); // only use that certificate
CFRelease(anchorCert);
CFRelease(anchorCerts);
// Validate cert
SecTrustResultType secresult = kSecTrustResultInvalid;
if (SecTrustEvaluate(trust, &secresult) != errSecSuccess) {
[challenge.sender cancelAuthenticationChallenge:challenge];
return;
}
switch (secresult) {
case kSecTrustResultInvalid:
case kSecTrustResultDeny:
case kSecTrustResultFatalTrustFailure:
case kSecTrustResultOtherError:
case kSecTrustResultRecoverableTrustFailure: {
// !!! It's always kSecTrustResultRecoverableTrustFailure, aka 5
NSLog(@"Failing due to result: %lu", secresult);
[challenge.sender cancelAuthenticationChallenge:challenge];
return;
}
case kSecTrustResultUnspecified: // The OS trusts this certificate implicitly.
case kSecTrustResultProceed: { // The user explicitly told the OS to trust it.
NSURLCredential *credential = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
return;
}
default: ;
/* It's somebody else's key. Fall through. */
}
/* The server sent a key other than the trusted key. */
[connection cancel];
// Perform other cleanup here, as needed.
} else {
NSLog(@"In weird space... not handling authentication method: %@", [protectionSpace authenticationMethod]);
[connection cancel];
}
}
我总是得到kSecTrustResultRecoverableTrustFailure作为结果.我不认为这是localhost问题,因为我已经尝试使用Apple的代码来改变它.该怎么办?
谢谢!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Frida应用基础及APP https证书验证破解
- Apache Tomcat Native 1.2.17 发布,修复证书验证
- 技术讨论 | 四种绕过iOS SSL验证和证书固定的方法
- 使用SPIRE(自动)提供TLS证书给Envoy以进行更强大的身份验证
- RHCE证书,CCNA证书
- DV SSL证书和其他证书有什么不同?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
第一本Docker书 修订版
詹姆斯·特恩布尔 (James Turnbull) / 李兆海、刘斌、巨震 / 人民邮电出版社 / 2016-4-1 / CNY 59.00
Docker是一个开源的应用容器引擎,开发者可以利用Docker打包自己的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。 本书由Docker公司前服务与支持副总裁James Turnbull编写,是Docker开发指南。本书专注于Docker 1.9及以上版本,指导读者完成Docker的安装、部署、管理和扩展,带领读者经历从测试到生产的整个开发生......一起来看看 《第一本Docker书 修订版》 这本书的介绍吧!