2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <AssertMacros.h>
27 #include "SOSAccountPriv.h"
28 #include "keychain/SecureObjectSync/SOSInternal.h"
30 #include "SOSPeerInfoV2.h"
31 #include "SOSPeerInfoPriv.h"
32 #import "keychain/SecureObjectSync/SOSAccountPriv.h"
33 #import "keychain/SecureObjectSync/SOSAccountTrust.h"
34 #import "keychain/SecureObjectSync/SOSAccountTrustClassic.h"
35 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
36 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
38 static CFStringRef kicloud_identity_name = CFSTR("Cloud Identity");
40 SecKeyRef SOSAccountCopyDeviceKey(SOSAccount* account, CFErrorRef *error) {
41 SecKeyRef privateKey = NULL;
42 if(account.peerPublicKey) {
43 privateKey = SecKeyCopyMatchingPrivateKey(account.peerPublicKey, error);
45 SOSErrorCreate(kSOSErrorPeerNotFound, error, NULL, CFSTR("No identity to get key from"));
50 SOSFullPeerInfoRef CopyCloudKeychainIdentity(SOSPeerInfoRef cloudPeer, CFErrorRef *error) {
51 return SOSFullPeerInfoCreateCloudIdentity(NULL, cloudPeer, error);
55 static CF_RETURNS_RETAINED SecKeyRef GeneratePermanentFullECKey_internal(int keySize, CFStringRef name, CFTypeRef accessibility, CFBooleanRef sync, CFErrorRef* error)
57 SecKeyRef full_key = NULL;
59 CFNumberRef key_size_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &keySize);
61 CFDictionaryRef priv_key_attrs = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
62 kSecAttrIsPermanent, kCFBooleanTrue,
65 CFDictionaryRef keygen_parameters = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
66 kSecAttrKeyType, kSecAttrKeyTypeEC,
67 kSecAttrKeySizeInBits, key_size_num,
68 kSecPrivateKeyAttrs, priv_key_attrs,
69 kSecAttrAccessible, accessibility,
70 kSecAttrAccessGroup, kSOSInternalAccessGroup,
72 kSecAttrSynchronizable, sync,
73 kSecUseTombstones, kCFBooleanTrue,
76 CFReleaseNull(priv_key_attrs);
78 CFReleaseNull(key_size_num);
79 OSStatus status = SecKeyGeneratePair(keygen_parameters, NULL, &full_key);
80 CFReleaseNull(keygen_parameters);
83 secerror("status: %ld", (long)status);
84 if (status != errSecSuccess && error != NULL && *error == NULL) {
85 *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainOSStatus, status, NULL);
91 CF_RETURNS_RETAINED SecKeyRef GeneratePermanentFullECKey(int keySize, CFStringRef name, CFErrorRef* error) {
92 return GeneratePermanentFullECKey_internal(keySize, name, kSecAttrAccessibleWhenUnlockedThisDeviceOnly, kCFBooleanFalse, error);
95 static CF_RETURNS_RETAINED SecKeyRef GeneratePermanentFullECKeyForCloudIdentity(int keySize, CFStringRef name, CFErrorRef* error) {
96 return GeneratePermanentFullECKey_internal(keySize, name, kSecAttrAccessibleWhenUnlocked, kCFBooleanTrue, error);
99 static SecKeyRef sosKeyForLabel(CFStringRef label) {
100 CFTypeRef queryResult = NULL;
101 SecKeyRef retval = NULL;
102 CFDictionaryRef query = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
103 kSecMatchLimit, kSecMatchLimitOne,
104 kSecClass, kSecClassKey,
105 kSecAttrKeyClass, kSecAttrKeyClassPrivate,
106 kSecAttrSynchronizable, kSecAttrSynchronizableAny,
107 kSecAttrAccessGroup, kSOSInternalAccessGroup,
108 kSecAttrLabel, label,
109 kSecReturnRef, kCFBooleanTrue,
111 OSStatus stat = SecItemCopyMatching(query, &queryResult);
112 if(errSecSuccess == stat) {
113 retval = (SecKeyRef) queryResult;
114 secnotice("iCloudIdentity", "Got key for label (%@)", label);
116 secnotice("iCloudIdentity", "Failed query(%d) for %@", (int) stat, label);
118 CFReleaseNull(query);
122 void SOSiCloudIdentityPrivateKeyForEach(void (^complete)(SecKeyRef privKey)) {
123 CFTypeRef queryResult = NULL;
124 CFArrayRef iCloudPrivKeys = NULL;
126 CFDictionaryRef query = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
127 kSecMatchLimit, kSecMatchLimitAll,
128 kSecClass, kSecClassKey,
129 kSecAttrKeyClass, kSecAttrKeyClassPrivate,
130 kSecAttrSynchronizable, kSecAttrSynchronizableAny,
131 kSecAttrAccessGroup, kSOSInternalAccessGroup,
132 kSecReturnAttributes, kCFBooleanTrue,
135 if((errSecSuccess == SecItemCopyMatching(query, &queryResult)) && (iCloudPrivKeys = asArray(queryResult, NULL))) {
136 secnotice("iCloudIdentity", "Screening %ld icloud private key candidates", (long)CFArrayGetCount(iCloudPrivKeys));
137 CFReleaseNull(query);
139 secnotice("iCloudIdentity", "Can't get iCloud Identity private key candidates");
140 CFReleaseNull(query);
141 CFReleaseNull(queryResult);
145 CFArrayForEach(iCloudPrivKeys, ^(const void *value) {
146 CFDictionaryRef privKeyDict = (CFDictionaryRef) value;
147 CFStringRef label = CFDictionaryGetValue(privKeyDict, kSecAttrLabel);
151 if(CFStringHasPrefix(label, CFSTR("Cloud Identity"))) {
152 SecKeyRef privKey = sosKeyForLabel(label);
155 CFReleaseNull(privKey);
159 CFReleaseNull(queryResult);
162 bool SOSAccountHasCircle(SOSAccount* account, CFErrorRef* error) {
163 SOSAccountTrustClassic *trust = account.trust;
164 SOSCircleRef circle = trust.trustedCircle;
167 SOSCreateErrorWithFormat(kSOSErrorNoCircle, NULL, error, NULL, CFSTR("No trusted circle"));
169 return circle != NULL;
172 bool SOSAccountHasFullPeerInfo(SOSAccount* account, CFErrorRef* error) {
173 bool hasPeer = false;
174 SOSAccountTrustClassic *trust = account.trust;
175 SOSFullPeerInfoRef identity = trust.fullPeerInfo;
177 require(SOSAccountHasCircle(account, error), fail);
179 hasPeer = identity != NULL;
182 SOSCreateErrorWithFormat(kSOSErrorPeerNotFound, NULL, error, NULL, CFSTR("No peer for circle"));
188 bool SOSAccountIsAccountIdentity(SOSAccount* account, SOSPeerInfoRef peer_info, CFErrorRef *error)
190 SOSFullPeerInfoRef identity = NULL;
192 SOSAccountTrustClassic *trust = account.trust;
193 identity = trust.fullPeerInfo;
194 return CFEqualSafe(peer_info, SOSFullPeerInfoGetPeerInfo(identity));
197 bool SOSAccountFullPeerInfoVerify(SOSAccount* account, SecKeyRef privKey, CFErrorRef *error) {
198 SOSFullPeerInfoRef identity = NULL;
200 SOSAccountTrustClassic *trust = account.trust;
201 identity = trust.fullPeerInfo;
202 if(!identity) return false;
203 SecKeyRef pubKey = SecKeyCreatePublicFromPrivate(privKey);
204 bool retval = SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(identity), pubKey, error);
205 CFReleaseNull(pubKey);
209 static bool UpdateKeyName(SecKeyRef key, SOSPeerInfoRef peer, CFErrorRef* error)
211 CFDictionaryRef query = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
212 kSecClass, kSecClassKey,
213 kSecAttrSynchronizable,kCFBooleanTrue,
214 kSecUseTombstones, kCFBooleanTrue,
218 CFStringRef new_name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
219 CFSTR("Cloud Identity - '%@'"), SOSPeerInfoGetPeerID(peer));
221 CFDictionaryRef change = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
222 kSecAttrLabel, new_name,
225 bool result = SecError(SecItemUpdate(query, change), error, CFSTR("Couldn't update name"));
227 CFReleaseNull(new_name);
228 CFReleaseNull(query);
229 CFReleaseNull(change);
233 SOSPeerInfoRef GenerateNewCloudIdentityPeerInfo(CFErrorRef *error) {
234 SecKeyRef cloud_key = GeneratePermanentFullECKeyForCloudIdentity(256, kicloud_identity_name, error);
235 SOSPeerInfoRef cloud_peer = NULL;
237 CFDictionaryRef gestalt = NULL;
239 require_action_quiet(cloud_key, fail, SecError(errSecAllocate, error, CFSTR("Can't generate keypair for icloud identity")));
241 gestalt = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
242 kPIUserDefinedDeviceNameKey, CFSTR("iCloud"),
244 require_action_quiet(gestalt, fail, SecError(errSecAllocate, error, CFSTR("Can't allocate gestalt")));
246 cloud_peer = SOSPeerInfoCreateCloudIdentity(kCFAllocatorDefault, gestalt, cloud_key, error);
248 require(cloud_peer, fail);
250 UpdateKeyName(cloud_key, cloud_peer, error);
253 CFReleaseNull(gestalt);
254 CFReleaseNull(cloud_key);
260 bool SOSAccountUpdatePeerInfo(SOSAccount* account, CFStringRef updateDescription, CFErrorRef *error, bool (^update)(SOSFullPeerInfoRef fpi, CFErrorRef *error)) {
262 if (!account.hasPeerInfo)
265 bool result = update(account.fullPeerInfo, error);
267 if (result && SOSAccountHasCircle(account, NULL)) {
268 return [account.trust modifyCircle:account.circle_transport err:error action:^(SOSCircleRef circle_to_change) {
269 secnotice("circleChange", "Calling SOSCircleUpdatePeerInfo for %@", updateDescription);
270 return SOSCircleUpdatePeerInfo(circle_to_change, account.peerInfo);
278 bool SOSAccountUpdatePeerInfoAndPush(SOSAccount* account, CFStringRef updateDescription, CFErrorRef *error,
279 bool (^update)(SOSPeerInfoRef pi, CFErrorRef *error)) {
280 return SOSAccountUpdatePeerInfo(account, updateDescription, error, ^bool(SOSFullPeerInfoRef fpi, CFErrorRef *localError) {
281 return SOSFullPeerInfoUpdate(fpi, localError, ^SOSPeerInfoRef(SOSPeerInfoRef pi, SecKeyRef peerPriv, CFErrorRef *localError) {
282 SOSPeerInfoRef newPI = SOSPeerInfoCreateCopy(kCFAllocatorDefault, pi, localError);
283 if(update(newPI, error)) {
284 if(peerPriv && SOSPeerInfoSign(peerPriv, newPI, localError)) {
285 secnotice("circleOp", "Signed Peerinfo to update");
289 secnotice("circleOp", "Failed updating PeerInfo");
290 CFReleaseNull(newPI);