MYIdentity.m
author snej@snej.local
Thu Apr 09 22:46:48 2009 -0700 (2009-04-09)
changeset 6 2d7692f9b6b4
parent 4 f4709533c816
child 8 4c0eafa7b233
permissions -rw-r--r--
Updated the README for the 0.1 release.
     1 //
     2 //  MYIdentity.m
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 4/9/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYIdentity.h"
    10 #import "MYCrypto_Private.h"
    11 
    12 
    13 @implementation MYIdentity
    14 
    15 
    16 - (id) initWithIdentityRef: (SecIdentityRef)identityRef {
    17     Assert(identityRef);
    18     SecCertificateRef certificateRef;
    19     if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) {
    20         [self release];
    21         return nil;
    22     }
    23     self = [super initWithCertificateRef: certificateRef];
    24     if (self) {
    25         _identityRef = identityRef;
    26         CFRetain(identityRef);
    27     }
    28     CFRelease(certificateRef);
    29     return self;
    30 }
    31 
    32 
    33 #if !TARGET_OS_IPHONE
    34 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    35     self = [super initWithCertificateRef: certificateRef];
    36     if (self) {
    37         if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
    38                    @"SecIdentityCreateWithCertificate")) {
    39             [self release];
    40             return nil;
    41         }
    42     }
    43     return self;
    44 }
    45 #endif
    46 
    47 - (void) dealloc
    48 {
    49     if (_identityRef) CFRelease(_identityRef);
    50     [super dealloc];
    51 }
    52 
    53 - (void) finalize
    54 {
    55     if (_identityRef) CFRelease(_identityRef);
    56     [super finalize];
    57 }
    58 
    59 
    60 - (MYPrivateKey*) privateKey {
    61     SecKeyRef keyRef = NULL;
    62     if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
    63         return NULL;
    64     MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
    65                                                           publicKey: self.publicKey];
    66     CFRelease(keyRef);
    67     return [privateKey autorelease];
    68 }
    69 
    70 
    71 #if !TARGET_OS_IPHONE
    72 
    73 + (MYIdentity*) preferredIdentityForName: (NSString*)name
    74 {
    75     Assert(name);
    76     SecIdentityRef identityRef;
    77     if (!check(SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef),
    78                @"SecIdentityCopyPreference"))
    79         return nil;
    80     return identityRef ?[[[self alloc] initWithIdentityRef: identityRef] autorelease] :nil;
    81 }
    82 
    83 - (BOOL) makePreferredIdentityForName: (NSString*)name {
    84     Assert(name);
    85     return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
    86                  @"SecIdentitySetPreference");
    87 }
    88 
    89 #endif !TARGET_OS_IPHONE
    90 
    91 @end