jens@16
|
1 |
//
|
jens@16
|
2 |
// MYASN1Object.m
|
jens@16
|
3 |
// MYCrypto-iPhone
|
jens@16
|
4 |
//
|
jens@16
|
5 |
// Created by Jens Alfke on 5/28/09.
|
jens@16
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
jens@16
|
7 |
//
|
jens@16
|
8 |
|
jens@20
|
9 |
// Reference:
|
jens@20
|
10 |
// <http://www.columbia.edu/~ariel/ssleay/layman.html> "Layman's Guide To ASN.1/BER/DER"
|
jens@20
|
11 |
|
jens@16
|
12 |
#import "MYASN1Object.h"
|
jens@16
|
13 |
|
jens@16
|
14 |
|
jens@16
|
15 |
@implementation MYASN1Object
|
jens@16
|
16 |
|
jens@16
|
17 |
|
jens@16
|
18 |
- (id) initWithTag: (uint32_t)tag
|
jens@16
|
19 |
ofClass: (uint8_t)tagClass
|
jens@16
|
20 |
constructed: (BOOL)constructed
|
jens@16
|
21 |
value: (NSData*)value
|
jens@16
|
22 |
{
|
jens@16
|
23 |
Assert(value);
|
jens@16
|
24 |
self = [super init];
|
jens@16
|
25 |
if (self != nil) {
|
jens@16
|
26 |
_tag = tag;
|
jens@16
|
27 |
_tagClass = tagClass;
|
jens@16
|
28 |
_constructed = constructed;
|
jens@16
|
29 |
_value = [value copy];
|
jens@16
|
30 |
}
|
jens@16
|
31 |
return self;
|
jens@16
|
32 |
}
|
jens@16
|
33 |
|
jens@16
|
34 |
- (id) initWithTag: (uint32_t)tag
|
jens@16
|
35 |
ofClass: (uint8_t)tagClass
|
jens@16
|
36 |
components: (NSArray*)components
|
jens@16
|
37 |
{
|
jens@16
|
38 |
Assert(components);
|
jens@16
|
39 |
self = [super init];
|
jens@16
|
40 |
if (self != nil) {
|
jens@16
|
41 |
_tag = tag;
|
jens@16
|
42 |
_tagClass = tagClass;
|
jens@16
|
43 |
_constructed = YES;
|
jens@16
|
44 |
_components = [components copy];
|
jens@16
|
45 |
}
|
jens@16
|
46 |
return self;
|
jens@16
|
47 |
}
|
jens@16
|
48 |
|
jens@16
|
49 |
- (void) dealloc
|
jens@16
|
50 |
{
|
jens@16
|
51 |
[_value release];
|
jens@16
|
52 |
[_components release];
|
jens@16
|
53 |
[super dealloc];
|
jens@16
|
54 |
}
|
jens@16
|
55 |
|
jens@16
|
56 |
|
jens@16
|
57 |
@synthesize tag=_tag, tagClass=_tagClass, constructed=_constructed, value=_value, components=_components;
|
jens@16
|
58 |
|
jens@16
|
59 |
|
jens@16
|
60 |
- (NSString*)description {
|
jens@16
|
61 |
if (_components)
|
jens@16
|
62 |
return $sprintf(@"%@[%hhu/%u/%u]%@", self.class, _tagClass,(unsigned)_constructed,_tag, _components);
|
jens@16
|
63 |
else
|
jens@16
|
64 |
return $sprintf(@"%@[%hhu/%u/%u, %u bytes]", self.class, _tagClass,(unsigned)_constructed,_tag, _value.length);
|
jens@16
|
65 |
}
|
jens@16
|
66 |
|
jens@19
|
67 |
- (BOOL) isEqual: (id)object {
|
jens@19
|
68 |
return [object isKindOfClass: [MYASN1Object class]]
|
jens@19
|
69 |
&& _tag==[object tag]
|
jens@19
|
70 |
&& _tagClass==[object tagClass]
|
jens@19
|
71 |
&& _constructed==[object constructed]
|
jens@19
|
72 |
&& $equal(_value,[object value])
|
jens@19
|
73 |
&& $equal(_components,[object components]);
|
jens@19
|
74 |
}
|
jens@19
|
75 |
|
jens@16
|
76 |
static void dump(id object, NSMutableString *output, NSString *indent) {
|
jens@16
|
77 |
if ([object isKindOfClass: [MYASN1Object class]]) {
|
jens@16
|
78 |
MYASN1Object *asn1Obj = object;
|
jens@16
|
79 |
[output appendFormat: @"%@%@[%hhu/%u]", indent, asn1Obj.class, asn1Obj.tagClass,asn1Obj.tag];
|
jens@16
|
80 |
if (asn1Obj.components) {
|
jens@16
|
81 |
[output appendString: @":\n"];
|
jens@16
|
82 |
NSString *subindent = [indent stringByAppendingString: @" "];
|
jens@16
|
83 |
for (id o in asn1Obj.components)
|
jens@16
|
84 |
dump(o,output, subindent);
|
jens@16
|
85 |
} else
|
jens@16
|
86 |
[output appendFormat: @" %@\n", asn1Obj.value];
|
jens@16
|
87 |
} else if([object respondsToSelector: @selector(objectEnumerator)]) {
|
jens@16
|
88 |
[output appendString: indent];
|
jens@16
|
89 |
if ([object isKindOfClass: [NSArray class]])
|
jens@16
|
90 |
[output appendString: @"Sequence:\n"];
|
jens@16
|
91 |
else if ([object isKindOfClass: [NSSet class]])
|
jens@16
|
92 |
[output appendString: @"Set:\n"];
|
jens@16
|
93 |
else
|
jens@16
|
94 |
[output appendFormat: @"%@:\n", [object class]];
|
jens@16
|
95 |
NSString *subindent = [indent stringByAppendingString: @" "];
|
jens@16
|
96 |
for (id o in object)
|
jens@16
|
97 |
dump(o,output, subindent);
|
jens@16
|
98 |
} else {
|
jens@16
|
99 |
[output appendFormat: @"%@%@\n", indent, object];
|
jens@16
|
100 |
}
|
jens@16
|
101 |
}
|
jens@16
|
102 |
|
jens@16
|
103 |
+ (NSString*) dump: (id)object {
|
jens@16
|
104 |
NSMutableString *output = [NSMutableString stringWithCapacity: 512];
|
jens@16
|
105 |
dump(object,output,@"");
|
jens@16
|
106 |
return output;
|
jens@16
|
107 |
}
|
jens@16
|
108 |
|
jens@16
|
109 |
|
jens@16
|
110 |
@end
|
jens@16
|
111 |
|
jens@16
|
112 |
|
jens@16
|
113 |
|
jens@16
|
114 |
@implementation MYASN1BigInteger
|
jens@16
|
115 |
|
jens@16
|
116 |
@end
|
jens@16
|
117 |
|
jens@16
|
118 |
|
jens@16
|
119 |
|
jens@16
|
120 |
@implementation MYBitString
|
jens@16
|
121 |
|
jens@16
|
122 |
|
jens@19
|
123 |
- (id)initWithBits: (NSData*)bits count: (unsigned)bitCount {
|
jens@16
|
124 |
Assert(bits);
|
jens@16
|
125 |
Assert(bitCount <= 8*bits.length);
|
jens@16
|
126 |
self = [super init];
|
jens@16
|
127 |
if (self != nil) {
|
jens@16
|
128 |
_bits = [bits copy];
|
jens@16
|
129 |
_bitCount = bitCount;
|
jens@16
|
130 |
}
|
jens@16
|
131 |
return self;
|
jens@16
|
132 |
}
|
jens@16
|
133 |
|
jens@19
|
134 |
+ (MYBitString*) bitStringWithData: (NSData*)bits {
|
jens@19
|
135 |
return [[[self alloc] initWithBits: bits count: 8*bits.length] autorelease];
|
jens@19
|
136 |
}
|
jens@19
|
137 |
|
jens@16
|
138 |
- (void) dealloc
|
jens@16
|
139 |
{
|
jens@16
|
140 |
[_bits release];
|
jens@16
|
141 |
[super dealloc];
|
jens@16
|
142 |
}
|
jens@16
|
143 |
|
jens@16
|
144 |
@synthesize bits=_bits, bitCount=_bitCount;
|
jens@16
|
145 |
|
jens@17
|
146 |
- (NSString*) description {
|
jens@17
|
147 |
return $sprintf(@"%@%@", [self class], _bits);
|
jens@17
|
148 |
}
|
jens@17
|
149 |
|
jens@19
|
150 |
- (unsigned) hash {
|
jens@19
|
151 |
return _bits.hash ^ _bitCount;
|
jens@19
|
152 |
}
|
jens@19
|
153 |
|
jens@19
|
154 |
- (BOOL) isEqual: (id)object {
|
jens@19
|
155 |
return [object isKindOfClass: [MYBitString class]]
|
jens@19
|
156 |
&& _bitCount==[object bitCount]
|
jens@19
|
157 |
&& [_bits isEqual: [object bits]];
|
jens@19
|
158 |
}
|
jens@19
|
159 |
|
jens@16
|
160 |
@end
|