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 "keychain/ot/OTVouchWithBottleOperation.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 OTVouchWithBottleOperation ()
37 @property OTOperationDependencies* deps;
39 @property NSOperation* finishedOp;
42 @implementation OTVouchWithBottleOperation
43 @synthesize intendedState = _intendedState;
45 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
46 intendedState:(OctagonState*)intendedState
47 errorState:(OctagonState*)errorState
48 bottleID:(NSString*)bottleID
49 entropy:(NSData*)entropy
50 bottleSalt:(NSString*)bottleSalt
52 if((self = [super init])) {
54 _intendedState = intendedState;
55 _nextState = errorState;
59 _bottleSalt = bottleSalt;
66 secnotice("octagon", "creating voucher using a bottle with escrow record id: %@", self.bottleID);
68 self.finishedOp = [[NSOperation alloc] init];
69 [self dependOnBeforeGroupFinished:self.finishedOp];
71 if(self.bottleSalt != nil) {
72 secnotice("octagon", "using passed in altdsid, altdsid is: %@", self.bottleSalt);
76 NSString* altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:&error];
78 secnotice("octagon", "fetched altdsid is: %@", altDSID);
79 self.bottleSalt = altDSID;
82 secnotice("octagon", "authkit doesn't know about the altdsid, using stored value: %@", error);
84 NSError* accountError = nil;
85 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
87 if(account && !accountError) {
88 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
89 self.bottleSalt = account.altDSID;
91 if(accountError || !account){
92 secerror("failed to rerieve account object: %@", accountError);
99 // After a vouch, we also want to acquire all TLKs that the bottled peer might have had
100 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps];
101 [self runBeforeGroupFinished:fetchKeysOp];
103 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"bottle-tlks"
106 [self proceedWithKeys:fetchKeysOp.viewKeySets tlkShares:fetchKeysOp.tlkShares];
109 [proceedWithKeys addDependency:fetchKeysOp];
110 [self runBeforeGroupFinished:proceedWithKeys];
113 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets tlkShares:(NSArray<CKKSTLKShare*>*)tlkShares
115 // Preflight the vouch: this will tell us the peerID of the recovering peer.
116 // Then, filter the tlkShares array to include only tlks sent to that peer.
118 [self.deps.cuttlefishXPCWrapper preflightVouchWithBottleWithContainer:self.deps.containerName
119 context:self.deps.contextID
120 bottleID:self.bottleID
121 reply:^(NSString * _Nullable peerID, NSError * _Nullable error) {
123 [[CKKSAnalytics logger] logResultForEvent:OctagonEventPreflightVouchWithBottle hardFailure:true result:error];
126 secerror("octagon: Error preflighting voucher using bottle: %@", error);
128 [self runBeforeGroupFinished:self.finishedOp];
132 secnotice("octagon", "Bottle %@ is for peerID %@", self.bottleID, peerID);
134 NSMutableArray<CKKSTLKShare*>* filteredTLKShares = [NSMutableArray array];
135 for(CKKSTLKShare* share in tlkShares) {
136 // If we didn't get a peerID, just pass every tlkshare and hope for the best
137 if(peerID == nil || [share.receiverPeerID isEqualToString:peerID]) {
138 [filteredTLKShares addObject:share];
142 [self proceedWithKeys:viewKeySets filteredTLKShares:filteredTLKShares];
146 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets filteredTLKShares:(NSArray<CKKSTLKShare*>*)tlkShares
150 [self.deps.cuttlefishXPCWrapper vouchWithBottleWithContainer:self.deps.containerName
151 context:self.deps.contextID
152 bottleID:self.bottleID
154 bottleSalt:self.bottleSalt
156 reply:^(NSData * _Nullable voucher, NSData * _Nullable voucherSig, NSError * _Nullable error) {
158 [[CKKSAnalytics logger] logResultForEvent:OctagonEventVoucherWithBottle hardFailure:true result:error];
161 secerror("octagon: Error preparing voucher using bottle: %@", error);
163 [self runBeforeGroupFinished:self.finishedOp];
167 secnotice("octagon", "Received bottle voucher");
169 self.voucher = voucher;
170 self.voucherSig = voucherSig;
171 self.nextState = self.intendedState;
172 [self runBeforeGroupFinished:self.finishedOp];