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, or null
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 /* --- Copy the key data if there's anything we want --- */
259 /* --- Run through the checklist --- */
261 key_fulltag(k
->p
, &t
);
262 if ((k
->p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
263 die(EXIT_FAILURE
, "parameter key `%s' is not structured", t
.buf
);
265 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
268 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
270 if (!KEY_MATCH(kd
, &kf
)) {
272 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
277 /* --- Copy over the parameters --- */
279 kd
= key_copydata(k
->p
->k
, &kf
);
281 key_setkeydata(k
->kf
, k
->k
, kd
);
285 /* --- Copy over attributes --- */
287 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
288 key_putattr(k
->kf
, k
->k
, n
, v
);
298 * Arguments: @key_data *k@ = pointer to key data block
299 * @const char *tag@ = tag string to use
301 * Returns: Pointer to multiprecision integer key item.
303 * Use: Fetches an MP key component.
306 static mp
*getmp(key_data
*k
, const char *tag
)
308 k
= key_structfind(k
, tag
);
310 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
311 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
312 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
316 /* --- @keyrand@ --- *
318 * Arguments: @key_file *kf@ = pointer to key file
319 * @const char *id@ = pointer to key id (or null)
323 * Use: Keys the random number generator.
326 static void keyrand(key_file
*kf
, const char *id
)
330 /* --- Find the key --- */
333 if ((k
= key_bytag(kf
, id
)) == 0)
334 die(EXIT_FAILURE
, "key `%s' not found", id
);
336 k
= key_bytype(kf
, "catacomb-rand");
339 key_data
*kd
= k
->k
, *kkd
;
343 switch (kd
->e
& KF_ENCMASK
) {
349 if (key_punlock(&kkd
, kd
, d
.buf
))
350 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
358 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
362 /* --- Key the generator --- */
364 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
369 /* --- Key generation algorithms --- */
371 static void alg_empty(keyopts
*k
)
374 key_setkeydata(k
->kf
, k
->k
,
375 key_newstring(KCAT_SHARE
, k
->curve ? k
->curve
: "."));
378 static void alg_binary(keyopts
*k
)
389 sz
= (k
->bits
+ 7) >> 3;
391 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
392 k
->r
->ops
->fill(k
->r
, p
, sz
);
394 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
397 key_setkeydata(k
->kf
, k
->k
, kd
);
402 static void alg_des(keyopts
*k
)
412 if (k
->bits
% 56 || k
->bits
> 168)
413 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
417 k
->r
->ops
->fill(k
->r
, p
, sz
);
418 for (i
= 0; i
< sz
; i
++) {
419 octet x
= p
[i
] | 0x01;
423 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
425 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
428 key_setkeydata(k
->kf
, k
->k
, kd
);
433 static void alg_rsa(keyopts
*k
)
438 /* --- Sanity checking --- */
444 /* --- Generate the RSA parameters --- */
446 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
447 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
448 die(EXIT_FAILURE
, "RSA key generation failed");
450 /* --- Run a test encryption --- */
453 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
455 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
460 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
461 c
= rsa_qprivop(&rp
, c
, c
, g
);
464 die(EXIT_FAILURE
, "test encryption failed");
470 /* --- Allrighty then --- */
472 kd
= key_newstruct();
473 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
474 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
476 kkd
= key_newstruct();
477 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
478 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
479 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
480 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
481 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
482 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
483 dolock(k
, &kkd
, "private");
484 key_structsteal(kd
, "private", kkd
);
485 key_setkeydata(k
->kf
, k
->k
, kd
);
490 static void alg_dsaparam(keyopts
*k
)
492 static const char *pl
[] = { "q", "p", "g", 0 };
493 if (!copyparam(k
, pl
)) {
502 /* --- Choose appropriate bit lengths if necessary --- */
509 /* --- Allocate a seed block --- */
511 sz
= (k
->qbits
+ 7) >> 3;
513 k
->r
->ops
->fill(k
->r
, p
, sz
);
515 /* --- Allocate the parameters --- */
517 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
518 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
519 die(EXIT_FAILURE
, "DSA parameter generation failed");
521 /* --- Store the parameters --- */
523 kd
= key_newstruct();
524 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
525 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
526 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
530 key_setkeydata(k
->kf
, k
->k
, kd
);
533 /* --- Store the seed for future verification --- */
535 c
= base64_class
.encoder(0, "", 0);
536 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
539 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
541 dstr_putf(&d
, "%u", ds
.count
);
542 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
549 static void alg_dsa(keyopts
*k
)
556 /* --- Get the shared parameters --- */
559 key_split(&k
->k
->k
); kd
= k
->k
->k
;
564 /* --- Choose a private key --- */
566 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
567 mpmont_create(&mm
, p
);
568 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
570 /* --- Store everything away --- */
572 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
574 kkd
= key_newstruct();
575 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
576 dolock(k
, &kkd
, "private");
577 key_structsteal(kd
, "private", kkd
);
579 mp_drop(x
); mp_drop(y
);
582 static void alg_dhparam(keyopts
*k
)
584 static const char *pl
[] = { "p", "q", "g", 0 };
586 if (!copyparam(k
, pl
)) {
595 if (strcmp(k
->curve
, "list") == 0) {
597 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
601 if (dh_parse(&qd
, &dp
))
602 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
604 die(EXIT_FAILURE
, "junk at end of field spec");
605 if ((g
= group_prime(&dp
)) == 0)
606 die(EXIT_FAILURE
, "invalid prime field");
607 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
608 moan("WARNING! group check failed: %s", e
);
616 /* --- Choose a large safe prime number --- */
618 if (k
->f
& f_limlee
) {
623 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
624 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
625 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
626 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
630 for (i
= 0; i
< nf
; i
++) {
633 mp_writedstr(f
[i
], &d
, 10);
636 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
639 } else if (k
->f
& f_kcdsa
) {
642 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
643 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
648 mp_writedstr(dp
.q
, &d
, 10);
649 mp_div(&v
, 0, dp
.p
, dp
.q
);
652 mp_writedstr(v
, &d
, 10);
654 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
658 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
659 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
662 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
665 kd
= key_newstruct();
666 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
667 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
668 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
672 key_setkeydata(k
->kf
, k
->k
, kd
);
677 static void alg_dh(keyopts
*k
)
684 /* --- Get the shared parameters --- */
687 key_split(&k
->k
->k
); kd
= k
->k
->k
;
692 /* --- Choose a suitable private key --- *
694 * Since %$g$% has order %$q$%, choose %$x < q$%.
697 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
699 /* --- Compute the public key %$y = g^x \bmod p$% --- */
701 mpmont_create(&mm
, p
);
702 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
705 /* --- Store everything away --- */
707 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
709 kkd
= key_newstruct();
710 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
711 dolock(k
, &kkd
, "private");
712 key_structsteal(kd
, "private", kkd
);
714 mp_drop(x
); mp_drop(y
);
717 static void alg_bbs(keyopts
*k
)
722 /* --- Sanity checking --- */
728 /* --- Generate the BBS parameters --- */
730 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
731 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
732 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
734 /* --- Allrighty then --- */
736 kd
= key_newstruct();
737 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
739 kkd
= key_newstruct();
740 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
741 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
742 dolock(k
, &kkd
, "private");
743 key_structsteal(kd
, "private", kkd
);
744 key_setkeydata(k
->kf
, k
->k
, kd
);
750 static void alg_binparam(keyopts
*k
)
752 static const char *pl
[] = { "p", "q", "g", 0 };
753 if (!copyparam(k
, pl
)) {
760 /* --- Decide on a field --- */
762 if (!k
->bits
) k
->bits
= 128;
763 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
765 LIST("Built-in binary fields", stdout
,
766 bintab
[i
].name
, bintab
[i
].name
);
770 if (k
->bits
<= 40) k
->curve
= "p1363-40";
771 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
772 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
773 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
774 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
775 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
778 "no built-in binary fields provide %u-bit security",
783 /* --- Check it --- */
787 if (dhbin_parse(&qd
, &gb
))
788 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
790 die(EXIT_FAILURE
, "junk at end of field spec");
791 if ((g
= group_binary(&gb
)) == 0)
792 die(EXIT_FAILURE
, "invalid binary field");
793 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
794 moan("WARNING! group check failed: %s", e
);
797 /* --- Write out the answer --- */
799 kd
= key_newstruct();
800 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
801 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
802 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
806 key_setkeydata(k
->kf
, k
->k
, kd
);
811 static void alg_bin(keyopts
*k
)
818 /* --- Get the shared parameters --- */
821 key_split(&k
->k
->k
); kd
= k
->k
->k
;
826 /* --- Choose a suitable private key --- *
828 * Since %$g$% has order %$q$%, choose %$x < q$%.
831 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
833 /* --- Compute the public key %$y = g^x \bmod p$% --- */
835 gfreduce_create(&r
, p
);
836 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
837 gfreduce_destroy(&r
);
839 /* --- Store everything away --- */
841 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
843 kkd
= key_newstruct();
844 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
845 dolock(k
, &kkd
, "private");
846 key_structsteal(kd
, "private", kkd
);
848 mp_drop(x
); mp_drop(y
);
851 static void alg_ecparam(keyopts
*k
)
853 static const char *pl
[] = { "curve", 0 };
854 if (!copyparam(k
, pl
)) {
859 /* --- Decide on a curve --- */
861 if (!k
->bits
) k
->bits
= 256;
862 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
864 LIST("Built-in elliptic curves", stdout
,
865 ectab
[i
].name
, ectab
[i
].name
);
869 if (k
->bits
<= 56) k
->curve
= "secp112r1";
870 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
871 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
872 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
873 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
874 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
875 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
876 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
878 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
882 /* --- Check it --- */
884 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
885 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
886 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
887 moan("WARNING! curve check failed: %s", e
);
890 /* --- Write out the answer --- */
892 kd
= key_newstruct();
893 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
894 key_setkeydata(k
->kf
, k
->k
, kd
);
899 static void alg_ec(keyopts
*k
)
908 /* --- Get the curve --- */
911 key_split(&k
->k
->k
); kd
= k
->k
->k
;
912 if ((kkd
= key_structfind(kd
, "curve")) == 0)
913 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
914 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
915 die(EXIT_FAILURE
, "subkey `curve' is not a string");
916 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
917 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
919 /* --- Invent a private exponent and compute the public key --- */
921 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
922 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
924 /* --- Store everything away --- */
926 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
928 kkd
= key_newstruct();
929 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
930 dolock(k
, &kkd
, "private");
931 key_structsteal(kd
, "private", kkd
);
939 /* --- The algorithm tables --- */
941 typedef struct keyalg
{
943 void (*proc
)(keyopts
*o
);
947 static keyalg algtab
[] = {
948 { "binary", alg_binary
, "Plain binary data" },
949 { "des", alg_des
, "Binary with DES-style parity" },
950 { "rsa", alg_rsa
, "RSA public-key encryption" },
951 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
952 { "dsa", alg_dsa
, "DSA digital signatures" },
953 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
954 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
955 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
956 { "bindh", alg_bin
, "DH over a binary field" },
957 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
958 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
959 { "ec", alg_ec
, "Elliptic curve crypto" },
960 { "empty", alg_empty
, "Empty parametrs-only key" },
964 /* --- @cmd_add@ --- */
966 static int cmd_add(int argc
, char *argv
[])
969 time_t exp
= KEXP_EXPIRE
;
970 uint32 kid
= rand_global
.ops
->word(&rand_global
);
971 const char *tag
= 0, *ptag
= 0;
973 keyalg
*alg
= algtab
;
974 const char *rtag
= 0;
975 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
976 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
977 const char *seed
= 0;
980 /* --- Parse options for the subcommand --- */
983 static struct option opt
[] = {
984 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
985 { "bits", OPTF_ARGREQ
, 0, 'b' },
986 { "qbits", OPTF_ARGREQ
, 0, 'B' },
987 { "parameters", OPTF_ARGREQ
, 0, 'p' },
988 { "expire", OPTF_ARGREQ
, 0, 'e' },
989 { "comment", OPTF_ARGREQ
, 0, 'c' },
990 { "tag", OPTF_ARGREQ
, 0, 't' },
991 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
992 { "key-id", OPTF_ARGREQ
, 0, 'I' },
993 { "curve", OPTF_ARGREQ
, 0, 'C' },
994 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
995 { "seed", OPTF_ARGREQ
, 0, 's' },
996 { "newseed", OPTF_ARGREQ
, 0, 'n' },
997 { "lock", 0, 0, 'l' },
998 { "quiet", 0, 0, 'q' },
999 { "lim-lee", 0, 0, 'L' },
1000 { "subgroup", 0, 0, 'S' },
1001 { "kcdsa", 0, 0, 'K' },
1004 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
1009 /* --- Handle the various options --- */
1013 /* --- Read an algorithm name --- */
1017 size_t sz
= strlen(optarg
);
1019 if (strcmp(optarg
, "list") == 0) {
1020 for (a
= algtab
; a
->name
; a
++)
1021 printf("%-10s %s\n", a
->name
, a
->help
);
1026 for (a
= algtab
; a
->name
; a
++) {
1027 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1028 if (a
->name
[sz
] == 0) {
1032 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1038 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1041 /* --- Bits must be nonzero and a multiple of 8 --- */
1045 k
.bits
= strtoul(optarg
, &p
, 0);
1046 if (k
.bits
== 0 || *p
!= 0)
1047 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1052 k
.qbits
= strtoul(optarg
, &p
, 0);
1053 if (k
.qbits
== 0 || *p
!= 0)
1054 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1057 /* --- Parameter selection --- */
1063 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1066 if (strcmp(optarg
, "forever") == 0)
1069 exp
= get_date(optarg
, 0);
1071 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1075 /* --- Store comments without interpretation --- */
1078 if (key_chkcomment(optarg
))
1079 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1083 /* --- Elliptic curve parameters --- */
1089 /* --- Store tags --- */
1092 if (key_chkident(optarg
))
1093 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1100 /* --- Seeding --- */
1103 const struct seedalg
*ss
;
1104 if (strcmp(optarg
, "list") == 0) {
1105 printf("Seed algorithms:\n");
1106 for (ss
= seedtab
; ss
->p
; ss
++)
1107 printf(" %s\n", ss
->p
);
1110 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1112 for (ss
= seedtab
; ss
->p
; ss
++) {
1113 if (strcmp(optarg
, ss
->p
) == 0)
1117 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1124 if (seed
) die(EXIT_FAILURE
, "seed already set");
1125 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1126 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1127 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1128 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1130 k
.r
= sa
->gen(d
.buf
, d
.len
);
1139 unsigned n
= strtoul(optarg
, &p
, 0);
1140 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1141 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1142 if (seed
) die(EXIT_FAILURE
, "seed already set");
1145 rand_get(RAND_GLOBAL
, p
, n
);
1146 c
= base64_class
.encoder(0, "", 0);
1147 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1150 k
.r
= sa
->gen(p
, n
);
1153 /* --- Key id --- */
1160 id
= strtoul(optarg
, &p
, 16);
1161 if (errno
|| *p
|| id
> MASK32
)
1162 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1166 /* --- Other flags --- */
1187 /* --- Other things are bogus --- */
1195 /* --- Various sorts of bogosity --- */
1197 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1199 "Usage: add [OPTIONS] TYPE [ATTR...]");
1201 if (key_chkident(argv
[optind
]))
1202 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1204 /* --- Set up various bits of the state --- */
1206 if (exp
== KEXP_EXPIRE
)
1207 exp
= time(0) + 14 * 24 * 60 * 60;
1209 /* --- Open the file and create the basic key block --- *
1211 * Keep on generating keyids until one of them doesn't collide.
1214 doopen(&f
, KOPEN_WRITE
);
1217 /* --- Key the generator --- */
1223 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1225 else if (err
!= KERR_DUPID
)
1226 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1229 /* --- Set various simple attributes --- */
1234 if (k
.f
& f_retag
) {
1235 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1237 strcmp(kk
->tag
, tag
) == 0)
1238 key_settag(&f
, kk
, 0);
1240 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1241 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1245 int err
= key_setcomment(&f
, k
.k
, c
);
1247 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1250 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1252 key_putattr(&f
, k
.k
, "genseed", seed
);
1253 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1256 key_fulltag(k
.k
, &k
.tag
);
1258 /* --- Find the parameter key --- */
1260 if (ptag
&& (k
.p
= key_bytag(&f
, ptag
)) == 0)
1261 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1263 /* --- Now generate the actual key data --- */
1269 dstr_destroy(&k
.tag
);
1274 /*----- Key listing -------------------------------------------------------*/
1276 /* --- Listing options --- */
1278 typedef struct listopts
{
1279 const char *tfmt
; /* Date format (@strftime@-style) */
1280 int v
; /* Verbosity level */
1281 unsigned f
; /* Various flags */
1282 time_t t
; /* Time now (for key expiry) */
1283 key_filter kf
; /* Filter for matching keys */
1286 /* --- Listing flags --- */
1288 #define f_newline 2u /* Write newline before next entry */
1289 #define f_attr 4u /* Written at least one attribute */
1290 #define f_utc 8u /* Emit UTC time, not local time */
1292 /* --- @showkeydata@ --- *
1294 * Arguments: @key_data *k@ = pointer to key to write
1295 * @int ind@ = indentation level
1296 * @listopts *o@ = listing options
1297 * @dstr *d@ = tag string for this subkey
1301 * Use: Emits a piece of key data in a human-readable format.
1304 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1306 #define INDENT(i) do { \
1308 for (_i = 0; _i < (i); _i++) { \
1313 switch (k
->e
& KF_ENCMASK
) {
1315 /* --- Binary key data --- *
1317 * Emit as a simple hex dump.
1321 const octet
*p
= k
->u
.k
.k
;
1322 const octet
*l
= p
+ k
->u
.k
.sz
;
1325 fputs(" {", stdout
);
1330 } else if (sz
% 8 == 0)
1334 printf("%02x", *p
++);
1339 fputs("}\n", stdout
);
1342 /* --- Encrypted data --- *
1344 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1345 * key. Otherwise just say that it's encrypted and move on.
1350 fputs(" encrypted\n", stdout
);
1353 if (key_punlock(&kd
, k
, d
->buf
))
1354 printf(" <failed to unlock %s>\n", d
->buf
);
1356 fputs(" encrypted", stdout
);
1357 showkeydata(kd
, ind
, o
, d
);
1363 /* --- Integer keys --- *
1365 * Emit as a large integer in decimal. This makes using the key in
1366 * `calc' or whatever easier.
1371 mp_writefile(k
->u
.m
, stdout
, 10);
1375 /* --- Strings --- */
1378 printf(" `%s'\n", k
->u
.p
);
1381 /* --- Elliptic curve points --- */
1384 if (EC_ATINF(&k
->u
.e
))
1385 fputs(" inf\n", stdout
);
1387 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1388 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1393 /* --- Structured keys --- *
1395 * Just iterate over the subkeys.
1403 fputs(" {\n", stdout
);
1404 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1405 if (!key_match(k
, &o
->kf
))
1408 printf("%s =", tag
);
1410 dstr_putf(d
, ".%s", tag
);
1411 showkeydata(k
, ind
+ 2, o
, d
);
1414 fputs("}\n", stdout
);
1421 /* --- @showkey@ --- *
1423 * Arguments: @key *k@ = pointer to key to show
1424 * @listopts *o@ = pointer to listing options
1428 * Use: Emits a listing of a particular key.
1431 static void showkey(key
*k
, listopts
*o
)
1433 char ebuf
[24], dbuf
[24];
1436 /* --- Skip the key if the filter doesn't match --- */
1438 if (!key_match(k
->k
, &o
->kf
))
1441 /* --- Sort out the expiry and deletion times --- */
1443 if (KEY_EXPIRED(o
->t
, k
->exp
))
1444 strcpy(ebuf
, "expired");
1445 else if (k
->exp
== KEXP_FOREVER
)
1446 strcpy(ebuf
, "forever");
1448 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1449 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1452 if (KEY_EXPIRED(o
->t
, k
->del
))
1453 strcpy(dbuf
, "deleted");
1454 else if (k
->del
== KEXP_FOREVER
)
1455 strcpy(dbuf
, "forever");
1457 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1458 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1461 /* --- If in compact format, just display and quit --- */
1464 if (!(o
->f
& f_newline
)) {
1465 printf("%8s %-20s %-20s %-10s %s\n",
1466 "Id", "Tag", "Type", "Expire", "Delete");
1468 printf("%08lx %-20s %-20s %-10s %s\n",
1469 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1470 k
->type
, ebuf
, dbuf
);
1475 /* --- Display the standard header --- */
1477 if (o
->f
& f_newline
)
1478 fputc('\n', stdout
);
1479 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1480 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1481 printf("type: %s\n", k
->type
);
1482 printf("expiry: %s\n", ebuf
);
1483 printf("delete: %s\n", dbuf
);
1484 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1486 /* --- Display the attributes --- */
1490 const char *av
, *an
;
1493 printf("attributes:");
1494 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1495 printf("\n %s = %s", an
, av
);
1499 fputc('\n', stdout
);
1504 /* --- If dumping requested, dump the raw key data --- */
1508 fputs("key:", stdout
);
1510 showkeydata(k
->k
, 0, o
, &d
);
1517 /* --- @cmd_list@ --- */
1519 static int cmd_list(int argc
, char *argv
[])
1523 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1525 /* --- Parse subcommand options --- */
1528 static struct option opt
[] = {
1529 { "quiet", 0, 0, 'q' },
1530 { "verbose", 0, 0, 'v' },
1531 { "utc", 0, 0, 'u' },
1532 { "filter", OPTF_ARGREQ
, 0, 'f' },
1535 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1552 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1554 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1563 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1565 /* --- Open the key file --- */
1567 doopen(&f
, KOPEN_READ
);
1570 /* --- Set up the time format --- */
1573 o
.tfmt
= "%Y-%m-%d";
1574 else if (o
.f
& f_utc
)
1575 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1577 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1579 /* --- If specific keys were requested use them, otherwise do all --- *
1581 * Some day, this might turn into a wildcard match.
1584 if (optind
< argc
) {
1586 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1589 moan("key `%s' not found", argv
[optind
]);
1593 } while (optind
< argc
);
1596 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1604 return (EXIT_FAILURE
);
1609 /*----- Command implementation --------------------------------------------*/
1611 /* --- @cmd_expire@ --- */
1613 static int cmd_expire(int argc
, char *argv
[])
1621 die(EXIT_FAILURE
, "Usage: expire TAG...");
1622 doopen(&f
, KOPEN_WRITE
);
1623 for (i
= 1; i
< argc
; i
++) {
1624 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1627 moan("key `%s' not found", argv
[i
]);
1635 /* --- @cmd_delete@ --- */
1637 static int cmd_delete(int argc
, char *argv
[])
1645 die(EXIT_FAILURE
, "Usage: delete TAG...");
1646 doopen(&f
, KOPEN_WRITE
);
1647 for (i
= 1; i
< argc
; i
++) {
1648 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1651 moan("key `%s' not found", argv
[i
]);
1659 /* --- @cmd_setattr@ --- */
1661 static int cmd_setattr(int argc
, char *argv
[])
1667 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1668 doopen(&f
, KOPEN_WRITE
);
1669 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1670 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1671 setattr(&f
, k
, argv
+ 2);
1676 /* --- @cmd_getattr@ --- */
1678 static int cmd_getattr(int argc
, char *argv
[])
1686 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1687 doopen(&f
, KOPEN_READ
);
1688 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1689 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1691 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1692 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1699 /* --- @cmd_finger@ --- */
1701 static const struct fpres
{
1703 const codec_class
*cdc
;
1704 unsigned short ival
;
1707 { "hex", &hex_class
, 8, "-:" },
1708 { "base32", &base32_class
, 6, ":" },
1712 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1713 const gchash
*ch
, const key_filter
*kf
)
1716 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1722 if (key_fingerprint(k
, h
, kf
)) {
1724 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1725 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1727 for (i
= 0; i
< dd
.len
; i
++) {
1728 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1729 dstr_putc(&d
, dd
.buf
[i
]);
1731 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1732 dstr_write(&d
, stdout
);
1734 dstr_destroy(&d
); dstr_destroy(&dd
);
1738 static const struct fpres
*lookup_fpres(const char *name
)
1740 const struct fpres
*fpres
;
1741 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1742 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1743 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1746 static int cmd_finger(int argc
, char *argv
[])
1750 const struct fpres
*fpres
= fprestab
;
1751 const gchash
*ch
= &rmd160
;
1752 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1755 static struct option opt
[] = {
1756 { "filter", OPTF_ARGREQ
, 0, 'f' },
1757 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1758 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1761 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1767 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1769 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1772 fpres
= lookup_fpres(optarg
);
1775 if ((ch
= ghash_byname(optarg
)) == 0)
1776 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1784 argv
+= optind
; argc
-= optind
;
1787 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1790 doopen(&f
, KOPEN_READ
);
1794 for (i
= 0; i
< argc
; i
++) {
1795 key
*k
= key_bytag(&f
, argv
[i
]);
1797 fingerprint(k
, fpres
, ch
, &kf
);
1800 moan("key `%s' not found", argv
[i
]);
1806 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1807 fingerprint(k
, fpres
, ch
, &kf
);
1812 /* --- @cmd_verify@ --- */
1814 static int cmd_verify(int argc
, char *argv
[])
1818 const gchash
*ch
= &rmd160
;
1822 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1825 const struct fpres
*fpres
= fprestab
;
1826 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1829 static struct option opt
[] = {
1830 { "filter", OPTF_ARGREQ
, 0, 'f' },
1831 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1832 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1835 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1841 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1843 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1846 fpres
= lookup_fpres(optarg
);
1849 if ((ch
= ghash_byname(optarg
)) == 0)
1850 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1858 argv
+= optind
; argc
-= optind
;
1859 if (rc
|| argc
!= 2) {
1861 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1864 doopen(&f
, KOPEN_READ
);
1866 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1867 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1868 for (p
= argv
[1]; *p
; p
++) {
1869 if (strchr(fpres
->sep
, *p
)) continue;
1872 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1873 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1874 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1875 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1876 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1878 if (d
.len
!= ch
->hashsz
) {
1879 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1880 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1883 if (!key_fingerprint(k
, h
, &kf
))
1884 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1885 fpr
= GH_DONE(h
, 0);
1886 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1887 die(EXIT_FAILURE
, "key fingerprint mismatch");
1888 dstr_destroy(&d
); dstr_destroy(&dd
);
1893 /* --- @cmd_comment@ --- */
1895 static int cmd_comment(int argc
, char *argv
[])
1901 if (argc
< 2 || argc
> 3)
1902 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1903 doopen(&f
, KOPEN_WRITE
);
1904 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1905 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1906 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1907 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1912 /* --- @cmd_tag@ --- */
1914 static int cmd_tag(int argc
, char *argv
[])
1923 static struct option opt
[] = {
1924 { "retag", 0, 0, 'r' },
1927 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1940 argv
+= optind
; argc
-= optind
;
1941 if (argc
< 1 || argc
> 2 || rc
)
1942 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1943 doopen(&f
, KOPEN_WRITE
);
1944 if (flags
& f_retag
) {
1945 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1946 key_settag(&f
, k
, 0);
1948 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1949 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1950 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1951 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1956 /* --- @cmd_lock@ --- */
1958 static int cmd_lock(int argc
, char *argv
[])
1966 die(EXIT_FAILURE
, "Usage: lock QTAG");
1967 doopen(&f
, KOPEN_WRITE
);
1968 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1969 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1970 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
1971 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1972 if (key_plock(kd
, 0, d
.buf
))
1973 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1979 /* --- @cmd_unlock@ --- */
1981 static int cmd_unlock(int argc
, char *argv
[])
1989 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1990 doopen(&f
, KOPEN_WRITE
);
1991 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1992 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1993 if ((*kd
)->e
!= KENC_ENCRYPT
)
1994 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1995 if (key_punlock(kd
, 0, d
.buf
))
1996 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2002 /* --- @cmd_extract@ --- */
2004 static int cmd_extract(int argc
, char *argv
[])
2010 key_filter kf
= { 0, 0 };
2012 const char *outfile
= 0;
2016 static struct option opt
[] = {
2017 { "filter", OPTF_ARGREQ
, 0, 'f' },
2020 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2026 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2028 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2036 argv
+= optind
; argc
-= optind
;
2038 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2039 if (strcmp(*argv
, "-") == 0)
2043 dstr_putf(&d
, "%s.new", outfile
);
2044 if (!(fp
= fopen(d
.buf
, "w"))) {
2045 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2046 d
.buf
, strerror(errno
));
2050 doopen(&f
, KOPEN_READ
);
2054 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2055 key_extract(&f
, k
, fp
, &kf
);
2057 for (i
= 1; i
< argc
; i
++) {
2058 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2059 key_extract(&f
, k
, fp
, &kf
);
2061 moan("key `%s' not found", argv
[i
]);
2066 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2067 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2073 /* --- @cmd_tidy@ --- */
2075 static int cmd_tidy(int argc
, char *argv
[])
2079 die(EXIT_FAILURE
, "Usage: tidy");
2080 doopen(&f
, KOPEN_WRITE
);
2081 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2086 /* --- @cmd_merge@ --- */
2088 static int cmd_merge(int argc
, char *argv
[])
2094 die(EXIT_FAILURE
, "Usage: merge FILE");
2095 if (strcmp(argv
[1], "-") == 0)
2097 else if (!(fp
= fopen(argv
[1], "r"))) {
2098 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2099 argv
[1], strerror(errno
));
2102 doopen(&f
, KOPEN_WRITE
);
2103 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2108 /* --- @cmd_show@ --- */
2112 listtab[i].name, listtab[i].name) \
2113 LI("Hash functions", hash, \
2114 ghashtab[i], ghashtab[i]->name) \
2115 LI("Elliptic curves", ec, \
2116 ectab[i].name, ectab[i].name) \
2117 LI("Prime Diffie-Hellman groups", dh, \
2118 ptab[i].name, ptab[i].name) \
2119 LI("Binary Diffie-Hellman groups", bindh, \
2120 bintab[i].name, bintab[i].name) \
2121 LI("Key-generation algorithms", keygen, \
2122 algtab[i].name, algtab[i].name) \
2123 LI("Random seeding algorithms", seed, \
2124 seedtab[i].p, seedtab[i].p) \
2125 LI("Fingerprint presentation styles", fpres, \
2126 fprestab[i].name, fprestab[i].name)
2128 MAKELISTTAB(listtab
, LISTS
)
2130 static int cmd_show(int argc
, char *argv
[])
2132 return (displaylists(listtab
, argv
+ 1));
2135 /*----- Main command table ------------------------------------------------*/
2137 static int cmd_help(int argc
, char *argv
[]);
2139 static cmd cmds
[] = {
2140 { "help", cmd_help
, "help [COMMAND...]" },
2141 { "show", cmd_show
, "show [ITEM...]" },
2142 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2145 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2146 -q, --quiet Show less information.\n\
2147 -v, --verbose Show more information.\n\
2149 { "fingerprint", cmd_finger
,
2150 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2153 -f, --filter=FILT Only hash key components matching FILT.\n\
2154 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2155 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2156 ($ show hash for list.)\n\
2158 { "verify", cmd_verify
,
2159 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2162 -f, --filter=FILT Only hash key components matching FILT.\n\
2163 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2164 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2165 ($ show hash for list.)\n\
2167 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2170 -f, --filter=FILT Only extract key components matching FILT.\n\
2172 { "merge", cmd_merge
, "merge FILE" },
2173 { "expire", cmd_expire
, "expire TAG..." },
2174 { "delete", cmd_delete
, "delete TAG..." },
2175 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2176 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2177 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2178 { "lock", cmd_lock
, "lock QTAG" },
2179 { "unlock", cmd_unlock
, "unlock QTAG" },
2180 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2183 -r, --retag Untag any key currently called new-tag.\n\
2185 { "tidy", cmd_tidy
, "tidy" },
2187 "add [-OPTIONS] TYPE [ATTR...]\n\
2188 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2189 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2190 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2193 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2194 ($ show keygen for list.)\n\
2195 -b, --bits=N Generate an N-bit key.\n\
2196 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2197 -p, --parameters=TAG Get group parameters from TAG.\n\
2198 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2199 ($ show ec or $ show dh for list.)\n\
2200 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2201 ($ show seed for list.)\n\
2202 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2203 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2204 -e, --expire=TIME Make the key expire after TIME.\n\
2205 -c, --comment=STRING Attach the command STRING to the key.\n\
2206 -t, --tag=TAG Tag the key with the name TAG.\n\
2207 -r, --retag Untag any key currently with that tag.\n\
2208 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2209 -I, --key-id=ID Force the key-id for the new key.\n\
2210 -l, --lock Lock the generated key with a passphrase.\n\
2211 -q, --quiet Don't give progress indicators while working.\n\
2212 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2213 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2214 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2219 static int cmd_help(int argc
, char *argv
[])
2221 sc_help(cmds
, stdout
, argv
+ 1);
2225 /*----- Main code ---------------------------------------------------------*/
2227 /* --- Helpful GNUy functions --- */
2229 static void usage(FILE *fp
)
2231 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2234 void version(FILE *fp
)
2236 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2239 void help_global(FILE *fp
)
2243 Performs various simple key management operations.\n\
2245 Global command line options:\n\
2247 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2248 -v, --version Display version number.\n\
2249 -u, --usage Display short usage summary.\n\
2251 -k, --keyring=FILE Read and write keys in FILE.\n",
2257 * Arguments: @int argc@ = number of command line arguments
2258 * @char *argv[]@ = array of command line arguments
2260 * Returns: Nonzero on failure.
2262 * Use: Main program. Performs simple key management functions.
2265 int main(int argc
, char *argv
[])
2271 /* --- Initialization --- */
2276 /* --- Parse command line options --- */
2279 static struct option opt
[] = {
2281 /* --- Standard GNUy help options --- */
2283 { "help", 0, 0, 'h' },
2284 { "version", 0, 0, 'v' },
2285 { "usage", 0, 0, 'u' },
2287 /* --- Real live useful options --- */
2289 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2291 /* --- Magic terminator --- */
2295 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2301 /* --- GNU help options --- */
2304 sc_help(cmds
, stdout
, argv
+ optind
);
2313 /* --- Real useful options --- */
2319 /* --- Bogosity --- */
2327 /* --- Complain about excessive bogons --- */
2329 if (f
& f_bogus
|| optind
== argc
) {
2334 /* --- Initialize the Catacomb random number generator --- */
2336 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2337 rand_seed(RAND_GLOBAL
, 160);
2339 /* --- Dispatch to appropriate command handler --- */
2344 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2347 /*----- That's all, folks -------------------------------------------------*/