ObjFW  Check-in [cfed9215e4]

Overview
Comment:Add ASN.1 Object Identifier type
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cfed9215e4d2302d94bdd1f61ff526770ae9c0131e96eb7545ca1f0e633062f0
User & Date: js on 2018-10-14 22:41:52
Other Links: manifest | tags
Context
2018-10-16
00:07
Remove intermediate OFASN1IntegerOrEnumerated check-in: 52b963d843 user: js tags: trunk
2018-10-14
22:41
Add ASN.1 Object Identifier type check-in: cfed9215e4 user: js tags: trunk
15:39
Add ASN.1 enumerated type check-in: 37a8ddc12a user: js tags: trunk
Changes

Modified src/Makefile from [1df459301d] to [57e9628c1d].

14
15
16
17
18
19
20

21
22
23
24
25
26
27
       OFASN1Boolean.m			\
       OFASN1Enumerated.m		\
       OFASN1IA5String.m		\
       OFASN1Integer.m			\
       OFASN1IntegerOrEnumerated.m	\
       OFASN1Null.m			\
       OFASN1NumericString.m		\

       OFASN1OctetString.m		\
       OFASN1PrintableString.m		\
       OFASN1UTF8String.m		\
       OFASN1Value.m			\
       OFApplication.m			\
       OFArray.m			\
       OFAutoreleasePool.m		\







>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
       OFASN1Boolean.m			\
       OFASN1Enumerated.m		\
       OFASN1IA5String.m		\
       OFASN1Integer.m			\
       OFASN1IntegerOrEnumerated.m	\
       OFASN1Null.m			\
       OFASN1NumericString.m		\
       OFASN1ObjectIdentifier.m		\
       OFASN1OctetString.m		\
       OFASN1PrintableString.m		\
       OFASN1UTF8String.m		\
       OFASN1Value.m			\
       OFApplication.m			\
       OFArray.m			\
       OFAutoreleasePool.m		\

Added src/OFASN1ObjectIdentifier.h version [24fd141231].















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018
 *   Jonathan Schleifer <js@heap.zone>
 *
 * 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 "OFASN1Value.h"

OF_ASSUME_NONNULL_BEGIN

@class OFArray OF_GENERIC(ObjetType);
@class OFNumber;

/*!
 * @brief An ASN.1 Object Identifier.
 */
@interface OFASN1ObjectIdentifier: OFASN1Value
{
	OFArray OF_GENERIC(OFNumber *) *_subidentifiers;
}

/*!
 * @brief The subidentifiers of the Object Identifier.
 */
@property (readonly, nonatomic) OFArray OF_GENERIC(OFNumber *) *subidentifiers;
@end

OF_ASSUME_NONNULL_END

Added src/OFASN1ObjectIdentifier.m version [94a9ef2a4a].





















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018
 *   Jonathan Schleifer <js@heap.zone>
 *
 * 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 "OFASN1ObjectIdentifier.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFNumber.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"

@implementation OFASN1ObjectIdentifier
@synthesize subidentifiers = _subidentifiers;

- (instancetype)initWithTagClass: (of_asn1_tag_class_t)tagClass
		       tagNumber: (of_asn1_tag_number_t)tagNumber
		     constructed: (bool)constructed
	      DEREncodedContents: (OFData *)DEREncodedContents
{
	self = [super initWithTagClass: tagClass
			     tagNumber: tagNumber
			   constructed: constructed
		    DEREncodedContents: DEREncodedContents];

	@try {
		void *pool = objc_autoreleasePoolPush();
		const unsigned char *items = [_DEREncodedContents items];
		size_t count = [_DEREncodedContents count];
		OFMutableArray *subidentifiers = [OFMutableArray array];
		uintmax_t value = 0;
		uint_fast8_t bits = 0;

		if (_tagClass != OF_ASN1_TAG_CLASS_UNIVERSAL ||
		    _tagNumber != OF_ASN1_TAG_NUMBER_OBJECT_IDENTIFIER ||
		    _constructed)
			@throw [OFInvalidArgumentException exception];

		if (count == 0)
			@throw [OFInvalidFormatException exception];

		for (size_t i = 0; i < count; i++) {
			if (bits == 0 && items[i] == 0x80)
				@throw [OFInvalidFormatException exception];

			value = (value << 7) | (items[i] & 0x7F);
			bits += 7;

			if (bits > sizeof(uintmax_t) * 8)
				@throw [OFOutOfRangeException exception];

			if (items[i] & 0x80)
				continue;

			if ([subidentifiers count] == 0) {
				if (value < 40)
					[subidentifiers addObject:
					    [OFNumber numberWithUIntMax: 0]];
				else if (value < 80) {
					[subidentifiers addObject:
					    [OFNumber numberWithUIntMax: 1]];
					value -= 40;
				} else {
					[subidentifiers addObject:
					    [OFNumber numberWithUIntMax: 2]];
					value -= 80;
				}
			}

			[subidentifiers addObject:
			    [OFNumber numberWithUIntMax: value]];

			value = 0;
			bits = 0;
		}

		if (items[count - 1] & 0x80)
			@throw [OFInvalidFormatException exception];

		[subidentifiers makeImmutable];
		_subidentifiers = [subidentifiers copy];

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

	return self;
}

- (void)dealloc
{
	[_subidentifiers release];

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"<OFASN1ObjectIdentifier: %@>",
	    [_subidentifiers componentsJoinedByString: @"."]];
}
@end

Modified src/OFASN1Value.h from [80c2788f18] to [3df22b0a2b].

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
64
65
66
67
68
69
70
71
72
73
74
} of_asn1_tag_class_t;

/*!
 * @brief ASN.1 tag number.
 */
typedef enum {
	/*! Boolean */
	OF_ASN1_TAG_NUMBER_BOOLEAN	    = 0x01,
	/*! Integer */
	OF_ASN1_TAG_NUMBER_INTEGER	    = 0x02,
	/*! Bit string */
	OF_ASN1_TAG_NUMBER_BIT_STRING	    = 0x03,
	/*! Octet string */
	OF_ASN1_TAG_NUMBER_OCTET_STRING	    = 0x04,
	/*! Null */
	OF_ASN1_TAG_NUMBER_NULL		    = 0x05,


	/*! Enumerated */
	OF_ASN1_TAG_NUMBER_ENUMERATED	    = 0x0A,
	/*! UTF-8 string */
	OF_ASN1_TAG_NUMBER_UTF8_STRING	    = 0x0C,
	/*! Sequence */
	OF_ASN1_TAG_NUMBER_SEQUENCE	    = 0x10,
	/*! Set */
	OF_ASN1_TAG_NUMBER_SET		    = 0x11,
	/*! NumericString */
	OF_ASN1_TAG_NUMBER_NUMERIC_STRING   = 0x12,
	/*! PrintableString */
	OF_ASN1_TAG_NUMBER_PRINTABLE_STRING = 0x13,
	/*! IA5String */
	OF_ASN1_TAG_NUMBER_IA5_STRING	    = 0x16
} of_asn1_tag_number_t;

/*!
 * @brief A class representing an ASN.1 value.
 */
@interface OFASN1Value: OFObject <OFCopying>
{







|

|

|

|

|
>
>

|

|

|

|

|

|

|







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
64
65
66
67
68
69
70
71
72
73
74
75
76
} of_asn1_tag_class_t;

/*!
 * @brief ASN.1 tag number.
 */
typedef enum {
	/*! Boolean */
	OF_ASN1_TAG_NUMBER_BOOLEAN	     = 0x01,
	/*! Integer */
	OF_ASN1_TAG_NUMBER_INTEGER	     = 0x02,
	/*! Bit string */
	OF_ASN1_TAG_NUMBER_BIT_STRING	     = 0x03,
	/*! Octet string */
	OF_ASN1_TAG_NUMBER_OCTET_STRING	     = 0x04,
	/*! Null */
	OF_ASN1_TAG_NUMBER_NULL		     = 0x05,
	/*! Object Identifier */
	OF_ASN1_TAG_NUMBER_OBJECT_IDENTIFIER = 0x06,
	/*! Enumerated */
	OF_ASN1_TAG_NUMBER_ENUMERATED	     = 0x0A,
	/*! UTF-8 string */
	OF_ASN1_TAG_NUMBER_UTF8_STRING	     = 0x0C,
	/*! Sequence */
	OF_ASN1_TAG_NUMBER_SEQUENCE	     = 0x10,
	/*! Set */
	OF_ASN1_TAG_NUMBER_SET		     = 0x11,
	/*! NumericString */
	OF_ASN1_TAG_NUMBER_NUMERIC_STRING    = 0x12,
	/*! PrintableString */
	OF_ASN1_TAG_NUMBER_PRINTABLE_STRING  = 0x13,
	/*! IA5String */
	OF_ASN1_TAG_NUMBER_IA5_STRING	     = 0x16
} of_asn1_tag_number_t;

/*!
 * @brief A class representing an ASN.1 value.
 */
@interface OFASN1Value: OFObject <OFCopying>
{

Modified src/OFData+ASN1DERValue.m from [bca44b6429] to [84eafb2ba8].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
#import "OFASN1BitString.h"
#import "OFASN1Boolean.h"
#import "OFASN1Enumerated.h"
#import "OFASN1IA5String.h"
#import "OFASN1Integer.h"
#import "OFASN1Null.h"
#import "OFASN1NumericString.h"

#import "OFASN1OctetString.h"
#import "OFASN1PrintableString.h"
#import "OFASN1UTF8String.h"
#import "OFASN1Value.h"
#import "OFArray.h"
#import "OFSet.h"








>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#import "OFASN1BitString.h"
#import "OFASN1Boolean.h"
#import "OFASN1Enumerated.h"
#import "OFASN1IA5String.h"
#import "OFASN1Integer.h"
#import "OFASN1Null.h"
#import "OFASN1NumericString.h"
#import "OFASN1ObjectIdentifier.h"
#import "OFASN1OctetString.h"
#import "OFASN1PrintableString.h"
#import "OFASN1UTF8String.h"
#import "OFASN1Value.h"
#import "OFArray.h"
#import "OFSet.h"

171
172
173
174
175
176
177



178
179
180
181
182
183
184
		break;
	case OF_ASN1_TAG_NUMBER_OCTET_STRING:
		valueClass = [OFASN1OctetString class];
		break;
	case OF_ASN1_TAG_NUMBER_NULL:
		valueClass = [OFASN1Null class];
		break;



	case OF_ASN1_TAG_NUMBER_ENUMERATED:
		valueClass = [OFASN1Enumerated class];
		break;
	case OF_ASN1_TAG_NUMBER_UTF8_STRING:
		valueClass = [OFASN1UTF8String class];
		break;
	case OF_ASN1_TAG_NUMBER_SEQUENCE:







>
>
>







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		break;
	case OF_ASN1_TAG_NUMBER_OCTET_STRING:
		valueClass = [OFASN1OctetString class];
		break;
	case OF_ASN1_TAG_NUMBER_NULL:
		valueClass = [OFASN1Null class];
		break;
	case OF_ASN1_TAG_NUMBER_OBJECT_IDENTIFIER:
		valueClass = [OFASN1ObjectIdentifier class];
		break;
	case OF_ASN1_TAG_NUMBER_ENUMERATED:
		valueClass = [OFASN1Enumerated class];
		break;
	case OF_ASN1_TAG_NUMBER_UTF8_STRING:
		valueClass = [OFASN1UTF8String class];
		break;
	case OF_ASN1_TAG_NUMBER_SEQUENCE:

Modified tests/OFASN1DERValueTests.m from [203d7b4c12] to [2a96ab0844].

20
21
22
23
24
25
26

27
28
29
30

31
32
33
34
35
36
37
#import "OFData.h"
#import "OFASN1BitString.h"
#import "OFASN1Boolean.h"
#import "OFASN1IA5String.h"
#import "OFASN1Integer.h"
#import "OFASN1Null.h"
#import "OFASN1NumericString.h"

#import "OFASN1OctetString.h"
#import "OFASN1PrintableString.h"
#import "OFASN1UTF8String.h"
#import "OFArray.h"

#import "OFSet.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

#import "OFInvalidEncodingException.h"







>




>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFData.h"
#import "OFASN1BitString.h"
#import "OFASN1Boolean.h"
#import "OFASN1IA5String.h"
#import "OFASN1Integer.h"
#import "OFASN1Null.h"
#import "OFASN1NumericString.h"
#import "OFASN1ObjectIdentifier.h"
#import "OFASN1OctetString.h"
#import "OFASN1PrintableString.h"
#import "OFASN1UTF8String.h"
#import "OFArray.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

#import "OFInvalidEncodingException.h"
186
187
188
189
190
191
192
193
194
195
196















































197
198
199
200
201
202
203

	/* Null */
	TEST(@"Parsing of null",
	    [[[OFData dataWithItems: "\x05\x00"
			      count: 2] ASN1DERValue]
	    isKindOfClass: [OFASN1Null class]])

	EXPECT_EXCEPTION(@"Detection of invalid NULL",
	    OFInvalidFormatException, [[OFData dataWithItems: "\x05\x01\x00"
						       count: 3] ASN1DERValue])
















































	/* Enumerated */
	TEST(@"Parsing of enumerated",
	    [[[OFData dataWithItems: "\x0A\x00"
			      count: 2] ASN1DERValue] integerValue] == 0 &&
	    [[[OFData dataWithItems: "\x0A\x01\x01"
			      count: 3] ASN1DERValue] integerValue] == 1 &&
	    [[[OFData dataWithItems: "\x0A\x02\x01\x04"







|



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







188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

	/* Null */
	TEST(@"Parsing of null",
	    [[[OFData dataWithItems: "\x05\x00"
			      count: 2] ASN1DERValue]
	    isKindOfClass: [OFASN1Null class]])

	EXPECT_EXCEPTION(@"Detection of invalid null",
	    OFInvalidFormatException, [[OFData dataWithItems: "\x05\x01\x00"
						       count: 3] ASN1DERValue])

	/* Object Identifier */
	TEST(@"Parsing of Object Identifier",
	    (array = [[[OFData dataWithItems: "\x06\x01\x27"
				       count: 3] ASN1DERValue]
	    subidentifiers]) && [array count] == 2 &&
	    [[array objectAtIndex: 0] uIntMaxValue] == 0 &&
	    [[array objectAtIndex: 1] uIntMaxValue] == 39 &&
	    (array = [[[OFData dataWithItems: "\x06\x01\x4F"
				       count: 3] ASN1DERValue]
	    subidentifiers]) && [array count] == 2 &&
	    [[array objectAtIndex: 0] uIntMaxValue] == 1 &&
	    [[array objectAtIndex: 1] uIntMaxValue] == 39 &&
	    (array = [[[OFData dataWithItems: "\x06\x02\x88\x37"
				       count: 4] ASN1DERValue]
	    subidentifiers]) && [array count] == 2 &&
	    [[array objectAtIndex: 0] uIntMaxValue] == 2 &&
	    [[array objectAtIndex: 1] uIntMaxValue] == 999 &&
	    (array = [[[OFData dataWithItems: "\x06\x09\x2A\x86\x48\x86\xF7\x0D"
					      "\x01\x01\x0B"
				       count: 11] ASN1DERValue]
	    subidentifiers]) && [array count] == 7 &&
	    [[array objectAtIndex: 0] uIntMaxValue] == 1 &&
	    [[array objectAtIndex: 1] uIntMaxValue] == 2 &&
	    [[array objectAtIndex: 2] uIntMaxValue] == 840 &&
	    [[array objectAtIndex: 3] uIntMaxValue] == 113549 &&
	    [[array objectAtIndex: 4] uIntMaxValue] == 1 &&
	    [[array objectAtIndex: 5] uIntMaxValue] == 1 &&
	    [[array objectAtIndex: 6] uIntMaxValue] == 11)

	EXPECT_EXCEPTION(@"Detection of invalid Object Identifier #1",
	    OFInvalidFormatException, [[OFData dataWithItems: "\x06\x01\x81"
						       count: 3] ASN1DERValue])

	EXPECT_EXCEPTION(@"Detection of invalid Object Identifier #2",
	    OFInvalidFormatException, [[OFData dataWithItems: "\x06\x02\x80\x01"
						       count: 4] ASN1DERValue])

	EXPECT_EXCEPTION(@"Detection of out of range Object Identifier",
	    OFOutOfRangeException,
	    [[OFData dataWithItems: "\x06\x0A\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
				    "\xFF\x7F"
			     count: 12] ASN1DERValue])

	EXPECT_EXCEPTION(@"Detection of truncated Object Identifier",
	    OFTruncatedDataException, [[OFData dataWithItems: "\x06\x02\x00"
						       count: 3] ASN1DERValue])

	/* Enumerated */
	TEST(@"Parsing of enumerated",
	    [[[OFData dataWithItems: "\x0A\x00"
			      count: 2] ASN1DERValue] integerValue] == 0 &&
	    [[[OFData dataWithItems: "\x0A\x01\x01"
			      count: 3] ASN1DERValue] integerValue] == 1 &&
	    [[[OFData dataWithItems: "\x0A\x02\x01\x04"