9 #include <Security/Security.h>
10 #include <utilLib/common.h>
11 #include <clAppUtils/identPicker.h>
12 #include <clAppUtils/printCertName.h>
13 #include <security_cdsa_utils/cuPrintCert.h>
15 static void usage(char **argv
)
17 printf("usage: %s command domain [options]\n", argv
[0]);
18 printf("Commands:\n");
19 printf(" s -- select with picker, set as identity for domain\n");
20 printf(" d -- display identity for domain\n");
21 printf(" D -- delete identity for domain\n");
23 printf(" -v -- verbose display of certs\n");
24 printf(" -l -- loop for malloc debug\n");
25 printf(" <none for now>\n");
31 static int selectId(CFStringRef domain
)
33 /* open system keychain */
35 const char *sysKcPath
= kSystemKeychainDir kSystemKeychainName
;
37 OSStatus ortn
= SecKeychainOpen(sysKcPath
, &kcRef
);
39 cssmPerror("SecKeychainOpen", ortn
);
43 /* pick an identity */
44 SecIdentityRef idRef
= NULL
;
45 ortn
= sslSimpleIdentPicker(kcRef
, &idRef
);
48 printf("IdentityPicker aborted\n");
52 ortn
= SecIdentitySetSystemIdentity(domain
, idRef
);
54 cssmPerror("SecIdentitySetSystemIdentity", ortn
);
57 printf("...system identity set.\n");
63 static void printCFString(
68 if(!CFStringGetCString(cfString
, cstr
, sizeof(cstr
),
69 kCFStringEncodingUTF8
)) {
70 printf("***Error converting %s to UTF8\n", label
);
73 printf("%s '%s'\n", label
, cstr
);
77 static int showId(CFStringRef domain
, bool verbose
)
79 SecIdentityRef idRef
= NULL
;
80 CFStringRef actualDomain
= NULL
;
83 ortn
= SecIdentityCopySystemIdentity(domain
, &idRef
, &actualDomain
);
85 cssmPerror("SecIdentityCopySystemIdentity", ortn
);
88 SecCertificateRef certRef
= NULL
;
89 ortn
= SecIdentityCopyCertificate(idRef
, &certRef
);
91 cssmPerror("SecIdentityCopyCertificate", ortn
);
96 ortn
= SecCertificateGetData(certRef
, &certData
);
98 cssmPerror("SecCertificateGetData", ortn
);
104 printCFString("Identity obtained for domain", domain
);
106 printf("\n ---- System Identity Certificate ----\n");
107 printCert(certData
.Data
, certData
.Length
, CSSM_FALSE
);
108 printf(" ---- End of System Identity Certificate ----\n");
111 printCertName(certData
.Data
, certData
.Length
, NameIssuer
);
113 printCFString("Actual domain :", actualDomain
);
116 CFRelease(actualDomain
);
120 int main(int argc
, char **argv
)
131 bool verbose
= false;
134 //extern char *optarg;
137 while ((arg
= getopt(argc
, argv
, "hvl")) != -1) {
153 CFStringRef cfDomain
= CFStringCreateWithCString(NULL
, domain
, kCFStringEncodingASCII
);
158 ourRtn
= selectId(cfDomain
);
161 ourRtn
= showId(cfDomain
, verbose
);
164 ourRtn
= SecIdentitySetSystemIdentity(cfDomain
, NULL
);
166 cssmPerror("SecIdentitySetSystemIdentity(NULL)", ourRtn
);
169 printf("...system identity assignment deleted.\n");
180 printf("q to quit, CR to loop again: ");
181 if(getchar() == 'q') {