3 * $Id: keyutil.c,v 1.14 2001/02/23 09:03: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.14 2001/02/23 09:03:27 mdw
34 * Simplify usage message by removing nonexistant options.
36 * Revision 1.13 2001/02/21 20:04:27 mdw
37 * Provide help on individual commands (some need it desparately). Allow
38 * atomic retagging of keys.
40 * Revision 1.12 2001/02/03 11:58:22 mdw
41 * Store the correct seed information and count for DSA keys now that it's
44 * Revision 1.11 2000/12/06 20:33:27 mdw
45 * Make flags be macros rather than enumerations, to ensure that they're
48 * Revision 1.10 2000/10/08 12:02:21 mdw
49 * Use @MP_EQ@ instead of @MP_CMP@.
51 * Revision 1.9 2000/08/15 21:40:49 mdw
52 * Minor formatting change in listing attributes.
54 * Revision 1.8 2000/07/29 09:59:13 mdw
55 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
57 * Revision 1.7 2000/07/01 11:18:51 mdw
58 * Use new interfaces for key manipulation.
60 * Revision 1.6 2000/06/17 11:28:22 mdw
61 * Use secure memory interface from MP library. `rand_getgood' is
64 * Revision 1.5 2000/02/12 18:21:03 mdw
65 * Overhaul of key management (again).
67 * Revision 1.4 1999/12/22 15:48:10 mdw
68 * Track new key-management changes. Support new key generation
71 * Revision 1.3 1999/11/02 15:23:24 mdw
72 * Fix newlines in keyring list.
74 * Revision 1.2 1999/10/15 21:05:28 mdw
75 * In `key list', show timezone for local times, and support `-u' option
78 * Revision 1.1 1999/09/03 08:41:12 mdw
83 /*----- Header files ------------------------------------------------------*/
93 #include <mLib/base64.h>
94 #include <mLib/mdwopt.h>
95 #include <mLib/quis.h>
96 #include <mLib/report.h>
115 /*----- Handy global state ------------------------------------------------*/
117 static const char *keyfile
= "keyring";
119 /*----- Useful shared functions -------------------------------------------*/
121 /* --- @doopen@ --- *
123 * Arguments: @key_file *f@ = pointer to key file block
124 * @unsigned how@ = method to open file with
128 * Use: Opens a key file and handles errors by panicking
132 static void doopen(key_file
*f
, unsigned how
)
134 if (key_open(f
, keyfile
, how
, key_moan
, 0))
135 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
138 /* --- @doclose@ --- *
140 * Arguments: @key_file *f@ = pointer to key file block
144 * Use: Closes a key file and handles errors by panicking
148 static void doclose(key_file
*f
)
150 switch (key_close(f
)) {
152 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
153 keyfile
, strerror(errno
));
155 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
156 keyfile
, strerror(errno
));
160 /* --- @setattr@ --- *
162 * Arguments: @key_file *f@ = pointer to key file block
163 * @key *k@ = pointer to key block
164 * @char *v[]@ = array of assignments (overwritten!)
168 * Use: Applies the attribute assignments to the key.
171 static void setattr(key_file
*f
, key
*k
, char *v
[])
176 size_t eq
= strcspn(p
, "=");
178 moan("invalid assignment: `%s'", p
);
181 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
182 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
187 /*----- Key generation ----------------------------------------------------*/
189 /* --- Key generation parameters --- */
191 typedef struct keyopts
{
192 key_file
*kf
; /* Pointer to key file */
193 key
*k
; /* Pointer to the actual key */
194 dstr tag
; /* Full tag name for the key */
195 unsigned f
; /* Flags for the new key */
196 unsigned bits
, qbits
; /* Bit length for the new key */
197 key
*p
; /* Parameters key-data */
200 #define f_bogus 1u /* Error in parsing */
201 #define f_lock 2u /* Passphrase-lock private key */
202 #define f_quiet 4u /* Don't show a progress indicator */
203 #define f_limlee 8u /* Generate Lim-Lee primes */
204 #define f_subgroup 16u /* Generate a subgroup */
205 #define f_retag 32u /* Remove any existing tag */
207 /* --- @dolock@ --- *
209 * Arguments: @keyopts *k@ = key generation options
210 * @key_data *kd@ = pointer to key data to lock
211 * @const char *t@ = tag suffix or null
215 * Use: Does passphrase locking on new keys.
218 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
220 if (!(k
->f
& f_lock
))
223 dstr_putf(&k
->tag
, ".%s", t
);
224 if (key_plock(k
->tag
.buf
, kd
, kd
))
225 die(EXIT_FAILURE
, "couldn't lock key");
230 * Arguments: @key_data *kd@ = pointer to parent key block
231 * @const char *tag@ = pointer to tag string
232 * @mp *m@ = integer to store
233 * @unsigned f@ = flags to set
237 * Use: Sets a multiprecision integer subkey.
240 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
242 key_data
*kkd
= key_structcreate(kd
, tag
);
247 /* --- @copyparam@ --- *
249 * Arguments: @keyopts *k@ = pointer to key options
250 * @const char **pp@ = checklist of parameters
252 * Returns: Nonzero if parameters copied; zero if you have to generate
255 * Use: Copies parameters from a source key to the current one.
258 static int copyparam(keyopts
*k
, const char **pp
)
262 /* --- Quick check if no parameters supplied --- */
267 /* --- Run through the checklist --- */
270 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
272 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
273 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
274 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
278 /* --- Copy over the parameters --- */
282 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
283 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
289 * Arguments: @key_data *k@ = pointer to key data block
290 * @const char *tag@ = tag string to use
292 * Returns: Pointer to multiprecision integer key item.
294 * Use: Fetches an MP key component.
297 static mp
*getmp(key_data
*k
, const char *tag
)
299 k
= key_structfind(k
, tag
);
301 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
302 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
303 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
307 /* --- @keyrand@ --- *
309 * Arguments: @key_file *kf@ = pointer to key file
310 * @const char *id@ = pointer to key id (or null)
314 * Use: Keys the random number generator.
317 static void keyrand(key_file
*kf
, const char *id
)
321 /* --- Find the key --- */
324 if ((k
= key_bytag(kf
, id
)) == 0)
325 die(EXIT_FAILURE
, "key `%s' not found", id
);
327 k
= key_bytype(kf
, "catacomb-rand");
330 key_data
*kd
= &k
->k
, kkd
;
333 switch (kd
->e
& KF_ENCMASK
) {
339 if (key_punlock(d
.buf
, kd
, &kkd
))
340 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
347 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
351 /* --- Key the generator --- */
353 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
359 /* --- Key generation algorithms --- */
361 static void alg_binary(keyopts
*k
)
370 die(EXIT_FAILURE
, "no shared parameters for binary keys");
372 sz
= (k
->bits
+ 7) >> 3;
374 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
375 rand_get(RAND_GLOBAL
, p
, sz
);
377 key_binary(&k
->k
->k
, p
, sz
);
378 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
381 dolock(k
, &k
->k
->k
, 0);
384 static void alg_des(keyopts
*k
)
393 die(EXIT_FAILURE
, "no shared parameters for DES keys");
394 if (k
->bits
% 56 || k
->bits
> 168)
395 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
399 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
400 for (i
= 0; i
< sz
; i
++) {
401 octet x
= p
[i
] | 0x01;
405 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
407 key_binary(&k
->k
->k
, p
, sz
);
408 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
411 dolock(k
, &k
->k
->k
, 0);
414 static void alg_rsa(keyopts
*k
)
419 /* --- Sanity checking --- */
422 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
426 /* --- Generate the RSA parameters --- */
428 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
429 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
430 die(EXIT_FAILURE
, "RSA key generation failed");
432 /* --- Run a test encryption --- */
435 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
437 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
442 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
443 c
= rsa_qprivop(&rp
, c
, c
, g
);
446 die(EXIT_FAILURE
, "test encryption failed");
452 /* --- Allrighty then --- */
456 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
457 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
459 kd
= key_structcreate(kd
, "private");
461 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
462 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
463 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
464 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
465 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
466 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
467 dolock(k
, kd
, "private");
472 static void alg_dsaparam(keyopts
*k
)
474 static const char *pl
[] = { "q", "p", "g", 0 };
475 if (!copyparam(k
, pl
)) {
481 key_data
*kd
= &k
->k
->k
;
484 /* --- Choose appropriate bit lengths if necessary --- */
491 /* --- Allocate a seed block --- */
493 sz
= (k
->qbits
+ 7) >> 3;
495 rand_get(RAND_GLOBAL
, p
, sz
);
497 /* --- Allocate the parameters --- */
499 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
500 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
501 die(EXIT_FAILURE
, "DSA parameter generation failed");
503 /* --- Store the parameters --- */
506 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
507 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
508 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
513 /* --- Store the seed for future verification --- */
518 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
519 base64_encode(&c
, 0, 0, &d
);
520 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
522 dstr_putf(&d
, "%u", ds
.count
);
523 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
530 static void alg_dsa(keyopts
*k
)
535 key_data
*kd
= &k
->k
->k
;
537 /* --- Get the shared parameters --- */
544 /* --- Choose a private key --- */
546 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
547 mpmont_create(&mm
, p
);
548 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
550 /* --- Store everything away --- */
552 mpkey(kd
, "y", y
, KCAT_PUB
);
554 kd
= key_structcreate(kd
, "private");
556 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
557 dolock(k
, kd
, "private");
559 mp_drop(x
); mp_drop(y
);
562 static void alg_dhparam(keyopts
*k
)
564 static const char *pl
[] = { "p", "q", "g", 0 };
565 if (!copyparam(k
, pl
)) {
567 key_data
*kd
= &k
->k
->k
;
573 /* --- Choose a large safe prime number --- */
575 if (k
->f
& f_limlee
) {
580 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
581 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
582 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
583 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
587 for (i
= 0; i
< nf
; i
++) {
590 mp_writedstr(f
[i
], &d
, 10);
593 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
597 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
598 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
601 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
604 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
605 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
606 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
613 static void alg_dh(keyopts
*k
)
618 key_data
*kd
= &k
->k
->k
;
620 /* --- Get the shared parameters --- */
627 /* --- Choose a suitable private key --- *
629 * Since %$g$% has order %$q$%, choose %$x < q$%.
632 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
634 /* --- Compute the public key %$y = g^x \bmod p$% --- */
636 mpmont_create(&mm
, p
);
637 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
640 /* --- Store everything away --- */
642 mpkey(kd
, "y", y
, KCAT_PUB
);
644 kd
= key_structcreate(kd
, "private");
646 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
647 dolock(k
, kd
, "private");
649 mp_drop(x
); mp_drop(y
);
652 static void alg_bbs(keyopts
*k
)
657 /* --- Sanity checking --- */
660 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
664 /* --- Generate the BBS parameters --- */
666 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
667 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
668 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
670 /* --- Allrighty then --- */
674 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
676 kd
= key_structcreate(kd
, "private");
678 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
679 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
680 dolock(k
, kd
, "private");
685 /* --- The algorithm tables --- */
687 typedef struct keyalg
{
689 void (*proc
)(keyopts
*o
);
693 static keyalg algtab
[] = {
694 { "binary", alg_binary
, "Plain binary data" },
695 { "des", alg_des
, "Binary with DES-style parity" },
696 { "rsa", alg_rsa
, "RSA public-key encryption" },
697 { "dsa", alg_dsa
, "DSA digital signatures" },
698 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
699 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
700 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
701 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
705 /* --- @cmd_add@ --- */
707 static int cmd_add(int argc
, char *argv
[])
710 time_t exp
= KEXP_EXPIRE
;
711 const char *tag
= 0, *ptag
= 0;
713 keyalg
*alg
= algtab
;
714 const char *rtag
= 0;
715 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
717 /* --- Parse options for the subcommand --- */
720 static struct option opt
[] = {
721 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
722 { "bits", OPTF_ARGREQ
, 0, 'b' },
723 { "qbits", OPTF_ARGREQ
, 0, 'B' },
724 { "parameters", OPTF_ARGREQ
, 0, 'p' },
725 { "expire", OPTF_ARGREQ
, 0, 'e' },
726 { "comment", OPTF_ARGREQ
, 0, 'c' },
727 { "tag", OPTF_ARGREQ
, 0, 't' },
728 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
729 { "lock", 0, 0, 'l' },
730 { "quiet", 0, 0, 'q' },
731 { "lim-lee", 0, 0, 'L' },
732 { "subgroup", 0, 0, 'S' },
735 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:lqrLS", opt
, 0, 0, 0);
739 /* --- Handle the various options --- */
743 /* --- Read an algorithm name --- */
747 size_t sz
= strlen(optarg
);
749 if (strcmp(optarg
, "list") == 0) {
750 for (a
= algtab
; a
->name
; a
++)
751 printf("%-10s %s\n", a
->name
, a
->help
);
756 for (a
= algtab
; a
->name
; a
++) {
757 if (strncmp(optarg
, a
->name
, sz
) == 0) {
758 if (a
->name
[sz
] == 0) {
762 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
768 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
771 /* --- Bits must be nonzero and a multiple of 8 --- */
775 k
.bits
= strtoul(optarg
, &p
, 0);
776 if (k
.bits
== 0 || *p
!= 0)
777 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
782 k
.qbits
= strtoul(optarg
, &p
, 0);
783 if (k
.qbits
== 0 || *p
!= 0)
784 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
787 /* --- Parameter selection --- */
793 /* --- Expiry dates get passed to @get_date@ for parsing --- */
796 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
799 exp
= get_date(optarg
, 0);
801 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
805 /* --- Store comments without interpretation --- */
808 if (key_chkcomment(optarg
))
809 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
813 /* --- Store tags --- */
816 if (key_chkident(optarg
))
817 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
824 /* --- Other flags --- */
842 /* --- Other things are bogus --- */
850 /* --- Various sorts of bogosity --- */
852 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
854 "Usage: add [options] type [attr...]");
856 if (key_chkident(argv
[optind
]))
857 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
859 /* --- Set up various bits of the state --- */
861 if (exp
== KEXP_EXPIRE
)
862 exp
= time(0) + 14 * 24 * 60 * 60;
864 /* --- Open the file and create the basic key block --- *
866 * Keep on generating keyids until one of them doesn't collide.
869 doopen(&f
, KOPEN_WRITE
);
872 /* --- Key the generator --- */
877 uint32 id
= rand_global
.ops
->word(&rand_global
);
879 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
882 if (err
!= KERR_DUPID
)
883 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
886 /* --- Set various simple attributes --- */
892 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
893 key_settag(&f
, kk
, 0);
895 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
896 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
900 int err
= key_setcomment(&f
, k
.k
, c
);
902 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
905 setattr(&f
, k
.k
, argv
+ optind
+ 1);
907 key_fulltag(k
.k
, &k
.tag
);
909 /* --- Find the parameter key --- */
912 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
913 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
914 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
915 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
918 /* --- Now generate the actual key data --- */
928 /*----- Key listing -------------------------------------------------------*/
930 /* --- Listing options --- */
932 typedef struct listopts
{
933 const char *tfmt
; /* Date format (@strftime@-style) */
934 int v
; /* Verbosity level */
935 unsigned f
; /* Various flags */
936 time_t t
; /* Time now (for key expiry) */
937 key_filter kf
; /* Filter for matching keys */
940 /* --- Listing flags --- */
942 #define f_newline 2u /* Write newline before next entry */
943 #define f_attr 4u /* Written at least one attribute */
944 #define f_utc 8u /* Emit UTC time, not local time */
946 /* --- @showkeydata@ --- *
948 * Arguments: @key_data *k@ = pointer to key to write
949 * @int ind@ = indentation level
950 * @listopts *o@ = listing options
951 * @dstr *d@ = tag string for this subkey
955 * Use: Emits a piece of key data in a human-readable format.
958 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
960 #define INDENT(i) do { \
962 for (_i = 0; _i < (i); _i++) { \
967 switch (k
->e
& KF_ENCMASK
) {
969 /* --- Binary key data --- *
971 * Emit as a simple hex dump.
975 const octet
*p
= k
->u
.k
.k
;
976 const octet
*l
= p
+ k
->u
.k
.sz
;
984 } else if (sz
% 8 == 0)
988 printf("%02x", *p
++);
993 fputs("}\n", stdout
);
996 /* --- Encrypted data --- *
998 * If the user is sufficiently keen, ask for a passphrase and decrypt the
999 * key. Otherwise just say that it's encrypted and move on.
1004 fputs(" encrypted\n", stdout
);
1007 if (key_punlock(d
->buf
, k
, &kd
))
1008 printf(" <failed to unlock %s>\n", d
->buf
);
1010 fputs(" encrypted", stdout
);
1011 showkeydata(&kd
, ind
, o
, d
);
1017 /* --- Integer keys --- *
1019 * Emit as a large integer in decimal. This makes using the key in
1020 * `calc' or whatever easier.
1025 mp_writefile(k
->u
.m
, stdout
, 10);
1029 /* --- Structured keys --- *
1031 * Just iterate over the subkeys.
1039 fputs(" {\n", stdout
);
1040 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1041 if (!key_match(&ks
->k
, &o
->kf
))
1044 printf("%s =", SYM_NAME(ks
));
1046 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1047 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1050 fputs("}\n", stdout
);
1057 /* --- @showkey@ --- *
1059 * Arguments: @key *k@ = pointer to key to show
1060 * @listopts *o@ = pointer to listing options
1064 * Use: Emits a listing of a particular key.
1067 static void showkey(key
*k
, listopts
*o
)
1069 char ebuf
[24], dbuf
[24];
1072 /* --- Skip the key if the filter doesn't match --- */
1074 if (!key_match(&k
->k
, &o
->kf
))
1077 /* --- Sort out the expiry and deletion times --- */
1079 if (KEY_EXPIRED(o
->t
, k
->exp
))
1080 strcpy(ebuf
, "expired");
1081 else if (k
->exp
== KEXP_FOREVER
)
1082 strcpy(ebuf
, "forever");
1084 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1085 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1088 if (KEY_EXPIRED(o
->t
, k
->del
))
1089 strcpy(dbuf
, "deleted");
1090 else if (k
->del
== KEXP_FOREVER
)
1091 strcpy(dbuf
, "forever");
1093 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1094 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1097 /* --- If in compact format, just display and quit --- */
1100 if (!(o
->f
& f_newline
)) {
1101 printf("%8s %-20s %-20s %-10s %-10s\n",
1102 "Id", "Tag", "Type", "Expire", "Delete");
1104 printf("%08lx %-20s %-20s %-10s %-10s\n",
1105 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1106 k
->type
, ebuf
, dbuf
);
1111 /* --- Display the standard header --- */
1113 if (o
->f
& f_newline
)
1114 fputc('\n', stdout
);
1115 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1116 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1117 printf("type: %s\n", k
->type
);
1118 printf("expiry: %s\n", ebuf
);
1119 printf("delete: %s\n", dbuf
);
1120 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1122 /* --- Display the attributes --- */
1126 const char *av
, *an
;
1129 printf("attributes:");
1130 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1131 printf("\n %s = %s", an
, av
);
1135 fputc('\n', stdout
);
1140 /* --- If dumping requested, dump the raw key data --- */
1144 fputs("key:", stdout
);
1146 showkeydata(&k
->k
, 0, o
, &d
);
1153 /* --- @cmd_list@ --- */
1155 static int cmd_list(int argc
, char *argv
[])
1159 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1161 /* --- Parse subcommand options --- */
1164 static struct option opt
[] = {
1165 { "quiet", 0, 0, 'q' },
1166 { "verbose", 0, 0, 'v' },
1167 { "utc", 0, 0, 'u' },
1168 { "filter", OPTF_ARGREQ
, 0, 'f' },
1171 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1188 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1190 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1199 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1201 /* --- Open the key file --- */
1203 doopen(&f
, KOPEN_READ
);
1206 /* --- Set up the time format --- */
1209 o
.tfmt
= "%Y-%m-%d";
1210 else if (o
.f
& f_utc
)
1211 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1213 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1215 /* --- If specific keys were requested use them, otherwise do all --- *
1217 * Some day, this might turn into a wildcard match.
1220 if (optind
< argc
) {
1222 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1225 moan("key `%s' not found", argv
[optind
]);
1229 } while (optind
< argc
);
1232 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1240 return (EXIT_FAILURE
);
1245 /*----- Command implementation --------------------------------------------*/
1247 /* --- @cmd_expire@ --- */
1249 static int cmd_expire(int argc
, char *argv
[])
1257 die(EXIT_FAILURE
, "Usage: expire tag...");
1258 doopen(&f
, KOPEN_WRITE
);
1259 for (i
= 1; i
< argc
; i
++) {
1260 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1263 moan("key `%s' not found", argv
[i
]);
1271 /* --- @cmd_delete@ --- */
1273 static int cmd_delete(int argc
, char *argv
[])
1281 die(EXIT_FAILURE
, "Usage: delete tag...");
1282 doopen(&f
, KOPEN_WRITE
);
1283 for (i
= 1; i
< argc
; i
++) {
1284 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1287 moan("key `%s' not found", argv
[i
]);
1295 /* --- @cmd_setattr@ --- */
1297 static int cmd_setattr(int argc
, char *argv
[])
1303 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1304 doopen(&f
, KOPEN_WRITE
);
1305 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1306 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1307 setattr(&f
, k
, argv
+ 2);
1312 /* --- @cmd_finger@ --- */
1314 static void fingerprint(key
*k
, const key_filter
*kf
)
1317 octet hash
[RMD160_HASHSZ
];
1321 if (!key_encode(&k
->k
, &d
, kf
))
1324 rmd160_hash(&r
, d
.buf
, d
.len
);
1325 rmd160_done(&r
, hash
);
1329 for (i
= 0; i
< sizeof(hash
); i
++) {
1330 if (i
&& i
% 4 == 0)
1332 printf("%02x", hash
[i
]);
1334 printf(" %s\n", d
.buf
);
1338 static int cmd_finger(int argc
, char *argv
[])
1342 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1345 static struct option opt
[] = {
1346 { "filter", OPTF_ARGREQ
, 0, 'f' },
1349 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1355 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1357 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1365 argv
+= optind
; argc
-= optind
;
1367 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1369 doopen(&f
, KOPEN_READ
);
1373 for (i
= 0; i
< argc
; i
++) {
1374 key
*k
= key_bytag(&f
, argv
[i
]);
1376 fingerprint(k
, &kf
);
1379 moan("key `%s' not found", argv
[i
]);
1385 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1386 fingerprint(k
, &kf
);
1391 /* --- @cmd_comment@ --- */
1393 static int cmd_comment(int argc
, char *argv
[])
1399 if (argc
< 2 || argc
> 3)
1400 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1401 doopen(&f
, KOPEN_WRITE
);
1402 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1403 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1404 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1405 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1410 /* --- @cmd_tag@ --- */
1412 static int cmd_tag(int argc
, char *argv
[])
1421 static struct option opt
[] = {
1422 { "retag", 0, 0, 'r' },
1425 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1438 argv
+= optind
; argc
-= optind
;
1439 if (argc
< 1 || argc
> 2 || rc
)
1440 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1441 doopen(&f
, KOPEN_WRITE
);
1442 if (flags
& f_retag
) {
1443 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1444 key_settag(&f
, k
, 0);
1446 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1447 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1448 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1449 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1454 /* --- @cmd_lock@ --- */
1456 static int cmd_lock(int argc
, char *argv
[])
1464 die(EXIT_FAILURE
, "Usage: lock qtag");
1465 doopen(&f
, KOPEN_WRITE
);
1466 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1467 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1468 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1469 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1470 if (key_plock(d
.buf
, kd
, kd
))
1471 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1477 /* --- @cmd_unlock@ --- */
1479 static int cmd_unlock(int argc
, char *argv
[])
1487 die(EXIT_FAILURE
, "Usage: unlock qtag");
1488 doopen(&f
, KOPEN_WRITE
);
1489 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1490 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1491 if (kd
->e
!= KENC_ENCRYPT
)
1492 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1493 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1494 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1500 /* --- @cmd_extract@ --- */
1502 static int cmd_extract(int argc
, char *argv
[])
1508 key_filter kf
= { 0, 0 };
1512 static struct option opt
[] = {
1513 { "filter", OPTF_ARGREQ
, 0, 'f' },
1516 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1522 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1524 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1532 argv
+= optind
; argc
-= optind
;
1534 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1535 if (strcmp(*argv
, "-") == 0)
1537 else if (!(fp
= fopen(*argv
, "w"))) {
1538 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1539 *argv
, strerror(errno
));
1542 doopen(&f
, KOPEN_READ
);
1546 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1547 key_extract(&f
, k
, fp
, &kf
);
1549 for (i
= 1; i
< argc
; i
++) {
1550 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1551 key_extract(&f
, k
, fp
, &kf
);
1553 moan("key `%s' not found", argv
[i
]);
1559 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1564 /* --- @cmd_tidy@ --- */
1566 static int cmd_tidy(int argc
, char *argv
[])
1570 die(EXIT_FAILURE
, "usage: tidy");
1571 doopen(&f
, KOPEN_WRITE
);
1572 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1577 /* --- @cmd_merge@ --- */
1579 static int cmd_merge(int argc
, char *argv
[])
1585 die(EXIT_FAILURE
, "Usage: merge file");
1586 if (strcmp(argv
[1], "-") == 0)
1588 else if (!(fp
= fopen(argv
[1], "r"))) {
1589 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1590 argv
[1], strerror(errno
));
1593 doopen(&f
, KOPEN_WRITE
);
1594 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1599 /*----- Main command table ------------------------------------------------*/
1603 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1608 "add [options] type [attr...]\n\
1609 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1610 [-e expire] [-t tag] [-c comment]", "\
1613 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1614 -b, --bits=N Generate an N-bit key.\n\
1615 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1616 -p, --parameters=TAG Get group parameters from TAG.\n\
1617 -e, --expire=TIME Make the key expire after TIME.\n\
1618 -c, --comment=STRING Attach the command STRING to the key.\n\
1619 -t, --tag=TAG Tag the key with the name TAG.\n\
1620 -r, --retag Untag any key currently with that tag.\n\
1621 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1622 -l, --lock Lock the generated key with a passphrase.\n\
1623 -q, --quiet Don't give progress indicators while working.\n\
1624 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1625 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1627 { "expire", cmd_expire
, "expire tag..." },
1628 { "delete", cmd_delete
, "delete tag..." },
1629 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1632 -r, --retag Untag any key currently called new-tag.\n\
1634 { "setattr", cmd_setattr
, "setattr tag attr..." },
1635 { "comment", cmd_comment
, "comment tag [comment]" },
1636 { "lock", cmd_lock
, "lock qtag" },
1637 { "unlock", cmd_unlock
, "unlock qtag" },
1638 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1641 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1642 -q, --quiet Show less information.\n\
1643 -v, --verbose Show more information.\n\
1645 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1648 -f, --filter=FILT Only hash key components matching FILT.\n\
1650 { "tidy", cmd_tidy
, "tidy" },
1651 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1654 -f, --filter=FILT Only extract key components matching FILT.\n\
1656 { "merge", cmd_merge
, "merge file" },
1660 typedef struct cmd cmd
;
1662 /*----- Main code ---------------------------------------------------------*/
1664 /* --- @findcmd@ --- *
1666 * Arguments: @const char *name@ = a command name
1668 * Returns: Pointer to the command structure.
1670 * Use: Looks up a command by name. If the command isn't found, an
1671 * error is reported and the program is terminated.
1674 static cmd
*findcmd(const char *name
)
1676 cmd
*c
, *chosen
= 0;
1677 size_t sz
= strlen(name
);
1679 for (c
= cmds
; c
->name
; c
++) {
1680 if (strncmp(name
, c
->name
, sz
) == 0) {
1681 if (c
->name
[sz
] == 0) {
1685 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1691 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1695 /* --- Helpful GNUy functions --- */
1697 void usage(FILE *fp
)
1699 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1702 void version(FILE *fp
)
1704 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1707 void help(FILE *fp
, char **argv
)
1715 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1725 Performs various simple key management operations. Command line options\n\
1728 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1729 -v, --version Display version number.\n\
1730 -u, --usage Display short usage summary.\n\
1732 -k, --keyring=FILE Read and write keys in FILE.\n\
1733 -i, --id=TAG Use key TAG for random number generator.\n\
1734 -t, --type=TYPE Use key TYPE for random number generator.\n\
1736 The following commands are understood:\n\n",
1738 for (c
= cmds
; c
->name
; c
++)
1739 fprintf(fp
, "%s\n", c
->usage
);
1745 * Arguments: @int argc@ = number of command line arguments
1746 * @char *argv[]@ = array of command line arguments
1748 * Returns: Nonzero on failure.
1750 * Use: Main program. Performs simple key management functions.
1753 int main(int argc
, char *argv
[])
1759 /* --- Initialization --- */
1764 /* --- Parse command line options --- */
1767 static struct option opt
[] = {
1769 /* --- Standard GNUy help options --- */
1771 { "help", 0, 0, 'h' },
1772 { "version", 0, 0, 'v' },
1773 { "usage", 0, 0, 'u' },
1775 /* --- Real live useful options --- */
1777 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1779 /* --- Magic terminator --- */
1783 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1789 /* --- GNU help options --- */
1792 help(stdout
, argv
+ optind
);
1801 /* --- Real useful options --- */
1807 /* --- Bogosity --- */
1815 /* --- Complain about excessive bogons --- */
1817 if (f
& f_bogus
|| optind
== argc
) {
1822 /* --- Initialize the Catacomb random number generator --- */
1824 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1825 rand_seed(RAND_GLOBAL
, 160);
1827 /* --- Dispatch to appropriate command handler --- */
1832 return (findcmd(argv
[0])->cmd(argc
, argv
));
1835 /*----- That's all, folks -------------------------------------------------*/