]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTVouchWithBottleOperation.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / ot / OTVouchWithBottleOperation.m
1 /*
2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if OCTAGON
25
26 #import <utilities/debugging.h>
27
28 #import "keychain/ot/OTVouchWithBottleOperation.h"
29 #import "keychain/ot/OTClientStateMachine.h"
30 #import "keychain/ot/OTCuttlefishContext.h"
31 #import "keychain/ot/OTFetchCKKSKeysOperation.h"
32
33 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
34 #import "keychain/ot/ObjCImprovements.h"
35
36 @interface OTVouchWithBottleOperation ()
37 @property OTOperationDependencies* deps;
38
39 @property NSOperation* finishedOp;
40 @end
41
42 @implementation OTVouchWithBottleOperation
43 @synthesize intendedState = _intendedState;
44
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
51 {
52 if((self = [super init])) {
53 _deps = dependencies;
54 _intendedState = intendedState;
55 _nextState = errorState;
56
57 _bottleID = bottleID;
58 _entropy = entropy;
59 _bottleSalt = bottleSalt;
60 }
61 return self;
62 }
63
64 - (void)groupStart
65 {
66 secnotice("octagon", "creating voucher using a bottle with escrow record id: %@", self.bottleID);
67
68 self.finishedOp = [[NSOperation alloc] init];
69 [self dependOnBeforeGroupFinished:self.finishedOp];
70
71 if(self.bottleSalt != nil) {
72 secnotice("octagon", "using passed in altdsid, altdsid is: %@", self.bottleSalt);
73 } else{
74 NSString* altDSID = self.deps.authKitAdapter.primaryiCloudAccountAltDSID;
75 if(altDSID){
76 secnotice("octagon", "fetched altdsid is: %@", altDSID);
77 self.bottleSalt = altDSID;
78 }
79 else {
80 NSError* accountError = nil;
81 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
82
83 if(account && !accountError) {
84 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
85 self.bottleSalt = account.altDSID;
86 }
87 if(accountError || !account){
88 secerror("failed to rerieve account object: %@", accountError);
89 }
90 }
91 }
92
93 WEAKIFY(self);
94
95 // After a vouch, we also want to acquire all TLKs that the bottled peer might have had
96 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps];
97 [self runBeforeGroupFinished:fetchKeysOp];
98
99 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"bottle-tlks"
100 withBlock:^{
101 STRONGIFY(self);
102 [self proceedWithKeys:fetchKeysOp.viewKeySets tlkShares:fetchKeysOp.tlkShares];
103 }];
104
105 [proceedWithKeys addDependency:fetchKeysOp];
106 [self runBeforeGroupFinished:proceedWithKeys];
107 }
108
109 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets tlkShares:(NSArray<CKKSTLKShare*>*)tlkShares
110 {
111 // Preflight the vouch: this will tell us the peerID of the recovering peer.
112 // Then, filter the tlkShares array to include only tlks sent to that peer.
113 WEAKIFY(self);
114 [self.deps.cuttlefishXPCWrapper preflightVouchWithBottleWithContainer:self.deps.containerName
115 context:self.deps.contextID
116 bottleID:self.bottleID
117 reply:^(NSString * _Nullable peerID, NSError * _Nullable error) {
118 STRONGIFY(self);
119 [[CKKSAnalytics logger] logResultForEvent:OctagonEventPreflightVouchWithBottle hardFailure:true result:error];
120
121 if(error){
122 secerror("octagon: Error preflighting voucher using bottle: %@", error);
123 self.error = error;
124 [self runBeforeGroupFinished:self.finishedOp];
125 return;
126 }
127
128 secnotice("octagon", "Bottle %@ is for peerID %@", self.bottleID, peerID);
129
130 NSMutableArray<CKKSTLKShare*>* filteredTLKShares = [NSMutableArray array];
131 for(CKKSTLKShare* share in tlkShares) {
132 // If we didn't get a peerID, just pass every tlkshare and hope for the best
133 if(peerID == nil || [share.receiverPeerID isEqualToString:peerID]) {
134 [filteredTLKShares addObject:share];
135 }
136 }
137
138 [self proceedWithKeys:viewKeySets filteredTLKShares:filteredTLKShares];
139 }];
140 }
141
142 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets filteredTLKShares:(NSArray<CKKSTLKShare*>*)tlkShares
143 {
144 WEAKIFY(self);
145
146 [self.deps.cuttlefishXPCWrapper vouchWithBottleWithContainer:self.deps.containerName
147 context:self.deps.contextID
148 bottleID:self.bottleID
149 entropy:self.entropy
150 bottleSalt:self.bottleSalt
151 tlkShares:tlkShares
152 reply:^(NSData * _Nullable voucher, NSData * _Nullable voucherSig, NSError * _Nullable error) {
153 STRONGIFY(self);
154 [[CKKSAnalytics logger] logResultForEvent:OctagonEventVoucherWithBottle hardFailure:true result:error];
155
156 if(error){
157 secerror("octagon: Error preparing voucher using bottle: %@", error);
158 self.error = error;
159 [self runBeforeGroupFinished:self.finishedOp];
160 return;
161 }
162
163 secnotice("octagon", "Received bottle voucher");
164
165 self.voucher = voucher;
166 self.voucherSig = voucherSig;
167 self.nextState = self.intendedState;
168 [self runBeforeGroupFinished:self.finishedOp];
169 }];
170 }
171
172 @end
173
174 #endif // OCTAGON