3 * $Id: keyutil.c,v 1.16 2003/10/15 09:31:45 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.16 2003/10/15 09:31:45 mdw
36 * Revision 1.15 2003/05/15 23:23:24 mdw
37 * Fix behaviour with bogus trailing attributes.
39 * Revision 1.14 2001/02/23 09:03:27 mdw
40 * Simplify usage message by removing nonexistant options.
42 * Revision 1.13 2001/02/21 20:04:27 mdw
43 * Provide help on individual commands (some need it desparately). Allow
44 * atomic retagging of keys.
46 * Revision 1.12 2001/02/03 11:58:22 mdw
47 * Store the correct seed information and count for DSA keys now that it's
50 * Revision 1.11 2000/12/06 20:33:27 mdw
51 * Make flags be macros rather than enumerations, to ensure that they're
54 * Revision 1.10 2000/10/08 12:02:21 mdw
55 * Use @MP_EQ@ instead of @MP_CMP@.
57 * Revision 1.9 2000/08/15 21:40:49 mdw
58 * Minor formatting change in listing attributes.
60 * Revision 1.8 2000/07/29 09:59:13 mdw
61 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
63 * Revision 1.7 2000/07/01 11:18:51 mdw
64 * Use new interfaces for key manipulation.
66 * Revision 1.6 2000/06/17 11:28:22 mdw
67 * Use secure memory interface from MP library. `rand_getgood' is
70 * Revision 1.5 2000/02/12 18:21:03 mdw
71 * Overhaul of key management (again).
73 * Revision 1.4 1999/12/22 15:48:10 mdw
74 * Track new key-management changes. Support new key generation
77 * Revision 1.3 1999/11/02 15:23:24 mdw
78 * Fix newlines in keyring list.
80 * Revision 1.2 1999/10/15 21:05:28 mdw
81 * In `key list', show timezone for local times, and support `-u' option
84 * Revision 1.1 1999/09/03 08:41:12 mdw
89 /*----- Header files ------------------------------------------------------*/
99 #include <mLib/base64.h>
100 #include <mLib/mdwopt.h>
101 #include <mLib/quis.h>
102 #include <mLib/report.h>
103 #include <mLib/sub.h>
121 /*----- Handy global state ------------------------------------------------*/
123 static const char *keyfile
= "keyring";
125 /*----- Useful shared functions -------------------------------------------*/
127 /* --- @doopen@ --- *
129 * Arguments: @key_file *f@ = pointer to key file block
130 * @unsigned how@ = method to open file with
134 * Use: Opens a key file and handles errors by panicking
138 static void doopen(key_file
*f
, unsigned how
)
140 if (key_open(f
, keyfile
, how
, key_moan
, 0))
141 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
144 /* --- @doclose@ --- *
146 * Arguments: @key_file *f@ = pointer to key file block
150 * Use: Closes a key file and handles errors by panicking
154 static void doclose(key_file
*f
)
156 switch (key_close(f
)) {
158 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
159 keyfile
, strerror(errno
));
161 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
162 keyfile
, strerror(errno
));
166 /* --- @setattr@ --- *
168 * Arguments: @key_file *f@ = pointer to key file block
169 * @key *k@ = pointer to key block
170 * @char *v[]@ = array of assignments (overwritten!)
174 * Use: Applies the attribute assignments to the key.
177 static void setattr(key_file
*f
, key
*k
, char *v
[])
182 size_t eq
= strcspn(p
, "=");
184 moan("invalid assignment: `%s' (ignored)", p
);
190 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
191 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
196 /*----- Key generation ----------------------------------------------------*/
198 /* --- Key generation parameters --- */
200 typedef struct keyopts
{
201 key_file
*kf
; /* Pointer to key file */
202 key
*k
; /* Pointer to the actual key */
203 dstr tag
; /* Full tag name for the key */
204 unsigned f
; /* Flags for the new key */
205 unsigned bits
, qbits
; /* Bit length for the new key */
206 key
*p
; /* Parameters key-data */
209 #define f_bogus 1u /* Error in parsing */
210 #define f_lock 2u /* Passphrase-lock private key */
211 #define f_quiet 4u /* Don't show a progress indicator */
212 #define f_limlee 8u /* Generate Lim-Lee primes */
213 #define f_subgroup 16u /* Generate a subgroup */
214 #define f_retag 32u /* Remove any existing tag */
216 /* --- @dolock@ --- *
218 * Arguments: @keyopts *k@ = key generation options
219 * @key_data *kd@ = pointer to key data to lock
220 * @const char *t@ = tag suffix or null
224 * Use: Does passphrase locking on new keys.
227 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
229 if (!(k
->f
& f_lock
))
232 dstr_putf(&k
->tag
, ".%s", t
);
233 if (key_plock(k
->tag
.buf
, kd
, kd
))
234 die(EXIT_FAILURE
, "couldn't lock key");
239 * Arguments: @key_data *kd@ = pointer to parent key block
240 * @const char *tag@ = pointer to tag string
241 * @mp *m@ = integer to store
242 * @unsigned f@ = flags to set
246 * Use: Sets a multiprecision integer subkey.
249 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
251 key_data
*kkd
= key_structcreate(kd
, tag
);
256 /* --- @copyparam@ --- *
258 * Arguments: @keyopts *k@ = pointer to key options
259 * @const char **pp@ = checklist of parameters
261 * Returns: Nonzero if parameters copied; zero if you have to generate
264 * Use: Copies parameters from a source key to the current one.
267 static int copyparam(keyopts
*k
, const char **pp
)
271 /* --- Quick check if no parameters supplied --- */
276 /* --- Run through the checklist --- */
279 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
281 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
282 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
283 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
287 /* --- Copy over the parameters --- */
291 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
292 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
298 * Arguments: @key_data *k@ = pointer to key data block
299 * @const char *tag@ = tag string to use
301 * Returns: Pointer to multiprecision integer key item.
303 * Use: Fetches an MP key component.
306 static mp
*getmp(key_data
*k
, const char *tag
)
308 k
= key_structfind(k
, tag
);
310 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
311 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
312 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
316 /* --- @keyrand@ --- *
318 * Arguments: @key_file *kf@ = pointer to key file
319 * @const char *id@ = pointer to key id (or null)
323 * Use: Keys the random number generator.
326 static void keyrand(key_file
*kf
, const char *id
)
330 /* --- Find the key --- */
333 if ((k
= key_bytag(kf
, id
)) == 0)
334 die(EXIT_FAILURE
, "key `%s' not found", id
);
336 k
= key_bytype(kf
, "catacomb-rand");
339 key_data
*kd
= &k
->k
, kkd
;
342 switch (kd
->e
& KF_ENCMASK
) {
348 if (key_punlock(d
.buf
, kd
, &kkd
))
349 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
356 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
360 /* --- Key the generator --- */
362 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
368 /* --- Key generation algorithms --- */
370 static void alg_binary(keyopts
*k
)
379 die(EXIT_FAILURE
, "no shared parameters for binary keys");
381 sz
= (k
->bits
+ 7) >> 3;
383 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
384 rand_get(RAND_GLOBAL
, p
, sz
);
386 key_binary(&k
->k
->k
, p
, sz
);
387 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
390 dolock(k
, &k
->k
->k
, 0);
393 static void alg_des(keyopts
*k
)
402 die(EXIT_FAILURE
, "no shared parameters for DES keys");
403 if (k
->bits
% 56 || k
->bits
> 168)
404 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
408 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
409 for (i
= 0; i
< sz
; i
++) {
410 octet x
= p
[i
] | 0x01;
414 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
416 key_binary(&k
->k
->k
, p
, sz
);
417 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
420 dolock(k
, &k
->k
->k
, 0);
423 static void alg_rsa(keyopts
*k
)
428 /* --- Sanity checking --- */
431 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
435 /* --- Generate the RSA parameters --- */
437 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
438 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
439 die(EXIT_FAILURE
, "RSA key generation failed");
441 /* --- Run a test encryption --- */
444 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
446 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
451 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
452 c
= rsa_qprivop(&rp
, c
, c
, g
);
455 die(EXIT_FAILURE
, "test encryption failed");
461 /* --- Allrighty then --- */
465 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
466 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
468 kd
= key_structcreate(kd
, "private");
470 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
471 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
472 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
473 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
474 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
475 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
476 dolock(k
, kd
, "private");
481 static void alg_dsaparam(keyopts
*k
)
483 static const char *pl
[] = { "q", "p", "g", 0 };
484 if (!copyparam(k
, pl
)) {
490 key_data
*kd
= &k
->k
->k
;
493 /* --- Choose appropriate bit lengths if necessary --- */
500 /* --- Allocate a seed block --- */
502 sz
= (k
->qbits
+ 7) >> 3;
504 rand_get(RAND_GLOBAL
, p
, sz
);
506 /* --- Allocate the parameters --- */
508 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
509 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
510 die(EXIT_FAILURE
, "DSA parameter generation failed");
512 /* --- Store the parameters --- */
515 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
516 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
517 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
522 /* --- Store the seed for future verification --- */
527 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
528 base64_encode(&c
, 0, 0, &d
);
529 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
531 dstr_putf(&d
, "%u", ds
.count
);
532 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
539 static void alg_dsa(keyopts
*k
)
544 key_data
*kd
= &k
->k
->k
;
546 /* --- Get the shared parameters --- */
553 /* --- Choose a private key --- */
555 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
556 mpmont_create(&mm
, p
);
557 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
559 /* --- Store everything away --- */
561 mpkey(kd
, "y", y
, KCAT_PUB
);
563 kd
= key_structcreate(kd
, "private");
565 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
566 dolock(k
, kd
, "private");
568 mp_drop(x
); mp_drop(y
);
571 static void alg_dhparam(keyopts
*k
)
573 static const char *pl
[] = { "p", "q", "g", 0 };
574 if (!copyparam(k
, pl
)) {
576 key_data
*kd
= &k
->k
->k
;
582 /* --- Choose a large safe prime number --- */
584 if (k
->f
& f_limlee
) {
589 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
590 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
591 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
592 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
596 for (i
= 0; i
< nf
; i
++) {
599 mp_writedstr(f
[i
], &d
, 10);
602 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
606 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
607 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
610 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
613 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
614 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
615 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
622 static void alg_dh(keyopts
*k
)
627 key_data
*kd
= &k
->k
->k
;
629 /* --- Get the shared parameters --- */
636 /* --- Choose a suitable private key --- *
638 * Since %$g$% has order %$q$%, choose %$x < q$%.
641 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
643 /* --- Compute the public key %$y = g^x \bmod p$% --- */
645 mpmont_create(&mm
, p
);
646 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
649 /* --- Store everything away --- */
651 mpkey(kd
, "y", y
, KCAT_PUB
);
653 kd
= key_structcreate(kd
, "private");
655 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
656 dolock(k
, kd
, "private");
658 mp_drop(x
); mp_drop(y
);
661 static void alg_bbs(keyopts
*k
)
666 /* --- Sanity checking --- */
669 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
673 /* --- Generate the BBS parameters --- */
675 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
676 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
677 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
679 /* --- Allrighty then --- */
683 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
685 kd
= key_structcreate(kd
, "private");
687 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
688 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
689 dolock(k
, kd
, "private");
694 /* --- The algorithm tables --- */
696 typedef struct keyalg
{
698 void (*proc
)(keyopts
*o
);
702 static keyalg algtab
[] = {
703 { "binary", alg_binary
, "Plain binary data" },
704 { "des", alg_des
, "Binary with DES-style parity" },
705 { "rsa", alg_rsa
, "RSA public-key encryption" },
706 { "dsa", alg_dsa
, "DSA digital signatures" },
707 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
708 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
709 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
710 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
714 /* --- @cmd_add@ --- */
716 static int cmd_add(int argc
, char *argv
[])
719 time_t exp
= KEXP_EXPIRE
;
720 const char *tag
= 0, *ptag
= 0;
722 keyalg
*alg
= algtab
;
723 const char *rtag
= 0;
724 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
726 /* --- Parse options for the subcommand --- */
729 static struct option opt
[] = {
730 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
731 { "bits", OPTF_ARGREQ
, 0, 'b' },
732 { "qbits", OPTF_ARGREQ
, 0, 'B' },
733 { "parameters", OPTF_ARGREQ
, 0, 'p' },
734 { "expire", OPTF_ARGREQ
, 0, 'e' },
735 { "comment", OPTF_ARGREQ
, 0, 'c' },
736 { "tag", OPTF_ARGREQ
, 0, 't' },
737 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
738 { "lock", 0, 0, 'l' },
739 { "quiet", 0, 0, 'q' },
740 { "lim-lee", 0, 0, 'L' },
741 { "subgroup", 0, 0, 'S' },
744 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:lqrLS", opt
, 0, 0, 0);
748 /* --- Handle the various options --- */
752 /* --- Read an algorithm name --- */
756 size_t sz
= strlen(optarg
);
758 if (strcmp(optarg
, "list") == 0) {
759 for (a
= algtab
; a
->name
; a
++)
760 printf("%-10s %s\n", a
->name
, a
->help
);
765 for (a
= algtab
; a
->name
; a
++) {
766 if (strncmp(optarg
, a
->name
, sz
) == 0) {
767 if (a
->name
[sz
] == 0) {
771 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
777 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
780 /* --- Bits must be nonzero and a multiple of 8 --- */
784 k
.bits
= strtoul(optarg
, &p
, 0);
785 if (k
.bits
== 0 || *p
!= 0)
786 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
791 k
.qbits
= strtoul(optarg
, &p
, 0);
792 if (k
.qbits
== 0 || *p
!= 0)
793 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
796 /* --- Parameter selection --- */
802 /* --- Expiry dates get passed to @get_date@ for parsing --- */
805 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
808 exp
= get_date(optarg
, 0);
810 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
814 /* --- Store comments without interpretation --- */
817 if (key_chkcomment(optarg
))
818 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
822 /* --- Store tags --- */
825 if (key_chkident(optarg
))
826 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
833 /* --- Other flags --- */
851 /* --- Other things are bogus --- */
859 /* --- Various sorts of bogosity --- */
861 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
863 "Usage: add [options] type [attr...]");
865 if (key_chkident(argv
[optind
]))
866 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
868 /* --- Set up various bits of the state --- */
870 if (exp
== KEXP_EXPIRE
)
871 exp
= time(0) + 14 * 24 * 60 * 60;
873 /* --- Open the file and create the basic key block --- *
875 * Keep on generating keyids until one of them doesn't collide.
878 doopen(&f
, KOPEN_WRITE
);
881 /* --- Key the generator --- */
886 uint32 id
= rand_global
.ops
->word(&rand_global
);
888 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
891 if (err
!= KERR_DUPID
)
892 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
895 /* --- Set various simple attributes --- */
901 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
902 key_settag(&f
, kk
, 0);
904 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
905 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
909 int err
= key_setcomment(&f
, k
.k
, c
);
911 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
914 setattr(&f
, k
.k
, argv
+ optind
+ 1);
916 key_fulltag(k
.k
, &k
.tag
);
918 /* --- Find the parameter key --- */
921 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
922 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
923 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
924 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
927 /* --- Now generate the actual key data --- */
937 /*----- Key listing -------------------------------------------------------*/
939 /* --- Listing options --- */
941 typedef struct listopts
{
942 const char *tfmt
; /* Date format (@strftime@-style) */
943 int v
; /* Verbosity level */
944 unsigned f
; /* Various flags */
945 time_t t
; /* Time now (for key expiry) */
946 key_filter kf
; /* Filter for matching keys */
949 /* --- Listing flags --- */
951 #define f_newline 2u /* Write newline before next entry */
952 #define f_attr 4u /* Written at least one attribute */
953 #define f_utc 8u /* Emit UTC time, not local time */
955 /* --- @showkeydata@ --- *
957 * Arguments: @key_data *k@ = pointer to key to write
958 * @int ind@ = indentation level
959 * @listopts *o@ = listing options
960 * @dstr *d@ = tag string for this subkey
964 * Use: Emits a piece of key data in a human-readable format.
967 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
969 #define INDENT(i) do { \
971 for (_i = 0; _i < (i); _i++) { \
976 switch (k
->e
& KF_ENCMASK
) {
978 /* --- Binary key data --- *
980 * Emit as a simple hex dump.
984 const octet
*p
= k
->u
.k
.k
;
985 const octet
*l
= p
+ k
->u
.k
.sz
;
993 } else if (sz
% 8 == 0)
997 printf("%02x", *p
++);
1002 fputs("}\n", stdout
);
1005 /* --- Encrypted data --- *
1007 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1008 * key. Otherwise just say that it's encrypted and move on.
1013 fputs(" encrypted\n", stdout
);
1016 if (key_punlock(d
->buf
, k
, &kd
))
1017 printf(" <failed to unlock %s>\n", d
->buf
);
1019 fputs(" encrypted", stdout
);
1020 showkeydata(&kd
, ind
, o
, d
);
1026 /* --- Integer keys --- *
1028 * Emit as a large integer in decimal. This makes using the key in
1029 * `calc' or whatever easier.
1034 mp_writefile(k
->u
.m
, stdout
, 10);
1038 /* --- Structured keys --- *
1040 * Just iterate over the subkeys.
1048 fputs(" {\n", stdout
);
1049 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1050 if (!key_match(&ks
->k
, &o
->kf
))
1053 printf("%s =", SYM_NAME(ks
));
1055 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1056 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1059 fputs("}\n", stdout
);
1066 /* --- @showkey@ --- *
1068 * Arguments: @key *k@ = pointer to key to show
1069 * @listopts *o@ = pointer to listing options
1073 * Use: Emits a listing of a particular key.
1076 static void showkey(key
*k
, listopts
*o
)
1078 char ebuf
[24], dbuf
[24];
1081 /* --- Skip the key if the filter doesn't match --- */
1083 if (!key_match(&k
->k
, &o
->kf
))
1086 /* --- Sort out the expiry and deletion times --- */
1088 if (KEY_EXPIRED(o
->t
, k
->exp
))
1089 strcpy(ebuf
, "expired");
1090 else if (k
->exp
== KEXP_FOREVER
)
1091 strcpy(ebuf
, "forever");
1093 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1094 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1097 if (KEY_EXPIRED(o
->t
, k
->del
))
1098 strcpy(dbuf
, "deleted");
1099 else if (k
->del
== KEXP_FOREVER
)
1100 strcpy(dbuf
, "forever");
1102 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1103 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1106 /* --- If in compact format, just display and quit --- */
1109 if (!(o
->f
& f_newline
)) {
1110 printf("%8s %-20s %-20s %-10s %-10s\n",
1111 "Id", "Tag", "Type", "Expire", "Delete");
1113 printf("%08lx %-20s %-20s %-10s %-10s\n",
1114 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1115 k
->type
, ebuf
, dbuf
);
1120 /* --- Display the standard header --- */
1122 if (o
->f
& f_newline
)
1123 fputc('\n', stdout
);
1124 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1125 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1126 printf("type: %s\n", k
->type
);
1127 printf("expiry: %s\n", ebuf
);
1128 printf("delete: %s\n", dbuf
);
1129 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1131 /* --- Display the attributes --- */
1135 const char *av
, *an
;
1138 printf("attributes:");
1139 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1140 printf("\n %s = %s", an
, av
);
1144 fputc('\n', stdout
);
1149 /* --- If dumping requested, dump the raw key data --- */
1153 fputs("key:", stdout
);
1155 showkeydata(&k
->k
, 0, o
, &d
);
1162 /* --- @cmd_list@ --- */
1164 static int cmd_list(int argc
, char *argv
[])
1168 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1170 /* --- Parse subcommand options --- */
1173 static struct option opt
[] = {
1174 { "quiet", 0, 0, 'q' },
1175 { "verbose", 0, 0, 'v' },
1176 { "utc", 0, 0, 'u' },
1177 { "filter", OPTF_ARGREQ
, 0, 'f' },
1180 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1197 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1199 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1208 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1210 /* --- Open the key file --- */
1212 doopen(&f
, KOPEN_READ
);
1215 /* --- Set up the time format --- */
1218 o
.tfmt
= "%Y-%m-%d";
1219 else if (o
.f
& f_utc
)
1220 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1222 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1224 /* --- If specific keys were requested use them, otherwise do all --- *
1226 * Some day, this might turn into a wildcard match.
1229 if (optind
< argc
) {
1231 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1234 moan("key `%s' not found", argv
[optind
]);
1238 } while (optind
< argc
);
1241 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1249 return (EXIT_FAILURE
);
1254 /*----- Command implementation --------------------------------------------*/
1256 /* --- @cmd_expire@ --- */
1258 static int cmd_expire(int argc
, char *argv
[])
1266 die(EXIT_FAILURE
, "Usage: expire tag...");
1267 doopen(&f
, KOPEN_WRITE
);
1268 for (i
= 1; i
< argc
; i
++) {
1269 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1272 moan("key `%s' not found", argv
[i
]);
1280 /* --- @cmd_delete@ --- */
1282 static int cmd_delete(int argc
, char *argv
[])
1290 die(EXIT_FAILURE
, "Usage: delete tag...");
1291 doopen(&f
, KOPEN_WRITE
);
1292 for (i
= 1; i
< argc
; i
++) {
1293 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1296 moan("key `%s' not found", argv
[i
]);
1304 /* --- @cmd_setattr@ --- */
1306 static int cmd_setattr(int argc
, char *argv
[])
1312 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1313 doopen(&f
, KOPEN_WRITE
);
1314 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1315 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1316 setattr(&f
, k
, argv
+ 2);
1321 /* --- @cmd_finger@ --- */
1323 static void fingerprint(key
*k
, const key_filter
*kf
)
1326 octet hash
[RMD160_HASHSZ
];
1330 if (!key_encode(&k
->k
, &d
, kf
))
1333 rmd160_hash(&r
, d
.buf
, d
.len
);
1334 rmd160_done(&r
, hash
);
1338 for (i
= 0; i
< sizeof(hash
); i
++) {
1339 if (i
&& i
% 4 == 0)
1341 printf("%02x", hash
[i
]);
1343 printf(" %s\n", d
.buf
);
1347 static int cmd_finger(int argc
, char *argv
[])
1351 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1354 static struct option opt
[] = {
1355 { "filter", OPTF_ARGREQ
, 0, 'f' },
1358 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1364 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1366 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1374 argv
+= optind
; argc
-= optind
;
1376 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1378 doopen(&f
, KOPEN_READ
);
1382 for (i
= 0; i
< argc
; i
++) {
1383 key
*k
= key_bytag(&f
, argv
[i
]);
1385 fingerprint(k
, &kf
);
1388 moan("key `%s' not found", argv
[i
]);
1394 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1395 fingerprint(k
, &kf
);
1400 /* --- @cmd_comment@ --- */
1402 static int cmd_comment(int argc
, char *argv
[])
1408 if (argc
< 2 || argc
> 3)
1409 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1410 doopen(&f
, KOPEN_WRITE
);
1411 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1412 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1413 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1414 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1419 /* --- @cmd_tag@ --- */
1421 static int cmd_tag(int argc
, char *argv
[])
1430 static struct option opt
[] = {
1431 { "retag", 0, 0, 'r' },
1434 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1447 argv
+= optind
; argc
-= optind
;
1448 if (argc
< 1 || argc
> 2 || rc
)
1449 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1450 doopen(&f
, KOPEN_WRITE
);
1451 if (flags
& f_retag
) {
1452 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1453 key_settag(&f
, k
, 0);
1455 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1456 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1457 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1458 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1463 /* --- @cmd_lock@ --- */
1465 static int cmd_lock(int argc
, char *argv
[])
1473 die(EXIT_FAILURE
, "Usage: lock qtag");
1474 doopen(&f
, KOPEN_WRITE
);
1475 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1476 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1477 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1478 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1479 if (key_plock(d
.buf
, kd
, kd
))
1480 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1486 /* --- @cmd_unlock@ --- */
1488 static int cmd_unlock(int argc
, char *argv
[])
1496 die(EXIT_FAILURE
, "Usage: unlock qtag");
1497 doopen(&f
, KOPEN_WRITE
);
1498 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1499 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1500 if (kd
->e
!= KENC_ENCRYPT
)
1501 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1502 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1503 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1509 /* --- @cmd_extract@ --- */
1511 static int cmd_extract(int argc
, char *argv
[])
1517 key_filter kf
= { 0, 0 };
1521 static struct option opt
[] = {
1522 { "filter", OPTF_ARGREQ
, 0, 'f' },
1525 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1531 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1533 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1541 argv
+= optind
; argc
-= optind
;
1543 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1544 if (strcmp(*argv
, "-") == 0)
1546 else if (!(fp
= fopen(*argv
, "w"))) {
1547 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1548 *argv
, strerror(errno
));
1551 doopen(&f
, KOPEN_READ
);
1555 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1556 key_extract(&f
, k
, fp
, &kf
);
1558 for (i
= 1; i
< argc
; i
++) {
1559 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1560 key_extract(&f
, k
, fp
, &kf
);
1562 moan("key `%s' not found", argv
[i
]);
1568 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1573 /* --- @cmd_tidy@ --- */
1575 static int cmd_tidy(int argc
, char *argv
[])
1579 die(EXIT_FAILURE
, "usage: tidy");
1580 doopen(&f
, KOPEN_WRITE
);
1581 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1586 /* --- @cmd_merge@ --- */
1588 static int cmd_merge(int argc
, char *argv
[])
1594 die(EXIT_FAILURE
, "Usage: merge file");
1595 if (strcmp(argv
[1], "-") == 0)
1597 else if (!(fp
= fopen(argv
[1], "r"))) {
1598 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1599 argv
[1], strerror(errno
));
1602 doopen(&f
, KOPEN_WRITE
);
1603 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1608 /*----- Main command table ------------------------------------------------*/
1612 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1617 "add [options] type [attr...]\n\
1618 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1619 [-e expire] [-t tag] [-c comment]", "\
1622 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1623 -b, --bits=N Generate an N-bit key.\n\
1624 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1625 -p, --parameters=TAG Get group parameters from TAG.\n\
1626 -e, --expire=TIME Make the key expire after TIME.\n\
1627 -c, --comment=STRING Attach the command STRING to the key.\n\
1628 -t, --tag=TAG Tag the key with the name TAG.\n\
1629 -r, --retag Untag any key currently with that tag.\n\
1630 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1631 -l, --lock Lock the generated key with a passphrase.\n\
1632 -q, --quiet Don't give progress indicators while working.\n\
1633 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1634 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1636 { "expire", cmd_expire
, "expire tag..." },
1637 { "delete", cmd_delete
, "delete tag..." },
1638 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1641 -r, --retag Untag any key currently called new-tag.\n\
1643 { "setattr", cmd_setattr
, "setattr tag attr..." },
1644 { "comment", cmd_comment
, "comment tag [comment]" },
1645 { "lock", cmd_lock
, "lock qtag" },
1646 { "unlock", cmd_unlock
, "unlock qtag" },
1647 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1650 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1651 -q, --quiet Show less information.\n\
1652 -v, --verbose Show more information.\n\
1654 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1657 -f, --filter=FILT Only hash key components matching FILT.\n\
1659 { "tidy", cmd_tidy
, "tidy" },
1660 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1663 -f, --filter=FILT Only extract key components matching FILT.\n\
1665 { "merge", cmd_merge
, "merge file" },
1669 typedef struct cmd cmd
;
1671 /*----- Main code ---------------------------------------------------------*/
1673 /* --- @findcmd@ --- *
1675 * Arguments: @const char *name@ = a command name
1677 * Returns: Pointer to the command structure.
1679 * Use: Looks up a command by name. If the command isn't found, an
1680 * error is reported and the program is terminated.
1683 static cmd
*findcmd(const char *name
)
1685 cmd
*c
, *chosen
= 0;
1686 size_t sz
= strlen(name
);
1688 for (c
= cmds
; c
->name
; c
++) {
1689 if (strncmp(name
, c
->name
, sz
) == 0) {
1690 if (c
->name
[sz
] == 0) {
1694 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1700 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1704 /* --- Helpful GNUy functions --- */
1706 void usage(FILE *fp
)
1708 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1711 void version(FILE *fp
)
1713 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1716 void help(FILE *fp
, char **argv
)
1724 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1732 Performs various simple key management operations. Command line options\n\
1735 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1736 -v, --version Display version number.\n\
1737 -u, --usage Display short usage summary.\n\
1739 -k, --keyring=FILE Read and write keys in FILE.\n\
1740 -i, --id=TAG Use key TAG for random number generator.\n\
1741 -t, --type=TYPE Use key TYPE for random number generator.\n\
1743 The following commands are understood:\n\n",
1745 for (c
= cmds
; c
->name
; c
++)
1746 fprintf(fp
, "%s\n", c
->usage
);
1752 * Arguments: @int argc@ = number of command line arguments
1753 * @char *argv[]@ = array of command line arguments
1755 * Returns: Nonzero on failure.
1757 * Use: Main program. Performs simple key management functions.
1760 int main(int argc
, char *argv
[])
1766 /* --- Initialization --- */
1771 /* --- Parse command line options --- */
1774 static struct option opt
[] = {
1776 /* --- Standard GNUy help options --- */
1778 { "help", 0, 0, 'h' },
1779 { "version", 0, 0, 'v' },
1780 { "usage", 0, 0, 'u' },
1782 /* --- Real live useful options --- */
1784 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1786 /* --- Magic terminator --- */
1790 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1796 /* --- GNU help options --- */
1799 help(stdout
, argv
+ optind
);
1808 /* --- Real useful options --- */
1814 /* --- Bogosity --- */
1822 /* --- Complain about excessive bogons --- */
1824 if (f
& f_bogus
|| optind
== argc
) {
1829 /* --- Initialize the Catacomb random number generator --- */
1831 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1832 rand_seed(RAND_GLOBAL
, 160);
1834 /* --- Dispatch to appropriate command handler --- */
1839 return (findcmd(argv
[0])->cmd(argc
, argv
));
1842 /*----- That's all, folks -------------------------------------------------*/