147e946e7SWyllys Ingersoll /* 247e946e7SWyllys Ingersoll * CDDL HEADER START 347e946e7SWyllys Ingersoll * 447e946e7SWyllys Ingersoll * The contents of this file are subject to the terms of the 547e946e7SWyllys Ingersoll * Common Development and Distribution License (the "License"). 647e946e7SWyllys Ingersoll * You may not use this file except in compliance with the License. 747e946e7SWyllys Ingersoll * 847e946e7SWyllys Ingersoll * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 947e946e7SWyllys Ingersoll * or http://www.opensolaris.org/os/licensing. 1047e946e7SWyllys Ingersoll * See the License for the specific language governing permissions 1147e946e7SWyllys Ingersoll * and limitations under the License. 1247e946e7SWyllys Ingersoll * 1347e946e7SWyllys Ingersoll * When distributing Covered Code, include this CDDL HEADER in each 1447e946e7SWyllys Ingersoll * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1547e946e7SWyllys Ingersoll * If applicable, add the following below this CDDL HEADER, with the 1647e946e7SWyllys Ingersoll * fields enclosed by brackets "[]" replaced with your own identifying 1747e946e7SWyllys Ingersoll * information: Portions Copyright [yyyy] [name of copyright owner] 1847e946e7SWyllys Ingersoll * 1947e946e7SWyllys Ingersoll * CDDL HEADER END 2047e946e7SWyllys Ingersoll */ 2147e946e7SWyllys Ingersoll /* 2247e946e7SWyllys Ingersoll * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2347e946e7SWyllys Ingersoll * Use is subject to license terms. 24*33f5ff17SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved. 2547e946e7SWyllys Ingersoll */ 2647e946e7SWyllys Ingersoll 2747e946e7SWyllys Ingersoll /* 2847e946e7SWyllys Ingersoll * This file implements the inittoken operation for this tool. 2947e946e7SWyllys Ingersoll * The basic flow of the process is to load the PKCS#11 module, 3047e946e7SWyllys Ingersoll * find the token to be initialize , login using the SO pin, 3147e946e7SWyllys Ingersoll * and call C_InitToken. 3247e946e7SWyllys Ingersoll */ 3347e946e7SWyllys Ingersoll 3447e946e7SWyllys Ingersoll #include <stdio.h> 3547e946e7SWyllys Ingersoll #include <stdlib.h> 3647e946e7SWyllys Ingersoll #include <errno.h> 3747e946e7SWyllys Ingersoll #include <string.h> 3847e946e7SWyllys Ingersoll #include <cryptoutil.h> 3947e946e7SWyllys Ingersoll #include <security/cryptoki.h> 4047e946e7SWyllys Ingersoll #include "common.h" 4147e946e7SWyllys Ingersoll 4247e946e7SWyllys Ingersoll int 4347e946e7SWyllys Ingersoll pk_inittoken(int argc, char *argv[]) 4447e946e7SWyllys Ingersoll /* ARGSUSED */ 4547e946e7SWyllys Ingersoll { 4647e946e7SWyllys Ingersoll int opt; 4747e946e7SWyllys Ingersoll int rv; 4847e946e7SWyllys Ingersoll extern int optind_av; 4947e946e7SWyllys Ingersoll extern char *optarg_av; 5047e946e7SWyllys Ingersoll char *newlabel = NULL; 5147e946e7SWyllys Ingersoll char *currlabel = NULL; 5247e946e7SWyllys Ingersoll CK_UTF8CHAR_PTR sopin; 5347e946e7SWyllys Ingersoll CK_ULONG sopinlen; 5447e946e7SWyllys Ingersoll KMF_HANDLE_T handle; 5547e946e7SWyllys Ingersoll 5647e946e7SWyllys Ingersoll /* Parse command line options. Do NOT i18n/l10n. */ 5747e946e7SWyllys Ingersoll while ((opt = getopt_av(argc, argv, 5847e946e7SWyllys Ingersoll "n:(newlabel)" 5947e946e7SWyllys Ingersoll "l:(currlabel)")) != EOF) { 6047e946e7SWyllys Ingersoll switch (opt) { 6147e946e7SWyllys Ingersoll case 'l': /* token specifier */ 6247e946e7SWyllys Ingersoll if (currlabel) 6347e946e7SWyllys Ingersoll return (PK_ERR_USAGE); 6447e946e7SWyllys Ingersoll currlabel = optarg_av; 6547e946e7SWyllys Ingersoll break; 6647e946e7SWyllys Ingersoll case 'n': /* token specifier */ 6747e946e7SWyllys Ingersoll if (newlabel) 6847e946e7SWyllys Ingersoll return (PK_ERR_USAGE); 6947e946e7SWyllys Ingersoll newlabel = optarg_av; 7047e946e7SWyllys Ingersoll break; 7147e946e7SWyllys Ingersoll default: 7247e946e7SWyllys Ingersoll return (PK_ERR_USAGE); 7347e946e7SWyllys Ingersoll } 7447e946e7SWyllys Ingersoll } 7547e946e7SWyllys Ingersoll 7647e946e7SWyllys Ingersoll /* No additional args allowed. */ 7747e946e7SWyllys Ingersoll argc -= optind_av; 7847e946e7SWyllys Ingersoll argv += optind_av; 7947e946e7SWyllys Ingersoll if (argc != 0) 8047e946e7SWyllys Ingersoll return (PK_ERR_USAGE); 8147e946e7SWyllys Ingersoll 8247e946e7SWyllys Ingersoll if ((rv = kmf_initialize(&handle, NULL, NULL)) != KMF_OK) 8347e946e7SWyllys Ingersoll return (rv); 8447e946e7SWyllys Ingersoll 8547e946e7SWyllys Ingersoll if ((rv = get_pin(gettext("Enter SO PIN:"), NULL, &sopin, &sopinlen)) 8647e946e7SWyllys Ingersoll != CKR_OK) { 8747e946e7SWyllys Ingersoll cryptoerror(LOG_STDERR, 8847e946e7SWyllys Ingersoll gettext("Unable to get SO PIN for token")); 8947e946e7SWyllys Ingersoll return (PK_ERR_SYSTEM); 9047e946e7SWyllys Ingersoll } 9147e946e7SWyllys Ingersoll if ((currlabel == NULL || !strlen(currlabel))) { 9247e946e7SWyllys Ingersoll cryptoerror(LOG_STDERR, 9347e946e7SWyllys Ingersoll gettext("The current token is not identified by label.")); 9447e946e7SWyllys Ingersoll return (PK_ERR_SYSTEM); 9547e946e7SWyllys Ingersoll } 9647e946e7SWyllys Ingersoll 9747e946e7SWyllys Ingersoll rv = kmf_pk11_init_token(handle, currlabel, newlabel, 9847e946e7SWyllys Ingersoll sopin, sopinlen); 9947e946e7SWyllys Ingersoll 10047e946e7SWyllys Ingersoll (void) kmf_finalize(handle); 10147e946e7SWyllys Ingersoll 10247e946e7SWyllys Ingersoll free(sopin); 10347e946e7SWyllys Ingersoll 10447e946e7SWyllys Ingersoll if (rv == KMF_ERR_AUTH_FAILED) { 10547e946e7SWyllys Ingersoll cryptoerror(LOG_STDERR, 10647e946e7SWyllys Ingersoll gettext("Incorrect passphrase.")); 10747e946e7SWyllys Ingersoll return (PK_ERR_SYSTEM); 10847e946e7SWyllys Ingersoll } else if (rv != CKR_OK) { 10947e946e7SWyllys Ingersoll cryptoerror(LOG_STDERR, 11047e946e7SWyllys Ingersoll gettext("Unable to initialize token.")); 11147e946e7SWyllys Ingersoll return (PK_ERR_SYSTEM); 11247e946e7SWyllys Ingersoll } else { 11347e946e7SWyllys Ingersoll (void) fprintf(stdout, gettext("Token %s initialized.\n"), 11447e946e7SWyllys Ingersoll (newlabel ? newlabel : currlabel)); 11547e946e7SWyllys Ingersoll } 11647e946e7SWyllys Ingersoll return (0); 11747e946e7SWyllys Ingersoll } 118