3 * $Id: keyutil.c,v 1.25 2004/04/21 00:38:22 mdw Exp $
5 * Simple key manager program
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/base64.h>
41 #include <mLib/mdwopt.h>
42 #include <mLib/quis.h>
43 #include <mLib/report.h>
68 #include "sha256-mgf.h"
69 #include "sha224-mgf.h"
70 #include "sha384-mgf.h"
71 #include "sha512-mgf.h"
72 #include "tiger-mgf.h"
73 #include "rmd128-mgf.h"
74 #include "rmd160-mgf.h"
75 #include "rmd256-mgf.h"
76 #include "rmd320-mgf.h"
80 /*----- Handy global state ------------------------------------------------*/
82 static const char *keyfile
= "keyring";
84 /*----- Useful shared functions -------------------------------------------*/
88 * Arguments: @key_file *f@ = pointer to key file block
89 * @unsigned how@ = method to open file with
93 * Use: Opens a key file and handles errors by panicking
97 static void doopen(key_file
*f
, unsigned how
)
99 if (key_open(f
, keyfile
, how
, key_moan
, 0))
100 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
103 /* --- @doclose@ --- *
105 * Arguments: @key_file *f@ = pointer to key file block
109 * Use: Closes a key file and handles errors by panicking
113 static void doclose(key_file
*f
)
115 switch (key_close(f
)) {
117 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
118 keyfile
, strerror(errno
));
120 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
121 keyfile
, strerror(errno
));
125 /* --- @setattr@ --- *
127 * Arguments: @key_file *f@ = pointer to key file block
128 * @key *k@ = pointer to key block
129 * @char *v[]@ = array of assignments (overwritten!)
133 * Use: Applies the attribute assignments to the key.
136 static void setattr(key_file
*f
, key
*k
, char *v
[])
141 size_t eq
= strcspn(p
, "=");
143 moan("invalid assignment: `%s' (ignored)", p
);
149 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
150 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
155 /*----- Seeding -----------------------------------------------------------*/
157 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
159 { "dsarand", dsarand_create
},
160 { "rmd128-mgf", rmd128_mgfrand
},
161 { "rmd160-mgf", rmd160_mgfrand
},
162 { "rmd256-mgf", rmd256_mgfrand
},
163 { "rmd320-mgf", rmd320_mgfrand
},
164 { "sha-mgf", sha_mgfrand
},
165 { "sha224-mgf", sha224_mgfrand
},
166 { "sha256-mgf", sha256_mgfrand
},
167 { "sha384-mgf", sha384_mgfrand
},
168 { "sha512-mgf", sha512_mgfrand
},
169 { "tiger-mgf", tiger_mgfrand
},
173 #define SEEDALG_DEFAULT (seedtab + 2)
175 /*----- Key generation ----------------------------------------------------*/
177 /* --- Key generation parameters --- */
179 typedef struct keyopts
{
180 key_file
*kf
; /* Pointer to key file */
181 key
*k
; /* Pointer to the actual key */
182 dstr tag
; /* Full tag name for the key */
183 unsigned f
; /* Flags for the new key */
184 unsigned bits
, qbits
; /* Bit length for the new key */
185 const char *curve
; /* Elliptic curve name/info */
186 grand
*r
; /* Random number source */
187 key
*p
; /* Parameters key-data */
190 #define f_bogus 1u /* Error in parsing */
191 #define f_lock 2u /* Passphrase-lock private key */
192 #define f_quiet 4u /* Don't show a progress indicator */
193 #define f_limlee 8u /* Generate Lim-Lee primes */
194 #define f_subgroup 16u /* Generate a subgroup */
195 #define f_retag 32u /* Remove any existing tag */
197 /* --- @dolock@ --- *
199 * Arguments: @keyopts *k@ = key generation options
200 * @key_data *kd@ = pointer to key data to lock
201 * @const char *t@ = tag suffix or null
205 * Use: Does passphrase locking on new keys.
208 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
210 if (!(k
->f
& f_lock
))
213 dstr_putf(&k
->tag
, ".%s", t
);
214 if (key_plock(k
->tag
.buf
, kd
, kd
))
215 die(EXIT_FAILURE
, "couldn't lock key");
220 * Arguments: @key_data *kd@ = pointer to parent key block
221 * @const char *tag@ = pointer to tag string
222 * @mp *m@ = integer to store
223 * @unsigned f@ = flags to set
227 * Use: Sets a multiprecision integer subkey.
230 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
232 key_data
*kkd
= key_structcreate(kd
, tag
);
237 /* --- @copyparam@ --- *
239 * Arguments: @keyopts *k@ = pointer to key options
240 * @const char **pp@ = checklist of parameters
242 * Returns: Nonzero if parameters copied; zero if you have to generate
245 * Use: Copies parameters from a source key to the current one.
248 static int copyparam(keyopts
*k
, const char **pp
)
254 /* --- Quick check if no parameters supplied --- */
259 /* --- Run through the checklist --- */
262 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
264 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
265 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
266 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
270 /* --- Copy over the parameters --- */
274 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
275 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
277 /* --- Copy over attributes --- */
279 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
280 key_putattr(k
->kf
, k
->k
, n
, v
);
289 * Arguments: @key_data *k@ = pointer to key data block
290 * @const char *tag@ = tag string to use
292 * Returns: Pointer to multiprecision integer key item.
294 * Use: Fetches an MP key component.
297 static mp
*getmp(key_data
*k
, const char *tag
)
299 k
= key_structfind(k
, tag
);
301 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
302 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
303 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
307 /* --- @keyrand@ --- *
309 * Arguments: @key_file *kf@ = pointer to key file
310 * @const char *id@ = pointer to key id (or null)
314 * Use: Keys the random number generator.
317 static void keyrand(key_file
*kf
, const char *id
)
321 /* --- Find the key --- */
324 if ((k
= key_bytag(kf
, id
)) == 0)
325 die(EXIT_FAILURE
, "key `%s' not found", id
);
327 k
= key_bytype(kf
, "catacomb-rand");
330 key_data
*kd
= &k
->k
, kkd
;
333 switch (kd
->e
& KF_ENCMASK
) {
339 if (key_punlock(d
.buf
, kd
, &kkd
))
340 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
347 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
351 /* --- Key the generator --- */
353 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
359 /* --- Key generation algorithms --- */
361 static void alg_binary(keyopts
*k
)
370 die(EXIT_FAILURE
, "no shared parameters for binary keys");
372 sz
= (k
->bits
+ 7) >> 3;
374 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
375 k
->r
->ops
->fill(k
->r
, p
, sz
);
377 key_binary(&k
->k
->k
, p
, sz
);
378 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
381 dolock(k
, &k
->k
->k
, 0);
384 static void alg_des(keyopts
*k
)
393 die(EXIT_FAILURE
, "no shared parameters for DES keys");
394 if (k
->bits
% 56 || k
->bits
> 168)
395 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
399 k
->r
->ops
->fill(k
->r
, p
, sz
);
400 for (i
= 0; i
< sz
; i
++) {
401 octet x
= p
[i
] | 0x01;
405 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
407 key_binary(&k
->k
->k
, p
, sz
);
408 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
411 dolock(k
, &k
->k
->k
, 0);
414 static void alg_rsa(keyopts
*k
)
419 /* --- Sanity checking --- */
422 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
426 /* --- Generate the RSA parameters --- */
428 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
429 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
430 die(EXIT_FAILURE
, "RSA key generation failed");
432 /* --- Run a test encryption --- */
435 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
437 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
442 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
443 c
= rsa_qprivop(&rp
, c
, c
, g
);
446 die(EXIT_FAILURE
, "test encryption failed");
452 /* --- Allrighty then --- */
456 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
457 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
459 kd
= key_structcreate(kd
, "private");
461 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
462 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
463 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
464 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
465 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
466 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
467 dolock(k
, kd
, "private");
472 static void alg_dsaparam(keyopts
*k
)
474 static const char *pl
[] = { "q", "p", "g", 0 };
475 if (!copyparam(k
, pl
)) {
481 key_data
*kd
= &k
->k
->k
;
484 /* --- Choose appropriate bit lengths if necessary --- */
491 /* --- Allocate a seed block --- */
493 sz
= (k
->qbits
+ 7) >> 3;
495 k
->r
->ops
->fill(k
->r
, p
, sz
);
497 /* --- Allocate the parameters --- */
499 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
500 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
501 die(EXIT_FAILURE
, "DSA parameter generation failed");
503 /* --- Store the parameters --- */
506 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
507 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
508 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
513 /* --- Store the seed for future verification --- */
518 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
519 base64_encode(&c
, 0, 0, &d
);
520 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
522 dstr_putf(&d
, "%u", ds
.count
);
523 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
530 static void alg_dsa(keyopts
*k
)
535 key_data
*kd
= &k
->k
->k
;
537 /* --- Get the shared parameters --- */
544 /* --- Choose a private key --- */
546 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
547 mpmont_create(&mm
, p
);
548 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
550 /* --- Store everything away --- */
552 mpkey(kd
, "y", y
, KCAT_PUB
);
554 kd
= key_structcreate(kd
, "private");
556 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
557 dolock(k
, kd
, "private");
559 mp_drop(x
); mp_drop(y
);
562 static void alg_dhparam(keyopts
*k
)
564 static const char *pl
[] = { "p", "q", "g", 0 };
565 key_data
*kd
= &k
->k
->k
;
566 if (!copyparam(k
, pl
)) {
573 if (strcmp(k
->curve
, "list") == 0) {
575 printf("Built-in prime groups:\n");
576 for (pe
= ptab
; pe
->name
; pe
++)
577 printf(" %s\n", pe
->name
);
581 if (dh_parse(&qd
, &dp
))
582 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
589 /* --- Choose a large safe prime number --- */
591 if (k
->f
& f_limlee
) {
596 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
597 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
598 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
599 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
603 for (i
= 0; i
< nf
; i
++) {
606 mp_writedstr(f
[i
], &d
, 10);
609 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
613 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
614 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
617 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
621 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
622 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
623 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
630 static void alg_dh(keyopts
*k
)
635 key_data
*kd
= &k
->k
->k
;
637 /* --- Get the shared parameters --- */
644 /* --- Choose a suitable private key --- *
646 * Since %$g$% has order %$q$%, choose %$x < q$%.
649 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
651 /* --- Compute the public key %$y = g^x \bmod p$% --- */
653 mpmont_create(&mm
, p
);
654 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
657 /* --- Store everything away --- */
659 mpkey(kd
, "y", y
, KCAT_PUB
);
661 kd
= key_structcreate(kd
, "private");
663 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
664 dolock(k
, kd
, "private");
666 mp_drop(x
); mp_drop(y
);
669 static void alg_bbs(keyopts
*k
)
674 /* --- Sanity checking --- */
677 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
681 /* --- Generate the BBS parameters --- */
683 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
684 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
685 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
687 /* --- Allrighty then --- */
691 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
693 kd
= key_structcreate(kd
, "private");
695 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
696 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
697 dolock(k
, kd
, "private");
702 static void alg_ecparam(keyopts
*k
)
704 static const char *pl
[] = { "curve", 0 };
705 if (!copyparam(k
, pl
)) {
708 key_data
*kd
= &k
->k
->k
;
710 /* --- Decide on a curve --- */
712 if (!k
->bits
) k
->bits
= 256;
713 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
715 printf("Built-in elliptic curves:\n");
716 for (ee
= ectab
; ee
->name
; ee
++)
717 printf(" %s\n", ee
->name
);
721 if (k
->bits
<= 56) k
->curve
= "secp112r1";
722 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
723 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
724 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
725 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
726 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
727 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
728 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
730 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
734 /* --- Check it --- */
736 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
737 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
738 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
739 moan("WARNING! curve check failed: %s", e
);
742 /* --- Write out the answer --- */
745 kd
= key_structcreate(kd
, "curve");
746 key_string(kd
, k
->curve
);
751 static void alg_ec(keyopts
*k
)
753 key_data
*kd
= &k
->k
->k
;
760 /* --- Get the curve --- */
763 if ((kkd
= key_structfind(kd
, "curve")) == 0)
764 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
765 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
766 die(EXIT_FAILURE
, "subkey `curve' is not a string");
767 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
768 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
770 /* --- Invent a private exponent and compute the public key --- */
772 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
773 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
775 /* --- Store everything away --- */
777 kkd
= key_structcreate(kd
, "p");
780 kkd
= key_structcreate(kd
, "private");
782 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
790 /* --- The algorithm tables --- */
792 typedef struct keyalg
{
794 void (*proc
)(keyopts
*o
);
798 static keyalg algtab
[] = {
799 { "binary", alg_binary
, "Plain binary data" },
800 { "des", alg_des
, "Binary with DES-style parity" },
801 { "rsa", alg_rsa
, "RSA public-key encryption" },
802 { "dsa", alg_dsa
, "DSA digital signatures" },
803 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
804 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
805 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
806 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
807 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
808 { "ec", alg_ec
, "Elliptic curve crypto" },
812 /* --- @cmd_add@ --- */
814 static int cmd_add(int argc
, char *argv
[])
817 time_t exp
= KEXP_EXPIRE
;
818 const char *tag
= 0, *ptag
= 0;
820 keyalg
*alg
= algtab
;
821 const char *rtag
= 0;
822 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
823 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
824 const char *seed
= 0;
827 /* --- Parse options for the subcommand --- */
830 static struct option opt
[] = {
831 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
832 { "bits", OPTF_ARGREQ
, 0, 'b' },
833 { "qbits", OPTF_ARGREQ
, 0, 'B' },
834 { "parameters", OPTF_ARGREQ
, 0, 'p' },
835 { "expire", OPTF_ARGREQ
, 0, 'e' },
836 { "comment", OPTF_ARGREQ
, 0, 'c' },
837 { "tag", OPTF_ARGREQ
, 0, 't' },
838 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
839 { "curve", OPTF_ARGREQ
, 0, 'C' },
840 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
841 { "seed", OPTF_ARGREQ
, 0, 's' },
842 { "newseed", OPTF_ARGREQ
, 0, 'n' },
843 { "lock", 0, 0, 'l' },
844 { "quiet", 0, 0, 'q' },
845 { "lim-lee", 0, 0, 'L' },
846 { "subgroup", 0, 0, 'S' },
849 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
854 /* --- Handle the various options --- */
858 /* --- Read an algorithm name --- */
862 size_t sz
= strlen(optarg
);
864 if (strcmp(optarg
, "list") == 0) {
865 for (a
= algtab
; a
->name
; a
++)
866 printf("%-10s %s\n", a
->name
, a
->help
);
871 for (a
= algtab
; a
->name
; a
++) {
872 if (strncmp(optarg
, a
->name
, sz
) == 0) {
873 if (a
->name
[sz
] == 0) {
877 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
883 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
886 /* --- Bits must be nonzero and a multiple of 8 --- */
890 k
.bits
= strtoul(optarg
, &p
, 0);
891 if (k
.bits
== 0 || *p
!= 0)
892 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
897 k
.qbits
= strtoul(optarg
, &p
, 0);
898 if (k
.qbits
== 0 || *p
!= 0)
899 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
902 /* --- Parameter selection --- */
908 /* --- Expiry dates get passed to @get_date@ for parsing --- */
911 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
914 exp
= get_date(optarg
, 0);
916 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
920 /* --- Store comments without interpretation --- */
923 if (key_chkcomment(optarg
))
924 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
928 /* --- Elliptic curve parameters --- */
934 /* --- Store tags --- */
937 if (key_chkident(optarg
))
938 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
945 /* --- Seeding --- */
948 const struct seedalg
*ss
;
949 if (strcmp(optarg
, "list") == 0) {
950 printf("Seed algorithms:\n");
951 for (ss
= seedtab
; ss
->p
; ss
++)
952 printf(" %s\n", ss
->p
);
955 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
957 for (ss
= seedtab
; ss
->p
; ss
++) {
958 if (strcmp(optarg
, ss
->p
) == 0)
962 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
968 if (seed
) die(EXIT_FAILURE
, "seed already set");
970 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
971 base64_decode(&b
, 0, 0, &d
);
972 k
.r
= sa
->gen(d
.buf
, d
.len
);
981 unsigned n
= strtoul(optarg
, &p
, 0);
982 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
983 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
984 if (seed
) die(EXIT_FAILURE
, "seed already set");
987 rand_get(RAND_GLOBAL
, p
, n
);
989 base64_encode(&b
, p
, n
, &d
);
990 base64_encode(&b
, 0, 0, &d
);
995 /* --- Other flags --- */
1013 /* --- Other things are bogus --- */
1021 /* --- Various sorts of bogosity --- */
1023 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1025 "Usage: add [options] type [attr...]");
1027 if (key_chkident(argv
[optind
]))
1028 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1030 /* --- Set up various bits of the state --- */
1032 if (exp
== KEXP_EXPIRE
)
1033 exp
= time(0) + 14 * 24 * 60 * 60;
1035 /* --- Open the file and create the basic key block --- *
1037 * Keep on generating keyids until one of them doesn't collide.
1040 doopen(&f
, KOPEN_WRITE
);
1043 /* --- Key the generator --- */
1048 uint32 id
= rand_global
.ops
->word(&rand_global
);
1050 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1053 if (err
!= KERR_DUPID
)
1054 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1057 /* --- Set various simple attributes --- */
1062 if (k
.f
& f_retag
) {
1063 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1064 key_settag(&f
, kk
, 0);
1066 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1067 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1071 int err
= key_setcomment(&f
, k
.k
, c
);
1073 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1076 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1078 key_putattr(&f
, k
.k
, "genseed", seed
);
1079 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1082 key_fulltag(k
.k
, &k
.tag
);
1084 /* --- Find the parameter key --- */
1087 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1088 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1089 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1090 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1093 /* --- Now generate the actual key data --- */
1103 /*----- Key listing -------------------------------------------------------*/
1105 /* --- Listing options --- */
1107 typedef struct listopts
{
1108 const char *tfmt
; /* Date format (@strftime@-style) */
1109 int v
; /* Verbosity level */
1110 unsigned f
; /* Various flags */
1111 time_t t
; /* Time now (for key expiry) */
1112 key_filter kf
; /* Filter for matching keys */
1115 /* --- Listing flags --- */
1117 #define f_newline 2u /* Write newline before next entry */
1118 #define f_attr 4u /* Written at least one attribute */
1119 #define f_utc 8u /* Emit UTC time, not local time */
1121 /* --- @showkeydata@ --- *
1123 * Arguments: @key_data *k@ = pointer to key to write
1124 * @int ind@ = indentation level
1125 * @listopts *o@ = listing options
1126 * @dstr *d@ = tag string for this subkey
1130 * Use: Emits a piece of key data in a human-readable format.
1133 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1135 #define INDENT(i) do { \
1137 for (_i = 0; _i < (i); _i++) { \
1142 switch (k
->e
& KF_ENCMASK
) {
1144 /* --- Binary key data --- *
1146 * Emit as a simple hex dump.
1150 const octet
*p
= k
->u
.k
.k
;
1151 const octet
*l
= p
+ k
->u
.k
.sz
;
1154 fputs(" {", stdout
);
1159 } else if (sz
% 8 == 0)
1163 printf("%02x", *p
++);
1168 fputs("}\n", stdout
);
1171 /* --- Encrypted data --- *
1173 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1174 * key. Otherwise just say that it's encrypted and move on.
1179 fputs(" encrypted\n", stdout
);
1182 if (key_punlock(d
->buf
, k
, &kd
))
1183 printf(" <failed to unlock %s>\n", d
->buf
);
1185 fputs(" encrypted", stdout
);
1186 showkeydata(&kd
, ind
, o
, d
);
1192 /* --- Integer keys --- *
1194 * Emit as a large integer in decimal. This makes using the key in
1195 * `calc' or whatever easier.
1200 mp_writefile(k
->u
.m
, stdout
, 10);
1204 /* --- Strings --- */
1207 printf(" `%s'\n", k
->u
.p
);
1210 /* --- Elliptic curve points --- */
1213 if (EC_ATINF(&k
->u
.e
))
1214 fputs(" inf\n", stdout
);
1216 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1217 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1222 /* --- Structured keys --- *
1224 * Just iterate over the subkeys.
1232 fputs(" {\n", stdout
);
1233 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1234 if (!key_match(&ks
->k
, &o
->kf
))
1237 printf("%s =", SYM_NAME(ks
));
1239 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1240 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1243 fputs("}\n", stdout
);
1250 /* --- @showkey@ --- *
1252 * Arguments: @key *k@ = pointer to key to show
1253 * @listopts *o@ = pointer to listing options
1257 * Use: Emits a listing of a particular key.
1260 static void showkey(key
*k
, listopts
*o
)
1262 char ebuf
[24], dbuf
[24];
1265 /* --- Skip the key if the filter doesn't match --- */
1267 if (!key_match(&k
->k
, &o
->kf
))
1270 /* --- Sort out the expiry and deletion times --- */
1272 if (KEY_EXPIRED(o
->t
, k
->exp
))
1273 strcpy(ebuf
, "expired");
1274 else if (k
->exp
== KEXP_FOREVER
)
1275 strcpy(ebuf
, "forever");
1277 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1278 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1281 if (KEY_EXPIRED(o
->t
, k
->del
))
1282 strcpy(dbuf
, "deleted");
1283 else if (k
->del
== KEXP_FOREVER
)
1284 strcpy(dbuf
, "forever");
1286 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1287 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1290 /* --- If in compact format, just display and quit --- */
1293 if (!(o
->f
& f_newline
)) {
1294 printf("%8s %-20s %-20s %-10s %-10s\n",
1295 "Id", "Tag", "Type", "Expire", "Delete");
1297 printf("%08lx %-20s %-20s %-10s %-10s\n",
1298 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1299 k
->type
, ebuf
, dbuf
);
1304 /* --- Display the standard header --- */
1306 if (o
->f
& f_newline
)
1307 fputc('\n', stdout
);
1308 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1309 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1310 printf("type: %s\n", k
->type
);
1311 printf("expiry: %s\n", ebuf
);
1312 printf("delete: %s\n", dbuf
);
1313 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1315 /* --- Display the attributes --- */
1319 const char *av
, *an
;
1322 printf("attributes:");
1323 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1324 printf("\n %s = %s", an
, av
);
1328 fputc('\n', stdout
);
1333 /* --- If dumping requested, dump the raw key data --- */
1337 fputs("key:", stdout
);
1339 showkeydata(&k
->k
, 0, o
, &d
);
1346 /* --- @cmd_list@ --- */
1348 static int cmd_list(int argc
, char *argv
[])
1352 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1354 /* --- Parse subcommand options --- */
1357 static struct option opt
[] = {
1358 { "quiet", 0, 0, 'q' },
1359 { "verbose", 0, 0, 'v' },
1360 { "utc", 0, 0, 'u' },
1361 { "filter", OPTF_ARGREQ
, 0, 'f' },
1364 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1381 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1383 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1392 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1394 /* --- Open the key file --- */
1396 doopen(&f
, KOPEN_READ
);
1399 /* --- Set up the time format --- */
1402 o
.tfmt
= "%Y-%m-%d";
1403 else if (o
.f
& f_utc
)
1404 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1406 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1408 /* --- If specific keys were requested use them, otherwise do all --- *
1410 * Some day, this might turn into a wildcard match.
1413 if (optind
< argc
) {
1415 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1418 moan("key `%s' not found", argv
[optind
]);
1422 } while (optind
< argc
);
1425 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1433 return (EXIT_FAILURE
);
1438 /*----- Command implementation --------------------------------------------*/
1440 /* --- @cmd_expire@ --- */
1442 static int cmd_expire(int argc
, char *argv
[])
1450 die(EXIT_FAILURE
, "Usage: expire tag...");
1451 doopen(&f
, KOPEN_WRITE
);
1452 for (i
= 1; i
< argc
; i
++) {
1453 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1456 moan("key `%s' not found", argv
[i
]);
1464 /* --- @cmd_delete@ --- */
1466 static int cmd_delete(int argc
, char *argv
[])
1474 die(EXIT_FAILURE
, "Usage: delete tag...");
1475 doopen(&f
, KOPEN_WRITE
);
1476 for (i
= 1; i
< argc
; i
++) {
1477 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1480 moan("key `%s' not found", argv
[i
]);
1488 /* --- @cmd_setattr@ --- */
1490 static int cmd_setattr(int argc
, char *argv
[])
1496 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1497 doopen(&f
, KOPEN_WRITE
);
1498 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1499 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1500 setattr(&f
, k
, argv
+ 2);
1505 /* --- @cmd_finger@ --- */
1507 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1515 if (key_fingerprint(k
, h
, kf
)) {
1518 for (i
= 0; i
< ch
->hashsz
; i
++) {
1519 if (i
&& i
% 4 == 0)
1521 printf("%02x", p
[i
]);
1523 printf(" %s\n", d
.buf
);
1529 static int cmd_finger(int argc
, char *argv
[])
1533 const gchash
*ch
= &rmd160
;
1534 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1537 static struct option opt
[] = {
1538 { "filter", OPTF_ARGREQ
, 0, 'f' },
1539 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1542 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1548 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1550 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1553 if ((ch
= ghash_byname(optarg
)) == 0)
1554 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1562 argv
+= optind
; argc
-= optind
;
1564 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1566 doopen(&f
, KOPEN_READ
);
1570 for (i
= 0; i
< argc
; i
++) {
1571 key
*k
= key_bytag(&f
, argv
[i
]);
1573 fingerprint(k
, ch
, &kf
);
1576 moan("key `%s' not found", argv
[i
]);
1582 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1583 fingerprint(k
, ch
, &kf
);
1588 /* --- @cmd_comment@ --- */
1590 static int cmd_comment(int argc
, char *argv
[])
1596 if (argc
< 2 || argc
> 3)
1597 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1598 doopen(&f
, KOPEN_WRITE
);
1599 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1600 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1601 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1602 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1607 /* --- @cmd_tag@ --- */
1609 static int cmd_tag(int argc
, char *argv
[])
1618 static struct option opt
[] = {
1619 { "retag", 0, 0, 'r' },
1622 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1635 argv
+= optind
; argc
-= optind
;
1636 if (argc
< 1 || argc
> 2 || rc
)
1637 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1638 doopen(&f
, KOPEN_WRITE
);
1639 if (flags
& f_retag
) {
1640 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1641 key_settag(&f
, k
, 0);
1643 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1644 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1645 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1646 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1651 /* --- @cmd_lock@ --- */
1653 static int cmd_lock(int argc
, char *argv
[])
1661 die(EXIT_FAILURE
, "Usage: lock qtag");
1662 doopen(&f
, KOPEN_WRITE
);
1663 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1664 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1665 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1666 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1667 if (key_plock(d
.buf
, kd
, kd
))
1668 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1674 /* --- @cmd_unlock@ --- */
1676 static int cmd_unlock(int argc
, char *argv
[])
1684 die(EXIT_FAILURE
, "Usage: unlock qtag");
1685 doopen(&f
, KOPEN_WRITE
);
1686 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1687 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1688 if (kd
->e
!= KENC_ENCRYPT
)
1689 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1690 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1691 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1697 /* --- @cmd_extract@ --- */
1699 static int cmd_extract(int argc
, char *argv
[])
1705 key_filter kf
= { 0, 0 };
1709 static struct option opt
[] = {
1710 { "filter", OPTF_ARGREQ
, 0, 'f' },
1713 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1719 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1721 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1729 argv
+= optind
; argc
-= optind
;
1731 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1732 if (strcmp(*argv
, "-") == 0)
1734 else if (!(fp
= fopen(*argv
, "w"))) {
1735 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1736 *argv
, strerror(errno
));
1739 doopen(&f
, KOPEN_READ
);
1743 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1744 key_extract(&f
, k
, fp
, &kf
);
1746 for (i
= 1; i
< argc
; i
++) {
1747 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1748 key_extract(&f
, k
, fp
, &kf
);
1750 moan("key `%s' not found", argv
[i
]);
1756 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1761 /* --- @cmd_tidy@ --- */
1763 static int cmd_tidy(int argc
, char *argv
[])
1767 die(EXIT_FAILURE
, "usage: tidy");
1768 doopen(&f
, KOPEN_WRITE
);
1769 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1774 /* --- @cmd_merge@ --- */
1776 static int cmd_merge(int argc
, char *argv
[])
1782 die(EXIT_FAILURE
, "Usage: merge file");
1783 if (strcmp(argv
[1], "-") == 0)
1785 else if (!(fp
= fopen(argv
[1], "r"))) {
1786 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1787 argv
[1], strerror(errno
));
1790 doopen(&f
, KOPEN_WRITE
);
1791 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1796 /*----- Main command table ------------------------------------------------*/
1800 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1805 "add [options] type [attr...]\n\
1806 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1807 [-e expire] [-t tag] [-c comment]", "\
1810 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1811 -b, --bits=N Generate an N-bit key.\n\
1812 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1813 -p, --parameters=TAG Get group parameters from TAG.\n\
1814 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1815 -e, --expire=TIME Make the key expire after TIME.\n\
1816 -c, --comment=STRING Attach the command STRING to the key.\n\
1817 -t, --tag=TAG Tag the key with the name TAG.\n\
1818 -r, --retag Untag any key currently with that tag.\n\
1819 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1820 -l, --lock Lock the generated key with a passphrase.\n\
1821 -q, --quiet Don't give progress indicators while working.\n\
1822 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1823 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1825 { "expire", cmd_expire
, "expire tag..." },
1826 { "delete", cmd_delete
, "delete tag..." },
1827 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1830 -r, --retag Untag any key currently called new-tag.\n\
1832 { "setattr", cmd_setattr
, "setattr tag attr..." },
1833 { "comment", cmd_comment
, "comment tag [comment]" },
1834 { "lock", cmd_lock
, "lock qtag" },
1835 { "unlock", cmd_unlock
, "unlock qtag" },
1836 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1839 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1840 -q, --quiet Show less information.\n\
1841 -v, --verbose Show more information.\n\
1843 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1846 -f, --filter=FILT Only hash key components matching FILT.\n\
1847 -a, --algorithm=HASH Use the named HASH algorithm.\n\
1849 { "tidy", cmd_tidy
, "tidy" },
1850 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1853 -f, --filter=FILT Only extract key components matching FILT.\n\
1855 { "merge", cmd_merge
, "merge file" },
1859 typedef struct cmd cmd
;
1861 /*----- Main code ---------------------------------------------------------*/
1863 /* --- @findcmd@ --- *
1865 * Arguments: @const char *name@ = a command name
1867 * Returns: Pointer to the command structure.
1869 * Use: Looks up a command by name. If the command isn't found, an
1870 * error is reported and the program is terminated.
1873 static cmd
*findcmd(const char *name
)
1875 cmd
*c
, *chosen
= 0;
1876 size_t sz
= strlen(name
);
1878 for (c
= cmds
; c
->name
; c
++) {
1879 if (strncmp(name
, c
->name
, sz
) == 0) {
1880 if (c
->name
[sz
] == 0) {
1884 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1890 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1894 /* --- Helpful GNUy functions --- */
1896 void usage(FILE *fp
)
1898 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1901 void version(FILE *fp
)
1903 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1906 void help(FILE *fp
, char **argv
)
1914 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1922 Performs various simple key management operations. Command line options\n\
1925 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1926 -v, --version Display version number.\n\
1927 -u, --usage Display short usage summary.\n\
1929 -k, --keyring=FILE Read and write keys in FILE.\n\
1930 -i, --id=TAG Use key TAG for random number generator.\n\
1931 -t, --type=TYPE Use key TYPE for random number generator.\n\
1933 The following commands are understood:\n\n",
1935 for (c
= cmds
; c
->name
; c
++)
1936 fprintf(fp
, "%s\n", c
->usage
);
1942 * Arguments: @int argc@ = number of command line arguments
1943 * @char *argv[]@ = array of command line arguments
1945 * Returns: Nonzero on failure.
1947 * Use: Main program. Performs simple key management functions.
1950 int main(int argc
, char *argv
[])
1956 /* --- Initialization --- */
1961 /* --- Parse command line options --- */
1964 static struct option opt
[] = {
1966 /* --- Standard GNUy help options --- */
1968 { "help", 0, 0, 'h' },
1969 { "version", 0, 0, 'v' },
1970 { "usage", 0, 0, 'u' },
1972 /* --- Real live useful options --- */
1974 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1976 /* --- Magic terminator --- */
1980 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1986 /* --- GNU help options --- */
1989 help(stdout
, argv
+ optind
);
1998 /* --- Real useful options --- */
2004 /* --- Bogosity --- */
2012 /* --- Complain about excessive bogons --- */
2014 if (f
& f_bogus
|| optind
== argc
) {
2019 /* --- Initialize the Catacomb random number generator --- */
2021 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2022 rand_seed(RAND_GLOBAL
, 160);
2024 /* --- Dispatch to appropriate command handler --- */
2029 return (findcmd(argv
[0])->cmd(argc
, argv
));
2032 /*----- That's all, folks -------------------------------------------------*/