iOS Openssl套接字连接

时间:2019-07-16 00:16:04

标签: objective-c openssl

我正在尝试通过证书验证来连接到本地服务器。从服务器获取奇怪的响应消息,我不知道如何解析它们。

我与服务器使用的是同一个wifi,可以在终端上获得响应

openssl s_client -connect 18.21.19.162:9000

我尝试为NSStreamSocketSecurityLevelKey设置不​​同的安全级别。尝试为SSLSettings添加不同的值。

-(void)setSSL
{
    NSLog(@"setting SSL");

    NSDictionary *sslSettings = [ [NSDictionary alloc]initWithObjectsAndKeys:
                                 [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
                                 [NSNumber numberWithBool:NO], kCFStreamSSLIsServer,
                                 nil ];
    CFReadStreamSetProperty((CFReadStreamRef)inputStream,kCFStreamPropertySSLSettings, (CFTypeRef)sslSettings);
    CFWriteStreamSetProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)sslSettings);

}

- (void)open {

    NSLog(@"Opening streams.");

    outputStream = (__bridge NSOutputStream *)writeStream;
    inputStream = (__bridge NSInputStream *)readStream;

    [outputStream setDelegate:self];
    [inputStream setDelegate:self];

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];

    [outputStream open];
    [inputStream open];

    NSLog(@"connected");
}

- (IBAction)connectBtnEvent:(id)sender {

   NSString *ip = @"18.21.19.162";
   NSString *port = @"9000";

    NSLog(@"Setting up connection to %@ : %i", ip, [port intValue]);
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) ip, [port intValue], &readStream, &writeStream);

    [self open];
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

    NSLog(@"stream event %lu", streamEvent);

    receivedData = @"";

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
        {
            NSLog(@"Stream opened");

            [self setSSL];
        }

            break;
        case NSStreamEventHasBytesAvailable:
        {
            //Trust object is available here
            NSLog(@"has byte available");
            if (theStream == inputStream)
            {
                uint8_t buffer[2048];
                NSInteger len;

                while ([inputStream hasBytesAvailable])
                {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0)
                    {
                       NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                       // NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];

                        if(receivedData == nil || receivedData.length == 0)
                        {
                            receivedData = output;
                        }
                        else
                        {
                            receivedData = [receivedData stringByAppendingString:output];
                        }
                        if (output != nil)
                        {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }

                NSLog(@"receieved data: %@", receivedData);
            }
        }


            break;

        case NSStreamEventHasSpaceAvailable:
        {
            //Trust object is available here
             NSLog(@"Stream has space available");

        }

            break;

        case NSStreamEventErrorOccurred:
        {
            NSLog(@"has error occured: %@",[theStream streamError].localizedDescription);
        }

            break;

        case NSStreamEventEndEncountered:
        {
            [self close];

            NSLog(@"end encounter");
        }

            break;
        default:
            NSLog(@"Unknown event");
            break;
    }

}

我希望看到一个标识性的握手字符串。

但是我得到了以下输出。服务器使用\ ^ B的响应,不确定是什么原因,或者我没有正确解析它。

2019-07-15 17:01:06.180575-0700 ProxyTest[3222:881204] Opening streams.
2019-07-15 17:01:06.186495-0700 ProxyTest[3222:881204] connected
2019-07-15 17:01:06.194703-0700 ProxyTest[3222:881204] stream event 1
2019-07-15 17:01:06.194795-0700 ProxyTest[3222:881204] Stream opened
2019-07-15 17:01:06.194822-0700 ProxyTest[3222:881204] setting SSL
2019-07-15 17:01:06.195147-0700 ProxyTest[3222:881204] stream event 1
2019-07-15 17:01:06.195197-0700 ProxyTest[3222:881204] Stream opened
2019-07-15 17:01:06.195218-0700 ProxyTest[3222:881204] setting SSL
2019-07-15 17:01:06.265101-0700 ProxyTest[3222:881204] stream event 4
2019-07-15 17:01:06.265194-0700 ProxyTest[3222:881204] Stream has space available
2019-07-15 17:01:06.266564-0700 ProxyTest[3222:881204] stream event 2
2019-07-15 17:01:06.266639-0700 ProxyTest[3222:881204] has byte available
2019-07-15 17:01:06.266703-0700 ProxyTest[3222:881204] server said: \^B
2019-07-15 17:01:06.266801-0700 ProxyTest[3222:881204] message got it: \^B
2019-07-15 17:01:06.266841-0700 ProxyTest[3222:881204] \^B
2019-07-15 17:01:06.266948-0700 ProxyTest[3222:881204] receieved data: \^B
2019-07-15 17:01:11.331533-0700 ProxyTest[3222:881204] stream event 2
2019-07-15 17:01:11.331686-0700 ProxyTest[3222:881204] has byte available
2019-07-15 17:01:11.331799-0700 ProxyTest[3222:881204] receieved data:
2019-07-15 17:01:11.331912-0700 ProxyTest[3222:881204] stream event 16
2019-07-15 17:01:11.331973-0700 ProxyTest[3222:881204] Closing streams.
2019-07-15 17:01:11.332337-0700 ProxyTest[3222:881204] Disconnected
2019-07-15 17:01:11.332390-0700 ProxyTest[3222:881204] end encounter

1 个答案:

答案 0 :(得分:0)

为自己找到答案。我只需要将它们存储到NSMutableData中,然后将NSMutableData转换为十六进制并将十六进制转换回可读的字符串即可。我能够看到标识字符串。