9ea8419ce5d5ac9e163fd772c04001d8bb273df1
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
);
897 /* --- The algorithm tables --- */
899 typedef struct keyalg
{
901 void (*proc
)(keyopts
*o
);
905 static keyalg algtab
[] = {
906 { "binary", alg_binary
, "Plain binary data" },
907 { "des", alg_des
, "Binary with DES-style parity" },
908 { "rsa", alg_rsa
, "RSA public-key encryption" },
909 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
910 { "dsa", alg_dsa
, "DSA digital signatures" },
911 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
912 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
913 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
914 { "bindh", alg_bin
, "DH over a binary field" },
915 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
916 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
917 { "ec", alg_ec
, "Elliptic curve crypto" },
921 /* --- @cmd_add@ --- */
923 static int cmd_add(int argc
, char *argv
[])
926 time_t exp
= KEXP_EXPIRE
;
927 const char *tag
= 0, *ptag
= 0;
929 keyalg
*alg
= algtab
;
930 const char *rtag
= 0;
931 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
932 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
933 const char *seed
= 0;
936 /* --- Parse options for the subcommand --- */
939 static struct option opt
[] = {
940 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
941 { "bits", OPTF_ARGREQ
, 0, 'b' },
942 { "qbits", OPTF_ARGREQ
, 0, 'B' },
943 { "parameters", OPTF_ARGREQ
, 0, 'p' },
944 { "expire", OPTF_ARGREQ
, 0, 'e' },
945 { "comment", OPTF_ARGREQ
, 0, 'c' },
946 { "tag", OPTF_ARGREQ
, 0, 't' },
947 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
948 { "curve", OPTF_ARGREQ
, 0, 'C' },
949 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
950 { "seed", OPTF_ARGREQ
, 0, 's' },
951 { "newseed", OPTF_ARGREQ
, 0, 'n' },
952 { "lock", 0, 0, 'l' },
953 { "quiet", 0, 0, 'q' },
954 { "lim-lee", 0, 0, 'L' },
955 { "subgroup", 0, 0, 'S' },
958 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
963 /* --- Handle the various options --- */
967 /* --- Read an algorithm name --- */
971 size_t sz
= strlen(optarg
);
973 if (strcmp(optarg
, "list") == 0) {
974 for (a
= algtab
; a
->name
; a
++)
975 printf("%-10s %s\n", a
->name
, a
->help
);
980 for (a
= algtab
; a
->name
; a
++) {
981 if (strncmp(optarg
, a
->name
, sz
) == 0) {
982 if (a
->name
[sz
] == 0) {
986 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
992 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
995 /* --- Bits must be nonzero and a multiple of 8 --- */
999 k
.bits
= strtoul(optarg
, &p
, 0);
1000 if (k
.bits
== 0 || *p
!= 0)
1001 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1006 k
.qbits
= strtoul(optarg
, &p
, 0);
1007 if (k
.qbits
== 0 || *p
!= 0)
1008 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1011 /* --- Parameter selection --- */
1017 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1020 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
1023 exp
= get_date(optarg
, 0);
1025 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1029 /* --- Store comments without interpretation --- */
1032 if (key_chkcomment(optarg
))
1033 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1037 /* --- Elliptic curve parameters --- */
1043 /* --- Store tags --- */
1046 if (key_chkident(optarg
))
1047 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1054 /* --- Seeding --- */
1057 const struct seedalg
*ss
;
1058 if (strcmp(optarg
, "list") == 0) {
1059 printf("Seed algorithms:\n");
1060 for (ss
= seedtab
; ss
->p
; ss
++)
1061 printf(" %s\n", ss
->p
);
1064 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1066 for (ss
= seedtab
; ss
->p
; ss
++) {
1067 if (strcmp(optarg
, ss
->p
) == 0)
1071 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1077 if (seed
) die(EXIT_FAILURE
, "seed already set");
1079 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1080 base64_decode(&b
, 0, 0, &d
);
1081 k
.r
= sa
->gen(d
.buf
, d
.len
);
1090 unsigned n
= strtoul(optarg
, &p
, 0);
1091 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1092 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1093 if (seed
) die(EXIT_FAILURE
, "seed already set");
1096 rand_get(RAND_GLOBAL
, p
, n
);
1098 base64_encode(&b
, p
, n
, &d
);
1099 base64_encode(&b
, 0, 0, &d
);
1101 k
.r
= sa
->gen(p
, n
);
1104 /* --- Other flags --- */
1122 /* --- Other things are bogus --- */
1130 /* --- Various sorts of bogosity --- */
1132 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1134 "Usage: add [OPTIONS] TYPE [ATTR...]");
1136 if (key_chkident(argv
[optind
]))
1137 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1139 /* --- Set up various bits of the state --- */
1141 if (exp
== KEXP_EXPIRE
)
1142 exp
= time(0) + 14 * 24 * 60 * 60;
1144 /* --- Open the file and create the basic key block --- *
1146 * Keep on generating keyids until one of them doesn't collide.
1149 doopen(&f
, KOPEN_WRITE
);
1152 /* --- Key the generator --- */
1157 uint32 id
= rand_global
.ops
->word(&rand_global
);
1159 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1162 if (err
!= KERR_DUPID
)
1163 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1166 /* --- Set various simple attributes --- */
1171 if (k
.f
& f_retag
) {
1172 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1173 key_settag(&f
, kk
, 0);
1175 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1176 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1180 int err
= key_setcomment(&f
, k
.k
, c
);
1182 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1185 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1187 key_putattr(&f
, k
.k
, "genseed", seed
);
1188 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1191 key_fulltag(k
.k
, &k
.tag
);
1193 /* --- Find the parameter key --- */
1196 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1197 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1198 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1199 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1202 /* --- Now generate the actual key data --- */
1212 /*----- Key listing -------------------------------------------------------*/
1214 /* --- Listing options --- */
1216 typedef struct listopts
{
1217 const char *tfmt
; /* Date format (@strftime@-style) */
1218 int v
; /* Verbosity level */
1219 unsigned f
; /* Various flags */
1220 time_t t
; /* Time now (for key expiry) */
1221 key_filter kf
; /* Filter for matching keys */
1224 /* --- Listing flags --- */
1226 #define f_newline 2u /* Write newline before next entry */
1227 #define f_attr 4u /* Written at least one attribute */
1228 #define f_utc 8u /* Emit UTC time, not local time */
1230 /* --- @showkeydata@ --- *
1232 * Arguments: @key_data *k@ = pointer to key to write
1233 * @int ind@ = indentation level
1234 * @listopts *o@ = listing options
1235 * @dstr *d@ = tag string for this subkey
1239 * Use: Emits a piece of key data in a human-readable format.
1242 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1244 #define INDENT(i) do { \
1246 for (_i = 0; _i < (i); _i++) { \
1251 switch (k
->e
& KF_ENCMASK
) {
1253 /* --- Binary key data --- *
1255 * Emit as a simple hex dump.
1259 const octet
*p
= k
->u
.k
.k
;
1260 const octet
*l
= p
+ k
->u
.k
.sz
;
1263 fputs(" {", stdout
);
1268 } else if (sz
% 8 == 0)
1272 printf("%02x", *p
++);
1277 fputs("}\n", stdout
);
1280 /* --- Encrypted data --- *
1282 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1283 * key. Otherwise just say that it's encrypted and move on.
1288 fputs(" encrypted\n", stdout
);
1291 if (key_punlock(d
->buf
, k
, &kd
))
1292 printf(" <failed to unlock %s>\n", d
->buf
);
1294 fputs(" encrypted", stdout
);
1295 showkeydata(&kd
, ind
, o
, d
);
1301 /* --- Integer keys --- *
1303 * Emit as a large integer in decimal. This makes using the key in
1304 * `calc' or whatever easier.
1309 mp_writefile(k
->u
.m
, stdout
, 10);
1313 /* --- Strings --- */
1316 printf(" `%s'\n", k
->u
.p
);
1319 /* --- Elliptic curve points --- */
1322 if (EC_ATINF(&k
->u
.e
))
1323 fputs(" inf\n", stdout
);
1325 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1326 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1331 /* --- Structured keys --- *
1333 * Just iterate over the subkeys.
1341 fputs(" {\n", stdout
);
1342 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1343 if (!key_match(&ks
->k
, &o
->kf
))
1346 printf("%s =", SYM_NAME(ks
));
1348 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1349 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1352 fputs("}\n", stdout
);
1359 /* --- @showkey@ --- *
1361 * Arguments: @key *k@ = pointer to key to show
1362 * @listopts *o@ = pointer to listing options
1366 * Use: Emits a listing of a particular key.
1369 static void showkey(key
*k
, listopts
*o
)
1371 char ebuf
[24], dbuf
[24];
1374 /* --- Skip the key if the filter doesn't match --- */
1376 if (!key_match(&k
->k
, &o
->kf
))
1379 /* --- Sort out the expiry and deletion times --- */
1381 if (KEY_EXPIRED(o
->t
, k
->exp
))
1382 strcpy(ebuf
, "expired");
1383 else if (k
->exp
== KEXP_FOREVER
)
1384 strcpy(ebuf
, "forever");
1386 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1387 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1390 if (KEY_EXPIRED(o
->t
, k
->del
))
1391 strcpy(dbuf
, "deleted");
1392 else if (k
->del
== KEXP_FOREVER
)
1393 strcpy(dbuf
, "forever");
1395 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1396 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1399 /* --- If in compact format, just display and quit --- */
1402 if (!(o
->f
& f_newline
)) {
1403 printf("%8s %-20s %-20s %-10s %-10s\n",
1404 "Id", "Tag", "Type", "Expire", "Delete");
1406 printf("%08lx %-20s %-20s %-10s %-10s\n",
1407 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1408 k
->type
, ebuf
, dbuf
);
1413 /* --- Display the standard header --- */
1415 if (o
->f
& f_newline
)
1416 fputc('\n', stdout
);
1417 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1418 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1419 printf("type: %s\n", k
->type
);
1420 printf("expiry: %s\n", ebuf
);
1421 printf("delete: %s\n", dbuf
);
1422 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1424 /* --- Display the attributes --- */
1428 const char *av
, *an
;
1431 printf("attributes:");
1432 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1433 printf("\n %s = %s", an
, av
);
1437 fputc('\n', stdout
);
1442 /* --- If dumping requested, dump the raw key data --- */
1446 fputs("key:", stdout
);
1448 showkeydata(&k
->k
, 0, o
, &d
);
1455 /* --- @cmd_list@ --- */
1457 static int cmd_list(int argc
, char *argv
[])
1461 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1463 /* --- Parse subcommand options --- */
1466 static struct option opt
[] = {
1467 { "quiet", 0, 0, 'q' },
1468 { "verbose", 0, 0, 'v' },
1469 { "utc", 0, 0, 'u' },
1470 { "filter", OPTF_ARGREQ
, 0, 'f' },
1473 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1490 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1492 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1501 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1503 /* --- Open the key file --- */
1505 doopen(&f
, KOPEN_READ
);
1508 /* --- Set up the time format --- */
1511 o
.tfmt
= "%Y-%m-%d";
1512 else if (o
.f
& f_utc
)
1513 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1515 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1517 /* --- If specific keys were requested use them, otherwise do all --- *
1519 * Some day, this might turn into a wildcard match.
1522 if (optind
< argc
) {
1524 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1527 moan("key `%s' not found", argv
[optind
]);
1531 } while (optind
< argc
);
1534 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1542 return (EXIT_FAILURE
);
1547 /*----- Command implementation --------------------------------------------*/
1549 /* --- @cmd_expire@ --- */
1551 static int cmd_expire(int argc
, char *argv
[])
1559 die(EXIT_FAILURE
, "Usage: expire TAG...");
1560 doopen(&f
, KOPEN_WRITE
);
1561 for (i
= 1; i
< argc
; i
++) {
1562 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1565 moan("key `%s' not found", argv
[i
]);
1573 /* --- @cmd_delete@ --- */
1575 static int cmd_delete(int argc
, char *argv
[])
1583 die(EXIT_FAILURE
, "Usage: delete TAG...");
1584 doopen(&f
, KOPEN_WRITE
);
1585 for (i
= 1; i
< argc
; i
++) {
1586 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1589 moan("key `%s' not found", argv
[i
]);
1597 /* --- @cmd_setattr@ --- */
1599 static int cmd_setattr(int argc
, char *argv
[])
1605 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1606 doopen(&f
, KOPEN_WRITE
);
1607 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1608 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1609 setattr(&f
, k
, argv
+ 2);
1614 /* --- @cmd_finger@ --- */
1616 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1624 if (key_fingerprint(k
, h
, kf
)) {
1627 for (i
= 0; i
< ch
->hashsz
; i
++) {
1628 if (i
&& i
% 4 == 0)
1630 printf("%02x", p
[i
]);
1632 printf(" %s\n", d
.buf
);
1638 static int cmd_finger(int argc
, char *argv
[])
1642 const gchash
*ch
= &rmd160
;
1643 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1646 static struct option opt
[] = {
1647 { "filter", OPTF_ARGREQ
, 0, 'f' },
1648 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1651 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1657 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1659 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1662 if ((ch
= ghash_byname(optarg
)) == 0)
1663 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1671 argv
+= optind
; argc
-= optind
;
1673 die(EXIT_FAILURE
, "Usage: fingerprint [-f FILTER] [TAG...]");
1675 doopen(&f
, KOPEN_READ
);
1679 for (i
= 0; i
< argc
; i
++) {
1680 key
*k
= key_bytag(&f
, argv
[i
]);
1682 fingerprint(k
, ch
, &kf
);
1685 moan("key `%s' not found", argv
[i
]);
1691 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1692 fingerprint(k
, ch
, &kf
);
1697 /* --- @cmd_comment@ --- */
1699 static int cmd_comment(int argc
, char *argv
[])
1705 if (argc
< 2 || argc
> 3)
1706 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1707 doopen(&f
, KOPEN_WRITE
);
1708 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1709 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1710 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1711 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1716 /* --- @cmd_tag@ --- */
1718 static int cmd_tag(int argc
, char *argv
[])
1727 static struct option opt
[] = {
1728 { "retag", 0, 0, 'r' },
1731 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1744 argv
+= optind
; argc
-= optind
;
1745 if (argc
< 1 || argc
> 2 || rc
)
1746 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1747 doopen(&f
, KOPEN_WRITE
);
1748 if (flags
& f_retag
) {
1749 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1750 key_settag(&f
, k
, 0);
1752 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1753 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1754 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1755 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1760 /* --- @cmd_lock@ --- */
1762 static int cmd_lock(int argc
, char *argv
[])
1770 die(EXIT_FAILURE
, "Usage: lock QTAG");
1771 doopen(&f
, KOPEN_WRITE
);
1772 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1773 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1774 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1775 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1776 if (key_plock(d
.buf
, kd
, kd
))
1777 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1783 /* --- @cmd_unlock@ --- */
1785 static int cmd_unlock(int argc
, char *argv
[])
1793 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1794 doopen(&f
, KOPEN_WRITE
);
1795 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1796 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1797 if (kd
->e
!= KENC_ENCRYPT
)
1798 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1799 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1800 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1806 /* --- @cmd_extract@ --- */
1808 static int cmd_extract(int argc
, char *argv
[])
1814 key_filter kf
= { 0, 0 };
1818 static struct option opt
[] = {
1819 { "filter", OPTF_ARGREQ
, 0, 'f' },
1822 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1828 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1830 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1838 argv
+= optind
; argc
-= optind
;
1840 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
1841 if (strcmp(*argv
, "-") == 0)
1843 else if (!(fp
= fopen(*argv
, "w"))) {
1844 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1845 *argv
, strerror(errno
));
1848 doopen(&f
, KOPEN_READ
);
1852 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1853 key_extract(&f
, k
, fp
, &kf
);
1855 for (i
= 1; i
< argc
; i
++) {
1856 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1857 key_extract(&f
, k
, fp
, &kf
);
1859 moan("key `%s' not found", argv
[i
]);
1865 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1870 /* --- @cmd_tidy@ --- */
1872 static int cmd_tidy(int argc
, char *argv
[])
1876 die(EXIT_FAILURE
, "Usage: tidy");
1877 doopen(&f
, KOPEN_WRITE
);
1878 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1883 /* --- @cmd_merge@ --- */
1885 static int cmd_merge(int argc
, char *argv
[])
1891 die(EXIT_FAILURE
, "Usage: merge FILE");
1892 if (strcmp(argv
[1], "-") == 0)
1894 else if (!(fp
= fopen(argv
[1], "r"))) {
1895 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1896 argv
[1], strerror(errno
));
1899 doopen(&f
, KOPEN_WRITE
);
1900 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1905 /* --- @cmd_show@ --- */
1909 listtab[i].name, listtab[i].name) \
1910 LI("Hash functions", hash, \
1911 ghashtab[i], ghashtab[i]->name) \
1912 LI("Elliptic curves", ec, \
1913 ectab[i].name, ectab[i].name) \
1914 LI("Prime Diffie-Hellman groups", dh, \
1915 ptab[i].name, ptab[i].name) \
1916 LI("Binary Diffie-Hellman groups", bindh, \
1917 bintab[i].name, bintab[i].name) \
1918 LI("Key-generation algorithms", keygen, \
1919 algtab[i].name, algtab[i].name) \
1920 LI("Random seeding algorithms", seed, \
1921 seedtab[i].p, seedtab[i].p)
1923 MAKELISTTAB(listtab
, LISTS
)
1925 static int cmd_show(int argc
, char *argv
[])
1927 return (displaylists(listtab
, argv
+ 1));
1930 /*----- Main command table ------------------------------------------------*/
1932 static int cmd_help(int argc
, char *argv
[]);
1934 static cmd cmds
[] = {
1935 { "help", cmd_help
, "help [COMMAND...]" },
1936 { "show", cmd_show
, "show [ITEM...]" },
1937 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
1940 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1941 -q, --quiet Show less information.\n\
1942 -v, --verbose Show more information.\n\
1944 { "fingerprint", cmd_finger
, "fingerprint [-f FILTER] [TAG...]", "\
1947 -f, --filter=FILT Only hash key components matching FILT.\n\
1948 -a, --algorithm=HASH Use the named HASH algorithm.\n\
1949 ($ show hash for list.)\n\
1951 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
1954 -f, --filter=FILT Only extract key components matching FILT.\n\
1956 { "merge", cmd_merge
, "merge FILE" },
1957 { "expire", cmd_expire
, "expire TAG..." },
1958 { "delete", cmd_delete
, "delete TAG..." },
1959 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
1960 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
1961 { "lock", cmd_lock
, "lock QTAG" },
1962 { "unlock", cmd_unlock
, "unlock QTAG" },
1963 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
1966 -r, --retag Untag any key currently called new-tag.\n\
1968 { "tidy", cmd_tidy
, "tidy" },
1970 "add [-OPTIONS] TYPE [ATTR...]\n\
1971 Options: [-lqrLS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
1972 [-A SEEDALG] [-s SEED] [-n BITS]\n\
1973 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
1976 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1977 ($ show keygen for list.)\n\
1978 -b, --bits=N Generate an N-bit key.\n\
1979 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1980 -p, --parameters=TAG Get group parameters from TAG.\n\
1981 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
1982 ($ show ec or $ show dh for list.)\n\
1983 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
1984 ($ show seed for list.)\n\
1985 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
1986 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
1987 -e, --expire=TIME Make the key expire after TIME.\n\
1988 -c, --comment=STRING Attach the command STRING to the key.\n\
1989 -t, --tag=TAG Tag the key with the name TAG.\n\
1990 -r, --retag Untag any key currently with that tag.\n\
1991 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1992 -l, --lock Lock the generated key with a passphrase.\n\
1993 -q, --quiet Don't give progress indicators while working.\n\
1994 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1995 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2000 static int cmd_help(int argc
, char *argv
[])
2002 sc_help(cmds
, stdout
, argv
+ 1);
2006 /*----- Main code ---------------------------------------------------------*/
2008 /* --- Helpful GNUy functions --- */
2010 static void usage(FILE *fp
)
2012 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2015 void version(FILE *fp
)
2017 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2020 void help_global(FILE *fp
)
2024 Performs various simple key management operations.\n\
2026 Global command line options:\n\
2028 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2029 -v, --version Display version number.\n\
2030 -u, --usage Display short usage summary.\n\
2032 -k, --keyring=FILE Read and write keys in FILE.\n",
2038 * Arguments: @int argc@ = number of command line arguments
2039 * @char *argv[]@ = array of command line arguments
2041 * Returns: Nonzero on failure.
2043 * Use: Main program. Performs simple key management functions.
2046 int main(int argc
, char *argv
[])
2052 /* --- Initialization --- */
2057 /* --- Parse command line options --- */
2060 static struct option opt
[] = {
2062 /* --- Standard GNUy help options --- */
2064 { "help", 0, 0, 'h' },
2065 { "version", 0, 0, 'v' },
2066 { "usage", 0, 0, 'u' },
2068 /* --- Real live useful options --- */
2070 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2072 /* --- Magic terminator --- */
2076 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2082 /* --- GNU help options --- */
2085 sc_help(cmds
, stdout
, argv
+ optind
);
2094 /* --- Real useful options --- */
2100 /* --- Bogosity --- */
2108 /* --- Complain about excessive bogons --- */
2110 if (f
& f_bogus
|| optind
== argc
) {
2115 /* --- Initialize the Catacomb random number generator --- */
2117 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2118 rand_seed(RAND_GLOBAL
, 160);
2120 /* --- Dispatch to appropriate command handler --- */
2125 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2128 /*----- That's all, folks -------------------------------------------------*/