2 * Copyright (c) 2004,2006,2008 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@
24 * krbtool.cpp - basic PKINIT tool
26 * Created 20 May 2004 by Doug Mitchell.
32 #include <sys/types.h>
34 #include "identPicker.h"
36 #include <Kerberos/KerberosLogin.h>
37 #include <Kerberos/pkinit_cert_store.h>
39 static void usage(char **argv
)
41 printf("Usage: op [option..]\n");
44 printf(" s select PKINIT client cert\n");
45 printf(" d display PKINIT client cert\n");
46 printf(" D delete PKINIT client cert setting\n");
48 printf(" -p principal -- default is current user for login\n");
49 printf(" -l -- loop (for malloc debug)\n");
62 const char *principal
;
66 static int pkinitLogin(
67 const KrbToolArgs
*args
)
69 /* Get a principal string one way or the other */
70 const char *principalStr
= args
->principal
;
73 KLLoginOptions klOpts
= NULL
;
75 if(principalStr
== NULL
) {
76 struct passwd
*pw
= getpwuid(getuid ());
78 printf("***Sorry, can't find current user info. Aborting.\n");
81 principalStr
= strdup(pw
->pw_name
);
85 KLPrincipal principal
= NULL
;
88 klrtn
= KLCreatePrincipalFromString(principalStr
, kerberosVersion_V5
, &principal
);
90 printf("***KLCreatePrincipalFromString returned %d. Aborting.\n", (int)klrtn
);
95 /* Options, later maybe */
96 /* FIXME - don't know if the login options arg is optional */
98 /* By convention we use a non-NULL string as password; this may change later */
99 printf("...attempting TGT acquisition\n");
100 klrtn
= KLAcquireNewInitialTicketsWithPassword(principal
, klOpts
, " ", NULL
);
102 printf("***KLAcquireInitialTicketsWithPassword returned %d\n", (int)klrtn
);
105 printf("...TGT acquisition successful.\n");
108 if(freeStr
&& (principalStr
!= NULL
)) {
109 free((void *)principalStr
);
112 KLDisposeLoginOptions(klOpts
);
114 if(principal
!= NULL
) {
115 KLDisposePrincipal(principal
);
120 static int pkinitSelect(
121 const KrbToolArgs
*args
)
124 SecIdentityRef idRef
= NULL
;
126 if(args
->principal
== NULL
) {
127 printf("***You must supply a principal name for this operation.\n");
130 ortn
= simpleIdentPicker(NULL
, &idRef
);
132 case CSSMERR_CSSM_USER_CANCELED
:
133 printf("...operation terminated with no change to your settings.\n");
138 printf("***Operation aborted.\n");
142 krb5_error_code krtn
= krb5_pkinit_set_client_cert(args
->principal
,
143 (krb5_pkinit_signing_cert_t
)idRef
);
145 cssmPerror("krb5_pkinit_set_client_cert", krtn
);
148 printf("...PKINIT client cert selection successful.\n\n");
156 static int pkinitDisplay(
157 const KrbToolArgs
*args
)
159 krb5_pkinit_signing_cert_t idRef
= NULL
;
160 krb5_error_code krtn
;
162 if(args
->principal
== NULL
) {
163 printf("***You must supply a principal name for this operation.\n");
166 krtn
= krb5_pkinit_get_client_cert(args
->principal
, &idRef
);
168 case errSecItemNotFound
:
169 printf("...No PKINIT client cert configured for %s.\n",
170 args
->principal
? args
->principal
: "Default");
174 SecCertificateRef certRef
= NULL
;
178 ortn
= SecIdentityCopyCertificate((SecIdentityRef
)idRef
, &certRef
);
180 cssmPerror("SecIdentityCopyCertificate", ortn
);
183 ortn
= SecCertificateGetData(certRef
, &cdata
);
185 cssmPerror("SecCertificateGetData", ortn
);
188 printf("--------- PKINIT Client Certificate ---------\n");
189 printCertName(cdata
.Data
, cdata
.Length
, NameBoth
);
190 printf("---------------------------------------------\n\n");
192 char *cert_hash
= NULL
;
193 krb5_data kcert
= {0, cdata
.Length
, (char *)cdata
.Data
};
194 cert_hash
= krb5_pkinit_cert_hash_str(&kcert
);
195 if(cert_hash
== NULL
) {
196 printf("***Error obtaining cert hash\n");
199 printf("Cert hash string : %s\n\n", cert_hash
);
206 cssmPerror("krb5_pkinit_get_client_cert", krtn
);
207 printf("***Error obtaining client cert\n");
211 krb5_pkinit_release_cert(idRef
);
217 static int pkinitDeleteCert(
218 const KrbToolArgs
*args
)
220 krb5_error_code krtn
;
222 krtn
= krb5_pkinit_set_client_cert(args
->principal
, NULL
);
224 cssmPerror("krb5_pkinit_set_client_cert(NULL)", krtn
);
225 printf("***Error deleting client cert entry\n");
228 printf("...client cert setting for %s deleted\n", args
->principal
?
229 args
->principal
: "Default principal");
234 int main(int argc
, char **argv
)
240 KrbToolOp op
= KTO_Login
;
265 memset(&args
, 0, sizeof(args
));
267 while ((arg
= getopt(argc
, argv
, "p:lh")) != -1) {
270 args
.principal
= optarg
;
284 ourRtn
= pkinitLogin(&args
);
287 ourRtn
= pkinitSelect(&args
);
290 ourRtn
= pkinitDisplay(&args
);
293 ourRtn
= pkinitDeleteCert(&args
);
296 printf("***BRRZAP! Internal error.\n");
303 printf("q to quit, anything else to loop: ");