Factored out the name accessors of MYParsedCertificate into a new class MYCertificateName, so that both subject and issuer can be accessed. A bit of other cleanup too.
5 // Created by Jens Alfke on 5/28/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
15 - (id) initWithComponents: (const UInt32*)components count: (unsigned)count
19 _data = [[NSData alloc] initWithBytes: components length: count*sizeof(UInt32)];
24 - (id) initWithBEREncoding: (NSData*)encoding
28 size_t len = encoding.length;
29 const UInt8 *src = encoding.bytes;
30 const UInt8 *end = src+len;
31 NSMutableData *data = [NSMutableData dataWithLength: (len+1)*sizeof(UInt32)];
32 UInt32* dst = data.mutableBytes;
48 component = (component << 7) | (byte & 0x7F);
52 data.length = (UInt8*)dst - (UInt8*)data.mutableBytes;
58 + (MYOID*) OIDWithEncoding: (NSData*)encoding {
59 return [[[self alloc] initWithBEREncoding: encoding] autorelease];
63 - (id) copyWithZone: (NSZone*)zone {
74 - (NSString*) description {
75 NSMutableString *desc = [NSMutableString stringWithString: @"{"];
76 const UInt32* components = self.components;
77 unsigned count = self.componentCount;
78 for (unsigned i=0; i<count; i++) {
80 [desc appendString: @" "];
81 [desc appendFormat: @"%u", components[i]];
83 [desc appendString: @"}"];
88 - (NSData*) componentData {return _data;}
89 - (const UInt32*) components {return (const UInt32*)_data.bytes;}
90 - (unsigned) componentCount {return _data.length / sizeof(UInt32);}
96 - (BOOL)isEqual:(id)object {
97 return [object isKindOfClass: [MYOID class]] && [_data isEqual: [object componentData]];
101 - (NSData*) DEREncoding {
102 unsigned count = self.componentCount;
103 UInt8 encoding[5*count]; // worst-case size
104 const UInt32 *src=self.components, *end=src+count;
105 UInt8 *dst = encoding;
106 if (count >= 2 && src[0]<=3 && src[1]<40) {
107 // Weird collapsing of 1st two components into one byte:
108 *dst++ = src[0]*40 + src[1];
112 UInt32 component = *src++;
113 // Write the component in 7-bit groups, most significant first:
115 for (int shift=28; shift>=0; shift -= 7) {
116 UInt8 byte = (component >> shift) & 0x7F;
125 return [NSData dataWithBytes: encoding length: dst-encoding];
133 #define $data(BYTES...) ({const uint8_t bytes[] = {BYTES}; [NSData dataWithBytes: bytes length: sizeof(bytes)];})
135 #define $components(INTS...) ({const UInt32 components[] = {INTS}; components;})
138 CAssertEqual([[MYOID OIDWithEncoding: $data(0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01)] description],
139 @"{1 2 840 113549 1 1 1}");
140 CAssertEqual([[MYOID OIDWithEncoding: $data(0x55,0x04,0x04)] description],
142 CAssertEqual([[MYOID OIDWithEncoding: $data(0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x01)] description],
143 @"{1 2 840 113549 1 9 1}");
145 CAssertEqual([[[MYOID alloc] initWithComponents: $components(1,2,840,113549,1,1,1) count: 7] description],
146 @"{1 2 840 113549 1 1 1}");
148 CAssertEqual([[[MYOID alloc] initWithComponents: $components(1,2,840,113549,1,1,1) count: 7] DEREncoding],
149 $data(0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01));
150 CAssertEqual([[[MYOID alloc] initWithComponents: $components(2,5,4,4) count: 4] DEREncoding],
151 $data(0x55,0x04,0x04));