]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Tool/recovery_key.m
Security-58286.20.16.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Tool / recovery_key.m
1 /*
2 * Copyright (c) 2013-2016 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 #import <Foundation/Foundation.h>
25 #include "recovery_key.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <utilities/SecCFWrappers.h>
31
32 #include <Security/SecureObjectSync/SOSCloudCircle.h>
33 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
34 #include <Security/SecRecoveryKey.h>
35
36 #import <CoreCDP/CoreCDP.h>
37
38
39 #include "secToolFileIO.h"
40
41 int
42 recovery_key(int argc, char * const *argv)
43 {
44 int ch, result = 0;
45 CFErrorRef error = NULL;
46 BOOL hadError = false;
47 SOSLogSetOutputTo(NULL, NULL);
48
49 static struct option long_options[] =
50 {
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'},
57 {0, 0, 0, 0}
58 };
59 int option_index = 0;
60
61 while ((ch = getopt_long(argc, argv, "FG:Rs:gcV:", long_options, &option_index)) != -1)
62 switch (ch) {
63 case 'G': {
64 NSError *nserror = NULL;
65 NSString *testString = [NSString stringWithUTF8String:optarg];
66 if(testString == nil)
67 return 2;
68
69 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(testString, &nserror);
70 if(rk == nil) {
71 printmsg(CFSTR("SecRKCreateRecoveryKeyWithError: %@\n"), nserror);
72 return 2;
73 }
74 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
75 if(publicKey == nil)
76 return 2;
77
78 printmsg(CFSTR("public recovery key: %@\n"), publicKey);
79 break;
80 }
81 case 'R': {
82 NSString *testString = SecRKCreateRecoveryKeyString(NULL);
83 if(testString == nil)
84 return 2;
85
86 printmsg(CFSTR("public recovery string: %@\n"), testString);
87
88 break;
89 }
90 case 's':
91 {
92 NSError *nserror = NULL;
93 NSString *testString = [NSString stringWithUTF8String:optarg];
94 if(testString == nil)
95 return 2;
96
97 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(testString, &nserror);
98 if(rk == nil) {
99 printmsg(CFSTR("SecRKCreateRecoveryKeyWithError: %@\n"), nserror);
100 return 2;
101 }
102
103 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
104 if(publicKey == nil)
105 return 2;
106
107 hadError = SOSCCRegisterRecoveryPublicKey((__bridge CFDataRef)(publicKey), &error) != true;
108 break;
109 }
110 case 'g':
111 {
112 CFDataRef recovery_key = SOSCCCopyRecoveryPublicKey(&error);
113 hadError = recovery_key == NULL;
114 if(!hadError)
115 printmsg(CFSTR("recovery key: %@\n"), recovery_key);
116 CFReleaseNull(recovery_key);
117 break;
118 }
119 case 'c':
120 {
121 hadError = SOSCCRegisterRecoveryPublicKey(NULL, &error) != true;
122 break;
123 }
124 case 'F':
125 {
126 NSError *localError = nil;
127
128 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
129
130 CDPFollowUpContext *context = [CDPFollowUpContext contextForRecoveryKeyRepair];
131 context.force = true;
132
133 [cdpd postFollowUpWithContext:context error:&localError];
134 if(localError){
135 printmsg(CFSTR("Request to CoreCDP to follow up failed: %@\n"), localError);
136 } else {
137 printmsg(CFSTR("CoreCDP handling follow up\n"));
138 }
139 break;
140 }
141 case 'V': {
142 NSError *localError = nil;
143 NSString *testString = [NSString stringWithUTF8String:optarg];
144 NSString *fileName = [NSString stringWithFormat:@"%@.plist", testString];
145 if(testString == nil)
146 return 2;
147
148 NSDictionary *ver = SecRKCopyAccountRecoveryVerifier(testString, &localError);
149 if(ver == nil) {
150 printmsg(CFSTR("Failed to make verifier dictionary: %@\n"), localError);
151 return 2;
152 }
153
154 printmsg(CFSTR("Verifier Dictionary: %@\n\n"), ver);
155 printmsg(CFSTR("Writing plist to %@\n"), (__bridge CFStringRef) fileName);
156
157 [ver writeToFile:fileName atomically:YES];
158
159 }
160 break;
161
162 case '?':
163 default:
164 {
165 printf("%s [...options]\n", getprogname());
166 for (unsigned n = 0; n < sizeof(long_options)/sizeof(long_options[0]); n++) {
167 printf("\t [-%c|--%s\n", long_options[n].val, long_options[n].name);
168 }
169 return 2;
170 }
171 }
172 if (hadError)
173 printerr(CFSTR("Error: %@\n"), error);
174
175 return result;
176 }