如何使iPhoneHTTPServer安全服务器(How to make iPhoneHTTPSer

2019-06-25 21:42发布

我是很新的iPhone开发。

我下载了波纹管链接iPhoneHTTPServer应用。 https://github.com/robbiehanson/CocoaHTTPServer/tree/master/Samples/iPhoneHTTPServer

它工作正常的HTTP请求。

现在,我希望把它作为一个安全的服务器。 (使用HTTPS)的,我已经覆盖按照MyHTTPConnection.m两种方法

我可以肯定在这个方法的变化:

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

我需要应用在波纹管方法的变化:(请指引我在这里。)问题:DDKeychain和Cocoa.h不适用于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;
  }

Answer 1:

我已经解决了与下面的步骤问题:

  1. 从您的钥匙串访问导出证书(Mac OS X中)
    • 打开钥匙串访问
    • 选择证书,点击右键并选择Export ...
    • 导出证书与文件格式:个人信息交换(或.p12)
    • 提供名和密码来导出文件。
      文件名:TestCertificate.p12
      密码:test123(*试试你的管理员登录通行证,如果没有工作)

  2. 进口TestCertificate.p12在你的XCode项目。

  3. 在项目中添加Security.framework。

  4. 进口Security.h文件在你的代码。

    #import <Security/Security.h>

  5. 重写和变更sslIdentityAndCertificates方法波纹管。

    /**
     * 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;    
    }



文章来源: How to make iPhoneHTTPServer secure server