5 #import <TargetConditionals.h>
6 #import <Foundation/Foundation.h>
7 #import <Security/SecInternalReleasePriv.h>
8 #import <Security/Security.h>
11 #import "keychain/otctl/OTControlCLI.h"
12 #import "keychain/otctl/EscrowRequestCLI.h"
13 #import "keychain/escrowrequest/Framework/SecEscrowRequest.h"
14 #include "lib/SecArgParse.h"
15 #include "utilities/debugging.h"
18 #import "keychain/otpaird/OTPairingClient.h"
19 #endif /* TARGET_OS_WATCH */
21 static int start = false;
22 static int signIn = false;
23 static int signOut = false;
24 static int resetoctagon = false;
25 static int resetProtectedData = false;
27 static int fetchAllBottles = false;
28 static int recover = false;
29 static int depart = false;
31 static int status = false;
33 static int er_trigger = false;
34 static int er_status = false;
35 static int er_reset = false;
36 static int er_store = false;
37 static int ckks_policy_flag = false;
39 static int ttr_flag = false;
41 static int health = false;
44 static int pairme = false;
45 #endif /* TARGET_OS_WATCH */
47 static char* bottleIDArg = NULL;
48 static char* contextNameArg = NULL;
49 static char* secretArg = NULL;
50 static char* skipRateLimitingCheckArg = NULL;
51 static int json = false;
53 static char* altDSIDArg = NULL;
54 static char* containerStr = NULL;
55 static char* radarNumber = NULL;
56 static char* appleIDArg = NULL;
57 static char* dsidArg = NULL;
59 static void internalOnly(void)
61 if(!SecIsInternalRelease()) {
62 secnotice("octagon", "Tool not available on non internal builds");
63 errx(1, "Tool not available on non internal builds");
67 int main(int argc, char** argv)
69 static struct argument options[] = {
70 {.shortname = 's', .longname = "secret", .argument = &secretArg, .description = "escrow secret"},
71 {.shortname = 'e', .longname = "bottleID", .argument = &bottleIDArg, .description = "bottle record id"},
72 {.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"},
73 {.shortname = 'j', .longname = "json", .flag = &json, .flagval = true, .description = "Output in JSON"},
75 {.longname = "altDSID", .argument = &altDSIDArg, .description = "altDSID (for sign-in/out)"},
76 {.longname = "entropy", .argument = &secretArg, .description = "escrowed entropy in JSON"},
78 {.longname = "appleID", .argument = &appleIDArg, .description = "AppleID"},
79 {.longname = "dsid", .argument = &dsidArg, .description = "DSID"},
81 {.longname = "container", .argument = &containerStr, .description = "CloudKit container name"},
82 {.longname = "radar", .argument = &radarNumber, .description = "Radar number"},
84 {.command = "start", .flag = &start, .flagval = true, .description = "Start Octagon state machine"},
85 {.command = "sign-in", .flag = &signIn, .flagval = true, .description = "Inform Cuttlefish container of sign in"},
86 {.command = "sign-out", .flag = &signOut, .flagval = true, .description = "Inform Cuttlefish container of sign out"},
87 {.command = "status", .flag = &status, .flagval = true, .description = "Report Octagon status"},
89 {.command = "resetoctagon", .flag = &resetoctagon, .flagval = true, .description = "Reset and establish new Octagon trust"},
90 {.command = "resetProtectedData", .flag = &resetProtectedData, .flagval = true, .description = "Reset ProtectedData"},
92 {.command = "allBottles", .flag = &fetchAllBottles, .flagval = true, .description = "Fetch all viable bottles"},
93 {.command = "recover", .flag = &recover, .flagval = true, .description = "Recover using this bottle"},
94 {.command = "depart", .flag = &depart, .flagval = true, .description = "Depart from Octagon Trust"},
96 {.command = "er-trigger", .flag = &er_trigger, .flagval = true, .description = "Trigger an Escrow Request request"},
97 {.command = "er-status", .flag = &er_status, .flagval = true, .description = "Report status on any pending Escrow Request requests"},
98 {.command = "er-reset", .flag = &er_reset, .flagval = true, .description = "Delete all Escrow Request requests"},
99 {.command = "er-store", .flag = &er_store, .flagval = true, .description = "Store any pending Escrow Request prerecords"},
101 {.command = "health", .flag = &health, .flagval = true, .description = "Check Octagon Health status"},
102 {.command = "ckks-policy", .flag = &ckks_policy_flag, .flagval = true, .description = "Trigger a refetch of the CKKS policy"},
104 {.command = "taptoradar", .flag = &ttr_flag, .flagval = true, .description = "Trigger a TapToRadar"},
108 {.command = "pairme", .flag = &pairme, .flagval = true, .description = "Perform pairing (watchOS only)"},
109 #endif /* TARGET_OS_WATCH */
112 static struct arguments args = {
113 .programname = "otctl",
114 .description = "Control and report on Octagon Trust",
115 .arguments = options,
118 if(!options_parse(argc, argv, &args)) {
125 NSError* error = nil;
127 // Use a synchronous control object
128 OTControl* rpc = [OTControl controlObject:true error:&error];
130 errx(1, "no OTControl failed: %s", [[error description] UTF8String]);
133 NSString* context = contextNameArg ? [NSString stringWithCString:contextNameArg encoding:NSUTF8StringEncoding] : OTDefaultContext;
134 NSString* container = containerStr ? [NSString stringWithCString:containerStr encoding:NSUTF8StringEncoding] : nil;
135 NSString* altDSID = altDSIDArg ? [NSString stringWithCString:altDSIDArg encoding:NSUTF8StringEncoding] : nil;
136 NSString* dsid = dsidArg ? [NSString stringWithCString:dsidArg encoding:NSUTF8StringEncoding] : nil;
137 NSString* appleID = appleIDArg ? [NSString stringWithCString:appleIDArg encoding:NSUTF8StringEncoding] : nil;
139 NSString* skipRateLimitingCheck = skipRateLimitingCheckArg ? [NSString stringWithCString:skipRateLimitingCheckArg encoding:NSUTF8StringEncoding] : @"NO";
141 OTControlCLI* ctl = [[OTControlCLI alloc] initWithOTControl:rpc];
143 NSError* escrowRequestError = nil;
144 EscrowRequestCLI* escrowctl = [[EscrowRequestCLI alloc] initWithEscrowRequest:[SecEscrowRequest request:&escrowRequestError]];
145 if(escrowRequestError) {
146 errx(1, "SecEscrowRequest failed: %s", [[escrowRequestError description] UTF8String]);
149 long ret = [ctl resetOctagon:container context:context altDSID:altDSID];
152 if(resetProtectedData) {
154 long ret = [ctl resetProtectedData:container context:context altDSID:altDSID appleID:appleID dsid:dsid];
157 if(fetchAllBottles) {
158 return (int)[ctl fetchAllBottles:altDSID containerName:container context:context control:rpc];
161 NSString* entropyJSON = secretArg ? [NSString stringWithCString:secretArg encoding:NSUTF8StringEncoding] : nil;
162 NSString* bottleID = bottleIDArg ? [NSString stringWithCString:bottleIDArg encoding:NSUTF8StringEncoding] : nil;
164 if(!entropyJSON || !bottleID) {
169 NSData* entropy = [[NSData alloc] initWithBase64EncodedString:entropyJSON options:0];
175 return (int)[ctl recoverUsingBottleID:bottleID
178 containerName:container
183 return (int)[ctl depart:container context:context];
187 return (int)[ctl startOctagonStateMachine:container context:context];
191 return (int)[ctl signIn:altDSID container:container context:context];
195 return (int)[ctl signOut:container context:context];
199 return (int)[ctl status:container context:context json:json];
204 if([skipRateLimitingCheck isEqualToString:@"YES"]) {
209 return (int)[ctl healthCheck:container context:context skipRateLimitingCheck:skip];
211 if(ckks_policy_flag) {
212 return (int)[ctl refetchCKKSPolicy:container context:context];
215 if (radarNumber == NULL) {
218 return (int)[ctl tapToRadar:@"action" description:@"description" radar:[NSString stringWithUTF8String:radarNumber]];
223 return (int)[escrowctl trigger];
226 return (int)[escrowctl status];
229 return (int)[escrowctl reset];
232 return (int)[escrowctl storePrerecordsInEscrow];
238 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
239 OTPairingInitiateWithCompletion(NULL, ^(bool success, NSError *pairingError) {
241 printf("successfully paired with companion\n");
243 printf("failed to pair with companion: %s\n", pairingError.description.UTF8String);
245 dispatch_semaphore_signal(sema);
247 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
250 #endif /* TARGET_OS_WATCH */