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@5: #if !TARGET_OS_IPHONE snej@4: - (id) initWithCertificateRef: (SecCertificateRef)certificateRef { snej@4: self = [super initWithCertificateRef: certificateRef]; snej@4: if (self) { snej@4: if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef), snej@4: @"SecIdentityCreateWithCertificate")) { snej@4: [self release]; snej@4: return nil; snej@4: } snej@4: } snej@4: return self; snej@4: } snej@5: #endif 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 snej@4: 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