我正在尝试使用基于 TCP/IP 的 TLS 将 iOS 应用程序连接到 Windows C# 服务器。
TLS 连接正在使用不受信任的证书,该证书是使用makecert 实用程序从不受信任的 CA 根证书创建的。
为了测试这些证书,我创建了一个简单的 C# 客户端,并使用这些证书连接服务器并与之通信。
我不擅长 iOS 开发,但我确实设法找到了一些将我连接到服务器的代码,如下所示:
-(bool)CreateAndConnect:(NSString *) remoteHost withPort:(NSInteger) serverPort
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(remoteHost),
serverPort, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertySocketSecurityLevel,
kCFStreamSocketSecurityLevelNegotiatedSSL);
NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
// load certificate from servers exported p12 file
NSArray *certificates = [[NSArray alloc] init];
[self loadClientCertificates:certificates];
NSDictionary *sslSettings = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanFalse, (id)kCFStreamSSLValidatesCertificateChain,
certificates,(id)kCFStreamSSLCertificates,
nil];
[inputStream setProperty:sslSettings forKey:(__bridge NSString *)kCFStreamPropertySSLSettings];
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
return true;
}
该代码似乎还执行某种形式的 TLS 协商,因为如果未将 p12 证书作为 NSStream 设置的一部分提供,C# 服务器将拒绝连接。
所以看起来 TLS 协商的第一阶段正在工作。
为了验证服务器证书,我有这个函数,它被 NSStream 委托(delegate)在 NSStreamEventHasSpaceAvailable 事件上调用:
// return YES if certificate verification is successful, otherwise NO
-(BOOL) VerifyCertificate:(NSStream *)stream
{
NSData *trustedCertData = nil;
BOOL result = NO;
SecTrustRef trustRef = NULL;
NSString *root_certificate_name = @"reference_cert";
NSString *root_certificate_extension = @"der";
/* Load reference cetificate */
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
trustedCertData = [NSData dataWithContentsOfFile:[bundle pathForResource: root_certificate_name ofType: root_certificate_extension]];
/* get trust object */
/* !!!!! error is here as trustRef is NULL !!!! */
trustRef = (__bridge SecTrustRef)[stream propertyForKey:(__bridge id)kCFStreamPropertySSLPeerTrust];
/* loacate the reference certificate */
NSInteger numCerts = SecTrustGetCertificateCount(trustRef);
for (NSInteger i = 0; i < numCerts; i++) {
SecCertificateRef secCertRef = SecTrustGetCertificateAtIndex(trustRef, i);
NSData *certData = CFBridgingRelease(SecCertificateCopyData(secCertRef));
if ([trustedCertData isEqualToData: certData]) {
result = YES;
break;
}
}
return result;
}
现在的问题是,无论我尝试什么,trustRef 对象始终为 null。
有这句话表明情况不应该是这样的:
By the time your stream delegate’s event handler gets called to indicate that there is space available on the socket, the operating system has already constructed a TLS channel, obtained a certificate chain from the other end of the connection, and created a trust object to evaluate it.
关于如何解决这个问题的任何提示?
我怎样才能访问 NSStream 的 trustRef 对象?
编辑:
感谢 100phole 的回复。
在尝试让它工作时,我认为这可能与问题有关,在我的许多尝试中,我将所有这些与套接字相关的项目移到了一个类中:
像这样:
@interface Socket
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
NSInputStream *inputStream;
NSOutputStream *outputStream;
@end
但这得出了相同的结果:(
我只是恢复到上面显示的版本,因为根据我的 Google 搜索,这似乎是一个相当常见的代码模式。
例如,即使是来自 Apple Developer 网站的这段代码也使用了非常相似的风格:
正如我之前提到的,我不是 Objective-C 方面的专家(远不是它),所以我可能是错的,但从我所看到的,将这些项目移到一个类中并让它们持久化似乎并没有有所作为。
最佳答案
由于似乎有人对这个问题感兴趣,所以我决定用一个答案来更新问题,并详细说明这个问题最终是如何解决的。
首先是一些背景。我从以前的开发人员那里继承了这段代码,我的职责是让损坏的代码正常工作。
我花了很多时间使用 Apple iOS 开发人员网页中的详细信息编写和重写连接代码,但似乎没有任何效果。
我最终决定仔细研究这个函数,我继承并错误地假设的代码正在运行:
[self loadClientCertificates:certificates];
乍一看,代码看起来没问题。该函数只是从文件中加载证书。但是仔细检查,虽然代码正确加载了证书,但它没有将这些证书返回给调用者!!!
修复该代码以使其正确返回证书后,连接代码工作正常并且 SecTrustRef 不再为 NULL。
总结:
1) Apple 文档虽然缺少很好的示例,但看起来确实准确。
2) SecTrustRef 为 NULL 的原因是因为在连接协商阶段找不到有效的证书,这是因为没有证书可供连接 API 由于前面提到的编码错误。
3) 如果您看到类似的错误,我的建议是检查并仔细检查您的代码,因为正如预期的那样,等式的 iOS 端按照文档工作。
关于iOS SecTrustRef 始终为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33163306/