iOS NSOutputStream在首次签名后拆分数据

时间:2015-10-14 09:53:28

标签: ios ssl nsoutputstream tls1.2

我们目前正在开发TLS服务器客户端应用程序。为了测试客户端,发送一个包含内容" 0001"并且服务器发送答案 客户端通过NSOutputStream的write方法发送数据。当数据第一次发送时,它完全到达。当我第二次或第三次发送数据时,数据在第一个字节后被分割。服务器获得第一个" 0"而其余的" 001"。我不知道为什么会分裂。

  

通信如下:
ServerConnect
发送到服务器:   0001
从服务器接收:OK
发送到服务器:0
接收   从服务器:错误
发送到服务器:001
从服务器接收:   错误
发送到服务器:0
从服务器接收:错误发送   到服务器:001
从服务器接收:错误
ServerDisconnect   

我希望有人能帮助我。

- (void)initNetworkCommunication:(NSString*)ns_IP :(UInt32)ui_Port :(BOOL)b_ValidateCertificate
{

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)ns_IP, ui_Port, &readStream, &writeStream);

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

    [inputStream setDelegate:(id)self];
    [outputStream setDelegate:(id)self];

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

    NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSNumber numberWithBool:b_ValidateCertificate], kCFStreamSSLValidatesCertificateChain,
                          [NSNumber numberWithBool:YES], kCFStreamPropertyShouldCloseNativeSocket,
                          kCFNull,kCFStreamSSLPeerName,
                          kCFStreamSocketSecurityLevelSSLv3, kCFStreamSSLLevel,
                          nil];

    CFReadStreamSetProperty((CFReadStreamRef)inputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
    CFWriteStreamSetProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

    [inputStream open];
    [outputStream open];   
}
- (BOOL) sendData:(NSString*)ns_SendData :(NSUInteger)nui_bufferLength
{
  NSUInteger nui_length = [ns_SendData length];
  NSUInteger nui_chunkSize = nui_bufferLength;
  NSUInteger nui_offset = 0;
  long l_Result;

  NSString *ns_response  = [NSString stringWithFormat:@"%@", ns_SendData];
  NSData *nd_data = [[NSData alloc] initWithData:[ns_response dataUsingEncoding:NSASCIIStringEncoding]];

  // a  none-ASCII-symbol exists
  if ([nd_data length] == 0)
    return FALSE;

  // split the data
  do {
    NSUInteger nui_thisChunkSize = nui_length - nui_offset > nui_chunkSize ? nui_chunkSize : nui_length - nui_offset;
    NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[nd_data bytes] + nui_offset
                                         length:nui_thisChunkSize
                                   freeWhenDone:NO];
    nui_offset += nui_thisChunkSize;

    // send data to server
    l_Result = [outputStream write:[chunk bytes] maxLength:[chunk length]];
    NSLog(@"%s - %lu", __PRETTY_FUNCTION__, l_Result);

    if (l_Result == -1) {
        NSLog(@"error sending data");
        return false;
    }

  } while (nui_offset < nui_length);

  return true;
}

1 个答案:

答案 0 :(得分:0)

使用TLS 1.2解决了这个问题。请参阅AppTechnical Note TN2287

    const void* keys[] = { kCFStreamSSLValidatesCertificateChain,   kCFStreamSSLLevel };
    // New CFString values available only on iOS 5:
    // (if these values are used on versions before iOS 5, then will
    //    default to max TLS 1.0, min SSLv3)
    // kCFStreamSocketSecurityLevelTLSv1_0SSLv3 configures max TLS 1.0, min SSLv3
    // (same as default behavior on versions before iOS 5).
    // kCFStreamSocketSecurityLevelTLSv1_0 configures to use only TLS 1.0.
    // kCFStreamSocketSecurityLevelTLSv1_1 configures to use only TLS 1.1.
    // kCFStreamSocketSecurityLevelTLSv1_2 configures to use only TLS 1.2.
    const void* values[] = { CFSTR("kCFBooleanTrue"), CFSTR("kCFStreamSocketSecurityLevelTLSv1_2") };

    CFDictionaryRef sslSettingsDict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 2,
                                                         &kCFTypeDictionaryKeyCallBacks,
                                                         &kCFTypeDictionaryValueCallBacks);

    CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, sslSettingsDict);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, sslSettingsDict);
    CFRelease(sslSettingsDict);