1*7711facfSdinak /*
2*7711facfSdinak  * CDDL HEADER START
3*7711facfSdinak  *
4*7711facfSdinak  * The contents of this file are subject to the terms of the
5*7711facfSdinak  * Common Development and Distribution License, Version 1.0 only
6*7711facfSdinak  * (the "License").  You may not use this file except in compliance
7*7711facfSdinak  * with the License.
8*7711facfSdinak  *
9*7711facfSdinak  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7711facfSdinak  * or http://www.opensolaris.org/os/licensing.
11*7711facfSdinak  * See the License for the specific language governing permissions
12*7711facfSdinak  * and limitations under the License.
13*7711facfSdinak  *
14*7711facfSdinak  * When distributing Covered Code, include this CDDL HEADER in each
15*7711facfSdinak  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7711facfSdinak  * If applicable, add the following below this CDDL HEADER, with the
17*7711facfSdinak  * fields enclosed by brackets "[]" replaced with your own identifying
18*7711facfSdinak  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7711facfSdinak  *
20*7711facfSdinak  * CDDL HEADER END
21*7711facfSdinak  */
22*7711facfSdinak /*
23*7711facfSdinak  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7711facfSdinak  * Use is subject to license terms.
25*7711facfSdinak  */
26*7711facfSdinak 
27*7711facfSdinak #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7711facfSdinak 
29*7711facfSdinak /*
30*7711facfSdinak  * This file implements the token list operation for this tool.
31*7711facfSdinak  * It loads the PKCS#11 modules, gets the list of slots with
32*7711facfSdinak  * tokens in them, displays the list, and cleans up.
33*7711facfSdinak  */
34*7711facfSdinak 
35*7711facfSdinak #include <stdio.h>
36*7711facfSdinak #include <string.h>
37*7711facfSdinak #include <cryptoutil.h>
38*7711facfSdinak #include <security/cryptoki.h>
39*7711facfSdinak #include "common.h"
40*7711facfSdinak 
41*7711facfSdinak /*
42*7711facfSdinak  * Lists all slots with tokens in them.
43*7711facfSdinak  */
44*7711facfSdinak int
45*7711facfSdinak pk_tokens(int argc, char *argv[])
46*7711facfSdinak {
47*7711facfSdinak 	CK_SLOT_ID_PTR	slots = NULL;
48*7711facfSdinak 	CK_ULONG	slot_count = 0;
49*7711facfSdinak 	CK_TOKEN_INFO	token_info;
50*7711facfSdinak 	const char	*fmt = NULL;
51*7711facfSdinak 	CK_RV		rv = CKR_OK;
52*7711facfSdinak 	int		i;
53*7711facfSdinak 
54*7711facfSdinak 	cryptodebug("inside pk_tokens");
55*7711facfSdinak 
56*7711facfSdinak 	/* Get rid of subcommand word "tokens". */
57*7711facfSdinak 	argc--;
58*7711facfSdinak 	argv++;
59*7711facfSdinak 
60*7711facfSdinak 	/* No additional args allowed. */
61*7711facfSdinak 	if (argc != 0)
62*7711facfSdinak 		return (PK_ERR_USAGE);
63*7711facfSdinak 	/* Done parsing command line options. */
64*7711facfSdinak 
65*7711facfSdinak 	/* Get the list of slots with tokens in them. */
66*7711facfSdinak 	if ((rv = get_token_slots(&slots, &slot_count)) != CKR_OK) {
67*7711facfSdinak 		cryptoerror(LOG_STDERR,
68*7711facfSdinak 		    gettext("Unable to get token slot list (%s)."),
69*7711facfSdinak 		    pkcs11_strerror(rv));
70*7711facfSdinak 		return (PK_ERR_PK11);
71*7711facfSdinak 	}
72*7711facfSdinak 
73*7711facfSdinak 	/* Make sure we have something to display. */
74*7711facfSdinak 	if (slot_count == 0) {
75*7711facfSdinak 		cryptoerror(LOG_STDERR, gettext("No slots with tokens found."));
76*7711facfSdinak 		return (0);
77*7711facfSdinak 	}
78*7711facfSdinak 
79*7711facfSdinak 	/* Display the list. */
80*7711facfSdinak 	fmt = "%-30.30s  %-15.15s  %-15.15s  %-10.10s\n"; /* No I18N/L10N. */
81*7711facfSdinak 	(void) fprintf(stdout, fmt, gettext("Token Label"), gettext("Manuf ID"),
82*7711facfSdinak 	    gettext("Serial No"), gettext("PIN State"));
83*7711facfSdinak 	for (i = 0; i < slot_count; i++) {
84*7711facfSdinak 		cryptodebug("calling C_GetTokenInfo");
85*7711facfSdinak 		if ((rv = C_GetTokenInfo(slots[i], &token_info)) != CKR_OK) {
86*7711facfSdinak 			cryptoerror(LOG_STDERR,
87*7711facfSdinak 			    gettext("Unable to get slot %d token info (%s)."),
88*7711facfSdinak 			    i, pkcs11_strerror(rv));
89*7711facfSdinak 			cryptodebug("token info error, slot %d (%s)", i,
90*7711facfSdinak 				pkcs11_strerror(rv));
91*7711facfSdinak 			continue;
92*7711facfSdinak 		}
93*7711facfSdinak 
94*7711facfSdinak 		(void) fprintf(stdout, fmt, token_info.label,
95*7711facfSdinak 		    token_info.manufacturerID, token_info.serialNumber,
96*7711facfSdinak 		    (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) ?
97*7711facfSdinak 		    gettext("default") : gettext("user set"));
98*7711facfSdinak 	}
99*7711facfSdinak 
100*7711facfSdinak 	/* Clean up. */
101*7711facfSdinak 	free(slots);
102*7711facfSdinak 	quick_finish(NULL);
103*7711facfSdinak 	return (0);
104*7711facfSdinak }
105