NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSData *iosTrustedCertDerData = [NSData dataWithContentsOfFile:[bundle pathForResource:@"keytool_crt" ofType:@"der"]]; CFDataRef myCertData = (__bridge_retained CFDataRef)iosTrustedCertDerData; assert(myCertData); SecCertificateRef myCert; myCert = SecCertificateCreateWithData(NULL, myCertData); SecPolicyRef myPolicy = SecPolicyCreateBasicX509(); SecCertificateRef certArray[1] = { myCert }; CFArrayRef myCerts = CFArrayCreate( NULL, (void *)certArray, 1, NULL); NSMutableDictionary *SSLOptions = [NSMutableDictionary dictionaryWithCapacity:3]; [SSLOptions setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsExpiredRoots]; [SSLOptions setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsExpiredCertificates]; [SSLOptions setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot]; [SSLOptions setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCFStreamSSLValidatesCertificateChain]; [SSLOptions setObject:@"localhost:4444" forKey:(NSString *)kCFStreamSSLPeerName]; [SSLOptions setObject:(NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL forKey:(NSString*)kCFStreamSSLLevel]; [SSLOptions setObject:(NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL forKey:(NSString*)kCFStreamPropertySocketSecurityLevel]; [SSLOptions setObject:(__bridge id)(myCerts) forKey:(NSString *)kCFStreamSSLCertificates]; [SSLOptions setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCFStreamSSLIsServer]; SecTrustRef myTrust; OSStatus status = SecTrustCreateWithCertificates( myCerts, myPolicy, &myTrust); SecTrustResultType trustResult; if (status == noErr) { status = SecTrustEvaluate(myTrust, &trustResult); } NSLog(@"Status: %d", status); if (myPolicy){ CFRelease(myPolicy); } CFReadStreamRef readStream; CFWriteStreamRef writeStream; NSLog(@"Trying to connect to server"); CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", //REMOTE SERVER IP ADDRESS , 4444, &readStream, &writeStream); NSLog(@"Connection established"); CFReadStreamSetProperty(readStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelTLSv1); CFReadStreamOpen(readStream); CFWriteStreamOpen(writeStream); NSInputStream *inputStream = (__bridge NSInputStream *)readStream; NSOutputStream *outputStream = (__bridge NSOutputStream *)writeStream; [inputStream setDelegate:self]; [outputStream setDelegate:self]; // [inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; NSLog(@"Open input and output streams"); [inputStream open]; [outputStream open]; NSLog(@"Input and output streams opened"); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)