3 * $Id: keyutil.c,v 1.9 2000/08/15 21:40:49 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.9 2000/08/15 21:40:49 mdw
34 * Minor formatting change in listing attributes.
36 * Revision 1.8 2000/07/29 09:59:13 mdw
37 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
39 * Revision 1.7 2000/07/01 11:18:51 mdw
40 * Use new interfaces for key manipulation.
42 * Revision 1.6 2000/06/17 11:28:22 mdw
43 * Use secure memory interface from MP library. `rand_getgood' is
46 * Revision 1.5 2000/02/12 18:21:03 mdw
47 * Overhaul of key management (again).
49 * Revision 1.4 1999/12/22 15:48:10 mdw
50 * Track new key-management changes. Support new key generation
53 * Revision 1.3 1999/11/02 15:23:24 mdw
54 * Fix newlines in keyring list.
56 * Revision 1.2 1999/10/15 21:05:28 mdw
57 * In `key list', show timezone for local times, and support `-u' option
60 * Revision 1.1 1999/09/03 08:41:12 mdw
65 /*----- Header files ------------------------------------------------------*/
75 #include <mLib/base64.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
97 /*----- Handy global state ------------------------------------------------*/
99 static const char *keyfile
= "keyring";
101 /*----- Useful shared functions -------------------------------------------*/
103 /* --- @doopen@ --- *
105 * Arguments: @key_file *f@ = pointer to key file block
106 * @unsigned how@ = method to open file with
110 * Use: Opens a key file and handles errors by panicking
114 static void doopen(key_file
*f
, unsigned how
)
116 if (key_open(f
, keyfile
, how
, key_moan
, 0))
117 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
120 /* --- @doclose@ --- *
122 * Arguments: @key_file *f@ = pointer to key file block
126 * Use: Closes a key file and handles errors by panicking
130 static void doclose(key_file
*f
)
132 switch (key_close(f
)) {
134 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
135 keyfile
, strerror(errno
));
137 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
138 keyfile
, strerror(errno
));
142 /* --- @setattr@ --- *
144 * Arguments: @key_file *f@ = pointer to key file block
145 * @key *k@ = pointer to key block
146 * @char *v[]@ = array of assignments (overwritten!)
150 * Use: Applies the attribute assignments to the key.
153 static void setattr(key_file
*f
, key
*k
, char *v
[])
158 size_t eq
= strcspn(p
, "=");
160 moan("invalid assignment: `%s'", p
);
163 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
164 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
169 /*----- Key generation ----------------------------------------------------*/
171 /* --- Key generation parameters --- */
173 typedef struct keyopts
{
174 key_file
*kf
; /* Pointer to key file */
175 key
*k
; /* Pointer to the actual key */
176 dstr tag
; /* Full tag name for the key */
177 unsigned f
; /* Flags for the new key */
178 unsigned bits
, qbits
; /* Bit length for the new key */
179 key
*p
; /* Parameters key-data */
183 f_bogus
= 1, /* Error in parsing */
184 f_lock
= 2, /* Passphrase-lock private key */
185 f_quiet
= 4, /* Don't show a progress indicator */
186 f_limlee
= 8, /* Generate Lim-Lee primes */
187 f_subgroup
= 16 /* Generate a subgroup */
190 /* --- @dolock@ --- *
192 * Arguments: @keyopts *k@ = key generation options
193 * @key_data *kd@ = pointer to key data to lock
194 * @const char *t@ = tag suffix or null
198 * Use: Does passphrase locking on new keys.
201 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
203 if (!(k
->f
& f_lock
))
206 dstr_putf(&k
->tag
, ".%s", t
);
207 if (key_plock(k
->tag
.buf
, kd
, kd
))
208 die(EXIT_FAILURE
, "couldn't lock key");
213 * Arguments: @key_data *kd@ = pointer to parent key block
214 * @const char *tag@ = pointer to tag string
215 * @mp *m@ = integer to store
216 * @unsigned f@ = flags to set
220 * Use: Sets a multiprecision integer subkey.
223 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
225 key_data
*kkd
= key_structcreate(kd
, tag
);
230 /* --- @copyparam@ --- *
232 * Arguments: @keyopts *k@ = pointer to key options
233 * @const char **pp@ = checklist of parameters
235 * Returns: Nonzero if parameters copied; zero if you have to generate
238 * Use: Copies parameters from a source key to the current one.
241 static int copyparam(keyopts
*k
, const char **pp
)
245 /* --- Quick check if no parameters supplied --- */
250 /* --- Run through the checklist --- */
253 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
255 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
256 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
257 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
261 /* --- Copy over the parameters --- */
265 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
266 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
272 * Arguments: @key_data *k@ = pointer to key data block
273 * @const char *tag@ = tag string to use
275 * Returns: Pointer to multiprecision integer key item.
277 * Use: Fetches an MP key component.
280 static mp
*getmp(key_data
*k
, const char *tag
)
282 k
= key_structfind(k
, tag
);
284 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
285 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
286 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
290 /* --- @keyrand@ --- *
292 * Arguments: @key_file *kf@ = pointer to key file
293 * @const char *id@ = pointer to key id (or null)
297 * Use: Keys the random number generator.
300 static void keyrand(key_file
*kf
, const char *id
)
304 /* --- Find the key --- */
307 if ((k
= key_bytag(kf
, id
)) == 0)
308 die(EXIT_FAILURE
, "key `%s' not found", id
);
310 k
= key_bytype(kf
, "catacomb-rand");
313 key_data
*kd
= &k
->k
, kkd
;
316 switch (kd
->e
& KF_ENCMASK
) {
322 if (key_punlock(d
.buf
, kd
, &kkd
))
323 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
330 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
334 /* --- Key the generator --- */
336 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
342 /* --- Key generation algorithms --- */
344 static void alg_binary(keyopts
*k
)
353 die(EXIT_FAILURE
, "no shared parameters for binary keys");
355 sz
= (k
->bits
+ 7) >> 3;
357 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
358 rand_get(RAND_GLOBAL
, p
, sz
);
360 key_binary(&k
->k
->k
, p
, sz
);
361 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
364 dolock(k
, &k
->k
->k
, 0);
367 static void alg_des(keyopts
*k
)
376 die(EXIT_FAILURE
, "no shared parameters for DES keys");
377 if (k
->bits
% 56 || k
->bits
> 168)
378 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
382 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
383 for (i
= 0; i
< sz
; i
++) {
384 octet x
= p
[i
] | 0x01;
388 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
390 key_binary(&k
->k
->k
, p
, sz
);
391 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
394 dolock(k
, &k
->k
->k
, 0);
397 static void alg_rsa(keyopts
*k
)
402 /* --- Sanity checking --- */
405 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
409 /* --- Generate the RSA parameters --- */
411 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
412 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
413 die(EXIT_FAILURE
, "RSA key generation failed");
415 /* --- Run a test encryption --- */
418 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
420 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
425 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
426 c
= rsa_qprivop(&rp
, c
, c
, g
);
428 if (MP_CMP(c
, !=, m
))
429 die(EXIT_FAILURE
, "test encryption failed");
435 /* --- Allrighty then --- */
439 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
440 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
442 kd
= key_structcreate(kd
, "private");
444 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
445 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
446 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
447 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
448 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
449 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
450 dolock(k
, kd
, "private");
455 static void alg_dsaparam(keyopts
*k
)
457 static const char *pl
[] = { "q", "p", "g", 0 };
458 if (!copyparam(k
, pl
)) {
464 key_data
*kd
= &k
->k
->k
;
466 /* --- Choose appropriate bit lengths if necessary --- */
473 /* --- Allocate a seed block --- */
475 sz
= (k
->qbits
+ 7) >> 3;
477 rand_get(RAND_GLOBAL
, p
, sz
);
479 /* --- Allocate the parameters --- */
481 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
,
482 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
483 die(EXIT_FAILURE
, "DSA parameter generation failed");
485 /* --- Store the parameters --- */
488 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
489 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
490 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
495 /* --- Store the seed for future verification --- */
500 base64_encode(&c
, p
, sz
, &d
);
501 base64_encode(&c
, 0, 0, &d
);
502 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
508 static void alg_dsa(keyopts
*k
)
513 key_data
*kd
= &k
->k
->k
;
515 /* --- Get the shared parameters --- */
522 /* --- Choose a private key --- */
524 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
525 mpmont_create(&mm
, p
);
526 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
528 /* --- Store everything away --- */
530 mpkey(kd
, "y", y
, KCAT_PUB
);
532 kd
= key_structcreate(kd
, "private");
534 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
535 dolock(k
, kd
, "private");
537 mp_drop(x
); mp_drop(y
);
540 static void alg_dhparam(keyopts
*k
)
542 static const char *pl
[] = { "p", "q", "g", 0 };
543 if (!copyparam(k
, pl
)) {
545 key_data
*kd
= &k
->k
->k
;
551 /* --- Choose a large safe prime number --- */
553 if (k
->f
& f_limlee
) {
558 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
559 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
560 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
561 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
565 for (i
= 0; i
< nf
; i
++) {
568 mp_writedstr(f
[i
], &d
, 10);
571 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
575 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
576 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
579 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
582 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
583 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
584 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
591 static void alg_dh(keyopts
*k
)
596 key_data
*kd
= &k
->k
->k
;
598 /* --- Get the shared parameters --- */
605 /* --- Choose a suitable private key --- *
607 * Since %$g$% has order %$q$%, choose %$x < q$%.
610 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
612 /* --- Compute the public key %$y = g^x \bmod p$% --- */
614 mpmont_create(&mm
, p
);
615 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
618 /* --- Store everything away --- */
620 mpkey(kd
, "y", y
, KCAT_PUB
);
622 kd
= key_structcreate(kd
, "private");
624 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
625 dolock(k
, kd
, "private");
627 mp_drop(x
); mp_drop(y
);
630 static void alg_bbs(keyopts
*k
)
635 /* --- Sanity checking --- */
638 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
642 /* --- Generate the BBS parameters --- */
644 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
645 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
646 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
648 /* --- Allrighty then --- */
652 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
654 kd
= key_structcreate(kd
, "private");
656 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
657 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
658 dolock(k
, kd
, "private");
663 /* --- The algorithm tables --- */
665 typedef struct keyalg
{
667 void (*proc
)(keyopts
*o
);
671 static keyalg algtab
[] = {
672 { "binary", alg_binary
, "Plain binary data" },
673 { "des", alg_des
, "Binary with DES-style parity" },
674 { "rsa", alg_rsa
, "RSA public-key encryption" },
675 { "dsa", alg_dsa
, "DSA digital signatures" },
676 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
677 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
678 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
679 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
683 /* --- @cmd_add@ --- */
685 static int cmd_add(int argc
, char *argv
[])
688 time_t exp
= KEXP_EXPIRE
;
689 const char *tag
= 0, *ptag
= 0;
691 keyalg
*alg
= algtab
;
692 const char *rtag
= 0;
693 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
695 /* --- Parse options for the subcommand --- */
698 static struct option opt
[] = {
699 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
700 { "bits", OPTF_ARGREQ
, 0, 'b' },
701 { "qbits", OPTF_ARGREQ
, 0, 'B' },
702 { "parameters", OPTF_ARGREQ
, 0, 'p' },
703 { "expire", OPTF_ARGREQ
, 0, 'e' },
704 { "comment", OPTF_ARGREQ
, 0, 'c' },
705 { "tag", OPTF_ARGREQ
, 0, 't' },
706 { "rand-id", OPTF_ARGREQ
, 0, 'r' },
707 { "lock", 0, 0, 'l' },
708 { "quiet", 0, 0, 'q' },
709 { "lim-lee", 0, 0, 'L' },
710 { "subgroup", 0, 0, 'S' },
713 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:r:lqLS", opt
, 0, 0, 0);
717 /* --- Handle the various options --- */
721 /* --- Read an algorithm name --- */
725 size_t sz
= strlen(optarg
);
727 if (strcmp(optarg
, "list") == 0) {
728 for (a
= algtab
; a
->name
; a
++)
729 printf("%-10s %s\n", a
->name
, a
->help
);
734 for (a
= algtab
; a
->name
; a
++) {
735 if (strncmp(optarg
, a
->name
, sz
) == 0) {
736 if (a
->name
[sz
] == 0) {
740 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
746 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
749 /* --- Bits must be nonzero and a multiple of 8 --- */
753 k
.bits
= strtoul(optarg
, &p
, 0);
754 if (k
.bits
== 0 || *p
!= 0)
755 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
760 k
.qbits
= strtoul(optarg
, &p
, 0);
761 if (k
.qbits
== 0 || *p
!= 0)
762 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
765 /* --- Parameter selection --- */
771 /* --- Expiry dates get passed to @get_date@ for parsing --- */
774 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
777 exp
= get_date(optarg
, 0);
779 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
783 /* --- Store comments without interpretation --- */
786 if (key_chkcomment(optarg
))
787 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
791 /* --- Store tags --- */
794 if (key_chkident(optarg
))
795 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
799 /* --- Other flags --- */
817 /* --- Other things are bogus --- */
825 /* --- Various sorts of bogosity --- */
827 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
829 "Usage: add [options] type [attr...]");
831 if (key_chkident(argv
[optind
]))
832 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
834 /* --- Set up various bits of the state --- */
836 if (exp
== KEXP_EXPIRE
)
837 exp
= time(0) + 14 * 24 * 60 * 60;
839 /* --- Open the file and create the basic key block --- *
841 * Keep on generating keyids until one of them doesn't collide.
844 doopen(&f
, KOPEN_WRITE
);
847 /* --- Key the generator --- */
852 uint32 id
= rand_global
.ops
->word(&rand_global
);
854 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
857 if (err
!= KERR_DUPID
)
858 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
861 /* --- Set various simple attributes --- */
864 int err
= key_settag(&f
, k
.k
, tag
);
866 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
870 int err
= key_setcomment(&f
, k
.k
, c
);
872 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
875 setattr(&f
, k
.k
, argv
+ optind
+ 1);
877 key_fulltag(k
.k
, &k
.tag
);
879 /* --- Find the parameter key --- */
882 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
883 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
884 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
885 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
888 /* --- Now generate the actual key data --- */
898 /*----- Key listing -------------------------------------------------------*/
900 /* --- Listing options --- */
902 typedef struct listopts
{
903 const char *tfmt
; /* Date format (@strftime@-style) */
904 int v
; /* Verbosity level */
905 unsigned f
; /* Various flags */
906 time_t t
; /* Time now (for key expiry) */
907 key_filter kf
; /* Filter for matching keys */
910 /* --- Listing flags --- */
913 f_newline
= 2, /* Write newline before next entry */
914 f_attr
= 4, /* Written at least one attribute */
915 f_utc
= 8 /* Emit UTC time, not local time */
918 /* --- @showkeydata@ --- *
920 * Arguments: @key_data *k@ = pointer to key to write
921 * @int ind@ = indentation level
922 * @listopts *o@ = listing options
923 * @dstr *d@ = tag string for this subkey
927 * Use: Emits a piece of key data in a human-readable format.
930 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
932 #define INDENT(i) do { \
934 for (_i = 0; _i < (i); _i++) { \
939 switch (k
->e
& KF_ENCMASK
) {
941 /* --- Binary key data --- *
943 * Emit as a simple hex dump.
947 const octet
*p
= k
->u
.k
.k
;
948 const octet
*l
= p
+ k
->u
.k
.sz
;
956 } else if (sz
% 8 == 0)
960 printf("%02x", *p
++);
965 fputs("}\n", stdout
);
968 /* --- Encrypted data --- *
970 * If the user is sufficiently keen, ask for a passphrase and decrypt the
971 * key. Otherwise just say that it's encrypted and move on.
976 fputs(" encrypted\n", stdout
);
979 if (key_punlock(d
->buf
, k
, &kd
))
980 printf(" <failed to unlock %s>\n", d
->buf
);
982 fputs(" encrypted", stdout
);
983 showkeydata(&kd
, ind
, o
, d
);
989 /* --- Integer keys --- *
991 * Emit as a large integer in decimal. This makes using the key in
992 * `calc' or whatever easier.
997 mp_writefile(k
->u
.m
, stdout
, 10);
1001 /* --- Structured keys --- *
1003 * Just iterate over the subkeys.
1011 fputs(" {\n", stdout
);
1012 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1013 if (!key_match(&ks
->k
, &o
->kf
))
1016 printf("%s =", SYM_NAME(ks
));
1018 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1019 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1022 fputs("}\n", stdout
);
1029 /* --- @showkey@ --- *
1031 * Arguments: @key *k@ = pointer to key to show
1032 * @listopts *o@ = pointer to listing options
1036 * Use: Emits a listing of a particular key.
1039 static void showkey(key
*k
, listopts
*o
)
1041 char ebuf
[24], dbuf
[24];
1044 /* --- Skip the key if the filter doesn't match --- */
1046 if (!key_match(&k
->k
, &o
->kf
))
1049 /* --- Sort out the expiry and deletion times --- */
1051 if (KEY_EXPIRED(o
->t
, k
->exp
))
1052 strcpy(ebuf
, "expired");
1053 else if (k
->exp
== KEXP_FOREVER
)
1054 strcpy(ebuf
, "forever");
1056 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1057 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1060 if (KEY_EXPIRED(o
->t
, k
->del
))
1061 strcpy(dbuf
, "deleted");
1062 else if (k
->del
== KEXP_FOREVER
)
1063 strcpy(dbuf
, "forever");
1065 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1066 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1069 /* --- If in compact format, just display and quit --- */
1072 if (!(o
->f
& f_newline
)) {
1073 printf("%8s %-20s %-20s %-10s %-10s\n",
1074 "Id", "Tag", "Type", "Expire", "Delete");
1076 printf("%08lx %-20s %-20s %-10s %-10s\n",
1077 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1078 k
->type
, ebuf
, dbuf
);
1083 /* --- Display the standard header --- */
1085 if (o
->f
& f_newline
)
1086 fputc('\n', stdout
);
1087 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1088 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1089 printf("type: %s\n", k
->type
);
1090 printf("expiry: %s\n", ebuf
);
1091 printf("delete: %s\n", dbuf
);
1092 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1094 /* --- Display the attributes --- */
1098 const char *av
, *an
;
1101 printf("attributes:");
1102 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1103 printf("\n %s = %s", an
, av
);
1107 fputc('\n', stdout
);
1112 /* --- If dumping requested, dump the raw key data --- */
1116 fputs("key:", stdout
);
1118 showkeydata(&k
->k
, 0, o
, &d
);
1125 /* --- @cmd_list@ --- */
1127 static int cmd_list(int argc
, char *argv
[])
1131 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1133 /* --- Parse subcommand options --- */
1136 static struct option opt
[] = {
1137 { "quiet", 0, 0, 'q' },
1138 { "verbose", 0, 0, 'v' },
1139 { "utc", 0, 0, 'u' },
1140 { "filter", OPTF_ARGREQ
, 0, 'f' },
1143 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1160 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1162 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1171 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1173 /* --- Open the key file --- */
1175 doopen(&f
, KOPEN_READ
);
1178 /* --- Set up the time format --- */
1181 o
.tfmt
= "%Y-%m-%d";
1182 else if (o
.f
& f_utc
)
1183 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1185 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1187 /* --- If specific keys were requested use them, otherwise do all --- *
1189 * Some day, this might turn into a wildcard match.
1192 if (optind
< argc
) {
1194 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1197 moan("key `%s' not found", argv
[optind
]);
1201 } while (optind
< argc
);
1204 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1212 return (EXIT_FAILURE
);
1217 /*----- Command implementation --------------------------------------------*/
1219 /* --- @cmd_expire@ --- */
1221 static int cmd_expire(int argc
, char *argv
[])
1229 die(EXIT_FAILURE
, "Usage: expire tag...");
1230 doopen(&f
, KOPEN_WRITE
);
1231 for (i
= 1; i
< argc
; i
++) {
1232 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1235 moan("key `%s' not found", argv
[i
]);
1243 /* --- @cmd_delete@ --- */
1245 static int cmd_delete(int argc
, char *argv
[])
1253 die(EXIT_FAILURE
, "Usage: delete tag...");
1254 doopen(&f
, KOPEN_WRITE
);
1255 for (i
= 1; i
< argc
; i
++) {
1256 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1259 moan("key `%s' not found", argv
[i
]);
1267 /* --- @cmd_setattr@ --- */
1269 static int cmd_setattr(int argc
, char *argv
[])
1275 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1276 doopen(&f
, KOPEN_WRITE
);
1277 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1278 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1279 setattr(&f
, k
, argv
+ 2);
1284 /* --- @cmd_finger@ --- */
1286 static void fingerprint(key
*k
, const key_filter
*kf
)
1289 octet hash
[RMD160_HASHSZ
];
1293 if (!key_encode(&k
->k
, &d
, kf
))
1296 rmd160_hash(&r
, d
.buf
, d
.len
);
1297 rmd160_done(&r
, hash
);
1301 for (i
= 0; i
< sizeof(hash
); i
++) {
1302 if (i
&& i
% 4 == 0)
1304 printf("%02x", hash
[i
]);
1306 printf(" %s\n", d
.buf
);
1310 static int cmd_finger(int argc
, char *argv
[])
1314 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1317 static struct option opt
[] = {
1318 { "filter", OPTF_ARGREQ
, 0, 'f' },
1321 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1327 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1329 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1337 argv
+= optind
; argc
-= optind
;
1339 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1341 doopen(&f
, KOPEN_READ
);
1345 for (i
= 0; i
< argc
; i
++) {
1346 key
*k
= key_bytag(&f
, argv
[i
]);
1348 fingerprint(k
, &kf
);
1351 moan("key `%s' not found", argv
[i
]);
1357 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1358 fingerprint(k
, &kf
);
1363 /* --- @cmd_comment@ --- */
1365 static int cmd_comment(int argc
, char *argv
[])
1371 if (argc
< 2 || argc
> 3)
1372 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1373 doopen(&f
, KOPEN_WRITE
);
1374 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1375 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1376 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1377 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1382 /* --- @cmd_tag@ --- */
1384 static int cmd_tag(int argc
, char *argv
[])
1390 if (argc
< 2 || argc
> 3)
1391 die(EXIT_FAILURE
, "Usage: tag tag [new-tag]");
1392 doopen(&f
, KOPEN_WRITE
);
1393 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1394 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1395 if ((err
= key_settag(&f
, k
, argv
[2])) != 0)
1396 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[2], key_strerror(err
));
1401 /* --- @cmd_lock@ --- */
1403 static int cmd_lock(int argc
, char *argv
[])
1411 die(EXIT_FAILURE
, "Usage: lock qtag");
1412 doopen(&f
, KOPEN_WRITE
);
1413 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1414 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1415 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1416 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1417 if (key_plock(d
.buf
, kd
, kd
))
1418 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1424 /* --- @cmd_unlock@ --- */
1426 static int cmd_unlock(int argc
, char *argv
[])
1434 die(EXIT_FAILURE
, "Usage: unlock qtag");
1435 doopen(&f
, KOPEN_WRITE
);
1436 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1437 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1438 if (kd
->e
!= KENC_ENCRYPT
)
1439 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1440 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1441 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1447 /* --- @cmd_extract@ --- */
1449 static int cmd_extract(int argc
, char *argv
[])
1455 key_filter kf
= { 0, 0 };
1459 static struct option opt
[] = {
1460 { "filter", OPTF_ARGREQ
, 0, 'f' },
1463 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1469 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1471 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1479 argv
+= optind
; argc
-= optind
;
1481 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1482 if (strcmp(*argv
, "-") == 0)
1484 else if (!(fp
= fopen(*argv
, "w"))) {
1485 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1486 *argv
, strerror(errno
));
1489 doopen(&f
, KOPEN_READ
);
1493 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1494 key_extract(&f
, k
, fp
, &kf
);
1496 for (i
= 1; i
< argc
; i
++) {
1497 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1498 key_extract(&f
, k
, fp
, &kf
);
1500 moan("key `%s' not found", argv
[i
]);
1506 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1511 /* --- @cmd_tidy@ --- */
1513 static int cmd_tidy(int argc
, char *argv
[])
1517 die(EXIT_FAILURE
, "usage: tidy");
1518 doopen(&f
, KOPEN_WRITE
);
1519 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1524 /* --- @cmd_merge@ --- */
1526 static int cmd_merge(int argc
, char *argv
[])
1532 die(EXIT_FAILURE
, "Usage: merge file");
1533 if (strcmp(argv
[1], "-") == 0)
1535 else if (!(fp
= fopen(argv
[1], "r"))) {
1536 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1537 argv
[1], strerror(errno
));
1540 doopen(&f
, KOPEN_WRITE
);
1541 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1546 /*----- Main command table ------------------------------------------------*/
1550 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1554 "add [options] type [attr...]\n\
1555 Options: [-lqLS] [-a alg] [-b|-B bits] [-p param] [-r tag]\n\
1556 [-e expire] [-t tag] [-c comment]"
1558 { "expire", cmd_expire
, "expire tag..." },
1559 { "delete", cmd_delete
, "delete tag..." },
1560 { "tag", cmd_tag
, "tag tag [new-tag]" },
1561 { "setattr", cmd_setattr
, "setattr tag attr..." },
1562 { "comment", cmd_comment
, "comment tag [comment]" },
1563 { "lock", cmd_lock
, "lock qtag" },
1564 { "unlock", cmd_unlock
, "unlock qtag" },
1565 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]" },
1566 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]" },
1567 { "tidy", cmd_tidy
, "tidy" },
1568 { "extract", cmd_extract
, "extract [-f filter] file [tag...]" },
1569 { "merge", cmd_merge
, "merge file" },
1573 typedef struct cmd cmd
;
1575 /*----- Main code ---------------------------------------------------------*/
1577 /* --- Helpful GNUy functions --- */
1579 void usage(FILE *fp
)
1581 pquis(fp
, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1584 void version(FILE *fp
)
1586 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1596 Performs various simple key management operations. Command line options\n\
1599 -h, --help Display this help text.\n\
1600 -v, --version Display version number.\n\
1601 -u, --usage Display short usage summary.\n\
1603 -k, --keyring=FILE Read and write keys in FILE.\n\
1604 -i, --id=TAG Use key TAG for random number generator.\n\
1605 -t, --type=TYPE Use key TYPE for random number generator.\n\
1607 The following commands are understood:\n\n",
1609 for (c
= cmds
; c
->name
; c
++)
1610 fprintf(fp
, "%s\n", c
->help
);
1615 * Arguments: @int argc@ = number of command line arguments
1616 * @char *argv[]@ = array of command line arguments
1618 * Returns: Nonzero on failure.
1620 * Use: Main program. Performs simple key management functions.
1623 int main(int argc
, char *argv
[])
1631 /* --- Initialization --- */
1636 /* --- Parse command line options --- */
1639 static struct option opt
[] = {
1641 /* --- Standard GNUy help options --- */
1643 { "help", 0, 0, 'h' },
1644 { "version", 0, 0, 'v' },
1645 { "usage", 0, 0, 'u' },
1647 /* --- Real live useful options --- */
1649 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1650 { "id", OPTF_ARGREQ
, 0, 'i' },
1651 { "type", OPTF_ARGREQ
, 0, 't' },
1653 /* --- Magic terminator --- */
1657 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1663 /* --- GNU help options --- */
1674 /* --- Real useful options --- */
1680 /* --- Bogosity --- */
1688 /* --- Complain about excessive bogons --- */
1690 if (f
& f_bogus
|| optind
== argc
) {
1695 /* --- Initialize the Catacomb random number generator --- */
1697 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1698 rand_seed(RAND_GLOBAL
, 160);
1700 /* --- Dispatch to appropriate command handler --- */
1707 cmd
*c
, *chosen
= 0;
1708 size_t sz
= strlen(argv
[0]);
1710 for (c
= cmds
; c
->name
; c
++) {
1711 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1712 if (c
->name
[sz
] == 0) {
1716 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1722 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1723 return (chosen
->cmd(argc
, argv
));
1727 /*----- That's all, folks -------------------------------------------------*/