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 */
201 /* --- @dolock@ --- *
203 * Arguments: @keyopts *k@ = key generation options
204 * @key_data *kd@ = pointer to key data to lock
205 * @const char *t@ = tag suffix or null
209 * Use: Does passphrase locking on new keys.
212 static void dolock(keyopts
*k
, key_data
*kd
, const char *t
)
214 if (!(k
->f
& f_lock
))
217 dstr_putf(&k
->tag
, ".%s", t
);
218 if (key_plock(k
->tag
.buf
, kd
, kd
))
219 die(EXIT_FAILURE
, "couldn't lock key");
224 * Arguments: @key_data *kd@ = pointer to parent key block
225 * @const char *tag@ = pointer to tag string
226 * @mp *m@ = integer to store
227 * @unsigned f@ = flags to set
231 * Use: Sets a multiprecision integer subkey.
234 static void mpkey(key_data
*kd
, const char *tag
, mp
*m
, unsigned f
)
236 key_data
*kkd
= key_structcreate(kd
, tag
);
241 /* --- @copyparam@ --- *
243 * Arguments: @keyopts *k@ = pointer to key options
244 * @const char **pp@ = checklist of parameters
246 * Returns: Nonzero if parameters copied; zero if you have to generate
249 * Use: Copies parameters from a source key to the current one.
252 static int copyparam(keyopts
*k
, const char **pp
)
258 /* --- Quick check if no parameters supplied --- */
263 /* --- Run through the checklist --- */
266 key_data
*kd
= key_structfind(&k
->p
->k
, *pp
);
268 die(EXIT_FAILURE
, "bad parameter key: parameter `%s' not found", *pp
);
269 if ((kd
->e
& KF_CATMASK
) != KCAT_SHARE
)
270 die(EXIT_FAILURE
, "bad parameter key: subkey `%s' is not shared", *pp
);
274 /* --- Copy over the parameters --- */
278 if (!key_copy(&k
->k
->k
, &k
->p
->k
, &kf
))
279 die(EXIT_FAILURE
, "unexpected failure while copying parameters");
281 /* --- Copy over attributes --- */
283 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
284 key_putattr(k
->kf
, k
->k
, n
, v
);
293 * Arguments: @key_data *k@ = pointer to key data block
294 * @const char *tag@ = tag string to use
296 * Returns: Pointer to multiprecision integer key item.
298 * Use: Fetches an MP key component.
301 static mp
*getmp(key_data
*k
, const char *tag
)
303 k
= key_structfind(k
, tag
);
305 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
306 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
307 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type");
311 /* --- @keyrand@ --- *
313 * Arguments: @key_file *kf@ = pointer to key file
314 * @const char *id@ = pointer to key id (or null)
318 * Use: Keys the random number generator.
321 static void keyrand(key_file
*kf
, const char *id
)
325 /* --- Find the key --- */
328 if ((k
= key_bytag(kf
, id
)) == 0)
329 die(EXIT_FAILURE
, "key `%s' not found", id
);
331 k
= key_bytype(kf
, "catacomb-rand");
334 key_data
*kd
= &k
->k
, kkd
;
337 switch (kd
->e
& KF_ENCMASK
) {
343 if (key_punlock(d
.buf
, kd
, &kkd
))
344 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
351 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
355 /* --- Key the generator --- */
357 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
363 /* --- Key generation algorithms --- */
365 static void alg_binary(keyopts
*k
)
374 die(EXIT_FAILURE
, "no shared parameters for binary keys");
376 sz
= (k
->bits
+ 7) >> 3;
378 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
379 k
->r
->ops
->fill(k
->r
, p
, sz
);
381 key_binary(&k
->k
->k
, p
, sz
);
382 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
385 dolock(k
, &k
->k
->k
, 0);
388 static void alg_des(keyopts
*k
)
397 die(EXIT_FAILURE
, "no shared parameters for DES keys");
398 if (k
->bits
% 56 || k
->bits
> 168)
399 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
403 k
->r
->ops
->fill(k
->r
, p
, sz
);
404 for (i
= 0; i
< sz
; i
++) {
405 octet x
= p
[i
] | 0x01;
409 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
411 key_binary(&k
->k
->k
, p
, sz
);
412 k
->k
->k
.e
|= KCAT_SYMM
| KF_BURN
;
415 dolock(k
, &k
->k
->k
, 0);
418 static void alg_rsa(keyopts
*k
)
423 /* --- Sanity checking --- */
426 die(EXIT_FAILURE
, "no shared parameters for RSA keys");
430 /* --- Generate the RSA parameters --- */
432 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
433 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
434 die(EXIT_FAILURE
, "RSA key generation failed");
436 /* --- Run a test encryption --- */
439 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
441 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
446 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
447 c
= rsa_qprivop(&rp
, c
, c
, g
);
450 die(EXIT_FAILURE
, "test encryption failed");
456 /* --- Allrighty then --- */
460 mpkey(kd
, "n", rp
.n
, KCAT_PUB
);
461 mpkey(kd
, "e", rp
.e
, KCAT_PUB
);
463 kd
= key_structcreate(kd
, "private");
465 mpkey(kd
, "d", rp
.d
, KCAT_PRIV
| KF_BURN
);
466 mpkey(kd
, "p", rp
.p
, KCAT_PRIV
| KF_BURN
);
467 mpkey(kd
, "q", rp
.q
, KCAT_PRIV
| KF_BURN
);
468 mpkey(kd
, "q-inv", rp
.q_inv
, KCAT_PRIV
| KF_BURN
);
469 mpkey(kd
, "d-mod-p", rp
.dp
, KCAT_PRIV
| KF_BURN
);
470 mpkey(kd
, "d-mod-q", rp
.dq
, KCAT_PRIV
| KF_BURN
);
471 dolock(k
, kd
, "private");
476 static void alg_dsaparam(keyopts
*k
)
478 static const char *pl
[] = { "q", "p", "g", 0 };
479 if (!copyparam(k
, pl
)) {
485 key_data
*kd
= &k
->k
->k
;
488 /* --- Choose appropriate bit lengths if necessary --- */
495 /* --- Allocate a seed block --- */
497 sz
= (k
->qbits
+ 7) >> 3;
499 k
->r
->ops
->fill(k
->r
, p
, sz
);
501 /* --- Allocate the parameters --- */
503 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
504 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
505 die(EXIT_FAILURE
, "DSA parameter generation failed");
507 /* --- Store the parameters --- */
510 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
511 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
512 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
517 /* --- Store the seed for future verification --- */
522 base64_encode(&c
, ds
.p
, ds
.sz
, &d
);
523 base64_encode(&c
, 0, 0, &d
);
524 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
526 dstr_putf(&d
, "%u", ds
.count
);
527 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
534 static void alg_dsa(keyopts
*k
)
539 key_data
*kd
= &k
->k
->k
;
541 /* --- Get the shared parameters --- */
548 /* --- Choose a private key --- */
550 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
551 mpmont_create(&mm
, p
);
552 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
554 /* --- Store everything away --- */
556 mpkey(kd
, "y", y
, KCAT_PUB
);
558 kd
= key_structcreate(kd
, "private");
560 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
561 dolock(k
, kd
, "private");
563 mp_drop(x
); mp_drop(y
);
566 static void alg_dhparam(keyopts
*k
)
568 static const char *pl
[] = { "p", "q", "g", 0 };
569 key_data
*kd
= &k
->k
->k
;
570 if (!copyparam(k
, pl
)) {
579 if (strcmp(k
->curve
, "list") == 0) {
581 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
585 if (dh_parse(&qd
, &dp
))
586 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
588 die(EXIT_FAILURE
, "junk at end of field spec");
589 if ((g
= group_prime(&dp
)) == 0)
590 die(EXIT_FAILURE
, "invalid prime field");
591 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
592 moan("WARNING! group check failed: %s", e
);
600 /* --- Choose a large safe prime number --- */
602 if (k
->f
& f_limlee
) {
607 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
608 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
609 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
610 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
614 for (i
= 0; i
< nf
; i
++) {
617 mp_writedstr(f
[i
], &d
, 10);
620 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
624 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
625 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
628 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
632 mpkey(kd
, "p", dp
.p
, KCAT_SHARE
);
633 mpkey(kd
, "q", dp
.q
, KCAT_SHARE
);
634 mpkey(kd
, "g", dp
.g
, KCAT_SHARE
);
641 static void alg_dh(keyopts
*k
)
646 key_data
*kd
= &k
->k
->k
;
648 /* --- Get the shared parameters --- */
655 /* --- Choose a suitable private key --- *
657 * Since %$g$% has order %$q$%, choose %$x < q$%.
660 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
662 /* --- Compute the public key %$y = g^x \bmod p$% --- */
664 mpmont_create(&mm
, p
);
665 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
668 /* --- Store everything away --- */
670 mpkey(kd
, "y", y
, KCAT_PUB
);
672 kd
= key_structcreate(kd
, "private");
674 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
675 dolock(k
, kd
, "private");
677 mp_drop(x
); mp_drop(y
);
680 static void alg_bbs(keyopts
*k
)
685 /* --- Sanity checking --- */
688 die(EXIT_FAILURE
, "no shared parameters for Blum-Blum-Shub keys");
692 /* --- Generate the BBS parameters --- */
694 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
695 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
696 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
698 /* --- Allrighty then --- */
702 mpkey(kd
, "n", bp
.n
, KCAT_PUB
);
704 kd
= key_structcreate(kd
, "private");
706 mpkey(kd
, "p", bp
.p
, KCAT_PRIV
| KF_BURN
);
707 mpkey(kd
, "q", bp
.q
, KCAT_PRIV
| KF_BURN
);
708 dolock(k
, kd
, "private");
713 static void alg_binparam(keyopts
*k
)
715 static const char *pl
[] = { "p", "q", "g", 0 };
716 if (!copyparam(k
, pl
)) {
721 key_data
*kd
= &k
->k
->k
;
723 /* --- Decide on a field --- */
725 if (!k
->bits
) k
->bits
= 128;
726 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
728 LIST("Built-in binary fields", stdout
,
729 bintab
[i
].name
, bintab
[i
].name
);
733 if (k
->bits
<= 40) k
->curve
= "p1363-40";
734 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
735 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
736 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
737 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
738 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
741 "no built-in binary fields provide %u-bit security",
746 /* --- Check it --- */
750 if (dhbin_parse(&qd
, &gb
))
751 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
753 die(EXIT_FAILURE
, "junk at end of field spec");
754 if ((g
= group_binary(&gb
)) == 0)
755 die(EXIT_FAILURE
, "invalid binary field");
756 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
757 moan("WARNING! group check failed: %s", e
);
760 /* --- Write out the answer --- */
763 mpkey(kd
, "p", gb
.p
, KCAT_SHARE
);
764 mpkey(kd
, "q", gb
.q
, KCAT_SHARE
);
765 mpkey(kd
, "g", gb
.g
, KCAT_SHARE
);
772 static void alg_bin(keyopts
*k
)
777 key_data
*kd
= &k
->k
->k
;
779 /* --- Get the shared parameters --- */
786 /* --- Choose a suitable private key --- *
788 * Since %$g$% has order %$q$%, choose %$x < q$%.
791 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
793 /* --- Compute the public key %$y = g^x \bmod p$% --- */
795 gfreduce_create(&r
, p
);
796 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
797 gfreduce_destroy(&r
);
799 /* --- Store everything away --- */
801 mpkey(kd
, "y", y
, KCAT_PUB
);
803 kd
= key_structcreate(kd
, "private");
805 mpkey(kd
, "x", x
, KCAT_PRIV
| KF_BURN
);
806 dolock(k
, kd
, "private");
808 mp_drop(x
); mp_drop(y
);
811 static void alg_ecparam(keyopts
*k
)
813 static const char *pl
[] = { "curve", 0 };
814 if (!copyparam(k
, pl
)) {
817 key_data
*kd
= &k
->k
->k
;
819 /* --- Decide on a curve --- */
821 if (!k
->bits
) k
->bits
= 256;
822 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
824 LIST("Built-in elliptic curves", stdout
,
825 ectab
[i
].name
, ectab
[i
].name
);
829 if (k
->bits
<= 56) k
->curve
= "secp112r1";
830 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
831 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
832 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
833 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
834 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
835 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
836 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
838 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
842 /* --- Check it --- */
844 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
845 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
846 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
847 moan("WARNING! curve check failed: %s", e
);
850 /* --- Write out the answer --- */
853 kd
= key_structcreate(kd
, "curve");
854 key_string(kd
, k
->curve
);
859 static void alg_ec(keyopts
*k
)
861 key_data
*kd
= &k
->k
->k
;
868 /* --- Get the curve --- */
871 if ((kkd
= key_structfind(kd
, "curve")) == 0)
872 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
873 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
874 die(EXIT_FAILURE
, "subkey `curve' is not a string");
875 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
876 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
878 /* --- Invent a private exponent and compute the public key --- */
880 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
881 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
883 /* --- Store everything away --- */
885 kkd
= key_structcreate(kd
, "p");
888 kkd
= key_structcreate(kd
, "private");
890 mpkey(kkd
, "x", x
, KCAT_PRIV
| KF_BURN
);
891 dolock(k
, kkd
, "private");
899 /* --- The algorithm tables --- */
901 typedef struct keyalg
{
903 void (*proc
)(keyopts
*o
);
907 static keyalg algtab
[] = {
908 { "binary", alg_binary
, "Plain binary data" },
909 { "des", alg_des
, "Binary with DES-style parity" },
910 { "rsa", alg_rsa
, "RSA public-key encryption" },
911 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
912 { "dsa", alg_dsa
, "DSA digital signatures" },
913 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
914 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
915 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
916 { "bindh", alg_bin
, "DH over a binary field" },
917 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
918 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
919 { "ec", alg_ec
, "Elliptic curve crypto" },
923 /* --- @cmd_add@ --- */
925 static int cmd_add(int argc
, char *argv
[])
928 time_t exp
= KEXP_EXPIRE
;
929 const char *tag
= 0, *ptag
= 0;
931 keyalg
*alg
= algtab
;
932 const char *rtag
= 0;
933 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
934 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
935 const char *seed
= 0;
938 /* --- Parse options for the subcommand --- */
941 static struct option opt
[] = {
942 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
943 { "bits", OPTF_ARGREQ
, 0, 'b' },
944 { "qbits", OPTF_ARGREQ
, 0, 'B' },
945 { "parameters", OPTF_ARGREQ
, 0, 'p' },
946 { "expire", OPTF_ARGREQ
, 0, 'e' },
947 { "comment", OPTF_ARGREQ
, 0, 'c' },
948 { "tag", OPTF_ARGREQ
, 0, 't' },
949 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
950 { "curve", OPTF_ARGREQ
, 0, 'C' },
951 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
952 { "seed", OPTF_ARGREQ
, 0, 's' },
953 { "newseed", OPTF_ARGREQ
, 0, 'n' },
954 { "lock", 0, 0, 'l' },
955 { "quiet", 0, 0, 'q' },
956 { "lim-lee", 0, 0, 'L' },
957 { "subgroup", 0, 0, 'S' },
960 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:C:A:s:n:lqrLS",
965 /* --- Handle the various options --- */
969 /* --- Read an algorithm name --- */
973 size_t sz
= strlen(optarg
);
975 if (strcmp(optarg
, "list") == 0) {
976 for (a
= algtab
; a
->name
; a
++)
977 printf("%-10s %s\n", a
->name
, a
->help
);
982 for (a
= algtab
; a
->name
; a
++) {
983 if (strncmp(optarg
, a
->name
, sz
) == 0) {
984 if (a
->name
[sz
] == 0) {
988 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
994 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
997 /* --- Bits must be nonzero and a multiple of 8 --- */
1001 k
.bits
= strtoul(optarg
, &p
, 0);
1002 if (k
.bits
== 0 || *p
!= 0)
1003 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1008 k
.qbits
= strtoul(optarg
, &p
, 0);
1009 if (k
.qbits
== 0 || *p
!= 0)
1010 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1013 /* --- Parameter selection --- */
1019 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1022 if (strncmp(optarg
, "forever", strlen(optarg
)) == 0)
1025 exp
= get_date(optarg
, 0);
1027 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1031 /* --- Store comments without interpretation --- */
1034 if (key_chkcomment(optarg
))
1035 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1039 /* --- Elliptic curve parameters --- */
1045 /* --- Store tags --- */
1048 if (key_chkident(optarg
))
1049 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1056 /* --- Seeding --- */
1059 const struct seedalg
*ss
;
1060 if (strcmp(optarg
, "list") == 0) {
1061 printf("Seed algorithms:\n");
1062 for (ss
= seedtab
; ss
->p
; ss
++)
1063 printf(" %s\n", ss
->p
);
1066 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1068 for (ss
= seedtab
; ss
->p
; ss
++) {
1069 if (strcmp(optarg
, ss
->p
) == 0)
1073 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1079 if (seed
) die(EXIT_FAILURE
, "seed already set");
1081 base64_decode(&b
, optarg
, strlen(optarg
), &d
);
1082 base64_decode(&b
, 0, 0, &d
);
1083 k
.r
= sa
->gen(d
.buf
, d
.len
);
1092 unsigned n
= strtoul(optarg
, &p
, 0);
1093 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1094 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1095 if (seed
) die(EXIT_FAILURE
, "seed already set");
1098 rand_get(RAND_GLOBAL
, p
, n
);
1100 base64_encode(&b
, p
, n
, &d
);
1101 base64_encode(&b
, 0, 0, &d
);
1103 k
.r
= sa
->gen(p
, n
);
1106 /* --- Other flags --- */
1124 /* --- Other things are bogus --- */
1132 /* --- Various sorts of bogosity --- */
1134 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1136 "Usage: add [OPTIONS] TYPE [ATTR...]");
1138 if (key_chkident(argv
[optind
]))
1139 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1141 /* --- Set up various bits of the state --- */
1143 if (exp
== KEXP_EXPIRE
)
1144 exp
= time(0) + 14 * 24 * 60 * 60;
1146 /* --- Open the file and create the basic key block --- *
1148 * Keep on generating keyids until one of them doesn't collide.
1151 doopen(&f
, KOPEN_WRITE
);
1154 /* --- Key the generator --- */
1159 uint32 id
= rand_global
.ops
->word(&rand_global
);
1161 k
.k
= key_new(&f
, id
, argv
[optind
], exp
, &err
);
1164 if (err
!= KERR_DUPID
)
1165 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1168 /* --- Set various simple attributes --- */
1173 if (k
.f
& f_retag
) {
1174 if ((kk
= key_bytag(&f
, tag
)) != 0 && strcmp(kk
->tag
, tag
) == 0)
1175 key_settag(&f
, kk
, 0);
1177 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1178 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1182 int err
= key_setcomment(&f
, k
.k
, c
);
1184 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1187 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1189 key_putattr(&f
, k
.k
, "genseed", seed
);
1190 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1193 key_fulltag(k
.k
, &k
.tag
);
1195 /* --- Find the parameter key --- */
1198 if ((k
.p
= key_bytag(&f
, ptag
)) == 0)
1199 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1200 if ((k
.p
->k
.e
& KF_ENCMASK
) != KENC_STRUCT
)
1201 die(EXIT_FAILURE
, "parameter key `%s' is not structured", ptag
);
1204 /* --- Now generate the actual key data --- */
1214 /*----- Key listing -------------------------------------------------------*/
1216 /* --- Listing options --- */
1218 typedef struct listopts
{
1219 const char *tfmt
; /* Date format (@strftime@-style) */
1220 int v
; /* Verbosity level */
1221 unsigned f
; /* Various flags */
1222 time_t t
; /* Time now (for key expiry) */
1223 key_filter kf
; /* Filter for matching keys */
1226 /* --- Listing flags --- */
1228 #define f_newline 2u /* Write newline before next entry */
1229 #define f_attr 4u /* Written at least one attribute */
1230 #define f_utc 8u /* Emit UTC time, not local time */
1232 /* --- @showkeydata@ --- *
1234 * Arguments: @key_data *k@ = pointer to key to write
1235 * @int ind@ = indentation level
1236 * @listopts *o@ = listing options
1237 * @dstr *d@ = tag string for this subkey
1241 * Use: Emits a piece of key data in a human-readable format.
1244 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1246 #define INDENT(i) do { \
1248 for (_i = 0; _i < (i); _i++) { \
1253 switch (k
->e
& KF_ENCMASK
) {
1255 /* --- Binary key data --- *
1257 * Emit as a simple hex dump.
1261 const octet
*p
= k
->u
.k
.k
;
1262 const octet
*l
= p
+ k
->u
.k
.sz
;
1265 fputs(" {", stdout
);
1270 } else if (sz
% 8 == 0)
1274 printf("%02x", *p
++);
1279 fputs("}\n", stdout
);
1282 /* --- Encrypted data --- *
1284 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1285 * key. Otherwise just say that it's encrypted and move on.
1290 fputs(" encrypted\n", stdout
);
1293 if (key_punlock(d
->buf
, k
, &kd
))
1294 printf(" <failed to unlock %s>\n", d
->buf
);
1296 fputs(" encrypted", stdout
);
1297 showkeydata(&kd
, ind
, o
, d
);
1303 /* --- Integer keys --- *
1305 * Emit as a large integer in decimal. This makes using the key in
1306 * `calc' or whatever easier.
1311 mp_writefile(k
->u
.m
, stdout
, 10);
1315 /* --- Strings --- */
1318 printf(" `%s'\n", k
->u
.p
);
1321 /* --- Elliptic curve points --- */
1324 if (EC_ATINF(&k
->u
.e
))
1325 fputs(" inf\n", stdout
);
1327 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1328 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1333 /* --- Structured keys --- *
1335 * Just iterate over the subkeys.
1343 fputs(" {\n", stdout
);
1344 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
1345 if (!key_match(&ks
->k
, &o
->kf
))
1348 printf("%s =", SYM_NAME(ks
));
1350 dstr_putf(d
, ".%s", SYM_NAME(ks
));
1351 showkeydata(&ks
->k
, ind
+ 2, o
, d
);
1354 fputs("}\n", stdout
);
1361 /* --- @showkey@ --- *
1363 * Arguments: @key *k@ = pointer to key to show
1364 * @listopts *o@ = pointer to listing options
1368 * Use: Emits a listing of a particular key.
1371 static void showkey(key
*k
, listopts
*o
)
1373 char ebuf
[24], dbuf
[24];
1376 /* --- Skip the key if the filter doesn't match --- */
1378 if (!key_match(&k
->k
, &o
->kf
))
1381 /* --- Sort out the expiry and deletion times --- */
1383 if (KEY_EXPIRED(o
->t
, k
->exp
))
1384 strcpy(ebuf
, "expired");
1385 else if (k
->exp
== KEXP_FOREVER
)
1386 strcpy(ebuf
, "forever");
1388 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1389 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1392 if (KEY_EXPIRED(o
->t
, k
->del
))
1393 strcpy(dbuf
, "deleted");
1394 else if (k
->del
== KEXP_FOREVER
)
1395 strcpy(dbuf
, "forever");
1397 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1398 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1401 /* --- If in compact format, just display and quit --- */
1404 if (!(o
->f
& f_newline
)) {
1405 printf("%8s %-20s %-20s %-10s %-10s\n",
1406 "Id", "Tag", "Type", "Expire", "Delete");
1408 printf("%08lx %-20s %-20s %-10s %-10s\n",
1409 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1410 k
->type
, ebuf
, dbuf
);
1415 /* --- Display the standard header --- */
1417 if (o
->f
& f_newline
)
1418 fputc('\n', stdout
);
1419 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1420 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1421 printf("type: %s\n", k
->type
);
1422 printf("expiry: %s\n", ebuf
);
1423 printf("delete: %s\n", dbuf
);
1424 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1426 /* --- Display the attributes --- */
1430 const char *av
, *an
;
1433 printf("attributes:");
1434 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1435 printf("\n %s = %s", an
, av
);
1439 fputc('\n', stdout
);
1444 /* --- If dumping requested, dump the raw key data --- */
1448 fputs("key:", stdout
);
1450 showkeydata(&k
->k
, 0, o
, &d
);
1457 /* --- @cmd_list@ --- */
1459 static int cmd_list(int argc
, char *argv
[])
1463 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1465 /* --- Parse subcommand options --- */
1468 static struct option opt
[] = {
1469 { "quiet", 0, 0, 'q' },
1470 { "verbose", 0, 0, 'v' },
1471 { "utc", 0, 0, 'u' },
1472 { "filter", OPTF_ARGREQ
, 0, 'f' },
1475 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1492 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1494 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1503 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1505 /* --- Open the key file --- */
1507 doopen(&f
, KOPEN_READ
);
1510 /* --- Set up the time format --- */
1513 o
.tfmt
= "%Y-%m-%d";
1514 else if (o
.f
& f_utc
)
1515 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1517 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1519 /* --- If specific keys were requested use them, otherwise do all --- *
1521 * Some day, this might turn into a wildcard match.
1524 if (optind
< argc
) {
1526 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1529 moan("key `%s' not found", argv
[optind
]);
1533 } while (optind
< argc
);
1536 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1544 return (EXIT_FAILURE
);
1549 /*----- Command implementation --------------------------------------------*/
1551 /* --- @cmd_expire@ --- */
1553 static int cmd_expire(int argc
, char *argv
[])
1561 die(EXIT_FAILURE
, "Usage: expire TAG...");
1562 doopen(&f
, KOPEN_WRITE
);
1563 for (i
= 1; i
< argc
; i
++) {
1564 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1567 moan("key `%s' not found", argv
[i
]);
1575 /* --- @cmd_delete@ --- */
1577 static int cmd_delete(int argc
, char *argv
[])
1585 die(EXIT_FAILURE
, "Usage: delete TAG...");
1586 doopen(&f
, KOPEN_WRITE
);
1587 for (i
= 1; i
< argc
; i
++) {
1588 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1591 moan("key `%s' not found", argv
[i
]);
1599 /* --- @cmd_setattr@ --- */
1601 static int cmd_setattr(int argc
, char *argv
[])
1607 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1608 doopen(&f
, KOPEN_WRITE
);
1609 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1610 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1611 setattr(&f
, k
, argv
+ 2);
1616 /* --- @cmd_finger@ --- */
1618 static void fingerprint(key
*k
, const gchash
*ch
, const key_filter
*kf
)
1626 if (key_fingerprint(k
, h
, kf
)) {
1629 for (i
= 0; i
< ch
->hashsz
; i
++) {
1630 if (i
&& i
% 4 == 0)
1632 printf("%02x", p
[i
]);
1634 printf(" %s\n", d
.buf
);
1640 static int cmd_finger(int argc
, char *argv
[])
1644 const gchash
*ch
= &rmd160
;
1645 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1648 static struct option opt
[] = {
1649 { "filter", OPTF_ARGREQ
, 0, 'f' },
1650 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1653 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1659 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1661 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1664 if ((ch
= ghash_byname(optarg
)) == 0)
1665 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1673 argv
+= optind
; argc
-= optind
;
1675 die(EXIT_FAILURE
, "Usage: fingerprint [-f FILTER] [TAG...]");
1677 doopen(&f
, KOPEN_READ
);
1681 for (i
= 0; i
< argc
; i
++) {
1682 key
*k
= key_bytag(&f
, argv
[i
]);
1684 fingerprint(k
, ch
, &kf
);
1687 moan("key `%s' not found", argv
[i
]);
1693 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1694 fingerprint(k
, ch
, &kf
);
1699 /* --- @cmd_verify@ --- */
1701 static unsigned xdigit(char c
)
1703 if ('A' <= c
&& c
<= 'Z') return (c
+ 10 - 'A');
1704 if ('a' <= c
&& c
<= 'z') return (c
+ 10 - 'a');
1705 if ('0' <= c
&& c
<= '9') return (c
- '0');
1709 static void unhexify(octet
*q
, char *p
, size_t n
)
1715 if (*p
== '-' || *p
== ':' || isspace((unsigned char)*p
)) {
1722 die(EXIT_FAILURE
, "hex string too short");
1723 if (!isxdigit((unsigned char)*p
))
1724 die(EXIT_FAILURE
, "bad hex string");
1726 die(EXIT_FAILURE
, "hex string too long");
1727 a
= (a
<< 4) | xdigit(*p
++);
1738 static int cmd_verify(int argc
, char *argv
[])
1742 const gchash
*ch
= &rmd160
;
1747 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1750 static struct option opt
[] = {
1751 { "filter", OPTF_ARGREQ
, 0, 'f' },
1752 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1755 int i
= mdwopt(argc
, argv
, "+f:a:", opt
, 0, 0, 0);
1761 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1763 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1766 if ((ch
= ghash_byname(optarg
)) == 0)
1767 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1775 argv
+= optind
; argc
-= optind
;
1776 if (rc
|| argc
!= 2)
1777 die(EXIT_FAILURE
, "Usage: verify [-f FILTER] TAG FINGERPRINT");
1779 doopen(&f
, KOPEN_READ
);
1781 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1782 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1783 buf
= xmalloc(ch
->hashsz
);
1784 unhexify(buf
, argv
[1], ch
->hashsz
);
1786 if (!key_fingerprint(k
, h
, &kf
))
1787 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1788 fpr
= GH_DONE(h
, 0);
1789 if (memcmp(fpr
, buf
, ch
->hashsz
) != 0)
1790 die(EXIT_FAILURE
, "key fingerprint mismatch");
1794 /* --- @cmd_comment@ --- */
1796 static int cmd_comment(int argc
, char *argv
[])
1802 if (argc
< 2 || argc
> 3)
1803 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1804 doopen(&f
, KOPEN_WRITE
);
1805 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1806 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1807 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1808 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1813 /* --- @cmd_tag@ --- */
1815 static int cmd_tag(int argc
, char *argv
[])
1824 static struct option opt
[] = {
1825 { "retag", 0, 0, 'r' },
1828 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1841 argv
+= optind
; argc
-= optind
;
1842 if (argc
< 1 || argc
> 2 || rc
)
1843 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1844 doopen(&f
, KOPEN_WRITE
);
1845 if (flags
& f_retag
) {
1846 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1847 key_settag(&f
, k
, 0);
1849 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1850 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1851 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1852 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1857 /* --- @cmd_lock@ --- */
1859 static int cmd_lock(int argc
, char *argv
[])
1867 die(EXIT_FAILURE
, "Usage: lock QTAG");
1868 doopen(&f
, KOPEN_WRITE
);
1869 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1870 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1871 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1872 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1873 if (key_plock(d
.buf
, kd
, kd
))
1874 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
1880 /* --- @cmd_unlock@ --- */
1882 static int cmd_unlock(int argc
, char *argv
[])
1890 die(EXIT_FAILURE
, "Usage: unlock QTAG");
1891 doopen(&f
, KOPEN_WRITE
);
1892 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
1893 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1894 if (kd
->e
!= KENC_ENCRYPT
)
1895 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
1896 if (kd
->e
== KENC_ENCRYPT
&& key_punlock(d
.buf
, kd
, kd
))
1897 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
1903 /* --- @cmd_extract@ --- */
1905 static int cmd_extract(int argc
, char *argv
[])
1911 key_filter kf
= { 0, 0 };
1915 static struct option opt
[] = {
1916 { "filter", OPTF_ARGREQ
, 0, 'f' },
1919 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
1925 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1927 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1935 argv
+= optind
; argc
-= optind
;
1937 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
1938 if (strcmp(*argv
, "-") == 0)
1940 else if (!(fp
= fopen(*argv
, "w"))) {
1941 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
1942 *argv
, strerror(errno
));
1945 doopen(&f
, KOPEN_READ
);
1949 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1950 key_extract(&f
, k
, fp
, &kf
);
1952 for (i
= 1; i
< argc
; i
++) {
1953 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1954 key_extract(&f
, k
, fp
, &kf
);
1956 moan("key `%s' not found", argv
[i
]);
1962 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
1967 /* --- @cmd_tidy@ --- */
1969 static int cmd_tidy(int argc
, char *argv
[])
1973 die(EXIT_FAILURE
, "Usage: tidy");
1974 doopen(&f
, KOPEN_WRITE
);
1975 f
.f
|= KF_MODIFIED
; /* Nasty hack */
1980 /* --- @cmd_merge@ --- */
1982 static int cmd_merge(int argc
, char *argv
[])
1988 die(EXIT_FAILURE
, "Usage: merge FILE");
1989 if (strcmp(argv
[1], "-") == 0)
1991 else if (!(fp
= fopen(argv
[1], "r"))) {
1992 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
1993 argv
[1], strerror(errno
));
1996 doopen(&f
, KOPEN_WRITE
);
1997 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2002 /* --- @cmd_show@ --- */
2006 listtab[i].name, listtab[i].name) \
2007 LI("Hash functions", hash, \
2008 ghashtab[i], ghashtab[i]->name) \
2009 LI("Elliptic curves", ec, \
2010 ectab[i].name, ectab[i].name) \
2011 LI("Prime Diffie-Hellman groups", dh, \
2012 ptab[i].name, ptab[i].name) \
2013 LI("Binary Diffie-Hellman groups", bindh, \
2014 bintab[i].name, bintab[i].name) \
2015 LI("Key-generation algorithms", keygen, \
2016 algtab[i].name, algtab[i].name) \
2017 LI("Random seeding algorithms", seed, \
2018 seedtab[i].p, seedtab[i].p)
2020 MAKELISTTAB(listtab
, LISTS
)
2022 static int cmd_show(int argc
, char *argv
[])
2024 return (displaylists(listtab
, argv
+ 1));
2027 /*----- Main command table ------------------------------------------------*/
2029 static int cmd_help(int argc
, char *argv
[]);
2031 static cmd cmds
[] = {
2032 { "help", cmd_help
, "help [COMMAND...]" },
2033 { "show", cmd_show
, "show [ITEM...]" },
2034 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2037 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2038 -q, --quiet Show less information.\n\
2039 -v, --verbose Show more information.\n\
2041 { "fingerprint", cmd_finger
, "fingerprint [-f FILTER] [TAG...]", "\
2044 -f, --filter=FILT Only hash key components matching FILT.\n\
2045 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2046 ($ show hash for list.)\n\
2048 { "verify", cmd_verify
, "verify [-f FILTER] TAG FINGERPRINT", "\
2051 -f, --filter=FILT Only hash key components matching FILT.\n\
2052 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2053 ($ show hash for list.)\n\
2055 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2058 -f, --filter=FILT Only extract key components matching FILT.\n\
2060 { "merge", cmd_merge
, "merge FILE" },
2061 { "expire", cmd_expire
, "expire TAG..." },
2062 { "delete", cmd_delete
, "delete TAG..." },
2063 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2064 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2065 { "lock", cmd_lock
, "lock QTAG" },
2066 { "unlock", cmd_unlock
, "unlock QTAG" },
2067 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2070 -r, --retag Untag any key currently called new-tag.\n\
2072 { "tidy", cmd_tidy
, "tidy" },
2074 "add [-OPTIONS] TYPE [ATTR...]\n\
2075 Options: [-lqrLS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2076 [-A SEEDALG] [-s SEED] [-n BITS]\n\
2077 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2080 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2081 ($ show keygen for list.)\n\
2082 -b, --bits=N Generate an N-bit key.\n\
2083 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2084 -p, --parameters=TAG Get group parameters from TAG.\n\
2085 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2086 ($ show ec or $ show dh for list.)\n\
2087 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2088 ($ show seed for list.)\n\
2089 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2090 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2091 -e, --expire=TIME Make the key expire after TIME.\n\
2092 -c, --comment=STRING Attach the command STRING to the key.\n\
2093 -t, --tag=TAG Tag the key with the name TAG.\n\
2094 -r, --retag Untag any key currently with that tag.\n\
2095 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2096 -l, --lock Lock the generated key with a passphrase.\n\
2097 -q, --quiet Don't give progress indicators while working.\n\
2098 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2099 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2104 static int cmd_help(int argc
, char *argv
[])
2106 sc_help(cmds
, stdout
, argv
+ 1);
2110 /*----- Main code ---------------------------------------------------------*/
2112 /* --- Helpful GNUy functions --- */
2114 static void usage(FILE *fp
)
2116 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2119 void version(FILE *fp
)
2121 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2124 void help_global(FILE *fp
)
2128 Performs various simple key management operations.\n\
2130 Global command line options:\n\
2132 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2133 -v, --version Display version number.\n\
2134 -u, --usage Display short usage summary.\n\
2136 -k, --keyring=FILE Read and write keys in FILE.\n",
2142 * Arguments: @int argc@ = number of command line arguments
2143 * @char *argv[]@ = array of command line arguments
2145 * Returns: Nonzero on failure.
2147 * Use: Main program. Performs simple key management functions.
2150 int main(int argc
, char *argv
[])
2156 /* --- Initialization --- */
2161 /* --- Parse command line options --- */
2164 static struct option opt
[] = {
2166 /* --- Standard GNUy help options --- */
2168 { "help", 0, 0, 'h' },
2169 { "version", 0, 0, 'v' },
2170 { "usage", 0, 0, 'u' },
2172 /* --- Real live useful options --- */
2174 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2176 /* --- Magic terminator --- */
2180 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2186 /* --- GNU help options --- */
2189 sc_help(cmds
, stdout
, argv
+ optind
);
2198 /* --- Real useful options --- */
2204 /* --- Bogosity --- */
2212 /* --- Complain about excessive bogons --- */
2214 if (f
& f_bogus
|| optind
== argc
) {
2219 /* --- Initialize the Catacomb random number generator --- */
2221 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2222 rand_seed(RAND_GLOBAL
, 160);
2224 /* --- Dispatch to appropriate command handler --- */
2229 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2232 /*----- That's all, folks -------------------------------------------------*/