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 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 #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #include <stdio.h>
29*0Sstevel@tonic-gate #include <alloca.h>
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <locale.h>
35*0Sstevel@tonic-gate #include <limits.h>
36*0Sstevel@tonic-gate #include <libgen.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <wanbootutil.h>
40*0Sstevel@tonic-gate #include <sys/sysmacros.h>
41*0Sstevel@tonic-gate #include <sys/socket.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/stat.h>
44*0Sstevel@tonic-gate #include <sys/wanboot_impl.h>
45*0Sstevel@tonic-gate #include <netinet/in.h>
46*0Sstevel@tonic-gate #include <arpa/inet.h>
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate /* Return codes */
49*0Sstevel@tonic-gate #define KEYMGMT_SUCCESS 0
50*0Sstevel@tonic-gate #define KEYMGMT_ERROR 1
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /* Suboption. */
53*0Sstevel@tonic-gate #define TYPE 0
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate static char *opts[] = { "type", NULL };
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate * This routine is used to parse the suboptions of '-o' option.
59*0Sstevel@tonic-gate *
60*0Sstevel@tonic-gate * The option should be of the form: type=<3des|aes|sha1|rsa>
61*0Sstevel@tonic-gate *
62*0Sstevel@tonic-gate * This routine will pass the value of the suboption back in the
63*0Sstevel@tonic-gate * supplied arguments, 'ka'.
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * Returns:
66*0Sstevel@tonic-gate * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
67*0Sstevel@tonic-gate */
68*0Sstevel@tonic-gate static int
process_option(char * arg,wbku_key_attr_t * ka)69*0Sstevel@tonic-gate process_option(char *arg, wbku_key_attr_t *ka)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate char *value;
72*0Sstevel@tonic-gate wbku_retcode_t ret;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate while (*arg != '\0') {
75*0Sstevel@tonic-gate switch (getsubopt(&arg, opts, &value)) {
76*0Sstevel@tonic-gate case TYPE:
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate * Key type.
79*0Sstevel@tonic-gate */
80*0Sstevel@tonic-gate ret = wbku_str_to_keyattr(value, ka, WBKU_ANY_KEY);
81*0Sstevel@tonic-gate if (ret != WBKU_SUCCESS) {
82*0Sstevel@tonic-gate wbku_printerr("%s\n", wbku_retmsg(ret));
83*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate break;
86*0Sstevel@tonic-gate default:
87*0Sstevel@tonic-gate wbku_printerr("%s is not a valid option\n", value);
88*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate * Success.
94*0Sstevel@tonic-gate */
95*0Sstevel@tonic-gate return (KEYMGMT_SUCCESS);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate /*
99*0Sstevel@tonic-gate * This routine extracts a key of type 'ka' from the keystore named
100*0Sstevel@tonic-gate * 'keystore_name' and writes it the the file identified by 'name'.
101*0Sstevel@tonic-gate *
102*0Sstevel@tonic-gate * Returns:
103*0Sstevel@tonic-gate * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
104*0Sstevel@tonic-gate */
105*0Sstevel@tonic-gate static int
process_extract(const char * keystore_name,const char * name,wbku_key_attr_t * ka)106*0Sstevel@tonic-gate process_extract(const char *keystore_name, const char *name,
107*0Sstevel@tonic-gate wbku_key_attr_t *ka)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate size_t i;
110*0Sstevel@tonic-gate uint8_t ex_key[WANBOOT_MAXKEYLEN];
111*0Sstevel@tonic-gate FILE *keystore_fp;
112*0Sstevel@tonic-gate FILE *fp;
113*0Sstevel@tonic-gate wbku_retcode_t ret;
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate /*
116*0Sstevel@tonic-gate * Open the keystore for reading.
117*0Sstevel@tonic-gate */
118*0Sstevel@tonic-gate if ((keystore_fp = fopen(keystore_name, "r")) == NULL) {
119*0Sstevel@tonic-gate wbku_printerr("Cannot open %s", keystore_name);
120*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate * Find the client key.
125*0Sstevel@tonic-gate */
126*0Sstevel@tonic-gate ret = wbku_find_key(keystore_fp, NULL, ka, ex_key, B_FALSE);
127*0Sstevel@tonic-gate if (ret != WBKU_SUCCESS) {
128*0Sstevel@tonic-gate if (ret == WBKU_NOKEY) {
129*0Sstevel@tonic-gate wbku_printerr("The client %s key does not exist\n",
130*0Sstevel@tonic-gate ka->ka_str);
131*0Sstevel@tonic-gate } else {
132*0Sstevel@tonic-gate wbku_printerr("%s\n", wbku_retmsg(ret));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate (void) fclose(keystore_fp);
135*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate (void) fclose(keystore_fp);
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate * Open the output file.
141*0Sstevel@tonic-gate */
142*0Sstevel@tonic-gate if ((fp = fopen(name, "w")) == NULL) {
143*0Sstevel@tonic-gate wbku_printerr("Cannot open %s", name);
144*0Sstevel@tonic-gate (void) fclose(keystore_fp);
145*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate * Dump the key to the output file.
150*0Sstevel@tonic-gate */
151*0Sstevel@tonic-gate i = fwrite(ex_key, sizeof (uint8_t), ka->ka_len, fp);
152*0Sstevel@tonic-gate if (i != ka->ka_len) {
153*0Sstevel@tonic-gate wbku_printerr("Error writing to %s", name);
154*0Sstevel@tonic-gate (void) fclose(fp);
155*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate (void) fclose(fp);
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate * Success.
161*0Sstevel@tonic-gate */
162*0Sstevel@tonic-gate return (KEYMGMT_SUCCESS);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate * There is a key which needs to be removed from the keystore. Given basic
167*0Sstevel@tonic-gate * information about the key to be deleted, go through the keystore and
168*0Sstevel@tonic-gate * remove it. The steps are:
169*0Sstevel@tonic-gate * 1) create a temp file in the same directory as the keystore.
170*0Sstevel@tonic-gate * 2) copy the existing keystore to the temp file, omitting the key being
171*0Sstevel@tonic-gate * removed.
172*0Sstevel@tonic-gate * 3) shuffle files. Close the keystore and move it aside. Close the
173*0Sstevel@tonic-gate * temp file and move in to the keystore.
174*0Sstevel@tonic-gate *
175*0Sstevel@tonic-gate * Returns:
176*0Sstevel@tonic-gate * B_TRUE on success
177*0Sstevel@tonic-gate * B_FALSE on error
178*0Sstevel@tonic-gate */
179*0Sstevel@tonic-gate static boolean_t
compress_keystore(const char * keystore_name,FILE * fp,const wbku_key_attr_t * ka)180*0Sstevel@tonic-gate compress_keystore(const char *keystore_name, FILE *fp,
181*0Sstevel@tonic-gate const wbku_key_attr_t *ka)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate char *tmp_path;
184*0Sstevel@tonic-gate FILE *tmp_fp;
185*0Sstevel@tonic-gate int tmp_fd;
186*0Sstevel@tonic-gate int len;
187*0Sstevel@tonic-gate wbku_retcode_t ret;
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate * Allocate storage for the temporary path from the stack.
191*0Sstevel@tonic-gate */
192*0Sstevel@tonic-gate len = strlen(keystore_name) + sizeof (".XXXXXX");
193*0Sstevel@tonic-gate tmp_path = alloca(len);
194*0Sstevel@tonic-gate (void) snprintf(tmp_path, len, "%s.XXXXXX", keystore_name);
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate * Make the temp working file where a new store will be created.
198*0Sstevel@tonic-gate */
199*0Sstevel@tonic-gate if ((tmp_fd = mkstemp(tmp_path)) == -1) {
200*0Sstevel@tonic-gate wbku_printerr("Error creating %s\n", tmp_path);
201*0Sstevel@tonic-gate return (B_FALSE);
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate * Need to reference this file as a stream.
206*0Sstevel@tonic-gate */
207*0Sstevel@tonic-gate if ((tmp_fp = fdopen(tmp_fd, "w")) == NULL) {
208*0Sstevel@tonic-gate wbku_printerr("Error opening %s", tmp_path);
209*0Sstevel@tonic-gate (void) close(tmp_fd);
210*0Sstevel@tonic-gate (void) unlink(tmp_path);
211*0Sstevel@tonic-gate return (B_FALSE);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate * Copy the existing keystore to the temp one, omitting the
216*0Sstevel@tonic-gate * key being deleted.
217*0Sstevel@tonic-gate */
218*0Sstevel@tonic-gate ret = wbku_delete_key(fp, tmp_fp, ka);
219*0Sstevel@tonic-gate (void) fclose(tmp_fp);
220*0Sstevel@tonic-gate if (ret != WBKU_SUCCESS) {
221*0Sstevel@tonic-gate wbku_printerr("%s\n", wbku_retmsg(ret));
222*0Sstevel@tonic-gate (void) unlink(tmp_path);
223*0Sstevel@tonic-gate return (B_FALSE);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate * Shuffle files.
228*0Sstevel@tonic-gate */
229*0Sstevel@tonic-gate if (rename(tmp_path, keystore_name) == -1) {
230*0Sstevel@tonic-gate wbku_printerr("Error moving new keystore file from %s to %s",
231*0Sstevel@tonic-gate tmp_path, keystore_name);
232*0Sstevel@tonic-gate (void) unlink(tmp_path);
233*0Sstevel@tonic-gate return (B_FALSE);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate return (B_TRUE);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate * This routine reads a key of type 'ka' from the file identified 'name' and
241*0Sstevel@tonic-gate * inserts it into the keystore named 'keystore_name'.
242*0Sstevel@tonic-gate *
243*0Sstevel@tonic-gate * Returns:
244*0Sstevel@tonic-gate * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
245*0Sstevel@tonic-gate */
246*0Sstevel@tonic-gate static int
process_insert(const char * keystore_name,const char * name,wbku_key_attr_t * ka)247*0Sstevel@tonic-gate process_insert(const char *keystore_name, const char *name,
248*0Sstevel@tonic-gate wbku_key_attr_t *ka)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate int fd;
251*0Sstevel@tonic-gate FILE *keystore_fp = NULL;
252*0Sstevel@tonic-gate FILE *fp;
253*0Sstevel@tonic-gate fpos_t pos;
254*0Sstevel@tonic-gate uint8_t rd_key[WANBOOT_MAXKEYLEN];
255*0Sstevel@tonic-gate int inlen;
256*0Sstevel@tonic-gate boolean_t newfile = B_TRUE;
257*0Sstevel@tonic-gate wbku_retcode_t ret;
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate * If the file already exists, then open the file for update.
261*0Sstevel@tonic-gate * Otherwise, create it and open it for writing.
262*0Sstevel@tonic-gate */
263*0Sstevel@tonic-gate fd = open(keystore_name, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
264*0Sstevel@tonic-gate if (fd < 0) {
265*0Sstevel@tonic-gate if (errno == EEXIST) {
266*0Sstevel@tonic-gate keystore_fp = fopen(keystore_name, "r+");
267*0Sstevel@tonic-gate newfile = B_FALSE;
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate } else {
270*0Sstevel@tonic-gate if ((keystore_fp = fdopen(fd, "w")) == NULL) {
271*0Sstevel@tonic-gate (void) close(fd);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate if (keystore_fp == NULL) {
276*0Sstevel@tonic-gate wbku_printerr("Cannot open %s", keystore_name);
277*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate /*
281*0Sstevel@tonic-gate * Open the input file.
282*0Sstevel@tonic-gate */
283*0Sstevel@tonic-gate fp = fopen(name, "r");
284*0Sstevel@tonic-gate if (fp == NULL) {
285*0Sstevel@tonic-gate wbku_printerr("Cannot open %s", name);
286*0Sstevel@tonic-gate (void) fclose(keystore_fp);
287*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate * Read the key from the file.
292*0Sstevel@tonic-gate */
293*0Sstevel@tonic-gate inlen = fread(rd_key, sizeof (uint8_t), ka->ka_maxlen, fp);
294*0Sstevel@tonic-gate if (inlen == 0 && ferror(fp) != 0) {
295*0Sstevel@tonic-gate wbku_printerr("Error reading %s", name);
296*0Sstevel@tonic-gate (void) fclose(fp);
297*0Sstevel@tonic-gate (void) fclose(keystore_fp);
298*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate (void) fclose(fp);
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate if ((inlen < ka->ka_minlen) || (inlen > ka->ka_maxlen)) {
303*0Sstevel@tonic-gate wbku_printerr("Key length is not valid\n");
304*0Sstevel@tonic-gate (void) fclose(keystore_fp);
305*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate * If the keystore exists, search for a key of the type
310*0Sstevel@tonic-gate * being inserted. If found, note its file position.
311*0Sstevel@tonic-gate */
312*0Sstevel@tonic-gate ret = WBKU_NOKEY;
313*0Sstevel@tonic-gate if (!newfile) {
314*0Sstevel@tonic-gate ret = wbku_find_key(keystore_fp, &pos, ka, NULL, B_FALSE);
315*0Sstevel@tonic-gate if (ret != WBKU_SUCCESS && ret != WBKU_NOKEY) {
316*0Sstevel@tonic-gate wbku_printerr("%s\n", wbku_retmsg(ret));
317*0Sstevel@tonic-gate (void) fclose(keystore_fp);
318*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate /*
322*0Sstevel@tonic-gate * Unfortuantely, RSA keys have variable lengths. If
323*0Sstevel@tonic-gate * the one being inserted is a different length than
324*0Sstevel@tonic-gate * than the one that already exists in the file, then
325*0Sstevel@tonic-gate * the key must be removed from the keystore and then
326*0Sstevel@tonic-gate * readded.
327*0Sstevel@tonic-gate */
328*0Sstevel@tonic-gate if (ret == WBKU_SUCCESS && inlen != ka->ka_len) {
329*0Sstevel@tonic-gate if (!compress_keystore(keystore_name,
330*0Sstevel@tonic-gate keystore_fp, ka)) {
331*0Sstevel@tonic-gate wbku_printerr("Insertion required compression"
332*0Sstevel@tonic-gate " of keystore, but compression failed\n"
333*0Sstevel@tonic-gate "Key was not inserted\n");
334*0Sstevel@tonic-gate (void) fclose(keystore_fp);
335*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate * The original keystore is history. Close the
340*0Sstevel@tonic-gate * stream and open a stream to the new keystore.
341*0Sstevel@tonic-gate */
342*0Sstevel@tonic-gate (void) fclose(keystore_fp);
343*0Sstevel@tonic-gate keystore_fp = fopen(keystore_name, "r+");
344*0Sstevel@tonic-gate if (keystore_fp == NULL) {
345*0Sstevel@tonic-gate wbku_printerr("Cannot open %s", keystore_name);
346*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate /* Force new key to end of file */
350*0Sstevel@tonic-gate ret = WBKU_NOKEY;
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate ka->ka_len = inlen;
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate * If wbku_find_key() did not find the key position for us,
357*0Sstevel@tonic-gate * then we should set position to the end of the file.
358*0Sstevel@tonic-gate */
359*0Sstevel@tonic-gate if (ret == WBKU_NOKEY && (fseek(keystore_fp, 0, SEEK_END) != 0 ||
360*0Sstevel@tonic-gate fgetpos(keystore_fp, &pos) != 0)) {
361*0Sstevel@tonic-gate wbku_printerr("Internal error");
362*0Sstevel@tonic-gate (void) fclose(keystore_fp);
363*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate
366*0Sstevel@tonic-gate /*
367*0Sstevel@tonic-gate * Write the key to the keystore.
368*0Sstevel@tonic-gate */
369*0Sstevel@tonic-gate ret = wbku_write_key(keystore_fp, &pos, ka, rd_key, B_FALSE);
370*0Sstevel@tonic-gate (void) fclose(keystore_fp);
371*0Sstevel@tonic-gate if (ret != WBKU_SUCCESS) {
372*0Sstevel@tonic-gate wbku_printerr("%s\n", wbku_retmsg(ret));
373*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate (void) printf(gettext("The client's %s key has been set\n"),
377*0Sstevel@tonic-gate ka->ka_str);
378*0Sstevel@tonic-gate /*
379*0Sstevel@tonic-gate * Success.
380*0Sstevel@tonic-gate */
381*0Sstevel@tonic-gate return (KEYMGMT_SUCCESS);
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate /*
385*0Sstevel@tonic-gate * Prints usage().
386*0Sstevel@tonic-gate */
387*0Sstevel@tonic-gate static void
usage(const char * cmd)388*0Sstevel@tonic-gate usage(const char *cmd)
389*0Sstevel@tonic-gate {
390*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s"
391*0Sstevel@tonic-gate " -i -k <key_file> -s <keystore> -o type=<%s|%s|%s|%s>\n"
392*0Sstevel@tonic-gate " %s -x -f <out_file> -s <keystore> -o"
393*0Sstevel@tonic-gate " type=<%s|%s|%s|%s>\n"),
394*0Sstevel@tonic-gate cmd, WBKU_KW_3DES, WBKU_KW_AES_128, WBKU_KW_HMAC_SHA1, WBKU_KW_RSA,
395*0Sstevel@tonic-gate cmd, WBKU_KW_3DES, WBKU_KW_AES_128, WBKU_KW_HMAC_SHA1, WBKU_KW_RSA);
396*0Sstevel@tonic-gate }
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gate /*
399*0Sstevel@tonic-gate * This program is used to insert and extract WAN boot encryption and
400*0Sstevel@tonic-gate * hash keys into and from keystores. The paths to the keystores are
401*0Sstevel@tonic-gate * provided by the user as are the input and output files.
402*0Sstevel@tonic-gate *
403*0Sstevel@tonic-gate * Note:
404*0Sstevel@tonic-gate * This program assumes all keys being inserted or extracted
405*0Sstevel@tonic-gate * are client keys. There is no way for a user to insert or
406*0Sstevel@tonic-gate * extract a master key using this program.
407*0Sstevel@tonic-gate *
408*0Sstevel@tonic-gate * We do not do any file locking scheme. This means that if two
409*0Sstevel@tonic-gate * keymgmt commands are run concurrently, results can be disastrous.
410*0Sstevel@tonic-gate *
411*0Sstevel@tonic-gate * Returns:
412*0Sstevel@tonic-gate * KEYMGMT_SUCCESS or KEYMGMT_ERROR.
413*0Sstevel@tonic-gate */
414*0Sstevel@tonic-gate int
main(int argc,char ** argv)415*0Sstevel@tonic-gate main(int argc, char **argv)
416*0Sstevel@tonic-gate {
417*0Sstevel@tonic-gate int c;
418*0Sstevel@tonic-gate boolean_t is_insert = B_FALSE;
419*0Sstevel@tonic-gate boolean_t is_extract = B_FALSE;
420*0Sstevel@tonic-gate char *keystore_name = NULL;
421*0Sstevel@tonic-gate char *filename = NULL;
422*0Sstevel@tonic-gate wbku_key_attr_t ka;
423*0Sstevel@tonic-gate int ret;
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate * Do the necessary magic for localization support.
427*0Sstevel@tonic-gate */
428*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
429*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
430*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
431*0Sstevel@tonic-gate #endif
432*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate /*
435*0Sstevel@tonic-gate * Initialize program name for use by wbku_printerr().
436*0Sstevel@tonic-gate */
437*0Sstevel@tonic-gate wbku_errinit(argv[0]);
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate /*
440*0Sstevel@tonic-gate * At the very least, we'll need one arg.
441*0Sstevel@tonic-gate */
442*0Sstevel@tonic-gate if (argc < 2) {
443*0Sstevel@tonic-gate usage(argv[0]);
444*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate /*
448*0Sstevel@tonic-gate * Parse the options.
449*0Sstevel@tonic-gate */
450*0Sstevel@tonic-gate ka.ka_type = WBKU_KEY_UNKNOWN;
451*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "ixf:k:s:o:")) != EOF) {
452*0Sstevel@tonic-gate switch (c) {
453*0Sstevel@tonic-gate case 'i':
454*0Sstevel@tonic-gate is_insert = B_TRUE;
455*0Sstevel@tonic-gate break;
456*0Sstevel@tonic-gate case 'x':
457*0Sstevel@tonic-gate is_extract = B_TRUE;
458*0Sstevel@tonic-gate break;
459*0Sstevel@tonic-gate case 'o':
460*0Sstevel@tonic-gate /*
461*0Sstevel@tonic-gate * Suboptions.
462*0Sstevel@tonic-gate */
463*0Sstevel@tonic-gate if (process_option(optarg, &ka) != KEYMGMT_SUCCESS) {
464*0Sstevel@tonic-gate usage(argv[0]);
465*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate break;
468*0Sstevel@tonic-gate case 's':
469*0Sstevel@tonic-gate /*
470*0Sstevel@tonic-gate * Keystore path.
471*0Sstevel@tonic-gate */
472*0Sstevel@tonic-gate keystore_name = optarg;
473*0Sstevel@tonic-gate break;
474*0Sstevel@tonic-gate case 'f':
475*0Sstevel@tonic-gate /*
476*0Sstevel@tonic-gate * Input file.
477*0Sstevel@tonic-gate */
478*0Sstevel@tonic-gate if (is_insert || filename != NULL) {
479*0Sstevel@tonic-gate usage(argv[0]);
480*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
481*0Sstevel@tonic-gate }
482*0Sstevel@tonic-gate filename = optarg;
483*0Sstevel@tonic-gate break;
484*0Sstevel@tonic-gate case 'k':
485*0Sstevel@tonic-gate /*
486*0Sstevel@tonic-gate * Input file.
487*0Sstevel@tonic-gate */
488*0Sstevel@tonic-gate if (is_extract || filename != NULL) {
489*0Sstevel@tonic-gate usage(argv[0]);
490*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
491*0Sstevel@tonic-gate }
492*0Sstevel@tonic-gate filename = optarg;
493*0Sstevel@tonic-gate break;
494*0Sstevel@tonic-gate default:
495*0Sstevel@tonic-gate usage(argv[0]);
496*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate }
499*0Sstevel@tonic-gate
500*0Sstevel@tonic-gate /*
501*0Sstevel@tonic-gate * Must be inserting or extracting a key and we must have a
502*0Sstevel@tonic-gate * key type, keystore filename and an input or output filename.
503*0Sstevel@tonic-gate */
504*0Sstevel@tonic-gate if ((is_insert == is_extract) || keystore_name == NULL ||
505*0Sstevel@tonic-gate filename == NULL || ka.ka_type == WBKU_KEY_UNKNOWN) {
506*0Sstevel@tonic-gate usage(argv[0]);
507*0Sstevel@tonic-gate return (KEYMGMT_ERROR);
508*0Sstevel@tonic-gate }
509*0Sstevel@tonic-gate
510*0Sstevel@tonic-gate /*
511*0Sstevel@tonic-gate * Insert or extract the key.
512*0Sstevel@tonic-gate */
513*0Sstevel@tonic-gate if (is_insert) {
514*0Sstevel@tonic-gate ret = process_insert(keystore_name, filename, &ka);
515*0Sstevel@tonic-gate } else {
516*0Sstevel@tonic-gate ret = process_extract(keystore_name, filename, &ka);
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate return (ret);
520*0Sstevel@tonic-gate }
521