ObjFW  Check-in [ea6e0818b0]

Overview
Comment:Add -[OFASN1BitString DEREncodedValue]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ea6e0818b0e5e63f554bfb7b44d6a8f0b6731b516d7ca3a8e12fde7b0b260ae1
User & Date: js on 2019-06-23 22:53:38
Other Links: manifest | tags
Context
2019-06-25
20:09
runtime: Don't use static selectors check-in: 3b3729a316 user: js tags: trunk
2019-06-23
22:53
Add -[OFASN1BitString DEREncodedValue] check-in: ea6e0818b0 user: js tags: trunk
16:00
objfw-compile: Add --help check-in: dedb037552 user: js tags: trunk
Changes

Modified src/OFASN1BitString.h from [ec48849d8d] to [0137a88b8d].

37
38
39
40
41
42
43





44
45
46
47
48
49
50
@property (readonly, nonatomic) OFData *bitStringValue;

/*!
 * @brief The length of the BitString in bits.
 */
@property (readonly, nonatomic) size_t bitStringLength;






/*!
 * @brief Creates an ASN.1 BitString with the specified BitString value and
 *	  length.
 *
 * @param bitStringValue The value of the BitString
 * @param bitStringLength The length of the BitString in bits
 * @return A new, autoreleased OFASN1BitString







>
>
>
>
>







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@property (readonly, nonatomic) OFData *bitStringValue;

/*!
 * @brief The length of the BitString in bits.
 */
@property (readonly, nonatomic) size_t bitStringLength;

/*!
 * @brief The BitString in DER encoding.
 */
@property (readonly, nonatomic) OFData *DEREncodedValue;

/*!
 * @brief Creates an ASN.1 BitString with the specified BitString value and
 *	  length.
 *
 * @param bitStringValue The value of the BitString
 * @param bitStringLength The length of the BitString in bits
 * @return A new, autoreleased OFASN1BitString

Modified src/OFASN1BitString.m from [1e12f722e3] to [e3b3ec7fe0].

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- (instancetype)initWithBitStringValue: (OFData *)bitStringValue
		       bitStringLength: (size_t)bitStringLength
{
	self = [super init];

	@try {
		if (bitStringValue.count * bitStringValue.itemSize !=
		    bitStringLength / 8)
			@throw [OFInvalidFormatException exception];

		_bitStringValue = [bitStringValue copy];
		_bitStringLength = bitStringLength;
	} @catch (id e) {
		[self release];
		@throw e;







|







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- (instancetype)initWithBitStringValue: (OFData *)bitStringValue
		       bitStringLength: (size_t)bitStringLength
{
	self = [super init];

	@try {
		if (bitStringValue.count * bitStringValue.itemSize !=
		    OF_ROUND_UP_POW2(8, bitStringLength) / 8)
			@throw [OFInvalidFormatException exception];

		_bitStringValue = [bitStringValue copy];
		_bitStringLength = bitStringLength;
	} @catch (id e) {
		[self release];
		@throw e;
76
77
78
79
80
81
82






83
84
85
86
87
88
89
90
91
92



93
94
95
96
97
98
99

		if (DEREncodedContents.itemSize != 1 || count == 0)
			@throw [OFInvalidFormatException exception];

		lastByteBits =
		    *(unsigned char *)[DEREncodedContents itemAtIndex: 0];







		if (count == 1 && lastByteBits != 0)
			@throw [OFInvalidFormatException exception];

		if (SIZE_MAX / 8 < count - 1 ||
		    SIZE_MAX - (count - 1) * 8 < lastByteBits)
			@throw [OFOutOfRangeException exception];

		bitStringLength = (count - 1) * 8 + lastByteBits;
		bitStringValue = [[DEREncodedContents
		    subdataWithRange: of_range(1, count - 1)] copy];



	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithBitStringValue: bitStringValue
			    bitStringLength: bitStringLength];







>
>
>
>
>
>







|


>
>
>







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

		if (DEREncodedContents.itemSize != 1 || count == 0)
			@throw [OFInvalidFormatException exception];

		lastByteBits =
		    *(unsigned char *)[DEREncodedContents itemAtIndex: 0];

		if (lastByteBits > 7)
			@throw [OFInvalidFormatException exception];

		/*
		 * Can't have any bits of the last byte used if we have no byte.
		 */
		if (count == 1 && lastByteBits != 0)
			@throw [OFInvalidFormatException exception];

		if (SIZE_MAX / 8 < count - 1 ||
		    SIZE_MAX - (count - 1) * 8 < lastByteBits)
			@throw [OFOutOfRangeException exception];

		bitStringLength = (count - 1) * 8;
		bitStringValue = [[DEREncodedContents
		    subdataWithRange: of_range(1, count - 1)] copy];

		if (lastByteBits != 0)
			bitStringLength -= (8 - lastByteBits);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithBitStringValue: bitStringValue
			    bitStringLength: bitStringLength];
110
111
112
113
114
115
116






















117
118
119
120
121
122
123

- (void)dealloc
{
	[_bitStringValue release];

	[super dealloc];
}























- (bool)isEqual: (id)object
{
	OFASN1BitString *bitString;

	if (![object isKindOfClass: [OFASN1BitString class]])
		return false;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

- (void)dealloc
{
	[_bitStringValue release];

	[super dealloc];
}

- (OFData *)DEREncodedValue
{
	size_t bitStringValueCount = [_bitStringValue count];
	unsigned char lastByteBits = _bitStringLength % 8;
	unsigned char header[] = { 3, bitStringValueCount + 1, lastByteBits };
	OFMutableData *data;

	if (bitStringValueCount + 1 > UINT8_MAX ||
	    bitStringValueCount != OF_ROUND_UP_POW2(8, _bitStringLength) / 8)
		@throw [OFInvalidFormatException exception];

	data = [OFMutableData dataWithCapacity: 3 + bitStringValueCount];
	[data addItems: header
		 count: 3];
	[data addItems: [_bitStringValue items]
		 count: bitStringValueCount];

	[data makeImmutable];

	return data;
}

- (bool)isEqual: (id)object
{
	OFASN1BitString *bitString;

	if (![object isKindOfClass: [OFASN1BitString class]])
		return false;

Modified tests/OFASN1DEREncodedValueTests.m from [40295ad8ca] to [c8a78a7d4d].

21
22
23
24
25
26
27























28
29
30
31
32
33
34
35
36
37
38
39
40

static OFString *module;

@implementation TestsAppDelegate (OFASN1DEREncodedValueTests)
- (void)ASN1DEREncodedValueTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
























	module = @"OFASN1Boolean";
	TEST(@"-[DEREncodedValue]",
	    [[[OFASN1Boolean booleanWithBooleanValue: false] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x01\x01\x00"
				     count: 3]] &&
	    [[[OFASN1Boolean booleanWithBooleanValue: true] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x01\x01\xFF"
				     count: 3]])

	[pool drain];
}
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

static OFString *module;

@implementation TestsAppDelegate (OFASN1DEREncodedValueTests)
- (void)ASN1DEREncodedValueTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFData *data;

	module = @"OFASN1BitString";
	TEST(@"-[DEREncodedValue]",
	    (data = [OFData dataWithItems: "\xFF\x00\xF8"
				    count: 3]) &&
	    [[[OFASN1BitString bitStringWithBitStringValue: data
					   bitStringLength: 20] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x03\x04\x04\xFF\x00\xF8"
				     count: 6]] &&
	    (data = [OFData dataWithItems: "abcdefäöü"
				    count: 12]) &&
	    [[[OFASN1BitString bitStringWithBitStringValue: data
					   bitStringLength: 12 * 8]
	    DEREncodedValue] isEqual:
	    [OFData dataWithItems: "\x03\x0D\x00" "abcdefäöü"
			    count: 15]] &&
	    (data = [OFData dataWithItems: ""
				    count: 0]) &&
	    [[[OFASN1BitString bitStringWithBitStringValue: data
					   bitStringLength: 0] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x03\x01\x00"
				     count: 3]])

	module = @"OFASN1Boolean";
	TEST(@"-[DEREncodedValue]",
	    [[[OFASN1Boolean booleanWithBooleanValue: false] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x01\x01\x00"
				     count: 3]] &&
	    [[[OFASN1Boolean booleanWithBooleanValue: true] DEREncodedValue]
	    isEqual: [OFData dataWithItems: "\x01\x01\xFF"
				     count: 3]])

	[pool drain];
}
@end

Modified tests/OFASN1DERValueTests.m from [454df6e1d7] to [2a6ef23950].

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
					count: 0]] &&
	    bitString.bitStringLength == 0 &&
	    (bitString = [[OFData dataWithItems: "\x03\x0D\x01Hello World\x80"
					  count: 15] ASN1DERValue]) &&
	    [bitString.bitStringValue
	    isEqual: [OFData dataWithItems: "Hello World\x80"
				     count: 12]] &&
	    bitString.bitStringLength == 97 &&
	    (bitString = [[OFData dataWithItems: "\x03\x81\x80\x00xxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
					  count: 131] ASN1DERValue]) &&
	    [bitString.bitStringValue







|







100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
					count: 0]] &&
	    bitString.bitStringLength == 0 &&
	    (bitString = [[OFData dataWithItems: "\x03\x0D\x01Hello World\x80"
					  count: 15] ASN1DERValue]) &&
	    [bitString.bitStringValue
	    isEqual: [OFData dataWithItems: "Hello World\x80"
				     count: 12]] &&
	    bitString.bitStringLength == 89 &&
	    (bitString = [[OFData dataWithItems: "\x03\x81\x80\x00xxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
					  count: 131] ASN1DERValue]) &&
	    [bitString.bitStringValue