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