2 * Copyright (c) 2003-2010,2012,2014 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@
26 #include "keychain_list.h"
28 #include "keychain_utilities.h"
29 #include "security_tool.h"
33 #include <sys/param.h>
35 #include <CoreFoundation/CFArray.h>
36 #include <Security/SecKeychain.h>
38 // SecKeychainCopyLogin
39 #include <Security/SecKeychainPriv.h>
40 #include <Security/SecItem.h>
51 display_name(const void *value
, void *context
)
53 SecKeychainRef keychain
= (SecKeychainRef
)value
;
54 UInt32 pathLength
= MAXPATHLEN
;
55 char pathName
[MAXPATHLEN
+ 1];
56 OSStatus result
= SecKeychainGetPath(keychain
, &pathLength
, pathName
);
58 sec_error("SecKeychainGetPath %p: %s", keychain
, sec_errstr(result
));
60 fprintf(stdout
, " \"%*s\"\n", (int)pathLength
, pathName
);
65 display_list(const char *desc
, CFTypeRef keychainOrArray
)
67 if (desc
&& strlen(desc
))
68 fprintf(stdout
, "%s\n", desc
);
72 fprintf(stdout
, " <NULL>\n");
74 else if (CFGetTypeID(keychainOrArray
) == SecKeychainGetTypeID())
76 display_name(keychainOrArray
, NULL
);
80 CFArrayRef array
= (CFArrayRef
)keychainOrArray
;
81 CFRange range
= { 0, CFArrayGetCount(array
) };
82 CFArrayApplyFunction(array
, range
, display_name
, NULL
);
87 parse_domain(const char *name
, SecPreferencesDomain
*domain
)
89 size_t len
= strlen(name
);
91 if (!strncmp("user", name
, len
))
92 *domain
= kSecPreferencesDomainUser
;
93 else if (!strncmp("system", name
, len
))
94 *domain
= kSecPreferencesDomainSystem
;
95 else if (!strncmp("common", name
, len
))
96 *domain
= kSecPreferencesDomainCommon
;
97 else if (!strncmp("dynamic", name
, len
))
98 *domain
= kSecPreferencesDomainDynamic
;
101 sec_error("Invalid domain: %s", name
);
102 return SHOW_USAGE_MESSAGE
;
109 domain2str(SecPreferencesDomain domain
)
113 case kSecPreferencesDomainUser
:
115 case kSecPreferencesDomainSystem
:
117 case kSecPreferencesDomainCommon
:
119 case kSecPreferencesDomainDynamic
:
127 keychain_ctk_list_item(CFTypeRef item
)
129 char buf
[128] = { 0 };
131 CFTypeID tid
= CFGetTypeID(item
);
132 if (tid
== CFDictionaryGetTypeID()) {
133 CFStringRef tkid
= CFDictionaryGetValue((CFDictionaryRef
)item
, CFSTR("tkid"));
134 if (CFStringGetCString(tkid
, buf
, sizeof(buf
), kCFStringEncodingUTF8
)) {
135 fprintf(stdout
, "%s\n", buf
);
138 fprintf(stderr
, "Unexpected item.");
143 keychain_ctk_list_items(CFArrayRef items
)
145 CFMutableSetRef displayedItemsRef
= CFSetCreateMutable(kCFAllocatorDefault
, CFArrayGetCount(items
), &kCFTypeSetCallBacks
);
147 for (CFIndex i
= 0; i
< CFArrayGetCount(items
); i
++) {
148 CFDictionaryRef item
= CFArrayGetValueAtIndex(items
, i
);
150 if (CFGetTypeID(item
) == CFDictionaryGetTypeID()) {
151 CFStringRef tkid
= CFDictionaryGetValue((CFDictionaryRef
)item
, CFSTR("tkid"));
153 if (tkid
&& !CFSetContainsValue(displayedItemsRef
, tkid
)) {
154 keychain_ctk_list_item(CFArrayGetValueAtIndex(items
, i
));
155 CFSetAddValue(displayedItemsRef
, tkid
);
158 keychain_ctk_list_item(item
);
162 CFRelease(displayedItemsRef
);
166 keychain_list(int argc
, char * const *argv
)
168 CFTypeRef keychainOrArray
= NULL
;
169 CFArrayRef searchList
= NULL
;
170 list_operation operation
= kList
;
171 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
172 Boolean use_domain
= false;
176 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
181 result
= parse_domain(optarg
, &domain
);
191 return SHOW_USAGE_MESSAGE
;
208 result
= 2; // Show usage
213 status
= SecKeychainCopyDomainSearchList(domain
, &searchList
);
216 sec_error("SecKeychainCopyDomainSearchList %s: %s", domain2str(domain
), sec_errstr(status
));
222 fprintf(stdout
, "%s search list: ", domain2str(domain
));
224 display_list("", searchList
);
229 status
= SecKeychainCopySearchList(&searchList
);
232 sec_perror("SecKeychainCopySearchList", status
);
238 display_list("search list:", searchList
);
240 display_list("", searchList
);
247 keychainOrArray
= keychain_create_array(argc
, argv
);
249 searchList
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
251 searchList
= CFArrayCreate(NULL
, &keychainOrArray
, 1, &kCFTypeArrayCallBacks
);
253 searchList
= (CFArrayRef
)CFRetain(keychainOrArray
);
257 status
= SecKeychainSetDomainSearchList(domain
, searchList
);
260 sec_error("SecKeychainSetDomainSearchList %s: %s", domain2str(domain
), sec_errstr(status
));
266 status
= SecKeychainSetSearchList(searchList
);
269 sec_perror("SecKeychainSetSearchList", status
);
277 CFRelease(keychainOrArray
);
279 CFRelease(searchList
);
285 ctk_list(int argc
, char * const *argv
)
287 OSStatus stat
= errSecSuccess
;
288 CFDictionaryRef query
= NULL
;
289 CFTypeRef result
= NULL
;
291 const void *keys
[] = {
298 const void *values
[] = {
301 kSecAttrAccessGroupToken
,
305 query
= CFDictionaryCreate(kCFAllocatorDefault
,
308 sizeof(values
) / sizeof(values
[0]),
309 &kCFTypeDictionaryKeyCallBacks
,
310 &kCFTypeDictionaryValueCallBacks
);
312 stat
= SecItemCopyMatching(query
, (CFTypeRef
*)&result
);
314 if (stat
== errSecItemNotFound
) {
315 fprintf(stderr
, "No smartcards found.\n");
317 sec_error("SecItemCopyMatching: %x (%d) - %s", stat
, stat
, sec_errstr(stat
));
323 if (CFGetTypeID(result
) == CFArrayGetTypeID()) {
324 keychain_ctk_list_items((CFArrayRef
)result
);
326 keychain_ctk_list_item(result
);
342 keychain_default(int argc
, char * const *argv
)
344 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
345 SecKeychainRef keychain
= NULL
;
346 Boolean use_domain
= false;
347 Boolean do_set
= false;
351 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
356 result
= parse_domain(optarg
, &domain
);
366 return SHOW_USAGE_MESSAGE
;
376 keychain
= (SecKeychainRef
)keychain_create_array(argc
, argv
);
378 return SHOW_USAGE_MESSAGE
;
382 status
= SecKeychainSetDomainDefault(domain
, keychain
);
385 sec_error("SecKeychainSetDomainDefault %s: %s", domain2str(domain
), sec_errstr(status
));
391 status
= SecKeychainSetDefault(keychain
);
394 sec_perror("SecKeychainSetDefault", status
);
402 return SHOW_USAGE_MESSAGE
;
406 status
= SecKeychainCopyDomainDefault(domain
, &keychain
);
409 sec_error("SecKeychainCopyDomainDefault %s: %s", domain2str(domain
), sec_errstr(status
));
415 fprintf(stdout
, "default %s keychain: ", domain2str(domain
));
417 display_list("", keychain
);
422 status
= SecKeychainCopyDefault(&keychain
);
425 sec_perror("SecKeychainCopyDefault", status
);
431 display_list("default keychain: ", keychain
);
433 display_list("", keychain
);
446 keychain_login(int argc
, char * const *argv
)
448 SecPreferencesDomain domain
= kSecPreferencesDomainUser
;
449 SecKeychainRef keychain
= NULL
;
450 Boolean use_domain
= false;
451 Boolean do_set
= false;
455 while ((ch
= getopt(argc
, argv
, "d:hs")) != -1)
460 result
= parse_domain(optarg
, &domain
);
470 return SHOW_USAGE_MESSAGE
;
480 keychain
= (SecKeychainRef
)keychain_create_array(argc
, argv
);
482 return SHOW_USAGE_MESSAGE
;
487 status
= SecKeychainSetDomainLogin(domain
, keychain
);
490 sec_error("SecKeychainSetDomainLogin %s: %s", domain2str(domain
), sec_errstr(status
));
496 status
= SecKeychainSetLogin(keychain
);
499 sec_perror("SecKeychainSetLogin", status
);
510 return SHOW_USAGE_MESSAGE
;
515 status
= SecKeychainCopyDomainLogin(domain
, &keychain
);
518 sec_error("SecKeychainCopyDomainLogin %s: %s", domain2str(domain
), sec_errstr(status
));
524 fprintf(stdout
, "login %s keychain: ", domain2str(domain
));
526 display_list("", keychain
);
534 status
= SecKeychainCopyLogin(&keychain
);
537 sec_perror("SecKeychainCopyLogin", status
);
543 display_list("login keychain: ", keychain
);
545 display_list("", keychain
);