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
56 voucherData:(NSData*)voucherData
57 voucherSig:(NSData*)voucherSig
59 if((self = [super init])) {
62 _intendedState = intendedState;
63 _nextState = errorState;
64 _ckksConflictState = ckksConflictState;
66 _voucherData = voucherData;
67 _voucherSig = voucherSig;
74 secnotice("octagon", "joining");
76 self.finishedOp = [[NSOperation alloc] init];
77 [self dependOnBeforeGroupFinished:self.finishedOp];
81 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps];
82 [self runBeforeGroupFinished:fetchKeysOp];
84 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"vouch-with-keys"
87 [self proceedWithKeys:fetchKeysOp.viewKeySets
88 pendingTLKShares:fetchKeysOp.pendingTLKShares];
91 [proceedWithKeys addDependency:fetchKeysOp];
92 [self runBeforeGroupFinished:proceedWithKeys];
95 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets pendingTLKShares:(NSArray<CKKSTLKShare*>*)pendingTLKShares
99 NSArray<NSData*>* publicSigningSPKIs = nil;
100 if(self.deps.sosAdapter.sosEnabled) {
101 NSError* sosPreapprovalError = nil;
102 publicSigningSPKIs = [OTSOSAdapterHelpers peerPublicSigningKeySPKIsForCircle:self.deps.sosAdapter error:&sosPreapprovalError];
104 if(publicSigningSPKIs) {
105 secnotice("octagon-sos", "SOS preapproved keys are %@", publicSigningSPKIs);
107 secnotice("octagon-sos", "Unable to fetch SOS preapproved keys: %@", sosPreapprovalError);
111 secnotice("octagon-sos", "SOS not enabled; no preapproved keys");
114 [self.deps.cuttlefishXPCWrapper joinWithContainer:self.deps.containerName
115 context:self.deps.contextID
116 voucherData:self.voucherData
117 voucherSig:self.voucherSig
119 tlkShares:pendingTLKShares
120 preapprovedKeys:publicSigningSPKIs
121 reply:^(NSString * _Nullable peerID,
122 NSArray<CKRecord*>* keyHierarchyRecords,
123 NSSet<NSString*>* _Nullable syncingViews,
124 TPPolicy* _Nullable syncingPolicy,
125 NSError * _Nullable error) {
128 secerror("octagon: Error joining with voucher: %@", error);
129 [[CKKSAnalytics logger] logRecoverableError:error forEvent:OctagonEventJoinWithVoucher withAttributes:NULL];
131 // IF this is a CKKS conflict error, don't retry
132 if ([error isCuttlefishError:CuttlefishErrorKeyHierarchyAlreadyExists]) {
133 secnotice("octagon-ckks", "A CKKS key hierarchy is out of date; going to state '%@'", self.ckksConflictState);
134 self.nextState = self.ckksConflictState;
135 } else if ([error isCuttlefishError:CuttlefishErrorResultGraphNotFullyReachable]) {
136 secnotice("octagon", "requesting cuttlefish health check");
137 self.nextState = OctagonStateCuttlefishTrustCheck;
143 self.peerID = peerID;
145 [[CKKSAnalytics logger] logSuccessForEventNamed:OctagonEventJoinWithVoucher];
147 [self.deps.viewManager setSyncingViews:syncingViews sortingPolicy:syncingPolicy];
149 NSError* localError = nil;
150 BOOL persisted = [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC * _Nonnull(OTAccountMetadataClassC * _Nonnull metadata) {
151 metadata.trustState = OTAccountMetadataClassC_TrustState_TRUSTED;
152 metadata.peerID = peerID;
153 metadata.syncingViews = [syncingViews mutableCopy];
154 [metadata setTPPolicy:syncingPolicy];
156 } error:&localError];
157 if(!persisted || localError) {
158 secnotice("octagon", "Couldn't persist results: %@", localError);
159 self.error = localError;
161 secerror("octagon: join successful");
162 self.nextState = self.intendedState;
165 // Tell CKKS about our shiny new records!
166 for (id key in self.deps.viewManager.views) {
167 CKKSKeychainView* view = self.deps.viewManager.views[key];
168 secnotice("octagon-ckks", "Providing join() records to %@", view);
169 [view receiveTLKUploadRecords: keyHierarchyRecords];
172 [self runBeforeGroupFinished:self.finishedOp];