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 ------------------------------------------------------*/
40 #include <mLib/base64.h>
41 #include <mLib/mdwopt.h>
42 #include <mLib/quis.h>
43 #include <mLib/report.h>
71 #include "sha256-mgf.h"
72 #include "sha224-mgf.h"
73 #include "sha384-mgf.h"
74 #include "sha512-mgf.h"
75 #include "tiger-mgf.h"
76 #include "rmd128-mgf.h"
77 #include "rmd160-mgf.h"
78 #include "rmd256-mgf.h"
79 #include "rmd320-mgf.h"
83 /*----- Handy global state ------------------------------------------------*/
85 static const char *keyfile
= "keyring";
87 /*----- Useful shared functions -------------------------------------------*/
91 * Arguments: @key_file *f@ = pointer to key file block
92 * @unsigned how@ = method to open file with
96 * Use: Opens a key file and handles errors by panicking
100 static void doopen(key_file
*f
, unsigned how
)
102 if (key_open(f
, keyfile
, how
, key_moan
, 0))
103 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
106 /* --- @doclose@ --- *
108 * Arguments: @key_file *f@ = pointer to key file block
112 * Use: Closes a key file and handles errors by panicking
116 static void doclose(key_file
*f
)
118 switch (key_close(f
)) {
120 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
121 keyfile
, strerror(errno
));
123 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
124 keyfile
, strerror(errno
));
128 /* --- @setattr@ --- *
130 * Arguments: @key_file *f@ = pointer to key file block
131 * @key *k@ = pointer to key block
132 * @char *v[]@ = array of assignments (overwritten!)
136 * Use: Applies the attribute assignments to the key.
139 static void setattr(key_file
*f
, key
*k
, char *v
[])
144 size_t eq
= strcspn(p
, "=");
146 moan("invalid assignment: `%s' (ignored)", p
);
152 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
153 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
158 /*----- Seeding -----------------------------------------------------------*/
160 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
162 { "dsarand", dsarand_create
},
163 { "rmd128-mgf", rmd128_mgfrand
},
164 { "rmd160-mgf", rmd160_mgfrand
},
165 { "rmd256-mgf", rmd256_mgfrand
},
166 { "rmd320-mgf", rmd320_mgfrand
},
167 { "sha-mgf", sha_mgfrand
},
168 { "sha224-mgf", sha224_mgfrand
},
169 { "sha256-mgf", sha256_mgfrand
},
170 { "sha384-mgf", sha384_mgfrand
},
171 { "sha512-mgf", sha512_mgfrand
},
172 { "tiger-mgf", tiger_mgfrand
},
176 #define SEEDALG_DEFAULT (seedtab + 2)
178 /*----- Key generation ----------------------------------------------------*/
180 /* --- Key generation parameters --- */
182 typedef struct keyopts
{
183 key_file
*kf
; /* Pointer to key file */
184 key
*k
; /* Pointer to the actual key */
185 dstr tag
; /* Full tag name for the key */
186 unsigned f
; /* Flags for the new key */
187 unsigned bits
, qbits
; /* Bit length for the new key */
188 const char *curve
; /* Elliptic curve name/info */
189 grand
*r
; /* Random number source */
190 key
*p
; /* Parameters key-data */
193 #define f_bogus 1u /* Error in parsing */
194 #define f_lock 2u /* Passphrase-lock private key */
195 #define f_quiet 4u /* Don't show a progress indicator */
196 #define f_limlee 8u /* Generate Lim-Lee primes */
197 #define f_subgroup 16u /* Generate a subgroup */
198 #define f_retag 32u /* Remove any existing tag */
200 /* --- @dolock@ --- *
202 * Arguments: @keyopts *k@ = key generation options
203 * @key_data *kd@ = pointer to key data to lock
204 * @const char *t@ = tag suffix or null
208 * Use: Does passphrase locking on new keys.
211 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
213 if (!(k
->f
& f_lock
))
216 dstr_putf(&k
->tag
, ".%s", t
);
217 if (key_plock(k
->tag
.buf
, kd
, kd
))
218 die(EXIT_FAILURE
, "couldn't lock key");
223 * Arguments: @key_data *kd@ = pointer to parent key block
224 * @const char *tag@ = pointer to tag string
225 * @mp *m@ = integer to store
226 * @unsigned f@ = flags to set
230 * Use: Sets a multiprecision integer subkey.
233 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
235 key_data
*kkd
= key_structcreate(kd
, tag
);
240 /* --- @copyparam@ --- *
242 * Arguments: @keyopts *k@ = pointer to key options
243 * @const char **pp@ = checklist of parameters
245 * Returns: Nonzero if parameters copied; zero if you have to generate
248 * Use: Copies parameters from a source key to the current one.
251 static int copyparam(keyopts
*k
, const char **pp
)
257 /* --- Quick check if no parameters supplied --- */
262 /* --- Run through the checklist --- */
265 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
267 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
268 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
269 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
273 /* --- Copy over the parameters --- */
277 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
278 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
280 /* --- Copy over attributes --- */
282 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
283 key_putattr(k
->kf
, k
->k
, n
, v
);
292 * Arguments: @key_data *k@ = pointer to key data block
293 * @const char *tag@ = tag string to use
295 * Returns: Pointer to multiprecision integer key item.
297 * Use: Fetches an MP key component.
300 static mp
*getmp(key_data
*k
, const char *tag
)
302 k
= key_structfind(k
, tag
);
304 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
305 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
306 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
310 /* --- @keyrand@ --- *
312 * Arguments: @key_file *kf@ = pointer to key file
313 * @const char *id@ = pointer to key id (or null)
317 * Use: Keys the random number generator.
320 static void keyrand(key_file
*kf
, const char *id
)
324 /* --- Find the key --- */
327 if ((k
= key_bytag(kf
, id
)) == 0)
328 die(EXIT_FAILURE
, "key `%s' not found", id
);
330 k
= key_bytype(kf
, "catacomb-rand");
333 key_data
*kd
= &k
->k
, kkd
;
336 switch (kd
->e
& KF_ENCMASK
) {
342 if (key_punlock(d
.buf
, kd
, &kkd
))
343 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
350 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
354 /* --- Key the generator --- */
356 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
362 /* --- Key generation algorithms --- */
364 static void alg_binary(keyopts
*k
)
373 die(EXIT_FAILURE
, "no shared parameters for binary keys");
375 sz
= (k
->bits
+ 7) >> 3;
377 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
378 k
->r
->ops
->fill(k
->r
, p
, sz
);
380 key_binary(&k
->k
->k
, p
, sz
);
381 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
384 dolock(k
, &k
->k
->k
, 0);
387 static void alg_des(keyopts
*k
)
396 die(EXIT_FAILURE
, "no shared parameters for DES keys");
397 if (k
->bits
% 56 || k
->bits
> 168)
398 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
402 k
->r
->ops
->fill(k
->r
, p
, sz
);
403 for (i
= 0; i
< sz
; i
++) {
404 octet x
= p
[i
] | 0x01;
408 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
410 key_binary(&k
->k
->k
, p
, sz
);
411 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
414 dolock(k
, &k
->k
->k
, 0);
417 static void alg_rsa(keyopts
*k
)
422 /* --- Sanity checking --- */
425 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
429 /* --- Generate the RSA parameters --- */
431 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
432 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
433 die(EXIT_FAILURE
, "RSA key generation failed");
435 /* --- Run a test encryption --- */
438 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
440 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
445 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
446 c
= rsa_qprivop(&rp
, c
, c
, g
);
449 die(EXIT_FAILURE
, "test encryption failed");
455 /* --- Allrighty then --- */
459 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
460 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
462 kd
= key_structcreate(kd
, "private");
464 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
465 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
466 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
467 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
468 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
469 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
470 dolock(k
, kd
, "private");
475 static void alg_dsaparam(keyopts
*k
)
477 static const char *pl
[] = { "q", "p", "g", 0 };
478 if (!copyparam(k
, pl
)) {
484 key_data
*kd
= &k
->k
->k
;
487 /* --- Choose appropriate bit lengths if necessary --- */
494 /* --- Allocate a seed block --- */
496 sz
= (k
->qbits
+ 7) >> 3;
498 k
->r
->ops
->fill(k
->r
, p
, sz
);
500 /* --- Allocate the parameters --- */
502 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
503 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
504 die(EXIT_FAILURE
, "DSA parameter generation failed");
506 /* --- Store the parameters --- */
509 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
510 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
511 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
516 /* --- Store the seed for future verification --- */
521 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
522 base64_encode(&c
, 0, 0, &d
);
523 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
525 dstr_putf(&d
, "%u", ds
.count
);
526 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
533 static void alg_dsa(keyopts
*k
)
538 key_data
*kd
= &k
->k
->k
;
540 /* --- Get the shared parameters --- */
547 /* --- Choose a private key --- */
549 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
550 mpmont_create(&mm
, p
);
551 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
553 /* --- Store everything away --- */
555 mpkey(kd
, "y", y
, KCAT_PUB
);
557 kd
= key_structcreate(kd
, "private");
559 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
560 dolock(k
, kd
, "private");
562 mp_drop(x
); mp_drop(y
);
565 static void alg_dhparam(keyopts
*k
)
567 static const char *pl
[] = { "p", "q", "g", 0 };
568 key_data
*kd
= &k
->k
->k
;
569 if (!copyparam(k
, pl
)) {
578 if (strcmp(k
->curve
, "list") == 0) {
580 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
584 if (dh_parse(&qd
, &dp
))
585 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
587 die(EXIT_FAILURE
, "junk at end of field spec");
588 if ((g
= group_prime(&dp
)) == 0)
589 die(EXIT_FAILURE
, "invalid prime field");
590 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
591 moan("WARNING! group check failed: %s", e
);
599 /* --- Choose a large safe prime number --- */
601 if (k
->f
& f_limlee
) {
606 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
607 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
608 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
609 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
613 for (i
= 0; i
< nf
; i
++) {
616 mp_writedstr(f
[i
], &d
, 10);
619 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
623 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
624 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
627 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
631 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
632 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
633 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
640 static void alg_dh(keyopts
*k
)
645 key_data
*kd
= &k
->k
->k
;
647 /* --- Get the shared parameters --- */
654 /* --- Choose a suitable private key --- *
656 * Since %$g$% has order %$q$%, choose %$x < q$%.
659 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
661 /* --- Compute the public key %$y = g^x \bmod p$% --- */
663 mpmont_create(&mm
, p
);
664 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
667 /* --- Store everything away --- */
669 mpkey(kd
, "y", y
, KCAT_PUB
);
671 kd
= key_structcreate(kd
, "private");
673 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
674 dolock(k
, kd
, "private");
676 mp_drop(x
); mp_drop(y
);
679 static void alg_bbs(keyopts
*k
)
684 /* --- Sanity checking --- */
687 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
691 /* --- Generate the BBS parameters --- */
693 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
694 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
695 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
697 /* --- Allrighty then --- */
701 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
703 kd
= key_structcreate(kd
, "private");
705 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
706 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
707 dolock(k
, kd
, "private");
712 static void alg_binparam(keyopts
*k
)
714 static const char *pl
[] = { "p", "q", "g", 0 };
715 if (!copyparam(k
, pl
)) {
720 key_data
*kd
= &k
->k
->k
;
722 /* --- Decide on a field --- */
724 if (!k
->bits
) k
->bits
= 128;
725 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
727 LIST("Built-in binary fields", stdout
,
728 bintab
[i
].name
, bintab
[i
].name
);
732 if (k
->bits
<= 40) k
->curve
= "p1363-40";
733 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
734 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
735 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
736 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
737 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
740 "no built-in binary fields provide %u-bit security",
745 /* --- Check it --- */
749 if (dhbin_parse(&qd
, &gb
))
750 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
752 die(EXIT_FAILURE
, "junk at end of field spec");
753 if ((g
= group_binary(&gb
)) == 0)
754 die(EXIT_FAILURE
, "invalid binary field");
755 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
756 moan("WARNING! group check failed: %s", e
);
759 /* --- Write out the answer --- */
762 mpkey(kd
, "p", gb
.p
, KCAT_SHARE
);
763 mpkey(kd
, "q", gb
.q
, KCAT_SHARE
);
764 mpkey(kd
, "g", gb
.g
, KCAT_SHARE
);
771 static void alg_bin(keyopts
*k
)
776 key_data
*kd
= &k
->k
->k
;
778 /* --- Get the shared parameters --- */
785 /* --- Choose a suitable private key --- *
787 * Since %$g$% has order %$q$%, choose %$x < q$%.
790 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
792 /* --- Compute the public key %$y = g^x \bmod p$% --- */
794 gfreduce_create(&r
, p
);
795 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
796 gfreduce_destroy(&r
);
798 /* --- Store everything away --- */
800 mpkey(kd
, "y", y
, KCAT_PUB
);
802 kd
= key_structcreate(kd
, "private");
804 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
805 dolock(k
, kd
, "private");
807 mp_drop(x
); mp_drop(y
);
810 static void alg_ecparam(keyopts
*k
)
812 static const char *pl
[] = { "curve", 0 };
813 if (!copyparam(k
, pl
)) {
816 key_data
*kd
= &k
->k
->k
;
818 /* --- Decide on a curve --- */
820 if (!k
->bits
) k
->bits
= 256;
821 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
823 LIST("Built-in elliptic curves", stdout
,
824 ectab
[i
].name
, ectab
[i
].name
);
828 if (k
->bits
<= 56) k
->curve
= "secp112r1";
829 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
830 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
831 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
832 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
833 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
834 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
835 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
837 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
841 /* --- Check it --- */
843 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
844 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
845 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
846 moan("WARNING! curve check failed: %s", e
);
849 /* --- Write out the answer --- */
852 kd
= key_structcreate(kd
, "curve");
853 key_string(kd
, k
->curve
);
858 static void alg_ec(keyopts
*k
)
860 key_data
*kd
= &k
->k
->k
;
867 /* --- Get the curve --- */
870 if ((kkd
= key_structfind(kd
, "curve")) == 0)
871 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
872 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
873 die(EXIT_FAILURE
, "subkey `curve' is not a string");
874 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
875 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
877 /* --- Invent a private exponent and compute the public key --- */
879 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
880 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
882 /* --- Store everything away --- */
884 kkd
= key_structcreate(kd
, "p");
887 kkd
= key_structcreate(kd
, "private");
889 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
890 dolock(k
, kkd
, "private");
898 /* --- The algorithm tables --- */
900 typedef struct keyalg
{
902 void (*proc
)(keyopts
*o
);
906 static keyalg algtab
[] = {
907 { "binary", alg_binary
, "Plain binary data" },
908 { "des", alg_des
, "Binary with DES-style parity" },
909 { "rsa", alg_rsa
, "RSA public-key encryption" },
910 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
911 { "dsa", alg_dsa
, "DSA digital signatures" },
912 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
913 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
914 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
915 { "bindh", alg_bin
, "DH over a binary field" },
916 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
917 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
918 { "ec", alg_ec
, "Elliptic curve crypto" },
922 /* --- @cmd_add@ --- */
924 static int cmd_add(int argc
, char *argv
[])
927 time_t exp
= KEXP_EXPIRE
;
928 const char *tag
= 0, *ptag
= 0;
930 keyalg
*alg
= algtab
;
931 const char *rtag
= 0;
932 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
933 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
934 const char *seed
= 0;
937 /* --- Parse options for the subcommand --- */
940 static struct option opt
[] = {
941 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
942 { "bits", OPTF_ARGREQ
, 0, 'b' },
943 { "qbits", OPTF_ARGREQ
, 0, 'B' },
944 { "parameters", OPTF_ARGREQ
, 0, 'p' },
945 { "expire", OPTF_ARGREQ
, 0, 'e' },
946 { "comment", OPTF_ARGREQ
, 0, 'c' },
947 { "tag", OPTF_ARGREQ
, 0, 't' },
948 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
949 { "curve", OPTF_ARGREQ
, 0, 'C' },
950 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
951 { "seed", OPTF_ARGREQ
, 0, 's' },
952 { "newseed", OPTF_ARGREQ
, 0, 'n' },
953 { "lock", 0, 0, 'l' },
954 { "quiet", 0, 0, 'q' },
955 { "lim-lee", 0, 0, 'L' },
956 { "subgroup", 0, 0, 'S' },
959 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
964 /* --- Handle the various options --- */
968 /* --- Read an algorithm name --- */
972 size_t sz
= strlen(optarg
);
974 if (strcmp(optarg
, "list") == 0) {
975 for (a
= algtab
; a
->name
; a
++)
976 printf("%-10s %s\n", a
->name
, a
->help
);
981 for (a
= algtab
; a
->name
; a
++) {
982 if (strncmp(optarg
, a
->name
, sz
) == 0) {
983 if (a
->name
[sz
] == 0) {
987 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
993 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
996 /* --- Bits must be nonzero and a multiple of 8 --- */
1000 k
.bits
= strtoul(optarg
, &p
, 0);
1001 if (k
.bits
== 0 || *p
!= 0)
1002 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1007 k
.qbits
= strtoul(optarg
, &p
, 0);
1008 if (k
.qbits
== 0 || *p
!= 0)
1009 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1012 /* --- Parameter selection --- */
1018 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1021 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
1024 exp
= get_date(optarg
, 0);
1026 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1030 /* --- Store comments without interpretation --- */
1033 if (key_chkcomment(optarg
))
1034 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1038 /* --- Elliptic curve parameters --- */
1044 /* --- Store tags --- */
1047 if (key_chkident(optarg
))
1048 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1055 /* --- Seeding --- */
1058 const struct seedalg
*ss
;
1059 if (strcmp(optarg
, "list") == 0) {
1060 printf("Seed algorithms:\n");
1061 for (ss
= seedtab
; ss
->p
; ss
++)
1062 printf(" %s\n", ss
->p
);
1065 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1067 for (ss
= seedtab
; ss
->p
; ss
++) {
1068 if (strcmp(optarg
, ss
->p
) == 0)
1072 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1078 if (seed
) die(EXIT_FAILURE
, "seed already set");
1080 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1081 base64_decode(&b
, 0, 0, &d
);
1082 k
.r
= sa
->gen(d
.buf
, d
.len
);
1091 unsigned n
= strtoul(optarg
, &p
, 0);
1092 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1093 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1094 if (seed
) die(EXIT_FAILURE
, "seed already set");
1097 rand_get(RAND_GLOBAL
, p
, n
);
1099 base64_encode(&b
, p
, n
, &d
);
1100 base64_encode(&b
, 0, 0, &d
);
1102 k
.r
= sa
->gen(p
, n
);
1105 /* --- Other flags --- */
1123 /* --- Other things are bogus --- */
1131 /* --- Various sorts of bogosity --- */
1133 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1135 "Usage: add [OPTIONS] TYPE [ATTR...]");
1137 if (key_chkident(argv
[optind
]))
1138 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1140 /* --- Set up various bits of the state --- */
1142 if (exp
== KEXP_EXPIRE
)
1143 exp
= time(0) + 14 * 24 * 60 * 60;
1145 /* --- Open the file and create the basic key block --- *
1147 * Keep on generating keyids until one of them doesn't collide.
1150 doopen(&f
, KOPEN_WRITE
);
1153 /* --- Key the generator --- */
1158 uint32 id
= rand_global
.ops
->word(&rand_global
);
1160 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1163 if (err
!= KERR_DUPID
)
1164 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1167 /* --- Set various simple attributes --- */
1172 if (k
.f
& f_retag
) {
1173 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1174 key_settag(&f
, kk
, 0);
1176 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1177 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1181 int err
= key_setcomment(&f
, k
.k
, c
);
1183 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1186 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1188 key_putattr(&f
, k
.k
, "genseed", seed
);
1189 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1192 key_fulltag(k
.k
, &k
.tag
);
1194 /* --- Find the parameter key --- */
1197 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1198 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1199 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1200 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1203 /* --- Now generate the actual key data --- */
1213 /*----- Key listing -------------------------------------------------------*/
1215 /* --- Listing options --- */
1217 typedef struct listopts
{
1218 const char *tfmt
; /* Date format (@strftime@-style) */
1219 int v
; /* Verbosity level */
1220 unsigned f
; /* Various flags */
1221 time_t t
; /* Time now (for key expiry) */
1222 key_filter kf
; /* Filter for matching keys */
1225 /* --- Listing flags --- */
1227 #define f_newline 2u /* Write newline before next entry */
1228 #define f_attr 4u /* Written at least one attribute */
1229 #define f_utc 8u /* Emit UTC time, not local time */
1231 /* --- @showkeydata@ --- *
1233 * Arguments: @key_data *k@ = pointer to key to write
1234 * @int ind@ = indentation level
1235 * @listopts *o@ = listing options
1236 * @dstr *d@ = tag string for this subkey
1240 * Use: Emits a piece of key data in a human-readable format.
1243 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1245 #define INDENT(i) do { \
1247 for (_i = 0; _i < (i); _i++) { \
1252 switch (k
->e
& KF_ENCMASK
) {
1254 /* --- Binary key data --- *
1256 * Emit as a simple hex dump.
1260 const octet
*p
= k
->u
.k
.k
;
1261 const octet
*l
= p
+ k
->u
.k
.sz
;
1264 fputs(" {", stdout
);
1269 } else if (sz
% 8 == 0)
1273 printf("%02x", *p
++);
1278 fputs("}\n", stdout
);
1281 /* --- Encrypted data --- *
1283 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1284 * key. Otherwise just say that it's encrypted and move on.
1289 fputs(" encrypted\n", stdout
);
1292 if (key_punlock(d
->buf
, k
, &kd
))
1293 printf(" <failed to unlock %s>\n", d
->buf
);
1295 fputs(" encrypted", stdout
);
1296 showkeydata(&kd
, ind
, o
, d
);
1302 /* --- Integer keys --- *
1304 * Emit as a large integer in decimal. This makes using the key in
1305 * `calc' or whatever easier.
1310 mp_writefile(k
->u
.m
, stdout
, 10);
1314 /* --- Strings --- */
1317 printf(" `%s'\n", k
->u
.p
);
1320 /* --- Elliptic curve points --- */
1323 if (EC_ATINF(&k
->u
.e
))
1324 fputs(" inf\n", stdout
);
1326 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1327 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1332 /* --- Structured keys --- *
1334 * Just iterate over the subkeys.
1342 fputs(" {\n", stdout
);
1343 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1344 if (!key_match(&ks
->k
, &o
->kf
))
1347 printf("%s =", SYM_NAME(ks
));
1349 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1350 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1353 fputs("}\n", stdout
);
1360 /* --- @showkey@ --- *
1362 * Arguments: @key *k@ = pointer to key to show
1363 * @listopts *o@ = pointer to listing options
1367 * Use: Emits a listing of a particular key.
1370 static void showkey(key
*k
, listopts
*o
)
1372 char ebuf
[24], dbuf
[24];
1375 /* --- Skip the key if the filter doesn't match --- */
1377 if (!key_match(&k
->k
, &o
->kf
))
1380 /* --- Sort out the expiry and deletion times --- */
1382 if (KEY_EXPIRED(o
->t
, k
->exp
))
1383 strcpy(ebuf
, "expired");
1384 else if (k
->exp
== KEXP_FOREVER
)
1385 strcpy(ebuf
, "forever");
1387 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1388 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1391 if (KEY_EXPIRED(o
->t
, k
->del
))
1392 strcpy(dbuf
, "deleted");
1393 else if (k
->del
== KEXP_FOREVER
)
1394 strcpy(dbuf
, "forever");
1396 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1397 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1400 /* --- If in compact format, just display and quit --- */
1403 if (!(o
->f
& f_newline
)) {
1404 printf("%8s %-20s %-20s %-10s %-10s\n",
1405 "Id", "Tag", "Type", "Expire", "Delete");
1407 printf("%08lx %-20s %-20s %-10s %-10s\n",
1408 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1409 k
->type
, ebuf
, dbuf
);
1414 /* --- Display the standard header --- */
1416 if (o
->f
& f_newline
)
1417 fputc('\n', stdout
);
1418 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1419 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1420 printf("type: %s\n", k
->type
);
1421 printf("expiry: %s\n", ebuf
);
1422 printf("delete: %s\n", dbuf
);
1423 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1425 /* --- Display the attributes --- */
1429 const char *av
, *an
;
1432 printf("attributes:");
1433 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1434 printf("\n %s = %s", an
, av
);
1438 fputc('\n', stdout
);
1443 /* --- If dumping requested, dump the raw key data --- */
1447 fputs("key:", stdout
);
1449 showkeydata(&k
->k
, 0, o
, &d
);
1456 /* --- @cmd_list@ --- */
1458 static int cmd_list(int argc
, char *argv
[])
1462 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1464 /* --- Parse subcommand options --- */
1467 static struct option opt
[] = {
1468 { "quiet", 0, 0, 'q' },
1469 { "verbose", 0, 0, 'v' },
1470 { "utc", 0, 0, 'u' },
1471 { "filter", OPTF_ARGREQ
, 0, 'f' },
1474 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1491 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1493 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1502 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1504 /* --- Open the key file --- */
1506 doopen(&f
, KOPEN_READ
);
1509 /* --- Set up the time format --- */
1512 o
.tfmt
= "%Y-%m-%d";
1513 else if (o
.f
& f_utc
)
1514 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1516 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1518 /* --- If specific keys were requested use them, otherwise do all --- *
1520 * Some day, this might turn into a wildcard match.
1523 if (optind
< argc
) {
1525 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1528 moan("key `%s' not found", argv
[optind
]);
1532 } while (optind
< argc
);
1535 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1543 return (EXIT_FAILURE
);
1548 /*----- Command implementation --------------------------------------------*/
1550 /* --- @cmd_expire@ --- */
1552 static int cmd_expire(int argc
, char *argv
[])
1560 die(EXIT_FAILURE
, "Usage: expire TAG...");
1561 doopen(&f
, KOPEN_WRITE
);
1562 for (i
= 1; i
< argc
; i
++) {
1563 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1566 moan("key `%s' not found", argv
[i
]);
1574 /* --- @cmd_delete@ --- */
1576 static int cmd_delete(int argc
, char *argv
[])
1584 die(EXIT_FAILURE
, "Usage: delete TAG...");
1585 doopen(&f
, KOPEN_WRITE
);
1586 for (i
= 1; i
< argc
; i
++) {
1587 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1590 moan("key `%s' not found", argv
[i
]);
1598 /* --- @cmd_setattr@ --- */
1600 static int cmd_setattr(int argc
, char *argv
[])
1606 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1607 doopen(&f
, KOPEN_WRITE
);
1608 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1609 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1610 setattr(&f
, k
, argv
+ 2);
1615 /* --- @cmd_finger@ --- */
1617 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1625 if (key_fingerprint(k
, h
, kf
)) {
1628 for (i
= 0; i
< ch
->hashsz
; i
++) {
1629 if (i
&& i
% 4 == 0)
1631 printf("%02x", p
[i
]);
1633 printf(" %s\n", d
.buf
);
1639 static int cmd_finger(int argc
, char *argv
[])
1643 const gchash
*ch
= &rmd160
;
1644 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1647 static struct option opt
[] = {
1648 { "filter", OPTF_ARGREQ
, 0, 'f' },
1649 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1652 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1658 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1660 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1663 if ((ch
= ghash_byname(optarg
)) == 0)
1664 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1672 argv
+= optind
; argc
-= optind
;
1674 die(EXIT_FAILURE
, "Usage: fingerprint [-f FILTER] [TAG...]");
1676 doopen(&f
, KOPEN_READ
);
1680 for (i
= 0; i
< argc
; i
++) {
1681 key
*k
= key_bytag(&f
, argv
[i
]);
1683 fingerprint(k
, ch
, &kf
);
1686 moan("key `%s' not found", argv
[i
]);
1692 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1693 fingerprint(k
, ch
, &kf
);
1698 /* --- @cmd_comment@ --- */
1700 static int cmd_comment(int argc
, char *argv
[])
1706 if (argc
< 2 || argc
> 3)
1707 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1708 doopen(&f
, KOPEN_WRITE
);
1709 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1710 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1711 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1712 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1717 /* --- @cmd_tag@ --- */
1719 static int cmd_tag(int argc
, char *argv
[])
1728 static struct option opt
[] = {
1729 { "retag", 0, 0, 'r' },
1732 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1745 argv
+= optind
; argc
-= optind
;
1746 if (argc
< 1 || argc
> 2 || rc
)
1747 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1748 doopen(&f
, KOPEN_WRITE
);
1749 if (flags
& f_retag
) {
1750 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1751 key_settag(&f
, k
, 0);
1753 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1754 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1755 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1756 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1761 /* --- @cmd_lock@ --- */
1763 static int cmd_lock(int argc
, char *argv
[])
1771 die(EXIT_FAILURE
, "Usage: lock QTAG");
1772 doopen(&f
, KOPEN_WRITE
);
1773 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1774 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1775 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1776 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1777 if (key_plock(d
.buf
, kd
, kd
))
1778 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1784 /* --- @cmd_unlock@ --- */
1786 static int cmd_unlock(int argc
, char *argv
[])
1794 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1795 doopen(&f
, KOPEN_WRITE
);
1796 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1797 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1798 if (kd
->e
!= KENC_ENCRYPT
)
1799 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1800 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1801 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1807 /* --- @cmd_extract@ --- */
1809 static int cmd_extract(int argc
, char *argv
[])
1815 key_filter kf
= { 0, 0 };
1819 static struct option opt
[] = {
1820 { "filter", OPTF_ARGREQ
, 0, 'f' },
1823 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1829 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1831 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1839 argv
+= optind
; argc
-= optind
;
1841 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
1842 if (strcmp(*argv
, "-") == 0)
1844 else if (!(fp
= fopen(*argv
, "w"))) {
1845 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1846 *argv
, strerror(errno
));
1849 doopen(&f
, KOPEN_READ
);
1853 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1854 key_extract(&f
, k
, fp
, &kf
);
1856 for (i
= 1; i
< argc
; i
++) {
1857 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1858 key_extract(&f
, k
, fp
, &kf
);
1860 moan("key `%s' not found", argv
[i
]);
1866 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1871 /* --- @cmd_tidy@ --- */
1873 static int cmd_tidy(int argc
, char *argv
[])
1877 die(EXIT_FAILURE
, "Usage: tidy");
1878 doopen(&f
, KOPEN_WRITE
);
1879 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1884 /* --- @cmd_merge@ --- */
1886 static int cmd_merge(int argc
, char *argv
[])
1892 die(EXIT_FAILURE
, "Usage: merge FILE");
1893 if (strcmp(argv
[1], "-") == 0)
1895 else if (!(fp
= fopen(argv
[1], "r"))) {
1896 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1897 argv
[1], strerror(errno
));
1900 doopen(&f
, KOPEN_WRITE
);
1901 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1906 /* --- @cmd_show@ --- */
1910 listtab[i].name, listtab[i].name) \
1911 LI("Hash functions", hash, \
1912 ghashtab[i], ghashtab[i]->name) \
1913 LI("Elliptic curves", ec, \
1914 ectab[i].name, ectab[i].name) \
1915 LI("Prime Diffie-Hellman groups", dh, \
1916 ptab[i].name, ptab[i].name) \
1917 LI("Binary Diffie-Hellman groups", bindh, \
1918 bintab[i].name, bintab[i].name) \
1919 LI("Key-generation algorithms", keygen, \
1920 algtab[i].name, algtab[i].name) \
1921 LI("Random seeding algorithms", seed, \
1922 seedtab[i].p, seedtab[i].p)
1924 MAKELISTTAB(listtab
, LISTS
)
1926 static int cmd_show(int argc
, char *argv
[])
1928 return (displaylists(listtab
, argv
+ 1));
1931 /*----- Main command table ------------------------------------------------*/
1933 static int cmd_help(int argc
, char *argv
[]);
1935 static cmd cmds
[] = {
1936 { "help", cmd_help
, "help [COMMAND...]" },
1937 { "show", cmd_show
, "show [ITEM...]" },
1938 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
1941 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1942 -q, --quiet Show less information.\n\
1943 -v, --verbose Show more information.\n\
1945 { "fingerprint", cmd_finger
, "fingerprint [-f FILTER] [TAG...]", "\
1948 -f, --filter=FILT Only hash key components matching FILT.\n\
1949 -a, --algorithm=HASH Use the named HASH algorithm.\n\
1950 ($ show hash for list.)\n\
1952 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
1955 -f, --filter=FILT Only extract key components matching FILT.\n\
1957 { "merge", cmd_merge
, "merge FILE" },
1958 { "expire", cmd_expire
, "expire TAG..." },
1959 { "delete", cmd_delete
, "delete TAG..." },
1960 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
1961 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
1962 { "lock", cmd_lock
, "lock QTAG" },
1963 { "unlock", cmd_unlock
, "unlock QTAG" },
1964 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
1967 -r, --retag Untag any key currently called new-tag.\n\
1969 { "tidy", cmd_tidy
, "tidy" },
1971 "add [-OPTIONS] TYPE [ATTR...]\n\
1972 Options: [-lqrLS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
1973 [-A SEEDALG] [-s SEED] [-n BITS]\n\
1974 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
1977 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1978 ($ show keygen for list.)\n\
1979 -b, --bits=N Generate an N-bit key.\n\
1980 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1981 -p, --parameters=TAG Get group parameters from TAG.\n\
1982 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
1983 ($ show ec or $ show dh for list.)\n\
1984 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
1985 ($ show seed for list.)\n\
1986 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
1987 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
1988 -e, --expire=TIME Make the key expire after TIME.\n\
1989 -c, --comment=STRING Attach the command STRING to the key.\n\
1990 -t, --tag=TAG Tag the key with the name TAG.\n\
1991 -r, --retag Untag any key currently with that tag.\n\
1992 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1993 -l, --lock Lock the generated key with a passphrase.\n\
1994 -q, --quiet Don't give progress indicators while working.\n\
1995 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1996 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2001 static int cmd_help(int argc
, char *argv
[])
2003 sc_help(cmds
, stdout
, argv
+ 1);
2007 /*----- Main code ---------------------------------------------------------*/
2009 /* --- Helpful GNUy functions --- */
2011 static void usage(FILE *fp
)
2013 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2016 void version(FILE *fp
)
2018 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2021 void help_global(FILE *fp
)
2025 Performs various simple key management operations.\n\
2027 Global command line options:\n\
2029 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2030 -v, --version Display version number.\n\
2031 -u, --usage Display short usage summary.\n\
2033 -k, --keyring=FILE Read and write keys in FILE.\n",
2039 * Arguments: @int argc@ = number of command line arguments
2040 * @char *argv[]@ = array of command line arguments
2042 * Returns: Nonzero on failure.
2044 * Use: Main program. Performs simple key management functions.
2047 int main(int argc
, char *argv
[])
2053 /* --- Initialization --- */
2058 /* --- Parse command line options --- */
2061 static struct option opt
[] = {
2063 /* --- Standard GNUy help options --- */
2065 { "help", 0, 0, 'h' },
2066 { "version", 0, 0, 'v' },
2067 { "usage", 0, 0, 'u' },
2069 /* --- Real live useful options --- */
2071 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2073 /* --- Magic terminator --- */
2077 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2083 /* --- GNU help options --- */
2086 sc_help(cmds
, stdout
, argv
+ optind
);
2095 /* --- Real useful options --- */
2101 /* --- Bogosity --- */
2109 /* --- Complain about excessive bogons --- */
2111 if (f
& f_bogus
|| optind
== argc
) {
2116 /* --- Initialize the Catacomb random number generator --- */
2118 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2119 rand_seed(RAND_GLOBAL
, 160);
2121 /* --- Dispatch to appropriate command handler --- */
2126 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2129 /*----- That's all, folks -------------------------------------------------*/