3 * $Id: keyutil.c,v 1.4 1999/12/22 15:48:10 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.4 1999/12/22 15:48:10 mdw
34 * Track new key-management changes. Support new key generation
37 * Revision 1.3 1999/11/02 15:23:24 mdw
38 * Fix newlines in keyring list.
40 * Revision 1.2 1999/10/15 21:05:28 mdw
41 * In `key list', show timezone for local times, and support `-u' option
44 * Revision 1.1 1999/09/03 08:41:12 mdw
49 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/base64.h>
60 #include <mLib/mdwopt.h>
61 #include <mLib/quis.h>
62 #include <mLib/report.h>
80 /*----- Handy global state ------------------------------------------------*/
82 static const char *keyfile
= "keyring";
84 /*----- Useful shared functions -------------------------------------------*/
88 * Arguments: @key_file *f@ = pointer to key file block
89 * @unsigned how@ = method to open file with
93 * Use: Opens a key file and handles errors by panicking
97 static void doopen(key_file
*f
, unsigned how
)
99 if (key_open(f
, keyfile
, how
, key_moan
, 0))
100 die(1, "couldn't open file `%s': %s", keyfile
, strerror(errno
));
103 /* --- @doclose@ --- *
105 * Arguments: @key_file *f@ = pointer to key file block
109 * Use: Closes a key file and handles errors by panicking
113 static void doclose(key_file
*f
)
115 switch (key_close(f
)) {
117 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
118 keyfile
, strerror(errno
));
120 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
121 keyfile
, strerror(errno
));
125 /* --- @setattr@ --- *
127 * Arguments: @key_file *f@ = pointer to key file block
128 * @key *k@ = pointer to key block
129 * @char *v[]@ = array of assignments (overwritten!)
133 * Use: Applies the attribute assignments to the key.
136 static void setattr(key_file
*f
, key
*k
, char *v
[])
141 size_t eq
= strcspn(p
, "=");
143 moan("invalid assignment: `%s'", p
);
146 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
147 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
152 /*----- Key generation ----------------------------------------------------*/
154 /* --- Key generation parameters --- */
156 typedef struct keyopts
{
157 key_file
*kf
; /* Pointer to key file */
158 key
*k
; /* Pointer to the actual key */
159 dstr tag
; /* Full tag name for the key */
160 unsigned f
; /* Flags for the new key */
161 unsigned bits
, qbits
; /* Bit length for the new key */
162 key
*p
; /* Parameters key-data */
166 f_bogus
= 1, /* Error in parsing */
167 f_lock
= 2, /* Passphrase-lock private key */
168 f_quiet
= 4 /* Don't show a progress indicator */
171 /* --- @dolock@ --- *
173 * Arguments: @keyopts *k@ = key generation options
174 * @key_data *kd@ = pointer to key data to lock
175 * @const char *t@ = tag suffix or null
179 * Use: Does passphrase locking on new keys.
182 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
184 if (!(k
->f
& f_lock
))
187 dstr_putf(&k
->tag
, ".%s", t
);
188 if (key_plock(k
->tag
.buf
, kd
, kd
))
189 die(EXIT_FAILURE
, "couldn't lock key");
194 * Arguments: @key_data *kd@ = pointer to parent key block
195 * @const char *tag@ = pointer to tag string
196 * @mp *m@ = integer to store
197 * @unsigned f@ = flags to set
201 * Use: Sets a multiprecision integer subkey.
204 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
206 key_data
*kkd
= key_structcreate(kd
, tag
);
211 /* --- @copyparam@ --- *
213 * Arguments: @keyopts *k@ = pointer to key options
214 * @const char **pp@ = checklist of parameters
216 * Returns: Nonzero if parameters copied; zero if you have to generate
219 * Use: Copies parameters from a source key to the current one.
222 static int copyparam(keyopts
*k
, const char **pp
)
226 /* --- Quick check if no parameters supplied --- */
231 /* --- Run through the checklist --- */
234 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
236 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
237 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
238 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
242 /* --- Copy over the parameters --- */
246 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
247 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
253 * Arguments: @key_data *k@ = pointer to key data block
254 * @const char *tag@ = tag string to use
256 * Returns: Pointer to multiprecision integer key item.
258 * Use: Fetches an MP key component.
261 static mp
*getmp(key_data
*k
, const char *tag
)
263 k
= key_structfind(k
, tag
);
265 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
266 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
267 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
271 /* --- Key generation algorithms --- */
273 static void alg_binary(keyopts
*k
)
282 die(EXIT_FAILURE
, "no shared parameters for binary keys");
284 sz
= (k
->bits
+ 7) >> 3;
286 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
287 rand_getgood(RAND_GLOBAL
, p
, sz
);
289 key_binary(&k
->k
->k
, p
, sz
);
290 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
293 dolock(k
, &k
->k
->k
, 0);
296 static void alg_des(keyopts
*k
)
305 die(EXIT_FAILURE
, "no shared parameters for DES keys");
306 if (k
->bits
% 56 || k
->bits
> 168)
307 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
311 rand_getgood(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
312 for (i
= 0; i
< sz
; i
++) {
313 octet x
= p
[i
] & 0xfe;
316 x
= x
^ (x
>> 1) ^ 1;
317 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
319 key_binary(&k
->k
->k
, p
, sz
);
320 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
323 dolock(k
, &k
->k
->k
, 0);
326 static void alg_rsa(keyopts
*k
)
331 /* --- Sanity checking --- */
334 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
338 /* --- Generate the RSA parameters --- */
340 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
341 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
342 die(EXIT_FAILURE
, "RSA key generation failed");
344 /* --- Run a test encryption --- */
347 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
349 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
352 /* --- Encrypt the plaintext --- */
354 mpmont_create(&mm
, rp
.n
);
355 c
= mpmont_exp(&mm
, MP_NEW
, m
, rp
.e
);
358 /* --- Decrypt the ciphertext --- */
360 c
= rsa_decrypt(&rp
, c
, c
, g
);
362 /* --- Check everything went OK --- */
364 if (MP_CMP(c
, !=, m
))
365 die(EXIT_FAILURE
, "test encryption failed");
371 /* --- Allrighty then --- */
375 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
376 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
378 kd
= key_structcreate(kd
, "private");
380 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
381 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
382 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
383 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
384 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
385 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
386 dolock(k
, kd
, "private");
388 mp_drop(rp
.p
); mp_drop(rp
.q
); mp_drop(rp
.n
); mp_drop(rp
.q_inv
);
389 mp_drop(rp
.e
); mp_drop(rp
.d
); mp_drop(rp
.dp
); mp_drop(rp
.dq
);
392 static void alg_dsaparam(keyopts
*k
)
394 static const char *pl
[] = { "q", "p", "g", 0 };
395 if (!copyparam(k
, pl
)) {
401 key_data
*kd
= &k
->k
->k
;
403 /* --- Choose appropriate bit lengths if necessary --- */
410 /* --- Allocate a seed block --- */
412 sz
= (k
->qbits
+ 7) >> 3;
414 rand_getgood(RAND_GLOBAL
, p
, sz
);
416 /* --- Allocate the parameters --- */
418 if (dsa_seed(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
,
419 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
420 die(EXIT_FAILURE
, "DSA parameter generation failed");
422 /* --- Store the parameters --- */
425 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
426 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
427 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
432 /* --- Store the seed for future verification --- */
437 base64_encode(&c
, p
, sz
, &d
);
438 base64_encode(&c
, 0, 0, &d
);
439 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
445 static void alg_dsa(keyopts
*k
)
450 key_data
*kd
= &k
->k
->k
;
452 /* --- Get the shared parameters --- */
459 /* --- Choose a private key --- */
461 x
= mprand_range(MP_NEW
, q
, &rand_global
, 0);
463 mpmont_create(&mm
, p
);
464 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
466 /* --- Store everything away --- */
468 mpkey(kd
, "y", y
, KCAT_PUB
);
470 kd
= key_structcreate(kd
, "private");
472 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
473 dolock(k
, kd
, "private");
476 static void alg_dhparam(keyopts
*k
)
478 static const char *pl
[] = { "p", "g", 0 };
479 if (!copyparam(k
, pl
)) {
482 key_data
*kd
= &k
->k
->k
;
487 /* --- Choose a large safe prime number --- */
490 q
= mprand(q
, k
->bits
, &rand_global
, 3);
491 p
= pgen("p", MP_NEW
, q
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
492 0, pgen_safestep
, &c
.c
,
493 rabin_iters(k
->bits
), pgen_safetest
, &c
);
495 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
498 mpkey(kd
, "p", p
, KCAT_SHARE
);
502 /* --- The generator 4 is good --- *
504 * Since 4 is clearly a quadratic residue, and %$p = 2q + 1$% for prime
505 * %$q$%, the number 4 has order %$q$%. This is better than choosing a
506 * real primitive element, because it could conceivably be trapped in an
507 * order-2 subgroup. (Not very likely, I'll admit, but possible.)
510 mpkey(kd
, "g", MP_FOUR
, KCAT_SHARE
);
514 static void alg_dh(keyopts
*k
)
519 key_data
*kd
= &k
->k
->k
;
521 /* --- Get the shared parameters --- */
527 /* --- Choose a suitable private key --- *
529 * Since %$g$% has order %$q$%, choose %$x < q$%.
532 y
= mp_lsr(MP_NEW
, p
, 1);
533 x
= mprand_range(MP_NEW
, y
, &rand_global
, 0);
536 /* --- Compute the public key %$y = g^x \bmod p$% --- */
538 mpmont_create(&mm
, p
);
539 y
= mpmont_exp(&mm
, y
, g
, x
);
542 /* --- Store everything away --- */
544 mpkey(kd
, "y", y
, KCAT_PUB
);
546 kd
= key_structcreate(kd
, "private");
548 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
549 dolock(k
, kd
, "private");
552 static void alg_bbs(keyopts
*k
)
558 /* --- Sanity checking --- */
561 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
565 /* --- Generate the BBS parameters --- */
567 p
= mprand(MP_NEW
, k
->bits
/ 2, &rand_global
, 3);
568 q
= mprand(MP_NEW
, k
->bits
- k
->bits
/ 2, &rand_global
, 3);
569 mp_burn(p
); mp_burn(q
);
570 if (bbs_gen(&bp
, p
, q
, 0, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
571 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
572 mp_drop(p
); mp_drop(q
);
574 /* --- Allrighty then --- */
578 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
580 kd
= key_structcreate(kd
, "private");
582 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
583 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
584 dolock(k
, kd
, "private");
586 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
);
589 /* --- The algorithm tables --- */
591 typedef struct keyalg
{
593 void (*proc
)(keyopts
*o
);
597 static keyalg algtab
[] = {
598 { "binary", alg_binary
, "Plain binary data" },
599 { "des", alg_des
, "Binary with DES-style parity" },
600 { "rsa", alg_rsa
, "RSA public-key encryption" },
601 { "dsa", alg_dsa
, "DSA digital signatures" },
602 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
603 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
604 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
605 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
609 /* --- @cmd_add@ --- */
611 static int cmd_add(int argc
, char *argv
[])
614 time_t exp
= KEXP_EXPIRE
;
615 const char *tag
= 0, *ptag
= 0;
617 keyalg
*alg
= algtab
;
618 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
620 /* --- Parse options for the subcommand --- */
623 static struct option opt
[] = {
624 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
625 { "bits", OPTF_ARGREQ
, 0, 'b' },
626 { "qbits", OPTF_ARGREQ
, 0, 'B' },
627 { "parameters", OPTF_ARGREQ
, 0, 'p' },
628 { "expire", OPTF_ARGREQ
, 0, 'e' },
629 { "comment", OPTF_ARGREQ
, 0, 'c' },
630 { "tag", OPTF_ARGREQ
, 0, 't' },
631 { "lock", 0, 0, 'l' },
632 { "quiet", 0, 0, 'q' },
635 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:l", opt
, 0, 0, 0);
639 /* --- Handle the various options --- */
643 /* --- Read an algorithm name --- */
647 size_t sz
= strlen(optarg
);
649 if (strcmp(optarg
, "list") == 0) {
650 for (a
= algtab
; a
->name
; a
++)
651 printf("%-10s %s\n", a
->name
, a
->help
);
656 for (a
= algtab
; a
->name
; a
++) {
657 if (strncmp(optarg
, a
->name
, sz
) == 0) {
658 if (a
->name
[sz
] == 0) {
662 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
668 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
671 /* --- Bits must be nonzero and a multiple of 8 --- */
675 k
.bits
= strtoul(optarg
, &p
, 0);
676 if (k
.bits
== 0 || *p
!= 0)
677 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
682 k
.qbits
= strtoul(optarg
, &p
, 0);
683 if (k
.qbits
== 0 || *p
!= 0)
684 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
687 /* --- Parameter selection --- */
693 /* --- Expiry dates get passed to @get_date@ for parsing --- */
696 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
699 exp
= get_date(optarg
, 0);
701 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
705 /* --- Store comments without interpretation --- */
708 if (key_chkcomment(optarg
))
709 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
713 /* --- Store tags --- */
716 if (key_chkident(optarg
))
717 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
721 /* --- Other flags --- */
730 /* --- Other things are bogus --- */
738 /* --- Various sorts of bogosity --- */
740 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
742 "Usage: add [options] type [attr...]");
744 if (key_chkident(argv
[optind
]))
745 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
747 /* --- Set up various bits of the state --- */
749 if (exp
== KEXP_EXPIRE
)
750 exp
= time(0) + 14 * 24 * 60 * 60;
752 /* --- Open the file and create the basic key block --- *
754 * Keep on generating keyids until one of them doesn't collide.
757 doopen(&f
, KOPEN_WRITE
);
761 uint32 id
= rand_global
.ops
->word(&rand_global
);
763 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
766 if (err
!= KERR_DUPID
)
767 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
770 /* --- Set various simple attributes --- */
773 int err
= key_settag(&f
, k
.k
, tag
);
775 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
779 int err
= key_setcomment(&f
, k
.k
, c
);
781 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
784 setattr(&f
, k
.k
, argv
+ optind
+ 1);
786 key_fulltag(k
.k
, &k
.tag
);
788 /* --- Find the parameter key --- */
791 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
792 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
793 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
794 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
797 /* --- Now generate the actual key data --- */
807 /*----- Key listing -------------------------------------------------------*/
809 /* --- Listing options --- */
811 typedef struct listopts
{
812 const char *tfmt
; /* Date format (@strftime@-style) */
813 int v
; /* Verbosity level */
814 unsigned f
; /* Various flags */
815 time_t t
; /* Time now (for key expiry) */
816 key_filter kf
; /* Filter for matching keys */
819 /* --- Listing flags --- */
822 f_newline
= 2, /* Write newline before next entry */
823 f_attr
= 4, /* Written at least one attribute */
824 f_utc
= 8 /* Emit UTC time, not local time */
827 /* --- @showkeydata@ --- *
829 * Arguments: @key_data *k@ = pointer to key to write
830 * @int ind@ = indentation level
831 * @listopts *o@ = listing options
832 * @dstr *d@ = tag string for this subkey
836 * Use: Emits a piece of key data in a human-readable format.
839 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
841 #define INDENT(i) do { \
843 for (_i = 0; _i < (i); _i++) { \
848 switch (k
->e
& KF_ENCMASK
) {
850 /* --- Binary key data --- *
852 * Emit as a simple hex dump.
856 const octet
*p
= k
->u
.k
.k
;
857 const octet
*l
= p
+ k
->u
.k
.sz
;
865 } else if (sz
% 8 == 0)
869 printf("%02x", *p
++);
874 fputs("}\n", stdout
);
877 /* --- Encrypted data --- *
879 * If the user is sufficiently keen, ask for a passphrase and decrypt the
880 * key. Otherwise just say that it's encrypted and move on.
885 fputs(" encrypted\n", stdout
);
888 if (key_punlock(d
->buf
, k
, &kd
))
889 printf(" <failed to unlock %s>\n", d
->buf
);
891 fputs(" encrypted", stdout
);
892 showkeydata(&kd
, ind
, o
, d
);
898 /* --- Integer keys --- *
900 * Emit as a large integer in decimal. This makes using the key in
901 * `calc' or whatever easier.
906 mp_writefile(k
->u
.m
, stdout
, 10);
910 /* --- Structured keys --- *
912 * Just iterate over the subkeys.
920 fputs(" {\n", stdout
);
921 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
922 if (!key_match(&ks
->k
, &o
->kf
))
925 printf("%s =", SYM_NAME(ks
));
927 dstr_putf(d
, ".%s", SYM_NAME(ks
));
928 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
931 fputs("}\n", stdout
);
938 /* --- @showkey@ --- *
940 * Arguments: @key *k@ = pointer to key to show
941 * @listopts *o@ = pointer to listing options
945 * Use: Emits a listing of a particular key.
948 static void showkey(key
*k
, listopts
*o
)
950 char ebuf
[24], dbuf
[24];
953 /* --- Skip the key if the filter doesn't match --- */
955 if (!key_match(&k
->k
, &o
->kf
))
958 /* --- Sort out the expiry and deletion times --- */
960 if (KEY_EXPIRED(o
->t
, k
->exp
))
961 strcpy(ebuf
, "expired");
962 else if (k
->exp
== KEXP_FOREVER
)
963 strcpy(ebuf
, "forever");
965 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
966 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
969 if (KEY_EXPIRED(o
->t
, k
->del
))
970 strcpy(dbuf
, "deleted");
971 else if (k
->del
== KEXP_FOREVER
)
972 strcpy(dbuf
, "forever");
974 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
975 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
978 /* --- If in compact format, just display and quit --- */
981 if (!(o
->f
& f_newline
)) {
982 printf("%8s %-20s %-20s %-10s %-10s\n",
983 "Id", "Tag", "Type", "Expire", "Delete");
985 printf("%08lx %-20s %-20s %-10s %-10s\n",
986 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
987 k
->type
, ebuf
, dbuf
);
992 /* --- Display the standard header --- */
994 if (o
->f
& f_newline
)
996 printf("keyid: %08lx\n", (unsigned long)k
->id
);
997 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
998 printf("type: %s\n", k
->type
);
999 printf("expiry: %s\n", ebuf
);
1000 printf("delete: %s\n", dbuf
);
1001 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1003 /* --- Display the attributes --- */
1007 const char *av
, *an
;
1010 printf("attributes:");
1011 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1012 printf("\n\t%s = %s", an
, av
);
1016 fputc('\n', stdout
);
1021 /* --- If dumping requested, dump the raw key data --- */
1025 fputs("key:", stdout
);
1027 showkeydata(&k
->k
, 0, o
, &d
);
1034 /* --- @cmd_list@ --- */
1036 static int cmd_list(int argc
, char *argv
[])
1040 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1042 /* --- Parse subcommand options --- */
1045 static struct option opt
[] = {
1046 { "quiet", 0, 0, 'q' },
1047 { "verbose", 0, 0, 'v' },
1048 { "utc", 0, 0, 'u' },
1049 { "filter", OPTF_ARGREQ
, 0, 'f' },
1052 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1069 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1071 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1080 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1082 /* --- Open the key file --- */
1084 doopen(&f
, KOPEN_READ
);
1087 /* --- Set up the time format --- */
1090 o
.tfmt
= "%Y-%m-%d";
1091 else if (o
.f
& f_utc
)
1092 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1094 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1096 /* --- If specific keys were requested use them, otherwise do all --- *
1098 * Some day, this might turn into a wildcard match.
1101 if (optind
< argc
) {
1103 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1106 moan("key `%s' not found", argv
[optind
]);
1110 } while (optind
< argc
);
1113 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1121 return (EXIT_FAILURE
);
1126 /*----- Command implementation --------------------------------------------*/
1128 /* --- @cmd_expire@ --- */
1130 static int cmd_expire(int argc
, char *argv
[])
1138 die(EXIT_FAILURE
, "Usage: expire tag...");
1139 doopen(&f
, KOPEN_WRITE
);
1140 for (i
= 1; i
< argc
; i
++) {
1141 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1144 moan("key `%s' not found", argv
[i
]);
1152 /* --- @cmd_delete@ --- */
1154 static int cmd_delete(int argc
, char *argv
[])
1162 die(EXIT_FAILURE
, "Usage: delete tag...");
1163 doopen(&f
, KOPEN_WRITE
);
1164 for (i
= 1; i
< argc
; i
++) {
1165 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1168 moan("key `%s' not found", argv
[i
]);
1176 /* --- @cmd_setattr@ --- */
1178 static int cmd_setattr(int argc
, char *argv
[])
1184 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1185 doopen(&f
, KOPEN_WRITE
);
1186 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1187 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1188 setattr(&f
, k
, argv
+ 2);
1193 /* --- @cmd_finger@ --- */
1195 static int fpkey(key_data
*kd
, dstr
*d
, void *p
)
1198 switch (kd
->e
& KF_ENCMASK
) {
1201 rmd160_hash(r
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
1204 size_t sz
= mp_octets(kd
->u
.m
);
1205 octet
*q
= sub_alloc(sz
);
1206 mp_storeb(kd
->u
.m
, q
, sz
);
1207 rmd160_hash(r
, q
, sz
);
1215 static void fingerprint(key
*k
, const key_filter
*kf
)
1218 octet hash
[RMD160_HASHSZ
];
1222 if (!key_match(&k
->k
, kf
))
1225 key_do(&k
->k
, kf
, 0, fpkey
, &r
);
1226 rmd160_done(&r
, hash
);
1229 for (i
= 0; i
< sizeof(hash
); i
++) {
1230 if (i
&& i
% 4 == 0)
1232 printf("%02x", hash
[i
]);
1234 printf(" %s\n", d
.buf
);
1238 static int cmd_finger(int argc
, char *argv
[])
1242 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1245 static struct option opt
[] = {
1246 { "filter", OPTF_ARGREQ
, 0, 'f' },
1249 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1255 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1257 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1265 argv
+= optind
; argc
-= optind
;
1267 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1269 doopen(&f
, KOPEN_READ
);
1273 for (i
= 0; i
< argc
; i
++) {
1274 key
*k
= key_bytag(&f
, argv
[i
]);
1276 fingerprint(k
, &kf
);
1279 moan("key `%s' not found", argv
[i
]);
1285 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1286 fingerprint(k
, &kf
);
1291 /* --- @cmd_comment@ --- */
1293 static int cmd_comment(int argc
, char *argv
[])
1299 if (argc
< 2 || argc
> 3)
1300 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1301 doopen(&f
, KOPEN_WRITE
);
1302 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1303 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1304 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1305 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1310 /* --- @cmd_tag@ --- */
1312 static int cmd_tag(int argc
, char *argv
[])
1318 if (argc
< 2 || argc
> 3)
1319 die(EXIT_FAILURE
, "Usage: tag tag [new-tag]");
1320 doopen(&f
, KOPEN_WRITE
);
1321 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1322 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1323 if ((err
= key_settag(&f
, k
, argv
[2])) != 0)
1324 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[2], key_strerror(err
));
1329 /* --- @cmd_lock@ --- */
1331 static int cmd_lock(int argc
, char *argv
[])
1339 die(EXIT_FAILURE
, "Usage: lock qtag");
1340 doopen(&f
, KOPEN_WRITE
);
1341 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1342 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1343 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1344 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1345 if (key_plock(d
.buf
, kd
, kd
))
1346 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1352 /* --- @cmd_unlock@ --- */
1354 static int cmd_unlock(int argc
, char *argv
[])
1362 die(EXIT_FAILURE
, "Usage: unlock qtag");
1363 doopen(&f
, KOPEN_WRITE
);
1364 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1365 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1366 if (kd
->e
!= KENC_ENCRYPT
)
1367 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1368 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1369 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1375 /* --- @cmd_extract@ --- */
1377 static int cmd_extract(int argc
, char *argv
[])
1383 key_filter kf
= { 0, 0 };
1387 static struct option opt
[] = {
1388 { "filter", OPTF_ARGREQ
, 0, 'f' },
1391 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1397 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1399 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1407 argv
+= optind
; argc
-= optind
;
1409 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1410 if (strcmp(*argv
, "-") == 0)
1412 else if (!(fp
= fopen(*argv
, "w"))) {
1413 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1414 *argv
, strerror(errno
));
1417 doopen(&f
, KOPEN_READ
);
1421 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1422 key_extract(&f
, k
, fp
, &kf
);
1424 for (i
= 1; i
< argc
; i
++) {
1425 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1426 key_extract(&f
, k
, fp
, &kf
);
1428 moan("key `%s' not found", argv
[i
]);
1434 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1439 /* --- @cmd_tidy@ --- */
1441 static int cmd_tidy(int argc
, char *argv
[])
1445 die(EXIT_FAILURE
, "usage: tidy");
1446 doopen(&f
, KOPEN_WRITE
);
1447 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1452 /* --- @cmd_merge@ --- */
1454 static int cmd_merge(int argc
, char *argv
[])
1460 die(EXIT_FAILURE
, "Usage: merge file");
1461 if (strcmp(argv
[1], "-") == 0)
1463 else if (!(fp
= fopen(argv
[1], "r"))) {
1464 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1465 argv
[1], strerror(errno
));
1468 doopen(&f
, KOPEN_WRITE
);
1469 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1474 /*----- Main command table ------------------------------------------------*/
1478 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1482 "add [options] type [attr...]\n\
1483 Options: [-l] [-a alg] [-b bits] [-p param]\n\
1484 [-e expire] [-t tag] [-c comment]"
1486 { "expire", cmd_expire
, "expire tag..." },
1487 { "delete", cmd_delete
, "delete tag..." },
1488 { "tag", cmd_tag
, "tag tag [new-tag]" },
1489 { "setattr", cmd_setattr
, "setattr tag attr..." },
1490 { "comment", cmd_comment
, "comment tag [comment]" },
1491 { "lock", cmd_lock
, "lock qtag" },
1492 { "unlock", cmd_unlock
, "unlock qtag" },
1493 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]" },
1494 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]" },
1495 { "tidy", cmd_tidy
, "tidy" },
1496 { "extract", cmd_extract
, "extract file qtag..." },
1497 { "merge", cmd_merge
, "merge file" },
1501 typedef struct cmd cmd
;
1503 /*----- Main code ---------------------------------------------------------*/
1505 /* --- Helpful GNUy functions --- */
1507 void usage(FILE *fp
)
1509 fprintf(fp
, "Usage: %s [-k file] command [args]\n", QUIS
);
1512 void version(FILE *fp
)
1514 fprintf(fp
, "%s, Catacomb version " VERSION
"\n", QUIS
);
1524 Performs various simple key management operations. Command line options\n\
1527 -h, --help Display this help text.\n\
1528 -v, --version Display version number.\n\
1529 -u, --usage Display short usage summary.\n\
1531 -k, --keyring=FILE Read and write keys in FILE.\n\
1533 The following commands are understood:\n\n",
1535 for (c
= cmds
; c
->name
; c
++)
1536 fprintf(fp
, "%s\n", c
->help
);
1541 * Arguments: @int argc@ = number of command line arguments
1542 * @char *argv[]@ = array of command line arguments
1544 * Returns: Nonzero on failure.
1546 * Use: Main program. Performs simple key management functions.
1549 int main(int argc
, char *argv
[])
1557 /* --- Initialization --- */
1562 /* --- Initialize the Catacomb random number generator --- */
1564 rand_init(RAND_GLOBAL
);
1565 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1567 /* --- Parse command line options --- */
1570 static struct option opt
[] = {
1572 /* --- Standard GNUy help options --- */
1574 { "help", 0, 0, 'h' },
1575 { "version", 0, 0, 'v' },
1576 { "usage", 0, 0, 'u' },
1578 /* --- Real live useful options --- */
1580 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1582 /* --- Magic terminator --- */
1586 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1592 /* --- GNU help options --- */
1603 /* --- Real useful options --- */
1609 /* --- Bogosity --- */
1617 /* --- Complain about excessive bogons --- */
1619 if (f
& f_bogus
|| optind
== argc
) {
1624 /* --- Dispatch to appropriate command handler --- */
1631 cmd
*c
, *chosen
= 0;
1632 size_t sz
= strlen(argv
[0]);
1634 for (c
= cmds
; c
->name
; c
++) {
1635 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1636 if (c
->name
[sz
] == 0) {
1640 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1646 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1647 return (chosen
->cmd(argc
, argv
));
1651 /*----- That's all, folks -------------------------------------------------*/