xref: /titanic_44/usr/src/cmd/cmd-crypto/pktool/tokens.c (revision 99ebb4ca412cb0a19d77a3899a87c055b9c30fa8)
17711facfSdinak /*
27711facfSdinak  * CDDL HEADER START
37711facfSdinak  *
47711facfSdinak  * The contents of this file are subject to the terms of the
5*99ebb4caSwyllys  * Common Development and Distribution License (the "License").
6*99ebb4caSwyllys  * You may not use this file except in compliance with the License.
77711facfSdinak  *
87711facfSdinak  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97711facfSdinak  * or http://www.opensolaris.org/os/licensing.
107711facfSdinak  * See the License for the specific language governing permissions
117711facfSdinak  * and limitations under the License.
127711facfSdinak  *
137711facfSdinak  * When distributing Covered Code, include this CDDL HEADER in each
147711facfSdinak  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157711facfSdinak  * If applicable, add the following below this CDDL HEADER, with the
167711facfSdinak  * fields enclosed by brackets "[]" replaced with your own identifying
177711facfSdinak  * information: Portions Copyright [yyyy] [name of copyright owner]
187711facfSdinak  *
197711facfSdinak  * CDDL HEADER END
207711facfSdinak  */
217711facfSdinak /*
22*99ebb4caSwyllys  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237711facfSdinak  * Use is subject to license terms.
247711facfSdinak  */
257711facfSdinak 
267711facfSdinak #pragma ident	"%Z%%M%	%I%	%E% SMI"
277711facfSdinak 
287711facfSdinak /*
297711facfSdinak  * This file implements the token list operation for this tool.
307711facfSdinak  * It loads the PKCS#11 modules, gets the list of slots with
317711facfSdinak  * tokens in them, displays the list, and cleans up.
327711facfSdinak  */
337711facfSdinak 
347711facfSdinak #include <stdio.h>
357711facfSdinak #include <string.h>
367711facfSdinak #include <cryptoutil.h>
377711facfSdinak #include <security/cryptoki.h>
387711facfSdinak #include "common.h"
397711facfSdinak 
407711facfSdinak /*
417711facfSdinak  * Lists all slots with tokens in them.
427711facfSdinak  */
437711facfSdinak int
pk_tokens(int argc,char * argv[])447711facfSdinak pk_tokens(int argc, char *argv[])
457711facfSdinak {
467711facfSdinak 	CK_SLOT_ID_PTR	slots = NULL;
477711facfSdinak 	CK_ULONG	slot_count = 0;
487711facfSdinak 	CK_TOKEN_INFO	token_info;
497711facfSdinak 	const char	*fmt = NULL;
507711facfSdinak 	CK_RV		rv = CKR_OK;
517711facfSdinak 	int		i;
527711facfSdinak 
537711facfSdinak 
547711facfSdinak 	/* Get rid of subcommand word "tokens". */
557711facfSdinak 	argc--;
567711facfSdinak 	argv++;
577711facfSdinak 
587711facfSdinak 	/* No additional args allowed. */
597711facfSdinak 	if (argc != 0)
607711facfSdinak 		return (PK_ERR_USAGE);
617711facfSdinak 	/* Done parsing command line options. */
627711facfSdinak 
637711facfSdinak 	/* Get the list of slots with tokens in them. */
647711facfSdinak 	if ((rv = get_token_slots(&slots, &slot_count)) != CKR_OK) {
657711facfSdinak 		cryptoerror(LOG_STDERR,
667711facfSdinak 		    gettext("Unable to get token slot list (%s)."),
677711facfSdinak 		    pkcs11_strerror(rv));
687711facfSdinak 		return (PK_ERR_PK11);
697711facfSdinak 	}
707711facfSdinak 
717711facfSdinak 	/* Make sure we have something to display. */
727711facfSdinak 	if (slot_count == 0) {
737711facfSdinak 		cryptoerror(LOG_STDERR, gettext("No slots with tokens found."));
747711facfSdinak 		return (0);
757711facfSdinak 	}
767711facfSdinak 
777711facfSdinak 	/* Display the list. */
787711facfSdinak 	fmt = "%-30.30s  %-15.15s  %-15.15s  %-10.10s\n"; /* No I18N/L10N. */
797711facfSdinak 	(void) fprintf(stdout, fmt, gettext("Token Label"), gettext("Manuf ID"),
807711facfSdinak 	    gettext("Serial No"), gettext("PIN State"));
817711facfSdinak 	for (i = 0; i < slot_count; i++) {
827711facfSdinak 		if ((rv = C_GetTokenInfo(slots[i], &token_info)) != CKR_OK) {
837711facfSdinak 			cryptoerror(LOG_STDERR,
847711facfSdinak 			    gettext("Unable to get slot %d token info (%s)."),
857711facfSdinak 			    i, pkcs11_strerror(rv));
867711facfSdinak 			continue;
877711facfSdinak 		}
887711facfSdinak 
897711facfSdinak 		(void) fprintf(stdout, fmt, token_info.label,
907711facfSdinak 		    token_info.manufacturerID, token_info.serialNumber,
917711facfSdinak 		    (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) ?
927711facfSdinak 		    gettext("default") : gettext("user set"));
937711facfSdinak 	}
947711facfSdinak 
957711facfSdinak 	/* Clean up. */
967711facfSdinak 	free(slots);
97*99ebb4caSwyllys 	(void) C_Finalize(NULL);
987711facfSdinak 	return (0);
997711facfSdinak }
100