ObjFW  Check-in [7361cac1f1]

Overview
Comment:OFASN1BitString: Fix unused bits

This was misunderstood to be the number of used bits when it's the
number of unused bits.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7361cac1f1cfd3e301b8039a781ac88355818087842201632149951c9d520861
User & Date: js on 2019-06-25 23:29:22
Other Links: manifest | tags
Context
2019-06-26
18:26
Use EOPNOTSUPP for SOCKS5 error code 7 check-in: f6b58b80a8 user: js tags: trunk
2019-06-25
23:29
OFASN1BitString: Fix unused bits check-in: 7361cac1f1 user: js tags: trunk
20:53
Add OFASN1DERRepresentation protocol check-in: cbaae715ce user: js tags: trunk
Changes

Modified src/OFASN1BitString.m from [eada094776] to [060f7fe7bc].

63
64
65
66
67
68
69
70

71
72
73
74
75
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
63
64
65
66
67
68
69

70
71
72
73
74
75
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







-
+









-
+


-
+



-
+
+

-
+


-
+
-






-
-
+
+







	      DEREncodedContents: (OFData *)DEREncodedContents
{
	void *pool = objc_autoreleasePoolPush();
	OFData *bitStringValue;
	size_t bitStringLength;

	@try {
		unsigned char lastByteBits;
		unsigned char unusedBits;
		size_t count = DEREncodedContents.count;

		if (tagClass != OF_ASN1_TAG_CLASS_UNIVERSAL ||
		    tagNumber != OF_ASN1_TAG_NUMBER_BIT_STRING || constructed)
			@throw [OFInvalidArgumentException exception];

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

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

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

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

		if (SIZE_MAX / 8 < count - 1 ||
		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);
		if (unusedBits != 0)
			bitStringLength -= unusedBits;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithBitStringValue: bitStringValue
			    bitStringLength: bitStringLength];
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
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







+
-
+



-
+




-
+


-
+
+








	[super dealloc];
}

- (OFData *)ASN1DERRepresentation
{
	size_t bitStringValueCount = [_bitStringValue count];
	size_t roundedUpLength = OF_ROUND_UP_POW2(8, _bitStringLength);
	unsigned char lastByteBits = _bitStringLength % 8;
	unsigned char unusedBits = roundedUpLength - _bitStringLength;
	unsigned char header[] = {
		OF_ASN1_TAG_NUMBER_BIT_STRING,
		bitStringValueCount + 1,
		lastByteBits
		unusedBits
	};
	OFMutableData *data;

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

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

	[data makeImmutable];

Modified tests/OFASN1DERRepresentationTests.m from [4fc2b56703] to [256530b0fd].

28
29
30
31
32
33
34
35

36
37

38
39
40
41
42
43
44
28
29
30
31
32
33
34

35
36

37
38
39
40
41
42
43
44







-
+

-
+







	OFData *data;

	module = @"OFASN1BitString";
	TEST(@"-[ASN1DERRepresentation]",
	    (data = [OFData dataWithItems: "\xFF\x00\xF8"
				    count: 3]) &&
	    [[[OFASN1BitString bitStringWithBitStringValue: data
					   bitStringLength: 20]
					   bitStringLength: 21]
	    ASN1DERRepresentation] isEqual:
	    [OFData dataWithItems: "\x03\x04\x04\xFF\x00\xF8"
	    [OFData dataWithItems: "\x03\x04\x03\xFF\x00\xF8"
			    count: 6]] &&
	    (data = [OFData dataWithItems: "abcdefäöü"
				    count: 12]) &&
	    [[[OFASN1BitString bitStringWithBitStringValue: data
					   bitStringLength: 12 * 8]
	    ASN1DERRepresentation] isEqual:
	    [OFData dataWithItems: "\x03\x0D\x00" "abcdefäöü"
57
58
59
60
61
62
63






64
65
66
67
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73







+
+
+
+
+
+




	    ASN1DERRepresentation] isEqual:
	    [OFData dataWithItems: "\x01\x01\x00"
			    count: 3]] &&
	    [[[OFASN1Boolean booleanWithBooleanValue: true]
	    ASN1DERRepresentation] isEqual:
	    [OFData dataWithItems: "\x01\x01\xFF"
			    count: 3]])

	module = @"OFNull";
	TEST(@"-[OFASN1DERRepresentation]",
	    [[[OFNull null] ASN1DERRepresentation] isEqual:
	    [OFData dataWithItems: "\x05\x00"
			    count: 2]])

	[pool drain];
}
@end

Modified tests/OFASN1DERValueTests.m from [2a6ef23950] to [309814cb97].

100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
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.bitStringLength == 95 &&
	    (bitString = [[OFData dataWithItems: "\x03\x81\x80\x00xxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
						 "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
					  count: 131] ASN1DERValue]) &&
	    [bitString.bitStringValue