]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTFollowup.m
Security-59306.80.4.tar.gz
[apple/security.git] / keychain / ot / OTFollowup.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 "OTFollowup.h"
27
28 #if __has_include(<CoreFollowUp/CoreFollowUp.h>) && !TARGET_OS_SIMULATOR
29 #import <CoreFollowUp/CoreFollowUp.h>
30 #define HAVE_COREFOLLOW_UP 1
31 #endif
32
33 #import <CoreCDP/CDPFollowUpController.h>
34 #import <CoreCDP/CDPFollowUpContext.h>
35
36 #include "utilities/debugging.h"
37
38 static NSString * const kOTFollowupEventCompleteKey = @"OTFollowupContextType";
39
40 NSString* OTFollowupContextTypeToString(OTFollowupContextType contextType)
41 {
42 switch(contextType) {
43 case OTFollowupContextTypeNone:
44 return @"none";
45 case OTFollowupContextTypeRecoveryKeyRepair:
46 return @"recovery key";
47 case OTFollowupContextTypeStateRepair:
48 return @"repair";
49 case OTFollowupContextTypeOfflinePasscodeChange:
50 return @"offline passcode change";
51 }
52 }
53
54 @interface OTFollowup()
55 @property id<OctagonFollowUpControllerProtocol> cdpd;
56 @property NSTimeInterval previousFollowupEnd;
57 @property NSTimeInterval followupStart;
58 @property NSTimeInterval followupEnd;
59 @end
60
61 @implementation OTFollowup : NSObject
62
63 - (id)initWithFollowupController:(id<OctagonFollowUpControllerProtocol>)cdpFollowupController
64 {
65 if (self = [super init]) {
66 self.cdpd = cdpFollowupController;
67 }
68 return self;
69 }
70
71 - (CDPFollowUpContext *)createCDPFollowupContext:(OTFollowupContextType)contextType
72 {
73 switch (contextType) {
74 case OTFollowupContextTypeStateRepair: {
75 return [CDPFollowUpContext contextForStateRepair];
76 }
77 case OTFollowupContextTypeRecoveryKeyRepair: {
78 return [CDPFollowUpContext contextForRecoveryKeyRepair];
79 }
80 case OTFollowupContextTypeOfflinePasscodeChange: {
81 return [CDPFollowUpContext contextForOfflinePasscodeChange];
82 }
83 default: {
84 return nil;
85 }
86 }
87 }
88
89 - (BOOL)postFollowUp:(OTFollowupContextType)contextType
90 error:(NSError **)error
91 {
92 CDPFollowUpContext *context = [self createCDPFollowupContext:contextType];
93 if (!context) {
94 return NO;
95 }
96
97 NSError *followupError = nil;
98 BOOL result = [self.cdpd postFollowUpWithContext:context error:&followupError];
99 if (error) {
100 *error = followupError;
101 }
102
103 return result;
104 }
105
106 - (BOOL)clearFollowUp:(OTFollowupContextType)contextType
107 error:(NSError **)error
108 {
109 // Note(caw): we don't track metrics for clearing CFU prompts.
110 CDPFollowUpContext *context = [self createCDPFollowupContext:contextType];
111 if (!context) {
112 return NO;
113 }
114
115 return [self.cdpd clearFollowUpWithContext:context error:error];
116 }
117
118
119 - (NSDictionary *)sysdiagnoseStatus
120 {
121 NSMutableDictionary *pendingCFUs = nil;
122
123 #if HAVE_COREFOLLOW_UP
124 if ([FLFollowUpController class]) {
125 NSError *error = nil;
126 pendingCFUs = [NSMutableDictionary dictionary];
127
128 FLFollowUpController *followUpController = [[FLFollowUpController alloc] initWithClientIdentifier:@"com.apple.corecdp"];
129 NSArray <FLFollowUpItem*>* followUps = [followUpController pendingFollowUpItems:&error];
130 if (error) {
131 secnotice("octagon", "Fetching pending follow ups failed with: %@", error);
132 pendingCFUs[@"error"] = [error description];
133 }
134 for (FLFollowUpItem *followUp in followUps) {
135 NSDate *creationDate = followUp.notification.creationDate;
136 pendingCFUs[followUp.uniqueIdentifier] = creationDate;
137 }
138 }
139 #endif
140 return pendingCFUs;
141 }
142
143 - (NSDictionary<NSString*,NSNumber *> *)sfaStatus {
144 NSMutableDictionary<NSString*, NSNumber*>* values = [NSMutableDictionary dictionary];
145 #if HAVE_COREFOLLOW_UP
146 if ([FLFollowUpController class]) {
147 NSError *error = nil;
148
149 //pretend to be CDP
150 FLFollowUpController *followUpController = [[FLFollowUpController alloc] initWithClientIdentifier:@"com.apple.corecdp"];
151
152 NSArray <FLFollowUpItem*>* followUps = [followUpController pendingFollowUpItems:&error];
153 if (error) {
154 secnotice("octagon", "Fetching pending follow ups failed with: %@", error);
155 }
156 for (FLFollowUpItem *followUp in followUps) {
157 NSInteger created = 10000;
158
159 NSDate *creationDate = followUp.notification.creationDate;
160 if (creationDate) {
161 created = [CKKSAnalytics fuzzyDaysSinceDate:creationDate];
162 }
163 NSString *key = [NSString stringWithFormat:@"OACFU-%@", followUp.uniqueIdentifier];
164 values[key] = @(created);
165 }
166 }
167 #endif
168 return values;
169 }
170
171
172 @end
173
174 #endif // OCTAGON