我正在使用 NSStream 将我的 Mac 应用程序与我的 iPhone 应用程序同步,并尝试使用 SSL 加密通信。当我分别设置 NSInputStream 和 NSOutputStream 时,我尝试CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
在 iPhone 端和 Mac 端运行。CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
对于设置字典,我遵循http://iphonedevelopment.blogspot.com/2010/05/nsstream-tcp-and-ssl.html的建议并忽略证书的属性。但是,以这种方式加密它似乎不起作用,因为传输没有通过 - 我还需要做些什么来让这个功能正常工作吗?
谢谢!
编辑:这是一些代码:
在 Mac 上:
NSOutputStream *outStream;
[service getInputStream:nil outputStream:&outStream];
[outStream open];
[outStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
forKey:NSStreamSocketSecurityLevelKey];
NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
kCFNull,kCFStreamSSLPeerName, nil];
CFWriteStreamSetProperty((CFWriteStreamRef)outStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
int bytes = [outStream write:[rawPacket bytes] maxLength:[rawPacket length]];
[outStream close];
在 iPhone 上:
CFReadStreamRef readStream;
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"file"];
NSOutputStream *fileStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:NO];
[fileStream open];
CFStreamCreatePairWithSocket(NULL, fd, &readStream, NULL);
NSInputStream *networkStream = (NSInputStream *) readStream;
CFRelease(readStream);
[networkStream setProperty:(id)kCFBooleanTrue forKey:(NSString *)kCFStreamPropertyShouldCloseNativeSocket];
networkStream.delegate = self;
[networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[networkStream open];
[self.networkStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
forKey:NSStreamSocketSecurityLevelKey];
NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
kCFNull,kCFStreamSSLPeerName, nil];
CFReadStreamSetProperty((CFReadStreamRef)self.networkStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);