Overhaul of key management (again).
[u/mdw/catacomb] / keyutil.c
1 /* -*-c-*-
2 *
3 * $Id: keyutil.c,v 1.5 2000/02/12 18:21:03 mdw Exp $
4 *
5 * Simple key manager program
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: keyutil.c,v $
33 * Revision 1.5 2000/02/12 18:21:03 mdw
34 * Overhaul of key management (again).
35 *
36 * Revision 1.4 1999/12/22 15:48:10 mdw
37 * Track new key-management changes. Support new key generation
38 * algorithms.
39 *
40 * Revision 1.3 1999/11/02 15:23:24 mdw
41 * Fix newlines in keyring list.
42 *
43 * Revision 1.2 1999/10/15 21:05:28 mdw
44 * In `key list', show timezone for local times, and support `-u' option
45 * for UTC output.
46 *
47 * Revision 1.1 1999/09/03 08:41:12 mdw
48 * Initial import.
49 *
50 */
51
52 /*----- Header files ------------------------------------------------------*/
53
54 #include "config.h"
55
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61
62 #include <mLib/base64.h>
63 #include <mLib/mdwopt.h>
64 #include <mLib/quis.h>
65 #include <mLib/report.h>
66 #include <mLib/sub.h>
67
68 #include <noise.h>
69 #include <rand.h>
70
71 #include "bbs.h"
72 #include "dh.h"
73 #include "dsa.h"
74 #include "fibrand.h"
75 #include "getdate.h"
76 #include "key.h"
77 #include "mp.h"
78 #include "mpmont.h"
79 #include "mprand.h"
80 #include "mptext.h"
81 #include "pgen.h"
82 #include "rsa.h"
83
84 /*----- Handy global state ------------------------------------------------*/
85
86 static const char *keyfile = "keyring";
87
88 /*----- Useful shared functions -------------------------------------------*/
89
90 /* --- @doopen@ --- *
91 *
92 * Arguments: @key_file *f@ = pointer to key file block
93 * @unsigned how@ = method to open file with
94 *
95 * Returns: ---
96 *
97 * Use: Opens a key file and handles errors by panicking
98 * appropriately.
99 */
100
101 static void doopen(key_file *f, unsigned how)
102 {
103 if (key_open(f, keyfile, how, key_moan, 0))
104 die(1, "couldn't open keyring `%s': %s", keyfile, strerror(errno));
105 }
106
107 /* --- @doclose@ --- *
108 *
109 * Arguments: @key_file *f@ = pointer to key file block
110 *
111 * Returns: ---
112 *
113 * Use: Closes a key file and handles errors by panicking
114 * appropriately.
115 */
116
117 static void doclose(key_file *f)
118 {
119 switch (key_close(f)) {
120 case KWRITE_FAIL:
121 die(EXIT_FAILURE, "couldn't write file `%s': %s",
122 keyfile, strerror(errno));
123 case KWRITE_BROKEN:
124 die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)",
125 keyfile, strerror(errno));
126 }
127 }
128
129 /* --- @setattr@ --- *
130 *
131 * Arguments: @key_file *f@ = pointer to key file block
132 * @key *k@ = pointer to key block
133 * @char *v[]@ = array of assignments (overwritten!)
134 *
135 * Returns: ---
136 *
137 * Use: Applies the attribute assignments to the key.
138 */
139
140 static void setattr(key_file *f, key *k, char *v[])
141 {
142 while (*v) {
143 int err;
144 char *p = *v;
145 size_t eq = strcspn(p, "=");
146 if (p[eq] == 0)
147 moan("invalid assignment: `%s'", p);
148 p[eq] = 0;
149 p += eq + 1;
150 if ((err = key_putattr(f, k, *v, *p ? p : 0)) != 0)
151 die(EXIT_FAILURE, "couldn't set attributes: %s", key_strerror(err));
152 v++;
153 }
154 }
155
156 /*----- Key generation ----------------------------------------------------*/
157
158 /* --- Key generation parameters --- */
159
160 typedef struct keyopts {
161 key_file *kf; /* Pointer to key file */
162 key *k; /* Pointer to the actual key */
163 dstr tag; /* Full tag name for the key */
164 unsigned f; /* Flags for the new key */
165 unsigned bits, qbits; /* Bit length for the new key */
166 key *p; /* Parameters key-data */
167 } keyopts;
168
169 enum {
170 f_bogus = 1, /* Error in parsing */
171 f_lock = 2, /* Passphrase-lock private key */
172 f_quiet = 4 /* Don't show a progress indicator */
173 };
174
175 /* --- @dolock@ --- *
176 *
177 * Arguments: @keyopts *k@ = key generation options
178 * @key_data *kd@ = pointer to key data to lock
179 * @const char *t@ = tag suffix or null
180 *
181 * Returns: ---
182 *
183 * Use: Does passphrase locking on new keys.
184 */
185
186 static void dolock(keyopts *k, key_data *kd, const char *t)
187 {
188 if (!(k->f & f_lock))
189 return;
190 if (t)
191 dstr_putf(&k->tag, ".%s", t);
192 if (key_plock(k->tag.buf, kd, kd))
193 die(EXIT_FAILURE, "couldn't lock key");
194 }
195
196 /* --- @mpkey@ --- *
197 *
198 * Arguments: @key_data *kd@ = pointer to parent key block
199 * @const char *tag@ = pointer to tag string
200 * @mp *m@ = integer to store
201 * @unsigned f@ = flags to set
202 *
203 * Returns: ---
204 *
205 * Use: Sets a multiprecision integer subkey.
206 */
207
208 static void mpkey(key_data *kd, const char *tag, mp *m, unsigned f)
209 {
210 key_data *kkd = key_structcreate(kd, tag);
211 key_mp(kkd, m);
212 kkd->e |= f;
213 }
214
215 /* --- @copyparam@ --- *
216 *
217 * Arguments: @keyopts *k@ = pointer to key options
218 * @const char **pp@ = checklist of parameters
219 *
220 * Returns: Nonzero if parameters copied; zero if you have to generate
221 * them.
222 *
223 * Use: Copies parameters from a source key to the current one.
224 */
225
226 static int copyparam(keyopts *k, const char **pp)
227 {
228 key_filter kf;
229
230 /* --- Quick check if no parameters supplied --- */
231
232 if (!k->p)
233 return (0);
234
235 /* --- Run through the checklist --- */
236
237 while (*pp) {
238 key_data *kd = key_structfind(&k->p->k, *pp);
239 if (!kd)
240 die(EXIT_FAILURE, "bad parameter key: parameter `%s' not found", *pp);
241 if ((kd->e & KF_CATMASK) != KCAT_SHARE)
242 die(EXIT_FAILURE, "bad parameter key: subkey `%s' is not shared", *pp);
243 pp++;
244 }
245
246 /* --- Copy over the parameters --- */
247
248 kf.f = KCAT_SHARE;
249 kf.m = KF_CATMASK;
250 if (!key_copy(&k->k->k, &k->p->k, &kf))
251 die(EXIT_FAILURE, "unexpected failure while copying parameters");
252 return (1);
253 }
254
255 /* --- @getmp@ --- *
256 *
257 * Arguments: @key_data *k@ = pointer to key data block
258 * @const char *tag@ = tag string to use
259 *
260 * Returns: Pointer to multiprecision integer key item.
261 *
262 * Use: Fetches an MP key component.
263 */
264
265 static mp *getmp(key_data *k, const char *tag)
266 {
267 k = key_structfind(k, tag);
268 if (!k)
269 die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag);
270 if ((k->e & KF_ENCMASK) != KENC_MP)
271 die(EXIT_FAILURE, "subkey `%s' has an incompatible type");
272 return (k->u.m);
273 }
274
275 /* --- @keyrand@ --- *
276 *
277 * Arguments: @key_file *kf@ = pointer to key file
278 * @const char *id@ = pointer to key id (or null)
279 *
280 * Returns: ---
281 *
282 * Use: Keys the random number generator.
283 */
284
285 static void keyrand(key_file *kf, const char *id)
286 {
287 key *k;
288
289 /* --- Find the key --- */
290
291 if (id) {
292 if ((k = key_bytag(kf, id)) == 0)
293 die(EXIT_FAILURE, "key `%s' not found", id);
294 } else
295 k = key_bytype(kf, "catacomb-rand");
296
297 if (k) {
298 key_data *kd = &k->k, kkd;
299
300 again:
301 switch (kd->e & KF_ENCMASK) {
302 case KENC_BINARY:
303 break;
304 case KENC_ENCRYPT: {
305 dstr d = DSTR_INIT;
306 key_fulltag(k, &d);
307 if (key_punlock(d.buf, kd, &kkd))
308 die(EXIT_FAILURE, "error unlocking key `%s'", d.buf);
309 dstr_destroy(&d);
310 kd = &kkd;
311 } goto again;
312 default: {
313 dstr d = DSTR_INIT;
314 key_fulltag(k, &d);
315 die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf);
316 } break;
317 }
318
319 /* --- Key the generator --- */
320
321 rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz);
322 if (kd == &kkd)
323 key_destroy(&kkd);
324 }
325 }
326
327 /* --- Key generation algorithms --- */
328
329 static void alg_binary(keyopts *k)
330 {
331 unsigned sz;
332 unsigned m;
333 octet *p;
334
335 if (!k->bits)
336 k->bits = 128;
337 if (k->p)
338 die(EXIT_FAILURE, "no shared parameters for binary keys");
339
340 sz = (k->bits + 7) >> 3;
341 p = sub_alloc(sz);
342 m = (1 << (((k->bits - 1) & 7) + 1)) - 1;
343 rand_getgood(RAND_GLOBAL, p, sz);
344 *p &= m;
345 key_binary(&k->k->k, p, sz);
346 k->k->k.e |= KCAT_SYMM | KF_BURN;
347 memset(p, 0, sz);
348 sub_free(p, sz);
349 dolock(k, &k->k->k, 0);
350 }
351
352 static void alg_des(keyopts *k)
353 {
354 unsigned sz;
355 octet *p;
356 int i;
357
358 if (!k->bits)
359 k->bits = 112;
360 if (k->p)
361 die(EXIT_FAILURE, "no shared parameters for DES keys");
362 if (k->bits % 56 || k->bits > 168)
363 die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long");
364
365 sz = k->bits / 7;
366 p = sub_alloc(sz);
367 rand_getgood(RAND_GLOBAL, p, sz); /* Too much work done here! */
368 for (i = 0; i < sz; i++) {
369 octet x = p[i] | 0x01;
370 x = x ^ (x >> 4);
371 x = x ^ (x >> 2);
372 x = x ^ (x >> 1);
373 p[i] = (p[i] & 0xfe) | (x & 0x01);
374 }
375 key_binary(&k->k->k, p, sz);
376 k->k->k.e |= KCAT_SYMM | KF_BURN;
377 memset(p, 0, sz);
378 sub_free(p, sz);
379 dolock(k, &k->k->k, 0);
380 }
381
382 static void alg_rsa(keyopts *k)
383 {
384 rsa_param rp;
385 key_data *kd;
386
387 /* --- Sanity checking --- */
388
389 if (k->p)
390 die(EXIT_FAILURE, "no shared parameters for RSA keys");
391 if (!k->bits)
392 k->bits = 1024;
393
394 /* --- Generate the RSA parameters --- */
395
396 if (rsa_gen(&rp, k->bits, &rand_global, 0,
397 (k->f & f_quiet) ? 0 : pgen_ev, 0))
398 die(EXIT_FAILURE, "RSA key generation failed");
399
400 /* --- Run a test encryption --- */
401
402 {
403 grand *g = fibrand_create(rand_global.ops->word(&rand_global));
404 mpmont mm;
405 mp *m = mprand_range(MP_NEW, rp.n, g, 0);
406 mp *c;
407
408 /* --- Encrypt the plaintext --- */
409
410 mpmont_create(&mm, rp.n);
411 c = mpmont_exp(&mm, MP_NEW, m, rp.e);
412 mpmont_destroy(&mm);
413
414 /* --- Decrypt the ciphertext --- */
415
416 c = rsa_decrypt(&rp, c, c, g);
417
418 /* --- Check everything went OK --- */
419
420 if (MP_CMP(c, !=, m))
421 die(EXIT_FAILURE, "test encryption failed");
422 mp_drop(c);
423 mp_drop(m);
424 g->ops->destroy(g);
425 }
426
427 /* --- Allrighty then --- */
428
429 kd = &k->k->k;
430 key_structure(kd);
431 mpkey(kd, "n", rp.n, KCAT_PUB);
432 mpkey(kd, "e", rp.e, KCAT_PUB);
433
434 kd = key_structcreate(kd, "private");
435 key_structure(kd);
436 mpkey(kd, "d", rp.d, KCAT_PRIV | KF_BURN);
437 mpkey(kd, "p", rp.p, KCAT_PRIV | KF_BURN);
438 mpkey(kd, "q", rp.q, KCAT_PRIV | KF_BURN);
439 mpkey(kd, "q-inv", rp.q_inv, KCAT_PRIV | KF_BURN);
440 mpkey(kd, "d-mod-p", rp.dp, KCAT_PRIV | KF_BURN);
441 mpkey(kd, "d-mod-q", rp.dq, KCAT_PRIV | KF_BURN);
442 dolock(k, kd, "private");
443
444 mp_drop(rp.p); mp_drop(rp.q); mp_drop(rp.n); mp_drop(rp.q_inv);
445 mp_drop(rp.e); mp_drop(rp.d); mp_drop(rp.dp); mp_drop(rp.dq);
446 }
447
448 static void alg_dsaparam(keyopts *k)
449 {
450 static const char *pl[] = { "q", "p", "g", 0 };
451 if (!copyparam(k, pl)) {
452 dsa_param dp;
453 octet *p;
454 size_t sz;
455 dstr d = DSTR_INIT;
456 base64_ctx c;
457 key_data *kd = &k->k->k;
458
459 /* --- Choose appropriate bit lengths if necessary --- */
460
461 if (!k->qbits)
462 k->qbits = 160;
463 if (!k->bits)
464 k->bits = 768;
465
466 /* --- Allocate a seed block --- */
467
468 sz = (k->qbits + 7) >> 3;
469 p = sub_alloc(sz);
470 rand_getgood(RAND_GLOBAL, p, sz);
471
472 /* --- Allocate the parameters --- */
473
474 if (dsa_seed(&dp, k->qbits, k->bits, 0, p, sz,
475 (k->f & f_quiet) ? 0 : pgen_ev, 0))
476 die(EXIT_FAILURE, "DSA parameter generation failed");
477
478 /* --- Store the parameters --- */
479
480 key_structure(kd);
481 mpkey(kd, "q", dp.q, KCAT_SHARE);
482 mpkey(kd, "p", dp.p, KCAT_SHARE);
483 mpkey(kd, "g", dp.g, KCAT_SHARE);
484 mp_drop(dp.q);
485 mp_drop(dp.p);
486 mp_drop(dp.g);
487
488 /* --- Store the seed for future verification --- */
489
490 base64_init(&c);
491 c.maxline = 0;
492 c.indent = "";
493 base64_encode(&c, p, sz, &d);
494 base64_encode(&c, 0, 0, &d);
495 key_putattr(k->kf, k->k, "seed", d.buf);
496 sub_free(p, sz);
497 dstr_destroy(&d);
498 }
499 }
500
501 static void alg_dsa(keyopts *k)
502 {
503 mp *q, *p, *g;
504 mp *x, *y;
505 mpmont mm;
506 key_data *kd = &k->k->k;
507
508 /* --- Get the shared parameters --- */
509
510 alg_dsaparam(k);
511 q = getmp(kd, "q");
512 p = getmp(kd, "p");
513 g = getmp(kd, "g");
514
515 /* --- Choose a private key --- */
516
517 x = mprand_range(MP_NEW, q, &rand_global, 0);
518 mp_burn(x);
519 mpmont_create(&mm, p);
520 y = mpmont_exp(&mm, MP_NEW, g, x);
521
522 /* --- Store everything away --- */
523
524 mpkey(kd, "y", y, KCAT_PUB);
525
526 kd = key_structcreate(kd, "private");
527 key_structure(kd);
528 mpkey(kd, "x", x, KCAT_PRIV | KF_BURN);
529 dolock(k, kd, "private");
530
531 mp_drop(x); mp_drop(y);
532 }
533
534 static void alg_dhparam(keyopts *k)
535 {
536 static const char *pl[] = { "p", "q", "g", 0 };
537 if (!copyparam(k, pl)) {
538 dh_param dp;
539 key_data *kd = &k->k->k;
540
541 if (!k->bits)
542 k->bits = 1024;
543
544 /* --- Choose a large safe prime number --- */
545
546 if (dh_gen(&dp, k->qbits, k->bits, 0, &rand_global,
547 (k->f & f_quiet) ? 0 : pgen_ev, 0))
548 die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed");
549
550 key_structure(kd);
551 mpkey(kd, "p", dp.p, KCAT_SHARE);
552 mpkey(kd, "q", dp.q, KCAT_SHARE);
553 mpkey(kd, "g", dp.g, KCAT_SHARE);
554 mp_drop(dp.q);
555 mp_drop(dp.p);
556 mp_drop(dp.g);
557 }
558 }
559
560 static void alg_dh(keyopts *k)
561 {
562 mp *x, *y;
563 mp *p, *q, *g;
564 mpmont mm;
565 key_data *kd = &k->k->k;
566
567 /* --- Get the shared parameters --- */
568
569 alg_dhparam(k);
570 p = getmp(kd, "p");
571 q = getmp(kd, "q");
572 g = getmp(kd, "g");
573
574 /* --- Choose a suitable private key --- *
575 *
576 * Since %$g$% has order %$q$%, choose %$x < q$%.
577 */
578
579 x = mprand_range(MP_NEW, q, &rand_global, 0);
580 mp_burn(x);
581
582 /* --- Compute the public key %$y = g^x \bmod p$% --- */
583
584 mpmont_create(&mm, p);
585 y = mpmont_exp(&mm, MP_NEW, g, x);
586 mpmont_destroy(&mm);
587
588 /* --- Store everything away --- */
589
590 mpkey(kd, "y", y, KCAT_PUB);
591
592 kd = key_structcreate(kd, "private");
593 key_structure(kd);
594 mpkey(kd, "x", x, KCAT_PRIV | KF_BURN);
595 dolock(k, kd, "private");
596
597 mp_drop(x); mp_drop(y);
598 }
599
600 static void alg_bbs(keyopts *k)
601 {
602 bbs_param bp;
603 key_data *kd;
604
605 /* --- Sanity checking --- */
606
607 if (k->p)
608 die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys");
609 if (!k->bits)
610 k->bits = 1024;
611
612 /* --- Generate the BBS parameters --- */
613
614 if (bbs_gen(&bp, k->bits, &rand_global, 0,
615 (k->f & f_quiet) ? 0 : pgen_ev, 0))
616 die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed");
617
618 /* --- Allrighty then --- */
619
620 kd = &k->k->k;
621 key_structure(kd);
622 mpkey(kd, "n", bp.n, KCAT_PUB);
623
624 kd = key_structcreate(kd, "private");
625 key_structure(kd);
626 mpkey(kd, "p", bp.p, KCAT_PRIV | KF_BURN);
627 mpkey(kd, "q", bp.q, KCAT_PRIV | KF_BURN);
628 dolock(k, kd, "private");
629
630 mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n);
631 }
632
633 /* --- The algorithm tables --- */
634
635 typedef struct keyalg {
636 const char *name;
637 void (*proc)(keyopts *o);
638 const char *help;
639 } keyalg;
640
641 static keyalg algtab[] = {
642 { "binary", alg_binary, "Plain binary data" },
643 { "des", alg_des, "Binary with DES-style parity" },
644 { "rsa", alg_rsa, "RSA public-key encryption" },
645 { "dsa", alg_dsa, "DSA digital signatures" },
646 { "dsa-param", alg_dsaparam, "DSA shared parameters" },
647 { "dh", alg_dh, "Diffie-Hellman key exchange" },
648 { "dh-param", alg_dhparam, "Diffie-Hellman parameters" },
649 { "bbs", alg_bbs, "Blum-Blum-Shub generator" },
650 { 0, 0 }
651 };
652
653 /* --- @cmd_add@ --- */
654
655 static int cmd_add(int argc, char *argv[])
656 {
657 key_file f;
658 time_t exp = KEXP_EXPIRE;
659 const char *tag = 0, *ptag = 0;
660 const char *c = 0;
661 keyalg *alg = algtab;
662 const char *rtag = 0;
663 keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0 };
664
665 /* --- Parse options for the subcommand --- */
666
667 for (;;) {
668 static struct option opt[] = {
669 { "algorithm", OPTF_ARGREQ, 0, 'a' },
670 { "bits", OPTF_ARGREQ, 0, 'b' },
671 { "qbits", OPTF_ARGREQ, 0, 'B' },
672 { "parameters", OPTF_ARGREQ, 0, 'p' },
673 { "expire", OPTF_ARGREQ, 0, 'e' },
674 { "comment", OPTF_ARGREQ, 0, 'c' },
675 { "tag", OPTF_ARGREQ, 0, 't' },
676 { "rand-id", OPTF_ARGREQ, 0, 'r' },
677 { "lock", 0, 0, 'l' },
678 { "quiet", 0, 0, 'q' },
679 { 0, 0, 0, 0 }
680 };
681 int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:r:lq", opt, 0, 0, 0);
682 if (i < 0)
683 break;
684
685 /* --- Handle the various options --- */
686
687 switch (i) {
688
689 /* --- Read an algorithm name --- */
690
691 case 'a': {
692 keyalg *a;
693 size_t sz = strlen(optarg);
694
695 if (strcmp(optarg, "list") == 0) {
696 for (a = algtab; a->name; a++)
697 printf("%-10s %s\n", a->name, a->help);
698 return (0);
699 }
700
701 alg = 0;
702 for (a = algtab; a->name; a++) {
703 if (strncmp(optarg, a->name, sz) == 0) {
704 if (a->name[sz] == 0) {
705 alg = a;
706 break;
707 } else if (alg)
708 die(EXIT_FAILURE, "ambiguous algorithm name `%s'", optarg);
709 else
710 alg = a;
711 }
712 }
713 if (!alg)
714 die(EXIT_FAILURE, "unknown algorithm name `%s'", optarg);
715 } break;
716
717 /* --- Bits must be nonzero and a multiple of 8 --- */
718
719 case 'b': {
720 char *p;
721 k.bits = strtoul(optarg, &p, 0);
722 if (k.bits == 0 || *p != 0)
723 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
724 } break;
725
726 case 'B': {
727 char *p;
728 k.qbits = strtoul(optarg, &p, 0);
729 if (k.qbits == 0 || *p != 0)
730 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
731 } break;
732
733 /* --- Parameter selection --- */
734
735 case 'p':
736 ptag = optarg;
737 break;
738
739 /* --- Expiry dates get passed to @get_date@ for parsing --- */
740
741 case 'e':
742 if (strncmp(optarg, "forever", strlen(optarg)) == 0)
743 exp = KEXP_FOREVER;
744 else {
745 exp = get_date(optarg, 0);
746 if (exp == -1)
747 die(EXIT_FAILURE, "bad expiry date `%s'", optarg);
748 }
749 break;
750
751 /* --- Store comments without interpretation --- */
752
753 case 'c':
754 if (key_chkcomment(optarg))
755 die(EXIT_FAILURE, "bad comment string `%s'", optarg);
756 c = optarg;
757 break;
758
759 /* --- Store tags --- */
760
761 case 't':
762 if (key_chkident(optarg))
763 die(EXIT_FAILURE, "bad tag string `%s'", optarg);
764 tag = optarg;
765 break;
766
767 /* --- Other flags --- */
768
769 case 'r':
770 rtag = optarg;
771 break;
772 case 'l':
773 k.f |= f_lock;
774 break;
775 case 'q':
776 k.f |= f_quiet;
777 break;
778
779 /* --- Other things are bogus --- */
780
781 default:
782 k.f |= f_bogus;
783 break;
784 }
785 }
786
787 /* --- Various sorts of bogosity --- */
788
789 if ((k.f & f_bogus) || optind + 1 > argc) {
790 die(EXIT_FAILURE,
791 "Usage: add [options] type [attr...]");
792 }
793 if (key_chkident(argv[optind]))
794 die(EXIT_FAILURE, "bad key type `%s'", argv[optind]);
795
796 /* --- Set up various bits of the state --- */
797
798 if (exp == KEXP_EXPIRE)
799 exp = time(0) + 14 * 24 * 60 * 60;
800
801 /* --- Open the file and create the basic key block --- *
802 *
803 * Keep on generating keyids until one of them doesn't collide.
804 */
805
806 doopen(&f, KOPEN_WRITE);
807 k.kf = &f;
808
809 /* --- Key the generator --- */
810
811 keyrand(&f, rtag);
812
813 for (;;) {
814 uint32 id = rand_global.ops->word(&rand_global);
815 int err;
816 k.k = key_new(&f, id, argv[optind], exp, &err);
817 if (k.k)
818 break;
819 if (err != KERR_DUPID)
820 die(EXIT_FAILURE, "error adding new key: %s", key_strerror(err));
821 }
822
823 /* --- Set various simple attributes --- */
824
825 if (tag) {
826 int err = key_settag(&f, k.k, tag);
827 if (err)
828 die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err));
829 }
830
831 if (c) {
832 int err = key_setcomment(&f, k.k, c);
833 if (err)
834 die(EXIT_FAILURE, "error setting key comment: %s", key_strerror(err));
835 }
836
837 setattr(&f, k.k, argv + optind + 1);
838
839 key_fulltag(k.k, &k.tag);
840
841 /* --- Find the parameter key --- */
842
843 if (ptag) {
844 if ((k.p = key_bytag(&f, ptag)) == 0)
845 die(EXIT_FAILURE, "parameter key `%s' not found", ptag);
846 if ((k.p->k.e & KF_ENCMASK) != KENC_STRUCT)
847 die(EXIT_FAILURE, "parameter key `%s' is not structured", ptag);
848 }
849
850 /* --- Now generate the actual key data --- */
851
852 alg->proc(&k);
853
854 /* --- Done --- */
855
856 doclose(&f);
857 return (0);
858 }
859
860 /*----- Key listing -------------------------------------------------------*/
861
862 /* --- Listing options --- */
863
864 typedef struct listopts {
865 const char *tfmt; /* Date format (@strftime@-style) */
866 int v; /* Verbosity level */
867 unsigned f; /* Various flags */
868 time_t t; /* Time now (for key expiry) */
869 key_filter kf; /* Filter for matching keys */
870 } listopts;
871
872 /* --- Listing flags --- */
873
874 enum {
875 f_newline = 2, /* Write newline before next entry */
876 f_attr = 4, /* Written at least one attribute */
877 f_utc = 8 /* Emit UTC time, not local time */
878 };
879
880 /* --- @showkeydata@ --- *
881 *
882 * Arguments: @key_data *k@ = pointer to key to write
883 * @int ind@ = indentation level
884 * @listopts *o@ = listing options
885 * @dstr *d@ = tag string for this subkey
886 *
887 * Returns: ---
888 *
889 * Use: Emits a piece of key data in a human-readable format.
890 */
891
892 static void showkeydata(key_data *k, int ind, listopts *o, dstr *d)
893 {
894 #define INDENT(i) do { \
895 int _i; \
896 for (_i = 0; _i < (i); _i++) { \
897 putchar(' '); \
898 } \
899 } while (0)
900
901 switch (k->e & KF_ENCMASK) {
902
903 /* --- Binary key data --- *
904 *
905 * Emit as a simple hex dump.
906 */
907
908 case KENC_BINARY: {
909 const octet *p = k->u.k.k;
910 const octet *l = p + k->u.k.sz;
911 size_t sz = 0;
912
913 fputs(" {", stdout);
914 while (p < l) {
915 if (sz % 16 == 0) {
916 putchar('\n');
917 INDENT(ind + 2);
918 } else if (sz % 8 == 0)
919 fputs(" ", stdout);
920 else
921 putc(' ', stdout);
922 printf("%02x", *p++);
923 sz++;
924 }
925 putchar('\n');
926 INDENT(ind);
927 fputs("}\n", stdout);
928 } break;
929
930 /* --- Encrypted data --- *
931 *
932 * If the user is sufficiently keen, ask for a passphrase and decrypt the
933 * key. Otherwise just say that it's encrypted and move on.
934 */
935
936 case KENC_ENCRYPT:
937 if (o->v <= 3)
938 fputs(" encrypted\n", stdout);
939 else {
940 key_data kd;
941 if (key_punlock(d->buf, k, &kd))
942 printf(" <failed to unlock %s>\n", d->buf);
943 else {
944 fputs(" encrypted", stdout);
945 showkeydata(&kd, ind, o, d);
946 key_destroy(&kd);
947 }
948 }
949 break;
950
951 /* --- Integer keys --- *
952 *
953 * Emit as a large integer in decimal. This makes using the key in
954 * `calc' or whatever easier.
955 */
956
957 case KENC_MP:
958 putchar(' ');
959 mp_writefile(k->u.m, stdout, 10);
960 putchar('\n');
961 break;
962
963 /* --- Structured keys --- *
964 *
965 * Just iterate over the subkeys.
966 */
967
968 case KENC_STRUCT: {
969 sym_iter i;
970 key_struct *ks;
971 size_t n = d->len;
972
973 fputs(" {\n", stdout);
974 for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
975 if (!key_match(&ks->k, &o->kf))
976 continue;
977 INDENT(ind + 2);
978 printf("%s =", SYM_NAME(ks));
979 d->len = n;
980 dstr_putf(d, ".%s", SYM_NAME(ks));
981 showkeydata(&ks->k, ind + 2, o, d);
982 }
983 INDENT(ind);
984 fputs("}\n", stdout);
985 } break;
986 }
987
988 #undef INDENT
989 }
990
991 /* --- @showkey@ --- *
992 *
993 * Arguments: @key *k@ = pointer to key to show
994 * @listopts *o@ = pointer to listing options
995 *
996 * Returns: ---
997 *
998 * Use: Emits a listing of a particular key.
999 */
1000
1001 static void showkey(key *k, listopts *o)
1002 {
1003 char ebuf[24], dbuf[24];
1004 struct tm *tm;
1005
1006 /* --- Skip the key if the filter doesn't match --- */
1007
1008 if (!key_match(&k->k, &o->kf))
1009 return;
1010
1011 /* --- Sort out the expiry and deletion times --- */
1012
1013 if (KEY_EXPIRED(o->t, k->exp))
1014 strcpy(ebuf, "expired");
1015 else if (k->exp == KEXP_FOREVER)
1016 strcpy(ebuf, "forever");
1017 else {
1018 tm = (o->f & f_utc) ? gmtime(&k->exp) : localtime(&k->exp);
1019 strftime(ebuf, sizeof(ebuf), o->tfmt, tm);
1020 }
1021
1022 if (KEY_EXPIRED(o->t, k->del))
1023 strcpy(dbuf, "deleted");
1024 else if (k->del == KEXP_FOREVER)
1025 strcpy(dbuf, "forever");
1026 else {
1027 tm = (o->f & f_utc) ? gmtime(&k->del) : localtime(&k->del);
1028 strftime(dbuf, sizeof(dbuf), o->tfmt, tm);
1029 }
1030
1031 /* --- If in compact format, just display and quit --- */
1032
1033 if (!o->v) {
1034 if (!(o->f & f_newline)) {
1035 printf("%8s %-20s %-20s %-10s %-10s\n",
1036 "Id", "Tag", "Type", "Expire", "Delete");
1037 }
1038 printf("%08lx %-20s %-20s %-10s %-10s\n",
1039 (unsigned long)k->id, k->tag ? k->tag : "<none>",
1040 k->type, ebuf, dbuf);
1041 o->f |= f_newline;
1042 return;
1043 }
1044
1045 /* --- Display the standard header --- */
1046
1047 if (o->f & f_newline)
1048 fputc('\n', stdout);
1049 printf("keyid: %08lx\n", (unsigned long)k->id);
1050 printf("tag: %s\n", k->tag ? k->tag : "<none>");
1051 printf("type: %s\n", k->type);
1052 printf("expiry: %s\n", ebuf);
1053 printf("delete: %s\n", dbuf);
1054 printf("comment: %s\n", k->c ? k->c : "<none>");
1055
1056 /* --- Display the attributes --- */
1057
1058 if (o->v > 1) {
1059 key_attriter i;
1060 const char *av, *an;
1061
1062 o->f &= ~f_attr;
1063 printf("attributes:");
1064 for (key_mkattriter(&i, k); key_nextattr(&i, &an, &av); ) {
1065 printf("\n\t%s = %s", an, av);
1066 o->f |= f_attr;
1067 }
1068 if (o->f & f_attr)
1069 fputc('\n', stdout);
1070 else
1071 puts(" <none>");
1072 }
1073
1074 /* --- If dumping requested, dump the raw key data --- */
1075
1076 if (o->v > 2) {
1077 dstr d = DSTR_INIT;
1078 fputs("key:", stdout);
1079 key_fulltag(k, &d);
1080 showkeydata(&k->k, 0, o, &d);
1081 dstr_destroy(&d);
1082 }
1083
1084 o->f |= f_newline;
1085 }
1086
1087 /* --- @cmd_list@ --- */
1088
1089 static int cmd_list(int argc, char *argv[])
1090 {
1091 key_file f;
1092 key *k;
1093 listopts o = { 0, 0, 0, 0, { 0, 0 } };
1094
1095 /* --- Parse subcommand options --- */
1096
1097 for (;;) {
1098 static struct option opt[] = {
1099 { "quiet", 0, 0, 'q' },
1100 { "verbose", 0, 0, 'v' },
1101 { "utc", 0, 0, 'u' },
1102 { "filter", OPTF_ARGREQ, 0, 'f' },
1103 { 0, 0, 0, 0 }
1104 };
1105 int i = mdwopt(argc, argv, "+uqvf:", opt, 0, 0, 0);
1106 if (i < 0)
1107 break;
1108
1109 switch (i) {
1110 case 'u':
1111 o.f |= f_utc;
1112 break;
1113 case 'q':
1114 if (o.v)
1115 o.v--;
1116 break;
1117 case 'v':
1118 o.v++;
1119 break;
1120 case 'f': {
1121 char *p;
1122 int e = key_readflags(optarg, &p, &o.kf.f, &o.kf.m);
1123 if (e || *p)
1124 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1125 } break;
1126 default:
1127 o.f |= f_bogus;
1128 break;
1129 }
1130 }
1131
1132 if (o.f & f_bogus)
1133 die(EXIT_FAILURE, "Usage: list [-uqv] [-f filter] [tag...]");
1134
1135 /* --- Open the key file --- */
1136
1137 doopen(&f, KOPEN_READ);
1138 o.t = time(0);
1139
1140 /* --- Set up the time format --- */
1141
1142 if (!o.v)
1143 o.tfmt = "%Y-%m-%d";
1144 else if (o.f & f_utc)
1145 o.tfmt = "%Y-%m-%d %H:%M:%S UTC";
1146 else
1147 o.tfmt = "%Y-%m-%d %H:%M:%S %Z";
1148
1149 /* --- If specific keys were requested use them, otherwise do all --- *
1150 *
1151 * Some day, this might turn into a wildcard match.
1152 */
1153
1154 if (optind < argc) {
1155 do {
1156 if ((k = key_bytag(&f, argv[optind])) != 0)
1157 showkey(k, &o);
1158 else {
1159 moan("key `%s' not found", argv[optind]);
1160 o.f |= f_bogus;
1161 }
1162 optind++;
1163 } while (optind < argc);
1164 } else {
1165 key_iter i;
1166 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1167 showkey(k, &o);
1168 }
1169
1170 /* --- Done --- */
1171
1172 doclose(&f);
1173 if (o.f & f_bogus)
1174 return (EXIT_FAILURE);
1175 else
1176 return (0);
1177 }
1178
1179 /*----- Command implementation --------------------------------------------*/
1180
1181 /* --- @cmd_expire@ --- */
1182
1183 static int cmd_expire(int argc, char *argv[])
1184 {
1185 key_file f;
1186 key *k;
1187 int i;
1188 int rc = 0;
1189
1190 if (argc < 2)
1191 die(EXIT_FAILURE, "Usage: expire tag...");
1192 doopen(&f, KOPEN_WRITE);
1193 for (i = 1; i < argc; i++) {
1194 if ((k = key_bytag(&f, argv[i])) != 0)
1195 key_expire(&f, k);
1196 else {
1197 moan("key `%s' not found", argv[i]);
1198 rc = 1;
1199 }
1200 }
1201 doclose(&f);
1202 return (rc);
1203 }
1204
1205 /* --- @cmd_delete@ --- */
1206
1207 static int cmd_delete(int argc, char *argv[])
1208 {
1209 key_file f;
1210 key *k;
1211 int i;
1212 int rc = 0;
1213
1214 if (argc < 2)
1215 die(EXIT_FAILURE, "Usage: delete tag...");
1216 doopen(&f, KOPEN_WRITE);
1217 for (i = 1; i < argc; i++) {
1218 if ((k = key_bytag(&f, argv[i])) != 0)
1219 key_delete(&f, k);
1220 else {
1221 moan("key `%s' not found", argv[i]);
1222 rc = 1;
1223 }
1224 }
1225 doclose(&f);
1226 return (rc);
1227 }
1228
1229 /* --- @cmd_setattr@ --- */
1230
1231 static int cmd_setattr(int argc, char *argv[])
1232 {
1233 key_file f;
1234 key *k;
1235
1236 if (argc < 3)
1237 die(EXIT_FAILURE, "Usage: setattr tag attr...");
1238 doopen(&f, KOPEN_WRITE);
1239 if ((k = key_bytag(&f, argv[1])) == 0)
1240 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1241 setattr(&f, k, argv + 2);
1242 doclose(&f);
1243 return (0);
1244 }
1245
1246 /* --- @cmd_finger@ --- */
1247
1248 static void fingerprint(key *k, const key_filter *kf)
1249 {
1250 rmd160_ctx r;
1251 octet hash[RMD160_HASHSZ];
1252 dstr d = DSTR_INIT;
1253 int i;
1254
1255 if (!key_encode(&k->k, &d, kf))
1256 return;
1257 rmd160_init(&r);
1258 rmd160_hash(&r, d.buf, d.len);
1259 rmd160_done(&r, hash);
1260
1261 DRESET(&d);
1262 key_fulltag(k, &d);
1263 for (i = 0; i < sizeof(hash); i++) {
1264 if (i && i % 4 == 0)
1265 putchar('-');
1266 printf("%02x", hash[i]);
1267 }
1268 printf(" %s\n", d.buf);
1269 dstr_destroy(&d);
1270 }
1271
1272 static int cmd_finger(int argc, char *argv[])
1273 {
1274 key_file f;
1275 int rc = 0;
1276 key_filter kf = { KF_NONSECRET, KF_NONSECRET };
1277
1278 for (;;) {
1279 static struct option opt[] = {
1280 { "filter", OPTF_ARGREQ, 0, 'f' },
1281 { 0, 0, 0, 0 }
1282 };
1283 int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0);
1284 if (i < 0)
1285 break;
1286 switch (i) {
1287 case 'f': {
1288 char *p;
1289 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1290 if (err || *p)
1291 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1292 } break;
1293 default:
1294 rc = 1;
1295 break;
1296 }
1297 }
1298
1299 argv += optind; argc -= optind;
1300 if (rc)
1301 die(EXIT_FAILURE, "Usage: fingerprint [-f filter] [tag...]");
1302
1303 doopen(&f, KOPEN_READ);
1304
1305 if (argc) {
1306 int i;
1307 for (i = 0; i < argc; i++) {
1308 key *k = key_bytag(&f, argv[i]);
1309 if (k)
1310 fingerprint(k, &kf);
1311 else {
1312 rc = 1;
1313 moan("key `%s' not found", argv[i]);
1314 }
1315 }
1316 } else {
1317 key_iter i;
1318 key *k;
1319 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1320 fingerprint(k, &kf);
1321 }
1322 return (rc);
1323 }
1324
1325 /* --- @cmd_comment@ --- */
1326
1327 static int cmd_comment(int argc, char *argv[])
1328 {
1329 key_file f;
1330 key *k;
1331 int err;
1332
1333 if (argc < 2 || argc > 3)
1334 die(EXIT_FAILURE, "Usage: comment tag [comment]");
1335 doopen(&f, KOPEN_WRITE);
1336 if ((k = key_bytag(&f, argv[1])) == 0)
1337 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1338 if ((err = key_setcomment(&f, k, argv[2])) != 0)
1339 die(EXIT_FAILURE, "bad comment `%s': %s", argv[2], key_strerror(err));
1340 doclose(&f);
1341 return (0);
1342 }
1343
1344 /* --- @cmd_tag@ --- */
1345
1346 static int cmd_tag(int argc, char *argv[])
1347 {
1348 key_file f;
1349 key *k;
1350 int err;
1351
1352 if (argc < 2 || argc > 3)
1353 die(EXIT_FAILURE, "Usage: tag tag [new-tag]");
1354 doopen(&f, KOPEN_WRITE);
1355 if ((k = key_bytag(&f, argv[1])) == 0)
1356 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1357 if ((err = key_settag(&f, k, argv[2])) != 0)
1358 die(EXIT_FAILURE, "bad tag `%s': %s", argv[2], key_strerror(err));
1359 doclose(&f);
1360 return (0);
1361 }
1362
1363 /* --- @cmd_lock@ --- */
1364
1365 static int cmd_lock(int argc, char *argv[])
1366 {
1367 key_file f;
1368 key *k;
1369 key_data *kd;
1370 dstr d = DSTR_INIT;
1371
1372 if (argc != 2)
1373 die(EXIT_FAILURE, "Usage: lock qtag");
1374 doopen(&f, KOPEN_WRITE);
1375 if (key_qtag(&f, argv[1], &d, &k, &kd))
1376 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1377 if (kd->e == KENC_ENCRYPT && key_punlock(d.buf, kd, kd))
1378 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1379 if (key_plock(d.buf, kd, kd))
1380 die(EXIT_FAILURE, "failed to lock key `%s'", d.buf);
1381 f.f |= KF_MODIFIED;
1382 doclose(&f);
1383 return (0);
1384 }
1385
1386 /* --- @cmd_unlock@ --- */
1387
1388 static int cmd_unlock(int argc, char *argv[])
1389 {
1390 key_file f;
1391 key *k;
1392 key_data *kd;
1393 dstr d = DSTR_INIT;
1394
1395 if (argc != 2)
1396 die(EXIT_FAILURE, "Usage: unlock qtag");
1397 doopen(&f, KOPEN_WRITE);
1398 if (key_qtag(&f, argv[1], &d, &k, &kd))
1399 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1400 if (kd->e != KENC_ENCRYPT)
1401 die(EXIT_FAILURE, "key `%s' is not encrypted", d.buf);
1402 if (kd->e == KENC_ENCRYPT && key_punlock(d.buf, kd, kd))
1403 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1404 f.f |= KF_MODIFIED;
1405 doclose(&f);
1406 return (0);
1407 }
1408
1409 /* --- @cmd_extract@ --- */
1410
1411 static int cmd_extract(int argc, char *argv[])
1412 {
1413 key_file f;
1414 key *k;
1415 int i;
1416 int rc = 0;
1417 key_filter kf = { 0, 0 };
1418 FILE *fp;
1419
1420 for (;;) {
1421 static struct option opt[] = {
1422 { "filter", OPTF_ARGREQ, 0, 'f' },
1423 { 0, 0, 0, 0 }
1424 };
1425 int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0);
1426 if (i < 0)
1427 break;
1428 switch (i) {
1429 case 'f': {
1430 char *p;
1431 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1432 if (err || *p)
1433 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1434 } break;
1435 default:
1436 rc = 1;
1437 break;
1438 }
1439 }
1440
1441 argv += optind; argc -= optind;
1442 if (rc || argc < 1)
1443 die(EXIT_FAILURE, "Usage: extract [-f filter] file [tag...]");
1444 if (strcmp(*argv, "-") == 0)
1445 fp = stdout;
1446 else if (!(fp = fopen(*argv, "w"))) {
1447 die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
1448 *argv, strerror(errno));
1449 }
1450
1451 doopen(&f, KOPEN_READ);
1452 if (argc < 2) {
1453 key_iter i;
1454 key *k;
1455 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1456 key_extract(&f, k, fp, &kf);
1457 } else {
1458 for (i = 1; i < argc; i++) {
1459 if ((k = key_bytag(&f, argv[i])) != 0)
1460 key_extract(&f, k, fp, &kf);
1461 else {
1462 moan("key `%s' not found", argv[i]);
1463 rc = 1;
1464 }
1465 }
1466 }
1467 if (fclose(fp))
1468 die(EXIT_FAILURE, "error writing file: %s", strerror(errno));
1469 doclose(&f);
1470 return (rc);
1471 }
1472
1473 /* --- @cmd_tidy@ --- */
1474
1475 static int cmd_tidy(int argc, char *argv[])
1476 {
1477 key_file f;
1478 if (argc != 1)
1479 die(EXIT_FAILURE, "usage: tidy");
1480 doopen(&f, KOPEN_WRITE);
1481 f.f |= KF_MODIFIED; /* Nasty hack */
1482 doclose(&f);
1483 return (0);
1484 }
1485
1486 /* --- @cmd_merge@ --- */
1487
1488 static int cmd_merge(int argc, char *argv[])
1489 {
1490 key_file f;
1491 FILE *fp;
1492
1493 if (argc != 2)
1494 die(EXIT_FAILURE, "Usage: merge file");
1495 if (strcmp(argv[1], "-") == 0)
1496 fp = stdin;
1497 else if (!(fp = fopen(argv[1], "r"))) {
1498 die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
1499 argv[1], strerror(errno));
1500 }
1501
1502 doopen(&f, KOPEN_WRITE);
1503 key_merge(&f, argv[1], fp, key_moan, 0);
1504 doclose(&f);
1505 return (0);
1506 }
1507
1508 /*----- Main command table ------------------------------------------------*/
1509
1510 static struct cmd {
1511 const char *name;
1512 int (*cmd)(int /*argc*/, char */*argv*/[]);
1513 const char *help;
1514 } cmds[] = {
1515 { "add", cmd_add,
1516 "add [options] type [attr...]\n\
1517 Options: [-lq] [-a alg] [-b|-B bits] [-p param] [-r tag]\n\
1518 [-e expire] [-t tag] [-c comment]"
1519 },
1520 { "expire", cmd_expire, "expire tag..." },
1521 { "delete", cmd_delete, "delete tag..." },
1522 { "tag", cmd_tag, "tag tag [new-tag]" },
1523 { "setattr", cmd_setattr, "setattr tag attr..." },
1524 { "comment", cmd_comment, "comment tag [comment]" },
1525 { "lock", cmd_lock, "lock qtag" },
1526 { "unlock", cmd_unlock, "unlock qtag" },
1527 { "list", cmd_list, "list [-uqv] [-f filter] [tag...]" },
1528 { "fingerprint", cmd_finger, "fingerprint [-f filter] [tag...]" },
1529 { "tidy", cmd_tidy, "tidy" },
1530 { "extract", cmd_extract, "extract [-f filter] file [tag...]" },
1531 { "merge", cmd_merge, "merge file" },
1532 { 0, 0, 0 }
1533 };
1534
1535 typedef struct cmd cmd;
1536
1537 /*----- Main code ---------------------------------------------------------*/
1538
1539 /* --- Helpful GNUy functions --- */
1540
1541 void usage(FILE *fp)
1542 {
1543 pquis(fp, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1544 }
1545
1546 void version(FILE *fp)
1547 {
1548 pquis(fp, "$, Catacomb version " VERSION "\n");
1549 }
1550
1551 void help(FILE *fp)
1552 {
1553 cmd *c;
1554 version(fp);
1555 fputc('\n', fp);
1556 usage(fp);
1557 fputs("\n\
1558 Performs various simple key management operations. Command line options\n\
1559 recognized are:\n\
1560 \n\
1561 -h, --help Display this help text.\n\
1562 -v, --version Display version number.\n\
1563 -u, --usage Display short usage summary.\n\
1564 \n\
1565 -k, --keyring=FILE Read and write keys in FILE.\n\
1566 -i, --id=TAG Use key TAG for random number generator.\n\
1567 -t, --type=TYPE Use key TYPE for random number generator.\n\
1568 \n\
1569 The following commands are understood:\n\n",
1570 fp);
1571 for (c = cmds; c->name; c++)
1572 fprintf(fp, "%s\n", c->help);
1573 }
1574
1575 /* --- @main@ --- *
1576 *
1577 * Arguments: @int argc@ = number of command line arguments
1578 * @char *argv[]@ = array of command line arguments
1579 *
1580 * Returns: Nonzero on failure.
1581 *
1582 * Use: Main program. Performs simple key management functions.
1583 */
1584
1585 int main(int argc, char *argv[])
1586 {
1587 unsigned f = 0;
1588
1589 enum {
1590 f_bogus = 1
1591 };
1592
1593 /* --- Initialization --- */
1594
1595 ego(argv[0]);
1596 sub_init();
1597
1598 /* --- Parse command line options --- */
1599
1600 for (;;) {
1601 static struct option opt[] = {
1602
1603 /* --- Standard GNUy help options --- */
1604
1605 { "help", 0, 0, 'h' },
1606 { "version", 0, 0, 'v' },
1607 { "usage", 0, 0, 'u' },
1608
1609 /* --- Real live useful options --- */
1610
1611 { "keyring", OPTF_ARGREQ, 0, 'k' },
1612 { "id", OPTF_ARGREQ, 0, 'i' },
1613 { "type", OPTF_ARGREQ, 0, 't' },
1614
1615 /* --- Magic terminator --- */
1616
1617 { 0, 0, 0, 0 }
1618 };
1619 int i = mdwopt(argc, argv, "+hvu k:i:t:", opt, 0, 0, 0);
1620
1621 if (i < 0)
1622 break;
1623 switch (i) {
1624
1625 /* --- GNU help options --- */
1626 case 'h':
1627 help(stdout);
1628 exit(0);
1629 case 'v':
1630 version(stdout);
1631 exit(0);
1632 case 'u':
1633 usage(stdout);
1634 exit(0);
1635
1636 /* --- Real useful options --- */
1637
1638 case 'k':
1639 keyfile = optarg;
1640 break;
1641
1642 /* --- Bogosity --- */
1643
1644 default:
1645 f |= f_bogus;
1646 break;
1647 }
1648 }
1649
1650 /* --- Complain about excessive bogons --- */
1651
1652 if (f & f_bogus || optind == argc) {
1653 usage(stderr);
1654 exit(1);
1655 }
1656
1657 /* --- Initialize the Catacomb random number generator --- */
1658
1659 rand_init(RAND_GLOBAL);
1660 rand_noisesrc(RAND_GLOBAL, &noise_source);
1661
1662 /* --- Dispatch to appropriate command handler --- */
1663
1664 argc -= optind;
1665 argv += optind;
1666 optind = 0;
1667
1668 {
1669 cmd *c, *chosen = 0;
1670 size_t sz = strlen(argv[0]);
1671
1672 for (c = cmds; c->name; c++) {
1673 if (strncmp(argv[0], c->name, sz) == 0) {
1674 if (c->name[sz] == 0) {
1675 chosen = c;
1676 break;
1677 } else if (chosen)
1678 die(EXIT_FAILURE, "ambiguous command name `%s'", argv[0]);
1679 else
1680 chosen = c;
1681 }
1682 }
1683 if (!chosen)
1684 die(EXIT_FAILURE, "unknown command name `%s'", argv[0]);
1685 return (chosen->cmd(argc, argv));
1686 }
1687 }
1688
1689 /*----- That's all, folks -------------------------------------------------*/