3 * $Id: keyutil.c,v 1.22 2004/04/03 15:45:06 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.22 2004/04/03 15:45:06 mdw
34 * Oops. Fix formatting. :-S
36 * Revision 1.21 2004/04/03 15:15:19 mdw
37 * Fix stupid error in previous rashly-committed version.
39 * Revision 1.20 2004/04/03 15:14:28 mdw
40 * Handle points at infinity properly in listings.
42 * Revision 1.19 2004/04/03 03:31:01 mdw
43 * Allow explicit group parameters for DH groups.
45 * Revision 1.18 2004/04/01 12:50:09 mdw
46 * Add cyclic group abstraction, with test code. Separate off exponentation
47 * functions for better static linking. Fix a buttload of bugs on the way.
48 * Generally ensure that negative exponents do inversion correctly. Add
49 * table of standard prime-field subgroups. (Binary field subgroups are
50 * currently unimplemented but easy to add if anyone ever finds a good one.)
52 * Revision 1.17 2004/03/28 01:58:47 mdw
53 * Generate, store and retreive elliptic curve keys.
55 * Revision 1.16 2003/10/15 09:31:45 mdw
58 * Revision 1.15 2003/05/15 23:23:24 mdw
59 * Fix behaviour with bogus trailing attributes.
61 * Revision 1.14 2001/02/23 09:03:27 mdw
62 * Simplify usage message by removing nonexistant options.
64 * Revision 1.13 2001/02/21 20:04:27 mdw
65 * Provide help on individual commands (some need it desparately). Allow
66 * atomic retagging of keys.
68 * Revision 1.12 2001/02/03 11:58:22 mdw
69 * Store the correct seed information and count for DSA keys now that it's
72 * Revision 1.11 2000/12/06 20:33:27 mdw
73 * Make flags be macros rather than enumerations, to ensure that they're
76 * Revision 1.10 2000/10/08 12:02:21 mdw
77 * Use @MP_EQ@ instead of @MP_CMP@.
79 * Revision 1.9 2000/08/15 21:40:49 mdw
80 * Minor formatting change in listing attributes.
82 * Revision 1.8 2000/07/29 09:59:13 mdw
83 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
85 * Revision 1.7 2000/07/01 11:18:51 mdw
86 * Use new interfaces for key manipulation.
88 * Revision 1.6 2000/06/17 11:28:22 mdw
89 * Use secure memory interface from MP library. `rand_getgood' is
92 * Revision 1.5 2000/02/12 18:21:03 mdw
93 * Overhaul of key management (again).
95 * Revision 1.4 1999/12/22 15:48:10 mdw
96 * Track new key-management changes. Support new key generation
99 * Revision 1.3 1999/11/02 15:23:24 mdw
100 * Fix newlines in keyring list.
102 * Revision 1.2 1999/10/15 21:05:28 mdw
103 * In `key list', show timezone for local times, and support `-u' option
106 * Revision 1.1 1999/09/03 08:41:12 mdw
111 /*----- Header files ------------------------------------------------------*/
121 #include <mLib/base64.h>
122 #include <mLib/mdwopt.h>
123 #include <mLib/quis.h>
124 #include <mLib/report.h>
125 #include <mLib/sub.h>
149 #include "sha256-mgf.h"
150 #include "sha224-mgf.h"
151 #include "sha384-mgf.h"
152 #include "sha512-mgf.h"
153 #include "tiger-mgf.h"
154 #include "rmd128-mgf.h"
155 #include "rmd160-mgf.h"
156 #include "rmd256-mgf.h"
157 #include "rmd320-mgf.h"
161 /*----- Handy global state ------------------------------------------------*/
163 static const char *keyfile
= "keyring";
165 /*----- Useful shared functions -------------------------------------------*/
167 /* --- @doopen@ --- *
169 * Arguments: @key_file *f@ = pointer to key file block
170 * @unsigned how@ = method to open file with
174 * Use: Opens a key file and handles errors by panicking
178 static void doopen(key_file
*f
, unsigned how
)
180 if (key_open(f
, keyfile
, how
, key_moan
, 0))
181 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
184 /* --- @doclose@ --- *
186 * Arguments: @key_file *f@ = pointer to key file block
190 * Use: Closes a key file and handles errors by panicking
194 static void doclose(key_file
*f
)
196 switch (key_close(f
)) {
198 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
199 keyfile
, strerror(errno
));
201 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
202 keyfile
, strerror(errno
));
206 /* --- @setattr@ --- *
208 * Arguments: @key_file *f@ = pointer to key file block
209 * @key *k@ = pointer to key block
210 * @char *v[]@ = array of assignments (overwritten!)
214 * Use: Applies the attribute assignments to the key.
217 static void setattr(key_file
*f
, key
*k
, char *v
[])
222 size_t eq
= strcspn(p
, "=");
224 moan("invalid assignment: `%s' (ignored)", p
);
230 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
231 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
236 /*----- Seeding -----------------------------------------------------------*/
238 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
240 { "dsarand", dsarand_create
},
241 { "rmd128-mgf", rmd128_mgfrand
},
242 { "rmd160-mgf", rmd160_mgfrand
},
243 { "rmd256-mgf", rmd256_mgfrand
},
244 { "rmd320-mgf", rmd320_mgfrand
},
245 { "sha-mgf", sha_mgfrand
},
246 { "sha224-mgf", sha224_mgfrand
},
247 { "sha256-mgf", sha256_mgfrand
},
248 { "sha384-mgf", sha384_mgfrand
},
249 { "sha512-mgf", sha512_mgfrand
},
250 { "tiger-mgf", tiger_mgfrand
},
254 #define SEEDALG_DEFAULT (seedtab + 2)
256 /*----- Key generation ----------------------------------------------------*/
258 /* --- Key generation parameters --- */
260 typedef struct keyopts
{
261 key_file
*kf
; /* Pointer to key file */
262 key
*k
; /* Pointer to the actual key */
263 dstr tag
; /* Full tag name for the key */
264 unsigned f
; /* Flags for the new key */
265 unsigned bits
, qbits
; /* Bit length for the new key */
266 const char *curve
; /* Elliptic curve name/info */
267 grand
*r
; /* Random number source */
268 key
*p
; /* Parameters key-data */
271 #define f_bogus 1u /* Error in parsing */
272 #define f_lock 2u /* Passphrase-lock private key */
273 #define f_quiet 4u /* Don't show a progress indicator */
274 #define f_limlee 8u /* Generate Lim-Lee primes */
275 #define f_subgroup 16u /* Generate a subgroup */
276 #define f_retag 32u /* Remove any existing tag */
278 /* --- @dolock@ --- *
280 * Arguments: @keyopts *k@ = key generation options
281 * @key_data *kd@ = pointer to key data to lock
282 * @const char *t@ = tag suffix or null
286 * Use: Does passphrase locking on new keys.
289 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
291 if (!(k
->f
& f_lock
))
294 dstr_putf(&k
->tag
, ".%s", t
);
295 if (key_plock(k
->tag
.buf
, kd
, kd
))
296 die(EXIT_FAILURE
, "couldn't lock key");
301 * Arguments: @key_data *kd@ = pointer to parent key block
302 * @const char *tag@ = pointer to tag string
303 * @mp *m@ = integer to store
304 * @unsigned f@ = flags to set
308 * Use: Sets a multiprecision integer subkey.
311 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
313 key_data
*kkd
= key_structcreate(kd
, tag
);
318 /* --- @copyparam@ --- *
320 * Arguments: @keyopts *k@ = pointer to key options
321 * @const char **pp@ = checklist of parameters
323 * Returns: Nonzero if parameters copied; zero if you have to generate
326 * Use: Copies parameters from a source key to the current one.
329 static int copyparam(keyopts
*k
, const char **pp
)
333 /* --- Quick check if no parameters supplied --- */
338 /* --- Run through the checklist --- */
341 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
343 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
344 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
345 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
349 /* --- Copy over the parameters --- */
353 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
354 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
360 * Arguments: @key_data *k@ = pointer to key data block
361 * @const char *tag@ = tag string to use
363 * Returns: Pointer to multiprecision integer key item.
365 * Use: Fetches an MP key component.
368 static mp
*getmp(key_data
*k
, const char *tag
)
370 k
= key_structfind(k
, tag
);
372 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
373 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
374 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
378 /* --- @keyrand@ --- *
380 * Arguments: @key_file *kf@ = pointer to key file
381 * @const char *id@ = pointer to key id (or null)
385 * Use: Keys the random number generator.
388 static void keyrand(key_file
*kf
, const char *id
)
392 /* --- Find the key --- */
395 if ((k
= key_bytag(kf
, id
)) == 0)
396 die(EXIT_FAILURE
, "key `%s' not found", id
);
398 k
= key_bytype(kf
, "catacomb-rand");
401 key_data
*kd
= &k
->k
, kkd
;
404 switch (kd
->e
& KF_ENCMASK
) {
410 if (key_punlock(d
.buf
, kd
, &kkd
))
411 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
418 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
422 /* --- Key the generator --- */
424 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
430 /* --- Key generation algorithms --- */
432 static void alg_binary(keyopts
*k
)
441 die(EXIT_FAILURE
, "no shared parameters for binary keys");
443 sz
= (k
->bits
+ 7) >> 3;
445 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
446 k
->r
->ops
->fill(k
->r
, p
, sz
);
448 key_binary(&k
->k
->k
, p
, sz
);
449 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
452 dolock(k
, &k
->k
->k
, 0);
455 static void alg_des(keyopts
*k
)
464 die(EXIT_FAILURE
, "no shared parameters for DES keys");
465 if (k
->bits
% 56 || k
->bits
> 168)
466 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
470 k
->r
->ops
->fill(k
->r
, p
, sz
);
471 for (i
= 0; i
< sz
; i
++) {
472 octet x
= p
[i
] | 0x01;
476 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
478 key_binary(&k
->k
->k
, p
, sz
);
479 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
482 dolock(k
, &k
->k
->k
, 0);
485 static void alg_rsa(keyopts
*k
)
490 /* --- Sanity checking --- */
493 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
497 /* --- Generate the RSA parameters --- */
499 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
500 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
501 die(EXIT_FAILURE
, "RSA key generation failed");
503 /* --- Run a test encryption --- */
506 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
508 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
513 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
514 c
= rsa_qprivop(&rp
, c
, c
, g
);
517 die(EXIT_FAILURE
, "test encryption failed");
523 /* --- Allrighty then --- */
527 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
528 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
530 kd
= key_structcreate(kd
, "private");
532 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
533 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
534 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
535 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
536 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
537 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
538 dolock(k
, kd
, "private");
543 static void alg_dsaparam(keyopts
*k
)
545 static const char *pl
[] = { "q", "p", "g", 0 };
546 if (!copyparam(k
, pl
)) {
552 key_data
*kd
= &k
->k
->k
;
555 /* --- Choose appropriate bit lengths if necessary --- */
562 /* --- Allocate a seed block --- */
564 sz
= (k
->qbits
+ 7) >> 3;
566 k
->r
->ops
->fill(k
->r
, p
, sz
);
568 /* --- Allocate the parameters --- */
570 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
571 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
572 die(EXIT_FAILURE
, "DSA parameter generation failed");
574 /* --- Store the parameters --- */
577 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
578 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
579 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
584 /* --- Store the seed for future verification --- */
589 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
590 base64_encode(&c
, 0, 0, &d
);
591 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
593 dstr_putf(&d
, "%u", ds
.count
);
594 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
601 static void alg_dsa(keyopts
*k
)
606 key_data
*kd
= &k
->k
->k
;
608 /* --- Get the shared parameters --- */
615 /* --- Choose a private key --- */
617 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
618 mpmont_create(&mm
, p
);
619 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
621 /* --- Store everything away --- */
623 mpkey(kd
, "y", y
, KCAT_PUB
);
625 kd
= key_structcreate(kd
, "private");
627 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
628 dolock(k
, kd
, "private");
630 mp_drop(x
); mp_drop(y
);
633 static void alg_dhparam(keyopts
*k
)
635 static const char *pl
[] = { "p", "q", "g", 0 };
636 key_data
*kd
= &k
->k
->k
;
637 if (!copyparam(k
, pl
)) {
644 if (strcmp(k
->curve
, "list") == 0) {
646 printf("Built-in prime groups:\n");
647 for (pe
= ptab
; pe
->name
; pe
++)
648 printf(" %s\n", pe
->name
);
652 if (dh_parse(&qd
, &dp
))
653 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
660 /* --- Choose a large safe prime number --- */
662 if (k
->f
& f_limlee
) {
667 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
668 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
669 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
670 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
674 for (i
= 0; i
< nf
; i
++) {
677 mp_writedstr(f
[i
], &d
, 10);
680 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
684 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
685 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
688 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
692 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
693 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
694 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
701 static void alg_dh(keyopts
*k
)
706 key_data
*kd
= &k
->k
->k
;
708 /* --- Get the shared parameters --- */
715 /* --- Choose a suitable private key --- *
717 * Since %$g$% has order %$q$%, choose %$x < q$%.
720 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
722 /* --- Compute the public key %$y = g^x \bmod p$% --- */
724 mpmont_create(&mm
, p
);
725 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
728 /* --- Store everything away --- */
730 mpkey(kd
, "y", y
, KCAT_PUB
);
732 kd
= key_structcreate(kd
, "private");
734 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
735 dolock(k
, kd
, "private");
737 mp_drop(x
); mp_drop(y
);
740 static void alg_bbs(keyopts
*k
)
745 /* --- Sanity checking --- */
748 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
752 /* --- Generate the BBS parameters --- */
754 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
755 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
756 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
758 /* --- Allrighty then --- */
762 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
764 kd
= key_structcreate(kd
, "private");
766 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
767 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
768 dolock(k
, kd
, "private");
773 static void alg_ecparam(keyopts
*k
)
775 static const char *pl
[] = { "curve", 0 };
776 if (!copyparam(k
, pl
)) {
779 key_data
*kd
= &k
->k
->k
;
781 /* --- Decide on a curve --- */
783 if (!k
->bits
) k
->bits
= 256;
784 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
786 printf("Built-in elliptic curves:\n");
787 for (ee
= ectab
; ee
->name
; ee
++)
788 printf(" %s\n", ee
->name
);
792 if (k
->bits
<= 56) k
->curve
= "secp112r1";
793 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
794 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
795 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
796 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
797 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
798 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
799 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
801 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
805 /* --- Check it --- */
807 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
808 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
809 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
810 moan("WARNING! curve check failed: %s", e
);
813 /* --- Write out the answer --- */
816 kd
= key_structcreate(kd
, "curve");
817 key_string(kd
, k
->curve
);
822 static void alg_ec(keyopts
*k
)
824 key_data
*kd
= &k
->k
->k
;
831 /* --- Get the curve --- */
834 if ((kkd
= key_structfind(kd
, "curve")) == 0)
835 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
836 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
837 die(EXIT_FAILURE
, "subkey `curve' is not a string");
838 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
839 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
841 /* --- Invent a private exponent and compute the public key --- */
843 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
844 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
846 /* --- Store everything away --- */
848 kkd
= key_structcreate(kd
, "p");
851 kkd
= key_structcreate(kd
, "private");
853 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
861 /* --- The algorithm tables --- */
863 typedef struct keyalg
{
865 void (*proc
)(keyopts
*o
);
869 static keyalg algtab
[] = {
870 { "binary", alg_binary
, "Plain binary data" },
871 { "des", alg_des
, "Binary with DES-style parity" },
872 { "rsa", alg_rsa
, "RSA public-key encryption" },
873 { "dsa", alg_dsa
, "DSA digital signatures" },
874 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
875 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
876 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
877 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
878 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
879 { "ec", alg_ec
, "Elliptic curve crypto" },
883 /* --- @cmd_add@ --- */
885 static int cmd_add(int argc
, char *argv
[])
888 time_t exp
= KEXP_EXPIRE
;
889 const char *tag
= 0, *ptag
= 0;
891 keyalg
*alg
= algtab
;
892 const char *rtag
= 0;
893 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
894 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
895 const char *seed
= 0;
898 /* --- Parse options for the subcommand --- */
901 static struct option opt
[] = {
902 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
903 { "bits", OPTF_ARGREQ
, 0, 'b' },
904 { "qbits", OPTF_ARGREQ
, 0, 'B' },
905 { "parameters", OPTF_ARGREQ
, 0, 'p' },
906 { "expire", OPTF_ARGREQ
, 0, 'e' },
907 { "comment", OPTF_ARGREQ
, 0, 'c' },
908 { "tag", OPTF_ARGREQ
, 0, 't' },
909 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
910 { "curve", OPTF_ARGREQ
, 0, 'C' },
911 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
912 { "seed", OPTF_ARGREQ
, 0, 's' },
913 { "newseed", OPTF_ARGREQ
, 0, 'n' },
914 { "lock", 0, 0, 'l' },
915 { "quiet", 0, 0, 'q' },
916 { "lim-lee", 0, 0, 'L' },
917 { "subgroup", 0, 0, 'S' },
920 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
925 /* --- Handle the various options --- */
929 /* --- Read an algorithm name --- */
933 size_t sz
= strlen(optarg
);
935 if (strcmp(optarg
, "list") == 0) {
936 for (a
= algtab
; a
->name
; a
++)
937 printf("%-10s %s\n", a
->name
, a
->help
);
942 for (a
= algtab
; a
->name
; a
++) {
943 if (strncmp(optarg
, a
->name
, sz
) == 0) {
944 if (a
->name
[sz
] == 0) {
948 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
954 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
957 /* --- Bits must be nonzero and a multiple of 8 --- */
961 k
.bits
= strtoul(optarg
, &p
, 0);
962 if (k
.bits
== 0 || *p
!= 0)
963 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
968 k
.qbits
= strtoul(optarg
, &p
, 0);
969 if (k
.qbits
== 0 || *p
!= 0)
970 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
973 /* --- Parameter selection --- */
979 /* --- Expiry dates get passed to @get_date@ for parsing --- */
982 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
985 exp
= get_date(optarg
, 0);
987 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
991 /* --- Store comments without interpretation --- */
994 if (key_chkcomment(optarg
))
995 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
999 /* --- Elliptic curve parameters --- */
1005 /* --- Store tags --- */
1008 if (key_chkident(optarg
))
1009 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1016 /* --- Seeding --- */
1019 const struct seedalg
*ss
;
1020 if (strcmp(optarg
, "list") == 0) {
1021 printf("Seed algorithms:\n");
1022 for (ss
= seedtab
; ss
->p
; ss
++)
1023 printf(" %s\n", ss
->p
);
1026 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1028 for (ss
= seedtab
; ss
->p
; ss
++) {
1029 if (strcmp(optarg
, ss
->p
) == 0)
1033 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1039 if (seed
) die(EXIT_FAILURE
, "seed already set");
1041 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1042 base64_decode(&b
, 0, 0, &d
);
1043 k
.r
= sa
->gen(d
.buf
, d
.len
);
1052 unsigned n
= strtoul(optarg
, &p
, 0);
1053 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1054 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1055 if (seed
) die(EXIT_FAILURE
, "seed already set");
1058 rand_get(RAND_GLOBAL
, p
, n
);
1060 base64_encode(&b
, p
, n
, &d
);
1061 base64_encode(&b
, 0, 0, &d
);
1063 k
.r
= sa
->gen(p
, n
);
1066 /* --- Other flags --- */
1084 /* --- Other things are bogus --- */
1092 /* --- Various sorts of bogosity --- */
1094 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1096 "Usage: add [options] type [attr...]");
1098 if (key_chkident(argv
[optind
]))
1099 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1101 /* --- Set up various bits of the state --- */
1103 if (exp
== KEXP_EXPIRE
)
1104 exp
= time(0) + 14 * 24 * 60 * 60;
1106 /* --- Open the file and create the basic key block --- *
1108 * Keep on generating keyids until one of them doesn't collide.
1111 doopen(&f
, KOPEN_WRITE
);
1114 /* --- Key the generator --- */
1119 uint32 id
= rand_global
.ops
->word(&rand_global
);
1121 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1124 if (err
!= KERR_DUPID
)
1125 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1128 /* --- Set various simple attributes --- */
1133 if (k
.f
& f_retag
) {
1134 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1135 key_settag(&f
, kk
, 0);
1137 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1138 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1142 int err
= key_setcomment(&f
, k
.k
, c
);
1144 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1147 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1149 key_putattr(&f
, k
.k
, "genseed", seed
);
1150 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1153 key_fulltag(k
.k
, &k
.tag
);
1155 /* --- Find the parameter key --- */
1158 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1159 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1160 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1161 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1164 /* --- Now generate the actual key data --- */
1174 /*----- Key listing -------------------------------------------------------*/
1176 /* --- Listing options --- */
1178 typedef struct listopts
{
1179 const char *tfmt
; /* Date format (@strftime@-style) */
1180 int v
; /* Verbosity level */
1181 unsigned f
; /* Various flags */
1182 time_t t
; /* Time now (for key expiry) */
1183 key_filter kf
; /* Filter for matching keys */
1186 /* --- Listing flags --- */
1188 #define f_newline 2u /* Write newline before next entry */
1189 #define f_attr 4u /* Written at least one attribute */
1190 #define f_utc 8u /* Emit UTC time, not local time */
1192 /* --- @showkeydata@ --- *
1194 * Arguments: @key_data *k@ = pointer to key to write
1195 * @int ind@ = indentation level
1196 * @listopts *o@ = listing options
1197 * @dstr *d@ = tag string for this subkey
1201 * Use: Emits a piece of key data in a human-readable format.
1204 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1206 #define INDENT(i) do { \
1208 for (_i = 0; _i < (i); _i++) { \
1213 switch (k
->e
& KF_ENCMASK
) {
1215 /* --- Binary key data --- *
1217 * Emit as a simple hex dump.
1221 const octet
*p
= k
->u
.k
.k
;
1222 const octet
*l
= p
+ k
->u
.k
.sz
;
1225 fputs(" {", stdout
);
1230 } else if (sz
% 8 == 0)
1234 printf("%02x", *p
++);
1239 fputs("}\n", stdout
);
1242 /* --- Encrypted data --- *
1244 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1245 * key. Otherwise just say that it's encrypted and move on.
1250 fputs(" encrypted\n", stdout
);
1253 if (key_punlock(d
->buf
, k
, &kd
))
1254 printf(" <failed to unlock %s>\n", d
->buf
);
1256 fputs(" encrypted", stdout
);
1257 showkeydata(&kd
, ind
, o
, d
);
1263 /* --- Integer keys --- *
1265 * Emit as a large integer in decimal. This makes using the key in
1266 * `calc' or whatever easier.
1271 mp_writefile(k
->u
.m
, stdout
, 10);
1275 /* --- Strings --- */
1278 printf(" `%s'\n", k
->u
.p
);
1281 /* --- Elliptic curve points --- */
1284 if (EC_ATINF(&k
->u
.e
))
1285 fputs(" inf\n", stdout
);
1287 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1288 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1293 /* --- Structured keys --- *
1295 * Just iterate over the subkeys.
1303 fputs(" {\n", stdout
);
1304 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1305 if (!key_match(&ks
->k
, &o
->kf
))
1308 printf("%s =", SYM_NAME(ks
));
1310 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1311 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1314 fputs("}\n", stdout
);
1321 /* --- @showkey@ --- *
1323 * Arguments: @key *k@ = pointer to key to show
1324 * @listopts *o@ = pointer to listing options
1328 * Use: Emits a listing of a particular key.
1331 static void showkey(key
*k
, listopts
*o
)
1333 char ebuf
[24], dbuf
[24];
1336 /* --- Skip the key if the filter doesn't match --- */
1338 if (!key_match(&k
->k
, &o
->kf
))
1341 /* --- Sort out the expiry and deletion times --- */
1343 if (KEY_EXPIRED(o
->t
, k
->exp
))
1344 strcpy(ebuf
, "expired");
1345 else if (k
->exp
== KEXP_FOREVER
)
1346 strcpy(ebuf
, "forever");
1348 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1349 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1352 if (KEY_EXPIRED(o
->t
, k
->del
))
1353 strcpy(dbuf
, "deleted");
1354 else if (k
->del
== KEXP_FOREVER
)
1355 strcpy(dbuf
, "forever");
1357 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1358 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1361 /* --- If in compact format, just display and quit --- */
1364 if (!(o
->f
& f_newline
)) {
1365 printf("%8s %-20s %-20s %-10s %-10s\n",
1366 "Id", "Tag", "Type", "Expire", "Delete");
1368 printf("%08lx %-20s %-20s %-10s %-10s\n",
1369 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1370 k
->type
, ebuf
, dbuf
);
1375 /* --- Display the standard header --- */
1377 if (o
->f
& f_newline
)
1378 fputc('\n', stdout
);
1379 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1380 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1381 printf("type: %s\n", k
->type
);
1382 printf("expiry: %s\n", ebuf
);
1383 printf("delete: %s\n", dbuf
);
1384 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1386 /* --- Display the attributes --- */
1390 const char *av
, *an
;
1393 printf("attributes:");
1394 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1395 printf("\n %s = %s", an
, av
);
1399 fputc('\n', stdout
);
1404 /* --- If dumping requested, dump the raw key data --- */
1408 fputs("key:", stdout
);
1410 showkeydata(&k
->k
, 0, o
, &d
);
1417 /* --- @cmd_list@ --- */
1419 static int cmd_list(int argc
, char *argv
[])
1423 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1425 /* --- Parse subcommand options --- */
1428 static struct option opt
[] = {
1429 { "quiet", 0, 0, 'q' },
1430 { "verbose", 0, 0, 'v' },
1431 { "utc", 0, 0, 'u' },
1432 { "filter", OPTF_ARGREQ
, 0, 'f' },
1435 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1452 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1454 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1463 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1465 /* --- Open the key file --- */
1467 doopen(&f
, KOPEN_READ
);
1470 /* --- Set up the time format --- */
1473 o
.tfmt
= "%Y-%m-%d";
1474 else if (o
.f
& f_utc
)
1475 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1477 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1479 /* --- If specific keys were requested use them, otherwise do all --- *
1481 * Some day, this might turn into a wildcard match.
1484 if (optind
< argc
) {
1486 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1489 moan("key `%s' not found", argv
[optind
]);
1493 } while (optind
< argc
);
1496 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1504 return (EXIT_FAILURE
);
1509 /*----- Command implementation --------------------------------------------*/
1511 /* --- @cmd_expire@ --- */
1513 static int cmd_expire(int argc
, char *argv
[])
1521 die(EXIT_FAILURE
, "Usage: expire tag...");
1522 doopen(&f
, KOPEN_WRITE
);
1523 for (i
= 1; i
< argc
; i
++) {
1524 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1527 moan("key `%s' not found", argv
[i
]);
1535 /* --- @cmd_delete@ --- */
1537 static int cmd_delete(int argc
, char *argv
[])
1545 die(EXIT_FAILURE
, "Usage: delete tag...");
1546 doopen(&f
, KOPEN_WRITE
);
1547 for (i
= 1; i
< argc
; i
++) {
1548 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1551 moan("key `%s' not found", argv
[i
]);
1559 /* --- @cmd_setattr@ --- */
1561 static int cmd_setattr(int argc
, char *argv
[])
1567 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1568 doopen(&f
, KOPEN_WRITE
);
1569 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1570 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1571 setattr(&f
, k
, argv
+ 2);
1576 /* --- @cmd_finger@ --- */
1578 static void fingerprint(key
*k
, const key_filter
*kf
)
1581 octet hash
[RMD160_HASHSZ
];
1585 if (!key_encode(&k
->k
, &d
, kf
))
1588 rmd160_hash(&r
, d
.buf
, d
.len
);
1589 rmd160_done(&r
, hash
);
1593 for (i
= 0; i
< sizeof(hash
); i
++) {
1594 if (i
&& i
% 4 == 0)
1596 printf("%02x", hash
[i
]);
1598 printf(" %s\n", d
.buf
);
1602 static int cmd_finger(int argc
, char *argv
[])
1606 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1609 static struct option opt
[] = {
1610 { "filter", OPTF_ARGREQ
, 0, 'f' },
1613 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1619 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1621 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1629 argv
+= optind
; argc
-= optind
;
1631 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1633 doopen(&f
, KOPEN_READ
);
1637 for (i
= 0; i
< argc
; i
++) {
1638 key
*k
= key_bytag(&f
, argv
[i
]);
1640 fingerprint(k
, &kf
);
1643 moan("key `%s' not found", argv
[i
]);
1649 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1650 fingerprint(k
, &kf
);
1655 /* --- @cmd_comment@ --- */
1657 static int cmd_comment(int argc
, char *argv
[])
1663 if (argc
< 2 || argc
> 3)
1664 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1665 doopen(&f
, KOPEN_WRITE
);
1666 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1667 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1668 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1669 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1674 /* --- @cmd_tag@ --- */
1676 static int cmd_tag(int argc
, char *argv
[])
1685 static struct option opt
[] = {
1686 { "retag", 0, 0, 'r' },
1689 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1702 argv
+= optind
; argc
-= optind
;
1703 if (argc
< 1 || argc
> 2 || rc
)
1704 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1705 doopen(&f
, KOPEN_WRITE
);
1706 if (flags
& f_retag
) {
1707 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1708 key_settag(&f
, k
, 0);
1710 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1711 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1712 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1713 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1718 /* --- @cmd_lock@ --- */
1720 static int cmd_lock(int argc
, char *argv
[])
1728 die(EXIT_FAILURE
, "Usage: lock qtag");
1729 doopen(&f
, KOPEN_WRITE
);
1730 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1731 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1732 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1733 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1734 if (key_plock(d
.buf
, kd
, kd
))
1735 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1741 /* --- @cmd_unlock@ --- */
1743 static int cmd_unlock(int argc
, char *argv
[])
1751 die(EXIT_FAILURE
, "Usage: unlock qtag");
1752 doopen(&f
, KOPEN_WRITE
);
1753 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1754 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1755 if (kd
->e
!= KENC_ENCRYPT
)
1756 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1757 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1758 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1764 /* --- @cmd_extract@ --- */
1766 static int cmd_extract(int argc
, char *argv
[])
1772 key_filter kf
= { 0, 0 };
1776 static struct option opt
[] = {
1777 { "filter", OPTF_ARGREQ
, 0, 'f' },
1780 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1786 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1788 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1796 argv
+= optind
; argc
-= optind
;
1798 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1799 if (strcmp(*argv
, "-") == 0)
1801 else if (!(fp
= fopen(*argv
, "w"))) {
1802 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1803 *argv
, strerror(errno
));
1806 doopen(&f
, KOPEN_READ
);
1810 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1811 key_extract(&f
, k
, fp
, &kf
);
1813 for (i
= 1; i
< argc
; i
++) {
1814 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1815 key_extract(&f
, k
, fp
, &kf
);
1817 moan("key `%s' not found", argv
[i
]);
1823 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1828 /* --- @cmd_tidy@ --- */
1830 static int cmd_tidy(int argc
, char *argv
[])
1834 die(EXIT_FAILURE
, "usage: tidy");
1835 doopen(&f
, KOPEN_WRITE
);
1836 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1841 /* --- @cmd_merge@ --- */
1843 static int cmd_merge(int argc
, char *argv
[])
1849 die(EXIT_FAILURE
, "Usage: merge file");
1850 if (strcmp(argv
[1], "-") == 0)
1852 else if (!(fp
= fopen(argv
[1], "r"))) {
1853 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1854 argv
[1], strerror(errno
));
1857 doopen(&f
, KOPEN_WRITE
);
1858 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1863 /*----- Main command table ------------------------------------------------*/
1867 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1872 "add [options] type [attr...]\n\
1873 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1874 [-e expire] [-t tag] [-c comment]", "\
1877 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1878 -b, --bits=N Generate an N-bit key.\n\
1879 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1880 -p, --parameters=TAG Get group parameters from TAG.\n\
1881 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1882 -e, --expire=TIME Make the key expire after TIME.\n\
1883 -c, --comment=STRING Attach the command STRING to the key.\n\
1884 -t, --tag=TAG Tag the key with the name TAG.\n\
1885 -r, --retag Untag any key currently with that tag.\n\
1886 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1887 -l, --lock Lock the generated key with a passphrase.\n\
1888 -q, --quiet Don't give progress indicators while working.\n\
1889 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1890 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1892 { "expire", cmd_expire
, "expire tag..." },
1893 { "delete", cmd_delete
, "delete tag..." },
1894 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1897 -r, --retag Untag any key currently called new-tag.\n\
1899 { "setattr", cmd_setattr
, "setattr tag attr..." },
1900 { "comment", cmd_comment
, "comment tag [comment]" },
1901 { "lock", cmd_lock
, "lock qtag" },
1902 { "unlock", cmd_unlock
, "unlock qtag" },
1903 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1906 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1907 -q, --quiet Show less information.\n\
1908 -v, --verbose Show more information.\n\
1910 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1913 -f, --filter=FILT Only hash key components matching FILT.\n\
1915 { "tidy", cmd_tidy
, "tidy" },
1916 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1919 -f, --filter=FILT Only extract key components matching FILT.\n\
1921 { "merge", cmd_merge
, "merge file" },
1925 typedef struct cmd cmd
;
1927 /*----- Main code ---------------------------------------------------------*/
1929 /* --- @findcmd@ --- *
1931 * Arguments: @const char *name@ = a command name
1933 * Returns: Pointer to the command structure.
1935 * Use: Looks up a command by name. If the command isn't found, an
1936 * error is reported and the program is terminated.
1939 static cmd
*findcmd(const char *name
)
1941 cmd
*c
, *chosen
= 0;
1942 size_t sz
= strlen(name
);
1944 for (c
= cmds
; c
->name
; c
++) {
1945 if (strncmp(name
, c
->name
, sz
) == 0) {
1946 if (c
->name
[sz
] == 0) {
1950 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1956 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1960 /* --- Helpful GNUy functions --- */
1962 void usage(FILE *fp
)
1964 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1967 void version(FILE *fp
)
1969 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1972 void help(FILE *fp
, char **argv
)
1980 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1988 Performs various simple key management operations. Command line options\n\
1991 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1992 -v, --version Display version number.\n\
1993 -u, --usage Display short usage summary.\n\
1995 -k, --keyring=FILE Read and write keys in FILE.\n\
1996 -i, --id=TAG Use key TAG for random number generator.\n\
1997 -t, --type=TYPE Use key TYPE for random number generator.\n\
1999 The following commands are understood:\n\n",
2001 for (c
= cmds
; c
->name
; c
++)
2002 fprintf(fp
, "%s\n", c
->usage
);
2008 * Arguments: @int argc@ = number of command line arguments
2009 * @char *argv[]@ = array of command line arguments
2011 * Returns: Nonzero on failure.
2013 * Use: Main program. Performs simple key management functions.
2016 int main(int argc
, char *argv
[])
2022 /* --- Initialization --- */
2027 /* --- Parse command line options --- */
2030 static struct option opt
[] = {
2032 /* --- Standard GNUy help options --- */
2034 { "help", 0, 0, 'h' },
2035 { "version", 0, 0, 'v' },
2036 { "usage", 0, 0, 'u' },
2038 /* --- Real live useful options --- */
2040 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2042 /* --- Magic terminator --- */
2046 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2052 /* --- GNU help options --- */
2055 help(stdout
, argv
+ optind
);
2064 /* --- Real useful options --- */
2070 /* --- Bogosity --- */
2078 /* --- Complain about excessive bogons --- */
2080 if (f
& f_bogus
|| optind
== argc
) {
2085 /* --- Initialize the Catacomb random number generator --- */
2087 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2088 rand_seed(RAND_GLOBAL
, 160);
2090 /* --- Dispatch to appropriate command handler --- */
2095 return (findcmd(argv
[0])->cmd(argc
, argv
));
2098 /*----- That's all, folks -------------------------------------------------*/