snej@0: // snej@0: // MYKey-iPhone.m snej@0: // MYCrypto-iPhone snej@0: // snej@0: // Created by Jens Alfke on 4/4/09. snej@0: // Copyright 2009 Jens Alfke. All rights reserved. snej@0: // snej@0: snej@0: snej@0: #import "MYCrypto_Private.h" snej@0: snej@0: #if USE_IPHONE_API snej@0: snej@0: #import "MYDigest.h" snej@0: #import "MYErrorUtils.h" snej@0: snej@0: snej@0: #pragma mark - snej@0: @implementation MYKey snej@0: snej@0: snej@0: - (id) initWithKeyRef: (SecKeyRef)key { snej@1: return [super initWithKeychainItemRef: (SecKeychainItemRef)key]; snej@0: } snej@0: snej@0: snej@0: - (id) _initWithKeyData: (NSData*)data snej@0: forKeychain: (SecKeychainRef)keychain snej@0: { snej@0: NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey}, snej@1: {(id)kSecAttrKeyClass, (id)self.keyType}, snej@0: {(id)kSecValueData, data}, snej@0: {(id)kSecAttrIsPermanent, $object(keychain!=nil)}, snej@0: {(id)kSecReturnRef, $true} ); snej@0: SecKeyRef key; snej@0: if (!check(SecItemAdd((CFDictionaryRef)info, (CFTypeRef*)&key), @"SecItemAdd")) snej@0: return nil; snej@0: else snej@0: return [self initWithKeyRef: (SecKeyRef)key]; snej@0: } snej@0: snej@0: - (id) initWithKeyData: (NSData*)data { snej@0: return [self _initWithKeyData: data forKeychain: nil]; snej@0: } snej@0: snej@0: snej@0: - (SecExternalItemType) keyType { snej@0: AssertAbstractMethod(); snej@0: } snej@0: snej@0: snej@0: - (NSData*) keyData { snej@0: NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey}, snej@1: {(id)kSecAttrKeyClass, (id)self.keyType}, snej@1: {(id)kSecMatchItemList, $array((id)self.keyRef)}, snej@0: {(id)kSecReturnData, $true} ); snej@0: CFDataRef data; snej@0: if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching")) snej@0: return nil; snej@0: else snej@0: return [(id)CFMakeCollectable(data) autorelease]; snej@0: } snej@0: snej@0: snej@1: - (SecKeyRef) keyRef { snej@1: return (SecKeyRef) self.keychainItemRef; snej@0: } snej@0: snej@0: snej@0: - (id) _attribute: (CFTypeRef)attribute { snej@0: NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey}, snej@1: {(id)kSecAttrKeyClass, (id)self.keyType}, snej@1: {(id)kSecMatchItemList, $array((id)self.keyRef)}, snej@0: {(id)kSecReturnAttributes, $true} ); snej@0: CFDictionaryRef attrs; snej@0: if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching")) snej@0: return nil; snej@0: CFTypeRef rawValue = CFDictionaryGetValue(attrs,attribute); snej@0: id value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil; snej@0: CFRelease(attrs); snej@0: return value; snej@0: } snej@0: snej@0: - (BOOL) setValue: (NSString*)value ofAttribute: (SecKeychainAttrType)attribute { snej@0: if (!value) snej@0: value = (id)[NSNull null]; snej@0: NSDictionary *query = $dict( {(id)kSecClass, (id)kSecClassKey}, snej@1: {(id)kSecAttrKeyClass, (id)self.keyType}, snej@0: {(id)kSecMatchItemList, self._itemList} ); snej@0: NSDictionary *attrs = $dict( {(id)attribute, value} ); snej@0: return check(SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attrs), @"SecItemUpdate"); snej@0: } snej@0: snej@0: snej@0: - (NSString*) name { snej@0: return [self _attribute: kSecAttrLabel]; snej@0: } snej@0: snej@0: - (void) setName: (NSString*)name { snej@0: [self setValue: name ofAttribute: kSecAttrLabel]; snej@0: } snej@0: snej@0: - (NSString*) alias { snej@0: return [self _attribute: kSecAttrApplicationTag]; snej@0: } snej@0: snej@0: - (void) setAlias: (NSString*)alias { snej@0: [self setValue: alias ofAttribute: kSecAttrApplicationTag]; snej@0: } snej@0: snej@0: snej@0: @end snej@0: snej@0: snej@0: #endif USE_IPHONE_API snej@0: snej@0: snej@0: snej@0: /* snej@0: Copyright (c) 2009, Jens Alfke . All rights reserved. snej@0: snej@0: Redistribution and use in source and binary forms, with or without modification, are permitted snej@0: provided that the following conditions are met: snej@0: snej@0: * Redistributions of source code must retain the above copyright notice, this list of conditions snej@0: and the following disclaimer. snej@0: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions snej@0: and the following disclaimer in the documentation and/or other materials provided with the snej@0: distribution. snej@0: snej@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR snej@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND snej@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- snej@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES snej@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR snej@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN snej@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF snej@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. snej@0: */