3 * $Id: keyutil.c,v 1.15 2003/05/15 23:23:24 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.15 2003/05/15 23:23:24 mdw
34 * Fix behaviour with bogus trailing attributes.
36 * Revision 1.14 2001/02/23 09:03:27 mdw
37 * Simplify usage message by removing nonexistant options.
39 * Revision 1.13 2001/02/21 20:04:27 mdw
40 * Provide help on individual commands (some need it desparately). Allow
41 * atomic retagging of keys.
43 * Revision 1.12 2001/02/03 11:58:22 mdw
44 * Store the correct seed information and count for DSA keys now that it's
47 * Revision 1.11 2000/12/06 20:33:27 mdw
48 * Make flags be macros rather than enumerations, to ensure that they're
51 * Revision 1.10 2000/10/08 12:02:21 mdw
52 * Use @MP_EQ@ instead of @MP_CMP@.
54 * Revision 1.9 2000/08/15 21:40:49 mdw
55 * Minor formatting change in listing attributes.
57 * Revision 1.8 2000/07/29 09:59:13 mdw
58 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
60 * Revision 1.7 2000/07/01 11:18:51 mdw
61 * Use new interfaces for key manipulation.
63 * Revision 1.6 2000/06/17 11:28:22 mdw
64 * Use secure memory interface from MP library. `rand_getgood' is
67 * Revision 1.5 2000/02/12 18:21:03 mdw
68 * Overhaul of key management (again).
70 * Revision 1.4 1999/12/22 15:48:10 mdw
71 * Track new key-management changes. Support new key generation
74 * Revision 1.3 1999/11/02 15:23:24 mdw
75 * Fix newlines in keyring list.
77 * Revision 1.2 1999/10/15 21:05:28 mdw
78 * In `key list', show timezone for local times, and support `-u' option
81 * Revision 1.1 1999/09/03 08:41:12 mdw
86 /*----- Header files ------------------------------------------------------*/
96 #include <mLib/base64.h>
97 #include <mLib/mdwopt.h>
98 #include <mLib/quis.h>
99 #include <mLib/report.h>
100 #include <mLib/sub.h>
118 /*----- Handy global state ------------------------------------------------*/
120 static const char *keyfile
= "keyring";
122 /*----- Useful shared functions -------------------------------------------*/
124 /* --- @doopen@ --- *
126 * Arguments: @key_file *f@ = pointer to key file block
127 * @unsigned how@ = method to open file with
131 * Use: Opens a key file and handles errors by panicking
135 static void doopen(key_file
*f
, unsigned how
)
137 if (key_open(f
, keyfile
, how
, key_moan
, 0))
138 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
141 /* --- @doclose@ --- *
143 * Arguments: @key_file *f@ = pointer to key file block
147 * Use: Closes a key file and handles errors by panicking
151 static void doclose(key_file
*f
)
153 switch (key_close(f
)) {
155 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
156 keyfile
, strerror(errno
));
158 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
159 keyfile
, strerror(errno
));
163 /* --- @setattr@ --- *
165 * Arguments: @key_file *f@ = pointer to key file block
166 * @key *k@ = pointer to key block
167 * @char *v[]@ = array of assignments (overwritten!)
171 * Use: Applies the attribute assignments to the key.
174 static void setattr(key_file
*f
, key
*k
, char *v
[])
179 size_t eq
= strcspn(p
, "=");
181 moan("invalid assignment: `%s' (ignored)", p
);
187 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
188 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
193 /*----- Key generation ----------------------------------------------------*/
195 /* --- Key generation parameters --- */
197 typedef struct keyopts
{
198 key_file
*kf
; /* Pointer to key file */
199 key
*k
; /* Pointer to the actual key */
200 dstr tag
; /* Full tag name for the key */
201 unsigned f
; /* Flags for the new key */
202 unsigned bits
, qbits
; /* Bit length for the new key */
203 key
*p
; /* Parameters key-data */
206 #define f_bogus 1u /* Error in parsing */
207 #define f_lock 2u /* Passphrase-lock private key */
208 #define f_quiet 4u /* Don't show a progress indicator */
209 #define f_limlee 8u /* Generate Lim-Lee primes */
210 #define f_subgroup 16u /* Generate a subgroup */
211 #define f_retag 32u /* Remove any existing tag */
213 /* --- @dolock@ --- *
215 * Arguments: @keyopts *k@ = key generation options
216 * @key_data *kd@ = pointer to key data to lock
217 * @const char *t@ = tag suffix or null
221 * Use: Does passphrase locking on new keys.
224 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
226 if (!(k
->f
& f_lock
))
229 dstr_putf(&k
->tag
, ".%s", t
);
230 if (key_plock(k
->tag
.buf
, kd
, kd
))
231 die(EXIT_FAILURE
, "couldn't lock key");
236 * Arguments: @key_data *kd@ = pointer to parent key block
237 * @const char *tag@ = pointer to tag string
238 * @mp *m@ = integer to store
239 * @unsigned f@ = flags to set
243 * Use: Sets a multiprecision integer subkey.
246 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
248 key_data
*kkd
= key_structcreate(kd
, tag
);
253 /* --- @copyparam@ --- *
255 * Arguments: @keyopts *k@ = pointer to key options
256 * @const char **pp@ = checklist of parameters
258 * Returns: Nonzero if parameters copied; zero if you have to generate
261 * Use: Copies parameters from a source key to the current one.
264 static int copyparam(keyopts
*k
, const char **pp
)
268 /* --- Quick check if no parameters supplied --- */
273 /* --- Run through the checklist --- */
276 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
278 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
279 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
280 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
284 /* --- Copy over the parameters --- */
288 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
289 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
295 * Arguments: @key_data *k@ = pointer to key data block
296 * @const char *tag@ = tag string to use
298 * Returns: Pointer to multiprecision integer key item.
300 * Use: Fetches an MP key component.
303 static mp
*getmp(key_data
*k
, const char *tag
)
305 k
= key_structfind(k
, tag
);
307 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
308 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
309 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
313 /* --- @keyrand@ --- *
315 * Arguments: @key_file *kf@ = pointer to key file
316 * @const char *id@ = pointer to key id (or null)
320 * Use: Keys the random number generator.
323 static void keyrand(key_file
*kf
, const char *id
)
327 /* --- Find the key --- */
330 if ((k
= key_bytag(kf
, id
)) == 0)
331 die(EXIT_FAILURE
, "key `%s' not found", id
);
333 k
= key_bytype(kf
, "catacomb-rand");
336 key_data
*kd
= &k
->k
, kkd
;
339 switch (kd
->e
& KF_ENCMASK
) {
345 if (key_punlock(d
.buf
, kd
, &kkd
))
346 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
353 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
357 /* --- Key the generator --- */
359 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
365 /* --- Key generation algorithms --- */
367 static void alg_binary(keyopts
*k
)
376 die(EXIT_FAILURE
, "no shared parameters for binary keys");
378 sz
= (k
->bits
+ 7) >> 3;
380 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
381 rand_get(RAND_GLOBAL
, p
, sz
);
383 key_binary(&k
->k
->k
, p
, sz
);
384 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
387 dolock(k
, &k
->k
->k
, 0);
390 static void alg_des(keyopts
*k
)
399 die(EXIT_FAILURE
, "no shared parameters for DES keys");
400 if (k
->bits
% 56 || k
->bits
> 168)
401 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
405 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
406 for (i
= 0; i
< sz
; i
++) {
407 octet x
= p
[i
] | 0x01;
411 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
413 key_binary(&k
->k
->k
, p
, sz
);
414 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
417 dolock(k
, &k
->k
->k
, 0);
420 static void alg_rsa(keyopts
*k
)
425 /* --- Sanity checking --- */
428 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
432 /* --- Generate the RSA parameters --- */
434 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
435 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
436 die(EXIT_FAILURE
, "RSA key generation failed");
438 /* --- Run a test encryption --- */
441 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
443 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
448 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
449 c
= rsa_qprivop(&rp
, c
, c
, g
);
452 die(EXIT_FAILURE
, "test encryption failed");
458 /* --- Allrighty then --- */
462 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
463 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
465 kd
= key_structcreate(kd
, "private");
467 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
468 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
469 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
470 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
471 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
472 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
473 dolock(k
, kd
, "private");
478 static void alg_dsaparam(keyopts
*k
)
480 static const char *pl
[] = { "q", "p", "g", 0 };
481 if (!copyparam(k
, pl
)) {
487 key_data
*kd
= &k
->k
->k
;
490 /* --- Choose appropriate bit lengths if necessary --- */
497 /* --- Allocate a seed block --- */
499 sz
= (k
->qbits
+ 7) >> 3;
501 rand_get(RAND_GLOBAL
, p
, sz
);
503 /* --- Allocate the parameters --- */
505 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
506 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
507 die(EXIT_FAILURE
, "DSA parameter generation failed");
509 /* --- Store the parameters --- */
512 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
513 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
514 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
519 /* --- Store the seed for future verification --- */
524 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
525 base64_encode(&c
, 0, 0, &d
);
526 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
528 dstr_putf(&d
, "%u", ds
.count
);
529 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
536 static void alg_dsa(keyopts
*k
)
541 key_data
*kd
= &k
->k
->k
;
543 /* --- Get the shared parameters --- */
550 /* --- Choose a private key --- */
552 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
553 mpmont_create(&mm
, p
);
554 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
556 /* --- Store everything away --- */
558 mpkey(kd
, "y", y
, KCAT_PUB
);
560 kd
= key_structcreate(kd
, "private");
562 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
563 dolock(k
, kd
, "private");
565 mp_drop(x
); mp_drop(y
);
568 static void alg_dhparam(keyopts
*k
)
570 static const char *pl
[] = { "p", "q", "g", 0 };
571 if (!copyparam(k
, pl
)) {
573 key_data
*kd
= &k
->k
->k
;
579 /* --- Choose a large safe prime number --- */
581 if (k
->f
& f_limlee
) {
586 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
587 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
588 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
589 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
593 for (i
= 0; i
< nf
; i
++) {
596 mp_writedstr(f
[i
], &d
, 10);
599 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
603 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
604 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
607 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
610 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
611 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
612 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
619 static void alg_dh(keyopts
*k
)
624 key_data
*kd
= &k
->k
->k
;
626 /* --- Get the shared parameters --- */
633 /* --- Choose a suitable private key --- *
635 * Since %$g$% has order %$q$%, choose %$x < q$%.
638 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
640 /* --- Compute the public key %$y = g^x \bmod p$% --- */
642 mpmont_create(&mm
, p
);
643 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
646 /* --- Store everything away --- */
648 mpkey(kd
, "y", y
, KCAT_PUB
);
650 kd
= key_structcreate(kd
, "private");
652 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
653 dolock(k
, kd
, "private");
655 mp_drop(x
); mp_drop(y
);
658 static void alg_bbs(keyopts
*k
)
663 /* --- Sanity checking --- */
666 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
670 /* --- Generate the BBS parameters --- */
672 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
673 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
674 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
676 /* --- Allrighty then --- */
680 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
682 kd
= key_structcreate(kd
, "private");
684 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
685 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
686 dolock(k
, kd
, "private");
691 /* --- The algorithm tables --- */
693 typedef struct keyalg
{
695 void (*proc
)(keyopts
*o
);
699 static keyalg algtab
[] = {
700 { "binary", alg_binary
, "Plain binary data" },
701 { "des", alg_des
, "Binary with DES-style parity" },
702 { "rsa", alg_rsa
, "RSA public-key encryption" },
703 { "dsa", alg_dsa
, "DSA digital signatures" },
704 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
705 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
706 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
707 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
711 /* --- @cmd_add@ --- */
713 static int cmd_add(int argc
, char *argv
[])
716 time_t exp
= KEXP_EXPIRE
;
717 const char *tag
= 0, *ptag
= 0;
719 keyalg
*alg
= algtab
;
720 const char *rtag
= 0;
721 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
723 /* --- Parse options for the subcommand --- */
726 static struct option opt
[] = {
727 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
728 { "bits", OPTF_ARGREQ
, 0, 'b' },
729 { "qbits", OPTF_ARGREQ
, 0, 'B' },
730 { "parameters", OPTF_ARGREQ
, 0, 'p' },
731 { "expire", OPTF_ARGREQ
, 0, 'e' },
732 { "comment", OPTF_ARGREQ
, 0, 'c' },
733 { "tag", OPTF_ARGREQ
, 0, 't' },
734 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
735 { "lock", 0, 0, 'l' },
736 { "quiet", 0, 0, 'q' },
737 { "lim-lee", 0, 0, 'L' },
738 { "subgroup", 0, 0, 'S' },
741 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:lqrLS", opt
, 0, 0, 0);
745 /* --- Handle the various options --- */
749 /* --- Read an algorithm name --- */
753 size_t sz
= strlen(optarg
);
755 if (strcmp(optarg
, "list") == 0) {
756 for (a
= algtab
; a
->name
; a
++)
757 printf("%-10s %s\n", a
->name
, a
->help
);
762 for (a
= algtab
; a
->name
; a
++) {
763 if (strncmp(optarg
, a
->name
, sz
) == 0) {
764 if (a
->name
[sz
] == 0) {
768 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
774 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
777 /* --- Bits must be nonzero and a multiple of 8 --- */
781 k
.bits
= strtoul(optarg
, &p
, 0);
782 if (k
.bits
== 0 || *p
!= 0)
783 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
788 k
.qbits
= strtoul(optarg
, &p
, 0);
789 if (k
.qbits
== 0 || *p
!= 0)
790 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
793 /* --- Parameter selection --- */
799 /* --- Expiry dates get passed to @get_date@ for parsing --- */
802 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
805 exp
= get_date(optarg
, 0);
807 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
811 /* --- Store comments without interpretation --- */
814 if (key_chkcomment(optarg
))
815 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
819 /* --- Store tags --- */
822 if (key_chkident(optarg
))
823 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
830 /* --- Other flags --- */
848 /* --- Other things are bogus --- */
856 /* --- Various sorts of bogosity --- */
858 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
860 "Usage: add [options] type [attr...]");
862 if (key_chkident(argv
[optind
]))
863 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
865 /* --- Set up various bits of the state --- */
867 if (exp
== KEXP_EXPIRE
)
868 exp
= time(0) + 14 * 24 * 60 * 60;
870 /* --- Open the file and create the basic key block --- *
872 * Keep on generating keyids until one of them doesn't collide.
875 doopen(&f
, KOPEN_WRITE
);
878 /* --- Key the generator --- */
883 uint32 id
= rand_global
.ops
->word(&rand_global
);
885 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
888 if (err
!= KERR_DUPID
)
889 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
892 /* --- Set various simple attributes --- */
898 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
899 key_settag(&f
, kk
, 0);
901 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
902 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
906 int err
= key_setcomment(&f
, k
.k
, c
);
908 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
911 setattr(&f
, k
.k
, argv
+ optind
+ 1);
913 key_fulltag(k
.k
, &k
.tag
);
915 /* --- Find the parameter key --- */
918 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
919 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
920 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
921 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
924 /* --- Now generate the actual key data --- */
934 /*----- Key listing -------------------------------------------------------*/
936 /* --- Listing options --- */
938 typedef struct listopts
{
939 const char *tfmt
; /* Date format (@strftime@-style) */
940 int v
; /* Verbosity level */
941 unsigned f
; /* Various flags */
942 time_t t
; /* Time now (for key expiry) */
943 key_filter kf
; /* Filter for matching keys */
946 /* --- Listing flags --- */
948 #define f_newline 2u /* Write newline before next entry */
949 #define f_attr 4u /* Written at least one attribute */
950 #define f_utc 8u /* Emit UTC time, not local time */
952 /* --- @showkeydata@ --- *
954 * Arguments: @key_data *k@ = pointer to key to write
955 * @int ind@ = indentation level
956 * @listopts *o@ = listing options
957 * @dstr *d@ = tag string for this subkey
961 * Use: Emits a piece of key data in a human-readable format.
964 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
966 #define INDENT(i) do { \
968 for (_i = 0; _i < (i); _i++) { \
973 switch (k
->e
& KF_ENCMASK
) {
975 /* --- Binary key data --- *
977 * Emit as a simple hex dump.
981 const octet
*p
= k
->u
.k
.k
;
982 const octet
*l
= p
+ k
->u
.k
.sz
;
990 } else if (sz
% 8 == 0)
994 printf("%02x", *p
++);
999 fputs("}\n", stdout
);
1002 /* --- Encrypted data --- *
1004 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1005 * key. Otherwise just say that it's encrypted and move on.
1010 fputs(" encrypted\n", stdout
);
1013 if (key_punlock(d
->buf
, k
, &kd
))
1014 printf(" <failed to unlock %s>\n", d
->buf
);
1016 fputs(" encrypted", stdout
);
1017 showkeydata(&kd
, ind
, o
, d
);
1023 /* --- Integer keys --- *
1025 * Emit as a large integer in decimal. This makes using the key in
1026 * `calc' or whatever easier.
1031 mp_writefile(k
->u
.m
, stdout
, 10);
1035 /* --- Structured keys --- *
1037 * Just iterate over the subkeys.
1045 fputs(" {\n", stdout
);
1046 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1047 if (!key_match(&ks
->k
, &o
->kf
))
1050 printf("%s =", SYM_NAME(ks
));
1052 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1053 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1056 fputs("}\n", stdout
);
1063 /* --- @showkey@ --- *
1065 * Arguments: @key *k@ = pointer to key to show
1066 * @listopts *o@ = pointer to listing options
1070 * Use: Emits a listing of a particular key.
1073 static void showkey(key
*k
, listopts
*o
)
1075 char ebuf
[24], dbuf
[24];
1078 /* --- Skip the key if the filter doesn't match --- */
1080 if (!key_match(&k
->k
, &o
->kf
))
1083 /* --- Sort out the expiry and deletion times --- */
1085 if (KEY_EXPIRED(o
->t
, k
->exp
))
1086 strcpy(ebuf
, "expired");
1087 else if (k
->exp
== KEXP_FOREVER
)
1088 strcpy(ebuf
, "forever");
1090 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1091 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1094 if (KEY_EXPIRED(o
->t
, k
->del
))
1095 strcpy(dbuf
, "deleted");
1096 else if (k
->del
== KEXP_FOREVER
)
1097 strcpy(dbuf
, "forever");
1099 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1100 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1103 /* --- If in compact format, just display and quit --- */
1106 if (!(o
->f
& f_newline
)) {
1107 printf("%8s %-20s %-20s %-10s %-10s\n",
1108 "Id", "Tag", "Type", "Expire", "Delete");
1110 printf("%08lx %-20s %-20s %-10s %-10s\n",
1111 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1112 k
->type
, ebuf
, dbuf
);
1117 /* --- Display the standard header --- */
1119 if (o
->f
& f_newline
)
1120 fputc('\n', stdout
);
1121 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1122 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1123 printf("type: %s\n", k
->type
);
1124 printf("expiry: %s\n", ebuf
);
1125 printf("delete: %s\n", dbuf
);
1126 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1128 /* --- Display the attributes --- */
1132 const char *av
, *an
;
1135 printf("attributes:");
1136 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1137 printf("\n %s = %s", an
, av
);
1141 fputc('\n', stdout
);
1146 /* --- If dumping requested, dump the raw key data --- */
1150 fputs("key:", stdout
);
1152 showkeydata(&k
->k
, 0, o
, &d
);
1159 /* --- @cmd_list@ --- */
1161 static int cmd_list(int argc
, char *argv
[])
1165 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1167 /* --- Parse subcommand options --- */
1170 static struct option opt
[] = {
1171 { "quiet", 0, 0, 'q' },
1172 { "verbose", 0, 0, 'v' },
1173 { "utc", 0, 0, 'u' },
1174 { "filter", OPTF_ARGREQ
, 0, 'f' },
1177 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1194 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1196 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1205 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1207 /* --- Open the key file --- */
1209 doopen(&f
, KOPEN_READ
);
1212 /* --- Set up the time format --- */
1215 o
.tfmt
= "%Y-%m-%d";
1216 else if (o
.f
& f_utc
)
1217 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1219 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1221 /* --- If specific keys were requested use them, otherwise do all --- *
1223 * Some day, this might turn into a wildcard match.
1226 if (optind
< argc
) {
1228 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1231 moan("key `%s' not found", argv
[optind
]);
1235 } while (optind
< argc
);
1238 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1246 return (EXIT_FAILURE
);
1251 /*----- Command implementation --------------------------------------------*/
1253 /* --- @cmd_expire@ --- */
1255 static int cmd_expire(int argc
, char *argv
[])
1263 die(EXIT_FAILURE
, "Usage: expire tag...");
1264 doopen(&f
, KOPEN_WRITE
);
1265 for (i
= 1; i
< argc
; i
++) {
1266 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1269 moan("key `%s' not found", argv
[i
]);
1277 /* --- @cmd_delete@ --- */
1279 static int cmd_delete(int argc
, char *argv
[])
1287 die(EXIT_FAILURE
, "Usage: delete tag...");
1288 doopen(&f
, KOPEN_WRITE
);
1289 for (i
= 1; i
< argc
; i
++) {
1290 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1293 moan("key `%s' not found", argv
[i
]);
1301 /* --- @cmd_setattr@ --- */
1303 static int cmd_setattr(int argc
, char *argv
[])
1309 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1310 doopen(&f
, KOPEN_WRITE
);
1311 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1312 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1313 setattr(&f
, k
, argv
+ 2);
1318 /* --- @cmd_finger@ --- */
1320 static void fingerprint(key
*k
, const key_filter
*kf
)
1323 octet hash
[RMD160_HASHSZ
];
1327 if (!key_encode(&k
->k
, &d
, kf
))
1330 rmd160_hash(&r
, d
.buf
, d
.len
);
1331 rmd160_done(&r
, hash
);
1335 for (i
= 0; i
< sizeof(hash
); i
++) {
1336 if (i
&& i
% 4 == 0)
1338 printf("%02x", hash
[i
]);
1340 printf(" %s\n", d
.buf
);
1344 static int cmd_finger(int argc
, char *argv
[])
1348 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1351 static struct option opt
[] = {
1352 { "filter", OPTF_ARGREQ
, 0, 'f' },
1355 int i
= mdwopt(argc
, argv
, "+f:", opt
, 0, 0, 0);
1361 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1363 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1371 argv
+= optind
; argc
-= optind
;
1373 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1375 doopen(&f
, KOPEN_READ
);
1379 for (i
= 0; i
< argc
; i
++) {
1380 key
*k
= key_bytag(&f
, argv
[i
]);
1382 fingerprint(k
, &kf
);
1385 moan("key `%s' not found", argv
[i
]);
1391 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1392 fingerprint(k
, &kf
);
1397 /* --- @cmd_comment@ --- */
1399 static int cmd_comment(int argc
, char *argv
[])
1405 if (argc
< 2 || argc
> 3)
1406 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1407 doopen(&f
, KOPEN_WRITE
);
1408 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1409 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1410 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1411 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1416 /* --- @cmd_tag@ --- */
1418 static int cmd_tag(int argc
, char *argv
[])
1427 static struct option opt
[] = {
1428 { "retag", 0, 0, 'r' },
1431 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1444 argv
+= optind
; argc
-= optind
;
1445 if (argc
< 1 || argc
> 2 || rc
)
1446 die(EXIT_FAILURE
, "Usage: tag [-r] tag [new-tag]");
1447 doopen(&f
, KOPEN_WRITE
);
1448 if (flags
& f_retag
) {
1449 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1450 key_settag(&f
, k
, 0);
1452 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1453 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1454 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1455 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1460 /* --- @cmd_lock@ --- */
1462 static int cmd_lock(int argc
, char *argv
[])
1470 die(EXIT_FAILURE
, "Usage: lock qtag");
1471 doopen(&f
, KOPEN_WRITE
);
1472 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1473 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1474 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1475 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1476 if (key_plock(d
.buf
, kd
, kd
))
1477 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1483 /* --- @cmd_unlock@ --- */
1485 static int cmd_unlock(int argc
, char *argv
[])
1493 die(EXIT_FAILURE
, "Usage: unlock qtag");
1494 doopen(&f
, KOPEN_WRITE
);
1495 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1496 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1497 if (kd
->e
!= KENC_ENCRYPT
)
1498 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1499 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1500 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1506 /* --- @cmd_extract@ --- */
1508 static int cmd_extract(int argc
, char *argv
[])
1514 key_filter kf
= { 0, 0 };
1518 static struct option opt
[] = {
1519 { "filter", OPTF_ARGREQ
, 0, 'f' },
1522 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1528 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1530 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1538 argv
+= optind
; argc
-= optind
;
1540 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1541 if (strcmp(*argv
, "-") == 0)
1543 else if (!(fp
= fopen(*argv
, "w"))) {
1544 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1545 *argv
, strerror(errno
));
1548 doopen(&f
, KOPEN_READ
);
1552 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1553 key_extract(&f
, k
, fp
, &kf
);
1555 for (i
= 1; i
< argc
; i
++) {
1556 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1557 key_extract(&f
, k
, fp
, &kf
);
1559 moan("key `%s' not found", argv
[i
]);
1565 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1570 /* --- @cmd_tidy@ --- */
1572 static int cmd_tidy(int argc
, char *argv
[])
1576 die(EXIT_FAILURE
, "usage: tidy");
1577 doopen(&f
, KOPEN_WRITE
);
1578 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1583 /* --- @cmd_merge@ --- */
1585 static int cmd_merge(int argc
, char *argv
[])
1591 die(EXIT_FAILURE
, "Usage: merge file");
1592 if (strcmp(argv
[1], "-") == 0)
1594 else if (!(fp
= fopen(argv
[1], "r"))) {
1595 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1596 argv
[1], strerror(errno
));
1599 doopen(&f
, KOPEN_WRITE
);
1600 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1605 /*----- Main command table ------------------------------------------------*/
1609 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1614 "add [options] type [attr...]\n\
1615 Options: [-lqrLS] [-a alg] [-bB bits] [-p param] [-R tag]\n\
1616 [-e expire] [-t tag] [-c comment]", "\
1619 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
1620 -b, --bits=N Generate an N-bit key.\n\
1621 -B, --qbits=N Use an N-bit subgroup or factors.\n\
1622 -p, --parameters=TAG Get group parameters from TAG.\n\
1623 -e, --expire=TIME Make the key expire after TIME.\n\
1624 -c, --comment=STRING Attach the command STRING to the key.\n\
1625 -t, --tag=TAG Tag the key with the name TAG.\n\
1626 -r, --retag Untag any key currently with that tag.\n\
1627 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
1628 -l, --lock Lock the generated key with a passphrase.\n\
1629 -q, --quiet Don't give progress indicators while working.\n\
1630 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
1631 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
1633 { "expire", cmd_expire
, "expire tag..." },
1634 { "delete", cmd_delete
, "delete tag..." },
1635 { "tag", cmd_tag
, "tag [-r] tag [new-tag]", "\
1638 -r, --retag Untag any key currently called new-tag.\n\
1640 { "setattr", cmd_setattr
, "setattr tag attr..." },
1641 { "comment", cmd_comment
, "comment tag [comment]" },
1642 { "lock", cmd_lock
, "lock qtag" },
1643 { "unlock", cmd_unlock
, "unlock qtag" },
1644 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]", "\
1647 -u, --utc Display expiry times etc. in UTC, not local time.\n\
1648 -q, --quiet Show less information.\n\
1649 -v, --verbose Show more information.\n\
1651 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]", "\
1654 -f, --filter=FILT Only hash key components matching FILT.\n\
1656 { "tidy", cmd_tidy
, "tidy" },
1657 { "extract", cmd_extract
, "extract [-f filter] file [tag...]", "\
1660 -f, --filter=FILT Only extract key components matching FILT.\n\
1662 { "merge", cmd_merge
, "merge file" },
1666 typedef struct cmd cmd
;
1668 /*----- Main code ---------------------------------------------------------*/
1670 /* --- @findcmd@ --- *
1672 * Arguments: @const char *name@ = a command name
1674 * Returns: Pointer to the command structure.
1676 * Use: Looks up a command by name. If the command isn't found, an
1677 * error is reported and the program is terminated.
1680 static cmd
*findcmd(const char *name
)
1682 cmd
*c
, *chosen
= 0;
1683 size_t sz
= strlen(name
);
1685 for (c
= cmds
; c
->name
; c
++) {
1686 if (strncmp(name
, c
->name
, sz
) == 0) {
1687 if (c
->name
[sz
] == 0) {
1691 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
1697 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
1701 /* --- Helpful GNUy functions --- */
1703 void usage(FILE *fp
)
1705 pquis(fp
, "Usage: $ [-k keyring] command [args]\n");
1708 void version(FILE *fp
)
1710 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1713 void help(FILE *fp
, char **argv
)
1721 fprintf(fp
, "Usage: %s [-k keyring] %s\n", QUIS
, c
->usage
);
1731 Performs various simple key management operations. Command line options\n\
1734 -h, --help [COMMAND] Display this help text (or help for COMMAND).\n\
1735 -v, --version Display version number.\n\
1736 -u, --usage Display short usage summary.\n\
1738 -k, --keyring=FILE Read and write keys in FILE.\n\
1739 -i, --id=TAG Use key TAG for random number generator.\n\
1740 -t, --type=TYPE Use key TYPE for random number generator.\n\
1742 The following commands are understood:\n\n",
1744 for (c
= cmds
; c
->name
; c
++)
1745 fprintf(fp
, "%s\n", c
->usage
);
1751 * Arguments: @int argc@ = number of command line arguments
1752 * @char *argv[]@ = array of command line arguments
1754 * Returns: Nonzero on failure.
1756 * Use: Main program. Performs simple key management functions.
1759 int main(int argc
, char *argv
[])
1765 /* --- Initialization --- */
1770 /* --- Parse command line options --- */
1773 static struct option opt
[] = {
1775 /* --- Standard GNUy help options --- */
1777 { "help", 0, 0, 'h' },
1778 { "version", 0, 0, 'v' },
1779 { "usage", 0, 0, 'u' },
1781 /* --- Real live useful options --- */
1783 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1785 /* --- Magic terminator --- */
1789 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
1795 /* --- GNU help options --- */
1798 help(stdout
, argv
+ optind
);
1807 /* --- Real useful options --- */
1813 /* --- Bogosity --- */
1821 /* --- Complain about excessive bogons --- */
1823 if (f
& f_bogus
|| optind
== argc
) {
1828 /* --- Initialize the Catacomb random number generator --- */
1830 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1831 rand_seed(RAND_GLOBAL
, 160);
1833 /* --- Dispatch to appropriate command handler --- */
1838 return (findcmd(argv
[0])->cmd(argc
, argv
));
1841 /*----- That's all, folks -------------------------------------------------*/