xref: /titanic_50/usr/src/cmd/cmd-crypto/pktool/pktool.c (revision d00756ccb34596a328f8a15d1965da5412d366d0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d00756ccSwyllys  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file comprises the main driver for this tool.
307711facfSdinak  * Upon parsing the command verbs from user input, it
317711facfSdinak  * branches to the appropriate modules to perform the
327711facfSdinak  * requested task.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <malloc.h>
39*d00756ccSwyllys #include <libintl.h>
407c478bd9Sstevel@tonic-gate #include <libgen.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
437c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
447c478bd9Sstevel@tonic-gate #include "common.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * The verbcmd construct allows genericizing information about a verb so
487c478bd9Sstevel@tonic-gate  * that it is easier to manipulate.  Makes parsing code easier to read,
497c478bd9Sstevel@tonic-gate  * fix, and extend with new verbs.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate typedef struct verbcmd_s {
527c478bd9Sstevel@tonic-gate 	char	*verb;
537c478bd9Sstevel@tonic-gate 	int	(*action)(int, char *[]);
547711facfSdinak 	int	mode;
55985be8f1Swyllys 	char	*summary;
567711facfSdinak 	char	*synopsis;
577c478bd9Sstevel@tonic-gate } verbcmd;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* External declarations for supported verb actions. */
607c478bd9Sstevel@tonic-gate extern int	pk_setpin(int argc, char *argv[]);
617711facfSdinak extern int	pk_list(int argc, char *argv[]);
627711facfSdinak extern int	pk_delete(int argc, char *argv[]);
637711facfSdinak extern int	pk_import(int argc, char *argv[]);
647711facfSdinak extern int	pk_export(int argc, char *argv[]);
657711facfSdinak extern int	pk_tokens(int argc, char *argv[]);
6699ebb4caSwyllys extern int	pk_gencert(int argc, char *argv[]);
6799ebb4caSwyllys extern int	pk_gencsr(int argc, char *argv[]);
6899ebb4caSwyllys extern int	pk_download(int argc, char *argv[]);
6999ebb4caSwyllys extern int	pk_genkey(int argc, char *argv[]);
70*d00756ccSwyllys extern int	pk_signcsr(int argc, char *argv[]);
717711facfSdinak 
727711facfSdinak /* Forward declarations for "built-in" verb actions. */
737711facfSdinak static int	pk_help(int argc, char *argv[]);
747c478bd9Sstevel@tonic-gate 
75*d00756ccSwyllys #define	TOKEN_IDX 0
76*d00756ccSwyllys #define	TOKEN_VERB gettext("tokens")
77*d00756ccSwyllys #define	TOKEN_SUMM gettext("lists all visible PKCS#11 tokens")
78*d00756ccSwyllys #define	TOKEN_SYN  gettext("tokens")
79*d00756ccSwyllys 
80*d00756ccSwyllys #define	SETPIN_IDX 1
81*d00756ccSwyllys #define	SETPIN_VERB gettext("setpin")
82*d00756ccSwyllys #define	SETPIN_SUMM gettext("changes user authentication passphrase "\
83*d00756ccSwyllys 	"for keystore access")
84*d00756ccSwyllys #define	SETPIN_SYN gettext(\
85*d00756ccSwyllys 	"setpin [ keystore=pkcs11 ]\n\t\t" \
86*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
87*d00756ccSwyllys 	"setpin keystore=nss\n\t\t" \
88*d00756ccSwyllys 	"[ token=token ]\n\t\t" \
89*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
90*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t")
91*d00756ccSwyllys 
92*d00756ccSwyllys #define	LIST_IDX 2
93*d00756ccSwyllys #define	LIST_VERB gettext("list")
94*d00756ccSwyllys #define	LIST_SUMM gettext("lists a summary of objects in the keystore")
95*d00756ccSwyllys #define	LIST_SYN gettext(\
96*d00756ccSwyllys 	"list [ token=token[:manuf[:serial]]]\n\t\t" \
97*d00756ccSwyllys 	"[ objtype=private|public|both ]\n\t\t" \
98*d00756ccSwyllys 	"[ label=label ]\n\t" \
99*d00756ccSwyllys  \
100*d00756ccSwyllys 	"list objtype=cert[:[public | private | both ]]\n\t\t" \
101*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
102*d00756ccSwyllys 	"[ keystore=pkcs11 ]\n\t\t" \
103*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
104*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
105*d00756ccSwyllys 	"[ label=cert-label ]\n\t\t" \
106*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
107*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
108*d00756ccSwyllys  \
109*d00756ccSwyllys 	"list objtype=key[:[public | private | both ]]\n\t\t" \
110*d00756ccSwyllys 	"[ keystore=pkcs11 ]\n\t\t" \
111*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
112*d00756ccSwyllys 	"[ label=key-label ]\n\t\t" \
113*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
114*d00756ccSwyllys  \
115*d00756ccSwyllys 	"list keystore=pkcs11 objtype=crl\n\t\t" \
116*d00756ccSwyllys 	"infile=crl-fn\n\t\t" \
117*d00756ccSwyllys 	"[ dir=directory-path ]\n\t" \
118*d00756ccSwyllys  \
119*d00756ccSwyllys 	"list keystore=nss objtype=cert\n\t\t" \
120*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
121*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
122*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
123*d00756ccSwyllys 	"[ nickname=cert-nickname ]\n\t\t" \
124*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
125*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
126*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
127*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
128*d00756ccSwyllys  \
129*d00756ccSwyllys 	"list keystore=nss objtype=key\n\t\t" \
130*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
131*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
132*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
133*d00756ccSwyllys 	"[ nickname=key-nickname ]\n\t" \
134*d00756ccSwyllys  \
135*d00756ccSwyllys 	"list keystore=file objtype=cert\n\t\t" \
136*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
137*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
138*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
139*d00756ccSwyllys 	"[ infile=cert-fn ]\n\t\t" \
140*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
141*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
142*d00756ccSwyllys  \
143*d00756ccSwyllys 	"list keystore=file objtype=key\n\t\t" \
144*d00756ccSwyllys 	"[ infile=key-fn ]\n\t\t" \
145*d00756ccSwyllys 	"[ dir=directory-path ]\n\t" \
146*d00756ccSwyllys  \
147*d00756ccSwyllys 	"list keystore=file objtype=crl\n\t\t" \
148*d00756ccSwyllys 	"infile=crl-fn\n\t\t" \
149*d00756ccSwyllys 	"[ dir=directory-path ]\n\t")
150*d00756ccSwyllys 
151*d00756ccSwyllys #define	DELETE_IDX 3
152*d00756ccSwyllys #define	DELETE_VERB gettext("delete")
153*d00756ccSwyllys #define	DELETE_SUMM gettext("deletes objects in the keystore")
154*d00756ccSwyllys #define	DELETE_SYN gettext(\
155*d00756ccSwyllys 	"delete [ token=token[:manuf[:serial]]]\n\t\t" \
156*d00756ccSwyllys 	"[ objtype=private|public|both ]\n\t\t" \
157*d00756ccSwyllys 	"[ label=object-label ]\n\t" \
158*d00756ccSwyllys  \
159*d00756ccSwyllys 	"delete keystore=nss objtype=cert\n\t\t" \
160*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
161*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
162*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
163*d00756ccSwyllys 	"[ label=cert-label ]\n\t\t" \
164*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
165*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
166*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
167*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
168*d00756ccSwyllys  \
169*d00756ccSwyllys 	"delete keystore=nss objtype=key\n\t\t" \
170*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
171*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
172*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
173*d00756ccSwyllys 	"[ nickname=key-nickname ]\n\t\t" \
174*d00756ccSwyllys  \
175*d00756ccSwyllys 	"delete keystore=nss objtype=crl\n\t\t" \
176*d00756ccSwyllys 	"[ nickname=issuer-nickname ]\n\t\t" \
177*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
178*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
179*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
180*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t" \
181*d00756ccSwyllys  \
182*d00756ccSwyllys 	"delete keystore=pkcs11 " \
183*d00756ccSwyllys 	"objtype=cert[:[public | private | both]]\n\t\t" \
184*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
185*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
186*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
187*d00756ccSwyllys 	"[ label=cert-label ]\n\t\t" \
188*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
189*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
190*d00756ccSwyllys  \
191*d00756ccSwyllys 	"delete keystore=pkcs11 " \
192*d00756ccSwyllys 	"objtype=key[:[public | private | both]]\n\t\t" \
193*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
194*d00756ccSwyllys 	"[ label=key-label ]\n\t\t" \
195*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
196*d00756ccSwyllys  \
197*d00756ccSwyllys 	"delete keystore=pkcs11 objtype=crl\n\t\t" \
198*d00756ccSwyllys 	"infile=crl-fn\n\t\t" \
199*d00756ccSwyllys 	"[ dir=directory-path ]\n\t" \
200*d00756ccSwyllys  \
201*d00756ccSwyllys 	"delete keystore=file objtype=cert\n\t\t" \
202*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
203*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
204*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
205*d00756ccSwyllys 	"[ infile=cert-fn ]\n\t\t" \
206*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
207*d00756ccSwyllys 	"[ criteria=valid|expired|both ]\n\t" \
208*d00756ccSwyllys  \
209*d00756ccSwyllys 	"delete keystore=file objtype=key\n\t\t" \
210*d00756ccSwyllys 	"[ infile=key-fn ]\n\t\t" \
211*d00756ccSwyllys 	"[ dir=directory-path ]\n\t" \
212*d00756ccSwyllys  \
213*d00756ccSwyllys 	"delete keystore=file objtype=crl\n\t\t" \
214*d00756ccSwyllys 	"infile=crl-fn\n\t\t" \
215*d00756ccSwyllys 	"[ dir=directory-path ]\n\t")
216*d00756ccSwyllys 
217*d00756ccSwyllys #define	IMPORT_IDX 4
218*d00756ccSwyllys #define	IMPORT_VERB gettext("import")
219*d00756ccSwyllys #define	IMPORT_SUMM gettext("imports objects from an external source")
220*d00756ccSwyllys #define	IMPORT_SYN gettext(\
221*d00756ccSwyllys 	"import [token=token[:manuf[:serial]]]\n\t\t" \
222*d00756ccSwyllys 	"infile=input-fn\n\t" \
223*d00756ccSwyllys  \
224*d00756ccSwyllys 	"import keystore=nss objtype=cert\n\t\t" \
225*d00756ccSwyllys 	"infile=input-fn\n\t\t" \
226*d00756ccSwyllys 	"label=cert-label\n\t\t" \
227*d00756ccSwyllys 	"[ trust=trust-value ]\n\t\t" \
228*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
229*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
230*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t" \
231*d00756ccSwyllys  \
232*d00756ccSwyllys 	"import keystore=nss objtype=crl\n\t\t" \
233*d00756ccSwyllys 	"infile=input-fn\n\t\t" \
234*d00756ccSwyllys 	"[ verifycrl=y|n ]\n\t\t" \
235*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
236*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
237*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t" \
238*d00756ccSwyllys  \
239*d00756ccSwyllys 	"import keystore=pkcs11\n\t\t" \
240*d00756ccSwyllys 	"infile=input-fn\n\t\t" \
241*d00756ccSwyllys 	"label=label\n\t\t" \
242*d00756ccSwyllys 	"[ objtype=cert|key ]\n\t\t" \
243*d00756ccSwyllys 	"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
244*d00756ccSwyllys 	"[ sensitive=y|n ]\n\t\t" \
245*d00756ccSwyllys 	"[ extractable=y|n ]\n\t\t" \
246*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
247*d00756ccSwyllys  \
248*d00756ccSwyllys 	"import keystore=pkcs11 objtype=crl\n\t\t" \
249*d00756ccSwyllys 	"infile=input-crl-fn\n\t\t" \
250*d00756ccSwyllys 	"outcrl=output-crl-fn\n\t\t" \
251*d00756ccSwyllys 	"outformat=pem|der\n\t\t" \
252*d00756ccSwyllys 	"[ dir=output-crl-directory-path ]\n\t" \
253*d00756ccSwyllys  \
254*d00756ccSwyllys 	"import keystore=file\n\t\t" \
255*d00756ccSwyllys 	"infile=input-fn\n\t\t" \
256*d00756ccSwyllys 	"outkey=output-key-fn\n\t\t" \
257*d00756ccSwyllys 	"outcert=output-cert-fn\n\t\t" \
258*d00756ccSwyllys 	"[ dir=output-cert-dir-path ]\n\t\t" \
259*d00756ccSwyllys 	"[ keydir=output-key-dir-path ]\n\t\t" \
260*d00756ccSwyllys 	"[ outformat=pem|der|pkcs12 ]\n\t" \
261*d00756ccSwyllys  \
262*d00756ccSwyllys 	"import keystore=file objtype=crl\n\t\t" \
263*d00756ccSwyllys 	"infile=input-crl-fn\n\t\t" \
264*d00756ccSwyllys 	"outcrl=output-crl-fn\n\t\t" \
265*d00756ccSwyllys 	"outformat=pem|der\n\t\t" \
266*d00756ccSwyllys 	"[ dir=output-crl-directory-path ]\n\t")
267*d00756ccSwyllys 
268*d00756ccSwyllys #define	EXPORT_IDX 5
269*d00756ccSwyllys #define	EXPORT_VERB gettext("export")
270*d00756ccSwyllys #define	EXPORT_SUMM gettext("exports objects from the keystore to a file")
271*d00756ccSwyllys #define	EXPORT_SYN gettext(\
272*d00756ccSwyllys 	"export [token=token[:manuf[:serial]]]\n\t\t" \
273*d00756ccSwyllys 	"outfile=output-fn\n\t" \
274*d00756ccSwyllys  \
275*d00756ccSwyllys 	"export keystore=nss\n\t\t" \
276*d00756ccSwyllys 	"outfile=output-fn\n\t\t" \
277*d00756ccSwyllys 	"[ objtype=cert|key ]\n\t\t" \
278*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
279*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
280*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
281*d00756ccSwyllys 	"[ nickname=cert-nickname ]\n\t\t" \
282*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
283*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
284*d00756ccSwyllys 	"[ prefix=DBPrefix ]\n\t\t" \
285*d00756ccSwyllys 	"[ outformat=pem|der|pkcs12 ]\n\t" \
286*d00756ccSwyllys  \
287*d00756ccSwyllys 	"export keystore=pkcs11\n\t\t" \
288*d00756ccSwyllys 	"outfile=output-fn\n\t\t" \
289*d00756ccSwyllys 	"[ objtype=cert|key ]\n\t\t" \
290*d00756ccSwyllys 	"[ label=label ]\n\t\t" \
291*d00756ccSwyllys 	"[ subject=subject-DN ]\n\t\t" \
292*d00756ccSwyllys 	"[ issuer=issuer-DN ]\n\t\t" \
293*d00756ccSwyllys 	"[ serial=serial number ]\n\t\t" \
294*d00756ccSwyllys 	"[ outformat=pem|der|pkcs12|raw ]\n\t\t" \
295*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
296*d00756ccSwyllys  \
297*d00756ccSwyllys 	"export keystore=file\n\t\t" \
298*d00756ccSwyllys 	"certfile=cert-input-fn\n\t\t" \
299*d00756ccSwyllys 	"keyfile=key-input-fn\n\t\t" \
300*d00756ccSwyllys 	"outfile=output-pkcs12-fn\n\t\t" \
301*d00756ccSwyllys 	"[ dir=directory-path ]\n\t")
302*d00756ccSwyllys 
303*d00756ccSwyllys #define	GENCERT_IDX 6
304*d00756ccSwyllys #define	GENCERT_VERB gettext("gencert")
305*d00756ccSwyllys #define	GENCERT_SUMM gettext("creates a self-signed X.509v3 certificate")
306*d00756ccSwyllys #define	GENCERT_SYN gettext(\
307*d00756ccSwyllys 	"gencert [-i] keystore=nss\n\t\t" \
308*d00756ccSwyllys 	"label=cert-nickname\n\t\t" \
309*d00756ccSwyllys 	"serial=serial number hex string]\n\t\t" \
310*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
311*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
312*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
313*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
314*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
315*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
316*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
317*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
318*d00756ccSwyllys 	"[ trust=trust-value ]\n\t\t" \
319*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
320*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t" \
321*d00756ccSwyllys  \
322*d00756ccSwyllys 	"gencert [-i] [ keystore=pkcs11 ]\n\t\t" \
323*d00756ccSwyllys 	"label=key/cert-label\n\t\t" \
324*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
325*d00756ccSwyllys 	"serial=serial number hex string\n\t\t" \
326*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
327*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
328*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
329*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
330*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
331*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
332*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t" \
333*d00756ccSwyllys  \
334*d00756ccSwyllys 	"gencert [-i] keystore=file\n\t\t" \
335*d00756ccSwyllys 	"outcert=cert_filename\n\t\t" \
336*d00756ccSwyllys 	"outkey=key_filename\n\t\t" \
337*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
338*d00756ccSwyllys 	"serial=serial number hex string\n\t\t" \
339*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
340*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
341*d00756ccSwyllys 	"[ format=der|pem ]\n\t\t" \
342*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
343*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
344*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
345*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
346*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
347*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t")
348*d00756ccSwyllys 
349*d00756ccSwyllys #define	GENCSR_IDX 7
350*d00756ccSwyllys #define	GENCSR_VERB gettext("gencsr")
351*d00756ccSwyllys #define	GENCSR_SUMM gettext("creates a PKCS#10 certificate signing " \
352*d00756ccSwyllys 	"request file")
353*d00756ccSwyllys 
354*d00756ccSwyllys #define	GENCSR_SYN gettext(\
355*d00756ccSwyllys 	"gencsr [-i] keystore=nss \n\t\t" \
356*d00756ccSwyllys 	"nickname=cert-nickname\n\t\t" \
357*d00756ccSwyllys 	"outcsr=csr-fn\n\t\t" \
358*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
359*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
360*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
361*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
362*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
363*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t\t" \
364*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
365*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
366*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
367*d00756ccSwyllys 	"[ format=pem|der ]\n\t" \
368*d00756ccSwyllys  \
369*d00756ccSwyllys 	"gencsr [-i] [ keystore=pkcs11 ]\n\t\t" \
370*d00756ccSwyllys 	"label=key-label\n\t\t" \
371*d00756ccSwyllys 	"outcsr=csr-fn\n\t\t" \
372*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
373*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
374*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
375*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
376*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
377*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
378*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
379*d00756ccSwyllys 	"[ format=pem|der ]]\n\t" \
380*d00756ccSwyllys  \
381*d00756ccSwyllys 	"gencsr [-i] keystore=file\n\t\t" \
382*d00756ccSwyllys 	"outcsr=csr-fn\n\t\t" \
383*d00756ccSwyllys 	"outkey=key-fn\n\t\t" \
384*d00756ccSwyllys 	"subject=subject-DN\n\t\t" \
385*d00756ccSwyllys 	"[ altname=[critical:]SubjectAltName ]\n\t\t" \
386*d00756ccSwyllys 	"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
387*d00756ccSwyllys 	"[ keytype=rsa|dsa ]\n\t\t" \
388*d00756ccSwyllys 	"[ keylen=key-size ]\n\t\t" \
389*d00756ccSwyllys 	"[ eku=[critical:]EKU name,...]\n\t\t" \
390*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
391*d00756ccSwyllys 	"[ format=pem|der ]\n\t")
392*d00756ccSwyllys 
393*d00756ccSwyllys #define	DOWNLOAD_IDX 8
394*d00756ccSwyllys #define	DOWNLOAD_VERB gettext("download")
395*d00756ccSwyllys #define	DOWNLOAD_SUMM gettext("downloads a CRL or certificate file " \
396*d00756ccSwyllys 	"from an external source")
397*d00756ccSwyllys #define	DOWNLOAD_SYN gettext(\
398*d00756ccSwyllys 	"download url=url_str\n\t\t" \
399*d00756ccSwyllys 	"[ objtype=crl|cert ]\n\t\t" \
400*d00756ccSwyllys 	"[ http_proxy=proxy_str ]\n\t\t" \
401*d00756ccSwyllys 	"[ outfile = outfile ]\n\t")
402*d00756ccSwyllys 
403*d00756ccSwyllys #define	GENKEY_IDX 9
404*d00756ccSwyllys #define	GENKEY_VERB gettext("genkey")
405*d00756ccSwyllys #define	GENKEY_SUMM gettext("creates a symmetric key in the keystore")
406*d00756ccSwyllys #define	GENKEY_SYN gettext(\
407*d00756ccSwyllys 	"genkey [ keystore=pkcs11 ]\n\t\t" \
408*d00756ccSwyllys 	"label=key-label\n\t\t" \
409*d00756ccSwyllys 	"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
410*d00756ccSwyllys 	"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
411*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
412*d00756ccSwyllys 	"[ sensitive=y|n ]\n\t\t" \
413*d00756ccSwyllys 	"[ extractable=y|n ]\n\t\t" \
414*d00756ccSwyllys 	"[ print=y|n ]\n\t" \
415*d00756ccSwyllys  \
416*d00756ccSwyllys 	"genkey keystore=nss\n\t\t" \
417*d00756ccSwyllys 	"label=key-label\n\t\t" \
418*d00756ccSwyllys 	"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
419*d00756ccSwyllys 	"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
420*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
421*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
422*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t" \
423*d00756ccSwyllys  \
424*d00756ccSwyllys 	"genkey keystore=file\n\t\t" \
425*d00756ccSwyllys 	"outkey=key-fn\n\t\t" \
426*d00756ccSwyllys 	"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
427*d00756ccSwyllys 	"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
428*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
429*d00756ccSwyllys 	"[ print=y|n ]\n\t")
430*d00756ccSwyllys 
431*d00756ccSwyllys #define	SIGNCSR_IDX 10
432*d00756ccSwyllys #define	SIGNCSR_VERB gettext("signcsr")
433*d00756ccSwyllys #define	SIGNCSR_SUMM gettext("Sign a PKCS#10 Certificate Signing Request")
434*d00756ccSwyllys #define	SIGNCSR_SYN gettext(\
435*d00756ccSwyllys 	"signcsr keystore=pkcs11\n\t\t" \
436*d00756ccSwyllys 	"signkey=label (label of signing key)\n\t\t" \
437*d00756ccSwyllys 	"csr=CSR filename\n\t\t" \
438*d00756ccSwyllys 	"serial=serial number hex string\n\t\t" \
439*d00756ccSwyllys 	"outcert=filename for final certificate\n\t\t" \
440*d00756ccSwyllys 	"issuer=issuer-DN\n\t\t" \
441*d00756ccSwyllys 	"[ store=y|n ] (store the new cert in NSS DB, default=n)\n\t\t" \
442*d00756ccSwyllys 	"[ outlabel=certificate label ]\n\t\t" \
443*d00756ccSwyllys 	"[ format=pem|der ] (output format)\n\t\t" \
444*d00756ccSwyllys 	"[ subject=subject-DN ] (new subject name)\n\t\t" \
445*d00756ccSwyllys 	"[ altname=subjectAltName ]\n\t\t" \
446*d00756ccSwyllys 	"[ keyusage=[critical:]usage,...]\n\t\t" \
447*d00756ccSwyllys 	"[ eku=[critical:]EKU Name,...]\n\t\t" \
448*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
449*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t" \
450*d00756ccSwyllys  \
451*d00756ccSwyllys 	"signcsr keystore=file\n\t\t" \
452*d00756ccSwyllys 	"signkey=filename\n\t\t" \
453*d00756ccSwyllys 	"csr=CSR filename\n\t\t" \
454*d00756ccSwyllys 	"serial=serial number hex string\n\t\t" \
455*d00756ccSwyllys 	"outcert=filename for final certificate\n\t\t" \
456*d00756ccSwyllys 	"issuer=issuer-DN\n\t\t" \
457*d00756ccSwyllys 	"[ format=pem|der ] (output format)\n\t\t" \
458*d00756ccSwyllys 	"[ subject=subject-DN ] (new subject name)\n\t\t" \
459*d00756ccSwyllys 	"[ altname=subjectAltName ]\n\t\t" \
460*d00756ccSwyllys 	"[ keyusage=[critical:]usage,...]\n\t\t" \
461*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
462*d00756ccSwyllys 	"[ eku=[critical:]EKU Name,...]\n\t" \
463*d00756ccSwyllys  \
464*d00756ccSwyllys 	"signcsr keystore=nss\n\t\t" \
465*d00756ccSwyllys 	"signkey=label (label of signing key)\n\t\t" \
466*d00756ccSwyllys 	"csr=CSR filename\n\t\t" \
467*d00756ccSwyllys 	"serial=serial number hex string\n\t\t" \
468*d00756ccSwyllys 	"outcert=filename for final certificate\n\t\t" \
469*d00756ccSwyllys 	"issuer=issuer-DN\n\t\t" \
470*d00756ccSwyllys 	"[ store=y|n ] (store the new cert in NSS DB, default=n)\n\t\t" \
471*d00756ccSwyllys 	"[ outlabel=certificate label ]\n\t\t" \
472*d00756ccSwyllys 	"[ format=pem|der ] (output format)\n\t\t" \
473*d00756ccSwyllys 	"[ subject=subject-DN ] (new subject name)\n\t\t" \
474*d00756ccSwyllys 	"[ altname=subjectAltName ]\n\t\t" \
475*d00756ccSwyllys 	"[ keyusage=[critical:]usage,...]\n\t\t" \
476*d00756ccSwyllys 	"[ eku=[critical:]EKU Name,...]\n\t\t" \
477*d00756ccSwyllys 	"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
478*d00756ccSwyllys 	"[ token=token[:manuf[:serial]]]\n\t\t" \
479*d00756ccSwyllys 	"[ dir=directory-path ]\n\t\t" \
480*d00756ccSwyllys 	"[ prefix=DBprefix ]\n\t")
481*d00756ccSwyllys 
482*d00756ccSwyllys #define	HELP_IDX 11
483*d00756ccSwyllys #define	HELP_VERB gettext("help")
484*d00756ccSwyllys #define	HELP_SUMM gettext("displays help message")
485*d00756ccSwyllys #define	HELP_SYN gettext("help\t(help and usage)")
486*d00756ccSwyllys 
4877c478bd9Sstevel@tonic-gate /* Command structure for verbs and their actions.  Do NOT i18n/l10n. */
4887c478bd9Sstevel@tonic-gate static verbcmd	cmds[] = {
489*d00756ccSwyllys 	{ NULL,	pk_tokens, 0, NULL, NULL},
490*d00756ccSwyllys 	{ NULL,	pk_setpin, 0, NULL, NULL},
491*d00756ccSwyllys 	{ NULL, pk_list, 0, NULL, NULL},
492*d00756ccSwyllys 	{ NULL, pk_delete, 0, NULL, NULL},
493*d00756ccSwyllys 	{ NULL,	pk_import, 0, NULL, NULL},
494*d00756ccSwyllys 	{ NULL,	pk_export, 0, NULL, NULL},
495*d00756ccSwyllys 	{ NULL,	pk_gencert, 0, NULL, NULL},
496*d00756ccSwyllys 	{ NULL,	pk_gencsr, 0, NULL, NULL},
497*d00756ccSwyllys 	{ NULL,	pk_download, 0, NULL, NULL},
498*d00756ccSwyllys 	{ NULL,	pk_genkey, 0, NULL, NULL},
499*d00756ccSwyllys 	{ NULL, pk_signcsr, 0, NULL, NULL},
500*d00756ccSwyllys 	{ NULL,	pk_help, 0, NULL, NULL}
5017c478bd9Sstevel@tonic-gate };
502985be8f1Swyllys 
5037c478bd9Sstevel@tonic-gate static int	num_cmds = sizeof (cmds) / sizeof (verbcmd);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate static char	*prog;
50699ebb4caSwyllys static void	usage(int);
5077c478bd9Sstevel@tonic-gate 
508*d00756ccSwyllys static void
509*d00756ccSwyllys init_command_list()
510*d00756ccSwyllys {
511*d00756ccSwyllys 	cmds[TOKEN_IDX].verb = TOKEN_VERB;
512*d00756ccSwyllys 	cmds[TOKEN_IDX].summary = TOKEN_SUMM;
513*d00756ccSwyllys 	cmds[TOKEN_IDX].synopsis = TOKEN_SYN;
514*d00756ccSwyllys 
515*d00756ccSwyllys 	cmds[SETPIN_IDX].verb = SETPIN_VERB;
516*d00756ccSwyllys 	cmds[SETPIN_IDX].summary = SETPIN_SUMM;
517*d00756ccSwyllys 	cmds[SETPIN_IDX].synopsis = SETPIN_SYN;
518*d00756ccSwyllys 
519*d00756ccSwyllys 	cmds[LIST_IDX].verb = LIST_VERB;
520*d00756ccSwyllys 	cmds[LIST_IDX].summary = LIST_SUMM;
521*d00756ccSwyllys 	cmds[LIST_IDX].synopsis = LIST_SYN;
522*d00756ccSwyllys 
523*d00756ccSwyllys 	cmds[DELETE_IDX].verb = DELETE_VERB;
524*d00756ccSwyllys 	cmds[DELETE_IDX].summary = DELETE_SUMM;
525*d00756ccSwyllys 	cmds[DELETE_IDX].synopsis = DELETE_SYN;
526*d00756ccSwyllys 
527*d00756ccSwyllys 	cmds[IMPORT_IDX].verb = IMPORT_VERB;
528*d00756ccSwyllys 	cmds[IMPORT_IDX].summary = IMPORT_SUMM;
529*d00756ccSwyllys 	cmds[IMPORT_IDX].synopsis = IMPORT_SYN;
530*d00756ccSwyllys 
531*d00756ccSwyllys 	cmds[EXPORT_IDX].verb = EXPORT_VERB;
532*d00756ccSwyllys 	cmds[EXPORT_IDX].summary = EXPORT_SUMM;
533*d00756ccSwyllys 	cmds[EXPORT_IDX].synopsis = EXPORT_SYN;
534*d00756ccSwyllys 
535*d00756ccSwyllys 	cmds[GENCERT_IDX].verb = GENCERT_VERB;
536*d00756ccSwyllys 	cmds[GENCERT_IDX].summary = GENCERT_SUMM;
537*d00756ccSwyllys 	cmds[GENCERT_IDX].synopsis = GENCERT_SYN;
538*d00756ccSwyllys 
539*d00756ccSwyllys 	cmds[GENCSR_IDX].verb = GENCSR_VERB;
540*d00756ccSwyllys 	cmds[GENCSR_IDX].summary = GENCSR_SUMM;
541*d00756ccSwyllys 	cmds[GENCSR_IDX].synopsis = GENCSR_SYN;
542*d00756ccSwyllys 
543*d00756ccSwyllys 	cmds[DOWNLOAD_IDX].verb = DOWNLOAD_VERB;
544*d00756ccSwyllys 	cmds[DOWNLOAD_IDX].summary = DOWNLOAD_SUMM;
545*d00756ccSwyllys 	cmds[DOWNLOAD_IDX].synopsis = DOWNLOAD_SYN;
546*d00756ccSwyllys 
547*d00756ccSwyllys 	cmds[GENKEY_IDX].verb = GENKEY_VERB;
548*d00756ccSwyllys 	cmds[GENKEY_IDX].summary = GENKEY_SUMM;
549*d00756ccSwyllys 	cmds[GENKEY_IDX].synopsis = GENKEY_SYN;
550*d00756ccSwyllys 
551*d00756ccSwyllys 	cmds[SIGNCSR_IDX].verb = SIGNCSR_VERB;
552*d00756ccSwyllys 	cmds[SIGNCSR_IDX].summary = SIGNCSR_SUMM;
553*d00756ccSwyllys 	cmds[SIGNCSR_IDX].synopsis = SIGNCSR_SYN;
554*d00756ccSwyllys 
555*d00756ccSwyllys 	cmds[HELP_IDX].verb = HELP_VERB;
556*d00756ccSwyllys 	cmds[HELP_IDX].summary = HELP_SUMM;
557*d00756ccSwyllys 	cmds[HELP_IDX].synopsis = HELP_SYN;
558*d00756ccSwyllys 
559*d00756ccSwyllys }
560*d00756ccSwyllys 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Usage information.  This function must be updated when new verbs or
5637c478bd9Sstevel@tonic-gate  * options are added.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static void
56699ebb4caSwyllys usage(int idx)
5677c478bd9Sstevel@tonic-gate {
5687711facfSdinak 	int	i;
5697711facfSdinak 
5707711facfSdinak 	/* Display this block only in command-line mode. */
5717711facfSdinak 	(void) fprintf(stdout, gettext("Usage:\n"));
572985be8f1Swyllys 	(void) fprintf(stdout, gettext("   %s -?\t(help and usage)\n"),
573985be8f1Swyllys 	    prog);
574985be8f1Swyllys 	(void) fprintf(stdout, gettext("   %s -f option_file\n"), prog);
575985be8f1Swyllys 	(void) fprintf(stdout, gettext("   %s subcommand [options...]\n"),
576985be8f1Swyllys 	    prog);
5777711facfSdinak 	(void) fprintf(stdout, gettext("where subcommands may be:\n"));
5787711facfSdinak 
5797711facfSdinak 	/* Display only those verbs that match the current tool mode. */
58099ebb4caSwyllys 	if (idx == -1) {
5817711facfSdinak 		for (i = 0; i < num_cmds; i++) {
5827711facfSdinak 			/* Do NOT i18n/l10n. */
583985be8f1Swyllys 			(void) fprintf(stdout, "   %-8s	- %s\n",
584985be8f1Swyllys 			    cmds[i].verb, cmds[i].summary);
5857711facfSdinak 		}
586985be8f1Swyllys 		(void) fprintf(stdout, gettext("\nFurther details on the "
587985be8f1Swyllys 		    "subcommands can be found by adding \'help\'.\n"
588985be8f1Swyllys 		    "Ex: pktool gencert help\n\n"));
58999ebb4caSwyllys 	} else {
59099ebb4caSwyllys 		(void) fprintf(stdout, "\t%s\n", cmds[idx].synopsis);
59199ebb4caSwyllys 	}
5927711facfSdinak }
5937711facfSdinak 
5947711facfSdinak /*
5957711facfSdinak  * Provide help, in the form of displaying the usage.
5967711facfSdinak  */
5977711facfSdinak static int
5987711facfSdinak pk_help(int argc, char *argv[])
5997711facfSdinak /* ARGSUSED */
6007711facfSdinak {
60199ebb4caSwyllys 	usage(-1);
60299ebb4caSwyllys 	return (0);
60399ebb4caSwyllys }
6047711facfSdinak 
60599ebb4caSwyllys /*
60699ebb4caSwyllys  * Process arguments from the argfile and create a new
60799ebb4caSwyllys  * argv/argc list to be processed later.
60899ebb4caSwyllys  */
60999ebb4caSwyllys static int
61099ebb4caSwyllys process_arg_file(char *argfile, char ***argv, int *argc)
61199ebb4caSwyllys {
61299ebb4caSwyllys 	FILE *fp;
61399ebb4caSwyllys 	char argline[2 * BUFSIZ]; /* 2048 bytes should be plenty */
61499ebb4caSwyllys 	char *p;
61599ebb4caSwyllys 	int nargs = 0;
61699ebb4caSwyllys 
61799ebb4caSwyllys 	if ((fp = fopen(argfile, "rF")) == NULL) {
61899ebb4caSwyllys 		(void) fprintf(stderr,
61999ebb4caSwyllys 		    gettext("Cannot read argfile %s: %s\n"),
62099ebb4caSwyllys 		    argfile, strerror(errno));
62199ebb4caSwyllys 		return (errno);
62299ebb4caSwyllys 	}
62399ebb4caSwyllys 
62499ebb4caSwyllys 	while (fgets(argline, sizeof (argline), fp) != NULL) {
62599ebb4caSwyllys 		int j;
62699ebb4caSwyllys 		/* remove trailing whitespace */
62799ebb4caSwyllys 		j = strlen(argline) - 1;
62899ebb4caSwyllys 		while (j >= 0 && isspace(argline[j])) {
62999ebb4caSwyllys 			argline[j] = 0;
63099ebb4caSwyllys 			j--;
63199ebb4caSwyllys 		}
63299ebb4caSwyllys 		/* If it was a blank line, get the next one. */
63399ebb4caSwyllys 		if (!strlen(argline))
63499ebb4caSwyllys 			continue;
63599ebb4caSwyllys 
63630a5e8faSwyllys 		(*argv) = realloc((*argv),
63730a5e8faSwyllys 		    (nargs + 1) * sizeof (char *));
63899ebb4caSwyllys 		if ((*argv) == NULL) {
63999ebb4caSwyllys 			perror("memory error");
64099ebb4caSwyllys 			(void) fclose(fp);
64199ebb4caSwyllys 			return (errno);
64299ebb4caSwyllys 		}
64399ebb4caSwyllys 		p = (char *)strdup(argline);
64499ebb4caSwyllys 		if (p == NULL) {
64599ebb4caSwyllys 			perror("memory error");
64699ebb4caSwyllys 			(void) fclose(fp);
64799ebb4caSwyllys 			return (errno);
64899ebb4caSwyllys 		}
64999ebb4caSwyllys 		(*argv)[nargs] = p;
65099ebb4caSwyllys 		nargs++;
65199ebb4caSwyllys 	}
65299ebb4caSwyllys 	*argc = nargs;
65399ebb4caSwyllys 	(void) fclose(fp);
6547711facfSdinak 	return (0);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * MAIN() -- where all the action is
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate int
6617c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
6627c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	int	i, found = -1;
6657c478bd9Sstevel@tonic-gate 	int	rv;
6667c478bd9Sstevel@tonic-gate 	int	pk_argc = 0;
6677c478bd9Sstevel@tonic-gate 	char	**pk_argv = NULL;
6687711facfSdinak 	int	save_errno = 0;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* Set up for i18n/l10n. */
6717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6727c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
6737c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
6747c478bd9Sstevel@tonic-gate #endif
6757c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6767c478bd9Sstevel@tonic-gate 
677*d00756ccSwyllys 	init_command_list();
678*d00756ccSwyllys 
6797c478bd9Sstevel@tonic-gate 	/* Get program base name and move pointer over 0th arg. */
6807c478bd9Sstevel@tonic-gate 	prog = basename(argv[0]);
6817c478bd9Sstevel@tonic-gate 	argv++, argc--;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* Set up for debug and error output. */
6847c478bd9Sstevel@tonic-gate 	if (argc == 0) {
68599ebb4caSwyllys 		usage(-1);
6867c478bd9Sstevel@tonic-gate 		return (1);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897711facfSdinak 	/* Check for help options.  For CLIP-compliance. */
69099ebb4caSwyllys 	if (strcmp(argv[0], "-?") == 0) {
6917711facfSdinak 		return (pk_help(argc, argv));
69299ebb4caSwyllys 	} else if (strcmp(argv[0], "-f") == 0 && argc == 2) {
69399ebb4caSwyllys 		rv = process_arg_file(argv[1], &pk_argv, &pk_argc);
69499ebb4caSwyllys 		if (rv)
69599ebb4caSwyllys 			return (rv);
69699ebb4caSwyllys 	} else if (argc >= 1 && argv[0][0] == '-') {
69799ebb4caSwyllys 		usage(-1);
6987711facfSdinak 		return (1);
6997711facfSdinak 	}
7007711facfSdinak 
7017711facfSdinak 	/* Always turns off Metaslot so that we can see softtoken. */
7027c478bd9Sstevel@tonic-gate 	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
7037711facfSdinak 		save_errno = errno;
7047c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
7057711facfSdinak 		    gettext("Disabling Metaslot failed (%s)."),
7067711facfSdinak 		    strerror(save_errno));
7077c478bd9Sstevel@tonic-gate 		return (1);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/* Begin parsing command line. */
71199ebb4caSwyllys 	if (pk_argc == 0 && pk_argv == NULL) {
7127c478bd9Sstevel@tonic-gate 		pk_argc = argc;
7137c478bd9Sstevel@tonic-gate 		pk_argv = argv;
71499ebb4caSwyllys 	}
7157c478bd9Sstevel@tonic-gate 
7167711facfSdinak 	/* Check for valid verb (or an abbreviation of it). */
7177c478bd9Sstevel@tonic-gate 	found = -1;
7187c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_cmds; i++) {
7197c478bd9Sstevel@tonic-gate 		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
7207c478bd9Sstevel@tonic-gate 			if (found < 0) {
7217c478bd9Sstevel@tonic-gate 				found = i;
7227c478bd9Sstevel@tonic-gate 				break;
7237c478bd9Sstevel@tonic-gate 			}
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	/* Stop here if no valid verb found. */
7277c478bd9Sstevel@tonic-gate 	if (found < 0) {
7287711facfSdinak 		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
7297711facfSdinak 		    pk_argv[0]);
7307c478bd9Sstevel@tonic-gate 		return (1);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/* Get to work! */
7347c478bd9Sstevel@tonic-gate 	rv = (*cmds[found].action)(pk_argc, pk_argv);
7357c478bd9Sstevel@tonic-gate 	switch (rv) {
7367c478bd9Sstevel@tonic-gate 	case PK_ERR_NONE:
7377c478bd9Sstevel@tonic-gate 		break;		/* Command succeeded, do nothing. */
7387c478bd9Sstevel@tonic-gate 	case PK_ERR_USAGE:
73999ebb4caSwyllys 		usage(found);
7407c478bd9Sstevel@tonic-gate 		break;
7417c478bd9Sstevel@tonic-gate 	case PK_ERR_QUIT:
7427c478bd9Sstevel@tonic-gate 		exit(0);
7437c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7447711facfSdinak 	case PK_ERR_PK11:
7457711facfSdinak 	case PK_ERR_SYSTEM:
7467711facfSdinak 	case PK_ERR_OPENSSL:
74799ebb4caSwyllys 	case PK_ERR_NSS:
7487c478bd9Sstevel@tonic-gate 	default:
7497c478bd9Sstevel@tonic-gate 		break;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	return (rv);
7527c478bd9Sstevel@tonic-gate }
753