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/OTVouchWithRecoveryKeyOperation.h"
29 #import "keychain/ot/OTClientStateMachine.h"
30 #import "keychain/ot/OTCuttlefishContext.h"
31 #import "keychain/ot/OTFetchCKKSKeysOperation.h"
32 #import "keychain/ot/OTStates.h"
34 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
35 #import "keychain/ot/ObjCImprovements.h"
37 @interface OTVouchWithRecoveryKeyOperation ()
38 @property OTOperationDependencies* deps;
40 @property NSString* salt;
41 @property NSString* recoveryKey;
43 @property NSOperation* finishOp;
46 @implementation OTVouchWithRecoveryKeyOperation
47 @synthesize intendedState = _intendedState;
49 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
50 intendedState:(OctagonState*)intendedState
51 errorState:(OctagonState*)errorState
52 recoveryKey:(NSString*)recoveryKey
53 saveVoucher:(BOOL)saveVoucher
55 if((self = [super init])) {
57 _intendedState = intendedState;
58 _nextState = errorState;
60 _recoveryKey = recoveryKey;
62 _saveVoucher = saveVoucher;
69 secnotice("octagon", "creating voucher using a recovery key");
71 self.finishOp = [[NSOperation alloc] init];
72 [self dependOnBeforeGroupFinished:self.finishOp];
74 NSString *altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:nil];
76 secnotice("octagon", "using auth kit adapter, altdsid is: %@", altDSID);
80 NSError* accountError = nil;
81 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
83 if(account && !accountError) {
84 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
85 self.salt = account.altDSID;
87 if(accountError || !account){
88 secerror("failed to rerieve account object: %@", accountError);
92 // First, let's preflight the vouch (to receive a policy and view set to use for TLK fetching
94 [self.deps.cuttlefishXPCWrapper preflightVouchWithRecoveryKeyWithContainer:self.deps.containerName
95 context:self.deps.contextID
96 recoveryKey:self.recoveryKey
98 reply:^(NSString * _Nullable recoveryKeyID,
99 TPSyncingPolicy* _Nullable peerSyncingPolicy,
100 NSError * _Nullable error) {
102 [[CKKSAnalytics logger] logResultForEvent:OctagonEventPreflightVouchWithRecoveryKey hardFailure:true result:error];
104 if(error || !recoveryKeyID) {
105 secerror("octagon: Error preflighting voucher using recovery key: %@", error);
107 [self runBeforeGroupFinished:self.finishOp];
111 secnotice("octagon", "Recovery key ID %@ looks good to go", recoveryKeyID);
113 // Tell CKKS to spin up the new views and policy
114 // But, do not persist this view set! We'll do that when we actually manage to join
115 [self.deps.viewManager setCurrentSyncingPolicy:peerSyncingPolicy];
117 [self proceedWithRecoveryKeyID:recoveryKeyID];
121 - (void)proceedWithRecoveryKeyID:(NSString*)recoveryKeyID
125 // After a vouch, we also want to acquire all TLKs that the bottled peer might have had
126 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps
128 [self runBeforeGroupFinished:fetchKeysOp];
130 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"recovery-tlks"
134 NSMutableArray<CKKSTLKShare*>* filteredTLKShares = [NSMutableArray array];
135 for(CKKSTLKShare* share in fetchKeysOp.tlkShares) {
136 // If we didn't get a recoveryKeyID, just pass every tlkshare and hope for the best
137 if(recoveryKeyID == nil || [share.receiverPeerID isEqualToString:recoveryKeyID]) {
138 [filteredTLKShares addObject:share];
142 if(fetchKeysOp.viewsTimedOutWithoutKeysets.count > 0) {
143 // At least one view failed to find a keyset in time.
144 // Set up a retry with this recovery key, once CKKS is done fetching
145 secnotice("octagon", "Timed out fetching key hierarchy for CKKS views; marking for TLK recovery follow up: %@", fetchKeysOp.viewsTimedOutWithoutKeysets);
146 OctagonPendingFlag* flag = [[OctagonPendingFlag alloc] initWithFlag:OctagonFlagAttemptRecoveryKeyTLKExtraction
147 after:self.deps.viewManager.zoneChangeFetcher.inflightFetch];
148 [self.deps.flagHandler handlePendingFlag:flag];
151 [self proceedWithKeys:fetchKeysOp.viewKeySets tlkShares:filteredTLKShares salt:self.salt];
154 [proceedWithKeys addDependency:fetchKeysOp];
155 [self runBeforeGroupFinished:proceedWithKeys];
158 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets tlkShares:(NSArray<CKKSTLKShare*>*)tlkShares salt:(NSString*)salt
162 [self.deps.cuttlefishXPCWrapper vouchWithRecoveryKeyWithContainer:self.deps.containerName
163 context:self.deps.contextID
164 recoveryKey:self.recoveryKey
167 reply:^(NSData * _Nullable voucher, NSData * _Nullable voucherSig, NSError * _Nullable error) {
170 [[CKKSAnalytics logger] logResultForEvent:OctagonEventVoucherWithRecoveryKey hardFailure:true result:error];
171 secerror("octagon: Error preparing voucher using recovery key: %@", error);
173 [self runBeforeGroupFinished:self.finishOp];
176 self.voucher = voucher;
177 self.voucherSig = voucherSig;
179 if(self.saveVoucher) {
180 secnotice("octagon", "Saving voucher for later use...");
181 NSError* saveError = nil;
182 [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC * _Nullable(OTAccountMetadataClassC * _Nonnull metadata) {
183 metadata.voucher = voucher;
184 metadata.voucherSignature = voucherSig;
188 secnotice("octagon", "unable to save voucher: %@", saveError);
189 [self runBeforeGroupFinished:self.finishOp];
194 secnotice("octagon", "Successfully vouched with a recovery key: %@, %@", voucher, voucherSig);
195 self.nextState = self.intendedState;
196 [self runBeforeGroupFinished:self.finishOp];