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 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <wanbootutil.h>
40 #include <sys/sysmacros.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
44 #include <sys/wanboot_impl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #define KEYMGMT_SUCCESS 0
50 #define KEYMGMT_ERROR 1
55 static char *opts
[] = { "type", NULL
};
58 * This routine is used to parse the suboptions of '-o' option.
60 * The option should be of the form: type=<3des|aes|sha1|rsa>
62 * This routine will pass the value of the suboption back in the
63 * supplied arguments, 'ka'.
66 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
69 process_option(char *arg
, wbku_key_attr_t
*ka
)
74 while (*arg
!= '\0') {
75 switch (getsubopt(&arg
, opts
, &value
)) {
80 ret
= wbku_str_to_keyattr(value
, ka
, WBKU_ANY_KEY
);
81 if (ret
!= WBKU_SUCCESS
) {
82 wbku_printerr("%s\n", wbku_retmsg(ret
));
83 return (KEYMGMT_ERROR
);
87 wbku_printerr("%s is not a valid option\n", value
);
88 return (KEYMGMT_ERROR
);
95 return (KEYMGMT_SUCCESS
);
99 * This routine extracts a key of type 'ka' from the keystore named
100 * 'keystore_name' and writes it the the file identified by 'name'.
103 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
106 process_extract(const char *keystore_name
, const char *name
,
110 uint8_t ex_key
[WANBOOT_MAXKEYLEN
];
116 * Open the keystore for reading.
118 if ((keystore_fp
= fopen(keystore_name
, "r")) == NULL
) {
119 wbku_printerr("Cannot open %s", keystore_name
);
120 return (KEYMGMT_ERROR
);
124 * Find the client key.
126 ret
= wbku_find_key(keystore_fp
, NULL
, ka
, ex_key
, B_FALSE
);
127 if (ret
!= WBKU_SUCCESS
) {
128 if (ret
== WBKU_NOKEY
) {
129 wbku_printerr("The client %s key does not exist\n",
132 wbku_printerr("%s\n", wbku_retmsg(ret
));
134 (void) fclose(keystore_fp
);
135 return (KEYMGMT_ERROR
);
137 (void) fclose(keystore_fp
);
140 * Open the output file.
142 if ((fp
= fopen(name
, "w")) == NULL
) {
143 wbku_printerr("Cannot open %s", name
);
144 (void) fclose(keystore_fp
);
145 return (KEYMGMT_ERROR
);
149 * Dump the key to the output file.
151 i
= fwrite(ex_key
, sizeof (uint8_t), ka
->ka_len
, fp
);
152 if (i
!= ka
->ka_len
) {
153 wbku_printerr("Error writing to %s", name
);
155 return (KEYMGMT_ERROR
);
162 return (KEYMGMT_SUCCESS
);
166 * There is a key which needs to be removed from the keystore. Given basic
167 * information about the key to be deleted, go through the keystore and
168 * remove it. The steps are:
169 * 1) create a temp file in the same directory as the keystore.
170 * 2) copy the existing keystore to the temp file, omitting the key being
172 * 3) shuffle files. Close the keystore and move it aside. Close the
173 * temp file and move in to the keystore.
180 compress_keystore(const char *keystore_name
, FILE *fp
,
181 const wbku_key_attr_t
*ka
)
190 * Allocate storage for the temporary path from the stack.
192 len
= strlen(keystore_name
) + sizeof (".XXXXXX");
193 tmp_path
= alloca(len
);
194 (void) snprintf(tmp_path
, len
, "%s.XXXXXX", keystore_name
);
197 * Make the temp working file where a new store will be created.
199 if ((tmp_fd
= mkstemp(tmp_path
)) == -1) {
200 wbku_printerr("Error creating %s\n", tmp_path
);
205 * Need to reference this file as a stream.
207 if ((tmp_fp
= fdopen(tmp_fd
, "w")) == NULL
) {
208 wbku_printerr("Error opening %s", tmp_path
);
209 (void) close(tmp_fd
);
210 (void) unlink(tmp_path
);
215 * Copy the existing keystore to the temp one, omitting the
218 ret
= wbku_delete_key(fp
, tmp_fp
, ka
);
219 (void) fclose(tmp_fp
);
220 if (ret
!= WBKU_SUCCESS
) {
221 wbku_printerr("%s\n", wbku_retmsg(ret
));
222 (void) unlink(tmp_path
);
229 if (rename(tmp_path
, keystore_name
) == -1) {
230 wbku_printerr("Error moving new keystore file from %s to %s",
231 tmp_path
, keystore_name
);
232 (void) unlink(tmp_path
);
240 * This routine reads a key of type 'ka' from the file identified 'name' and
241 * inserts it into the keystore named 'keystore_name'.
244 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
247 process_insert(const char *keystore_name
, const char *name
,
251 FILE *keystore_fp
= NULL
;
254 uint8_t rd_key
[WANBOOT_MAXKEYLEN
];
256 boolean_t newfile
= B_TRUE
;
260 * If the file already exists, then open the file for update.
261 * Otherwise, create it and open it for writing.
263 fd
= open(keystore_name
, O_CREAT
|O_EXCL
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
265 if (errno
== EEXIST
) {
266 keystore_fp
= fopen(keystore_name
, "r+");
270 if ((keystore_fp
= fdopen(fd
, "w")) == NULL
) {
275 if (keystore_fp
== NULL
) {
276 wbku_printerr("Cannot open %s", keystore_name
);
277 return (KEYMGMT_ERROR
);
281 * Open the input file.
283 fp
= fopen(name
, "r");
285 wbku_printerr("Cannot open %s", name
);
286 (void) fclose(keystore_fp
);
287 return (KEYMGMT_ERROR
);
291 * Read the key from the file.
293 inlen
= fread(rd_key
, sizeof (uint8_t), ka
->ka_maxlen
, fp
);
294 if (inlen
== 0 && ferror(fp
) != 0) {
295 wbku_printerr("Error reading %s", name
);
297 (void) fclose(keystore_fp
);
298 return (KEYMGMT_ERROR
);
302 if ((inlen
< ka
->ka_minlen
) || (inlen
> ka
->ka_maxlen
)) {
303 wbku_printerr("Key length is not valid\n");
304 (void) fclose(keystore_fp
);
305 return (KEYMGMT_ERROR
);
309 * If the keystore exists, search for a key of the type
310 * being inserted. If found, note its file position.
314 ret
= wbku_find_key(keystore_fp
, &pos
, ka
, NULL
, B_FALSE
);
315 if (ret
!= WBKU_SUCCESS
&& ret
!= WBKU_NOKEY
) {
316 wbku_printerr("%s\n", wbku_retmsg(ret
));
317 (void) fclose(keystore_fp
);
318 return (KEYMGMT_ERROR
);
322 * Unfortuantely, RSA keys have variable lengths. If
323 * the one being inserted is a different length than
324 * than the one that already exists in the file, then
325 * the key must be removed from the keystore and then
328 if (ret
== WBKU_SUCCESS
&& inlen
!= ka
->ka_len
) {
329 if (!compress_keystore(keystore_name
,
331 wbku_printerr("Insertion required compression"
332 " of keystore, but compression failed\n"
333 "Key was not inserted\n");
334 (void) fclose(keystore_fp
);
335 return (KEYMGMT_ERROR
);
339 * The original keystore is history. Close the
340 * stream and open a stream to the new keystore.
342 (void) fclose(keystore_fp
);
343 keystore_fp
= fopen(keystore_name
, "r+");
344 if (keystore_fp
== NULL
) {
345 wbku_printerr("Cannot open %s", keystore_name
);
346 return (KEYMGMT_ERROR
);
349 /* Force new key to end of file */
356 * If wbku_find_key() did not find the key position for us,
357 * then we should set position to the end of the file.
359 if (ret
== WBKU_NOKEY
&& (fseek(keystore_fp
, 0, SEEK_END
) != 0 ||
360 fgetpos(keystore_fp
, &pos
) != 0)) {
361 wbku_printerr("Internal error");
362 (void) fclose(keystore_fp
);
363 return (KEYMGMT_ERROR
);
367 * Write the key to the keystore.
369 ret
= wbku_write_key(keystore_fp
, &pos
, ka
, rd_key
, B_FALSE
);
370 (void) fclose(keystore_fp
);
371 if (ret
!= WBKU_SUCCESS
) {
372 wbku_printerr("%s\n", wbku_retmsg(ret
));
373 return (KEYMGMT_ERROR
);
376 (void) printf(gettext("The client's %s key has been set\n"),
381 return (KEYMGMT_SUCCESS
);
388 usage(const char *cmd
)
390 (void) fprintf(stderr
, gettext("Usage: %s"
391 " -i -k <key_file> -s <keystore> -o type=<%s|%s|%s|%s>\n"
392 " %s -x -f <out_file> -s <keystore> -o"
393 " type=<%s|%s|%s|%s>\n"),
394 cmd
, WBKU_KW_3DES
, WBKU_KW_AES_128
, WBKU_KW_HMAC_SHA1
, WBKU_KW_RSA
,
395 cmd
, WBKU_KW_3DES
, WBKU_KW_AES_128
, WBKU_KW_HMAC_SHA1
, WBKU_KW_RSA
);
399 * This program is used to insert and extract WAN boot encryption and
400 * hash keys into and from keystores. The paths to the keystores are
401 * provided by the user as are the input and output files.
404 * This program assumes all keys being inserted or extracted
405 * are client keys. There is no way for a user to insert or
406 * extract a master key using this program.
408 * We do not do any file locking scheme. This means that if two
409 * keymgmt commands are run concurrently, results can be disastrous.
412 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
415 main(int argc
, char **argv
)
418 boolean_t is_insert
= B_FALSE
;
419 boolean_t is_extract
= B_FALSE
;
420 char *keystore_name
= NULL
;
421 char *filename
= NULL
;
426 * Do the necessary magic for localization support.
428 (void) setlocale(LC_ALL
, "");
429 #if !defined(TEXT_DOMAIN)
430 #define TEXT_DOMAIN "SYS_TEST"
432 (void) textdomain(TEXT_DOMAIN
);
435 * Initialize program name for use by wbku_printerr().
437 wbku_errinit(argv
[0]);
440 * At the very least, we'll need one arg.
444 return (KEYMGMT_ERROR
);
450 ka
.ka_type
= WBKU_KEY_UNKNOWN
;
451 while ((c
= getopt(argc
, argv
, "ixf:k:s:o:")) != EOF
) {
463 if (process_option(optarg
, &ka
) != KEYMGMT_SUCCESS
) {
465 return (KEYMGMT_ERROR
);
472 keystore_name
= optarg
;
478 if (is_insert
|| filename
!= NULL
) {
480 return (KEYMGMT_ERROR
);
488 if (is_extract
|| filename
!= NULL
) {
490 return (KEYMGMT_ERROR
);
496 return (KEYMGMT_ERROR
);
501 * Must be inserting or extracting a key and we must have a
502 * key type, keystore filename and an input or output filename.
504 if ((is_insert
== is_extract
) || keystore_name
== NULL
||
505 filename
== NULL
|| ka
.ka_type
== WBKU_KEY_UNKNOWN
) {
507 return (KEYMGMT_ERROR
);
511 * Insert or extract the key.
514 ret
= process_insert(keystore_name
, filename
, &ka
);
516 ret
= process_extract(keystore_name
, filename
, &ka
);