将 SSL 添加到 iOS 应用程序同步

发布于 2024-12-01 18:44:04 字数 2617 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2024-12-08 18:44:04

我不确定确切您的问题是什么。但有几件事对我来说是个警示。

  1. 你的 Mac 端只有一个输出流,而你的 iPhone 端只有一个输入流,这似乎很可疑。我不明白如何在这样的单向流中进行 SSL 握手,尽管它可能是在幕后处理的。
  2. 在我看来,您正在尝试在两个客户端应用程序之间进行客户端-服务器通信。您的 Mac 应用程序如何提供 SSL 证书? 编辑:我现在看到您的outputStream基于某些服务对象,因此您可能正在处理此问题而只是不显示代码。
  3. 您链接到的文章中的评论表明当流写入“-1 字节”时,打开输出流时出现问题。因此,首先您需要解决 Mac 端的问题,然后再转移到 iPhone 应用程序(我相信 iPhone 应用程序也存在一些问题)。
  4. 您在 iPhone 端的读取流似乎没有正确创建。您所要做的就是使用 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.

  1. It seems fishy that your Mac side only has an output stream and your iPhone side only has an input stream. I don't see how you can have an SSL handshake in a one-way stream like that, though it's possible that it's taken care of behind the scenes.
  2. You're attempting client-server communication between what, to me, seems to be two client applications. How is your Mac app serving an SSL certificate? Edit: I now see that your outputStream is based on some service object, so it's possible you're handling this and just not showing the code.
  3. A comment in the article you linked to suggests that when a stream writes "-1 bytes", there's a problem opening the output stream. So first you will need to resolve the issues on the Mac side before moving to the iPhone app (which I believe also has some problems).
  4. It doesn't look like your read stream on the iPhone side is properly created. All you do is declare it with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文