3 * $Id: keyutil.c,v 1.19 2004/04/03 03:31:01 mdw Exp $
5 * Simple key manager program
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.19 2004/04/03 03:31:01 mdw
34 * Allow explicit group parameters for DH groups.
36 * Revision 1.18 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
43 * Revision 1.17 2004/03/28 01:58:47 mdw
44 * Generate, store and retreive elliptic curve keys.
46 * Revision 1.16 2003/10/15 09:31:45 mdw
49 * Revision 1.15 2003/05/15 23:23:24 mdw
50 * Fix behaviour with bogus trailing attributes.
52 * Revision 1.14 2001/02/23 09:03:27 mdw
53 * Simplify usage message by removing nonexistant options.
55 * Revision 1.13 2001/02/21 20:04:27 mdw
56 * Provide help on individual commands (some need it desparately). Allow
57 * atomic retagging of keys.
59 * Revision 1.12 2001/02/03 11:58:22 mdw
60 * Store the correct seed information and count for DSA keys now that it's
63 * Revision 1.11 2000/12/06 20:33:27 mdw
64 * Make flags be macros rather than enumerations, to ensure that they're
67 * Revision 1.10 2000/10/08 12:02:21 mdw
68 * Use @MP_EQ@ instead of @MP_CMP@.
70 * Revision 1.9 2000/08/15 21:40:49 mdw
71 * Minor formatting change in listing attributes.
73 * Revision 1.8 2000/07/29 09:59:13 mdw
74 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
76 * Revision 1.7 2000/07/01 11:18:51 mdw
77 * Use new interfaces for key manipulation.
79 * Revision 1.6 2000/06/17 11:28:22 mdw
80 * Use secure memory interface from MP library. `rand_getgood' is
83 * Revision 1.5 2000/02/12 18:21:03 mdw
84 * Overhaul of key management (again).
86 * Revision 1.4 1999/12/22 15:48:10 mdw
87 * Track new key-management changes. Support new key generation
90 * Revision 1.3 1999/11/02 15:23:24 mdw
91 * Fix newlines in keyring list.
93 * Revision 1.2 1999/10/15 21:05:28 mdw
94 * In `key list', show timezone for local times, and support `-u' option
97 * Revision 1.1 1999/09/03 08:41:12 mdw
102 /*----- Header files ------------------------------------------------------*/
112 #include <mLib/base64.h>
113 #include <mLib/mdwopt.h>
114 #include <mLib/quis.h>
115 #include <mLib/report.h>
116 #include <mLib/sub.h>
140 #include "sha256-mgf.h"
141 #include "sha224-mgf.h"
142 #include "sha384-mgf.h"
143 #include "sha512-mgf.h"
144 #include "tiger-mgf.h"
145 #include "rmd128-mgf.h"
146 #include "rmd160-mgf.h"
147 #include "rmd256-mgf.h"
148 #include "rmd320-mgf.h"
152 /*----- Handy global state ------------------------------------------------*/
154 static const char *keyfile
= "keyring";
156 /*----- Useful shared functions -------------------------------------------*/
158 /* --- @doopen@ --- *
160 * Arguments: @key_file *f@ = pointer to key file block
161 * @unsigned how@ = method to open file with
165 * Use: Opens a key file and handles errors by panicking
169 static void doopen(key_file
*f
, unsigned how
)
171 if (key_open(f
, keyfile
, how
, key_moan
, 0))
172 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
175 /* --- @doclose@ --- *
177 * Arguments: @key_file *f@ = pointer to key file block
181 * Use: Closes a key file and handles errors by panicking
185 static void doclose(key_file
*f
)
187 switch (key_close(f
)) {
189 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
190 keyfile
, strerror(errno
));
192 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
193 keyfile
, strerror(errno
));
197 /* --- @setattr@ --- *
199 * Arguments: @key_file *f@ = pointer to key file block
200 * @key *k@ = pointer to key block
201 * @char *v[]@ = array of assignments (overwritten!)
205 * Use: Applies the attribute assignments to the key.
208 static void setattr(key_file
*f
, key
*k
, char *v
[])
213 size_t eq
= strcspn(p
, "=");
215 moan("invalid assignment: `%s' (ignored)", p
);
221 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
222 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
227 /*----- Seeding -----------------------------------------------------------*/
229 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
231 { "dsarand", dsarand_create
},
232 { "rmd128-mgf", rmd128_mgfrand
},
233 { "rmd160-mgf", rmd160_mgfrand
},
234 { "rmd256-mgf", rmd256_mgfrand
},
235 { "rmd320-mgf", rmd320_mgfrand
},
236 { "sha-mgf", sha_mgfrand
},
237 { "sha224-mgf", sha224_mgfrand
},
238 { "sha256-mgf", sha256_mgfrand
},
239 { "sha384-mgf", sha384_mgfrand
},
240 { "sha512-mgf", sha512_mgfrand
},
241 { "tiger-mgf", tiger_mgfrand
},
245 #define SEEDALG_DEFAULT (seedtab + 2)
247 /*----- Key generation ----------------------------------------------------*/
249 /* --- Key generation parameters --- */
251 typedef struct keyopts
{
252 key_file
*kf
; /* Pointer to key file */
253 key
*k
; /* Pointer to the actual key */
254 dstr tag
; /* Full tag name for the key */
255 unsigned f
; /* Flags for the new key */
256 unsigned bits
, qbits
; /* Bit length for the new key */
257 const char *curve
; /* Elliptic curve name/info */
258 grand
*r
; /* Random number source */
259 key
*p
; /* Parameters key-data */
262 #define f_bogus 1u /* Error in parsing */
263 #define f_lock 2u /* Passphrase-lock private key */
264 #define f_quiet 4u /* Don't show a progress indicator */
265 #define f_limlee 8u /* Generate Lim-Lee primes */
266 #define f_subgroup 16u /* Generate a subgroup */
267 #define f_retag 32u /* Remove any existing tag */
269 /* --- @dolock@ --- *
271 * Arguments: @keyopts *k@ = key generation options
272 * @key_data *kd@ = pointer to key data to lock
273 * @const char *t@ = tag suffix or null
277 * Use: Does passphrase locking on new keys.
280 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
282 if (!(k
->f
& f_lock
))
285 dstr_putf(&k
->tag
, ".%s", t
);
286 if (key_plock(k
->tag
.buf
, kd
, kd
))
287 die(EXIT_FAILURE
, "couldn't lock key");
292 * Arguments: @key_data *kd@ = pointer to parent key block
293 * @const char *tag@ = pointer to tag string
294 * @mp *m@ = integer to store
295 * @unsigned f@ = flags to set
299 * Use: Sets a multiprecision integer subkey.
302 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
304 key_data
*kkd
= key_structcreate(kd
, tag
);
309 /* --- @copyparam@ --- *
311 * Arguments: @keyopts *k@ = pointer to key options
312 * @const char **pp@ = checklist of parameters
314 * Returns: Nonzero if parameters copied; zero if you have to generate
317 * Use: Copies parameters from a source key to the current one.
320 static int copyparam(keyopts
*k
, const char **pp
)
324 /* --- Quick check if no parameters supplied --- */
329 /* --- Run through the checklist --- */
332 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
334 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
335 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
336 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
340 /* --- Copy over the parameters --- */
344 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
345 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
351 * Arguments: @key_data *k@ = pointer to key data block
352 * @const char *tag@ = tag string to use
354 * Returns: Pointer to multiprecision integer key item.
356 * Use: Fetches an MP key component.
359 static mp
*getmp(key_data
*k
, const char *tag
)
361 k
= key_structfind(k
, tag
);
363 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
364 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
365 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
369 /* --- @keyrand@ --- *
371 * Arguments: @key_file *kf@ = pointer to key file
372 * @const char *id@ = pointer to key id (or null)
376 * Use: Keys the random number generator.
379 static void keyrand(key_file
*kf
, const char *id
)
383 /* --- Find the key --- */
386 if ((k
= key_bytag(kf
, id
)) == 0)
387 die(EXIT_FAILURE
, "key `%s' not found", id
);
389 k
= key_bytype(kf
, "catacomb-rand");
392 key_data
*kd
= &k
->k
, kkd
;
395 switch (kd
->e
& KF_ENCMASK
) {
401 if (key_punlock(d
.buf
, kd
, &kkd
))
402 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
409 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
413 /* --- Key the generator --- */
415 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
421 /* --- Key generation algorithms --- */
423 static void alg_binary(keyopts
*k
)
432 die(EXIT_FAILURE
, "no shared parameters for binary keys");
434 sz
= (k
->bits
+ 7) >> 3;
436 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
437 k
->r
->ops
->fill(k
->r
, p
, sz
);
439 key_binary(&k
->k
->k
, p
, sz
);
440 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
443 dolock(k
, &k
->k
->k
, 0);
446 static void alg_des(keyopts
*k
)
455 die(EXIT_FAILURE
, "no shared parameters for DES keys");
456 if (k
->bits
% 56 || k
->bits
> 168)
457 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
461 k
->r
->ops
->fill(k
->r
, p
, sz
);
462 for (i
= 0; i
< sz
; i
++) {
463 octet x
= p
[i
] | 0x01;
467 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
469 key_binary(&k
->k
->k
, p
, sz
);
470 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
473 dolock(k
, &k
->k
->k
, 0);
476 static void alg_rsa(keyopts
*k
)
481 /* --- Sanity checking --- */
484 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
488 /* --- Generate the RSA parameters --- */
490 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
491 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
492 die(EXIT_FAILURE
, "RSA key generation failed");
494 /* --- Run a test encryption --- */
497 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
499 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
504 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
505 c
= rsa_qprivop(&rp
, c
, c
, g
);
508 die(EXIT_FAILURE
, "test encryption failed");
514 /* --- Allrighty then --- */
518 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
519 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
521 kd
= key_structcreate(kd
, "private");
523 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
524 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
525 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
526 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
527 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
528 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
529 dolock(k
, kd
, "private");
534 static void alg_dsaparam(keyopts
*k
)
536 static const char *pl
[] = { "q", "p", "g", 0 };
537 if (!copyparam(k
, pl
)) {
543 key_data
*kd
= &k
->k
->k
;
546 /* --- Choose appropriate bit lengths if necessary --- */
553 /* --- Allocate a seed block --- */
555 sz
= (k
->qbits
+ 7) >> 3;
557 k
->r
->ops
->fill(k
->r
, p
, sz
);
559 /* --- Allocate the parameters --- */
561 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
562 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
563 die(EXIT_FAILURE
, "DSA parameter generation failed");
565 /* --- Store the parameters --- */
568 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
569 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
570 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
575 /* --- Store the seed for future verification --- */
580 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
581 base64_encode(&c
, 0, 0, &d
);
582 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
584 dstr_putf(&d
, "%u", ds
.count
);
585 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
592 static void alg_dsa(keyopts
*k
)
597 key_data
*kd
= &k
->k
->k
;
599 /* --- Get the shared parameters --- */
606 /* --- Choose a private key --- */
608 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
609 mpmont_create(&mm
, p
);
610 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
612 /* --- Store everything away --- */
614 mpkey(kd
, "y", y
, KCAT_PUB
);
616 kd
= key_structcreate(kd
, "private");
618 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
619 dolock(k
, kd
, "private");
621 mp_drop(x
); mp_drop(y
);
624 static void alg_dhparam(keyopts
*k
)
626 static const char *pl
[] = { "p", "q", "g", 0 };
627 key_data
*kd
= &k
->k
->k
;
628 if (!copyparam(k
, pl
)) {
635 if (strcmp(k
->curve
, "list") == 0) {
637 printf("Built-in prime groups:\n");
638 for (pe
= ptab
; pe
->name
; pe
++)
639 printf(" %s\n", pe
->name
);
643 if (dh_parse(&qd
, &dp
))
644 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
651 /* --- Choose a large safe prime number --- */
653 if (k
->f
& f_limlee
) {
658 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
659 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
660 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
661 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
665 for (i
= 0; i
< nf
; i
++) {
668 mp_writedstr(f
[i
], &d
, 10);
671 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
675 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
676 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
679 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
683 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
684 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
685 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
692 static void alg_dh(keyopts
*k
)
697 key_data
*kd
= &k
->k
->k
;
699 /* --- Get the shared parameters --- */
706 /* --- Choose a suitable private key --- *
708 * Since %$g$% has order %$q$%, choose %$x < q$%.
711 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
713 /* --- Compute the public key %$y = g^x \bmod p$% --- */
715 mpmont_create(&mm
, p
);
716 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
719 /* --- Store everything away --- */
721 mpkey(kd
, "y", y
, KCAT_PUB
);
723 kd
= key_structcreate(kd
, "private");
725 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
726 dolock(k
, kd
, "private");
728 mp_drop(x
); mp_drop(y
);
731 static void alg_bbs(keyopts
*k
)
736 /* --- Sanity checking --- */
739 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
743 /* --- Generate the BBS parameters --- */
745 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
746 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
747 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
749 /* --- Allrighty then --- */
753 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
755 kd
= key_structcreate(kd
, "private");
757 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
758 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
759 dolock(k
, kd
, "private");
764 static void alg_ecparam(keyopts
*k
)
766 static const char *pl
[] = { "curve", 0 };
767 if (!copyparam(k
, pl
)) {
770 key_data
*kd
= &k
->k
->k
;
772 /* --- Decide on a curve --- */
774 if (!k
->bits
) k
->bits
= 256;
775 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
777 printf("Built-in elliptic curves:\n");
778 for (ee
= ectab
; ee
->name
; ee
++)
779 printf(" %s\n", ee
->name
);
783 if (k
->bits
<= 56) k
->curve
= "secp112r1";
784 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
785 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
786 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
787 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
788 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
789 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
790 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
792 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
796 /* --- Check it --- */
798 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
799 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
800 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
801 moan("WARNING! curve check failed: %s", e
);
804 /* --- Write out the answer --- */
807 kd
= key_structcreate(kd
, "curve");
808 key_string(kd
, k
->curve
);
813 static void alg_ec(keyopts
*k
)
815 key_data
*kd
= &k
->k
->k
;
822 /* --- Get the curve --- */
825 if ((kkd
= key_structfind(kd
, "curve")) == 0)
826 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
827 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
828 die(EXIT_FAILURE
, "subkey `curve' is not a string");
829 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
830 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
832 /* --- Invent a private exponent and compute the public key --- */
834 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
835 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
837 /* --- Store everything away --- */
839 kkd
= key_structcreate(kd
, "p");
842 kkd
= key_structcreate(kd
, "private");
844 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
852 /* --- The algorithm tables --- */
854 typedef struct keyalg
{
856 void (*proc
)(keyopts
*o
);
860 static keyalg algtab
[] = {
861 { "binary", alg_binary
, "Plain binary data" },
862 { "des", alg_des
, "Binary with DES-style parity" },
863 { "rsa", alg_rsa
, "RSA public-key encryption" },
864 { "dsa", alg_dsa
, "DSA digital signatures" },
865 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
866 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
867 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
868 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
869 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
870 { "ec", alg_ec
, "Elliptic curve crypto" },
874 /* --- @cmd_add@ --- */
876 static int cmd_add(int argc
, char *argv
[])
879 time_t exp
= KEXP_EXPIRE
;
880 const char *tag
= 0, *ptag
= 0;
882 keyalg
*alg
= algtab
;
883 const char *rtag
= 0;
884 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
885 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
886 const char *seed
= 0;
889 /* --- Parse options for the subcommand --- */
892 static struct option opt
[] = {
893 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
894 { "bits", OPTF_ARGREQ
, 0, 'b' },
895 { "qbits", OPTF_ARGREQ
, 0, 'B' },
896 { "parameters", OPTF_ARGREQ
, 0, 'p' },
897 { "expire", OPTF_ARGREQ
, 0, 'e' },
898 { "comment", OPTF_ARGREQ
, 0, 'c' },
899 { "tag", OPTF_ARGREQ
, 0, 't' },
900 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
901 { "curve", OPTF_ARGREQ
, 0, 'C' },
902 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
903 { "seed", OPTF_ARGREQ
, 0, 's' },
904 { "newseed", OPTF_ARGREQ
, 0, 'n' },
905 { "lock", 0, 0, 'l' },
906 { "quiet", 0, 0, 'q' },
907 { "lim-lee", 0, 0, 'L' },
908 { "subgroup", 0, 0, 'S' },
911 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS", opt
, 0, 0, 0);
915 /* --- Handle the various options --- */
919 /* --- Read an algorithm name --- */
923 size_t sz
= strlen(optarg
);
925 if (strcmp(optarg
, "list") == 0) {
926 for (a
= algtab
; a
->name
; a
++)
927 printf("%-10s %s\n", a
->name
, a
->help
);
932 for (a
= algtab
; a
->name
; a
++) {
933 if (strncmp(optarg
, a
->name
, sz
) == 0) {
934 if (a
->name
[sz
] == 0) {
938 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
944 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
947 /* --- Bits must be nonzero and a multiple of 8 --- */
951 k
.bits
= strtoul(optarg
, &p
, 0);
952 if (k
.bits
== 0 || *p
!= 0)
953 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
958 k
.qbits
= strtoul(optarg
, &p
, 0);
959 if (k
.qbits
== 0 || *p
!= 0)
960 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
963 /* --- Parameter selection --- */
969 /* --- Expiry dates get passed to @get_date@ for parsing --- */
972 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
975 exp
= get_date(optarg
, 0);
977 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
981 /* --- Store comments without interpretation --- */
984 if (key_chkcomment(optarg
))
985 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
989 /* --- Elliptic curve parameters --- */
995 /* --- Store tags --- */
998 if (key_chkident(optarg
))
999 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1006 /* --- Seeding --- */
1009 const struct seedalg
*ss
;
1010 if (strcmp(optarg
, "list") == 0) {
1011 printf("Seed algorithms:\n");
1012 for (ss
= seedtab
; ss
->p
; ss
++)
1013 printf(" %s\n", ss
->p
);
1016 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1018 for (ss
= seedtab
; ss
->p
; ss
++) {
1019 if (strcmp(optarg
, ss
->p
) == 0)
1023 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1029 if (seed
) die(EXIT_FAILURE
, "seed already set");
1031 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1032 base64_decode(&b
, 0, 0, &d
);
1033 k
.r
= sa
->gen(d
.buf
, d
.len
);
1042 unsigned n
= strtoul(optarg
, &p
, 0);
1043 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1044 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1045 if (seed
) die(EXIT_FAILURE
, "seed already set");
1048 rand_get(RAND_GLOBAL
, p
, n
);
1050 base64_encode(&b
, p
, n
, &d
);
1051 base64_encode(&b
, 0, 0, &d
);
1053 k
.r
= sa
->gen(p
, n
);
1056 /* --- Other flags --- */
1074 /* --- Other things are bogus --- */
1082 /* --- Various sorts of bogosity --- */
1084 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1086 "Usage: add [options] type [attr...]");
1088 if (key_chkident(argv
[optind
]))
1089 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1091 /* --- Set up various bits of the state --- */
1093 if (exp
== KEXP_EXPIRE
)
1094 exp
= time(0) + 14 * 24 * 60 * 60;
1096 /* --- Open the file and create the basic key block --- *
1098 * Keep on generating keyids until one of them doesn't collide.
1101 doopen(&f
, KOPEN_WRITE
);
1104 /* --- Key the generator --- */
1109 uint32 id
= rand_global
.ops
->word(&rand_global
);
1111 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1114 if (err
!= KERR_DUPID
)
1115 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1118 /* --- Set various simple attributes --- */
1123 if (k
.f
& f_retag
) {
1124 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1125 key_settag(&f
, kk
, 0);
1127 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1128 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1132 int err
= key_setcomment(&f
, k
.k
, c
);
1134 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1137 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1139 key_putattr(&f
, k
.k
, "genseed", seed
);
1140 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1143 key_fulltag(k
.k
, &k
.tag
);
1145 /* --- Find the parameter key --- */
1148 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1149 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1150 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1151 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1154 /* --- Now generate the actual key data --- */
1164 /*----- Key listing -------------------------------------------------------*/
1166 /* --- Listing options --- */
1168 typedef struct listopts
{
1169 const char *tfmt
; /* Date format (@strftime@-style) */
1170 int v
; /* Verbosity level */
1171 unsigned f
; /* Various flags */
1172 time_t t
; /* Time now (for key expiry) */
1173 key_filter kf
; /* Filter for matching keys */
1176 /* --- Listing flags --- */
1178 #define f_newline 2u /* Write newline before next entry */
1179 #define f_attr 4u /* Written at least one attribute */
1180 #define f_utc 8u /* Emit UTC time, not local time */
1182 /* --- @showkeydata@ --- *
1184 * Arguments: @key_data *k@ = pointer to key to write
1185 * @int ind@ = indentation level
1186 * @listopts *o@ = listing options
1187 * @dstr *d@ = tag string for this subkey
1191 * Use: Emits a piece of key data in a human-readable format.
1194 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1196 #define INDENT(i) do { \
1198 for (_i = 0; _i < (i); _i++) { \
1203 switch (k
->e
& KF_ENCMASK
) {
1205 /* --- Binary key data --- *
1207 * Emit as a simple hex dump.
1211 const octet
*p
= k
->u
.k
.k
;
1212 const octet
*l
= p
+ k
->u
.k
.sz
;
1215 fputs(" {", stdout
);
1220 } else if (sz
% 8 == 0)
1224 printf("%02x", *p
++);
1229 fputs("}\n", stdout
);
1232 /* --- Encrypted data --- *
1234 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1235 * key. Otherwise just say that it's encrypted and move on.
1240 fputs(" encrypted\n", stdout
);
1243 if (key_punlock(d
->buf
, k
, &kd
))
1244 printf(" <failed to unlock %s>\n", d
->buf
);
1246 fputs(" encrypted", stdout
);
1247 showkeydata(&kd
, ind
, o
, d
);
1253 /* --- Integer keys --- *
1255 * Emit as a large integer in decimal. This makes using the key in
1256 * `calc' or whatever easier.
1261 mp_writefile(k
->u
.m
, stdout
, 10);
1265 /* --- Strings --- */
1268 printf(" `%s'\n", k
->u
.p
);
1271 /* --- Elliptic curve points --- */
1274 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1275 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1279 /* --- Structured keys --- *
1281 * Just iterate over the subkeys.
1289 fputs(" {\n", stdout
);
1290 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1291 if (!key_match(&ks
->k
, &o
->kf
))
1294 printf("%s =", SYM_NAME(ks
));
1296 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1297 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1300 fputs("}\n", stdout
);
1307 /* --- @showkey@ --- *
1309 * Arguments: @key *k@ = pointer to key to show
1310 * @listopts *o@ = pointer to listing options
1314 * Use: Emits a listing of a particular key.
1317 static void showkey(key
*k
, listopts
*o
)
1319 char ebuf
[24], dbuf
[24];
1322 /* --- Skip the key if the filter doesn't match --- */
1324 if (!key_match(&k
->k
, &o
->kf
))
1327 /* --- Sort out the expiry and deletion times --- */
1329 if (KEY_EXPIRED(o
->t
, k
->exp
))
1330 strcpy(ebuf
, "expired");
1331 else if (k
->exp
== KEXP_FOREVER
)
1332 strcpy(ebuf
, "forever");
1334 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1335 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1338 if (KEY_EXPIRED(o
->t
, k
->del
))
1339 strcpy(dbuf
, "deleted");
1340 else if (k
->del
== KEXP_FOREVER
)
1341 strcpy(dbuf
, "forever");
1343 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1344 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1347 /* --- If in compact format, just display and quit --- */
1350 if (!(o
->f
& f_newline
)) {
1351 printf("%8s %-20s %-20s %-10s %-10s\n",
1352 "Id", "Tag", "Type", "Expire", "Delete");
1354 printf("%08lx %-20s %-20s %-10s %-10s\n",
1355 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1356 k
->type
, ebuf
, dbuf
);
1361 /* --- Display the standard header --- */
1363 if (o
->f
& f_newline
)
1364 fputc('\n', stdout
);
1365 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1366 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1367 printf("type: %s\n", k
->type
);
1368 printf("expiry: %s\n", ebuf
);
1369 printf("delete: %s\n", dbuf
);
1370 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1372 /* --- Display the attributes --- */
1376 const char *av
, *an
;
1379 printf("attributes:");
1380 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1381 printf("\n %s = %s", an
, av
);
1385 fputc('\n', stdout
);
1390 /* --- If dumping requested, dump the raw key data --- */
1394 fputs("key:", stdout
);
1396 showkeydata(&k
->k
, 0, o
, &d
);
1403 /* --- @cmd_list@ --- */
1405 static int cmd_list(int argc
, char *argv
[])
1409 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1411 /* --- Parse subcommand options --- */
1414 static struct option opt
[] = {
1415 { "quiet", 0, 0, 'q' },
1416 { "verbose", 0, 0, 'v' },
1417 { "utc", 0, 0, 'u' },
1418 { "filter", OPTF_ARGREQ
, 0, 'f' },
1421 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1438 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1440 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1449 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1451 /* --- Open the key file --- */
1453 doopen(&f
, KOPEN_READ
);
1456 /* --- Set up the time format --- */
1459 o
.tfmt
= "%Y-%m-%d";
1460 else if (o
.f
& f_utc
)
1461 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1463 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1465 /* --- If specific keys were requested use them, otherwise do all --- *
1467 * Some day, this might turn into a wildcard match.
1470 if (optind
< argc
) {
1472 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1475 moan("key `%s' not found", argv
[optind
]);
1479 } while (optind
< argc
);
1482 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1490 return (EXIT_FAILURE
);
1495 /*----- Command implementation --------------------------------------------*/
1497 /* --- @cmd_expire@ --- */
1499 static int cmd_expire(int argc
, char *argv
[])
1507 die(EXIT_FAILURE
, "Usage: expire tag...");
1508 doopen(&f
, KOPEN_WRITE
);
1509 for (i
= 1; i
< argc
; i
++) {
1510 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1513 moan("key `%s' not found", argv
[i
]);
1521 /* --- @cmd_delete@ --- */
1523 static int cmd_delete(int argc
, char *argv
[])
1531 die(EXIT_FAILURE
, "Usage: delete tag...");
1532 doopen(&f
, KOPEN_WRITE
);
1533 for (i
= 1; i
< argc
; i
++) {
1534 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1537 moan("key `%s' not found", argv
[i
]);
1545 /* --- @cmd_setattr@ --- */
1547 static int cmd_setattr(int argc
, char *argv
[])
1553 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1554 doopen(&f
, KOPEN_WRITE
);
1555 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1556 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1557 setattr(&f
, k
, argv
+ 2);
1562 /* --- @cmd_finger@ --- */
1564 static void fingerprint(key
*k
, const key_filter
*kf
)
1567 octet hash
[RMD160_HASHSZ
];
1571 if (!key_encode(&k
->k
, &d
, kf
))
1574 rmd160_hash(&r
, d
.buf
, d
.len
);
1575 rmd160_done(&r
, hash
);
1579 for (i
= 0; i
< sizeof(hash
); i
++) {
1580 if (i
&& i
% 4 == 0)
1582 printf("%02x", hash
[i
]);
1584 printf(" %s\n", d
.buf
);
1588 static int cmd_finger(int argc
, char *argv
[])
1592 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1595 static struct option opt
[] = {
1596 { "filter", OPTF_ARGREQ
, 0, 'f' },
1599 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1605 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1607 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1615 argv
+= optind
; argc
-= optind
;
1617 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1619 doopen(&f
, KOPEN_READ
);
1623 for (i
= 0; i
< argc
; i
++) {
1624 key
*k
= key_bytag(&f
, argv
[i
]);
1626 fingerprint(k
, &kf
);
1629 moan("key `%s' not found", argv
[i
]);
1635 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1636 fingerprint(k
, &kf
);
1641 /* --- @cmd_comment@ --- */
1643 static int cmd_comment(int argc
, char *argv
[])
1649 if (argc
< 2 || argc
> 3)
1650 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1651 doopen(&f
, KOPEN_WRITE
);
1652 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1653 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1654 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1655 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1660 /* --- @cmd_tag@ --- */
1662 static int cmd_tag(int argc
, char *argv
[])
1671 static struct option opt
[] = {
1672 { "retag", 0, 0, 'r' },
1675 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1688 argv
+= optind
; argc
-= optind
;
1689 if (argc
< 1 || argc
> 2 || rc
)
1690 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1691 doopen(&f
, KOPEN_WRITE
);
1692 if (flags
& f_retag
) {
1693 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1694 key_settag(&f
, k
, 0);
1696 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1697 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1698 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1699 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1704 /* --- @cmd_lock@ --- */
1706 static int cmd_lock(int argc
, char *argv
[])
1714 die(EXIT_FAILURE
, "Usage: lock qtag");
1715 doopen(&f
, KOPEN_WRITE
);
1716 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1717 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1718 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1719 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1720 if (key_plock(d
.buf
, kd
, kd
))
1721 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1727 /* --- @cmd_unlock@ --- */
1729 static int cmd_unlock(int argc
, char *argv
[])
1737 die(EXIT_FAILURE
, "Usage: unlock qtag");
1738 doopen(&f
, KOPEN_WRITE
);
1739 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1740 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1741 if (kd
->e
!= KENC_ENCRYPT
)
1742 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1743 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1744 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1750 /* --- @cmd_extract@ --- */
1752 static int cmd_extract(int argc
, char *argv
[])
1758 key_filter kf
= { 0, 0 };
1762 static struct option opt
[] = {
1763 { "filter", OPTF_ARGREQ
, 0, 'f' },
1766 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1772 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1774 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1782 argv
+= optind
; argc
-= optind
;
1784 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1785 if (strcmp(*argv
, "-") == 0)
1787 else if (!(fp
= fopen(*argv
, "w"))) {
1788 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1789 *argv
, strerror(errno
));
1792 doopen(&f
, KOPEN_READ
);
1796 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1797 key_extract(&f
, k
, fp
, &kf
);
1799 for (i
= 1; i
< argc
; i
++) {
1800 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1801 key_extract(&f
, k
, fp
, &kf
);
1803 moan("key `%s' not found", argv
[i
]);
1809 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1814 /* --- @cmd_tidy@ --- */
1816 static int cmd_tidy(int argc
, char *argv
[])
1820 die(EXIT_FAILURE
, "usage: tidy");
1821 doopen(&f
, KOPEN_WRITE
);
1822 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1827 /* --- @cmd_merge@ --- */
1829 static int cmd_merge(int argc
, char *argv
[])
1835 die(EXIT_FAILURE
, "Usage: merge file");
1836 if (strcmp(argv
[1], "-") == 0)
1838 else if (!(fp
= fopen(argv
[1], "r"))) {
1839 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1840 argv
[1], strerror(errno
));
1843 doopen(&f
, KOPEN_WRITE
);
1844 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1849 /*----- Main command table ------------------------------------------------*/
1853 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1858 "add [options] type [attr...]\n\
1859 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1860 [-e expire] [-t tag] [-c comment]", "\
1863 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1864 -b, --bits=N Generate an N-bit key.\n\
1865 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1866 -p, --parameters=TAG Get group parameters from TAG.\n\
1867 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1868 -e, --expire=TIME Make the key expire after TIME.\n\
1869 -c, --comment=STRING Attach the command STRING to the key.\n\
1870 -t, --tag=TAG Tag the key with the name TAG.\n\
1871 -r, --retag Untag any key currently with that tag.\n\
1872 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1873 -l, --lock Lock the generated key with a passphrase.\n\
1874 -q, --quiet Don't give progress indicators while working.\n\
1875 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1876 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1878 { "expire", cmd_expire
, "expire tag..." },
1879 { "delete", cmd_delete
, "delete tag..." },
1880 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1883 -r, --retag Untag any key currently called new-tag.\n\
1885 { "setattr", cmd_setattr
, "setattr tag attr..." },
1886 { "comment", cmd_comment
, "comment tag [comment]" },
1887 { "lock", cmd_lock
, "lock qtag" },
1888 { "unlock", cmd_unlock
, "unlock qtag" },
1889 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1892 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1893 -q, --quiet Show less information.\n\
1894 -v, --verbose Show more information.\n\
1896 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1899 -f, --filter=FILT Only hash key components matching FILT.\n\
1901 { "tidy", cmd_tidy
, "tidy" },
1902 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1905 -f, --filter=FILT Only extract key components matching FILT.\n\
1907 { "merge", cmd_merge
, "merge file" },
1911 typedef struct cmd cmd
;
1913 /*----- Main code ---------------------------------------------------------*/
1915 /* --- @findcmd@ --- *
1917 * Arguments: @const char *name@ = a command name
1919 * Returns: Pointer to the command structure.
1921 * Use: Looks up a command by name. If the command isn't found, an
1922 * error is reported and the program is terminated.
1925 static cmd
*findcmd(const char *name
)
1927 cmd
*c
, *chosen
= 0;
1928 size_t sz
= strlen(name
);
1930 for (c
= cmds
; c
->name
; c
++) {
1931 if (strncmp(name
, c
->name
, sz
) == 0) {
1932 if (c
->name
[sz
] == 0) {
1936 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1942 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1946 /* --- Helpful GNUy functions --- */
1948 void usage(FILE *fp
)
1950 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1953 void version(FILE *fp
)
1955 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1958 void help(FILE *fp
, char **argv
)
1966 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1974 Performs various simple key management operations. Command line options\n\
1977 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1978 -v, --version Display version number.\n\
1979 -u, --usage Display short usage summary.\n\
1981 -k, --keyring=FILE Read and write keys in FILE.\n\
1982 -i, --id=TAG Use key TAG for random number generator.\n\
1983 -t, --type=TYPE Use key TYPE for random number generator.\n\
1985 The following commands are understood:\n\n",
1987 for (c
= cmds
; c
->name
; c
++)
1988 fprintf(fp
, "%s\n", c
->usage
);
1994 * Arguments: @int argc@ = number of command line arguments
1995 * @char *argv[]@ = array of command line arguments
1997 * Returns: Nonzero on failure.
1999 * Use: Main program. Performs simple key management functions.
2002 int main(int argc
, char *argv
[])
2008 /* --- Initialization --- */
2013 /* --- Parse command line options --- */
2016 static struct option opt
[] = {
2018 /* --- Standard GNUy help options --- */
2020 { "help", 0, 0, 'h' },
2021 { "version", 0, 0, 'v' },
2022 { "usage", 0, 0, 'u' },
2024 /* --- Real live useful options --- */
2026 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2028 /* --- Magic terminator --- */
2032 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2038 /* --- GNU help options --- */
2041 help(stdout
, argv
+ optind
);
2050 /* --- Real useful options --- */
2056 /* --- Bogosity --- */
2064 /* --- Complain about excessive bogons --- */
2066 if (f
& f_bogus
|| optind
== argc
) {
2071 /* --- Initialize the Catacomb random number generator --- */
2073 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2074 rand_seed(RAND_GLOBAL
, 160);
2076 /* --- Dispatch to appropriate command handler --- */
2081 return (findcmd(argv
[0])->cmd(argc
, argv
));
2084 /*----- That's all, folks -------------------------------------------------*/