5 #import <TargetConditionals.h>
6 #import <Foundation/Foundation.h>
7 #import <Security/SecInternalReleasePriv.h>
8 #import <Security/Security.h>
10 #import <OctagonTrust/OctagonTrust.h>
12 #import "keychain/otctl/OTControlCLI.h"
13 #import "keychain/otctl/EscrowRequestCLI.h"
14 #import "keychain/escrowrequest/Framework/SecEscrowRequest.h"
15 #include "lib/SecArgParse.h"
16 #include "utilities/debugging.h"
19 #import "keychain/otpaird/OTPairingClient.h"
20 #endif /* TARGET_OS_WATCH */
22 static int start = false;
23 static int signIn = false;
24 static int signOut = false;
25 static int resetoctagon = false;
26 static int resetProtectedData = false;
27 static int userControllableViewsSyncStatus = false;
29 static int fetchAllBottles = false;
30 static int recover = false;
31 static int depart = false;
33 static int status = false;
35 static int er_trigger = false;
36 static int er_status = false;
37 static int er_reset = false;
38 static int er_store = false;
39 static int ckks_policy_flag = false;
41 static int ttr_flag = false;
43 static int fetch_escrow_records = false;
44 static int fetch_all_escrow_records = false;
46 static int recoverRecord = false;
47 static int recoverSilentRecord = false;
49 static int health = false;
52 static int pairme = false;
53 #endif /* TARGET_OS_WATCH */
55 static char* bottleIDArg = NULL;
56 static char* contextNameArg = NULL;
57 static char* secretArg = NULL;
58 static char* skipRateLimitingCheckArg = NULL;
59 static char* recordID = NULL;
61 static int argEnable = false;
62 static int argPause = false;
64 static int json = false;
66 static char* altDSIDArg = NULL;
67 static char* containerStr = NULL;
68 static char* radarNumber = NULL;
69 static char* appleIDArg = NULL;
70 static char* dsidArg = NULL;
72 static void internalOnly(void)
74 if(!SecIsInternalRelease()) {
75 secnotice("octagon", "Tool not available on non internal builds");
76 errx(1, "Tool not available on non internal builds");
80 int main(int argc, char** argv)
82 static struct argument options[] = {
83 {.shortname = 's', .longname = "secret", .argument = &secretArg, .description = "escrow secret"},
84 {.shortname = 'e', .longname = "bottleID", .argument = &bottleIDArg, .description = "bottle record id"},
85 {.shortname = 'r', .longname = "skipRateLimiting", .argument = &skipRateLimitingCheckArg, .description = " enter values YES or NO, option defaults to NO, This gives you the opportunity to skip the rate limiting check when performing the cuttlefish health check"},
86 {.shortname = 'j', .longname = "json", .flag = &json, .flagval = true, .description = "Output in JSON"},
87 {.shortname = 'i', .longname = "recordID", .argument = &recordID, .flagval = true, .description = "recordID"},
89 {.shortname = 'E', .longname = "enable", .flag = &argEnable, .flagval = true, .description = "Enable something (pair with a modification command)"},
90 {.shortname = 'P', .longname = "pause", .flag = &argPause, .flagval = true, .description = "Pause something (pair with a modification command)"},
92 {.longname = "altDSID", .argument = &altDSIDArg, .description = "altDSID (for sign-in/out)"},
93 {.longname = "entropy", .argument = &secretArg, .description = "escrowed entropy in JSON"},
95 {.longname = "appleID", .argument = &appleIDArg, .description = "AppleID"},
96 {.longname = "dsid", .argument = &dsidArg, .description = "DSID"},
98 {.longname = "container", .argument = &containerStr, .description = "CloudKit container name"},
99 {.longname = "radar", .argument = &radarNumber, .description = "Radar number"},
101 {.command = "start", .flag = &start, .flagval = true, .description = "Start Octagon state machine"},
102 {.command = "sign-in", .flag = &signIn, .flagval = true, .description = "Inform Cuttlefish container of sign in"},
103 {.command = "sign-out", .flag = &signOut, .flagval = true, .description = "Inform Cuttlefish container of sign out"},
104 {.command = "status", .flag = &status, .flagval = true, .description = "Report Octagon status"},
106 {.command = "resetoctagon", .flag = &resetoctagon, .flagval = true, .description = "Reset and establish new Octagon trust"},
107 {.command = "resetProtectedData", .flag = &resetProtectedData, .flagval = true, .description = "Reset ProtectedData"},
109 {.command = "user-controllable-views", .flag = &userControllableViewsSyncStatus, .flagval = true, .description = "Modify or view user-controllable views status (If one of --enable or --pause is passed, will modify status)"},
111 {.command = "allBottles", .flag = &fetchAllBottles, .flagval = true, .description = "Fetch all viable bottles"},
112 {.command = "recover", .flag = &recover, .flagval = true, .description = "Recover using this bottle"},
113 {.command = "depart", .flag = &depart, .flagval = true, .description = "Depart from Octagon Trust"},
115 {.command = "er-trigger", .flag = &er_trigger, .flagval = true, .description = "Trigger an Escrow Request request"},
116 {.command = "er-status", .flag = &er_status, .flagval = true, .description = "Report status on any pending Escrow Request requests"},
117 {.command = "er-reset", .flag = &er_reset, .flagval = true, .description = "Delete all Escrow Request requests"},
118 {.command = "er-store", .flag = &er_store, .flagval = true, .description = "Store any pending Escrow Request prerecords"},
120 {.command = "health", .flag = &health, .flagval = true, .description = "Check Octagon Health status"},
121 {.command = "ckks-policy", .flag = &ckks_policy_flag, .flagval = true, .description = "Trigger a refetch of the CKKS policy"},
123 {.command = "taptoradar", .flag = &ttr_flag, .flagval = true, .description = "Trigger a TapToRadar"},
125 {.command = "fetchEscrowRecords", .flag = &fetch_escrow_records, .flagval = true, .description = "Fetch Escrow Records"},
126 {.command = "fetchAllEscrowRecords", .flag = &fetch_all_escrow_records, .flagval = true, .description = "Fetch All Escrow Records"},
128 {.command = "recover-record", .flag = &recoverRecord, .flagval = true, .description = "Recover record"},
129 {.command = "recover-record-silent", .flag = &recoverSilentRecord, .flagval = true, .description = "Silent record recovery"},
134 {.command = "pairme", .flag = &pairme, .flagval = true, .description = "Perform pairing (watchOS only)"},
135 #endif /* TARGET_OS_WATCH */
138 static struct arguments args = {
139 .programname = "otctl",
140 .description = "Control and report on Octagon Trust",
141 .arguments = options,
144 if(!options_parse(argc, argv, &args)) {
151 NSError* error = nil;
153 // Use a synchronous control object
154 OTControl* rpc = [OTControl controlObject:true error:&error];
156 errx(1, "no OTControl failed: %s", [[error description] UTF8String]);
159 NSString* context = contextNameArg ? [NSString stringWithCString:contextNameArg encoding:NSUTF8StringEncoding] : OTDefaultContext;
160 NSString* container = containerStr ? [NSString stringWithCString:containerStr encoding:NSUTF8StringEncoding] : nil;
161 NSString* altDSID = altDSIDArg ? [NSString stringWithCString:altDSIDArg encoding:NSUTF8StringEncoding] : nil;
162 NSString* dsid = dsidArg ? [NSString stringWithCString:dsidArg encoding:NSUTF8StringEncoding] : nil;
163 NSString* appleID = appleIDArg ? [NSString stringWithCString:appleIDArg encoding:NSUTF8StringEncoding] : nil;
165 NSString* skipRateLimitingCheck = skipRateLimitingCheckArg ? [NSString stringWithCString:skipRateLimitingCheckArg encoding:NSUTF8StringEncoding] : @"NO";
167 OTControlCLI* ctl = [[OTControlCLI alloc] initWithOTControl:rpc];
169 NSError* escrowRequestError = nil;
170 EscrowRequestCLI* escrowctl = [[EscrowRequestCLI alloc] initWithEscrowRequest:[SecEscrowRequest request:&escrowRequestError]];
171 if(escrowRequestError) {
172 errx(1, "SecEscrowRequest failed: %s", [[escrowRequestError description] UTF8String]);
175 long ret = [ctl resetOctagon:container context:context altDSID:altDSID];
178 if(resetProtectedData) {
180 long ret = [ctl resetProtectedData:container context:context altDSID:altDSID appleID:appleID dsid:dsid];
183 if(userControllableViewsSyncStatus) {
186 if(argEnable && argPause) {
191 if(argEnable == false && argPause == false) {
192 return (int)[ctl fetchUserControllableViewsSyncStatus:container contextID:context];
195 // At this point, we're sure that either argEnabled or argPause is set; so the value of argEnabled captures the user's intention
196 return (int)[ctl setUserControllableViewsSyncStatus:container contextID:context enabled:argEnable];
199 if(fetchAllBottles) {
200 return (int)[ctl fetchAllBottles:altDSID containerName:container context:context control:rpc];
203 NSString* entropyJSON = secretArg ? [NSString stringWithCString:secretArg encoding:NSUTF8StringEncoding] : nil;
204 NSString* bottleID = bottleIDArg ? [NSString stringWithCString:bottleIDArg encoding:NSUTF8StringEncoding] : nil;
206 if(!entropyJSON || !bottleID) {
211 NSData* entropy = [[NSData alloc] initWithBase64EncodedString:entropyJSON options:0];
217 return (int)[ctl recoverUsingBottleID:bottleID
220 containerName:container
225 return (int)[ctl depart:container context:context];
229 return (int)[ctl startOctagonStateMachine:container context:context];
233 return (int)[ctl signIn:altDSID container:container context:context];
237 return (int)[ctl signOut:container context:context];
241 return (int)[ctl status:container context:context json:json];
243 if(fetch_escrow_records) {
244 return (int)[ctl fetchEscrowRecords:container context:context];
246 if(fetch_all_escrow_records) {
247 return (int)[ctl fetchAllEscrowRecords:container context:context];
250 NSString* recordIDString = recordID ? [NSString stringWithCString:recordID encoding:NSUTF8StringEncoding] : nil;
251 NSString* secret = secretArg ? [NSString stringWithCString:secretArg encoding:NSUTF8StringEncoding] : nil;
253 if(!recordIDString || !secret || !appleID) {
258 return (int)[ctl performEscrowRecovery:container context:context recordID:recordIDString appleID:appleID secret:secret];
260 if(recoverSilentRecord){
261 NSString* secret = secretArg ? [NSString stringWithCString:secretArg encoding:NSUTF8StringEncoding] : nil;
263 if(!secret || !appleID) {
268 return (int)[ctl performSilentEscrowRecovery:container context:context appleID:appleID secret:secret];
272 if([skipRateLimitingCheck isEqualToString:@"YES"]) {
277 return (int)[ctl healthCheck:container context:context skipRateLimitingCheck:skip];
279 if(ckks_policy_flag) {
280 return (int)[ctl refetchCKKSPolicy:container context:context];
283 if (radarNumber == NULL) {
286 return (int)[ctl tapToRadar:@"action" description:@"description" radar:[NSString stringWithUTF8String:radarNumber]];
291 return (int)[escrowctl trigger];
294 return (int)[escrowctl status];
297 return (int)[escrowctl reset];
300 return (int)[escrowctl storePrerecordsInEscrow];
306 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
307 OTPairingInitiateWithCompletion(NULL, ^(bool success, NSError *pairingError) {
309 printf("successfully paired with companion\n");
311 printf("failed to pair with companion: %s\n", pairingError.description.UTF8String);
313 dispatch_semaphore_signal(sema);
315 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
318 #endif /* TARGET_OS_WATCH */