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@
26 #include <AssertMacros.h>
27 #include "SOSAccountPriv.h"
28 #include "SOSPeerInfoCollections.h"
29 #include "SOSTransport.h"
30 #import "keychain/SecureObjectSync/SOSAccountTrust.h"
31 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
32 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
34 #define kPublicKeyAvailable "com.apple.security.publickeyavailable"
36 // MARK: User Credential management
40 // List of things to do
41 // Update myFullPeerInfo in circle if needed
42 // Fix iCloud Identity if needed
43 // Gen sign if private key changed
45 bool SOSAccountGenerationSignatureUpdate(SOSAccount* account, CFErrorRef *error) {
47 SecKeyRef priv_key = SOSAccountGetPrivateCredential(account, error);
48 require_quiet(priv_key, bail);
50 [account.trust generationSignatureUpdateWith:account key:priv_key];
57 /* this one is meant to be local - not published over KVS. */
58 static bool SOSAccountPeerSignatureUpdate(SOSAccount* account, SecKeyRef privKey, CFErrorRef *error) {
59 SOSFullPeerInfoRef identity = NULL;
60 SOSAccountTrustClassic *trust = account.trust;
61 identity = trust.fullPeerInfo;
63 return identity && SOSFullPeerInfoUpgradeSignatures(identity, privKey, error);
67 void SOSAccountPurgePrivateCredential(SOSAccount* account)
69 secnotice("circleOps", "Purging private account credential");
71 if(account.accountPrivateKey)
73 account.accountPrivateKey = NULL;
75 if(account._password_tmp)
77 account._password_tmp = NULL;
79 if (account.user_private_timer) {
80 dispatch_source_cancel(account.user_private_timer);
81 account.user_private_timer = NULL;
82 xpc_transaction_end();
84 if (account.lock_notification_token != NOTIFY_TOKEN_INVALID) {
85 notify_cancel(account.lock_notification_token);
86 account.lock_notification_token = NOTIFY_TOKEN_INVALID;
91 static void SOSAccountSetTrustedUserPublicKey(SOSAccount* account, bool public_was_trusted, SecKeyRef privKey)
94 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
96 if (account.accountKey && account.accountKeyIsTrusted && CFEqual(publicKey, account.accountKey)) {
97 CFReleaseNull(publicKey);
101 if(public_was_trusted && account.accountKey) {
102 account.previousAccountKey = account.accountKey;
105 account.accountKey = publicKey;
106 account.accountKeyIsTrusted = true;
108 if(!account.previousAccountKey) {
109 account.previousAccountKey = account.accountKey;
112 CFReleaseNull(publicKey);
114 CFStringRef keyid = SOSCopyIDOfKeyWithLength(account.accountKey, 8, NULL);
115 secnotice("circleOps", "trusting new public key: %@", keyid);
116 CFReleaseNull(keyid);
117 notify_post(kPublicKeyAvailable);
120 void SOSAccountSetUnTrustedUserPublicKey(SOSAccount* account, SecKeyRef publicKey) {
121 if(account.accountKeyIsTrusted && account.accountKey) {
122 secnotice("circleOps", "Moving : %@ to previousAccountKey", account.accountKey);
123 account.previousAccountKey = account.accountKey;
126 account.accountKey = publicKey;
127 account.accountKeyIsTrusted = false;
129 if(!account.previousAccountKey) {
130 account.previousAccountKey = account.accountKey;
132 CFStringRef keyid = SOSCopyIDOfKeyWithLength(account.accountKey, 8, NULL);
133 secnotice("circleOps", "not trusting new public key: %@", keyid);
134 CFReleaseNull(keyid);
138 static void SOSAccountSetPrivateCredential(SOSAccount* account, SecKeyRef private, CFDataRef password) {
140 return SOSAccountPurgePrivateCredential(account);
142 secnotice("circleOps", "setting new private credential");
144 account.accountPrivateKey = private;
147 account._password_tmp = [[NSData alloc] initWithData:(__bridge NSData * _Nonnull)(password)];
149 account._password_tmp = NULL;
152 bool resume_timer = false;
153 if (!account.user_private_timer) {
154 xpc_transaction_begin();
156 account.user_private_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, account.queue);
157 dispatch_source_set_event_handler(account.user_private_timer, ^{
158 secnotice("keygen", "Timing out, purging private account credential");
159 SOSAccountPurgePrivateCredential(account);
161 int lockNotification;
163 notify_register_dispatch(kUserKeybagStateChangeNotification, &lockNotification, account.queue, ^(int token) {
165 CFErrorRef lockCheckError = NULL;
167 if (!SecAKSGetIsLocked(&locked, &lockCheckError)) {
168 secerror("Checking for locked after change failed: %@", lockCheckError);
172 SOSAccountPurgePrivateCredential(account);
175 [account setLock_notification_token:lockNotification];
178 SOSAccountRestartPrivateCredentialTimer(account);
181 dispatch_resume(account.user_private_timer);
184 void SOSAccountRestartPrivateCredentialTimer(SOSAccount* account)
186 if (account.user_private_timer) {
187 // (Re)set the timer's fire time to now + 10 minutes with a 5 second fuzz factor.
188 dispatch_time_t purgeTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * 60 * NSEC_PER_SEC));
189 dispatch_source_set_timer(account.user_private_timer, purgeTime, DISPATCH_TIME_FOREVER, (int64_t)(5 * NSEC_PER_SEC));
193 SecKeyRef SOSAccountGetPrivateCredential(SOSAccount* account, CFErrorRef* error)
195 if (account.accountPrivateKey == NULL) {
196 SOSCreateError(kSOSErrorPrivateKeyAbsent, CFSTR("Private Key not available - failed to prompt user recently"), NULL, error);
198 return account.accountPrivateKey;
201 CFDataRef SOSAccountGetCachedPassword(SOSAccount* account, CFErrorRef* error)
203 if (account._password_tmp == NULL) {
204 secnotice("circleOps", "Password cache expired");
206 return (__bridge CFDataRef)(account._password_tmp);
208 static NSString *SOSUserCredentialAccount = @"SOSUserCredential";
209 static NSString *SOSUserCredentialAccessGroup = @"com.apple.security.sos-usercredential";
211 void SOSAccountStashAccountKey(SOSAccount* account)
214 SecKeyRef user_private = account.accountPrivateKey;
215 NSData *data = CFBridgingRelease(SecKeyCopyExternalRepresentation(user_private, NULL));
220 NSDictionary *attributes = @{
221 (__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword,
222 (__bridge id)kSecAttrAccount : SOSUserCredentialAccount,
223 (__bridge id)kSecAttrIsInvisible : @YES,
224 (__bridge id)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleWhenUnlocked,
225 (__bridge id)kSecAttrAccessGroup : SOSUserCredentialAccessGroup,
226 (__bridge id)kSecAttrSysBound : @(kSecSecAttrSysBoundPreserveDuringRestore),
227 (__bridge id)kSecValueData : data,
230 status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
232 if (status == errSecDuplicateItem) {
234 (__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword,
235 (__bridge id)kSecAttrAccount : SOSUserCredentialAccount,
236 (__bridge id)kSecAttrAccessGroup : SOSUserCredentialAccessGroup,
237 (__bridge id)kSecUseDataProtectionKeychain : @YES,
240 status = SecItemUpdate((__bridge CFDictionaryRef)attributes, (__bridge CFDictionaryRef)@{
241 (__bridge id)kSecValueData : data,
242 (__bridge id)kSecAttrSysBound : @(kSecSecAttrSysBoundPreserveDuringRestore)
246 secnotice("circleOps", "Failed to update user private key to keychain: %d", (int)status);
248 } else if (status != 0) {
249 secnotice("circleOps", "Failed to add user private key to keychain: %d", (int)status);
253 secnotice("circleOps", "Stored user private key stashed local keychain");
259 SecKeyRef SOSAccountCopyStashedUserPrivateKey(SOSAccount* account, CFErrorRef *error)
261 SecKeyRef key = NULL;
262 CFDataRef data = NULL;
265 NSDictionary *attributes = @{
266 (__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword,
267 (__bridge id)kSecAttrAccount : SOSUserCredentialAccount,
268 (__bridge id)kSecAttrAccessGroup : SOSUserCredentialAccessGroup,
269 (__bridge id)kSecReturnData : @YES,
270 (__bridge id)kSecMatchLimit : (__bridge id)kSecMatchLimitOne,
273 status = SecItemCopyMatching((__bridge CFDictionaryRef)attributes, (CFTypeRef *)&data);
275 SecError(status, error, CFSTR("Failed fetching account credential: %d"), (int)status);
279 NSDictionary *keyAttributes = @{
280 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
281 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
285 key = SecKeyCreateWithData(data, (__bridge CFDictionaryRef)keyAttributes, error);
291 SecKeyRef SOSAccountGetTrustedPublicCredential(SOSAccount* account, CFErrorRef* error)
293 if (account.accountKey == NULL || account.accountKeyIsTrusted == false) {
294 SOSCreateError(kSOSErrorPublicKeyAbsent, CFSTR("Public Key isn't available. The iCloud Password must be provided to the syncing subsystem to repair this."), NULL, error);
297 return account.accountKey;
300 bool SOSAccountHasPublicKey(SOSAccount* account, CFErrorRef* error)
302 return SOSAccountGetTrustedPublicCredential(account, error);
305 static void sosAccountSetTrustedCredentials(SOSAccount* account, CFDataRef user_password, SecKeyRef user_private, bool public_was_trusted) {
306 if(!SOSAccountFullPeerInfoVerify(account, user_private, NULL)){
307 (void) SOSAccountPeerSignatureUpdate(account, user_private, NULL);
309 SOSAccountSetTrustedUserPublicKey(account, public_was_trusted, user_private);
310 SOSAccountSetPrivateCredential(account, user_private, user_password);
311 SOSAccountCheckForAlwaysOnViews(account);
314 static SecKeyRef sosAccountCreateKeyIfPasswordIsCorrect(SOSAccount* account, CFDataRef user_password, CFErrorRef *error) {
315 SecKeyRef user_private = NULL;
316 require_quiet(account.accountKey && account.accountKeyDerivationParameters, errOut);
317 user_private = SOSUserKeygen(user_password, (__bridge CFDataRef)(account.accountKeyDerivationParameters), error);
318 require_quiet(user_private, errOut);
320 require_action_quiet(SOSAccountValidateAccountCredential(account, user_private, error), errOut, CFReleaseNull(user_private));
325 static bool sosAccountValidatePasswordOrFail(SOSAccount* account, CFDataRef user_password, CFErrorRef *error) {
326 SecKeyRef privKey = sosAccountCreateKeyIfPasswordIsCorrect(account, user_password, error);
328 if(account.accountKeyDerivationParameters) {
329 SecKeyRef newKey = NULL;
330 CFDataRef pbkdfParams = NULL;
331 CFErrorRef localError = NULL;
332 if(SOSAccountRetrieveCloudParameters(account, &newKey, (__bridge CFDataRef)(account.accountKeyDerivationParameters), &pbkdfParams, &localError)) {
333 debugDumpUserParameters(CFSTR("sosAccountValidatePasswordOrFail"), pbkdfParams);
335 secnotice("circleOps", "Failed to retrieve cloud parameters - %@", localError);
337 CFTransferRetained(*error, localError);
340 CFReleaseNull(newKey);
341 CFReleaseNull(pbkdfParams);
343 SOSCreateError(kSOSErrorWrongPassword, CFSTR("Could not create correct key with password."), NULL, error);
346 sosAccountSetTrustedCredentials(account, user_password, privKey, account.accountKeyIsTrusted);
347 CFReleaseNull(privKey);
351 void SOSAccountSetParameters(SOSAccount* account, CFDataRef parameters) {
352 account.accountKeyDerivationParameters = (__bridge NSData *) parameters;
355 bool SOSAccountValidateAccountCredential(SOSAccount* account, SecKeyRef accountPrivateKey, CFErrorRef *error)
358 SecKeyRef publicCandidate = SecKeyCreatePublicFromPrivate(accountPrivateKey);
360 if(CFEqualSafe(account.accountKey, publicCandidate)) {
363 CFErrorRef localError = NULL;
364 CFStringRef accountHpub = SOSCopyIDOfKey(account.accountKey, NULL);
365 CFStringRef candidateHpub = SOSCopyIDOfKey(publicCandidate, NULL);
366 SOSCreateErrorWithFormat(kSOSErrorWrongPassword, NULL, &localError, NULL, CFSTR("Password generated pubkey doesn't match - candidate: %@ known: %@"), candidateHpub, accountHpub);
367 secnotice("circleop", "Password generated pubkey doesn't match - candidate: %@ known: %@", candidateHpub, accountHpub);
372 CFReleaseNull(localError);
374 CFReleaseNull(accountHpub);
375 CFReleaseNull(candidateHpub);
377 CFReleaseNull(publicCandidate);
381 bool SOSAccountAssertStashedAccountCredential(SOSAccount* account, CFErrorRef *error)
383 SecKeyRef accountPrivateKey = NULL, publicCandidate = NULL;
386 require_action(account.accountKey, fail, SOSCreateError(kSOSErrorWrongPassword, CFSTR("account public key missing, can't check stashed copy"), NULL, error));
387 require_action(account.accountKeyIsTrusted, fail, SOSCreateError(kSOSErrorWrongPassword, CFSTR("public key no not valid, can't check stashed copy"), NULL, error));
389 accountPrivateKey = SOSAccountCopyStashedUserPrivateKey(account, error);
390 require_action_quiet(accountPrivateKey, fail, secnotice("circleOps", "Looked for a stashed private key, didn't find one"));
392 require(SOSAccountValidateAccountCredential(account, accountPrivateKey, error), fail);
394 sosAccountSetTrustedCredentials(account, NULL, accountPrivateKey, true);
398 CFReleaseSafe(publicCandidate);
399 CFReleaseSafe(accountPrivateKey);
406 bool SOSAccountAssertUserCredentials(SOSAccount* account, CFStringRef user_account, CFDataRef user_password, CFErrorRef *error)
408 bool public_was_trusted = account.accountKeyIsTrusted;
409 account.accountKeyIsTrusted = false;
410 SecKeyRef user_private = NULL;
411 CFDataRef parameters = NULL;
413 // if this succeeds, skip to the end. Success will update account.accountKeyIsTrusted by side-effect.
414 require_quiet(!sosAccountValidatePasswordOrFail(account, user_password, error), recordCred);
416 // We may or may not have parameters here.
417 // In any case we tried using them and they didn't match
418 // So forget all that and start again, assume we're the first to push anything useful.
420 if (CFDataGetLength(user_password) > 20) {
421 secwarning("Long password (>20 byte utf8) being used to derive account key – this may be a PET by mistake!!");
424 parameters = SOSUserKeyCreateGenerateParameters(error);
425 require_quiet(user_private = SOSUserKeygen(user_password, parameters, error), errOut);
426 SOSAccountSetParameters(account, parameters);
427 sosAccountSetTrustedCredentials(account, user_password, user_private, public_was_trusted);
429 CFErrorRef publishError = NULL;
430 if (!SOSAccountPublishCloudParameters(account, &publishError)) {
431 secerror("Failed to publish new cloud parameters: %@", publishError);
434 CFReleaseNull(publishError);
436 SOSAccountStashAccountKey(account);
437 SOSAccountSetValue(account, kSOSAccountName, user_account, NULL);
439 CFReleaseNull(parameters);
440 CFReleaseNull(user_private);
441 secnotice("circleop", "Setting account.key_interests_need_updating to true in SOSAccountAssertUserCredentials");
442 account.key_interests_need_updating = true;
443 return account.accountKeyIsTrusted;
447 bool SOSAccountTryUserCredentials(SOSAccount* account, CFStringRef user_account, CFDataRef user_password, CFErrorRef *error) {
448 bool success = sosAccountValidatePasswordOrFail(account, user_password, error);
450 SOSAccountStashAccountKey(account);
451 SOSAccountSetValue(account, kSOSAccountName, user_account, NULL);
453 secnotice("circleop", "Setting account.key_interests_need_updating to true in SOSAccountTryUserCredentials");
454 account.key_interests_need_updating = true;
458 bool SOSAccountTryUserPrivateKey(SOSAccount* account, SecKeyRef user_private, CFErrorRef *error) {
459 bool retval = SOSAccountValidateAccountCredential(account, user_private, error);
461 secnotice("circleOps", "Failed to accept provided user_private as credential");
464 sosAccountSetTrustedCredentials(account, NULL, user_private, account.accountKeyIsTrusted);
465 SOSAccountStashAccountKey(account);
466 secnotice("circleop", "Setting account.key_interests_need_updating to true in SOSAccountTryUserPrivateKey");
467 account.key_interests_need_updating = true;
468 secnotice("circleOps", "Accepted provided user_private as credential");
472 bool SOSAccountRetryUserCredentials(SOSAccount* account) {
473 CFDataRef cachedPassword = SOSAccountGetCachedPassword(account, NULL);
474 if (cachedPassword == NULL)
477 * SOSAccountTryUserCredentials reset the cached password internally,
478 * so we must have a retain of the password over SOSAccountTryUserCredentials().
480 CFRetain(cachedPassword);
481 bool res = SOSAccountTryUserCredentials(account, NULL, cachedPassword, NULL);
482 CFRelease(cachedPassword);