3 * $Id: keyutil.c,v 1.13 2001/02/21 20:04: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.13 2001/02/21 20:04:27 mdw
34 * Provide help on individual commands (some need it desparately). Allow
35 * atomic retagging of keys.
37 * Revision 1.12 2001/02/03 11:58:22 mdw
38 * Store the correct seed information and count for DSA keys now that it's
41 * Revision 1.11 2000/12/06 20:33:27 mdw
42 * Make flags be macros rather than enumerations, to ensure that they're
45 * Revision 1.10 2000/10/08 12:02:21 mdw
46 * Use @MP_EQ@ instead of @MP_CMP@.
48 * Revision 1.9 2000/08/15 21:40:49 mdw
49 * Minor formatting change in listing attributes.
51 * Revision 1.8 2000/07/29 09:59:13 mdw
52 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
54 * Revision 1.7 2000/07/01 11:18:51 mdw
55 * Use new interfaces for key manipulation.
57 * Revision 1.6 2000/06/17 11:28:22 mdw
58 * Use secure memory interface from MP library. `rand_getgood' is
61 * Revision 1.5 2000/02/12 18:21:03 mdw
62 * Overhaul of key management (again).
64 * Revision 1.4 1999/12/22 15:48:10 mdw
65 * Track new key-management changes. Support new key generation
68 * Revision 1.3 1999/11/02 15:23:24 mdw
69 * Fix newlines in keyring list.
71 * Revision 1.2 1999/10/15 21:05:28 mdw
72 * In `key list', show timezone for local times, and support `-u' option
75 * Revision 1.1 1999/09/03 08:41:12 mdw
80 /*----- Header files ------------------------------------------------------*/
90 #include <mLib/base64.h>
91 #include <mLib/mdwopt.h>
92 #include <mLib/quis.h>
93 #include <mLib/report.h>
112 /*----- Handy global state ------------------------------------------------*/
114 static const char *keyfile
= "keyring";
116 /*----- Useful shared functions -------------------------------------------*/
118 /* --- @doopen@ --- *
120 * Arguments: @key_file *f@ = pointer to key file block
121 * @unsigned how@ = method to open file with
125 * Use: Opens a key file and handles errors by panicking
129 static void doopen(key_file
*f
, unsigned how
)
131 if (key_open(f
, keyfile
, how
, key_moan
, 0))
132 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
135 /* --- @doclose@ --- *
137 * Arguments: @key_file *f@ = pointer to key file block
141 * Use: Closes a key file and handles errors by panicking
145 static void doclose(key_file
*f
)
147 switch (key_close(f
)) {
149 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
150 keyfile
, strerror(errno
));
152 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
153 keyfile
, strerror(errno
));
157 /* --- @setattr@ --- *
159 * Arguments: @key_file *f@ = pointer to key file block
160 * @key *k@ = pointer to key block
161 * @char *v[]@ = array of assignments (overwritten!)
165 * Use: Applies the attribute assignments to the key.
168 static void setattr(key_file
*f
, key
*k
, char *v
[])
173 size_t eq
= strcspn(p
, "=");
175 moan("invalid assignment: `%s'", p
);
178 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
179 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
184 /*----- Key generation ----------------------------------------------------*/
186 /* --- Key generation parameters --- */
188 typedef struct keyopts
{
189 key_file
*kf
; /* Pointer to key file */
190 key
*k
; /* Pointer to the actual key */
191 dstr tag
; /* Full tag name for the key */
192 unsigned f
; /* Flags for the new key */
193 unsigned bits
, qbits
; /* Bit length for the new key */
194 key
*p
; /* Parameters key-data */
197 #define f_bogus 1u /* Error in parsing */
198 #define f_lock 2u /* Passphrase-lock private key */
199 #define f_quiet 4u /* Don't show a progress indicator */
200 #define f_limlee 8u /* Generate Lim-Lee primes */
201 #define f_subgroup 16u /* Generate a subgroup */
202 #define f_retag 32u /* Remove any existing tag */
204 /* --- @dolock@ --- *
206 * Arguments: @keyopts *k@ = key generation options
207 * @key_data *kd@ = pointer to key data to lock
208 * @const char *t@ = tag suffix or null
212 * Use: Does passphrase locking on new keys.
215 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
217 if (!(k
->f
& f_lock
))
220 dstr_putf(&k
->tag
, ".%s", t
);
221 if (key_plock(k
->tag
.buf
, kd
, kd
))
222 die(EXIT_FAILURE
, "couldn't lock key");
227 * Arguments: @key_data *kd@ = pointer to parent key block
228 * @const char *tag@ = pointer to tag string
229 * @mp *m@ = integer to store
230 * @unsigned f@ = flags to set
234 * Use: Sets a multiprecision integer subkey.
237 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
239 key_data
*kkd
= key_structcreate(kd
, tag
);
244 /* --- @copyparam@ --- *
246 * Arguments: @keyopts *k@ = pointer to key options
247 * @const char **pp@ = checklist of parameters
249 * Returns: Nonzero if parameters copied; zero if you have to generate
252 * Use: Copies parameters from a source key to the current one.
255 static int copyparam(keyopts
*k
, const char **pp
)
259 /* --- Quick check if no parameters supplied --- */
264 /* --- Run through the checklist --- */
267 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
269 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
270 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
271 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
275 /* --- Copy over the parameters --- */
279 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
280 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
286 * Arguments: @key_data *k@ = pointer to key data block
287 * @const char *tag@ = tag string to use
289 * Returns: Pointer to multiprecision integer key item.
291 * Use: Fetches an MP key component.
294 static mp
*getmp(key_data
*k
, const char *tag
)
296 k
= key_structfind(k
, tag
);
298 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
299 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
300 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
304 /* --- @keyrand@ --- *
306 * Arguments: @key_file *kf@ = pointer to key file
307 * @const char *id@ = pointer to key id (or null)
311 * Use: Keys the random number generator.
314 static void keyrand(key_file
*kf
, const char *id
)
318 /* --- Find the key --- */
321 if ((k
= key_bytag(kf
, id
)) == 0)
322 die(EXIT_FAILURE
, "key `%s' not found", id
);
324 k
= key_bytype(kf
, "catacomb-rand");
327 key_data
*kd
= &k
->k
, kkd
;
330 switch (kd
->e
& KF_ENCMASK
) {
336 if (key_punlock(d
.buf
, kd
, &kkd
))
337 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
344 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
348 /* --- Key the generator --- */
350 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
356 /* --- Key generation algorithms --- */
358 static void alg_binary(keyopts
*k
)
367 die(EXIT_FAILURE
, "no shared parameters for binary keys");
369 sz
= (k
->bits
+ 7) >> 3;
371 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
372 rand_get(RAND_GLOBAL
, p
, sz
);
374 key_binary(&k
->k
->k
, p
, sz
);
375 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
378 dolock(k
, &k
->k
->k
, 0);
381 static void alg_des(keyopts
*k
)
390 die(EXIT_FAILURE
, "no shared parameters for DES keys");
391 if (k
->bits
% 56 || k
->bits
> 168)
392 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
396 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
397 for (i
= 0; i
< sz
; i
++) {
398 octet x
= p
[i
] | 0x01;
402 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
404 key_binary(&k
->k
->k
, p
, sz
);
405 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
408 dolock(k
, &k
->k
->k
, 0);
411 static void alg_rsa(keyopts
*k
)
416 /* --- Sanity checking --- */
419 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
423 /* --- Generate the RSA parameters --- */
425 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
426 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
427 die(EXIT_FAILURE
, "RSA key generation failed");
429 /* --- Run a test encryption --- */
432 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
434 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
439 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
440 c
= rsa_qprivop(&rp
, c
, c
, g
);
443 die(EXIT_FAILURE
, "test encryption failed");
449 /* --- Allrighty then --- */
453 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
454 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
456 kd
= key_structcreate(kd
, "private");
458 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
459 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
460 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
461 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
462 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
463 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
464 dolock(k
, kd
, "private");
469 static void alg_dsaparam(keyopts
*k
)
471 static const char *pl
[] = { "q", "p", "g", 0 };
472 if (!copyparam(k
, pl
)) {
478 key_data
*kd
= &k
->k
->k
;
481 /* --- Choose appropriate bit lengths if necessary --- */
488 /* --- Allocate a seed block --- */
490 sz
= (k
->qbits
+ 7) >> 3;
492 rand_get(RAND_GLOBAL
, p
, sz
);
494 /* --- Allocate the parameters --- */
496 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
497 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
498 die(EXIT_FAILURE
, "DSA parameter generation failed");
500 /* --- Store the parameters --- */
503 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
504 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
505 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
510 /* --- Store the seed for future verification --- */
515 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
516 base64_encode(&c
, 0, 0, &d
);
517 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
519 dstr_putf(&d
, "%u", ds
.count
);
520 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
527 static void alg_dsa(keyopts
*k
)
532 key_data
*kd
= &k
->k
->k
;
534 /* --- Get the shared parameters --- */
541 /* --- Choose a private key --- */
543 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
544 mpmont_create(&mm
, p
);
545 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
547 /* --- Store everything away --- */
549 mpkey(kd
, "y", y
, KCAT_PUB
);
551 kd
= key_structcreate(kd
, "private");
553 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
554 dolock(k
, kd
, "private");
556 mp_drop(x
); mp_drop(y
);
559 static void alg_dhparam(keyopts
*k
)
561 static const char *pl
[] = { "p", "q", "g", 0 };
562 if (!copyparam(k
, pl
)) {
564 key_data
*kd
= &k
->k
->k
;
570 /* --- Choose a large safe prime number --- */
572 if (k
->f
& f_limlee
) {
577 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
578 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
579 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
580 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
584 for (i
= 0; i
< nf
; i
++) {
587 mp_writedstr(f
[i
], &d
, 10);
590 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
594 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
595 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
598 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
601 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
602 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
603 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
610 static void alg_dh(keyopts
*k
)
615 key_data
*kd
= &k
->k
->k
;
617 /* --- Get the shared parameters --- */
624 /* --- Choose a suitable private key --- *
626 * Since %$g$% has order %$q$%, choose %$x < q$%.
629 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
631 /* --- Compute the public key %$y = g^x \bmod p$% --- */
633 mpmont_create(&mm
, p
);
634 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
637 /* --- Store everything away --- */
639 mpkey(kd
, "y", y
, KCAT_PUB
);
641 kd
= key_structcreate(kd
, "private");
643 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
644 dolock(k
, kd
, "private");
646 mp_drop(x
); mp_drop(y
);
649 static void alg_bbs(keyopts
*k
)
654 /* --- Sanity checking --- */
657 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
661 /* --- Generate the BBS parameters --- */
663 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
664 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
665 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
667 /* --- Allrighty then --- */
671 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
673 kd
= key_structcreate(kd
, "private");
675 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
676 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
677 dolock(k
, kd
, "private");
682 /* --- The algorithm tables --- */
684 typedef struct keyalg
{
686 void (*proc
)(keyopts
*o
);
690 static keyalg algtab
[] = {
691 { "binary", alg_binary
, "Plain binary data" },
692 { "des", alg_des
, "Binary with DES-style parity" },
693 { "rsa", alg_rsa
, "RSA public-key encryption" },
694 { "dsa", alg_dsa
, "DSA digital signatures" },
695 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
696 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
697 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
698 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
702 /* --- @cmd_add@ --- */
704 static int cmd_add(int argc
, char *argv
[])
707 time_t exp
= KEXP_EXPIRE
;
708 const char *tag
= 0, *ptag
= 0;
710 keyalg
*alg
= algtab
;
711 const char *rtag
= 0;
712 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
714 /* --- Parse options for the subcommand --- */
717 static struct option opt
[] = {
718 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
719 { "bits", OPTF_ARGREQ
, 0, 'b' },
720 { "qbits", OPTF_ARGREQ
, 0, 'B' },
721 { "parameters", OPTF_ARGREQ
, 0, 'p' },
722 { "expire", OPTF_ARGREQ
, 0, 'e' },
723 { "comment", OPTF_ARGREQ
, 0, 'c' },
724 { "tag", OPTF_ARGREQ
, 0, 't' },
725 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
726 { "lock", 0, 0, 'l' },
727 { "quiet", 0, 0, 'q' },
728 { "lim-lee", 0, 0, 'L' },
729 { "subgroup", 0, 0, 'S' },
732 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:lqrLS", opt
, 0, 0, 0);
736 /* --- Handle the various options --- */
740 /* --- Read an algorithm name --- */
744 size_t sz
= strlen(optarg
);
746 if (strcmp(optarg
, "list") == 0) {
747 for (a
= algtab
; a
->name
; a
++)
748 printf("%-10s %s\n", a
->name
, a
->help
);
753 for (a
= algtab
; a
->name
; a
++) {
754 if (strncmp(optarg
, a
->name
, sz
) == 0) {
755 if (a
->name
[sz
] == 0) {
759 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
765 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
768 /* --- Bits must be nonzero and a multiple of 8 --- */
772 k
.bits
= strtoul(optarg
, &p
, 0);
773 if (k
.bits
== 0 || *p
!= 0)
774 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
779 k
.qbits
= strtoul(optarg
, &p
, 0);
780 if (k
.qbits
== 0 || *p
!= 0)
781 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
784 /* --- Parameter selection --- */
790 /* --- Expiry dates get passed to @get_date@ for parsing --- */
793 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
796 exp
= get_date(optarg
, 0);
798 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
802 /* --- Store comments without interpretation --- */
805 if (key_chkcomment(optarg
))
806 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
810 /* --- Store tags --- */
813 if (key_chkident(optarg
))
814 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
821 /* --- Other flags --- */
839 /* --- Other things are bogus --- */
847 /* --- Various sorts of bogosity --- */
849 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
851 "Usage: add [options] type [attr...]");
853 if (key_chkident(argv
[optind
]))
854 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
856 /* --- Set up various bits of the state --- */
858 if (exp
== KEXP_EXPIRE
)
859 exp
= time(0) + 14 * 24 * 60 * 60;
861 /* --- Open the file and create the basic key block --- *
863 * Keep on generating keyids until one of them doesn't collide.
866 doopen(&f
, KOPEN_WRITE
);
869 /* --- Key the generator --- */
874 uint32 id
= rand_global
.ops
->word(&rand_global
);
876 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
879 if (err
!= KERR_DUPID
)
880 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
883 /* --- Set various simple attributes --- */
889 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
890 key_settag(&f
, kk
, 0);
892 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
893 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
897 int err
= key_setcomment(&f
, k
.k
, c
);
899 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
902 setattr(&f
, k
.k
, argv
+ optind
+ 1);
904 key_fulltag(k
.k
, &k
.tag
);
906 /* --- Find the parameter key --- */
909 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
910 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
911 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
912 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
915 /* --- Now generate the actual key data --- */
925 /*----- Key listing -------------------------------------------------------*/
927 /* --- Listing options --- */
929 typedef struct listopts
{
930 const char *tfmt
; /* Date format (@strftime@-style) */
931 int v
; /* Verbosity level */
932 unsigned f
; /* Various flags */
933 time_t t
; /* Time now (for key expiry) */
934 key_filter kf
; /* Filter for matching keys */
937 /* --- Listing flags --- */
939 #define f_newline 2u /* Write newline before next entry */
940 #define f_attr 4u /* Written at least one attribute */
941 #define f_utc 8u /* Emit UTC time, not local time */
943 /* --- @showkeydata@ --- *
945 * Arguments: @key_data *k@ = pointer to key to write
946 * @int ind@ = indentation level
947 * @listopts *o@ = listing options
948 * @dstr *d@ = tag string for this subkey
952 * Use: Emits a piece of key data in a human-readable format.
955 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
957 #define INDENT(i) do { \
959 for (_i = 0; _i < (i); _i++) { \
964 switch (k
->e
& KF_ENCMASK
) {
966 /* --- Binary key data --- *
968 * Emit as a simple hex dump.
972 const octet
*p
= k
->u
.k
.k
;
973 const octet
*l
= p
+ k
->u
.k
.sz
;
981 } else if (sz
% 8 == 0)
985 printf("%02x", *p
++);
990 fputs("}\n", stdout
);
993 /* --- Encrypted data --- *
995 * If the user is sufficiently keen, ask for a passphrase and decrypt the
996 * key. Otherwise just say that it's encrypted and move on.
1001 fputs(" encrypted\n", stdout
);
1004 if (key_punlock(d
->buf
, k
, &kd
))
1005 printf(" <failed to unlock %s>\n", d
->buf
);
1007 fputs(" encrypted", stdout
);
1008 showkeydata(&kd
, ind
, o
, d
);
1014 /* --- Integer keys --- *
1016 * Emit as a large integer in decimal. This makes using the key in
1017 * `calc' or whatever easier.
1022 mp_writefile(k
->u
.m
, stdout
, 10);
1026 /* --- Structured keys --- *
1028 * Just iterate over the subkeys.
1036 fputs(" {\n", stdout
);
1037 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1038 if (!key_match(&ks
->k
, &o
->kf
))
1041 printf("%s =", SYM_NAME(ks
));
1043 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1044 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1047 fputs("}\n", stdout
);
1054 /* --- @showkey@ --- *
1056 * Arguments: @key *k@ = pointer to key to show
1057 * @listopts *o@ = pointer to listing options
1061 * Use: Emits a listing of a particular key.
1064 static void showkey(key
*k
, listopts
*o
)
1066 char ebuf
[24], dbuf
[24];
1069 /* --- Skip the key if the filter doesn't match --- */
1071 if (!key_match(&k
->k
, &o
->kf
))
1074 /* --- Sort out the expiry and deletion times --- */
1076 if (KEY_EXPIRED(o
->t
, k
->exp
))
1077 strcpy(ebuf
, "expired");
1078 else if (k
->exp
== KEXP_FOREVER
)
1079 strcpy(ebuf
, "forever");
1081 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1082 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1085 if (KEY_EXPIRED(o
->t
, k
->del
))
1086 strcpy(dbuf
, "deleted");
1087 else if (k
->del
== KEXP_FOREVER
)
1088 strcpy(dbuf
, "forever");
1090 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1091 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1094 /* --- If in compact format, just display and quit --- */
1097 if (!(o
->f
& f_newline
)) {
1098 printf("%8s %-20s %-20s %-10s %-10s\n",
1099 "Id", "Tag", "Type", "Expire", "Delete");
1101 printf("%08lx %-20s %-20s %-10s %-10s\n",
1102 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1103 k
->type
, ebuf
, dbuf
);
1108 /* --- Display the standard header --- */
1110 if (o
->f
& f_newline
)
1111 fputc('\n', stdout
);
1112 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1113 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1114 printf("type: %s\n", k
->type
);
1115 printf("expiry: %s\n", ebuf
);
1116 printf("delete: %s\n", dbuf
);
1117 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1119 /* --- Display the attributes --- */
1123 const char *av
, *an
;
1126 printf("attributes:");
1127 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1128 printf("\n %s = %s", an
, av
);
1132 fputc('\n', stdout
);
1137 /* --- If dumping requested, dump the raw key data --- */
1141 fputs("key:", stdout
);
1143 showkeydata(&k
->k
, 0, o
, &d
);
1150 /* --- @cmd_list@ --- */
1152 static int cmd_list(int argc
, char *argv
[])
1156 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1158 /* --- Parse subcommand options --- */
1161 static struct option opt
[] = {
1162 { "quiet", 0, 0, 'q' },
1163 { "verbose", 0, 0, 'v' },
1164 { "utc", 0, 0, 'u' },
1165 { "filter", OPTF_ARGREQ
, 0, 'f' },
1168 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1185 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1187 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1196 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1198 /* --- Open the key file --- */
1200 doopen(&f
, KOPEN_READ
);
1203 /* --- Set up the time format --- */
1206 o
.tfmt
= "%Y-%m-%d";
1207 else if (o
.f
& f_utc
)
1208 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1210 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1212 /* --- If specific keys were requested use them, otherwise do all --- *
1214 * Some day, this might turn into a wildcard match.
1217 if (optind
< argc
) {
1219 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1222 moan("key `%s' not found", argv
[optind
]);
1226 } while (optind
< argc
);
1229 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1237 return (EXIT_FAILURE
);
1242 /*----- Command implementation --------------------------------------------*/
1244 /* --- @cmd_expire@ --- */
1246 static int cmd_expire(int argc
, char *argv
[])
1254 die(EXIT_FAILURE
, "Usage: expire tag...");
1255 doopen(&f
, KOPEN_WRITE
);
1256 for (i
= 1; i
< argc
; i
++) {
1257 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1260 moan("key `%s' not found", argv
[i
]);
1268 /* --- @cmd_delete@ --- */
1270 static int cmd_delete(int argc
, char *argv
[])
1278 die(EXIT_FAILURE
, "Usage: delete tag...");
1279 doopen(&f
, KOPEN_WRITE
);
1280 for (i
= 1; i
< argc
; i
++) {
1281 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1284 moan("key `%s' not found", argv
[i
]);
1292 /* --- @cmd_setattr@ --- */
1294 static int cmd_setattr(int argc
, char *argv
[])
1300 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
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 setattr(&f
, k
, argv
+ 2);
1309 /* --- @cmd_finger@ --- */
1311 static void fingerprint(key
*k
, const key_filter
*kf
)
1314 octet hash
[RMD160_HASHSZ
];
1318 if (!key_encode(&k
->k
, &d
, kf
))
1321 rmd160_hash(&r
, d
.buf
, d
.len
);
1322 rmd160_done(&r
, hash
);
1326 for (i
= 0; i
< sizeof(hash
); i
++) {
1327 if (i
&& i
% 4 == 0)
1329 printf("%02x", hash
[i
]);
1331 printf(" %s\n", d
.buf
);
1335 static int cmd_finger(int argc
, char *argv
[])
1339 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1342 static struct option opt
[] = {
1343 { "filter", OPTF_ARGREQ
, 0, 'f' },
1346 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1352 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1354 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1362 argv
+= optind
; argc
-= optind
;
1364 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1366 doopen(&f
, KOPEN_READ
);
1370 for (i
= 0; i
< argc
; i
++) {
1371 key
*k
= key_bytag(&f
, argv
[i
]);
1373 fingerprint(k
, &kf
);
1376 moan("key `%s' not found", argv
[i
]);
1382 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1383 fingerprint(k
, &kf
);
1388 /* --- @cmd_comment@ --- */
1390 static int cmd_comment(int argc
, char *argv
[])
1396 if (argc
< 2 || argc
> 3)
1397 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1398 doopen(&f
, KOPEN_WRITE
);
1399 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1400 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1401 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1402 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1407 /* --- @cmd_tag@ --- */
1409 static int cmd_tag(int argc
, char *argv
[])
1418 static struct option opt
[] = {
1419 { "retag", 0, 0, 'r' },
1422 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1435 argv
+= optind
; argc
-= optind
;
1436 if (argc
< 1 || argc
> 2 || rc
)
1437 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1438 doopen(&f
, KOPEN_WRITE
);
1439 if (flags
& f_retag
) {
1440 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1441 key_settag(&f
, k
, 0);
1443 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1444 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1445 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1446 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1451 /* --- @cmd_lock@ --- */
1453 static int cmd_lock(int argc
, char *argv
[])
1461 die(EXIT_FAILURE
, "Usage: lock qtag");
1462 doopen(&f
, KOPEN_WRITE
);
1463 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1464 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1465 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1466 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1467 if (key_plock(d
.buf
, kd
, kd
))
1468 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1474 /* --- @cmd_unlock@ --- */
1476 static int cmd_unlock(int argc
, char *argv
[])
1484 die(EXIT_FAILURE
, "Usage: unlock qtag");
1485 doopen(&f
, KOPEN_WRITE
);
1486 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1487 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1488 if (kd
->e
!= KENC_ENCRYPT
)
1489 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1490 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1491 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1497 /* --- @cmd_extract@ --- */
1499 static int cmd_extract(int argc
, char *argv
[])
1505 key_filter kf
= { 0, 0 };
1509 static struct option opt
[] = {
1510 { "filter", OPTF_ARGREQ
, 0, 'f' },
1513 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1519 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1521 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1529 argv
+= optind
; argc
-= optind
;
1531 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1532 if (strcmp(*argv
, "-") == 0)
1534 else if (!(fp
= fopen(*argv
, "w"))) {
1535 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1536 *argv
, strerror(errno
));
1539 doopen(&f
, KOPEN_READ
);
1543 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1544 key_extract(&f
, k
, fp
, &kf
);
1546 for (i
= 1; i
< argc
; i
++) {
1547 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1548 key_extract(&f
, k
, fp
, &kf
);
1550 moan("key `%s' not found", argv
[i
]);
1556 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1561 /* --- @cmd_tidy@ --- */
1563 static int cmd_tidy(int argc
, char *argv
[])
1567 die(EXIT_FAILURE
, "usage: tidy");
1568 doopen(&f
, KOPEN_WRITE
);
1569 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1574 /* --- @cmd_merge@ --- */
1576 static int cmd_merge(int argc
, char *argv
[])
1582 die(EXIT_FAILURE
, "Usage: merge file");
1583 if (strcmp(argv
[1], "-") == 0)
1585 else if (!(fp
= fopen(argv
[1], "r"))) {
1586 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1587 argv
[1], strerror(errno
));
1590 doopen(&f
, KOPEN_WRITE
);
1591 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1596 /*----- Main command table ------------------------------------------------*/
1600 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1605 "add [options] type [attr...]\n\
1606 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1607 [-e expire] [-t tag] [-c comment]", "\
1610 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1611 -b, --bits=N Generate an N-bit key.\n\
1612 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1613 -p, --parameters=TAG Get group parameters from TAG.\n\
1614 -e, --expire=TIME Make the key expire after TIME.\n\
1615 -c, --comment=STRING Attach the command STRING to the key.\n\
1616 -t, --tag=TAG Tag the key with the name TAG.\n\
1617 -r, --retag Untag any key currently with that tag.\n\
1618 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1619 -l, --lock Lock the generated key with a passphrase.\n\
1620 -q, --quiet Don't give progress indicators while working.\n\
1621 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1622 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1624 { "expire", cmd_expire
, "expire tag..." },
1625 { "delete", cmd_delete
, "delete tag..." },
1626 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1629 -r, --retag Untag any key currently called new-tag.\n\
1631 { "setattr", cmd_setattr
, "setattr tag attr..." },
1632 { "comment", cmd_comment
, "comment tag [comment]" },
1633 { "lock", cmd_lock
, "lock qtag" },
1634 { "unlock", cmd_unlock
, "unlock qtag" },
1635 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1638 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1639 -q, --quiet Show less information.\n\
1640 -v, --verbose Show more information.\n\
1642 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1645 -f, --filter=FILT Only hash key components matching FILT.\n\
1647 { "tidy", cmd_tidy
, "tidy" },
1648 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1651 -f, --filter=FILT Only extract key components matching FILT.\n\
1653 { "merge", cmd_merge
, "merge file" },
1657 typedef struct cmd cmd
;
1659 /*----- Main code ---------------------------------------------------------*/
1661 /* --- @findcmd@ --- *
1663 * Arguments: @const char *name@ = a command name
1665 * Returns: Pointer to the command structure.
1667 * Use: Looks up a command by name. If the command isn't found, an
1668 * error is reported and the program is terminated.
1671 static cmd
*findcmd(const char *name
)
1673 cmd
*c
, *chosen
= 0;
1674 size_t sz
= strlen(name
);
1676 for (c
= cmds
; c
->name
; c
++) {
1677 if (strncmp(name
, c
->name
, sz
) == 0) {
1678 if (c
->name
[sz
] == 0) {
1682 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1688 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1692 /* --- Helpful GNUy functions --- */
1694 void usage(FILE *fp
)
1696 pquis(fp
, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1699 void version(FILE *fp
)
1701 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1704 void help(FILE *fp
, char **argv
)
1712 fprintf(fp
, "Usage: %s [-options] %s\n", QUIS
, c
->usage
);
1722 Performs various simple key management operations. Command line options\n\
1725 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1726 -v, --version Display version number.\n\
1727 -u, --usage Display short usage summary.\n\
1729 -k, --keyring=FILE Read and write keys in FILE.\n\
1730 -i, --id=TAG Use key TAG for random number generator.\n\
1731 -t, --type=TYPE Use key TYPE for random number generator.\n\
1733 The following commands are understood:\n\n",
1735 for (c
= cmds
; c
->name
; c
++)
1736 fprintf(fp
, "%s\n", c
->usage
);
1742 * Arguments: @int argc@ = number of command line arguments
1743 * @char *argv[]@ = array of command line arguments
1745 * Returns: Nonzero on failure.
1747 * Use: Main program. Performs simple key management functions.
1750 int main(int argc
, char *argv
[])
1756 /* --- Initialization --- */
1761 /* --- Parse command line options --- */
1764 static struct option opt
[] = {
1766 /* --- Standard GNUy help options --- */
1768 { "help", 0, 0, 'h' },
1769 { "version", 0, 0, 'v' },
1770 { "usage", 0, 0, 'u' },
1772 /* --- Real live useful options --- */
1774 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1775 { "id", OPTF_ARGREQ
, 0, 'i' },
1776 { "type", OPTF_ARGREQ
, 0, 't' },
1778 /* --- Magic terminator --- */
1782 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1788 /* --- GNU help options --- */
1791 help(stdout
, argv
+ optind
);
1800 /* --- Real useful options --- */
1806 /* --- Bogosity --- */
1814 /* --- Complain about excessive bogons --- */
1816 if (f
& f_bogus
|| optind
== argc
) {
1821 /* --- Initialize the Catacomb random number generator --- */
1823 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1824 rand_seed(RAND_GLOBAL
, 160);
1826 /* --- Dispatch to appropriate command handler --- */
1831 return (findcmd(argv
[0])->cmd(argc
, argv
));
1834 /*----- That's all, folks -------------------------------------------------*/