如何制作iPhoneHTTPServer安全服务器 [英] How to make iPhoneHTTPServer secure server

查看:170
本文介绍了如何制作iPhoneHTTPServer安全服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发的新手。

I am very new to iPhone development.

我从bellow链接下载了iPhoneHTTPServer应用程序。
https://github.com/robbiehanson/CocoaHTTPServer/tree/master / Samples / iPhoneHTTPServer

I downloaded the iPhoneHTTPServer application from bellow link. https://github.com/robbiehanson/CocoaHTTPServer/tree/master/Samples/iPhoneHTTPServer

它适用于HTTP请求。

It works fine for HTTP request.

现在我想把它作为一个安全的服务器。 (使用HTTPS)
因为我在MyHTTPConnection.m中有以下两种方法覆盖

Now I want to make it as a secure server. (use HTTPS) for that I have override following two methods in MyHTTPConnection.m

我确信这种方法有变化:

I am sure about changes in this method:

 /**
 * Overrides HTTPConnection's method
 **/
 - (BOOL)isSecureServer
 {
    // Create an HTTPS server (all connections will be secured via SSL/TLS)
    return YES; 
 }

我需要在波纹管方法中应用更改:(请在这里指导我。)
问题:DDKeychain和Cocoa.h不适用于iOS。

I need to apply changes in bellow method: (Please guide me here.) PROBLEM : DDKeychain and Cocoa.h is not available for iOS.

 /**
  * Overrides HTTPConnection's method
  * 
  * This method is expected to returns an array appropriate for use in  
  * kCFStreamSSLCertificates SSL Settings.
  * It should be an array of SecCertificateRefs except for the first element in
  * the array, which is a SecIdentityRef.
  **/
  - (NSArray *)sslIdentityAndCertificates
  {
      NSArray *result = [DDKeychain SSLIdentityAndCertificates];
      if([result count] == 0)
      {
        [DDKeychain createNewIdentity];
        return [DDKeychain SSLIdentityAndCertificates];
      }
      return result;
  }


推荐答案

我已解决了以下问题步骤:

I have solved issue with following steps:


  1. 钥匙串访问(Mac OS X)中导出证书

    • 打开钥匙串访问

    • 选择证书,右键单击并选择导出...

    • 导出带文件格式的证书:个人信息交换(.p12)

    • 提供导出文件的名称和密码。

      FileName: TestCertificate.p12

      密码: test123 (*如果不能正常工作,请尝试管理员登录通行证)



  1. Export certificate from your Keychain Access(Mac OS X)
    • Open Keychain Access
    • Select Certificate, Right click and select Export...
    • Export Certificate with file format : Personal Information Exchange (.p12)
    • Provide name and password to export file.
      FileName: TestCertificate.p12
      Password: test123 (* try your admin login pass if not worked)

在您的XCode项目中导入 TestCertificate.p12

Import TestCertificate.p12 in you XCode project.

添加项目中的 Security.framework

在您的代码中导入 Security.h 文件。



#import< Security / Security.h>

Import Security.h file in you code.

#import <Security/Security.h>

如下所示,覆盖并更改 sslIdentityAndCertificates 方法。

Override and change sslIdentityAndCertificates method as bellow.



    /**
     * Overrides HTTPConnection's method
     * 
     * This method is expected to returns an array appropriate for use in kCFStreamSSLCertificates SSL Settings.
     * It should be an array of SecCertificateRefs except for the first element in the array, which is a SecIdentityRef.
     **/
    - (NSArray *)sslIdentityAndCertificates
    {    
        SecIdentityRef identityRef = NULL;
        SecCertificateRef certificateRef = NULL;
        SecTrustRef trustRef = NULL;

        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"TestCertificate" ofType:@"p12"];
        NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; 
        CFDataRef inPKCS12Data = (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:(id)identityRef, (id)certificateRef, nil];

        return result;    
    }

这篇关于如何制作iPhoneHTTPServer安全服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆