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>
80 #include "sha256-mgf.h"
81 #include "sha224-mgf.h"
82 #include "sha384-mgf.h"
83 #include "sha512-mgf.h"
84 #include "tiger-mgf.h"
85 #include "rmd128-mgf.h"
86 #include "rmd160-mgf.h"
87 #include "rmd256-mgf.h"
88 #include "rmd320-mgf.h"
92 /*----- Handy global state ------------------------------------------------*/
94 static const char *keyfile
= "keyring";
96 /*----- Useful shared functions -------------------------------------------*/
100 * Arguments: @key_file *f@ = pointer to key file block
101 * @unsigned how@ = method to open file with
105 * Use: Opens a key file and handles errors by panicking
109 static void doopen(key_file
*f
, unsigned how
)
111 if (key_open(f
, keyfile
, how
, key_moan
, 0)){
112 die(EXIT_FAILURE
, "couldn't open keyring `%s': %s",
113 keyfile
, strerror(errno
));
117 /* --- @doclose@ --- *
119 * Arguments: @key_file *f@ = pointer to key file block
123 * Use: Closes a key file and handles errors by panicking
127 static void doclose(key_file
*f
)
129 switch (key_close(f
)) {
131 die(EXIT_FAILURE
, "couldn't write file `%s': %s",
132 keyfile
, strerror(errno
));
134 die(EXIT_FAILURE
, "keyring file `%s' broken: %s (repair manually)",
135 keyfile
, strerror(errno
));
139 /* --- @setattr@ --- *
141 * Arguments: @key_file *f@ = pointer to key file block
142 * @key *k@ = pointer to key block
143 * @char *v[]@ = array of assignments (overwritten!)
147 * Use: Applies the attribute assignments to the key.
150 static void setattr(key_file
*f
, key
*k
, char *v
[])
155 size_t eq
= strcspn(p
, "=");
157 moan("invalid assignment: `%s' (ignored)", p
);
163 if ((err
= key_putattr(f
, k
, *v
, *p ? p
: 0)) != 0)
164 die(EXIT_FAILURE
, "couldn't set attributes: %s", key_strerror(err
));
169 /*----- Seeding -----------------------------------------------------------*/
171 const struct seedalg
{ const char *p
; grand
*(*gen
)(const void *, size_t); }
173 { "dsarand", dsarand_create
},
174 { "rmd128-mgf", rmd128_mgfrand
},
175 { "rmd160-mgf", rmd160_mgfrand
},
176 { "rmd256-mgf", rmd256_mgfrand
},
177 { "rmd320-mgf", rmd320_mgfrand
},
178 { "sha-mgf", sha_mgfrand
},
179 { "sha224-mgf", sha224_mgfrand
},
180 { "sha256-mgf", sha256_mgfrand
},
181 { "sha384-mgf", sha384_mgfrand
},
182 { "sha512-mgf", sha512_mgfrand
},
183 { "tiger-mgf", tiger_mgfrand
},
187 #define SEEDALG_DEFAULT (seedtab + 2)
189 /*----- Key generation ----------------------------------------------------*/
191 /* --- Key generation parameters --- */
193 typedef struct keyopts
{
194 key_file
*kf
; /* Pointer to key file */
195 key
*k
; /* Pointer to the actual key */
196 dstr tag
; /* Full tag name for the key */
197 unsigned f
; /* Flags for the new key */
198 unsigned bits
, qbits
; /* Bit length for the new key */
199 const char *curve
; /* Elliptic curve name/info */
200 grand
*r
; /* Random number source */
201 mp
*e
; /* Public exponent */
202 key
*p
; /* Parameters key-data */
205 #define f_bogus 1u /* Error in parsing */
206 #define f_lock 2u /* Passphrase-lock private key */
207 #define f_quiet 4u /* Don't show a progress indicator */
208 #define f_limlee 8u /* Generate Lim-Lee primes */
209 #define f_subgroup 16u /* Generate a subgroup */
210 #define f_retag 32u /* Remove any existing tag */
211 #define f_kcdsa 64u /* Generate KCDSA primes */
213 /* --- @dolock@ --- *
215 * Arguments: @keyopts *k@ = key generation options
216 * @key_data **kd@ = pointer to key data to lock
217 * @const char *t@ = tag suffix or null
221 * Use: Does passphrase locking on new keys.
224 static void dolock(keyopts
*k
, key_data
**kd
, const char *t
)
226 if (!(k
->f
& f_lock
))
229 dstr_putf(&k
->tag
, ".%s", t
);
230 if (key_plock(kd
, 0, k
->tag
.buf
))
231 die(EXIT_FAILURE
, "couldn't lock key");
234 /* --- @copyparam@ --- *
236 * Arguments: @keyopts *k@ = pointer to key options
237 * @const char **pp@ = checklist of parameters, or null
239 * Returns: Nonzero if parameters copied; zero if you have to generate
242 * Use: Copies parameters from a source key to the current one.
245 static int copyparam(keyopts
*k
, const char **pp
)
256 /* --- Quick check if no parameters supplied --- */
261 /* --- Copy the key data if there's anything we want --- */
265 /* --- Run through the checklist --- */
267 key_fulltag(k
->p
, &t
);
268 if ((k
->p
->k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
269 die(EXIT_FAILURE
, "parameter key `%s' is not structured", t
.buf
);
271 key_data
*kd
= key_structfind(k
->p
->k
, *pp
);
274 "bad parameter key `%s': parameter `%s' not found", t
.buf
, *pp
);
276 if (!KEY_MATCH(kd
, &kf
)) {
278 "bad parameter key `%s': subkey `%s' is not shared", t
.buf
, *pp
);
283 /* --- Copy over the parameters --- */
285 kd
= key_copydata(k
->p
->k
, &kf
);
287 key_setkeydata(k
->kf
, k
->k
, kd
);
291 /* --- Copy over attributes --- */
293 for (key_mkattriter(&i
, k
->p
); key_nextattr(&i
, &n
, &v
); )
294 key_putattr(k
->kf
, k
->k
, n
, v
);
304 * Arguments: @key_data *k@ = pointer to key data block
305 * @const char *tag@ = tag string to use
307 * Returns: Pointer to multiprecision integer key item.
309 * Use: Fetches an MP key component.
312 static mp
*getmp(key_data
*k
, const char *tag
)
314 k
= key_structfind(k
, tag
);
316 die(EXIT_FAILURE
, "unexpected failure looking up subkey `%s'", tag
);
317 if ((k
->e
& KF_ENCMASK
) != KENC_MP
)
318 die(EXIT_FAILURE
, "subkey `%s' has an incompatible type", tag
);
322 /* --- @keyrand@ --- *
324 * Arguments: @key_file *kf@ = pointer to key file
325 * @const char *id@ = pointer to key id (or null)
329 * Use: Keys the random number generator.
332 static void keyrand(key_file
*kf
, const char *id
)
336 /* --- Find the key --- */
339 if ((k
= key_bytag(kf
, id
)) == 0)
340 die(EXIT_FAILURE
, "key `%s' not found", id
);
342 k
= key_bytype(kf
, "catacomb-rand");
345 key_data
*kd
= k
->k
, *kkd
;
349 switch (kd
->e
& KF_ENCMASK
) {
355 if (key_punlock(&kkd
, kd
, d
.buf
))
356 die(EXIT_FAILURE
, "error unlocking key `%s'", d
.buf
);
364 die(EXIT_FAILURE
, "bad encoding type for key `%s'", d
.buf
);
368 /* --- Key the generator --- */
370 rand_key(RAND_GLOBAL
, kd
->u
.k
.k
, kd
->u
.k
.sz
);
375 /* --- Key generation algorithms --- */
377 static void alg_empty(keyopts
*k
)
380 key_setkeydata(k
->kf
, k
->k
,
381 key_newstring(KCAT_SHARE
, k
->curve ? k
->curve
: "."));
384 static void alg_binary(keyopts
*k
)
395 sz
= (k
->bits
+ 7) >> 3;
397 m
= (1 << (((k
->bits
- 1) & 7) + 1)) - 1;
398 k
->r
->ops
->fill(k
->r
, p
, sz
);
400 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
403 key_setkeydata(k
->kf
, k
->k
, kd
);
408 static void alg_des(keyopts
*k
)
418 if (k
->bits
% 56 || k
->bits
> 168)
419 die(EXIT_FAILURE
, "DES keys must be 56, 112 or 168 bits long");
423 k
->r
->ops
->fill(k
->r
, p
, sz
);
424 for (i
= 0; i
< sz
; i
++) {
425 octet x
= p
[i
] | 0x01;
429 p
[i
] = (p
[i
] & 0xfe) | (x
& 0x01);
431 kd
= key_newbinary(KCAT_SYMM
| KF_BURN
, p
, sz
);
434 key_setkeydata(k
->kf
, k
->k
, kd
);
439 static void alg_rsa(keyopts
*k
)
444 /* --- Sanity checking --- */
450 die(EXIT_FAILURE
, "RSA key too tiny");
452 k
->e
= mp_fromulong(MP_NEW
, 65537);
454 /* --- Generate the RSA parameters --- */
456 if (rsa_gen_e(&rp
, k
->bits
, k
->e
, k
->r
, 0,
457 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
458 die(EXIT_FAILURE
, "RSA key generation failed");
460 /* --- Run a test encryption --- */
463 grand
*g
= fibrand_create(k
->r
->ops
->word(k
->r
));
465 mp
*m
= mprand_range(MP_NEW
, rp
.n
, g
, 0);
470 c
= rsa_qpubop(&rpp
, MP_NEW
, m
);
471 c
= rsa_qprivop(&rp
, c
, c
, g
);
474 die(EXIT_FAILURE
, "test encryption failed");
480 /* --- Allrighty then --- */
482 kd
= key_newstruct();
483 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, rp
.n
));
484 key_structsteal(kd
, "e", key_newmp(KCAT_PUB
, rp
.e
));
486 kkd
= key_newstruct();
487 key_structsteal(kkd
, "d", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.d
));
488 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.p
));
489 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q
));
490 key_structsteal(kkd
, "q-inv", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.q_inv
));
491 key_structsteal(kkd
, "d-mod-p", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dp
));
492 key_structsteal(kkd
, "d-mod-q", key_newmp(KCAT_PRIV
| KF_BURN
, rp
.dq
));
493 dolock(k
, &kkd
, "private");
494 key_structsteal(kd
, "private", kkd
);
495 key_setkeydata(k
->kf
, k
->k
, kd
);
500 static void alg_dsaparam(keyopts
*k
)
502 static const char *pl
[] = { "q", "p", "g", 0 };
503 if (!copyparam(k
, pl
)) {
512 /* --- Choose appropriate bit lengths if necessary --- */
519 /* --- Allocate a seed block --- */
521 sz
= (k
->qbits
+ 7) >> 3;
523 k
->r
->ops
->fill(k
->r
, p
, sz
);
525 /* --- Allocate the parameters --- */
527 if (dsa_gen(&dp
, k
->qbits
, k
->bits
, 0, p
, sz
, &ds
,
528 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
529 die(EXIT_FAILURE
, "DSA parameter generation failed");
531 /* --- Store the parameters --- */
533 kd
= key_newstruct();
534 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
535 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
536 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
540 key_setkeydata(k
->kf
, k
->k
, kd
);
543 /* --- Store the seed for future verification --- */
545 c
= base64_class
.encoder(0, "", 0);
546 c
->ops
->code(c
, ds
.p
, ds
.sz
, &d
); c
->ops
->code(c
, 0, 0, &d
);
549 key_putattr(k
->kf
, k
->k
, "seed", d
.buf
);
551 dstr_putf(&d
, "%u", ds
.count
);
552 key_putattr(k
->kf
, k
->k
, "count", d
.buf
);
559 static void alg_dsa(keyopts
*k
)
566 /* --- Get the shared parameters --- */
569 key_split(&k
->k
->k
); kd
= k
->k
->k
;
574 /* --- Choose a private key --- */
576 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
577 mpmont_create(&mm
, p
);
578 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
580 /* --- Store everything away --- */
582 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
584 kkd
= key_newstruct();
585 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
586 dolock(k
, &kkd
, "private");
587 key_structsteal(kd
, "private", kkd
);
589 mp_drop(x
); mp_drop(y
);
592 static void alg_dhparam(keyopts
*k
)
594 static const char *pl
[] = { "p", "q", "g", 0 };
596 if (!copyparam(k
, pl
)) {
605 if (strcmp(k
->curve
, "list") == 0) {
607 LIST("Built-in prime fields", stdout
, ptab
[i
].name
, ptab
[i
].name
);
611 if (dh_parse(&qd
, &dp
))
612 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
614 die(EXIT_FAILURE
, "junk at end of field spec");
615 if ((g
= group_prime(&dp
)) == 0)
616 die(EXIT_FAILURE
, "invalid prime field");
617 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
618 moan("WARNING! group check failed: %s", e
);
626 /* --- Choose a large safe prime number --- */
628 if (k
->f
& f_limlee
) {
633 rc
= dh_limlee(&dp
, k
->qbits
, k
->bits
,
634 (k
->f
& f_subgroup
) ? DH_SUBGROUP
: 0,
635 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0,
636 (k
->f
& f_quiet
) ?
0 : pgen_evspin
, 0, &nf
, &f
);
640 for (i
= 0; i
< nf
; i
++) {
643 mp_writedstr(f
[i
], &d
, 10);
646 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
649 } else if (k
->f
& f_kcdsa
) {
652 rc
= dh_kcdsagen(&dp
, k
->qbits
, k
->bits
, 0,
653 0, k
->r
, (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
658 mp_writedstr(dp
.q
, &d
, 10);
659 mp_div(&v
, 0, dp
.p
, dp
.q
);
662 mp_writedstr(v
, &d
, 10);
664 key_putattr(k
->kf
, k
->k
, "factors", d
.buf
);
668 rc
= dh_gen(&dp
, k
->qbits
, k
->bits
, 0, k
->r
,
669 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0);
672 die(EXIT_FAILURE
, "Diffie-Hellman parameter generation failed");
675 kd
= key_newstruct();
676 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, dp
.p
));
677 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, dp
.q
));
678 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, dp
.g
));
682 key_setkeydata(k
->kf
, k
->k
, kd
);
687 static void alg_dh(keyopts
*k
)
694 /* --- Get the shared parameters --- */
697 key_split(&k
->k
->k
); kd
= k
->k
->k
;
702 /* --- Choose a suitable private key --- *
704 * Since %$g$% has order %$q$%, choose %$x < q$%.
707 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
709 /* --- Compute the public key %$y = g^x \bmod p$% --- */
711 mpmont_create(&mm
, p
);
712 y
= mpmont_exp(&mm
, MP_NEW
, g
, x
);
715 /* --- Store everything away --- */
717 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
719 kkd
= key_newstruct();
720 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
721 dolock(k
, &kkd
, "private");
722 key_structsteal(kd
, "private", kkd
);
724 mp_drop(x
); mp_drop(y
);
727 static void alg_bbs(keyopts
*k
)
732 /* --- Sanity checking --- */
738 /* --- Generate the BBS parameters --- */
740 if (bbs_gen(&bp
, k
->bits
, k
->r
, 0,
741 (k
->f
& f_quiet
) ?
0 : pgen_ev
, 0))
742 die(EXIT_FAILURE
, "Blum-Blum-Shub key generation failed");
744 /* --- Allrighty then --- */
746 kd
= key_newstruct();
747 key_structsteal(kd
, "n", key_newmp(KCAT_PUB
, bp
.n
));
749 kkd
= key_newstruct();
750 key_structsteal(kkd
, "p", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.p
));
751 key_structsteal(kkd
, "q", key_newmp(KCAT_PRIV
| KF_BURN
, bp
.q
));
752 dolock(k
, &kkd
, "private");
753 key_structsteal(kd
, "private", kkd
);
754 key_setkeydata(k
->kf
, k
->k
, kd
);
760 static void alg_binparam(keyopts
*k
)
762 static const char *pl
[] = { "p", "q", "g", 0 };
763 if (!copyparam(k
, pl
)) {
770 /* --- Decide on a field --- */
772 if (!k
->bits
) k
->bits
= 128;
773 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
775 LIST("Built-in binary fields", stdout
,
776 bintab
[i
].name
, bintab
[i
].name
);
780 if (k
->bits
<= 40) k
->curve
= "p1363-40";
781 else if (k
->bits
<= 56) k
->curve
= "p1363-56";
782 else if (k
->bits
<= 64) k
->curve
= "p1363-64";
783 else if (k
->bits
<= 80) k
->curve
= "p1363-80";
784 else if (k
->bits
<= 112) k
->curve
= "p1363-112";
785 else if (k
->bits
<= 128) k
->curve
= "p1363-128";
788 "no built-in binary fields provide %u-bit security",
793 /* --- Check it --- */
797 if (dhbin_parse(&qd
, &gb
))
798 die(EXIT_FAILURE
, "error in field spec: %s", qd
.e
);
800 die(EXIT_FAILURE
, "junk at end of field spec");
801 if ((g
= group_binary(&gb
)) == 0)
802 die(EXIT_FAILURE
, "invalid binary field");
803 if (!(k
->f
& f_quiet
) && (e
= G_CHECK(g
, &rand_global
)) != 0)
804 moan("WARNING! group check failed: %s", e
);
807 /* --- Write out the answer --- */
809 kd
= key_newstruct();
810 key_structsteal(kd
, "p", key_newmp(KCAT_SHARE
, gb
.p
));
811 key_structsteal(kd
, "q", key_newmp(KCAT_SHARE
, gb
.q
));
812 key_structsteal(kd
, "g", key_newmp(KCAT_SHARE
, gb
.g
));
816 key_setkeydata(k
->kf
, k
->k
, kd
);
821 static void alg_bin(keyopts
*k
)
828 /* --- Get the shared parameters --- */
831 key_split(&k
->k
->k
); kd
= k
->k
->k
;
836 /* --- Choose a suitable private key --- *
838 * Since %$g$% has order %$q$%, choose %$x < q$%.
841 x
= mprand_range(MP_NEWSEC
, q
, k
->r
, 0);
843 /* --- Compute the public key %$y = g^x \bmod p$% --- */
845 gfreduce_create(&r
, p
);
846 y
= gfreduce_exp(&r
, MP_NEW
, g
, x
);
847 gfreduce_destroy(&r
);
849 /* --- Store everything away --- */
851 key_structsteal(kd
, "y", key_newmp(KCAT_PUB
, y
));
853 kkd
= key_newstruct();
854 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
855 dolock(k
, &kkd
, "private");
856 key_structsteal(kd
, "private", kkd
);
858 mp_drop(x
); mp_drop(y
);
861 static void alg_ecparam(keyopts
*k
)
863 static const char *pl
[] = { "curve", 0 };
864 if (!copyparam(k
, pl
)) {
869 /* --- Decide on a curve --- */
871 if (!k
->bits
) k
->bits
= 256;
872 if (k
->curve
&& strcmp(k
->curve
, "list") == 0) {
874 LIST("Built-in elliptic curves", stdout
,
875 ectab
[i
].name
, ectab
[i
].name
);
879 if (k
->bits
<= 56) k
->curve
= "secp112r1";
880 else if (k
->bits
<= 64) k
->curve
= "secp128r1";
881 else if (k
->bits
<= 80) k
->curve
= "secp160r1";
882 else if (k
->bits
<= 96) k
->curve
= "secp192r1";
883 else if (k
->bits
<= 112) k
->curve
= "secp224r1";
884 else if (k
->bits
<= 128) k
->curve
= "secp256r1";
885 else if (k
->bits
<= 192) k
->curve
= "secp384r1";
886 else if (k
->bits
<= 256) k
->curve
= "secp521r1";
888 die(EXIT_FAILURE
, "no built-in curves provide %u-bit security",
892 /* --- Check it --- */
894 if ((e
= ec_getinfo(&ei
, k
->curve
)) != 0)
895 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
896 if (!(k
->f
& f_quiet
) && (e
= ec_checkinfo(&ei
, k
->r
)) != 0)
897 moan("WARNING! curve check failed: %s", e
);
900 /* --- Write out the answer --- */
902 kd
= key_newstruct();
903 key_structsteal(kd
, "curve", key_newstring(KCAT_SHARE
, k
->curve
));
904 key_setkeydata(k
->kf
, k
->k
, kd
);
909 static void alg_ec(keyopts
*k
)
918 /* --- Get the curve --- */
921 key_split(&k
->k
->k
); kd
= k
->k
->k
;
922 if ((kkd
= key_structfind(kd
, "curve")) == 0)
923 die(EXIT_FAILURE
, "unexpected failure looking up subkey `curve')");
924 if ((kkd
->e
& KF_ENCMASK
) != KENC_STRING
)
925 die(EXIT_FAILURE
, "subkey `curve' is not a string");
926 if ((e
= ec_getinfo(&ei
, kkd
->u
.p
)) != 0)
927 die(EXIT_FAILURE
, "error in curve spec: %s", e
);
929 /* --- Invent a private exponent and compute the public key --- */
931 x
= mprand_range(MP_NEWSEC
, ei
.r
, k
->r
, 0);
932 ec_mul(ei
.c
, &p
, &ei
.g
, x
);
934 /* --- Store everything away --- */
936 key_structsteal(kd
, "p", key_newec(KCAT_PUB
, &p
));
938 kkd
= key_newstruct();
939 key_structsteal(kkd
, "x", key_newmp(KCAT_PRIV
| KF_BURN
, x
));
940 dolock(k
, &kkd
, "private");
941 key_structsteal(kd
, "private", kkd
);
950 _(x25519, X25519, "X25519") \
951 _(x448, X448, "X448")
953 #define XDHALG(xdh, XDH, name) \
955 static void alg_##xdh(keyopts *k) \
957 key_data *kd, *kkd; \
958 octet priv[XDH##_KEYSZ], pub[XDH##_PUBSZ]; \
961 k->r->ops->fill(k->r, priv, sizeof(priv)); \
962 xdh(pub, priv, xdh##_base); \
963 kkd = key_newstruct(); \
964 key_structsteal(kkd, "priv", \
965 key_newbinary(KCAT_PRIV | KF_BURN, \
966 priv, sizeof(priv))); \
967 kd = key_newstruct(); \
968 key_structsteal(kd, "private", kkd); \
969 key_structsteal(kd, "pub", \
970 key_newbinary(KCAT_PUB, pub, sizeof(pub))); \
972 key_setkeydata(k->kf, k->k, kd); \
979 _(ed25519, ED25519, "Ed25519") \
980 _(ed448, ED448, "Ed448")
982 #define EDDSAALG(ed, ED, name) \
984 static void alg_##ed(keyopts *k) \
986 key_data *kd, *kkd; \
987 octet priv[ED##_KEYSZ], pub[ED##_PUBSZ]; \
990 k->r->ops->fill(k->r, priv, sizeof(priv)); \
991 ed##_pubkey(pub, priv, sizeof(priv)); \
992 kkd = key_newstruct(); \
993 key_structsteal(kkd, "priv", \
994 key_newbinary(KCAT_PRIV | KF_BURN, \
995 priv, sizeof(priv))); \
996 kd = key_newstruct(); \
997 key_structsteal(kd, "private", kkd); \
998 key_structsteal(kd, "pub", \
999 key_newbinary(KCAT_PUB, pub, sizeof(pub))); \
1001 key_setkeydata(k->kf, k->k, kd); \
1007 /* --- The algorithm tables --- */
1009 typedef struct keyalg
{
1011 void (*proc
)(keyopts
*o
);
1015 static keyalg algtab
[] = {
1016 { "binary", alg_binary
, "Plain binary data" },
1017 { "des", alg_des
, "Binary with DES-style parity" },
1018 { "rsa", alg_rsa
, "RSA public-key encryption" },
1019 { "bbs", alg_bbs
, "Blum-Blum-Shub generator" },
1020 { "dsa", alg_dsa
, "DSA digital signatures" },
1021 { "dsa-param", alg_dsaparam
, "DSA shared parameters" },
1022 { "dh", alg_dh
, "Diffie-Hellman key exchange" },
1023 { "dh-param", alg_dhparam
, "Diffie-Hellman parameters" },
1024 { "bindh", alg_bin
, "DH over a binary field" },
1025 { "bindh-param", alg_binparam
, "Binary-field DH parameters" },
1026 { "ec-param", alg_ecparam
, "Elliptic curve parameters" },
1027 { "ec", alg_ec
, "Elliptic curve crypto" },
1028 #define XDHTAB(xdh, XDH, name) \
1029 { #xdh, alg_##xdh, "" name " key exchange" },
1032 #define EDDSATAB(ed, ED, name) \
1033 { #ed, alg_##ed, "" name " digital signatures" },
1036 { "empty", alg_empty
, "Empty parametrs-only key" },
1040 /* --- @cmd_add@ --- */
1042 static int cmd_add(int argc
, char *argv
[])
1045 time_t exp
= KEXP_EXPIRE
;
1046 uint32 kid
= rand_global
.ops
->word(&rand_global
);
1047 const char *tag
= 0, *ptag
= 0;
1049 keyalg
*alg
= algtab
;
1050 const char *rtag
= 0;
1051 const struct seedalg
*sa
= SEEDALG_DEFAULT
;
1052 keyopts k
= { 0, 0, DSTR_INIT
, 0, 0, 0, 0, 0, 0 };
1053 const char *seed
= 0;
1056 /* --- Parse options for the subcommand --- */
1059 static struct option opt
[] = {
1060 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1061 { "bits", OPTF_ARGREQ
, 0, 'b' },
1062 { "qbits", OPTF_ARGREQ
, 0, 'B' },
1063 { "parameters", OPTF_ARGREQ
, 0, 'p' },
1064 { "expire", OPTF_ARGREQ
, 0, 'e' },
1065 { "comment", OPTF_ARGREQ
, 0, 'c' },
1066 { "tag", OPTF_ARGREQ
, 0, 't' },
1067 { "rand-id", OPTF_ARGREQ
, 0, 'R' },
1068 { "key-id", OPTF_ARGREQ
, 0, 'I' },
1069 { "curve", OPTF_ARGREQ
, 0, 'C' },
1070 { "seedalg", OPTF_ARGREQ
, 0, 'A' },
1071 { "seed", OPTF_ARGREQ
, 0, 's' },
1072 { "newseed", OPTF_ARGREQ
, 0, 'n' },
1073 { "public-exponent", OPTF_ARGREQ
, 0, 'E' },
1074 { "lock", 0, 0, 'l' },
1075 { "quiet", 0, 0, 'q' },
1076 { "lim-lee", 0, 0, 'L' },
1077 { "subgroup", 0, 0, 'S' },
1078 { "kcdsa", 0, 0, 'K' },
1081 int i
= mdwopt(argc
, argv
, "+a:b:B:p:e:c:t:R:I:C:A:s:n:E:lqrLKS",
1086 /* --- Handle the various options --- */
1090 /* --- Read an algorithm name --- */
1094 size_t sz
= strlen(optarg
);
1096 if (strcmp(optarg
, "list") == 0) {
1097 for (a
= algtab
; a
->name
; a
++)
1098 printf("%-10s %s\n", a
->name
, a
->help
);
1103 for (a
= algtab
; a
->name
; a
++) {
1104 if (strncmp(optarg
, a
->name
, sz
) == 0) {
1105 if (a
->name
[sz
] == 0) {
1109 die(EXIT_FAILURE
, "ambiguous algorithm name `%s'", optarg
);
1115 die(EXIT_FAILURE
, "unknown algorithm name `%s'", optarg
);
1118 /* --- Bits must be nonzero and a multiple of 8 --- */
1122 k
.bits
= strtoul(optarg
, &p
, 0);
1123 if (k
.bits
== 0 || *p
!= 0)
1124 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1129 k
.qbits
= strtoul(optarg
, &p
, 0);
1130 if (k
.qbits
== 0 || *p
!= 0)
1131 die(EXIT_FAILURE
, "bad bitlength `%s'", optarg
);
1134 /* --- Parameter selection --- */
1140 /* --- Expiry dates get passed to @get_date@ for parsing --- */
1143 if (strcmp(optarg
, "forever") == 0)
1146 exp
= get_date(optarg
, 0);
1148 die(EXIT_FAILURE
, "bad expiry date `%s'", optarg
);
1152 /* --- Store comments without interpretation --- */
1155 if (key_chkcomment(optarg
))
1156 die(EXIT_FAILURE
, "bad comment string `%s'", optarg
);
1160 /* --- Elliptic curve parameters --- */
1166 /* --- Store tags --- */
1169 if (key_chkident(optarg
))
1170 die(EXIT_FAILURE
, "bad tag string `%s'", optarg
);
1177 /* --- Seeding --- */
1180 const struct seedalg
*ss
;
1181 if (strcmp(optarg
, "list") == 0) {
1182 printf("Seed algorithms:\n");
1183 for (ss
= seedtab
; ss
->p
; ss
++)
1184 printf(" %s\n", ss
->p
);
1187 if (seed
) die(EXIT_FAILURE
, "seed already set -- put -A first");
1189 for (ss
= seedtab
; ss
->p
; ss
++) {
1190 if (strcmp(optarg
, ss
->p
) == 0)
1194 die(EXIT_FAILURE
, "seed algorithm `%s' not known", optarg
);
1201 if (seed
) die(EXIT_FAILURE
, "seed already set");
1202 c
= base64_class
.decoder(CDCF_IGNEQPAD
);
1203 if ((rc
= c
->ops
->code(c
, optarg
, strlen(optarg
), &d
)) != 0 ||
1204 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1205 die(EXIT_FAILURE
, "invalid seed base64: %s", codec_strerror(rc
));
1207 k
.r
= sa
->gen(d
.buf
, d
.len
);
1216 unsigned n
= strtoul(optarg
, &p
, 0);
1217 if (n
== 0 || *p
!= 0 || n
% 8 != 0)
1218 die(EXIT_FAILURE
, "bad seed length `%s'", optarg
);
1219 if (seed
) die(EXIT_FAILURE
, "seed already set");
1222 rand_get(RAND_GLOBAL
, p
, n
);
1223 c
= base64_class
.encoder(0, "", 0);
1224 c
->ops
->code(c
, p
, n
, &d
); c
->ops
->code(c
, 0, 0, &d
);
1227 k
.r
= sa
->gen(p
, n
);
1230 /* --- Key id --- */
1237 id
= strtoul(optarg
, &p
, 16);
1238 if (errno
|| *p
|| id
> MASK32
)
1239 die(EXIT_FAILURE
, "bad key-id `%s'", optarg
);
1243 /* --- Public exponent --- */
1247 k
.e
= mp_readstring(k
.e
, optarg
, &p
, 0);
1248 if (!k
.e
|| *p
|| MP_CMP(k
.e
, <, MP_THREE
) || MP_EVENP(k
.e
))
1249 die(EXIT_FAILURE
, "bad exponent `%s'", optarg
);
1252 /* --- Other flags --- */
1273 /* --- Other things are bogus --- */
1281 /* --- Various sorts of bogosity --- */
1283 if ((k
.f
& f_bogus
) || optind
+ 1 > argc
) {
1285 "Usage: add [OPTIONS] TYPE [ATTR...]");
1287 if (key_chkident(argv
[optind
]))
1288 die(EXIT_FAILURE
, "bad key type `%s'", argv
[optind
]);
1290 /* --- Set up various bits of the state --- */
1292 if (exp
== KEXP_EXPIRE
)
1293 exp
= time(0) + 14 * 24 * 60 * 60;
1295 /* --- Open the file and create the basic key block --- *
1297 * Keep on generating keyids until one of them doesn't collide.
1300 doopen(&f
, KOPEN_WRITE
);
1303 /* --- Key the generator --- */
1309 if ((err
= key_new(&f
, kid
, argv
[optind
], exp
, &k
.k
)) == 0)
1311 else if (err
!= KERR_DUPID
)
1312 die(EXIT_FAILURE
, "error adding new key: %s", key_strerror(err
));
1315 /* --- Set various simple attributes --- */
1320 if (k
.f
& f_retag
) {
1321 if ((kk
= key_bytag(&f
, tag
)) != 0 &&
1323 strcmp(kk
->tag
, tag
) == 0)
1324 key_settag(&f
, kk
, 0);
1326 if ((err
= key_settag(&f
, k
.k
, tag
)) != 0)
1327 die(EXIT_FAILURE
, "error setting key tag: %s", key_strerror(err
));
1331 int err
= key_setcomment(&f
, k
.k
, c
);
1333 die(EXIT_FAILURE
, "error setting key comment: %s", key_strerror(err
));
1336 setattr(&f
, k
.k
, argv
+ optind
+ 1);
1338 key_putattr(&f
, k
.k
, "genseed", seed
);
1339 key_putattr(&f
, k
.k
, "seedalg", sa
->p
);
1342 key_fulltag(k
.k
, &k
.tag
);
1344 /* --- Find the parameter key --- */
1346 if (ptag
&& (k
.p
= key_bytag(&f
, ptag
)) == 0)
1347 die(EXIT_FAILURE
, "parameter key `%s' not found", ptag
);
1349 /* --- Now generate the actual key data --- */
1355 dstr_destroy(&k
.tag
);
1360 /*----- Key listing -------------------------------------------------------*/
1362 /* --- Listing options --- */
1364 typedef struct listopts
{
1365 const char *tfmt
; /* Date format (@strftime@-style) */
1366 int v
; /* Verbosity level */
1367 unsigned f
; /* Various flags */
1368 time_t t
; /* Time now (for key expiry) */
1369 key_filter kf
; /* Filter for matching keys */
1372 /* --- Listing flags --- */
1374 #define f_newline 2u /* Write newline before next entry */
1375 #define f_attr 4u /* Written at least one attribute */
1376 #define f_utc 8u /* Emit UTC time, not local time */
1378 /* --- @showkeydata@ --- *
1380 * Arguments: @key_data *k@ = pointer to key to write
1381 * @int ind@ = indentation level
1382 * @listopts *o@ = listing options
1383 * @dstr *d@ = tag string for this subkey
1387 * Use: Emits a piece of key data in a human-readable format.
1390 static void showkeydata(key_data
*k
, int ind
, listopts
*o
, dstr
*d
)
1392 #define INDENT(i) do { \
1394 for (_i = 0; _i < (i); _i++) { \
1399 switch (k
->e
& KF_ENCMASK
) {
1401 /* --- Binary key data --- *
1403 * Emit as a simple hex dump.
1407 const octet
*p
= k
->u
.k
.k
;
1408 const octet
*l
= p
+ k
->u
.k
.sz
;
1411 fputs(" {", stdout
);
1416 } else if (sz
% 8 == 0)
1420 printf("%02x", *p
++);
1425 fputs("}\n", stdout
);
1428 /* --- Encrypted data --- *
1430 * If the user is sufficiently keen, ask for a passphrase and decrypt the
1431 * key. Otherwise just say that it's encrypted and move on.
1436 fputs(" encrypted\n", stdout
);
1439 if (key_punlock(&kd
, k
, d
->buf
))
1440 printf(" <failed to unlock %s>\n", d
->buf
);
1442 fputs(" encrypted", stdout
);
1443 showkeydata(kd
, ind
, o
, d
);
1449 /* --- Integer keys --- *
1451 * Emit as a large integer in decimal. This makes using the key in
1452 * `calc' or whatever easier.
1457 mp_writefile(k
->u
.m
, stdout
, 10);
1461 /* --- Strings --- */
1464 printf(" `%s'\n", k
->u
.p
);
1467 /* --- Elliptic curve points --- */
1470 if (EC_ATINF(&k
->u
.e
))
1471 fputs(" inf\n", stdout
);
1473 fputs(" 0x", stdout
); mp_writefile(k
->u
.e
.x
, stdout
, 16);
1474 fputs(", 0x", stdout
); mp_writefile(k
->u
.e
.y
, stdout
, 16);
1479 /* --- Structured keys --- *
1481 * Just iterate over the subkeys.
1489 fputs(" {\n", stdout
);
1490 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
1491 if (!key_match(k
, &o
->kf
))
1494 printf("%s =", tag
);
1496 dstr_putf(d
, ".%s", tag
);
1497 showkeydata(k
, ind
+ 2, o
, d
);
1500 fputs("}\n", stdout
);
1507 /* --- @showkey@ --- *
1509 * Arguments: @key *k@ = pointer to key to show
1510 * @listopts *o@ = pointer to listing options
1514 * Use: Emits a listing of a particular key.
1517 static void showkey(key
*k
, listopts
*o
)
1519 char ebuf
[24], dbuf
[24];
1522 /* --- Skip the key if the filter doesn't match --- */
1524 if (!key_match(k
->k
, &o
->kf
))
1527 /* --- Sort out the expiry and deletion times --- */
1529 if (KEY_EXPIRED(o
->t
, k
->exp
))
1530 strcpy(ebuf
, "expired");
1531 else if (k
->exp
== KEXP_FOREVER
)
1532 strcpy(ebuf
, "forever");
1534 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->exp
) : localtime(&k
->exp
);
1535 strftime(ebuf
, sizeof(ebuf
), o
->tfmt
, tm
);
1538 if (KEY_EXPIRED(o
->t
, k
->del
))
1539 strcpy(dbuf
, "deleted");
1540 else if (k
->del
== KEXP_FOREVER
)
1541 strcpy(dbuf
, "forever");
1543 tm
= (o
->f
& f_utc
) ?
gmtime(&k
->del
) : localtime(&k
->del
);
1544 strftime(dbuf
, sizeof(dbuf
), o
->tfmt
, tm
);
1547 /* --- If in compact format, just display and quit --- */
1550 if (!(o
->f
& f_newline
)) {
1551 printf("%8s %-20s %-20s %-10s %s\n",
1552 "Id", "Tag", "Type", "Expire", "Delete");
1554 printf("%08lx %-20s %-20s %-10s %s\n",
1555 (unsigned long)k
->id
, k
->tag ? k
->tag
: "<none>",
1556 k
->type
, ebuf
, dbuf
);
1561 /* --- Display the standard header --- */
1563 if (o
->f
& f_newline
)
1564 fputc('\n', stdout
);
1565 printf("keyid: %08lx\n", (unsigned long)k
->id
);
1566 printf("tag: %s\n", k
->tag ? k
->tag
: "<none>");
1567 printf("type: %s\n", k
->type
);
1568 printf("expiry: %s\n", ebuf
);
1569 printf("delete: %s\n", dbuf
);
1570 printf("comment: %s\n", k
->c ? k
->c
: "<none>");
1572 /* --- Display the attributes --- */
1576 const char *av
, *an
;
1579 printf("attributes:");
1580 for (key_mkattriter(&i
, k
); key_nextattr(&i
, &an
, &av
); ) {
1581 printf("\n %s = %s", an
, av
);
1585 fputc('\n', stdout
);
1590 /* --- If dumping requested, dump the raw key data --- */
1594 fputs("key:", stdout
);
1596 showkeydata(k
->k
, 0, o
, &d
);
1603 /* --- @cmd_list@ --- */
1605 static int cmd_list(int argc
, char *argv
[])
1609 listopts o
= { 0, 0, 0, 0, { 0, 0 } };
1611 /* --- Parse subcommand options --- */
1614 static struct option opt
[] = {
1615 { "quiet", 0, 0, 'q' },
1616 { "verbose", 0, 0, 'v' },
1617 { "utc", 0, 0, 'u' },
1618 { "filter", OPTF_ARGREQ
, 0, 'f' },
1621 int i
= mdwopt(argc
, argv
, "+uqvf:", opt
, 0, 0, 0);
1638 int e
= key_readflags(optarg
, &p
, &o
.kf
.f
, &o
.kf
.m
);
1640 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1649 die(EXIT_FAILURE
, "Usage: list [-uqv] [-f FILTER] [TAG...]");
1651 /* --- Open the key file --- */
1653 doopen(&f
, KOPEN_READ
);
1656 /* --- Set up the time format --- */
1659 o
.tfmt
= "%Y-%m-%d";
1660 else if (o
.f
& f_utc
)
1661 o
.tfmt
= "%Y-%m-%d %H:%M:%S UTC";
1663 o
.tfmt
= "%Y-%m-%d %H:%M:%S %Z";
1665 /* --- If specific keys were requested use them, otherwise do all --- *
1667 * Some day, this might turn into a wildcard match.
1670 if (optind
< argc
) {
1672 if ((k
= key_bytag(&f
, argv
[optind
])) != 0)
1675 moan("key `%s' not found", argv
[optind
]);
1679 } while (optind
< argc
);
1682 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1690 return (EXIT_FAILURE
);
1695 /*----- Command implementation --------------------------------------------*/
1697 /* --- @cmd_expire@ --- */
1699 static int cmd_expire(int argc
, char *argv
[])
1707 die(EXIT_FAILURE
, "Usage: expire TAG...");
1708 doopen(&f
, KOPEN_WRITE
);
1709 for (i
= 1; i
< argc
; i
++) {
1710 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1713 moan("key `%s' not found", argv
[i
]);
1721 /* --- @cmd_delete@ --- */
1723 static int cmd_delete(int argc
, char *argv
[])
1731 die(EXIT_FAILURE
, "Usage: delete TAG...");
1732 doopen(&f
, KOPEN_WRITE
);
1733 for (i
= 1; i
< argc
; i
++) {
1734 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
1737 moan("key `%s' not found", argv
[i
]);
1745 /* --- @cmd_setattr@ --- */
1747 static int cmd_setattr(int argc
, char *argv
[])
1753 die(EXIT_FAILURE
, "Usage: setattr TAG ATTR...");
1754 doopen(&f
, KOPEN_WRITE
);
1755 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1756 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1757 setattr(&f
, k
, argv
+ 2);
1762 /* --- @cmd_getattr@ --- */
1764 static int cmd_getattr(int argc
, char *argv
[])
1772 die(EXIT_FAILURE
, "Usage: getattr TAG ATTR");
1773 doopen(&f
, KOPEN_READ
);
1774 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1775 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1777 if ((p
= key_getattr(&f
, k
, argv
[2])) == 0)
1778 die(EXIT_FAILURE
, "no attribute `%s' for key `%s'", argv
[2], d
.buf
);
1785 /* --- @cmd_finger@ --- */
1787 static const struct fpres
{
1789 const codec_class
*cdc
;
1790 unsigned short ival
;
1793 { "hex", &hex_class
, 8, "-:" },
1794 { "base32", &base32_class
, 6, ":" },
1798 static void fingerprint(key
*k
, const struct fpres
*fpres
,
1799 const gchash
*ch
, const key_filter
*kf
)
1802 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1808 if (key_fingerprint(k
, h
, kf
)) {
1810 c
= fpres
->cdc
->encoder(CDCF_LOWERC
| CDCF_NOEQPAD
, "", 0);
1811 c
->ops
->code(c
, p
, ch
->hashsz
, &dd
); c
->ops
->code(c
, 0, 0, &dd
);
1813 for (i
= 0; i
< dd
.len
; i
++) {
1814 if (i
&& i
%fpres
->ival
== 0) dstr_putc(&d
, fpres
->sep
[0]);
1815 dstr_putc(&d
, dd
.buf
[i
]);
1817 dstr_putc(&d
, ' '); key_fulltag(k
, &d
); dstr_putc(&d
, '\n');
1818 dstr_write(&d
, stdout
);
1820 dstr_destroy(&d
); dstr_destroy(&dd
);
1824 static const struct fpres
*lookup_fpres(const char *name
)
1826 const struct fpres
*fpres
;
1827 for (fpres
= fprestab
; fpres
->name
; fpres
++)
1828 if (strcmp(fpres
->name
, name
) == 0) return (fpres
);
1829 die(EXIT_FAILURE
, "unknown presentation syle `%s'", name
);
1832 static int cmd_finger(int argc
, char *argv
[])
1836 const struct fpres
*fpres
= fprestab
;
1837 const gchash
*ch
= &rmd160
;
1838 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1841 static struct option opt
[] = {
1842 { "filter", OPTF_ARGREQ
, 0, 'f' },
1843 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1844 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1847 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1853 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1855 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1858 fpres
= lookup_fpres(optarg
);
1861 if ((ch
= ghash_byname(optarg
)) == 0)
1862 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1870 argv
+= optind
; argc
-= optind
;
1873 "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]");
1876 doopen(&f
, KOPEN_READ
);
1880 for (i
= 0; i
< argc
; i
++) {
1881 key
*k
= key_bytag(&f
, argv
[i
]);
1883 fingerprint(k
, fpres
, ch
, &kf
);
1886 moan("key `%s' not found", argv
[i
]);
1892 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
1893 fingerprint(k
, fpres
, ch
, &kf
);
1898 /* --- @cmd_verify@ --- */
1900 static int cmd_verify(int argc
, char *argv
[])
1904 const gchash
*ch
= &rmd160
;
1908 dstr d
= DSTR_INIT
, dd
= DSTR_INIT
;
1911 const struct fpres
*fpres
= fprestab
;
1912 key_filter kf
= { KF_NONSECRET
, KF_NONSECRET
};
1915 static struct option opt
[] = {
1916 { "filter", OPTF_ARGREQ
, 0, 'f' },
1917 { "presentation", OPTF_ARGREQ
, 0, 'p' },
1918 { "algorithm", OPTF_ARGREQ
, 0, 'a' },
1921 int i
= mdwopt(argc
, argv
, "+f:a:p:", opt
, 0, 0, 0);
1927 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
1929 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
1932 fpres
= lookup_fpres(optarg
);
1935 if ((ch
= ghash_byname(optarg
)) == 0)
1936 die(EXIT_FAILURE
, "unknown hash algorithm `%s'", optarg
);
1944 argv
+= optind
; argc
-= optind
;
1945 if (rc
|| argc
!= 2) {
1947 "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT");
1950 doopen(&f
, KOPEN_READ
);
1952 if ((k
= key_bytag(&f
, argv
[0])) == 0)
1953 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
1954 for (p
= argv
[1]; *p
; p
++) {
1955 if (strchr(fpres
->sep
, *p
)) continue;
1958 c
= fpres
->cdc
->decoder(CDCF_IGNCASE
| CDCF_IGNEQPAD
|
1959 CDCF_IGNSPC
| CDCF_IGNNEWL
);
1960 if ((rc
= c
->ops
->code(c
, dd
.buf
, dd
.len
, &d
)) != 0 ||
1961 (rc
= c
->ops
->code(c
, 0, 0, &d
)) != 0)
1962 die(EXIT_FAILURE
, "invalid fingerprint: %s", codec_strerror(rc
));
1964 if (d
.len
!= ch
->hashsz
) {
1965 die(EXIT_FAILURE
, "incorrect fingerprint length (%lu != %lu)",
1966 (unsigned long)d
.len
, (unsigned long)ch
->hashsz
);
1969 if (!key_fingerprint(k
, h
, &kf
))
1970 die(EXIT_FAILURE
, "key has no fingerprintable components (as filtered)");
1971 fpr
= GH_DONE(h
, 0);
1972 if (memcmp(fpr
, d
.buf
, ch
->hashsz
) != 0)
1973 die(EXIT_FAILURE
, "key fingerprint mismatch");
1974 dstr_destroy(&d
); dstr_destroy(&dd
);
1979 /* --- @cmd_comment@ --- */
1981 static int cmd_comment(int argc
, char *argv
[])
1987 if (argc
< 2 || argc
> 3)
1988 die(EXIT_FAILURE
, "Usage: comment TAG [COMMENT]");
1989 doopen(&f
, KOPEN_WRITE
);
1990 if ((k
= key_bytag(&f
, argv
[1])) == 0)
1991 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
1992 if ((err
= key_setcomment(&f
, k
, argv
[2])) != 0)
1993 die(EXIT_FAILURE
, "bad comment `%s': %s", argv
[2], key_strerror(err
));
1998 /* --- @cmd_tag@ --- */
2000 static int cmd_tag(int argc
, char *argv
[])
2009 static struct option opt
[] = {
2010 { "retag", 0, 0, 'r' },
2013 int i
= mdwopt(argc
, argv
, "+r", opt
, 0, 0, 0);
2026 argv
+= optind
; argc
-= optind
;
2027 if (argc
< 1 || argc
> 2 || rc
)
2028 die(EXIT_FAILURE
, "Usage: tag [-r] TAG [NEW-TAG]");
2029 doopen(&f
, KOPEN_WRITE
);
2030 if (flags
& f_retag
) {
2031 if ((k
= key_bytag(&f
, argv
[1])) != 0 && strcmp(k
->tag
, argv
[1]) == 0)
2032 key_settag(&f
, k
, 0);
2034 if ((k
= key_bytag(&f
, argv
[0])) == 0)
2035 die(EXIT_FAILURE
, "key `%s' not found", argv
[0]);
2036 if ((err
= key_settag(&f
, k
, argv
[1])) != 0)
2037 die(EXIT_FAILURE
, "bad tag `%s': %s", argv
[1], key_strerror(err
));
2042 /* --- @cmd_lock@ --- */
2044 static int cmd_lock(int argc
, char *argv
[])
2052 die(EXIT_FAILURE
, "Usage: lock QTAG");
2053 doopen(&f
, KOPEN_WRITE
);
2054 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2055 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2056 if ((*kd
)->e
== KENC_ENCRYPT
&& key_punlock(kd
, 0, d
.buf
))
2057 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2058 if (key_plock(kd
, 0, d
.buf
))
2059 die(EXIT_FAILURE
, "failed to lock key `%s'", d
.buf
);
2065 /* --- @cmd_unlock@ --- */
2067 static int cmd_unlock(int argc
, char *argv
[])
2075 die(EXIT_FAILURE
, "Usage: unlock QTAG");
2076 doopen(&f
, KOPEN_WRITE
);
2077 if (key_qtag(&f
, argv
[1], &d
, &k
, &kd
))
2078 die(EXIT_FAILURE
, "key `%s' not found", argv
[1]);
2079 if ((*kd
)->e
!= KENC_ENCRYPT
)
2080 die(EXIT_FAILURE
, "key `%s' is not encrypted", d
.buf
);
2081 if (key_punlock(kd
, 0, d
.buf
))
2082 die(EXIT_FAILURE
, "couldn't unlock key `%s'", d
.buf
);
2088 /* --- @cmd_extract@ --- */
2090 static int cmd_extract(int argc
, char *argv
[])
2096 key_filter kf
= { 0, 0 };
2098 const char *outfile
= 0;
2102 static struct option opt
[] = {
2103 { "filter", OPTF_ARGREQ
, 0, 'f' },
2106 int i
= mdwopt(argc
, argv
, "f:", opt
, 0, 0, 0);
2112 int err
= key_readflags(optarg
, &p
, &kf
.f
, &kf
.m
);
2114 die(EXIT_FAILURE
, "bad filter string `%s'", optarg
);
2122 argv
+= optind
; argc
-= optind
;
2124 die(EXIT_FAILURE
, "Usage: extract [-f FILTER] FILE [TAG...]");
2125 if (strcmp(*argv
, "-") == 0)
2129 dstr_putf(&d
, "%s.new", outfile
);
2130 if (!(fp
= fopen(d
.buf
, "w"))) {
2131 die(EXIT_FAILURE
, "couldn't open `%s' for writing: %s",
2132 d
.buf
, strerror(errno
));
2136 doopen(&f
, KOPEN_READ
);
2140 for (key_mkiter(&i
, &f
); (k
= key_next(&i
)) != 0; )
2141 key_extract(&f
, k
, fp
, &kf
);
2143 for (i
= 1; i
< argc
; i
++) {
2144 if ((k
= key_bytag(&f
, argv
[i
])) != 0)
2145 key_extract(&f
, k
, fp
, &kf
);
2147 moan("key `%s' not found", argv
[i
]);
2152 if (fclose(fp
) || (outfile
&& rename(d
.buf
, outfile
)))
2153 die(EXIT_FAILURE
, "error writing file: %s", strerror(errno
));
2159 /* --- @cmd_tidy@ --- */
2161 static int cmd_tidy(int argc
, char *argv
[])
2165 die(EXIT_FAILURE
, "Usage: tidy");
2166 doopen(&f
, KOPEN_WRITE
);
2167 f
.f
|= KF_MODIFIED
; /* Nasty hack */
2172 /* --- @cmd_merge@ --- */
2174 static int cmd_merge(int argc
, char *argv
[])
2180 die(EXIT_FAILURE
, "Usage: merge FILE");
2181 if (strcmp(argv
[1], "-") == 0)
2183 else if (!(fp
= fopen(argv
[1], "r"))) {
2184 die(EXIT_FAILURE
, "couldn't open `%s' for reading: %s",
2185 argv
[1], strerror(errno
));
2188 doopen(&f
, KOPEN_WRITE
);
2189 key_merge(&f
, argv
[1], fp
, key_moan
, 0);
2194 /* --- @cmd_show@ --- */
2198 listtab[i].name, listtab[i].name) \
2199 LI("Hash functions", hash, \
2200 ghashtab[i], ghashtab[i]->name) \
2201 LI("Elliptic curves", ec, \
2202 ectab[i].name, ectab[i].name) \
2203 LI("Prime Diffie-Hellman groups", dh, \
2204 ptab[i].name, ptab[i].name) \
2205 LI("Binary Diffie-Hellman groups", bindh, \
2206 bintab[i].name, bintab[i].name) \
2207 LI("Key-generation algorithms", keygen, \
2208 algtab[i].name, algtab[i].name) \
2209 LI("Random seeding algorithms", seed, \
2210 seedtab[i].p, seedtab[i].p) \
2211 LI("Fingerprint presentation styles", fpres, \
2212 fprestab[i].name, fprestab[i].name)
2214 MAKELISTTAB(listtab
, LISTS
)
2216 static int cmd_show(int argc
, char *argv
[])
2218 return (displaylists(listtab
, argv
+ 1));
2221 /*----- Main command table ------------------------------------------------*/
2223 static int cmd_help(int argc
, char *argv
[]);
2225 static cmd cmds
[] = {
2226 { "help", cmd_help
, "help [COMMAND...]" },
2227 { "show", cmd_show
, "show [ITEM...]" },
2228 { "list", cmd_list
, "list [-uqv] [-f FILTER] [TAG...]", "\
2231 -u, --utc Display expiry times etc. in UTC, not local time.\n\
2232 -q, --quiet Show less information.\n\
2233 -v, --verbose Show more information.\n\
2235 { "fingerprint", cmd_finger
,
2236 "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\
2239 -f, --filter=FILT Only hash key components matching FILT.\n\
2240 -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\
2241 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2242 ($ show hash for list.)\n\
2244 { "verify", cmd_verify
,
2245 "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\
2248 -f, --filter=FILT Only hash key components matching FILT.\n\
2249 -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\
2250 -a, --algorithm=HASH Use the named HASH algorithm.\n\
2251 ($ show hash for list.)\n\
2253 { "extract", cmd_extract
, "extract [-f FILTER] FILE [TAG...]", "\
2256 -f, --filter=FILT Only extract key components matching FILT.\n\
2258 { "merge", cmd_merge
, "merge FILE" },
2259 { "expire", cmd_expire
, "expire TAG..." },
2260 { "delete", cmd_delete
, "delete TAG..." },
2261 { "setattr", cmd_setattr
, "setattr TAG ATTR..." },
2262 { "getattr", cmd_getattr
, "getattr TAG ATTR" },
2263 { "comment", cmd_comment
, "comment TAG [COMMENT]" },
2264 { "lock", cmd_lock
, "lock QTAG" },
2265 { "unlock", cmd_unlock
, "unlock QTAG" },
2266 { "tag", cmd_tag
, "tag [-r] TAG [NEW-TAG]", "\
2269 -r, --retag Untag any key currently called new-tag.\n\
2271 { "tidy", cmd_tidy
, "tidy" },
2273 "add [-OPTIONS] TYPE [ATTR...]\n\
2274 Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\
2275 [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\
2276 [-e EXPIRE] [-t TAG] [-c COMMENT]", "\
2279 -a, --algorithm=ALG Generate keys suitable for ALG.\n\
2280 ($ show keygen for list.)\n\
2281 -b, --bits=N Generate an N-bit key.\n\
2282 -B, --qbits=N Use an N-bit subgroup or factors.\n\
2283 -p, --parameters=TAG Get group parameters from TAG.\n\
2284 -C, --curve=NAME Use elliptic curve or DH group NAME.\n\
2285 ($ show ec or $ show dh for list.)\n\
2286 -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\
2287 ($ show seed for list.)\n\
2288 -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\
2289 -n, --newseed=COUNT Generate new COUNT-bit seed.\n\
2290 -e, --expire=TIME Make the key expire after TIME.\n\
2291 -c, --comment=STRING Attach the command STRING to the key.\n\
2292 -t, --tag=TAG Tag the key with the name TAG.\n\
2293 -r, --retag Untag any key currently with that tag.\n\
2294 -R, --rand-id=TAG Use key named TAG for the random number generator.\n\
2295 -I, --key-id=ID Force the key-id for the new key.\n\
2296 -l, --lock Lock the generated key with a passphrase.\n\
2297 -q, --quiet Don't give progress indicators while working.\n\
2298 -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\
2299 -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\
2300 -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\
2305 static int cmd_help(int argc
, char *argv
[])
2307 sc_help(cmds
, stdout
, argv
+ 1);
2311 /*----- Main code ---------------------------------------------------------*/
2313 /* --- Helpful GNUy functions --- */
2315 static void usage(FILE *fp
)
2317 pquis(fp
, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
2320 void version(FILE *fp
)
2322 pquis(fp
, "$, Catacomb version " VERSION
"\n");
2325 void help_global(FILE *fp
)
2329 Performs various simple key management operations.\n\
2331 Global command line options:\n\
2333 -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\
2334 -v, --version Display version number.\n\
2335 -u, --usage Display short usage summary.\n\
2337 -k, --keyring=FILE Read and write keys in FILE.\n",
2343 * Arguments: @int argc@ = number of command line arguments
2344 * @char *argv[]@ = array of command line arguments
2346 * Returns: Nonzero on failure.
2348 * Use: Main program. Performs simple key management functions.
2351 int main(int argc
, char *argv
[])
2357 /* --- Initialization --- */
2362 /* --- Parse command line options --- */
2365 static struct option opt
[] = {
2367 /* --- Standard GNUy help options --- */
2369 { "help", 0, 0, 'h' },
2370 { "version", 0, 0, 'v' },
2371 { "usage", 0, 0, 'u' },
2373 /* --- Real live useful options --- */
2375 { "keyring", OPTF_ARGREQ
, 0, 'k' },
2377 /* --- Magic terminator --- */
2381 int i
= mdwopt(argc
, argv
, "+hvu k:", opt
, 0, 0, 0);
2387 /* --- GNU help options --- */
2390 sc_help(cmds
, stdout
, argv
+ optind
);
2399 /* --- Real useful options --- */
2405 /* --- Bogosity --- */
2413 /* --- Complain about excessive bogons --- */
2415 if (f
& f_bogus
|| optind
== argc
) {
2420 /* --- Initialize the Catacomb random number generator --- */
2422 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
2423 rand_seed(RAND_GLOBAL
, 160);
2425 /* --- Dispatch to appropriate command handler --- */
2430 return (findcmd(cmds
, argv
[0])->cmd(argc
, argv
));
2433 /*----- That's all, folks -------------------------------------------------*/