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
)
249 /* --- Quick check if no parameters supplied --- */
254 /* --- Run through the checklist --- */
257 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
259 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
260 if (!KEY_MATCH(kd
, &kf
))
261 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
265 /* --- Copy over the parameters --- */
267 kd
= key_copydata(k
->p
->k
, &kf
);
269 key_setkeydata(k
->kf
, k
->k
, kd
);
272 /* --- Copy over attributes --- */
274 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
275 key_putattr(k
->kf
, k
->k
, n
, v
);
284 * Arguments: @key_data *k@ = pointer to key data block
285 * @const char *tag@ = tag string to use
287 * Returns: Pointer to multiprecision integer key item.
289 * Use: Fetches an MP key component.
292 static mp
*getmp(key_data
*k
, const char *tag
)
294 k
= key_structfind(k
, tag
);
296 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
297 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
298 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
302 /* --- @keyrand@ --- *
304 * Arguments: @key_file *kf@ = pointer to key file
305 * @const char *id@ = pointer to key id (or null)
309 * Use: Keys the random number generator.
312 static void keyrand(key_file
*kf
, const char *id
)
316 /* --- Find the key --- */
319 if ((k
= key_bytag(kf
, id
)) == 0)
320 die(EXIT_FAILURE
, "key `%s' not found", id
);
322 k
= key_bytype(kf
, "catacomb-rand");
325 key_data
*kd
= k
->k
, *kkd
;
329 switch (kd
->e
& KF_ENCMASK
) {
335 if (key_punlock(&kkd
, kd
, d
.buf
))
336 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
344 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
348 /* --- Key the generator --- */
350 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
355 /* --- Key generation algorithms --- */
357 static void alg_binary(keyopts
*k
)
367 die(EXIT_FAILURE
, "no shared parameters for binary keys");
369 sz
= (k
->bits
+ 7) >> 3;
371 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
372 k
->r
->ops
->fill(k
->r
, p
, sz
);
374 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
377 key_setkeydata(k
->kf
, k
->k
, kd
);
382 static void alg_des(keyopts
*k
)
392 die(EXIT_FAILURE
, "no shared parameters for DES keys");
393 if (k
->bits
% 56 || k
->bits
> 168)
394 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
398 k
->r
->ops
->fill(k
->r
, p
, sz
);
399 for (i
= 0; i
< sz
; i
++) {
400 octet x
= p
[i
] | 0x01;
404 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
406 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
409 key_setkeydata(k
->kf
, k
->k
, kd
);
414 static void alg_rsa(keyopts
*k
)
419 /* --- Sanity checking --- */
422 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
426 /* --- Generate the RSA parameters --- */
428 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
429 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
430 die(EXIT_FAILURE
, "RSA key generation failed");
432 /* --- Run a test encryption --- */
435 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
437 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
442 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
443 c
= rsa_qprivop(&rp
, c
, c
, g
);
446 die(EXIT_FAILURE
, "test encryption failed");
452 /* --- Allrighty then --- */
454 kd
= key_newstruct();
455 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
456 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
458 kkd
= key_newstruct();
459 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
460 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
461 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
462 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
463 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
464 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
465 dolock(k
, &kkd
, "private");
466 key_structsteal(kd
, "private", kkd
);
467 key_setkeydata(k
->kf
, k
->k
, kd
);
472 static void alg_dsaparam(keyopts
*k
)
474 static const char *pl
[] = { "q", "p", "g", 0 };
475 if (!copyparam(k
, pl
)) {
484 /* --- Choose appropriate bit lengths if necessary --- */
491 /* --- Allocate a seed block --- */
493 sz
= (k
->qbits
+ 7) >> 3;
495 k
->r
->ops
->fill(k
->r
, p
, sz
);
497 /* --- Allocate the parameters --- */
499 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
500 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
501 die(EXIT_FAILURE
, "DSA parameter generation failed");
503 /* --- Store the parameters --- */
505 kd
= key_newstruct();
506 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
507 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
508 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
512 key_setkeydata(k
->kf
, k
->k
, kd
);
515 /* --- Store the seed for future verification --- */
517 c
= base64_class
.encoder(0, "", 0);
518 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
521 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
523 dstr_putf(&d
, "%u", ds
.count
);
524 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
531 static void alg_dsa(keyopts
*k
)
538 /* --- Get the shared parameters --- */
541 key_split(&k
->k
->k
); kd
= k
->k
->k
;
546 /* --- Choose a private key --- */
548 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
549 mpmont_create(&mm
, p
);
550 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
552 /* --- Store everything away --- */
554 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
556 kkd
= key_newstruct();
557 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
558 dolock(k
, &kkd
, "private");
559 key_structsteal(kd
, "private", kkd
);
561 mp_drop(x
); mp_drop(y
);
564 static void alg_dhparam(keyopts
*k
)
566 static const char *pl
[] = { "p", "q", "g", 0 };
568 if (!copyparam(k
, pl
)) {
577 if (strcmp(k
->curve
, "list") == 0) {
579 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
583 if (dh_parse(&qd
, &dp
))
584 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
586 die(EXIT_FAILURE
, "junk at end of field spec");
587 if ((g
= group_prime(&dp
)) == 0)
588 die(EXIT_FAILURE
, "invalid prime field");
589 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
590 moan("WARNING! group check failed: %s", e
);
598 /* --- Choose a large safe prime number --- */
600 if (k
->f
& f_limlee
) {
605 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
606 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
607 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
608 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
612 for (i
= 0; i
< nf
; i
++) {
615 mp_writedstr(f
[i
], &d
, 10);
618 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
621 } else if (k
->f
& f_kcdsa
) {
624 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
625 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
630 mp_writedstr(dp
.q
, &d
, 10);
631 mp_div(&v
, 0, dp
.p
, dp
.q
);
634 mp_writedstr(v
, &d
, 10);
636 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
640 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
641 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
644 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
647 kd
= key_newstruct();
648 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
649 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
650 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
654 key_setkeydata(k
->kf
, k
->k
, kd
);
659 static void alg_dh(keyopts
*k
)
666 /* --- Get the shared parameters --- */
669 key_split(&k
->k
->k
); kd
= k
->k
->k
;
674 /* --- Choose a suitable private key --- *
676 * Since %$g$% has order %$q$%, choose %$x < q$%.
679 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
681 /* --- Compute the public key %$y = g^x \bmod p$% --- */
683 mpmont_create(&mm
, p
);
684 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
687 /* --- Store everything away --- */
689 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
691 kkd
= key_newstruct();
692 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
693 dolock(k
, &kkd
, "private");
694 key_structsteal(kd
, "private", kkd
);
696 mp_drop(x
); mp_drop(y
);
699 static void alg_bbs(keyopts
*k
)
704 /* --- Sanity checking --- */
707 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
711 /* --- Generate the BBS parameters --- */
713 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
714 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
715 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
717 /* --- Allrighty then --- */
719 kd
= key_newstruct();
720 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
722 kkd
= key_newstruct();
723 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
724 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
725 dolock(k
, &kkd
, "private");
726 key_structsteal(kd
, "private", kkd
);
727 key_setkeydata(k
->kf
, k
->k
, kd
);
733 static void alg_binparam(keyopts
*k
)
735 static const char *pl
[] = { "p", "q", "g", 0 };
736 if (!copyparam(k
, pl
)) {
743 /* --- Decide on a field --- */
745 if (!k
->bits
) k
->bits
= 128;
746 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
748 LIST("Built-in binary fields", stdout
,
749 bintab
[i
].name
, bintab
[i
].name
);
753 if (k
->bits
<= 40) k
->curve
= "p1363-40";
754 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
755 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
756 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
757 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
758 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
761 "no built-in binary fields provide %u-bit security",
766 /* --- Check it --- */
770 if (dhbin_parse(&qd
, &gb
))
771 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
773 die(EXIT_FAILURE
, "junk at end of field spec");
774 if ((g
= group_binary(&gb
)) == 0)
775 die(EXIT_FAILURE
, "invalid binary field");
776 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
777 moan("WARNING! group check failed: %s", e
);
780 /* --- Write out the answer --- */
782 kd
= key_newstruct();
783 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
784 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
785 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
789 key_setkeydata(k
->kf
, k
->k
, kd
);
794 static void alg_bin(keyopts
*k
)
801 /* --- Get the shared parameters --- */
804 key_split(&k
->k
->k
); kd
= k
->k
->k
;
809 /* --- Choose a suitable private key --- *
811 * Since %$g$% has order %$q$%, choose %$x < q$%.
814 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
816 /* --- Compute the public key %$y = g^x \bmod p$% --- */
818 gfreduce_create(&r
, p
);
819 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
820 gfreduce_destroy(&r
);
822 /* --- Store everything away --- */
824 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
826 kkd
= key_newstruct();
827 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
828 dolock(k
, &kkd
, "private");
829 key_structsteal(kd
, "private", kkd
);
831 mp_drop(x
); mp_drop(y
);
834 static void alg_ecparam(keyopts
*k
)
836 static const char *pl
[] = { "curve", 0 };
837 if (!copyparam(k
, pl
)) {
842 /* --- Decide on a curve --- */
844 if (!k
->bits
) k
->bits
= 256;
845 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
847 LIST("Built-in elliptic curves", stdout
,
848 ectab
[i
].name
, ectab
[i
].name
);
852 if (k
->bits
<= 56) k
->curve
= "secp112r1";
853 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
854 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
855 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
856 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
857 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
858 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
859 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
861 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
865 /* --- Check it --- */
867 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
868 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
869 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
870 moan("WARNING! curve check failed: %s", e
);
873 /* --- Write out the answer --- */
875 kd
= key_newstruct();
876 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
877 key_setkeydata(k
->kf
, k
->k
, kd
);
882 static void alg_ec(keyopts
*k
)
891 /* --- Get the curve --- */
894 key_split(&k
->k
->k
); kd
= k
->k
->k
;
895 if ((kkd
= key_structfind(kd
, "curve")) == 0)
896 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
897 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
898 die(EXIT_FAILURE
, "subkey `curve' is not a string");
899 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
900 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
902 /* --- Invent a private exponent and compute the public key --- */
904 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
905 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
907 /* --- Store everything away --- */
909 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
911 kkd
= key_newstruct();
912 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
913 dolock(k
, &kkd
, "private");
914 key_structsteal(kd
, "private", kkd
);
922 /* --- The algorithm tables --- */
924 typedef struct keyalg
{
926 void (*proc
)(keyopts
*o
);
930 static keyalg algtab
[] = {
931 { "binary", alg_binary
, "Plain binary data" },
932 { "des", alg_des
, "Binary with DES-style parity" },
933 { "rsa", alg_rsa
, "RSA public-key encryption" },
934 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
935 { "dsa", alg_dsa
, "DSA digital signatures" },
936 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
937 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
938 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
939 { "bindh", alg_bin
, "DH over a binary field" },
940 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
941 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
942 { "ec", alg_ec
, "Elliptic curve crypto" },
946 /* --- @cmd_add@ --- */
948 static int cmd_add(int argc
, char *argv
[])
951 time_t exp
= KEXP_EXPIRE
;
952 uint32 kid
= rand_global
.ops
->word(&rand_global
);
953 const char *tag
= 0, *ptag
= 0;
955 keyalg
*alg
= algtab
;
956 const char *rtag
= 0;
957 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
958 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
959 const char *seed
= 0;
962 /* --- Parse options for the subcommand --- */
965 static struct option opt
[] = {
966 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
967 { "bits", OPTF_ARGREQ
, 0, 'b' },
968 { "qbits", OPTF_ARGREQ
, 0, 'B' },
969 { "parameters", OPTF_ARGREQ
, 0, 'p' },
970 { "expire", OPTF_ARGREQ
, 0, 'e' },
971 { "comment", OPTF_ARGREQ
, 0, 'c' },
972 { "tag", OPTF_ARGREQ
, 0, 't' },
973 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
974 { "key-id", OPTF_ARGREQ
, 0, 'I' },
975 { "curve", OPTF_ARGREQ
, 0, 'C' },
976 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
977 { "seed", OPTF_ARGREQ
, 0, 's' },
978 { "newseed", OPTF_ARGREQ
, 0, 'n' },
979 { "lock", 0, 0, 'l' },
980 { "quiet", 0, 0, 'q' },
981 { "lim-lee", 0, 0, 'L' },
982 { "subgroup", 0, 0, 'S' },
983 { "kcdsa", 0, 0, 'K' },
986 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
991 /* --- Handle the various options --- */
995 /* --- Read an algorithm name --- */
999 size_t sz
= strlen(optarg
);
1001 if (strcmp(optarg
, "list") == 0) {
1002 for (a
= algtab
; a
->name
; a
++)
1003 printf("%-10s %s\n", a
->name
, a
->help
);
1008 for (a
= algtab
; a
->name
; a
++) {
1009 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1010 if (a
->name
[sz
] == 0) {
1014 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1020 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1023 /* --- Bits must be nonzero and a multiple of 8 --- */
1027 k
.bits
= strtoul(optarg
, &p
, 0);
1028 if (k
.bits
== 0 || *p
!= 0)
1029 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1034 k
.qbits
= strtoul(optarg
, &p
, 0);
1035 if (k
.qbits
== 0 || *p
!= 0)
1036 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1039 /* --- Parameter selection --- */
1045 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1048 if (strcmp(optarg
, "forever") == 0)
1051 exp
= get_date(optarg
, 0);
1053 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1057 /* --- Store comments without interpretation --- */
1060 if (key_chkcomment(optarg
))
1061 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1065 /* --- Elliptic curve parameters --- */
1071 /* --- Store tags --- */
1074 if (key_chkident(optarg
))
1075 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1082 /* --- Seeding --- */
1085 const struct seedalg
*ss
;
1086 if (strcmp(optarg
, "list") == 0) {
1087 printf("Seed algorithms:\n");
1088 for (ss
= seedtab
; ss
->p
; ss
++)
1089 printf(" %s\n", ss
->p
);
1092 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1094 for (ss
= seedtab
; ss
->p
; ss
++) {
1095 if (strcmp(optarg
, ss
->p
) == 0)
1099 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1106 if (seed
) die(EXIT_FAILURE
, "seed already set");
1107 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1108 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1109 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1110 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1112 k
.r
= sa
->gen(d
.buf
, d
.len
);
1121 unsigned n
= strtoul(optarg
, &p
, 0);
1122 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1123 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1124 if (seed
) die(EXIT_FAILURE
, "seed already set");
1127 rand_get(RAND_GLOBAL
, p
, n
);
1128 c
= base64_class
.encoder(0, "", 0);
1129 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1132 k
.r
= sa
->gen(p
, n
);
1135 /* --- Key id --- */
1142 id
= strtoul(optarg
, &p
, 16);
1143 if (errno
|| *p
|| id
> MASK32
)
1144 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1148 /* --- Other flags --- */
1169 /* --- Other things are bogus --- */
1177 /* --- Various sorts of bogosity --- */
1179 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1181 "Usage: add [OPTIONS] TYPE [ATTR...]");
1183 if (key_chkident(argv
[optind
]))
1184 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1186 /* --- Set up various bits of the state --- */
1188 if (exp
== KEXP_EXPIRE
)
1189 exp
= time(0) + 14 * 24 * 60 * 60;
1191 /* --- Open the file and create the basic key block --- *
1193 * Keep on generating keyids until one of them doesn't collide.
1196 doopen(&f
, KOPEN_WRITE
);
1199 /* --- Key the generator --- */
1205 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1207 else if (err
!= KERR_DUPID
)
1208 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1211 /* --- Set various simple attributes --- */
1216 if (k
.f
& f_retag
) {
1217 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1219 strcmp(kk
->tag
, tag
) == 0)
1220 key_settag(&f
, kk
, 0);
1222 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1223 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1227 int err
= key_setcomment(&f
, k
.k
, c
);
1229 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1232 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1234 key_putattr(&f
, k
.k
, "genseed", seed
);
1235 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1238 key_fulltag(k
.k
, &k
.tag
);
1240 /* --- Find the parameter key --- */
1243 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1244 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1245 if ((k
.p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
1246 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1249 /* --- Now generate the actual key data --- */
1255 dstr_destroy(&k
.tag
);
1260 /*----- Key listing -------------------------------------------------------*/
1262 /* --- Listing options --- */
1264 typedef struct listopts
{
1265 const char *tfmt
; /* Date format (@strftime@-style) */
1266 int v
; /* Verbosity level */
1267 unsigned f
; /* Various flags */
1268 time_t t
; /* Time now (for key expiry) */
1269 key_filter kf
; /* Filter for matching keys */
1272 /* --- Listing flags --- */
1274 #define f_newline 2u /* Write newline before next entry */
1275 #define f_attr 4u /* Written at least one attribute */
1276 #define f_utc 8u /* Emit UTC time, not local time */
1278 /* --- @showkeydata@ --- *
1280 * Arguments: @key_data *k@ = pointer to key to write
1281 * @int ind@ = indentation level
1282 * @listopts *o@ = listing options
1283 * @dstr *d@ = tag string for this subkey
1287 * Use: Emits a piece of key data in a human-readable format.
1290 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1292 #define INDENT(i) do { \
1294 for (_i = 0; _i < (i); _i++) { \
1299 switch (k
->e
& KF_ENCMASK
) {
1301 /* --- Binary key data --- *
1303 * Emit as a simple hex dump.
1307 const octet
*p
= k
->u
.k
.k
;
1308 const octet
*l
= p
+ k
->u
.k
.sz
;
1311 fputs(" {", stdout
);
1316 } else if (sz
% 8 == 0)
1320 printf("%02x", *p
++);
1325 fputs("}\n", stdout
);
1328 /* --- Encrypted data --- *
1330 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1331 * key. Otherwise just say that it's encrypted and move on.
1336 fputs(" encrypted\n", stdout
);
1339 if (key_punlock(&kd
, k
, d
->buf
))
1340 printf(" <failed to unlock %s>\n", d
->buf
);
1342 fputs(" encrypted", stdout
);
1343 showkeydata(kd
, ind
, o
, d
);
1349 /* --- Integer keys --- *
1351 * Emit as a large integer in decimal. This makes using the key in
1352 * `calc' or whatever easier.
1357 mp_writefile(k
->u
.m
, stdout
, 10);
1361 /* --- Strings --- */
1364 printf(" `%s'\n", k
->u
.p
);
1367 /* --- Elliptic curve points --- */
1370 if (EC_ATINF(&k
->u
.e
))
1371 fputs(" inf\n", stdout
);
1373 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1374 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1379 /* --- Structured keys --- *
1381 * Just iterate over the subkeys.
1389 fputs(" {\n", stdout
);
1390 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1391 if (!key_match(k
, &o
->kf
))
1394 printf("%s =", tag
);
1396 dstr_putf(d
, ".%s", tag
);
1397 showkeydata(k
, ind
+ 2, o
, d
);
1400 fputs("}\n", stdout
);
1407 /* --- @showkey@ --- *
1409 * Arguments: @key *k@ = pointer to key to show
1410 * @listopts *o@ = pointer to listing options
1414 * Use: Emits a listing of a particular key.
1417 static void showkey(key
*k
, listopts
*o
)
1419 char ebuf
[24], dbuf
[24];
1422 /* --- Skip the key if the filter doesn't match --- */
1424 if (!key_match(k
->k
, &o
->kf
))
1427 /* --- Sort out the expiry and deletion times --- */
1429 if (KEY_EXPIRED(o
->t
, k
->exp
))
1430 strcpy(ebuf
, "expired");
1431 else if (k
->exp
== KEXP_FOREVER
)
1432 strcpy(ebuf
, "forever");
1434 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1435 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1438 if (KEY_EXPIRED(o
->t
, k
->del
))
1439 strcpy(dbuf
, "deleted");
1440 else if (k
->del
== KEXP_FOREVER
)
1441 strcpy(dbuf
, "forever");
1443 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1444 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1447 /* --- If in compact format, just display and quit --- */
1450 if (!(o
->f
& f_newline
)) {
1451 printf("%8s %-20s %-20s %-10s %s\n",
1452 "Id", "Tag", "Type", "Expire", "Delete");
1454 printf("%08lx %-20s %-20s %-10s %s\n",
1455 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1456 k
->type
, ebuf
, dbuf
);
1461 /* --- Display the standard header --- */
1463 if (o
->f
& f_newline
)
1464 fputc('\n', stdout
);
1465 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1466 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1467 printf("type: %s\n", k
->type
);
1468 printf("expiry: %s\n", ebuf
);
1469 printf("delete: %s\n", dbuf
);
1470 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1472 /* --- Display the attributes --- */
1476 const char *av
, *an
;
1479 printf("attributes:");
1480 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1481 printf("\n %s = %s", an
, av
);
1485 fputc('\n', stdout
);
1490 /* --- If dumping requested, dump the raw key data --- */
1494 fputs("key:", stdout
);
1496 showkeydata(k
->k
, 0, o
, &d
);
1503 /* --- @cmd_list@ --- */
1505 static int cmd_list(int argc
, char *argv
[])
1509 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1511 /* --- Parse subcommand options --- */
1514 static struct option opt
[] = {
1515 { "quiet", 0, 0, 'q' },
1516 { "verbose", 0, 0, 'v' },
1517 { "utc", 0, 0, 'u' },
1518 { "filter", OPTF_ARGREQ
, 0, 'f' },
1521 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1538 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1540 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1549 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1551 /* --- Open the key file --- */
1553 doopen(&f
, KOPEN_READ
);
1556 /* --- Set up the time format --- */
1559 o
.tfmt
= "%Y-%m-%d";
1560 else if (o
.f
& f_utc
)
1561 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1563 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1565 /* --- If specific keys were requested use them, otherwise do all --- *
1567 * Some day, this might turn into a wildcard match.
1570 if (optind
< argc
) {
1572 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1575 moan("key `%s' not found", argv
[optind
]);
1579 } while (optind
< argc
);
1582 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1590 return (EXIT_FAILURE
);
1595 /*----- Command implementation --------------------------------------------*/
1597 /* --- @cmd_expire@ --- */
1599 static int cmd_expire(int argc
, char *argv
[])
1607 die(EXIT_FAILURE
, "Usage: expire TAG...");
1608 doopen(&f
, KOPEN_WRITE
);
1609 for (i
= 1; i
< argc
; i
++) {
1610 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1613 moan("key `%s' not found", argv
[i
]);
1621 /* --- @cmd_delete@ --- */
1623 static int cmd_delete(int argc
, char *argv
[])
1631 die(EXIT_FAILURE
, "Usage: delete TAG...");
1632 doopen(&f
, KOPEN_WRITE
);
1633 for (i
= 1; i
< argc
; i
++) {
1634 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1637 moan("key `%s' not found", argv
[i
]);
1645 /* --- @cmd_setattr@ --- */
1647 static int cmd_setattr(int argc
, char *argv
[])
1653 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1654 doopen(&f
, KOPEN_WRITE
);
1655 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1656 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1657 setattr(&f
, k
, argv
+ 2);
1662 /* --- @cmd_getattr@ --- */
1664 static int cmd_getattr(int argc
, char *argv
[])
1672 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1673 doopen(&f
, KOPEN_READ
);
1674 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1675 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1677 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1678 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1685 /* --- @cmd_finger@ --- */
1687 static const struct fpres
{
1689 const codec_class
*cdc
;
1690 unsigned short ival
;
1693 { "hex", &hex_class
, 8, "-:" },
1694 { "base32", &base32_class
, 6, ":" },
1698 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1699 const gchash
*ch
, const key_filter
*kf
)
1702 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1708 if (key_fingerprint(k
, h
, kf
)) {
1710 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1711 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1713 for (i
= 0; i
< dd
.len
; i
++) {
1714 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1715 dstr_putc(&d
, dd
.buf
[i
]);
1717 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1718 dstr_write(&d
, stdout
);
1720 dstr_destroy(&d
); dstr_destroy(&dd
);
1724 static const struct fpres
*lookup_fpres(const char *name
)
1726 const struct fpres
*fpres
;
1727 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1728 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1729 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1732 static int cmd_finger(int argc
, char *argv
[])
1736 const struct fpres
*fpres
= fprestab
;
1737 const gchash
*ch
= &rmd160
;
1738 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1741 static struct option opt
[] = {
1742 { "filter", OPTF_ARGREQ
, 0, 'f' },
1743 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1744 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1747 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1753 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1755 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1758 fpres
= lookup_fpres(optarg
);
1761 if ((ch
= ghash_byname(optarg
)) == 0)
1762 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1770 argv
+= optind
; argc
-= optind
;
1773 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1776 doopen(&f
, KOPEN_READ
);
1780 for (i
= 0; i
< argc
; i
++) {
1781 key
*k
= key_bytag(&f
, argv
[i
]);
1783 fingerprint(k
, fpres
, ch
, &kf
);
1786 moan("key `%s' not found", argv
[i
]);
1792 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1793 fingerprint(k
, fpres
, ch
, &kf
);
1798 /* --- @cmd_verify@ --- */
1800 static int cmd_verify(int argc
, char *argv
[])
1804 const gchash
*ch
= &rmd160
;
1808 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1811 const struct fpres
*fpres
= fprestab
;
1812 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1815 static struct option opt
[] = {
1816 { "filter", OPTF_ARGREQ
, 0, 'f' },
1817 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1818 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1821 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1827 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1829 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1832 fpres
= lookup_fpres(optarg
);
1835 if ((ch
= ghash_byname(optarg
)) == 0)
1836 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1844 argv
+= optind
; argc
-= optind
;
1845 if (rc
|| argc
!= 2) {
1847 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1850 doopen(&f
, KOPEN_READ
);
1852 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1853 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1854 for (p
= argv
[1]; *p
; p
++) {
1855 if (strchr(fpres
->sep
, *p
)) continue;
1858 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1859 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1860 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1861 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1862 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1864 if (d
.len
!= ch
->hashsz
) {
1865 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1866 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1869 if (!key_fingerprint(k
, h
, &kf
))
1870 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1871 fpr
= GH_DONE(h
, 0);
1872 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1873 die(EXIT_FAILURE
, "key fingerprint mismatch");
1874 dstr_destroy(&d
); dstr_destroy(&dd
);
1879 /* --- @cmd_comment@ --- */
1881 static int cmd_comment(int argc
, char *argv
[])
1887 if (argc
< 2 || argc
> 3)
1888 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1889 doopen(&f
, KOPEN_WRITE
);
1890 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1891 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1892 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1893 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1898 /* --- @cmd_tag@ --- */
1900 static int cmd_tag(int argc
, char *argv
[])
1909 static struct option opt
[] = {
1910 { "retag", 0, 0, 'r' },
1913 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1926 argv
+= optind
; argc
-= optind
;
1927 if (argc
< 1 || argc
> 2 || rc
)
1928 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1929 doopen(&f
, KOPEN_WRITE
);
1930 if (flags
& f_retag
) {
1931 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1932 key_settag(&f
, k
, 0);
1934 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1935 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1936 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1937 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1942 /* --- @cmd_lock@ --- */
1944 static int cmd_lock(int argc
, char *argv
[])
1952 die(EXIT_FAILURE
, "Usage: lock QTAG");
1953 doopen(&f
, KOPEN_WRITE
);
1954 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1955 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1956 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
1957 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1958 if (key_plock(kd
, 0, d
.buf
))
1959 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1965 /* --- @cmd_unlock@ --- */
1967 static int cmd_unlock(int argc
, char *argv
[])
1975 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1976 doopen(&f
, KOPEN_WRITE
);
1977 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1978 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1979 if ((*kd
)->e
!= KENC_ENCRYPT
)
1980 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1981 if (key_punlock(kd
, 0, d
.buf
))
1982 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1988 /* --- @cmd_extract@ --- */
1990 static int cmd_extract(int argc
, char *argv
[])
1996 key_filter kf
= { 0, 0 };
1998 const char *outfile
= 0;
2002 static struct option opt
[] = {
2003 { "filter", OPTF_ARGREQ
, 0, 'f' },
2006 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2012 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2014 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2022 argv
+= optind
; argc
-= optind
;
2024 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2025 if (strcmp(*argv
, "-") == 0)
2029 dstr_putf(&d
, "%s.new", outfile
);
2030 if (!(fp
= fopen(d
.buf
, "w"))) {
2031 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2032 d
.buf
, strerror(errno
));
2036 doopen(&f
, KOPEN_READ
);
2040 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2041 key_extract(&f
, k
, fp
, &kf
);
2043 for (i
= 1; i
< argc
; i
++) {
2044 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2045 key_extract(&f
, k
, fp
, &kf
);
2047 moan("key `%s' not found", argv
[i
]);
2052 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2053 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2059 /* --- @cmd_tidy@ --- */
2061 static int cmd_tidy(int argc
, char *argv
[])
2065 die(EXIT_FAILURE
, "Usage: tidy");
2066 doopen(&f
, KOPEN_WRITE
);
2067 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2072 /* --- @cmd_merge@ --- */
2074 static int cmd_merge(int argc
, char *argv
[])
2080 die(EXIT_FAILURE
, "Usage: merge FILE");
2081 if (strcmp(argv
[1], "-") == 0)
2083 else if (!(fp
= fopen(argv
[1], "r"))) {
2084 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2085 argv
[1], strerror(errno
));
2088 doopen(&f
, KOPEN_WRITE
);
2089 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2094 /* --- @cmd_show@ --- */
2098 listtab[i].name, listtab[i].name) \
2099 LI("Hash functions", hash, \
2100 ghashtab[i], ghashtab[i]->name) \
2101 LI("Elliptic curves", ec, \
2102 ectab[i].name, ectab[i].name) \
2103 LI("Prime Diffie-Hellman groups", dh, \
2104 ptab[i].name, ptab[i].name) \
2105 LI("Binary Diffie-Hellman groups", bindh, \
2106 bintab[i].name, bintab[i].name) \
2107 LI("Key-generation algorithms", keygen, \
2108 algtab[i].name, algtab[i].name) \
2109 LI("Random seeding algorithms", seed, \
2110 seedtab[i].p, seedtab[i].p) \
2111 LI("Fingerprint presentation styles", fpres, \
2112 fprestab[i].name, fprestab[i].name)
2114 MAKELISTTAB(listtab
, LISTS
)
2116 static int cmd_show(int argc
, char *argv
[])
2118 return (displaylists(listtab
, argv
+ 1));
2121 /*----- Main command table ------------------------------------------------*/
2123 static int cmd_help(int argc
, char *argv
[]);
2125 static cmd cmds
[] = {
2126 { "help", cmd_help
, "help [COMMAND...]" },
2127 { "show", cmd_show
, "show [ITEM...]" },
2128 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2131 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2132 -q, --quiet Show less information.\n\
2133 -v, --verbose Show more information.\n\
2135 { "fingerprint", cmd_finger
,
2136 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2139 -f, --filter=FILT Only hash key components matching FILT.\n\
2140 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2141 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2142 ($ show hash for list.)\n\
2144 { "verify", cmd_verify
,
2145 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2148 -f, --filter=FILT Only hash key components matching FILT.\n\
2149 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2150 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2151 ($ show hash for list.)\n\
2153 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2156 -f, --filter=FILT Only extract key components matching FILT.\n\
2158 { "merge", cmd_merge
, "merge FILE" },
2159 { "expire", cmd_expire
, "expire TAG..." },
2160 { "delete", cmd_delete
, "delete TAG..." },
2161 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2162 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2163 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2164 { "lock", cmd_lock
, "lock QTAG" },
2165 { "unlock", cmd_unlock
, "unlock QTAG" },
2166 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2169 -r, --retag Untag any key currently called new-tag.\n\
2171 { "tidy", cmd_tidy
, "tidy" },
2173 "add [-OPTIONS] TYPE [ATTR...]\n\
2174 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2175 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2176 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2179 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2180 ($ show keygen for list.)\n\
2181 -b, --bits=N Generate an N-bit key.\n\
2182 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2183 -p, --parameters=TAG Get group parameters from TAG.\n\
2184 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2185 ($ show ec or $ show dh for list.)\n\
2186 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2187 ($ show seed for list.)\n\
2188 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2189 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2190 -e, --expire=TIME Make the key expire after TIME.\n\
2191 -c, --comment=STRING Attach the command STRING to the key.\n\
2192 -t, --tag=TAG Tag the key with the name TAG.\n\
2193 -r, --retag Untag any key currently with that tag.\n\
2194 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2195 -I, --key-id=ID Force the key-id for the new key.\n\
2196 -l, --lock Lock the generated key with a passphrase.\n\
2197 -q, --quiet Don't give progress indicators while working.\n\
2198 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2199 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2200 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2205 static int cmd_help(int argc
, char *argv
[])
2207 sc_help(cmds
, stdout
, argv
+ 1);
2211 /*----- Main code ---------------------------------------------------------*/
2213 /* --- Helpful GNUy functions --- */
2215 static void usage(FILE *fp
)
2217 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2220 void version(FILE *fp
)
2222 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2225 void help_global(FILE *fp
)
2229 Performs various simple key management operations.\n\
2231 Global command line options:\n\
2233 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2234 -v, --version Display version number.\n\
2235 -u, --usage Display short usage summary.\n\
2237 -k, --keyring=FILE Read and write keys in FILE.\n",
2243 * Arguments: @int argc@ = number of command line arguments
2244 * @char *argv[]@ = array of command line arguments
2246 * Returns: Nonzero on failure.
2248 * Use: Main program. Performs simple key management functions.
2251 int main(int argc
, char *argv
[])
2257 /* --- Initialization --- */
2262 /* --- Parse command line options --- */
2265 static struct option opt
[] = {
2267 /* --- Standard GNUy help options --- */
2269 { "help", 0, 0, 'h' },
2270 { "version", 0, 0, 'v' },
2271 { "usage", 0, 0, 'u' },
2273 /* --- Real live useful options --- */
2275 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2277 /* --- Magic terminator --- */
2281 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2287 /* --- GNU help options --- */
2290 sc_help(cmds
, stdout
, argv
+ optind
);
2299 /* --- Real useful options --- */
2305 /* --- Bogosity --- */
2313 /* --- Complain about excessive bogons --- */
2315 if (f
& f_bogus
|| optind
== argc
) {
2320 /* --- Initialize the Catacomb random number generator --- */
2322 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2323 rand_seed(RAND_GLOBAL
, 160);
2325 /* --- Dispatch to appropriate command handler --- */
2330 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2333 /*----- That's all, folks -------------------------------------------------*/