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 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/base64.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
72 #include "sha256-mgf.h"
73 #include "sha224-mgf.h"
74 #include "sha384-mgf.h"
75 #include "sha512-mgf.h"
76 #include "tiger-mgf.h"
77 #include "rmd128-mgf.h"
78 #include "rmd160-mgf.h"
79 #include "rmd256-mgf.h"
80 #include "rmd320-mgf.h"
84 /*----- Handy global state ------------------------------------------------*/
86 static const char *keyfile = "keyring";
88 /*----- Useful shared functions -------------------------------------------*/
92 * Arguments: @key_file *f@ = pointer to key file block
93 * @unsigned how@ = method to open file with
97 * Use: Opens a key file and handles errors by panicking
101 static void doopen(key_file *f, unsigned how)
103 if (key_open(f, keyfile, how, key_moan, 0))
104 die(1, "couldn't open keyring `%s': %s", keyfile, strerror(errno));
107 /* --- @doclose@ --- *
109 * Arguments: @key_file *f@ = pointer to key file block
113 * Use: Closes a key file and handles errors by panicking
117 static void doclose(key_file *f)
119 switch (key_close(f)) {
121 die(EXIT_FAILURE, "couldn't write file `%s': %s",
122 keyfile, strerror(errno));
124 die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)",
125 keyfile, strerror(errno));
129 /* --- @setattr@ --- *
131 * Arguments: @key_file *f@ = pointer to key file block
132 * @key *k@ = pointer to key block
133 * @char *v[]@ = array of assignments (overwritten!)
137 * Use: Applies the attribute assignments to the key.
140 static void setattr(key_file *f, key *k, char *v[])
145 size_t eq = strcspn(p, "=");
147 moan("invalid assignment: `%s' (ignored)", p);
153 if ((err = key_putattr(f, k, *v, *p ? p : 0)) != 0)
154 die(EXIT_FAILURE, "couldn't set attributes: %s", key_strerror(err));
159 /*----- Seeding -----------------------------------------------------------*/
161 const struct seedalg { const char *p; grand *(*gen)(const void *, size_t); }
163 { "dsarand", dsarand_create },
164 { "rmd128-mgf", rmd128_mgfrand },
165 { "rmd160-mgf", rmd160_mgfrand },
166 { "rmd256-mgf", rmd256_mgfrand },
167 { "rmd320-mgf", rmd320_mgfrand },
168 { "sha-mgf", sha_mgfrand },
169 { "sha224-mgf", sha224_mgfrand },
170 { "sha256-mgf", sha256_mgfrand },
171 { "sha384-mgf", sha384_mgfrand },
172 { "sha512-mgf", sha512_mgfrand },
173 { "tiger-mgf", tiger_mgfrand },
177 #define SEEDALG_DEFAULT (seedtab + 2)
179 /*----- Key generation ----------------------------------------------------*/
181 /* --- Key generation parameters --- */
183 typedef struct keyopts {
184 key_file *kf; /* Pointer to key file */
185 key *k; /* Pointer to the actual key */
186 dstr tag; /* Full tag name for the key */
187 unsigned f; /* Flags for the new key */
188 unsigned bits, qbits; /* Bit length for the new key */
189 const char *curve; /* Elliptic curve name/info */
190 grand *r; /* Random number source */
191 key *p; /* Parameters key-data */
194 #define f_bogus 1u /* Error in parsing */
195 #define f_lock 2u /* Passphrase-lock private key */
196 #define f_quiet 4u /* Don't show a progress indicator */
197 #define f_limlee 8u /* Generate Lim-Lee primes */
198 #define f_subgroup 16u /* Generate a subgroup */
199 #define f_retag 32u /* Remove any existing tag */
200 #define f_kcdsa 64u /* Generate KCDSA primes */
202 /* --- @dolock@ --- *
204 * Arguments: @keyopts *k@ = key generation options
205 * @key_data **kd@ = pointer to key data to lock
206 * @const char *t@ = tag suffix or null
210 * Use: Does passphrase locking on new keys.
213 static void dolock(keyopts *k, key_data **kd, const char *t)
215 if (!(k->f & f_lock))
218 dstr_putf(&k->tag, ".%s", t);
219 if (key_plock(kd, 0, k->tag.buf))
220 die(EXIT_FAILURE, "couldn't lock key");
223 /* --- @copyparam@ --- *
225 * Arguments: @keyopts *k@ = pointer to key options
226 * @const char **pp@ = checklist of parameters
228 * Returns: Nonzero if parameters copied; zero if you have to generate
231 * Use: Copies parameters from a source key to the current one.
234 static int copyparam(keyopts *k, const char **pp)
240 /* --- Quick check if no parameters supplied --- */
245 /* --- Run through the checklist --- */
248 key_data *kd = key_structfind(k->p->k, *pp);
250 die(EXIT_FAILURE, "bad parameter key: parameter `%s' not found", *pp);
251 if ((kd->e & KF_CATMASK) != KCAT_SHARE)
252 die(EXIT_FAILURE, "bad parameter key: subkey `%s' is not shared", *pp);
256 /* --- Copy over the parameters --- */
260 key_setkeydata(k->kf, k->k, k->p->k);
262 /* --- Copy over attributes --- */
264 for (key_mkattriter(&i, k->p); key_nextattr(&i, &n, &v); )
265 key_putattr(k->kf, k->k, n, v);
274 * Arguments: @key_data *k@ = pointer to key data block
275 * @const char *tag@ = tag string to use
277 * Returns: Pointer to multiprecision integer key item.
279 * Use: Fetches an MP key component.
282 static mp *getmp(key_data *k, const char *tag)
284 k = key_structfind(k, tag);
286 die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag);
287 if ((k->e & KF_ENCMASK) != KENC_MP)
288 die(EXIT_FAILURE, "subkey `%s' has an incompatible type");
292 /* --- @keyrand@ --- *
294 * Arguments: @key_file *kf@ = pointer to key file
295 * @const char *id@ = pointer to key id (or null)
299 * Use: Keys the random number generator.
302 static void keyrand(key_file *kf, const char *id)
306 /* --- Find the key --- */
309 if ((k = key_bytag(kf, id)) == 0)
310 die(EXIT_FAILURE, "key `%s' not found", id);
312 k = key_bytype(kf, "catacomb-rand");
315 key_data *kd = k->k, *kkd;
319 switch (kd->e & KF_ENCMASK) {
325 if (key_punlock(&kkd, kd, d.buf))
326 die(EXIT_FAILURE, "error unlocking key `%s'", d.buf);
334 die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf);
338 /* --- Key the generator --- */
340 rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz);
345 /* --- Key generation algorithms --- */
347 static void alg_binary(keyopts *k)
357 die(EXIT_FAILURE, "no shared parameters for binary keys");
359 sz = (k->bits + 7) >> 3;
361 m = (1 << (((k->bits - 1) & 7) + 1)) - 1;
362 k->r->ops->fill(k->r, p, sz);
364 kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz);
367 key_setkeydata(k->kf, k->k, kd);
372 static void alg_des(keyopts *k)
382 die(EXIT_FAILURE, "no shared parameters for DES keys");
383 if (k->bits % 56 || k->bits > 168)
384 die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long");
388 k->r->ops->fill(k->r, p, sz);
389 for (i = 0; i < sz; i++) {
390 octet x = p[i] | 0x01;
394 p[i] = (p[i] & 0xfe) | (x & 0x01);
396 kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz);
399 key_setkeydata(k->kf, k->k, kd);
404 static void alg_rsa(keyopts *k)
409 /* --- Sanity checking --- */
412 die(EXIT_FAILURE, "no shared parameters for RSA keys");
416 /* --- Generate the RSA parameters --- */
418 if (rsa_gen(&rp, k->bits, k->r, 0,
419 (k->f & f_quiet) ? 0 : pgen_ev, 0))
420 die(EXIT_FAILURE, "RSA key generation failed");
422 /* --- Run a test encryption --- */
425 grand *g = fibrand_create(k->r->ops->word(k->r));
427 mp *m = mprand_range(MP_NEW, rp.n, g, 0);
432 c = rsa_qpubop(&rpp, MP_NEW, m);
433 c = rsa_qprivop(&rp, c, c, g);
436 die(EXIT_FAILURE, "test encryption failed");
442 /* --- Allrighty then --- */
444 kd = key_newstruct();
445 key_structsteal(kd, "n", key_newmp(KCAT_PUB, rp.n));
446 key_structsteal(kd, "e", key_newmp(KCAT_PUB, rp.e));
448 kkd = key_newstruct();
449 key_structsteal(kkd, "d", key_newmp(KCAT_PRIV | KF_BURN, rp.d));
450 key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, rp.p));
451 key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, rp.q));
452 key_structsteal(kkd, "q-inv", key_newmp(KCAT_PRIV | KF_BURN, rp.q_inv));
453 key_structsteal(kkd, "d-mod-p", key_newmp(KCAT_PRIV | KF_BURN, rp.dp));
454 key_structsteal(kkd, "d-mod-q", key_newmp(KCAT_PRIV | KF_BURN, rp.dq));
455 dolock(k, &kkd, "private");
456 key_structsteal(kd, "private", kkd);
457 key_setkeydata(k->kf, k->k, kd);
462 static void alg_dsaparam(keyopts *k)
464 static const char *pl[] = { "q", "p", "g", 0 };
465 if (!copyparam(k, pl)) {
474 /* --- Choose appropriate bit lengths if necessary --- */
481 /* --- Allocate a seed block --- */
483 sz = (k->qbits + 7) >> 3;
485 k->r->ops->fill(k->r, p, sz);
487 /* --- Allocate the parameters --- */
489 if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, &ds,
490 (k->f & f_quiet) ? 0 : pgen_ev, 0))
491 die(EXIT_FAILURE, "DSA parameter generation failed");
493 /* --- Store the parameters --- */
495 kd = key_newstruct();
496 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q));
497 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p));
498 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g));
502 key_setkeydata(k->kf, k->k, kd);
505 /* --- Store the seed for future verification --- */
510 base64_encode(&c, ds.p, ds.sz, &d);
511 base64_encode(&c, 0, 0, &d);
513 key_putattr(k->kf, k->k, "seed", d.buf);
515 dstr_putf(&d, "%u", ds.count);
516 key_putattr(k->kf, k->k, "count", d.buf);
523 static void alg_dsa(keyopts *k)
530 /* --- Get the shared parameters --- */
533 key_split(&k->k->k); kd = k->k->k;
538 /* --- Choose a private key --- */
540 x = mprand_range(MP_NEWSEC, q, k->r, 0);
541 mpmont_create(&mm, p);
542 y = mpmont_exp(&mm, MP_NEW, g, x);
544 /* --- Store everything away --- */
546 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
548 kkd = key_newstruct();
549 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
550 dolock(k, &kkd, "private");
551 key_structsteal(kd, "private", kkd);
553 mp_drop(x); mp_drop(y);
556 static void alg_dhparam(keyopts *k)
558 static const char *pl[] = { "p", "q", "g", 0 };
560 if (!copyparam(k, pl)) {
569 if (strcmp(k->curve, "list") == 0) {
571 LIST("Built-in prime fields", stdout, ptab[i].name, ptab[i].name);
575 if (dh_parse(&qd, &dp))
576 die(EXIT_FAILURE, "error in field spec: %s", qd.e);
578 die(EXIT_FAILURE, "junk at end of field spec");
579 if ((g = group_prime(&dp)) == 0)
580 die(EXIT_FAILURE, "invalid prime field");
581 if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0)
582 moan("WARNING! group check failed: %s", e);
590 /* --- Choose a large safe prime number --- */
592 if (k->f & f_limlee) {
597 rc = dh_limlee(&dp, k->qbits, k->bits,
598 (k->f & f_subgroup) ? DH_SUBGROUP : 0,
599 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0,
600 (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f);
604 for (i = 0; i < nf; i++) {
607 mp_writedstr(f[i], &d, 10);
610 key_putattr(k->kf, k->k, "factors", d.buf);
613 } else if (k->f & f_kcdsa) {
616 rc = dh_kcdsagen(&dp, k->qbits, k->bits, 0,
617 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0);
622 mp_writedstr(dp.q, &d, 10);
623 mp_div(&v, 0, dp.p, dp.q);
626 mp_writedstr(v, &d, 10);
628 key_putattr(k->kf, k->k, "factors", d.buf);
632 rc = dh_gen(&dp, k->qbits, k->bits, 0, k->r,
633 (k->f & f_quiet) ? 0 : pgen_ev, 0);
636 die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed");
639 kd = key_newstruct();
640 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p));
641 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q));
642 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g));
646 key_setkeydata(k->kf, k->k, kd);
651 static void alg_dh(keyopts *k)
658 /* --- Get the shared parameters --- */
661 key_split(&k->k->k); kd = k->k->k;
666 /* --- Choose a suitable private key --- *
668 * Since %$g$% has order %$q$%, choose %$x < q$%.
671 x = mprand_range(MP_NEWSEC, q, k->r, 0);
673 /* --- Compute the public key %$y = g^x \bmod p$% --- */
675 mpmont_create(&mm, p);
676 y = mpmont_exp(&mm, MP_NEW, g, x);
679 /* --- Store everything away --- */
681 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
683 kkd = key_newstruct();
684 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
685 dolock(k, &kkd, "private");
686 key_structsteal(kd, "private", kkd);
688 mp_drop(x); mp_drop(y);
691 static void alg_bbs(keyopts *k)
696 /* --- Sanity checking --- */
699 die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys");
703 /* --- Generate the BBS parameters --- */
705 if (bbs_gen(&bp, k->bits, k->r, 0,
706 (k->f & f_quiet) ? 0 : pgen_ev, 0))
707 die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed");
709 /* --- Allrighty then --- */
711 kd = key_newstruct();
712 key_structsteal(kd, "n", key_newmp(KCAT_PUB, bp.n));
714 kkd = key_newstruct();
715 key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, bp.p));
716 key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, bp.q));
717 dolock(k, &kkd, "private");
718 key_structsteal(kd, "private", kkd);
719 key_setkeydata(k->kf, k->k, kd);
725 static void alg_binparam(keyopts *k)
727 static const char *pl[] = { "p", "q", "g", 0 };
728 if (!copyparam(k, pl)) {
735 /* --- Decide on a field --- */
737 if (!k->bits) k->bits = 128;
738 if (k->curve && strcmp(k->curve, "list") == 0) {
740 LIST("Built-in binary fields", stdout,
741 bintab[i].name, bintab[i].name);
745 if (k->bits <= 40) k->curve = "p1363-40";
746 else if (k->bits <= 56) k->curve = "p1363-56";
747 else if (k->bits <= 64) k->curve = "p1363-64";
748 else if (k->bits <= 80) k->curve = "p1363-80";
749 else if (k->bits <= 112) k->curve = "p1363-112";
750 else if (k->bits <= 128) k->curve = "p1363-128";
753 "no built-in binary fields provide %u-bit security",
758 /* --- Check it --- */
762 if (dhbin_parse(&qd, &gb))
763 die(EXIT_FAILURE, "error in field spec: %s", qd.e);
765 die(EXIT_FAILURE, "junk at end of field spec");
766 if ((g = group_binary(&gb)) == 0)
767 die(EXIT_FAILURE, "invalid binary field");
768 if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0)
769 moan("WARNING! group check failed: %s", e);
772 /* --- Write out the answer --- */
774 kd = key_newstruct();
775 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, gb.p));
776 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, gb.q));
777 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, gb.g));
781 key_setkeydata(k->kf, k->k, kd);
786 static void alg_bin(keyopts *k)
793 /* --- Get the shared parameters --- */
796 key_split(&k->k->k); kd = k->k->k;
801 /* --- Choose a suitable private key --- *
803 * Since %$g$% has order %$q$%, choose %$x < q$%.
806 x = mprand_range(MP_NEWSEC, q, k->r, 0);
808 /* --- Compute the public key %$y = g^x \bmod p$% --- */
810 gfreduce_create(&r, p);
811 y = gfreduce_exp(&r, MP_NEW, g, x);
812 gfreduce_destroy(&r);
814 /* --- Store everything away --- */
816 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
818 kkd = key_newstruct();
819 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
820 dolock(k, &kkd, "private");
821 key_structsteal(kd, "private", kkd);
823 mp_drop(x); mp_drop(y);
826 static void alg_ecparam(keyopts *k)
828 static const char *pl[] = { "curve", 0 };
829 if (!copyparam(k, pl)) {
834 /* --- Decide on a curve --- */
836 if (!k->bits) k->bits = 256;
837 if (k->curve && strcmp(k->curve, "list") == 0) {
839 LIST("Built-in elliptic curves", stdout,
840 ectab[i].name, ectab[i].name);
844 if (k->bits <= 56) k->curve = "secp112r1";
845 else if (k->bits <= 64) k->curve = "secp128r1";
846 else if (k->bits <= 80) k->curve = "secp160r1";
847 else if (k->bits <= 96) k->curve = "secp192r1";
848 else if (k->bits <= 112) k->curve = "secp224r1";
849 else if (k->bits <= 128) k->curve = "secp256r1";
850 else if (k->bits <= 192) k->curve = "secp384r1";
851 else if (k->bits <= 256) k->curve = "secp521r1";
853 die(EXIT_FAILURE, "no built-in curves provide %u-bit security",
857 /* --- Check it --- */
859 if ((e = ec_getinfo(&ei, k->curve)) != 0)
860 die(EXIT_FAILURE, "error in curve spec: %s", e);
861 if (!(k->f & f_quiet) && (e = ec_checkinfo(&ei, k->r)) != 0)
862 moan("WARNING! curve check failed: %s", e);
865 /* --- Write out the answer --- */
867 kd = key_newstruct();
868 key_structsteal(kd, "curve", key_newstring(KCAT_SHARE, k->curve));
869 key_setkeydata(k->kf, k->k, kd);
874 static void alg_ec(keyopts *k)
883 /* --- Get the curve --- */
886 key_split(&k->k->k); kd = k->k->k;
887 if ((kkd = key_structfind(kd, "curve")) == 0)
888 die(EXIT_FAILURE, "unexpected failure looking up subkey `curve')");
889 if ((kkd->e & KF_ENCMASK) != KENC_STRING)
890 die(EXIT_FAILURE, "subkey `curve' is not a string");
891 if ((e = ec_getinfo(&ei, kkd->u.p)) != 0)
892 die(EXIT_FAILURE, "error in curve spec: %s", e);
894 /* --- Invent a private exponent and compute the public key --- */
896 x = mprand_range(MP_NEWSEC, ei.r, k->r, 0);
897 ec_mul(ei.c, &p, &ei.g, x);
899 /* --- Store everything away --- */
901 key_structsteal(kd, "p", key_newec(KCAT_PUB, &p));
903 kkd = key_newstruct();
904 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
905 dolock(k, &kkd, "private");
906 key_structsteal(kd, "private", kkd);
914 /* --- The algorithm tables --- */
916 typedef struct keyalg {
918 void (*proc)(keyopts *o);
922 static keyalg algtab[] = {
923 { "binary", alg_binary, "Plain binary data" },
924 { "des", alg_des, "Binary with DES-style parity" },
925 { "rsa", alg_rsa, "RSA public-key encryption" },
926 { "bbs", alg_bbs, "Blum-Blum-Shub generator" },
927 { "dsa", alg_dsa, "DSA digital signatures" },
928 { "dsa-param", alg_dsaparam, "DSA shared parameters" },
929 { "dh", alg_dh, "Diffie-Hellman key exchange" },
930 { "dh-param", alg_dhparam, "Diffie-Hellman parameters" },
931 { "bindh", alg_bin, "DH over a binary field" },
932 { "bindh-param", alg_binparam, "Binary-field DH parameters" },
933 { "ec-param", alg_ecparam, "Elliptic curve parameters" },
934 { "ec", alg_ec, "Elliptic curve crypto" },
938 /* --- @cmd_add@ --- */
940 static int cmd_add(int argc, char *argv[])
943 time_t exp = KEXP_EXPIRE;
944 uint32 kid = rand_global.ops->word(&rand_global);
945 const char *tag = 0, *ptag = 0;
947 keyalg *alg = algtab;
948 const char *rtag = 0;
949 const struct seedalg *sa = SEEDALG_DEFAULT;
950 keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0, 0 };
951 const char *seed = 0;
954 /* --- Parse options for the subcommand --- */
957 static struct option opt[] = {
958 { "algorithm", OPTF_ARGREQ, 0, 'a' },
959 { "bits", OPTF_ARGREQ, 0, 'b' },
960 { "qbits", OPTF_ARGREQ, 0, 'B' },
961 { "parameters", OPTF_ARGREQ, 0, 'p' },
962 { "expire", OPTF_ARGREQ, 0, 'e' },
963 { "comment", OPTF_ARGREQ, 0, 'c' },
964 { "tag", OPTF_ARGREQ, 0, 't' },
965 { "rand-id", OPTF_ARGREQ, 0, 'R' },
966 { "key-id", OPTF_ARGREQ, 0, 'I' },
967 { "curve", OPTF_ARGREQ, 0, 'C' },
968 { "seedalg", OPTF_ARGREQ, 0, 'A' },
969 { "seed", OPTF_ARGREQ, 0, 's' },
970 { "newseed", OPTF_ARGREQ, 0, 'n' },
971 { "lock", 0, 0, 'l' },
972 { "quiet", 0, 0, 'q' },
973 { "lim-lee", 0, 0, 'L' },
974 { "subgroup", 0, 0, 'S' },
975 { "kcdsa", 0, 0, 'K' },
978 int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
983 /* --- Handle the various options --- */
987 /* --- Read an algorithm name --- */
991 size_t sz = strlen(optarg);
993 if (strcmp(optarg, "list") == 0) {
994 for (a = algtab; a->name; a++)
995 printf("%-10s %s\n", a->name, a->help);
1000 for (a = algtab; a->name; a++) {
1001 if (strncmp(optarg, a->name, sz) == 0) {
1002 if (a->name[sz] == 0) {
1006 die(EXIT_FAILURE, "ambiguous algorithm name `%s'", optarg);
1012 die(EXIT_FAILURE, "unknown algorithm name `%s'", optarg);
1015 /* --- Bits must be nonzero and a multiple of 8 --- */
1019 k.bits = strtoul(optarg, &p, 0);
1020 if (k.bits == 0 || *p != 0)
1021 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
1026 k.qbits = strtoul(optarg, &p, 0);
1027 if (k.qbits == 0 || *p != 0)
1028 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
1031 /* --- Parameter selection --- */
1037 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1040 if (strcmp(optarg, "forever") == 0)
1043 exp = get_date(optarg, 0);
1045 die(EXIT_FAILURE, "bad expiry date `%s'", optarg);
1049 /* --- Store comments without interpretation --- */
1052 if (key_chkcomment(optarg))
1053 die(EXIT_FAILURE, "bad comment string `%s'", optarg);
1057 /* --- Elliptic curve parameters --- */
1063 /* --- Store tags --- */
1066 if (key_chkident(optarg))
1067 die(EXIT_FAILURE, "bad tag string `%s'", optarg);
1074 /* --- Seeding --- */
1077 const struct seedalg *ss;
1078 if (strcmp(optarg, "list") == 0) {
1079 printf("Seed algorithms:\n");
1080 for (ss = seedtab; ss->p; ss++)
1081 printf(" %s\n", ss->p);
1084 if (seed) die(EXIT_FAILURE, "seed already set -- put -A first");
1086 for (ss = seedtab; ss->p; ss++) {
1087 if (strcmp(optarg, ss->p) == 0)
1091 die(EXIT_FAILURE, "seed algorithm `%s' not known", optarg);
1097 if (seed) die(EXIT_FAILURE, "seed already set");
1099 base64_decode(&b, optarg, strlen(optarg), &d);
1100 base64_decode(&b, 0, 0, &d);
1101 k.r = sa->gen(d.buf, d.len);
1110 unsigned n = strtoul(optarg, &p, 0);
1111 if (n == 0 || *p != 0 || n % 8 != 0)
1112 die(EXIT_FAILURE, "bad seed length `%s'", optarg);
1113 if (seed) die(EXIT_FAILURE, "seed already set");
1116 rand_get(RAND_GLOBAL, p, n);
1118 base64_encode(&b, p, n, &d);
1119 base64_encode(&b, 0, 0, &d);
1121 k.r = sa->gen(p, n);
1124 /* --- Key id --- */
1131 id = strtoul(optarg, &p, 16);
1132 if (errno || *p || id > MASK32)
1133 die(EXIT_FAILURE, "bad key-id `%s'", optarg);
1137 /* --- Other flags --- */
1158 /* --- Other things are bogus --- */
1166 /* --- Various sorts of bogosity --- */
1168 if ((k.f & f_bogus) || optind + 1 > argc) {
1170 "Usage: add [OPTIONS] TYPE [ATTR...]");
1172 if (key_chkident(argv[optind]))
1173 die(EXIT_FAILURE, "bad key type `%s'", argv[optind]);
1175 /* --- Set up various bits of the state --- */
1177 if (exp == KEXP_EXPIRE)
1178 exp = time(0) + 14 * 24 * 60 * 60;
1180 /* --- Open the file and create the basic key block --- *
1182 * Keep on generating keyids until one of them doesn't collide.
1185 doopen(&f, KOPEN_WRITE);
1188 /* --- Key the generator --- */
1194 if ((err = key_new(&f, kid, argv[optind], exp, &k.k)) == 0)
1196 else if (err != KERR_DUPID)
1197 die(EXIT_FAILURE, "error adding new key: %s", key_strerror(err));
1200 /* --- Set various simple attributes --- */
1205 if (k.f & f_retag) {
1206 if ((kk = key_bytag(&f, tag)) != 0 &&
1208 strcmp(kk->tag, tag) == 0)
1209 key_settag(&f, kk, 0);
1211 if ((err = key_settag(&f, k.k, tag)) != 0)
1212 die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err));
1216 int err = key_setcomment(&f, k.k, c);
1218 die(EXIT_FAILURE, "error setting key comment: %s", key_strerror(err));
1221 setattr(&f, k.k, argv + optind + 1);
1223 key_putattr(&f, k.k, "genseed", seed);
1224 key_putattr(&f, k.k, "seedalg", sa->p);
1227 key_fulltag(k.k, &k.tag);
1229 /* --- Find the parameter key --- */
1232 if ((k.p = key_bytag(&f, ptag)) == 0)
1233 die(EXIT_FAILURE, "parameter key `%s' not found", ptag);
1234 if ((k.p->k->e & KF_ENCMASK) != KENC_STRUCT)
1235 die(EXIT_FAILURE, "parameter key `%s' is not structured", ptag);
1238 /* --- Now generate the actual key data --- */
1244 dstr_destroy(&k.tag);
1249 /*----- Key listing -------------------------------------------------------*/
1251 /* --- Listing options --- */
1253 typedef struct listopts {
1254 const char *tfmt; /* Date format (@strftime@-style) */
1255 int v; /* Verbosity level */
1256 unsigned f; /* Various flags */
1257 time_t t; /* Time now (for key expiry) */
1258 key_filter kf; /* Filter for matching keys */
1261 /* --- Listing flags --- */
1263 #define f_newline 2u /* Write newline before next entry */
1264 #define f_attr 4u /* Written at least one attribute */
1265 #define f_utc 8u /* Emit UTC time, not local time */
1267 /* --- @showkeydata@ --- *
1269 * Arguments: @key_data *k@ = pointer to key to write
1270 * @int ind@ = indentation level
1271 * @listopts *o@ = listing options
1272 * @dstr *d@ = tag string for this subkey
1276 * Use: Emits a piece of key data in a human-readable format.
1279 static void showkeydata(key_data *k, int ind, listopts *o, dstr *d)
1281 #define INDENT(i) do { \
1283 for (_i = 0; _i < (i); _i++) { \
1288 switch (k->e & KF_ENCMASK) {
1290 /* --- Binary key data --- *
1292 * Emit as a simple hex dump.
1296 const octet *p = k->u.k.k;
1297 const octet *l = p + k->u.k.sz;
1300 fputs(" {", stdout);
1305 } else if (sz % 8 == 0)
1309 printf("%02x", *p++);
1314 fputs("}\n", stdout);
1317 /* --- Encrypted data --- *
1319 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1320 * key. Otherwise just say that it's encrypted and move on.
1325 fputs(" encrypted\n", stdout);
1328 if (key_punlock(&kd, k, d->buf))
1329 printf(" <failed to unlock %s>\n", d->buf);
1331 fputs(" encrypted", stdout);
1332 showkeydata(kd, ind, o, d);
1338 /* --- Integer keys --- *
1340 * Emit as a large integer in decimal. This makes using the key in
1341 * `calc' or whatever easier.
1346 mp_writefile(k->u.m, stdout, 10);
1350 /* --- Strings --- */
1353 printf(" `%s'\n", k->u.p);
1356 /* --- Elliptic curve points --- */
1359 if (EC_ATINF(&k->u.e))
1360 fputs(" inf\n", stdout);
1362 fputs(" 0x", stdout); mp_writefile(k->u.e.x, stdout, 16);
1363 fputs(", 0x", stdout); mp_writefile(k->u.e.y, stdout, 16);
1368 /* --- Structured keys --- *
1370 * Just iterate over the subkeys.
1378 fputs(" {\n", stdout);
1379 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
1380 if (!key_match(k, &o->kf))
1383 printf("%s =", tag);
1385 dstr_putf(d, ".%s", tag);
1386 showkeydata(k, ind + 2, o, d);
1389 fputs("}\n", stdout);
1396 /* --- @showkey@ --- *
1398 * Arguments: @key *k@ = pointer to key to show
1399 * @listopts *o@ = pointer to listing options
1403 * Use: Emits a listing of a particular key.
1406 static void showkey(key *k, listopts *o)
1408 char ebuf[24], dbuf[24];
1411 /* --- Skip the key if the filter doesn't match --- */
1413 if (!key_match(k->k, &o->kf))
1416 /* --- Sort out the expiry and deletion times --- */
1418 if (KEY_EXPIRED(o->t, k->exp))
1419 strcpy(ebuf, "expired");
1420 else if (k->exp == KEXP_FOREVER)
1421 strcpy(ebuf, "forever");
1423 tm = (o->f & f_utc) ? gmtime(&k->exp) : localtime(&k->exp);
1424 strftime(ebuf, sizeof(ebuf), o->tfmt, tm);
1427 if (KEY_EXPIRED(o->t, k->del))
1428 strcpy(dbuf, "deleted");
1429 else if (k->del == KEXP_FOREVER)
1430 strcpy(dbuf, "forever");
1432 tm = (o->f & f_utc) ? gmtime(&k->del) : localtime(&k->del);
1433 strftime(dbuf, sizeof(dbuf), o->tfmt, tm);
1436 /* --- If in compact format, just display and quit --- */
1439 if (!(o->f & f_newline)) {
1440 printf("%8s %-20s %-20s %-10s %-10s\n",
1441 "Id", "Tag", "Type", "Expire", "Delete");
1443 printf("%08lx %-20s %-20s %-10s %-10s\n",
1444 (unsigned long)k->id, k->tag ? k->tag : "<none>",
1445 k->type, ebuf, dbuf);
1450 /* --- Display the standard header --- */
1452 if (o->f & f_newline)
1453 fputc('\n', stdout);
1454 printf("keyid: %08lx\n", (unsigned long)k->id);
1455 printf("tag: %s\n", k->tag ? k->tag : "<none>");
1456 printf("type: %s\n", k->type);
1457 printf("expiry: %s\n", ebuf);
1458 printf("delete: %s\n", dbuf);
1459 printf("comment: %s\n", k->c ? k->c : "<none>");
1461 /* --- Display the attributes --- */
1465 const char *av, *an;
1468 printf("attributes:");
1469 for (key_mkattriter(&i, k); key_nextattr(&i, &an, &av); ) {
1470 printf("\n %s = %s", an, av);
1474 fputc('\n', stdout);
1479 /* --- If dumping requested, dump the raw key data --- */
1483 fputs("key:", stdout);
1485 showkeydata(k->k, 0, o, &d);
1492 /* --- @cmd_list@ --- */
1494 static int cmd_list(int argc, char *argv[])
1498 listopts o = { 0, 0, 0, 0, { 0, 0 } };
1500 /* --- Parse subcommand options --- */
1503 static struct option opt[] = {
1504 { "quiet", 0, 0, 'q' },
1505 { "verbose", 0, 0, 'v' },
1506 { "utc", 0, 0, 'u' },
1507 { "filter", OPTF_ARGREQ, 0, 'f' },
1510 int i = mdwopt(argc, argv, "+uqvf:", opt, 0, 0, 0);
1527 int e = key_readflags(optarg, &p, &o.kf.f, &o.kf.m);
1529 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1538 die(EXIT_FAILURE, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1540 /* --- Open the key file --- */
1542 doopen(&f, KOPEN_READ);
1545 /* --- Set up the time format --- */
1548 o.tfmt = "%Y-%m-%d";
1549 else if (o.f & f_utc)
1550 o.tfmt = "%Y-%m-%d %H:%M:%S UTC";
1552 o.tfmt = "%Y-%m-%d %H:%M:%S %Z";
1554 /* --- If specific keys were requested use them, otherwise do all --- *
1556 * Some day, this might turn into a wildcard match.
1559 if (optind < argc) {
1561 if ((k = key_bytag(&f, argv[optind])) != 0)
1564 moan("key `%s' not found", argv[optind]);
1568 } while (optind < argc);
1571 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1579 return (EXIT_FAILURE);
1584 /*----- Command implementation --------------------------------------------*/
1586 /* --- @cmd_expire@ --- */
1588 static int cmd_expire(int argc, char *argv[])
1596 die(EXIT_FAILURE, "Usage: expire TAG...");
1597 doopen(&f, KOPEN_WRITE);
1598 for (i = 1; i < argc; i++) {
1599 if ((k = key_bytag(&f, argv[i])) != 0)
1602 moan("key `%s' not found", argv[i]);
1610 /* --- @cmd_delete@ --- */
1612 static int cmd_delete(int argc, char *argv[])
1620 die(EXIT_FAILURE, "Usage: delete TAG...");
1621 doopen(&f, KOPEN_WRITE);
1622 for (i = 1; i < argc; i++) {
1623 if ((k = key_bytag(&f, argv[i])) != 0)
1626 moan("key `%s' not found", argv[i]);
1634 /* --- @cmd_setattr@ --- */
1636 static int cmd_setattr(int argc, char *argv[])
1642 die(EXIT_FAILURE, "Usage: setattr TAG ATTR...");
1643 doopen(&f, KOPEN_WRITE);
1644 if ((k = key_bytag(&f, argv[1])) == 0)
1645 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1646 setattr(&f, k, argv + 2);
1651 /* --- @cmd_getattr@ --- */
1653 static int cmd_getattr(int argc, char *argv[])
1661 die(EXIT_FAILURE, "Usage: getattr TAG ATTR");
1662 doopen(&f, KOPEN_READ);
1663 if ((k = key_bytag(&f, argv[1])) == 0)
1664 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1666 if ((p = key_getattr(&f, k, argv[2])) == 0)
1667 die(EXIT_FAILURE, "no attribute `%s' for key `%s'", argv[2], d.buf);
1674 /* --- @cmd_finger@ --- */
1676 static void fingerprint(key *k, const gchash *ch, const key_filter *kf)
1684 if (key_fingerprint(k, h, kf)) {
1687 for (i = 0; i < ch->hashsz; i++) {
1688 if (i && i % 4 == 0)
1690 printf("%02x", p[i]);
1692 printf(" %s\n", d.buf);
1698 static int cmd_finger(int argc, char *argv[])
1702 const gchash *ch = &rmd160;
1703 key_filter kf = { KF_NONSECRET, KF_NONSECRET };
1706 static struct option opt[] = {
1707 { "filter", OPTF_ARGREQ, 0, 'f' },
1708 { "algorithm", OPTF_ARGREQ, 0, 'a' },
1711 int i = mdwopt(argc, argv, "+f:a:", opt, 0, 0, 0);
1717 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1719 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1722 if ((ch = ghash_byname(optarg)) == 0)
1723 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
1731 argv += optind; argc -= optind;
1733 die(EXIT_FAILURE, "Usage: fingerprint [-f FILTER] [TAG...]");
1735 doopen(&f, KOPEN_READ);
1739 for (i = 0; i < argc; i++) {
1740 key *k = key_bytag(&f, argv[i]);
1742 fingerprint(k, ch, &kf);
1745 moan("key `%s' not found", argv[i]);
1751 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1752 fingerprint(k, ch, &kf);
1757 /* --- @cmd_verify@ --- */
1759 static unsigned xdigit(char c)
1761 if ('A' <= c && c <= 'Z') return (c + 10 - 'A');
1762 if ('a' <= c && c <= 'z') return (c + 10 - 'a');
1763 if ('0' <= c && c <= '9') return (c - '0');
1767 static void unhexify(octet *q, char *p, size_t n)
1773 if (*p == '-' || *p == ':' || isspace((unsigned char)*p)) {
1780 die(EXIT_FAILURE, "hex string too short");
1781 if (!isxdigit((unsigned char)*p))
1782 die(EXIT_FAILURE, "bad hex string");
1784 die(EXIT_FAILURE, "hex string too long");
1785 a = (a << 4) | xdigit(*p++);
1796 static int cmd_verify(int argc, char *argv[])
1800 const gchash *ch = &rmd160;
1805 key_filter kf = { KF_NONSECRET, KF_NONSECRET };
1808 static struct option opt[] = {
1809 { "filter", OPTF_ARGREQ, 0, 'f' },
1810 { "algorithm", OPTF_ARGREQ, 0, 'a' },
1813 int i = mdwopt(argc, argv, "+f:a:", opt, 0, 0, 0);
1819 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1821 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1824 if ((ch = ghash_byname(optarg)) == 0)
1825 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
1833 argv += optind; argc -= optind;
1834 if (rc || argc != 2)
1835 die(EXIT_FAILURE, "Usage: verify [-f FILTER] TAG FINGERPRINT");
1837 doopen(&f, KOPEN_READ);
1839 if ((k = key_bytag(&f, argv[0])) == 0)
1840 die(EXIT_FAILURE, "key `%s' not found", argv[0]);
1841 buf = xmalloc(ch->hashsz);
1842 unhexify(buf, argv[1], ch->hashsz);
1844 if (!key_fingerprint(k, h, &kf))
1845 die(EXIT_FAILURE, "key has no fingerprintable components (as filtered)");
1846 fpr = GH_DONE(h, 0);
1847 if (memcmp(fpr, buf, ch->hashsz) != 0)
1848 die(EXIT_FAILURE, "key fingerprint mismatch");
1853 /* --- @cmd_comment@ --- */
1855 static int cmd_comment(int argc, char *argv[])
1861 if (argc < 2 || argc > 3)
1862 die(EXIT_FAILURE, "Usage: comment TAG [COMMENT]");
1863 doopen(&f, KOPEN_WRITE);
1864 if ((k = key_bytag(&f, argv[1])) == 0)
1865 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1866 if ((err = key_setcomment(&f, k, argv[2])) != 0)
1867 die(EXIT_FAILURE, "bad comment `%s': %s", argv[2], key_strerror(err));
1872 /* --- @cmd_tag@ --- */
1874 static int cmd_tag(int argc, char *argv[])
1883 static struct option opt[] = {
1884 { "retag", 0, 0, 'r' },
1887 int i = mdwopt(argc, argv, "+r", opt, 0, 0, 0);
1900 argv += optind; argc -= optind;
1901 if (argc < 1 || argc > 2 || rc)
1902 die(EXIT_FAILURE, "Usage: tag [-r] TAG [NEW-TAG]");
1903 doopen(&f, KOPEN_WRITE);
1904 if (flags & f_retag) {
1905 if ((k = key_bytag(&f, argv[1])) != 0 && strcmp(k->tag, argv[1]) == 0)
1906 key_settag(&f, k, 0);
1908 if ((k = key_bytag(&f, argv[0])) == 0)
1909 die(EXIT_FAILURE, "key `%s' not found", argv[0]);
1910 if ((err = key_settag(&f, k, argv[1])) != 0)
1911 die(EXIT_FAILURE, "bad tag `%s': %s", argv[1], key_strerror(err));
1916 /* --- @cmd_lock@ --- */
1918 static int cmd_lock(int argc, char *argv[])
1926 die(EXIT_FAILURE, "Usage: lock QTAG");
1927 doopen(&f, KOPEN_WRITE);
1928 if (key_qtag(&f, argv[1], &d, &k, &kd))
1929 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1930 if ((*kd)->e == KENC_ENCRYPT && key_punlock(kd, 0, d.buf))
1931 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1932 if (key_plock(kd, 0, d.buf))
1933 die(EXIT_FAILURE, "failed to lock key `%s'", d.buf);
1939 /* --- @cmd_unlock@ --- */
1941 static int cmd_unlock(int argc, char *argv[])
1949 die(EXIT_FAILURE, "Usage: unlock QTAG");
1950 doopen(&f, KOPEN_WRITE);
1951 if (key_qtag(&f, argv[1], &d, &k, &kd))
1952 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1953 if ((*kd)->e != KENC_ENCRYPT)
1954 die(EXIT_FAILURE, "key `%s' is not encrypted", d.buf);
1955 if (key_punlock(kd, 0, d.buf))
1956 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1962 /* --- @cmd_extract@ --- */
1964 static int cmd_extract(int argc, char *argv[])
1970 key_filter kf = { 0, 0 };
1972 const char *outfile = 0;
1976 static struct option opt[] = {
1977 { "filter", OPTF_ARGREQ, 0, 'f' },
1980 int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0);
1986 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1988 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1996 argv += optind; argc -= optind;
1998 die(EXIT_FAILURE, "Usage: extract [-f FILTER] FILE [TAG...]");
1999 if (strcmp(*argv, "-") == 0)
2003 dstr_putf(&d, "%s.new", outfile);
2004 if (!(fp = fopen(d.buf, "w"))) {
2005 die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
2006 d.buf, strerror(errno));
2010 doopen(&f, KOPEN_READ);
2014 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
2015 key_extract(&f, k, fp, &kf);
2017 for (i = 1; i < argc; i++) {
2018 if ((k = key_bytag(&f, argv[i])) != 0)
2019 key_extract(&f, k, fp, &kf);
2021 moan("key `%s' not found", argv[i]);
2026 if (fclose(fp) || (outfile && rename(d.buf, outfile)))
2027 die(EXIT_FAILURE, "error writing file: %s", strerror(errno));
2033 /* --- @cmd_tidy@ --- */
2035 static int cmd_tidy(int argc, char *argv[])
2039 die(EXIT_FAILURE, "Usage: tidy");
2040 doopen(&f, KOPEN_WRITE);
2041 f.f |= KF_MODIFIED; /* Nasty hack */
2046 /* --- @cmd_merge@ --- */
2048 static int cmd_merge(int argc, char *argv[])
2054 die(EXIT_FAILURE, "Usage: merge FILE");
2055 if (strcmp(argv[1], "-") == 0)
2057 else if (!(fp = fopen(argv[1], "r"))) {
2058 die(EXIT_FAILURE, "couldn't open `%s' for reading: %s",
2059 argv[1], strerror(errno));
2062 doopen(&f, KOPEN_WRITE);
2063 key_merge(&f, argv[1], fp, key_moan, 0);
2068 /* --- @cmd_show@ --- */
2072 listtab[i].name, listtab[i].name) \
2073 LI("Hash functions", hash, \
2074 ghashtab[i], ghashtab[i]->name) \
2075 LI("Elliptic curves", ec, \
2076 ectab[i].name, ectab[i].name) \
2077 LI("Prime Diffie-Hellman groups", dh, \
2078 ptab[i].name, ptab[i].name) \
2079 LI("Binary Diffie-Hellman groups", bindh, \
2080 bintab[i].name, bintab[i].name) \
2081 LI("Key-generation algorithms", keygen, \
2082 algtab[i].name, algtab[i].name) \
2083 LI("Random seeding algorithms", seed, \
2084 seedtab[i].p, seedtab[i].p)
2086 MAKELISTTAB(listtab, LISTS)
2088 static int cmd_show(int argc, char *argv[])
2090 return (displaylists(listtab, argv + 1));
2093 /*----- Main command table ------------------------------------------------*/
2095 static int cmd_help(int argc, char *argv[]);
2097 static cmd cmds[] = {
2098 { "help", cmd_help, "help [COMMAND...]" },
2099 { "show", cmd_show, "show [ITEM...]" },
2100 { "list", cmd_list, "list [-uqv] [-f FILTER] [TAG...]", "\
2103 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2104 -q, --quiet Show less information.\n\
2105 -v, --verbose Show more information.\n\
2107 { "fingerprint", cmd_finger, "fingerprint [-f FILTER] [TAG...]", "\
2110 -f, --filter=FILT Only hash key components matching FILT.\n\
2111 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2112 ($ show hash for list.)\n\
2114 { "verify", cmd_verify, "verify [-f FILTER] TAG FINGERPRINT", "\
2117 -f, --filter=FILT Only hash key components matching FILT.\n\
2118 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2119 ($ show hash for list.)\n\
2121 { "extract", cmd_extract, "extract [-f FILTER] FILE [TAG...]", "\
2124 -f, --filter=FILT Only extract key components matching FILT.\n\
2126 { "merge", cmd_merge, "merge FILE" },
2127 { "expire", cmd_expire, "expire TAG..." },
2128 { "delete", cmd_delete, "delete TAG..." },
2129 { "setattr", cmd_setattr, "setattr TAG ATTR..." },
2130 { "getattr", cmd_getattr, "getattr TAG ATTR" },
2131 { "comment", cmd_comment, "comment TAG [COMMENT]" },
2132 { "lock", cmd_lock, "lock QTAG" },
2133 { "unlock", cmd_unlock, "unlock QTAG" },
2134 { "tag", cmd_tag, "tag [-r] TAG [NEW-TAG]", "\
2137 -r, --retag Untag any key currently called new-tag.\n\
2139 { "tidy", cmd_tidy, "tidy" },
2141 "add [-OPTIONS] TYPE [ATTR...]\n\
2142 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2143 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2144 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2147 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2148 ($ show keygen for list.)\n\
2149 -b, --bits=N Generate an N-bit key.\n\
2150 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2151 -p, --parameters=TAG Get group parameters from TAG.\n\
2152 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2153 ($ show ec or $ show dh for list.)\n\
2154 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2155 ($ show seed for list.)\n\
2156 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2157 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2158 -e, --expire=TIME Make the key expire after TIME.\n\
2159 -c, --comment=STRING Attach the command STRING to the key.\n\
2160 -t, --tag=TAG Tag the key with the name TAG.\n\
2161 -r, --retag Untag any key currently with that tag.\n\
2162 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2163 -I, --key-id=ID Force the key-id for the new key.\n\
2164 -l, --lock Lock the generated key with a passphrase.\n\
2165 -q, --quiet Don't give progress indicators while working.\n\
2166 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2167 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2168 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2173 static int cmd_help(int argc, char *argv[])
2175 sc_help(cmds, stdout, argv + 1);
2179 /*----- Main code ---------------------------------------------------------*/
2181 /* --- Helpful GNUy functions --- */
2183 static void usage(FILE *fp)
2185 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2188 void version(FILE *fp)
2190 pquis(fp, "$, Catacomb version " VERSION "\n");
2193 void help_global(FILE *fp)
2197 Performs various simple key management operations.\n\
2199 Global command line options:\n\
2201 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2202 -v, --version Display version number.\n\
2203 -u, --usage Display short usage summary.\n\
2205 -k, --keyring=FILE Read and write keys in FILE.\n",
2211 * Arguments: @int argc@ = number of command line arguments
2212 * @char *argv[]@ = array of command line arguments
2214 * Returns: Nonzero on failure.
2216 * Use: Main program. Performs simple key management functions.
2219 int main(int argc, char *argv[])
2225 /* --- Initialization --- */
2230 /* --- Parse command line options --- */
2233 static struct option opt[] = {
2235 /* --- Standard GNUy help options --- */
2237 { "help", 0, 0, 'h' },
2238 { "version", 0, 0, 'v' },
2239 { "usage", 0, 0, 'u' },
2241 /* --- Real live useful options --- */
2243 { "keyring", OPTF_ARGREQ, 0, 'k' },
2245 /* --- Magic terminator --- */
2249 int i = mdwopt(argc, argv, "+hvu k:", opt, 0, 0, 0);
2255 /* --- GNU help options --- */
2258 sc_help(cmds, stdout, argv + optind);
2267 /* --- Real useful options --- */
2273 /* --- Bogosity --- */
2281 /* --- Complain about excessive bogons --- */
2283 if (f & f_bogus || optind == argc) {
2288 /* --- Initialize the Catacomb random number generator --- */
2290 rand_noisesrc(RAND_GLOBAL, &noise_source);
2291 rand_seed(RAND_GLOBAL, 160);
2293 /* --- Dispatch to appropriate command handler --- */
2298 return (findcmd(cmds, argv[0])->cmd(argc, argv));
2301 /*----- That's all, folks -------------------------------------------------*/