将 SSL 添加到 iOS 应用程序同步
我正在使用 NSStream 将 Mac 应用程序与 iPhone 应用程序同步,并尝试使用 SSL 加密通信。我尝试在 iPhone 端运行 CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
并在 Mac 上运行 CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
当我分别设置 NSInputStream 和 NSOutputStream 时。对于设置字典,我遵循 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);
I'm syncing my Mac application with my iPhone application using NSStream and am trying to encrypt the communication with SSL. I've tried to run CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
on the iPhone side and CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings)
on the Mac side when I set up the NSInputStream and NSOutputStream respectively. For the settings dictionary, I'm following the advice of http://iphonedevelopment.blogspot.com/2010/05/nsstream-tcp-and-ssl.html and ignoring the properties of the certificate. However, encrypting it in this way doesn't seem to work as the transfer does not go through - is there something else I need to do to get this functionality working?
Thanks!
EDIT: Here's some code:
On the 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];
On the 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定确切您的问题是什么。但有几件事对我来说是个警示。
CFReadStreamRef readStream;
声明它。我会查看文档 查看如何正确创建一个。总而言之,您似乎遗漏了很多部分,因此希望其中一些资源可以帮助您走上正轨:Mac-iPhone 与 python 服务器通信, Apple 的 Bonjour 文档 和 相关 SO 帖子的答案,其中包含一些很好的链接< /a>.
I'm not sure exactly what your problem is. But there are several things that raise flags for me.
CFReadStreamRef readStream;
. I would look at the documentation to see how to properly create one.All in all it seems like you're missing a lot of pieces, so hopefully some of these resources can help you get on track: Mac-iPhone communication with a python server, Apple's Bonjour documentation, and an answer on a related SO post with some good links.