From 5197b9bd0b0150503824dfc68a6910116c2bf344 Mon Sep 17 00:00:00 2001 From: Hivert Quentin Date: Mon, 12 Feb 2024 11:50:35 +0100 Subject: [PATCH] fix(crypto): check if NSData is null terminated --- SoObjects/SOGo/NSString+Crypto.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/SoObjects/SOGo/NSString+Crypto.m b/SoObjects/SOGo/NSString+Crypto.m index e404efc48..1bd339d10 100644 --- a/SoObjects/SOGo/NSString+Crypto.m +++ b/SoObjects/SOGo/NSString+Crypto.m @@ -660,7 +660,16 @@ static const NSString *kAES256GCMError = @"kAES256GCMError"; if (rv > 0) { if (outputData) { - value = [NSString stringWithUTF8String: [outputData bytes]]; + char lastByte; + [outputData getBytes:&lastByte range:NSMakeRange([outputData length]-1, 1)]; + if (lastByte == 0x0) { + // string is null terminated + value = [NSString stringWithUTF8String: [outputData bytes]]; + } else { + // string is not null terminated + value = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding]; + [value autorelease]; + } } else { *ex = [NSException exceptionWithName: kAES256GCMError reason:@"Decryption ok but output empty" userInfo: nil]; }