How to make iPhoneHTTPServer secure server

前端 未结 1 1044
滥情空心
滥情空心 2020-12-15 14:55

I am very new to iPhone development.

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

相关标签:
1条回答
  • 2020-12-15 15:31

    I have solved issue with following steps:

    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)

    2. Import TestCertificate.p12 in you XCode project.

    3. Add Security.framework in your project.

    4. Import Security.h file in you code.

      #import <Security/Security.h>

    5. 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;    
        }
    
    
    0 讨论(0)
提交回复
热议问题