3 * $Id: keyutil.c,v 1.20 2004/04/03 15:14:28 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.20 2004/04/03 15:14:28 mdw
34 * Handle points at infinity properly in listings.
36 * Revision 1.19 2004/04/03 03:31:01 mdw
37 * Allow explicit group parameters for DH groups.
39 * Revision 1.18 2004/04/01 12:50:09 mdw
40 * Add cyclic group abstraction, with test code. Separate off exponentation
41 * functions for better static linking. Fix a buttload of bugs on the way.
42 * Generally ensure that negative exponents do inversion correctly. Add
43 * table of standard prime-field subgroups. (Binary field subgroups are
44 * currently unimplemented but easy to add if anyone ever finds a good one.)
46 * Revision 1.17 2004/03/28 01:58:47 mdw
47 * Generate, store and retreive elliptic curve keys.
49 * Revision 1.16 2003/10/15 09:31:45 mdw
52 * Revision 1.15 2003/05/15 23:23:24 mdw
53 * Fix behaviour with bogus trailing attributes.
55 * Revision 1.14 2001/02/23 09:03:27 mdw
56 * Simplify usage message by removing nonexistant options.
58 * Revision 1.13 2001/02/21 20:04:27 mdw
59 * Provide help on individual commands (some need it desparately). Allow
60 * atomic retagging of keys.
62 * Revision 1.12 2001/02/03 11:58:22 mdw
63 * Store the correct seed information and count for DSA keys now that it's
66 * Revision 1.11 2000/12/06 20:33:27 mdw
67 * Make flags be macros rather than enumerations, to ensure that they're
70 * Revision 1.10 2000/10/08 12:02:21 mdw
71 * Use @MP_EQ@ instead of @MP_CMP@.
73 * Revision 1.9 2000/08/15 21:40:49 mdw
74 * Minor formatting change in listing attributes.
76 * Revision 1.8 2000/07/29 09:59:13 mdw
77 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
79 * Revision 1.7 2000/07/01 11:18:51 mdw
80 * Use new interfaces for key manipulation.
82 * Revision 1.6 2000/06/17 11:28:22 mdw
83 * Use secure memory interface from MP library. `rand_getgood' is
86 * Revision 1.5 2000/02/12 18:21:03 mdw
87 * Overhaul of key management (again).
89 * Revision 1.4 1999/12/22 15:48:10 mdw
90 * Track new key-management changes. Support new key generation
93 * Revision 1.3 1999/11/02 15:23:24 mdw
94 * Fix newlines in keyring list.
96 * Revision 1.2 1999/10/15 21:05:28 mdw
97 * In `key list', show timezone for local times, and support `-u' option
100 * Revision 1.1 1999/09/03 08:41:12 mdw
105 /*----- Header files ------------------------------------------------------*/
115 #include <mLib/base64.h>
116 #include <mLib/mdwopt.h>
117 #include <mLib/quis.h>
118 #include <mLib/report.h>
119 #include <mLib/sub.h>
143 #include "sha256-mgf.h"
144 #include "sha224-mgf.h"
145 #include "sha384-mgf.h"
146 #include "sha512-mgf.h"
147 #include "tiger-mgf.h"
148 #include "rmd128-mgf.h"
149 #include "rmd160-mgf.h"
150 #include "rmd256-mgf.h"
151 #include "rmd320-mgf.h"
155 /*----- Handy global state ------------------------------------------------*/
157 static const char *keyfile
= "keyring";
159 /*----- Useful shared functions -------------------------------------------*/
161 /* --- @doopen@ --- *
163 * Arguments: @key_file *f@ = pointer to key file block
164 * @unsigned how@ = method to open file with
168 * Use: Opens a key file and handles errors by panicking
172 static void doopen(key_file
*f
, unsigned how
)
174 if (key_open(f
, keyfile
, how
, key_moan
, 0))
175 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
178 /* --- @doclose@ --- *
180 * Arguments: @key_file *f@ = pointer to key file block
184 * Use: Closes a key file and handles errors by panicking
188 static void doclose(key_file
*f
)
190 switch (key_close(f
)) {
192 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
193 keyfile
, strerror(errno
));
195 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
196 keyfile
, strerror(errno
));
200 /* --- @setattr@ --- *
202 * Arguments: @key_file *f@ = pointer to key file block
203 * @key *k@ = pointer to key block
204 * @char *v[]@ = array of assignments (overwritten!)
208 * Use: Applies the attribute assignments to the key.
211 static void setattr(key_file
*f
, key
*k
, char *v
[])
216 size_t eq
= strcspn(p
, "=");
218 moan("invalid assignment: `%s' (ignored)", p
);
224 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
225 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
230 /*----- Seeding -----------------------------------------------------------*/
232 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
234 { "dsarand", dsarand_create
},
235 { "rmd128-mgf", rmd128_mgfrand
},
236 { "rmd160-mgf", rmd160_mgfrand
},
237 { "rmd256-mgf", rmd256_mgfrand
},
238 { "rmd320-mgf", rmd320_mgfrand
},
239 { "sha-mgf", sha_mgfrand
},
240 { "sha224-mgf", sha224_mgfrand
},
241 { "sha256-mgf", sha256_mgfrand
},
242 { "sha384-mgf", sha384_mgfrand
},
243 { "sha512-mgf", sha512_mgfrand
},
244 { "tiger-mgf", tiger_mgfrand
},
248 #define SEEDALG_DEFAULT (seedtab + 2)
250 /*----- Key generation ----------------------------------------------------*/
252 /* --- Key generation parameters --- */
254 typedef struct keyopts
{
255 key_file
*kf
; /* Pointer to key file */
256 key
*k
; /* Pointer to the actual key */
257 dstr tag
; /* Full tag name for the key */
258 unsigned f
; /* Flags for the new key */
259 unsigned bits
, qbits
; /* Bit length for the new key */
260 const char *curve
; /* Elliptic curve name/info */
261 grand
*r
; /* Random number source */
262 key
*p
; /* Parameters key-data */
265 #define f_bogus 1u /* Error in parsing */
266 #define f_lock 2u /* Passphrase-lock private key */
267 #define f_quiet 4u /* Don't show a progress indicator */
268 #define f_limlee 8u /* Generate Lim-Lee primes */
269 #define f_subgroup 16u /* Generate a subgroup */
270 #define f_retag 32u /* Remove any existing tag */
272 /* --- @dolock@ --- *
274 * Arguments: @keyopts *k@ = key generation options
275 * @key_data *kd@ = pointer to key data to lock
276 * @const char *t@ = tag suffix or null
280 * Use: Does passphrase locking on new keys.
283 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
285 if (!(k
->f
& f_lock
))
288 dstr_putf(&k
->tag
, ".%s", t
);
289 if (key_plock(k
->tag
.buf
, kd
, kd
))
290 die(EXIT_FAILURE
, "couldn't lock key");
295 * Arguments: @key_data *kd@ = pointer to parent key block
296 * @const char *tag@ = pointer to tag string
297 * @mp *m@ = integer to store
298 * @unsigned f@ = flags to set
302 * Use: Sets a multiprecision integer subkey.
305 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
307 key_data
*kkd
= key_structcreate(kd
, tag
);
312 /* --- @copyparam@ --- *
314 * Arguments: @keyopts *k@ = pointer to key options
315 * @const char **pp@ = checklist of parameters
317 * Returns: Nonzero if parameters copied; zero if you have to generate
320 * Use: Copies parameters from a source key to the current one.
323 static int copyparam(keyopts
*k
, const char **pp
)
327 /* --- Quick check if no parameters supplied --- */
332 /* --- Run through the checklist --- */
335 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
337 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
338 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
339 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
343 /* --- Copy over the parameters --- */
347 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
348 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
354 * Arguments: @key_data *k@ = pointer to key data block
355 * @const char *tag@ = tag string to use
357 * Returns: Pointer to multiprecision integer key item.
359 * Use: Fetches an MP key component.
362 static mp
*getmp(key_data
*k
, const char *tag
)
364 k
= key_structfind(k
, tag
);
366 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
367 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
368 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
372 /* --- @keyrand@ --- *
374 * Arguments: @key_file *kf@ = pointer to key file
375 * @const char *id@ = pointer to key id (or null)
379 * Use: Keys the random number generator.
382 static void keyrand(key_file
*kf
, const char *id
)
386 /* --- Find the key --- */
389 if ((k
= key_bytag(kf
, id
)) == 0)
390 die(EXIT_FAILURE
, "key `%s' not found", id
);
392 k
= key_bytype(kf
, "catacomb-rand");
395 key_data
*kd
= &k
->k
, kkd
;
398 switch (kd
->e
& KF_ENCMASK
) {
404 if (key_punlock(d
.buf
, kd
, &kkd
))
405 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
412 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
416 /* --- Key the generator --- */
418 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
424 /* --- Key generation algorithms --- */
426 static void alg_binary(keyopts
*k
)
435 die(EXIT_FAILURE
, "no shared parameters for binary keys");
437 sz
= (k
->bits
+ 7) >> 3;
439 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
440 k
->r
->ops
->fill(k
->r
, p
, sz
);
442 key_binary(&k
->k
->k
, p
, sz
);
443 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
446 dolock(k
, &k
->k
->k
, 0);
449 static void alg_des(keyopts
*k
)
458 die(EXIT_FAILURE
, "no shared parameters for DES keys");
459 if (k
->bits
% 56 || k
->bits
> 168)
460 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
464 k
->r
->ops
->fill(k
->r
, p
, sz
);
465 for (i
= 0; i
< sz
; i
++) {
466 octet x
= p
[i
] | 0x01;
470 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
472 key_binary(&k
->k
->k
, p
, sz
);
473 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
476 dolock(k
, &k
->k
->k
, 0);
479 static void alg_rsa(keyopts
*k
)
484 /* --- Sanity checking --- */
487 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
491 /* --- Generate the RSA parameters --- */
493 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
494 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
495 die(EXIT_FAILURE
, "RSA key generation failed");
497 /* --- Run a test encryption --- */
500 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
502 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
507 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
508 c
= rsa_qprivop(&rp
, c
, c
, g
);
511 die(EXIT_FAILURE
, "test encryption failed");
517 /* --- Allrighty then --- */
521 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
522 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
524 kd
= key_structcreate(kd
, "private");
526 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
527 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
528 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
529 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
530 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
531 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
532 dolock(k
, kd
, "private");
537 static void alg_dsaparam(keyopts
*k
)
539 static const char *pl
[] = { "q", "p", "g", 0 };
540 if (!copyparam(k
, pl
)) {
546 key_data
*kd
= &k
->k
->k
;
549 /* --- Choose appropriate bit lengths if necessary --- */
556 /* --- Allocate a seed block --- */
558 sz
= (k
->qbits
+ 7) >> 3;
560 k
->r
->ops
->fill(k
->r
, p
, sz
);
562 /* --- Allocate the parameters --- */
564 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
565 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
566 die(EXIT_FAILURE
, "DSA parameter generation failed");
568 /* --- Store the parameters --- */
571 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
572 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
573 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
578 /* --- Store the seed for future verification --- */
583 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
584 base64_encode(&c
, 0, 0, &d
);
585 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
587 dstr_putf(&d
, "%u", ds
.count
);
588 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
595 static void alg_dsa(keyopts
*k
)
600 key_data
*kd
= &k
->k
->k
;
602 /* --- Get the shared parameters --- */
609 /* --- Choose a private key --- */
611 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
612 mpmont_create(&mm
, p
);
613 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
615 /* --- Store everything away --- */
617 mpkey(kd
, "y", y
, KCAT_PUB
);
619 kd
= key_structcreate(kd
, "private");
621 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
622 dolock(k
, kd
, "private");
624 mp_drop(x
); mp_drop(y
);
627 static void alg_dhparam(keyopts
*k
)
629 static const char *pl
[] = { "p", "q", "g", 0 };
630 key_data
*kd
= &k
->k
->k
;
631 if (!copyparam(k
, pl
)) {
638 if (strcmp(k
->curve
, "list") == 0) {
640 printf("Built-in prime groups:\n");
641 for (pe
= ptab
; pe
->name
; pe
++)
642 printf(" %s\n", pe
->name
);
646 if (dh_parse(&qd
, &dp
))
647 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
654 /* --- Choose a large safe prime number --- */
656 if (k
->f
& f_limlee
) {
661 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
662 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
663 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
664 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
668 for (i
= 0; i
< nf
; i
++) {
671 mp_writedstr(f
[i
], &d
, 10);
674 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
678 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
679 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
682 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
686 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
687 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
688 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
695 static void alg_dh(keyopts
*k
)
700 key_data
*kd
= &k
->k
->k
;
702 /* --- Get the shared parameters --- */
709 /* --- Choose a suitable private key --- *
711 * Since %$g$% has order %$q$%, choose %$x < q$%.
714 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
716 /* --- Compute the public key %$y = g^x \bmod p$% --- */
718 mpmont_create(&mm
, p
);
719 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
722 /* --- Store everything away --- */
724 mpkey(kd
, "y", y
, KCAT_PUB
);
726 kd
= key_structcreate(kd
, "private");
728 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
729 dolock(k
, kd
, "private");
731 mp_drop(x
); mp_drop(y
);
734 static void alg_bbs(keyopts
*k
)
739 /* --- Sanity checking --- */
742 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
746 /* --- Generate the BBS parameters --- */
748 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
749 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
750 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
752 /* --- Allrighty then --- */
756 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
758 kd
= key_structcreate(kd
, "private");
760 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
761 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
762 dolock(k
, kd
, "private");
767 static void alg_ecparam(keyopts
*k
)
769 static const char *pl
[] = { "curve", 0 };
770 if (!copyparam(k
, pl
)) {
773 key_data
*kd
= &k
->k
->k
;
775 /* --- Decide on a curve --- */
777 if (!k
->bits
) k
->bits
= 256;
778 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
780 printf("Built-in elliptic curves:\n");
781 for (ee
= ectab
; ee
->name
; ee
++)
782 printf(" %s\n", ee
->name
);
786 if (k
->bits
<= 56) k
->curve
= "secp112r1";
787 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
788 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
789 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
790 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
791 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
792 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
793 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
795 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
799 /* --- Check it --- */
801 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
802 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
803 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
804 moan("WARNING! curve check failed: %s", e
);
807 /* --- Write out the answer --- */
810 kd
= key_structcreate(kd
, "curve");
811 key_string(kd
, k
->curve
);
816 static void alg_ec(keyopts
*k
)
818 key_data
*kd
= &k
->k
->k
;
825 /* --- Get the curve --- */
828 if ((kkd
= key_structfind(kd
, "curve")) == 0)
829 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
830 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
831 die(EXIT_FAILURE
, "subkey `curve' is not a string");
832 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
833 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
835 /* --- Invent a private exponent and compute the public key --- */
837 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
838 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
840 /* --- Store everything away --- */
842 kkd
= key_structcreate(kd
, "p");
845 kkd
= key_structcreate(kd
, "private");
847 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
855 /* --- The algorithm tables --- */
857 typedef struct keyalg
{
859 void (*proc
)(keyopts
*o
);
863 static keyalg algtab
[] = {
864 { "binary", alg_binary
, "Plain binary data" },
865 { "des", alg_des
, "Binary with DES-style parity" },
866 { "rsa", alg_rsa
, "RSA public-key encryption" },
867 { "dsa", alg_dsa
, "DSA digital signatures" },
868 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
869 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
870 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
871 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
872 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
873 { "ec", alg_ec
, "Elliptic curve crypto" },
877 /* --- @cmd_add@ --- */
879 static int cmd_add(int argc
, char *argv
[])
882 time_t exp
= KEXP_EXPIRE
;
883 const char *tag
= 0, *ptag
= 0;
885 keyalg
*alg
= algtab
;
886 const char *rtag
= 0;
887 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
888 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
889 const char *seed
= 0;
892 /* --- Parse options for the subcommand --- */
895 static struct option opt
[] = {
896 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
897 { "bits", OPTF_ARGREQ
, 0, 'b' },
898 { "qbits", OPTF_ARGREQ
, 0, 'B' },
899 { "parameters", OPTF_ARGREQ
, 0, 'p' },
900 { "expire", OPTF_ARGREQ
, 0, 'e' },
901 { "comment", OPTF_ARGREQ
, 0, 'c' },
902 { "tag", OPTF_ARGREQ
, 0, 't' },
903 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
904 { "curve", OPTF_ARGREQ
, 0, 'C' },
905 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
906 { "seed", OPTF_ARGREQ
, 0, 's' },
907 { "newseed", OPTF_ARGREQ
, 0, 'n' },
908 { "lock", 0, 0, 'l' },
909 { "quiet", 0, 0, 'q' },
910 { "lim-lee", 0, 0, 'L' },
911 { "subgroup", 0, 0, 'S' },
914 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS", opt
, 0, 0, 0);
918 /* --- Handle the various options --- */
922 /* --- Read an algorithm name --- */
926 size_t sz
= strlen(optarg
);
928 if (strcmp(optarg
, "list") == 0) {
929 for (a
= algtab
; a
->name
; a
++)
930 printf("%-10s %s\n", a
->name
, a
->help
);
935 for (a
= algtab
; a
->name
; a
++) {
936 if (strncmp(optarg
, a
->name
, sz
) == 0) {
937 if (a
->name
[sz
] == 0) {
941 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
947 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
950 /* --- Bits must be nonzero and a multiple of 8 --- */
954 k
.bits
= strtoul(optarg
, &p
, 0);
955 if (k
.bits
== 0 || *p
!= 0)
956 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
961 k
.qbits
= strtoul(optarg
, &p
, 0);
962 if (k
.qbits
== 0 || *p
!= 0)
963 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
966 /* --- Parameter selection --- */
972 /* --- Expiry dates get passed to @get_date@ for parsing --- */
975 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
978 exp
= get_date(optarg
, 0);
980 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
984 /* --- Store comments without interpretation --- */
987 if (key_chkcomment(optarg
))
988 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
992 /* --- Elliptic curve parameters --- */
998 /* --- Store tags --- */
1001 if (key_chkident(optarg
))
1002 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1009 /* --- Seeding --- */
1012 const struct seedalg
*ss
;
1013 if (strcmp(optarg
, "list") == 0) {
1014 printf("Seed algorithms:\n");
1015 for (ss
= seedtab
; ss
->p
; ss
++)
1016 printf(" %s\n", ss
->p
);
1019 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1021 for (ss
= seedtab
; ss
->p
; ss
++) {
1022 if (strcmp(optarg
, ss
->p
) == 0)
1026 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1032 if (seed
) die(EXIT_FAILURE
, "seed already set");
1034 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1035 base64_decode(&b
, 0, 0, &d
);
1036 k
.r
= sa
->gen(d
.buf
, d
.len
);
1045 unsigned n
= strtoul(optarg
, &p
, 0);
1046 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1047 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1048 if (seed
) die(EXIT_FAILURE
, "seed already set");
1051 rand_get(RAND_GLOBAL
, p
, n
);
1053 base64_encode(&b
, p
, n
, &d
);
1054 base64_encode(&b
, 0, 0, &d
);
1056 k
.r
= sa
->gen(p
, n
);
1059 /* --- Other flags --- */
1077 /* --- Other things are bogus --- */
1085 /* --- Various sorts of bogosity --- */
1087 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1089 "Usage: add [options] type [attr...]");
1091 if (key_chkident(argv
[optind
]))
1092 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1094 /* --- Set up various bits of the state --- */
1096 if (exp
== KEXP_EXPIRE
)
1097 exp
= time(0) + 14 * 24 * 60 * 60;
1099 /* --- Open the file and create the basic key block --- *
1101 * Keep on generating keyids until one of them doesn't collide.
1104 doopen(&f
, KOPEN_WRITE
);
1107 /* --- Key the generator --- */
1112 uint32 id
= rand_global
.ops
->word(&rand_global
);
1114 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1117 if (err
!= KERR_DUPID
)
1118 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1121 /* --- Set various simple attributes --- */
1126 if (k
.f
& f_retag
) {
1127 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1128 key_settag(&f
, kk
, 0);
1130 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1131 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1135 int err
= key_setcomment(&f
, k
.k
, c
);
1137 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1140 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1142 key_putattr(&f
, k
.k
, "genseed", seed
);
1143 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1146 key_fulltag(k
.k
, &k
.tag
);
1148 /* --- Find the parameter key --- */
1151 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1152 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1153 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1154 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1157 /* --- Now generate the actual key data --- */
1167 /*----- Key listing -------------------------------------------------------*/
1169 /* --- Listing options --- */
1171 typedef struct listopts
{
1172 const char *tfmt
; /* Date format (@strftime@-style) */
1173 int v
; /* Verbosity level */
1174 unsigned f
; /* Various flags */
1175 time_t t
; /* Time now (for key expiry) */
1176 key_filter kf
; /* Filter for matching keys */
1179 /* --- Listing flags --- */
1181 #define f_newline 2u /* Write newline before next entry */
1182 #define f_attr 4u /* Written at least one attribute */
1183 #define f_utc 8u /* Emit UTC time, not local time */
1185 /* --- @showkeydata@ --- *
1187 * Arguments: @key_data *k@ = pointer to key to write
1188 * @int ind@ = indentation level
1189 * @listopts *o@ = listing options
1190 * @dstr *d@ = tag string for this subkey
1194 * Use: Emits a piece of key data in a human-readable format.
1197 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1199 #define INDENT(i) do { \
1201 for (_i = 0; _i < (i); _i++) { \
1206 switch (k
->e
& KF_ENCMASK
) {
1208 /* --- Binary key data --- *
1210 * Emit as a simple hex dump.
1214 const octet
*p
= k
->u
.k
.k
;
1215 const octet
*l
= p
+ k
->u
.k
.sz
;
1218 fputs(" {", stdout
);
1223 } else if (sz
% 8 == 0)
1227 printf("%02x", *p
++);
1232 fputs("}\n", stdout
);
1235 /* --- Encrypted data --- *
1237 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1238 * key. Otherwise just say that it's encrypted and move on.
1243 fputs(" encrypted\n", stdout
);
1246 if (key_punlock(d
->buf
, k
, &kd
))
1247 printf(" <failed to unlock %s>\n", d
->buf
);
1249 fputs(" encrypted", stdout
);
1250 showkeydata(&kd
, ind
, o
, d
);
1256 /* --- Integer keys --- *
1258 * Emit as a large integer in decimal. This makes using the key in
1259 * `calc' or whatever easier.
1264 mp_writefile(k
->u
.m
, stdout
, 10);
1268 /* --- Strings --- */
1271 printf(" `%s'\n", k
->u
.p
);
1274 /* --- Elliptic curve points --- */
1277 if (EC_ATINF(&k
->u
.e
))
1280 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1281 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1286 /* --- Structured keys --- *
1288 * Just iterate over the subkeys.
1296 fputs(" {\n", stdout
);
1297 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1298 if (!key_match(&ks
->k
, &o
->kf
))
1301 printf("%s =", SYM_NAME(ks
));
1303 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1304 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1307 fputs("}\n", stdout
);
1314 /* --- @showkey@ --- *
1316 * Arguments: @key *k@ = pointer to key to show
1317 * @listopts *o@ = pointer to listing options
1321 * Use: Emits a listing of a particular key.
1324 static void showkey(key
*k
, listopts
*o
)
1326 char ebuf
[24], dbuf
[24];
1329 /* --- Skip the key if the filter doesn't match --- */
1331 if (!key_match(&k
->k
, &o
->kf
))
1334 /* --- Sort out the expiry and deletion times --- */
1336 if (KEY_EXPIRED(o
->t
, k
->exp
))
1337 strcpy(ebuf
, "expired");
1338 else if (k
->exp
== KEXP_FOREVER
)
1339 strcpy(ebuf
, "forever");
1341 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1342 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1345 if (KEY_EXPIRED(o
->t
, k
->del
))
1346 strcpy(dbuf
, "deleted");
1347 else if (k
->del
== KEXP_FOREVER
)
1348 strcpy(dbuf
, "forever");
1350 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1351 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1354 /* --- If in compact format, just display and quit --- */
1357 if (!(o
->f
& f_newline
)) {
1358 printf("%8s %-20s %-20s %-10s %-10s\n",
1359 "Id", "Tag", "Type", "Expire", "Delete");
1361 printf("%08lx %-20s %-20s %-10s %-10s\n",
1362 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1363 k
->type
, ebuf
, dbuf
);
1368 /* --- Display the standard header --- */
1370 if (o
->f
& f_newline
)
1371 fputc('\n', stdout
);
1372 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1373 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1374 printf("type: %s\n", k
->type
);
1375 printf("expiry: %s\n", ebuf
);
1376 printf("delete: %s\n", dbuf
);
1377 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1379 /* --- Display the attributes --- */
1383 const char *av
, *an
;
1386 printf("attributes:");
1387 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1388 printf("\n %s = %s", an
, av
);
1392 fputc('\n', stdout
);
1397 /* --- If dumping requested, dump the raw key data --- */
1401 fputs("key:", stdout
);
1403 showkeydata(&k
->k
, 0, o
, &d
);
1410 /* --- @cmd_list@ --- */
1412 static int cmd_list(int argc
, char *argv
[])
1416 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1418 /* --- Parse subcommand options --- */
1421 static struct option opt
[] = {
1422 { "quiet", 0, 0, 'q' },
1423 { "verbose", 0, 0, 'v' },
1424 { "utc", 0, 0, 'u' },
1425 { "filter", OPTF_ARGREQ
, 0, 'f' },
1428 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1445 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1447 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1456 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1458 /* --- Open the key file --- */
1460 doopen(&f
, KOPEN_READ
);
1463 /* --- Set up the time format --- */
1466 o
.tfmt
= "%Y-%m-%d";
1467 else if (o
.f
& f_utc
)
1468 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1470 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1472 /* --- If specific keys were requested use them, otherwise do all --- *
1474 * Some day, this might turn into a wildcard match.
1477 if (optind
< argc
) {
1479 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1482 moan("key `%s' not found", argv
[optind
]);
1486 } while (optind
< argc
);
1489 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1497 return (EXIT_FAILURE
);
1502 /*----- Command implementation --------------------------------------------*/
1504 /* --- @cmd_expire@ --- */
1506 static int cmd_expire(int argc
, char *argv
[])
1514 die(EXIT_FAILURE
, "Usage: expire tag...");
1515 doopen(&f
, KOPEN_WRITE
);
1516 for (i
= 1; i
< argc
; i
++) {
1517 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1520 moan("key `%s' not found", argv
[i
]);
1528 /* --- @cmd_delete@ --- */
1530 static int cmd_delete(int argc
, char *argv
[])
1538 die(EXIT_FAILURE
, "Usage: delete tag...");
1539 doopen(&f
, KOPEN_WRITE
);
1540 for (i
= 1; i
< argc
; i
++) {
1541 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1544 moan("key `%s' not found", argv
[i
]);
1552 /* --- @cmd_setattr@ --- */
1554 static int cmd_setattr(int argc
, char *argv
[])
1560 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1561 doopen(&f
, KOPEN_WRITE
);
1562 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1563 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1564 setattr(&f
, k
, argv
+ 2);
1569 /* --- @cmd_finger@ --- */
1571 static void fingerprint(key
*k
, const key_filter
*kf
)
1574 octet hash
[RMD160_HASHSZ
];
1578 if (!key_encode(&k
->k
, &d
, kf
))
1581 rmd160_hash(&r
, d
.buf
, d
.len
);
1582 rmd160_done(&r
, hash
);
1586 for (i
= 0; i
< sizeof(hash
); i
++) {
1587 if (i
&& i
% 4 == 0)
1589 printf("%02x", hash
[i
]);
1591 printf(" %s\n", d
.buf
);
1595 static int cmd_finger(int argc
, char *argv
[])
1599 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1602 static struct option opt
[] = {
1603 { "filter", OPTF_ARGREQ
, 0, 'f' },
1606 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1612 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1614 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1622 argv
+= optind
; argc
-= optind
;
1624 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1626 doopen(&f
, KOPEN_READ
);
1630 for (i
= 0; i
< argc
; i
++) {
1631 key
*k
= key_bytag(&f
, argv
[i
]);
1633 fingerprint(k
, &kf
);
1636 moan("key `%s' not found", argv
[i
]);
1642 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1643 fingerprint(k
, &kf
);
1648 /* --- @cmd_comment@ --- */
1650 static int cmd_comment(int argc
, char *argv
[])
1656 if (argc
< 2 || argc
> 3)
1657 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1658 doopen(&f
, KOPEN_WRITE
);
1659 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1660 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1661 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1662 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1667 /* --- @cmd_tag@ --- */
1669 static int cmd_tag(int argc
, char *argv
[])
1678 static struct option opt
[] = {
1679 { "retag", 0, 0, 'r' },
1682 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1695 argv
+= optind
; argc
-= optind
;
1696 if (argc
< 1 || argc
> 2 || rc
)
1697 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1698 doopen(&f
, KOPEN_WRITE
);
1699 if (flags
& f_retag
) {
1700 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1701 key_settag(&f
, k
, 0);
1703 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1704 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1705 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1706 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1711 /* --- @cmd_lock@ --- */
1713 static int cmd_lock(int argc
, char *argv
[])
1721 die(EXIT_FAILURE
, "Usage: lock qtag");
1722 doopen(&f
, KOPEN_WRITE
);
1723 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1724 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1725 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1726 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1727 if (key_plock(d
.buf
, kd
, kd
))
1728 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1734 /* --- @cmd_unlock@ --- */
1736 static int cmd_unlock(int argc
, char *argv
[])
1744 die(EXIT_FAILURE
, "Usage: unlock qtag");
1745 doopen(&f
, KOPEN_WRITE
);
1746 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1747 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1748 if (kd
->e
!= KENC_ENCRYPT
)
1749 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1750 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1751 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1757 /* --- @cmd_extract@ --- */
1759 static int cmd_extract(int argc
, char *argv
[])
1765 key_filter kf
= { 0, 0 };
1769 static struct option opt
[] = {
1770 { "filter", OPTF_ARGREQ
, 0, 'f' },
1773 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1779 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1781 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1789 argv
+= optind
; argc
-= optind
;
1791 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1792 if (strcmp(*argv
, "-") == 0)
1794 else if (!(fp
= fopen(*argv
, "w"))) {
1795 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1796 *argv
, strerror(errno
));
1799 doopen(&f
, KOPEN_READ
);
1803 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1804 key_extract(&f
, k
, fp
, &kf
);
1806 for (i
= 1; i
< argc
; i
++) {
1807 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1808 key_extract(&f
, k
, fp
, &kf
);
1810 moan("key `%s' not found", argv
[i
]);
1816 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1821 /* --- @cmd_tidy@ --- */
1823 static int cmd_tidy(int argc
, char *argv
[])
1827 die(EXIT_FAILURE
, "usage: tidy");
1828 doopen(&f
, KOPEN_WRITE
);
1829 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1834 /* --- @cmd_merge@ --- */
1836 static int cmd_merge(int argc
, char *argv
[])
1842 die(EXIT_FAILURE
, "Usage: merge file");
1843 if (strcmp(argv
[1], "-") == 0)
1845 else if (!(fp
= fopen(argv
[1], "r"))) {
1846 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1847 argv
[1], strerror(errno
));
1850 doopen(&f
, KOPEN_WRITE
);
1851 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1856 /*----- Main command table ------------------------------------------------*/
1860 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1865 "add [options] type [attr...]\n\
1866 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1867 [-e expire] [-t tag] [-c comment]", "\
1870 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1871 -b, --bits=N Generate an N-bit key.\n\
1872 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1873 -p, --parameters=TAG Get group parameters from TAG.\n\
1874 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1875 -e, --expire=TIME Make the key expire after TIME.\n\
1876 -c, --comment=STRING Attach the command STRING to the key.\n\
1877 -t, --tag=TAG Tag the key with the name TAG.\n\
1878 -r, --retag Untag any key currently with that tag.\n\
1879 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1880 -l, --lock Lock the generated key with a passphrase.\n\
1881 -q, --quiet Don't give progress indicators while working.\n\
1882 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1883 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1885 { "expire", cmd_expire
, "expire tag..." },
1886 { "delete", cmd_delete
, "delete tag..." },
1887 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1890 -r, --retag Untag any key currently called new-tag.\n\
1892 { "setattr", cmd_setattr
, "setattr tag attr..." },
1893 { "comment", cmd_comment
, "comment tag [comment]" },
1894 { "lock", cmd_lock
, "lock qtag" },
1895 { "unlock", cmd_unlock
, "unlock qtag" },
1896 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1899 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1900 -q, --quiet Show less information.\n\
1901 -v, --verbose Show more information.\n\
1903 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1906 -f, --filter=FILT Only hash key components matching FILT.\n\
1908 { "tidy", cmd_tidy
, "tidy" },
1909 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1912 -f, --filter=FILT Only extract key components matching FILT.\n\
1914 { "merge", cmd_merge
, "merge file" },
1918 typedef struct cmd cmd
;
1920 /*----- Main code ---------------------------------------------------------*/
1922 /* --- @findcmd@ --- *
1924 * Arguments: @const char *name@ = a command name
1926 * Returns: Pointer to the command structure.
1928 * Use: Looks up a command by name. If the command isn't found, an
1929 * error is reported and the program is terminated.
1932 static cmd
*findcmd(const char *name
)
1934 cmd
*c
, *chosen
= 0;
1935 size_t sz
= strlen(name
);
1937 for (c
= cmds
; c
->name
; c
++) {
1938 if (strncmp(name
, c
->name
, sz
) == 0) {
1939 if (c
->name
[sz
] == 0) {
1943 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1949 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1953 /* --- Helpful GNUy functions --- */
1955 void usage(FILE *fp
)
1957 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1960 void version(FILE *fp
)
1962 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1965 void help(FILE *fp
, char **argv
)
1973 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1981 Performs various simple key management operations. Command line options\n\
1984 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1985 -v, --version Display version number.\n\
1986 -u, --usage Display short usage summary.\n\
1988 -k, --keyring=FILE Read and write keys in FILE.\n\
1989 -i, --id=TAG Use key TAG for random number generator.\n\
1990 -t, --type=TYPE Use key TYPE for random number generator.\n\
1992 The following commands are understood:\n\n",
1994 for (c
= cmds
; c
->name
; c
++)
1995 fprintf(fp
, "%s\n", c
->usage
);
2001 * Arguments: @int argc@ = number of command line arguments
2002 * @char *argv[]@ = array of command line arguments
2004 * Returns: Nonzero on failure.
2006 * Use: Main program. Performs simple key management functions.
2009 int main(int argc
, char *argv
[])
2015 /* --- Initialization --- */
2020 /* --- Parse command line options --- */
2023 static struct option opt
[] = {
2025 /* --- Standard GNUy help options --- */
2027 { "help", 0, 0, 'h' },
2028 { "version", 0, 0, 'v' },
2029 { "usage", 0, 0, 'u' },
2031 /* --- Real live useful options --- */
2033 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2035 /* --- Magic terminator --- */
2039 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2045 /* --- GNU help options --- */
2048 help(stdout
, argv
+ optind
);
2057 /* --- Real useful options --- */
2063 /* --- Bogosity --- */
2071 /* --- Complain about excessive bogons --- */
2073 if (f
& f_bogus
|| optind
== argc
) {
2078 /* --- Initialize the Catacomb random number generator --- */
2080 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2081 rand_seed(RAND_GLOBAL
, 160);
2083 /* --- Dispatch to appropriate command handler --- */
2088 return (findcmd(argv
[0])->cmd(argc
, argv
));
2091 /*----- That's all, folks -------------------------------------------------*/