MYIdentity.m
author Jens Alfke <jens@mooseyard.com>
Tue Jul 21 10:13:08 2009 -0700 (2009-07-21)
changeset 27 d0aadddb9c64
parent 23 39fec79de6e8
permissions -rw-r--r--
MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
     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 #import "MYDigest.h"
    12 
    13 
    14 @implementation MYIdentity
    15 
    16 
    17 /** Creates a MYIdentity object for an existing Keychain identity reference. */
    18 + (MYIdentity*) identityWithIdentityRef: (SecIdentityRef)identityRef {
    19     return [[[self alloc] initWithIdentityRef: identityRef] autorelease];
    20 }
    21 
    22 - (id) initWithIdentityRef: (SecIdentityRef)identityRef {
    23     Assert(identityRef);
    24     SecCertificateRef certificateRef;
    25     if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) {
    26         [self release];
    27         return nil;
    28     }
    29     self = [super initWithCertificateRef: certificateRef];
    30     if (self) {
    31         _identityRef = identityRef;
    32         CFRetain(identityRef);
    33     }
    34     CFRelease(certificateRef);
    35     return self;
    36 }
    37 
    38 
    39 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    40     self = [super initWithCertificateRef: certificateRef];
    41     if (self) {
    42 #if !MYCRYPTO_USE_IPHONE_API
    43         if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
    44                    @"SecIdentityCreateWithCertificate")) {
    45             [self release];
    46             return nil;
    47         }
    48 #else
    49         MYSHA1Digest *keyDigest = self.publicKey.publicKeyDigest;
    50         if (!keyDigest) {
    51             Warn(@"MYIdentity: Couldn't get key digest of cert %@",certificateRef);
    52             [self release];
    53             return nil;
    54         }
    55         _identityRef = [self.keychain identityWithDigest: keyDigest].identityRef;
    56         if (!_identityRef) {
    57             Warn(@"MYIdentity: Couldn't look up identity for cert %@ with %@",certificateRef, keyDigest);
    58             [self release];
    59             return nil;
    60         }
    61         
    62         // Debugging: Make sure the cert is correct
    63         SecCertificateRef identitysCert = NULL;
    64         SecIdentityCopyCertificate(_identityRef, &identitysCert);
    65         CFDataRef identitysData = SecCertificateCopyData(identitysCert);
    66         AssertEqual(self.certificateData, (NSData*)identitysData);
    67         CFRelease(identitysData);
    68         CFRelease(identitysCert);
    69         
    70         CFRetain(_identityRef);
    71 #endif
    72     }
    73     return self;
    74 }
    75 
    76 - (void) dealloc
    77 {
    78     if (_identityRef) CFRelease(_identityRef);
    79     [super dealloc];
    80 }
    81 
    82 - (void) finalize
    83 {
    84     if (_identityRef) CFRelease(_identityRef);
    85     [super finalize];
    86 }
    87 
    88 
    89 @synthesize identityRef=_identityRef;
    90 
    91 - (MYPrivateKey*) privateKey {
    92     SecKeyRef keyRef = NULL;
    93     if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
    94         return NULL;
    95     MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
    96                                                            publicKey: self.publicKey];
    97     CFRelease(keyRef);
    98     return [privateKey autorelease];
    99 }
   100 
   101 
   102 - (BOOL) removeFromKeychain {
   103     return [self.privateKey removeFromKeychain] && [super removeFromKeychain];
   104 }
   105 
   106 
   107 #if !TARGET_OS_IPHONE
   108 
   109 + (MYIdentity*) preferredIdentityForName: (NSString*)name
   110 {
   111     Assert(name);
   112     SecIdentityRef identityRef;
   113     OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
   114     if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
   115         return nil;
   116     return [self identityWithIdentityRef: identityRef];
   117 }
   118 
   119 - (BOOL) makePreferredIdentityForName: (NSString*)name {
   120     Assert(name);
   121     return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
   122                  @"SecIdentitySetPreference");
   123 }
   124 
   125 #endif !TARGET_OS_IPHONE
   126 
   127 @end
   128 
   129 
   130 
   131 /*
   132  Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   133  
   134  Redistribution and use in source and binary forms, with or without modification, are permitted
   135  provided that the following conditions are met:
   136  
   137  * Redistributions of source code must retain the above copyright notice, this list of conditions
   138  and the following disclaimer.
   139  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   140  and the following disclaimer in the documentation and/or other materials provided with the
   141  distribution.
   142  
   143  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   144  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   145  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   146  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   147  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   148   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   149  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   150  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   151  */