xref: /titanic_50/usr/src/cmd/cmd-crypto/pktool/setpin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This file implements the setpin operation for this tool.
31*7c478bd9Sstevel@tonic-gate  * The basic flow of the process is to load the PKCS#11 module,
32*7c478bd9Sstevel@tonic-gate  * finds the soft token, log into it, prompt the user for the
33*7c478bd9Sstevel@tonic-gate  * new PIN, change the token's PIN, and log out.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
40*7c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
41*7c478bd9Sstevel@tonic-gate #include "common.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static int
44*7c478bd9Sstevel@tonic-gate set_token_pin(CK_SESSION_HANDLE hdl, CK_UTF8CHAR_PTR oldpin, CK_ULONG oldpinlen)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR_PTR		pin1, pin2;
47*7c478bd9Sstevel@tonic-gate 	int			len1, len2;
48*7c478bd9Sstevel@tonic-gate 	int			rv;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	cryptodebug("inside set_token_pin");
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	if ((len1 = get_password(gettext("Enter new PIN:"),
53*7c478bd9Sstevel@tonic-gate 	    (char **)&pin1)) < 0)
54*7c478bd9Sstevel@tonic-gate 		return (PK_ERR_NEWPIN);
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if ((len2 = get_password(gettext("Re-enter new PIN:"),
57*7c478bd9Sstevel@tonic-gate 	    (char **)&pin2)) < 0) {
58*7c478bd9Sstevel@tonic-gate 		free(pin1);
59*7c478bd9Sstevel@tonic-gate 		return (PK_ERR_PINCONFIRM);
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	/* NOTE:  Do not use strcmp on pin1 and pin2; they are UTF strings */
63*7c478bd9Sstevel@tonic-gate 	if (len1 != len2 || memcmp(pin1, pin2, len1) != 0) {
64*7c478bd9Sstevel@tonic-gate 		free(pin1);
65*7c478bd9Sstevel@tonic-gate 		free(pin2);
66*7c478bd9Sstevel@tonic-gate 		return (PK_ERR_PINMATCH);
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if ((rv = C_SetPIN(hdl, oldpin, oldpinlen, pin1, (CK_ULONG)len1))
70*7c478bd9Sstevel@tonic-gate 	    != CKR_OK) {
71*7c478bd9Sstevel@tonic-gate 		pk11_errno = rv;
72*7c478bd9Sstevel@tonic-gate 		free(pin1);
73*7c478bd9Sstevel@tonic-gate 		free(pin2);
74*7c478bd9Sstevel@tonic-gate 		return (PK_ERR_PK11SETPIN);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	free(pin1);
78*7c478bd9Sstevel@tonic-gate 	free(pin2);
79*7c478bd9Sstevel@tonic-gate 	return (PK_ERR_NONE);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * This is the main entry point in this module.  It controls the process
84*7c478bd9Sstevel@tonic-gate  * by which the token's PIN is changed.  It relies on set_token_pin() to
85*7c478bd9Sstevel@tonic-gate  * handle the extra work of prompting and confirming the new PIN.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate int
88*7c478bd9Sstevel@tonic-gate pk_setpin(int argc, char *argv[])
89*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	char		*token_name = NULL;
92*7c478bd9Sstevel@tonic-gate 	char		*manuf_id = NULL;
93*7c478bd9Sstevel@tonic-gate 	char		*serial_no = NULL;
94*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID		slot_id;
95*7c478bd9Sstevel@tonic-gate 	CK_FLAGS		pin_state;
96*7c478bd9Sstevel@tonic-gate 	CK_SESSION_HANDLE	hdl;
97*7c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR_PTR		pin;
98*7c478bd9Sstevel@tonic-gate 	int			pinlen;
99*7c478bd9Sstevel@tonic-gate 	int			rv;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	cryptodebug("inside pk_setpin");
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	/*
104*7c478bd9Sstevel@tonic-gate 	 * Token_name, manuf_id, and serial_no are all optional.
105*7c478bd9Sstevel@tonic-gate 	 * If unspecified, token_name must have a default value
106*7c478bd9Sstevel@tonic-gate 	 * at least.
107*7c478bd9Sstevel@tonic-gate 	 */
108*7c478bd9Sstevel@tonic-gate 	token_name = SOFT_TOKEN_LABEL;
109*7c478bd9Sstevel@tonic-gate 	manuf_id = SOFT_MANUFACTURER_ID;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/* No additional args allowed. */
112*7c478bd9Sstevel@tonic-gate 	if (argc != 1)
113*7c478bd9Sstevel@tonic-gate 		return (PK_ERR_USAGE);
114*7c478bd9Sstevel@tonic-gate 	/* Done parsing command line options. */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/* Initialize PKCS11, find the slot with token. */
117*7c478bd9Sstevel@tonic-gate 	if ((rv = init_pk11()) != PK_ERR_NONE)
118*7c478bd9Sstevel@tonic-gate 		return (rv);
119*7c478bd9Sstevel@tonic-gate 	if ((rv = find_token_slot(token_name, manuf_id, serial_no,
120*7c478bd9Sstevel@tonic-gate 	    &slot_id, &pin_state)) != PK_ERR_NONE)
121*7c478bd9Sstevel@tonic-gate 		return (rv);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/* Check if the token flags show the PIN has not be set yet. */
124*7c478bd9Sstevel@tonic-gate 	if (pin_state == CKF_USER_PIN_TO_BE_CHANGED) {
125*7c478bd9Sstevel@tonic-gate 		cryptodebug("pin_state: first time pin is being set");
126*7c478bd9Sstevel@tonic-gate 		if ((pin = (CK_UTF8CHAR_PTR)strdup(SOFT_DEFAULT_PIN)) == NULL)
127*7c478bd9Sstevel@tonic-gate 			return (PK_ERR_NOMEMORY);
128*7c478bd9Sstevel@tonic-gate 		pinlen = strlen(SOFT_DEFAULT_PIN);
129*7c478bd9Sstevel@tonic-gate 	} else {
130*7c478bd9Sstevel@tonic-gate 		cryptodebug("pin_state: changing an existing pin ");
131*7c478bd9Sstevel@tonic-gate 		/* Have user unlock token with correct password */
132*7c478bd9Sstevel@tonic-gate 		if ((pinlen = get_password(gettext("Enter token PIN:"),
133*7c478bd9Sstevel@tonic-gate 		    (char **)&pin)) < 0)
134*7c478bd9Sstevel@tonic-gate 			return (PK_ERR_PASSPHRASE);
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * Log into the token.  If login fails with an uninitialized PIN,
139*7c478bd9Sstevel@tonic-gate 	 * it means this is the first time the token has been used.
140*7c478bd9Sstevel@tonic-gate 	 * Or if the login is successful, but all subsequent calls to
141*7c478bd9Sstevel@tonic-gate 	 * any function return with an expired PIN, then this is the
142*7c478bd9Sstevel@tonic-gate 	 * first time the token is used.  In either case, use the
143*7c478bd9Sstevel@tonic-gate 	 * passphrase "changeme" as the initial PIN.
144*7c478bd9Sstevel@tonic-gate 	 */
145*7c478bd9Sstevel@tonic-gate 	if ((rv = login_token(slot_id, pin, (CK_ULONG)pinlen, &hdl))
146*7c478bd9Sstevel@tonic-gate 	    != PK_ERR_NONE) {
147*7c478bd9Sstevel@tonic-gate 		free(pin);
148*7c478bd9Sstevel@tonic-gate 		return (rv);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/* Set the pin for the PKCS11 token. */
152*7c478bd9Sstevel@tonic-gate 	if ((rv = set_token_pin(hdl, pin, (CK_ULONG)pinlen)) != PK_ERR_NONE) {
153*7c478bd9Sstevel@tonic-gate 		free(pin);
154*7c478bd9Sstevel@tonic-gate 		logout_token(hdl);
155*7c478bd9Sstevel@tonic-gate 		return (rv);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	free(pin);
159*7c478bd9Sstevel@tonic-gate 	logout_token(hdl);
160*7c478bd9Sstevel@tonic-gate 	return (PK_ERR_NONE);
161*7c478bd9Sstevel@tonic-gate }
162