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