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