3 * Simple key manager program
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
41 #include <mLib/codec.h>
42 #include <mLib/base32.h>
43 #include <mLib/base64.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
78 #include "sha256-mgf.h"
79 #include "sha224-mgf.h"
80 #include "sha384-mgf.h"
81 #include "sha512-mgf.h"
82 #include "tiger-mgf.h"
83 #include "rmd128-mgf.h"
84 #include "rmd160-mgf.h"
85 #include "rmd256-mgf.h"
86 #include "rmd320-mgf.h"
90 /*----- Handy global state ------------------------------------------------*/
92 static const char *keyfile
= "keyring";
94 /*----- Useful shared functions -------------------------------------------*/
98 * Arguments: @key_file *f@ = pointer to key file block
99 * @unsigned how@ = method to open file with
103 * Use: Opens a key file and handles errors by panicking
107 static void doopen(key_file
*f
, unsigned how
)
109 if (key_open(f
, keyfile
, how
, key_moan
, 0)){
110 die(EXIT_FAILURE
, "couldn't open keyring `%s': %s",
111 keyfile
, strerror(errno
));
115 /* --- @doclose@ --- *
117 * Arguments: @key_file *f@ = pointer to key file block
121 * Use: Closes a key file and handles errors by panicking
125 static void doclose(key_file
*f
)
127 switch (key_close(f
)) {
129 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
130 keyfile
, strerror(errno
));
132 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
133 keyfile
, strerror(errno
));
137 /* --- @setattr@ --- *
139 * Arguments: @key_file *f@ = pointer to key file block
140 * @key *k@ = pointer to key block
141 * @char *v[]@ = array of assignments (overwritten!)
145 * Use: Applies the attribute assignments to the key.
148 static void setattr(key_file
*f
, key
*k
, char *v
[])
153 size_t eq
= strcspn(p
, "=");
155 moan("invalid assignment: `%s' (ignored)", p
);
161 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
162 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
167 /*----- Seeding -----------------------------------------------------------*/
169 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
171 { "dsarand", dsarand_create
},
172 { "rmd128-mgf", rmd128_mgfrand
},
173 { "rmd160-mgf", rmd160_mgfrand
},
174 { "rmd256-mgf", rmd256_mgfrand
},
175 { "rmd320-mgf", rmd320_mgfrand
},
176 { "sha-mgf", sha_mgfrand
},
177 { "sha224-mgf", sha224_mgfrand
},
178 { "sha256-mgf", sha256_mgfrand
},
179 { "sha384-mgf", sha384_mgfrand
},
180 { "sha512-mgf", sha512_mgfrand
},
181 { "tiger-mgf", tiger_mgfrand
},
185 #define SEEDALG_DEFAULT (seedtab + 2)
187 /*----- Key generation ----------------------------------------------------*/
189 /* --- Key generation parameters --- */
191 typedef struct keyopts
{
192 key_file
*kf
; /* Pointer to key file */
193 key
*k
; /* Pointer to the actual key */
194 dstr tag
; /* Full tag name for the key */
195 unsigned f
; /* Flags for the new key */
196 unsigned bits
, qbits
; /* Bit length for the new key */
197 const char *curve
; /* Elliptic curve name/info */
198 grand
*r
; /* Random number source */
199 key
*p
; /* Parameters key-data */
202 #define f_bogus 1u /* Error in parsing */
203 #define f_lock 2u /* Passphrase-lock private key */
204 #define f_quiet 4u /* Don't show a progress indicator */
205 #define f_limlee 8u /* Generate Lim-Lee primes */
206 #define f_subgroup 16u /* Generate a subgroup */
207 #define f_retag 32u /* Remove any existing tag */
208 #define f_kcdsa 64u /* Generate KCDSA primes */
210 /* --- @dolock@ --- *
212 * Arguments: @keyopts *k@ = key generation options
213 * @key_data **kd@ = pointer to key data to lock
214 * @const char *t@ = tag suffix or null
218 * Use: Does passphrase locking on new keys.
221 static void dolock(keyopts
*k
, key_data
**kd
, const char *t
)
223 if (!(k
->f
& f_lock
))
226 dstr_putf(&k
->tag
, ".%s", t
);
227 if (key_plock(kd
, 0, k
->tag
.buf
))
228 die(EXIT_FAILURE
, "couldn't lock key");
231 /* --- @copyparam@ --- *
233 * Arguments: @keyopts *k@ = pointer to key options
234 * @const char **pp@ = checklist of parameters, or null
236 * Returns: Nonzero if parameters copied; zero if you have to generate
239 * Use: Copies parameters from a source key to the current one.
242 static int copyparam(keyopts
*k
, const char **pp
)
253 /* --- Quick check if no parameters supplied --- */
258 /* --- Copy the key data if there's anything we want --- */
262 /* --- Run through the checklist --- */
264 key_fulltag(k
->p
, &t
);
265 if ((k
->p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
266 die(EXIT_FAILURE
, "parameter key `%s' is not structured", t
.buf
);
268 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
271 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
273 if (!KEY_MATCH(kd
, &kf
)) {
275 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
280 /* --- Copy over the parameters --- */
282 kd
= key_copydata(k
->p
->k
, &kf
);
284 key_setkeydata(k
->kf
, k
->k
, kd
);
288 /* --- Copy over attributes --- */
290 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
291 key_putattr(k
->kf
, k
->k
, n
, v
);
301 * Arguments: @key_data *k@ = pointer to key data block
302 * @const char *tag@ = tag string to use
304 * Returns: Pointer to multiprecision integer key item.
306 * Use: Fetches an MP key component.
309 static mp
*getmp(key_data
*k
, const char *tag
)
311 k
= key_structfind(k
, tag
);
313 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
314 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
315 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
319 /* --- @keyrand@ --- *
321 * Arguments: @key_file *kf@ = pointer to key file
322 * @const char *id@ = pointer to key id (or null)
326 * Use: Keys the random number generator.
329 static void keyrand(key_file
*kf
, const char *id
)
333 /* --- Find the key --- */
336 if ((k
= key_bytag(kf
, id
)) == 0)
337 die(EXIT_FAILURE
, "key `%s' not found", id
);
339 k
= key_bytype(kf
, "catacomb-rand");
342 key_data
*kd
= k
->k
, *kkd
;
346 switch (kd
->e
& KF_ENCMASK
) {
352 if (key_punlock(&kkd
, kd
, d
.buf
))
353 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
361 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
365 /* --- Key the generator --- */
367 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
372 /* --- Key generation algorithms --- */
374 static void alg_empty(keyopts
*k
)
377 key_setkeydata(k
->kf
, k
->k
,
378 key_newstring(KCAT_SHARE
, k
->curve ? k
->curve
: "."));
381 static void alg_binary(keyopts
*k
)
392 sz
= (k
->bits
+ 7) >> 3;
394 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
395 k
->r
->ops
->fill(k
->r
, p
, sz
);
397 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
400 key_setkeydata(k
->kf
, k
->k
, kd
);
405 static void alg_des(keyopts
*k
)
415 if (k
->bits
% 56 || k
->bits
> 168)
416 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
420 k
->r
->ops
->fill(k
->r
, p
, sz
);
421 for (i
= 0; i
< sz
; i
++) {
422 octet x
= p
[i
] | 0x01;
426 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
428 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
431 key_setkeydata(k
->kf
, k
->k
, kd
);
436 static void alg_rsa(keyopts
*k
)
441 /* --- Sanity checking --- */
447 /* --- Generate the RSA parameters --- */
449 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
450 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
451 die(EXIT_FAILURE
, "RSA key generation failed");
453 /* --- Run a test encryption --- */
456 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
458 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
463 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
464 c
= rsa_qprivop(&rp
, c
, c
, g
);
467 die(EXIT_FAILURE
, "test encryption failed");
473 /* --- Allrighty then --- */
475 kd
= key_newstruct();
476 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
477 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
479 kkd
= key_newstruct();
480 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
481 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
482 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
483 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
484 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
485 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
486 dolock(k
, &kkd
, "private");
487 key_structsteal(kd
, "private", kkd
);
488 key_setkeydata(k
->kf
, k
->k
, kd
);
493 static void alg_dsaparam(keyopts
*k
)
495 static const char *pl
[] = { "q", "p", "g", 0 };
496 if (!copyparam(k
, pl
)) {
505 /* --- Choose appropriate bit lengths if necessary --- */
512 /* --- Allocate a seed block --- */
514 sz
= (k
->qbits
+ 7) >> 3;
516 k
->r
->ops
->fill(k
->r
, p
, sz
);
518 /* --- Allocate the parameters --- */
520 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
521 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
522 die(EXIT_FAILURE
, "DSA parameter generation failed");
524 /* --- Store the parameters --- */
526 kd
= key_newstruct();
527 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
528 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
529 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
533 key_setkeydata(k
->kf
, k
->k
, kd
);
536 /* --- Store the seed for future verification --- */
538 c
= base64_class
.encoder(0, "", 0);
539 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
542 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
544 dstr_putf(&d
, "%u", ds
.count
);
545 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
552 static void alg_dsa(keyopts
*k
)
559 /* --- Get the shared parameters --- */
562 key_split(&k
->k
->k
); kd
= k
->k
->k
;
567 /* --- Choose a private key --- */
569 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
570 mpmont_create(&mm
, p
);
571 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
573 /* --- Store everything away --- */
575 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
577 kkd
= key_newstruct();
578 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
579 dolock(k
, &kkd
, "private");
580 key_structsteal(kd
, "private", kkd
);
582 mp_drop(x
); mp_drop(y
);
585 static void alg_dhparam(keyopts
*k
)
587 static const char *pl
[] = { "p", "q", "g", 0 };
589 if (!copyparam(k
, pl
)) {
598 if (strcmp(k
->curve
, "list") == 0) {
600 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
604 if (dh_parse(&qd
, &dp
))
605 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
607 die(EXIT_FAILURE
, "junk at end of field spec");
608 if ((g
= group_prime(&dp
)) == 0)
609 die(EXIT_FAILURE
, "invalid prime field");
610 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
611 moan("WARNING! group check failed: %s", e
);
619 /* --- Choose a large safe prime number --- */
621 if (k
->f
& f_limlee
) {
626 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
627 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
628 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
629 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
633 for (i
= 0; i
< nf
; i
++) {
636 mp_writedstr(f
[i
], &d
, 10);
639 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
642 } else if (k
->f
& f_kcdsa
) {
645 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
646 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
651 mp_writedstr(dp
.q
, &d
, 10);
652 mp_div(&v
, 0, dp
.p
, dp
.q
);
655 mp_writedstr(v
, &d
, 10);
657 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
661 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
662 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
665 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
668 kd
= key_newstruct();
669 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
670 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
671 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
675 key_setkeydata(k
->kf
, k
->k
, kd
);
680 static void alg_dh(keyopts
*k
)
687 /* --- Get the shared parameters --- */
690 key_split(&k
->k
->k
); kd
= k
->k
->k
;
695 /* --- Choose a suitable private key --- *
697 * Since %$g$% has order %$q$%, choose %$x < q$%.
700 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
702 /* --- Compute the public key %$y = g^x \bmod p$% --- */
704 mpmont_create(&mm
, p
);
705 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
708 /* --- Store everything away --- */
710 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
712 kkd
= key_newstruct();
713 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
714 dolock(k
, &kkd
, "private");
715 key_structsteal(kd
, "private", kkd
);
717 mp_drop(x
); mp_drop(y
);
720 static void alg_bbs(keyopts
*k
)
725 /* --- Sanity checking --- */
731 /* --- Generate the BBS parameters --- */
733 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
734 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
735 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
737 /* --- Allrighty then --- */
739 kd
= key_newstruct();
740 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
742 kkd
= key_newstruct();
743 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
744 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
745 dolock(k
, &kkd
, "private");
746 key_structsteal(kd
, "private", kkd
);
747 key_setkeydata(k
->kf
, k
->k
, kd
);
753 static void alg_binparam(keyopts
*k
)
755 static const char *pl
[] = { "p", "q", "g", 0 };
756 if (!copyparam(k
, pl
)) {
763 /* --- Decide on a field --- */
765 if (!k
->bits
) k
->bits
= 128;
766 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
768 LIST("Built-in binary fields", stdout
,
769 bintab
[i
].name
, bintab
[i
].name
);
773 if (k
->bits
<= 40) k
->curve
= "p1363-40";
774 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
775 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
776 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
777 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
778 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
781 "no built-in binary fields provide %u-bit security",
786 /* --- Check it --- */
790 if (dhbin_parse(&qd
, &gb
))
791 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
793 die(EXIT_FAILURE
, "junk at end of field spec");
794 if ((g
= group_binary(&gb
)) == 0)
795 die(EXIT_FAILURE
, "invalid binary field");
796 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
797 moan("WARNING! group check failed: %s", e
);
800 /* --- Write out the answer --- */
802 kd
= key_newstruct();
803 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
804 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
805 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
809 key_setkeydata(k
->kf
, k
->k
, kd
);
814 static void alg_bin(keyopts
*k
)
821 /* --- Get the shared parameters --- */
824 key_split(&k
->k
->k
); kd
= k
->k
->k
;
829 /* --- Choose a suitable private key --- *
831 * Since %$g$% has order %$q$%, choose %$x < q$%.
834 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
836 /* --- Compute the public key %$y = g^x \bmod p$% --- */
838 gfreduce_create(&r
, p
);
839 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
840 gfreduce_destroy(&r
);
842 /* --- Store everything away --- */
844 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
846 kkd
= key_newstruct();
847 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
848 dolock(k
, &kkd
, "private");
849 key_structsteal(kd
, "private", kkd
);
851 mp_drop(x
); mp_drop(y
);
854 static void alg_ecparam(keyopts
*k
)
856 static const char *pl
[] = { "curve", 0 };
857 if (!copyparam(k
, pl
)) {
862 /* --- Decide on a curve --- */
864 if (!k
->bits
) k
->bits
= 256;
865 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
867 LIST("Built-in elliptic curves", stdout
,
868 ectab
[i
].name
, ectab
[i
].name
);
872 if (k
->bits
<= 56) k
->curve
= "secp112r1";
873 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
874 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
875 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
876 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
877 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
878 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
879 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
881 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
885 /* --- Check it --- */
887 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
888 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
889 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
890 moan("WARNING! curve check failed: %s", e
);
893 /* --- Write out the answer --- */
895 kd
= key_newstruct();
896 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
897 key_setkeydata(k
->kf
, k
->k
, kd
);
902 static void alg_ec(keyopts
*k
)
911 /* --- Get the curve --- */
914 key_split(&k
->k
->k
); kd
= k
->k
->k
;
915 if ((kkd
= key_structfind(kd
, "curve")) == 0)
916 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
917 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
918 die(EXIT_FAILURE
, "subkey `curve' is not a string");
919 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
920 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
922 /* --- Invent a private exponent and compute the public key --- */
924 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
925 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
927 /* --- Store everything away --- */
929 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
931 kkd
= key_newstruct();
932 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
933 dolock(k
, &kkd
, "private");
934 key_structsteal(kd
, "private", kkd
);
942 static void alg_x25519(keyopts
*k
)
945 octet priv
[X25519_KEYSZ
], pub
[X25519_PUBSZ
];
948 k
->r
->ops
->fill(k
->r
, priv
, sizeof(priv
));
949 x25519(pub
, priv
, x25519_base
);
950 kkd
= key_newstruct();
951 key_structsteal(kkd
, "priv",
952 key_newbinary(KCAT_PRIV
| KF_BURN
, priv
, sizeof(priv
)));
953 kd
= key_newstruct();
954 key_structsteal(kd
, "private", kkd
);
955 key_structsteal(kd
, "pub", key_newbinary(KCAT_PUB
, pub
, sizeof(pub
)));
957 key_setkeydata(k
->kf
, k
->k
, kd
);
960 static void alg_x448(keyopts
*k
)
963 octet priv
[X448_KEYSZ
], pub
[X448_PUBSZ
];
966 k
->r
->ops
->fill(k
->r
, priv
, sizeof(priv
));
967 x448(pub
, priv
, x448_base
);
968 kkd
= key_newstruct();
969 key_structsteal(kkd
, "priv",
970 key_newbinary(KCAT_PRIV
| KF_BURN
, priv
, sizeof(priv
)));
971 kd
= key_newstruct();
972 key_structsteal(kd
, "private", kkd
);
973 key_structsteal(kd
, "pub", key_newbinary(KCAT_PUB
, pub
, sizeof(pub
)));
975 key_setkeydata(k
->kf
, k
->k
, kd
);
978 static void alg_ed25519(keyopts
*k
)
981 octet priv
[ED25519_KEYSZ
], pub
[ED25519_PUBSZ
];
984 k
->r
->ops
->fill(k
->r
, priv
, sizeof(priv
));
985 ed25519_pubkey(pub
, priv
, sizeof(priv
));
986 kkd
= key_newstruct();
987 key_structsteal(kkd
, "priv",
988 key_newbinary(KCAT_PRIV
| KF_BURN
, priv
, sizeof(priv
)));
989 kd
= key_newstruct();
990 key_structsteal(kd
, "private", kkd
);
991 key_structsteal(kd
, "pub", key_newbinary(KCAT_PUB
, pub
, sizeof(pub
)));
993 key_setkeydata(k
->kf
, k
->k
, kd
);
996 /* --- The algorithm tables --- */
998 typedef struct keyalg
{
1000 void (*proc
)(keyopts
*o
);
1004 static keyalg algtab
[] = {
1005 { "binary", alg_binary
, "Plain binary data" },
1006 { "des", alg_des
, "Binary with DES-style parity" },
1007 { "rsa", alg_rsa
, "RSA public-key encryption" },
1008 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
1009 { "dsa", alg_dsa
, "DSA digital signatures" },
1010 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
1011 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
1012 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
1013 { "bindh", alg_bin
, "DH over a binary field" },
1014 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
1015 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
1016 { "ec", alg_ec
, "Elliptic curve crypto" },
1017 { "x25519", alg_x25519
, "X25519 key exchange" },
1018 { "x448", alg_x448
, "X448 key exchange" },
1019 { "ed25519", alg_ed25519
, "Ed25519 digital signatures" },
1020 { "empty", alg_empty
, "Empty parametrs-only key" },
1024 /* --- @cmd_add@ --- */
1026 static int cmd_add(int argc
, char *argv
[])
1029 time_t exp
= KEXP_EXPIRE
;
1030 uint32 kid
= rand_global
.ops
->word(&rand_global
);
1031 const char *tag
= 0, *ptag
= 0;
1033 keyalg
*alg
= algtab
;
1034 const char *rtag
= 0;
1035 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
1036 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
1037 const char *seed
= 0;
1040 /* --- Parse options for the subcommand --- */
1043 static struct option opt
[] = {
1044 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1045 { "bits", OPTF_ARGREQ
, 0, 'b' },
1046 { "qbits", OPTF_ARGREQ
, 0, 'B' },
1047 { "parameters", OPTF_ARGREQ
, 0, 'p' },
1048 { "expire", OPTF_ARGREQ
, 0, 'e' },
1049 { "comment", OPTF_ARGREQ
, 0, 'c' },
1050 { "tag", OPTF_ARGREQ
, 0, 't' },
1051 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
1052 { "key-id", OPTF_ARGREQ
, 0, 'I' },
1053 { "curve", OPTF_ARGREQ
, 0, 'C' },
1054 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
1055 { "seed", OPTF_ARGREQ
, 0, 's' },
1056 { "newseed", OPTF_ARGREQ
, 0, 'n' },
1057 { "lock", 0, 0, 'l' },
1058 { "quiet", 0, 0, 'q' },
1059 { "lim-lee", 0, 0, 'L' },
1060 { "subgroup", 0, 0, 'S' },
1061 { "kcdsa", 0, 0, 'K' },
1064 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
1069 /* --- Handle the various options --- */
1073 /* --- Read an algorithm name --- */
1077 size_t sz
= strlen(optarg
);
1079 if (strcmp(optarg
, "list") == 0) {
1080 for (a
= algtab
; a
->name
; a
++)
1081 printf("%-10s %s\n", a
->name
, a
->help
);
1086 for (a
= algtab
; a
->name
; a
++) {
1087 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1088 if (a
->name
[sz
] == 0) {
1092 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1098 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1101 /* --- Bits must be nonzero and a multiple of 8 --- */
1105 k
.bits
= strtoul(optarg
, &p
, 0);
1106 if (k
.bits
== 0 || *p
!= 0)
1107 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1112 k
.qbits
= strtoul(optarg
, &p
, 0);
1113 if (k
.qbits
== 0 || *p
!= 0)
1114 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1117 /* --- Parameter selection --- */
1123 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1126 if (strcmp(optarg
, "forever") == 0)
1129 exp
= get_date(optarg
, 0);
1131 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1135 /* --- Store comments without interpretation --- */
1138 if (key_chkcomment(optarg
))
1139 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1143 /* --- Elliptic curve parameters --- */
1149 /* --- Store tags --- */
1152 if (key_chkident(optarg
))
1153 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1160 /* --- Seeding --- */
1163 const struct seedalg
*ss
;
1164 if (strcmp(optarg
, "list") == 0) {
1165 printf("Seed algorithms:\n");
1166 for (ss
= seedtab
; ss
->p
; ss
++)
1167 printf(" %s\n", ss
->p
);
1170 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1172 for (ss
= seedtab
; ss
->p
; ss
++) {
1173 if (strcmp(optarg
, ss
->p
) == 0)
1177 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1184 if (seed
) die(EXIT_FAILURE
, "seed already set");
1185 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1186 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1187 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1188 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1190 k
.r
= sa
->gen(d
.buf
, d
.len
);
1199 unsigned n
= strtoul(optarg
, &p
, 0);
1200 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1201 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1202 if (seed
) die(EXIT_FAILURE
, "seed already set");
1205 rand_get(RAND_GLOBAL
, p
, n
);
1206 c
= base64_class
.encoder(0, "", 0);
1207 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1210 k
.r
= sa
->gen(p
, n
);
1213 /* --- Key id --- */
1220 id
= strtoul(optarg
, &p
, 16);
1221 if (errno
|| *p
|| id
> MASK32
)
1222 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1226 /* --- Other flags --- */
1247 /* --- Other things are bogus --- */
1255 /* --- Various sorts of bogosity --- */
1257 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1259 "Usage: add [OPTIONS] TYPE [ATTR...]");
1261 if (key_chkident(argv
[optind
]))
1262 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1264 /* --- Set up various bits of the state --- */
1266 if (exp
== KEXP_EXPIRE
)
1267 exp
= time(0) + 14 * 24 * 60 * 60;
1269 /* --- Open the file and create the basic key block --- *
1271 * Keep on generating keyids until one of them doesn't collide.
1274 doopen(&f
, KOPEN_WRITE
);
1277 /* --- Key the generator --- */
1283 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1285 else if (err
!= KERR_DUPID
)
1286 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1289 /* --- Set various simple attributes --- */
1294 if (k
.f
& f_retag
) {
1295 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1297 strcmp(kk
->tag
, tag
) == 0)
1298 key_settag(&f
, kk
, 0);
1300 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1301 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1305 int err
= key_setcomment(&f
, k
.k
, c
);
1307 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1310 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1312 key_putattr(&f
, k
.k
, "genseed", seed
);
1313 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1316 key_fulltag(k
.k
, &k
.tag
);
1318 /* --- Find the parameter key --- */
1320 if (ptag
&& (k
.p
= key_bytag(&f
, ptag
)) == 0)
1321 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1323 /* --- Now generate the actual key data --- */
1329 dstr_destroy(&k
.tag
);
1334 /*----- Key listing -------------------------------------------------------*/
1336 /* --- Listing options --- */
1338 typedef struct listopts
{
1339 const char *tfmt
; /* Date format (@strftime@-style) */
1340 int v
; /* Verbosity level */
1341 unsigned f
; /* Various flags */
1342 time_t t
; /* Time now (for key expiry) */
1343 key_filter kf
; /* Filter for matching keys */
1346 /* --- Listing flags --- */
1348 #define f_newline 2u /* Write newline before next entry */
1349 #define f_attr 4u /* Written at least one attribute */
1350 #define f_utc 8u /* Emit UTC time, not local time */
1352 /* --- @showkeydata@ --- *
1354 * Arguments: @key_data *k@ = pointer to key to write
1355 * @int ind@ = indentation level
1356 * @listopts *o@ = listing options
1357 * @dstr *d@ = tag string for this subkey
1361 * Use: Emits a piece of key data in a human-readable format.
1364 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1366 #define INDENT(i) do { \
1368 for (_i = 0; _i < (i); _i++) { \
1373 switch (k
->e
& KF_ENCMASK
) {
1375 /* --- Binary key data --- *
1377 * Emit as a simple hex dump.
1381 const octet
*p
= k
->u
.k
.k
;
1382 const octet
*l
= p
+ k
->u
.k
.sz
;
1385 fputs(" {", stdout
);
1390 } else if (sz
% 8 == 0)
1394 printf("%02x", *p
++);
1399 fputs("}\n", stdout
);
1402 /* --- Encrypted data --- *
1404 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1405 * key. Otherwise just say that it's encrypted and move on.
1410 fputs(" encrypted\n", stdout
);
1413 if (key_punlock(&kd
, k
, d
->buf
))
1414 printf(" <failed to unlock %s>\n", d
->buf
);
1416 fputs(" encrypted", stdout
);
1417 showkeydata(kd
, ind
, o
, d
);
1423 /* --- Integer keys --- *
1425 * Emit as a large integer in decimal. This makes using the key in
1426 * `calc' or whatever easier.
1431 mp_writefile(k
->u
.m
, stdout
, 10);
1435 /* --- Strings --- */
1438 printf(" `%s'\n", k
->u
.p
);
1441 /* --- Elliptic curve points --- */
1444 if (EC_ATINF(&k
->u
.e
))
1445 fputs(" inf\n", stdout
);
1447 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1448 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1453 /* --- Structured keys --- *
1455 * Just iterate over the subkeys.
1463 fputs(" {\n", stdout
);
1464 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1465 if (!key_match(k
, &o
->kf
))
1468 printf("%s =", tag
);
1470 dstr_putf(d
, ".%s", tag
);
1471 showkeydata(k
, ind
+ 2, o
, d
);
1474 fputs("}\n", stdout
);
1481 /* --- @showkey@ --- *
1483 * Arguments: @key *k@ = pointer to key to show
1484 * @listopts *o@ = pointer to listing options
1488 * Use: Emits a listing of a particular key.
1491 static void showkey(key
*k
, listopts
*o
)
1493 char ebuf
[24], dbuf
[24];
1496 /* --- Skip the key if the filter doesn't match --- */
1498 if (!key_match(k
->k
, &o
->kf
))
1501 /* --- Sort out the expiry and deletion times --- */
1503 if (KEY_EXPIRED(o
->t
, k
->exp
))
1504 strcpy(ebuf
, "expired");
1505 else if (k
->exp
== KEXP_FOREVER
)
1506 strcpy(ebuf
, "forever");
1508 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1509 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1512 if (KEY_EXPIRED(o
->t
, k
->del
))
1513 strcpy(dbuf
, "deleted");
1514 else if (k
->del
== KEXP_FOREVER
)
1515 strcpy(dbuf
, "forever");
1517 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1518 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1521 /* --- If in compact format, just display and quit --- */
1524 if (!(o
->f
& f_newline
)) {
1525 printf("%8s %-20s %-20s %-10s %s\n",
1526 "Id", "Tag", "Type", "Expire", "Delete");
1528 printf("%08lx %-20s %-20s %-10s %s\n",
1529 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1530 k
->type
, ebuf
, dbuf
);
1535 /* --- Display the standard header --- */
1537 if (o
->f
& f_newline
)
1538 fputc('\n', stdout
);
1539 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1540 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1541 printf("type: %s\n", k
->type
);
1542 printf("expiry: %s\n", ebuf
);
1543 printf("delete: %s\n", dbuf
);
1544 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1546 /* --- Display the attributes --- */
1550 const char *av
, *an
;
1553 printf("attributes:");
1554 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1555 printf("\n %s = %s", an
, av
);
1559 fputc('\n', stdout
);
1564 /* --- If dumping requested, dump the raw key data --- */
1568 fputs("key:", stdout
);
1570 showkeydata(k
->k
, 0, o
, &d
);
1577 /* --- @cmd_list@ --- */
1579 static int cmd_list(int argc
, char *argv
[])
1583 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1585 /* --- Parse subcommand options --- */
1588 static struct option opt
[] = {
1589 { "quiet", 0, 0, 'q' },
1590 { "verbose", 0, 0, 'v' },
1591 { "utc", 0, 0, 'u' },
1592 { "filter", OPTF_ARGREQ
, 0, 'f' },
1595 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1612 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1614 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1623 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1625 /* --- Open the key file --- */
1627 doopen(&f
, KOPEN_READ
);
1630 /* --- Set up the time format --- */
1633 o
.tfmt
= "%Y-%m-%d";
1634 else if (o
.f
& f_utc
)
1635 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1637 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1639 /* --- If specific keys were requested use them, otherwise do all --- *
1641 * Some day, this might turn into a wildcard match.
1644 if (optind
< argc
) {
1646 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1649 moan("key `%s' not found", argv
[optind
]);
1653 } while (optind
< argc
);
1656 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1664 return (EXIT_FAILURE
);
1669 /*----- Command implementation --------------------------------------------*/
1671 /* --- @cmd_expire@ --- */
1673 static int cmd_expire(int argc
, char *argv
[])
1681 die(EXIT_FAILURE
, "Usage: expire TAG...");
1682 doopen(&f
, KOPEN_WRITE
);
1683 for (i
= 1; i
< argc
; i
++) {
1684 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1687 moan("key `%s' not found", argv
[i
]);
1695 /* --- @cmd_delete@ --- */
1697 static int cmd_delete(int argc
, char *argv
[])
1705 die(EXIT_FAILURE
, "Usage: delete TAG...");
1706 doopen(&f
, KOPEN_WRITE
);
1707 for (i
= 1; i
< argc
; i
++) {
1708 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1711 moan("key `%s' not found", argv
[i
]);
1719 /* --- @cmd_setattr@ --- */
1721 static int cmd_setattr(int argc
, char *argv
[])
1727 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1728 doopen(&f
, KOPEN_WRITE
);
1729 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1730 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1731 setattr(&f
, k
, argv
+ 2);
1736 /* --- @cmd_getattr@ --- */
1738 static int cmd_getattr(int argc
, char *argv
[])
1746 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1747 doopen(&f
, KOPEN_READ
);
1748 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1749 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1751 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1752 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1759 /* --- @cmd_finger@ --- */
1761 static const struct fpres
{
1763 const codec_class
*cdc
;
1764 unsigned short ival
;
1767 { "hex", &hex_class
, 8, "-:" },
1768 { "base32", &base32_class
, 6, ":" },
1772 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1773 const gchash
*ch
, const key_filter
*kf
)
1776 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1782 if (key_fingerprint(k
, h
, kf
)) {
1784 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1785 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1787 for (i
= 0; i
< dd
.len
; i
++) {
1788 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1789 dstr_putc(&d
, dd
.buf
[i
]);
1791 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1792 dstr_write(&d
, stdout
);
1794 dstr_destroy(&d
); dstr_destroy(&dd
);
1798 static const struct fpres
*lookup_fpres(const char *name
)
1800 const struct fpres
*fpres
;
1801 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1802 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1803 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1806 static int cmd_finger(int argc
, char *argv
[])
1810 const struct fpres
*fpres
= fprestab
;
1811 const gchash
*ch
= &rmd160
;
1812 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1815 static struct option opt
[] = {
1816 { "filter", OPTF_ARGREQ
, 0, 'f' },
1817 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1818 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1821 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1827 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1829 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1832 fpres
= lookup_fpres(optarg
);
1835 if ((ch
= ghash_byname(optarg
)) == 0)
1836 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1844 argv
+= optind
; argc
-= optind
;
1847 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1850 doopen(&f
, KOPEN_READ
);
1854 for (i
= 0; i
< argc
; i
++) {
1855 key
*k
= key_bytag(&f
, argv
[i
]);
1857 fingerprint(k
, fpres
, ch
, &kf
);
1860 moan("key `%s' not found", argv
[i
]);
1866 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1867 fingerprint(k
, fpres
, ch
, &kf
);
1872 /* --- @cmd_verify@ --- */
1874 static int cmd_verify(int argc
, char *argv
[])
1878 const gchash
*ch
= &rmd160
;
1882 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1885 const struct fpres
*fpres
= fprestab
;
1886 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1889 static struct option opt
[] = {
1890 { "filter", OPTF_ARGREQ
, 0, 'f' },
1891 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1892 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1895 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1901 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1903 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1906 fpres
= lookup_fpres(optarg
);
1909 if ((ch
= ghash_byname(optarg
)) == 0)
1910 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1918 argv
+= optind
; argc
-= optind
;
1919 if (rc
|| argc
!= 2) {
1921 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1924 doopen(&f
, KOPEN_READ
);
1926 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1927 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1928 for (p
= argv
[1]; *p
; p
++) {
1929 if (strchr(fpres
->sep
, *p
)) continue;
1932 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1933 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1934 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1935 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1936 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1938 if (d
.len
!= ch
->hashsz
) {
1939 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1940 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1943 if (!key_fingerprint(k
, h
, &kf
))
1944 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1945 fpr
= GH_DONE(h
, 0);
1946 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1947 die(EXIT_FAILURE
, "key fingerprint mismatch");
1948 dstr_destroy(&d
); dstr_destroy(&dd
);
1953 /* --- @cmd_comment@ --- */
1955 static int cmd_comment(int argc
, char *argv
[])
1961 if (argc
< 2 || argc
> 3)
1962 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1963 doopen(&f
, KOPEN_WRITE
);
1964 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1965 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1966 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1967 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1972 /* --- @cmd_tag@ --- */
1974 static int cmd_tag(int argc
, char *argv
[])
1983 static struct option opt
[] = {
1984 { "retag", 0, 0, 'r' },
1987 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
2000 argv
+= optind
; argc
-= optind
;
2001 if (argc
< 1 || argc
> 2 || rc
)
2002 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
2003 doopen(&f
, KOPEN_WRITE
);
2004 if (flags
& f_retag
) {
2005 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
2006 key_settag(&f
, k
, 0);
2008 if ((k
= key_bytag(&f
, argv
[0])) == 0)
2009 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
2010 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
2011 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
2016 /* --- @cmd_lock@ --- */
2018 static int cmd_lock(int argc
, char *argv
[])
2026 die(EXIT_FAILURE
, "Usage: lock QTAG");
2027 doopen(&f
, KOPEN_WRITE
);
2028 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2029 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2030 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
2031 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2032 if (key_plock(kd
, 0, d
.buf
))
2033 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
2039 /* --- @cmd_unlock@ --- */
2041 static int cmd_unlock(int argc
, char *argv
[])
2049 die(EXIT_FAILURE
, "Usage: unlock QTAG");
2050 doopen(&f
, KOPEN_WRITE
);
2051 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2052 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2053 if ((*kd
)->e
!= KENC_ENCRYPT
)
2054 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
2055 if (key_punlock(kd
, 0, d
.buf
))
2056 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2062 /* --- @cmd_extract@ --- */
2064 static int cmd_extract(int argc
, char *argv
[])
2070 key_filter kf
= { 0, 0 };
2072 const char *outfile
= 0;
2076 static struct option opt
[] = {
2077 { "filter", OPTF_ARGREQ
, 0, 'f' },
2080 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2086 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2088 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2096 argv
+= optind
; argc
-= optind
;
2098 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2099 if (strcmp(*argv
, "-") == 0)
2103 dstr_putf(&d
, "%s.new", outfile
);
2104 if (!(fp
= fopen(d
.buf
, "w"))) {
2105 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2106 d
.buf
, strerror(errno
));
2110 doopen(&f
, KOPEN_READ
);
2114 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2115 key_extract(&f
, k
, fp
, &kf
);
2117 for (i
= 1; i
< argc
; i
++) {
2118 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2119 key_extract(&f
, k
, fp
, &kf
);
2121 moan("key `%s' not found", argv
[i
]);
2126 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2127 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2133 /* --- @cmd_tidy@ --- */
2135 static int cmd_tidy(int argc
, char *argv
[])
2139 die(EXIT_FAILURE
, "Usage: tidy");
2140 doopen(&f
, KOPEN_WRITE
);
2141 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2146 /* --- @cmd_merge@ --- */
2148 static int cmd_merge(int argc
, char *argv
[])
2154 die(EXIT_FAILURE
, "Usage: merge FILE");
2155 if (strcmp(argv
[1], "-") == 0)
2157 else if (!(fp
= fopen(argv
[1], "r"))) {
2158 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2159 argv
[1], strerror(errno
));
2162 doopen(&f
, KOPEN_WRITE
);
2163 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2168 /* --- @cmd_show@ --- */
2172 listtab[i].name, listtab[i].name) \
2173 LI("Hash functions", hash, \
2174 ghashtab[i], ghashtab[i]->name) \
2175 LI("Elliptic curves", ec, \
2176 ectab[i].name, ectab[i].name) \
2177 LI("Prime Diffie-Hellman groups", dh, \
2178 ptab[i].name, ptab[i].name) \
2179 LI("Binary Diffie-Hellman groups", bindh, \
2180 bintab[i].name, bintab[i].name) \
2181 LI("Key-generation algorithms", keygen, \
2182 algtab[i].name, algtab[i].name) \
2183 LI("Random seeding algorithms", seed, \
2184 seedtab[i].p, seedtab[i].p) \
2185 LI("Fingerprint presentation styles", fpres, \
2186 fprestab[i].name, fprestab[i].name)
2188 MAKELISTTAB(listtab
, LISTS
)
2190 static int cmd_show(int argc
, char *argv
[])
2192 return (displaylists(listtab
, argv
+ 1));
2195 /*----- Main command table ------------------------------------------------*/
2197 static int cmd_help(int argc
, char *argv
[]);
2199 static cmd cmds
[] = {
2200 { "help", cmd_help
, "help [COMMAND...]" },
2201 { "show", cmd_show
, "show [ITEM...]" },
2202 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2205 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2206 -q, --quiet Show less information.\n\
2207 -v, --verbose Show more information.\n\
2209 { "fingerprint", cmd_finger
,
2210 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2213 -f, --filter=FILT Only hash key components matching FILT.\n\
2214 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2215 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2216 ($ show hash for list.)\n\
2218 { "verify", cmd_verify
,
2219 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2222 -f, --filter=FILT Only hash key components matching FILT.\n\
2223 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2224 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2225 ($ show hash for list.)\n\
2227 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2230 -f, --filter=FILT Only extract key components matching FILT.\n\
2232 { "merge", cmd_merge
, "merge FILE" },
2233 { "expire", cmd_expire
, "expire TAG..." },
2234 { "delete", cmd_delete
, "delete TAG..." },
2235 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2236 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2237 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2238 { "lock", cmd_lock
, "lock QTAG" },
2239 { "unlock", cmd_unlock
, "unlock QTAG" },
2240 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2243 -r, --retag Untag any key currently called new-tag.\n\
2245 { "tidy", cmd_tidy
, "tidy" },
2247 "add [-OPTIONS] TYPE [ATTR...]\n\
2248 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2249 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2250 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2253 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2254 ($ show keygen for list.)\n\
2255 -b, --bits=N Generate an N-bit key.\n\
2256 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2257 -p, --parameters=TAG Get group parameters from TAG.\n\
2258 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2259 ($ show ec or $ show dh for list.)\n\
2260 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2261 ($ show seed for list.)\n\
2262 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2263 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2264 -e, --expire=TIME Make the key expire after TIME.\n\
2265 -c, --comment=STRING Attach the command STRING to the key.\n\
2266 -t, --tag=TAG Tag the key with the name TAG.\n\
2267 -r, --retag Untag any key currently with that tag.\n\
2268 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2269 -I, --key-id=ID Force the key-id for the new key.\n\
2270 -l, --lock Lock the generated key with a passphrase.\n\
2271 -q, --quiet Don't give progress indicators while working.\n\
2272 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2273 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2274 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2279 static int cmd_help(int argc
, char *argv
[])
2281 sc_help(cmds
, stdout
, argv
+ 1);
2285 /*----- Main code ---------------------------------------------------------*/
2287 /* --- Helpful GNUy functions --- */
2289 static void usage(FILE *fp
)
2291 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2294 void version(FILE *fp
)
2296 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2299 void help_global(FILE *fp
)
2303 Performs various simple key management operations.\n\
2305 Global command line options:\n\
2307 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2308 -v, --version Display version number.\n\
2309 -u, --usage Display short usage summary.\n\
2311 -k, --keyring=FILE Read and write keys in FILE.\n",
2317 * Arguments: @int argc@ = number of command line arguments
2318 * @char *argv[]@ = array of command line arguments
2320 * Returns: Nonzero on failure.
2322 * Use: Main program. Performs simple key management functions.
2325 int main(int argc
, char *argv
[])
2331 /* --- Initialization --- */
2336 /* --- Parse command line options --- */
2339 static struct option opt
[] = {
2341 /* --- Standard GNUy help options --- */
2343 { "help", 0, 0, 'h' },
2344 { "version", 0, 0, 'v' },
2345 { "usage", 0, 0, 'u' },
2347 /* --- Real live useful options --- */
2349 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2351 /* --- Magic terminator --- */
2355 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2361 /* --- GNU help options --- */
2364 sc_help(cmds
, stdout
, argv
+ optind
);
2373 /* --- Real useful options --- */
2379 /* --- Bogosity --- */
2387 /* --- Complain about excessive bogons --- */
2389 if (f
& f_bogus
|| optind
== argc
) {
2394 /* --- Initialize the Catacomb random number generator --- */
2396 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2397 rand_seed(RAND_GLOBAL
, 160);
2399 /* --- Dispatch to appropriate command handler --- */
2404 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2407 /*----- That's all, folks -------------------------------------------------*/