MYIdentity.m
author Jens Alfke <jens@mooseyard.com>
Wed Jun 10 09:02:18 2009 -0700 (2009-06-10)
changeset 25 38c3c3923e1f
parent 14 3af1d1c0ceb5
child 26 d9c2a06d4e4e
permissions -rw-r--r--
Changed the X.509 version number in generated certs from 1 to 3, so that SecCertificateCreateFromData on iPhone will accept them. :-/
     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 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    39     self = [super initWithCertificateRef: certificateRef];
    40     if (self) {
    41 #if !MYCRYPTO_USE_IPHONE_API
    42         if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
    43                    @"SecIdentityCreateWithCertificate")) {
    44             [self release];
    45             return nil;
    46         }
    47 #else
    48         Assert(NO,@"-[MYIdentity initWithCertificateRef] isn't implemented for iPhone yet!");//FIX
    49 #endif
    50     }
    51     return self;
    52 }
    53 
    54 - (void) dealloc
    55 {
    56     if (_identityRef) CFRelease(_identityRef);
    57     [super dealloc];
    58 }
    59 
    60 - (void) finalize
    61 {
    62     if (_identityRef) CFRelease(_identityRef);
    63     [super finalize];
    64 }
    65 
    66 
    67 @synthesize identityRef=_identityRef;
    68 
    69 - (MYPrivateKey*) privateKey {
    70     SecKeyRef keyRef = NULL;
    71     if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
    72         return NULL;
    73     MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
    74                                                            publicKey: self.publicKey];
    75     CFRelease(keyRef);
    76     return [privateKey autorelease];
    77 }
    78 
    79 
    80 #if !TARGET_OS_IPHONE
    81 
    82 + (MYIdentity*) preferredIdentityForName: (NSString*)name
    83 {
    84     Assert(name);
    85     SecIdentityRef identityRef;
    86     OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
    87     if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
    88         return nil;
    89     return [self identityWithIdentityRef: identityRef];
    90 }
    91 
    92 - (BOOL) makePreferredIdentityForName: (NSString*)name {
    93     Assert(name);
    94     return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
    95                  @"SecIdentitySetPreference");
    96 }
    97 
    98 #endif !TARGET_OS_IPHONE
    99 
   100 @end
   101 
   102 
   103 
   104 /*
   105  Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   106  
   107  Redistribution and use in source and binary forms, with or without modification, are permitted
   108  provided that the following conditions are met:
   109  
   110  * Redistributions of source code must retain the above copyright notice, this list of conditions
   111  and the following disclaimer.
   112  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   113  and the following disclaimer in the documentation and/or other materials provided with the
   114  distribution.
   115  
   116  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   117  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   118  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   119  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   120  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   121   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   122  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   123  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   124  */