1*47e946e7SWyllys Ingersoll /*
2*47e946e7SWyllys Ingersoll  * CDDL HEADER START
3*47e946e7SWyllys Ingersoll  *
4*47e946e7SWyllys Ingersoll  * The contents of this file are subject to the terms of the
5*47e946e7SWyllys Ingersoll  * Common Development and Distribution License (the "License").
6*47e946e7SWyllys Ingersoll  * You may not use this file except in compliance with the License.
7*47e946e7SWyllys Ingersoll  *
8*47e946e7SWyllys Ingersoll  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*47e946e7SWyllys Ingersoll  * or http://www.opensolaris.org/os/licensing.
10*47e946e7SWyllys Ingersoll  * See the License for the specific language governing permissions
11*47e946e7SWyllys Ingersoll  * and limitations under the License.
12*47e946e7SWyllys Ingersoll  *
13*47e946e7SWyllys Ingersoll  * When distributing Covered Code, include this CDDL HEADER in each
14*47e946e7SWyllys Ingersoll  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*47e946e7SWyllys Ingersoll  * If applicable, add the following below this CDDL HEADER, with the
16*47e946e7SWyllys Ingersoll  * fields enclosed by brackets "[]" replaced with your own identifying
17*47e946e7SWyllys Ingersoll  * information: Portions Copyright [yyyy] [name of copyright owner]
18*47e946e7SWyllys Ingersoll  *
19*47e946e7SWyllys Ingersoll  * CDDL HEADER END
20*47e946e7SWyllys Ingersoll  */
21*47e946e7SWyllys Ingersoll /*
22*47e946e7SWyllys Ingersoll  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*47e946e7SWyllys Ingersoll  * Use is subject to license terms.
24*47e946e7SWyllys Ingersoll  */
25*47e946e7SWyllys Ingersoll 
26*47e946e7SWyllys Ingersoll /*
27*47e946e7SWyllys Ingersoll  * This file implements the inittoken operation for this tool.
28*47e946e7SWyllys Ingersoll  * The basic flow of the process is to load the PKCS#11 module,
29*47e946e7SWyllys Ingersoll  * find the token to be initialize , login using the SO pin,
30*47e946e7SWyllys Ingersoll  * and call C_InitToken.
31*47e946e7SWyllys Ingersoll  */
32*47e946e7SWyllys Ingersoll 
33*47e946e7SWyllys Ingersoll #include <stdio.h>
34*47e946e7SWyllys Ingersoll #include <stdlib.h>
35*47e946e7SWyllys Ingersoll #include <errno.h>
36*47e946e7SWyllys Ingersoll #include <string.h>
37*47e946e7SWyllys Ingersoll #include <cryptoutil.h>
38*47e946e7SWyllys Ingersoll #include <security/cryptoki.h>
39*47e946e7SWyllys Ingersoll #include "common.h"
40*47e946e7SWyllys Ingersoll 
41*47e946e7SWyllys Ingersoll int
42*47e946e7SWyllys Ingersoll pk_inittoken(int argc, char *argv[])
43*47e946e7SWyllys Ingersoll /* ARGSUSED */
44*47e946e7SWyllys Ingersoll {
45*47e946e7SWyllys Ingersoll 	int		opt;
46*47e946e7SWyllys Ingersoll 	int		rv;
47*47e946e7SWyllys Ingersoll 	extern int	optind_av;
48*47e946e7SWyllys Ingersoll 	extern char	*optarg_av;
49*47e946e7SWyllys Ingersoll 	char		*newlabel = NULL;
50*47e946e7SWyllys Ingersoll 	char		*currlabel = NULL;
51*47e946e7SWyllys Ingersoll 	CK_UTF8CHAR_PTR	sopin;
52*47e946e7SWyllys Ingersoll 	CK_ULONG	sopinlen;
53*47e946e7SWyllys Ingersoll 	KMF_HANDLE_T	handle;
54*47e946e7SWyllys Ingersoll 
55*47e946e7SWyllys Ingersoll 	/* Parse command line options.  Do NOT i18n/l10n. */
56*47e946e7SWyllys Ingersoll 	while ((opt = getopt_av(argc, argv,
57*47e946e7SWyllys Ingersoll 		"n:(newlabel)"
58*47e946e7SWyllys Ingersoll 		"l:(currlabel)")) != EOF) {
59*47e946e7SWyllys Ingersoll 		switch (opt) {
60*47e946e7SWyllys Ingersoll 			case 'l':	/* token specifier */
61*47e946e7SWyllys Ingersoll 				if (currlabel)
62*47e946e7SWyllys Ingersoll 					return (PK_ERR_USAGE);
63*47e946e7SWyllys Ingersoll 				currlabel = optarg_av;
64*47e946e7SWyllys Ingersoll 				break;
65*47e946e7SWyllys Ingersoll 			case 'n': /* token specifier */
66*47e946e7SWyllys Ingersoll 				if (newlabel)
67*47e946e7SWyllys Ingersoll 					return (PK_ERR_USAGE);
68*47e946e7SWyllys Ingersoll 				newlabel = optarg_av;
69*47e946e7SWyllys Ingersoll 				break;
70*47e946e7SWyllys Ingersoll 			default:
71*47e946e7SWyllys Ingersoll 				return (PK_ERR_USAGE);
72*47e946e7SWyllys Ingersoll 				break;
73*47e946e7SWyllys Ingersoll 		}
74*47e946e7SWyllys Ingersoll 	}
75*47e946e7SWyllys Ingersoll 
76*47e946e7SWyllys Ingersoll 	/* No additional args allowed. */
77*47e946e7SWyllys Ingersoll 	argc -= optind_av;
78*47e946e7SWyllys Ingersoll 	argv += optind_av;
79*47e946e7SWyllys Ingersoll 	if (argc != 0)
80*47e946e7SWyllys Ingersoll 		return (PK_ERR_USAGE);
81*47e946e7SWyllys Ingersoll 
82*47e946e7SWyllys Ingersoll 	if ((rv = kmf_initialize(&handle, NULL, NULL)) != KMF_OK)
83*47e946e7SWyllys Ingersoll 		return (rv);
84*47e946e7SWyllys Ingersoll 
85*47e946e7SWyllys Ingersoll 	if ((rv = get_pin(gettext("Enter SO PIN:"), NULL, &sopin, &sopinlen))
86*47e946e7SWyllys Ingersoll 	    != CKR_OK) {
87*47e946e7SWyllys Ingersoll 		cryptoerror(LOG_STDERR,
88*47e946e7SWyllys Ingersoll 		    gettext("Unable to get SO PIN for token"));
89*47e946e7SWyllys Ingersoll 		return (PK_ERR_SYSTEM);
90*47e946e7SWyllys Ingersoll 	}
91*47e946e7SWyllys Ingersoll 	if ((currlabel == NULL || !strlen(currlabel))) {
92*47e946e7SWyllys Ingersoll 		cryptoerror(LOG_STDERR,
93*47e946e7SWyllys Ingersoll 		    gettext("The current token is not identified by label."));
94*47e946e7SWyllys Ingersoll 		return (PK_ERR_SYSTEM);
95*47e946e7SWyllys Ingersoll 	}
96*47e946e7SWyllys Ingersoll 
97*47e946e7SWyllys Ingersoll 	rv = kmf_pk11_init_token(handle, currlabel, newlabel,
98*47e946e7SWyllys Ingersoll 	    sopin, sopinlen);
99*47e946e7SWyllys Ingersoll 
100*47e946e7SWyllys Ingersoll 	(void) kmf_finalize(handle);
101*47e946e7SWyllys Ingersoll 
102*47e946e7SWyllys Ingersoll 	free(sopin);
103*47e946e7SWyllys Ingersoll 
104*47e946e7SWyllys Ingersoll 	if (rv == KMF_ERR_AUTH_FAILED) {
105*47e946e7SWyllys Ingersoll 		cryptoerror(LOG_STDERR,
106*47e946e7SWyllys Ingersoll 		    gettext("Incorrect passphrase."));
107*47e946e7SWyllys Ingersoll 		return (PK_ERR_SYSTEM);
108*47e946e7SWyllys Ingersoll 	} else if (rv != CKR_OK) {
109*47e946e7SWyllys Ingersoll 		cryptoerror(LOG_STDERR,
110*47e946e7SWyllys Ingersoll 		    gettext("Unable to initialize token."));
111*47e946e7SWyllys Ingersoll 		return (PK_ERR_SYSTEM);
112*47e946e7SWyllys Ingersoll 	} else {
113*47e946e7SWyllys Ingersoll 		(void) fprintf(stdout, gettext("Token %s initialized.\n"),
114*47e946e7SWyllys Ingersoll 		    (newlabel ? newlabel : currlabel));
115*47e946e7SWyllys Ingersoll 	}
116*47e946e7SWyllys Ingersoll 	return (0);
117*47e946e7SWyllys Ingersoll }
118