snej@4: // snej@4: // MYIdentity.m snej@4: // MYCrypto snej@4: // snej@4: // Created by Jens Alfke on 4/9/09. snej@4: // Copyright 2009 Jens Alfke. All rights reserved. snej@4: // snej@4: snej@4: #import "MYIdentity.h" snej@4: #import "MYCrypto_Private.h" snej@4: snej@4: snej@4: @implementation MYIdentity snej@4: snej@4: snej@8: /** Creates a MYIdentity object for an existing Keychain identity reference. */ snej@8: + (MYIdentity*) identityWithIdentityRef: (SecIdentityRef)identityRef { snej@8: return [[[self alloc] initWithIdentityRef: identityRef] autorelease]; snej@8: } snej@8: snej@4: - (id) initWithIdentityRef: (SecIdentityRef)identityRef { snej@4: Assert(identityRef); snej@4: SecCertificateRef certificateRef; snej@4: if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) { snej@4: [self release]; snej@4: return nil; snej@4: } snej@4: self = [super initWithCertificateRef: certificateRef]; snej@4: if (self) { snej@4: _identityRef = identityRef; snej@4: CFRetain(identityRef); snej@4: } snej@4: CFRelease(certificateRef); snej@4: return self; snej@4: } snej@4: snej@4: snej@4: - (id) initWithCertificateRef: (SecCertificateRef)certificateRef { snej@4: self = [super initWithCertificateRef: certificateRef]; snej@4: if (self) { jens@23: #if !MYCRYPTO_USE_IPHONE_API snej@4: if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef), snej@4: @"SecIdentityCreateWithCertificate")) { snej@4: [self release]; snej@4: return nil; snej@4: } jens@23: #else jens@23: Assert(NO,@"-[MYIdentity initWithCertificateRef] isn't implemented for iPhone yet!");//FIX jens@23: #endif snej@4: } snej@4: return self; snej@4: } snej@4: snej@4: - (void) dealloc snej@4: { snej@4: if (_identityRef) CFRelease(_identityRef); snej@4: [super dealloc]; snej@4: } snej@4: snej@4: - (void) finalize snej@4: { snej@4: if (_identityRef) CFRelease(_identityRef); snej@4: [super finalize]; snej@4: } snej@4: snej@4: snej@8: @synthesize identityRef=_identityRef; snej@8: snej@4: - (MYPrivateKey*) privateKey { snej@4: SecKeyRef keyRef = NULL; snej@4: if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey")) snej@4: return NULL; snej@4: MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef jens@23: publicKey: self.publicKey]; snej@4: CFRelease(keyRef); snej@4: return [privateKey autorelease]; snej@4: } snej@4: snej@4: snej@5: #if !TARGET_OS_IPHONE snej@5: snej@4: + (MYIdentity*) preferredIdentityForName: (NSString*)name snej@4: { snej@4: Assert(name); snej@4: SecIdentityRef identityRef; snej@8: OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef); snej@8: if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef) snej@4: return nil; snej@8: return [self identityWithIdentityRef: identityRef]; snej@4: } snej@4: snej@4: - (BOOL) makePreferredIdentityForName: (NSString*)name { snej@4: Assert(name); snej@4: return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0), snej@4: @"SecIdentitySetPreference"); snej@4: } snej@4: snej@5: #endif !TARGET_OS_IPHONE snej@5: snej@4: @end snej@14: snej@14: snej@14: snej@14: /* snej@14: Copyright (c) 2009, Jens Alfke . All rights reserved. snej@14: snej@14: Redistribution and use in source and binary forms, with or without modification, are permitted snej@14: provided that the following conditions are met: snej@14: snej@14: * Redistributions of source code must retain the above copyright notice, this list of conditions snej@14: and the following disclaimer. snej@14: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions snej@14: and the following disclaimer in the documentation and/or other materials provided with the snej@14: distribution. snej@14: snej@14: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR snej@14: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND snej@14: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- snej@14: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES snej@14: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR snej@14: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN snej@14: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF snej@14: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. snej@14: */