]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTSetRecoveryKeyOperation.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / ot / OTSetRecoveryKeyOperation.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/OTSetRecoveryKeyOperation.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 OTSetRecoveryKeyOperation ()
37 @property OTOperationDependencies* deps;
38
39 @property NSOperation* finishOp;
40 @end
41
42 @implementation OTSetRecoveryKeyOperation
43
44 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
45 recoveryKey:(NSString*)recoveryKey
46 {
47 if((self = [super init])) {
48 _deps = dependencies;
49 _recoveryKey = recoveryKey;
50 }
51 return self;
52 }
53
54 - (void)groupStart
55 {
56 self.finishOp = [[NSOperation alloc] init];
57 [self dependOnBeforeGroupFinished:self.finishOp];
58
59 NSString* salt = nil;
60
61 NSError *authKitError = nil;
62 NSString *altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:&authKitError];
63
64 if (altDSID) {
65 salt = altDSID;
66 }
67 else {
68 secnotice("octagon", "authkit doesn't know about the altdsid, using stored value: %@", authKitError);
69
70 NSError* accountError = nil;
71 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
72
73 if(account && !accountError) {
74 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
75 salt = account.altDSID;
76 }
77 if(accountError || !account){
78 secerror("failed to rerieve account object: %@", accountError);
79 }
80 }
81
82 WEAKIFY(self);
83
84 OTFetchCKKSKeysOperation* fetchKeysOp = [[OTFetchCKKSKeysOperation alloc] initWithDependencies:self.deps];
85 [self runBeforeGroupFinished:fetchKeysOp];
86
87 CKKSResultOperation* proceedWithKeys = [CKKSResultOperation named:@"setting-recovery-tlks"
88 withBlock:^{
89 STRONGIFY(self);
90 [self proceedWithKeys:fetchKeysOp.viewKeySets salt:salt];
91 }];
92
93 [proceedWithKeys addDependency:fetchKeysOp];
94 [self runBeforeGroupFinished:proceedWithKeys];
95 }
96
97 - (void)proceedWithKeys:(NSArray<CKKSKeychainBackedKeySet*>*)viewKeySets salt:(NSString*)salt
98 {
99 WEAKIFY(self);
100
101 [self.deps.cuttlefishXPCWrapper setRecoveryKeyWithContainer:self.deps.containerName
102 context:self.deps.contextID
103 recoveryKey:self.recoveryKey
104 salt:salt
105 ckksKeys:viewKeySets
106 reply:^(NSError * _Nullable setError) {
107 STRONGIFY(self);
108 if(setError){
109 [[CKKSAnalytics logger] logResultForEvent:OctagonEventSetRecoveryKey hardFailure:true result:setError];
110 secerror("octagon: Error setting recovery key: %@", setError);
111 self.error = setError;
112 [self runBeforeGroupFinished:self.finishOp];
113 } else {
114 secnotice("octagon", "successfully set recovery key");
115 [self runBeforeGroupFinished:self.finishOp];
116 }
117 }];
118 }
119
120 @end
121
122 #endif // OCTAGON