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