3 * $Id: keyutil.c,v 1.12 2001/02/03 11:58:22 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.12 2001/02/03 11:58:22 mdw
34 * Store the correct seed information and count for DSA keys now that it's
37 * Revision 1.11 2000/12/06 20:33:27 mdw
38 * Make flags be macros rather than enumerations, to ensure that they're
41 * Revision 1.10 2000/10/08 12:02:21 mdw
42 * Use @MP_EQ@ instead of @MP_CMP@.
44 * Revision 1.9 2000/08/15 21:40:49 mdw
45 * Minor formatting change in listing attributes.
47 * Revision 1.8 2000/07/29 09:59:13 mdw
48 * Support Lim-Lee primes in Diffie-Hellman parameter generation.
50 * Revision 1.7 2000/07/01 11:18:51 mdw
51 * Use new interfaces for key manipulation.
53 * Revision 1.6 2000/06/17 11:28:22 mdw
54 * Use secure memory interface from MP library. `rand_getgood' is
57 * Revision 1.5 2000/02/12 18:21:03 mdw
58 * Overhaul of key management (again).
60 * Revision 1.4 1999/12/22 15:48:10 mdw
61 * Track new key-management changes. Support new key generation
64 * Revision 1.3 1999/11/02 15:23:24 mdw
65 * Fix newlines in keyring list.
67 * Revision 1.2 1999/10/15 21:05:28 mdw
68 * In `key list', show timezone for local times, and support `-u' option
71 * Revision 1.1 1999/09/03 08:41:12 mdw
76 /*----- Header files ------------------------------------------------------*/
86 #include <mLib/base64.h>
87 #include <mLib/mdwopt.h>
88 #include <mLib/quis.h>
89 #include <mLib/report.h>
108 /*----- Handy global state ------------------------------------------------*/
110 static const char *keyfile
= "keyring";
112 /*----- Useful shared functions -------------------------------------------*/
114 /* --- @doopen@ --- *
116 * Arguments: @key_file *f@ = pointer to key file block
117 * @unsigned how@ = method to open file with
121 * Use: Opens a key file and handles errors by panicking
125 static void doopen(key_file
*f
, unsigned how
)
127 if (key_open(f
, keyfile
, how
, key_moan
, 0))
128 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
131 /* --- @doclose@ --- *
133 * Arguments: @key_file *f@ = pointer to key file block
137 * Use: Closes a key file and handles errors by panicking
141 static void doclose(key_file
*f
)
143 switch (key_close(f
)) {
145 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
146 keyfile
, strerror(errno
));
148 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
149 keyfile
, strerror(errno
));
153 /* --- @setattr@ --- *
155 * Arguments: @key_file *f@ = pointer to key file block
156 * @key *k@ = pointer to key block
157 * @char *v[]@ = array of assignments (overwritten!)
161 * Use: Applies the attribute assignments to the key.
164 static void setattr(key_file
*f
, key
*k
, char *v
[])
169 size_t eq
= strcspn(p
, "=");
171 moan("invalid assignment: `%s'", p
);
174 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
175 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
180 /*----- Key generation ----------------------------------------------------*/
182 /* --- Key generation parameters --- */
184 typedef struct keyopts
{
185 key_file
*kf
; /* Pointer to key file */
186 key
*k
; /* Pointer to the actual key */
187 dstr tag
; /* Full tag name for the key */
188 unsigned f
; /* Flags for the new key */
189 unsigned bits
, qbits
; /* Bit length for the new key */
190 key
*p
; /* Parameters key-data */
193 #define f_bogus 1u /* Error in parsing */
194 #define f_lock 2u /* Passphrase-lock private key */
195 #define f_quiet 4u /* Don't show a progress indicator */
196 #define f_limlee 8u /* Generate Lim-Lee primes */
197 #define f_subgroup 16u /* Generate a subgroup */
199 /* --- @dolock@ --- *
201 * Arguments: @keyopts *k@ = key generation options
202 * @key_data *kd@ = pointer to key data to lock
203 * @const char *t@ = tag suffix or null
207 * Use: Does passphrase locking on new keys.
210 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
212 if (!(k
->f
& f_lock
))
215 dstr_putf(&k
->tag
, ".%s", t
);
216 if (key_plock(k
->tag
.buf
, kd
, kd
))
217 die(EXIT_FAILURE
, "couldn't lock key");
222 * Arguments: @key_data *kd@ = pointer to parent key block
223 * @const char *tag@ = pointer to tag string
224 * @mp *m@ = integer to store
225 * @unsigned f@ = flags to set
229 * Use: Sets a multiprecision integer subkey.
232 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
234 key_data
*kkd
= key_structcreate(kd
, tag
);
239 /* --- @copyparam@ --- *
241 * Arguments: @keyopts *k@ = pointer to key options
242 * @const char **pp@ = checklist of parameters
244 * Returns: Nonzero if parameters copied; zero if you have to generate
247 * Use: Copies parameters from a source key to the current one.
250 static int copyparam(keyopts
*k
, const char **pp
)
254 /* --- Quick check if no parameters supplied --- */
259 /* --- Run through the checklist --- */
262 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
264 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
265 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
266 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
270 /* --- Copy over the parameters --- */
274 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
275 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
281 * Arguments: @key_data *k@ = pointer to key data block
282 * @const char *tag@ = tag string to use
284 * Returns: Pointer to multiprecision integer key item.
286 * Use: Fetches an MP key component.
289 static mp
*getmp(key_data
*k
, const char *tag
)
291 k
= key_structfind(k
, tag
);
293 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
294 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
295 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
299 /* --- @keyrand@ --- *
301 * Arguments: @key_file *kf@ = pointer to key file
302 * @const char *id@ = pointer to key id (or null)
306 * Use: Keys the random number generator.
309 static void keyrand(key_file
*kf
, const char *id
)
313 /* --- Find the key --- */
316 if ((k
= key_bytag(kf
, id
)) == 0)
317 die(EXIT_FAILURE
, "key `%s' not found", id
);
319 k
= key_bytype(kf
, "catacomb-rand");
322 key_data
*kd
= &k
->k
, kkd
;
325 switch (kd
->e
& KF_ENCMASK
) {
331 if (key_punlock(d
.buf
, kd
, &kkd
))
332 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
339 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
343 /* --- Key the generator --- */
345 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
351 /* --- Key generation algorithms --- */
353 static void alg_binary(keyopts
*k
)
362 die(EXIT_FAILURE
, "no shared parameters for binary keys");
364 sz
= (k
->bits
+ 7) >> 3;
366 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
367 rand_get(RAND_GLOBAL
, p
, sz
);
369 key_binary(&k
->k
->k
, p
, sz
);
370 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
373 dolock(k
, &k
->k
->k
, 0);
376 static void alg_des(keyopts
*k
)
385 die(EXIT_FAILURE
, "no shared parameters for DES keys");
386 if (k
->bits
% 56 || k
->bits
> 168)
387 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
391 rand_get(RAND_GLOBAL
, p
, sz
); /* Too much work done here! */
392 for (i
= 0; i
< sz
; i
++) {
393 octet x
= p
[i
] | 0x01;
397 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
399 key_binary(&k
->k
->k
, p
, sz
);
400 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
403 dolock(k
, &k
->k
->k
, 0);
406 static void alg_rsa(keyopts
*k
)
411 /* --- Sanity checking --- */
414 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
418 /* --- Generate the RSA parameters --- */
420 if (rsa_gen(&rp
, k
->bits
, &rand_global
, 0,
421 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
422 die(EXIT_FAILURE
, "RSA key generation failed");
424 /* --- Run a test encryption --- */
427 grand
*g
= fibrand_create(rand_global
.ops
->word(&rand_global
));
429 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
434 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
435 c
= rsa_qprivop(&rp
, c
, c
, g
);
438 die(EXIT_FAILURE
, "test encryption failed");
444 /* --- Allrighty then --- */
448 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
449 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
451 kd
= key_structcreate(kd
, "private");
453 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
454 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
455 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
456 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
457 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
458 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
459 dolock(k
, kd
, "private");
464 static void alg_dsaparam(keyopts
*k
)
466 static const char *pl
[] = { "q", "p", "g", 0 };
467 if (!copyparam(k
, pl
)) {
473 key_data
*kd
= &k
->k
->k
;
476 /* --- Choose appropriate bit lengths if necessary --- */
483 /* --- Allocate a seed block --- */
485 sz
= (k
->qbits
+ 7) >> 3;
487 rand_get(RAND_GLOBAL
, p
, sz
);
489 /* --- Allocate the parameters --- */
491 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
492 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
493 die(EXIT_FAILURE
, "DSA parameter generation failed");
495 /* --- Store the parameters --- */
498 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
499 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
500 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
505 /* --- Store the seed for future verification --- */
510 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
511 base64_encode(&c
, 0, 0, &d
);
512 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
514 dstr_putf(&d
, "%u", ds
.count
);
515 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
522 static void alg_dsa(keyopts
*k
)
527 key_data
*kd
= &k
->k
->k
;
529 /* --- Get the shared parameters --- */
536 /* --- Choose a private key --- */
538 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
539 mpmont_create(&mm
, p
);
540 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
542 /* --- Store everything away --- */
544 mpkey(kd
, "y", y
, KCAT_PUB
);
546 kd
= key_structcreate(kd
, "private");
548 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
549 dolock(k
, kd
, "private");
551 mp_drop(x
); mp_drop(y
);
554 static void alg_dhparam(keyopts
*k
)
556 static const char *pl
[] = { "p", "q", "g", 0 };
557 if (!copyparam(k
, pl
)) {
559 key_data
*kd
= &k
->k
->k
;
565 /* --- Choose a large safe prime number --- */
567 if (k
->f
& f_limlee
) {
572 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
573 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
574 0, &rand_global
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
575 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
579 for (i
= 0; i
< nf
; i
++) {
582 mp_writedstr(f
[i
], &d
, 10);
585 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
589 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, &rand_global
,
590 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
593 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
596 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
597 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
598 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
605 static void alg_dh(keyopts
*k
)
610 key_data
*kd
= &k
->k
->k
;
612 /* --- Get the shared parameters --- */
619 /* --- Choose a suitable private key --- *
621 * Since %$g$% has order %$q$%, choose %$x < q$%.
624 x
= mprand_range(MP_NEWSEC
, q
, &rand_global
, 0);
626 /* --- Compute the public key %$y = g^x \bmod p$% --- */
628 mpmont_create(&mm
, p
);
629 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
632 /* --- Store everything away --- */
634 mpkey(kd
, "y", y
, KCAT_PUB
);
636 kd
= key_structcreate(kd
, "private");
638 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
639 dolock(k
, kd
, "private");
641 mp_drop(x
); mp_drop(y
);
644 static void alg_bbs(keyopts
*k
)
649 /* --- Sanity checking --- */
652 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
656 /* --- Generate the BBS parameters --- */
658 if (bbs_gen(&bp
, k
->bits
, &rand_global
, 0,
659 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
660 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
662 /* --- Allrighty then --- */
666 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
668 kd
= key_structcreate(kd
, "private");
670 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
671 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
672 dolock(k
, kd
, "private");
677 /* --- The algorithm tables --- */
679 typedef struct keyalg
{
681 void (*proc
)(keyopts
*o
);
685 static keyalg algtab
[] = {
686 { "binary", alg_binary
, "Plain binary data" },
687 { "des", alg_des
, "Binary with DES-style parity" },
688 { "rsa", alg_rsa
, "RSA public-key encryption" },
689 { "dsa", alg_dsa
, "DSA digital signatures" },
690 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
691 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
692 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
693 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
697 /* --- @cmd_add@ --- */
699 static int cmd_add(int argc
, char *argv
[])
702 time_t exp
= KEXP_EXPIRE
;
703 const char *tag
= 0, *ptag
= 0;
705 keyalg
*alg
= algtab
;
706 const char *rtag
= 0;
707 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0 };
709 /* --- Parse options for the subcommand --- */
712 static struct option opt
[] = {
713 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
714 { "bits", OPTF_ARGREQ
, 0, 'b' },
715 { "qbits", OPTF_ARGREQ
, 0, 'B' },
716 { "parameters", OPTF_ARGREQ
, 0, 'p' },
717 { "expire", OPTF_ARGREQ
, 0, 'e' },
718 { "comment", OPTF_ARGREQ
, 0, 'c' },
719 { "tag", OPTF_ARGREQ
, 0, 't' },
720 { "rand-id", OPTF_ARGREQ
, 0, 'r' },
721 { "lock", 0, 0, 'l' },
722 { "quiet", 0, 0, 'q' },
723 { "lim-lee", 0, 0, 'L' },
724 { "subgroup", 0, 0, 'S' },
727 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:r:lqLS", opt
, 0, 0, 0);
731 /* --- Handle the various options --- */
735 /* --- Read an algorithm name --- */
739 size_t sz
= strlen(optarg
);
741 if (strcmp(optarg
, "list") == 0) {
742 for (a
= algtab
; a
->name
; a
++)
743 printf("%-10s %s\n", a
->name
, a
->help
);
748 for (a
= algtab
; a
->name
; a
++) {
749 if (strncmp(optarg
, a
->name
, sz
) == 0) {
750 if (a
->name
[sz
] == 0) {
754 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
760 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
763 /* --- Bits must be nonzero and a multiple of 8 --- */
767 k
.bits
= strtoul(optarg
, &p
, 0);
768 if (k
.bits
== 0 || *p
!= 0)
769 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
774 k
.qbits
= strtoul(optarg
, &p
, 0);
775 if (k
.qbits
== 0 || *p
!= 0)
776 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
779 /* --- Parameter selection --- */
785 /* --- Expiry dates get passed to @get_date@ for parsing --- */
788 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
791 exp
= get_date(optarg
, 0);
793 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
797 /* --- Store comments without interpretation --- */
800 if (key_chkcomment(optarg
))
801 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
805 /* --- Store tags --- */
808 if (key_chkident(optarg
))
809 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
813 /* --- Other flags --- */
831 /* --- Other things are bogus --- */
839 /* --- Various sorts of bogosity --- */
841 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
843 "Usage: add [options] type [attr...]");
845 if (key_chkident(argv
[optind
]))
846 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
848 /* --- Set up various bits of the state --- */
850 if (exp
== KEXP_EXPIRE
)
851 exp
= time(0) + 14 * 24 * 60 * 60;
853 /* --- Open the file and create the basic key block --- *
855 * Keep on generating keyids until one of them doesn't collide.
858 doopen(&f
, KOPEN_WRITE
);
861 /* --- Key the generator --- */
866 uint32 id
= rand_global
.ops
->word(&rand_global
);
868 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
871 if (err
!= KERR_DUPID
)
872 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
875 /* --- Set various simple attributes --- */
878 int err
= key_settag(&f
, k
.k
, tag
);
880 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
884 int err
= key_setcomment(&f
, k
.k
, c
);
886 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
889 setattr(&f
, k
.k
, argv
+ optind
+ 1);
891 key_fulltag(k
.k
, &k
.tag
);
893 /* --- Find the parameter key --- */
896 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
897 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
898 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
899 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
902 /* --- Now generate the actual key data --- */
912 /*----- Key listing -------------------------------------------------------*/
914 /* --- Listing options --- */
916 typedef struct listopts
{
917 const char *tfmt
; /* Date format (@strftime@-style) */
918 int v
; /* Verbosity level */
919 unsigned f
; /* Various flags */
920 time_t t
; /* Time now (for key expiry) */
921 key_filter kf
; /* Filter for matching keys */
924 /* --- Listing flags --- */
926 #define f_newline 2u /* Write newline before next entry */
927 #define f_attr 4u /* Written at least one attribute */
928 #define f_utc 8u /* Emit UTC time, not local time */
930 /* --- @showkeydata@ --- *
932 * Arguments: @key_data *k@ = pointer to key to write
933 * @int ind@ = indentation level
934 * @listopts *o@ = listing options
935 * @dstr *d@ = tag string for this subkey
939 * Use: Emits a piece of key data in a human-readable format.
942 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
944 #define INDENT(i) do { \
946 for (_i = 0; _i < (i); _i++) { \
951 switch (k
->e
& KF_ENCMASK
) {
953 /* --- Binary key data --- *
955 * Emit as a simple hex dump.
959 const octet
*p
= k
->u
.k
.k
;
960 const octet
*l
= p
+ k
->u
.k
.sz
;
968 } else if (sz
% 8 == 0)
972 printf("%02x", *p
++);
977 fputs("}\n", stdout
);
980 /* --- Encrypted data --- *
982 * If the user is sufficiently keen, ask for a passphrase and decrypt the
983 * key. Otherwise just say that it's encrypted and move on.
988 fputs(" encrypted\n", stdout
);
991 if (key_punlock(d
->buf
, k
, &kd
))
992 printf(" <failed to unlock %s>\n", d
->buf
);
994 fputs(" encrypted", stdout
);
995 showkeydata(&kd
, ind
, o
, d
);
1001 /* --- Integer keys --- *
1003 * Emit as a large integer in decimal. This makes using the key in
1004 * `calc' or whatever easier.
1009 mp_writefile(k
->u
.m
, stdout
, 10);
1013 /* --- Structured keys --- *
1015 * Just iterate over the subkeys.
1023 fputs(" {\n", stdout
);
1024 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1025 if (!key_match(&ks
->k
, &o
->kf
))
1028 printf("%s =", SYM_NAME(ks
));
1030 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1031 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1034 fputs("}\n", stdout
);
1041 /* --- @showkey@ --- *
1043 * Arguments: @key *k@ = pointer to key to show
1044 * @listopts *o@ = pointer to listing options
1048 * Use: Emits a listing of a particular key.
1051 static void showkey(key
*k
, listopts
*o
)
1053 char ebuf
[24], dbuf
[24];
1056 /* --- Skip the key if the filter doesn't match --- */
1058 if (!key_match(&k
->k
, &o
->kf
))
1061 /* --- Sort out the expiry and deletion times --- */
1063 if (KEY_EXPIRED(o
->t
, k
->exp
))
1064 strcpy(ebuf
, "expired");
1065 else if (k
->exp
== KEXP_FOREVER
)
1066 strcpy(ebuf
, "forever");
1068 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1069 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1072 if (KEY_EXPIRED(o
->t
, k
->del
))
1073 strcpy(dbuf
, "deleted");
1074 else if (k
->del
== KEXP_FOREVER
)
1075 strcpy(dbuf
, "forever");
1077 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1078 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1081 /* --- If in compact format, just display and quit --- */
1084 if (!(o
->f
& f_newline
)) {
1085 printf("%8s %-20s %-20s %-10s %-10s\n",
1086 "Id", "Tag", "Type", "Expire", "Delete");
1088 printf("%08lx %-20s %-20s %-10s %-10s\n",
1089 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1090 k
->type
, ebuf
, dbuf
);
1095 /* --- Display the standard header --- */
1097 if (o
->f
& f_newline
)
1098 fputc('\n', stdout
);
1099 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1100 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1101 printf("type: %s\n", k
->type
);
1102 printf("expiry: %s\n", ebuf
);
1103 printf("delete: %s\n", dbuf
);
1104 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1106 /* --- Display the attributes --- */
1110 const char *av
, *an
;
1113 printf("attributes:");
1114 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1115 printf("\n %s = %s", an
, av
);
1119 fputc('\n', stdout
);
1124 /* --- If dumping requested, dump the raw key data --- */
1128 fputs("key:", stdout
);
1130 showkeydata(&k
->k
, 0, o
, &d
);
1137 /* --- @cmd_list@ --- */
1139 static int cmd_list(int argc
, char *argv
[])
1143 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1145 /* --- Parse subcommand options --- */
1148 static struct option opt
[] = {
1149 { "quiet", 0, 0, 'q' },
1150 { "verbose", 0, 0, 'v' },
1151 { "utc", 0, 0, 'u' },
1152 { "filter", OPTF_ARGREQ
, 0, 'f' },
1155 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1172 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1174 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1183 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f filter] [tag...]");
1185 /* --- Open the key file --- */
1187 doopen(&f
, KOPEN_READ
);
1190 /* --- Set up the time format --- */
1193 o
.tfmt
= "%Y-%m-%d";
1194 else if (o
.f
& f_utc
)
1195 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1197 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1199 /* --- If specific keys were requested use them, otherwise do all --- *
1201 * Some day, this might turn into a wildcard match.
1204 if (optind
< argc
) {
1206 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1209 moan("key `%s' not found", argv
[optind
]);
1213 } while (optind
< argc
);
1216 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1224 return (EXIT_FAILURE
);
1229 /*----- Command implementation --------------------------------------------*/
1231 /* --- @cmd_expire@ --- */
1233 static int cmd_expire(int argc
, char *argv
[])
1241 die(EXIT_FAILURE
, "Usage: expire tag...");
1242 doopen(&f
, KOPEN_WRITE
);
1243 for (i
= 1; i
< argc
; i
++) {
1244 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1247 moan("key `%s' not found", argv
[i
]);
1255 /* --- @cmd_delete@ --- */
1257 static int cmd_delete(int argc
, char *argv
[])
1265 die(EXIT_FAILURE
, "Usage: delete tag...");
1266 doopen(&f
, KOPEN_WRITE
);
1267 for (i
= 1; i
< argc
; i
++) {
1268 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1271 moan("key `%s' not found", argv
[i
]);
1279 /* --- @cmd_setattr@ --- */
1281 static int cmd_setattr(int argc
, char *argv
[])
1287 die(EXIT_FAILURE
, "Usage: setattr tag attr...");
1288 doopen(&f
, KOPEN_WRITE
);
1289 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1290 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1291 setattr(&f
, k
, argv
+ 2);
1296 /* --- @cmd_finger@ --- */
1298 static void fingerprint(key
*k
, const key_filter
*kf
)
1301 octet hash
[RMD160_HASHSZ
];
1305 if (!key_encode(&k
->k
, &d
, kf
))
1308 rmd160_hash(&r
, d
.buf
, d
.len
);
1309 rmd160_done(&r
, hash
);
1313 for (i
= 0; i
< sizeof(hash
); i
++) {
1314 if (i
&& i
% 4 == 0)
1316 printf("%02x", hash
[i
]);
1318 printf(" %s\n", d
.buf
);
1322 static int cmd_finger(int argc
, char *argv
[])
1326 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1329 static struct option opt
[] = {
1330 { "filter", OPTF_ARGREQ
, 0, 'f' },
1333 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1339 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1341 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1349 argv
+= optind
; argc
-= optind
;
1351 die(EXIT_FAILURE
, "Usage: fingerprint [-f filter] [tag...]");
1353 doopen(&f
, KOPEN_READ
);
1357 for (i
= 0; i
< argc
; i
++) {
1358 key
*k
= key_bytag(&f
, argv
[i
]);
1360 fingerprint(k
, &kf
);
1363 moan("key `%s' not found", argv
[i
]);
1369 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1370 fingerprint(k
, &kf
);
1375 /* --- @cmd_comment@ --- */
1377 static int cmd_comment(int argc
, char *argv
[])
1383 if (argc
< 2 || argc
> 3)
1384 die(EXIT_FAILURE
, "Usage: comment tag [comment]");
1385 doopen(&f
, KOPEN_WRITE
);
1386 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1387 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1388 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1389 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1394 /* --- @cmd_tag@ --- */
1396 static int cmd_tag(int argc
, char *argv
[])
1402 if (argc
< 2 || argc
> 3)
1403 die(EXIT_FAILURE
, "Usage: tag tag [new-tag]");
1404 doopen(&f
, KOPEN_WRITE
);
1405 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1406 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1407 if ((err
= key_settag(&f
, k
, argv
[2])) != 0)
1408 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[2], key_strerror(err
));
1413 /* --- @cmd_lock@ --- */
1415 static int cmd_lock(int argc
, char *argv
[])
1423 die(EXIT_FAILURE
, "Usage: lock qtag");
1424 doopen(&f
, KOPEN_WRITE
);
1425 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1426 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1427 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1428 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1429 if (key_plock(d
.buf
, kd
, kd
))
1430 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1436 /* --- @cmd_unlock@ --- */
1438 static int cmd_unlock(int argc
, char *argv
[])
1446 die(EXIT_FAILURE
, "Usage: unlock qtag");
1447 doopen(&f
, KOPEN_WRITE
);
1448 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1449 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1450 if (kd
->e
!= KENC_ENCRYPT
)
1451 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1452 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1453 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1459 /* --- @cmd_extract@ --- */
1461 static int cmd_extract(int argc
, char *argv
[])
1467 key_filter kf
= { 0, 0 };
1471 static struct option opt
[] = {
1472 { "filter", OPTF_ARGREQ
, 0, 'f' },
1475 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1481 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1483 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1491 argv
+= optind
; argc
-= optind
;
1493 die(EXIT_FAILURE
, "Usage: extract [-f filter] file [tag...]");
1494 if (strcmp(*argv
, "-") == 0)
1496 else if (!(fp
= fopen(*argv
, "w"))) {
1497 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1498 *argv
, strerror(errno
));
1501 doopen(&f
, KOPEN_READ
);
1505 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1506 key_extract(&f
, k
, fp
, &kf
);
1508 for (i
= 1; i
< argc
; i
++) {
1509 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1510 key_extract(&f
, k
, fp
, &kf
);
1512 moan("key `%s' not found", argv
[i
]);
1518 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1523 /* --- @cmd_tidy@ --- */
1525 static int cmd_tidy(int argc
, char *argv
[])
1529 die(EXIT_FAILURE
, "usage: tidy");
1530 doopen(&f
, KOPEN_WRITE
);
1531 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1536 /* --- @cmd_merge@ --- */
1538 static int cmd_merge(int argc
, char *argv
[])
1544 die(EXIT_FAILURE
, "Usage: merge file");
1545 if (strcmp(argv
[1], "-") == 0)
1547 else if (!(fp
= fopen(argv
[1], "r"))) {
1548 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1549 argv
[1], strerror(errno
));
1552 doopen(&f
, KOPEN_WRITE
);
1553 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
1558 /*----- Main command table ------------------------------------------------*/
1562 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
1566 "add [options] type [attr...]\n\
1567 Options: [-lqLS] [-a alg] [-b|-B bits] [-p param] [-r tag]\n\
1568 [-e expire] [-t tag] [-c comment]"
1570 { "expire", cmd_expire
, "expire tag..." },
1571 { "delete", cmd_delete
, "delete tag..." },
1572 { "tag", cmd_tag
, "tag tag [new-tag]" },
1573 { "setattr", cmd_setattr
, "setattr tag attr..." },
1574 { "comment", cmd_comment
, "comment tag [comment]" },
1575 { "lock", cmd_lock
, "lock qtag" },
1576 { "unlock", cmd_unlock
, "unlock qtag" },
1577 { "list", cmd_list
, "list [-uqv] [-f filter] [tag...]" },
1578 { "fingerprint", cmd_finger
, "fingerprint [-f filter] [tag...]" },
1579 { "tidy", cmd_tidy
, "tidy" },
1580 { "extract", cmd_extract
, "extract [-f filter] file [tag...]" },
1581 { "merge", cmd_merge
, "merge file" },
1585 typedef struct cmd cmd
;
1587 /*----- Main code ---------------------------------------------------------*/
1589 /* --- Helpful GNUy functions --- */
1591 void usage(FILE *fp
)
1593 pquis(fp
, "Usage: $ [-k file] [-i tag] [-t type] command [args]\n");
1596 void version(FILE *fp
)
1598 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1608 Performs various simple key management operations. Command line options\n\
1611 -h, --help Display this help text.\n\
1612 -v, --version Display version number.\n\
1613 -u, --usage Display short usage summary.\n\
1615 -k, --keyring=FILE Read and write keys in FILE.\n\
1616 -i, --id=TAG Use key TAG for random number generator.\n\
1617 -t, --type=TYPE Use key TYPE for random number generator.\n\
1619 The following commands are understood:\n\n",
1621 for (c
= cmds
; c
->name
; c
++)
1622 fprintf(fp
, "%s\n", c
->help
);
1627 * Arguments: @int argc@ = number of command line arguments
1628 * @char *argv[]@ = array of command line arguments
1630 * Returns: Nonzero on failure.
1632 * Use: Main program. Performs simple key management functions.
1635 int main(int argc
, char *argv
[])
1641 /* --- Initialization --- */
1646 /* --- Parse command line options --- */
1649 static struct option opt
[] = {
1651 /* --- Standard GNUy help options --- */
1653 { "help", 0, 0, 'h' },
1654 { "version", 0, 0, 'v' },
1655 { "usage", 0, 0, 'u' },
1657 /* --- Real live useful options --- */
1659 { "keyring", OPTF_ARGREQ
, 0, 'k' },
1660 { "id", OPTF_ARGREQ
, 0, 'i' },
1661 { "type", OPTF_ARGREQ
, 0, 't' },
1663 /* --- Magic terminator --- */
1667 int i
= mdwopt(argc
, argv
, "+hvu k:i:t:", opt
, 0, 0, 0);
1673 /* --- GNU help options --- */
1684 /* --- Real useful options --- */
1690 /* --- Bogosity --- */
1698 /* --- Complain about excessive bogons --- */
1700 if (f
& f_bogus
|| optind
== argc
) {
1705 /* --- Initialize the Catacomb random number generator --- */
1707 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1708 rand_seed(RAND_GLOBAL
, 160);
1710 /* --- Dispatch to appropriate command handler --- */
1717 cmd
*c
, *chosen
= 0;
1718 size_t sz
= strlen(argv
[0]);
1720 for (c
= cmds
; c
->name
; c
++) {
1721 if (strncmp(argv
[0], c
->name
, sz
) == 0) {
1722 if (c
->name
[sz
] == 0) {
1726 die(EXIT_FAILURE
, "ambiguous command name `%s'", argv
[0]);
1732 die(EXIT_FAILURE
, "unknown command name `%s'", argv
[0]);
1733 return (chosen
->cmd(argc
, argv
));
1737 /*----- That's all, folks -------------------------------------------------*/