xref: /onnv-gate/usr/src/cmd/cmd-crypto/pktool/tokens.c (revision 3089:8ddeb2ace8aa)
117Sdinak /*
217Sdinak  * CDDL HEADER START
317Sdinak  *
417Sdinak  * The contents of this file are subject to the terms of the
5*3089Swyllys  * Common Development and Distribution License (the "License").
6*3089Swyllys  * You may not use this file except in compliance with the License.
717Sdinak  *
817Sdinak  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
917Sdinak  * or http://www.opensolaris.org/os/licensing.
1017Sdinak  * See the License for the specific language governing permissions
1117Sdinak  * and limitations under the License.
1217Sdinak  *
1317Sdinak  * When distributing Covered Code, include this CDDL HEADER in each
1417Sdinak  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1517Sdinak  * If applicable, add the following below this CDDL HEADER, with the
1617Sdinak  * fields enclosed by brackets "[]" replaced with your own identifying
1717Sdinak  * information: Portions Copyright [yyyy] [name of copyright owner]
1817Sdinak  *
1917Sdinak  * CDDL HEADER END
2017Sdinak  */
2117Sdinak /*
22*3089Swyllys  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2317Sdinak  * Use is subject to license terms.
2417Sdinak  */
2517Sdinak 
2617Sdinak #pragma ident	"%Z%%M%	%I%	%E% SMI"
2717Sdinak 
2817Sdinak /*
2917Sdinak  * This file implements the token list operation for this tool.
3017Sdinak  * It loads the PKCS#11 modules, gets the list of slots with
3117Sdinak  * tokens in them, displays the list, and cleans up.
3217Sdinak  */
3317Sdinak 
3417Sdinak #include <stdio.h>
3517Sdinak #include <string.h>
3617Sdinak #include <cryptoutil.h>
3717Sdinak #include <security/cryptoki.h>
3817Sdinak #include "common.h"
3917Sdinak 
4017Sdinak /*
4117Sdinak  * Lists all slots with tokens in them.
4217Sdinak  */
4317Sdinak int
pk_tokens(int argc,char * argv[])4417Sdinak pk_tokens(int argc, char *argv[])
4517Sdinak {
4617Sdinak 	CK_SLOT_ID_PTR	slots = NULL;
4717Sdinak 	CK_ULONG	slot_count = 0;
4817Sdinak 	CK_TOKEN_INFO	token_info;
4917Sdinak 	const char	*fmt = NULL;
5017Sdinak 	CK_RV		rv = CKR_OK;
5117Sdinak 	int		i;
5217Sdinak 
5317Sdinak 
5417Sdinak 	/* Get rid of subcommand word "tokens". */
5517Sdinak 	argc--;
5617Sdinak 	argv++;
5717Sdinak 
5817Sdinak 	/* No additional args allowed. */
5917Sdinak 	if (argc != 0)
6017Sdinak 		return (PK_ERR_USAGE);
6117Sdinak 	/* Done parsing command line options. */
6217Sdinak 
6317Sdinak 	/* Get the list of slots with tokens in them. */
6417Sdinak 	if ((rv = get_token_slots(&slots, &slot_count)) != CKR_OK) {
6517Sdinak 		cryptoerror(LOG_STDERR,
6617Sdinak 		    gettext("Unable to get token slot list (%s)."),
6717Sdinak 		    pkcs11_strerror(rv));
6817Sdinak 		return (PK_ERR_PK11);
6917Sdinak 	}
7017Sdinak 
7117Sdinak 	/* Make sure we have something to display. */
7217Sdinak 	if (slot_count == 0) {
7317Sdinak 		cryptoerror(LOG_STDERR, gettext("No slots with tokens found."));
7417Sdinak 		return (0);
7517Sdinak 	}
7617Sdinak 
7717Sdinak 	/* Display the list. */
7817Sdinak 	fmt = "%-30.30s  %-15.15s  %-15.15s  %-10.10s\n"; /* No I18N/L10N. */
7917Sdinak 	(void) fprintf(stdout, fmt, gettext("Token Label"), gettext("Manuf ID"),
8017Sdinak 	    gettext("Serial No"), gettext("PIN State"));
8117Sdinak 	for (i = 0; i < slot_count; i++) {
8217Sdinak 		if ((rv = C_GetTokenInfo(slots[i], &token_info)) != CKR_OK) {
8317Sdinak 			cryptoerror(LOG_STDERR,
8417Sdinak 			    gettext("Unable to get slot %d token info (%s)."),
8517Sdinak 			    i, pkcs11_strerror(rv));
8617Sdinak 			continue;
8717Sdinak 		}
8817Sdinak 
8917Sdinak 		(void) fprintf(stdout, fmt, token_info.label,
9017Sdinak 		    token_info.manufacturerID, token_info.serialNumber,
9117Sdinak 		    (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) ?
9217Sdinak 		    gettext("default") : gettext("user set"));
9317Sdinak 	}
9417Sdinak 
9517Sdinak 	/* Clean up. */
9617Sdinak 	free(slots);
97*3089Swyllys 	(void) C_Finalize(NULL);
9817Sdinak 	return (0);
9917Sdinak }
100