3 * $Id: keyutil.c,v 1.5 2000/02/12 18:21:03 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.5 2000/02/12 18:21:03 mdw
34 * Overhaul of key management (again).
36 * Revision 1.4 1999/12/22 15:48:10 mdw
37 * Track new key-management changes. Support new key generation
40 * Revision 1.3 1999/11/02 15:23:24 mdw
41 * Fix newlines in keyring list.
43 * Revision 1.2 1999/10/15 21:05:28 mdw
44 * In `key list', show timezone for local times, and support `-u' option
47 * Revision 1.1 1999/09/03 08:41:12 mdw
52 /*----- Header files ------------------------------------------------------*/
62 #include <mLib/base64.h>
63 #include <mLib/mdwopt.h>
64 #include <mLib/quis.h>
65 #include <mLib/report.h>
84 /*----- Handy global state ------------------------------------------------*/
86 static const char *keyfile
= "keyring";
88 /*----- Useful shared functions -------------------------------------------*/
92 * Arguments: @key_file *f@ = pointer to key file block
93 * @unsigned how@ = method to open file with
97 * Use: Opens a key file and handles errors by panicking
101 static void doopen(key_file
*f
, unsigned how
)
103 if (key_open(f
, keyfile
, how
, key_moan
, 0))
104 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
107 /* --- @doclose@ --- *
109 * Arguments: @key_file *f@ = pointer to key file block
113 * Use: Closes a key file and handles errors by panicking
117 static void doclose(key_file
*f
)
119 switch (key_close(f
)) {
121 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
122 keyfile
, strerror(errno
));
124 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
125 keyfile
, strerror(errno
));
129 /* --- @setattr@ --- *
131 * Arguments: @key_file *f@ = pointer to key file block
132 * @key *k@ = pointer to key block
133 * @char *v[]@ = array of assignments (overwritten!)
137 * Use: Applies the attribute assignments to the key.
140 static void setattr(key_file
*f
, key
*k
, char *v
[])
145 size_t eq
= strcspn(p
, "=");
147 moan("invalid assignment: `%s'", p
);
150 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
151 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
156 /*----- Key generation ----------------------------------------------------*/
158 /* --- Key generation parameters --- */
160 typedef struct keyopts
{
161 key_file
*kf
; /* Pointer to key file */
162 key
*k
; /* Pointer to the actual key */
163 dstr tag
; /* Full tag name for the key */
164 unsigned f
; /* Flags for the new key */
165 unsigned bits
, qbits
; /* Bit length for the new key */
166 key
*p
; /* Parameters key-data */
170 f_bogus
= 1, /* Error in parsing */
171 f_lock
= 2, /* Passphrase-lock private key */
172 f_quiet
= 4 /* Don't show a progress indicator */
175 /* --- @dolock@ --- *
177 * Arguments: @keyopts *k@ = key generation options
178 * @key_data *kd@ = pointer to key data to lock
179 * @const char *t@ = tag suffix or null
183 * Use: Does passphrase locking on new keys.
186 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
188 if (!(k
->f
& f_lock
))
191 dstr_putf(&k
->tag
, ".%s", t
);
192 if (key_plock(k
->tag
.buf
, kd
, kd
))
193 die(EXIT_FAILURE
, "couldn't lock key");
198 * Arguments: @key_data *kd@ = pointer to parent key block
199 * @const char *tag@ = pointer to tag string
200 * @mp *m@ = integer to store
201 * @unsigned f@ = flags to set
205 * Use: Sets a multiprecision integer subkey.
208 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
210 key_data
*kkd
= key_structcreate(kd
, tag
);
215 /* --- @copyparam@ --- *
217 * Arguments: @keyopts *k@ = pointer to key options
218 * @const char **pp@ = checklist of parameters
220 * Returns: Nonzero if parameters copied; zero if you have to generate
223 * Use: Copies parameters from a source key to the current one.
226 static int copyparam(keyopts
*k
, const char **pp
)
230 /* --- Quick check if no parameters supplied --- */
235 /* --- Run through the checklist --- */
238 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
240 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
241 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
242 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
246 /* --- Copy over the parameters --- */
250 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
251 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
257 * Arguments: @key_data *k@ = pointer to key data block
258 * @const char *tag@ = tag string to use
260 * Returns: Pointer to multiprecision integer key item.
262 * Use: Fetches an MP key component.
265 static mp
*getmp(key_data
*k
, const char *tag
)
267 k
= key_structfind(k
, tag
);
269 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
270 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
271 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
275 /* --- @keyrand@ --- *
277 * Arguments: @key_file *kf@ = pointer to key file
278 * @const char *id@ = pointer to key id (or null)
282 * Use: Keys the random number generator.
285 static void keyrand(key_file
*kf
, const char *id
)
289 /* --- Find the key --- */
292 if ((k
= key_bytag(kf
, id
)) == 0)
293 die(EXIT_FAILURE
, "key `%s' not found", id
);
295 k
= key_bytype(kf
, "catacomb-rand");
298 key_data
*kd
= &k
->k
, kkd
;
301 switch (kd
->e
& KF_ENCMASK
) {
307 if (key_punlock(d
.buf
, kd
, &kkd
))
308 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
315 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
319 /* --- Key the generator --- */
321 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
327 /* --- Key generation algorithms --- */
329 static void alg_binary(keyopts
*k
)
338 die(EXIT_FAILURE
, "no shared parameters for binary keys");
340 sz
= (k
->bits
+ 7) >> 3;
342 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
343 rand_getgood(RAND_GLOBAL
, p
, sz
);
345 key_binary(&k
->k
->k
, p
, sz
);
346 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
349 dolock(k
, &k
->k
->k
, 0);
352 static void alg_des(keyopts
*k
)
361 die(EXIT_FAILURE
, "no shared parameters for DES keys");
362 if (k
->bits
% 56 || k
->bits
> 168)
363 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
367 rand_getgood(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
368 for (i
= 0; i
< sz
; i
++) {
369 octet x
= p
[i
] | 0x01;
373 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
375 key_binary(&k
->k
->k
, p
, sz
);
376 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
379 dolock(k
, &k
->k
->k
, 0);
382 static void alg_rsa(keyopts
*k
)
387 /* --- Sanity checking --- */
390 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
394 /* --- Generate the RSA parameters --- */
396 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
397 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
398 die(EXIT_FAILURE
, "RSA key generation failed");
400 /* --- Run a test encryption --- */
403 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
405 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
408 /* --- Encrypt the plaintext --- */
410 mpmont_create(&mm
, rp
.n
);
411 c
= mpmont_exp(&mm
, MP_NEW
, m
, rp
.e
);
414 /* --- Decrypt the ciphertext --- */
416 c
= rsa_decrypt(&rp
, c
, c
, g
);
418 /* --- Check everything went OK --- */
420 if (MP_CMP(c
, !=, m
))
421 die(EXIT_FAILURE
, "test encryption failed");
427 /* --- Allrighty then --- */
431 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
432 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
434 kd
= key_structcreate(kd
, "private");
436 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
437 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
438 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
439 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
440 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
441 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
442 dolock(k
, kd
, "private");
444 mp_drop(rp
.p
); mp_drop(rp
.q
); mp_drop(rp
.n
); mp_drop(rp
.q_inv
);
445 mp_drop(rp
.e
); mp_drop(rp
.d
); mp_drop(rp
.dp
); mp_drop(rp
.dq
);
448 static void alg_dsaparam(keyopts
*k
)
450 static const char *pl
[] = { "q", "p", "g", 0 };
451 if (!copyparam(k
, pl
)) {
457 key_data
*kd
= &k
->k
->k
;
459 /* --- Choose appropriate bit lengths if necessary --- */
466 /* --- Allocate a seed block --- */
468 sz
= (k
->qbits
+ 7) >> 3;
470 rand_getgood(RAND_GLOBAL
, p
, sz
);
472 /* --- Allocate the parameters --- */
474 if (dsa_seed(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
,
475 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
476 die(EXIT_FAILURE
, "DSA parameter generation failed");
478 /* --- Store the parameters --- */
481 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
482 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
483 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
488 /* --- Store the seed for future verification --- */
493 base64_encode(&c
, p
, sz
, &d
);
494 base64_encode(&c
, 0, 0, &d
);
495 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
501 static void alg_dsa(keyopts
*k
)
506 key_data
*kd
= &k
->k
->k
;
508 /* --- Get the shared parameters --- */
515 /* --- Choose a private key --- */
517 x
= mprand_range(MP_NEW
, q
, &rand_global
, 0);
519 mpmont_create(&mm
, p
);
520 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
522 /* --- Store everything away --- */
524 mpkey(kd
, "y", y
, KCAT_PUB
);
526 kd
= key_structcreate(kd
, "private");
528 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
529 dolock(k
, kd
, "private");
531 mp_drop(x
); mp_drop(y
);
534 static void alg_dhparam(keyopts
*k
)
536 static const char *pl
[] = { "p", "q", "g", 0 };
537 if (!copyparam(k
, pl
)) {
539 key_data
*kd
= &k
->k
->k
;
544 /* --- Choose a large safe prime number --- */
546 if (dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
547 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
548 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
551 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
552 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
553 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
560 static void alg_dh(keyopts
*k
)
565 key_data
*kd
= &k
->k
->k
;
567 /* --- Get the shared parameters --- */
574 /* --- Choose a suitable private key --- *
576 * Since %$g$% has order %$q$%, choose %$x < q$%.
579 x
= mprand_range(MP_NEW
, q
, &rand_global
, 0);
582 /* --- Compute the public key %$y = g^x \bmod p$% --- */
584 mpmont_create(&mm
, p
);
585 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
588 /* --- Store everything away --- */
590 mpkey(kd
, "y", y
, KCAT_PUB
);
592 kd
= key_structcreate(kd
, "private");
594 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
595 dolock(k
, kd
, "private");
597 mp_drop(x
); mp_drop(y
);
600 static void alg_bbs(keyopts
*k
)
605 /* --- Sanity checking --- */
608 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
612 /* --- Generate the BBS parameters --- */
614 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
615 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
616 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
618 /* --- Allrighty then --- */
622 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
624 kd
= key_structcreate(kd
, "private");
626 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
627 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
628 dolock(k
, kd
, "private");
630 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
);
633 /* --- The algorithm tables --- */
635 typedef struct keyalg
{
637 void (*proc
)(keyopts
*o
);
641 static keyalg algtab
[] = {
642 { "binary", alg_binary
, "Plain binary data" },
643 { "des", alg_des
, "Binary with DES-style parity" },
644 { "rsa", alg_rsa
, "RSA public-key encryption" },
645 { "dsa", alg_dsa
, "DSA digital signatures" },
646 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
647 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
648 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
649 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
653 /* --- @cmd_add@ --- */
655 static int cmd_add(int argc
, char *argv
[])
658 time_t exp
= KEXP_EXPIRE
;
659 const char *tag
= 0, *ptag
= 0;
661 keyalg
*alg
= algtab
;
662 const char *rtag
= 0;
663 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
665 /* --- Parse options for the subcommand --- */
668 static struct option opt
[] = {
669 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
670 { "bits", OPTF_ARGREQ
, 0, 'b' },
671 { "qbits", OPTF_ARGREQ
, 0, 'B' },
672 { "parameters", OPTF_ARGREQ
, 0, 'p' },
673 { "expire", OPTF_ARGREQ
, 0, 'e' },
674 { "comment", OPTF_ARGREQ
, 0, 'c' },
675 { "tag", OPTF_ARGREQ
, 0, 't' },
676 { "rand-id", OPTF_ARGREQ
, 0, 'r' },
677 { "lock", 0, 0, 'l' },
678 { "quiet", 0, 0, 'q' },
681 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:r:lq", opt
, 0, 0, 0);
685 /* --- Handle the various options --- */
689 /* --- Read an algorithm name --- */
693 size_t sz
= strlen(optarg
);
695 if (strcmp(optarg
, "list") == 0) {
696 for (a
= algtab
; a
->name
; a
++)
697 printf("%-10s %s\n", a
->name
, a
->help
);
702 for (a
= algtab
; a
->name
; a
++) {
703 if (strncmp(optarg
, a
->name
, sz
) == 0) {
704 if (a
->name
[sz
] == 0) {
708 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
714 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
717 /* --- Bits must be nonzero and a multiple of 8 --- */
721 k
.bits
= strtoul(optarg
, &p
, 0);
722 if (k
.bits
== 0 || *p
!= 0)
723 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
728 k
.qbits
= strtoul(optarg
, &p
, 0);
729 if (k
.qbits
== 0 || *p
!= 0)
730 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
733 /* --- Parameter selection --- */
739 /* --- Expiry dates get passed to @get_date@ for parsing --- */
742 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
745 exp
= get_date(optarg
, 0);
747 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
751 /* --- Store comments without interpretation --- */
754 if (key_chkcomment(optarg
))
755 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
759 /* --- Store tags --- */
762 if (key_chkident(optarg
))
763 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
767 /* --- Other flags --- */
779 /* --- Other things are bogus --- */
787 /* --- Various sorts of bogosity --- */
789 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
791 "Usage: add [options] type [attr...]");
793 if (key_chkident(argv
[optind
]))
794 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
796 /* --- Set up various bits of the state --- */
798 if (exp
== KEXP_EXPIRE
)
799 exp
= time(0) + 14 * 24 * 60 * 60;
801 /* --- Open the file and create the basic key block --- *
803 * Keep on generating keyids until one of them doesn't collide.
806 doopen(&f
, KOPEN_WRITE
);
809 /* --- Key the generator --- */
814 uint32 id
= rand_global
.ops
->word(&rand_global
);
816 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
819 if (err
!= KERR_DUPID
)
820 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
823 /* --- Set various simple attributes --- */
826 int err
= key_settag(&f
, k
.k
, tag
);
828 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
832 int err
= key_setcomment(&f
, k
.k
, c
);
834 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
837 setattr(&f
, k
.k
, argv
+ optind
+ 1);
839 key_fulltag(k
.k
, &k
.tag
);
841 /* --- Find the parameter key --- */
844 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
845 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
846 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
847 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
850 /* --- Now generate the actual key data --- */
860 /*----- Key listing -------------------------------------------------------*/
862 /* --- Listing options --- */
864 typedef struct listopts
{
865 const char *tfmt
; /* Date format (@strftime@-style) */
866 int v
; /* Verbosity level */
867 unsigned f
; /* Various flags */
868 time_t t
; /* Time now (for key expiry) */
869 key_filter kf
; /* Filter for matching keys */
872 /* --- Listing flags --- */
875 f_newline
= 2, /* Write newline before next entry */
876 f_attr
= 4, /* Written at least one attribute */
877 f_utc
= 8 /* Emit UTC time, not local time */
880 /* --- @showkeydata@ --- *
882 * Arguments: @key_data *k@ = pointer to key to write
883 * @int ind@ = indentation level
884 * @listopts *o@ = listing options
885 * @dstr *d@ = tag string for this subkey
889 * Use: Emits a piece of key data in a human-readable format.
892 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
894 #define INDENT(i) do { \
896 for (_i = 0; _i < (i); _i++) { \
901 switch (k
->e
& KF_ENCMASK
) {
903 /* --- Binary key data --- *
905 * Emit as a simple hex dump.
909 const octet
*p
= k
->u
.k
.k
;
910 const octet
*l
= p
+ k
->u
.k
.sz
;
918 } else if (sz
% 8 == 0)
922 printf("%02x", *p
++);
927 fputs("}\n", stdout
);
930 /* --- Encrypted data --- *
932 * If the user is sufficiently keen, ask for a passphrase and decrypt the
933 * key. Otherwise just say that it's encrypted and move on.
938 fputs(" encrypted\n", stdout
);
941 if (key_punlock(d
->buf
, k
, &kd
))
942 printf(" <failed to unlock %s>\n", d
->buf
);
944 fputs(" encrypted", stdout
);
945 showkeydata(&kd
, ind
, o
, d
);
951 /* --- Integer keys --- *
953 * Emit as a large integer in decimal. This makes using the key in
954 * `calc' or whatever easier.
959 mp_writefile(k
->u
.m
, stdout
, 10);
963 /* --- Structured keys --- *
965 * Just iterate over the subkeys.
973 fputs(" {\n", stdout
);
974 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
975 if (!key_match(&ks
->k
, &o
->kf
))
978 printf("%s =", SYM_NAME(ks
));
980 dstr_putf(d
, ".%s", SYM_NAME(ks
));
981 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
984 fputs("}\n", stdout
);
991 /* --- @showkey@ --- *
993 * Arguments: @key *k@ = pointer to key to show
994 * @listopts *o@ = pointer to listing options
998 * Use: Emits a listing of a particular key.
1001 static void showkey(key
*k
, listopts
*o
)
1003 char ebuf
[24], dbuf
[24];
1006 /* --- Skip the key if the filter doesn't match --- */
1008 if (!key_match(&k
->k
, &o
->kf
))
1011 /* --- Sort out the expiry and deletion times --- */
1013 if (KEY_EXPIRED(o
->t
, k
->exp
))
1014 strcpy(ebuf
, "expired");
1015 else if (k
->exp
== KEXP_FOREVER
)
1016 strcpy(ebuf
, "forever");
1018 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1019 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1022 if (KEY_EXPIRED(o
->t
, k
->del
))
1023 strcpy(dbuf
, "deleted");
1024 else if (k
->del
== KEXP_FOREVER
)
1025 strcpy(dbuf
, "forever");
1027 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1028 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1031 /* --- If in compact format, just display and quit --- */
1034 if (!(o
->f
& f_newline
)) {
1035 printf("%8s %-20s %-20s %-10s %-10s\n",
1036 "Id", "Tag", "Type", "Expire", "Delete");
1038 printf("%08lx %-20s %-20s %-10s %-10s\n",
1039 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1040 k
->type
, ebuf
, dbuf
);
1045 /* --- Display the standard header --- */
1047 if (o
->f
& f_newline
)
1048 fputc('\n', stdout
);
1049 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1050 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1051 printf("type: %s\n", k
->type
);
1052 printf("expiry: %s\n", ebuf
);
1053 printf("delete: %s\n", dbuf
);
1054 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1056 /* --- Display the attributes --- */
1060 const char *av
, *an
;
1063 printf("attributes:");
1064 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1065 printf("\n\t%s = %s", an
, av
);
1069 fputc('\n', stdout
);
1074 /* --- If dumping requested, dump the raw key data --- */
1078 fputs("key:", stdout
);
1080 showkeydata(&k
->k
, 0, o
, &d
);
1087 /* --- @cmd_list@ --- */
1089 static int cmd_list(int argc
, char *argv
[])
1093 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1095 /* --- Parse subcommand options --- */
1098 static struct option opt
[] = {
1099 { "quiet", 0, 0, 'q' },
1100 { "verbose", 0, 0, 'v' },
1101 { "utc", 0, 0, 'u' },
1102 { "filter", OPTF_ARGREQ
, 0, 'f' },
1105 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1122 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1124 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1133 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1135 /* --- Open the key file --- */
1137 doopen(&f
, KOPEN_READ
);
1140 /* --- Set up the time format --- */
1143 o
.tfmt
= "%Y-%m-%d";
1144 else if (o
.f
& f_utc
)
1145 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1147 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1149 /* --- If specific keys were requested use them, otherwise do all --- *
1151 * Some day, this might turn into a wildcard match.
1154 if (optind
< argc
) {
1156 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1159 moan("key `%s' not found", argv
[optind
]);
1163 } while (optind
< argc
);
1166 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1174 return (EXIT_FAILURE
);
1179 /*----- Command implementation --------------------------------------------*/
1181 /* --- @cmd_expire@ --- */
1183 static int cmd_expire(int argc
, char *argv
[])
1191 die(EXIT_FAILURE
, "Usage: expire tag...");
1192 doopen(&f
, KOPEN_WRITE
);
1193 for (i
= 1; i
< argc
; i
++) {
1194 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1197 moan("key `%s' not found", argv
[i
]);
1205 /* --- @cmd_delete@ --- */
1207 static int cmd_delete(int argc
, char *argv
[])
1215 die(EXIT_FAILURE
, "Usage: delete tag...");
1216 doopen(&f
, KOPEN_WRITE
);
1217 for (i
= 1; i
< argc
; i
++) {
1218 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1221 moan("key `%s' not found", argv
[i
]);
1229 /* --- @cmd_setattr@ --- */
1231 static int cmd_setattr(int argc
, char *argv
[])
1237 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1238 doopen(&f
, KOPEN_WRITE
);
1239 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1240 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1241 setattr(&f
, k
, argv
+ 2);
1246 /* --- @cmd_finger@ --- */
1248 static void fingerprint(key
*k
, const key_filter
*kf
)
1251 octet hash
[RMD160_HASHSZ
];
1255 if (!key_encode(&k
->k
, &d
, kf
))
1258 rmd160_hash(&r
, d
.buf
, d
.len
);
1259 rmd160_done(&r
, hash
);
1263 for (i
= 0; i
< sizeof(hash
); i
++) {
1264 if (i
&& i
% 4 == 0)
1266 printf("%02x", hash
[i
]);
1268 printf(" %s\n", d
.buf
);
1272 static int cmd_finger(int argc
, char *argv
[])
1276 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1279 static struct option opt
[] = {
1280 { "filter", OPTF_ARGREQ
, 0, 'f' },
1283 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1289 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1291 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1299 argv
+= optind
; argc
-= optind
;
1301 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1303 doopen(&f
, KOPEN_READ
);
1307 for (i
= 0; i
< argc
; i
++) {
1308 key
*k
= key_bytag(&f
, argv
[i
]);
1310 fingerprint(k
, &kf
);
1313 moan("key `%s' not found", argv
[i
]);
1319 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1320 fingerprint(k
, &kf
);
1325 /* --- @cmd_comment@ --- */
1327 static int cmd_comment(int argc
, char *argv
[])
1333 if (argc
< 2 || argc
> 3)
1334 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1335 doopen(&f
, KOPEN_WRITE
);
1336 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1337 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1338 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1339 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1344 /* --- @cmd_tag@ --- */
1346 static int cmd_tag(int argc
, char *argv
[])
1352 if (argc
< 2 || argc
> 3)
1353 die(EXIT_FAILURE
, "Usage: tag tag [new-tag]");
1354 doopen(&f
, KOPEN_WRITE
);
1355 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1356 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1357 if ((err
= key_settag(&f
, k
, argv
[2])) != 0)
1358 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[2], key_strerror(err
));
1363 /* --- @cmd_lock@ --- */
1365 static int cmd_lock(int argc
, char *argv
[])
1373 die(EXIT_FAILURE
, "Usage: lock qtag");
1374 doopen(&f
, KOPEN_WRITE
);
1375 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1376 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1377 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1378 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1379 if (key_plock(d
.buf
, kd
, kd
))
1380 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1386 /* --- @cmd_unlock@ --- */
1388 static int cmd_unlock(int argc
, char *argv
[])
1396 die(EXIT_FAILURE
, "Usage: unlock qtag");
1397 doopen(&f
, KOPEN_WRITE
);
1398 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1399 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1400 if (kd
->e
!= KENC_ENCRYPT
)
1401 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1402 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1403 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1409 /* --- @cmd_extract@ --- */
1411 static int cmd_extract(int argc
, char *argv
[])
1417 key_filter kf
= { 0, 0 };
1421 static struct option opt
[] = {
1422 { "filter", OPTF_ARGREQ
, 0, 'f' },
1425 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1431 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1433 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1441 argv
+= optind
; argc
-= optind
;
1443 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1444 if (strcmp(*argv
, "-") == 0)
1446 else if (!(fp
= fopen(*argv
, "w"))) {
1447 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1448 *argv
, strerror(errno
));
1451 doopen(&f
, KOPEN_READ
);
1455 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1456 key_extract(&f
, k
, fp
, &kf
);
1458 for (i
= 1; i
< argc
; i
++) {
1459 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1460 key_extract(&f
, k
, fp
, &kf
);
1462 moan("key `%s' not found", argv
[i
]);
1468 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1473 /* --- @cmd_tidy@ --- */
1475 static int cmd_tidy(int argc
, char *argv
[])
1479 die(EXIT_FAILURE
, "usage: tidy");
1480 doopen(&f
, KOPEN_WRITE
);
1481 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1486 /* --- @cmd_merge@ --- */
1488 static int cmd_merge(int argc
, char *argv
[])
1494 die(EXIT_FAILURE
, "Usage: merge file");
1495 if (strcmp(argv
[1], "-") == 0)
1497 else if (!(fp
= fopen(argv
[1], "r"))) {
1498 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1499 argv
[1], strerror(errno
));
1502 doopen(&f
, KOPEN_WRITE
);
1503 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1508 /*----- Main command table ------------------------------------------------*/
1512 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1516 "add [options] type [attr...]\n\
1517 Options: [-lq] [-a alg] [-b|-B bits] [-p param] [-r tag]\n\
1518 [-e expire] [-t tag] [-c comment]"
1520 { "expire", cmd_expire
, "expire tag..." },
1521 { "delete", cmd_delete
, "delete tag..." },
1522 { "tag", cmd_tag
, "tag tag [new-tag]" },
1523 { "setattr", cmd_setattr
, "setattr tag attr..." },
1524 { "comment", cmd_comment
, "comment tag [comment]" },
1525 { "lock", cmd_lock
, "lock qtag" },
1526 { "unlock", cmd_unlock
, "unlock qtag" },
1527 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]" },
1528 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]" },
1529 { "tidy", cmd_tidy
, "tidy" },
1530 { "extract", cmd_extract
, "extract [-f filter] file [tag...]" },
1531 { "merge", cmd_merge
, "merge file" },
1535 typedef struct cmd cmd
;
1537 /*----- Main code ---------------------------------------------------------*/
1539 /* --- Helpful GNUy functions --- */
1541 void usage(FILE *fp
)
1543 pquis(fp
, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1546 void version(FILE *fp
)
1548 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1558 Performs various simple key management operations. Command line options\n\
1561 -h, --help Display this help text.\n\
1562 -v, --version Display version number.\n\
1563 -u, --usage Display short usage summary.\n\
1565 -k, --keyring=FILE Read and write keys in FILE.\n\
1566 -i, --id=TAG Use key TAG for random number generator.\n\
1567 -t, --type=TYPE Use key TYPE for random number generator.\n\
1569 The following commands are understood:\n\n",
1571 for (c
= cmds
; c
->name
; c
++)
1572 fprintf(fp
, "%s\n", c
->help
);
1577 * Arguments: @int argc@ = number of command line arguments
1578 * @char *argv[]@ = array of command line arguments
1580 * Returns: Nonzero on failure.
1582 * Use: Main program. Performs simple key management functions.
1585 int main(int argc
, char *argv
[])
1593 /* --- Initialization --- */
1598 /* --- Parse command line options --- */
1601 static struct option opt
[] = {
1603 /* --- Standard GNUy help options --- */
1605 { "help", 0, 0, 'h' },
1606 { "version", 0, 0, 'v' },
1607 { "usage", 0, 0, 'u' },
1609 /* --- Real live useful options --- */
1611 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1612 { "id", OPTF_ARGREQ
, 0, 'i' },
1613 { "type", OPTF_ARGREQ
, 0, 't' },
1615 /* --- Magic terminator --- */
1619 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1625 /* --- GNU help options --- */
1636 /* --- Real useful options --- */
1642 /* --- Bogosity --- */
1650 /* --- Complain about excessive bogons --- */
1652 if (f
& f_bogus
|| optind
== argc
) {
1657 /* --- Initialize the Catacomb random number generator --- */
1659 rand_init(RAND_GLOBAL
);
1660 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1662 /* --- Dispatch to appropriate command handler --- */
1669 cmd
*c
, *chosen
= 0;
1670 size_t sz
= strlen(argv
[0]);
1672 for (c
= cmds
; c
->name
; c
++) {
1673 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1674 if (c
->name
[sz
] == 0) {
1678 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1684 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1685 return (chosen
->cmd(argc
, argv
));
1689 /*----- That's all, folks -------------------------------------------------*/