3 * Simple key manager program
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
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)
244 /* --- Quick check if no parameters supplied --- */
249 /* --- Run through the checklist --- */
252 key_data *kd = key_structfind(k->p->k, *pp);
254 die(EXIT_FAILURE, "bad parameter key: parameter `%s' not found", *pp);
255 if (!KEY_MATCH(kd, &kf))
256 die(EXIT_FAILURE, "bad parameter key: subkey `%s' is not shared", *pp);
260 /* --- Copy over the parameters --- */
262 kd = key_copydata(k->p->k, &kf);
264 key_setkeydata(k->kf, k->k, kd);
267 /* --- Copy over attributes --- */
269 for (key_mkattriter(&i, k->p); key_nextattr(&i, &n, &v); )
270 key_putattr(k->kf, k->k, n, v);
279 * Arguments: @key_data *k@ = pointer to key data block
280 * @const char *tag@ = tag string to use
282 * Returns: Pointer to multiprecision integer key item.
284 * Use: Fetches an MP key component.
287 static mp *getmp(key_data *k, const char *tag)
289 k = key_structfind(k, tag);
291 die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag);
292 if ((k->e & KF_ENCMASK) != KENC_MP)
293 die(EXIT_FAILURE, "subkey `%s' has an incompatible type");
297 /* --- @keyrand@ --- *
299 * Arguments: @key_file *kf@ = pointer to key file
300 * @const char *id@ = pointer to key id (or null)
304 * Use: Keys the random number generator.
307 static void keyrand(key_file *kf, const char *id)
311 /* --- Find the key --- */
314 if ((k = key_bytag(kf, id)) == 0)
315 die(EXIT_FAILURE, "key `%s' not found", id);
317 k = key_bytype(kf, "catacomb-rand");
320 key_data *kd = k->k, *kkd;
324 switch (kd->e & KF_ENCMASK) {
330 if (key_punlock(&kkd, kd, d.buf))
331 die(EXIT_FAILURE, "error unlocking key `%s'", d.buf);
339 die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf);
343 /* --- Key the generator --- */
345 rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz);
350 /* --- Key generation algorithms --- */
352 static void alg_binary(keyopts *k)
362 die(EXIT_FAILURE, "no shared parameters for binary keys");
364 sz = (k->bits + 7) >> 3;
366 m = (1 << (((k->bits - 1) & 7) + 1)) - 1;
367 k->r->ops->fill(k->r, p, sz);
369 kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz);
372 key_setkeydata(k->kf, k->k, kd);
377 static void alg_des(keyopts *k)
387 die(EXIT_FAILURE, "no shared parameters for DES keys");
388 if (k->bits % 56 || k->bits > 168)
389 die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long");
393 k->r->ops->fill(k->r, p, sz);
394 for (i = 0; i < sz; i++) {
395 octet x = p[i] | 0x01;
399 p[i] = (p[i] & 0xfe) | (x & 0x01);
401 kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz);
404 key_setkeydata(k->kf, k->k, kd);
409 static void alg_rsa(keyopts *k)
414 /* --- Sanity checking --- */
417 die(EXIT_FAILURE, "no shared parameters for RSA keys");
421 /* --- Generate the RSA parameters --- */
423 if (rsa_gen(&rp, k->bits, k->r, 0,
424 (k->f & f_quiet) ? 0 : pgen_ev, 0))
425 die(EXIT_FAILURE, "RSA key generation failed");
427 /* --- Run a test encryption --- */
430 grand *g = fibrand_create(k->r->ops->word(k->r));
432 mp *m = mprand_range(MP_NEW, rp.n, g, 0);
437 c = rsa_qpubop(&rpp, MP_NEW, m);
438 c = rsa_qprivop(&rp, c, c, g);
441 die(EXIT_FAILURE, "test encryption failed");
447 /* --- Allrighty then --- */
449 kd = key_newstruct();
450 key_structsteal(kd, "n", key_newmp(KCAT_PUB, rp.n));
451 key_structsteal(kd, "e", key_newmp(KCAT_PUB, rp.e));
453 kkd = key_newstruct();
454 key_structsteal(kkd, "d", key_newmp(KCAT_PRIV | KF_BURN, rp.d));
455 key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, rp.p));
456 key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, rp.q));
457 key_structsteal(kkd, "q-inv", key_newmp(KCAT_PRIV | KF_BURN, rp.q_inv));
458 key_structsteal(kkd, "d-mod-p", key_newmp(KCAT_PRIV | KF_BURN, rp.dp));
459 key_structsteal(kkd, "d-mod-q", key_newmp(KCAT_PRIV | KF_BURN, rp.dq));
460 dolock(k, &kkd, "private");
461 key_structsteal(kd, "private", kkd);
462 key_setkeydata(k->kf, k->k, kd);
467 static void alg_dsaparam(keyopts *k)
469 static const char *pl[] = { "q", "p", "g", 0 };
470 if (!copyparam(k, pl)) {
479 /* --- Choose appropriate bit lengths if necessary --- */
486 /* --- Allocate a seed block --- */
488 sz = (k->qbits + 7) >> 3;
490 k->r->ops->fill(k->r, p, sz);
492 /* --- Allocate the parameters --- */
494 if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, &ds,
495 (k->f & f_quiet) ? 0 : pgen_ev, 0))
496 die(EXIT_FAILURE, "DSA parameter generation failed");
498 /* --- Store the parameters --- */
500 kd = key_newstruct();
501 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q));
502 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p));
503 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g));
507 key_setkeydata(k->kf, k->k, kd);
510 /* --- Store the seed for future verification --- */
515 base64_encode(&c, ds.p, ds.sz, &d);
516 base64_encode(&c, 0, 0, &d);
518 key_putattr(k->kf, k->k, "seed", d.buf);
520 dstr_putf(&d, "%u", ds.count);
521 key_putattr(k->kf, k->k, "count", d.buf);
528 static void alg_dsa(keyopts *k)
535 /* --- Get the shared parameters --- */
538 key_split(&k->k->k); kd = k->k->k;
543 /* --- Choose a private key --- */
545 x = mprand_range(MP_NEWSEC, q, k->r, 0);
546 mpmont_create(&mm, p);
547 y = mpmont_exp(&mm, MP_NEW, g, x);
549 /* --- Store everything away --- */
551 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
553 kkd = key_newstruct();
554 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
555 dolock(k, &kkd, "private");
556 key_structsteal(kd, "private", kkd);
558 mp_drop(x); mp_drop(y);
561 static void alg_dhparam(keyopts *k)
563 static const char *pl[] = { "p", "q", "g", 0 };
565 if (!copyparam(k, pl)) {
574 if (strcmp(k->curve, "list") == 0) {
576 LIST("Built-in prime fields", stdout, ptab[i].name, ptab[i].name);
580 if (dh_parse(&qd, &dp))
581 die(EXIT_FAILURE, "error in field spec: %s", qd.e);
583 die(EXIT_FAILURE, "junk at end of field spec");
584 if ((g = group_prime(&dp)) == 0)
585 die(EXIT_FAILURE, "invalid prime field");
586 if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0)
587 moan("WARNING! group check failed: %s", e);
595 /* --- Choose a large safe prime number --- */
597 if (k->f & f_limlee) {
602 rc = dh_limlee(&dp, k->qbits, k->bits,
603 (k->f & f_subgroup) ? DH_SUBGROUP : 0,
604 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0,
605 (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f);
609 for (i = 0; i < nf; i++) {
612 mp_writedstr(f[i], &d, 10);
615 key_putattr(k->kf, k->k, "factors", d.buf);
618 } else if (k->f & f_kcdsa) {
621 rc = dh_kcdsagen(&dp, k->qbits, k->bits, 0,
622 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0);
627 mp_writedstr(dp.q, &d, 10);
628 mp_div(&v, 0, dp.p, dp.q);
631 mp_writedstr(v, &d, 10);
633 key_putattr(k->kf, k->k, "factors", d.buf);
637 rc = dh_gen(&dp, k->qbits, k->bits, 0, k->r,
638 (k->f & f_quiet) ? 0 : pgen_ev, 0);
641 die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed");
644 kd = key_newstruct();
645 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p));
646 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q));
647 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g));
651 key_setkeydata(k->kf, k->k, kd);
656 static void alg_dh(keyopts *k)
663 /* --- Get the shared parameters --- */
666 key_split(&k->k->k); kd = k->k->k;
671 /* --- Choose a suitable private key --- *
673 * Since %$g$% has order %$q$%, choose %$x < q$%.
676 x = mprand_range(MP_NEWSEC, q, k->r, 0);
678 /* --- Compute the public key %$y = g^x \bmod p$% --- */
680 mpmont_create(&mm, p);
681 y = mpmont_exp(&mm, MP_NEW, g, x);
684 /* --- Store everything away --- */
686 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
688 kkd = key_newstruct();
689 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
690 dolock(k, &kkd, "private");
691 key_structsteal(kd, "private", kkd);
693 mp_drop(x); mp_drop(y);
696 static void alg_bbs(keyopts *k)
701 /* --- Sanity checking --- */
704 die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys");
708 /* --- Generate the BBS parameters --- */
710 if (bbs_gen(&bp, k->bits, k->r, 0,
711 (k->f & f_quiet) ? 0 : pgen_ev, 0))
712 die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed");
714 /* --- Allrighty then --- */
716 kd = key_newstruct();
717 key_structsteal(kd, "n", key_newmp(KCAT_PUB, bp.n));
719 kkd = key_newstruct();
720 key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, bp.p));
721 key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, bp.q));
722 dolock(k, &kkd, "private");
723 key_structsteal(kd, "private", kkd);
724 key_setkeydata(k->kf, k->k, kd);
730 static void alg_binparam(keyopts *k)
732 static const char *pl[] = { "p", "q", "g", 0 };
733 if (!copyparam(k, pl)) {
740 /* --- Decide on a field --- */
742 if (!k->bits) k->bits = 128;
743 if (k->curve && strcmp(k->curve, "list") == 0) {
745 LIST("Built-in binary fields", stdout,
746 bintab[i].name, bintab[i].name);
750 if (k->bits <= 40) k->curve = "p1363-40";
751 else if (k->bits <= 56) k->curve = "p1363-56";
752 else if (k->bits <= 64) k->curve = "p1363-64";
753 else if (k->bits <= 80) k->curve = "p1363-80";
754 else if (k->bits <= 112) k->curve = "p1363-112";
755 else if (k->bits <= 128) k->curve = "p1363-128";
758 "no built-in binary fields provide %u-bit security",
763 /* --- Check it --- */
767 if (dhbin_parse(&qd, &gb))
768 die(EXIT_FAILURE, "error in field spec: %s", qd.e);
770 die(EXIT_FAILURE, "junk at end of field spec");
771 if ((g = group_binary(&gb)) == 0)
772 die(EXIT_FAILURE, "invalid binary field");
773 if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0)
774 moan("WARNING! group check failed: %s", e);
777 /* --- Write out the answer --- */
779 kd = key_newstruct();
780 key_structsteal(kd, "p", key_newmp(KCAT_SHARE, gb.p));
781 key_structsteal(kd, "q", key_newmp(KCAT_SHARE, gb.q));
782 key_structsteal(kd, "g", key_newmp(KCAT_SHARE, gb.g));
786 key_setkeydata(k->kf, k->k, kd);
791 static void alg_bin(keyopts *k)
798 /* --- Get the shared parameters --- */
801 key_split(&k->k->k); kd = k->k->k;
806 /* --- Choose a suitable private key --- *
808 * Since %$g$% has order %$q$%, choose %$x < q$%.
811 x = mprand_range(MP_NEWSEC, q, k->r, 0);
813 /* --- Compute the public key %$y = g^x \bmod p$% --- */
815 gfreduce_create(&r, p);
816 y = gfreduce_exp(&r, MP_NEW, g, x);
817 gfreduce_destroy(&r);
819 /* --- Store everything away --- */
821 key_structsteal(kd, "y", key_newmp(KCAT_PUB, y));
823 kkd = key_newstruct();
824 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
825 dolock(k, &kkd, "private");
826 key_structsteal(kd, "private", kkd);
828 mp_drop(x); mp_drop(y);
831 static void alg_ecparam(keyopts *k)
833 static const char *pl[] = { "curve", 0 };
834 if (!copyparam(k, pl)) {
839 /* --- Decide on a curve --- */
841 if (!k->bits) k->bits = 256;
842 if (k->curve && strcmp(k->curve, "list") == 0) {
844 LIST("Built-in elliptic curves", stdout,
845 ectab[i].name, ectab[i].name);
849 if (k->bits <= 56) k->curve = "secp112r1";
850 else if (k->bits <= 64) k->curve = "secp128r1";
851 else if (k->bits <= 80) k->curve = "secp160r1";
852 else if (k->bits <= 96) k->curve = "secp192r1";
853 else if (k->bits <= 112) k->curve = "secp224r1";
854 else if (k->bits <= 128) k->curve = "secp256r1";
855 else if (k->bits <= 192) k->curve = "secp384r1";
856 else if (k->bits <= 256) k->curve = "secp521r1";
858 die(EXIT_FAILURE, "no built-in curves provide %u-bit security",
862 /* --- Check it --- */
864 if ((e = ec_getinfo(&ei, k->curve)) != 0)
865 die(EXIT_FAILURE, "error in curve spec: %s", e);
866 if (!(k->f & f_quiet) && (e = ec_checkinfo(&ei, k->r)) != 0)
867 moan("WARNING! curve check failed: %s", e);
870 /* --- Write out the answer --- */
872 kd = key_newstruct();
873 key_structsteal(kd, "curve", key_newstring(KCAT_SHARE, k->curve));
874 key_setkeydata(k->kf, k->k, kd);
879 static void alg_ec(keyopts *k)
888 /* --- Get the curve --- */
891 key_split(&k->k->k); kd = k->k->k;
892 if ((kkd = key_structfind(kd, "curve")) == 0)
893 die(EXIT_FAILURE, "unexpected failure looking up subkey `curve')");
894 if ((kkd->e & KF_ENCMASK) != KENC_STRING)
895 die(EXIT_FAILURE, "subkey `curve' is not a string");
896 if ((e = ec_getinfo(&ei, kkd->u.p)) != 0)
897 die(EXIT_FAILURE, "error in curve spec: %s", e);
899 /* --- Invent a private exponent and compute the public key --- */
901 x = mprand_range(MP_NEWSEC, ei.r, k->r, 0);
902 ec_mul(ei.c, &p, &ei.g, x);
904 /* --- Store everything away --- */
906 key_structsteal(kd, "p", key_newec(KCAT_PUB, &p));
908 kkd = key_newstruct();
909 key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x));
910 dolock(k, &kkd, "private");
911 key_structsteal(kd, "private", kkd);
919 /* --- The algorithm tables --- */
921 typedef struct keyalg {
923 void (*proc)(keyopts *o);
927 static keyalg algtab[] = {
928 { "binary", alg_binary, "Plain binary data" },
929 { "des", alg_des, "Binary with DES-style parity" },
930 { "rsa", alg_rsa, "RSA public-key encryption" },
931 { "bbs", alg_bbs, "Blum-Blum-Shub generator" },
932 { "dsa", alg_dsa, "DSA digital signatures" },
933 { "dsa-param", alg_dsaparam, "DSA shared parameters" },
934 { "dh", alg_dh, "Diffie-Hellman key exchange" },
935 { "dh-param", alg_dhparam, "Diffie-Hellman parameters" },
936 { "bindh", alg_bin, "DH over a binary field" },
937 { "bindh-param", alg_binparam, "Binary-field DH parameters" },
938 { "ec-param", alg_ecparam, "Elliptic curve parameters" },
939 { "ec", alg_ec, "Elliptic curve crypto" },
943 /* --- @cmd_add@ --- */
945 static int cmd_add(int argc, char *argv[])
948 time_t exp = KEXP_EXPIRE;
949 uint32 kid = rand_global.ops->word(&rand_global);
950 const char *tag = 0, *ptag = 0;
952 keyalg *alg = algtab;
953 const char *rtag = 0;
954 const struct seedalg *sa = SEEDALG_DEFAULT;
955 keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0, 0 };
956 const char *seed = 0;
959 /* --- Parse options for the subcommand --- */
962 static struct option opt[] = {
963 { "algorithm", OPTF_ARGREQ, 0, 'a' },
964 { "bits", OPTF_ARGREQ, 0, 'b' },
965 { "qbits", OPTF_ARGREQ, 0, 'B' },
966 { "parameters", OPTF_ARGREQ, 0, 'p' },
967 { "expire", OPTF_ARGREQ, 0, 'e' },
968 { "comment", OPTF_ARGREQ, 0, 'c' },
969 { "tag", OPTF_ARGREQ, 0, 't' },
970 { "rand-id", OPTF_ARGREQ, 0, 'R' },
971 { "key-id", OPTF_ARGREQ, 0, 'I' },
972 { "curve", OPTF_ARGREQ, 0, 'C' },
973 { "seedalg", OPTF_ARGREQ, 0, 'A' },
974 { "seed", OPTF_ARGREQ, 0, 's' },
975 { "newseed", OPTF_ARGREQ, 0, 'n' },
976 { "lock", 0, 0, 'l' },
977 { "quiet", 0, 0, 'q' },
978 { "lim-lee", 0, 0, 'L' },
979 { "subgroup", 0, 0, 'S' },
980 { "kcdsa", 0, 0, 'K' },
983 int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
988 /* --- Handle the various options --- */
992 /* --- Read an algorithm name --- */
996 size_t sz = strlen(optarg);
998 if (strcmp(optarg, "list") == 0) {
999 for (a = algtab; a->name; a++)
1000 printf("%-10s %s\n", a->name, a->help);
1005 for (a = algtab; a->name; a++) {
1006 if (strncmp(optarg, a->name, sz) == 0) {
1007 if (a->name[sz] == 0) {
1011 die(EXIT_FAILURE, "ambiguous algorithm name `%s'", optarg);
1017 die(EXIT_FAILURE, "unknown algorithm name `%s'", optarg);
1020 /* --- Bits must be nonzero and a multiple of 8 --- */
1024 k.bits = strtoul(optarg, &p, 0);
1025 if (k.bits == 0 || *p != 0)
1026 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
1031 k.qbits = strtoul(optarg, &p, 0);
1032 if (k.qbits == 0 || *p != 0)
1033 die(EXIT_FAILURE, "bad bitlength `%s'", optarg);
1036 /* --- Parameter selection --- */
1042 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1045 if (strcmp(optarg, "forever") == 0)
1048 exp = get_date(optarg, 0);
1050 die(EXIT_FAILURE, "bad expiry date `%s'", optarg);
1054 /* --- Store comments without interpretation --- */
1057 if (key_chkcomment(optarg))
1058 die(EXIT_FAILURE, "bad comment string `%s'", optarg);
1062 /* --- Elliptic curve parameters --- */
1068 /* --- Store tags --- */
1071 if (key_chkident(optarg))
1072 die(EXIT_FAILURE, "bad tag string `%s'", optarg);
1079 /* --- Seeding --- */
1082 const struct seedalg *ss;
1083 if (strcmp(optarg, "list") == 0) {
1084 printf("Seed algorithms:\n");
1085 for (ss = seedtab; ss->p; ss++)
1086 printf(" %s\n", ss->p);
1089 if (seed) die(EXIT_FAILURE, "seed already set -- put -A first");
1091 for (ss = seedtab; ss->p; ss++) {
1092 if (strcmp(optarg, ss->p) == 0)
1096 die(EXIT_FAILURE, "seed algorithm `%s' not known", optarg);
1102 if (seed) die(EXIT_FAILURE, "seed already set");
1104 base64_decode(&b, optarg, strlen(optarg), &d);
1105 base64_decode(&b, 0, 0, &d);
1106 k.r = sa->gen(d.buf, d.len);
1115 unsigned n = strtoul(optarg, &p, 0);
1116 if (n == 0 || *p != 0 || n % 8 != 0)
1117 die(EXIT_FAILURE, "bad seed length `%s'", optarg);
1118 if (seed) die(EXIT_FAILURE, "seed already set");
1121 rand_get(RAND_GLOBAL, p, n);
1123 base64_encode(&b, p, n, &d);
1124 base64_encode(&b, 0, 0, &d);
1126 k.r = sa->gen(p, n);
1129 /* --- Key id --- */
1136 id = strtoul(optarg, &p, 16);
1137 if (errno || *p || id > MASK32)
1138 die(EXIT_FAILURE, "bad key-id `%s'", optarg);
1142 /* --- Other flags --- */
1163 /* --- Other things are bogus --- */
1171 /* --- Various sorts of bogosity --- */
1173 if ((k.f & f_bogus) || optind + 1 > argc) {
1175 "Usage: add [OPTIONS] TYPE [ATTR...]");
1177 if (key_chkident(argv[optind]))
1178 die(EXIT_FAILURE, "bad key type `%s'", argv[optind]);
1180 /* --- Set up various bits of the state --- */
1182 if (exp == KEXP_EXPIRE)
1183 exp = time(0) + 14 * 24 * 60 * 60;
1185 /* --- Open the file and create the basic key block --- *
1187 * Keep on generating keyids until one of them doesn't collide.
1190 doopen(&f, KOPEN_WRITE);
1193 /* --- Key the generator --- */
1199 if ((err = key_new(&f, kid, argv[optind], exp, &k.k)) == 0)
1201 else if (err != KERR_DUPID)
1202 die(EXIT_FAILURE, "error adding new key: %s", key_strerror(err));
1205 /* --- Set various simple attributes --- */
1210 if (k.f & f_retag) {
1211 if ((kk = key_bytag(&f, tag)) != 0 &&
1213 strcmp(kk->tag, tag) == 0)
1214 key_settag(&f, kk, 0);
1216 if ((err = key_settag(&f, k.k, tag)) != 0)
1217 die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err));
1221 int err = key_setcomment(&f, k.k, c);
1223 die(EXIT_FAILURE, "error setting key comment: %s", key_strerror(err));
1226 setattr(&f, k.k, argv + optind + 1);
1228 key_putattr(&f, k.k, "genseed", seed);
1229 key_putattr(&f, k.k, "seedalg", sa->p);
1232 key_fulltag(k.k, &k.tag);
1234 /* --- Find the parameter key --- */
1237 if ((k.p = key_bytag(&f, ptag)) == 0)
1238 die(EXIT_FAILURE, "parameter key `%s' not found", ptag);
1239 if ((k.p->k->e & KF_ENCMASK) != KENC_STRUCT)
1240 die(EXIT_FAILURE, "parameter key `%s' is not structured", ptag);
1243 /* --- Now generate the actual key data --- */
1249 dstr_destroy(&k.tag);
1254 /*----- Key listing -------------------------------------------------------*/
1256 /* --- Listing options --- */
1258 typedef struct listopts {
1259 const char *tfmt; /* Date format (@strftime@-style) */
1260 int v; /* Verbosity level */
1261 unsigned f; /* Various flags */
1262 time_t t; /* Time now (for key expiry) */
1263 key_filter kf; /* Filter for matching keys */
1266 /* --- Listing flags --- */
1268 #define f_newline 2u /* Write newline before next entry */
1269 #define f_attr 4u /* Written at least one attribute */
1270 #define f_utc 8u /* Emit UTC time, not local time */
1272 /* --- @showkeydata@ --- *
1274 * Arguments: @key_data *k@ = pointer to key to write
1275 * @int ind@ = indentation level
1276 * @listopts *o@ = listing options
1277 * @dstr *d@ = tag string for this subkey
1281 * Use: Emits a piece of key data in a human-readable format.
1284 static void showkeydata(key_data *k, int ind, listopts *o, dstr *d)
1286 #define INDENT(i) do { \
1288 for (_i = 0; _i < (i); _i++) { \
1293 switch (k->e & KF_ENCMASK) {
1295 /* --- Binary key data --- *
1297 * Emit as a simple hex dump.
1301 const octet *p = k->u.k.k;
1302 const octet *l = p + k->u.k.sz;
1305 fputs(" {", stdout);
1310 } else if (sz % 8 == 0)
1314 printf("%02x", *p++);
1319 fputs("}\n", stdout);
1322 /* --- Encrypted data --- *
1324 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1325 * key. Otherwise just say that it's encrypted and move on.
1330 fputs(" encrypted\n", stdout);
1333 if (key_punlock(&kd, k, d->buf))
1334 printf(" <failed to unlock %s>\n", d->buf);
1336 fputs(" encrypted", stdout);
1337 showkeydata(kd, ind, o, d);
1343 /* --- Integer keys --- *
1345 * Emit as a large integer in decimal. This makes using the key in
1346 * `calc' or whatever easier.
1351 mp_writefile(k->u.m, stdout, 10);
1355 /* --- Strings --- */
1358 printf(" `%s'\n", k->u.p);
1361 /* --- Elliptic curve points --- */
1364 if (EC_ATINF(&k->u.e))
1365 fputs(" inf\n", stdout);
1367 fputs(" 0x", stdout); mp_writefile(k->u.e.x, stdout, 16);
1368 fputs(", 0x", stdout); mp_writefile(k->u.e.y, stdout, 16);
1373 /* --- Structured keys --- *
1375 * Just iterate over the subkeys.
1383 fputs(" {\n", stdout);
1384 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
1385 if (!key_match(k, &o->kf))
1388 printf("%s =", tag);
1390 dstr_putf(d, ".%s", tag);
1391 showkeydata(k, ind + 2, o, d);
1394 fputs("}\n", stdout);
1401 /* --- @showkey@ --- *
1403 * Arguments: @key *k@ = pointer to key to show
1404 * @listopts *o@ = pointer to listing options
1408 * Use: Emits a listing of a particular key.
1411 static void showkey(key *k, listopts *o)
1413 char ebuf[24], dbuf[24];
1416 /* --- Skip the key if the filter doesn't match --- */
1418 if (!key_match(k->k, &o->kf))
1421 /* --- Sort out the expiry and deletion times --- */
1423 if (KEY_EXPIRED(o->t, k->exp))
1424 strcpy(ebuf, "expired");
1425 else if (k->exp == KEXP_FOREVER)
1426 strcpy(ebuf, "forever");
1428 tm = (o->f & f_utc) ? gmtime(&k->exp) : localtime(&k->exp);
1429 strftime(ebuf, sizeof(ebuf), o->tfmt, tm);
1432 if (KEY_EXPIRED(o->t, k->del))
1433 strcpy(dbuf, "deleted");
1434 else if (k->del == KEXP_FOREVER)
1435 strcpy(dbuf, "forever");
1437 tm = (o->f & f_utc) ? gmtime(&k->del) : localtime(&k->del);
1438 strftime(dbuf, sizeof(dbuf), o->tfmt, tm);
1441 /* --- If in compact format, just display and quit --- */
1444 if (!(o->f & f_newline)) {
1445 printf("%8s %-20s %-20s %-10s %s\n",
1446 "Id", "Tag", "Type", "Expire", "Delete");
1448 printf("%08lx %-20s %-20s %-10s %s\n",
1449 (unsigned long)k->id, k->tag ? k->tag : "<none>",
1450 k->type, ebuf, dbuf);
1455 /* --- Display the standard header --- */
1457 if (o->f & f_newline)
1458 fputc('\n', stdout);
1459 printf("keyid: %08lx\n", (unsigned long)k->id);
1460 printf("tag: %s\n", k->tag ? k->tag : "<none>");
1461 printf("type: %s\n", k->type);
1462 printf("expiry: %s\n", ebuf);
1463 printf("delete: %s\n", dbuf);
1464 printf("comment: %s\n", k->c ? k->c : "<none>");
1466 /* --- Display the attributes --- */
1470 const char *av, *an;
1473 printf("attributes:");
1474 for (key_mkattriter(&i, k); key_nextattr(&i, &an, &av); ) {
1475 printf("\n %s = %s", an, av);
1479 fputc('\n', stdout);
1484 /* --- If dumping requested, dump the raw key data --- */
1488 fputs("key:", stdout);
1490 showkeydata(k->k, 0, o, &d);
1497 /* --- @cmd_list@ --- */
1499 static int cmd_list(int argc, char *argv[])
1503 listopts o = { 0, 0, 0, 0, { 0, 0 } };
1505 /* --- Parse subcommand options --- */
1508 static struct option opt[] = {
1509 { "quiet", 0, 0, 'q' },
1510 { "verbose", 0, 0, 'v' },
1511 { "utc", 0, 0, 'u' },
1512 { "filter", OPTF_ARGREQ, 0, 'f' },
1515 int i = mdwopt(argc, argv, "+uqvf:", opt, 0, 0, 0);
1532 int e = key_readflags(optarg, &p, &o.kf.f, &o.kf.m);
1534 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1543 die(EXIT_FAILURE, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1545 /* --- Open the key file --- */
1547 doopen(&f, KOPEN_READ);
1550 /* --- Set up the time format --- */
1553 o.tfmt = "%Y-%m-%d";
1554 else if (o.f & f_utc)
1555 o.tfmt = "%Y-%m-%d %H:%M:%S UTC";
1557 o.tfmt = "%Y-%m-%d %H:%M:%S %Z";
1559 /* --- If specific keys were requested use them, otherwise do all --- *
1561 * Some day, this might turn into a wildcard match.
1564 if (optind < argc) {
1566 if ((k = key_bytag(&f, argv[optind])) != 0)
1569 moan("key `%s' not found", argv[optind]);
1573 } while (optind < argc);
1576 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1584 return (EXIT_FAILURE);
1589 /*----- Command implementation --------------------------------------------*/
1591 /* --- @cmd_expire@ --- */
1593 static int cmd_expire(int argc, char *argv[])
1601 die(EXIT_FAILURE, "Usage: expire TAG...");
1602 doopen(&f, KOPEN_WRITE);
1603 for (i = 1; i < argc; i++) {
1604 if ((k = key_bytag(&f, argv[i])) != 0)
1607 moan("key `%s' not found", argv[i]);
1615 /* --- @cmd_delete@ --- */
1617 static int cmd_delete(int argc, char *argv[])
1625 die(EXIT_FAILURE, "Usage: delete TAG...");
1626 doopen(&f, KOPEN_WRITE);
1627 for (i = 1; i < argc; i++) {
1628 if ((k = key_bytag(&f, argv[i])) != 0)
1631 moan("key `%s' not found", argv[i]);
1639 /* --- @cmd_setattr@ --- */
1641 static int cmd_setattr(int argc, char *argv[])
1647 die(EXIT_FAILURE, "Usage: setattr TAG ATTR...");
1648 doopen(&f, KOPEN_WRITE);
1649 if ((k = key_bytag(&f, argv[1])) == 0)
1650 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1651 setattr(&f, k, argv + 2);
1656 /* --- @cmd_getattr@ --- */
1658 static int cmd_getattr(int argc, char *argv[])
1666 die(EXIT_FAILURE, "Usage: getattr TAG ATTR");
1667 doopen(&f, KOPEN_READ);
1668 if ((k = key_bytag(&f, argv[1])) == 0)
1669 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1671 if ((p = key_getattr(&f, k, argv[2])) == 0)
1672 die(EXIT_FAILURE, "no attribute `%s' for key `%s'", argv[2], d.buf);
1679 /* --- @cmd_finger@ --- */
1681 static void fingerprint(key *k, const gchash *ch, const key_filter *kf)
1689 if (key_fingerprint(k, h, kf)) {
1692 for (i = 0; i < ch->hashsz; i++) {
1693 if (i && i % 4 == 0)
1695 printf("%02x", p[i]);
1697 printf(" %s\n", d.buf);
1703 static int cmd_finger(int argc, char *argv[])
1707 const gchash *ch = &rmd160;
1708 key_filter kf = { KF_NONSECRET, KF_NONSECRET };
1711 static struct option opt[] = {
1712 { "filter", OPTF_ARGREQ, 0, 'f' },
1713 { "algorithm", OPTF_ARGREQ, 0, 'a' },
1716 int i = mdwopt(argc, argv, "+f:a:", opt, 0, 0, 0);
1722 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1724 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1727 if ((ch = ghash_byname(optarg)) == 0)
1728 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
1736 argv += optind; argc -= optind;
1738 die(EXIT_FAILURE, "Usage: fingerprint [-f FILTER] [TAG...]");
1740 doopen(&f, KOPEN_READ);
1744 for (i = 0; i < argc; i++) {
1745 key *k = key_bytag(&f, argv[i]);
1747 fingerprint(k, ch, &kf);
1750 moan("key `%s' not found", argv[i]);
1756 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
1757 fingerprint(k, ch, &kf);
1762 /* --- @cmd_verify@ --- */
1764 static unsigned xdigit(char c)
1766 if ('A' <= c && c <= 'Z') return (c + 10 - 'A');
1767 if ('a' <= c && c <= 'z') return (c + 10 - 'a');
1768 if ('0' <= c && c <= '9') return (c - '0');
1772 static void unhexify(octet *q, char *p, size_t n)
1778 if (*p == '-' || *p == ':' || isspace((unsigned char)*p)) {
1785 die(EXIT_FAILURE, "hex string too short");
1786 if (!isxdigit((unsigned char)*p))
1787 die(EXIT_FAILURE, "bad hex string");
1789 die(EXIT_FAILURE, "hex string too long");
1790 a = (a << 4) | xdigit(*p++);
1801 static int cmd_verify(int argc, char *argv[])
1805 const gchash *ch = &rmd160;
1810 key_filter kf = { KF_NONSECRET, KF_NONSECRET };
1813 static struct option opt[] = {
1814 { "filter", OPTF_ARGREQ, 0, 'f' },
1815 { "algorithm", OPTF_ARGREQ, 0, 'a' },
1818 int i = mdwopt(argc, argv, "+f:a:", opt, 0, 0, 0);
1824 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1826 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
1829 if ((ch = ghash_byname(optarg)) == 0)
1830 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
1838 argv += optind; argc -= optind;
1839 if (rc || argc != 2)
1840 die(EXIT_FAILURE, "Usage: verify [-f FILTER] TAG FINGERPRINT");
1842 doopen(&f, KOPEN_READ);
1844 if ((k = key_bytag(&f, argv[0])) == 0)
1845 die(EXIT_FAILURE, "key `%s' not found", argv[0]);
1846 buf = xmalloc(ch->hashsz);
1847 unhexify(buf, argv[1], ch->hashsz);
1849 if (!key_fingerprint(k, h, &kf))
1850 die(EXIT_FAILURE, "key has no fingerprintable components (as filtered)");
1851 fpr = GH_DONE(h, 0);
1852 if (memcmp(fpr, buf, ch->hashsz) != 0)
1853 die(EXIT_FAILURE, "key fingerprint mismatch");
1858 /* --- @cmd_comment@ --- */
1860 static int cmd_comment(int argc, char *argv[])
1866 if (argc < 2 || argc > 3)
1867 die(EXIT_FAILURE, "Usage: comment TAG [COMMENT]");
1868 doopen(&f, KOPEN_WRITE);
1869 if ((k = key_bytag(&f, argv[1])) == 0)
1870 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1871 if ((err = key_setcomment(&f, k, argv[2])) != 0)
1872 die(EXIT_FAILURE, "bad comment `%s': %s", argv[2], key_strerror(err));
1877 /* --- @cmd_tag@ --- */
1879 static int cmd_tag(int argc, char *argv[])
1888 static struct option opt[] = {
1889 { "retag", 0, 0, 'r' },
1892 int i = mdwopt(argc, argv, "+r", opt, 0, 0, 0);
1905 argv += optind; argc -= optind;
1906 if (argc < 1 || argc > 2 || rc)
1907 die(EXIT_FAILURE, "Usage: tag [-r] TAG [NEW-TAG]");
1908 doopen(&f, KOPEN_WRITE);
1909 if (flags & f_retag) {
1910 if ((k = key_bytag(&f, argv[1])) != 0 && strcmp(k->tag, argv[1]) == 0)
1911 key_settag(&f, k, 0);
1913 if ((k = key_bytag(&f, argv[0])) == 0)
1914 die(EXIT_FAILURE, "key `%s' not found", argv[0]);
1915 if ((err = key_settag(&f, k, argv[1])) != 0)
1916 die(EXIT_FAILURE, "bad tag `%s': %s", argv[1], key_strerror(err));
1921 /* --- @cmd_lock@ --- */
1923 static int cmd_lock(int argc, char *argv[])
1931 die(EXIT_FAILURE, "Usage: lock QTAG");
1932 doopen(&f, KOPEN_WRITE);
1933 if (key_qtag(&f, argv[1], &d, &k, &kd))
1934 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1935 if ((*kd)->e == KENC_ENCRYPT && key_punlock(kd, 0, d.buf))
1936 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1937 if (key_plock(kd, 0, d.buf))
1938 die(EXIT_FAILURE, "failed to lock key `%s'", d.buf);
1944 /* --- @cmd_unlock@ --- */
1946 static int cmd_unlock(int argc, char *argv[])
1954 die(EXIT_FAILURE, "Usage: unlock QTAG");
1955 doopen(&f, KOPEN_WRITE);
1956 if (key_qtag(&f, argv[1], &d, &k, &kd))
1957 die(EXIT_FAILURE, "key `%s' not found", argv[1]);
1958 if ((*kd)->e != KENC_ENCRYPT)
1959 die(EXIT_FAILURE, "key `%s' is not encrypted", d.buf);
1960 if (key_punlock(kd, 0, d.buf))
1961 die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf);
1967 /* --- @cmd_extract@ --- */
1969 static int cmd_extract(int argc, char *argv[])
1975 key_filter kf = { 0, 0 };
1977 const char *outfile = 0;
1981 static struct option opt[] = {
1982 { "filter", OPTF_ARGREQ, 0, 'f' },
1985 int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0);
1991 int err = key_readflags(optarg, &p, &kf.f, &kf.m);
1993 die(EXIT_FAILURE, "bad filter string `%s'", optarg);
2001 argv += optind; argc -= optind;
2003 die(EXIT_FAILURE, "Usage: extract [-f FILTER] FILE [TAG...]");
2004 if (strcmp(*argv, "-") == 0)
2008 dstr_putf(&d, "%s.new", outfile);
2009 if (!(fp = fopen(d.buf, "w"))) {
2010 die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
2011 d.buf, strerror(errno));
2015 doopen(&f, KOPEN_READ);
2019 for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; )
2020 key_extract(&f, k, fp, &kf);
2022 for (i = 1; i < argc; i++) {
2023 if ((k = key_bytag(&f, argv[i])) != 0)
2024 key_extract(&f, k, fp, &kf);
2026 moan("key `%s' not found", argv[i]);
2031 if (fclose(fp) || (outfile && rename(d.buf, outfile)))
2032 die(EXIT_FAILURE, "error writing file: %s", strerror(errno));
2038 /* --- @cmd_tidy@ --- */
2040 static int cmd_tidy(int argc, char *argv[])
2044 die(EXIT_FAILURE, "Usage: tidy");
2045 doopen(&f, KOPEN_WRITE);
2046 f.f |= KF_MODIFIED; /* Nasty hack */
2051 /* --- @cmd_merge@ --- */
2053 static int cmd_merge(int argc, char *argv[])
2059 die(EXIT_FAILURE, "Usage: merge FILE");
2060 if (strcmp(argv[1], "-") == 0)
2062 else if (!(fp = fopen(argv[1], "r"))) {
2063 die(EXIT_FAILURE, "couldn't open `%s' for reading: %s",
2064 argv[1], strerror(errno));
2067 doopen(&f, KOPEN_WRITE);
2068 key_merge(&f, argv[1], fp, key_moan, 0);
2073 /* --- @cmd_show@ --- */
2077 listtab[i].name, listtab[i].name) \
2078 LI("Hash functions", hash, \
2079 ghashtab[i], ghashtab[i]->name) \
2080 LI("Elliptic curves", ec, \
2081 ectab[i].name, ectab[i].name) \
2082 LI("Prime Diffie-Hellman groups", dh, \
2083 ptab[i].name, ptab[i].name) \
2084 LI("Binary Diffie-Hellman groups", bindh, \
2085 bintab[i].name, bintab[i].name) \
2086 LI("Key-generation algorithms", keygen, \
2087 algtab[i].name, algtab[i].name) \
2088 LI("Random seeding algorithms", seed, \
2089 seedtab[i].p, seedtab[i].p)
2091 MAKELISTTAB(listtab, LISTS)
2093 static int cmd_show(int argc, char *argv[])
2095 return (displaylists(listtab, argv + 1));
2098 /*----- Main command table ------------------------------------------------*/
2100 static int cmd_help(int argc, char *argv[]);
2102 static cmd cmds[] = {
2103 { "help", cmd_help, "help [COMMAND...]" },
2104 { "show", cmd_show, "show [ITEM...]" },
2105 { "list", cmd_list, "list [-uqv] [-f FILTER] [TAG...]", "\
2108 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2109 -q, --quiet Show less information.\n\
2110 -v, --verbose Show more information.\n\
2112 { "fingerprint", cmd_finger, "fingerprint [-f FILTER] [TAG...]", "\
2115 -f, --filter=FILT Only hash key components matching FILT.\n\
2116 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2117 ($ show hash for list.)\n\
2119 { "verify", cmd_verify, "verify [-f FILTER] TAG FINGERPRINT", "\
2122 -f, --filter=FILT Only hash key components matching FILT.\n\
2123 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2124 ($ show hash for list.)\n\
2126 { "extract", cmd_extract, "extract [-f FILTER] FILE [TAG...]", "\
2129 -f, --filter=FILT Only extract key components matching FILT.\n\
2131 { "merge", cmd_merge, "merge FILE" },
2132 { "expire", cmd_expire, "expire TAG..." },
2133 { "delete", cmd_delete, "delete TAG..." },
2134 { "setattr", cmd_setattr, "setattr TAG ATTR..." },
2135 { "getattr", cmd_getattr, "getattr TAG ATTR" },
2136 { "comment", cmd_comment, "comment TAG [COMMENT]" },
2137 { "lock", cmd_lock, "lock QTAG" },
2138 { "unlock", cmd_unlock, "unlock QTAG" },
2139 { "tag", cmd_tag, "tag [-r] TAG [NEW-TAG]", "\
2142 -r, --retag Untag any key currently called new-tag.\n\
2144 { "tidy", cmd_tidy, "tidy" },
2146 "add [-OPTIONS] TYPE [ATTR...]\n\
2147 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2148 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2149 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2152 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2153 ($ show keygen for list.)\n\
2154 -b, --bits=N Generate an N-bit key.\n\
2155 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2156 -p, --parameters=TAG Get group parameters from TAG.\n\
2157 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2158 ($ show ec or $ show dh for list.)\n\
2159 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2160 ($ show seed for list.)\n\
2161 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2162 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2163 -e, --expire=TIME Make the key expire after TIME.\n\
2164 -c, --comment=STRING Attach the command STRING to the key.\n\
2165 -t, --tag=TAG Tag the key with the name TAG.\n\
2166 -r, --retag Untag any key currently with that tag.\n\
2167 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2168 -I, --key-id=ID Force the key-id for the new key.\n\
2169 -l, --lock Lock the generated key with a passphrase.\n\
2170 -q, --quiet Don't give progress indicators while working.\n\
2171 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2172 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2173 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2178 static int cmd_help(int argc, char *argv[])
2180 sc_help(cmds, stdout, argv + 1);
2184 /*----- Main code ---------------------------------------------------------*/
2186 /* --- Helpful GNUy functions --- */
2188 static void usage(FILE *fp)
2190 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2193 void version(FILE *fp)
2195 pquis(fp, "$, Catacomb version " VERSION "\n");
2198 void help_global(FILE *fp)
2202 Performs various simple key management operations.\n\
2204 Global command line options:\n\
2206 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2207 -v, --version Display version number.\n\
2208 -u, --usage Display short usage summary.\n\
2210 -k, --keyring=FILE Read and write keys in FILE.\n",
2216 * Arguments: @int argc@ = number of command line arguments
2217 * @char *argv[]@ = array of command line arguments
2219 * Returns: Nonzero on failure.
2221 * Use: Main program. Performs simple key management functions.
2224 int main(int argc, char *argv[])
2230 /* --- Initialization --- */
2235 /* --- Parse command line options --- */
2238 static struct option opt[] = {
2240 /* --- Standard GNUy help options --- */
2242 { "help", 0, 0, 'h' },
2243 { "version", 0, 0, 'v' },
2244 { "usage", 0, 0, 'u' },
2246 /* --- Real live useful options --- */
2248 { "keyring", OPTF_ARGREQ, 0, 'k' },
2250 /* --- Magic terminator --- */
2254 int i = mdwopt(argc, argv, "+hvu k:", opt, 0, 0, 0);
2260 /* --- GNU help options --- */
2263 sc_help(cmds, stdout, argv + optind);
2272 /* --- Real useful options --- */
2278 /* --- Bogosity --- */
2286 /* --- Complain about excessive bogons --- */
2288 if (f & f_bogus || optind == argc) {
2293 /* --- Initialize the Catacomb random number generator --- */
2295 rand_noisesrc(RAND_GLOBAL, &noise_source);
2296 rand_seed(RAND_GLOBAL, 160);
2298 /* --- Dispatch to appropriate command handler --- */
2303 return (findcmd(cmds, argv[0])->cmd(argc, argv));
2306 /*----- That's all, folks -------------------------------------------------*/