snej@0
|
1 |
//
|
snej@0
|
2 |
// MYKey-iPhone.m
|
snej@0
|
3 |
// MYCrypto-iPhone
|
snej@0
|
4 |
//
|
snej@0
|
5 |
// Created by Jens Alfke on 4/4/09.
|
snej@0
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
snej@0
|
7 |
//
|
snej@0
|
8 |
|
snej@0
|
9 |
|
snej@0
|
10 |
#import "MYCrypto_Private.h"
|
snej@0
|
11 |
|
snej@2
|
12 |
#if MYCRYPTO_USE_IPHONE_API
|
snej@0
|
13 |
|
snej@0
|
14 |
#import "MYDigest.h"
|
snej@0
|
15 |
#import "MYErrorUtils.h"
|
snej@0
|
16 |
|
snej@0
|
17 |
|
snej@0
|
18 |
#pragma mark -
|
snej@0
|
19 |
@implementation MYKey
|
snej@0
|
20 |
|
snej@0
|
21 |
|
snej@0
|
22 |
- (id) initWithKeyRef: (SecKeyRef)key {
|
snej@1
|
23 |
return [super initWithKeychainItemRef: (SecKeychainItemRef)key];
|
snej@0
|
24 |
}
|
snej@0
|
25 |
|
snej@0
|
26 |
|
snej@0
|
27 |
- (id) _initWithKeyData: (NSData*)data
|
snej@0
|
28 |
forKeychain: (SecKeychainRef)keychain
|
snej@0
|
29 |
{
|
snej@0
|
30 |
NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
|
snej@1
|
31 |
{(id)kSecAttrKeyClass, (id)self.keyType},
|
snej@0
|
32 |
{(id)kSecValueData, data},
|
snej@0
|
33 |
{(id)kSecAttrIsPermanent, $object(keychain!=nil)},
|
snej@0
|
34 |
{(id)kSecReturnRef, $true} );
|
snej@0
|
35 |
SecKeyRef key;
|
snej@0
|
36 |
if (!check(SecItemAdd((CFDictionaryRef)info, (CFTypeRef*)&key), @"SecItemAdd"))
|
snej@0
|
37 |
return nil;
|
snej@0
|
38 |
else
|
snej@0
|
39 |
return [self initWithKeyRef: (SecKeyRef)key];
|
snej@0
|
40 |
}
|
snej@0
|
41 |
|
snej@0
|
42 |
- (id) initWithKeyData: (NSData*)data {
|
snej@0
|
43 |
return [self _initWithKeyData: data forKeychain: nil];
|
snej@0
|
44 |
}
|
snej@0
|
45 |
|
snej@0
|
46 |
|
snej@0
|
47 |
- (SecExternalItemType) keyType {
|
snej@0
|
48 |
AssertAbstractMethod();
|
snej@0
|
49 |
}
|
snej@0
|
50 |
|
snej@0
|
51 |
|
snej@0
|
52 |
- (NSData*) keyData {
|
snej@0
|
53 |
NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
|
snej@1
|
54 |
{(id)kSecAttrKeyClass, (id)self.keyType},
|
snej@1
|
55 |
{(id)kSecMatchItemList, $array((id)self.keyRef)},
|
snej@0
|
56 |
{(id)kSecReturnData, $true} );
|
snej@0
|
57 |
CFDataRef data;
|
snej@0
|
58 |
if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching"))
|
snej@0
|
59 |
return nil;
|
snej@0
|
60 |
else
|
snej@0
|
61 |
return [(id)CFMakeCollectable(data) autorelease];
|
jens@16
|
62 |
|
jens@16
|
63 |
// The format of this data is not documented. There's been some reverse-engineering:
|
jens@16
|
64 |
// https://devforums.apple.com/message/32089#32089
|
jens@16
|
65 |
// Apparently it is a DER-formatted sequence of a modulus followed by an exponent.
|
jens@16
|
66 |
// This can be converted to OpenSSL format by wrapping it in some additional DER goop.
|
snej@0
|
67 |
}
|
snej@0
|
68 |
|
snej@0
|
69 |
|
snej@1
|
70 |
- (SecKeyRef) keyRef {
|
snej@1
|
71 |
return (SecKeyRef) self.keychainItemRef;
|
snej@0
|
72 |
}
|
snej@0
|
73 |
|
snej@0
|
74 |
|
snej@0
|
75 |
- (id) _attribute: (CFTypeRef)attribute {
|
snej@0
|
76 |
NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
|
snej@1
|
77 |
{(id)kSecAttrKeyClass, (id)self.keyType},
|
snej@1
|
78 |
{(id)kSecMatchItemList, $array((id)self.keyRef)},
|
snej@0
|
79 |
{(id)kSecReturnAttributes, $true} );
|
snej@2
|
80 |
CFDictionaryRef attrs = NULL;
|
snej@0
|
81 |
if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching"))
|
snej@0
|
82 |
return nil;
|
snej@0
|
83 |
CFTypeRef rawValue = CFDictionaryGetValue(attrs,attribute);
|
snej@0
|
84 |
id value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil;
|
snej@0
|
85 |
CFRelease(attrs);
|
snej@0
|
86 |
return value;
|
snej@0
|
87 |
}
|
snej@0
|
88 |
|
snej@0
|
89 |
- (BOOL) setValue: (NSString*)value ofAttribute: (SecKeychainAttrType)attribute {
|
snej@0
|
90 |
if (!value)
|
snej@0
|
91 |
value = (id)[NSNull null];
|
snej@0
|
92 |
NSDictionary *query = $dict( {(id)kSecClass, (id)kSecClassKey},
|
snej@1
|
93 |
{(id)kSecAttrKeyClass, (id)self.keyType},
|
snej@0
|
94 |
{(id)kSecMatchItemList, self._itemList} );
|
snej@0
|
95 |
NSDictionary *attrs = $dict( {(id)attribute, value} );
|
snej@0
|
96 |
return check(SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attrs), @"SecItemUpdate");
|
snej@0
|
97 |
}
|
snej@0
|
98 |
|
snej@0
|
99 |
|
snej@0
|
100 |
- (NSString*) name {
|
snej@0
|
101 |
return [self _attribute: kSecAttrLabel];
|
snej@0
|
102 |
}
|
snej@0
|
103 |
|
snej@0
|
104 |
- (void) setName: (NSString*)name {
|
snej@0
|
105 |
[self setValue: name ofAttribute: kSecAttrLabel];
|
snej@0
|
106 |
}
|
snej@0
|
107 |
|
snej@0
|
108 |
- (NSString*) alias {
|
snej@0
|
109 |
return [self _attribute: kSecAttrApplicationTag];
|
snej@0
|
110 |
}
|
snej@0
|
111 |
|
snej@0
|
112 |
- (void) setAlias: (NSString*)alias {
|
snej@0
|
113 |
[self setValue: alias ofAttribute: kSecAttrApplicationTag];
|
snej@0
|
114 |
}
|
snej@0
|
115 |
|
snej@0
|
116 |
|
snej@3
|
117 |
|
snej@3
|
118 |
|
snej@3
|
119 |
/** Asymmetric encryption/decryption; used by MYPublicKey and MYPrivateKey. */
|
snej@3
|
120 |
- (NSData*) _crypt: (NSData*)data operation: (BOOL)operation {
|
snej@3
|
121 |
CAssert(data);
|
snej@3
|
122 |
size_t dataLength = data.length;
|
snej@3
|
123 |
SecKeyRef key = self.keyRef;
|
snej@3
|
124 |
size_t outputLength = MAX(dataLength, SecKeyGetBlockSize(key));
|
snej@3
|
125 |
void *outputBuf = malloc(outputLength);
|
snej@3
|
126 |
if (!outputBuf) return nil;
|
snej@3
|
127 |
OSStatus err;
|
snej@3
|
128 |
if (operation)
|
snej@3
|
129 |
err = SecKeyEncrypt(key, kSecPaddingNone,//PKCS1,
|
snej@3
|
130 |
data.bytes, dataLength,
|
snej@3
|
131 |
outputBuf, &outputLength);
|
snej@3
|
132 |
else
|
snej@3
|
133 |
err = SecKeyDecrypt(key, kSecPaddingNone,//PKCS1,
|
snej@3
|
134 |
data.bytes, dataLength,
|
snej@3
|
135 |
outputBuf, &outputLength);
|
snej@3
|
136 |
if (err) {
|
snej@3
|
137 |
free(outputBuf);
|
snej@3
|
138 |
Warn(@"%scrypting failed (%i)", (operation ?"En" :"De"), err);
|
snej@3
|
139 |
// Note: One of the errors I've seen is -9809, which is errSSLCrypto (SecureTransport.h)
|
snej@3
|
140 |
return nil;
|
snej@3
|
141 |
} else
|
snej@3
|
142 |
return [NSData dataWithBytesNoCopy: outputBuf length: outputLength freeWhenDone: YES];
|
snej@3
|
143 |
}
|
snej@3
|
144 |
|
snej@3
|
145 |
|
snej@0
|
146 |
@end
|
snej@0
|
147 |
|
snej@0
|
148 |
|
snej@2
|
149 |
#endif MYCRYPTO_USE_IPHONE_API
|
snej@0
|
150 |
|
snej@0
|
151 |
|
snej@0
|
152 |
|
snej@0
|
153 |
/*
|
snej@0
|
154 |
Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
|
snej@0
|
155 |
|
snej@0
|
156 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
snej@0
|
157 |
provided that the following conditions are met:
|
snej@0
|
158 |
|
snej@0
|
159 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
snej@0
|
160 |
and the following disclaimer.
|
snej@0
|
161 |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
snej@0
|
162 |
and the following disclaimer in the documentation and/or other materials provided with the
|
snej@0
|
163 |
distribution.
|
snej@0
|
164 |
|
snej@0
|
165 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
snej@0
|
166 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
snej@0
|
167 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
snej@0
|
168 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
snej@0
|
169 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
snej@0
|
170 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
snej@0
|
171 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
snej@0
|
172 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
snej@0
|
173 |
*/
|