3 * $Id: keyutil.c,v 1.18 2004/04/01 12:50:09 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.18 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.17 2004/03/28 01:58:47 mdw
41 * Generate, store and retreive elliptic curve keys.
43 * Revision 1.16 2003/10/15 09:31:45 mdw
46 * Revision 1.15 2003/05/15 23:23:24 mdw
47 * Fix behaviour with bogus trailing attributes.
49 * Revision 1.14 2001/02/23 09:03:27 mdw
50 * Simplify usage message by removing nonexistant options.
52 * Revision 1.13 2001/02/21 20:04:27 mdw
53 * Provide help on individual commands (some need it desparately). Allow
54 * atomic retagging of keys.
56 * Revision 1.12 2001/02/03 11:58:22 mdw
57 * Store the correct seed information and count for DSA keys now that it's
60 * Revision 1.11 2000/12/06 20:33:27 mdw
61 * Make flags be macros rather than enumerations, to ensure that they're
64 * Revision 1.10 2000/10/08 12:02:21 mdw
65 * Use @MP_EQ@ instead of @MP_CMP@.
67 * Revision 1.9 2000/08/15 21:40:49 mdw
68 * Minor formatting change in listing attributes.
70 * Revision 1.8 2000/07/29 09:59:13 mdw
71 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
73 * Revision 1.7 2000/07/01 11:18:51 mdw
74 * Use new interfaces for key manipulation.
76 * Revision 1.6 2000/06/17 11:28:22 mdw
77 * Use secure memory interface from MP library. `rand_getgood' is
80 * Revision 1.5 2000/02/12 18:21:03 mdw
81 * Overhaul of key management (again).
83 * Revision 1.4 1999/12/22 15:48:10 mdw
84 * Track new key-management changes. Support new key generation
87 * Revision 1.3 1999/11/02 15:23:24 mdw
88 * Fix newlines in keyring list.
90 * Revision 1.2 1999/10/15 21:05:28 mdw
91 * In `key list', show timezone for local times, and support `-u' option
94 * Revision 1.1 1999/09/03 08:41:12 mdw
99 /*----- Header files ------------------------------------------------------*/
109 #include <mLib/base64.h>
110 #include <mLib/mdwopt.h>
111 #include <mLib/quis.h>
112 #include <mLib/report.h>
113 #include <mLib/sub.h>
136 #include "sha256-mgf.h"
137 #include "sha224-mgf.h"
138 #include "sha384-mgf.h"
139 #include "sha512-mgf.h"
140 #include "tiger-mgf.h"
141 #include "rmd128-mgf.h"
142 #include "rmd160-mgf.h"
143 #include "rmd256-mgf.h"
144 #include "rmd320-mgf.h"
148 /*----- Handy global state ------------------------------------------------*/
150 static const char *keyfile
= "keyring";
152 /*----- Useful shared functions -------------------------------------------*/
154 /* --- @doopen@ --- *
156 * Arguments: @key_file *f@ = pointer to key file block
157 * @unsigned how@ = method to open file with
161 * Use: Opens a key file and handles errors by panicking
165 static void doopen(key_file
*f
, unsigned how
)
167 if (key_open(f
, keyfile
, how
, key_moan
, 0))
168 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
171 /* --- @doclose@ --- *
173 * Arguments: @key_file *f@ = pointer to key file block
177 * Use: Closes a key file and handles errors by panicking
181 static void doclose(key_file
*f
)
183 switch (key_close(f
)) {
185 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
186 keyfile
, strerror(errno
));
188 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
189 keyfile
, strerror(errno
));
193 /* --- @setattr@ --- *
195 * Arguments: @key_file *f@ = pointer to key file block
196 * @key *k@ = pointer to key block
197 * @char *v[]@ = array of assignments (overwritten!)
201 * Use: Applies the attribute assignments to the key.
204 static void setattr(key_file
*f
, key
*k
, char *v
[])
209 size_t eq
= strcspn(p
, "=");
211 moan("invalid assignment: `%s' (ignored)", p
);
217 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
218 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
223 /*----- Seeding -----------------------------------------------------------*/
225 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
227 { "dsarand", dsarand_create
},
228 { "rmd128-mgf", rmd128_mgfrand
},
229 { "rmd160-mgf", rmd160_mgfrand
},
230 { "rmd256-mgf", rmd256_mgfrand
},
231 { "rmd320-mgf", rmd320_mgfrand
},
232 { "sha-mgf", sha_mgfrand
},
233 { "sha224-mgf", sha224_mgfrand
},
234 { "sha256-mgf", sha256_mgfrand
},
235 { "sha384-mgf", sha384_mgfrand
},
236 { "sha512-mgf", sha512_mgfrand
},
237 { "tiger-mgf", tiger_mgfrand
},
241 #define SEEDALG_DEFAULT (seedtab + 2)
243 /*----- Key generation ----------------------------------------------------*/
245 /* --- Key generation parameters --- */
247 typedef struct keyopts
{
248 key_file
*kf
; /* Pointer to key file */
249 key
*k
; /* Pointer to the actual key */
250 dstr tag
; /* Full tag name for the key */
251 unsigned f
; /* Flags for the new key */
252 unsigned bits
, qbits
; /* Bit length for the new key */
253 const char *curve
; /* Elliptic curve name/info */
254 grand
*r
; /* Random number source */
255 key
*p
; /* Parameters key-data */
258 #define f_bogus 1u /* Error in parsing */
259 #define f_lock 2u /* Passphrase-lock private key */
260 #define f_quiet 4u /* Don't show a progress indicator */
261 #define f_limlee 8u /* Generate Lim-Lee primes */
262 #define f_subgroup 16u /* Generate a subgroup */
263 #define f_retag 32u /* Remove any existing tag */
265 /* --- @dolock@ --- *
267 * Arguments: @keyopts *k@ = key generation options
268 * @key_data *kd@ = pointer to key data to lock
269 * @const char *t@ = tag suffix or null
273 * Use: Does passphrase locking on new keys.
276 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
278 if (!(k
->f
& f_lock
))
281 dstr_putf(&k
->tag
, ".%s", t
);
282 if (key_plock(k
->tag
.buf
, kd
, kd
))
283 die(EXIT_FAILURE
, "couldn't lock key");
288 * Arguments: @key_data *kd@ = pointer to parent key block
289 * @const char *tag@ = pointer to tag string
290 * @mp *m@ = integer to store
291 * @unsigned f@ = flags to set
295 * Use: Sets a multiprecision integer subkey.
298 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
300 key_data
*kkd
= key_structcreate(kd
, tag
);
305 /* --- @copyparam@ --- *
307 * Arguments: @keyopts *k@ = pointer to key options
308 * @const char **pp@ = checklist of parameters
310 * Returns: Nonzero if parameters copied; zero if you have to generate
313 * Use: Copies parameters from a source key to the current one.
316 static int copyparam(keyopts
*k
, const char **pp
)
320 /* --- Quick check if no parameters supplied --- */
325 /* --- Run through the checklist --- */
328 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
330 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
331 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
332 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
336 /* --- Copy over the parameters --- */
340 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
341 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
347 * Arguments: @key_data *k@ = pointer to key data block
348 * @const char *tag@ = tag string to use
350 * Returns: Pointer to multiprecision integer key item.
352 * Use: Fetches an MP key component.
355 static mp
*getmp(key_data
*k
, const char *tag
)
357 k
= key_structfind(k
, tag
);
359 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
360 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
361 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
365 /* --- @keyrand@ --- *
367 * Arguments: @key_file *kf@ = pointer to key file
368 * @const char *id@ = pointer to key id (or null)
372 * Use: Keys the random number generator.
375 static void keyrand(key_file
*kf
, const char *id
)
379 /* --- Find the key --- */
382 if ((k
= key_bytag(kf
, id
)) == 0)
383 die(EXIT_FAILURE
, "key `%s' not found", id
);
385 k
= key_bytype(kf
, "catacomb-rand");
388 key_data
*kd
= &k
->k
, kkd
;
391 switch (kd
->e
& KF_ENCMASK
) {
397 if (key_punlock(d
.buf
, kd
, &kkd
))
398 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
405 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
409 /* --- Key the generator --- */
411 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
417 /* --- Key generation algorithms --- */
419 static void alg_binary(keyopts
*k
)
428 die(EXIT_FAILURE
, "no shared parameters for binary keys");
430 sz
= (k
->bits
+ 7) >> 3;
432 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
433 k
->r
->ops
->fill(k
->r
, p
, sz
);
435 key_binary(&k
->k
->k
, p
, sz
);
436 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
439 dolock(k
, &k
->k
->k
, 0);
442 static void alg_des(keyopts
*k
)
451 die(EXIT_FAILURE
, "no shared parameters for DES keys");
452 if (k
->bits
% 56 || k
->bits
> 168)
453 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
457 k
->r
->ops
->fill(k
->r
, p
, sz
);
458 for (i
= 0; i
< sz
; i
++) {
459 octet x
= p
[i
] | 0x01;
463 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
465 key_binary(&k
->k
->k
, p
, sz
);
466 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
469 dolock(k
, &k
->k
->k
, 0);
472 static void alg_rsa(keyopts
*k
)
477 /* --- Sanity checking --- */
480 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
484 /* --- Generate the RSA parameters --- */
486 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
487 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
488 die(EXIT_FAILURE
, "RSA key generation failed");
490 /* --- Run a test encryption --- */
493 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
495 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
500 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
501 c
= rsa_qprivop(&rp
, c
, c
, g
);
504 die(EXIT_FAILURE
, "test encryption failed");
510 /* --- Allrighty then --- */
514 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
515 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
517 kd
= key_structcreate(kd
, "private");
519 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
520 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
521 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
522 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
523 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
524 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
525 dolock(k
, kd
, "private");
530 static void alg_dsaparam(keyopts
*k
)
532 static const char *pl
[] = { "q", "p", "g", 0 };
533 if (!copyparam(k
, pl
)) {
539 key_data
*kd
= &k
->k
->k
;
542 /* --- Choose appropriate bit lengths if necessary --- */
549 /* --- Allocate a seed block --- */
551 sz
= (k
->qbits
+ 7) >> 3;
553 k
->r
->ops
->fill(k
->r
, p
, sz
);
555 /* --- Allocate the parameters --- */
557 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
558 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
559 die(EXIT_FAILURE
, "DSA parameter generation failed");
561 /* --- Store the parameters --- */
564 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
565 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
566 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
571 /* --- Store the seed for future verification --- */
576 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
577 base64_encode(&c
, 0, 0, &d
);
578 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
580 dstr_putf(&d
, "%u", ds
.count
);
581 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
588 static void alg_dsa(keyopts
*k
)
593 key_data
*kd
= &k
->k
->k
;
595 /* --- Get the shared parameters --- */
602 /* --- Choose a private key --- */
604 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
605 mpmont_create(&mm
, p
);
606 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
608 /* --- Store everything away --- */
610 mpkey(kd
, "y", y
, KCAT_PUB
);
612 kd
= key_structcreate(kd
, "private");
614 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
615 dolock(k
, kd
, "private");
617 mp_drop(x
); mp_drop(y
);
620 static void alg_dhparam(keyopts
*k
)
622 static const char *pl
[] = { "p", "q", "g", 0 };
623 key_data
*kd
= &k
->k
->k
;
624 if (!copyparam(k
, pl
)) {
631 /* --- Choose a large safe prime number --- */
633 if (k
->f
& f_limlee
) {
638 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
639 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
640 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
641 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
645 for (i
= 0; i
< nf
; i
++) {
648 mp_writedstr(f
[i
], &d
, 10);
651 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
655 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
656 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
659 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
662 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
663 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
664 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
671 static void alg_dh(keyopts
*k
)
676 key_data
*kd
= &k
->k
->k
;
678 /* --- Get the shared parameters --- */
685 /* --- Choose a suitable private key --- *
687 * Since %$g$% has order %$q$%, choose %$x < q$%.
690 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
692 /* --- Compute the public key %$y = g^x \bmod p$% --- */
694 mpmont_create(&mm
, p
);
695 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
698 /* --- Store everything away --- */
700 mpkey(kd
, "y", y
, KCAT_PUB
);
702 kd
= key_structcreate(kd
, "private");
704 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
705 dolock(k
, kd
, "private");
707 mp_drop(x
); mp_drop(y
);
710 static void alg_bbs(keyopts
*k
)
715 /* --- Sanity checking --- */
718 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
722 /* --- Generate the BBS parameters --- */
724 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
725 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
726 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
728 /* --- Allrighty then --- */
732 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
734 kd
= key_structcreate(kd
, "private");
736 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
737 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
738 dolock(k
, kd
, "private");
743 static void alg_ecparam(keyopts
*k
)
745 static const char *pl
[] = { "curve", 0 };
746 if (!copyparam(k
, pl
)) {
749 key_data
*kd
= &k
->k
->k
;
751 /* --- Decide on a curve --- */
753 if (!k
->bits
) k
->bits
= 256;
755 if (k
->bits
<= 56) k
->curve
= "secp112r1";
756 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
757 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
758 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
759 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
760 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
761 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
762 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
764 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
768 /* --- Check it --- */
770 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
771 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
772 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
773 moan("WARNING! curve check failed: %s", e
);
776 /* --- Write out the answer --- */
779 kd
= key_structcreate(kd
, "curve");
780 key_string(kd
, k
->curve
);
785 static void alg_ec(keyopts
*k
)
787 key_data
*kd
= &k
->k
->k
;
794 /* --- Get the curve --- */
797 if ((kkd
= key_structfind(kd
, "curve")) == 0)
798 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
799 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
800 die(EXIT_FAILURE
, "subkey `curve' is not a string");
801 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
802 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
804 /* --- Invent a private exponent and compute the public key --- */
806 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
807 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
809 /* --- Store everything away --- */
811 kkd
= key_structcreate(kd
, "p");
814 kkd
= key_structcreate(kd
, "private");
816 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
824 /* --- The algorithm tables --- */
826 typedef struct keyalg
{
828 void (*proc
)(keyopts
*o
);
832 static keyalg algtab
[] = {
833 { "binary", alg_binary
, "Plain binary data" },
834 { "des", alg_des
, "Binary with DES-style parity" },
835 { "rsa", alg_rsa
, "RSA public-key encryption" },
836 { "dsa", alg_dsa
, "DSA digital signatures" },
837 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
838 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
839 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
840 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
841 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
842 { "ec", alg_ec
, "Elliptic curve crypto" },
846 /* --- @cmd_add@ --- */
848 static int cmd_add(int argc
, char *argv
[])
851 time_t exp
= KEXP_EXPIRE
;
852 const char *tag
= 0, *ptag
= 0;
854 keyalg
*alg
= algtab
;
855 const char *rtag
= 0;
856 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
857 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
858 const char *seed
= 0;
861 /* --- Parse options for the subcommand --- */
864 static struct option opt
[] = {
865 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
866 { "bits", OPTF_ARGREQ
, 0, 'b' },
867 { "qbits", OPTF_ARGREQ
, 0, 'B' },
868 { "parameters", OPTF_ARGREQ
, 0, 'p' },
869 { "expire", OPTF_ARGREQ
, 0, 'e' },
870 { "comment", OPTF_ARGREQ
, 0, 'c' },
871 { "tag", OPTF_ARGREQ
, 0, 't' },
872 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
873 { "curve", OPTF_ARGREQ
, 0, 'C' },
874 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
875 { "seed", OPTF_ARGREQ
, 0, 's' },
876 { "newseed", OPTF_ARGREQ
, 0, 'n' },
877 { "lock", 0, 0, 'l' },
878 { "quiet", 0, 0, 'q' },
879 { "lim-lee", 0, 0, 'L' },
880 { "subgroup", 0, 0, 'S' },
883 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS", opt
, 0, 0, 0);
887 /* --- Handle the various options --- */
891 /* --- Read an algorithm name --- */
895 size_t sz
= strlen(optarg
);
897 if (strcmp(optarg
, "list") == 0) {
898 for (a
= algtab
; a
->name
; a
++)
899 printf("%-10s %s\n", a
->name
, a
->help
);
904 for (a
= algtab
; a
->name
; a
++) {
905 if (strncmp(optarg
, a
->name
, sz
) == 0) {
906 if (a
->name
[sz
] == 0) {
910 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
916 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
919 /* --- Bits must be nonzero and a multiple of 8 --- */
923 k
.bits
= strtoul(optarg
, &p
, 0);
924 if (k
.bits
== 0 || *p
!= 0)
925 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
930 k
.qbits
= strtoul(optarg
, &p
, 0);
931 if (k
.qbits
== 0 || *p
!= 0)
932 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
935 /* --- Parameter selection --- */
941 /* --- Expiry dates get passed to @get_date@ for parsing --- */
944 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
947 exp
= get_date(optarg
, 0);
949 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
953 /* --- Store comments without interpretation --- */
956 if (key_chkcomment(optarg
))
957 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
961 /* --- Elliptic curve parameters --- */
964 if (strcmp(optarg
, "list") == 0) {
966 printf("Built-in elliptic curves:\n");
967 for (ee
= ectab
; ee
->name
; ee
++)
968 printf(" %s\n", ee
->name
);
974 /* --- Store tags --- */
977 if (key_chkident(optarg
))
978 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
985 /* --- Seeding --- */
988 const struct seedalg
*ss
;
989 if (strcmp(optarg
, "list") == 0) {
990 printf("Seed algorithms:\n");
991 for (ss
= seedtab
; ss
->p
; ss
++)
992 printf(" %s\n", ss
->p
);
995 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
997 for (ss
= seedtab
; ss
->p
; ss
++) {
998 if (strcmp(optarg
, ss
->p
) == 0)
1002 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1008 if (seed
) die(EXIT_FAILURE
, "seed already set");
1010 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1011 base64_decode(&b
, 0, 0, &d
);
1012 k
.r
= sa
->gen(d
.buf
, d
.len
);
1021 unsigned n
= strtoul(optarg
, &p
, 0);
1022 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1023 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1024 if (seed
) die(EXIT_FAILURE
, "seed already set");
1027 rand_get(RAND_GLOBAL
, p
, n
);
1029 base64_encode(&b
, p
, n
, &d
);
1030 base64_encode(&b
, 0, 0, &d
);
1032 k
.r
= sa
->gen(p
, n
);
1035 /* --- Other flags --- */
1053 /* --- Other things are bogus --- */
1061 /* --- Various sorts of bogosity --- */
1063 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1065 "Usage: add [options] type [attr...]");
1067 if (key_chkident(argv
[optind
]))
1068 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1070 /* --- Set up various bits of the state --- */
1072 if (exp
== KEXP_EXPIRE
)
1073 exp
= time(0) + 14 * 24 * 60 * 60;
1075 /* --- Open the file and create the basic key block --- *
1077 * Keep on generating keyids until one of them doesn't collide.
1080 doopen(&f
, KOPEN_WRITE
);
1083 /* --- Key the generator --- */
1088 uint32 id
= rand_global
.ops
->word(&rand_global
);
1090 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1093 if (err
!= KERR_DUPID
)
1094 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1097 /* --- Set various simple attributes --- */
1102 if (k
.f
& f_retag
) {
1103 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1104 key_settag(&f
, kk
, 0);
1106 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1107 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1111 int err
= key_setcomment(&f
, k
.k
, c
);
1113 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1116 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1118 key_putattr(&f
, k
.k
, "genseed", seed
);
1119 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1122 key_fulltag(k
.k
, &k
.tag
);
1124 /* --- Find the parameter key --- */
1127 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1128 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1129 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1130 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1133 /* --- Now generate the actual key data --- */
1143 /*----- Key listing -------------------------------------------------------*/
1145 /* --- Listing options --- */
1147 typedef struct listopts
{
1148 const char *tfmt
; /* Date format (@strftime@-style) */
1149 int v
; /* Verbosity level */
1150 unsigned f
; /* Various flags */
1151 time_t t
; /* Time now (for key expiry) */
1152 key_filter kf
; /* Filter for matching keys */
1155 /* --- Listing flags --- */
1157 #define f_newline 2u /* Write newline before next entry */
1158 #define f_attr 4u /* Written at least one attribute */
1159 #define f_utc 8u /* Emit UTC time, not local time */
1161 /* --- @showkeydata@ --- *
1163 * Arguments: @key_data *k@ = pointer to key to write
1164 * @int ind@ = indentation level
1165 * @listopts *o@ = listing options
1166 * @dstr *d@ = tag string for this subkey
1170 * Use: Emits a piece of key data in a human-readable format.
1173 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1175 #define INDENT(i) do { \
1177 for (_i = 0; _i < (i); _i++) { \
1182 switch (k
->e
& KF_ENCMASK
) {
1184 /* --- Binary key data --- *
1186 * Emit as a simple hex dump.
1190 const octet
*p
= k
->u
.k
.k
;
1191 const octet
*l
= p
+ k
->u
.k
.sz
;
1194 fputs(" {", stdout
);
1199 } else if (sz
% 8 == 0)
1203 printf("%02x", *p
++);
1208 fputs("}\n", stdout
);
1211 /* --- Encrypted data --- *
1213 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1214 * key. Otherwise just say that it's encrypted and move on.
1219 fputs(" encrypted\n", stdout
);
1222 if (key_punlock(d
->buf
, k
, &kd
))
1223 printf(" <failed to unlock %s>\n", d
->buf
);
1225 fputs(" encrypted", stdout
);
1226 showkeydata(&kd
, ind
, o
, d
);
1232 /* --- Integer keys --- *
1234 * Emit as a large integer in decimal. This makes using the key in
1235 * `calc' or whatever easier.
1240 mp_writefile(k
->u
.m
, stdout
, 10);
1244 /* --- Strings --- */
1247 printf(" `%s'\n", k
->u
.p
);
1250 /* --- Elliptic curve points --- */
1253 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1254 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1258 /* --- Structured keys --- *
1260 * Just iterate over the subkeys.
1268 fputs(" {\n", stdout
);
1269 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1270 if (!key_match(&ks
->k
, &o
->kf
))
1273 printf("%s =", SYM_NAME(ks
));
1275 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1276 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1279 fputs("}\n", stdout
);
1286 /* --- @showkey@ --- *
1288 * Arguments: @key *k@ = pointer to key to show
1289 * @listopts *o@ = pointer to listing options
1293 * Use: Emits a listing of a particular key.
1296 static void showkey(key
*k
, listopts
*o
)
1298 char ebuf
[24], dbuf
[24];
1301 /* --- Skip the key if the filter doesn't match --- */
1303 if (!key_match(&k
->k
, &o
->kf
))
1306 /* --- Sort out the expiry and deletion times --- */
1308 if (KEY_EXPIRED(o
->t
, k
->exp
))
1309 strcpy(ebuf
, "expired");
1310 else if (k
->exp
== KEXP_FOREVER
)
1311 strcpy(ebuf
, "forever");
1313 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1314 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1317 if (KEY_EXPIRED(o
->t
, k
->del
))
1318 strcpy(dbuf
, "deleted");
1319 else if (k
->del
== KEXP_FOREVER
)
1320 strcpy(dbuf
, "forever");
1322 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1323 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1326 /* --- If in compact format, just display and quit --- */
1329 if (!(o
->f
& f_newline
)) {
1330 printf("%8s %-20s %-20s %-10s %-10s\n",
1331 "Id", "Tag", "Type", "Expire", "Delete");
1333 printf("%08lx %-20s %-20s %-10s %-10s\n",
1334 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1335 k
->type
, ebuf
, dbuf
);
1340 /* --- Display the standard header --- */
1342 if (o
->f
& f_newline
)
1343 fputc('\n', stdout
);
1344 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1345 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1346 printf("type: %s\n", k
->type
);
1347 printf("expiry: %s\n", ebuf
);
1348 printf("delete: %s\n", dbuf
);
1349 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1351 /* --- Display the attributes --- */
1355 const char *av
, *an
;
1358 printf("attributes:");
1359 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1360 printf("\n %s = %s", an
, av
);
1364 fputc('\n', stdout
);
1369 /* --- If dumping requested, dump the raw key data --- */
1373 fputs("key:", stdout
);
1375 showkeydata(&k
->k
, 0, o
, &d
);
1382 /* --- @cmd_list@ --- */
1384 static int cmd_list(int argc
, char *argv
[])
1388 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1390 /* --- Parse subcommand options --- */
1393 static struct option opt
[] = {
1394 { "quiet", 0, 0, 'q' },
1395 { "verbose", 0, 0, 'v' },
1396 { "utc", 0, 0, 'u' },
1397 { "filter", OPTF_ARGREQ
, 0, 'f' },
1400 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1417 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1419 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1428 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1430 /* --- Open the key file --- */
1432 doopen(&f
, KOPEN_READ
);
1435 /* --- Set up the time format --- */
1438 o
.tfmt
= "%Y-%m-%d";
1439 else if (o
.f
& f_utc
)
1440 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1442 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1444 /* --- If specific keys were requested use them, otherwise do all --- *
1446 * Some day, this might turn into a wildcard match.
1449 if (optind
< argc
) {
1451 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1454 moan("key `%s' not found", argv
[optind
]);
1458 } while (optind
< argc
);
1461 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1469 return (EXIT_FAILURE
);
1474 /*----- Command implementation --------------------------------------------*/
1476 /* --- @cmd_expire@ --- */
1478 static int cmd_expire(int argc
, char *argv
[])
1486 die(EXIT_FAILURE
, "Usage: expire tag...");
1487 doopen(&f
, KOPEN_WRITE
);
1488 for (i
= 1; i
< argc
; i
++) {
1489 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1492 moan("key `%s' not found", argv
[i
]);
1500 /* --- @cmd_delete@ --- */
1502 static int cmd_delete(int argc
, char *argv
[])
1510 die(EXIT_FAILURE
, "Usage: delete tag...");
1511 doopen(&f
, KOPEN_WRITE
);
1512 for (i
= 1; i
< argc
; i
++) {
1513 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1516 moan("key `%s' not found", argv
[i
]);
1524 /* --- @cmd_setattr@ --- */
1526 static int cmd_setattr(int argc
, char *argv
[])
1532 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1533 doopen(&f
, KOPEN_WRITE
);
1534 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1535 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1536 setattr(&f
, k
, argv
+ 2);
1541 /* --- @cmd_finger@ --- */
1543 static void fingerprint(key
*k
, const key_filter
*kf
)
1546 octet hash
[RMD160_HASHSZ
];
1550 if (!key_encode(&k
->k
, &d
, kf
))
1553 rmd160_hash(&r
, d
.buf
, d
.len
);
1554 rmd160_done(&r
, hash
);
1558 for (i
= 0; i
< sizeof(hash
); i
++) {
1559 if (i
&& i
% 4 == 0)
1561 printf("%02x", hash
[i
]);
1563 printf(" %s\n", d
.buf
);
1567 static int cmd_finger(int argc
, char *argv
[])
1571 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1574 static struct option opt
[] = {
1575 { "filter", OPTF_ARGREQ
, 0, 'f' },
1578 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1584 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1586 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1594 argv
+= optind
; argc
-= optind
;
1596 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1598 doopen(&f
, KOPEN_READ
);
1602 for (i
= 0; i
< argc
; i
++) {
1603 key
*k
= key_bytag(&f
, argv
[i
]);
1605 fingerprint(k
, &kf
);
1608 moan("key `%s' not found", argv
[i
]);
1614 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1615 fingerprint(k
, &kf
);
1620 /* --- @cmd_comment@ --- */
1622 static int cmd_comment(int argc
, char *argv
[])
1628 if (argc
< 2 || argc
> 3)
1629 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1630 doopen(&f
, KOPEN_WRITE
);
1631 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1632 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1633 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1634 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1639 /* --- @cmd_tag@ --- */
1641 static int cmd_tag(int argc
, char *argv
[])
1650 static struct option opt
[] = {
1651 { "retag", 0, 0, 'r' },
1654 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1667 argv
+= optind
; argc
-= optind
;
1668 if (argc
< 1 || argc
> 2 || rc
)
1669 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1670 doopen(&f
, KOPEN_WRITE
);
1671 if (flags
& f_retag
) {
1672 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1673 key_settag(&f
, k
, 0);
1675 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1676 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1677 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1678 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1683 /* --- @cmd_lock@ --- */
1685 static int cmd_lock(int argc
, char *argv
[])
1693 die(EXIT_FAILURE
, "Usage: lock qtag");
1694 doopen(&f
, KOPEN_WRITE
);
1695 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1696 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1697 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1698 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1699 if (key_plock(d
.buf
, kd
, kd
))
1700 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1706 /* --- @cmd_unlock@ --- */
1708 static int cmd_unlock(int argc
, char *argv
[])
1716 die(EXIT_FAILURE
, "Usage: unlock qtag");
1717 doopen(&f
, KOPEN_WRITE
);
1718 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1719 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1720 if (kd
->e
!= KENC_ENCRYPT
)
1721 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1722 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1723 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1729 /* --- @cmd_extract@ --- */
1731 static int cmd_extract(int argc
, char *argv
[])
1737 key_filter kf
= { 0, 0 };
1741 static struct option opt
[] = {
1742 { "filter", OPTF_ARGREQ
, 0, 'f' },
1745 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1751 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1753 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1761 argv
+= optind
; argc
-= optind
;
1763 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1764 if (strcmp(*argv
, "-") == 0)
1766 else if (!(fp
= fopen(*argv
, "w"))) {
1767 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1768 *argv
, strerror(errno
));
1771 doopen(&f
, KOPEN_READ
);
1775 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1776 key_extract(&f
, k
, fp
, &kf
);
1778 for (i
= 1; i
< argc
; i
++) {
1779 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1780 key_extract(&f
, k
, fp
, &kf
);
1782 moan("key `%s' not found", argv
[i
]);
1788 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1793 /* --- @cmd_tidy@ --- */
1795 static int cmd_tidy(int argc
, char *argv
[])
1799 die(EXIT_FAILURE
, "usage: tidy");
1800 doopen(&f
, KOPEN_WRITE
);
1801 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1806 /* --- @cmd_merge@ --- */
1808 static int cmd_merge(int argc
, char *argv
[])
1814 die(EXIT_FAILURE
, "Usage: merge file");
1815 if (strcmp(argv
[1], "-") == 0)
1817 else if (!(fp
= fopen(argv
[1], "r"))) {
1818 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1819 argv
[1], strerror(errno
));
1822 doopen(&f
, KOPEN_WRITE
);
1823 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1828 /*----- Main command table ------------------------------------------------*/
1832 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1837 "add [options] type [attr...]\n\
1838 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1839 [-e expire] [-t tag] [-c comment]", "\
1842 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1843 -b, --bits=N Generate an N-bit key.\n\
1844 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1845 -p, --parameters=TAG Get group parameters from TAG.\n\
1846 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1847 -e, --expire=TIME Make the key expire after TIME.\n\
1848 -c, --comment=STRING Attach the command STRING to the key.\n\
1849 -t, --tag=TAG Tag the key with the name TAG.\n\
1850 -r, --retag Untag any key currently with that tag.\n\
1851 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1852 -l, --lock Lock the generated key with a passphrase.\n\
1853 -q, --quiet Don't give progress indicators while working.\n\
1854 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1855 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1857 { "expire", cmd_expire
, "expire tag..." },
1858 { "delete", cmd_delete
, "delete tag..." },
1859 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1862 -r, --retag Untag any key currently called new-tag.\n\
1864 { "setattr", cmd_setattr
, "setattr tag attr..." },
1865 { "comment", cmd_comment
, "comment tag [comment]" },
1866 { "lock", cmd_lock
, "lock qtag" },
1867 { "unlock", cmd_unlock
, "unlock qtag" },
1868 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1871 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1872 -q, --quiet Show less information.\n\
1873 -v, --verbose Show more information.\n\
1875 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1878 -f, --filter=FILT Only hash key components matching FILT.\n\
1880 { "tidy", cmd_tidy
, "tidy" },
1881 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1884 -f, --filter=FILT Only extract key components matching FILT.\n\
1886 { "merge", cmd_merge
, "merge file" },
1890 typedef struct cmd cmd
;
1892 /*----- Main code ---------------------------------------------------------*/
1894 /* --- @findcmd@ --- *
1896 * Arguments: @const char *name@ = a command name
1898 * Returns: Pointer to the command structure.
1900 * Use: Looks up a command by name. If the command isn't found, an
1901 * error is reported and the program is terminated.
1904 static cmd
*findcmd(const char *name
)
1906 cmd
*c
, *chosen
= 0;
1907 size_t sz
= strlen(name
);
1909 for (c
= cmds
; c
->name
; c
++) {
1910 if (strncmp(name
, c
->name
, sz
) == 0) {
1911 if (c
->name
[sz
] == 0) {
1915 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1921 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1925 /* --- Helpful GNUy functions --- */
1927 void usage(FILE *fp
)
1929 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1932 void version(FILE *fp
)
1934 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1937 void help(FILE *fp
, char **argv
)
1945 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1953 Performs various simple key management operations. Command line options\n\
1956 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1957 -v, --version Display version number.\n\
1958 -u, --usage Display short usage summary.\n\
1960 -k, --keyring=FILE Read and write keys in FILE.\n\
1961 -i, --id=TAG Use key TAG for random number generator.\n\
1962 -t, --type=TYPE Use key TYPE for random number generator.\n\
1964 The following commands are understood:\n\n",
1966 for (c
= cmds
; c
->name
; c
++)
1967 fprintf(fp
, "%s\n", c
->usage
);
1973 * Arguments: @int argc@ = number of command line arguments
1974 * @char *argv[]@ = array of command line arguments
1976 * Returns: Nonzero on failure.
1978 * Use: Main program. Performs simple key management functions.
1981 int main(int argc
, char *argv
[])
1987 /* --- Initialization --- */
1992 /* --- Parse command line options --- */
1995 static struct option opt
[] = {
1997 /* --- Standard GNUy help options --- */
1999 { "help", 0, 0, 'h' },
2000 { "version", 0, 0, 'v' },
2001 { "usage", 0, 0, 'u' },
2003 /* --- Real live useful options --- */
2005 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2007 /* --- Magic terminator --- */
2011 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2017 /* --- GNU help options --- */
2020 help(stdout
, argv
+ optind
);
2029 /* --- Real useful options --- */
2035 /* --- Bogosity --- */
2043 /* --- Complain about excessive bogons --- */
2045 if (f
& f_bogus
|| optind
== argc
) {
2050 /* --- Initialize the Catacomb random number generator --- */
2052 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2053 rand_seed(RAND_GLOBAL
, 160);
2055 /* --- Dispatch to appropriate command handler --- */
2060 return (findcmd(argv
[0])->cmd(argc
, argv
));
2063 /*----- That's all, folks -------------------------------------------------*/