1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/wanboot_impl.h>
31*0Sstevel@tonic-gate #include <libinetutil.h>
32*0Sstevel@tonic-gate #include <wanbootutil.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <locale.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <fcntl.h>
40*0Sstevel@tonic-gate #include <ctype.h>
41*0Sstevel@tonic-gate #include <assert.h>
42*0Sstevel@tonic-gate #include <sys/openpromio.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	TYPE	0
45*0Sstevel@tonic-gate static char	*progopts[] = {
46*0Sstevel@tonic-gate 	"type",
47*0Sstevel@tonic-gate 	NULL
48*0Sstevel@tonic-gate };
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  *	The key's handle is the name by which a user knows the key (i.e. the
52*0Sstevel@tonic-gate  *	name specified on the command line.  The keyname is the name this
53*0Sstevel@tonic-gate  *	utility uses to store the keys and the name OBP and wanboot use to
54*0Sstevel@tonic-gate  *	retrieve them.
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static struct keylist {
57*0Sstevel@tonic-gate 	const char	*handle;
58*0Sstevel@tonic-gate 	const char	*keyname;
59*0Sstevel@tonic-gate 	const int	keysize;	/* size of hex string representation */
60*0Sstevel@tonic-gate } keylist[] = {
61*0Sstevel@tonic-gate 	WBKU_KW_3DES, WANBOOT_DES3_KEY_NAME,
62*0Sstevel@tonic-gate 	    (DES3_KEY_SIZE * 2),
63*0Sstevel@tonic-gate 	WBKU_KW_AES_128, WANBOOT_AES_128_KEY_NAME,
64*0Sstevel@tonic-gate 	    (AES_128_KEY_SIZE * 2),
65*0Sstevel@tonic-gate 	WBKU_KW_HMAC_SHA1, WANBOOT_HMAC_SHA1_KEY_NAME,
66*0Sstevel@tonic-gate 	    (WANBOOT_HMAC_KEY_SIZE * 2)
67*0Sstevel@tonic-gate };
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static const struct keylist	*knownkeytype(char *);
70*0Sstevel@tonic-gate static char			*getkey(const struct keylist *);
71*0Sstevel@tonic-gate static void			deletekey(const struct keylist *);
72*0Sstevel@tonic-gate static void			installkey(const struct keylist *);
73*0Sstevel@tonic-gate static void			usage(const char *);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static boolean_t	delete = B_FALSE;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate int
78*0Sstevel@tonic-gate main(int ac, char **av)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	int			i;
81*0Sstevel@tonic-gate 	const struct keylist	*k;
82*0Sstevel@tonic-gate 	char			*typestring = NULL;
83*0Sstevel@tonic-gate 	char			*options;
84*0Sstevel@tonic-gate 	char			*value;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/*
87*0Sstevel@tonic-gate 	 * Do the necessary magic for localization support.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
90*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
91*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
92*0Sstevel@tonic-gate #endif
93*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	/*
96*0Sstevel@tonic-gate 	 * Initialize program name for use by wbku_printerr().
97*0Sstevel@tonic-gate 	 */
98*0Sstevel@tonic-gate 	wbku_errinit(av[0]);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	while ((i = getopt(ac, av, "do:")) != -1)
101*0Sstevel@tonic-gate 		switch (i) {
102*0Sstevel@tonic-gate 			case 'd':
103*0Sstevel@tonic-gate 				delete	= B_TRUE;
104*0Sstevel@tonic-gate 				break;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 			case 'o':
107*0Sstevel@tonic-gate 				options = optarg;
108*0Sstevel@tonic-gate 				while (*options != '\0') {
109*0Sstevel@tonic-gate 					switch (getsubopt(&options, progopts,
110*0Sstevel@tonic-gate 					    &value)) {
111*0Sstevel@tonic-gate 						case TYPE:
112*0Sstevel@tonic-gate 							typestring = value;
113*0Sstevel@tonic-gate 							break;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 						default:
116*0Sstevel@tonic-gate 							/* unknown token */
117*0Sstevel@tonic-gate 							usage(*av);
118*0Sstevel@tonic-gate 							/* NOTREACHED */
119*0Sstevel@tonic-gate 					}
120*0Sstevel@tonic-gate 				}
121*0Sstevel@tonic-gate 				break;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 			case '?':
124*0Sstevel@tonic-gate 				usage(*av);
125*0Sstevel@tonic-gate 				/* NOTREACHED */
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if ((optind >= ac) && (typestring != NULL) &&
129*0Sstevel@tonic-gate 	    ((k = knownkeytype(typestring)) != NULL)) {
130*0Sstevel@tonic-gate 		if (delete == B_TRUE)
131*0Sstevel@tonic-gate 			deletekey(k);
132*0Sstevel@tonic-gate 		else
133*0Sstevel@tonic-gate 			installkey(k);
134*0Sstevel@tonic-gate 		return (0);
135*0Sstevel@tonic-gate 	} else {
136*0Sstevel@tonic-gate 		usage(*av);
137*0Sstevel@tonic-gate 		/* NOTREACHED */
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static const struct keylist *
142*0Sstevel@tonic-gate knownkeytype(char *type)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	int	i;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (keylist)/sizeof (keylist[0]); i++) {
147*0Sstevel@tonic-gate 		if (strcmp(keylist[i].handle, type) == 0)
148*0Sstevel@tonic-gate 			return (&keylist[i]);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	return (NULL);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static void
155*0Sstevel@tonic-gate deletekey(const struct keylist *k)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	int			fd;
158*0Sstevel@tonic-gate 	struct wankeyio		wkio;
159*0Sstevel@tonic-gate 	struct openpromio	*oio;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	(void) strlcpy(wkio.wk_keyname, k->keyname, WANBOOT_MAXKEYNAMELEN);
162*0Sstevel@tonic-gate 	wkio.wk_keysize = 0;	/* zero key size indicates a deletion */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	oio = malloc(sizeof (struct openpromio) + sizeof (struct wankeyio));
165*0Sstevel@tonic-gate 	if (oio == NULL) {
166*0Sstevel@tonic-gate 		wbku_printerr("openpromio malloc (%d) failed\n",
167*0Sstevel@tonic-gate 		    sizeof (struct openpromio) +
168*0Sstevel@tonic-gate 		    sizeof (struct wankeyio));
169*0Sstevel@tonic-gate 		exit(1);
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 	oio->oprom_size = sizeof (struct wankeyio);
172*0Sstevel@tonic-gate 	bcopy(&wkio, oio->oprom_array, sizeof (struct wankeyio));
173*0Sstevel@tonic-gate 	fd = open("/dev/openprom", O_RDWR);
174*0Sstevel@tonic-gate 	if (fd == -1) {
175*0Sstevel@tonic-gate 		wbku_printerr("open: /dev/openprom");
176*0Sstevel@tonic-gate 		exit(1);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	if (ioctl(fd, WANBOOT_SETKEY, oio) == -1) {
180*0Sstevel@tonic-gate 		wbku_printerr("setkey: ioctl");
181*0Sstevel@tonic-gate 		exit(1);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	(void) close(fd);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static void
188*0Sstevel@tonic-gate installkey(const struct keylist *k)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	char			*keyptr;
191*0Sstevel@tonic-gate 	int			fd;
192*0Sstevel@tonic-gate 	struct wankeyio		wkio;
193*0Sstevel@tonic-gate 	struct openpromio	*oio;
194*0Sstevel@tonic-gate 	uint_t			rawkeysize;
195*0Sstevel@tonic-gate 	int			err;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	(void) strlcpy(wkio.wk_keyname, k->keyname, WANBOOT_MAXKEYNAMELEN);
198*0Sstevel@tonic-gate 	assert((k->keysize % 2) == 0);
199*0Sstevel@tonic-gate 	wkio.wk_keysize = k->keysize / 2;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if ((keyptr = getkey(k)) != NULL) {
202*0Sstevel@tonic-gate 		rawkeysize = sizeof (wkio.wk_u);
203*0Sstevel@tonic-gate 		if ((err = hexascii_to_octet(keyptr, strlen(keyptr),
204*0Sstevel@tonic-gate 		    wkio.wk_u.key, &rawkeysize)) != 0) {
205*0Sstevel@tonic-gate 			wbku_printerr(
206*0Sstevel@tonic-gate 			    "internal error: hexascii_to_octet returned %d\n",
207*0Sstevel@tonic-gate 			    err);
208*0Sstevel@tonic-gate 			exit(1);
209*0Sstevel@tonic-gate 		} else if (rawkeysize != wkio.wk_keysize) {
210*0Sstevel@tonic-gate 			wbku_printerr("internal error:  key size mismatch\n");
211*0Sstevel@tonic-gate 			exit(1);
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 		oio = malloc(sizeof (struct openpromio) +
215*0Sstevel@tonic-gate 		    sizeof (struct wankeyio));
216*0Sstevel@tonic-gate 		if (oio == NULL) {
217*0Sstevel@tonic-gate 			wbku_printerr("openpromio malloc (%d) failed\n",
218*0Sstevel@tonic-gate 			    sizeof (struct openpromio) +
219*0Sstevel@tonic-gate 			    sizeof (struct wankeyio));
220*0Sstevel@tonic-gate 			exit(1);
221*0Sstevel@tonic-gate 		}
222*0Sstevel@tonic-gate 		oio->oprom_size = sizeof (struct wankeyio);
223*0Sstevel@tonic-gate 		bcopy(&wkio, oio->oprom_array, sizeof (struct wankeyio));
224*0Sstevel@tonic-gate 		fd = open("/dev/openprom", O_RDWR);
225*0Sstevel@tonic-gate 		if (fd == -1) {
226*0Sstevel@tonic-gate 			wbku_printerr("open: /dev/openprom");
227*0Sstevel@tonic-gate 			exit(1);
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		if (ioctl(fd, WANBOOT_SETKEY, oio) == -1) {
231*0Sstevel@tonic-gate 			wbku_printerr("setkey: ioctl");
232*0Sstevel@tonic-gate 			exit(1);
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		(void) close(fd);
236*0Sstevel@tonic-gate 	} else {
237*0Sstevel@tonic-gate 		wbku_printerr("getpassphrase");	/* getpassphrase() failed */
238*0Sstevel@tonic-gate 		exit(1);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate static char *
243*0Sstevel@tonic-gate getkey(const struct keylist *k)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	char	prompt[BUFSIZ];
246*0Sstevel@tonic-gate 	char	*p;
247*0Sstevel@tonic-gate 	char	*q;
248*0Sstevel@tonic-gate 	int	len;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	(void) snprintf(prompt, sizeof (prompt),
251*0Sstevel@tonic-gate 	    gettext("Enter %s key:  "), k->handle);
252*0Sstevel@tonic-gate 	p = getpassphrase(prompt);
253*0Sstevel@tonic-gate 	if (p) {
254*0Sstevel@tonic-gate 		/* skip over initial "0[xX]" */
255*0Sstevel@tonic-gate 		if ((p[0] == '0') && (p[1] == 'x' || p[1] == 'X'))
256*0Sstevel@tonic-gate 			p += 2;
257*0Sstevel@tonic-gate 		len = strlen(p);
258*0Sstevel@tonic-gate 		if (len != k->keysize) {
259*0Sstevel@tonic-gate 			wbku_printerr(
260*0Sstevel@tonic-gate 			    "key length mismatch (expected %d, got %d)\n",
261*0Sstevel@tonic-gate 			    k->keysize, len);
262*0Sstevel@tonic-gate 			exit(1);
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 		for (q = p; q < p + len; q++)
265*0Sstevel@tonic-gate 			if (!isxdigit(*q)) {
266*0Sstevel@tonic-gate 				wbku_printerr(
267*0Sstevel@tonic-gate 				    "non-hexadecimal characters in key\n");
268*0Sstevel@tonic-gate 				exit(1);
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	return (p);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate static void
276*0Sstevel@tonic-gate usage(const char *progname)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate 	int	i;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
281*0Sstevel@tonic-gate 	    "usage:  %s [ -d ] -o type=keytype\nwhere keytype is one of "),
282*0Sstevel@tonic-gate 	    progname);
283*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (keylist)/sizeof (keylist[0]); i++)
284*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s ", keylist[i].handle);
285*0Sstevel@tonic-gate 	(void) fputc('\n', stderr);
286*0Sstevel@tonic-gate 	exit(1);
287*0Sstevel@tonic-gate }
288