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