8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.lib / wanboot / keymgmt / keymgmt.c
blob10e8e0dd6cc78b90cb4390e9bca0c8591dae52b1
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <alloca.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <limits.h>
36 #include <libgen.h>
37 #include <errno.h>
38 #include <ctype.h>
39 #include <wanbootutil.h>
40 #include <sys/sysmacros.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/wanboot_impl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 /* Return codes */
49 #define KEYMGMT_SUCCESS 0
50 #define KEYMGMT_ERROR 1
52 /* Suboption. */
53 #define TYPE 0
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'.
65 * Returns:
66 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
68 static int
69 process_option(char *arg, wbku_key_attr_t *ka)
71 char *value;
72 wbku_retcode_t ret;
74 while (*arg != '\0') {
75 switch (getsubopt(&arg, opts, &value)) {
76 case TYPE:
78 * Key type.
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);
85 break;
86 default:
87 wbku_printerr("%s is not a valid option\n", value);
88 return (KEYMGMT_ERROR);
93 * Success.
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'.
102 * Returns:
103 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
105 static int
106 process_extract(const char *keystore_name, const char *name,
107 wbku_key_attr_t *ka)
109 size_t i;
110 uint8_t ex_key[WANBOOT_MAXKEYLEN];
111 FILE *keystore_fp;
112 FILE *fp;
113 wbku_retcode_t ret;
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",
130 ka->ka_str);
131 } else {
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);
154 (void) fclose(fp);
155 return (KEYMGMT_ERROR);
157 (void) fclose(fp);
160 * Success.
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
171 * removed.
172 * 3) shuffle files. Close the keystore and move it aside. Close the
173 * temp file and move in to the keystore.
175 * Returns:
176 * B_TRUE on success
177 * B_FALSE on error
179 static boolean_t
180 compress_keystore(const char *keystore_name, FILE *fp,
181 const wbku_key_attr_t *ka)
183 char *tmp_path;
184 FILE *tmp_fp;
185 int tmp_fd;
186 int len;
187 wbku_retcode_t ret;
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);
201 return (B_FALSE);
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);
211 return (B_FALSE);
215 * Copy the existing keystore to the temp one, omitting the
216 * key being deleted.
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);
223 return (B_FALSE);
227 * Shuffle files.
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);
233 return (B_FALSE);
236 return (B_TRUE);
240 * This routine reads a key of type 'ka' from the file identified 'name' and
241 * inserts it into the keystore named 'keystore_name'.
243 * Returns:
244 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
246 static int
247 process_insert(const char *keystore_name, const char *name,
248 wbku_key_attr_t *ka)
250 int fd;
251 FILE *keystore_fp = NULL;
252 FILE *fp;
253 fpos_t pos;
254 uint8_t rd_key[WANBOOT_MAXKEYLEN];
255 int inlen;
256 boolean_t newfile = B_TRUE;
257 wbku_retcode_t ret;
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);
264 if (fd < 0) {
265 if (errno == EEXIST) {
266 keystore_fp = fopen(keystore_name, "r+");
267 newfile = B_FALSE;
269 } else {
270 if ((keystore_fp = fdopen(fd, "w")) == NULL) {
271 (void) close(fd);
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");
284 if (fp == NULL) {
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);
296 (void) fclose(fp);
297 (void) fclose(keystore_fp);
298 return (KEYMGMT_ERROR);
300 (void) fclose(fp);
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.
312 ret = WBKU_NOKEY;
313 if (!newfile) {
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
326 * readded.
328 if (ret == WBKU_SUCCESS && inlen != ka->ka_len) {
329 if (!compress_keystore(keystore_name,
330 keystore_fp, ka)) {
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 */
350 ret = WBKU_NOKEY;
353 ka->ka_len = inlen;
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"),
377 ka->ka_str);
379 * Success.
381 return (KEYMGMT_SUCCESS);
385 * Prints usage().
387 static void
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.
403 * Note:
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.
411 * Returns:
412 * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
415 main(int argc, char **argv)
417 int c;
418 boolean_t is_insert = B_FALSE;
419 boolean_t is_extract = B_FALSE;
420 char *keystore_name = NULL;
421 char *filename = NULL;
422 wbku_key_attr_t ka;
423 int ret;
426 * Do the necessary magic for localization support.
428 (void) setlocale(LC_ALL, "");
429 #if !defined(TEXT_DOMAIN)
430 #define TEXT_DOMAIN "SYS_TEST"
431 #endif
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.
442 if (argc < 2) {
443 usage(argv[0]);
444 return (KEYMGMT_ERROR);
448 * Parse the options.
450 ka.ka_type = WBKU_KEY_UNKNOWN;
451 while ((c = getopt(argc, argv, "ixf:k:s:o:")) != EOF) {
452 switch (c) {
453 case 'i':
454 is_insert = B_TRUE;
455 break;
456 case 'x':
457 is_extract = B_TRUE;
458 break;
459 case 'o':
461 * Suboptions.
463 if (process_option(optarg, &ka) != KEYMGMT_SUCCESS) {
464 usage(argv[0]);
465 return (KEYMGMT_ERROR);
467 break;
468 case 's':
470 * Keystore path.
472 keystore_name = optarg;
473 break;
474 case 'f':
476 * Input file.
478 if (is_insert || filename != NULL) {
479 usage(argv[0]);
480 return (KEYMGMT_ERROR);
482 filename = optarg;
483 break;
484 case 'k':
486 * Input file.
488 if (is_extract || filename != NULL) {
489 usage(argv[0]);
490 return (KEYMGMT_ERROR);
492 filename = optarg;
493 break;
494 default:
495 usage(argv[0]);
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) {
506 usage(argv[0]);
507 return (KEYMGMT_ERROR);
511 * Insert or extract the key.
513 if (is_insert) {
514 ret = process_insert(keystore_name, filename, &ka);
515 } else {
516 ret = process_extract(keystore_name, filename, &ka);
519 return (ret);