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>
77 #include "sha256-mgf.h"
78 #include "sha224-mgf.h"
79 #include "sha384-mgf.h"
80 #include "sha512-mgf.h"
81 #include "tiger-mgf.h"
82 #include "rmd128-mgf.h"
83 #include "rmd160-mgf.h"
84 #include "rmd256-mgf.h"
85 #include "rmd320-mgf.h"
89 /*----- Handy global state ------------------------------------------------*/
91 static const char *keyfile
= "keyring";
93 /*----- Useful shared functions -------------------------------------------*/
97 * Arguments: @key_file *f@ = pointer to key file block
98 * @unsigned how@ = method to open file with
102 * Use: Opens a key file and handles errors by panicking
106 static void doopen(key_file
*f
, unsigned how
)
108 if (key_open(f
, keyfile
, how
, key_moan
, 0)){
109 die(EXIT_FAILURE
, "couldn't open keyring `%s': %s",
110 keyfile
, strerror(errno
));
114 /* --- @doclose@ --- *
116 * Arguments: @key_file *f@ = pointer to key file block
120 * Use: Closes a key file and handles errors by panicking
124 static void doclose(key_file
*f
)
126 switch (key_close(f
)) {
128 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
129 keyfile
, strerror(errno
));
131 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
132 keyfile
, strerror(errno
));
136 /* --- @setattr@ --- *
138 * Arguments: @key_file *f@ = pointer to key file block
139 * @key *k@ = pointer to key block
140 * @char *v[]@ = array of assignments (overwritten!)
144 * Use: Applies the attribute assignments to the key.
147 static void setattr(key_file
*f
, key
*k
, char *v
[])
152 size_t eq
= strcspn(p
, "=");
154 moan("invalid assignment: `%s' (ignored)", p
);
160 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
161 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
166 /*----- Seeding -----------------------------------------------------------*/
168 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
170 { "dsarand", dsarand_create
},
171 { "rmd128-mgf", rmd128_mgfrand
},
172 { "rmd160-mgf", rmd160_mgfrand
},
173 { "rmd256-mgf", rmd256_mgfrand
},
174 { "rmd320-mgf", rmd320_mgfrand
},
175 { "sha-mgf", sha_mgfrand
},
176 { "sha224-mgf", sha224_mgfrand
},
177 { "sha256-mgf", sha256_mgfrand
},
178 { "sha384-mgf", sha384_mgfrand
},
179 { "sha512-mgf", sha512_mgfrand
},
180 { "tiger-mgf", tiger_mgfrand
},
184 #define SEEDALG_DEFAULT (seedtab + 2)
186 /*----- Key generation ----------------------------------------------------*/
188 /* --- Key generation parameters --- */
190 typedef struct keyopts
{
191 key_file
*kf
; /* Pointer to key file */
192 key
*k
; /* Pointer to the actual key */
193 dstr tag
; /* Full tag name for the key */
194 unsigned f
; /* Flags for the new key */
195 unsigned bits
, qbits
; /* Bit length for the new key */
196 const char *curve
; /* Elliptic curve name/info */
197 grand
*r
; /* Random number source */
198 key
*p
; /* Parameters key-data */
201 #define f_bogus 1u /* Error in parsing */
202 #define f_lock 2u /* Passphrase-lock private key */
203 #define f_quiet 4u /* Don't show a progress indicator */
204 #define f_limlee 8u /* Generate Lim-Lee primes */
205 #define f_subgroup 16u /* Generate a subgroup */
206 #define f_retag 32u /* Remove any existing tag */
207 #define f_kcdsa 64u /* Generate KCDSA primes */
209 /* --- @dolock@ --- *
211 * Arguments: @keyopts *k@ = key generation options
212 * @key_data **kd@ = pointer to key data to lock
213 * @const char *t@ = tag suffix or null
217 * Use: Does passphrase locking on new keys.
220 static void dolock(keyopts
*k
, key_data
**kd
, const char *t
)
222 if (!(k
->f
& f_lock
))
225 dstr_putf(&k
->tag
, ".%s", t
);
226 if (key_plock(kd
, 0, k
->tag
.buf
))
227 die(EXIT_FAILURE
, "couldn't lock key");
230 /* --- @copyparam@ --- *
232 * Arguments: @keyopts *k@ = pointer to key options
233 * @const char **pp@ = checklist of parameters, or null
235 * Returns: Nonzero if parameters copied; zero if you have to generate
238 * Use: Copies parameters from a source key to the current one.
241 static int copyparam(keyopts
*k
, const char **pp
)
252 /* --- Quick check if no parameters supplied --- */
257 /* --- Copy the key data if there's anything we want --- */
261 /* --- Run through the checklist --- */
263 key_fulltag(k
->p
, &t
);
264 if ((k
->p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
265 die(EXIT_FAILURE
, "parameter key `%s' is not structured", t
.buf
);
267 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
270 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
272 if (!KEY_MATCH(kd
, &kf
)) {
274 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
279 /* --- Copy over the parameters --- */
281 kd
= key_copydata(k
->p
->k
, &kf
);
283 key_setkeydata(k
->kf
, k
->k
, kd
);
287 /* --- Copy over attributes --- */
289 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
290 key_putattr(k
->kf
, k
->k
, n
, v
);
300 * Arguments: @key_data *k@ = pointer to key data block
301 * @const char *tag@ = tag string to use
303 * Returns: Pointer to multiprecision integer key item.
305 * Use: Fetches an MP key component.
308 static mp
*getmp(key_data
*k
, const char *tag
)
310 k
= key_structfind(k
, tag
);
312 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
313 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
314 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
318 /* --- @keyrand@ --- *
320 * Arguments: @key_file *kf@ = pointer to key file
321 * @const char *id@ = pointer to key id (or null)
325 * Use: Keys the random number generator.
328 static void keyrand(key_file
*kf
, const char *id
)
332 /* --- Find the key --- */
335 if ((k
= key_bytag(kf
, id
)) == 0)
336 die(EXIT_FAILURE
, "key `%s' not found", id
);
338 k
= key_bytype(kf
, "catacomb-rand");
341 key_data
*kd
= k
->k
, *kkd
;
345 switch (kd
->e
& KF_ENCMASK
) {
351 if (key_punlock(&kkd
, kd
, d
.buf
))
352 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
360 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
364 /* --- Key the generator --- */
366 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
371 /* --- Key generation algorithms --- */
373 static void alg_empty(keyopts
*k
)
376 key_setkeydata(k
->kf
, k
->k
,
377 key_newstring(KCAT_SHARE
, k
->curve ? k
->curve
: "."));
380 static void alg_binary(keyopts
*k
)
391 sz
= (k
->bits
+ 7) >> 3;
393 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
394 k
->r
->ops
->fill(k
->r
, p
, sz
);
396 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
399 key_setkeydata(k
->kf
, k
->k
, kd
);
404 static void alg_des(keyopts
*k
)
414 if (k
->bits
% 56 || k
->bits
> 168)
415 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
419 k
->r
->ops
->fill(k
->r
, p
, sz
);
420 for (i
= 0; i
< sz
; i
++) {
421 octet x
= p
[i
] | 0x01;
425 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
427 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
430 key_setkeydata(k
->kf
, k
->k
, kd
);
435 static void alg_rsa(keyopts
*k
)
440 /* --- Sanity checking --- */
446 /* --- Generate the RSA parameters --- */
448 if (rsa_gen(&rp
, k
->bits
, k
->r
, 0,
449 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
450 die(EXIT_FAILURE
, "RSA key generation failed");
452 /* --- Run a test encryption --- */
455 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
457 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
462 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
463 c
= rsa_qprivop(&rp
, c
, c
, g
);
466 die(EXIT_FAILURE
, "test encryption failed");
472 /* --- Allrighty then --- */
474 kd
= key_newstruct();
475 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
476 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
478 kkd
= key_newstruct();
479 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
480 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
481 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
482 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
483 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
484 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
485 dolock(k
, &kkd
, "private");
486 key_structsteal(kd
, "private", kkd
);
487 key_setkeydata(k
->kf
, k
->k
, kd
);
492 static void alg_dsaparam(keyopts
*k
)
494 static const char *pl
[] = { "q", "p", "g", 0 };
495 if (!copyparam(k
, pl
)) {
504 /* --- Choose appropriate bit lengths if necessary --- */
511 /* --- Allocate a seed block --- */
513 sz
= (k
->qbits
+ 7) >> 3;
515 k
->r
->ops
->fill(k
->r
, p
, sz
);
517 /* --- Allocate the parameters --- */
519 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
520 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
521 die(EXIT_FAILURE
, "DSA parameter generation failed");
523 /* --- Store the parameters --- */
525 kd
= key_newstruct();
526 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
527 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
528 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
532 key_setkeydata(k
->kf
, k
->k
, kd
);
535 /* --- Store the seed for future verification --- */
537 c
= base64_class
.encoder(0, "", 0);
538 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
541 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
543 dstr_putf(&d
, "%u", ds
.count
);
544 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
551 static void alg_dsa(keyopts
*k
)
558 /* --- Get the shared parameters --- */
561 key_split(&k
->k
->k
); kd
= k
->k
->k
;
566 /* --- Choose a private key --- */
568 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
569 mpmont_create(&mm
, p
);
570 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
572 /* --- Store everything away --- */
574 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
576 kkd
= key_newstruct();
577 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
578 dolock(k
, &kkd
, "private");
579 key_structsteal(kd
, "private", kkd
);
581 mp_drop(x
); mp_drop(y
);
584 static void alg_dhparam(keyopts
*k
)
586 static const char *pl
[] = { "p", "q", "g", 0 };
588 if (!copyparam(k
, pl
)) {
597 if (strcmp(k
->curve
, "list") == 0) {
599 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
603 if (dh_parse(&qd
, &dp
))
604 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
606 die(EXIT_FAILURE
, "junk at end of field spec");
607 if ((g
= group_prime(&dp
)) == 0)
608 die(EXIT_FAILURE
, "invalid prime field");
609 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
610 moan("WARNING! group check failed: %s", e
);
618 /* --- Choose a large safe prime number --- */
620 if (k
->f
& f_limlee
) {
625 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
626 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
627 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
628 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
632 for (i
= 0; i
< nf
; i
++) {
635 mp_writedstr(f
[i
], &d
, 10);
638 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
641 } else if (k
->f
& f_kcdsa
) {
644 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
645 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
650 mp_writedstr(dp
.q
, &d
, 10);
651 mp_div(&v
, 0, dp
.p
, dp
.q
);
654 mp_writedstr(v
, &d
, 10);
656 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
660 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
661 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
664 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
667 kd
= key_newstruct();
668 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
669 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
670 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
674 key_setkeydata(k
->kf
, k
->k
, kd
);
679 static void alg_dh(keyopts
*k
)
686 /* --- Get the shared parameters --- */
689 key_split(&k
->k
->k
); kd
= k
->k
->k
;
694 /* --- Choose a suitable private key --- *
696 * Since %$g$% has order %$q$%, choose %$x < q$%.
699 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
701 /* --- Compute the public key %$y = g^x \bmod p$% --- */
703 mpmont_create(&mm
, p
);
704 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
707 /* --- Store everything away --- */
709 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
711 kkd
= key_newstruct();
712 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
713 dolock(k
, &kkd
, "private");
714 key_structsteal(kd
, "private", kkd
);
716 mp_drop(x
); mp_drop(y
);
719 static void alg_bbs(keyopts
*k
)
724 /* --- Sanity checking --- */
730 /* --- Generate the BBS parameters --- */
732 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
733 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
734 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
736 /* --- Allrighty then --- */
738 kd
= key_newstruct();
739 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
741 kkd
= key_newstruct();
742 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
743 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
744 dolock(k
, &kkd
, "private");
745 key_structsteal(kd
, "private", kkd
);
746 key_setkeydata(k
->kf
, k
->k
, kd
);
752 static void alg_binparam(keyopts
*k
)
754 static const char *pl
[] = { "p", "q", "g", 0 };
755 if (!copyparam(k
, pl
)) {
762 /* --- Decide on a field --- */
764 if (!k
->bits
) k
->bits
= 128;
765 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
767 LIST("Built-in binary fields", stdout
,
768 bintab
[i
].name
, bintab
[i
].name
);
772 if (k
->bits
<= 40) k
->curve
= "p1363-40";
773 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
774 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
775 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
776 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
777 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
780 "no built-in binary fields provide %u-bit security",
785 /* --- Check it --- */
789 if (dhbin_parse(&qd
, &gb
))
790 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
792 die(EXIT_FAILURE
, "junk at end of field spec");
793 if ((g
= group_binary(&gb
)) == 0)
794 die(EXIT_FAILURE
, "invalid binary field");
795 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
796 moan("WARNING! group check failed: %s", e
);
799 /* --- Write out the answer --- */
801 kd
= key_newstruct();
802 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
803 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
804 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
808 key_setkeydata(k
->kf
, k
->k
, kd
);
813 static void alg_bin(keyopts
*k
)
820 /* --- Get the shared parameters --- */
823 key_split(&k
->k
->k
); kd
= k
->k
->k
;
828 /* --- Choose a suitable private key --- *
830 * Since %$g$% has order %$q$%, choose %$x < q$%.
833 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
835 /* --- Compute the public key %$y = g^x \bmod p$% --- */
837 gfreduce_create(&r
, p
);
838 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
839 gfreduce_destroy(&r
);
841 /* --- Store everything away --- */
843 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
845 kkd
= key_newstruct();
846 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
847 dolock(k
, &kkd
, "private");
848 key_structsteal(kd
, "private", kkd
);
850 mp_drop(x
); mp_drop(y
);
853 static void alg_ecparam(keyopts
*k
)
855 static const char *pl
[] = { "curve", 0 };
856 if (!copyparam(k
, pl
)) {
861 /* --- Decide on a curve --- */
863 if (!k
->bits
) k
->bits
= 256;
864 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
866 LIST("Built-in elliptic curves", stdout
,
867 ectab
[i
].name
, ectab
[i
].name
);
871 if (k
->bits
<= 56) k
->curve
= "secp112r1";
872 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
873 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
874 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
875 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
876 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
877 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
878 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
880 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
884 /* --- Check it --- */
886 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
887 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
888 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
889 moan("WARNING! curve check failed: %s", e
);
892 /* --- Write out the answer --- */
894 kd
= key_newstruct();
895 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
896 key_setkeydata(k
->kf
, k
->k
, kd
);
901 static void alg_ec(keyopts
*k
)
910 /* --- Get the curve --- */
913 key_split(&k
->k
->k
); kd
= k
->k
->k
;
914 if ((kkd
= key_structfind(kd
, "curve")) == 0)
915 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
916 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
917 die(EXIT_FAILURE
, "subkey `curve' is not a string");
918 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
919 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
921 /* --- Invent a private exponent and compute the public key --- */
923 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
924 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
926 /* --- Store everything away --- */
928 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
930 kkd
= key_newstruct();
931 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
932 dolock(k
, &kkd
, "private");
933 key_structsteal(kd
, "private", kkd
);
941 static void alg_x25519(keyopts
*k
)
944 octet priv
[X25519_KEYSZ
], pub
[X25519_PUBSZ
];
947 k
->r
->ops
->fill(k
->r
, priv
, sizeof(priv
));
948 x25519(pub
, priv
, x25519_base
);
949 kkd
= key_newstruct();
950 key_structsteal(kkd
, "priv",
951 key_newbinary(KCAT_PRIV
| KF_BURN
, priv
, sizeof(priv
)));
952 kd
= key_newstruct();
953 key_structsteal(kd
, "private", kkd
);
954 key_structsteal(kd
, "pub", key_newbinary(KCAT_PUB
, pub
, sizeof(pub
)));
956 key_setkeydata(k
->kf
, k
->k
, kd
);
959 static void alg_ed25519(keyopts
*k
)
962 octet priv
[ED25519_KEYSZ
], pub
[ED25519_PUBSZ
];
965 k
->r
->ops
->fill(k
->r
, priv
, sizeof(priv
));
966 ed25519_pubkey(pub
, priv
, sizeof(priv
));
967 kkd
= key_newstruct();
968 key_structsteal(kkd
, "priv",
969 key_newbinary(KCAT_PRIV
| KF_BURN
, priv
, sizeof(priv
)));
970 kd
= key_newstruct();
971 key_structsteal(kd
, "private", kkd
);
972 key_structsteal(kd
, "pub", key_newbinary(KCAT_PUB
, pub
, sizeof(pub
)));
974 key_setkeydata(k
->kf
, k
->k
, kd
);
977 /* --- The algorithm tables --- */
979 typedef struct keyalg
{
981 void (*proc
)(keyopts
*o
);
985 static keyalg algtab
[] = {
986 { "binary", alg_binary
, "Plain binary data" },
987 { "des", alg_des
, "Binary with DES-style parity" },
988 { "rsa", alg_rsa
, "RSA public-key encryption" },
989 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
990 { "dsa", alg_dsa
, "DSA digital signatures" },
991 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
992 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
993 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
994 { "bindh", alg_bin
, "DH over a binary field" },
995 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
996 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
997 { "ec", alg_ec
, "Elliptic curve crypto" },
998 { "x25519", alg_x25519
, "X25519 key exchange" },
999 { "ed25519", alg_ed25519
, "Ed25519 digital signatures" },
1000 { "empty", alg_empty
, "Empty parametrs-only key" },
1004 /* --- @cmd_add@ --- */
1006 static int cmd_add(int argc
, char *argv
[])
1009 time_t exp
= KEXP_EXPIRE
;
1010 uint32 kid
= rand_global
.ops
->word(&rand_global
);
1011 const char *tag
= 0, *ptag
= 0;
1013 keyalg
*alg
= algtab
;
1014 const char *rtag
= 0;
1015 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
1016 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0 };
1017 const char *seed
= 0;
1020 /* --- Parse options for the subcommand --- */
1023 static struct option opt
[] = {
1024 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1025 { "bits", OPTF_ARGREQ
, 0, 'b' },
1026 { "qbits", OPTF_ARGREQ
, 0, 'B' },
1027 { "parameters", OPTF_ARGREQ
, 0, 'p' },
1028 { "expire", OPTF_ARGREQ
, 0, 'e' },
1029 { "comment", OPTF_ARGREQ
, 0, 'c' },
1030 { "tag", OPTF_ARGREQ
, 0, 't' },
1031 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
1032 { "key-id", OPTF_ARGREQ
, 0, 'I' },
1033 { "curve", OPTF_ARGREQ
, 0, 'C' },
1034 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
1035 { "seed", OPTF_ARGREQ
, 0, 's' },
1036 { "newseed", OPTF_ARGREQ
, 0, 'n' },
1037 { "lock", 0, 0, 'l' },
1038 { "quiet", 0, 0, 'q' },
1039 { "lim-lee", 0, 0, 'L' },
1040 { "subgroup", 0, 0, 'S' },
1041 { "kcdsa", 0, 0, 'K' },
1044 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS",
1049 /* --- Handle the various options --- */
1053 /* --- Read an algorithm name --- */
1057 size_t sz
= strlen(optarg
);
1059 if (strcmp(optarg
, "list") == 0) {
1060 for (a
= algtab
; a
->name
; a
++)
1061 printf("%-10s %s\n", a
->name
, a
->help
);
1066 for (a
= algtab
; a
->name
; a
++) {
1067 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1068 if (a
->name
[sz
] == 0) {
1072 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1078 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1081 /* --- Bits must be nonzero and a multiple of 8 --- */
1085 k
.bits
= strtoul(optarg
, &p
, 0);
1086 if (k
.bits
== 0 || *p
!= 0)
1087 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1092 k
.qbits
= strtoul(optarg
, &p
, 0);
1093 if (k
.qbits
== 0 || *p
!= 0)
1094 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1097 /* --- Parameter selection --- */
1103 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1106 if (strcmp(optarg
, "forever") == 0)
1109 exp
= get_date(optarg
, 0);
1111 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1115 /* --- Store comments without interpretation --- */
1118 if (key_chkcomment(optarg
))
1119 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1123 /* --- Elliptic curve parameters --- */
1129 /* --- Store tags --- */
1132 if (key_chkident(optarg
))
1133 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1140 /* --- Seeding --- */
1143 const struct seedalg
*ss
;
1144 if (strcmp(optarg
, "list") == 0) {
1145 printf("Seed algorithms:\n");
1146 for (ss
= seedtab
; ss
->p
; ss
++)
1147 printf(" %s\n", ss
->p
);
1150 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1152 for (ss
= seedtab
; ss
->p
; ss
++) {
1153 if (strcmp(optarg
, ss
->p
) == 0)
1157 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1164 if (seed
) die(EXIT_FAILURE
, "seed already set");
1165 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1166 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1167 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1168 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1170 k
.r
= sa
->gen(d
.buf
, d
.len
);
1179 unsigned n
= strtoul(optarg
, &p
, 0);
1180 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1181 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1182 if (seed
) die(EXIT_FAILURE
, "seed already set");
1185 rand_get(RAND_GLOBAL
, p
, n
);
1186 c
= base64_class
.encoder(0, "", 0);
1187 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1190 k
.r
= sa
->gen(p
, n
);
1193 /* --- Key id --- */
1200 id
= strtoul(optarg
, &p
, 16);
1201 if (errno
|| *p
|| id
> MASK32
)
1202 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1206 /* --- Other flags --- */
1227 /* --- Other things are bogus --- */
1235 /* --- Various sorts of bogosity --- */
1237 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1239 "Usage: add [OPTIONS] TYPE [ATTR...]");
1241 if (key_chkident(argv
[optind
]))
1242 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1244 /* --- Set up various bits of the state --- */
1246 if (exp
== KEXP_EXPIRE
)
1247 exp
= time(0) + 14 * 24 * 60 * 60;
1249 /* --- Open the file and create the basic key block --- *
1251 * Keep on generating keyids until one of them doesn't collide.
1254 doopen(&f
, KOPEN_WRITE
);
1257 /* --- Key the generator --- */
1263 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1265 else if (err
!= KERR_DUPID
)
1266 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1269 /* --- Set various simple attributes --- */
1274 if (k
.f
& f_retag
) {
1275 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1277 strcmp(kk
->tag
, tag
) == 0)
1278 key_settag(&f
, kk
, 0);
1280 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1281 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1285 int err
= key_setcomment(&f
, k
.k
, c
);
1287 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1290 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1292 key_putattr(&f
, k
.k
, "genseed", seed
);
1293 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1296 key_fulltag(k
.k
, &k
.tag
);
1298 /* --- Find the parameter key --- */
1300 if (ptag
&& (k
.p
= key_bytag(&f
, ptag
)) == 0)
1301 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1303 /* --- Now generate the actual key data --- */
1309 dstr_destroy(&k
.tag
);
1314 /*----- Key listing -------------------------------------------------------*/
1316 /* --- Listing options --- */
1318 typedef struct listopts
{
1319 const char *tfmt
; /* Date format (@strftime@-style) */
1320 int v
; /* Verbosity level */
1321 unsigned f
; /* Various flags */
1322 time_t t
; /* Time now (for key expiry) */
1323 key_filter kf
; /* Filter for matching keys */
1326 /* --- Listing flags --- */
1328 #define f_newline 2u /* Write newline before next entry */
1329 #define f_attr 4u /* Written at least one attribute */
1330 #define f_utc 8u /* Emit UTC time, not local time */
1332 /* --- @showkeydata@ --- *
1334 * Arguments: @key_data *k@ = pointer to key to write
1335 * @int ind@ = indentation level
1336 * @listopts *o@ = listing options
1337 * @dstr *d@ = tag string for this subkey
1341 * Use: Emits a piece of key data in a human-readable format.
1344 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1346 #define INDENT(i) do { \
1348 for (_i = 0; _i < (i); _i++) { \
1353 switch (k
->e
& KF_ENCMASK
) {
1355 /* --- Binary key data --- *
1357 * Emit as a simple hex dump.
1361 const octet
*p
= k
->u
.k
.k
;
1362 const octet
*l
= p
+ k
->u
.k
.sz
;
1365 fputs(" {", stdout
);
1370 } else if (sz
% 8 == 0)
1374 printf("%02x", *p
++);
1379 fputs("}\n", stdout
);
1382 /* --- Encrypted data --- *
1384 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1385 * key. Otherwise just say that it's encrypted and move on.
1390 fputs(" encrypted\n", stdout
);
1393 if (key_punlock(&kd
, k
, d
->buf
))
1394 printf(" <failed to unlock %s>\n", d
->buf
);
1396 fputs(" encrypted", stdout
);
1397 showkeydata(kd
, ind
, o
, d
);
1403 /* --- Integer keys --- *
1405 * Emit as a large integer in decimal. This makes using the key in
1406 * `calc' or whatever easier.
1411 mp_writefile(k
->u
.m
, stdout
, 10);
1415 /* --- Strings --- */
1418 printf(" `%s'\n", k
->u
.p
);
1421 /* --- Elliptic curve points --- */
1424 if (EC_ATINF(&k
->u
.e
))
1425 fputs(" inf\n", stdout
);
1427 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1428 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1433 /* --- Structured keys --- *
1435 * Just iterate over the subkeys.
1443 fputs(" {\n", stdout
);
1444 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1445 if (!key_match(k
, &o
->kf
))
1448 printf("%s =", tag
);
1450 dstr_putf(d
, ".%s", tag
);
1451 showkeydata(k
, ind
+ 2, o
, d
);
1454 fputs("}\n", stdout
);
1461 /* --- @showkey@ --- *
1463 * Arguments: @key *k@ = pointer to key to show
1464 * @listopts *o@ = pointer to listing options
1468 * Use: Emits a listing of a particular key.
1471 static void showkey(key
*k
, listopts
*o
)
1473 char ebuf
[24], dbuf
[24];
1476 /* --- Skip the key if the filter doesn't match --- */
1478 if (!key_match(k
->k
, &o
->kf
))
1481 /* --- Sort out the expiry and deletion times --- */
1483 if (KEY_EXPIRED(o
->t
, k
->exp
))
1484 strcpy(ebuf
, "expired");
1485 else if (k
->exp
== KEXP_FOREVER
)
1486 strcpy(ebuf
, "forever");
1488 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1489 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1492 if (KEY_EXPIRED(o
->t
, k
->del
))
1493 strcpy(dbuf
, "deleted");
1494 else if (k
->del
== KEXP_FOREVER
)
1495 strcpy(dbuf
, "forever");
1497 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1498 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1501 /* --- If in compact format, just display and quit --- */
1504 if (!(o
->f
& f_newline
)) {
1505 printf("%8s %-20s %-20s %-10s %s\n",
1506 "Id", "Tag", "Type", "Expire", "Delete");
1508 printf("%08lx %-20s %-20s %-10s %s\n",
1509 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1510 k
->type
, ebuf
, dbuf
);
1515 /* --- Display the standard header --- */
1517 if (o
->f
& f_newline
)
1518 fputc('\n', stdout
);
1519 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1520 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1521 printf("type: %s\n", k
->type
);
1522 printf("expiry: %s\n", ebuf
);
1523 printf("delete: %s\n", dbuf
);
1524 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1526 /* --- Display the attributes --- */
1530 const char *av
, *an
;
1533 printf("attributes:");
1534 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1535 printf("\n %s = %s", an
, av
);
1539 fputc('\n', stdout
);
1544 /* --- If dumping requested, dump the raw key data --- */
1548 fputs("key:", stdout
);
1550 showkeydata(k
->k
, 0, o
, &d
);
1557 /* --- @cmd_list@ --- */
1559 static int cmd_list(int argc
, char *argv
[])
1563 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1565 /* --- Parse subcommand options --- */
1568 static struct option opt
[] = {
1569 { "quiet", 0, 0, 'q' },
1570 { "verbose", 0, 0, 'v' },
1571 { "utc", 0, 0, 'u' },
1572 { "filter", OPTF_ARGREQ
, 0, 'f' },
1575 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1592 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1594 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1603 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1605 /* --- Open the key file --- */
1607 doopen(&f
, KOPEN_READ
);
1610 /* --- Set up the time format --- */
1613 o
.tfmt
= "%Y-%m-%d";
1614 else if (o
.f
& f_utc
)
1615 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1617 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1619 /* --- If specific keys were requested use them, otherwise do all --- *
1621 * Some day, this might turn into a wildcard match.
1624 if (optind
< argc
) {
1626 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1629 moan("key `%s' not found", argv
[optind
]);
1633 } while (optind
< argc
);
1636 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1644 return (EXIT_FAILURE
);
1649 /*----- Command implementation --------------------------------------------*/
1651 /* --- @cmd_expire@ --- */
1653 static int cmd_expire(int argc
, char *argv
[])
1661 die(EXIT_FAILURE
, "Usage: expire TAG...");
1662 doopen(&f
, KOPEN_WRITE
);
1663 for (i
= 1; i
< argc
; i
++) {
1664 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1667 moan("key `%s' not found", argv
[i
]);
1675 /* --- @cmd_delete@ --- */
1677 static int cmd_delete(int argc
, char *argv
[])
1685 die(EXIT_FAILURE
, "Usage: delete TAG...");
1686 doopen(&f
, KOPEN_WRITE
);
1687 for (i
= 1; i
< argc
; i
++) {
1688 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1691 moan("key `%s' not found", argv
[i
]);
1699 /* --- @cmd_setattr@ --- */
1701 static int cmd_setattr(int argc
, char *argv
[])
1707 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1708 doopen(&f
, KOPEN_WRITE
);
1709 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1710 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1711 setattr(&f
, k
, argv
+ 2);
1716 /* --- @cmd_getattr@ --- */
1718 static int cmd_getattr(int argc
, char *argv
[])
1726 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1727 doopen(&f
, KOPEN_READ
);
1728 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1729 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1731 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1732 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1739 /* --- @cmd_finger@ --- */
1741 static const struct fpres
{
1743 const codec_class
*cdc
;
1744 unsigned short ival
;
1747 { "hex", &hex_class
, 8, "-:" },
1748 { "base32", &base32_class
, 6, ":" },
1752 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1753 const gchash
*ch
, const key_filter
*kf
)
1756 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1762 if (key_fingerprint(k
, h
, kf
)) {
1764 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1765 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1767 for (i
= 0; i
< dd
.len
; i
++) {
1768 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1769 dstr_putc(&d
, dd
.buf
[i
]);
1771 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1772 dstr_write(&d
, stdout
);
1774 dstr_destroy(&d
); dstr_destroy(&dd
);
1778 static const struct fpres
*lookup_fpres(const char *name
)
1780 const struct fpres
*fpres
;
1781 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1782 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1783 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1786 static int cmd_finger(int argc
, char *argv
[])
1790 const struct fpres
*fpres
= fprestab
;
1791 const gchash
*ch
= &rmd160
;
1792 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1795 static struct option opt
[] = {
1796 { "filter", OPTF_ARGREQ
, 0, 'f' },
1797 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1798 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1801 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1807 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1809 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1812 fpres
= lookup_fpres(optarg
);
1815 if ((ch
= ghash_byname(optarg
)) == 0)
1816 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1824 argv
+= optind
; argc
-= optind
;
1827 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1830 doopen(&f
, KOPEN_READ
);
1834 for (i
= 0; i
< argc
; i
++) {
1835 key
*k
= key_bytag(&f
, argv
[i
]);
1837 fingerprint(k
, fpres
, ch
, &kf
);
1840 moan("key `%s' not found", argv
[i
]);
1846 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1847 fingerprint(k
, fpres
, ch
, &kf
);
1852 /* --- @cmd_verify@ --- */
1854 static int cmd_verify(int argc
, char *argv
[])
1858 const gchash
*ch
= &rmd160
;
1862 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1865 const struct fpres
*fpres
= fprestab
;
1866 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1869 static struct option opt
[] = {
1870 { "filter", OPTF_ARGREQ
, 0, 'f' },
1871 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1872 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1875 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1881 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1883 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1886 fpres
= lookup_fpres(optarg
);
1889 if ((ch
= ghash_byname(optarg
)) == 0)
1890 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1898 argv
+= optind
; argc
-= optind
;
1899 if (rc
|| argc
!= 2) {
1901 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1904 doopen(&f
, KOPEN_READ
);
1906 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1907 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1908 for (p
= argv
[1]; *p
; p
++) {
1909 if (strchr(fpres
->sep
, *p
)) continue;
1912 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1913 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1914 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1915 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1916 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1918 if (d
.len
!= ch
->hashsz
) {
1919 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1920 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1923 if (!key_fingerprint(k
, h
, &kf
))
1924 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1925 fpr
= GH_DONE(h
, 0);
1926 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1927 die(EXIT_FAILURE
, "key fingerprint mismatch");
1928 dstr_destroy(&d
); dstr_destroy(&dd
);
1933 /* --- @cmd_comment@ --- */
1935 static int cmd_comment(int argc
, char *argv
[])
1941 if (argc
< 2 || argc
> 3)
1942 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1943 doopen(&f
, KOPEN_WRITE
);
1944 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1945 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1946 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1947 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1952 /* --- @cmd_tag@ --- */
1954 static int cmd_tag(int argc
, char *argv
[])
1963 static struct option opt
[] = {
1964 { "retag", 0, 0, 'r' },
1967 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
1980 argv
+= optind
; argc
-= optind
;
1981 if (argc
< 1 || argc
> 2 || rc
)
1982 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
1983 doopen(&f
, KOPEN_WRITE
);
1984 if (flags
& f_retag
) {
1985 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
1986 key_settag(&f
, k
, 0);
1988 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1989 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1990 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
1991 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
1996 /* --- @cmd_lock@ --- */
1998 static int cmd_lock(int argc
, char *argv
[])
2006 die(EXIT_FAILURE
, "Usage: lock QTAG");
2007 doopen(&f
, KOPEN_WRITE
);
2008 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2009 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2010 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
2011 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2012 if (key_plock(kd
, 0, d
.buf
))
2013 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
2019 /* --- @cmd_unlock@ --- */
2021 static int cmd_unlock(int argc
, char *argv
[])
2029 die(EXIT_FAILURE
, "Usage: unlock QTAG");
2030 doopen(&f
, KOPEN_WRITE
);
2031 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2032 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2033 if ((*kd
)->e
!= KENC_ENCRYPT
)
2034 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
2035 if (key_punlock(kd
, 0, d
.buf
))
2036 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2042 /* --- @cmd_extract@ --- */
2044 static int cmd_extract(int argc
, char *argv
[])
2050 key_filter kf
= { 0, 0 };
2052 const char *outfile
= 0;
2056 static struct option opt
[] = {
2057 { "filter", OPTF_ARGREQ
, 0, 'f' },
2060 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2066 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2068 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2076 argv
+= optind
; argc
-= optind
;
2078 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2079 if (strcmp(*argv
, "-") == 0)
2083 dstr_putf(&d
, "%s.new", outfile
);
2084 if (!(fp
= fopen(d
.buf
, "w"))) {
2085 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2086 d
.buf
, strerror(errno
));
2090 doopen(&f
, KOPEN_READ
);
2094 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2095 key_extract(&f
, k
, fp
, &kf
);
2097 for (i
= 1; i
< argc
; i
++) {
2098 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2099 key_extract(&f
, k
, fp
, &kf
);
2101 moan("key `%s' not found", argv
[i
]);
2106 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2107 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2113 /* --- @cmd_tidy@ --- */
2115 static int cmd_tidy(int argc
, char *argv
[])
2119 die(EXIT_FAILURE
, "Usage: tidy");
2120 doopen(&f
, KOPEN_WRITE
);
2121 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2126 /* --- @cmd_merge@ --- */
2128 static int cmd_merge(int argc
, char *argv
[])
2134 die(EXIT_FAILURE
, "Usage: merge FILE");
2135 if (strcmp(argv
[1], "-") == 0)
2137 else if (!(fp
= fopen(argv
[1], "r"))) {
2138 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2139 argv
[1], strerror(errno
));
2142 doopen(&f
, KOPEN_WRITE
);
2143 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2148 /* --- @cmd_show@ --- */
2152 listtab[i].name, listtab[i].name) \
2153 LI("Hash functions", hash, \
2154 ghashtab[i], ghashtab[i]->name) \
2155 LI("Elliptic curves", ec, \
2156 ectab[i].name, ectab[i].name) \
2157 LI("Prime Diffie-Hellman groups", dh, \
2158 ptab[i].name, ptab[i].name) \
2159 LI("Binary Diffie-Hellman groups", bindh, \
2160 bintab[i].name, bintab[i].name) \
2161 LI("Key-generation algorithms", keygen, \
2162 algtab[i].name, algtab[i].name) \
2163 LI("Random seeding algorithms", seed, \
2164 seedtab[i].p, seedtab[i].p) \
2165 LI("Fingerprint presentation styles", fpres, \
2166 fprestab[i].name, fprestab[i].name)
2168 MAKELISTTAB(listtab
, LISTS
)
2170 static int cmd_show(int argc
, char *argv
[])
2172 return (displaylists(listtab
, argv
+ 1));
2175 /*----- Main command table ------------------------------------------------*/
2177 static int cmd_help(int argc
, char *argv
[]);
2179 static cmd cmds
[] = {
2180 { "help", cmd_help
, "help [COMMAND...]" },
2181 { "show", cmd_show
, "show [ITEM...]" },
2182 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2185 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2186 -q, --quiet Show less information.\n\
2187 -v, --verbose Show more information.\n\
2189 { "fingerprint", cmd_finger
,
2190 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2193 -f, --filter=FILT Only hash key components matching FILT.\n\
2194 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2195 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2196 ($ show hash for list.)\n\
2198 { "verify", cmd_verify
,
2199 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2202 -f, --filter=FILT Only hash key components matching FILT.\n\
2203 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2204 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2205 ($ show hash for list.)\n\
2207 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2210 -f, --filter=FILT Only extract key components matching FILT.\n\
2212 { "merge", cmd_merge
, "merge FILE" },
2213 { "expire", cmd_expire
, "expire TAG..." },
2214 { "delete", cmd_delete
, "delete TAG..." },
2215 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2216 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2217 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2218 { "lock", cmd_lock
, "lock QTAG" },
2219 { "unlock", cmd_unlock
, "unlock QTAG" },
2220 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2223 -r, --retag Untag any key currently called new-tag.\n\
2225 { "tidy", cmd_tidy
, "tidy" },
2227 "add [-OPTIONS] TYPE [ATTR...]\n\
2228 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2229 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2230 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2233 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2234 ($ show keygen for list.)\n\
2235 -b, --bits=N Generate an N-bit key.\n\
2236 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2237 -p, --parameters=TAG Get group parameters from TAG.\n\
2238 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2239 ($ show ec or $ show dh for list.)\n\
2240 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2241 ($ show seed for list.)\n\
2242 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2243 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2244 -e, --expire=TIME Make the key expire after TIME.\n\
2245 -c, --comment=STRING Attach the command STRING to the key.\n\
2246 -t, --tag=TAG Tag the key with the name TAG.\n\
2247 -r, --retag Untag any key currently with that tag.\n\
2248 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2249 -I, --key-id=ID Force the key-id for the new key.\n\
2250 -l, --lock Lock the generated key with a passphrase.\n\
2251 -q, --quiet Don't give progress indicators while working.\n\
2252 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2253 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2254 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2259 static int cmd_help(int argc
, char *argv
[])
2261 sc_help(cmds
, stdout
, argv
+ 1);
2265 /*----- Main code ---------------------------------------------------------*/
2267 /* --- Helpful GNUy functions --- */
2269 static void usage(FILE *fp
)
2271 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2274 void version(FILE *fp
)
2276 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2279 void help_global(FILE *fp
)
2283 Performs various simple key management operations.\n\
2285 Global command line options:\n\
2287 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2288 -v, --version Display version number.\n\
2289 -u, --usage Display short usage summary.\n\
2291 -k, --keyring=FILE Read and write keys in FILE.\n",
2297 * Arguments: @int argc@ = number of command line arguments
2298 * @char *argv[]@ = array of command line arguments
2300 * Returns: Nonzero on failure.
2302 * Use: Main program. Performs simple key management functions.
2305 int main(int argc
, char *argv
[])
2311 /* --- Initialization --- */
2316 /* --- Parse command line options --- */
2319 static struct option opt
[] = {
2321 /* --- Standard GNUy help options --- */
2323 { "help", 0, 0, 'h' },
2324 { "version", 0, 0, 'v' },
2325 { "usage", 0, 0, 'u' },
2327 /* --- Real live useful options --- */
2329 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2331 /* --- Magic terminator --- */
2335 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2341 /* --- GNU help options --- */
2344 sc_help(cmds
, stdout
, argv
+ optind
);
2353 /* --- Real useful options --- */
2359 /* --- Bogosity --- */
2367 /* --- Complain about excessive bogons --- */
2369 if (f
& f_bogus
|| optind
== argc
) {
2374 /* --- Initialize the Catacomb random number generator --- */
2376 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2377 rand_seed(RAND_GLOBAL
, 160);
2379 /* --- Dispatch to appropriate command handler --- */
2384 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2387 /*----- That's all, folks -------------------------------------------------*/