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