4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/wanboot_impl.h>
31 #include <libinetutil.h>
32 #include <wanbootutil.h>
42 #include <sys/openpromio.h>
45 static char *progopts
[] = {
51 * The key's handle is the name by which a user knows the key (i.e. the
52 * name specified on the command line. The keyname is the name this
53 * utility uses to store the keys and the name OBP and wanboot use to
56 static struct keylist
{
59 const int keysize
; /* size of hex string representation */
61 WBKU_KW_3DES
, WANBOOT_DES3_KEY_NAME
,
63 WBKU_KW_AES_128
, WANBOOT_AES_128_KEY_NAME
,
64 (AES_128_KEY_SIZE
* 2),
65 WBKU_KW_HMAC_SHA1
, WANBOOT_HMAC_SHA1_KEY_NAME
,
66 (WANBOOT_HMAC_KEY_SIZE
* 2)
69 static const struct keylist
*knownkeytype(char *);
70 static char *getkey(const struct keylist
*);
71 static void deletekey(const struct keylist
*);
72 static void installkey(const struct keylist
*);
73 static void usage(const char *) __NORETURN
;
75 static boolean_t
delete = B_FALSE
;
78 main(int ac
, char **av
)
81 const struct keylist
*k
;
82 char *typestring
= NULL
;
87 * Do the necessary magic for localization support.
89 (void) setlocale(LC_ALL
, "");
90 #if !defined(TEXT_DOMAIN)
91 #define TEXT_DOMAIN "SYS_TEST"
93 (void) textdomain(TEXT_DOMAIN
);
96 * Initialize program name for use by wbku_printerr().
100 while ((i
= getopt(ac
, av
, "do:")) != -1)
108 while (*options
!= '\0') {
109 switch (getsubopt(&options
, progopts
,
128 if ((optind
>= ac
) && (typestring
!= NULL
) &&
129 ((k
= knownkeytype(typestring
)) != NULL
)) {
130 if (delete == B_TRUE
)
141 static const struct keylist
*
142 knownkeytype(char *type
)
146 for (i
= 0; i
< sizeof (keylist
)/sizeof (keylist
[0]); i
++) {
147 if (strcmp(keylist
[i
].handle
, type
) == 0)
148 return (&keylist
[i
]);
155 deletekey(const struct keylist
*k
)
158 struct wankeyio wkio
;
159 struct openpromio
*oio
;
161 (void) strlcpy(wkio
.wk_keyname
, k
->keyname
, WANBOOT_MAXKEYNAMELEN
);
162 wkio
.wk_keysize
= 0; /* zero key size indicates a deletion */
164 oio
= malloc(sizeof (struct openpromio
) + sizeof (struct wankeyio
));
166 wbku_printerr("openpromio malloc (%d) failed\n",
167 sizeof (struct openpromio
) +
168 sizeof (struct wankeyio
));
171 oio
->oprom_size
= sizeof (struct wankeyio
);
172 bcopy(&wkio
, oio
->oprom_array
, sizeof (struct wankeyio
));
173 fd
= open("/dev/openprom", O_RDWR
);
175 wbku_printerr("open: /dev/openprom");
179 if (ioctl(fd
, WANBOOT_SETKEY
, oio
) == -1) {
180 wbku_printerr("setkey: ioctl");
188 installkey(const struct keylist
*k
)
192 struct wankeyio wkio
;
193 struct openpromio
*oio
;
197 (void) strlcpy(wkio
.wk_keyname
, k
->keyname
, WANBOOT_MAXKEYNAMELEN
);
198 assert((k
->keysize
% 2) == 0);
199 wkio
.wk_keysize
= k
->keysize
/ 2;
201 if ((keyptr
= getkey(k
)) != NULL
) {
202 rawkeysize
= sizeof (wkio
.wk_u
);
203 if ((err
= hexascii_to_octet(keyptr
, strlen(keyptr
),
204 wkio
.wk_u
.key
, &rawkeysize
)) != 0) {
206 "internal error: hexascii_to_octet returned %d\n",
209 } else if (rawkeysize
!= wkio
.wk_keysize
) {
210 wbku_printerr("internal error: key size mismatch\n");
214 oio
= malloc(sizeof (struct openpromio
) +
215 sizeof (struct wankeyio
));
217 wbku_printerr("openpromio malloc (%d) failed\n",
218 sizeof (struct openpromio
) +
219 sizeof (struct wankeyio
));
222 oio
->oprom_size
= sizeof (struct wankeyio
);
223 bcopy(&wkio
, oio
->oprom_array
, sizeof (struct wankeyio
));
224 fd
= open("/dev/openprom", O_RDWR
);
226 wbku_printerr("open: /dev/openprom");
230 if (ioctl(fd
, WANBOOT_SETKEY
, oio
) == -1) {
231 wbku_printerr("setkey: ioctl");
237 wbku_printerr("getpassphrase"); /* getpassphrase() failed */
243 getkey(const struct keylist
*k
)
250 (void) snprintf(prompt
, sizeof (prompt
),
251 gettext("Enter %s key: "), k
->handle
);
252 p
= getpassphrase(prompt
);
254 /* skip over initial "0[xX]" */
255 if ((p
[0] == '0') && (p
[1] == 'x' || p
[1] == 'X'))
258 if (len
!= k
->keysize
) {
260 "key length mismatch (expected %d, got %d)\n",
264 for (q
= p
; q
< p
+ len
; q
++)
267 "non-hexadecimal characters in key\n");
276 usage(const char *progname
)
280 (void) fprintf(stderr
, gettext(
281 "usage: %s [ -d ] -o type=keytype\nwhere keytype is one of "),
283 for (i
= 0; i
< sizeof (keylist
)/sizeof (keylist
[0]); i
++)
284 (void) fprintf(stderr
, "%s ", keylist
[i
].handle
);
285 (void) fputc('\n', stderr
);