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 /*
27  * This file implements the token list operation for this tool.
28  * It loads the PKCS#11 modules, gets the list of slots with
29  * tokens in them, displays the list, and cleans up.
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <cryptoutil.h>
35 #include <security/cryptoki.h>
36 #include "common.h"
37 
38 /*
39  * Lists all slots with tokens in them.
40  */
41 int
42 pk_tokens(int argc, char *argv[])
43 {
44 	CK_SLOT_ID_PTR	slots = NULL;
45 	CK_ULONG	slot_count = 0;
46 	CK_TOKEN_INFO	token_info;
47 	const char	*fmt = NULL;
48 	CK_RV		rv = CKR_OK;
49 	int		i;
50 
51 
52 	/* Get rid of subcommand word "tokens". */
53 	argc--;
54 	argv++;
55 
56 	/* No additional args allowed. */
57 	if (argc != 0)
58 		return (PK_ERR_USAGE);
59 	/* Done parsing command line options. */
60 
61 	/* Get the list of slots with tokens in them. */
62 	if ((rv = get_token_slots(&slots, &slot_count)) != CKR_OK) {
63 		cryptoerror(LOG_STDERR,
64 		    gettext("Unable to get token slot list (%s)."),
65 		    pkcs11_strerror(rv));
66 		return (PK_ERR_PK11);
67 	}
68 
69 	/* Make sure we have something to display. */
70 	if (slot_count == 0) {
71 		cryptoerror(LOG_STDERR, gettext("No slots with tokens found."));
72 		return (0);
73 	}
74 
75 	/* Display the list. */
76 	fmt = "%-30.30s  %-15.15s  %-15.15s  %-10.10s\n"; /* No I18N/L10N. */
77 	(void) fprintf(stdout, fmt, gettext("Token Label"), gettext("Manuf ID"),
78 	    gettext("Serial No"), gettext("PIN State"));
79 	for (i = 0; i < slot_count; i++) {
80 		if ((rv = C_GetTokenInfo(slots[i], &token_info)) != CKR_OK) {
81 			cryptoerror(LOG_STDERR,
82 			    gettext("Unable to get slot %d token info (%s)."),
83 			    i, pkcs11_strerror(rv));
84 			continue;
85 		}
86 
87 		(void) fprintf(stdout, fmt, token_info.label,
88 		    token_info.manufacturerID, token_info.serialNumber,
89 		    (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) ?
90 		    gettext("default") : gettext("user set"));
91 	}
92 
93 	/* Clean up. */
94 	free(slots);
95 	(void) C_Finalize(NULL);
96 	return (0);
97 }
98