3 * $Id: keyutil.c,v 1.21 2004/04/03 15:15:19 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.21 2004/04/03 15:15:19 mdw
34 * Fix stupid error in previous rashly-committed version.
36 * Revision 1.20 2004/04/03 15:14:28 mdw
37 * Handle points at infinity properly in listings.
39 * Revision 1.19 2004/04/03 03:31:01 mdw
40 * Allow explicit group parameters for DH groups.
42 * Revision 1.18 2004/04/01 12:50:09 mdw
43 * Add cyclic group abstraction, with test code. Separate off exponentation
44 * functions for better static linking. Fix a buttload of bugs on the way.
45 * Generally ensure that negative exponents do inversion correctly. Add
46 * table of standard prime-field subgroups. (Binary field subgroups are
47 * currently unimplemented but easy to add if anyone ever finds a good one.)
49 * Revision 1.17 2004/03/28 01:58:47 mdw
50 * Generate, store and retreive elliptic curve keys.
52 * Revision 1.16 2003/10/15 09:31:45 mdw
55 * Revision 1.15 2003/05/15 23:23:24 mdw
56 * Fix behaviour with bogus trailing attributes.
58 * Revision 1.14 2001/02/23 09:03:27 mdw
59 * Simplify usage message by removing nonexistant options.
61 * Revision 1.13 2001/02/21 20:04:27 mdw
62 * Provide help on individual commands (some need it desparately). Allow
63 * atomic retagging of keys.
65 * Revision 1.12 2001/02/03 11:58:22 mdw
66 * Store the correct seed information and count for DSA keys now that it's
69 * Revision 1.11 2000/12/06 20:33:27 mdw
70 * Make flags be macros rather than enumerations, to ensure that they're
73 * Revision 1.10 2000/10/08 12:02:21 mdw
74 * Use @MP_EQ@ instead of @MP_CMP@.
76 * Revision 1.9 2000/08/15 21:40:49 mdw
77 * Minor formatting change in listing attributes.
79 * Revision 1.8 2000/07/29 09:59:13 mdw
80 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
82 * Revision 1.7 2000/07/01 11:18:51 mdw
83 * Use new interfaces for key manipulation.
85 * Revision 1.6 2000/06/17 11:28:22 mdw
86 * Use secure memory interface from MP library. `rand_getgood' is
89 * Revision 1.5 2000/02/12 18:21:03 mdw
90 * Overhaul of key management (again).
92 * Revision 1.4 1999/12/22 15:48:10 mdw
93 * Track new key-management changes. Support new key generation
96 * Revision 1.3 1999/11/02 15:23:24 mdw
97 * Fix newlines in keyring list.
99 * Revision 1.2 1999/10/15 21:05:28 mdw
100 * In `key list', show timezone for local times, and support `-u' option
103 * Revision 1.1 1999/09/03 08:41:12 mdw
108 /*----- Header files ------------------------------------------------------*/
118 #include <mLib/base64.h>
119 #include <mLib/mdwopt.h>
120 #include <mLib/quis.h>
121 #include <mLib/report.h>
122 #include <mLib/sub.h>
146 #include "sha256-mgf.h"
147 #include "sha224-mgf.h"
148 #include "sha384-mgf.h"
149 #include "sha512-mgf.h"
150 #include "tiger-mgf.h"
151 #include "rmd128-mgf.h"
152 #include "rmd160-mgf.h"
153 #include "rmd256-mgf.h"
154 #include "rmd320-mgf.h"
158 /*----- Handy global state ------------------------------------------------*/
160 static const char *keyfile
= "keyring";
162 /*----- Useful shared functions -------------------------------------------*/
164 /* --- @doopen@ --- *
166 * Arguments: @key_file *f@ = pointer to key file block
167 * @unsigned how@ = method to open file with
171 * Use: Opens a key file and handles errors by panicking
175 static void doopen(key_file
*f
, unsigned how
)
177 if (key_open(f
, keyfile
, how
, key_moan
, 0))
178 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
181 /* --- @doclose@ --- *
183 * Arguments: @key_file *f@ = pointer to key file block
187 * Use: Closes a key file and handles errors by panicking
191 static void doclose(key_file
*f
)
193 switch (key_close(f
)) {
195 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
196 keyfile
, strerror(errno
));
198 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
199 keyfile
, strerror(errno
));
203 /* --- @setattr@ --- *
205 * Arguments: @key_file *f@ = pointer to key file block
206 * @key *k@ = pointer to key block
207 * @char *v[]@ = array of assignments (overwritten!)
211 * Use: Applies the attribute assignments to the key.
214 static void setattr(key_file
*f
, key
*k
, char *v
[])
219 size_t eq
= strcspn(p
, "=");
221 moan("invalid assignment: `%s' (ignored)", p
);
227 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
228 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
233 /*----- Seeding -----------------------------------------------------------*/
235 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
237 { "dsarand", dsarand_create
},
238 { "rmd128-mgf", rmd128_mgfrand
},
239 { "rmd160-mgf", rmd160_mgfrand
},
240 { "rmd256-mgf", rmd256_mgfrand
},
241 { "rmd320-mgf", rmd320_mgfrand
},
242 { "sha-mgf", sha_mgfrand
},
243 { "sha224-mgf", sha224_mgfrand
},
244 { "sha256-mgf", sha256_mgfrand
},
245 { "sha384-mgf", sha384_mgfrand
},
246 { "sha512-mgf", sha512_mgfrand
},
247 { "tiger-mgf", tiger_mgfrand
},
251 #define SEEDALG_DEFAULT (seedtab + 2)
253 /*----- Key generation ----------------------------------------------------*/
255 /* --- Key generation parameters --- */
257 typedef struct keyopts
{
258 key_file
*kf
; /* Pointer to key file */
259 key
*k
; /* Pointer to the actual key */
260 dstr tag
; /* Full tag name for the key */
261 unsigned f
; /* Flags for the new key */
262 unsigned bits
, qbits
; /* Bit length for the new key */
263 const char *curve
; /* Elliptic curve name/info */
264 grand
*r
; /* Random number source */
265 key
*p
; /* Parameters key-data */
268 #define f_bogus 1u /* Error in parsing */
269 #define f_lock 2u /* Passphrase-lock private key */
270 #define f_quiet 4u /* Don't show a progress indicator */
271 #define f_limlee 8u /* Generate Lim-Lee primes */
272 #define f_subgroup 16u /* Generate a subgroup */
273 #define f_retag 32u /* Remove any existing tag */
275 /* --- @dolock@ --- *
277 * Arguments: @keyopts *k@ = key generation options
278 * @key_data *kd@ = pointer to key data to lock
279 * @const char *t@ = tag suffix or null
283 * Use: Does passphrase locking on new keys.
286 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
288 if (!(k
->f
& f_lock
))
291 dstr_putf(&k
->tag
, ".%s", t
);
292 if (key_plock(k
->tag
.buf
, kd
, kd
))
293 die(EXIT_FAILURE
, "couldn't lock key");
298 * Arguments: @key_data *kd@ = pointer to parent key block
299 * @const char *tag@ = pointer to tag string
300 * @mp *m@ = integer to store
301 * @unsigned f@ = flags to set
305 * Use: Sets a multiprecision integer subkey.
308 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
310 key_data
*kkd
= key_structcreate(kd
, tag
);
315 /* --- @copyparam@ --- *
317 * Arguments: @keyopts *k@ = pointer to key options
318 * @const char **pp@ = checklist of parameters
320 * Returns: Nonzero if parameters copied; zero if you have to generate
323 * Use: Copies parameters from a source key to the current one.
326 static int copyparam(keyopts
*k
, const char **pp
)
330 /* --- Quick check if no parameters supplied --- */
335 /* --- Run through the checklist --- */
338 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
340 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
341 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
342 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
346 /* --- Copy over the parameters --- */
350 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
351 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
357 * Arguments: @key_data *k@ = pointer to key data block
358 * @const char *tag@ = tag string to use
360 * Returns: Pointer to multiprecision integer key item.
362 * Use: Fetches an MP key component.
365 static mp
*getmp(key_data
*k
, const char *tag
)
367 k
= key_structfind(k
, tag
);
369 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
370 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
371 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
375 /* --- @keyrand@ --- *
377 * Arguments: @key_file *kf@ = pointer to key file
378 * @const char *id@ = pointer to key id (or null)
382 * Use: Keys the random number generator.
385 static void keyrand(key_file
*kf
, const char *id
)
389 /* --- Find the key --- */
392 if ((k
= key_bytag(kf
, id
)) == 0)
393 die(EXIT_FAILURE
, "key `%s' not found", id
);
395 k
= key_bytype(kf
, "catacomb-rand");
398 key_data
*kd
= &k
->k
, kkd
;
401 switch (kd
->e
& KF_ENCMASK
) {
407 if (key_punlock(d
.buf
, kd
, &kkd
))
408 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
415 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
419 /* --- Key the generator --- */
421 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
427 /* --- Key generation algorithms --- */
429 static void alg_binary(keyopts
*k
)
438 die(EXIT_FAILURE
, "no shared parameters for binary keys");
440 sz
= (k
->bits
+ 7) >> 3;
442 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
443 k
->r
->ops
->fill(k
->r
, p
, sz
);
445 key_binary(&k
->k
->k
, p
, sz
);
446 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
449 dolock(k
, &k
->k
->k
, 0);
452 static void alg_des(keyopts
*k
)
461 die(EXIT_FAILURE
, "no shared parameters for DES keys");
462 if (k
->bits
% 56 || k
->bits
> 168)
463 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
467 k
->r
->ops
->fill(k
->r
, p
, sz
);
468 for (i
= 0; i
< sz
; i
++) {
469 octet x
= p
[i
] | 0x01;
473 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
475 key_binary(&k
->k
->k
, p
, sz
);
476 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
479 dolock(k
, &k
->k
->k
, 0);
482 static void alg_rsa(keyopts
*k
)
487 /* --- Sanity checking --- */
490 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
494 /* --- Generate the RSA parameters --- */
496 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
497 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
498 die(EXIT_FAILURE
, "RSA key generation failed");
500 /* --- Run a test encryption --- */
503 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
505 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
510 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
511 c
= rsa_qprivop(&rp
, c
, c
, g
);
514 die(EXIT_FAILURE
, "test encryption failed");
520 /* --- Allrighty then --- */
524 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
525 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
527 kd
= key_structcreate(kd
, "private");
529 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
530 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
531 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
532 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
533 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
534 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
535 dolock(k
, kd
, "private");
540 static void alg_dsaparam(keyopts
*k
)
542 static const char *pl
[] = { "q", "p", "g", 0 };
543 if (!copyparam(k
, pl
)) {
549 key_data
*kd
= &k
->k
->k
;
552 /* --- Choose appropriate bit lengths if necessary --- */
559 /* --- Allocate a seed block --- */
561 sz
= (k
->qbits
+ 7) >> 3;
563 k
->r
->ops
->fill(k
->r
, p
, sz
);
565 /* --- Allocate the parameters --- */
567 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
568 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
569 die(EXIT_FAILURE
, "DSA parameter generation failed");
571 /* --- Store the parameters --- */
574 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
575 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
576 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
581 /* --- Store the seed for future verification --- */
586 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
587 base64_encode(&c
, 0, 0, &d
);
588 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
590 dstr_putf(&d
, "%u", ds
.count
);
591 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
598 static void alg_dsa(keyopts
*k
)
603 key_data
*kd
= &k
->k
->k
;
605 /* --- Get the shared parameters --- */
612 /* --- Choose a private key --- */
614 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
615 mpmont_create(&mm
, p
);
616 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
618 /* --- Store everything away --- */
620 mpkey(kd
, "y", y
, KCAT_PUB
);
622 kd
= key_structcreate(kd
, "private");
624 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
625 dolock(k
, kd
, "private");
627 mp_drop(x
); mp_drop(y
);
630 static void alg_dhparam(keyopts
*k
)
632 static const char *pl
[] = { "p", "q", "g", 0 };
633 key_data
*kd
= &k
->k
->k
;
634 if (!copyparam(k
, pl
)) {
641 if (strcmp(k
->curve
, "list") == 0) {
643 printf("Built-in prime groups:\n");
644 for (pe
= ptab
; pe
->name
; pe
++)
645 printf(" %s\n", pe
->name
);
649 if (dh_parse(&qd
, &dp
))
650 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
657 /* --- Choose a large safe prime number --- */
659 if (k
->f
& f_limlee
) {
664 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
665 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
666 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
667 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
671 for (i
= 0; i
< nf
; i
++) {
674 mp_writedstr(f
[i
], &d
, 10);
677 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
681 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
682 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
685 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
689 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
690 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
691 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
698 static void alg_dh(keyopts
*k
)
703 key_data
*kd
= &k
->k
->k
;
705 /* --- Get the shared parameters --- */
712 /* --- Choose a suitable private key --- *
714 * Since %$g$% has order %$q$%, choose %$x < q$%.
717 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
719 /* --- Compute the public key %$y = g^x \bmod p$% --- */
721 mpmont_create(&mm
, p
);
722 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
725 /* --- Store everything away --- */
727 mpkey(kd
, "y", y
, KCAT_PUB
);
729 kd
= key_structcreate(kd
, "private");
731 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
732 dolock(k
, kd
, "private");
734 mp_drop(x
); mp_drop(y
);
737 static void alg_bbs(keyopts
*k
)
742 /* --- Sanity checking --- */
745 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
749 /* --- Generate the BBS parameters --- */
751 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
752 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
753 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
755 /* --- Allrighty then --- */
759 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
761 kd
= key_structcreate(kd
, "private");
763 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
764 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
765 dolock(k
, kd
, "private");
770 static void alg_ecparam(keyopts
*k
)
772 static const char *pl
[] = { "curve", 0 };
773 if (!copyparam(k
, pl
)) {
776 key_data
*kd
= &k
->k
->k
;
778 /* --- Decide on a curve --- */
780 if (!k
->bits
) k
->bits
= 256;
781 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
783 printf("Built-in elliptic curves:\n");
784 for (ee
= ectab
; ee
->name
; ee
++)
785 printf(" %s\n", ee
->name
);
789 if (k
->bits
<= 56) k
->curve
= "secp112r1";
790 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
791 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
792 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
793 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
794 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
795 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
796 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
798 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
802 /* --- Check it --- */
804 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
805 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
806 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
807 moan("WARNING! curve check failed: %s", e
);
810 /* --- Write out the answer --- */
813 kd
= key_structcreate(kd
, "curve");
814 key_string(kd
, k
->curve
);
819 static void alg_ec(keyopts
*k
)
821 key_data
*kd
= &k
->k
->k
;
828 /* --- Get the curve --- */
831 if ((kkd
= key_structfind(kd
, "curve")) == 0)
832 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
833 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
834 die(EXIT_FAILURE
, "subkey `curve' is not a string");
835 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
836 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
838 /* --- Invent a private exponent and compute the public key --- */
840 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
841 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
843 /* --- Store everything away --- */
845 kkd
= key_structcreate(kd
, "p");
848 kkd
= key_structcreate(kd
, "private");
850 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
858 /* --- The algorithm tables --- */
860 typedef struct keyalg
{
862 void (*proc
)(keyopts
*o
);
866 static keyalg algtab
[] = {
867 { "binary", alg_binary
, "Plain binary data" },
868 { "des", alg_des
, "Binary with DES-style parity" },
869 { "rsa", alg_rsa
, "RSA public-key encryption" },
870 { "dsa", alg_dsa
, "DSA digital signatures" },
871 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
872 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
873 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
874 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
875 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
876 { "ec", alg_ec
, "Elliptic curve crypto" },
880 /* --- @cmd_add@ --- */
882 static int cmd_add(int argc
, char *argv
[])
885 time_t exp
= KEXP_EXPIRE
;
886 const char *tag
= 0, *ptag
= 0;
888 keyalg
*alg
= algtab
;
889 const char *rtag
= 0;
890 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
891 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
892 const char *seed
= 0;
895 /* --- Parse options for the subcommand --- */
898 static struct option opt
[] = {
899 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
900 { "bits", OPTF_ARGREQ
, 0, 'b' },
901 { "qbits", OPTF_ARGREQ
, 0, 'B' },
902 { "parameters", OPTF_ARGREQ
, 0, 'p' },
903 { "expire", OPTF_ARGREQ
, 0, 'e' },
904 { "comment", OPTF_ARGREQ
, 0, 'c' },
905 { "tag", OPTF_ARGREQ
, 0, 't' },
906 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
907 { "curve", OPTF_ARGREQ
, 0, 'C' },
908 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
909 { "seed", OPTF_ARGREQ
, 0, 's' },
910 { "newseed", OPTF_ARGREQ
, 0, 'n' },
911 { "lock", 0, 0, 'l' },
912 { "quiet", 0, 0, 'q' },
913 { "lim-lee", 0, 0, 'L' },
914 { "subgroup", 0, 0, 'S' },
917 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS", opt
, 0, 0, 0);
921 /* --- Handle the various options --- */
925 /* --- Read an algorithm name --- */
929 size_t sz
= strlen(optarg
);
931 if (strcmp(optarg
, "list") == 0) {
932 for (a
= algtab
; a
->name
; a
++)
933 printf("%-10s %s\n", a
->name
, a
->help
);
938 for (a
= algtab
; a
->name
; a
++) {
939 if (strncmp(optarg
, a
->name
, sz
) == 0) {
940 if (a
->name
[sz
] == 0) {
944 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
950 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
953 /* --- Bits must be nonzero and a multiple of 8 --- */
957 k
.bits
= strtoul(optarg
, &p
, 0);
958 if (k
.bits
== 0 || *p
!= 0)
959 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
964 k
.qbits
= strtoul(optarg
, &p
, 0);
965 if (k
.qbits
== 0 || *p
!= 0)
966 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
969 /* --- Parameter selection --- */
975 /* --- Expiry dates get passed to @get_date@ for parsing --- */
978 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
981 exp
= get_date(optarg
, 0);
983 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
987 /* --- Store comments without interpretation --- */
990 if (key_chkcomment(optarg
))
991 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
995 /* --- Elliptic curve parameters --- */
1001 /* --- Store tags --- */
1004 if (key_chkident(optarg
))
1005 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1012 /* --- Seeding --- */
1015 const struct seedalg
*ss
;
1016 if (strcmp(optarg
, "list") == 0) {
1017 printf("Seed algorithms:\n");
1018 for (ss
= seedtab
; ss
->p
; ss
++)
1019 printf(" %s\n", ss
->p
);
1022 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1024 for (ss
= seedtab
; ss
->p
; ss
++) {
1025 if (strcmp(optarg
, ss
->p
) == 0)
1029 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1035 if (seed
) die(EXIT_FAILURE
, "seed already set");
1037 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1038 base64_decode(&b
, 0, 0, &d
);
1039 k
.r
= sa
->gen(d
.buf
, d
.len
);
1048 unsigned n
= strtoul(optarg
, &p
, 0);
1049 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1050 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1051 if (seed
) die(EXIT_FAILURE
, "seed already set");
1054 rand_get(RAND_GLOBAL
, p
, n
);
1056 base64_encode(&b
, p
, n
, &d
);
1057 base64_encode(&b
, 0, 0, &d
);
1059 k
.r
= sa
->gen(p
, n
);
1062 /* --- Other flags --- */
1080 /* --- Other things are bogus --- */
1088 /* --- Various sorts of bogosity --- */
1090 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1092 "Usage: add [options] type [attr...]");
1094 if (key_chkident(argv
[optind
]))
1095 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1097 /* --- Set up various bits of the state --- */
1099 if (exp
== KEXP_EXPIRE
)
1100 exp
= time(0) + 14 * 24 * 60 * 60;
1102 /* --- Open the file and create the basic key block --- *
1104 * Keep on generating keyids until one of them doesn't collide.
1107 doopen(&f
, KOPEN_WRITE
);
1110 /* --- Key the generator --- */
1115 uint32 id
= rand_global
.ops
->word(&rand_global
);
1117 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1120 if (err
!= KERR_DUPID
)
1121 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1124 /* --- Set various simple attributes --- */
1129 if (k
.f
& f_retag
) {
1130 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1131 key_settag(&f
, kk
, 0);
1133 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1134 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1138 int err
= key_setcomment(&f
, k
.k
, c
);
1140 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1143 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1145 key_putattr(&f
, k
.k
, "genseed", seed
);
1146 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1149 key_fulltag(k
.k
, &k
.tag
);
1151 /* --- Find the parameter key --- */
1154 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1155 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1156 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1157 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1160 /* --- Now generate the actual key data --- */
1170 /*----- Key listing -------------------------------------------------------*/
1172 /* --- Listing options --- */
1174 typedef struct listopts
{
1175 const char *tfmt
; /* Date format (@strftime@-style) */
1176 int v
; /* Verbosity level */
1177 unsigned f
; /* Various flags */
1178 time_t t
; /* Time now (for key expiry) */
1179 key_filter kf
; /* Filter for matching keys */
1182 /* --- Listing flags --- */
1184 #define f_newline 2u /* Write newline before next entry */
1185 #define f_attr 4u /* Written at least one attribute */
1186 #define f_utc 8u /* Emit UTC time, not local time */
1188 /* --- @showkeydata@ --- *
1190 * Arguments: @key_data *k@ = pointer to key to write
1191 * @int ind@ = indentation level
1192 * @listopts *o@ = listing options
1193 * @dstr *d@ = tag string for this subkey
1197 * Use: Emits a piece of key data in a human-readable format.
1200 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1202 #define INDENT(i) do { \
1204 for (_i = 0; _i < (i); _i++) { \
1209 switch (k
->e
& KF_ENCMASK
) {
1211 /* --- Binary key data --- *
1213 * Emit as a simple hex dump.
1217 const octet
*p
= k
->u
.k
.k
;
1218 const octet
*l
= p
+ k
->u
.k
.sz
;
1221 fputs(" {", stdout
);
1226 } else if (sz
% 8 == 0)
1230 printf("%02x", *p
++);
1235 fputs("}\n", stdout
);
1238 /* --- Encrypted data --- *
1240 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1241 * key. Otherwise just say that it's encrypted and move on.
1246 fputs(" encrypted\n", stdout
);
1249 if (key_punlock(d
->buf
, k
, &kd
))
1250 printf(" <failed to unlock %s>\n", d
->buf
);
1252 fputs(" encrypted", stdout
);
1253 showkeydata(&kd
, ind
, o
, d
);
1259 /* --- Integer keys --- *
1261 * Emit as a large integer in decimal. This makes using the key in
1262 * `calc' or whatever easier.
1267 mp_writefile(k
->u
.m
, stdout
, 10);
1271 /* --- Strings --- */
1274 printf(" `%s'\n", k
->u
.p
);
1277 /* --- Elliptic curve points --- */
1280 if (EC_ATINF(&k
->u
.e
))
1281 fputs("inf\n", stdout
);
1283 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1284 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1289 /* --- Structured keys --- *
1291 * Just iterate over the subkeys.
1299 fputs(" {\n", stdout
);
1300 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1301 if (!key_match(&ks
->k
, &o
->kf
))
1304 printf("%s =", SYM_NAME(ks
));
1306 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1307 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1310 fputs("}\n", stdout
);
1317 /* --- @showkey@ --- *
1319 * Arguments: @key *k@ = pointer to key to show
1320 * @listopts *o@ = pointer to listing options
1324 * Use: Emits a listing of a particular key.
1327 static void showkey(key
*k
, listopts
*o
)
1329 char ebuf
[24], dbuf
[24];
1332 /* --- Skip the key if the filter doesn't match --- */
1334 if (!key_match(&k
->k
, &o
->kf
))
1337 /* --- Sort out the expiry and deletion times --- */
1339 if (KEY_EXPIRED(o
->t
, k
->exp
))
1340 strcpy(ebuf
, "expired");
1341 else if (k
->exp
== KEXP_FOREVER
)
1342 strcpy(ebuf
, "forever");
1344 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1345 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1348 if (KEY_EXPIRED(o
->t
, k
->del
))
1349 strcpy(dbuf
, "deleted");
1350 else if (k
->del
== KEXP_FOREVER
)
1351 strcpy(dbuf
, "forever");
1353 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1354 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1357 /* --- If in compact format, just display and quit --- */
1360 if (!(o
->f
& f_newline
)) {
1361 printf("%8s %-20s %-20s %-10s %-10s\n",
1362 "Id", "Tag", "Type", "Expire", "Delete");
1364 printf("%08lx %-20s %-20s %-10s %-10s\n",
1365 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1366 k
->type
, ebuf
, dbuf
);
1371 /* --- Display the standard header --- */
1373 if (o
->f
& f_newline
)
1374 fputc('\n', stdout
);
1375 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1376 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1377 printf("type: %s\n", k
->type
);
1378 printf("expiry: %s\n", ebuf
);
1379 printf("delete: %s\n", dbuf
);
1380 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1382 /* --- Display the attributes --- */
1386 const char *av
, *an
;
1389 printf("attributes:");
1390 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1391 printf("\n %s = %s", an
, av
);
1395 fputc('\n', stdout
);
1400 /* --- If dumping requested, dump the raw key data --- */
1404 fputs("key:", stdout
);
1406 showkeydata(&k
->k
, 0, o
, &d
);
1413 /* --- @cmd_list@ --- */
1415 static int cmd_list(int argc
, char *argv
[])
1419 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1421 /* --- Parse subcommand options --- */
1424 static struct option opt
[] = {
1425 { "quiet", 0, 0, 'q' },
1426 { "verbose", 0, 0, 'v' },
1427 { "utc", 0, 0, 'u' },
1428 { "filter", OPTF_ARGREQ
, 0, 'f' },
1431 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1448 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1450 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1459 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1461 /* --- Open the key file --- */
1463 doopen(&f
, KOPEN_READ
);
1466 /* --- Set up the time format --- */
1469 o
.tfmt
= "%Y-%m-%d";
1470 else if (o
.f
& f_utc
)
1471 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1473 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1475 /* --- If specific keys were requested use them, otherwise do all --- *
1477 * Some day, this might turn into a wildcard match.
1480 if (optind
< argc
) {
1482 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1485 moan("key `%s' not found", argv
[optind
]);
1489 } while (optind
< argc
);
1492 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1500 return (EXIT_FAILURE
);
1505 /*----- Command implementation --------------------------------------------*/
1507 /* --- @cmd_expire@ --- */
1509 static int cmd_expire(int argc
, char *argv
[])
1517 die(EXIT_FAILURE
, "Usage: expire tag...");
1518 doopen(&f
, KOPEN_WRITE
);
1519 for (i
= 1; i
< argc
; i
++) {
1520 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1523 moan("key `%s' not found", argv
[i
]);
1531 /* --- @cmd_delete@ --- */
1533 static int cmd_delete(int argc
, char *argv
[])
1541 die(EXIT_FAILURE
, "Usage: delete tag...");
1542 doopen(&f
, KOPEN_WRITE
);
1543 for (i
= 1; i
< argc
; i
++) {
1544 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1547 moan("key `%s' not found", argv
[i
]);
1555 /* --- @cmd_setattr@ --- */
1557 static int cmd_setattr(int argc
, char *argv
[])
1563 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1564 doopen(&f
, KOPEN_WRITE
);
1565 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1566 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1567 setattr(&f
, k
, argv
+ 2);
1572 /* --- @cmd_finger@ --- */
1574 static void fingerprint(key
*k
, const key_filter
*kf
)
1577 octet hash
[RMD160_HASHSZ
];
1581 if (!key_encode(&k
->k
, &d
, kf
))
1584 rmd160_hash(&r
, d
.buf
, d
.len
);
1585 rmd160_done(&r
, hash
);
1589 for (i
= 0; i
< sizeof(hash
); i
++) {
1590 if (i
&& i
% 4 == 0)
1592 printf("%02x", hash
[i
]);
1594 printf(" %s\n", d
.buf
);
1598 static int cmd_finger(int argc
, char *argv
[])
1602 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1605 static struct option opt
[] = {
1606 { "filter", OPTF_ARGREQ
, 0, 'f' },
1609 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1615 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1617 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1625 argv
+= optind
; argc
-= optind
;
1627 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1629 doopen(&f
, KOPEN_READ
);
1633 for (i
= 0; i
< argc
; i
++) {
1634 key
*k
= key_bytag(&f
, argv
[i
]);
1636 fingerprint(k
, &kf
);
1639 moan("key `%s' not found", argv
[i
]);
1645 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1646 fingerprint(k
, &kf
);
1651 /* --- @cmd_comment@ --- */
1653 static int cmd_comment(int argc
, char *argv
[])
1659 if (argc
< 2 || argc
> 3)
1660 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1661 doopen(&f
, KOPEN_WRITE
);
1662 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1663 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1664 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1665 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1670 /* --- @cmd_tag@ --- */
1672 static int cmd_tag(int argc
, char *argv
[])
1681 static struct option opt
[] = {
1682 { "retag", 0, 0, 'r' },
1685 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1698 argv
+= optind
; argc
-= optind
;
1699 if (argc
< 1 || argc
> 2 || rc
)
1700 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1701 doopen(&f
, KOPEN_WRITE
);
1702 if (flags
& f_retag
) {
1703 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1704 key_settag(&f
, k
, 0);
1706 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1707 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1708 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1709 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1714 /* --- @cmd_lock@ --- */
1716 static int cmd_lock(int argc
, char *argv
[])
1724 die(EXIT_FAILURE
, "Usage: lock qtag");
1725 doopen(&f
, KOPEN_WRITE
);
1726 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1727 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1728 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1729 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1730 if (key_plock(d
.buf
, kd
, kd
))
1731 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1737 /* --- @cmd_unlock@ --- */
1739 static int cmd_unlock(int argc
, char *argv
[])
1747 die(EXIT_FAILURE
, "Usage: unlock qtag");
1748 doopen(&f
, KOPEN_WRITE
);
1749 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1750 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1751 if (kd
->e
!= KENC_ENCRYPT
)
1752 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1753 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1754 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1760 /* --- @cmd_extract@ --- */
1762 static int cmd_extract(int argc
, char *argv
[])
1768 key_filter kf
= { 0, 0 };
1772 static struct option opt
[] = {
1773 { "filter", OPTF_ARGREQ
, 0, 'f' },
1776 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1782 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1784 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1792 argv
+= optind
; argc
-= optind
;
1794 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1795 if (strcmp(*argv
, "-") == 0)
1797 else if (!(fp
= fopen(*argv
, "w"))) {
1798 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1799 *argv
, strerror(errno
));
1802 doopen(&f
, KOPEN_READ
);
1806 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1807 key_extract(&f
, k
, fp
, &kf
);
1809 for (i
= 1; i
< argc
; i
++) {
1810 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1811 key_extract(&f
, k
, fp
, &kf
);
1813 moan("key `%s' not found", argv
[i
]);
1819 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1824 /* --- @cmd_tidy@ --- */
1826 static int cmd_tidy(int argc
, char *argv
[])
1830 die(EXIT_FAILURE
, "usage: tidy");
1831 doopen(&f
, KOPEN_WRITE
);
1832 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1837 /* --- @cmd_merge@ --- */
1839 static int cmd_merge(int argc
, char *argv
[])
1845 die(EXIT_FAILURE
, "Usage: merge file");
1846 if (strcmp(argv
[1], "-") == 0)
1848 else if (!(fp
= fopen(argv
[1], "r"))) {
1849 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1850 argv
[1], strerror(errno
));
1853 doopen(&f
, KOPEN_WRITE
);
1854 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1859 /*----- Main command table ------------------------------------------------*/
1863 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1868 "add [options] type [attr...]\n\
1869 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1870 [-e expire] [-t tag] [-c comment]", "\
1873 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1874 -b, --bits=N Generate an N-bit key.\n\
1875 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1876 -p, --parameters=TAG Get group parameters from TAG.\n\
1877 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1878 -e, --expire=TIME Make the key expire after TIME.\n\
1879 -c, --comment=STRING Attach the command STRING to the key.\n\
1880 -t, --tag=TAG Tag the key with the name TAG.\n\
1881 -r, --retag Untag any key currently with that tag.\n\
1882 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1883 -l, --lock Lock the generated key with a passphrase.\n\
1884 -q, --quiet Don't give progress indicators while working.\n\
1885 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1886 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1888 { "expire", cmd_expire
, "expire tag..." },
1889 { "delete", cmd_delete
, "delete tag..." },
1890 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1893 -r, --retag Untag any key currently called new-tag.\n\
1895 { "setattr", cmd_setattr
, "setattr tag attr..." },
1896 { "comment", cmd_comment
, "comment tag [comment]" },
1897 { "lock", cmd_lock
, "lock qtag" },
1898 { "unlock", cmd_unlock
, "unlock qtag" },
1899 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1902 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1903 -q, --quiet Show less information.\n\
1904 -v, --verbose Show more information.\n\
1906 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1909 -f, --filter=FILT Only hash key components matching FILT.\n\
1911 { "tidy", cmd_tidy
, "tidy" },
1912 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1915 -f, --filter=FILT Only extract key components matching FILT.\n\
1917 { "merge", cmd_merge
, "merge file" },
1921 typedef struct cmd cmd
;
1923 /*----- Main code ---------------------------------------------------------*/
1925 /* --- @findcmd@ --- *
1927 * Arguments: @const char *name@ = a command name
1929 * Returns: Pointer to the command structure.
1931 * Use: Looks up a command by name. If the command isn't found, an
1932 * error is reported and the program is terminated.
1935 static cmd
*findcmd(const char *name
)
1937 cmd
*c
, *chosen
= 0;
1938 size_t sz
= strlen(name
);
1940 for (c
= cmds
; c
->name
; c
++) {
1941 if (strncmp(name
, c
->name
, sz
) == 0) {
1942 if (c
->name
[sz
] == 0) {
1946 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1952 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1956 /* --- Helpful GNUy functions --- */
1958 void usage(FILE *fp
)
1960 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1963 void version(FILE *fp
)
1965 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1968 void help(FILE *fp
, char **argv
)
1976 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1984 Performs various simple key management operations. Command line options\n\
1987 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1988 -v, --version Display version number.\n\
1989 -u, --usage Display short usage summary.\n\
1991 -k, --keyring=FILE Read and write keys in FILE.\n\
1992 -i, --id=TAG Use key TAG for random number generator.\n\
1993 -t, --type=TYPE Use key TYPE for random number generator.\n\
1995 The following commands are understood:\n\n",
1997 for (c
= cmds
; c
->name
; c
++)
1998 fprintf(fp
, "%s\n", c
->usage
);
2004 * Arguments: @int argc@ = number of command line arguments
2005 * @char *argv[]@ = array of command line arguments
2007 * Returns: Nonzero on failure.
2009 * Use: Main program. Performs simple key management functions.
2012 int main(int argc
, char *argv
[])
2018 /* --- Initialization --- */
2023 /* --- Parse command line options --- */
2026 static struct option opt
[] = {
2028 /* --- Standard GNUy help options --- */
2030 { "help", 0, 0, 'h' },
2031 { "version", 0, 0, 'v' },
2032 { "usage", 0, 0, 'u' },
2034 /* --- Real live useful options --- */
2036 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2038 /* --- Magic terminator --- */
2042 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2048 /* --- GNU help options --- */
2051 help(stdout
, argv
+ optind
);
2060 /* --- Real useful options --- */
2066 /* --- Bogosity --- */
2074 /* --- Complain about excessive bogons --- */
2076 if (f
& f_bogus
|| optind
== argc
) {
2081 /* --- Initialize the Catacomb random number generator --- */
2083 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2084 rand_seed(RAND_GLOBAL
, 160);
2086 /* --- Dispatch to appropriate command handler --- */
2091 return (findcmd(argv
[0])->cmd(argc
, argv
));
2094 /*----- That's all, folks -------------------------------------------------*/