]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/idPref/idPref.cpp
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / clxutils / idPref / idPref.cpp
1 /*
2 * idPref.cpp - maniuplate Identity Prefs
3 */
4
5 #include <stdlib.h>
6 #include <strings.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <clAppUtils/identPicker.h>
10 #include <Security/Security.h>
11 #include <CoreFoundation/CoreFoundation.h>
12
13 static void usage(char **argv)
14 {
15 printf("usage: %s set|get [options]\n", argv[0]);
16 printf("Options:\n");
17 printf(" -p prefName\n");
18 printf(" -k keychain\n");
19 exit(1);
20 }
21
22 int main(int argc, char **argv)
23 {
24 char *kcName = NULL;
25 SecKeychainRef kcRef = NULL;
26 char *prefName = NULL;
27 bool doSet = false;
28
29 if((argc < 2) || (argv[1][0] == 'h')) {
30 usage(argv);
31 }
32 if(!strcmp(argv[1], "get")) {
33 doSet = false;
34 }
35 else if(!strcmp(argv[1], "set")) {
36 doSet = true;
37 }
38 else {
39 printf("Bad op argument\n");
40 usage(argv);
41 }
42
43 extern int optind;
44 optind = 2;
45 extern char *optarg;
46 int arg;
47 while ((arg = getopt(argc, argv, "p:k:h")) != -1) {
48 switch (arg) {
49 case 'p':
50 prefName = optarg;
51 break;
52 case 'k':
53 kcName = optarg;
54 break;
55 case 'h':
56 usage(argv);
57 }
58 }
59 if(optind != argc) {
60 usage(argv);
61 }
62 if(prefName == NULL) {
63 printf("***You must specify a preference name via -p.\n");
64 usage(argv);
65 }
66 CFStringRef prefStr = CFStringCreateWithCString(NULL, prefName, kCFStringEncodingASCII);
67 if(prefStr == NULL) {
68 printf("***Error converting pref name '%s' to CFString.\n", prefName);
69 exit(1);
70 }
71
72 OSStatus ortn;
73 if(kcName) {
74 ortn = SecKeychainOpen(kcName, &kcRef);
75 if(ortn) {
76 cssmPerror("SecKeychainOpen", ortn);
77 exit(1);
78 }
79 }
80
81 SecIdentityRef idRef = NULL;
82 if(doSet) {
83 ortn = sslSimpleIdentPicker(kcRef, &idRef);
84 if(ortn) {
85 printf("Error picking identity; aborting.\n");
86 exit(1);
87 }
88 ortn = SecIdentitySetPreference(idRef, prefStr, 0);
89 if(ortn) {
90 cssmPerror("SecIdentitySetPreference", ortn);
91 exit(1);
92 }
93 printf("...Identity preference set for name '%s'.\n", prefName);
94 }
95 else {
96 ortn = SecIdentityCopyPreference(prefStr, 0, NULL, &idRef);
97 if(ortn) {
98 cssmPerror("SecIdentityCopyPreference", ortn);
99 }
100 else {
101 SecCertificateRef certRef = NULL;
102 ortn = SecIdentityCopyCertificate(idRef, &certRef);
103 if(ortn) {
104 cssmPerror("SecIdentityCopyCertificate", ortn);
105 exit(1);
106 }
107 char *idName = kcItemPrintableName((SecKeychainItemRef)certRef);
108 printf("Identity for prefName '%s' found : '%s'\n",
109 prefName, idName);
110 free(idName);
111 CFRelease(certRef);
112 }
113 }
114 CFRelease(idRef);
115
116 return 0;
117 }
118