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