2 * Copyright (c) 2018 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 <CloudKit/CloudKit_Private.h>
30 #import "keychain/ot/OTJoinWithVoucherOperation.h"
31 #import "keychain/ot/OTOperationDependencies.h"
32 #import "keychain/ot/OTFetchCKKSKeysOperation.h"
33 #import "keychain/ckks/CKKSNearFutureScheduler.h"
34 #import "keychain/ckks/CloudKitCategories.h"
36 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
37 #import "keychain/ot/ObjCImprovements.h"
38 #import "keychain/ot/OTStates.h"
40 @interface OTJoinWithVoucherOperation ()
41 @property OTOperationDependencies* deps;
43 @property OctagonState* ckksConflictState;
45 @property NSOperation* finishedOp;
48 @implementation OTJoinWithVoucherOperation
50 @synthesize intendedState = _intendedState;
52 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
53 intendedState:(OctagonState*)intendedState
54 ckksConflictState:(OctagonState*)ckksConflictState
55 errorState:(OctagonState*)errorState
57 if((self = [super init])) {
60 _intendedState = intendedState;
61 _nextState = errorState;
62 _ckksConflictState = ckksConflictState;
73 // Load the voucher from the state handler
75 OTAccountMetadataClassC* metadata = [self.deps.stateHolder loadOrCreateAccountMetadata:&error];
77 if(!metadata.voucher || !metadata.voucherSignature || error) {
78 secnotice("octagon", "No voucher available: %@", error);
83 self.voucherData = metadata.voucher;
84 self.voucherSig = metadata.voucherSignature;
86 secnotice("octagon", "joining with a voucher: %@", self.voucherData);
88 self.finishedOp = [[NSOperation alloc] init];
89 [self dependOnBeforeGroupFinished:self.finishedOp];
93 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps
95 // We only care about TLKs that are ready for upload. Don't wait long.
96 fetchKeysOp.desiredTimeout = 2*NSEC_PER_SEC;
97 [self runBeforeGroupFinished:fetchKeysOp];
99 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"vouch-with-keys"
102 [self proceedWithKeys:fetchKeysOp.viewKeySets
103 pendingTLKShares:fetchKeysOp.pendingTLKShares];
106 [proceedWithKeys addDependency:fetchKeysOp];
107 [self runBeforeGroupFinished:proceedWithKeys];
110 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets pendingTLKShares:(NSArray<CKKSTLKShare*>*)pendingTLKShares
114 NSArray<NSData*>* publicSigningSPKIs = nil;
115 if(self.deps.sosAdapter.sosEnabled) {
116 NSError* sosPreapprovalError = nil;
117 publicSigningSPKIs = [OTSOSAdapterHelpers peerPublicSigningKeySPKIsForCircle:self.deps.sosAdapter error:&sosPreapprovalError];
119 if(publicSigningSPKIs) {
120 secnotice("octagon-sos", "SOS preapproved keys are %@", publicSigningSPKIs);
122 secnotice("octagon-sos", "Unable to fetch SOS preapproved keys: %@", sosPreapprovalError);
126 secnotice("octagon-sos", "SOS not enabled; no preapproved keys");
129 [self.deps.cuttlefishXPCWrapper joinWithContainer:self.deps.containerName
130 context:self.deps.contextID
131 voucherData:self.voucherData
132 voucherSig:self.voucherSig
134 tlkShares:pendingTLKShares
135 preapprovedKeys:publicSigningSPKIs
136 reply:^(NSString * _Nullable peerID,
137 NSArray<CKRecord*>* keyHierarchyRecords,
138 TPSyncingPolicy* _Nullable syncingPolicy,
139 NSError * _Nullable error) {
142 secerror("octagon: Error joining with voucher: %@", error);
143 [[CKKSAnalytics logger] logRecoverableError:error forEvent:OctagonEventJoinWithVoucher withAttributes:NULL];
145 // IF this is a CKKS conflict error, don't retry
146 if ([error isCuttlefishError:CuttlefishErrorKeyHierarchyAlreadyExists]) {
147 secnotice("octagon-ckks", "A CKKS key hierarchy is out of date; going to state '%@'", self.ckksConflictState);
148 self.nextState = self.ckksConflictState;
149 } else if ([error isCuttlefishError:CuttlefishErrorResultGraphNotFullyReachable]) {
150 secnotice("octagon", "requesting cuttlefish health check");
151 self.nextState = OctagonStateCuttlefishTrustCheck;
157 self.peerID = peerID;
159 [[CKKSAnalytics logger] logSuccessForEventNamed:OctagonEventJoinWithVoucher];
161 [self.deps.viewManager setCurrentSyncingPolicy:syncingPolicy];
163 NSError* localError = nil;
164 BOOL persisted = [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC * _Nonnull(OTAccountMetadataClassC * _Nonnull metadata) {
165 metadata.trustState = OTAccountMetadataClassC_TrustState_TRUSTED;
166 metadata.peerID = peerID;
168 metadata.voucher = nil;
169 metadata.voucherSignature = nil;
171 [metadata setTPSyncingPolicy:syncingPolicy];
173 } error:&localError];
174 if(!persisted || localError) {
175 secnotice("octagon", "Couldn't persist results: %@", localError);
176 self.error = localError;
178 secerror("octagon: join successful");
179 self.nextState = self.intendedState;
182 // Tell CKKS about our shiny new records!
183 for (id key in self.deps.viewManager.views) {
184 CKKSKeychainView* view = self.deps.viewManager.views[key];
185 secnotice("octagon-ckks", "Providing join() records to %@", view);
186 [view receiveTLKUploadRecords: keyHierarchyRecords];
189 [self runBeforeGroupFinished:self.finishedOp];