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);
74 if(self.deps.authKitAdapter.primaryiCloudAccountAltDSID){
75 secnotice("octagon", "using auth kit adapter, altdsid is: %@", self.deps.authKitAdapter.primaryiCloudAccountAltDSID);
76 self.bottleSalt = self.deps.authKitAdapter.primaryiCloudAccountAltDSID;
79 NSError* accountError = nil;
80 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
82 if(account && !accountError) {
83 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
84 self.bottleSalt = account.altDSID;
86 if(accountError || !account){
87 secerror("failed to rerieve account object: %@", accountError);
94 // After a vouch, we also want to acquire all TLKs that the bottled peer might have had
95 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps];
96 [self runBeforeGroupFinished:fetchKeysOp];
98 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"bottle-tlks"
101 [self proceedWithKeys:fetchKeysOp.viewKeySets tlkShares:fetchKeysOp.tlkShares];
104 [proceedWithKeys addDependency:fetchKeysOp];
105 [self runBeforeGroupFinished:proceedWithKeys];
108 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets tlkShares:(NSArray<CKKSTLKShare*>*)tlkShares
112 [[self.deps.cuttlefishXPC remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
114 secerror("octagon: Can't talk with TrustedPeersHelper: %@", error);
115 [[CKKSAnalytics logger] logRecoverableError:error forEvent:OctagonEventVoucherWithBottle withAttributes:NULL];
117 [self runBeforeGroupFinished:self.finishedOp];
119 }] vouchWithBottleWithContainer:self.deps.containerName
120 context:self.deps.contextID
121 bottleID:self.bottleID
123 bottleSalt:self.bottleSalt
125 reply:^(NSData * _Nullable voucher, NSData * _Nullable voucherSig, NSError * _Nullable error) {
126 [[CKKSAnalytics logger] logResultForEvent:OctagonEventVoucherWithBottle hardFailure:true result:error];
129 secerror("octagon: Error preparing voucher using bottle: %@", error);
131 [self runBeforeGroupFinished:self.finishedOp];
135 secnotice("octagon", "Received bottle voucher");
137 self.voucher = voucher;
138 self.voucherSig = voucherSig;
139 self.nextState = self.intendedState;
140 [self runBeforeGroupFinished:self.finishedOp];