2 * Copyright (c) 2019 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 #import <utilities/debugging.h>
28 #import "keychain/ot/OTSetRecoveryKeyOperation.h"
29 #import "keychain/ot/OTClientStateMachine.h"
30 #import "keychain/ot/OTCuttlefishContext.h"
31 #import "keychain/ot/OTFetchCKKSKeysOperation.h"
33 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
34 #import "keychain/ot/ObjCImprovements.h"
36 @interface OTSetRecoveryKeyOperation ()
37 @property OTOperationDependencies* deps;
39 @property NSOperation* finishOp;
42 @implementation OTSetRecoveryKeyOperation
44 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
45 recoveryKey:(NSString*)recoveryKey
47 if((self = [super init])) {
49 _recoveryKey = recoveryKey;
56 self.finishOp = [[NSOperation alloc] init];
57 [self dependOnBeforeGroupFinished:self.finishOp];
61 NSError *authKitError = nil;
62 NSString *altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:&authKitError];
68 secnotice("octagon", "authkit doesn't know about the altdsid, using stored value: %@", authKitError);
70 NSError* accountError = nil;
71 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
73 if(account && !accountError) {
74 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
75 salt = account.altDSID;
77 if(accountError || !account){
78 secerror("failed to rerieve account object: %@", accountError);
84 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps
86 [self runBeforeGroupFinished:fetchKeysOp];
88 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"setting-recovery-tlks"
91 [self proceedWithKeys:fetchKeysOp.viewKeySets salt:salt];
94 [proceedWithKeys addDependency:fetchKeysOp];
95 [self runBeforeGroupFinished:proceedWithKeys];
98 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets salt:(NSString*)salt
102 [self.deps.cuttlefishXPCWrapper setRecoveryKeyWithContainer:self.deps.containerName
103 context:self.deps.contextID
104 recoveryKey:self.recoveryKey
107 reply:^(NSArray<CKRecord*>* _Nullable keyHierarchyRecords,
108 NSError * _Nullable setError) {
111 [[CKKSAnalytics logger] logResultForEvent:OctagonEventSetRecoveryKey hardFailure:true result:setError];
112 secerror("octagon: Error setting recovery key: %@", setError);
113 self.error = setError;
114 [self runBeforeGroupFinished:self.finishOp];
116 secnotice("octagon", "successfully set recovery key");
118 for (id key in self.deps.viewManager.views) {
119 CKKSKeychainView* view = self.deps.viewManager.views[key];
120 secnotice("octagon-ckks", "Providing setRecoveryKey() records to %@", view);
121 [view receiveTLKUploadRecords:keyHierarchyRecords];
123 [self runBeforeGroupFinished:self.finishOp];