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>
69 #include "sha256-mgf.h"
70 #include "sha224-mgf.h"
71 #include "sha384-mgf.h"
72 #include "sha512-mgf.h"
73 #include "tiger-mgf.h"
74 #include "rmd128-mgf.h"
75 #include "rmd160-mgf.h"
76 #include "rmd256-mgf.h"
77 #include "rmd320-mgf.h"
81 /*----- Handy global state ------------------------------------------------*/
83 static const char *keyfile
= "keyring";
85 /*----- Useful shared functions -------------------------------------------*/
89 * Arguments: @key_file *f@ = pointer to key file block
90 * @unsigned how@ = method to open file with
94 * Use: Opens a key file and handles errors by panicking
98 static void doopen(key_file
*f
, unsigned how
)
100 if (key_open(f
, keyfile
, how
, key_moan
, 0))
101 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
104 /* --- @doclose@ --- *
106 * Arguments: @key_file *f@ = pointer to key file block
110 * Use: Closes a key file and handles errors by panicking
114 static void doclose(key_file
*f
)
116 switch (key_close(f
)) {
118 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
119 keyfile
, strerror(errno
));
121 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
122 keyfile
, strerror(errno
));
126 /* --- @setattr@ --- *
128 * Arguments: @key_file *f@ = pointer to key file block
129 * @key *k@ = pointer to key block
130 * @char *v[]@ = array of assignments (overwritten!)
134 * Use: Applies the attribute assignments to the key.
137 static void setattr(key_file
*f
, key
*k
, char *v
[])
142 size_t eq
= strcspn(p
, "=");
144 moan("invalid assignment: `%s' (ignored)", p
);
150 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
151 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
156 /*----- Seeding -----------------------------------------------------------*/
158 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
160 { "dsarand", dsarand_create
},
161 { "rmd128-mgf", rmd128_mgfrand
},
162 { "rmd160-mgf", rmd160_mgfrand
},
163 { "rmd256-mgf", rmd256_mgfrand
},
164 { "rmd320-mgf", rmd320_mgfrand
},
165 { "sha-mgf", sha_mgfrand
},
166 { "sha224-mgf", sha224_mgfrand
},
167 { "sha256-mgf", sha256_mgfrand
},
168 { "sha384-mgf", sha384_mgfrand
},
169 { "sha512-mgf", sha512_mgfrand
},
170 { "tiger-mgf", tiger_mgfrand
},
174 #define SEEDALG_DEFAULT (seedtab + 2)
176 /*----- Key generation ----------------------------------------------------*/
178 /* --- Key generation parameters --- */
180 typedef struct keyopts
{
181 key_file
*kf
; /* Pointer to key file */
182 key
*k
; /* Pointer to the actual key */
183 dstr tag
; /* Full tag name for the key */
184 unsigned f
; /* Flags for the new key */
185 unsigned bits
, qbits
; /* Bit length for the new key */
186 const char *curve
; /* Elliptic curve name/info */
187 grand
*r
; /* Random number source */
188 key
*p
; /* Parameters key-data */
191 #define f_bogus 1u /* Error in parsing */
192 #define f_lock 2u /* Passphrase-lock private key */
193 #define f_quiet 4u /* Don't show a progress indicator */
194 #define f_limlee 8u /* Generate Lim-Lee primes */
195 #define f_subgroup 16u /* Generate a subgroup */
196 #define f_retag 32u /* Remove any existing tag */
198 /* --- @dolock@ --- *
200 * Arguments: @keyopts *k@ = key generation options
201 * @key_data *kd@ = pointer to key data to lock
202 * @const char *t@ = tag suffix or null
206 * Use: Does passphrase locking on new keys.
209 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
211 if (!(k
->f
& f_lock
))
214 dstr_putf(&k
->tag
, ".%s", t
);
215 if (key_plock(k
->tag
.buf
, kd
, kd
))
216 die(EXIT_FAILURE
, "couldn't lock key");
221 * Arguments: @key_data *kd@ = pointer to parent key block
222 * @const char *tag@ = pointer to tag string
223 * @mp *m@ = integer to store
224 * @unsigned f@ = flags to set
228 * Use: Sets a multiprecision integer subkey.
231 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
233 key_data
*kkd
= key_structcreate(kd
, tag
);
238 /* --- @copyparam@ --- *
240 * Arguments: @keyopts *k@ = pointer to key options
241 * @const char **pp@ = checklist of parameters
243 * Returns: Nonzero if parameters copied; zero if you have to generate
246 * Use: Copies parameters from a source key to the current one.
249 static int copyparam(keyopts
*k
, const char **pp
)
255 /* --- Quick check if no parameters supplied --- */
260 /* --- Run through the checklist --- */
263 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
265 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
266 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
267 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
271 /* --- Copy over the parameters --- */
275 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
276 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
278 /* --- Copy over attributes --- */
280 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
281 key_putattr(k
->kf
, k
->k
, n
, v
);
290 * Arguments: @key_data *k@ = pointer to key data block
291 * @const char *tag@ = tag string to use
293 * Returns: Pointer to multiprecision integer key item.
295 * Use: Fetches an MP key component.
298 static mp
*getmp(key_data
*k
, const char *tag
)
300 k
= key_structfind(k
, tag
);
302 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
303 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
304 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
308 /* --- @keyrand@ --- *
310 * Arguments: @key_file *kf@ = pointer to key file
311 * @const char *id@ = pointer to key id (or null)
315 * Use: Keys the random number generator.
318 static void keyrand(key_file
*kf
, const char *id
)
322 /* --- Find the key --- */
325 if ((k
= key_bytag(kf
, id
)) == 0)
326 die(EXIT_FAILURE
, "key `%s' not found", id
);
328 k
= key_bytype(kf
, "catacomb-rand");
331 key_data
*kd
= &k
->k
, kkd
;
334 switch (kd
->e
& KF_ENCMASK
) {
340 if (key_punlock(d
.buf
, kd
, &kkd
))
341 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
348 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
352 /* --- Key the generator --- */
354 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
360 /* --- Key generation algorithms --- */
362 static void alg_binary(keyopts
*k
)
371 die(EXIT_FAILURE
, "no shared parameters for binary keys");
373 sz
= (k
->bits
+ 7) >> 3;
375 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
376 k
->r
->ops
->fill(k
->r
, p
, sz
);
378 key_binary(&k
->k
->k
, p
, sz
);
379 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
382 dolock(k
, &k
->k
->k
, 0);
385 static void alg_des(keyopts
*k
)
394 die(EXIT_FAILURE
, "no shared parameters for DES keys");
395 if (k
->bits
% 56 || k
->bits
> 168)
396 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
400 k
->r
->ops
->fill(k
->r
, p
, sz
);
401 for (i
= 0; i
< sz
; i
++) {
402 octet x
= p
[i
] | 0x01;
406 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
408 key_binary(&k
->k
->k
, p
, sz
);
409 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
412 dolock(k
, &k
->k
->k
, 0);
415 static void alg_rsa(keyopts
*k
)
420 /* --- Sanity checking --- */
423 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
427 /* --- Generate the RSA parameters --- */
429 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
430 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
431 die(EXIT_FAILURE
, "RSA key generation failed");
433 /* --- Run a test encryption --- */
436 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
438 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
443 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
444 c
= rsa_qprivop(&rp
, c
, c
, g
);
447 die(EXIT_FAILURE
, "test encryption failed");
453 /* --- Allrighty then --- */
457 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
458 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
460 kd
= key_structcreate(kd
, "private");
462 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
463 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
464 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
465 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
466 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
467 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
468 dolock(k
, kd
, "private");
473 static void alg_dsaparam(keyopts
*k
)
475 static const char *pl
[] = { "q", "p", "g", 0 };
476 if (!copyparam(k
, pl
)) {
482 key_data
*kd
= &k
->k
->k
;
485 /* --- Choose appropriate bit lengths if necessary --- */
492 /* --- Allocate a seed block --- */
494 sz
= (k
->qbits
+ 7) >> 3;
496 k
->r
->ops
->fill(k
->r
, p
, sz
);
498 /* --- Allocate the parameters --- */
500 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
501 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
502 die(EXIT_FAILURE
, "DSA parameter generation failed");
504 /* --- Store the parameters --- */
507 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
508 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
509 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
514 /* --- Store the seed for future verification --- */
519 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
520 base64_encode(&c
, 0, 0, &d
);
521 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
523 dstr_putf(&d
, "%u", ds
.count
);
524 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
531 static void alg_dsa(keyopts
*k
)
536 key_data
*kd
= &k
->k
->k
;
538 /* --- Get the shared parameters --- */
545 /* --- Choose a private key --- */
547 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
548 mpmont_create(&mm
, p
);
549 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
551 /* --- Store everything away --- */
553 mpkey(kd
, "y", y
, KCAT_PUB
);
555 kd
= key_structcreate(kd
, "private");
557 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
558 dolock(k
, kd
, "private");
560 mp_drop(x
); mp_drop(y
);
563 static void alg_dhparam(keyopts
*k
)
565 static const char *pl
[] = { "p", "q", "g", 0 };
566 key_data
*kd
= &k
->k
->k
;
567 if (!copyparam(k
, pl
)) {
574 if (strcmp(k
->curve
, "list") == 0) {
576 LIST("Built-in prime groups", stdout
, ptab
[i
].name
, ptab
[i
].name
);
580 if (dh_parse(&qd
, &dp
))
581 die(EXIT_FAILURE
, "error in group spec: %s", qd
.e
);
588 /* --- Choose a large safe prime number --- */
590 if (k
->f
& f_limlee
) {
595 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
596 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
597 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
598 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
602 for (i
= 0; i
< nf
; i
++) {
605 mp_writedstr(f
[i
], &d
, 10);
608 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
612 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
613 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
616 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
620 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
621 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
622 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
629 static void alg_dh(keyopts
*k
)
634 key_data
*kd
= &k
->k
->k
;
636 /* --- Get the shared parameters --- */
643 /* --- Choose a suitable private key --- *
645 * Since %$g$% has order %$q$%, choose %$x < q$%.
648 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
650 /* --- Compute the public key %$y = g^x \bmod p$% --- */
652 mpmont_create(&mm
, p
);
653 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
656 /* --- Store everything away --- */
658 mpkey(kd
, "y", y
, KCAT_PUB
);
660 kd
= key_structcreate(kd
, "private");
662 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
663 dolock(k
, kd
, "private");
665 mp_drop(x
); mp_drop(y
);
668 static void alg_bbs(keyopts
*k
)
673 /* --- Sanity checking --- */
676 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
680 /* --- Generate the BBS parameters --- */
682 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
683 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
684 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
686 /* --- Allrighty then --- */
690 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
692 kd
= key_structcreate(kd
, "private");
694 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
695 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
696 dolock(k
, kd
, "private");
701 static void alg_ecparam(keyopts
*k
)
703 static const char *pl
[] = { "curve", 0 };
704 if (!copyparam(k
, pl
)) {
707 key_data
*kd
= &k
->k
->k
;
709 /* --- Decide on a curve --- */
711 if (!k
->bits
) k
->bits
= 256;
712 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
714 LIST("Built-in elliptic curves", stdout
,
715 ectab
[i
].name
, ectab
[i
].name
);
719 if (k
->bits
<= 56) k
->curve
= "secp112r1";
720 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
721 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
722 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
723 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
724 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
725 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
726 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
728 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
732 /* --- Check it --- */
734 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
735 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
736 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
737 moan("WARNING! curve check failed: %s", e
);
740 /* --- Write out the answer --- */
743 kd
= key_structcreate(kd
, "curve");
744 key_string(kd
, k
->curve
);
749 static void alg_ec(keyopts
*k
)
751 key_data
*kd
= &k
->k
->k
;
758 /* --- Get the curve --- */
761 if ((kkd
= key_structfind(kd
, "curve")) == 0)
762 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
763 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
764 die(EXIT_FAILURE
, "subkey `curve' is not a string");
765 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
766 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
768 /* --- Invent a private exponent and compute the public key --- */
770 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
771 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
773 /* --- Store everything away --- */
775 kkd
= key_structcreate(kd
, "p");
778 kkd
= key_structcreate(kd
, "private");
780 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
788 /* --- The algorithm tables --- */
790 typedef struct keyalg
{
792 void (*proc
)(keyopts
*o
);
796 static keyalg algtab
[] = {
797 { "binary", alg_binary
, "Plain binary data" },
798 { "des", alg_des
, "Binary with DES-style parity" },
799 { "rsa", alg_rsa
, "RSA public-key encryption" },
800 { "dsa", alg_dsa
, "DSA digital signatures" },
801 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
802 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
803 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
804 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
805 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
806 { "ec", alg_ec
, "Elliptic curve crypto" },
810 /* --- @cmd_add@ --- */
812 static int cmd_add(int argc
, char *argv
[])
815 time_t exp
= KEXP_EXPIRE
;
816 const char *tag
= 0, *ptag
= 0;
818 keyalg
*alg
= algtab
;
819 const char *rtag
= 0;
820 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
821 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
822 const char *seed
= 0;
825 /* --- Parse options for the subcommand --- */
828 static struct option opt
[] = {
829 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
830 { "bits", OPTF_ARGREQ
, 0, 'b' },
831 { "qbits", OPTF_ARGREQ
, 0, 'B' },
832 { "parameters", OPTF_ARGREQ
, 0, 'p' },
833 { "expire", OPTF_ARGREQ
, 0, 'e' },
834 { "comment", OPTF_ARGREQ
, 0, 'c' },
835 { "tag", OPTF_ARGREQ
, 0, 't' },
836 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
837 { "curve", OPTF_ARGREQ
, 0, 'C' },
838 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
839 { "seed", OPTF_ARGREQ
, 0, 's' },
840 { "newseed", OPTF_ARGREQ
, 0, 'n' },
841 { "lock", 0, 0, 'l' },
842 { "quiet", 0, 0, 'q' },
843 { "lim-lee", 0, 0, 'L' },
844 { "subgroup", 0, 0, 'S' },
847 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
852 /* --- Handle the various options --- */
856 /* --- Read an algorithm name --- */
860 size_t sz
= strlen(optarg
);
862 if (strcmp(optarg
, "list") == 0) {
863 for (a
= algtab
; a
->name
; a
++)
864 printf("%-10s %s\n", a
->name
, a
->help
);
869 for (a
= algtab
; a
->name
; a
++) {
870 if (strncmp(optarg
, a
->name
, sz
) == 0) {
871 if (a
->name
[sz
] == 0) {
875 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
881 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
884 /* --- Bits must be nonzero and a multiple of 8 --- */
888 k
.bits
= strtoul(optarg
, &p
, 0);
889 if (k
.bits
== 0 || *p
!= 0)
890 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
895 k
.qbits
= strtoul(optarg
, &p
, 0);
896 if (k
.qbits
== 0 || *p
!= 0)
897 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
900 /* --- Parameter selection --- */
906 /* --- Expiry dates get passed to @get_date@ for parsing --- */
909 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
912 exp
= get_date(optarg
, 0);
914 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
918 /* --- Store comments without interpretation --- */
921 if (key_chkcomment(optarg
))
922 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
926 /* --- Elliptic curve parameters --- */
932 /* --- Store tags --- */
935 if (key_chkident(optarg
))
936 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
943 /* --- Seeding --- */
946 const struct seedalg
*ss
;
947 if (strcmp(optarg
, "list") == 0) {
948 printf("Seed algorithms:\n");
949 for (ss
= seedtab
; ss
->p
; ss
++)
950 printf(" %s\n", ss
->p
);
953 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
955 for (ss
= seedtab
; ss
->p
; ss
++) {
956 if (strcmp(optarg
, ss
->p
) == 0)
960 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
966 if (seed
) die(EXIT_FAILURE
, "seed already set");
968 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
969 base64_decode(&b
, 0, 0, &d
);
970 k
.r
= sa
->gen(d
.buf
, d
.len
);
979 unsigned n
= strtoul(optarg
, &p
, 0);
980 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
981 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
982 if (seed
) die(EXIT_FAILURE
, "seed already set");
985 rand_get(RAND_GLOBAL
, p
, n
);
987 base64_encode(&b
, p
, n
, &d
);
988 base64_encode(&b
, 0, 0, &d
);
993 /* --- Other flags --- */
1011 /* --- Other things are bogus --- */
1019 /* --- Various sorts of bogosity --- */
1021 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1023 "Usage: add [OPTIONS] TYPE [ATTR...]");
1025 if (key_chkident(argv
[optind
]))
1026 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1028 /* --- Set up various bits of the state --- */
1030 if (exp
== KEXP_EXPIRE
)
1031 exp
= time(0) + 14 * 24 * 60 * 60;
1033 /* --- Open the file and create the basic key block --- *
1035 * Keep on generating keyids until one of them doesn't collide.
1038 doopen(&f
, KOPEN_WRITE
);
1041 /* --- Key the generator --- */
1046 uint32 id
= rand_global
.ops
->word(&rand_global
);
1048 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1051 if (err
!= KERR_DUPID
)
1052 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1055 /* --- Set various simple attributes --- */
1060 if (k
.f
& f_retag
) {
1061 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1062 key_settag(&f
, kk
, 0);
1064 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1065 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1069 int err
= key_setcomment(&f
, k
.k
, c
);
1071 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1074 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1076 key_putattr(&f
, k
.k
, "genseed", seed
);
1077 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1080 key_fulltag(k
.k
, &k
.tag
);
1082 /* --- Find the parameter key --- */
1085 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1086 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1087 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1088 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1091 /* --- Now generate the actual key data --- */
1101 /*----- Key listing -------------------------------------------------------*/
1103 /* --- Listing options --- */
1105 typedef struct listopts
{
1106 const char *tfmt
; /* Date format (@strftime@-style) */
1107 int v
; /* Verbosity level */
1108 unsigned f
; /* Various flags */
1109 time_t t
; /* Time now (for key expiry) */
1110 key_filter kf
; /* Filter for matching keys */
1113 /* --- Listing flags --- */
1115 #define f_newline 2u /* Write newline before next entry */
1116 #define f_attr 4u /* Written at least one attribute */
1117 #define f_utc 8u /* Emit UTC time, not local time */
1119 /* --- @showkeydata@ --- *
1121 * Arguments: @key_data *k@ = pointer to key to write
1122 * @int ind@ = indentation level
1123 * @listopts *o@ = listing options
1124 * @dstr *d@ = tag string for this subkey
1128 * Use: Emits a piece of key data in a human-readable format.
1131 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1133 #define INDENT(i) do { \
1135 for (_i = 0; _i < (i); _i++) { \
1140 switch (k
->e
& KF_ENCMASK
) {
1142 /* --- Binary key data --- *
1144 * Emit as a simple hex dump.
1148 const octet
*p
= k
->u
.k
.k
;
1149 const octet
*l
= p
+ k
->u
.k
.sz
;
1152 fputs(" {", stdout
);
1157 } else if (sz
% 8 == 0)
1161 printf("%02x", *p
++);
1166 fputs("}\n", stdout
);
1169 /* --- Encrypted data --- *
1171 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1172 * key. Otherwise just say that it's encrypted and move on.
1177 fputs(" encrypted\n", stdout
);
1180 if (key_punlock(d
->buf
, k
, &kd
))
1181 printf(" <failed to unlock %s>\n", d
->buf
);
1183 fputs(" encrypted", stdout
);
1184 showkeydata(&kd
, ind
, o
, d
);
1190 /* --- Integer keys --- *
1192 * Emit as a large integer in decimal. This makes using the key in
1193 * `calc' or whatever easier.
1198 mp_writefile(k
->u
.m
, stdout
, 10);
1202 /* --- Strings --- */
1205 printf(" `%s'\n", k
->u
.p
);
1208 /* --- Elliptic curve points --- */
1211 if (EC_ATINF(&k
->u
.e
))
1212 fputs(" inf\n", stdout
);
1214 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1215 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1220 /* --- Structured keys --- *
1222 * Just iterate over the subkeys.
1230 fputs(" {\n", stdout
);
1231 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1232 if (!key_match(&ks
->k
, &o
->kf
))
1235 printf("%s =", SYM_NAME(ks
));
1237 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1238 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1241 fputs("}\n", stdout
);
1248 /* --- @showkey@ --- *
1250 * Arguments: @key *k@ = pointer to key to show
1251 * @listopts *o@ = pointer to listing options
1255 * Use: Emits a listing of a particular key.
1258 static void showkey(key
*k
, listopts
*o
)
1260 char ebuf
[24], dbuf
[24];
1263 /* --- Skip the key if the filter doesn't match --- */
1265 if (!key_match(&k
->k
, &o
->kf
))
1268 /* --- Sort out the expiry and deletion times --- */
1270 if (KEY_EXPIRED(o
->t
, k
->exp
))
1271 strcpy(ebuf
, "expired");
1272 else if (k
->exp
== KEXP_FOREVER
)
1273 strcpy(ebuf
, "forever");
1275 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1276 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1279 if (KEY_EXPIRED(o
->t
, k
->del
))
1280 strcpy(dbuf
, "deleted");
1281 else if (k
->del
== KEXP_FOREVER
)
1282 strcpy(dbuf
, "forever");
1284 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1285 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1288 /* --- If in compact format, just display and quit --- */
1291 if (!(o
->f
& f_newline
)) {
1292 printf("%8s %-20s %-20s %-10s %-10s\n",
1293 "Id", "Tag", "Type", "Expire", "Delete");
1295 printf("%08lx %-20s %-20s %-10s %-10s\n",
1296 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1297 k
->type
, ebuf
, dbuf
);
1302 /* --- Display the standard header --- */
1304 if (o
->f
& f_newline
)
1305 fputc('\n', stdout
);
1306 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1307 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1308 printf("type: %s\n", k
->type
);
1309 printf("expiry: %s\n", ebuf
);
1310 printf("delete: %s\n", dbuf
);
1311 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1313 /* --- Display the attributes --- */
1317 const char *av
, *an
;
1320 printf("attributes:");
1321 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1322 printf("\n %s = %s", an
, av
);
1326 fputc('\n', stdout
);
1331 /* --- If dumping requested, dump the raw key data --- */
1335 fputs("key:", stdout
);
1337 showkeydata(&k
->k
, 0, o
, &d
);
1344 /* --- @cmd_list@ --- */
1346 static int cmd_list(int argc
, char *argv
[])
1350 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1352 /* --- Parse subcommand options --- */
1355 static struct option opt
[] = {
1356 { "quiet", 0, 0, 'q' },
1357 { "verbose", 0, 0, 'v' },
1358 { "utc", 0, 0, 'u' },
1359 { "filter", OPTF_ARGREQ
, 0, 'f' },
1362 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1379 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1381 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1390 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1392 /* --- Open the key file --- */
1394 doopen(&f
, KOPEN_READ
);
1397 /* --- Set up the time format --- */
1400 o
.tfmt
= "%Y-%m-%d";
1401 else if (o
.f
& f_utc
)
1402 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1404 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1406 /* --- If specific keys were requested use them, otherwise do all --- *
1408 * Some day, this might turn into a wildcard match.
1411 if (optind
< argc
) {
1413 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1416 moan("key `%s' not found", argv
[optind
]);
1420 } while (optind
< argc
);
1423 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1431 return (EXIT_FAILURE
);
1436 /*----- Command implementation --------------------------------------------*/
1438 /* --- @cmd_expire@ --- */
1440 static int cmd_expire(int argc
, char *argv
[])
1448 die(EXIT_FAILURE
, "Usage: expire TAG...");
1449 doopen(&f
, KOPEN_WRITE
);
1450 for (i
= 1; i
< argc
; i
++) {
1451 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1454 moan("key `%s' not found", argv
[i
]);
1462 /* --- @cmd_delete@ --- */
1464 static int cmd_delete(int argc
, char *argv
[])
1472 die(EXIT_FAILURE
, "Usage: delete TAG...");
1473 doopen(&f
, KOPEN_WRITE
);
1474 for (i
= 1; i
< argc
; i
++) {
1475 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1478 moan("key `%s' not found", argv
[i
]);
1486 /* --- @cmd_setattr@ --- */
1488 static int cmd_setattr(int argc
, char *argv
[])
1494 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1495 doopen(&f
, KOPEN_WRITE
);
1496 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1497 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1498 setattr(&f
, k
, argv
+ 2);
1503 /* --- @cmd_finger@ --- */
1505 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1513 if (key_fingerprint(k
, h
, kf
)) {
1516 for (i
= 0; i
< ch
->hashsz
; i
++) {
1517 if (i
&& i
% 4 == 0)
1519 printf("%02x", p
[i
]);
1521 printf(" %s\n", d
.buf
);
1527 static int cmd_finger(int argc
, char *argv
[])
1531 const gchash
*ch
= &rmd160
;
1532 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1535 static struct option opt
[] = {
1536 { "filter", OPTF_ARGREQ
, 0, 'f' },
1537 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1540 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1546 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1548 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1551 if ((ch
= ghash_byname(optarg
)) == 0)
1552 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1560 argv
+= optind
; argc
-= optind
;
1562 die(EXIT_FAILURE
, "Usage: fingerprint [-f FILTER] [TAG...]");
1564 doopen(&f
, KOPEN_READ
);
1568 for (i
= 0; i
< argc
; i
++) {
1569 key
*k
= key_bytag(&f
, argv
[i
]);
1571 fingerprint(k
, ch
, &kf
);
1574 moan("key `%s' not found", argv
[i
]);
1580 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1581 fingerprint(k
, ch
, &kf
);
1586 /* --- @cmd_comment@ --- */
1588 static int cmd_comment(int argc
, char *argv
[])
1594 if (argc
< 2 || argc
> 3)
1595 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1596 doopen(&f
, KOPEN_WRITE
);
1597 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1598 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1599 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1600 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1605 /* --- @cmd_tag@ --- */
1607 static int cmd_tag(int argc
, char *argv
[])
1616 static struct option opt
[] = {
1617 { "retag", 0, 0, 'r' },
1620 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1633 argv
+= optind
; argc
-= optind
;
1634 if (argc
< 1 || argc
> 2 || rc
)
1635 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1636 doopen(&f
, KOPEN_WRITE
);
1637 if (flags
& f_retag
) {
1638 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1639 key_settag(&f
, k
, 0);
1641 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1642 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1643 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1644 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1649 /* --- @cmd_lock@ --- */
1651 static int cmd_lock(int argc
, char *argv
[])
1659 die(EXIT_FAILURE
, "Usage: lock QTAG");
1660 doopen(&f
, KOPEN_WRITE
);
1661 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1662 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1663 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1664 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1665 if (key_plock(d
.buf
, kd
, kd
))
1666 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1672 /* --- @cmd_unlock@ --- */
1674 static int cmd_unlock(int argc
, char *argv
[])
1682 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1683 doopen(&f
, KOPEN_WRITE
);
1684 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1685 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1686 if (kd
->e
!= KENC_ENCRYPT
)
1687 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1688 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1689 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1695 /* --- @cmd_extract@ --- */
1697 static int cmd_extract(int argc
, char *argv
[])
1703 key_filter kf
= { 0, 0 };
1707 static struct option opt
[] = {
1708 { "filter", OPTF_ARGREQ
, 0, 'f' },
1711 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1717 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1719 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1727 argv
+= optind
; argc
-= optind
;
1729 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
1730 if (strcmp(*argv
, "-") == 0)
1732 else if (!(fp
= fopen(*argv
, "w"))) {
1733 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1734 *argv
, strerror(errno
));
1737 doopen(&f
, KOPEN_READ
);
1741 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1742 key_extract(&f
, k
, fp
, &kf
);
1744 for (i
= 1; i
< argc
; i
++) {
1745 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1746 key_extract(&f
, k
, fp
, &kf
);
1748 moan("key `%s' not found", argv
[i
]);
1754 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1759 /* --- @cmd_tidy@ --- */
1761 static int cmd_tidy(int argc
, char *argv
[])
1765 die(EXIT_FAILURE
, "Usage: tidy");
1766 doopen(&f
, KOPEN_WRITE
);
1767 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1772 /* --- @cmd_merge@ --- */
1774 static int cmd_merge(int argc
, char *argv
[])
1780 die(EXIT_FAILURE
, "Usage: merge FILE");
1781 if (strcmp(argv
[1], "-") == 0)
1783 else if (!(fp
= fopen(argv
[1], "r"))) {
1784 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1785 argv
[1], strerror(errno
));
1788 doopen(&f
, KOPEN_WRITE
);
1789 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1794 /* --- @cmd_show@ --- */
1798 listtab[i].name, listtab[i].name) \
1799 LI("Hash functions", hash, \
1800 ghashtab[i], ghashtab[i]->name) \
1801 LI("Elliptic curves", ec, \
1802 ectab[i].name, ectab[i].name) \
1803 LI("Diffie-Hellman groups", dh, \
1804 ptab[i].name, ptab[i].name) \
1805 LI("Key-generation algorithms", keygen, \
1806 algtab[i].name, algtab[i].name) \
1807 LI("Random seeding algorithms", seed, \
1808 seedtab[i].p, seedtab[i].p)
1810 MAKELISTTAB(listtab
, LISTS
)
1812 static int cmd_show(int argc
, char *argv
[])
1814 return (displaylists(listtab
, argv
+ 1));
1817 /*----- Main command table ------------------------------------------------*/
1819 static int cmd_help(int argc
, char *argv
[]);
1821 static cmd cmds
[] = {
1822 { "help", cmd_help
, "help [COMMAND...]" },
1823 { "show", cmd_show
, "show [ITEM...]" },
1824 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
1827 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1828 -q, --quiet Show less information.\n\
1829 -v, --verbose Show more information.\n\
1831 { "fingerprint", cmd_finger
, "fingerprint [-f FILTER] [TAG...]", "\
1834 -f, --filter=FILT Only hash key components matching FILT.\n\
1835 -a, --algorithm=HASH Use the named HASH algorithm.\n\
1836 ($ show hash for list.)\n\
1838 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
1841 -f, --filter=FILT Only extract key components matching FILT.\n\
1843 { "merge", cmd_merge
, "merge FILE" },
1844 { "expire", cmd_expire
, "expire TAG..." },
1845 { "delete", cmd_delete
, "delete TAG..." },
1846 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
1847 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
1848 { "lock", cmd_lock
, "lock QTAG" },
1849 { "unlock", cmd_unlock
, "unlock QTAG" },
1850 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
1853 -r, --retag Untag any key currently called new-tag.\n\
1855 { "tidy", cmd_tidy
, "tidy" },
1857 "add [-OPTIONS] TYPE [ATTR...]\n\
1858 Options: [-lqrLS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
1859 [-A SEEDALG] [-s SEED] [-n BITS]\n\
1860 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
1863 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1864 ($ show keygen for list.)\n\
1865 -b, --bits=N Generate an N-bit key.\n\
1866 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1867 -p, --parameters=TAG Get group parameters from TAG.\n\
1868 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
1869 ($ show ec or $ show dh for list.)\n\
1870 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
1871 ($ show seed for list.)\n\
1872 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
1873 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
1874 -e, --expire=TIME Make the key expire after TIME.\n\
1875 -c, --comment=STRING Attach the command STRING to the key.\n\
1876 -t, --tag=TAG Tag the key with the name TAG.\n\
1877 -r, --retag Untag any key currently with that tag.\n\
1878 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1879 -l, --lock Lock the generated key with a passphrase.\n\
1880 -q, --quiet Don't give progress indicators while working.\n\
1881 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1882 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1887 static int cmd_help(int argc
, char *argv
[])
1889 sc_help(cmds
, stdout
, argv
+ 1);
1893 /*----- Main code ---------------------------------------------------------*/
1895 /* --- Helpful GNUy functions --- */
1897 static void usage(FILE *fp
)
1899 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
1902 void version(FILE *fp
)
1904 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1907 void help_global(FILE *fp
)
1911 Performs various simple key management operations.\n\
1913 Global command line options:\n\
1915 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\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",
1925 * Arguments: @int argc@ = number of command line arguments
1926 * @char *argv[]@ = array of command line arguments
1928 * Returns: Nonzero on failure.
1930 * Use: Main program. Performs simple key management functions.
1933 int main(int argc
, char *argv
[])
1939 /* --- Initialization --- */
1944 /* --- Parse command line options --- */
1947 static struct option opt
[] = {
1949 /* --- Standard GNUy help options --- */
1951 { "help", 0, 0, 'h' },
1952 { "version", 0, 0, 'v' },
1953 { "usage", 0, 0, 'u' },
1955 /* --- Real live useful options --- */
1957 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1959 /* --- Magic terminator --- */
1963 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1969 /* --- GNU help options --- */
1972 sc_help(cmds
, stdout
, argv
+ optind
);
1981 /* --- Real useful options --- */
1987 /* --- Bogosity --- */
1995 /* --- Complain about excessive bogons --- */
1997 if (f
& f_bogus
|| optind
== argc
) {
2002 /* --- Initialize the Catacomb random number generator --- */
2004 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2005 rand_seed(RAND_GLOBAL
, 160);
2007 /* --- Dispatch to appropriate command handler --- */
2012 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2015 /*----- That's all, folks -------------------------------------------------*/