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 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/base64.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
72 #include "sha256-mgf.h"
73 #include "sha224-mgf.h"
74 #include "sha384-mgf.h"
75 #include "sha512-mgf.h"
76 #include "tiger-mgf.h"
77 #include "rmd128-mgf.h"
78 #include "rmd160-mgf.h"
79 #include "rmd256-mgf.h"
80 #include "rmd320-mgf.h"
84 /*----- Handy global state ------------------------------------------------*/
86 static const char *keyfile
= "keyring";
88 /*----- Useful shared functions -------------------------------------------*/
92 * Arguments: @key_file *f@ = pointer to key file block
93 * @unsigned how@ = method to open file with
97 * Use: Opens a key file and handles errors by panicking
101 static void doopen(key_file
*f
, unsigned how
)
103 if (key_open(f
, keyfile
, how
, key_moan
, 0))
104 die(1, "couldn't open keyring `%s': %s", keyfile
, strerror(errno
));
107 /* --- @doclose@ --- *
109 * Arguments: @key_file *f@ = pointer to key file block
113 * Use: Closes a key file and handles errors by panicking
117 static void doclose(key_file
*f
)
119 switch (key_close(f
)) {
121 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
122 keyfile
, strerror(errno
));
124 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
125 keyfile
, strerror(errno
));
129 /* --- @setattr@ --- *
131 * Arguments: @key_file *f@ = pointer to key file block
132 * @key *k@ = pointer to key block
133 * @char *v[]@ = array of assignments (overwritten!)
137 * Use: Applies the attribute assignments to the key.
140 static void setattr(key_file
*f
, key
*k
, char *v
[])
145 size_t eq
= strcspn(p
, "=");
147 moan("invalid assignment: `%s' (ignored)", p
);
153 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
154 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
159 /*----- Seeding -----------------------------------------------------------*/
161 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
163 { "dsarand", dsarand_create
},
164 { "rmd128-mgf", rmd128_mgfrand
},
165 { "rmd160-mgf", rmd160_mgfrand
},
166 { "rmd256-mgf", rmd256_mgfrand
},
167 { "rmd320-mgf", rmd320_mgfrand
},
168 { "sha-mgf", sha_mgfrand
},
169 { "sha224-mgf", sha224_mgfrand
},
170 { "sha256-mgf", sha256_mgfrand
},
171 { "sha384-mgf", sha384_mgfrand
},
172 { "sha512-mgf", sha512_mgfrand
},
173 { "tiger-mgf", tiger_mgfrand
},
177 #define SEEDALG_DEFAULT (seedtab + 2)
179 /*----- Key generation ----------------------------------------------------*/
181 /* --- Key generation parameters --- */
183 typedef struct keyopts
{
184 key_file
*kf
; /* Pointer to key file */
185 key
*k
; /* Pointer to the actual key */
186 dstr tag
; /* Full tag name for the key */
187 unsigned f
; /* Flags for the new key */
188 unsigned bits
, qbits
; /* Bit length for the new key */
189 const char *curve
; /* Elliptic curve name/info */
190 grand
*r
; /* Random number source */
191 key
*p
; /* Parameters key-data */
194 #define f_bogus 1u /* Error in parsing */
195 #define f_lock 2u /* Passphrase-lock private key */
196 #define f_quiet 4u /* Don't show a progress indicator */
197 #define f_limlee 8u /* Generate Lim-Lee primes */
198 #define f_subgroup 16u /* Generate a subgroup */
199 #define f_retag 32u /* Remove any existing tag */
200 #define f_kcdsa 64u /* Generate KCDSA primes */
202 /* --- @dolock@ --- *
204 * Arguments: @keyopts *k@ = key generation options
205 * @key_data **kd@ = pointer to key data to lock
206 * @const char *t@ = tag suffix or null
210 * Use: Does passphrase locking on new keys.
213 static void dolock(keyopts
*k
, key_data
**kd
, const char *t
)
215 if (!(k
->f
& f_lock
))
218 dstr_putf(&k
->tag
, ".%s", t
);
219 if (key_plock(kd
, 0, k
->tag
.buf
))
220 die(EXIT_FAILURE
, "couldn't lock key");
223 /* --- @copyparam@ --- *
225 * Arguments: @keyopts *k@ = pointer to key options
226 * @const char **pp@ = checklist of parameters
228 * Returns: Nonzero if parameters copied; zero if you have to generate
231 * Use: Copies parameters from a source key to the current one.
234 static int copyparam(keyopts
*k
, const char **pp
)
240 /* --- Quick check if no parameters supplied --- */
245 /* --- Run through the checklist --- */
248 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
250 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
251 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
252 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
256 /* --- Copy over the parameters --- */
260 key_setkeydata(k
->kf
, k
->k
, k
->p
->k
);
262 /* --- Copy over attributes --- */
264 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
265 key_putattr(k
->kf
, k
->k
, n
, v
);
274 * Arguments: @key_data *k@ = pointer to key data block
275 * @const char *tag@ = tag string to use
277 * Returns: Pointer to multiprecision integer key item.
279 * Use: Fetches an MP key component.
282 static mp
*getmp(key_data
*k
, const char *tag
)
284 k
= key_structfind(k
, tag
);
286 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
287 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
288 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
292 /* --- @keyrand@ --- *
294 * Arguments: @key_file *kf@ = pointer to key file
295 * @const char *id@ = pointer to key id (or null)
299 * Use: Keys the random number generator.
302 static void keyrand(key_file
*kf
, const char *id
)
306 /* --- Find the key --- */
309 if ((k
= key_bytag(kf
, id
)) == 0)
310 die(EXIT_FAILURE
, "key `%s' not found", id
);
312 k
= key_bytype(kf
, "catacomb-rand");
315 key_data
*kd
= k
->k
, *kkd
;
319 switch (kd
->e
& KF_ENCMASK
) {
325 if (key_punlock(&kkd
, kd
, d
.buf
))
326 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
334 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
338 /* --- Key the generator --- */
340 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
345 /* --- Key generation algorithms --- */
347 static void alg_binary(keyopts
*k
)
357 die(EXIT_FAILURE
, "no shared parameters for binary keys");
359 sz
= (k
->bits
+ 7) >> 3;
361 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
362 k
->r
->ops
->fill(k
->r
, p
, sz
);
364 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
367 key_setkeydata(k
->kf
, k
->k
, kd
);
372 static void alg_des(keyopts
*k
)
382 die(EXIT_FAILURE
, "no shared parameters for DES keys");
383 if (k
->bits
% 56 || k
->bits
> 168)
384 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
388 k
->r
->ops
->fill(k
->r
, p
, sz
);
389 for (i
= 0; i
< sz
; i
++) {
390 octet x
= p
[i
] | 0x01;
394 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
396 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
399 key_setkeydata(k
->kf
, k
->k
, kd
);
404 static void alg_rsa(keyopts
*k
)
409 /* --- Sanity checking --- */
412 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
416 /* --- Generate the RSA parameters --- */
418 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
419 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
420 die(EXIT_FAILURE
, "RSA key generation failed");
422 /* --- Run a test encryption --- */
425 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
427 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
432 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
433 c
= rsa_qprivop(&rp
, c
, c
, g
);
436 die(EXIT_FAILURE
, "test encryption failed");
442 /* --- Allrighty then --- */
444 kd
= key_newstruct();
445 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
446 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
448 kkd
= key_newstruct();
449 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
450 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
451 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
452 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
453 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
454 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
455 dolock(k
, &kkd
, "private");
456 key_structsteal(kd
, "private", kkd
);
457 key_setkeydata(k
->kf
, k
->k
, kd
);
462 static void alg_dsaparam(keyopts
*k
)
464 static const char *pl
[] = { "q", "p", "g", 0 };
465 if (!copyparam(k
, pl
)) {
474 /* --- Choose appropriate bit lengths if necessary --- */
481 /* --- Allocate a seed block --- */
483 sz
= (k
->qbits
+ 7) >> 3;
485 k
->r
->ops
->fill(k
->r
, p
, sz
);
487 /* --- Allocate the parameters --- */
489 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
490 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
491 die(EXIT_FAILURE
, "DSA parameter generation failed");
493 /* --- Store the parameters --- */
495 kd
= key_newstruct();
496 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
497 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
498 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
502 key_setkeydata(k
->kf
, k
->k
, kd
);
505 /* --- Store the seed for future verification --- */
510 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
511 base64_encode(&c
, 0, 0, &d
);
513 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
515 dstr_putf(&d
, "%u", ds
.count
);
516 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
523 static void alg_dsa(keyopts
*k
)
530 /* --- Get the shared parameters --- */
533 key_split(&k
->k
->k
); kd
= k
->k
->k
;
538 /* --- Choose a private key --- */
540 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
541 mpmont_create(&mm
, p
);
542 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
544 /* --- Store everything away --- */
546 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
548 kkd
= key_newstruct();
549 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
550 dolock(k
, &kkd
, "private");
551 key_structsteal(kd
, "private", kkd
);
553 mp_drop(x
); mp_drop(y
);
556 static void alg_dhparam(keyopts
*k
)
558 static const char *pl
[] = { "p", "q", "g", 0 };
560 if (!copyparam(k
, pl
)) {
569 if (strcmp(k
->curve
, "list") == 0) {
571 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
575 if (dh_parse(&qd
, &dp
))
576 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
578 die(EXIT_FAILURE
, "junk at end of field spec");
579 if ((g
= group_prime(&dp
)) == 0)
580 die(EXIT_FAILURE
, "invalid prime field");
581 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
582 moan("WARNING! group check failed: %s", e
);
590 /* --- Choose a large safe prime number --- */
592 if (k
->f
& f_limlee
) {
597 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
598 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
599 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
600 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
604 for (i
= 0; i
< nf
; i
++) {
607 mp_writedstr(f
[i
], &d
, 10);
610 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
613 } else if (k
->f
& f_kcdsa
) {
616 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
617 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
622 mp_writedstr(dp
.q
, &d
, 10);
623 mp_div(&v
, 0, dp
.p
, dp
.q
);
626 mp_writedstr(v
, &d
, 10);
628 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
632 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
633 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
636 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
639 kd
= key_newstruct();
640 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
641 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
642 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
646 key_setkeydata(k
->kf
, k
->k
, kd
);
651 static void alg_dh(keyopts
*k
)
658 /* --- Get the shared parameters --- */
661 key_split(&k
->k
->k
); kd
= k
->k
->k
;
666 /* --- Choose a suitable private key --- *
668 * Since %$g$% has order %$q$%, choose %$x < q$%.
671 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
673 /* --- Compute the public key %$y = g^x \bmod p$% --- */
675 mpmont_create(&mm
, p
);
676 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
679 /* --- Store everything away --- */
681 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
683 kkd
= key_newstruct();
684 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
685 dolock(k
, &kkd
, "private");
686 key_structsteal(kd
, "private", kkd
);
688 mp_drop(x
); mp_drop(y
);
691 static void alg_bbs(keyopts
*k
)
696 /* --- Sanity checking --- */
699 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
703 /* --- Generate the BBS parameters --- */
705 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
706 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
707 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
709 /* --- Allrighty then --- */
711 kd
= key_newstruct();
712 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
714 kkd
= key_newstruct();
715 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
716 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
717 dolock(k
, &kkd
, "private");
718 key_structsteal(kd
, "private", kkd
);
719 key_setkeydata(k
->kf
, k
->k
, kd
);
725 static void alg_binparam(keyopts
*k
)
727 static const char *pl
[] = { "p", "q", "g", 0 };
728 if (!copyparam(k
, pl
)) {
735 /* --- Decide on a field --- */
737 if (!k
->bits
) k
->bits
= 128;
738 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
740 LIST("Built-in binary fields", stdout
,
741 bintab
[i
].name
, bintab
[i
].name
);
745 if (k
->bits
<= 40) k
->curve
= "p1363-40";
746 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
747 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
748 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
749 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
750 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
753 "no built-in binary fields provide %u-bit security",
758 /* --- Check it --- */
762 if (dhbin_parse(&qd
, &gb
))
763 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
765 die(EXIT_FAILURE
, "junk at end of field spec");
766 if ((g
= group_binary(&gb
)) == 0)
767 die(EXIT_FAILURE
, "invalid binary field");
768 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
769 moan("WARNING! group check failed: %s", e
);
772 /* --- Write out the answer --- */
774 kd
= key_newstruct();
775 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
776 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
777 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
781 key_setkeydata(k
->kf
, k
->k
, kd
);
786 static void alg_bin(keyopts
*k
)
793 /* --- Get the shared parameters --- */
796 key_split(&k
->k
->k
); kd
= k
->k
->k
;
801 /* --- Choose a suitable private key --- *
803 * Since %$g$% has order %$q$%, choose %$x < q$%.
806 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
808 /* --- Compute the public key %$y = g^x \bmod p$% --- */
810 gfreduce_create(&r
, p
);
811 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
812 gfreduce_destroy(&r
);
814 /* --- Store everything away --- */
816 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
818 kkd
= key_newstruct();
819 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
820 dolock(k
, &kkd
, "private");
821 key_structsteal(kd
, "private", kkd
);
823 mp_drop(x
); mp_drop(y
);
826 static void alg_ecparam(keyopts
*k
)
828 static const char *pl
[] = { "curve", 0 };
829 if (!copyparam(k
, pl
)) {
834 /* --- Decide on a curve --- */
836 if (!k
->bits
) k
->bits
= 256;
837 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
839 LIST("Built-in elliptic curves", stdout
,
840 ectab
[i
].name
, ectab
[i
].name
);
844 if (k
->bits
<= 56) k
->curve
= "secp112r1";
845 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
846 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
847 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
848 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
849 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
850 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
851 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
853 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
857 /* --- Check it --- */
859 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
860 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
861 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
862 moan("WARNING! curve check failed: %s", e
);
865 /* --- Write out the answer --- */
867 kd
= key_newstruct();
868 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
869 key_setkeydata(k
->kf
, k
->k
, kd
);
874 static void alg_ec(keyopts
*k
)
883 /* --- Get the curve --- */
886 key_split(&k
->k
->k
); kd
= k
->k
->k
;
887 if ((kkd
= key_structfind(kd
, "curve")) == 0)
888 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
889 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
890 die(EXIT_FAILURE
, "subkey `curve' is not a string");
891 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
892 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
894 /* --- Invent a private exponent and compute the public key --- */
896 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
897 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
899 /* --- Store everything away --- */
901 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
903 kkd
= key_newstruct();
904 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
905 dolock(k
, &kkd
, "private");
906 key_structsteal(kd
, "private", kkd
);
914 /* --- The algorithm tables --- */
916 typedef struct keyalg
{
918 void (*proc
)(keyopts
*o
);
922 static keyalg algtab
[] = {
923 { "binary", alg_binary
, "Plain binary data" },
924 { "des", alg_des
, "Binary with DES-style parity" },
925 { "rsa", alg_rsa
, "RSA public-key encryption" },
926 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
927 { "dsa", alg_dsa
, "DSA digital signatures" },
928 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
929 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
930 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
931 { "bindh", alg_bin
, "DH over a binary field" },
932 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
933 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
934 { "ec", alg_ec
, "Elliptic curve crypto" },
938 /* --- @cmd_add@ --- */
940 static int cmd_add(int argc
, char *argv
[])
943 time_t exp
= KEXP_EXPIRE
;
944 const char *tag
= 0, *ptag
= 0;
946 keyalg
*alg
= algtab
;
947 const char *rtag
= 0;
948 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
949 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
950 const char *seed
= 0;
953 /* --- Parse options for the subcommand --- */
956 static struct option opt
[] = {
957 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
958 { "bits", OPTF_ARGREQ
, 0, 'b' },
959 { "qbits", OPTF_ARGREQ
, 0, 'B' },
960 { "parameters", OPTF_ARGREQ
, 0, 'p' },
961 { "expire", OPTF_ARGREQ
, 0, 'e' },
962 { "comment", OPTF_ARGREQ
, 0, 'c' },
963 { "tag", OPTF_ARGREQ
, 0, 't' },
964 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
965 { "curve", OPTF_ARGREQ
, 0, 'C' },
966 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
967 { "seed", OPTF_ARGREQ
, 0, 's' },
968 { "newseed", OPTF_ARGREQ
, 0, 'n' },
969 { "lock", 0, 0, 'l' },
970 { "quiet", 0, 0, 'q' },
971 { "lim-lee", 0, 0, 'L' },
972 { "subgroup", 0, 0, 'S' },
973 { "kcdsa", 0, 0, 'K' },
976 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLKS",
981 /* --- Handle the various options --- */
985 /* --- Read an algorithm name --- */
989 size_t sz
= strlen(optarg
);
991 if (strcmp(optarg
, "list") == 0) {
992 for (a
= algtab
; a
->name
; a
++)
993 printf("%-10s %s\n", a
->name
, a
->help
);
998 for (a
= algtab
; a
->name
; a
++) {
999 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1000 if (a
->name
[sz
] == 0) {
1004 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1010 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1013 /* --- Bits must be nonzero and a multiple of 8 --- */
1017 k
.bits
= strtoul(optarg
, &p
, 0);
1018 if (k
.bits
== 0 || *p
!= 0)
1019 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1024 k
.qbits
= strtoul(optarg
, &p
, 0);
1025 if (k
.qbits
== 0 || *p
!= 0)
1026 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1029 /* --- Parameter selection --- */
1035 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1038 if (strcmp(optarg
, "forever") == 0)
1041 exp
= get_date(optarg
, 0);
1043 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1047 /* --- Store comments without interpretation --- */
1050 if (key_chkcomment(optarg
))
1051 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1055 /* --- Elliptic curve parameters --- */
1061 /* --- Store tags --- */
1064 if (key_chkident(optarg
))
1065 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1072 /* --- Seeding --- */
1075 const struct seedalg
*ss
;
1076 if (strcmp(optarg
, "list") == 0) {
1077 printf("Seed algorithms:\n");
1078 for (ss
= seedtab
; ss
->p
; ss
++)
1079 printf(" %s\n", ss
->p
);
1082 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1084 for (ss
= seedtab
; ss
->p
; ss
++) {
1085 if (strcmp(optarg
, ss
->p
) == 0)
1089 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1095 if (seed
) die(EXIT_FAILURE
, "seed already set");
1097 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1098 base64_decode(&b
, 0, 0, &d
);
1099 k
.r
= sa
->gen(d
.buf
, d
.len
);
1108 unsigned n
= strtoul(optarg
, &p
, 0);
1109 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1110 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1111 if (seed
) die(EXIT_FAILURE
, "seed already set");
1114 rand_get(RAND_GLOBAL
, p
, n
);
1116 base64_encode(&b
, p
, n
, &d
);
1117 base64_encode(&b
, 0, 0, &d
);
1119 k
.r
= sa
->gen(p
, n
);
1122 /* --- Other flags --- */
1143 /* --- Other things are bogus --- */
1151 /* --- Various sorts of bogosity --- */
1153 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1155 "Usage: add [OPTIONS] TYPE [ATTR...]");
1157 if (key_chkident(argv
[optind
]))
1158 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1160 /* --- Set up various bits of the state --- */
1162 if (exp
== KEXP_EXPIRE
)
1163 exp
= time(0) + 14 * 24 * 60 * 60;
1165 /* --- Open the file and create the basic key block --- *
1167 * Keep on generating keyids until one of them doesn't collide.
1170 doopen(&f
, KOPEN_WRITE
);
1173 /* --- Key the generator --- */
1178 uint32 id
= rand_global
.ops
->word(&rand_global
);
1180 if ((err
= key_new(&f
, id
, argv
[optind
], exp
, &k
.k
)) == 0)
1182 else if (err
!= KERR_DUPID
)
1183 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1186 /* --- Set various simple attributes --- */
1191 if (k
.f
& f_retag
) {
1192 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1194 strcmp(kk
->tag
, tag
) == 0)
1195 key_settag(&f
, kk
, 0);
1197 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1198 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1202 int err
= key_setcomment(&f
, k
.k
, c
);
1204 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1207 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1209 key_putattr(&f
, k
.k
, "genseed", seed
);
1210 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1213 key_fulltag(k
.k
, &k
.tag
);
1215 /* --- Find the parameter key --- */
1218 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1219 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1220 if ((k
.p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
1221 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1224 /* --- Now generate the actual key data --- */
1230 dstr_destroy(&k
.tag
);
1235 /*----- Key listing -------------------------------------------------------*/
1237 /* --- Listing options --- */
1239 typedef struct listopts
{
1240 const char *tfmt
; /* Date format (@strftime@-style) */
1241 int v
; /* Verbosity level */
1242 unsigned f
; /* Various flags */
1243 time_t t
; /* Time now (for key expiry) */
1244 key_filter kf
; /* Filter for matching keys */
1247 /* --- Listing flags --- */
1249 #define f_newline 2u /* Write newline before next entry */
1250 #define f_attr 4u /* Written at least one attribute */
1251 #define f_utc 8u /* Emit UTC time, not local time */
1253 /* --- @showkeydata@ --- *
1255 * Arguments: @key_data *k@ = pointer to key to write
1256 * @int ind@ = indentation level
1257 * @listopts *o@ = listing options
1258 * @dstr *d@ = tag string for this subkey
1262 * Use: Emits a piece of key data in a human-readable format.
1265 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1267 #define INDENT(i) do { \
1269 for (_i = 0; _i < (i); _i++) { \
1274 switch (k
->e
& KF_ENCMASK
) {
1276 /* --- Binary key data --- *
1278 * Emit as a simple hex dump.
1282 const octet
*p
= k
->u
.k
.k
;
1283 const octet
*l
= p
+ k
->u
.k
.sz
;
1286 fputs(" {", stdout
);
1291 } else if (sz
% 8 == 0)
1295 printf("%02x", *p
++);
1300 fputs("}\n", stdout
);
1303 /* --- Encrypted data --- *
1305 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1306 * key. Otherwise just say that it's encrypted and move on.
1311 fputs(" encrypted\n", stdout
);
1314 if (key_punlock(&kd
, k
, d
->buf
))
1315 printf(" <failed to unlock %s>\n", d
->buf
);
1317 fputs(" encrypted", stdout
);
1318 showkeydata(kd
, ind
, o
, d
);
1324 /* --- Integer keys --- *
1326 * Emit as a large integer in decimal. This makes using the key in
1327 * `calc' or whatever easier.
1332 mp_writefile(k
->u
.m
, stdout
, 10);
1336 /* --- Strings --- */
1339 printf(" `%s'\n", k
->u
.p
);
1342 /* --- Elliptic curve points --- */
1345 if (EC_ATINF(&k
->u
.e
))
1346 fputs(" inf\n", stdout
);
1348 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1349 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1354 /* --- Structured keys --- *
1356 * Just iterate over the subkeys.
1364 fputs(" {\n", stdout
);
1365 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1366 if (!key_match(k
, &o
->kf
))
1369 printf("%s =", tag
);
1371 dstr_putf(d
, ".%s", tag
);
1372 showkeydata(k
, ind
+ 2, o
, d
);
1375 fputs("}\n", stdout
);
1382 /* --- @showkey@ --- *
1384 * Arguments: @key *k@ = pointer to key to show
1385 * @listopts *o@ = pointer to listing options
1389 * Use: Emits a listing of a particular key.
1392 static void showkey(key
*k
, listopts
*o
)
1394 char ebuf
[24], dbuf
[24];
1397 /* --- Skip the key if the filter doesn't match --- */
1399 if (!key_match(k
->k
, &o
->kf
))
1402 /* --- Sort out the expiry and deletion times --- */
1404 if (KEY_EXPIRED(o
->t
, k
->exp
))
1405 strcpy(ebuf
, "expired");
1406 else if (k
->exp
== KEXP_FOREVER
)
1407 strcpy(ebuf
, "forever");
1409 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1410 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1413 if (KEY_EXPIRED(o
->t
, k
->del
))
1414 strcpy(dbuf
, "deleted");
1415 else if (k
->del
== KEXP_FOREVER
)
1416 strcpy(dbuf
, "forever");
1418 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1419 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1422 /* --- If in compact format, just display and quit --- */
1425 if (!(o
->f
& f_newline
)) {
1426 printf("%8s %-20s %-20s %-10s %-10s\n",
1427 "Id", "Tag", "Type", "Expire", "Delete");
1429 printf("%08lx %-20s %-20s %-10s %-10s\n",
1430 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1431 k
->type
, ebuf
, dbuf
);
1436 /* --- Display the standard header --- */
1438 if (o
->f
& f_newline
)
1439 fputc('\n', stdout
);
1440 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1441 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1442 printf("type: %s\n", k
->type
);
1443 printf("expiry: %s\n", ebuf
);
1444 printf("delete: %s\n", dbuf
);
1445 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1447 /* --- Display the attributes --- */
1451 const char *av
, *an
;
1454 printf("attributes:");
1455 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1456 printf("\n %s = %s", an
, av
);
1460 fputc('\n', stdout
);
1465 /* --- If dumping requested, dump the raw key data --- */
1469 fputs("key:", stdout
);
1471 showkeydata(k
->k
, 0, o
, &d
);
1478 /* --- @cmd_list@ --- */
1480 static int cmd_list(int argc
, char *argv
[])
1484 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1486 /* --- Parse subcommand options --- */
1489 static struct option opt
[] = {
1490 { "quiet", 0, 0, 'q' },
1491 { "verbose", 0, 0, 'v' },
1492 { "utc", 0, 0, 'u' },
1493 { "filter", OPTF_ARGREQ
, 0, 'f' },
1496 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1513 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1515 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1524 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1526 /* --- Open the key file --- */
1528 doopen(&f
, KOPEN_READ
);
1531 /* --- Set up the time format --- */
1534 o
.tfmt
= "%Y-%m-%d";
1535 else if (o
.f
& f_utc
)
1536 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1538 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1540 /* --- If specific keys were requested use them, otherwise do all --- *
1542 * Some day, this might turn into a wildcard match.
1545 if (optind
< argc
) {
1547 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1550 moan("key `%s' not found", argv
[optind
]);
1554 } while (optind
< argc
);
1557 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1565 return (EXIT_FAILURE
);
1570 /*----- Command implementation --------------------------------------------*/
1572 /* --- @cmd_expire@ --- */
1574 static int cmd_expire(int argc
, char *argv
[])
1582 die(EXIT_FAILURE
, "Usage: expire TAG...");
1583 doopen(&f
, KOPEN_WRITE
);
1584 for (i
= 1; i
< argc
; i
++) {
1585 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1588 moan("key `%s' not found", argv
[i
]);
1596 /* --- @cmd_delete@ --- */
1598 static int cmd_delete(int argc
, char *argv
[])
1606 die(EXIT_FAILURE
, "Usage: delete TAG...");
1607 doopen(&f
, KOPEN_WRITE
);
1608 for (i
= 1; i
< argc
; i
++) {
1609 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1612 moan("key `%s' not found", argv
[i
]);
1620 /* --- @cmd_setattr@ --- */
1622 static int cmd_setattr(int argc
, char *argv
[])
1628 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1629 doopen(&f
, KOPEN_WRITE
);
1630 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1631 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1632 setattr(&f
, k
, argv
+ 2);
1637 /* --- @cmd_getattr@ --- */
1639 static int cmd_getattr(int argc
, char *argv
[])
1647 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1648 doopen(&f
, KOPEN_READ
);
1649 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1650 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1652 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1653 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1660 /* --- @cmd_finger@ --- */
1662 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1670 if (key_fingerprint(k
, h
, kf
)) {
1673 for (i
= 0; i
< ch
->hashsz
; i
++) {
1674 if (i
&& i
% 4 == 0)
1676 printf("%02x", p
[i
]);
1678 printf(" %s\n", d
.buf
);
1684 static int cmd_finger(int argc
, char *argv
[])
1688 const gchash
*ch
= &rmd160
;
1689 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1692 static struct option opt
[] = {
1693 { "filter", OPTF_ARGREQ
, 0, 'f' },
1694 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1697 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1703 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1705 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1708 if ((ch
= ghash_byname(optarg
)) == 0)
1709 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1717 argv
+= optind
; argc
-= optind
;
1719 die(EXIT_FAILURE
, "Usage: fingerprint [-f FILTER] [TAG...]");
1721 doopen(&f
, KOPEN_READ
);
1725 for (i
= 0; i
< argc
; i
++) {
1726 key
*k
= key_bytag(&f
, argv
[i
]);
1728 fingerprint(k
, ch
, &kf
);
1731 moan("key `%s' not found", argv
[i
]);
1737 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1738 fingerprint(k
, ch
, &kf
);
1743 /* --- @cmd_verify@ --- */
1745 static unsigned xdigit(char c
)
1747 if ('A' <= c
&& c
<= 'Z') return (c
+ 10 - 'A');
1748 if ('a' <= c
&& c
<= 'z') return (c
+ 10 - 'a');
1749 if ('0' <= c
&& c
<= '9') return (c
- '0');
1753 static void unhexify(octet
*q
, char *p
, size_t n
)
1759 if (*p
== '-' || *p
== ':' || isspace((unsigned char)*p
)) {
1766 die(EXIT_FAILURE
, "hex string too short");
1767 if (!isxdigit((unsigned char)*p
))
1768 die(EXIT_FAILURE
, "bad hex string");
1770 die(EXIT_FAILURE
, "hex string too long");
1771 a
= (a
<< 4) | xdigit(*p
++);
1782 static int cmd_verify(int argc
, char *argv
[])
1786 const gchash
*ch
= &rmd160
;
1791 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1794 static struct option opt
[] = {
1795 { "filter", OPTF_ARGREQ
, 0, 'f' },
1796 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1799 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1805 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1807 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1810 if ((ch
= ghash_byname(optarg
)) == 0)
1811 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1819 argv
+= optind
; argc
-= optind
;
1820 if (rc
|| argc
!= 2)
1821 die(EXIT_FAILURE
, "Usage: verify [-f FILTER] TAG FINGERPRINT");
1823 doopen(&f
, KOPEN_READ
);
1825 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1826 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1827 buf
= xmalloc(ch
->hashsz
);
1828 unhexify(buf
, argv
[1], ch
->hashsz
);
1830 if (!key_fingerprint(k
, h
, &kf
))
1831 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1832 fpr
= GH_DONE(h
, 0);
1833 if (memcmp(fpr
, buf
, ch
->hashsz
) != 0)
1834 die(EXIT_FAILURE
, "key fingerprint mismatch");
1839 /* --- @cmd_comment@ --- */
1841 static int cmd_comment(int argc
, char *argv
[])
1847 if (argc
< 2 || argc
> 3)
1848 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1849 doopen(&f
, KOPEN_WRITE
);
1850 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1851 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1852 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1853 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1858 /* --- @cmd_tag@ --- */
1860 static int cmd_tag(int argc
, char *argv
[])
1869 static struct option opt
[] = {
1870 { "retag", 0, 0, 'r' },
1873 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1886 argv
+= optind
; argc
-= optind
;
1887 if (argc
< 1 || argc
> 2 || rc
)
1888 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1889 doopen(&f
, KOPEN_WRITE
);
1890 if (flags
& f_retag
) {
1891 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1892 key_settag(&f
, k
, 0);
1894 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1895 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1896 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1897 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1902 /* --- @cmd_lock@ --- */
1904 static int cmd_lock(int argc
, char *argv
[])
1912 die(EXIT_FAILURE
, "Usage: lock QTAG");
1913 doopen(&f
, KOPEN_WRITE
);
1914 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1915 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1916 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
1917 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1918 if (key_plock(kd
, 0, d
.buf
))
1919 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1925 /* --- @cmd_unlock@ --- */
1927 static int cmd_unlock(int argc
, char *argv
[])
1935 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1936 doopen(&f
, KOPEN_WRITE
);
1937 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1938 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1939 if ((*kd
)->e
!= KENC_ENCRYPT
)
1940 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1941 if (key_punlock(kd
, 0, d
.buf
))
1942 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1948 /* --- @cmd_extract@ --- */
1950 static int cmd_extract(int argc
, char *argv
[])
1956 key_filter kf
= { 0, 0 };
1958 const char *outfile
= 0;
1962 static struct option opt
[] = {
1963 { "filter", OPTF_ARGREQ
, 0, 'f' },
1966 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1972 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1974 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1982 argv
+= optind
; argc
-= optind
;
1984 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
1985 if (strcmp(*argv
, "-") == 0)
1989 dstr_putf(&d
, "%s.new", outfile
);
1990 if (!(fp
= fopen(d
.buf
, "w"))) {
1991 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1992 d
.buf
, strerror(errno
));
1996 doopen(&f
, KOPEN_READ
);
2000 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2001 key_extract(&f
, k
, fp
, &kf
);
2003 for (i
= 1; i
< argc
; i
++) {
2004 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2005 key_extract(&f
, k
, fp
, &kf
);
2007 moan("key `%s' not found", argv
[i
]);
2012 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2013 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2019 /* --- @cmd_tidy@ --- */
2021 static int cmd_tidy(int argc
, char *argv
[])
2025 die(EXIT_FAILURE
, "Usage: tidy");
2026 doopen(&f
, KOPEN_WRITE
);
2027 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2032 /* --- @cmd_merge@ --- */
2034 static int cmd_merge(int argc
, char *argv
[])
2040 die(EXIT_FAILURE
, "Usage: merge FILE");
2041 if (strcmp(argv
[1], "-") == 0)
2043 else if (!(fp
= fopen(argv
[1], "r"))) {
2044 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2045 argv
[1], strerror(errno
));
2048 doopen(&f
, KOPEN_WRITE
);
2049 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2054 /* --- @cmd_show@ --- */
2058 listtab[i].name, listtab[i].name) \
2059 LI("Hash functions", hash, \
2060 ghashtab[i], ghashtab[i]->name) \
2061 LI("Elliptic curves", ec, \
2062 ectab[i].name, ectab[i].name) \
2063 LI("Prime Diffie-Hellman groups", dh, \
2064 ptab[i].name, ptab[i].name) \
2065 LI("Binary Diffie-Hellman groups", bindh, \
2066 bintab[i].name, bintab[i].name) \
2067 LI("Key-generation algorithms", keygen, \
2068 algtab[i].name, algtab[i].name) \
2069 LI("Random seeding algorithms", seed, \
2070 seedtab[i].p, seedtab[i].p)
2072 MAKELISTTAB(listtab
, LISTS
)
2074 static int cmd_show(int argc
, char *argv
[])
2076 return (displaylists(listtab
, argv
+ 1));
2079 /*----- Main command table ------------------------------------------------*/
2081 static int cmd_help(int argc
, char *argv
[]);
2083 static cmd cmds
[] = {
2084 { "help", cmd_help
, "help [COMMAND...]" },
2085 { "show", cmd_show
, "show [ITEM...]" },
2086 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2089 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2090 -q, --quiet Show less information.\n\
2091 -v, --verbose Show more information.\n\
2093 { "fingerprint", cmd_finger
, "fingerprint [-f FILTER] [TAG...]", "\
2096 -f, --filter=FILT Only hash key components matching FILT.\n\
2097 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2098 ($ show hash for list.)\n\
2100 { "verify", cmd_verify
, "verify [-f FILTER] TAG FINGERPRINT", "\
2103 -f, --filter=FILT Only hash key components matching FILT.\n\
2104 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2105 ($ show hash for list.)\n\
2107 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2110 -f, --filter=FILT Only extract key components matching FILT.\n\
2112 { "merge", cmd_merge
, "merge FILE" },
2113 { "expire", cmd_expire
, "expire TAG..." },
2114 { "delete", cmd_delete
, "delete TAG..." },
2115 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2116 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2117 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2118 { "lock", cmd_lock
, "lock QTAG" },
2119 { "unlock", cmd_unlock
, "unlock QTAG" },
2120 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2123 -r, --retag Untag any key currently called new-tag.\n\
2125 { "tidy", cmd_tidy
, "tidy" },
2127 "add [-OPTIONS] TYPE [ATTR...]\n\
2128 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2129 [-A SEEDALG] [-s SEED] [-n BITS]\n\
2130 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2133 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2134 ($ show keygen for list.)\n\
2135 -b, --bits=N Generate an N-bit key.\n\
2136 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2137 -p, --parameters=TAG Get group parameters from TAG.\n\
2138 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2139 ($ show ec or $ show dh for list.)\n\
2140 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2141 ($ show seed for list.)\n\
2142 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2143 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2144 -e, --expire=TIME Make the key expire after TIME.\n\
2145 -c, --comment=STRING Attach the command STRING to the key.\n\
2146 -t, --tag=TAG Tag the key with the name TAG.\n\
2147 -r, --retag Untag any key currently with that tag.\n\
2148 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2149 -l, --lock Lock the generated key with a passphrase.\n\
2150 -q, --quiet Don't give progress indicators while working.\n\
2151 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2152 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2153 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2158 static int cmd_help(int argc
, char *argv
[])
2160 sc_help(cmds
, stdout
, argv
+ 1);
2164 /*----- Main code ---------------------------------------------------------*/
2166 /* --- Helpful GNUy functions --- */
2168 static void usage(FILE *fp
)
2170 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2173 void version(FILE *fp
)
2175 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2178 void help_global(FILE *fp
)
2182 Performs various simple key management operations.\n\
2184 Global command line options:\n\
2186 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2187 -v, --version Display version number.\n\
2188 -u, --usage Display short usage summary.\n\
2190 -k, --keyring=FILE Read and write keys in FILE.\n",
2196 * Arguments: @int argc@ = number of command line arguments
2197 * @char *argv[]@ = array of command line arguments
2199 * Returns: Nonzero on failure.
2201 * Use: Main program. Performs simple key management functions.
2204 int main(int argc
, char *argv
[])
2210 /* --- Initialization --- */
2215 /* --- Parse command line options --- */
2218 static struct option opt
[] = {
2220 /* --- Standard GNUy help options --- */
2222 { "help", 0, 0, 'h' },
2223 { "version", 0, 0, 'v' },
2224 { "usage", 0, 0, 'u' },
2226 /* --- Real live useful options --- */
2228 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2230 /* --- Magic terminator --- */
2234 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2240 /* --- GNU help options --- */
2243 sc_help(cmds
, stdout
, argv
+ optind
);
2252 /* --- Real useful options --- */
2258 /* --- Bogosity --- */
2266 /* --- Complain about excessive bogons --- */
2268 if (f
& f_bogus
|| optind
== argc
) {
2273 /* --- Initialize the Catacomb random number generator --- */
2275 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2276 rand_seed(RAND_GLOBAL
, 160);
2278 /* --- Dispatch to appropriate command handler --- */
2283 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2286 /*----- That's all, folks -------------------------------------------------*/