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