]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Tool/recovery_key.m
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / SecureObjectSync / 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 {"recovery-string", no_argument, NULL, 'R' },
53 {"generate", required_argument, NULL, 'G'},
54 {"set", required_argument, NULL, 's'},
55 {"get", no_argument, NULL, 'g'},
56 {"clear", no_argument, NULL, 'c'},
57 {"follow-up", no_argument, NULL, 'F'},
58 {"verifier", no_argument, NULL, 'V'},
59 {0, 0, 0, 0}
60 };
61 int option_index = 0;
62
63 while ((ch = getopt_long(argc, argv, "FG:Rs:gcV:", long_options, &option_index)) != -1)
64 switch (ch) {
65 case 'G': {
66 NSError *nserror = NULL;
67 NSString *testString = [NSString stringWithUTF8String:optarg];
68 if(testString == nil)
69 return SHOW_USAGE_MESSAGE;
70
71 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(testString, &nserror);
72 if(rk == nil) {
73 printmsg(CFSTR("SecRKCreateRecoveryKeyWithError: %@\n"), nserror);
74 return SHOW_USAGE_MESSAGE;
75 }
76 NSData *publicKey = SecRKCopyBackupPublicKey(rk);
77 if(publicKey == nil)
78 return SHOW_USAGE_MESSAGE;
79
80 printmsg(CFSTR("example (not registered) public recovery key: %@\n"), publicKey);
81 break;
82 }
83 case 'R': {
84 NSString *testString = SecRKCreateRecoveryKeyString(NULL);
85 if(testString == nil)
86 return SHOW_USAGE_MESSAGE;
87
88 printmsg(CFSTR("public recovery string: %@\n"), testString);
89
90 break;
91 }
92 case 's':
93 {
94 NSError *nserror = NULL;
95 NSString *testString = [NSString stringWithUTF8String:optarg];
96 if(testString == nil)
97 return SHOW_USAGE_MESSAGE;
98
99 SecRecoveryKey *rk = SecRKCreateRecoveryKeyWithError(testString, &nserror);
100 if(rk == nil) {
101 printmsg(CFSTR("SecRKCreateRecoveryKeyWithError: %@\n"), nserror);
102 return SHOW_USAGE_MESSAGE;
103 }
104
105 CFErrorRef cferror = NULL;
106 if(!SecRKRegisterBackupPublicKey(rk, &cferror)) {
107 printmsg(CFSTR("Error from SecRKRegisterBackupPublicKey: %@\n"), cferror);
108 CFReleaseNull(cferror);
109 return SHOW_USAGE_MESSAGE;
110 }
111 break;
112 }
113 case 'g':
114 {
115 CFDataRef recovery_key = SOSCCCopyRecoveryPublicKey(&error);
116 hadError = recovery_key == NULL;
117 if(!hadError)
118 printmsg(CFSTR("recovery key: %@\n"), recovery_key);
119 CFReleaseNull(recovery_key);
120 break;
121 }
122 case 'c':
123 {
124 hadError = SOSCCRegisterRecoveryPublicKey(NULL, &error) != true;
125 break;
126 }
127 case 'F':
128 {
129 NSError *localError = nil;
130
131 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
132
133 CDPFollowUpContext *context = [CDPFollowUpContext contextForRecoveryKeyRepair];
134 context.force = true;
135
136 secnotice("followup", "Posting a follow up (for SOS) of type recovery key");
137 [cdpd postFollowUpWithContext:context error:&localError];
138 if(localError){
139 printmsg(CFSTR("Request to CoreCDP to follow up failed: %@\n"), localError);
140 } else {
141 printmsg(CFSTR("CoreCDP handling follow up\n"));
142 }
143 break;
144 }
145 case 'V': {
146 NSError *localError = nil;
147 NSString *testString = [NSString stringWithUTF8String:optarg];
148 NSString *fileName = [NSString stringWithFormat:@"%@.plist", testString];
149 if(testString == nil)
150 return SHOW_USAGE_MESSAGE;
151
152 NSDictionary *ver = SecRKCopyAccountRecoveryVerifier(testString, &localError);
153 if(ver == nil) {
154 printmsg(CFSTR("Failed to make verifier dictionary: %@\n"), localError);
155 return SHOW_USAGE_MESSAGE;
156 }
157
158 printmsg(CFSTR("Verifier Dictionary: %@\n\n"), ver);
159 printmsg(CFSTR("Writing plist to %@\n"), (__bridge CFStringRef) fileName);
160
161 [ver writeToFile:fileName atomically:YES];
162
163 }
164 break;
165
166 case '?':
167 default:
168 {
169 printf("%s [...options]\n", getprogname());
170 for (unsigned n = 0; n < sizeof(long_options)/sizeof(long_options[0]); n++) {
171 printf("\t [-%c|--%s\n", long_options[n].val, long_options[n].name);
172 }
173 return SHOW_USAGE_MESSAGE;
174 }
175 }
176 if (hadError)
177 printerr(CFSTR("Error: %@\n"), error);
178
179 return result;
180 }