ios - 带有 SSL 和证书的 iPhoneHTTPServer

标签 ios objective-c ssl cocoahttpserver

我在我的项目中使用 iPhoneHTTPServer 示例以使用 HTTPServer,并托管一个带有 .ipa 的 .plist 文件来模拟临时部署。

您可能知道,自 iOS7 以来,托管文件的服务器必须受到保护,因此我尝试使用 SSL 身份验证,但失败了。

首先,服务器似乎可以正常启动,但是当我尝试像这样访问我的服务器时它失败了:

NSURL *plistUrl = [NSURL URLWithString:@"itms-services://?action=download-manifest&url=https://localhost:8080/TestMAJ2.plist"];
[[UIApplication sharedApplication] openURL:plistUrl];

我有这个错误:

NSAssert(NO, @"Security option unavailable - kCFStreamSSLLevel" @" - You must use GCDAsyncSocketSSLProtocolVersionMin & GCDAsyncSocketSSLProtocolVersionMax");

如何绕过这个错误?我试图删除 TLS 设置的 kCFStreamSSLLevel(为什么不呢?^^),但连接仍然不起作用,我有一个弹出窗口,其中显示“无法连接到本地主机”或类似的东西......

关于 SSL 身份验证,示例中的 DDKeychain 类不是很好,因为它是 Mac 的 API,所以我使用此代码:How to make iPhoneHTTPServer secure server ,证书来自 Keychain Access,它是我用于签署我的应用程序的证书。也许这不是正确的证书?还是正确的代码?您知道在 iOS 中使用 SecureHTTPServer 的一个非常简单的示例吗?

最佳答案

我的 SSL 证书与 .html 文件完美配合。我在使用 .manifest 和 .ipa 文件时遇到问题。

我为 SSL 所做的是创建实现以下内容的 MyHTTPConnection 类:

- (NSArray *)sslIdentityAndCertificates
{
   SecIdentityRef identityRef = NULL;
   SecCertificateRef certificateRef = NULL;
   SecTrustRef trustRef = NULL;
   NSString *thePath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"pfx"];
   NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
   CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
   CFStringRef password = CFSTR("test123");
   const void *keys[] = { kSecImportExportPassphrase };
   const void *values[] = { password };
   CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);

   OSStatus securityError = errSecSuccess;
   securityError =  SecPKCS12Import(inPKCS12Data, optionsDictionary, &items);
   if (securityError == 0) {
       CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
       const void *tempIdentity = NULL;
       tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
       identityRef = (SecIdentityRef)tempIdentity;
       const void *tempTrust = NULL;
       tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
       trustRef = (SecTrustRef)tempTrust;
   } else {
       NSLog(@"Failed with error code %d",(int)securityError);
       return nil;
  }

   SecIdentityCopyCertificate(identityRef, &certificateRef);
   NSArray *result = [[NSArray alloc] initWithObjects:(__bridge id)identityRef, (__bridge id)certificateRef, nil];

   return result;
}

然后在 App Delegate 中,我将这个自定义连接类设置为 httpServer:

 [httpServer setConnectionClass:[MyHTTPConnection class]];

您需要做的最后一件事是将 server.pfx 证书添加到包中。我在 this tutorial 之后创建了证书.我只是做了,直到你有 server.crt 文件。然后我在 this website 中将它转换为 pfx12 .我这样做是因为 p12 证书不起作用,我有 this problem .

Chris,你能否编辑一下你的条目,解释一下你是如何做到这一点的。我想做和你完全一样的事情,但我遇到了你需要打开的 URL 的问题。

关于ios - 带有 SSL 和证书的 iPhoneHTTPServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422464/

相关文章:

ios - 在 iOS 应用程序中退出 Facebook

ios - 如何从放置在 UIAlertController 上的 UITextField 的处理程序访问放置在 UIAlertController 上的按钮?

iphone - "release"如何在 ARC 上创建一个 UIWebView

ios - 如何在 iOS Swift 中获取应用程序的 CFBundleURLSchemes

ios - 如何使用多个数组设置 for 循环作为变量以用作 Swift 2 中标记中的文本

iOS模拟器安装应用失败

ios - UIButton 颜色变化

http - 如何将https代理到http并启用ssl?

WCF SSL 证书身份验证无法识别配置设置

java - 无法从 Java 在 SSL 下访问 Webservice