1. icyspider
  2. MYCrypto

Source

MYCrypto / MYCertificateInfo.m

  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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//
//  MYCertificateInfo.m
//  MYCrypto
//
//  Created by Jens Alfke on 6/2/09.
//  Copyright 2009 Jens Alfke. All rights reserved.
//

// References:
// <http://tools.ietf.org/html/rfc3280> "RFC 3280: Internet X.509 Certificate Profile"
// <http://www.columbia.edu/~ariel/ssleay/layman.html> "Layman's Guide To ASN.1/BER/DER"
// <http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt> "X.509 Style Guide"
// <http://en.wikipedia.org/wiki/X.509> Wikipedia article on X.509


#import "MYCertificateInfo.h"
#import "MYCrypto.h"
#import "MYASN1Object.h"
#import "MYOID.h"
#import "MYBERParser.h"
#import "MYDEREncoder.h"
#import "MYErrorUtils.h"


#define kDefaultExpirationTime (60.0 * 60.0 * 24.0 * 365.0)     /* that's 1 year */

/*  X.509 version number to generate. Even though my code doesn't (yet) add any of the post-v1
    metadata, it's necessary to write v3 or the resulting certs won't be accepted on some platforms,
    notably iPhone OS.
    "This field is used mainly for marketing purposes to claim that software is X.509v3 compliant 
    (even when it isn't)." --Peter Gutmann */
#define kCertRequestVersionNumber 3


/* "Safe" NSArray accessor -- returns nil if out of range. */
static id $atIf(NSArray *array, NSUInteger index) {
    return index < array.count ?[array objectAtIndex: index] :nil;
}


@interface MYCertificateName ()
- (id) _initWithComponents: (NSArray*)components;
@end

@interface MYCertificateExtensions ()
- (id) _initWithComponents: (NSArray*)components;
@end

@interface MYCertificateInfo ()
@property (retain) NSArray *_root;
@end


#pragma mark -
@implementation MYCertificateInfo


static MYOID *kRSAAlgorithmID, *kRSAWithSHA1AlgorithmID, *kRSAWithSHA256AlgorithmID,
             *kRSAWithMD5AlgorithmID, *kRSAWithMD2AlgorithmID,
             *kCommonNameOID, *kGivenNameOID, *kSurnameOID, *kDescriptionOID, *kEmailOID;
MYOID *kKeyUsageOID, *kExtendedKeyUsageOID,
      *kExtendedKeyUsageServerAuthOID, *kExtendedKeyUsageClientAuthOID,
      *kExtendedKeyUsageCodeSigningOID, *kExtendedKeyUsageEmailProtectionOID, 
      *kExtendedKeyUsageAnyOID;


+ (void) initialize {
    if (!kEmailOID) {
        kRSAAlgorithmID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 1, 1,}
                                                      count: 7];
        kRSAWithSHA1AlgorithmID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 1, 5}
                                                              count: 7];
        kRSAWithSHA256AlgorithmID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 1, 11}
                                                                count:7];
        kRSAWithMD5AlgorithmID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 1, 4 }
                                                             count:7];
        kRSAWithMD2AlgorithmID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 1, 2}
                                                             count:7];
        kCommonNameOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 4, 3}
                                                     count: 4];
        kGivenNameOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 4, 42}
                                                    count: 4];
        kSurnameOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 4, 4}
                                                  count: 4];
        kDescriptionOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 4, 13}
                                                count: 4];
        kEmailOID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 2, 840, 113549, 1, 9, 1}
                                                count: 7];
        kKeyUsageOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 29, 15}
                                                           count: 4];
        kExtendedKeyUsageOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 29, 37}
                                                           count: 4];

        kExtendedKeyUsageServerAuthOID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 3, 6, 1, 5, 5, 7, 3, 1}
                                                           count: 9];
        kExtendedKeyUsageClientAuthOID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 3, 6, 1, 5, 5, 7, 3, 2}
                                                                     count: 9];
        kExtendedKeyUsageCodeSigningOID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 3, 6, 1, 5, 5, 7, 3, 3}
                                                                     count: 9];
        kExtendedKeyUsageEmailProtectionOID = [[MYOID alloc] initWithComponents: (UInt32[]){1, 3, 6, 1, 5, 5, 7, 3, 4}
                                                                          count: 9];
        kExtendedKeyUsageAnyOID = [[MYOID alloc] initWithComponents: (UInt32[]){2, 5, 29, 37, 0}
                                                                          count: 5];
    }
}


- (id) initWithRoot: (NSArray*)root
{
    self = [super init];
    if (self != nil) {
        _root = [root retain];
    }
    return self;
}

+ (NSString*) validate: (id)root {
    NSArray *top = $castIf(NSArray,root);
    if (top.count < 3)
        return @"Too few top-level components";
    NSArray *info = $castIf(NSArray, [top objectAtIndex: 0]);
    if (info.count < 6) {
        return @"Too few identity components";      // there should be 7, but version has a default
    } else if (info.count > 6) {
        MYASN1Object *version = $castIf(MYASN1Object, [info objectAtIndex: 0]);
        if (!version || version.tag != 0)
            return @"Missing or invalid version";
        NSArray *versionComps = $castIf(NSArray, version.components);
        if (!versionComps || versionComps.count != 1)
            return @"Invalid version";
        NSNumber *versionNum = $castIf(NSNumber, [versionComps objectAtIndex: 0]);
        if (!versionNum || versionNum.intValue < 0 || versionNum.intValue > 2)
            return @"Unrecognized version number";
    }
    return nil;
}


- (id) initWithCertificateData: (NSData*)data error: (NSError**)outError;
{
    if (outError) *outError = nil;
    id root = MYBERParse(data,outError);
    NSString *errorMsg = [[self class] validate: root];
    if (errorMsg) {
        if (outError && !*outError)
            *outError = MYError(2, MYASN1ErrorDomain, @"Invalid certificate: %@", errorMsg);
        [self release];
        return nil;
    }

    self = [self initWithRoot: root];
    if (self) {
        _data = [data copy];
    }
    return self;
}

- (void) dealloc
{
    [_root release];
    [_data release];
    [super dealloc];
}

- (BOOL) isEqual: (id)object {
    return [object isKindOfClass: [MYCertificateInfo class]]
        && [_root isEqual: ((MYCertificateInfo*)object)->_root];
}

/* _info returns an NSArray representing the thing called TBSCertificate in the spec:
    TBSCertificate ::= SEQUENCE {
        version          [ 0 ]  Version DEFAULT v1(0),
        serialNumber            CertificateSerialNumber,
        signature               AlgorithmIdentifier,
        issuer                  Name,
        validity                Validity,
        subject                 Name,
        subjectPublicKeyInfo    SubjectPublicKeyInfo,
        issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
        subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
        extensions        [ 3 ] Extensions OPTIONAL
        }
*/
- (NSArray*) _info {
    NSArray* info = $castIf(NSArray,$atIf(_root,0));
    if (info.count >= 7)
        return info;
    // If version field is missing, insert it explicitly so the array indices will be normal:
    NSMutableArray* minfo = [[info mutableCopy] autorelease];
    [minfo insertObject: $object(0) atIndex: 0];
    return minfo;
}

- (NSArray*) _validDates {return $castIf(NSArray, [self._info objectAtIndex: 4]);}

@synthesize _root;


- (NSDate*) validFrom       {return $castIf(NSDate, $atIf(self._validDates, 0));}
- (NSDate*) validTo         {return $castIf(NSDate, $atIf(self._validDates, 1));}

- (MYCertificateName*) subject {
    return [[[MYCertificateName alloc] _initWithComponents: [self._info objectAtIndex: 5]] autorelease];
}

- (MYCertificateName*) issuer {
    return [[[MYCertificateName alloc] _initWithComponents: [self._info objectAtIndex: 3]] autorelease];
}

- (MYCertificateExtensions*)extensions {
    NSArray* info = self._info;
    for (NSUInteger i=7; i<info.count; i++) {
        MYASN1Object* obj = $castIf(MYASN1Object, [info objectAtIndex:i]);
        if (obj.tag == 3) {
            NSArray* extensions = $castIf(NSArray, $atIf(obj.components, 0));
            if (!extensions)
                return nil;
            return [[[MYCertificateExtensions alloc] _initWithComponents: extensions] autorelease];
        }
    }
    return nil;
}

- (BOOL) isSigned           {return [_root count] >= 3;}

- (BOOL) isRoot {
    id issuer = $atIf(self._info,3);
    return $equal(issuer, $atIf(self._info,5)) || $equal(issuer, $array());
}


- (NSData*) subjectPublicKeyData {
    NSArray *keyInfo = $cast(NSArray, $atIf(self._info, 6));
    MYOID *keyAlgorithmID = $castIf(MYOID, $atIf($castIf(NSArray,$atIf(keyInfo,0)), 0));
    if (!$equal(keyAlgorithmID, kRSAAlgorithmID))
        return nil;
    return $cast(MYBitString, $atIf(keyInfo, 1)).bits;
}

- (MYPublicKey*) subjectPublicKey {
    NSData *keyData = self.subjectPublicKeyData;
    if (!keyData) return nil;
    return [[[MYPublicKey alloc] initWithKeyData: keyData] autorelease];
}

- (NSData*) signedData {
    if (!_data)
        return nil;
    // The root object is a sequence; we want to extract the 1st object of that sequence.
    const UInt8 *certStart = _data.bytes;
    const UInt8 *start = MYBERGetContents(_data, nil);
    if (!start) return nil;
    size_t length = MYBERGetLength([NSData dataWithBytesNoCopy: (void*)start
                                                        length: _data.length - (start-certStart)
                                                  freeWhenDone: NO],
                                   NULL);
    if (length==0)
        return nil;
    return [NSData dataWithBytes: start length: (start + length - certStart)];
}

- (MYOID*) signatureAlgorithmID {
    return $castIf(MYOID, $atIf($castIf(NSArray,$atIf(_root,1)), 0));
}

- (NSData*) signature {
    id signature = $atIf(_root,2);
    if ([signature isKindOfClass: [MYBitString class]])
        signature = [signature bits];
    return $castIf(NSData,signature);
}

- (BOOL) verifySignatureWithKey: (MYPublicKey*)issuerPublicKey {
    NSData *signedData = self.signedData;
    NSData *signature = self.signature;
    if (!signedData || !signature)
        return NO;
    
#if !MYCRYPTO_USE_IPHONE_API
    // Determine which signature algorithm to use:
    CSSM_ALGORITHMS algorithm;
    MYOID* algID = self.signatureAlgorithmID;
    if ($equal(algID, kRSAWithSHA1AlgorithmID))
        algorithm = CSSM_ALGID_SHA1WithRSA;
    else if ($equal(algID, kRSAWithSHA256AlgorithmID))
        algorithm = CSSM_ALGID_SHA256WithRSA;
    else if ($equal(algID, kRSAWithMD5AlgorithmID))
        algorithm = CSSM_ALGID_MD5WithRSA;
    else if ($equal(algID, kRSAWithMD2AlgorithmID))
        algorithm = CSSM_ALGID_MD2WithRSA;
    else {
        Warn(@"MYCertificateInfo can't verify: unknown signature algorithm %@", algID);
        return NO;
    }
#endif
    
    return [issuerPublicKey verifySignature: signature
                                     ofData: signedData
#if !MYCRYPTO_USE_IPHONE_API
                              withAlgorithm: algorithm
#endif
            ];
}


@end




#pragma mark -
@implementation MYCertificateRequest

- (id) initWithPublicKey: (MYPublicKey*)publicKey {
    Assert(publicKey);
    id empty = [NSNull null];
    id version = [[MYASN1Object alloc] initWithTag: 0 
                                           ofClass: 2
                                        components: $array($object(kCertRequestVersionNumber - 1))];
    id extensions = [[MYASN1Object alloc] initWithTag:3
                                              ofClass:2
                                           components: $array($marray())];
    NSArray *root = $array( $marray(version,
                                    empty,       // serial #
                                    $array(kRSAAlgorithmID),
                                    $marray(),
                                    $marray(empty, empty),
                                    $marray(),
                                    $array( $array(kRSAAlgorithmID, empty),
                                           [MYBitString bitStringWithData: publicKey.keyData] ),
                                    extensions) );
    self = [super initWithRoot: root];
    [version release];
    [extensions release];
    if (self) {
        _publicKey = publicKey.retain;
    }
    return self;
}
    
- (void) dealloc
{
    [_publicKey release];
    [super dealloc];
}


- (NSDate*) validFrom       {return [super validFrom];}
- (NSDate*) validTo         {return [super validTo];}

- (void) setValidFrom: (NSDate*)validFrom {
    [(NSMutableArray*)self._validDates replaceObjectAtIndex: 0 withObject: validFrom];
}

- (void) setValidTo: (NSDate*)validTo {
    [(NSMutableArray*)self._validDates replaceObjectAtIndex: 1 withObject: validTo];
}


- (void) fillInValues {
    NSMutableArray *info = (NSMutableArray*)self._info;
    // Set serial number if there isn't one yet:
    if (!$castIf(NSNumber, [info objectAtIndex: 1])) {
        UInt64 serial = floor(CFAbsoluteTimeGetCurrent() * 1000);
        [info replaceObjectAtIndex: 1 withObject: $object(serial)];
    }
    
    // Set up valid date range if there isn't one yet:
    NSDate *validFrom = self.validFrom;
    if (!validFrom)
        validFrom = self.validFrom = [NSDate date];
    NSDate *validTo = self.validTo;
    if (!validTo) {
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5
        self.validTo = [validFrom addTimeInterval: kDefaultExpirationTime];
#else
        self.validTo = [validFrom dateByAddingTimeInterval: kDefaultExpirationTime];
#endif
    }
}


- (NSData*) requestData: (NSError**)outError {
    [self fillInValues];
    return [MYDEREncoder encodeRootObject: self._info error: outError];
}


- (NSData*) selfSignWithPrivateKey: (MYPrivateKey*)privateKey 
                             error: (NSError**)outError 
{
    AssertEqual(privateKey.publicKey, _publicKey);  // Keys must form a pair
    
    // Copy subject to issuer:
    NSMutableArray *info = (NSMutableArray*)self._info;
    [info replaceObjectAtIndex: 3 withObject: [info objectAtIndex: 5]];
    
    // Sign the request:
    NSData *dataToSign = [self requestData: outError];
    if (!dataToSign)
        return nil;
    MYBitString *signature = [MYBitString bitStringWithData: [privateKey signData: dataToSign]];
    
    // Generate and encode the certificate:
    NSArray *root = $array(info, 
                           $array(kRSAWithSHA1AlgorithmID, [NSNull null]),
                           signature);
    return [MYDEREncoder encodeRootObject: root error: outError];
}


- (MYIdentity*) createSelfSignedIdentityWithPrivateKey: (MYPrivateKey*)privateKey
                                                 error: (NSError**)outError
{
    Assert(privateKey.keychain!=nil);
    NSData *certData = [self selfSignWithPrivateKey: privateKey error: outError];
    if (!certData)
        return nil;
    MYCertificate *cert = [privateKey.keychain importCertificate: certData];
    Assert(cert!=nil);
    Assert(cert.keychain!=nil);
    AssertEqual(cert.publicKey.keyData, _publicKey.keyData);
    MYIdentity *identity = cert.identity;
    Assert(identity!=nil);
    return identity;
}


@end



#pragma mark -
@implementation MYCertificateName

- (id) _initWithComponents: (NSArray*)components
{
    self = [super init];
    if (self != nil) {
        _components = [components retain];
    }
    return self;
}

- (void) dealloc
{
    [_components release];
    [super dealloc];
}

- (BOOL) isEqual: (id)object {
    return [object isKindOfClass: [MYCertificateName class]]
        && [_components isEqual: ((MYCertificateName*)object)->_components];
}

- (NSArray*) _pairForOID: (MYOID*)oid {
    for (id nameEntry in _components) {
        for (id pair in $castIf(NSSet,nameEntry)) {
            if ([pair isKindOfClass: [NSArray class]] && [pair count] == 2) {
                if ($equal(oid, [pair objectAtIndex: 0]))
                    return pair;
            }
        }
    }
    return nil;
}

- (NSString*) stringForOID: (MYOID*)oid {
    return [[self _pairForOID: oid] objectAtIndex: 1];
}

- (void) setString: (NSString*)value forOID: (MYOID*)oid {
    NSMutableArray *pair = (NSMutableArray*) [self _pairForOID: oid];
    if (pair) {
        if (value)
            [pair replaceObjectAtIndex: 1 withObject: value];
        else
            Assert(NO,@"-setString:forOID: removing strings is unimplemented");//FIX
    } else {
        if (value)
            [(NSMutableArray*)_components addObject: [NSSet setWithObject: $marray(oid,value)]];
    }
}

- (NSString*) commonName    {return [self stringForOID: kCommonNameOID];}
- (NSString*) givenName     {return [self stringForOID: kGivenNameOID];}
- (NSString*) surname       {return [self stringForOID: kSurnameOID];}
- (NSString*) nameDescription {return [self stringForOID: kDescriptionOID];}
- (NSString*) emailAddress  {return [self stringForOID: kEmailOID];}

- (void) setCommonName: (NSString*)commonName   {[self setString: commonName forOID: kCommonNameOID];}
- (void) setGivenName: (NSString*)givenName     {[self setString: givenName forOID: kGivenNameOID];}
- (void) setSurname: (NSString*)surname         {[self setString: surname forOID: kSurnameOID];}
- (void) setNameDescription: (NSString*)desc    {[self setString: desc forOID: kDescriptionOID];}
- (void) setEmailAddress: (NSString*)email      {[self setString: email forOID: kEmailOID];}


@end






#pragma mark -
@implementation MYCertificateExtensions

- (id) _initWithComponents: (NSArray*)extensions
{
    self = [super init];
    if (self != nil) {
        _extensions = [extensions retain];
    }
    return self;
}

- (void) dealloc
{
    [_extensions release];
    [super dealloc];
}

- (NSArray*) _itemForOID: (MYOID*)oid {
    for (id item in _extensions) {
        NSArray* extension = $castIf(NSArray, item);
        if (extension.count == 3 && [[extension objectAtIndex:0] isEqual: oid])
            return extension;
    }
    return nil;
}

- (NSArray*) extensionOIDs {
    NSMutableArray* oids = $marray();
    for (id item in _extensions) {
        NSArray* extension = $castIf(NSArray, item);
        if (extension.count == 3) {
            MYOID* oid = $castIf(MYOID, [extension objectAtIndex:0]);
            if (oid)
                [oids addObject:oid];
        }
    }
    return oids;
}


- (id) extensionForOID: (MYOID*)oid isCritical: (BOOL*)outIsCritical {
    NSArray* extension = [self _itemForOID:oid];
    if (!extension)
        return nil;
    if (outIsCritical)
        *outIsCritical = [$castIf(NSNumber, [extension objectAtIndex: 1]) boolValue];
    NSData* ber = $castIf(NSData, [extension objectAtIndex: 2]);
    if (!ber)
        return nil;
    return MYBERParse(ber, NULL);
}

- (void) setExtension: (id)value isCritical: (BOOL)isCritical forOID: (MYOID*)oid {
    NSArray* item = nil;
    if (value) {
        NSData* ber = [MYDEREncoder encodeRootObject: value error: NULL];
        Assert(ber != nil);
        item = $marray(oid, (isCritical ?$true :$false), ber);
    }
    
    NSMutableArray* extension = (NSMutableArray*) [self _itemForOID:oid];
    if (extension) {
        if (item)
            [extension replaceObjectsInRange:NSMakeRange(0,2) withObjectsFromArray:item];
        else
            [(NSMutableArray*)_extensions removeObject: extension];
    } else {
        if (item)
            [(NSMutableArray*)_extensions addObject: item];
    }
}


- (UInt16) keyUsage {
    // RFC 3280 sec. 4.2.1.3
    MYBitString* bits = $castIf(MYBitString, [self extensionForOID:kKeyUsageOID isCritical:NULL]);
    if (!bits)
        return kKeyUsageUnspecified;
    const UInt8* bytes = [bits.bits bytes];
    UInt16 value = bytes[0];
    if (bits.bitCount > 8)      // 9 bits are defined, so the value could be multi-byte
        value |= bytes[1] << 8;
    return value;
}

- (void) setKeyUsage: (UInt16)keyUsage {
    MYBitString* bitString = nil;
    if (keyUsage != 0 && keyUsage != kKeyUsageUnspecified) {
        Assert((keyUsage & ~0x1FF) == 0, @"Invalid flags in keyUsage: 0x%x", keyUsage);
        UInt8 bytes[2] = {keyUsage & 0xFF, keyUsage >> 8};
        size_t length = 1 + (bytes[1] != 0);
        NSData* data = [NSData dataWithBytes: bytes length: length];
        bitString = [[MYBitString alloc] initWithBits: data count: 8*length];
    }
    [self setExtension: bitString 
            isCritical: YES 
                forOID: kKeyUsageOID];
    [bitString release];
}


- (BOOL) allowsKeyUsage: (UInt16)requestedKeyUsage {
    BOOL critical;
    if ([self extensionForOID: kKeyUsageOID isCritical:&critical] && critical) {
        if ((self.keyUsage & requestedKeyUsage) != requestedKeyUsage)
            return NO;
    }
    return YES;
}


- (NSSet*) extendedKeyUsage {
    // RFC 3280 sec. 4.2.1.13
    NSArray* oids = $castIf(NSArray, [self extensionForOID: kExtendedKeyUsageOID isCritical: NULL]);
    if (!oids)
        return nil;
    return [NSSet setWithArray:oids];
}

- (void) setExtendedKeyUsage: (NSSet*)usage {
    [self setExtension: (usage.count ?[usage allObjects] :nil)
            isCritical: YES
                forOID: kExtendedKeyUsageOID];
}

- (BOOL) allowsExtendedKeyUsage: (NSSet*) requestedKeyUsage {
    BOOL critical;
    if ([self extensionForOID: kExtendedKeyUsageOID isCritical:&critical] && critical) {
        NSSet* keyUsage = self.extendedKeyUsage;
        if (![requestedKeyUsage isSubsetOfSet: keyUsage]
                && ![keyUsage containsObject: kExtendedKeyUsageAnyOID])
            return NO;
    }
    return YES;
}

@end


/*
 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification, are permitted
 provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice, this list of conditions
 and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 and the following disclaimer in the documentation and/or other materials provided with the
 distribution.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */