3 * $Id: keyutil.c,v 1.10 2000/10/08 12:02:21 mdw Exp $
5 * Simple key manager program
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.10 2000/10/08 12:02:21 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.9 2000/08/15 21:40:49 mdw
37 * Minor formatting change in listing attributes.
39 * Revision 1.8 2000/07/29 09:59:13 mdw
40 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
42 * Revision 1.7 2000/07/01 11:18:51 mdw
43 * Use new interfaces for key manipulation.
45 * Revision 1.6 2000/06/17 11:28:22 mdw
46 * Use secure memory interface from MP library. `rand_getgood' is
49 * Revision 1.5 2000/02/12 18:21:03 mdw
50 * Overhaul of key management (again).
52 * Revision 1.4 1999/12/22 15:48:10 mdw
53 * Track new key-management changes. Support new key generation
56 * Revision 1.3 1999/11/02 15:23:24 mdw
57 * Fix newlines in keyring list.
59 * Revision 1.2 1999/10/15 21:05:28 mdw
60 * In `key list', show timezone for local times, and support `-u' option
63 * Revision 1.1 1999/09/03 08:41:12 mdw
68 /*----- Header files ------------------------------------------------------*/
78 #include <mLib/base64.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
100 /*----- Handy global state ------------------------------------------------*/
102 static const char *keyfile
= "keyring";
104 /*----- Useful shared functions -------------------------------------------*/
106 /* --- @doopen@ --- *
108 * Arguments: @key_file *f@ = pointer to key file block
109 * @unsigned how@ = method to open file with
113 * Use: Opens a key file and handles errors by panicking
117 static void doopen(key_file
*f
, unsigned how
)
119 if (key_open(f
, keyfile
, how
, key_moan
, 0))
120 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
123 /* --- @doclose@ --- *
125 * Arguments: @key_file *f@ = pointer to key file block
129 * Use: Closes a key file and handles errors by panicking
133 static void doclose(key_file
*f
)
135 switch (key_close(f
)) {
137 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
138 keyfile
, strerror(errno
));
140 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
141 keyfile
, strerror(errno
));
145 /* --- @setattr@ --- *
147 * Arguments: @key_file *f@ = pointer to key file block
148 * @key *k@ = pointer to key block
149 * @char *v[]@ = array of assignments (overwritten!)
153 * Use: Applies the attribute assignments to the key.
156 static void setattr(key_file
*f
, key
*k
, char *v
[])
161 size_t eq
= strcspn(p
, "=");
163 moan("invalid assignment: `%s'", p
);
166 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
167 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
172 /*----- Key generation ----------------------------------------------------*/
174 /* --- Key generation parameters --- */
176 typedef struct keyopts
{
177 key_file
*kf
; /* Pointer to key file */
178 key
*k
; /* Pointer to the actual key */
179 dstr tag
; /* Full tag name for the key */
180 unsigned f
; /* Flags for the new key */
181 unsigned bits
, qbits
; /* Bit length for the new key */
182 key
*p
; /* Parameters key-data */
186 f_bogus
= 1, /* Error in parsing */
187 f_lock
= 2, /* Passphrase-lock private key */
188 f_quiet
= 4, /* Don't show a progress indicator */
189 f_limlee
= 8, /* Generate Lim-Lee primes */
190 f_subgroup
= 16 /* Generate a subgroup */
193 /* --- @dolock@ --- *
195 * Arguments: @keyopts *k@ = key generation options
196 * @key_data *kd@ = pointer to key data to lock
197 * @const char *t@ = tag suffix or null
201 * Use: Does passphrase locking on new keys.
204 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
206 if (!(k
->f
& f_lock
))
209 dstr_putf(&k
->tag
, ".%s", t
);
210 if (key_plock(k
->tag
.buf
, kd
, kd
))
211 die(EXIT_FAILURE
, "couldn't lock key");
216 * Arguments: @key_data *kd@ = pointer to parent key block
217 * @const char *tag@ = pointer to tag string
218 * @mp *m@ = integer to store
219 * @unsigned f@ = flags to set
223 * Use: Sets a multiprecision integer subkey.
226 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
228 key_data
*kkd
= key_structcreate(kd
, tag
);
233 /* --- @copyparam@ --- *
235 * Arguments: @keyopts *k@ = pointer to key options
236 * @const char **pp@ = checklist of parameters
238 * Returns: Nonzero if parameters copied; zero if you have to generate
241 * Use: Copies parameters from a source key to the current one.
244 static int copyparam(keyopts
*k
, const char **pp
)
248 /* --- Quick check if no parameters supplied --- */
253 /* --- Run through the checklist --- */
256 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
258 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
259 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
260 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
264 /* --- Copy over the parameters --- */
268 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
269 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
275 * Arguments: @key_data *k@ = pointer to key data block
276 * @const char *tag@ = tag string to use
278 * Returns: Pointer to multiprecision integer key item.
280 * Use: Fetches an MP key component.
283 static mp
*getmp(key_data
*k
, const char *tag
)
285 k
= key_structfind(k
, tag
);
287 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
288 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
289 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
293 /* --- @keyrand@ --- *
295 * Arguments: @key_file *kf@ = pointer to key file
296 * @const char *id@ = pointer to key id (or null)
300 * Use: Keys the random number generator.
303 static void keyrand(key_file
*kf
, const char *id
)
307 /* --- Find the key --- */
310 if ((k
= key_bytag(kf
, id
)) == 0)
311 die(EXIT_FAILURE
, "key `%s' not found", id
);
313 k
= key_bytype(kf
, "catacomb-rand");
316 key_data
*kd
= &k
->k
, kkd
;
319 switch (kd
->e
& KF_ENCMASK
) {
325 if (key_punlock(d
.buf
, kd
, &kkd
))
326 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
333 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
337 /* --- Key the generator --- */
339 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
345 /* --- Key generation algorithms --- */
347 static void alg_binary(keyopts
*k
)
356 die(EXIT_FAILURE
, "no shared parameters for binary keys");
358 sz
= (k
->bits
+ 7) >> 3;
360 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
361 rand_get(RAND_GLOBAL
, p
, sz
);
363 key_binary(&k
->k
->k
, p
, sz
);
364 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
367 dolock(k
, &k
->k
->k
, 0);
370 static void alg_des(keyopts
*k
)
379 die(EXIT_FAILURE
, "no shared parameters for DES keys");
380 if (k
->bits
% 56 || k
->bits
> 168)
381 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
385 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
386 for (i
= 0; i
< sz
; i
++) {
387 octet x
= p
[i
] | 0x01;
391 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
393 key_binary(&k
->k
->k
, p
, sz
);
394 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
397 dolock(k
, &k
->k
->k
, 0);
400 static void alg_rsa(keyopts
*k
)
405 /* --- Sanity checking --- */
408 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
412 /* --- Generate the RSA parameters --- */
414 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
415 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
416 die(EXIT_FAILURE
, "RSA key generation failed");
418 /* --- Run a test encryption --- */
421 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
423 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
428 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
429 c
= rsa_qprivop(&rp
, c
, c
, g
);
432 die(EXIT_FAILURE
, "test encryption failed");
438 /* --- Allrighty then --- */
442 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
443 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
445 kd
= key_structcreate(kd
, "private");
447 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
448 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
449 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
450 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
451 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
452 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
453 dolock(k
, kd
, "private");
458 static void alg_dsaparam(keyopts
*k
)
460 static const char *pl
[] = { "q", "p", "g", 0 };
461 if (!copyparam(k
, pl
)) {
467 key_data
*kd
= &k
->k
->k
;
469 /* --- Choose appropriate bit lengths if necessary --- */
476 /* --- Allocate a seed block --- */
478 sz
= (k
->qbits
+ 7) >> 3;
480 rand_get(RAND_GLOBAL
, p
, sz
);
482 /* --- Allocate the parameters --- */
484 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
,
485 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
486 die(EXIT_FAILURE
, "DSA parameter generation failed");
488 /* --- Store the parameters --- */
491 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
492 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
493 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
498 /* --- Store the seed for future verification --- */
503 base64_encode(&c
, p
, sz
, &d
);
504 base64_encode(&c
, 0, 0, &d
);
505 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
511 static void alg_dsa(keyopts
*k
)
516 key_data
*kd
= &k
->k
->k
;
518 /* --- Get the shared parameters --- */
525 /* --- Choose a private key --- */
527 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
528 mpmont_create(&mm
, p
);
529 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
531 /* --- Store everything away --- */
533 mpkey(kd
, "y", y
, KCAT_PUB
);
535 kd
= key_structcreate(kd
, "private");
537 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
538 dolock(k
, kd
, "private");
540 mp_drop(x
); mp_drop(y
);
543 static void alg_dhparam(keyopts
*k
)
545 static const char *pl
[] = { "p", "q", "g", 0 };
546 if (!copyparam(k
, pl
)) {
548 key_data
*kd
= &k
->k
->k
;
554 /* --- Choose a large safe prime number --- */
556 if (k
->f
& f_limlee
) {
561 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
562 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
563 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
564 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
568 for (i
= 0; i
< nf
; i
++) {
571 mp_writedstr(f
[i
], &d
, 10);
574 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
578 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
579 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
582 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
585 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
586 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
587 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
594 static void alg_dh(keyopts
*k
)
599 key_data
*kd
= &k
->k
->k
;
601 /* --- Get the shared parameters --- */
608 /* --- Choose a suitable private key --- *
610 * Since %$g$% has order %$q$%, choose %$x < q$%.
613 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
615 /* --- Compute the public key %$y = g^x \bmod p$% --- */
617 mpmont_create(&mm
, p
);
618 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
621 /* --- Store everything away --- */
623 mpkey(kd
, "y", y
, KCAT_PUB
);
625 kd
= key_structcreate(kd
, "private");
627 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
628 dolock(k
, kd
, "private");
630 mp_drop(x
); mp_drop(y
);
633 static void alg_bbs(keyopts
*k
)
638 /* --- Sanity checking --- */
641 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
645 /* --- Generate the BBS parameters --- */
647 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
648 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
649 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
651 /* --- Allrighty then --- */
655 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
657 kd
= key_structcreate(kd
, "private");
659 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
660 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
661 dolock(k
, kd
, "private");
666 /* --- The algorithm tables --- */
668 typedef struct keyalg
{
670 void (*proc
)(keyopts
*o
);
674 static keyalg algtab
[] = {
675 { "binary", alg_binary
, "Plain binary data" },
676 { "des", alg_des
, "Binary with DES-style parity" },
677 { "rsa", alg_rsa
, "RSA public-key encryption" },
678 { "dsa", alg_dsa
, "DSA digital signatures" },
679 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
680 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
681 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
682 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
686 /* --- @cmd_add@ --- */
688 static int cmd_add(int argc
, char *argv
[])
691 time_t exp
= KEXP_EXPIRE
;
692 const char *tag
= 0, *ptag
= 0;
694 keyalg
*alg
= algtab
;
695 const char *rtag
= 0;
696 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
698 /* --- Parse options for the subcommand --- */
701 static struct option opt
[] = {
702 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
703 { "bits", OPTF_ARGREQ
, 0, 'b' },
704 { "qbits", OPTF_ARGREQ
, 0, 'B' },
705 { "parameters", OPTF_ARGREQ
, 0, 'p' },
706 { "expire", OPTF_ARGREQ
, 0, 'e' },
707 { "comment", OPTF_ARGREQ
, 0, 'c' },
708 { "tag", OPTF_ARGREQ
, 0, 't' },
709 { "rand-id", OPTF_ARGREQ
, 0, 'r' },
710 { "lock", 0, 0, 'l' },
711 { "quiet", 0, 0, 'q' },
712 { "lim-lee", 0, 0, 'L' },
713 { "subgroup", 0, 0, 'S' },
716 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:r:lqLS", opt
, 0, 0, 0);
720 /* --- Handle the various options --- */
724 /* --- Read an algorithm name --- */
728 size_t sz
= strlen(optarg
);
730 if (strcmp(optarg
, "list") == 0) {
731 for (a
= algtab
; a
->name
; a
++)
732 printf("%-10s %s\n", a
->name
, a
->help
);
737 for (a
= algtab
; a
->name
; a
++) {
738 if (strncmp(optarg
, a
->name
, sz
) == 0) {
739 if (a
->name
[sz
] == 0) {
743 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
749 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
752 /* --- Bits must be nonzero and a multiple of 8 --- */
756 k
.bits
= strtoul(optarg
, &p
, 0);
757 if (k
.bits
== 0 || *p
!= 0)
758 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
763 k
.qbits
= strtoul(optarg
, &p
, 0);
764 if (k
.qbits
== 0 || *p
!= 0)
765 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
768 /* --- Parameter selection --- */
774 /* --- Expiry dates get passed to @get_date@ for parsing --- */
777 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
780 exp
= get_date(optarg
, 0);
782 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
786 /* --- Store comments without interpretation --- */
789 if (key_chkcomment(optarg
))
790 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
794 /* --- Store tags --- */
797 if (key_chkident(optarg
))
798 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
802 /* --- Other flags --- */
820 /* --- Other things are bogus --- */
828 /* --- Various sorts of bogosity --- */
830 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
832 "Usage: add [options] type [attr...]");
834 if (key_chkident(argv
[optind
]))
835 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
837 /* --- Set up various bits of the state --- */
839 if (exp
== KEXP_EXPIRE
)
840 exp
= time(0) + 14 * 24 * 60 * 60;
842 /* --- Open the file and create the basic key block --- *
844 * Keep on generating keyids until one of them doesn't collide.
847 doopen(&f
, KOPEN_WRITE
);
850 /* --- Key the generator --- */
855 uint32 id
= rand_global
.ops
->word(&rand_global
);
857 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
860 if (err
!= KERR_DUPID
)
861 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
864 /* --- Set various simple attributes --- */
867 int err
= key_settag(&f
, k
.k
, tag
);
869 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
873 int err
= key_setcomment(&f
, k
.k
, c
);
875 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
878 setattr(&f
, k
.k
, argv
+ optind
+ 1);
880 key_fulltag(k
.k
, &k
.tag
);
882 /* --- Find the parameter key --- */
885 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
886 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
887 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
888 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
891 /* --- Now generate the actual key data --- */
901 /*----- Key listing -------------------------------------------------------*/
903 /* --- Listing options --- */
905 typedef struct listopts
{
906 const char *tfmt
; /* Date format (@strftime@-style) */
907 int v
; /* Verbosity level */
908 unsigned f
; /* Various flags */
909 time_t t
; /* Time now (for key expiry) */
910 key_filter kf
; /* Filter for matching keys */
913 /* --- Listing flags --- */
916 f_newline
= 2, /* Write newline before next entry */
917 f_attr
= 4, /* Written at least one attribute */
918 f_utc
= 8 /* Emit UTC time, not local time */
921 /* --- @showkeydata@ --- *
923 * Arguments: @key_data *k@ = pointer to key to write
924 * @int ind@ = indentation level
925 * @listopts *o@ = listing options
926 * @dstr *d@ = tag string for this subkey
930 * Use: Emits a piece of key data in a human-readable format.
933 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
935 #define INDENT(i) do { \
937 for (_i = 0; _i < (i); _i++) { \
942 switch (k
->e
& KF_ENCMASK
) {
944 /* --- Binary key data --- *
946 * Emit as a simple hex dump.
950 const octet
*p
= k
->u
.k
.k
;
951 const octet
*l
= p
+ k
->u
.k
.sz
;
959 } else if (sz
% 8 == 0)
963 printf("%02x", *p
++);
968 fputs("}\n", stdout
);
971 /* --- Encrypted data --- *
973 * If the user is sufficiently keen, ask for a passphrase and decrypt the
974 * key. Otherwise just say that it's encrypted and move on.
979 fputs(" encrypted\n", stdout
);
982 if (key_punlock(d
->buf
, k
, &kd
))
983 printf(" <failed to unlock %s>\n", d
->buf
);
985 fputs(" encrypted", stdout
);
986 showkeydata(&kd
, ind
, o
, d
);
992 /* --- Integer keys --- *
994 * Emit as a large integer in decimal. This makes using the key in
995 * `calc' or whatever easier.
1000 mp_writefile(k
->u
.m
, stdout
, 10);
1004 /* --- Structured keys --- *
1006 * Just iterate over the subkeys.
1014 fputs(" {\n", stdout
);
1015 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1016 if (!key_match(&ks
->k
, &o
->kf
))
1019 printf("%s =", SYM_NAME(ks
));
1021 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1022 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1025 fputs("}\n", stdout
);
1032 /* --- @showkey@ --- *
1034 * Arguments: @key *k@ = pointer to key to show
1035 * @listopts *o@ = pointer to listing options
1039 * Use: Emits a listing of a particular key.
1042 static void showkey(key
*k
, listopts
*o
)
1044 char ebuf
[24], dbuf
[24];
1047 /* --- Skip the key if the filter doesn't match --- */
1049 if (!key_match(&k
->k
, &o
->kf
))
1052 /* --- Sort out the expiry and deletion times --- */
1054 if (KEY_EXPIRED(o
->t
, k
->exp
))
1055 strcpy(ebuf
, "expired");
1056 else if (k
->exp
== KEXP_FOREVER
)
1057 strcpy(ebuf
, "forever");
1059 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1060 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1063 if (KEY_EXPIRED(o
->t
, k
->del
))
1064 strcpy(dbuf
, "deleted");
1065 else if (k
->del
== KEXP_FOREVER
)
1066 strcpy(dbuf
, "forever");
1068 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1069 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1072 /* --- If in compact format, just display and quit --- */
1075 if (!(o
->f
& f_newline
)) {
1076 printf("%8s %-20s %-20s %-10s %-10s\n",
1077 "Id", "Tag", "Type", "Expire", "Delete");
1079 printf("%08lx %-20s %-20s %-10s %-10s\n",
1080 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1081 k
->type
, ebuf
, dbuf
);
1086 /* --- Display the standard header --- */
1088 if (o
->f
& f_newline
)
1089 fputc('\n', stdout
);
1090 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1091 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1092 printf("type: %s\n", k
->type
);
1093 printf("expiry: %s\n", ebuf
);
1094 printf("delete: %s\n", dbuf
);
1095 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1097 /* --- Display the attributes --- */
1101 const char *av
, *an
;
1104 printf("attributes:");
1105 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1106 printf("\n %s = %s", an
, av
);
1110 fputc('\n', stdout
);
1115 /* --- If dumping requested, dump the raw key data --- */
1119 fputs("key:", stdout
);
1121 showkeydata(&k
->k
, 0, o
, &d
);
1128 /* --- @cmd_list@ --- */
1130 static int cmd_list(int argc
, char *argv
[])
1134 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1136 /* --- Parse subcommand options --- */
1139 static struct option opt
[] = {
1140 { "quiet", 0, 0, 'q' },
1141 { "verbose", 0, 0, 'v' },
1142 { "utc", 0, 0, 'u' },
1143 { "filter", OPTF_ARGREQ
, 0, 'f' },
1146 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1163 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1165 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1174 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1176 /* --- Open the key file --- */
1178 doopen(&f
, KOPEN_READ
);
1181 /* --- Set up the time format --- */
1184 o
.tfmt
= "%Y-%m-%d";
1185 else if (o
.f
& f_utc
)
1186 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1188 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1190 /* --- If specific keys were requested use them, otherwise do all --- *
1192 * Some day, this might turn into a wildcard match.
1195 if (optind
< argc
) {
1197 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1200 moan("key `%s' not found", argv
[optind
]);
1204 } while (optind
< argc
);
1207 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1215 return (EXIT_FAILURE
);
1220 /*----- Command implementation --------------------------------------------*/
1222 /* --- @cmd_expire@ --- */
1224 static int cmd_expire(int argc
, char *argv
[])
1232 die(EXIT_FAILURE
, "Usage: expire tag...");
1233 doopen(&f
, KOPEN_WRITE
);
1234 for (i
= 1; i
< argc
; i
++) {
1235 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1238 moan("key `%s' not found", argv
[i
]);
1246 /* --- @cmd_delete@ --- */
1248 static int cmd_delete(int argc
, char *argv
[])
1256 die(EXIT_FAILURE
, "Usage: delete tag...");
1257 doopen(&f
, KOPEN_WRITE
);
1258 for (i
= 1; i
< argc
; i
++) {
1259 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1262 moan("key `%s' not found", argv
[i
]);
1270 /* --- @cmd_setattr@ --- */
1272 static int cmd_setattr(int argc
, char *argv
[])
1278 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1279 doopen(&f
, KOPEN_WRITE
);
1280 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1281 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1282 setattr(&f
, k
, argv
+ 2);
1287 /* --- @cmd_finger@ --- */
1289 static void fingerprint(key
*k
, const key_filter
*kf
)
1292 octet hash
[RMD160_HASHSZ
];
1296 if (!key_encode(&k
->k
, &d
, kf
))
1299 rmd160_hash(&r
, d
.buf
, d
.len
);
1300 rmd160_done(&r
, hash
);
1304 for (i
= 0; i
< sizeof(hash
); i
++) {
1305 if (i
&& i
% 4 == 0)
1307 printf("%02x", hash
[i
]);
1309 printf(" %s\n", d
.buf
);
1313 static int cmd_finger(int argc
, char *argv
[])
1317 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1320 static struct option opt
[] = {
1321 { "filter", OPTF_ARGREQ
, 0, 'f' },
1324 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1330 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1332 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1340 argv
+= optind
; argc
-= optind
;
1342 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1344 doopen(&f
, KOPEN_READ
);
1348 for (i
= 0; i
< argc
; i
++) {
1349 key
*k
= key_bytag(&f
, argv
[i
]);
1351 fingerprint(k
, &kf
);
1354 moan("key `%s' not found", argv
[i
]);
1360 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1361 fingerprint(k
, &kf
);
1366 /* --- @cmd_comment@ --- */
1368 static int cmd_comment(int argc
, char *argv
[])
1374 if (argc
< 2 || argc
> 3)
1375 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1376 doopen(&f
, KOPEN_WRITE
);
1377 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1378 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1379 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1380 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1385 /* --- @cmd_tag@ --- */
1387 static int cmd_tag(int argc
, char *argv
[])
1393 if (argc
< 2 || argc
> 3)
1394 die(EXIT_FAILURE
, "Usage: tag tag [new-tag]");
1395 doopen(&f
, KOPEN_WRITE
);
1396 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1397 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1398 if ((err
= key_settag(&f
, k
, argv
[2])) != 0)
1399 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[2], key_strerror(err
));
1404 /* --- @cmd_lock@ --- */
1406 static int cmd_lock(int argc
, char *argv
[])
1414 die(EXIT_FAILURE
, "Usage: lock qtag");
1415 doopen(&f
, KOPEN_WRITE
);
1416 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1417 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1418 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1419 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1420 if (key_plock(d
.buf
, kd
, kd
))
1421 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1427 /* --- @cmd_unlock@ --- */
1429 static int cmd_unlock(int argc
, char *argv
[])
1437 die(EXIT_FAILURE
, "Usage: unlock qtag");
1438 doopen(&f
, KOPEN_WRITE
);
1439 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1440 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1441 if (kd
->e
!= KENC_ENCRYPT
)
1442 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1443 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1444 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1450 /* --- @cmd_extract@ --- */
1452 static int cmd_extract(int argc
, char *argv
[])
1458 key_filter kf
= { 0, 0 };
1462 static struct option opt
[] = {
1463 { "filter", OPTF_ARGREQ
, 0, 'f' },
1466 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1472 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1474 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1482 argv
+= optind
; argc
-= optind
;
1484 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1485 if (strcmp(*argv
, "-") == 0)
1487 else if (!(fp
= fopen(*argv
, "w"))) {
1488 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1489 *argv
, strerror(errno
));
1492 doopen(&f
, KOPEN_READ
);
1496 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1497 key_extract(&f
, k
, fp
, &kf
);
1499 for (i
= 1; i
< argc
; i
++) {
1500 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1501 key_extract(&f
, k
, fp
, &kf
);
1503 moan("key `%s' not found", argv
[i
]);
1509 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1514 /* --- @cmd_tidy@ --- */
1516 static int cmd_tidy(int argc
, char *argv
[])
1520 die(EXIT_FAILURE
, "usage: tidy");
1521 doopen(&f
, KOPEN_WRITE
);
1522 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1527 /* --- @cmd_merge@ --- */
1529 static int cmd_merge(int argc
, char *argv
[])
1535 die(EXIT_FAILURE
, "Usage: merge file");
1536 if (strcmp(argv
[1], "-") == 0)
1538 else if (!(fp
= fopen(argv
[1], "r"))) {
1539 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1540 argv
[1], strerror(errno
));
1543 doopen(&f
, KOPEN_WRITE
);
1544 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1549 /*----- Main command table ------------------------------------------------*/
1553 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1557 "add [options] type [attr...]\n\
1558 Options: [-lqLS] [-a alg] [-b|-B bits] [-p param] [-r tag]\n\
1559 [-e expire] [-t tag] [-c comment]"
1561 { "expire", cmd_expire
, "expire tag..." },
1562 { "delete", cmd_delete
, "delete tag..." },
1563 { "tag", cmd_tag
, "tag tag [new-tag]" },
1564 { "setattr", cmd_setattr
, "setattr tag attr..." },
1565 { "comment", cmd_comment
, "comment tag [comment]" },
1566 { "lock", cmd_lock
, "lock qtag" },
1567 { "unlock", cmd_unlock
, "unlock qtag" },
1568 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]" },
1569 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]" },
1570 { "tidy", cmd_tidy
, "tidy" },
1571 { "extract", cmd_extract
, "extract [-f filter] file [tag...]" },
1572 { "merge", cmd_merge
, "merge file" },
1576 typedef struct cmd cmd
;
1578 /*----- Main code ---------------------------------------------------------*/
1580 /* --- Helpful GNUy functions --- */
1582 void usage(FILE *fp
)
1584 pquis(fp
, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1587 void version(FILE *fp
)
1589 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1599 Performs various simple key management operations. Command line options\n\
1602 -h, --help Display this help text.\n\
1603 -v, --version Display version number.\n\
1604 -u, --usage Display short usage summary.\n\
1606 -k, --keyring=FILE Read and write keys in FILE.\n\
1607 -i, --id=TAG Use key TAG for random number generator.\n\
1608 -t, --type=TYPE Use key TYPE for random number generator.\n\
1610 The following commands are understood:\n\n",
1612 for (c
= cmds
; c
->name
; c
++)
1613 fprintf(fp
, "%s\n", c
->help
);
1618 * Arguments: @int argc@ = number of command line arguments
1619 * @char *argv[]@ = array of command line arguments
1621 * Returns: Nonzero on failure.
1623 * Use: Main program. Performs simple key management functions.
1626 int main(int argc
, char *argv
[])
1634 /* --- Initialization --- */
1639 /* --- Parse command line options --- */
1642 static struct option opt
[] = {
1644 /* --- Standard GNUy help options --- */
1646 { "help", 0, 0, 'h' },
1647 { "version", 0, 0, 'v' },
1648 { "usage", 0, 0, 'u' },
1650 /* --- Real live useful options --- */
1652 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1653 { "id", OPTF_ARGREQ
, 0, 'i' },
1654 { "type", OPTF_ARGREQ
, 0, 't' },
1656 /* --- Magic terminator --- */
1660 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1666 /* --- GNU help options --- */
1677 /* --- Real useful options --- */
1683 /* --- Bogosity --- */
1691 /* --- Complain about excessive bogons --- */
1693 if (f
& f_bogus
|| optind
== argc
) {
1698 /* --- Initialize the Catacomb random number generator --- */
1700 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1701 rand_seed(RAND_GLOBAL
, 160);
1703 /* --- Dispatch to appropriate command handler --- */
1710 cmd
*c
, *chosen
= 0;
1711 size_t sz
= strlen(argv
[0]);
1713 for (c
= cmds
; c
->name
; c
++) {
1714 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1715 if (c
->name
[sz
] == 0) {
1719 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1725 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1726 return (chosen
->cmd(argc
, argv
));
1730 /*----- That's all, folks -------------------------------------------------*/