]> git.saurik.com Git - apple/security.git/blob - SecurityTool/security.c
Security-57740.51.3.tar.gz
[apple/security.git] / SecurityTool / security.c
1 /*
2 * Copyright (c) 2003-2017 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 * security.c
24 */
25
26 #include "security_tool.h"
27
28 #include "leaks.h"
29 #include "readline_cssm.h"
30
31 #include "cmsutil.h"
32 #include "db_commands.h"
33 #include "keychain_add.h"
34 #include "keychain_create.h"
35 #include "keychain_delete.h"
36 #include "keychain_list.h"
37 #include "keychain_lock.h"
38 #include "keychain_set_settings.h"
39 #include "keychain_show_info.h"
40 #include "keychain_unlock.h"
41 #include "keychain_recode.h"
42 #include "key_create.h"
43 #include "keychain_find.h"
44 #include "keychain_import.h"
45 #include "keychain_export.h"
46 #include "identity_find.h"
47 #include "identity_prefs.h"
48 #include "mds_install.h"
49 #include "trusted_cert_add.h"
50 #include "trusted_cert_dump.h"
51 #include "user_trust_enable.h"
52 #include "trust_settings_impexp.h"
53 #include "verify_cert.h"
54 #include "authz.h"
55 #include "smartcards.h"
56 #include "display_error_code.h"
57 #include "createFVMaster.h"
58 #include "smartcards.h"
59 #include "translocate.h"
60
61 #include <ctype.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <dispatch/dispatch.h>
67
68 #include <CoreFoundation/CFRunLoop.h>
69 #include <Security/SecBasePriv.h>
70 #include <Security/SecKeychainPriv.h>
71 #include <security_asn1/secerr.h>
72
73 /* Maximum length of an input line in interactive mode. */
74 #define MAX_LINE_LEN 4096
75 /* Maximum number of arguments on an input line in interactive mode. */
76 #define MAX_ARGS 32
77
78 /* Entry in commands array for a command. */
79 typedef struct command
80 {
81 const char *c_name; /* name of the command. */
82 command_func c_func; /* function to execute the command. */
83 const char *c_usage; /* usage sting for command. */
84 const char *c_help; /* help string for (or description of) command. */
85 } command;
86
87 /* The default prompt. */
88 const char *prompt_string = "security> ";
89
90 /* The name of this program. */
91 const char *prog_name;
92
93
94 /* Forward declarations of static functions. */
95 static int help(int argc, char * const *argv);
96
97 /*
98 * The command array itself.
99 * Add commands here at will.
100 * Matching is done on a prefix basis. The first command in the array
101 * gets matched first.
102 */
103 const command commands[] =
104 {
105 { "help", help,
106 "[command ...]",
107 "Show all commands, or show usage for a command." },
108
109 { "list-keychains", keychain_list,
110 "[-d user|system|common|dynamic] [-s [keychain...]]\n"
111 " -d Use the specified preference domain\n"
112 " -s Set the search list to the specified keychains\n"
113 "With no parameters, display the search list.",
114 "Display or manipulate the keychain search list." },
115
116 { "list-smartcards", ctk_list,
117 "Display IDs of available smartcards.",
118 "Display available smartcards." },
119
120 { "default-keychain", keychain_default,
121 "[-d user|system|common|dynamic] [-s [keychain]]\n"
122 " -d Use the specified preference domain\n"
123 " -s Set the default keychain to the specified keychain\n"
124 "With no parameters, display the default keychain.",
125 "Display or set the default keychain." },
126
127 { "login-keychain", keychain_login,
128 "[-d user|system|common|dynamic] [-s [keychain]]\n"
129 " -d Use the specified preference domain\n"
130 " -s Set the login keychain to the specified keychain\n"
131 "With no parameters, display the login keychain.",
132 "Display or set the login keychain." },
133
134 { "create-keychain", keychain_create,
135 "[-P] [-p password] [keychains...]\n"
136 " -p Use \"password\" as the password for the keychains being created\n"
137 " -P Prompt the user for a password using the SecurityAgent\n"
138 "Use of the -p option is insecure",
139 "Create keychains and add them to the search list.",
140 },
141
142 { "delete-keychain", keychain_delete,
143 "[keychains...]",
144 "Delete keychains and remove them from the search list." },
145
146 { "lock-keychain", keychain_lock,
147 "[-a | keychain]\n"
148 " -a Lock all keychains",
149 "Lock the specified keychain."},
150
151 { "unlock-keychain", keychain_unlock,
152 "[-u] [-p password] [keychain]\n"
153 " -p Use \"password\" as the password to unlock the keychain\n"
154 " -u Do not use the password\n"
155 "Use of the -p option is insecure",
156 "Unlock the specified keychain."},
157
158 { "set-keychain-settings", keychain_set_settings,
159 "[-lu] [-t timeout] [keychain]\n"
160 " -l Lock keychain when the system sleeps\n"
161 " -u Lock keychain after timeout interval\n"
162 " -t Timeout in seconds (omitting this option specifies \"no timeout\")\n",
163 "Set settings for a keychain."},
164
165 { "set-keychain-password", keychain_set_password,
166 "[-o oldPassword] [-p newPassword] [keychain]\n"
167 " -o Old keychain password (if not provided, will prompt)\n"
168 " -p New keychain password (if not provided, will prompt)\n"
169 "Use of either the -o or -p options is insecure\n",
170 "Set password for a keychain."},
171
172 { "show-keychain-info", keychain_show_info,
173 "[keychain]",
174 "Show the settings for keychain." },
175
176 { "dump-keychain", keychain_dump,
177 "[-adir] [keychain...]\n"
178 " -a Dump access control list of items\n"
179 " -d Dump (decrypted) data of items\n"
180 " -i Interactive access control list editing mode\n"
181 " -r Dump the raw (encrypted) data of items",
182 "Dump the contents of one or more keychains." },
183
184 #ifndef NDEBUG
185 { "recode-keychain", keychain_recode,
186 "keychain_to_recode keychain_to_get_secrets_from",
187 "Recode a keychain to use the secrets from another one."},
188 #endif
189
190 { "create-keypair", key_create_pair,
191 "[-a alg] [-s size] [-f date] [-t date] [-d days] [-k keychain] [-A|-T appPath] description\n"
192 " -a Use alg as the algorithm, can be rsa, dh, dsa or fee (default rsa)\n"
193 " -s Specify the keysize in bits (default 512)\n"
194 " -f Make a key valid from the specified date\n"
195 " -t Make a key valid to the specified date\n"
196 " -d Make a key valid for the number of days specified from today\n"
197 " -k Use the specified keychain rather than the default\n"
198 " -A Allow any application to access this key without warning (insecure, not recommended!)\n"
199 " -T Specify an application which may access this key (multiple -T options are allowed)\n"
200 "If no options are provided, ask the user interactively.",
201 "Create an asymmetric key pair." },
202
203 #if 0
204 /* this was added in mb's integration of PR-3420772, but this is an unimplemented command */
205 { "create-csr", csr_create,
206 "[-a alg] [-s size] [-f date] [-t date] [-d days] [-k keychain] [-A|-T appPath] description\n"
207 " -a Use alg as the algorithm, can be rsa, dh, dsa or fee (default rsa)\n"
208 " -s Specify the keysize in bits (default 512)\n"
209 " -f Make a key valid from the specified date\n"
210 " -t Make a key valid to the specified date\n"
211 " -d Make a key valid for the number of days specified from today\n"
212 " -k Use the specified keychain rather than the default\n"
213 " -A Allow any application to access this key without warning (insecure, not recommended!)\n"
214 " -T Specify an application which may access this key (multiple -T options are allowed)\n"
215 "If no options are provided, ask the user interactively.",
216 "Create a certificate signing request." },
217 #endif
218
219 { "add-generic-password", keychain_add_generic_password,
220 "[-a account] [-s service] [-w password] [options...] [-A|-T appPath] [keychain]\n"
221 " -a Specify account name (required)\n"
222 " -c Specify item creator (optional four-character code)\n"
223 " -C Specify item type (optional four-character code)\n"
224 " -D Specify kind (default is \"application password\")\n"
225 " -G Specify generic attribute (optional)\n"
226 " -j Specify comment string (optional)\n"
227 " -l Specify label (if omitted, service name is used as default label)\n"
228 " -s Specify service name (required)\n"
229 " -p Specify password to be added (legacy option, equivalent to -w)\n"
230 " -w Specify password to be added\n"
231 " -A Allow any application to access this item without warning (insecure, not recommended!)\n"
232 " -T Specify an application which may access this item (multiple -T options are allowed)\n"
233 " -U Update item if it already exists (if omitted, the item cannot already exist)\n"
234 "\n"
235 "By default, the application which creates an item is trusted to access its data without warning.\n"
236 "You can remove this default access by explicitly specifying an empty app pathname: -T \"\"\n"
237 "If no keychain is specified, the password is added to the default keychain.\n"
238 "Use of the -p or -w options is insecure. Specify -w as the last option to be prompted.\n",
239 "Add a generic password item."},
240
241 { "add-internet-password", keychain_add_internet_password,
242 "[-a account] [-s server] [-w password] [options...] [-A|-T appPath] [keychain]\n"
243 " -a Specify account name (required)\n"
244 " -c Specify item creator (optional four-character code)\n"
245 " -C Specify item type (optional four-character code)\n"
246 " -d Specify security domain string (optional)\n"
247 " -D Specify kind (default is \"Internet password\")\n"
248 " -j Specify comment string (optional)\n"
249 " -l Specify label (if omitted, server name is used as default label)\n"
250 " -p Specify path string (optional)\n"
251 " -P Specify port number (optional)\n"
252 " -r Specify protocol (optional four-character SecProtocolType, e.g. \"http\", \"ftp \")\n"
253 " -s Specify server name (required)\n"
254 " -t Specify authentication type (as a four-character SecAuthenticationType, default is \"dflt\")\n"
255 " -w Specify password to be added\n"
256 " -A Allow any application to access this item without warning (insecure, not recommended!)\n"
257 " -T Specify an application which may access this item (multiple -T options are allowed)\n"
258 " -U Update item if it already exists (if omitted, the item cannot already exist)\n"
259 "\n"
260 "By default, the application which creates an item is trusted to access its data without warning.\n"
261 "You can remove this default access by explicitly specifying an empty app pathname: -T \"\"\n"
262 "If no keychain is specified, the password is added to the default keychain.\n"
263 "Use of the -p or -w options is insecure. Specify -w as the last option to be prompted.\n",
264 "Add an internet password item."},
265
266 { "add-certificates", keychain_add_certificates,
267 "[-k keychain] file...\n"
268 "If no keychain is specified, the certificates are added to the default keychain.",
269 "Add certificates to a keychain."},
270
271 { "find-generic-password", keychain_find_generic_password,
272 "[-a account] [-s service] [options...] [-g] [keychain...]\n"
273 " -a Match \"account\" string\n"
274 " -c Match \"creator\" (four-character code)\n"
275 " -C Match \"type\" (four-character code)\n"
276 " -D Match \"kind\" string\n"
277 " -G Match \"value\" string (generic attribute)\n"
278 " -j Match \"comment\" string\n"
279 " -l Match \"label\" string\n"
280 " -s Match \"service\" string\n"
281 " -g Display the password for the item found\n"
282 " -w Display only the password on stdout\n"
283 "If no keychains are specified to search, the default search list is used.",
284 "Find a generic password item."},
285
286 { "delete-generic-password", keychain_delete_generic_password,
287 "[-a account] [-s service] [options...] [keychain...]\n"
288 " -a Match \"account\" string\n"
289 " -c Match \"creator\" (four-character code)\n"
290 " -C Match \"type\" (four-character code)\n"
291 " -D Match \"kind\" string\n"
292 " -G Match \"value\" string (generic attribute)\n"
293 " -j Match \"comment\" string\n"
294 " -l Match \"label\" string\n"
295 " -s Match \"service\" string\n"
296 "If no keychains are specified to search, the default search list is used.",
297 "Delete a generic password item."},
298
299 { "set-generic-password-partition-list", keychain_set_generic_password_partition_list,
300 "[-a account] [-s service] [-S partition-list] [-k keychain password] [options...] [keychain]\n"
301 " -a Match \"account\" string\n"
302 " -c Match \"creator\" (four-character code)\n"
303 " -C Match \"type\" (four-character code)\n"
304 " -D Match \"kind\" string\n"
305 " -G Match \"value\" string (generic attribute)\n"
306 " -j Match \"comment\" string\n"
307 " -l Match \"label\" string\n"
308 " -s Match \"service\" string\n"
309 " -S Comma-separated list of allowed partition IDs\n"
310 " -k The password for the keychain (required)\n"
311 "If no keychains are specified to search, the default search list is used.\n"
312 "Use of the -k option is insecure. Omit it to be prompted.\n",
313 "Set the partition list of a generic password item."},
314
315 { "find-internet-password", keychain_find_internet_password,
316 "[-a account] [-s server] [options...] [-g] [keychain...]\n"
317 " -a Match \"account\" string\n"
318 " -c Match \"creator\" (four-character code)\n"
319 " -C Match \"type\" (four-character code)\n"
320 " -d Match \"securityDomain\" string\n"
321 " -D Match \"kind\" string\n"
322 " -j Match \"comment\" string\n"
323 " -l Match \"label\" string\n"
324 " -p Match \"path\" string\n"
325 " -P Match port number\n"
326 " -r Match \"protocol\" (four-character code)\n"
327 " -s Match \"server\" string\n"
328 " -t Match \"authenticationType\" (four-character code)\n"
329 " -g Display the password for the item found\n"
330 " -w Display only the password on stdout\n"
331 "If no keychains are specified to search, the default search list is used.",
332 "Find an internet password item."},
333
334 { "delete-internet-password", keychain_delete_internet_password,
335 "[-a account] [-s server] [options...] [keychain...]\n"
336 " -a Match \"account\" string\n"
337 " -c Match \"creator\" (four-character code)\n"
338 " -C Match \"type\" (four-character code)\n"
339 " -d Match \"securityDomain\" string\n"
340 " -D Match \"kind\" string\n"
341 " -j Match \"comment\" string\n"
342 " -l Match \"label\" string\n"
343 " -p Match \"path\" string\n"
344 " -P Match port number\n"
345 " -r Match \"protocol\" (four-character code)\n"
346 " -s Match \"server\" string\n"
347 " -t Match \"authenticationType\" (four-character code)\n"
348 "If no keychains are specified to search, the default search list is used.",
349 "Delete an internet password item."},
350
351 { "set-internet-password-partition-list", keychain_set_internet_password_partition_list,
352 "[-a account] [-s service] [-S partition-list] [-k keychain password] [options...] [keychain]\n"
353 " -a Match \"account\" string\n"
354 " -c Match \"creator\" (four-character code)\n"
355 " -C Match \"type\" (four-character code)\n"
356 " -d Match \"securityDomain\" string\n"
357 " -D Match \"kind\" string\n"
358 " -j Match \"comment\" string\n"
359 " -l Match \"label\" string\n"
360 " -p Match \"path\" string\n"
361 " -P Match port number\n"
362 " -r Match \"protocol\" (four-character code)\n"
363 " -s Match \"server\" string\n"
364 " -t Match \"authenticationType\" (four-character code)\n"
365 " -S Comma-separated list of allowed partition IDs\n"
366 " -k password for keychain (required)\n"
367
368 "If no keychains are specified to search, the default search list is used.\n"
369 "Use of the -k option is insecure. Omit it to be prompted.\n",
370 "Set the partition list of a internet password item."},
371
372 { "find-key", keychain_find_key,
373 "[options...] [keychain...]\n"
374 " -a Match \"application label\" string\n"
375 " -c Match \"creator\" (four-character code)\n"
376 " -d Match keys that can decrypt\n"
377 " -D Match \"description\" string\n"
378 " -e Match keys that can encrypt\n"
379 " -j Match \"comment\" string\n"
380 " -l Match \"label\" string\n"
381 " -r Match keys that can derive\n"
382 " -s Match keys that can sign\n"
383 " -t Type of key to find: one of \"symmetric\", \"public\", or \"private\"\n"
384 " -u Match keys that can unwrap\n"
385 " -v Match keys that can verify\n"
386 " -w Match keys that can wrap\n"
387
388 "If no keychains are specified to search, the default search list is used.",
389 "Find keys in the keychain"},
390
391 { "set-key-partition-list", keychain_set_key_partition_list,
392 "[options...] [keychain]\n"
393 " -a Match \"application label\" string\n"
394 " -c Match \"creator\" (four-character code)\n"
395 " -d Match keys that can decrypt\n"
396 " -D Match \"description\" string\n"
397 " -e Match keys that can encrypt\n"
398 " -j Match \"comment\" string\n"
399 " -l Match \"label\" string\n"
400 " -r Match keys that can derive\n"
401 " -s Match keys that can sign\n"
402 " -t Type of key to find: one of \"symmetric\", \"public\", or \"private\"\n"
403 " -u Match keys that can unwrap\n"
404 " -v Match keys that can verify\n"
405 " -w Match keys that can wrap\n"
406 " -S Comma-separated list of allowed partition IDs\n"
407 " -k password for keychain (required)\n"
408
409 "If no keychains are specified to search, the default search list is used.",
410 "Set the partition list of a key."},
411
412 { "find-certificate", keychain_find_certificate,
413 "[-a] [-c name] [-e emailAddress] [-m] [-p] [-Z] [keychain...]\n"
414 " -a Find all matching certificates, not just the first one\n"
415 " -c Match on \"name\" when searching (optional)\n"
416 " -e Match on \"emailAddress\" when searching (optional)\n"
417 " -m Show the email addresses in the certificate\n"
418 " -p Output certificate in pem format\n"
419 " -Z Print SHA-1 hash of the certificate\n"
420 "If no keychains are specified to search, the default search list is used.",
421 "Find a certificate item."},
422
423 { "find-identity", keychain_find_identity,
424 "[-p policy] [-s string] [-v] [keychain...]\n"
425 " -p Specify policy to evaluate (multiple -p options are allowed)\n"
426 " Supported policies: basic, ssl-client, ssl-server, smime, eap,\n"
427 " ipsec, ichat, codesigning, sys-default, sys-kerberos-kdc, macappstore, appleID\n"
428 " -s Specify optional policy-specific string (e.g. DNS hostname for SSL,\n"
429 " or RFC822 email address for S/MIME)\n"
430 " -v Show valid identities only (default is to show all identities)\n"
431 "If no keychains are specified to search, the default search list is used.",
432 "Find an identity (certificate + private key)."},
433
434 { "delete-certificate", keychain_delete_certificate,
435 "[-c name] [-Z hash] [-t] [keychain...]\n"
436 " -c Specify certificate to delete by its common name\n"
437 " -Z Specify certificate to delete by its SHA-1 hash value\n"
438 " -t Also delete user trust settings for this certificate\n"
439 "The certificate to be deleted must be uniquely specified either by a\n"
440 "string found in its common name, or by its SHA-1 hash.\n"
441 "If no keychains are specified to search, the default search list is used.",
442 "Delete a certificate from a keychain."},
443
444 { "delete-identity", keychain_delete_identity,
445 "[-c name] [-Z hash] [-t] [keychain...]\n"
446 " -c Specify certificate to delete by its common name\n"
447 " -Z Specify certificate to delete by its SHA-1 hash value\n"
448 " -t Also delete user trust settings for this identity certificate\n"
449 "The identity to be deleted must be uniquely specified either by a\n"
450 "string found in its common name, or by its SHA-1 hash.\n"
451 "If no keychains are specified to search, the default search list is used.",
452 "Delete an identity (certificate + private key) from a keychain."},
453
454 { "set-identity-preference", set_identity_preference,
455 "[-n] [-c identity] [-s service] [-u keyUsage] [-Z hash] [keychain...]\n"
456 " -n Specify no identity (clears existing preference for service)\n"
457 " -c Specify identity by common name of the certificate\n"
458 " -s Specify service (may be a URL, RFC822 email address, DNS host, or\n"
459 " other name) for which this identity is to be preferred\n"
460 " -u Specify key usage (optional) - see man page for values\n"
461 " -Z Specify identity by SHA-1 hash of certificate (optional)\n",
462 "Set the preferred identity to use for a service."},
463
464 { "get-identity-preference", get_identity_preference,
465 "[-s service] [-u keyUsage] [-p] [-c] [-Z] [keychain...]\n"
466 " -s Specify service (may be a URL, RFC822 email address, DNS host, or\n"
467 " other name)\n"
468 " -u Specify key usage (optional) - see man page for values\n"
469 " -p Output identity certificate in pem format\n"
470 " -c Print common name of the preferred identity certificate\n"
471 " -Z Print SHA-1 hash of the preferred identity certificate\n",
472 "Get the preferred identity to use for a service."},
473
474 { "create-db", db_create,
475 "[-ao0] [-g dl|cspdl] [-m mode] [name]\n"
476 " -a Turn off autocommit\n"
477 " -g Attach to \"guid\" rather than the AppleFileDL\n"
478 " -m Set the inital mode of the created db to \"mode\"\n"
479 " -o Force using openparams argument\n"
480 " -0 Force using version 0 openparams\n"
481 "If no name is provided, ask the user interactively.",
482 "Create a db using the DL." },
483
484 { "export" , keychain_export,
485 "[-k keychain] [-t type] [-f format] [-w] [-p] [-P passphrase] [-o outfile]\n"
486 " -k keychain to export items from\n"
487 " -t Type = certs|allKeys|pubKeys|privKeys|identities|all (Default: all)\n"
488 " -f Format = openssl|openssh1|openssh2|bsafe|pkcs7|pkcs8|pkcs12|pemseq|x509\n"
489 " ...default format is pemseq for aggregate, openssl for single\n"
490 " -w Private keys are wrapped\n"
491 " -p PEM encode the output\n"
492 " -P Specify wrapping passphrase immediately (default is secure passphrase via GUI)\n"
493 " -o Specify output file (default is stdout)\n"
494 "Use of the -P option is insecure\n",
495 "Export items from a keychain." },
496
497 { "import", keychain_import,
498 "inputfile [-k keychain] [-t type] [-f format] [-w] [-P passphrase] [options...]\n"
499 " -k Target keychain to import into\n"
500 " -t Type = pub|priv|session|cert|agg\n"
501 " -f Format = openssl|openssh1|openssh2|bsafe|raw|pkcs7|pkcs8|pkcs12|netscape|pemseq\n"
502 " -w Specify that private keys are wrapped and must be unwrapped on import\n"
503 " -x Specify that private keys are non-extractable after being imported\n"
504 " -P Specify wrapping passphrase immediately (default is secure passphrase via GUI)\n"
505 " -a Specify name and value of extended attribute (can be used multiple times)\n"
506 " -A Allow any application to access the imported key without warning (insecure, not recommended!)\n"
507 " -T Specify an application which may access the imported key (multiple -T options are allowed)\n"
508 "Use of the -P option is insecure\n",
509 "Import items into a keychain." },
510
511 { "export-smartcard" , ctk_export,
512 "[-i id] [-t type] \n"
513 " -i id of the smartcard to export (available IDs can be listed by list-smartcards\n"
514 " command, default: export all smartcards)\n"
515 " -t Type = certs|privKeys|identities|all (Default: all)\n",
516 "Export items from a smartcard." },
517
518 { "cms", cms_util,
519 "[-C|-D|-E|-S] [<options>]\n"
520 " -C create a CMS encrypted message\n"
521 " -D decode a CMS message\n"
522 " -E create a CMS enveloped message\n"
523 " -S create a CMS signed message\n"
524 "\n"
525 "Decoding options:\n"
526 " -c content use this detached content file\n"
527 " -h level generate email headers with info about CMS message\n"
528 " (output level >= 0)\n"
529 " -n suppress output of content\n"
530 "\n"
531 "Encoding options:\n"
532 " -r id,... create envelope for these recipients,\n"
533 " where id can be a certificate nickname or email address\n"
534 " -G include a signing time attribute\n"
535 " -H hash hash = MD2|MD4|MD5|SHA1|SHA256|SHA384|SHA512 (default: SHA1)\n"
536 " -N nick use certificate named \"nick\" for signing\n"
537 " -P include a SMIMECapabilities attribute\n"
538 " -T do not include content in CMS message\n"
539 " -Y nick include an EncryptionKeyPreference attribute with certificate\n"
540 " (use \"NONE\" to omit)\n"
541 " -Z hash find a certificate by subject key ID\n"
542 "\n"
543 "Common options:\n"
544 " -e envelope specify envelope file (valid with -D or -E)\n"
545 " -k keychain specify keychain to use\n"
546 " -i infile use infile as source of data (default: stdin)\n"
547 " -o outfile use outfile as destination of data (default: stdout)\n"
548 " -p password use password as key db password (default: prompt). Using -p is insecure\n"
549 " -s pass data a single byte at a time to CMS\n"
550 " -u certusage set type of certificate usage (default: certUsageEmailSigner)\n"
551 " -v print debugging information\n"
552 "\n"
553 "Cert usage codes:\n"
554 " 0 - certUsageSSLClient\n"
555 " 1 - certUsageSSLServer\n"
556 " 2 - certUsageSSLServerWithStepUp\n"
557 " 3 - certUsageSSLCA\n"
558 " 4 - certUsageEmailSigner\n"
559 " 5 - certUsageEmailRecipient\n"
560 " 6 - certUsageObjectSigner\n"
561 " 7 - certUsageUserCertImport\n"
562 " 8 - certUsageVerifyCA\n"
563 " 9 - certUsageProtectedObjectSigner\n"
564 " 10 - certUsageStatusResponder\n"
565 " 11 - certUsageAnyCA",
566 "Encode or decode CMS messages." },
567
568 { "install-mds" , mds_install,
569 "", /* no options */
570 "Install (or re-install) the MDS database." },
571
572 { "add-trusted-cert" , trusted_cert_add,
573 " [<options>] [certFile]\n"
574 " -d Add to admin cert store; default is user\n"
575 " -r resultType resultType = trustRoot|trustAsRoot|deny|unspecified;\n"
576 " default is trustRoot\n"
577 " -p policy Specify policy constraint (ssl, smime, codeSign, IPSec, iChat,\n"
578 " basic, swUpdate, pkgSign, pkinitClient, pkinitServer, eap)\n"
579 " -a appPath Specify application constraint\n"
580 " -s policyString Specify policy-specific string\n"
581 " -e allowedError Specify allowed error (certExpired, hostnameMismatch) or integer\n"
582 " -u keyUsage Specify key usage, an integer\n"
583 " -k keychain Specify keychain to which cert is added\n"
584 " -i settingsFileIn Input trust settings file; default is user domain\n"
585 " -o settingsFileOut Output trust settings file; default is user domain\n"
586 " certFile Certificate(s)",
587 "Add trusted certificate(s)." },
588
589 { "remove-trusted-cert" , trusted_cert_remove,
590 " [-d] [-D] [certFile]\n"
591 " -d Remove from admin cert store (default is user)\n"
592 " -D Remove default setting instead of per-cert setting\n"
593 " certFile Certificate(s)",
594 "Remove trusted certificate(s)." },
595
596 { "dump-trust-settings" , trusted_cert_dump,
597 " [-s] [-d]\n"
598 " -s Display trusted system certs (default is user)\n"
599 " -d Display trusted admin certs (default is user)\n",
600 "Display contents of trust settings." },
601
602 { "user-trust-settings-enable", user_trust_enable,
603 "[-d] [-e]\n"
604 " -d Disable user-level trust Settings\n"
605 " -e Enable user-level trust Settings\n"
606 "With no parameters, show current enable state of user-level trust settings.",
607 "Display or manipulate user-level trust settings." },
608
609 { "trust-settings-export", trust_settings_export,
610 " [-s] [-d] settings_file\n"
611 " -s Export system trust settings (default is user)\n"
612 " -d Export admin trust settings (default is user)\n",
613 "Export trust settings." },
614
615 { "trust-settings-import", trust_settings_import,
616 " [-d] settings_file\n"
617 " -d Import admin trust settings (default is user)\n",
618 "Import trust settings." },
619
620 { "verify-cert" , verify_cert,
621 " [<options>]\n"
622 " -c certFile Certificate to verify. Can be specified multiple times, leaf first.\n"
623 " -r rootCertFile Root Certificate. Can be specified multiple times.\n"
624 " -p policy Verify Policy (basic, ssl, smime, codeSign, IPSec, swUpdate, pkgSign,\n"
625 " eap, appleID, macappstore, timestamping); default is basic.\n"
626 " -d date Set date and time to use when verifying certificate,\n"
627 " provided in the form of YYYY-MM-DD-hh:mm:ss (time optional) in GMT.\n"
628 " e.g: 2016-04-25-15:59:59 for April 25, 2016 at 3:59:59 pm in GMT\n"
629 " -k keychain Keychain. Can be called multiple times. Default is default search list.\n"
630 " -n No keychain search list.\n"
631 " -L Local certificates only (do not try to fetch missing CA certs from net).\n"
632 " -l Leaf cert is a CA (normally an error, unless this option is given).\n"
633 " -e emailAddress Email address for smime policy.\n"
634 " -s sslHost SSL host name for ssl policy.\n"
635 " -q Quiet.\n",
636 "Verify certificate(s)." },
637
638 { "authorize" , authorize,
639 "[<options>] <right(s)...>\n"
640 " -u Allow user interaction.\n"
641 " -c Use login name and prompt for password.\n"
642 " -C login Use given login name and prompt for password.\n"
643 " -x Do NOT share -c/-C explicit credentials\n"
644 #ifndef NDEBUG
645 " -E Don't extend rights.\n"
646 #endif
647 " -p Allow returning partial rights.\n"
648 " -d Destroy acquired rights.\n"
649 " -P Pre-authorize rights only.\n"
650 " -l Operate authorizations in least privileged mode.\n"
651 " -i Internalize authref passed on stdin.\n"
652 " -e Externalize authref to stdout.\n"
653 " -w Wait until stdout is closed (to allow reading authref from pipe).\n"
654 "Extend rights flag is passed per default.",
655 "Perform authorization operations." },
656
657 { "authorizationdb" , authorizationdb,
658 "read <right-name>\n"
659 " authorizationdb remove <right-name>\n"
660 " authorizationdb write <right-name> [allow|deny|<rulename>]\n"
661 "If no rulename is specified, write will read a plist from stdin.\n"
662 " authorizationdb merge source [destination]\n"
663 "If no destination path is specified, merge will merge to /etc/authorization.\n"
664 " authorizationdb smartcard <enable|disable|status>\n"
665 "Enables/disables smartcard login support or report current status.",
666 "Make changes to the authorization policy database." },
667
668 { "execute-with-privileges" , execute_with_privileges,
669 "<program> [args...]\n"
670 "On success, stdin will be read and forwarded to the tool.",
671 "Execute tool with privileges." },
672
673 { "leaks", leaks,
674 "[-cycles] [-nocontext] [-nostacks] [-exclude symbol]\n"
675 " -cycles Use a stricter algorithm (\"man leaks\" for details)\n"
676 " -nocontext Withhold hex dumps of the leaked memory\n"
677 " -nostacks Don't show stack traces of leaked memory\n"
678 " -exclude Ignore leaks called from \"symbol\"\n"
679 "(Set the environment variable MallocStackLogging to get symbolic traces.)",
680 "Run /usr/bin/leaks on this process." },
681
682 { "error", display_error_code,
683 "<error code(s)...>\n"
684 "Display an error string for the given security-related error code.\n"
685 "The error can be in decimal or hex, e.g. 1234 or 0x1234. Multiple "
686 "errors can be separated by spaces.",
687 "Display a descriptive message for the given error code(s)." },
688
689 { "create-filevaultmaster-keychain", keychain_createMFV,
690 "[-p password] [keychain name]\n"
691 " -p Use \"password\" as the password for the keychain being created\n"
692 " -s Specify the keysize in bits (default 2048; 1024 & 4096 are allowed)\n"
693 "By default the keychain will be created in ~/Library/Keychains/.\n"
694 "Use of the -p option is insecure. Omit it to be prompted.\n",
695 "Create a keychain containing a key pair for FileVault recovery use."
696 },
697
698 { "smartcards" , smartcards,
699 "token [-l] [-e token] [-d token]\n"
700 " -l List disabled smartcard tokens]\n"
701 " -e token Enable specified token\n"
702 " -d token Disable specified token\n",
703 "Enable, disable or list disabled smartcard tokens." },
704
705 { "translocate-create", translocate_create,
706 "<path to translocate>\n"
707 "Displays the created path or the error returned.",
708 "Create a translocation point for the provided path" },
709
710 { "translocate-policy-check", translocate_policy,
711 "<path to check>\n"
712 "Displays \"Would translocate\" or \"Would not translocate\"\n"
713 "based on the current state of the path and system policy.",
714 "Check whether a path would be translocated." },
715
716 { "translocate-status-check", translocate_check,
717 "<path to check>\n"
718 "Displays \"TRANSLOCATED\" or \"NOT TRANSLOCATED\"\n"
719 "for the given path.",
720 "Check whether a path is translocated." },
721
722 { "translocate-original-path", translocate_original_path,
723 "<path to check>\n"
724 "If the provided path is translocated, display the original path\n"
725 "If the provided path is not translocated, display the passed in path",
726 "Find the original path for a translocated path." },
727 {}
728 };
729
730 /* Global variables. */
731 int do_quiet = 0;
732 int do_verbose = 0;
733
734 /* Return 1 if name matches command. */
735 static int
736 match_command(const char *command, const char *name)
737 {
738 return !strncmp(command, name, strlen(name));
739 }
740
741 /* The help command. */
742 static int
743 help(int argc, char * const *argv)
744 {
745 const command *c;
746
747 if (argc > 1)
748 {
749 char * const *arg;
750 for (arg = argv + 1; *arg; ++arg)
751 {
752 int found = 0;
753
754 for (c = commands; c->c_name; ++c)
755 {
756 if (match_command(c->c_name, *arg))
757 {
758 found = 1;
759 break;
760 }
761 }
762
763 if (found)
764 printf("Usage: %s %s\n", c->c_name, c->c_usage);
765 else
766 {
767 sec_error("%s: no such command: %s", argv[0], *arg);
768 return 1;
769 }
770 }
771 }
772 else
773 {
774 for (c = commands; c->c_name; ++c)
775 printf(" %-36s %s\n", c->c_name, c->c_help);
776 }
777
778 return 0;
779 }
780
781 /* States for split_line parser. */
782 typedef enum
783 {
784 SKIP_WS,
785 READ_ARG,
786 READ_ARG_ESCAPED,
787 QUOTED_ARG,
788 QUOTED_ARG_ESCAPED
789 } parse_state;
790
791 /* Split a line into multiple arguments and return them in *pargc and *pargv. */
792 static void
793 split_line(char *line, int *pargc, char * const **pargv)
794 {
795 static char *argvec[MAX_ARGS + 1];
796 int argc = 0;
797 char *ptr = line;
798 char *dst = line;
799 parse_state state = SKIP_WS;
800 int quote_ch = 0;
801
802 for (ptr = line; *ptr; ++ptr)
803 {
804 if (state == SKIP_WS)
805 {
806 if (isspace(*ptr))
807 continue;
808
809 if (*ptr == '"' || *ptr == '\'')
810 {
811 quote_ch = *ptr;
812 state = QUOTED_ARG;
813 argvec[argc] = dst;
814 continue; /* Skip the quote. */
815 }
816 else
817 {
818 state = READ_ARG;
819 argvec[argc] = dst;
820 }
821 }
822
823 if (state == READ_ARG)
824 {
825 if (*ptr == '\\')
826 {
827 state = READ_ARG_ESCAPED;
828 continue;
829 }
830 else if (isspace(*ptr))
831 {
832 /* 0 terminate each arg. */
833 *dst++ = '\0';
834 argc++;
835 state = SKIP_WS;
836 if (argc >= MAX_ARGS)
837 break;
838 }
839 else
840 *dst++ = *ptr;
841 }
842
843 if (state == QUOTED_ARG)
844 {
845 if (*ptr == '\\')
846 {
847 state = QUOTED_ARG_ESCAPED;
848 continue;
849 }
850 if (*ptr == quote_ch)
851 {
852 /* 0 terminate each arg. */
853 *dst++ = '\0';
854 argc++;
855 state = SKIP_WS;
856 if (argc >= MAX_ARGS)
857 break;
858 }
859 else
860 *dst++ = *ptr;
861 }
862
863 if (state == READ_ARG_ESCAPED)
864 {
865 *dst++ = *ptr;
866 state = READ_ARG;
867 }
868
869 if (state == QUOTED_ARG_ESCAPED)
870 {
871 *dst++ = *ptr;
872 state = QUOTED_ARG;
873 }
874 }
875
876 if (state != SKIP_WS)
877 {
878 /* Terminate last arg. */
879 *dst++ = '\0';
880 argc++;
881 }
882
883 /* Teminate arg vector. */
884 argvec[argc] = NULL;
885
886 *pargv = argvec;
887 *pargc = argc;
888 }
889
890 /* Print a (hopefully) useful usage message. */
891 static int
892 usage(void)
893 {
894 printf(
895 "Usage: %s [-h] [-i] [-l] [-p prompt] [-q] [-v] [command] [opt ...]\n"
896 " -i Run in interactive mode.\n"
897 " -l Run /usr/bin/leaks -nocontext before exiting.\n"
898 " -p Set the prompt to \"prompt\" (implies -i).\n"
899 " -q Be less verbose.\n"
900 " -v Be more verbose about what's going on.\n"
901 "%s commands are:\n", prog_name, prog_name);
902 help(0, NULL);
903 return 2;
904 }
905
906 /* Execute a single command. */
907 static int
908 execute_command(int argc, char * const *argv)
909 {
910 const command *c;
911 int found = 0;
912
913 /* Nothing to do. */
914 if (argc == 0)
915 return 0;
916
917 for (c = commands; c->c_name; ++c)
918 {
919 if (match_command(c->c_name, argv[0]))
920 {
921 found = 1;
922 break;
923 }
924 }
925
926 if (found)
927 {
928 int result;
929
930 /* Reset getopt for command proc. */
931 optind = 1;
932 optreset = 1;
933
934 if (do_verbose)
935 {
936 int ix;
937
938 fprintf(stderr, "%s", c->c_name);
939 for (ix = 1; ix < argc; ++ix)
940 fprintf(stderr, " \"%s\"", argv[ix]);
941 fprintf(stderr, "\n");
942 }
943
944 result = c->c_func(argc, argv);
945 if (result == 2)
946 fprintf(stderr, "Usage: %s %s\n %s\n", c->c_name, c->c_usage, c->c_help);
947
948 return result;
949 }
950 else
951 {
952 sec_error("unknown command \"%s\"", argv[0]);
953 return 1;
954 }
955 }
956
957 static void
958 receive_notifications(void)
959 {
960 /* Run the CFRunloop to get any pending notifications. */
961 while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, TRUE) == kCFRunLoopRunHandledSource);
962 }
963
964
965 const char *
966 sec_errstr(int err)
967 {
968 const char *errString;
969 if (IS_SEC_ERROR(err))
970 errString = SECErrorString(err);
971 else
972 errString = cssmErrorString(err);
973 return errString;
974 }
975
976 void
977 sec_error(const char *msg, ...)
978 {
979 va_list args;
980
981 fprintf(stderr, "%s: ", prog_name);
982
983 va_start(args, msg);
984 vfprintf(stderr, msg, args);
985 va_end(args);
986
987 fprintf(stderr, "\n");
988 }
989
990 void
991 sec_perror(const char *msg, int err)
992 {
993 sec_error("%s: %s", msg, sec_errstr(err));
994 }
995
996 int
997 main(int argc, char * const *argv)
998 {
999 int result = 0;
1000 int do_help = 0;
1001 int do_interactive = 0;
1002 int do_leaks = 0;
1003 int ch;
1004
1005
1006 /* Remember my name. */
1007 prog_name = strrchr(argv[0], '/');
1008 prog_name = prog_name ? prog_name + 1 : argv[0];
1009
1010 /* Do getopt stuff for global options. */
1011 optind = 1;
1012 optreset = 1;
1013 while ((ch = getopt(argc, argv, "hilp:qvR")) != -1)
1014 {
1015 switch (ch)
1016 {
1017 case 'h':
1018 do_help = 1;
1019 break;
1020 case 'i':
1021 do_interactive = 1;
1022 break;
1023 case 'l':
1024 do_leaks = 1;
1025 break;
1026 case 'p':
1027 do_interactive = 1;
1028 prompt_string = optarg;
1029 break;
1030 case 'q':
1031 do_quiet = 1;
1032 break;
1033 case 'v':
1034 do_verbose = 1;
1035 break;
1036 case 'R':
1037 // "Recovery mode", do NOT ask security-checksystem to run when using keychain APIs
1038 // NOTE: this is a hidden option (not in the usage message)
1039 SecKeychainSystemKeychainCheckWouldDeadlock();
1040 break;
1041 case '?':
1042 default:
1043 return usage();
1044 }
1045 }
1046
1047 argc -= optind;
1048 argv += optind;
1049
1050 if (do_help)
1051 {
1052 /* Munge argc/argv so that argv[0] is something. */
1053 return help(argc + 1, argv - 1);
1054 }
1055 else if (argc > 0)
1056 {
1057 receive_notifications();
1058 result = execute_command(argc, argv);
1059 receive_notifications();
1060 }
1061 else if (do_interactive)
1062 {
1063 /* In interactive mode we just read commands and run them until readline returns NULL. */
1064
1065 /* Only show prompt string if stdin is a tty. */
1066 int show_prompt = isatty(0);
1067
1068 for (;;)
1069 {
1070 static char buffer[MAX_LINE_LEN];
1071 char * const *av, *input;
1072 int ac;
1073
1074 if (show_prompt)
1075 fprintf(stderr, "%s", prompt_string);
1076
1077 input = readline(buffer, MAX_LINE_LEN);
1078 if (!input)
1079 break;
1080
1081 split_line(input, &ac, &av);
1082 receive_notifications();
1083 result = execute_command(ac, av);
1084 receive_notifications();
1085 if (result == -1)
1086 {
1087 result = 0;
1088 break;
1089 }
1090
1091 if (result && ! do_quiet)
1092 {
1093 fprintf(stderr, "%s: returned %d\n", av[0], result);
1094 }
1095 }
1096 }
1097 else
1098 result = usage();
1099
1100 if (do_leaks)
1101 {
1102 char *const argvec[3] = { "leaks", "-nocontext", NULL };
1103 leaks(2, argvec);
1104 }
1105
1106 return result;
1107 }