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 /*
277711facfSdinak  * This file implements the token list operation for this tool.
287711facfSdinak  * It loads the PKCS#11 modules, gets the list of slots with
297711facfSdinak  * tokens in them, displays the list, and cleans up.
307711facfSdinak  */
317711facfSdinak 
327711facfSdinak #include <stdio.h>
337711facfSdinak #include <string.h>
347711facfSdinak #include <cryptoutil.h>
357711facfSdinak #include <security/cryptoki.h>
367711facfSdinak #include "common.h"
377711facfSdinak 
387711facfSdinak /*
397711facfSdinak  * Lists all slots with tokens in them.
407711facfSdinak  */
417711facfSdinak int
pk_tokens(int argc,char * argv[])427711facfSdinak pk_tokens(int argc, char *argv[])
437711facfSdinak {
447711facfSdinak 	CK_SLOT_ID_PTR	slots = NULL;
457711facfSdinak 	CK_ULONG	slot_count = 0;
467711facfSdinak 	CK_TOKEN_INFO	token_info;
477711facfSdinak 	const char	*fmt = NULL;
487711facfSdinak 	CK_RV		rv = CKR_OK;
497711facfSdinak 	int		i;
507711facfSdinak 
517711facfSdinak 
527711facfSdinak 	/* Get rid of subcommand word "tokens". */
537711facfSdinak 	argc--;
547711facfSdinak 	argv++;
557711facfSdinak 
567711facfSdinak 	/* No additional args allowed. */
577711facfSdinak 	if (argc != 0)
587711facfSdinak 		return (PK_ERR_USAGE);
597711facfSdinak 	/* Done parsing command line options. */
607711facfSdinak 
617711facfSdinak 	/* Get the list of slots with tokens in them. */
627711facfSdinak 	if ((rv = get_token_slots(&slots, &slot_count)) != CKR_OK) {
637711facfSdinak 		cryptoerror(LOG_STDERR,
647711facfSdinak 		    gettext("Unable to get token slot list (%s)."),
657711facfSdinak 		    pkcs11_strerror(rv));
667711facfSdinak 		return (PK_ERR_PK11);
677711facfSdinak 	}
687711facfSdinak 
697711facfSdinak 	/* Make sure we have something to display. */
707711facfSdinak 	if (slot_count == 0) {
717711facfSdinak 		cryptoerror(LOG_STDERR, gettext("No slots with tokens found."));
727711facfSdinak 		return (0);
737711facfSdinak 	}
747711facfSdinak 
757711facfSdinak 	/* Display the list. */
767711facfSdinak 	fmt = "%-30.30s  %-15.15s  %-15.15s  %-10.10s\n"; /* No I18N/L10N. */
777711facfSdinak 	(void) fprintf(stdout, fmt, gettext("Token Label"), gettext("Manuf ID"),
787711facfSdinak 	    gettext("Serial No"), gettext("PIN State"));
797711facfSdinak 	for (i = 0; i < slot_count; i++) {
807711facfSdinak 		if ((rv = C_GetTokenInfo(slots[i], &token_info)) != CKR_OK) {
817711facfSdinak 			cryptoerror(LOG_STDERR,
827711facfSdinak 			    gettext("Unable to get slot %d token info (%s)."),
837711facfSdinak 			    i, pkcs11_strerror(rv));
847711facfSdinak 			continue;
857711facfSdinak 		}
867711facfSdinak 
877711facfSdinak 		(void) fprintf(stdout, fmt, token_info.label,
887711facfSdinak 		    token_info.manufacturerID, token_info.serialNumber,
897711facfSdinak 		    (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) ?
907711facfSdinak 		    gettext("default") : gettext("user set"));
917711facfSdinak 	}
927711facfSdinak 
937711facfSdinak 	/* Clean up. */
947711facfSdinak 	free(slots);
95*99ebb4caSwyllys 	(void) C_Finalize(NULL);
967711facfSdinak 	return (0);
977711facfSdinak }
98