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
);
259 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
262 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
264 if (!KEY_MATCH(kd
, &kf
)) {
266 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
271 /* --- Copy over the parameters --- */
273 kd
= key_copydata(k
->p
->k
, &kf
);
275 key_setkeydata(k
->kf
, k
->k
, kd
);
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
);
291 * Arguments: @key_data *k@ = pointer to key data block
292 * @const char *tag@ = tag string to use
294 * Returns: Pointer to multiprecision integer key item.
296 * Use: Fetches an MP key component.
299 static mp
*getmp(key_data
*k
, const char *tag
)
301 k
= key_structfind(k
, tag
);
303 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
304 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
305 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
309 /* --- @keyrand@ --- *
311 * Arguments: @key_file *kf@ = pointer to key file
312 * @const char *id@ = pointer to key id (or null)
316 * Use: Keys the random number generator.
319 static void keyrand(key_file
*kf
, const char *id
)
323 /* --- Find the key --- */
326 if ((k
= key_bytag(kf
, id
)) == 0)
327 die(EXIT_FAILURE
, "key `%s' not found", id
);
329 k
= key_bytype(kf
, "catacomb-rand");
332 key_data
*kd
= k
->k
, *kkd
;
336 switch (kd
->e
& KF_ENCMASK
) {
342 if (key_punlock(&kkd
, kd
, d
.buf
))
343 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
351 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
355 /* --- Key the generator --- */
357 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
362 /* --- Key generation algorithms --- */
364 static void alg_binary(keyopts
*k
)
374 die(EXIT_FAILURE
, "no shared parameters for binary keys");
376 sz
= (k
->bits
+ 7) >> 3;
378 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
379 k
->r
->ops
->fill(k
->r
, p
, sz
);
381 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
384 key_setkeydata(k
->kf
, k
->k
, kd
);
389 static void alg_des(keyopts
*k
)
399 die(EXIT_FAILURE
, "no shared parameters for DES keys");
400 if (k
->bits
% 56 || k
->bits
> 168)
401 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
405 k
->r
->ops
->fill(k
->r
, p
, sz
);
406 for (i
= 0; i
< sz
; i
++) {
407 octet x
= p
[i
] | 0x01;
411 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
413 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
416 key_setkeydata(k
->kf
, k
->k
, kd
);
421 static void alg_rsa(keyopts
*k
)
426 /* --- Sanity checking --- */
429 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
433 /* --- Generate the RSA parameters --- */
435 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
436 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
437 die(EXIT_FAILURE
, "RSA key generation failed");
439 /* --- Run a test encryption --- */
442 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
444 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
449 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
450 c
= rsa_qprivop(&rp
, c
, c
, g
);
453 die(EXIT_FAILURE
, "test encryption failed");
459 /* --- Allrighty then --- */
461 kd
= key_newstruct();
462 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
463 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
465 kkd
= key_newstruct();
466 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
467 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
468 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
469 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
470 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
471 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
472 dolock(k
, &kkd
, "private");
473 key_structsteal(kd
, "private", kkd
);
474 key_setkeydata(k
->kf
, k
->k
, kd
);
479 static void alg_dsaparam(keyopts
*k
)
481 static const char *pl
[] = { "q", "p", "g", 0 };
482 if (!copyparam(k
, pl
)) {
491 /* --- Choose appropriate bit lengths if necessary --- */
498 /* --- Allocate a seed block --- */
500 sz
= (k
->qbits
+ 7) >> 3;
502 k
->r
->ops
->fill(k
->r
, p
, sz
);
504 /* --- Allocate the parameters --- */
506 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
507 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
508 die(EXIT_FAILURE
, "DSA parameter generation failed");
510 /* --- Store the parameters --- */
512 kd
= key_newstruct();
513 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
514 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
515 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
519 key_setkeydata(k
->kf
, k
->k
, kd
);
522 /* --- Store the seed for future verification --- */
524 c
= base64_class
.encoder(0, "", 0);
525 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
528 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
530 dstr_putf(&d
, "%u", ds
.count
);
531 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
538 static void alg_dsa(keyopts
*k
)
545 /* --- Get the shared parameters --- */
548 key_split(&k
->k
->k
); kd
= k
->k
->k
;
553 /* --- Choose a private key --- */
555 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
556 mpmont_create(&mm
, p
);
557 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
559 /* --- Store everything away --- */
561 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
563 kkd
= key_newstruct();
564 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
565 dolock(k
, &kkd
, "private");
566 key_structsteal(kd
, "private", kkd
);
568 mp_drop(x
); mp_drop(y
);
571 static void alg_dhparam(keyopts
*k
)
573 static const char *pl
[] = { "p", "q", "g", 0 };
575 if (!copyparam(k
, pl
)) {
584 if (strcmp(k
->curve
, "list") == 0) {
586 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
590 if (dh_parse(&qd
, &dp
))
591 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
593 die(EXIT_FAILURE
, "junk at end of field spec");
594 if ((g
= group_prime(&dp
)) == 0)
595 die(EXIT_FAILURE
, "invalid prime field");
596 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
597 moan("WARNING! group check failed: %s", e
);
605 /* --- Choose a large safe prime number --- */
607 if (k
->f
& f_limlee
) {
612 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
613 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
614 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
615 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
619 for (i
= 0; i
< nf
; i
++) {
622 mp_writedstr(f
[i
], &d
, 10);
625 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
628 } else if (k
->f
& f_kcdsa
) {
631 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
632 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
637 mp_writedstr(dp
.q
, &d
, 10);
638 mp_div(&v
, 0, dp
.p
, dp
.q
);
641 mp_writedstr(v
, &d
, 10);
643 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
647 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
648 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
651 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
654 kd
= key_newstruct();
655 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
656 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
657 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
661 key_setkeydata(k
->kf
, k
->k
, kd
);
666 static void alg_dh(keyopts
*k
)
673 /* --- Get the shared parameters --- */
676 key_split(&k
->k
->k
); kd
= k
->k
->k
;
681 /* --- Choose a suitable private key --- *
683 * Since %$g$% has order %$q$%, choose %$x < q$%.
686 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
688 /* --- Compute the public key %$y = g^x \bmod p$% --- */
690 mpmont_create(&mm
, p
);
691 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
694 /* --- Store everything away --- */
696 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
698 kkd
= key_newstruct();
699 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
700 dolock(k
, &kkd
, "private");
701 key_structsteal(kd
, "private", kkd
);
703 mp_drop(x
); mp_drop(y
);
706 static void alg_bbs(keyopts
*k
)
711 /* --- Sanity checking --- */
714 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
718 /* --- Generate the BBS parameters --- */
720 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
721 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
722 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
724 /* --- Allrighty then --- */
726 kd
= key_newstruct();
727 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
729 kkd
= key_newstruct();
730 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
731 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
732 dolock(k
, &kkd
, "private");
733 key_structsteal(kd
, "private", kkd
);
734 key_setkeydata(k
->kf
, k
->k
, kd
);
740 static void alg_binparam(keyopts
*k
)
742 static const char *pl
[] = { "p", "q", "g", 0 };
743 if (!copyparam(k
, pl
)) {
750 /* --- Decide on a field --- */
752 if (!k
->bits
) k
->bits
= 128;
753 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
755 LIST("Built-in binary fields", stdout
,
756 bintab
[i
].name
, bintab
[i
].name
);
760 if (k
->bits
<= 40) k
->curve
= "p1363-40";
761 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
762 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
763 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
764 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
765 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
768 "no built-in binary fields provide %u-bit security",
773 /* --- Check it --- */
777 if (dhbin_parse(&qd
, &gb
))
778 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
780 die(EXIT_FAILURE
, "junk at end of field spec");
781 if ((g
= group_binary(&gb
)) == 0)
782 die(EXIT_FAILURE
, "invalid binary field");
783 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
784 moan("WARNING! group check failed: %s", e
);
787 /* --- Write out the answer --- */
789 kd
= key_newstruct();
790 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
791 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
792 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
796 key_setkeydata(k
->kf
, k
->k
, kd
);
801 static void alg_bin(keyopts
*k
)
808 /* --- Get the shared parameters --- */
811 key_split(&k
->k
->k
); kd
= k
->k
->k
;
816 /* --- Choose a suitable private key --- *
818 * Since %$g$% has order %$q$%, choose %$x < q$%.
821 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
823 /* --- Compute the public key %$y = g^x \bmod p$% --- */
825 gfreduce_create(&r
, p
);
826 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
827 gfreduce_destroy(&r
);
829 /* --- Store everything away --- */
831 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
833 kkd
= key_newstruct();
834 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
835 dolock(k
, &kkd
, "private");
836 key_structsteal(kd
, "private", kkd
);
838 mp_drop(x
); mp_drop(y
);
841 static void alg_ecparam(keyopts
*k
)
843 static const char *pl
[] = { "curve", 0 };
844 if (!copyparam(k
, pl
)) {
849 /* --- Decide on a curve --- */
851 if (!k
->bits
) k
->bits
= 256;
852 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
854 LIST("Built-in elliptic curves", stdout
,
855 ectab
[i
].name
, ectab
[i
].name
);
859 if (k
->bits
<= 56) k
->curve
= "secp112r1";
860 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
861 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
862 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
863 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
864 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
865 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
866 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
868 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
872 /* --- Check it --- */
874 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
875 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
876 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
877 moan("WARNING! curve check failed: %s", e
);
880 /* --- Write out the answer --- */
882 kd
= key_newstruct();
883 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
884 key_setkeydata(k
->kf
, k
->k
, kd
);
889 static void alg_ec(keyopts
*k
)
898 /* --- Get the curve --- */
901 key_split(&k
->k
->k
); kd
= k
->k
->k
;
902 if ((kkd
= key_structfind(kd
, "curve")) == 0)
903 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
904 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
905 die(EXIT_FAILURE
, "subkey `curve' is not a string");
906 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
907 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
909 /* --- Invent a private exponent and compute the public key --- */
911 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
912 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
914 /* --- Store everything away --- */
916 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
918 kkd
= key_newstruct();
919 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
920 dolock(k
, &kkd
, "private");
921 key_structsteal(kd
, "private", kkd
);
929 /* --- The algorithm tables --- */
931 typedef struct keyalg
{
933 void (*proc
)(keyopts
*o
);
937 static keyalg algtab
[] = {
938 { "binary", alg_binary
, "Plain binary data" },
939 { "des", alg_des
, "Binary with DES-style parity" },
940 { "rsa", alg_rsa
, "RSA public-key encryption" },
941 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
942 { "dsa", alg_dsa
, "DSA digital signatures" },
943 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
944 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
945 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
946 { "bindh", alg_bin
, "DH over a binary field" },
947 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
948 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
949 { "ec", alg_ec
, "Elliptic curve crypto" },
953 /* --- @cmd_add@ --- */
955 static int cmd_add(int argc
, char *argv
[])
958 time_t exp
= KEXP_EXPIRE
;
959 uint32 kid
= rand_global
.ops
->word(&rand_global
);
960 const char *tag
= 0, *ptag
= 0;
962 keyalg
*alg
= algtab
;
963 const char *rtag
= 0;
964 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
965 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
966 const char *seed
= 0;
969 /* --- Parse options for the subcommand --- */
972 static struct option opt
[] = {
973 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
974 { "bits", OPTF_ARGREQ
, 0, 'b' },
975 { "qbits", OPTF_ARGREQ
, 0, 'B' },
976 { "parameters", OPTF_ARGREQ
, 0, 'p' },
977 { "expire", OPTF_ARGREQ
, 0, 'e' },
978 { "comment", OPTF_ARGREQ
, 0, 'c' },
979 { "tag", OPTF_ARGREQ
, 0, 't' },
980 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
981 { "key-id", OPTF_ARGREQ
, 0, 'I' },
982 { "curve", OPTF_ARGREQ
, 0, 'C' },
983 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
984 { "seed", OPTF_ARGREQ
, 0, 's' },
985 { "newseed", OPTF_ARGREQ
, 0, 'n' },
986 { "lock", 0, 0, 'l' },
987 { "quiet", 0, 0, 'q' },
988 { "lim-lee", 0, 0, 'L' },
989 { "subgroup", 0, 0, 'S' },
990 { "kcdsa", 0, 0, 'K' },
993 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
998 /* --- Handle the various options --- */
1002 /* --- Read an algorithm name --- */
1006 size_t sz
= strlen(optarg
);
1008 if (strcmp(optarg
, "list") == 0) {
1009 for (a
= algtab
; a
->name
; a
++)
1010 printf("%-10s %s\n", a
->name
, a
->help
);
1015 for (a
= algtab
; a
->name
; a
++) {
1016 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1017 if (a
->name
[sz
] == 0) {
1021 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1027 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1030 /* --- Bits must be nonzero and a multiple of 8 --- */
1034 k
.bits
= strtoul(optarg
, &p
, 0);
1035 if (k
.bits
== 0 || *p
!= 0)
1036 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1041 k
.qbits
= strtoul(optarg
, &p
, 0);
1042 if (k
.qbits
== 0 || *p
!= 0)
1043 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1046 /* --- Parameter selection --- */
1052 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1055 if (strcmp(optarg
, "forever") == 0)
1058 exp
= get_date(optarg
, 0);
1060 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1064 /* --- Store comments without interpretation --- */
1067 if (key_chkcomment(optarg
))
1068 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1072 /* --- Elliptic curve parameters --- */
1078 /* --- Store tags --- */
1081 if (key_chkident(optarg
))
1082 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1089 /* --- Seeding --- */
1092 const struct seedalg
*ss
;
1093 if (strcmp(optarg
, "list") == 0) {
1094 printf("Seed algorithms:\n");
1095 for (ss
= seedtab
; ss
->p
; ss
++)
1096 printf(" %s\n", ss
->p
);
1099 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1101 for (ss
= seedtab
; ss
->p
; ss
++) {
1102 if (strcmp(optarg
, ss
->p
) == 0)
1106 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1113 if (seed
) die(EXIT_FAILURE
, "seed already set");
1114 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1115 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1116 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1117 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1119 k
.r
= sa
->gen(d
.buf
, d
.len
);
1128 unsigned n
= strtoul(optarg
, &p
, 0);
1129 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1130 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1131 if (seed
) die(EXIT_FAILURE
, "seed already set");
1134 rand_get(RAND_GLOBAL
, p
, n
);
1135 c
= base64_class
.encoder(0, "", 0);
1136 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1139 k
.r
= sa
->gen(p
, n
);
1142 /* --- Key id --- */
1149 id
= strtoul(optarg
, &p
, 16);
1150 if (errno
|| *p
|| id
> MASK32
)
1151 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1155 /* --- Other flags --- */
1176 /* --- Other things are bogus --- */
1184 /* --- Various sorts of bogosity --- */
1186 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1188 "Usage: add [OPTIONS] TYPE [ATTR...]");
1190 if (key_chkident(argv
[optind
]))
1191 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1193 /* --- Set up various bits of the state --- */
1195 if (exp
== KEXP_EXPIRE
)
1196 exp
= time(0) + 14 * 24 * 60 * 60;
1198 /* --- Open the file and create the basic key block --- *
1200 * Keep on generating keyids until one of them doesn't collide.
1203 doopen(&f
, KOPEN_WRITE
);
1206 /* --- Key the generator --- */
1212 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1214 else if (err
!= KERR_DUPID
)
1215 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1218 /* --- Set various simple attributes --- */
1223 if (k
.f
& f_retag
) {
1224 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1226 strcmp(kk
->tag
, tag
) == 0)
1227 key_settag(&f
, kk
, 0);
1229 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1230 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1234 int err
= key_setcomment(&f
, k
.k
, c
);
1236 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1239 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1241 key_putattr(&f
, k
.k
, "genseed", seed
);
1242 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1245 key_fulltag(k
.k
, &k
.tag
);
1247 /* --- Find the parameter key --- */
1250 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1251 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1252 if ((k
.p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
1253 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1256 /* --- Now generate the actual key data --- */
1262 dstr_destroy(&k
.tag
);
1267 /*----- Key listing -------------------------------------------------------*/
1269 /* --- Listing options --- */
1271 typedef struct listopts
{
1272 const char *tfmt
; /* Date format (@strftime@-style) */
1273 int v
; /* Verbosity level */
1274 unsigned f
; /* Various flags */
1275 time_t t
; /* Time now (for key expiry) */
1276 key_filter kf
; /* Filter for matching keys */
1279 /* --- Listing flags --- */
1281 #define f_newline 2u /* Write newline before next entry */
1282 #define f_attr 4u /* Written at least one attribute */
1283 #define f_utc 8u /* Emit UTC time, not local time */
1285 /* --- @showkeydata@ --- *
1287 * Arguments: @key_data *k@ = pointer to key to write
1288 * @int ind@ = indentation level
1289 * @listopts *o@ = listing options
1290 * @dstr *d@ = tag string for this subkey
1294 * Use: Emits a piece of key data in a human-readable format.
1297 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1299 #define INDENT(i) do { \
1301 for (_i = 0; _i < (i); _i++) { \
1306 switch (k
->e
& KF_ENCMASK
) {
1308 /* --- Binary key data --- *
1310 * Emit as a simple hex dump.
1314 const octet
*p
= k
->u
.k
.k
;
1315 const octet
*l
= p
+ k
->u
.k
.sz
;
1318 fputs(" {", stdout
);
1323 } else if (sz
% 8 == 0)
1327 printf("%02x", *p
++);
1332 fputs("}\n", stdout
);
1335 /* --- Encrypted data --- *
1337 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1338 * key. Otherwise just say that it's encrypted and move on.
1343 fputs(" encrypted\n", stdout
);
1346 if (key_punlock(&kd
, k
, d
->buf
))
1347 printf(" <failed to unlock %s>\n", d
->buf
);
1349 fputs(" encrypted", stdout
);
1350 showkeydata(kd
, ind
, o
, d
);
1356 /* --- Integer keys --- *
1358 * Emit as a large integer in decimal. This makes using the key in
1359 * `calc' or whatever easier.
1364 mp_writefile(k
->u
.m
, stdout
, 10);
1368 /* --- Strings --- */
1371 printf(" `%s'\n", k
->u
.p
);
1374 /* --- Elliptic curve points --- */
1377 if (EC_ATINF(&k
->u
.e
))
1378 fputs(" inf\n", stdout
);
1380 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1381 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1386 /* --- Structured keys --- *
1388 * Just iterate over the subkeys.
1396 fputs(" {\n", stdout
);
1397 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1398 if (!key_match(k
, &o
->kf
))
1401 printf("%s =", tag
);
1403 dstr_putf(d
, ".%s", tag
);
1404 showkeydata(k
, ind
+ 2, o
, d
);
1407 fputs("}\n", stdout
);
1414 /* --- @showkey@ --- *
1416 * Arguments: @key *k@ = pointer to key to show
1417 * @listopts *o@ = pointer to listing options
1421 * Use: Emits a listing of a particular key.
1424 static void showkey(key
*k
, listopts
*o
)
1426 char ebuf
[24], dbuf
[24];
1429 /* --- Skip the key if the filter doesn't match --- */
1431 if (!key_match(k
->k
, &o
->kf
))
1434 /* --- Sort out the expiry and deletion times --- */
1436 if (KEY_EXPIRED(o
->t
, k
->exp
))
1437 strcpy(ebuf
, "expired");
1438 else if (k
->exp
== KEXP_FOREVER
)
1439 strcpy(ebuf
, "forever");
1441 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1442 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1445 if (KEY_EXPIRED(o
->t
, k
->del
))
1446 strcpy(dbuf
, "deleted");
1447 else if (k
->del
== KEXP_FOREVER
)
1448 strcpy(dbuf
, "forever");
1450 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1451 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1454 /* --- If in compact format, just display and quit --- */
1457 if (!(o
->f
& f_newline
)) {
1458 printf("%8s %-20s %-20s %-10s %s\n",
1459 "Id", "Tag", "Type", "Expire", "Delete");
1461 printf("%08lx %-20s %-20s %-10s %s\n",
1462 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1463 k
->type
, ebuf
, dbuf
);
1468 /* --- Display the standard header --- */
1470 if (o
->f
& f_newline
)
1471 fputc('\n', stdout
);
1472 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1473 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1474 printf("type: %s\n", k
->type
);
1475 printf("expiry: %s\n", ebuf
);
1476 printf("delete: %s\n", dbuf
);
1477 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1479 /* --- Display the attributes --- */
1483 const char *av
, *an
;
1486 printf("attributes:");
1487 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1488 printf("\n %s = %s", an
, av
);
1492 fputc('\n', stdout
);
1497 /* --- If dumping requested, dump the raw key data --- */
1501 fputs("key:", stdout
);
1503 showkeydata(k
->k
, 0, o
, &d
);
1510 /* --- @cmd_list@ --- */
1512 static int cmd_list(int argc
, char *argv
[])
1516 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1518 /* --- Parse subcommand options --- */
1521 static struct option opt
[] = {
1522 { "quiet", 0, 0, 'q' },
1523 { "verbose", 0, 0, 'v' },
1524 { "utc", 0, 0, 'u' },
1525 { "filter", OPTF_ARGREQ
, 0, 'f' },
1528 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1545 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1547 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1556 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1558 /* --- Open the key file --- */
1560 doopen(&f
, KOPEN_READ
);
1563 /* --- Set up the time format --- */
1566 o
.tfmt
= "%Y-%m-%d";
1567 else if (o
.f
& f_utc
)
1568 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1570 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1572 /* --- If specific keys were requested use them, otherwise do all --- *
1574 * Some day, this might turn into a wildcard match.
1577 if (optind
< argc
) {
1579 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1582 moan("key `%s' not found", argv
[optind
]);
1586 } while (optind
< argc
);
1589 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1597 return (EXIT_FAILURE
);
1602 /*----- Command implementation --------------------------------------------*/
1604 /* --- @cmd_expire@ --- */
1606 static int cmd_expire(int argc
, char *argv
[])
1614 die(EXIT_FAILURE
, "Usage: expire TAG...");
1615 doopen(&f
, KOPEN_WRITE
);
1616 for (i
= 1; i
< argc
; i
++) {
1617 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1620 moan("key `%s' not found", argv
[i
]);
1628 /* --- @cmd_delete@ --- */
1630 static int cmd_delete(int argc
, char *argv
[])
1638 die(EXIT_FAILURE
, "Usage: delete TAG...");
1639 doopen(&f
, KOPEN_WRITE
);
1640 for (i
= 1; i
< argc
; i
++) {
1641 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1644 moan("key `%s' not found", argv
[i
]);
1652 /* --- @cmd_setattr@ --- */
1654 static int cmd_setattr(int argc
, char *argv
[])
1660 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1661 doopen(&f
, KOPEN_WRITE
);
1662 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1663 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1664 setattr(&f
, k
, argv
+ 2);
1669 /* --- @cmd_getattr@ --- */
1671 static int cmd_getattr(int argc
, char *argv
[])
1679 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1680 doopen(&f
, KOPEN_READ
);
1681 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1682 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1684 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1685 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1692 /* --- @cmd_finger@ --- */
1694 static const struct fpres
{
1696 const codec_class
*cdc
;
1697 unsigned short ival
;
1700 { "hex", &hex_class
, 8, "-:" },
1701 { "base32", &base32_class
, 6, ":" },
1705 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1706 const gchash
*ch
, const key_filter
*kf
)
1709 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1715 if (key_fingerprint(k
, h
, kf
)) {
1717 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1718 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1720 for (i
= 0; i
< dd
.len
; i
++) {
1721 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1722 dstr_putc(&d
, dd
.buf
[i
]);
1724 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1725 dstr_write(&d
, stdout
);
1727 dstr_destroy(&d
); dstr_destroy(&dd
);
1731 static const struct fpres
*lookup_fpres(const char *name
)
1733 const struct fpres
*fpres
;
1734 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1735 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1736 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1739 static int cmd_finger(int argc
, char *argv
[])
1743 const struct fpres
*fpres
= fprestab
;
1744 const gchash
*ch
= &rmd160
;
1745 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1748 static struct option opt
[] = {
1749 { "filter", OPTF_ARGREQ
, 0, 'f' },
1750 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1751 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1754 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1760 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1762 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1765 fpres
= lookup_fpres(optarg
);
1768 if ((ch
= ghash_byname(optarg
)) == 0)
1769 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1777 argv
+= optind
; argc
-= optind
;
1780 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1783 doopen(&f
, KOPEN_READ
);
1787 for (i
= 0; i
< argc
; i
++) {
1788 key
*k
= key_bytag(&f
, argv
[i
]);
1790 fingerprint(k
, fpres
, ch
, &kf
);
1793 moan("key `%s' not found", argv
[i
]);
1799 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1800 fingerprint(k
, fpres
, ch
, &kf
);
1805 /* --- @cmd_verify@ --- */
1807 static int cmd_verify(int argc
, char *argv
[])
1811 const gchash
*ch
= &rmd160
;
1815 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1818 const struct fpres
*fpres
= fprestab
;
1819 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1822 static struct option opt
[] = {
1823 { "filter", OPTF_ARGREQ
, 0, 'f' },
1824 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1825 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1828 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1834 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1836 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1839 fpres
= lookup_fpres(optarg
);
1842 if ((ch
= ghash_byname(optarg
)) == 0)
1843 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1851 argv
+= optind
; argc
-= optind
;
1852 if (rc
|| argc
!= 2) {
1854 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1857 doopen(&f
, KOPEN_READ
);
1859 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1860 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1861 for (p
= argv
[1]; *p
; p
++) {
1862 if (strchr(fpres
->sep
, *p
)) continue;
1865 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1866 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1867 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1868 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1869 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1871 if (d
.len
!= ch
->hashsz
) {
1872 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1873 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1876 if (!key_fingerprint(k
, h
, &kf
))
1877 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1878 fpr
= GH_DONE(h
, 0);
1879 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1880 die(EXIT_FAILURE
, "key fingerprint mismatch");
1881 dstr_destroy(&d
); dstr_destroy(&dd
);
1886 /* --- @cmd_comment@ --- */
1888 static int cmd_comment(int argc
, char *argv
[])
1894 if (argc
< 2 || argc
> 3)
1895 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1896 doopen(&f
, KOPEN_WRITE
);
1897 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1898 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1899 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1900 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1905 /* --- @cmd_tag@ --- */
1907 static int cmd_tag(int argc
, char *argv
[])
1916 static struct option opt
[] = {
1917 { "retag", 0, 0, 'r' },
1920 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1933 argv
+= optind
; argc
-= optind
;
1934 if (argc
< 1 || argc
> 2 || rc
)
1935 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1936 doopen(&f
, KOPEN_WRITE
);
1937 if (flags
& f_retag
) {
1938 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1939 key_settag(&f
, k
, 0);
1941 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1942 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1943 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1944 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1949 /* --- @cmd_lock@ --- */
1951 static int cmd_lock(int argc
, char *argv
[])
1959 die(EXIT_FAILURE
, "Usage: lock QTAG");
1960 doopen(&f
, KOPEN_WRITE
);
1961 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1962 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1963 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
1964 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1965 if (key_plock(kd
, 0, d
.buf
))
1966 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1972 /* --- @cmd_unlock@ --- */
1974 static int cmd_unlock(int argc
, char *argv
[])
1982 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1983 doopen(&f
, KOPEN_WRITE
);
1984 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1985 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1986 if ((*kd
)->e
!= KENC_ENCRYPT
)
1987 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1988 if (key_punlock(kd
, 0, d
.buf
))
1989 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1995 /* --- @cmd_extract@ --- */
1997 static int cmd_extract(int argc
, char *argv
[])
2003 key_filter kf
= { 0, 0 };
2005 const char *outfile
= 0;
2009 static struct option opt
[] = {
2010 { "filter", OPTF_ARGREQ
, 0, 'f' },
2013 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2019 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2021 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2029 argv
+= optind
; argc
-= optind
;
2031 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2032 if (strcmp(*argv
, "-") == 0)
2036 dstr_putf(&d
, "%s.new", outfile
);
2037 if (!(fp
= fopen(d
.buf
, "w"))) {
2038 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2039 d
.buf
, strerror(errno
));
2043 doopen(&f
, KOPEN_READ
);
2047 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2048 key_extract(&f
, k
, fp
, &kf
);
2050 for (i
= 1; i
< argc
; i
++) {
2051 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2052 key_extract(&f
, k
, fp
, &kf
);
2054 moan("key `%s' not found", argv
[i
]);
2059 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2060 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2066 /* --- @cmd_tidy@ --- */
2068 static int cmd_tidy(int argc
, char *argv
[])
2072 die(EXIT_FAILURE
, "Usage: tidy");
2073 doopen(&f
, KOPEN_WRITE
);
2074 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2079 /* --- @cmd_merge@ --- */
2081 static int cmd_merge(int argc
, char *argv
[])
2087 die(EXIT_FAILURE
, "Usage: merge FILE");
2088 if (strcmp(argv
[1], "-") == 0)
2090 else if (!(fp
= fopen(argv
[1], "r"))) {
2091 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2092 argv
[1], strerror(errno
));
2095 doopen(&f
, KOPEN_WRITE
);
2096 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2101 /* --- @cmd_show@ --- */
2105 listtab[i].name, listtab[i].name) \
2106 LI("Hash functions", hash, \
2107 ghashtab[i], ghashtab[i]->name) \
2108 LI("Elliptic curves", ec, \
2109 ectab[i].name, ectab[i].name) \
2110 LI("Prime Diffie-Hellman groups", dh, \
2111 ptab[i].name, ptab[i].name) \
2112 LI("Binary Diffie-Hellman groups", bindh, \
2113 bintab[i].name, bintab[i].name) \
2114 LI("Key-generation algorithms", keygen, \
2115 algtab[i].name, algtab[i].name) \
2116 LI("Random seeding algorithms", seed, \
2117 seedtab[i].p, seedtab[i].p) \
2118 LI("Fingerprint presentation styles", fpres, \
2119 fprestab[i].name, fprestab[i].name)
2121 MAKELISTTAB(listtab
, LISTS
)
2123 static int cmd_show(int argc
, char *argv
[])
2125 return (displaylists(listtab
, argv
+ 1));
2128 /*----- Main command table ------------------------------------------------*/
2130 static int cmd_help(int argc
, char *argv
[]);
2132 static cmd cmds
[] = {
2133 { "help", cmd_help
, "help [COMMAND...]" },
2134 { "show", cmd_show
, "show [ITEM...]" },
2135 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2138 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2139 -q, --quiet Show less information.\n\
2140 -v, --verbose Show more information.\n\
2142 { "fingerprint", cmd_finger
,
2143 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2146 -f, --filter=FILT Only hash key components matching FILT.\n\
2147 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2148 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2149 ($ show hash for list.)\n\
2151 { "verify", cmd_verify
,
2152 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2155 -f, --filter=FILT Only hash key components matching FILT.\n\
2156 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2157 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2158 ($ show hash for list.)\n\
2160 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2163 -f, --filter=FILT Only extract key components matching FILT.\n\
2165 { "merge", cmd_merge
, "merge FILE" },
2166 { "expire", cmd_expire
, "expire TAG..." },
2167 { "delete", cmd_delete
, "delete TAG..." },
2168 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2169 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2170 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2171 { "lock", cmd_lock
, "lock QTAG" },
2172 { "unlock", cmd_unlock
, "unlock QTAG" },
2173 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2176 -r, --retag Untag any key currently called new-tag.\n\
2178 { "tidy", cmd_tidy
, "tidy" },
2180 "add [-OPTIONS] TYPE [ATTR...]\n\
2181 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2182 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2183 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2186 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2187 ($ show keygen for list.)\n\
2188 -b, --bits=N Generate an N-bit key.\n\
2189 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2190 -p, --parameters=TAG Get group parameters from TAG.\n\
2191 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2192 ($ show ec or $ show dh for list.)\n\
2193 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2194 ($ show seed for list.)\n\
2195 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2196 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2197 -e, --expire=TIME Make the key expire after TIME.\n\
2198 -c, --comment=STRING Attach the command STRING to the key.\n\
2199 -t, --tag=TAG Tag the key with the name TAG.\n\
2200 -r, --retag Untag any key currently with that tag.\n\
2201 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2202 -I, --key-id=ID Force the key-id for the new key.\n\
2203 -l, --lock Lock the generated key with a passphrase.\n\
2204 -q, --quiet Don't give progress indicators while working.\n\
2205 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2206 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2207 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2212 static int cmd_help(int argc
, char *argv
[])
2214 sc_help(cmds
, stdout
, argv
+ 1);
2218 /*----- Main code ---------------------------------------------------------*/
2220 /* --- Helpful GNUy functions --- */
2222 static void usage(FILE *fp
)
2224 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2227 void version(FILE *fp
)
2229 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2232 void help_global(FILE *fp
)
2236 Performs various simple key management operations.\n\
2238 Global command line options:\n\
2240 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2241 -v, --version Display version number.\n\
2242 -u, --usage Display short usage summary.\n\
2244 -k, --keyring=FILE Read and write keys in FILE.\n",
2250 * Arguments: @int argc@ = number of command line arguments
2251 * @char *argv[]@ = array of command line arguments
2253 * Returns: Nonzero on failure.
2255 * Use: Main program. Performs simple key management functions.
2258 int main(int argc
, char *argv
[])
2264 /* --- Initialization --- */
2269 /* --- Parse command line options --- */
2272 static struct option opt
[] = {
2274 /* --- Standard GNUy help options --- */
2276 { "help", 0, 0, 'h' },
2277 { "version", 0, 0, 'v' },
2278 { "usage", 0, 0, 'u' },
2280 /* --- Real live useful options --- */
2282 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2284 /* --- Magic terminator --- */
2288 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2294 /* --- GNU help options --- */
2297 sc_help(cmds
, stdout
, argv
+ optind
);
2306 /* --- Real useful options --- */
2312 /* --- Bogosity --- */
2320 /* --- Complain about excessive bogons --- */
2322 if (f
& f_bogus
|| optind
== argc
) {
2327 /* --- Initialize the Catacomb random number generator --- */
2329 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2330 rand_seed(RAND_GLOBAL
, 160);
2332 /* --- Dispatch to appropriate command handler --- */
2337 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2340 /*----- That's all, folks -------------------------------------------------*/