5 // Created by Jens Alfke on 4/4/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import "MYCrypto_Private.h"
12 #if MYCRYPTO_USE_IPHONE_API
15 #import "MYErrorUtils.h"
22 - (id) initWithKeyRef: (SecKeyRef)key {
23 return [super initWithKeychainItemRef: (SecKeychainItemRef)key];
27 - (id) _initWithKeyData: (NSData*)data
28 forKeychain: (SecKeychainRef)keychain
30 NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
31 {(id)kSecAttrKeyClass, (id)self.keyType},
32 {(id)kSecValueData, data},
33 {(id)kSecAttrIsPermanent, $object(keychain!=nil)},
34 {(id)kSecReturnRef, $true} );
36 if (!check(SecItemAdd((CFDictionaryRef)info, (CFTypeRef*)&key), @"SecItemAdd"))
39 return [self initWithKeyRef: (SecKeyRef)key];
42 - (id) initWithKeyData: (NSData*)data {
43 return [self _initWithKeyData: data forKeychain: nil];
47 - (SecExternalItemType) keyType {
48 AssertAbstractMethod();
53 NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
54 {(id)kSecAttrKeyClass, (id)self.keyType},
55 {(id)kSecMatchItemList, $array((id)self.keyRef)},
56 {(id)kSecReturnData, $true} );
58 if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching"))
61 return [(id)CFMakeCollectable(data) autorelease];
65 - (SecKeyRef) keyRef {
66 return (SecKeyRef) self.keychainItemRef;
70 - (id) _attribute: (CFTypeRef)attribute {
71 NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
72 {(id)kSecAttrKeyClass, (id)self.keyType},
73 {(id)kSecMatchItemList, $array((id)self.keyRef)},
74 {(id)kSecReturnAttributes, $true} );
75 CFDictionaryRef attrs = NULL;
76 if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching"))
78 CFTypeRef rawValue = CFDictionaryGetValue(attrs,attribute);
79 id value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil;
84 - (BOOL) setValue: (NSString*)value ofAttribute: (SecKeychainAttrType)attribute {
86 value = (id)[NSNull null];
87 NSDictionary *query = $dict( {(id)kSecClass, (id)kSecClassKey},
88 {(id)kSecAttrKeyClass, (id)self.keyType},
89 {(id)kSecMatchItemList, self._itemList} );
90 NSDictionary *attrs = $dict( {(id)attribute, value} );
91 return check(SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attrs), @"SecItemUpdate");
96 return [self _attribute: kSecAttrLabel];
99 - (void) setName: (NSString*)name {
100 [self setValue: name ofAttribute: kSecAttrLabel];
103 - (NSString*) alias {
104 return [self _attribute: kSecAttrApplicationTag];
107 - (void) setAlias: (NSString*)alias {
108 [self setValue: alias ofAttribute: kSecAttrApplicationTag];
114 /** Asymmetric encryption/decryption; used by MYPublicKey and MYPrivateKey. */
115 - (NSData*) _crypt: (NSData*)data operation: (BOOL)operation {
117 size_t dataLength = data.length;
118 SecKeyRef key = self.keyRef;
119 size_t outputLength = MAX(dataLength, SecKeyGetBlockSize(key));
120 void *outputBuf = malloc(outputLength);
121 if (!outputBuf) return nil;
124 err = SecKeyEncrypt(key, kSecPaddingNone,//PKCS1,
125 data.bytes, dataLength,
126 outputBuf, &outputLength);
128 err = SecKeyDecrypt(key, kSecPaddingNone,//PKCS1,
129 data.bytes, dataLength,
130 outputBuf, &outputLength);
133 Warn(@"%scrypting failed (%i)", (operation ?"En" :"De"), err);
134 // Note: One of the errors I've seen is -9809, which is errSSLCrypto (SecureTransport.h)
137 return [NSData dataWithBytesNoCopy: outputBuf length: outputLength freeWhenDone: YES];
144 #endif MYCRYPTO_USE_IPHONE_API
149 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
151 Redistribution and use in source and binary forms, with or without modification, are permitted
152 provided that the following conditions are met:
154 * Redistributions of source code must retain the above copyright notice, this list of conditions
155 and the following disclaimer.
156 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
157 and the following disclaimer in the documentation and/or other materials provided with the
160 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
161 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
162 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
163 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
164 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
165 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
166 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
167 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.