3 * $Id: keyutil.c,v 1.24 2004/04/08 01:36:15 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
)
252 /* --- Quick check if no parameters supplied --- */
257 /* --- Run through the checklist --- */
260 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
262 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
263 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
264 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
268 /* --- Copy over the parameters --- */
272 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
273 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
279 * Arguments: @key_data *k@ = pointer to key data block
280 * @const char *tag@ = tag string to use
282 * Returns: Pointer to multiprecision integer key item.
284 * Use: Fetches an MP key component.
287 static mp
*getmp(key_data
*k
, const char *tag
)
289 k
= key_structfind(k
, tag
);
291 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
292 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
293 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
297 /* --- @keyrand@ --- *
299 * Arguments: @key_file *kf@ = pointer to key file
300 * @const char *id@ = pointer to key id (or null)
304 * Use: Keys the random number generator.
307 static void keyrand(key_file
*kf
, const char *id
)
311 /* --- Find the key --- */
314 if ((k
= key_bytag(kf
, id
)) == 0)
315 die(EXIT_FAILURE
, "key `%s' not found", id
);
317 k
= key_bytype(kf
, "catacomb-rand");
320 key_data
*kd
= &k
->k
, kkd
;
323 switch (kd
->e
& KF_ENCMASK
) {
329 if (key_punlock(d
.buf
, kd
, &kkd
))
330 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
337 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
341 /* --- Key the generator --- */
343 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
349 /* --- Key generation algorithms --- */
351 static void alg_binary(keyopts
*k
)
360 die(EXIT_FAILURE
, "no shared parameters for binary keys");
362 sz
= (k
->bits
+ 7) >> 3;
364 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
365 k
->r
->ops
->fill(k
->r
, p
, sz
);
367 key_binary(&k
->k
->k
, p
, sz
);
368 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
371 dolock(k
, &k
->k
->k
, 0);
374 static void alg_des(keyopts
*k
)
383 die(EXIT_FAILURE
, "no shared parameters for DES keys");
384 if (k
->bits
% 56 || k
->bits
> 168)
385 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
389 k
->r
->ops
->fill(k
->r
, p
, sz
);
390 for (i
= 0; i
< sz
; i
++) {
391 octet x
= p
[i
] | 0x01;
395 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
397 key_binary(&k
->k
->k
, p
, sz
);
398 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
401 dolock(k
, &k
->k
->k
, 0);
404 static void alg_rsa(keyopts
*k
)
409 /* --- Sanity checking --- */
412 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
416 /* --- Generate the RSA parameters --- */
418 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
419 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
420 die(EXIT_FAILURE
, "RSA key generation failed");
422 /* --- Run a test encryption --- */
425 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
427 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
432 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
433 c
= rsa_qprivop(&rp
, c
, c
, g
);
436 die(EXIT_FAILURE
, "test encryption failed");
442 /* --- Allrighty then --- */
446 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
447 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
449 kd
= key_structcreate(kd
, "private");
451 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
452 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
453 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
454 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
455 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
456 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
457 dolock(k
, kd
, "private");
462 static void alg_dsaparam(keyopts
*k
)
464 static const char *pl
[] = { "q", "p", "g", 0 };
465 if (!copyparam(k
, pl
)) {
471 key_data
*kd
= &k
->k
->k
;
474 /* --- Choose appropriate bit lengths if necessary --- */
481 /* --- Allocate a seed block --- */
483 sz
= (k
->qbits
+ 7) >> 3;
485 k
->r
->ops
->fill(k
->r
, p
, sz
);
487 /* --- Allocate the parameters --- */
489 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
490 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
491 die(EXIT_FAILURE
, "DSA parameter generation failed");
493 /* --- Store the parameters --- */
496 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
497 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
498 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
503 /* --- Store the seed for future verification --- */
508 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
509 base64_encode(&c
, 0, 0, &d
);
510 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
512 dstr_putf(&d
, "%u", ds
.count
);
513 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
520 static void alg_dsa(keyopts
*k
)
525 key_data
*kd
= &k
->k
->k
;
527 /* --- Get the shared parameters --- */
534 /* --- Choose a private key --- */
536 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
537 mpmont_create(&mm
, p
);
538 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
540 /* --- Store everything away --- */
542 mpkey(kd
, "y", y
, KCAT_PUB
);
544 kd
= key_structcreate(kd
, "private");
546 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
547 dolock(k
, kd
, "private");
549 mp_drop(x
); mp_drop(y
);
552 static void alg_dhparam(keyopts
*k
)
554 static const char *pl
[] = { "p", "q", "g", 0 };
555 key_data
*kd
= &k
->k
->k
;
556 if (!copyparam(k
, pl
)) {
563 if (strcmp(k
->curve
, "list") == 0) {
565 printf("Built-in prime groups:\n");
566 for (pe
= ptab
; pe
->name
; pe
++)
567 printf(" %s\n", pe
->name
);
571 if (dh_parse(&qd
, &dp
))
572 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
579 /* --- Choose a large safe prime number --- */
581 if (k
->f
& f_limlee
) {
586 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
587 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
588 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
589 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
593 for (i
= 0; i
< nf
; i
++) {
596 mp_writedstr(f
[i
], &d
, 10);
599 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
603 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
604 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
607 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
611 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
612 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
613 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
620 static void alg_dh(keyopts
*k
)
625 key_data
*kd
= &k
->k
->k
;
627 /* --- Get the shared parameters --- */
634 /* --- Choose a suitable private key --- *
636 * Since %$g$% has order %$q$%, choose %$x < q$%.
639 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
641 /* --- Compute the public key %$y = g^x \bmod p$% --- */
643 mpmont_create(&mm
, p
);
644 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
647 /* --- Store everything away --- */
649 mpkey(kd
, "y", y
, KCAT_PUB
);
651 kd
= key_structcreate(kd
, "private");
653 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
654 dolock(k
, kd
, "private");
656 mp_drop(x
); mp_drop(y
);
659 static void alg_bbs(keyopts
*k
)
664 /* --- Sanity checking --- */
667 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
671 /* --- Generate the BBS parameters --- */
673 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
674 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
675 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
677 /* --- Allrighty then --- */
681 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
683 kd
= key_structcreate(kd
, "private");
685 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
686 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
687 dolock(k
, kd
, "private");
692 static void alg_ecparam(keyopts
*k
)
694 static const char *pl
[] = { "curve", 0 };
695 if (!copyparam(k
, pl
)) {
698 key_data
*kd
= &k
->k
->k
;
700 /* --- Decide on a curve --- */
702 if (!k
->bits
) k
->bits
= 256;
703 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
705 printf("Built-in elliptic curves:\n");
706 for (ee
= ectab
; ee
->name
; ee
++)
707 printf(" %s\n", ee
->name
);
711 if (k
->bits
<= 56) k
->curve
= "secp112r1";
712 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
713 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
714 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
715 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
716 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
717 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
718 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
720 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
724 /* --- Check it --- */
726 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
727 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
728 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
729 moan("WARNING! curve check failed: %s", e
);
732 /* --- Write out the answer --- */
735 kd
= key_structcreate(kd
, "curve");
736 key_string(kd
, k
->curve
);
741 static void alg_ec(keyopts
*k
)
743 key_data
*kd
= &k
->k
->k
;
750 /* --- Get the curve --- */
753 if ((kkd
= key_structfind(kd
, "curve")) == 0)
754 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
755 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
756 die(EXIT_FAILURE
, "subkey `curve' is not a string");
757 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
758 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
760 /* --- Invent a private exponent and compute the public key --- */
762 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
763 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
765 /* --- Store everything away --- */
767 kkd
= key_structcreate(kd
, "p");
770 kkd
= key_structcreate(kd
, "private");
772 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
780 /* --- The algorithm tables --- */
782 typedef struct keyalg
{
784 void (*proc
)(keyopts
*o
);
788 static keyalg algtab
[] = {
789 { "binary", alg_binary
, "Plain binary data" },
790 { "des", alg_des
, "Binary with DES-style parity" },
791 { "rsa", alg_rsa
, "RSA public-key encryption" },
792 { "dsa", alg_dsa
, "DSA digital signatures" },
793 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
794 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
795 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
796 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
797 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
798 { "ec", alg_ec
, "Elliptic curve crypto" },
802 /* --- @cmd_add@ --- */
804 static int cmd_add(int argc
, char *argv
[])
807 time_t exp
= KEXP_EXPIRE
;
808 const char *tag
= 0, *ptag
= 0;
810 keyalg
*alg
= algtab
;
811 const char *rtag
= 0;
812 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
813 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
814 const char *seed
= 0;
817 /* --- Parse options for the subcommand --- */
820 static struct option opt
[] = {
821 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
822 { "bits", OPTF_ARGREQ
, 0, 'b' },
823 { "qbits", OPTF_ARGREQ
, 0, 'B' },
824 { "parameters", OPTF_ARGREQ
, 0, 'p' },
825 { "expire", OPTF_ARGREQ
, 0, 'e' },
826 { "comment", OPTF_ARGREQ
, 0, 'c' },
827 { "tag", OPTF_ARGREQ
, 0, 't' },
828 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
829 { "curve", OPTF_ARGREQ
, 0, 'C' },
830 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
831 { "seed", OPTF_ARGREQ
, 0, 's' },
832 { "newseed", OPTF_ARGREQ
, 0, 'n' },
833 { "lock", 0, 0, 'l' },
834 { "quiet", 0, 0, 'q' },
835 { "lim-lee", 0, 0, 'L' },
836 { "subgroup", 0, 0, 'S' },
839 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
844 /* --- Handle the various options --- */
848 /* --- Read an algorithm name --- */
852 size_t sz
= strlen(optarg
);
854 if (strcmp(optarg
, "list") == 0) {
855 for (a
= algtab
; a
->name
; a
++)
856 printf("%-10s %s\n", a
->name
, a
->help
);
861 for (a
= algtab
; a
->name
; a
++) {
862 if (strncmp(optarg
, a
->name
, sz
) == 0) {
863 if (a
->name
[sz
] == 0) {
867 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
873 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
876 /* --- Bits must be nonzero and a multiple of 8 --- */
880 k
.bits
= strtoul(optarg
, &p
, 0);
881 if (k
.bits
== 0 || *p
!= 0)
882 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
887 k
.qbits
= strtoul(optarg
, &p
, 0);
888 if (k
.qbits
== 0 || *p
!= 0)
889 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
892 /* --- Parameter selection --- */
898 /* --- Expiry dates get passed to @get_date@ for parsing --- */
901 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
904 exp
= get_date(optarg
, 0);
906 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
910 /* --- Store comments without interpretation --- */
913 if (key_chkcomment(optarg
))
914 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
918 /* --- Elliptic curve parameters --- */
924 /* --- Store tags --- */
927 if (key_chkident(optarg
))
928 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
935 /* --- Seeding --- */
938 const struct seedalg
*ss
;
939 if (strcmp(optarg
, "list") == 0) {
940 printf("Seed algorithms:\n");
941 for (ss
= seedtab
; ss
->p
; ss
++)
942 printf(" %s\n", ss
->p
);
945 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
947 for (ss
= seedtab
; ss
->p
; ss
++) {
948 if (strcmp(optarg
, ss
->p
) == 0)
952 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
958 if (seed
) die(EXIT_FAILURE
, "seed already set");
960 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
961 base64_decode(&b
, 0, 0, &d
);
962 k
.r
= sa
->gen(d
.buf
, d
.len
);
971 unsigned n
= strtoul(optarg
, &p
, 0);
972 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
973 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
974 if (seed
) die(EXIT_FAILURE
, "seed already set");
977 rand_get(RAND_GLOBAL
, p
, n
);
979 base64_encode(&b
, p
, n
, &d
);
980 base64_encode(&b
, 0, 0, &d
);
985 /* --- Other flags --- */
1003 /* --- Other things are bogus --- */
1011 /* --- Various sorts of bogosity --- */
1013 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1015 "Usage: add [options] type [attr...]");
1017 if (key_chkident(argv
[optind
]))
1018 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1020 /* --- Set up various bits of the state --- */
1022 if (exp
== KEXP_EXPIRE
)
1023 exp
= time(0) + 14 * 24 * 60 * 60;
1025 /* --- Open the file and create the basic key block --- *
1027 * Keep on generating keyids until one of them doesn't collide.
1030 doopen(&f
, KOPEN_WRITE
);
1033 /* --- Key the generator --- */
1038 uint32 id
= rand_global
.ops
->word(&rand_global
);
1040 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1043 if (err
!= KERR_DUPID
)
1044 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1047 /* --- Set various simple attributes --- */
1052 if (k
.f
& f_retag
) {
1053 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1054 key_settag(&f
, kk
, 0);
1056 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1057 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1061 int err
= key_setcomment(&f
, k
.k
, c
);
1063 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1066 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1068 key_putattr(&f
, k
.k
, "genseed", seed
);
1069 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1072 key_fulltag(k
.k
, &k
.tag
);
1074 /* --- Find the parameter key --- */
1077 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1078 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1079 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1080 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1083 /* --- Now generate the actual key data --- */
1093 /*----- Key listing -------------------------------------------------------*/
1095 /* --- Listing options --- */
1097 typedef struct listopts
{
1098 const char *tfmt
; /* Date format (@strftime@-style) */
1099 int v
; /* Verbosity level */
1100 unsigned f
; /* Various flags */
1101 time_t t
; /* Time now (for key expiry) */
1102 key_filter kf
; /* Filter for matching keys */
1105 /* --- Listing flags --- */
1107 #define f_newline 2u /* Write newline before next entry */
1108 #define f_attr 4u /* Written at least one attribute */
1109 #define f_utc 8u /* Emit UTC time, not local time */
1111 /* --- @showkeydata@ --- *
1113 * Arguments: @key_data *k@ = pointer to key to write
1114 * @int ind@ = indentation level
1115 * @listopts *o@ = listing options
1116 * @dstr *d@ = tag string for this subkey
1120 * Use: Emits a piece of key data in a human-readable format.
1123 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1125 #define INDENT(i) do { \
1127 for (_i = 0; _i < (i); _i++) { \
1132 switch (k
->e
& KF_ENCMASK
) {
1134 /* --- Binary key data --- *
1136 * Emit as a simple hex dump.
1140 const octet
*p
= k
->u
.k
.k
;
1141 const octet
*l
= p
+ k
->u
.k
.sz
;
1144 fputs(" {", stdout
);
1149 } else if (sz
% 8 == 0)
1153 printf("%02x", *p
++);
1158 fputs("}\n", stdout
);
1161 /* --- Encrypted data --- *
1163 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1164 * key. Otherwise just say that it's encrypted and move on.
1169 fputs(" encrypted\n", stdout
);
1172 if (key_punlock(d
->buf
, k
, &kd
))
1173 printf(" <failed to unlock %s>\n", d
->buf
);
1175 fputs(" encrypted", stdout
);
1176 showkeydata(&kd
, ind
, o
, d
);
1182 /* --- Integer keys --- *
1184 * Emit as a large integer in decimal. This makes using the key in
1185 * `calc' or whatever easier.
1190 mp_writefile(k
->u
.m
, stdout
, 10);
1194 /* --- Strings --- */
1197 printf(" `%s'\n", k
->u
.p
);
1200 /* --- Elliptic curve points --- */
1203 if (EC_ATINF(&k
->u
.e
))
1204 fputs(" inf\n", stdout
);
1206 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1207 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1212 /* --- Structured keys --- *
1214 * Just iterate over the subkeys.
1222 fputs(" {\n", stdout
);
1223 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1224 if (!key_match(&ks
->k
, &o
->kf
))
1227 printf("%s =", SYM_NAME(ks
));
1229 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1230 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1233 fputs("}\n", stdout
);
1240 /* --- @showkey@ --- *
1242 * Arguments: @key *k@ = pointer to key to show
1243 * @listopts *o@ = pointer to listing options
1247 * Use: Emits a listing of a particular key.
1250 static void showkey(key
*k
, listopts
*o
)
1252 char ebuf
[24], dbuf
[24];
1255 /* --- Skip the key if the filter doesn't match --- */
1257 if (!key_match(&k
->k
, &o
->kf
))
1260 /* --- Sort out the expiry and deletion times --- */
1262 if (KEY_EXPIRED(o
->t
, k
->exp
))
1263 strcpy(ebuf
, "expired");
1264 else if (k
->exp
== KEXP_FOREVER
)
1265 strcpy(ebuf
, "forever");
1267 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1268 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1271 if (KEY_EXPIRED(o
->t
, k
->del
))
1272 strcpy(dbuf
, "deleted");
1273 else if (k
->del
== KEXP_FOREVER
)
1274 strcpy(dbuf
, "forever");
1276 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1277 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1280 /* --- If in compact format, just display and quit --- */
1283 if (!(o
->f
& f_newline
)) {
1284 printf("%8s %-20s %-20s %-10s %-10s\n",
1285 "Id", "Tag", "Type", "Expire", "Delete");
1287 printf("%08lx %-20s %-20s %-10s %-10s\n",
1288 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1289 k
->type
, ebuf
, dbuf
);
1294 /* --- Display the standard header --- */
1296 if (o
->f
& f_newline
)
1297 fputc('\n', stdout
);
1298 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1299 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1300 printf("type: %s\n", k
->type
);
1301 printf("expiry: %s\n", ebuf
);
1302 printf("delete: %s\n", dbuf
);
1303 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1305 /* --- Display the attributes --- */
1309 const char *av
, *an
;
1312 printf("attributes:");
1313 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1314 printf("\n %s = %s", an
, av
);
1318 fputc('\n', stdout
);
1323 /* --- If dumping requested, dump the raw key data --- */
1327 fputs("key:", stdout
);
1329 showkeydata(&k
->k
, 0, o
, &d
);
1336 /* --- @cmd_list@ --- */
1338 static int cmd_list(int argc
, char *argv
[])
1342 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1344 /* --- Parse subcommand options --- */
1347 static struct option opt
[] = {
1348 { "quiet", 0, 0, 'q' },
1349 { "verbose", 0, 0, 'v' },
1350 { "utc", 0, 0, 'u' },
1351 { "filter", OPTF_ARGREQ
, 0, 'f' },
1354 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1371 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1373 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1382 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1384 /* --- Open the key file --- */
1386 doopen(&f
, KOPEN_READ
);
1389 /* --- Set up the time format --- */
1392 o
.tfmt
= "%Y-%m-%d";
1393 else if (o
.f
& f_utc
)
1394 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1396 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1398 /* --- If specific keys were requested use them, otherwise do all --- *
1400 * Some day, this might turn into a wildcard match.
1403 if (optind
< argc
) {
1405 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1408 moan("key `%s' not found", argv
[optind
]);
1412 } while (optind
< argc
);
1415 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1423 return (EXIT_FAILURE
);
1428 /*----- Command implementation --------------------------------------------*/
1430 /* --- @cmd_expire@ --- */
1432 static int cmd_expire(int argc
, char *argv
[])
1440 die(EXIT_FAILURE
, "Usage: expire tag...");
1441 doopen(&f
, KOPEN_WRITE
);
1442 for (i
= 1; i
< argc
; i
++) {
1443 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1446 moan("key `%s' not found", argv
[i
]);
1454 /* --- @cmd_delete@ --- */
1456 static int cmd_delete(int argc
, char *argv
[])
1464 die(EXIT_FAILURE
, "Usage: delete tag...");
1465 doopen(&f
, KOPEN_WRITE
);
1466 for (i
= 1; i
< argc
; i
++) {
1467 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1470 moan("key `%s' not found", argv
[i
]);
1478 /* --- @cmd_setattr@ --- */
1480 static int cmd_setattr(int argc
, char *argv
[])
1486 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1487 doopen(&f
, KOPEN_WRITE
);
1488 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1489 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1490 setattr(&f
, k
, argv
+ 2);
1495 /* --- @cmd_finger@ --- */
1497 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1505 if (key_fingerprint(k
, h
, kf
)) {
1508 for (i
= 0; i
< ch
->hashsz
; i
++) {
1509 if (i
&& i
% 4 == 0)
1511 printf("%02x", p
[i
]);
1513 printf(" %s\n", d
.buf
);
1519 static int cmd_finger(int argc
, char *argv
[])
1523 const gchash
*ch
= &rmd160
;
1524 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1527 static struct option opt
[] = {
1528 { "filter", OPTF_ARGREQ
, 0, 'f' },
1529 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1532 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1538 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1540 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1543 if ((ch
= ghash_byname(optarg
)) == 0)
1544 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1552 argv
+= optind
; argc
-= optind
;
1554 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1556 doopen(&f
, KOPEN_READ
);
1560 for (i
= 0; i
< argc
; i
++) {
1561 key
*k
= key_bytag(&f
, argv
[i
]);
1563 fingerprint(k
, ch
, &kf
);
1566 moan("key `%s' not found", argv
[i
]);
1572 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1573 fingerprint(k
, ch
, &kf
);
1578 /* --- @cmd_comment@ --- */
1580 static int cmd_comment(int argc
, char *argv
[])
1586 if (argc
< 2 || argc
> 3)
1587 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1588 doopen(&f
, KOPEN_WRITE
);
1589 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1590 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1591 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1592 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1597 /* --- @cmd_tag@ --- */
1599 static int cmd_tag(int argc
, char *argv
[])
1608 static struct option opt
[] = {
1609 { "retag", 0, 0, 'r' },
1612 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1625 argv
+= optind
; argc
-= optind
;
1626 if (argc
< 1 || argc
> 2 || rc
)
1627 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1628 doopen(&f
, KOPEN_WRITE
);
1629 if (flags
& f_retag
) {
1630 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1631 key_settag(&f
, k
, 0);
1633 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1634 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1635 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1636 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1641 /* --- @cmd_lock@ --- */
1643 static int cmd_lock(int argc
, char *argv
[])
1651 die(EXIT_FAILURE
, "Usage: lock qtag");
1652 doopen(&f
, KOPEN_WRITE
);
1653 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1654 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1655 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1656 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1657 if (key_plock(d
.buf
, kd
, kd
))
1658 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1664 /* --- @cmd_unlock@ --- */
1666 static int cmd_unlock(int argc
, char *argv
[])
1674 die(EXIT_FAILURE
, "Usage: unlock qtag");
1675 doopen(&f
, KOPEN_WRITE
);
1676 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1677 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1678 if (kd
->e
!= KENC_ENCRYPT
)
1679 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1680 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1681 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1687 /* --- @cmd_extract@ --- */
1689 static int cmd_extract(int argc
, char *argv
[])
1695 key_filter kf
= { 0, 0 };
1699 static struct option opt
[] = {
1700 { "filter", OPTF_ARGREQ
, 0, 'f' },
1703 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1709 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1711 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1719 argv
+= optind
; argc
-= optind
;
1721 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1722 if (strcmp(*argv
, "-") == 0)
1724 else if (!(fp
= fopen(*argv
, "w"))) {
1725 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1726 *argv
, strerror(errno
));
1729 doopen(&f
, KOPEN_READ
);
1733 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1734 key_extract(&f
, k
, fp
, &kf
);
1736 for (i
= 1; i
< argc
; i
++) {
1737 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1738 key_extract(&f
, k
, fp
, &kf
);
1740 moan("key `%s' not found", argv
[i
]);
1746 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1751 /* --- @cmd_tidy@ --- */
1753 static int cmd_tidy(int argc
, char *argv
[])
1757 die(EXIT_FAILURE
, "usage: tidy");
1758 doopen(&f
, KOPEN_WRITE
);
1759 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1764 /* --- @cmd_merge@ --- */
1766 static int cmd_merge(int argc
, char *argv
[])
1772 die(EXIT_FAILURE
, "Usage: merge file");
1773 if (strcmp(argv
[1], "-") == 0)
1775 else if (!(fp
= fopen(argv
[1], "r"))) {
1776 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1777 argv
[1], strerror(errno
));
1780 doopen(&f
, KOPEN_WRITE
);
1781 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1786 /*----- Main command table ------------------------------------------------*/
1790 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1795 "add [options] type [attr...]\n\
1796 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1797 [-e expire] [-t tag] [-c comment]", "\
1800 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1801 -b, --bits=N Generate an N-bit key.\n\
1802 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1803 -p, --parameters=TAG Get group parameters from TAG.\n\
1804 -C, --curve=CURVE Use elliptic curve CURVE.\n\
1805 -e, --expire=TIME Make the key expire after TIME.\n\
1806 -c, --comment=STRING Attach the command STRING to the key.\n\
1807 -t, --tag=TAG Tag the key with the name TAG.\n\
1808 -r, --retag Untag any key currently with that tag.\n\
1809 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1810 -l, --lock Lock the generated key with a passphrase.\n\
1811 -q, --quiet Don't give progress indicators while working.\n\
1812 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1813 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1815 { "expire", cmd_expire
, "expire tag..." },
1816 { "delete", cmd_delete
, "delete tag..." },
1817 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1820 -r, --retag Untag any key currently called new-tag.\n\
1822 { "setattr", cmd_setattr
, "setattr tag attr..." },
1823 { "comment", cmd_comment
, "comment tag [comment]" },
1824 { "lock", cmd_lock
, "lock qtag" },
1825 { "unlock", cmd_unlock
, "unlock qtag" },
1826 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1829 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1830 -q, --quiet Show less information.\n\
1831 -v, --verbose Show more information.\n\
1833 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1836 -f, --filter=FILT Only hash key components matching FILT.\n\
1837 -a, --algorithm=HASH Use the named HASH algorithm.\n\
1839 { "tidy", cmd_tidy
, "tidy" },
1840 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1843 -f, --filter=FILT Only extract key components matching FILT.\n\
1845 { "merge", cmd_merge
, "merge file" },
1849 typedef struct cmd cmd
;
1851 /*----- Main code ---------------------------------------------------------*/
1853 /* --- @findcmd@ --- *
1855 * Arguments: @const char *name@ = a command name
1857 * Returns: Pointer to the command structure.
1859 * Use: Looks up a command by name. If the command isn't found, an
1860 * error is reported and the program is terminated.
1863 static cmd
*findcmd(const char *name
)
1865 cmd
*c
, *chosen
= 0;
1866 size_t sz
= strlen(name
);
1868 for (c
= cmds
; c
->name
; c
++) {
1869 if (strncmp(name
, c
->name
, sz
) == 0) {
1870 if (c
->name
[sz
] == 0) {
1874 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1880 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1884 /* --- Helpful GNUy functions --- */
1886 void usage(FILE *fp
)
1888 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1891 void version(FILE *fp
)
1893 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1896 void help(FILE *fp
, char **argv
)
1904 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1912 Performs various simple key management operations. Command line options\n\
1915 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1916 -v, --version Display version number.\n\
1917 -u, --usage Display short usage summary.\n\
1919 -k, --keyring=FILE Read and write keys in FILE.\n\
1920 -i, --id=TAG Use key TAG for random number generator.\n\
1921 -t, --type=TYPE Use key TYPE for random number generator.\n\
1923 The following commands are understood:\n\n",
1925 for (c
= cmds
; c
->name
; c
++)
1926 fprintf(fp
, "%s\n", c
->usage
);
1932 * Arguments: @int argc@ = number of command line arguments
1933 * @char *argv[]@ = array of command line arguments
1935 * Returns: Nonzero on failure.
1937 * Use: Main program. Performs simple key management functions.
1940 int main(int argc
, char *argv
[])
1946 /* --- Initialization --- */
1951 /* --- Parse command line options --- */
1954 static struct option opt
[] = {
1956 /* --- Standard GNUy help options --- */
1958 { "help", 0, 0, 'h' },
1959 { "version", 0, 0, 'v' },
1960 { "usage", 0, 0, 'u' },
1962 /* --- Real live useful options --- */
1964 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1966 /* --- Magic terminator --- */
1970 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1976 /* --- GNU help options --- */
1979 help(stdout
, argv
+ optind
);
1988 /* --- Real useful options --- */
1994 /* --- Bogosity --- */
2002 /* --- Complain about excessive bogons --- */
2004 if (f
& f_bogus
|| optind
== argc
) {
2009 /* --- Initialize the Catacomb random number generator --- */
2011 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2012 rand_seed(RAND_GLOBAL
, 160);
2014 /* --- Dispatch to appropriate command handler --- */
2019 return (findcmd(argv
[0])->cmd(argc
, argv
));
2022 /*----- That's all, folks -------------------------------------------------*/