MYIdentity.m
author Jens Alfke <jens@mooseyard.com>
Thu Jun 04 18:36:30 2009 -0700 (2009-06-04)
changeset 19 f6c91b9da05b
parent 8 4c0eafa7b233
child 23 39fec79de6e8
permissions -rw-r--r--
Whew! MYParsedCertificate can now generate certs from scratch. Also added improvements and fixes to the BER/DER codecs.
     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 /** Creates a MYIdentity object for an existing Keychain identity reference. */
    17 + (MYIdentity*) identityWithIdentityRef: (SecIdentityRef)identityRef {
    18     return [[[self alloc] initWithIdentityRef: identityRef] autorelease];
    19 }
    20 
    21 - (id) initWithIdentityRef: (SecIdentityRef)identityRef {
    22     Assert(identityRef);
    23     SecCertificateRef certificateRef;
    24     if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) {
    25         [self release];
    26         return nil;
    27     }
    28     self = [super initWithCertificateRef: certificateRef];
    29     if (self) {
    30         _identityRef = identityRef;
    31         CFRetain(identityRef);
    32     }
    33     CFRelease(certificateRef);
    34     return self;
    35 }
    36 
    37 
    38 #if !TARGET_OS_IPHONE
    39 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    40     self = [super initWithCertificateRef: certificateRef];
    41     if (self) {
    42         if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
    43                    @"SecIdentityCreateWithCertificate")) {
    44             [self release];
    45             return nil;
    46         }
    47     }
    48     return self;
    49 }
    50 #endif
    51 
    52 - (void) dealloc
    53 {
    54     if (_identityRef) CFRelease(_identityRef);
    55     [super dealloc];
    56 }
    57 
    58 - (void) finalize
    59 {
    60     if (_identityRef) CFRelease(_identityRef);
    61     [super finalize];
    62 }
    63 
    64 
    65 @synthesize identityRef=_identityRef;
    66 
    67 - (MYPrivateKey*) privateKey {
    68     SecKeyRef keyRef = NULL;
    69     if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
    70         return NULL;
    71     MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
    72                                                           publicKey: self.publicKey];
    73     CFRelease(keyRef);
    74     return [privateKey autorelease];
    75 }
    76 
    77 
    78 #if !TARGET_OS_IPHONE
    79 
    80 + (MYIdentity*) preferredIdentityForName: (NSString*)name
    81 {
    82     Assert(name);
    83     SecIdentityRef identityRef;
    84     OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
    85     if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
    86         return nil;
    87     return [self identityWithIdentityRef: identityRef];
    88 }
    89 
    90 - (BOOL) makePreferredIdentityForName: (NSString*)name {
    91     Assert(name);
    92     return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
    93                  @"SecIdentitySetPreference");
    94 }
    95 
    96 #endif !TARGET_OS_IPHONE
    97 
    98 @end
    99 
   100 
   101 
   102 /*
   103  Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   104  
   105  Redistribution and use in source and binary forms, with or without modification, are permitted
   106  provided that the following conditions are met:
   107  
   108  * Redistributions of source code must retain the above copyright notice, this list of conditions
   109  and the following disclaimer.
   110  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   111  and the following disclaimer in the documentation and/or other materials provided with the
   112  distribution.
   113  
   114  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   115  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   116  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   117  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   118  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   119   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   120  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   121  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   122  */