2 * Copyright (c) 2013-2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Foundation/Foundation.h>
25 #include "recovery_key.h"
30 #include <utilities/SecCFWrappers.h>
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
34 #include <Security/SecRecoveryKey.h>
36 #import <CoreCDP/CoreCDP.h>
39 #include "secToolFileIO.h"
42 recovery_key(int argc, char * const *argv)
45 CFErrorRef error = NULL;
46 BOOL hadError = false;
47 SOSLogSetOutputTo(NULL, NULL);
49 static struct option long_options[] =
51 /* These options set a flag. */
52 {"generate", required_argument, NULL, 'G'},
53 {"set", required_argument, NULL, 's'},
54 {"get", no_argument, NULL, 'g'},
55 {"clear", no_argument, NULL, 'c'},
56 {"follow-up", no_argument, NULL, 'F'},
61 while ((ch = getopt_long(argc, argv, "FG:Rs:gc", long_options, &option_index)) != -1)
64 NSString *testString = [NSString stringWithUTF8String:optarg];
68 SecRecoveryKey *rk = SecRKCreateRecoveryKey(testString);
71 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
75 printmsg(CFSTR("public recovery key: %@\n"), publicKey);
79 NSString *testString = SecRKCreateRecoveryKeyString(NULL);
83 printmsg(CFSTR("public recovery string: %@\n"), testString);
89 NSString *testString = [NSString stringWithUTF8String:optarg];
93 SecRecoveryKey *rk = SecRKCreateRecoveryKey(testString);
97 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
101 hadError = SOSCCRegisterRecoveryPublicKey((__bridge CFDataRef)(publicKey), &error) != true;
106 CFDataRef recovery_key = SOSCCCopyRecoveryPublicKey(&error);
107 hadError = recovery_key == NULL;
109 printmsg(CFSTR("recovery key: %@\n"), recovery_key);
110 CFReleaseNull(recovery_key);
115 CFDataRef empty = CFDataCreate(kCFAllocatorDefault, 0, 0);
116 hadError = SOSCCRegisterRecoveryPublicKey(empty, &error) != true;
117 CFReleaseNull(empty);
122 NSError *localError = nil;
124 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
126 CDPFollowUpContext *context = [CDPFollowUpContext contextForRecoveryKeyRepair];
127 context.force = true;
129 [cdpd postFollowUpWithContext:context error:&localError];
131 printmsg(CFSTR("Request to CoreCDP to follow up failed: %@\n"), localError);
133 printmsg(CFSTR("CoreCDP handling follow up\n"));
140 printf("%s [...options]\n", getprogname());
141 for (unsigned n = 0; n < sizeof(long_options)/sizeof(long_options[0]); n++) {
142 printf("\t [-%c|--%s\n", long_options[n].val, long_options[n].name);
148 printerr(CFSTR("Error: %@\n"), error);