我想使用iOS中的代码随附的自签名证书来创建与服务器的SSL连接. 这样,我就不必担心更复杂的中间人攻击ios 自签名证书,即有人可以访问高级“受信任”证书颁发机构. 用我吧,我认为这是苹果的标准方式.
通过此处找到的程序生成证书
# 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作为结果. 我不认为这是本地主机问题ios 自签名证书,因为我尝试使用Apple的代码对其进行更改. 我该怎么办?
谢谢!
以上是本文的全部内容,希望对大家的学习有所帮助,希望您能给予大力支持.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shouji/article-175806-1.html
晚上你直接去他家就可以了
派几十艘渔船跟着漫天撒网搞死他