Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -16,11 +16,10 @@ OFASN1IA5String.m \ OFASN1NumericString.m \ OFASN1ObjectIdentifier.m \ OFASN1OctetString.m \ OFASN1PrintableString.m \ - OFASN1UTF8String.m \ OFASN1Value.m \ OFApplication.m \ OFArray.m \ OFBlock.m \ OFCharacterSet.m \ DELETED src/OFASN1UTF8String.h Index: src/OFASN1UTF8String.h ================================================================== --- src/OFASN1UTF8String.h +++ src/OFASN1UTF8String.h @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "OFObject.h" -#import "OFASN1Value.h" - -OF_ASSUME_NONNULL_BEGIN - -@class OFString; - -/** - * @brief An ASN.1 UTF8String. - */ -OF_SUBCLASSING_RESTRICTED -@interface OFASN1UTF8String: OFObject -{ - OFString *_UTF8StringValue; -} - -/** - * @brief The UTF8String value. - */ -@property (readonly, nonatomic) OFString *UTF8StringValue; - -/** - * @brief The string value. - */ -@property (readonly, nonatomic) OFString *stringValue; - -/** - * @brief Creates a UTF8String with the specified string value. - * - * @param string The string value of the UTF8String - * @return A new, autoreleased OFASN1UTF8String - */ -+ (instancetype)stringWithString: (OFString *)string; - -- (instancetype)init OF_UNAVAILABLE; - -/** - * @brief Initializes an already allocated UTF8String with the specified - * string value. - * - * @param string The string value of the UTF8String - * @return An initialized OFASN1UTF8String - */ -- (instancetype)initWithString: (OFString *)string OF_DESIGNATED_INITIALIZER; - -/** - * @brief Initializes an already allocated ASN.1 UTF8String with the specified - * arguments. - * - * @param tagClass The tag class of the value's type - * @param tagNumber The tag number of the value's type - * @param constructed Whether the value if of a constructed type - * @param DEREncodedContents The DER-encoded contents octets of the value. - * @return An initialized OFASN1UTF8String - */ -- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass - tagNumber: (OFASN1TagNumber)tagNumber - constructed: (bool)constructed - DEREncodedContents: (OFData *)DEREncodedContents; -@end - -OF_ASSUME_NONNULL_END DELETED src/OFASN1UTF8String.m Index: src/OFASN1UTF8String.m ================================================================== --- src/OFASN1UTF8String.m +++ src/OFASN1UTF8String.m @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#import "OFASN1UTF8String.h" -#import "OFData.h" -#import "OFString.h" - -#import "OFInvalidArgumentException.h" - -@implementation OFASN1UTF8String -@synthesize UTF8StringValue = _UTF8StringValue; - -+ (instancetype)stringWithString: (OFString *)string -{ - return [[[self alloc] initWithString: string] autorelease]; -} - -- (instancetype)initWithString: (OFString *)string -{ - self = [super init]; - - @try { - _UTF8StringValue = [string copy]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass - tagNumber: (OFASN1TagNumber)tagNumber - constructed: (bool)constructed - DEREncodedContents: (OFData *)DEREncodedContents -{ - void *pool = objc_autoreleasePoolPush(); - OFString *UTF8String; - - @try { - if (tagClass != OFASN1TagClassUniversal || - tagNumber != OFASN1TagNumberUTF8String || constructed) - @throw [OFInvalidArgumentException exception]; - - if (DEREncodedContents.itemSize != 1) - @throw [OFInvalidArgumentException exception]; - - UTF8String = [OFString - stringWithUTF8String: DEREncodedContents.items - length: DEREncodedContents.count]; - } @catch (id e) { - [self release]; - @throw e; - } - - self = [self initWithString: UTF8String]; - - objc_autoreleasePoolPop(pool); - - return self; -} - -- (instancetype)init -{ - OF_INVALID_INIT_METHOD -} - -- (void)dealloc -{ - [_UTF8StringValue release]; - - [super dealloc]; -} - -- (OFString *)stringValue -{ - return self.UTF8StringValue; -} - -- (bool)isEqual: (id)object -{ - OFASN1UTF8String *UTF8String; - - if (object == self) - return true; - - if (![object isKindOfClass: [OFASN1UTF8String class]]) - return false; - - UTF8String = object; - - if (![UTF8String->_UTF8StringValue isEqual: _UTF8StringValue]) - return false; - - return true; -} - -- (unsigned long)hash -{ - return _UTF8StringValue.hash; -} - -- (OFString *)description -{ - return [OFString stringWithFormat: @"", - _UTF8StringValue]; -} -@end Index: src/OFData+ASN1DERParsing.m ================================================================== --- src/OFData+ASN1DERParsing.m +++ src/OFData+ASN1DERParsing.m @@ -21,16 +21,16 @@ #import "OFASN1IA5String.h" #import "OFASN1NumericString.h" #import "OFASN1ObjectIdentifier.h" #import "OFASN1OctetString.h" #import "OFASN1PrintableString.h" -#import "OFASN1UTF8String.h" #import "OFASN1Value.h" #import "OFArray.h" #import "OFNull.h" #import "OFNumber.h" #import "OFSet.h" +#import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" #import "OFTruncatedDataException.h" @@ -224,12 +224,16 @@ break; case OFASN1TagNumberEnumerated: valueClass = [OFASN1Enumerated class]; break; case OFASN1TagNumberUTF8String: - valueClass = [OFASN1UTF8String class]; - break; + if (tag & tagConstructedMask) + @throw [OFInvalidFormatException exception]; + + *object = [OFString stringWithUTF8String: contents.items + length: contents.count]; + return bytesConsumed; case OFASN1TagNumberSequence: if (!(tag & tagConstructedMask)) @throw [OFInvalidFormatException exception]; *object = parseSequence(contents, depthLimit - 1); Index: tests/OFASN1DERParsingTests.m ================================================================== --- tests/OFASN1DERParsingTests.m +++ tests/OFASN1DERParsingTests.m @@ -290,22 +290,21 @@ [[OFData dataWithItems: "\x0A\x02\x00" count: 3] objectByParsingASN1DER]) /* UTF-8 string */ TEST(@"Parsing of UTF-8 string", - [[[[OFData dataWithItems: "\x0C\x0EHällo Wörld!" + [[[OFData dataWithItems: "\x0C\x0EHällo Wörld!" count: 16] objectByParsingASN1DER] - UTF8StringValue] isEqual: @"Hällo Wörld!"] && - [[[[OFData dataWithItems: "\x0C\x81\x80xxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "xxxxxxxxxxxxxxxxxxxx" - count: 131] objectByParsingASN1DER] - UTF8StringValue] isEqual: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - @"xxxxxxxxxxx"]) + isEqual: @"Hällo Wörld!"] && + [[[OFData dataWithItems: "\x0C\x81\x80xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxx" + count: 131] objectByParsingASN1DER] + isEqual: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxx"]) EXPECT_EXCEPTION(@"Detection of out of range UTF-8 string", OFOutOfRangeException, [[OFData dataWithItems: "\x0C\x89" "\x01\x01\x01\x01\x01\x01\x01\x01\x01" @@ -341,11 +340,11 @@ [array isKindOfClass: [OFArray class]] && array.count == 0 && (array = [[OFData dataWithItems: "\x30\x09\x02\x01\x7B\x0C\x04Test" count: 11] objectByParsingASN1DER]) && [array isKindOfClass: [OFArray class]] && array.count == 2 && [[array objectAtIndex: 0] longLongValue] == 123 && - [[[array objectAtIndex: 1] stringValue] isEqual: @"Test"]) + [[array objectAtIndex: 1] isEqual: @"Test"]) EXPECT_EXCEPTION(@"Detection of truncated sequence #1", OFTruncatedDataException, [[OFData dataWithItems: "\x30\x01" count: 2] objectByParsingASN1DER]) @@ -363,11 +362,11 @@ (set = [[OFData dataWithItems: "\x31\x09\x02\x01\x7B\x0C\x04Test" count: 11] objectByParsingASN1DER]) && [set isKindOfClass: [OFSet class]] && set.count == 2 && (enumerator = [set objectEnumerator]) && [[enumerator nextObject] longLongValue] == 123 && - [[[enumerator nextObject] stringValue] isEqual: @"Test"]) + [[enumerator nextObject] isEqual: @"Test"]) EXPECT_EXCEPTION(@"Detection of invalid set", OFInvalidFormatException, [[OFData dataWithItems: "\x31\x06\x02\x01\x02\x02\x01\x01" count: 8] objectByParsingASN1DER])