]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTFetchViewsOperation.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ot / OTFetchViewsOperation.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 "keychain/ot/OTFetchViewsOperation.h"
27 #import "keychain/ot/ObjCImprovements.h"
28 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
29 #import "keychain/ckks/CKKSAnalytics.h"
30
31 @interface OTFetchViewsOperation ()
32 @property OTOperationDependencies* deps;
33 @property NSOperation* finishedOp;
34 @property CKKSViewManager* ckm;
35 @end
36
37 @implementation OTFetchViewsOperation
38
39 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
40 {
41 if ((self = [super init])) {
42 _deps = dependencies;
43 _ckm = dependencies.viewManager;
44 }
45 return self;
46 }
47
48 - (void)groupStart
49 {
50 secnotice("octagon", "fetching views");
51
52 self.finishedOp = [[NSOperation alloc] init];
53 [self dependOnBeforeGroupFinished:self.finishedOp];
54
55 NSSet<NSString*>* sosViewList = [self.ckm viewList];
56 self.policy = nil;
57 self.viewList = sosViewList;
58
59 if ([self.ckm useCKKSViewsFromPolicy]) {
60 WEAKIFY(self);
61
62 NSXPCConnection<TrustedPeersHelperProtocol>* proxy = [self.deps.cuttlefishXPC remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
63 STRONGIFY(self);
64 secerror("octagon: Can't talk with TrustedPeersHelper: %@", error);
65 [[CKKSAnalytics logger] logUnrecoverableError:error forEvent:OctagonEventFetchViews withAttributes:nil];
66 self.error = error;
67 [self runBeforeGroupFinished:self.finishedOp];
68 }];
69 if (proxy) {
70 [proxy fetchPolicyWithContainer:self.deps.containerName context:self.deps.contextID reply:^(TPPolicy* _Nullable policy, NSError* _Nullable error) {
71 STRONGIFY(self);
72 if (error) {
73 secerror("octagon: failed to retrieve policy: %@", error);
74 [[CKKSAnalytics logger] logResultForEvent:OctagonEventFetchViews hardFailure:true result:error];
75 self.error = error;
76 [self runBeforeGroupFinished:self.finishedOp];
77 } else {
78 if (policy == nil) {
79 secerror("octagon: no policy returned");
80 }
81 self.policy = policy;
82 WEAKIFY(self);
83 NSArray<NSString*>* sosViews = [sosViewList allObjects];
84 [proxy getViewsWithContainer:self.deps.containerName context:self.deps.contextID inViews:sosViews reply:^(NSArray<NSString*>* _Nullable outViews, NSError* _Nullable error) {
85 STRONGIFY(self);
86 if (error) {
87 secerror("octagon: failed to retrieve list of views: %@", error);
88 [[CKKSAnalytics logger] logResultForEvent:OctagonEventFetchViews hardFailure:true result:error];
89 self.error = error;
90 [self runBeforeGroupFinished:self.finishedOp];
91 } else {
92 if (outViews == nil) {
93 secerror("octagon: bad results from getviews");
94 } else {
95 self.viewList = [NSSet setWithArray:outViews];
96 }
97 [self complete];
98 }
99 }];
100 }
101 }];
102 }
103 } else {
104 [self complete];
105 }
106 }
107
108 - (void)complete {
109 secnotice("octagon", "viewList: %@", self.viewList);
110 self.ckm.policy = self.policy;
111 self.ckm.viewList = self.viewList;
112
113 [self.ckm createViews];
114 [self.ckm beginCloudKitOperationOfAllViews];
115 [self runBeforeGroupFinished:self.finishedOp];
116 }
117
118 @end
119
120 #endif // OCTAGON