3 * Simple key manager program
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
41 #include <mLib/codec.h>
42 #include <mLib/base32.h>
43 #include <mLib/base64.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
75 #include "sha256-mgf.h"
76 #include "sha224-mgf.h"
77 #include "sha384-mgf.h"
78 #include "sha512-mgf.h"
79 #include "tiger-mgf.h"
80 #include "rmd128-mgf.h"
81 #include "rmd160-mgf.h"
82 #include "rmd256-mgf.h"
83 #include "rmd320-mgf.h"
87 /*----- Handy global state ------------------------------------------------*/
89 static const char *keyfile
= "keyring";
91 /*----- Useful shared functions -------------------------------------------*/
95 * Arguments: @key_file *f@ = pointer to key file block
96 * @unsigned how@ = method to open file with
100 * Use: Opens a key file and handles errors by panicking
104 static void doopen(key_file
*f
, unsigned how
)
106 if (key_open(f
, keyfile
, how
, key_moan
, 0)){
107 die(EXIT_FAILURE
, "couldn't open keyring `%s': %s",
108 keyfile
, strerror(errno
));
112 /* --- @doclose@ --- *
114 * Arguments: @key_file *f@ = pointer to key file block
118 * Use: Closes a key file and handles errors by panicking
122 static void doclose(key_file
*f
)
124 switch (key_close(f
)) {
126 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
127 keyfile
, strerror(errno
));
129 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
130 keyfile
, strerror(errno
));
134 /* --- @setattr@ --- *
136 * Arguments: @key_file *f@ = pointer to key file block
137 * @key *k@ = pointer to key block
138 * @char *v[]@ = array of assignments (overwritten!)
142 * Use: Applies the attribute assignments to the key.
145 static void setattr(key_file
*f
, key
*k
, char *v
[])
150 size_t eq
= strcspn(p
, "=");
152 moan("invalid assignment: `%s' (ignored)", p
);
158 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
159 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
164 /*----- Seeding -----------------------------------------------------------*/
166 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
168 { "dsarand", dsarand_create
},
169 { "rmd128-mgf", rmd128_mgfrand
},
170 { "rmd160-mgf", rmd160_mgfrand
},
171 { "rmd256-mgf", rmd256_mgfrand
},
172 { "rmd320-mgf", rmd320_mgfrand
},
173 { "sha-mgf", sha_mgfrand
},
174 { "sha224-mgf", sha224_mgfrand
},
175 { "sha256-mgf", sha256_mgfrand
},
176 { "sha384-mgf", sha384_mgfrand
},
177 { "sha512-mgf", sha512_mgfrand
},
178 { "tiger-mgf", tiger_mgfrand
},
182 #define SEEDALG_DEFAULT (seedtab + 2)
184 /*----- Key generation ----------------------------------------------------*/
186 /* --- Key generation parameters --- */
188 typedef struct keyopts
{
189 key_file
*kf
; /* Pointer to key file */
190 key
*k
; /* Pointer to the actual key */
191 dstr tag
; /* Full tag name for the key */
192 unsigned f
; /* Flags for the new key */
193 unsigned bits
, qbits
; /* Bit length for the new key */
194 const char *curve
; /* Elliptic curve name/info */
195 grand
*r
; /* Random number source */
196 key
*p
; /* Parameters key-data */
199 #define f_bogus 1u /* Error in parsing */
200 #define f_lock 2u /* Passphrase-lock private key */
201 #define f_quiet 4u /* Don't show a progress indicator */
202 #define f_limlee 8u /* Generate Lim-Lee primes */
203 #define f_subgroup 16u /* Generate a subgroup */
204 #define f_retag 32u /* Remove any existing tag */
205 #define f_kcdsa 64u /* Generate KCDSA primes */
207 /* --- @dolock@ --- *
209 * Arguments: @keyopts *k@ = key generation options
210 * @key_data **kd@ = pointer to key data to lock
211 * @const char *t@ = tag suffix or null
215 * Use: Does passphrase locking on new keys.
218 static void dolock(keyopts
*k
, key_data
**kd
, const char *t
)
220 if (!(k
->f
& f_lock
))
223 dstr_putf(&k
->tag
, ".%s", t
);
224 if (key_plock(kd
, 0, k
->tag
.buf
))
225 die(EXIT_FAILURE
, "couldn't lock key");
228 /* --- @copyparam@ --- *
230 * Arguments: @keyopts *k@ = pointer to key options
231 * @const char **pp@ = checklist of parameters
233 * Returns: Nonzero if parameters copied; zero if you have to generate
236 * Use: Copies parameters from a source key to the current one.
239 static int copyparam(keyopts
*k
, const char **pp
)
250 /* --- Quick check if no parameters supplied --- */
255 /* --- Run through the checklist --- */
257 key_fulltag(k
->p
, &t
);
258 if ((k
->p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
259 die(EXIT_FAILURE
, "parameter key `%s' is not structured", t
.buf
);
261 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
264 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
266 if (!KEY_MATCH(kd
, &kf
)) {
268 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
273 /* --- Copy over the parameters --- */
275 kd
= key_copydata(k
->p
->k
, &kf
);
277 key_setkeydata(k
->kf
, k
->k
, kd
);
280 /* --- Copy over attributes --- */
282 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
283 key_putattr(k
->kf
, k
->k
, n
, v
);
293 * Arguments: @key_data *k@ = pointer to key data block
294 * @const char *tag@ = tag string to use
296 * Returns: Pointer to multiprecision integer key item.
298 * Use: Fetches an MP key component.
301 static mp
*getmp(key_data
*k
, const char *tag
)
303 k
= key_structfind(k
, tag
);
305 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
306 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
307 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
311 /* --- @keyrand@ --- *
313 * Arguments: @key_file *kf@ = pointer to key file
314 * @const char *id@ = pointer to key id (or null)
318 * Use: Keys the random number generator.
321 static void keyrand(key_file
*kf
, const char *id
)
325 /* --- Find the key --- */
328 if ((k
= key_bytag(kf
, id
)) == 0)
329 die(EXIT_FAILURE
, "key `%s' not found", id
);
331 k
= key_bytype(kf
, "catacomb-rand");
334 key_data
*kd
= k
->k
, *kkd
;
338 switch (kd
->e
& KF_ENCMASK
) {
344 if (key_punlock(&kkd
, kd
, d
.buf
))
345 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
353 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
357 /* --- Key the generator --- */
359 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
364 /* --- Key generation algorithms --- */
366 static void alg_binary(keyopts
*k
)
376 die(EXIT_FAILURE
, "no shared parameters for binary keys");
378 sz
= (k
->bits
+ 7) >> 3;
380 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
381 k
->r
->ops
->fill(k
->r
, p
, sz
);
383 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
386 key_setkeydata(k
->kf
, k
->k
, kd
);
391 static void alg_des(keyopts
*k
)
401 die(EXIT_FAILURE
, "no shared parameters for DES keys");
402 if (k
->bits
% 56 || k
->bits
> 168)
403 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
407 k
->r
->ops
->fill(k
->r
, p
, sz
);
408 for (i
= 0; i
< sz
; i
++) {
409 octet x
= p
[i
] | 0x01;
413 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
415 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
418 key_setkeydata(k
->kf
, k
->k
, kd
);
423 static void alg_rsa(keyopts
*k
)
428 /* --- Sanity checking --- */
431 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
435 /* --- Generate the RSA parameters --- */
437 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
438 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
439 die(EXIT_FAILURE
, "RSA key generation failed");
441 /* --- Run a test encryption --- */
444 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
446 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
451 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
452 c
= rsa_qprivop(&rp
, c
, c
, g
);
455 die(EXIT_FAILURE
, "test encryption failed");
461 /* --- Allrighty then --- */
463 kd
= key_newstruct();
464 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
465 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
467 kkd
= key_newstruct();
468 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
469 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
470 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
471 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
472 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
473 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
474 dolock(k
, &kkd
, "private");
475 key_structsteal(kd
, "private", kkd
);
476 key_setkeydata(k
->kf
, k
->k
, kd
);
481 static void alg_dsaparam(keyopts
*k
)
483 static const char *pl
[] = { "q", "p", "g", 0 };
484 if (!copyparam(k
, pl
)) {
493 /* --- Choose appropriate bit lengths if necessary --- */
500 /* --- Allocate a seed block --- */
502 sz
= (k
->qbits
+ 7) >> 3;
504 k
->r
->ops
->fill(k
->r
, p
, sz
);
506 /* --- Allocate the parameters --- */
508 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
509 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
510 die(EXIT_FAILURE
, "DSA parameter generation failed");
512 /* --- Store the parameters --- */
514 kd
= key_newstruct();
515 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
516 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
517 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
521 key_setkeydata(k
->kf
, k
->k
, kd
);
524 /* --- Store the seed for future verification --- */
526 c
= base64_class
.encoder(0, "", 0);
527 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
530 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
532 dstr_putf(&d
, "%u", ds
.count
);
533 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
540 static void alg_dsa(keyopts
*k
)
547 /* --- Get the shared parameters --- */
550 key_split(&k
->k
->k
); kd
= k
->k
->k
;
555 /* --- Choose a private key --- */
557 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
558 mpmont_create(&mm
, p
);
559 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
561 /* --- Store everything away --- */
563 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
565 kkd
= key_newstruct();
566 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
567 dolock(k
, &kkd
, "private");
568 key_structsteal(kd
, "private", kkd
);
570 mp_drop(x
); mp_drop(y
);
573 static void alg_dhparam(keyopts
*k
)
575 static const char *pl
[] = { "p", "q", "g", 0 };
577 if (!copyparam(k
, pl
)) {
586 if (strcmp(k
->curve
, "list") == 0) {
588 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
592 if (dh_parse(&qd
, &dp
))
593 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
595 die(EXIT_FAILURE
, "junk at end of field spec");
596 if ((g
= group_prime(&dp
)) == 0)
597 die(EXIT_FAILURE
, "invalid prime field");
598 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
599 moan("WARNING! group check failed: %s", e
);
607 /* --- Choose a large safe prime number --- */
609 if (k
->f
& f_limlee
) {
614 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
615 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
616 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
617 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
621 for (i
= 0; i
< nf
; i
++) {
624 mp_writedstr(f
[i
], &d
, 10);
627 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
630 } else if (k
->f
& f_kcdsa
) {
633 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
634 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
639 mp_writedstr(dp
.q
, &d
, 10);
640 mp_div(&v
, 0, dp
.p
, dp
.q
);
643 mp_writedstr(v
, &d
, 10);
645 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
649 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
650 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
653 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
656 kd
= key_newstruct();
657 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
658 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
659 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
663 key_setkeydata(k
->kf
, k
->k
, kd
);
668 static void alg_dh(keyopts
*k
)
675 /* --- Get the shared parameters --- */
678 key_split(&k
->k
->k
); kd
= k
->k
->k
;
683 /* --- Choose a suitable private key --- *
685 * Since %$g$% has order %$q$%, choose %$x < q$%.
688 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
690 /* --- Compute the public key %$y = g^x \bmod p$% --- */
692 mpmont_create(&mm
, p
);
693 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
696 /* --- Store everything away --- */
698 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
700 kkd
= key_newstruct();
701 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
702 dolock(k
, &kkd
, "private");
703 key_structsteal(kd
, "private", kkd
);
705 mp_drop(x
); mp_drop(y
);
708 static void alg_bbs(keyopts
*k
)
713 /* --- Sanity checking --- */
716 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
720 /* --- Generate the BBS parameters --- */
722 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
723 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
724 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
726 /* --- Allrighty then --- */
728 kd
= key_newstruct();
729 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
731 kkd
= key_newstruct();
732 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
733 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
734 dolock(k
, &kkd
, "private");
735 key_structsteal(kd
, "private", kkd
);
736 key_setkeydata(k
->kf
, k
->k
, kd
);
742 static void alg_binparam(keyopts
*k
)
744 static const char *pl
[] = { "p", "q", "g", 0 };
745 if (!copyparam(k
, pl
)) {
752 /* --- Decide on a field --- */
754 if (!k
->bits
) k
->bits
= 128;
755 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
757 LIST("Built-in binary fields", stdout
,
758 bintab
[i
].name
, bintab
[i
].name
);
762 if (k
->bits
<= 40) k
->curve
= "p1363-40";
763 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
764 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
765 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
766 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
767 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
770 "no built-in binary fields provide %u-bit security",
775 /* --- Check it --- */
779 if (dhbin_parse(&qd
, &gb
))
780 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
782 die(EXIT_FAILURE
, "junk at end of field spec");
783 if ((g
= group_binary(&gb
)) == 0)
784 die(EXIT_FAILURE
, "invalid binary field");
785 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
786 moan("WARNING! group check failed: %s", e
);
789 /* --- Write out the answer --- */
791 kd
= key_newstruct();
792 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
793 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
794 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
798 key_setkeydata(k
->kf
, k
->k
, kd
);
803 static void alg_bin(keyopts
*k
)
810 /* --- Get the shared parameters --- */
813 key_split(&k
->k
->k
); kd
= k
->k
->k
;
818 /* --- Choose a suitable private key --- *
820 * Since %$g$% has order %$q$%, choose %$x < q$%.
823 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
825 /* --- Compute the public key %$y = g^x \bmod p$% --- */
827 gfreduce_create(&r
, p
);
828 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
829 gfreduce_destroy(&r
);
831 /* --- Store everything away --- */
833 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
835 kkd
= key_newstruct();
836 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
837 dolock(k
, &kkd
, "private");
838 key_structsteal(kd
, "private", kkd
);
840 mp_drop(x
); mp_drop(y
);
843 static void alg_ecparam(keyopts
*k
)
845 static const char *pl
[] = { "curve", 0 };
846 if (!copyparam(k
, pl
)) {
851 /* --- Decide on a curve --- */
853 if (!k
->bits
) k
->bits
= 256;
854 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
856 LIST("Built-in elliptic curves", stdout
,
857 ectab
[i
].name
, ectab
[i
].name
);
861 if (k
->bits
<= 56) k
->curve
= "secp112r1";
862 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
863 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
864 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
865 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
866 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
867 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
868 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
870 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
874 /* --- Check it --- */
876 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
877 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
878 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
879 moan("WARNING! curve check failed: %s", e
);
882 /* --- Write out the answer --- */
884 kd
= key_newstruct();
885 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
886 key_setkeydata(k
->kf
, k
->k
, kd
);
891 static void alg_ec(keyopts
*k
)
900 /* --- Get the curve --- */
903 key_split(&k
->k
->k
); kd
= k
->k
->k
;
904 if ((kkd
= key_structfind(kd
, "curve")) == 0)
905 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
906 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
907 die(EXIT_FAILURE
, "subkey `curve' is not a string");
908 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
909 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
911 /* --- Invent a private exponent and compute the public key --- */
913 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
914 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
916 /* --- Store everything away --- */
918 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
920 kkd
= key_newstruct();
921 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
922 dolock(k
, &kkd
, "private");
923 key_structsteal(kd
, "private", kkd
);
931 /* --- The algorithm tables --- */
933 typedef struct keyalg
{
935 void (*proc
)(keyopts
*o
);
939 static keyalg algtab
[] = {
940 { "binary", alg_binary
, "Plain binary data" },
941 { "des", alg_des
, "Binary with DES-style parity" },
942 { "rsa", alg_rsa
, "RSA public-key encryption" },
943 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
944 { "dsa", alg_dsa
, "DSA digital signatures" },
945 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
946 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
947 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
948 { "bindh", alg_bin
, "DH over a binary field" },
949 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
950 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
951 { "ec", alg_ec
, "Elliptic curve crypto" },
955 /* --- @cmd_add@ --- */
957 static int cmd_add(int argc
, char *argv
[])
960 time_t exp
= KEXP_EXPIRE
;
961 uint32 kid
= rand_global
.ops
->word(&rand_global
);
962 const char *tag
= 0, *ptag
= 0;
964 keyalg
*alg
= algtab
;
965 const char *rtag
= 0;
966 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
967 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
968 const char *seed
= 0;
971 /* --- Parse options for the subcommand --- */
974 static struct option opt
[] = {
975 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
976 { "bits", OPTF_ARGREQ
, 0, 'b' },
977 { "qbits", OPTF_ARGREQ
, 0, 'B' },
978 { "parameters", OPTF_ARGREQ
, 0, 'p' },
979 { "expire", OPTF_ARGREQ
, 0, 'e' },
980 { "comment", OPTF_ARGREQ
, 0, 'c' },
981 { "tag", OPTF_ARGREQ
, 0, 't' },
982 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
983 { "key-id", OPTF_ARGREQ
, 0, 'I' },
984 { "curve", OPTF_ARGREQ
, 0, 'C' },
985 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
986 { "seed", OPTF_ARGREQ
, 0, 's' },
987 { "newseed", OPTF_ARGREQ
, 0, 'n' },
988 { "lock", 0, 0, 'l' },
989 { "quiet", 0, 0, 'q' },
990 { "lim-lee", 0, 0, 'L' },
991 { "subgroup", 0, 0, 'S' },
992 { "kcdsa", 0, 0, 'K' },
995 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
1000 /* --- Handle the various options --- */
1004 /* --- Read an algorithm name --- */
1008 size_t sz
= strlen(optarg
);
1010 if (strcmp(optarg
, "list") == 0) {
1011 for (a
= algtab
; a
->name
; a
++)
1012 printf("%-10s %s\n", a
->name
, a
->help
);
1017 for (a
= algtab
; a
->name
; a
++) {
1018 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1019 if (a
->name
[sz
] == 0) {
1023 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1029 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1032 /* --- Bits must be nonzero and a multiple of 8 --- */
1036 k
.bits
= strtoul(optarg
, &p
, 0);
1037 if (k
.bits
== 0 || *p
!= 0)
1038 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1043 k
.qbits
= strtoul(optarg
, &p
, 0);
1044 if (k
.qbits
== 0 || *p
!= 0)
1045 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1048 /* --- Parameter selection --- */
1054 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1057 if (strcmp(optarg
, "forever") == 0)
1060 exp
= get_date(optarg
, 0);
1062 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1066 /* --- Store comments without interpretation --- */
1069 if (key_chkcomment(optarg
))
1070 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1074 /* --- Elliptic curve parameters --- */
1080 /* --- Store tags --- */
1083 if (key_chkident(optarg
))
1084 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1091 /* --- Seeding --- */
1094 const struct seedalg
*ss
;
1095 if (strcmp(optarg
, "list") == 0) {
1096 printf("Seed algorithms:\n");
1097 for (ss
= seedtab
; ss
->p
; ss
++)
1098 printf(" %s\n", ss
->p
);
1101 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1103 for (ss
= seedtab
; ss
->p
; ss
++) {
1104 if (strcmp(optarg
, ss
->p
) == 0)
1108 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1115 if (seed
) die(EXIT_FAILURE
, "seed already set");
1116 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1117 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1118 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1119 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1121 k
.r
= sa
->gen(d
.buf
, d
.len
);
1130 unsigned n
= strtoul(optarg
, &p
, 0);
1131 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1132 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1133 if (seed
) die(EXIT_FAILURE
, "seed already set");
1136 rand_get(RAND_GLOBAL
, p
, n
);
1137 c
= base64_class
.encoder(0, "", 0);
1138 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1141 k
.r
= sa
->gen(p
, n
);
1144 /* --- Key id --- */
1151 id
= strtoul(optarg
, &p
, 16);
1152 if (errno
|| *p
|| id
> MASK32
)
1153 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1157 /* --- Other flags --- */
1178 /* --- Other things are bogus --- */
1186 /* --- Various sorts of bogosity --- */
1188 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1190 "Usage: add [OPTIONS] TYPE [ATTR...]");
1192 if (key_chkident(argv
[optind
]))
1193 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1195 /* --- Set up various bits of the state --- */
1197 if (exp
== KEXP_EXPIRE
)
1198 exp
= time(0) + 14 * 24 * 60 * 60;
1200 /* --- Open the file and create the basic key block --- *
1202 * Keep on generating keyids until one of them doesn't collide.
1205 doopen(&f
, KOPEN_WRITE
);
1208 /* --- Key the generator --- */
1214 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1216 else if (err
!= KERR_DUPID
)
1217 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1220 /* --- Set various simple attributes --- */
1225 if (k
.f
& f_retag
) {
1226 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1228 strcmp(kk
->tag
, tag
) == 0)
1229 key_settag(&f
, kk
, 0);
1231 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1232 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1236 int err
= key_setcomment(&f
, k
.k
, c
);
1238 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1241 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1243 key_putattr(&f
, k
.k
, "genseed", seed
);
1244 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1247 key_fulltag(k
.k
, &k
.tag
);
1249 /* --- Find the parameter key --- */
1251 if (ptag
&& (k
.p
= key_bytag(&f
, ptag
)) == 0)
1252 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1254 /* --- Now generate the actual key data --- */
1260 dstr_destroy(&k
.tag
);
1265 /*----- Key listing -------------------------------------------------------*/
1267 /* --- Listing options --- */
1269 typedef struct listopts
{
1270 const char *tfmt
; /* Date format (@strftime@-style) */
1271 int v
; /* Verbosity level */
1272 unsigned f
; /* Various flags */
1273 time_t t
; /* Time now (for key expiry) */
1274 key_filter kf
; /* Filter for matching keys */
1277 /* --- Listing flags --- */
1279 #define f_newline 2u /* Write newline before next entry */
1280 #define f_attr 4u /* Written at least one attribute */
1281 #define f_utc 8u /* Emit UTC time, not local time */
1283 /* --- @showkeydata@ --- *
1285 * Arguments: @key_data *k@ = pointer to key to write
1286 * @int ind@ = indentation level
1287 * @listopts *o@ = listing options
1288 * @dstr *d@ = tag string for this subkey
1292 * Use: Emits a piece of key data in a human-readable format.
1295 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1297 #define INDENT(i) do { \
1299 for (_i = 0; _i < (i); _i++) { \
1304 switch (k
->e
& KF_ENCMASK
) {
1306 /* --- Binary key data --- *
1308 * Emit as a simple hex dump.
1312 const octet
*p
= k
->u
.k
.k
;
1313 const octet
*l
= p
+ k
->u
.k
.sz
;
1316 fputs(" {", stdout
);
1321 } else if (sz
% 8 == 0)
1325 printf("%02x", *p
++);
1330 fputs("}\n", stdout
);
1333 /* --- Encrypted data --- *
1335 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1336 * key. Otherwise just say that it's encrypted and move on.
1341 fputs(" encrypted\n", stdout
);
1344 if (key_punlock(&kd
, k
, d
->buf
))
1345 printf(" <failed to unlock %s>\n", d
->buf
);
1347 fputs(" encrypted", stdout
);
1348 showkeydata(kd
, ind
, o
, d
);
1354 /* --- Integer keys --- *
1356 * Emit as a large integer in decimal. This makes using the key in
1357 * `calc' or whatever easier.
1362 mp_writefile(k
->u
.m
, stdout
, 10);
1366 /* --- Strings --- */
1369 printf(" `%s'\n", k
->u
.p
);
1372 /* --- Elliptic curve points --- */
1375 if (EC_ATINF(&k
->u
.e
))
1376 fputs(" inf\n", stdout
);
1378 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1379 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1384 /* --- Structured keys --- *
1386 * Just iterate over the subkeys.
1394 fputs(" {\n", stdout
);
1395 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1396 if (!key_match(k
, &o
->kf
))
1399 printf("%s =", tag
);
1401 dstr_putf(d
, ".%s", tag
);
1402 showkeydata(k
, ind
+ 2, o
, d
);
1405 fputs("}\n", stdout
);
1412 /* --- @showkey@ --- *
1414 * Arguments: @key *k@ = pointer to key to show
1415 * @listopts *o@ = pointer to listing options
1419 * Use: Emits a listing of a particular key.
1422 static void showkey(key
*k
, listopts
*o
)
1424 char ebuf
[24], dbuf
[24];
1427 /* --- Skip the key if the filter doesn't match --- */
1429 if (!key_match(k
->k
, &o
->kf
))
1432 /* --- Sort out the expiry and deletion times --- */
1434 if (KEY_EXPIRED(o
->t
, k
->exp
))
1435 strcpy(ebuf
, "expired");
1436 else if (k
->exp
== KEXP_FOREVER
)
1437 strcpy(ebuf
, "forever");
1439 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1440 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1443 if (KEY_EXPIRED(o
->t
, k
->del
))
1444 strcpy(dbuf
, "deleted");
1445 else if (k
->del
== KEXP_FOREVER
)
1446 strcpy(dbuf
, "forever");
1448 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1449 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1452 /* --- If in compact format, just display and quit --- */
1455 if (!(o
->f
& f_newline
)) {
1456 printf("%8s %-20s %-20s %-10s %s\n",
1457 "Id", "Tag", "Type", "Expire", "Delete");
1459 printf("%08lx %-20s %-20s %-10s %s\n",
1460 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1461 k
->type
, ebuf
, dbuf
);
1466 /* --- Display the standard header --- */
1468 if (o
->f
& f_newline
)
1469 fputc('\n', stdout
);
1470 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1471 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1472 printf("type: %s\n", k
->type
);
1473 printf("expiry: %s\n", ebuf
);
1474 printf("delete: %s\n", dbuf
);
1475 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1477 /* --- Display the attributes --- */
1481 const char *av
, *an
;
1484 printf("attributes:");
1485 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1486 printf("\n %s = %s", an
, av
);
1490 fputc('\n', stdout
);
1495 /* --- If dumping requested, dump the raw key data --- */
1499 fputs("key:", stdout
);
1501 showkeydata(k
->k
, 0, o
, &d
);
1508 /* --- @cmd_list@ --- */
1510 static int cmd_list(int argc
, char *argv
[])
1514 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1516 /* --- Parse subcommand options --- */
1519 static struct option opt
[] = {
1520 { "quiet", 0, 0, 'q' },
1521 { "verbose", 0, 0, 'v' },
1522 { "utc", 0, 0, 'u' },
1523 { "filter", OPTF_ARGREQ
, 0, 'f' },
1526 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1543 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1545 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1554 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1556 /* --- Open the key file --- */
1558 doopen(&f
, KOPEN_READ
);
1561 /* --- Set up the time format --- */
1564 o
.tfmt
= "%Y-%m-%d";
1565 else if (o
.f
& f_utc
)
1566 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1568 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1570 /* --- If specific keys were requested use them, otherwise do all --- *
1572 * Some day, this might turn into a wildcard match.
1575 if (optind
< argc
) {
1577 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1580 moan("key `%s' not found", argv
[optind
]);
1584 } while (optind
< argc
);
1587 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1595 return (EXIT_FAILURE
);
1600 /*----- Command implementation --------------------------------------------*/
1602 /* --- @cmd_expire@ --- */
1604 static int cmd_expire(int argc
, char *argv
[])
1612 die(EXIT_FAILURE
, "Usage: expire TAG...");
1613 doopen(&f
, KOPEN_WRITE
);
1614 for (i
= 1; i
< argc
; i
++) {
1615 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1618 moan("key `%s' not found", argv
[i
]);
1626 /* --- @cmd_delete@ --- */
1628 static int cmd_delete(int argc
, char *argv
[])
1636 die(EXIT_FAILURE
, "Usage: delete TAG...");
1637 doopen(&f
, KOPEN_WRITE
);
1638 for (i
= 1; i
< argc
; i
++) {
1639 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1642 moan("key `%s' not found", argv
[i
]);
1650 /* --- @cmd_setattr@ --- */
1652 static int cmd_setattr(int argc
, char *argv
[])
1658 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1659 doopen(&f
, KOPEN_WRITE
);
1660 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1661 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1662 setattr(&f
, k
, argv
+ 2);
1667 /* --- @cmd_getattr@ --- */
1669 static int cmd_getattr(int argc
, char *argv
[])
1677 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1678 doopen(&f
, KOPEN_READ
);
1679 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1680 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1682 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1683 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1690 /* --- @cmd_finger@ --- */
1692 static const struct fpres
{
1694 const codec_class
*cdc
;
1695 unsigned short ival
;
1698 { "hex", &hex_class
, 8, "-:" },
1699 { "base32", &base32_class
, 6, ":" },
1703 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1704 const gchash
*ch
, const key_filter
*kf
)
1707 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1713 if (key_fingerprint(k
, h
, kf
)) {
1715 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1716 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1718 for (i
= 0; i
< dd
.len
; i
++) {
1719 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1720 dstr_putc(&d
, dd
.buf
[i
]);
1722 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1723 dstr_write(&d
, stdout
);
1725 dstr_destroy(&d
); dstr_destroy(&dd
);
1729 static const struct fpres
*lookup_fpres(const char *name
)
1731 const struct fpres
*fpres
;
1732 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1733 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1734 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1737 static int cmd_finger(int argc
, char *argv
[])
1741 const struct fpres
*fpres
= fprestab
;
1742 const gchash
*ch
= &rmd160
;
1743 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1746 static struct option opt
[] = {
1747 { "filter", OPTF_ARGREQ
, 0, 'f' },
1748 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1749 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1752 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1758 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1760 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1763 fpres
= lookup_fpres(optarg
);
1766 if ((ch
= ghash_byname(optarg
)) == 0)
1767 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1775 argv
+= optind
; argc
-= optind
;
1778 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1781 doopen(&f
, KOPEN_READ
);
1785 for (i
= 0; i
< argc
; i
++) {
1786 key
*k
= key_bytag(&f
, argv
[i
]);
1788 fingerprint(k
, fpres
, ch
, &kf
);
1791 moan("key `%s' not found", argv
[i
]);
1797 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1798 fingerprint(k
, fpres
, ch
, &kf
);
1803 /* --- @cmd_verify@ --- */
1805 static int cmd_verify(int argc
, char *argv
[])
1809 const gchash
*ch
= &rmd160
;
1813 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1816 const struct fpres
*fpres
= fprestab
;
1817 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1820 static struct option opt
[] = {
1821 { "filter", OPTF_ARGREQ
, 0, 'f' },
1822 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1823 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1826 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1832 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1834 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1837 fpres
= lookup_fpres(optarg
);
1840 if ((ch
= ghash_byname(optarg
)) == 0)
1841 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1849 argv
+= optind
; argc
-= optind
;
1850 if (rc
|| argc
!= 2) {
1852 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1855 doopen(&f
, KOPEN_READ
);
1857 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1858 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1859 for (p
= argv
[1]; *p
; p
++) {
1860 if (strchr(fpres
->sep
, *p
)) continue;
1863 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1864 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1865 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1866 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1867 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1869 if (d
.len
!= ch
->hashsz
) {
1870 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1871 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1874 if (!key_fingerprint(k
, h
, &kf
))
1875 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1876 fpr
= GH_DONE(h
, 0);
1877 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1878 die(EXIT_FAILURE
, "key fingerprint mismatch");
1879 dstr_destroy(&d
); dstr_destroy(&dd
);
1884 /* --- @cmd_comment@ --- */
1886 static int cmd_comment(int argc
, char *argv
[])
1892 if (argc
< 2 || argc
> 3)
1893 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1894 doopen(&f
, KOPEN_WRITE
);
1895 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1896 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1897 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1898 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1903 /* --- @cmd_tag@ --- */
1905 static int cmd_tag(int argc
, char *argv
[])
1914 static struct option opt
[] = {
1915 { "retag", 0, 0, 'r' },
1918 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1931 argv
+= optind
; argc
-= optind
;
1932 if (argc
< 1 || argc
> 2 || rc
)
1933 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1934 doopen(&f
, KOPEN_WRITE
);
1935 if (flags
& f_retag
) {
1936 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1937 key_settag(&f
, k
, 0);
1939 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1940 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1941 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1942 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1947 /* --- @cmd_lock@ --- */
1949 static int cmd_lock(int argc
, char *argv
[])
1957 die(EXIT_FAILURE
, "Usage: lock QTAG");
1958 doopen(&f
, KOPEN_WRITE
);
1959 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1960 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1961 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
1962 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1963 if (key_plock(kd
, 0, d
.buf
))
1964 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1970 /* --- @cmd_unlock@ --- */
1972 static int cmd_unlock(int argc
, char *argv
[])
1980 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1981 doopen(&f
, KOPEN_WRITE
);
1982 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1983 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1984 if ((*kd
)->e
!= KENC_ENCRYPT
)
1985 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1986 if (key_punlock(kd
, 0, d
.buf
))
1987 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1993 /* --- @cmd_extract@ --- */
1995 static int cmd_extract(int argc
, char *argv
[])
2001 key_filter kf
= { 0, 0 };
2003 const char *outfile
= 0;
2007 static struct option opt
[] = {
2008 { "filter", OPTF_ARGREQ
, 0, 'f' },
2011 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2017 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2019 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2027 argv
+= optind
; argc
-= optind
;
2029 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2030 if (strcmp(*argv
, "-") == 0)
2034 dstr_putf(&d
, "%s.new", outfile
);
2035 if (!(fp
= fopen(d
.buf
, "w"))) {
2036 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2037 d
.buf
, strerror(errno
));
2041 doopen(&f
, KOPEN_READ
);
2045 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2046 key_extract(&f
, k
, fp
, &kf
);
2048 for (i
= 1; i
< argc
; i
++) {
2049 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2050 key_extract(&f
, k
, fp
, &kf
);
2052 moan("key `%s' not found", argv
[i
]);
2057 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2058 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2064 /* --- @cmd_tidy@ --- */
2066 static int cmd_tidy(int argc
, char *argv
[])
2070 die(EXIT_FAILURE
, "Usage: tidy");
2071 doopen(&f
, KOPEN_WRITE
);
2072 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2077 /* --- @cmd_merge@ --- */
2079 static int cmd_merge(int argc
, char *argv
[])
2085 die(EXIT_FAILURE
, "Usage: merge FILE");
2086 if (strcmp(argv
[1], "-") == 0)
2088 else if (!(fp
= fopen(argv
[1], "r"))) {
2089 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2090 argv
[1], strerror(errno
));
2093 doopen(&f
, KOPEN_WRITE
);
2094 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2099 /* --- @cmd_show@ --- */
2103 listtab[i].name, listtab[i].name) \
2104 LI("Hash functions", hash, \
2105 ghashtab[i], ghashtab[i]->name) \
2106 LI("Elliptic curves", ec, \
2107 ectab[i].name, ectab[i].name) \
2108 LI("Prime Diffie-Hellman groups", dh, \
2109 ptab[i].name, ptab[i].name) \
2110 LI("Binary Diffie-Hellman groups", bindh, \
2111 bintab[i].name, bintab[i].name) \
2112 LI("Key-generation algorithms", keygen, \
2113 algtab[i].name, algtab[i].name) \
2114 LI("Random seeding algorithms", seed, \
2115 seedtab[i].p, seedtab[i].p) \
2116 LI("Fingerprint presentation styles", fpres, \
2117 fprestab[i].name, fprestab[i].name)
2119 MAKELISTTAB(listtab
, LISTS
)
2121 static int cmd_show(int argc
, char *argv
[])
2123 return (displaylists(listtab
, argv
+ 1));
2126 /*----- Main command table ------------------------------------------------*/
2128 static int cmd_help(int argc
, char *argv
[]);
2130 static cmd cmds
[] = {
2131 { "help", cmd_help
, "help [COMMAND...]" },
2132 { "show", cmd_show
, "show [ITEM...]" },
2133 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2136 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2137 -q, --quiet Show less information.\n\
2138 -v, --verbose Show more information.\n\
2140 { "fingerprint", cmd_finger
,
2141 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2144 -f, --filter=FILT Only hash key components matching FILT.\n\
2145 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2146 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2147 ($ show hash for list.)\n\
2149 { "verify", cmd_verify
,
2150 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2153 -f, --filter=FILT Only hash key components matching FILT.\n\
2154 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2155 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2156 ($ show hash for list.)\n\
2158 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2161 -f, --filter=FILT Only extract key components matching FILT.\n\
2163 { "merge", cmd_merge
, "merge FILE" },
2164 { "expire", cmd_expire
, "expire TAG..." },
2165 { "delete", cmd_delete
, "delete TAG..." },
2166 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2167 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2168 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2169 { "lock", cmd_lock
, "lock QTAG" },
2170 { "unlock", cmd_unlock
, "unlock QTAG" },
2171 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2174 -r, --retag Untag any key currently called new-tag.\n\
2176 { "tidy", cmd_tidy
, "tidy" },
2178 "add [-OPTIONS] TYPE [ATTR...]\n\
2179 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2180 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2181 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2184 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2185 ($ show keygen for list.)\n\
2186 -b, --bits=N Generate an N-bit key.\n\
2187 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2188 -p, --parameters=TAG Get group parameters from TAG.\n\
2189 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2190 ($ show ec or $ show dh for list.)\n\
2191 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2192 ($ show seed for list.)\n\
2193 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2194 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2195 -e, --expire=TIME Make the key expire after TIME.\n\
2196 -c, --comment=STRING Attach the command STRING to the key.\n\
2197 -t, --tag=TAG Tag the key with the name TAG.\n\
2198 -r, --retag Untag any key currently with that tag.\n\
2199 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2200 -I, --key-id=ID Force the key-id for the new key.\n\
2201 -l, --lock Lock the generated key with a passphrase.\n\
2202 -q, --quiet Don't give progress indicators while working.\n\
2203 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2204 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2205 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2210 static int cmd_help(int argc
, char *argv
[])
2212 sc_help(cmds
, stdout
, argv
+ 1);
2216 /*----- Main code ---------------------------------------------------------*/
2218 /* --- Helpful GNUy functions --- */
2220 static void usage(FILE *fp
)
2222 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2225 void version(FILE *fp
)
2227 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2230 void help_global(FILE *fp
)
2234 Performs various simple key management operations.\n\
2236 Global command line options:\n\
2238 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2239 -v, --version Display version number.\n\
2240 -u, --usage Display short usage summary.\n\
2242 -k, --keyring=FILE Read and write keys in FILE.\n",
2248 * Arguments: @int argc@ = number of command line arguments
2249 * @char *argv[]@ = array of command line arguments
2251 * Returns: Nonzero on failure.
2253 * Use: Main program. Performs simple key management functions.
2256 int main(int argc
, char *argv
[])
2262 /* --- Initialization --- */
2267 /* --- Parse command line options --- */
2270 static struct option opt
[] = {
2272 /* --- Standard GNUy help options --- */
2274 { "help", 0, 0, 'h' },
2275 { "version", 0, 0, 'v' },
2276 { "usage", 0, 0, 'u' },
2278 /* --- Real live useful options --- */
2280 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2282 /* --- Magic terminator --- */
2286 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2292 /* --- GNU help options --- */
2295 sc_help(cmds
, stdout
, argv
+ optind
);
2304 /* --- Real useful options --- */
2310 /* --- Bogosity --- */
2318 /* --- Complain about excessive bogons --- */
2320 if (f
& f_bogus
|| optind
== argc
) {
2325 /* --- Initialize the Catacomb random number generator --- */
2327 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2328 rand_seed(RAND_GLOBAL
, 160);
2330 /* --- Dispatch to appropriate command handler --- */
2335 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2338 /*----- That's all, folks -------------------------------------------------*/