]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTVouchWithRecoveryKeyOperation.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / ot / OTVouchWithRecoveryKeyOperation.m
1 /*
2 * Copyright (c) 2019 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/OTVouchWithRecoveryKeyOperation.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 OTVouchWithRecoveryKeyOperation ()
37 @property OTOperationDependencies* deps;
38
39 @property NSOperation* finishOp;
40 @end
41
42 @implementation OTVouchWithRecoveryKeyOperation
43 @synthesize intendedState = _intendedState;
44
45 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
46 intendedState:(OctagonState*)intendedState
47 errorState:(OctagonState*)errorState
48 recoveryKey:(NSString*)recoveryKey
49 {
50 if((self = [super init])) {
51 _deps = dependencies;
52 _intendedState = intendedState;
53 _nextState = errorState;
54
55 _recoveryKey = recoveryKey;
56 }
57 return self;
58 }
59
60 - (void)groupStart
61 {
62 secnotice("octagon", "creating voucher using a recovery key");
63
64 self.finishOp = [[NSOperation alloc] init];
65 [self dependOnBeforeGroupFinished:self.finishOp];
66
67 NSString* salt = nil;
68
69 if(self.salt != nil) {
70 secnotice("octagon", "using passed in altdsid, altdsid is: %@", self.salt);
71 salt = self.salt;
72 } else{
73 NSString *altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:nil];
74 if(altDSID){
75 secnotice("octagon", "using auth kit adapter, altdsid is: %@", altDSID);
76 salt = altDSID;
77 }
78 else {
79 NSError* accountError = nil;
80 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
81
82 if(account && !accountError) {
83 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
84 salt = account.altDSID;
85 }
86 if(accountError || !account){
87 secerror("failed to rerieve account object: %@", accountError);
88 }
89 }
90 }
91
92 WEAKIFY(self);
93
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];
97
98 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"recovery-tlks"
99 withBlock:^{
100 STRONGIFY(self);
101 [self proceedWithKeys:fetchKeysOp.viewKeySets tlkShares:fetchKeysOp.tlkShares salt:salt];
102 }];
103
104 [proceedWithKeys addDependency:fetchKeysOp];
105 [self runBeforeGroupFinished:proceedWithKeys];
106 }
107
108 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets tlkShares:(NSArray<CKKSTLKShare*>*)tlkShares salt:(NSString*)salt
109 {
110 WEAKIFY(self);
111
112 [self.deps.cuttlefishXPCWrapper vouchWithRecoveryKeyWithContainer:self.deps.containerName
113 context:self.deps.contextID
114 recoveryKey:self.recoveryKey
115 salt:salt
116 tlkShares:tlkShares
117 reply:^(NSData * _Nullable voucher, NSData * _Nullable voucherSig, NSError * _Nullable error) {
118 STRONGIFY(self);
119 if(error){
120 [[CKKSAnalytics logger] logResultForEvent:OctagonEventVoucherWithRecoveryKey hardFailure:true result:error];
121 secerror("octagon: Error preparing voucher using recovery key: %@", error);
122 self.error = error;
123 [self runBeforeGroupFinished:self.finishOp];
124 return;
125 }
126 self.voucher = voucher;
127 self.voucherSig = voucherSig;
128 self.nextState = self.intendedState;
129 [self runBeforeGroupFinished:self.finishOp];
130 }];
131 }
132
133 @end
134
135 #endif // OCTAGON