3 * $Id: keyutil.c,v 1.11 2000/12/06 20:33:27 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.11 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
37 * Revision 1.10 2000/10/08 12:02:21 mdw
38 * Use @MP_EQ@ instead of @MP_CMP@.
40 * Revision 1.9 2000/08/15 21:40:49 mdw
41 * Minor formatting change in listing attributes.
43 * Revision 1.8 2000/07/29 09:59:13 mdw
44 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
46 * Revision 1.7 2000/07/01 11:18:51 mdw
47 * Use new interfaces for key manipulation.
49 * Revision 1.6 2000/06/17 11:28:22 mdw
50 * Use secure memory interface from MP library. `rand_getgood' is
53 * Revision 1.5 2000/02/12 18:21:03 mdw
54 * Overhaul of key management (again).
56 * Revision 1.4 1999/12/22 15:48:10 mdw
57 * Track new key-management changes. Support new key generation
60 * Revision 1.3 1999/11/02 15:23:24 mdw
61 * Fix newlines in keyring list.
63 * Revision 1.2 1999/10/15 21:05:28 mdw
64 * In `key list', show timezone for local times, and support `-u' option
67 * Revision 1.1 1999/09/03 08:41:12 mdw
72 /*----- Header files ------------------------------------------------------*/
82 #include <mLib/base64.h>
83 #include <mLib/mdwopt.h>
84 #include <mLib/quis.h>
85 #include <mLib/report.h>
104 /*----- Handy global state ------------------------------------------------*/
106 static const char *keyfile
= "keyring";
108 /*----- Useful shared functions -------------------------------------------*/
110 /* --- @doopen@ --- *
112 * Arguments: @key_file *f@ = pointer to key file block
113 * @unsigned how@ = method to open file with
117 * Use: Opens a key file and handles errors by panicking
121 static void doopen(key_file
*f
, unsigned how
)
123 if (key_open(f
, keyfile
, how
, key_moan
, 0))
124 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
127 /* --- @doclose@ --- *
129 * Arguments: @key_file *f@ = pointer to key file block
133 * Use: Closes a key file and handles errors by panicking
137 static void doclose(key_file
*f
)
139 switch (key_close(f
)) {
141 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
142 keyfile
, strerror(errno
));
144 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
145 keyfile
, strerror(errno
));
149 /* --- @setattr@ --- *
151 * Arguments: @key_file *f@ = pointer to key file block
152 * @key *k@ = pointer to key block
153 * @char *v[]@ = array of assignments (overwritten!)
157 * Use: Applies the attribute assignments to the key.
160 static void setattr(key_file
*f
, key
*k
, char *v
[])
165 size_t eq
= strcspn(p
, "=");
167 moan("invalid assignment: `%s'", p
);
170 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
171 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
176 /*----- Key generation ----------------------------------------------------*/
178 /* --- Key generation parameters --- */
180 typedef struct keyopts
{
181 key_file
*kf
; /* Pointer to key file */
182 key
*k
; /* Pointer to the actual key */
183 dstr tag
; /* Full tag name for the key */
184 unsigned f
; /* Flags for the new key */
185 unsigned bits
, qbits
; /* Bit length for the new key */
186 key
*p
; /* Parameters key-data */
189 #define f_bogus 1u /* Error in parsing */
190 #define f_lock 2u /* Passphrase-lock private key */
191 #define f_quiet 4u /* Don't show a progress indicator */
192 #define f_limlee 8u /* Generate Lim-Lee primes */
193 #define f_subgroup 16u /* Generate a subgroup */
195 /* --- @dolock@ --- *
197 * Arguments: @keyopts *k@ = key generation options
198 * @key_data *kd@ = pointer to key data to lock
199 * @const char *t@ = tag suffix or null
203 * Use: Does passphrase locking on new keys.
206 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
208 if (!(k
->f
& f_lock
))
211 dstr_putf(&k
->tag
, ".%s", t
);
212 if (key_plock(k
->tag
.buf
, kd
, kd
))
213 die(EXIT_FAILURE
, "couldn't lock key");
218 * Arguments: @key_data *kd@ = pointer to parent key block
219 * @const char *tag@ = pointer to tag string
220 * @mp *m@ = integer to store
221 * @unsigned f@ = flags to set
225 * Use: Sets a multiprecision integer subkey.
228 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
230 key_data
*kkd
= key_structcreate(kd
, tag
);
235 /* --- @copyparam@ --- *
237 * Arguments: @keyopts *k@ = pointer to key options
238 * @const char **pp@ = checklist of parameters
240 * Returns: Nonzero if parameters copied; zero if you have to generate
243 * Use: Copies parameters from a source key to the current one.
246 static int copyparam(keyopts
*k
, const char **pp
)
250 /* --- Quick check if no parameters supplied --- */
255 /* --- Run through the checklist --- */
258 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
260 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
261 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
262 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
266 /* --- Copy over the parameters --- */
270 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
271 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
277 * Arguments: @key_data *k@ = pointer to key data block
278 * @const char *tag@ = tag string to use
280 * Returns: Pointer to multiprecision integer key item.
282 * Use: Fetches an MP key component.
285 static mp
*getmp(key_data
*k
, const char *tag
)
287 k
= key_structfind(k
, tag
);
289 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
290 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
291 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
295 /* --- @keyrand@ --- *
297 * Arguments: @key_file *kf@ = pointer to key file
298 * @const char *id@ = pointer to key id (or null)
302 * Use: Keys the random number generator.
305 static void keyrand(key_file
*kf
, const char *id
)
309 /* --- Find the key --- */
312 if ((k
= key_bytag(kf
, id
)) == 0)
313 die(EXIT_FAILURE
, "key `%s' not found", id
);
315 k
= key_bytype(kf
, "catacomb-rand");
318 key_data
*kd
= &k
->k
, kkd
;
321 switch (kd
->e
& KF_ENCMASK
) {
327 if (key_punlock(d
.buf
, kd
, &kkd
))
328 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
335 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
339 /* --- Key the generator --- */
341 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
347 /* --- Key generation algorithms --- */
349 static void alg_binary(keyopts
*k
)
358 die(EXIT_FAILURE
, "no shared parameters for binary keys");
360 sz
= (k
->bits
+ 7) >> 3;
362 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
363 rand_get(RAND_GLOBAL
, p
, sz
);
365 key_binary(&k
->k
->k
, p
, sz
);
366 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
369 dolock(k
, &k
->k
->k
, 0);
372 static void alg_des(keyopts
*k
)
381 die(EXIT_FAILURE
, "no shared parameters for DES keys");
382 if (k
->bits
% 56 || k
->bits
> 168)
383 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
387 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
388 for (i
= 0; i
< sz
; i
++) {
389 octet x
= p
[i
] | 0x01;
393 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
395 key_binary(&k
->k
->k
, p
, sz
);
396 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
399 dolock(k
, &k
->k
->k
, 0);
402 static void alg_rsa(keyopts
*k
)
407 /* --- Sanity checking --- */
410 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
414 /* --- Generate the RSA parameters --- */
416 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
417 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
418 die(EXIT_FAILURE
, "RSA key generation failed");
420 /* --- Run a test encryption --- */
423 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
425 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
430 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
431 c
= rsa_qprivop(&rp
, c
, c
, g
);
434 die(EXIT_FAILURE
, "test encryption failed");
440 /* --- Allrighty then --- */
444 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
445 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
447 kd
= key_structcreate(kd
, "private");
449 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
450 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
451 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
452 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
453 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
454 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
455 dolock(k
, kd
, "private");
460 static void alg_dsaparam(keyopts
*k
)
462 static const char *pl
[] = { "q", "p", "g", 0 };
463 if (!copyparam(k
, pl
)) {
469 key_data
*kd
= &k
->k
->k
;
471 /* --- Choose appropriate bit lengths if necessary --- */
478 /* --- Allocate a seed block --- */
480 sz
= (k
->qbits
+ 7) >> 3;
482 rand_get(RAND_GLOBAL
, p
, sz
);
484 /* --- Allocate the parameters --- */
486 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
,
487 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
488 die(EXIT_FAILURE
, "DSA parameter generation failed");
490 /* --- Store the parameters --- */
493 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
494 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
495 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
500 /* --- Store the seed for future verification --- */
505 base64_encode(&c
, p
, sz
, &d
);
506 base64_encode(&c
, 0, 0, &d
);
507 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
513 static void alg_dsa(keyopts
*k
)
518 key_data
*kd
= &k
->k
->k
;
520 /* --- Get the shared parameters --- */
527 /* --- Choose a private key --- */
529 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
530 mpmont_create(&mm
, p
);
531 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
533 /* --- Store everything away --- */
535 mpkey(kd
, "y", y
, KCAT_PUB
);
537 kd
= key_structcreate(kd
, "private");
539 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
540 dolock(k
, kd
, "private");
542 mp_drop(x
); mp_drop(y
);
545 static void alg_dhparam(keyopts
*k
)
547 static const char *pl
[] = { "p", "q", "g", 0 };
548 if (!copyparam(k
, pl
)) {
550 key_data
*kd
= &k
->k
->k
;
556 /* --- Choose a large safe prime number --- */
558 if (k
->f
& f_limlee
) {
563 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
564 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
565 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
566 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
570 for (i
= 0; i
< nf
; i
++) {
573 mp_writedstr(f
[i
], &d
, 10);
576 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
580 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
581 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
584 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
587 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
588 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
589 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
596 static void alg_dh(keyopts
*k
)
601 key_data
*kd
= &k
->k
->k
;
603 /* --- Get the shared parameters --- */
610 /* --- Choose a suitable private key --- *
612 * Since %$g$% has order %$q$%, choose %$x < q$%.
615 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
617 /* --- Compute the public key %$y = g^x \bmod p$% --- */
619 mpmont_create(&mm
, p
);
620 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
623 /* --- Store everything away --- */
625 mpkey(kd
, "y", y
, KCAT_PUB
);
627 kd
= key_structcreate(kd
, "private");
629 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
630 dolock(k
, kd
, "private");
632 mp_drop(x
); mp_drop(y
);
635 static void alg_bbs(keyopts
*k
)
640 /* --- Sanity checking --- */
643 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
647 /* --- Generate the BBS parameters --- */
649 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
650 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
651 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
653 /* --- Allrighty then --- */
657 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
659 kd
= key_structcreate(kd
, "private");
661 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
662 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
663 dolock(k
, kd
, "private");
668 /* --- The algorithm tables --- */
670 typedef struct keyalg
{
672 void (*proc
)(keyopts
*o
);
676 static keyalg algtab
[] = {
677 { "binary", alg_binary
, "Plain binary data" },
678 { "des", alg_des
, "Binary with DES-style parity" },
679 { "rsa", alg_rsa
, "RSA public-key encryption" },
680 { "dsa", alg_dsa
, "DSA digital signatures" },
681 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
682 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
683 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
684 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
688 /* --- @cmd_add@ --- */
690 static int cmd_add(int argc
, char *argv
[])
693 time_t exp
= KEXP_EXPIRE
;
694 const char *tag
= 0, *ptag
= 0;
696 keyalg
*alg
= algtab
;
697 const char *rtag
= 0;
698 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
700 /* --- Parse options for the subcommand --- */
703 static struct option opt
[] = {
704 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
705 { "bits", OPTF_ARGREQ
, 0, 'b' },
706 { "qbits", OPTF_ARGREQ
, 0, 'B' },
707 { "parameters", OPTF_ARGREQ
, 0, 'p' },
708 { "expire", OPTF_ARGREQ
, 0, 'e' },
709 { "comment", OPTF_ARGREQ
, 0, 'c' },
710 { "tag", OPTF_ARGREQ
, 0, 't' },
711 { "rand-id", OPTF_ARGREQ
, 0, 'r' },
712 { "lock", 0, 0, 'l' },
713 { "quiet", 0, 0, 'q' },
714 { "lim-lee", 0, 0, 'L' },
715 { "subgroup", 0, 0, 'S' },
718 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:r:lqLS", opt
, 0, 0, 0);
722 /* --- Handle the various options --- */
726 /* --- Read an algorithm name --- */
730 size_t sz
= strlen(optarg
);
732 if (strcmp(optarg
, "list") == 0) {
733 for (a
= algtab
; a
->name
; a
++)
734 printf("%-10s %s\n", a
->name
, a
->help
);
739 for (a
= algtab
; a
->name
; a
++) {
740 if (strncmp(optarg
, a
->name
, sz
) == 0) {
741 if (a
->name
[sz
] == 0) {
745 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
751 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
754 /* --- Bits must be nonzero and a multiple of 8 --- */
758 k
.bits
= strtoul(optarg
, &p
, 0);
759 if (k
.bits
== 0 || *p
!= 0)
760 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
765 k
.qbits
= strtoul(optarg
, &p
, 0);
766 if (k
.qbits
== 0 || *p
!= 0)
767 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
770 /* --- Parameter selection --- */
776 /* --- Expiry dates get passed to @get_date@ for parsing --- */
779 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
782 exp
= get_date(optarg
, 0);
784 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
788 /* --- Store comments without interpretation --- */
791 if (key_chkcomment(optarg
))
792 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
796 /* --- Store tags --- */
799 if (key_chkident(optarg
))
800 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
804 /* --- Other flags --- */
822 /* --- Other things are bogus --- */
830 /* --- Various sorts of bogosity --- */
832 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
834 "Usage: add [options] type [attr...]");
836 if (key_chkident(argv
[optind
]))
837 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
839 /* --- Set up various bits of the state --- */
841 if (exp
== KEXP_EXPIRE
)
842 exp
= time(0) + 14 * 24 * 60 * 60;
844 /* --- Open the file and create the basic key block --- *
846 * Keep on generating keyids until one of them doesn't collide.
849 doopen(&f
, KOPEN_WRITE
);
852 /* --- Key the generator --- */
857 uint32 id
= rand_global
.ops
->word(&rand_global
);
859 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
862 if (err
!= KERR_DUPID
)
863 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
866 /* --- Set various simple attributes --- */
869 int err
= key_settag(&f
, k
.k
, tag
);
871 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
875 int err
= key_setcomment(&f
, k
.k
, c
);
877 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
880 setattr(&f
, k
.k
, argv
+ optind
+ 1);
882 key_fulltag(k
.k
, &k
.tag
);
884 /* --- Find the parameter key --- */
887 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
888 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
889 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
890 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
893 /* --- Now generate the actual key data --- */
903 /*----- Key listing -------------------------------------------------------*/
905 /* --- Listing options --- */
907 typedef struct listopts
{
908 const char *tfmt
; /* Date format (@strftime@-style) */
909 int v
; /* Verbosity level */
910 unsigned f
; /* Various flags */
911 time_t t
; /* Time now (for key expiry) */
912 key_filter kf
; /* Filter for matching keys */
915 /* --- Listing flags --- */
917 #define f_newline 2u /* Write newline before next entry */
918 #define f_attr 4u /* Written at least one attribute */
919 #define f_utc 8u /* 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
[])
1632 /* --- Initialization --- */
1637 /* --- Parse command line options --- */
1640 static struct option opt
[] = {
1642 /* --- Standard GNUy help options --- */
1644 { "help", 0, 0, 'h' },
1645 { "version", 0, 0, 'v' },
1646 { "usage", 0, 0, 'u' },
1648 /* --- Real live useful options --- */
1650 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1651 { "id", OPTF_ARGREQ
, 0, 'i' },
1652 { "type", OPTF_ARGREQ
, 0, 't' },
1654 /* --- Magic terminator --- */
1658 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1664 /* --- GNU help options --- */
1675 /* --- Real useful options --- */
1681 /* --- Bogosity --- */
1689 /* --- Complain about excessive bogons --- */
1691 if (f
& f_bogus
|| optind
== argc
) {
1696 /* --- Initialize the Catacomb random number generator --- */
1698 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1699 rand_seed(RAND_GLOBAL
, 160);
1701 /* --- Dispatch to appropriate command handler --- */
1708 cmd
*c
, *chosen
= 0;
1709 size_t sz
= strlen(argv
[0]);
1711 for (c
= cmds
; c
->name
; c
++) {
1712 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1713 if (c
->name
[sz
] == 0) {
1717 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1723 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1724 return (chosen
->cmd(argc
, argv
));
1728 /*----- That's all, folks -------------------------------------------------*/