Commit | Line | Data |
---|---|---|
d03ab969 | 1 | /* -*-c-*- |
2 | * | |
d03ab969 | 3 | * Simple key manager program |
4 | * | |
252c122d | 5 | * (c) 1999 Straylight/Edgeware |
d03ab969 | 6 | */ |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
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. | |
45c0fd36 | 16 | * |
d03ab969 | 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. | |
45c0fd36 | 21 | * |
d03ab969 | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
d03ab969 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
cd6eca43 MW |
30 | #define _FILE_OFFSET_BITS 64 |
31 | ||
d03ab969 | 32 | #include "config.h" |
33 | ||
58507325 | 34 | #include <ctype.h> |
d03ab969 | 35 | #include <errno.h> |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <time.h> | |
40 | ||
2b4be81b | 41 | #include <mLib/codec.h> |
16810bbd | 42 | #include <mLib/base32.h> |
252c122d | 43 | #include <mLib/base64.h> |
16810bbd | 44 | #include <mLib/hex.h> |
d03ab969 | 45 | #include <mLib/mdwopt.h> |
46 | #include <mLib/quis.h> | |
47 | #include <mLib/report.h> | |
48 | #include <mLib/sub.h> | |
49 | ||
50 | #include <noise.h> | |
51 | #include <rand.h> | |
52 | ||
3688eb75 | 53 | #include "bintab.h" |
252c122d | 54 | #include "bbs.h" |
052b36d0 | 55 | #include "dh.h" |
252c122d | 56 | #include "dsa.h" |
34e4f738 | 57 | #include "dsarand.h" |
1ba83484 | 58 | #include "ec.h" |
59 | #include "ec-keys.h" | |
60 | #include "ectab.h" | |
252c122d | 61 | #include "fibrand.h" |
d03ab969 | 62 | #include "getdate.h" |
3688eb75 | 63 | #include "gfreduce.h" |
d03ab969 | 64 | #include "key.h" |
252c122d | 65 | #include "mp.h" |
66 | #include "mpmont.h" | |
67 | #include "mprand.h" | |
68 | #include "mptext.h" | |
69 | #include "pgen.h" | |
3563e365 | 70 | #include "ptab.h" |
252c122d | 71 | #include "rsa.h" |
d03ab969 | 72 | |
c65df279 | 73 | #include "cc.h" |
34e4f738 | 74 | #include "sha-mgf.h" |
75 | #include "sha256-mgf.h" | |
76 | #include "sha224-mgf.h" | |
77 | #include "sha384-mgf.h" | |
78 | #include "sha512-mgf.h" | |
79 | #include "tiger-mgf.h" | |
80 | #include "rmd128-mgf.h" | |
81 | #include "rmd160-mgf.h" | |
82 | #include "rmd256-mgf.h" | |
83 | #include "rmd320-mgf.h" | |
84 | #include "md5-mgf.h" | |
85 | #include "dsarand.h" | |
86 | ||
d03ab969 | 87 | /*----- Handy global state ------------------------------------------------*/ |
88 | ||
89 | static const char *keyfile = "keyring"; | |
90 | ||
91 | /*----- Useful shared functions -------------------------------------------*/ | |
92 | ||
93 | /* --- @doopen@ --- * | |
94 | * | |
95 | * Arguments: @key_file *f@ = pointer to key file block | |
96 | * @unsigned how@ = method to open file with | |
97 | * | |
98 | * Returns: --- | |
99 | * | |
100 | * Use: Opens a key file and handles errors by panicking | |
101 | * appropriately. | |
102 | */ | |
103 | ||
104 | static void doopen(key_file *f, unsigned how) | |
105 | { | |
204607bd MW |
106 | if (key_open(f, keyfile, how, key_moan, 0)){ |
107 | die(EXIT_FAILURE, "couldn't open keyring `%s': %s", | |
108 | keyfile, strerror(errno)); | |
109 | } | |
d03ab969 | 110 | } |
111 | ||
112 | /* --- @doclose@ --- * | |
113 | * | |
114 | * Arguments: @key_file *f@ = pointer to key file block | |
115 | * | |
116 | * Returns: --- | |
117 | * | |
118 | * Use: Closes a key file and handles errors by panicking | |
119 | * appropriately. | |
120 | */ | |
121 | ||
122 | static void doclose(key_file *f) | |
123 | { | |
124 | switch (key_close(f)) { | |
125 | case KWRITE_FAIL: | |
126 | die(EXIT_FAILURE, "couldn't write file `%s': %s", | |
127 | keyfile, strerror(errno)); | |
128 | case KWRITE_BROKEN: | |
129 | die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)", | |
130 | keyfile, strerror(errno)); | |
131 | } | |
132 | } | |
133 | ||
134 | /* --- @setattr@ --- * | |
135 | * | |
136 | * Arguments: @key_file *f@ = pointer to key file block | |
137 | * @key *k@ = pointer to key block | |
138 | * @char *v[]@ = array of assignments (overwritten!) | |
139 | * | |
140 | * Returns: --- | |
141 | * | |
142 | * Use: Applies the attribute assignments to the key. | |
143 | */ | |
144 | ||
145 | static void setattr(key_file *f, key *k, char *v[]) | |
146 | { | |
147 | while (*v) { | |
252c122d | 148 | int err; |
d03ab969 | 149 | char *p = *v; |
150 | size_t eq = strcspn(p, "="); | |
72f13799 | 151 | if (!p[eq]) { |
152 | moan("invalid assignment: `%s' (ignored)", p); | |
153 | v++; | |
154 | continue; | |
155 | } | |
d03ab969 | 156 | p[eq] = 0; |
157 | p += eq + 1; | |
252c122d | 158 | if ((err = key_putattr(f, k, *v, *p ? p : 0)) != 0) |
159 | die(EXIT_FAILURE, "couldn't set attributes: %s", key_strerror(err)); | |
d03ab969 | 160 | v++; |
161 | } | |
162 | } | |
163 | ||
34e4f738 | 164 | /*----- Seeding -----------------------------------------------------------*/ |
165 | ||
166 | const struct seedalg { const char *p; grand *(*gen)(const void *, size_t); } | |
167 | seedtab[] = { | |
45c0fd36 MW |
168 | { "dsarand", dsarand_create }, |
169 | { "rmd128-mgf", rmd128_mgfrand }, | |
170 | { "rmd160-mgf", rmd160_mgfrand }, | |
171 | { "rmd256-mgf", rmd256_mgfrand }, | |
172 | { "rmd320-mgf", rmd320_mgfrand }, | |
173 | { "sha-mgf", sha_mgfrand }, | |
174 | { "sha224-mgf", sha224_mgfrand }, | |
175 | { "sha256-mgf", sha256_mgfrand }, | |
176 | { "sha384-mgf", sha384_mgfrand }, | |
177 | { "sha512-mgf", sha512_mgfrand }, | |
178 | { "tiger-mgf", tiger_mgfrand }, | |
34e4f738 | 179 | { 0, 0 } |
180 | }; | |
181 | ||
182 | #define SEEDALG_DEFAULT (seedtab + 2) | |
183 | ||
252c122d | 184 | /*----- Key generation ----------------------------------------------------*/ |
185 | ||
186 | /* --- Key generation parameters --- */ | |
187 | ||
188 | typedef struct keyopts { | |
189 | key_file *kf; /* Pointer to key file */ | |
190 | key *k; /* Pointer to the actual key */ | |
191 | dstr tag; /* Full tag name for the key */ | |
192 | unsigned f; /* Flags for the new key */ | |
193 | unsigned bits, qbits; /* Bit length for the new key */ | |
1ba83484 | 194 | const char *curve; /* Elliptic curve name/info */ |
34e4f738 | 195 | grand *r; /* Random number source */ |
252c122d | 196 | key *p; /* Parameters key-data */ |
197 | } keyopts; | |
198 | ||
16efd15b | 199 | #define f_bogus 1u /* Error in parsing */ |
200 | #define f_lock 2u /* Passphrase-lock private key */ | |
201 | #define f_quiet 4u /* Don't show a progress indicator */ | |
202 | #define f_limlee 8u /* Generate Lim-Lee primes */ | |
203 | #define f_subgroup 16u /* Generate a subgroup */ | |
ce70ec47 | 204 | #define f_retag 32u /* Remove any existing tag */ |
4e67e30b | 205 | #define f_kcdsa 64u /* Generate KCDSA primes */ |
252c122d | 206 | |
207 | /* --- @dolock@ --- * | |
208 | * | |
209 | * Arguments: @keyopts *k@ = key generation options | |
ef13e9a4 | 210 | * @key_data **kd@ = pointer to key data to lock |
252c122d | 211 | * @const char *t@ = tag suffix or null |
212 | * | |
213 | * Returns: --- | |
214 | * | |
215 | * Use: Does passphrase locking on new keys. | |
216 | */ | |
217 | ||
ef13e9a4 | 218 | static void dolock(keyopts *k, key_data **kd, const char *t) |
252c122d | 219 | { |
220 | if (!(k->f & f_lock)) | |
221 | return; | |
222 | if (t) | |
223 | dstr_putf(&k->tag, ".%s", t); | |
ef13e9a4 | 224 | if (key_plock(kd, 0, k->tag.buf)) |
252c122d | 225 | die(EXIT_FAILURE, "couldn't lock key"); |
226 | } | |
227 | ||
252c122d | 228 | /* --- @copyparam@ --- * |
229 | * | |
230 | * Arguments: @keyopts *k@ = pointer to key options | |
231 | * @const char **pp@ = checklist of parameters | |
232 | * | |
233 | * Returns: Nonzero if parameters copied; zero if you have to generate | |
234 | * them. | |
235 | * | |
236 | * Use: Copies parameters from a source key to the current one. | |
237 | */ | |
238 | ||
239 | static int copyparam(keyopts *k, const char **pp) | |
240 | { | |
241 | key_filter kf; | |
4739c68a | 242 | key_attriter i; |
9eef0c14 | 243 | key_data *kd; |
4739c68a | 244 | const char *n, *v; |
f49fc4a0 | 245 | dstr t = DSTR_INIT; |
252c122d | 246 | |
9eef0c14 MW |
247 | kf.f = KCAT_SHARE; |
248 | kf.m = KF_CATMASK; | |
249 | ||
252c122d | 250 | /* --- Quick check if no parameters supplied --- */ |
251 | ||
252 | if (!k->p) | |
253 | return (0); | |
254 | ||
255 | /* --- Run through the checklist --- */ | |
256 | ||
f49fc4a0 | 257 | key_fulltag(k->p, &t); |
252c122d | 258 | while (*pp) { |
ef13e9a4 | 259 | key_data *kd = key_structfind(k->p->k, *pp); |
f49fc4a0 MW |
260 | if (!kd) { |
261 | die(EXIT_FAILURE, | |
262 | "bad parameter key `%s': parameter `%s' not found", t.buf, *pp); | |
263 | } | |
264 | if (!KEY_MATCH(kd, &kf)) { | |
265 | die(EXIT_FAILURE, | |
266 | "bad parameter key `%s': subkey `%s' is not shared", t.buf, *pp); | |
267 | } | |
252c122d | 268 | pp++; |
269 | } | |
270 | ||
271 | /* --- Copy over the parameters --- */ | |
272 | ||
9eef0c14 MW |
273 | kd = key_copydata(k->p->k, &kf); |
274 | assert(kd); | |
275 | key_setkeydata(k->kf, k->k, kd); | |
276 | key_drop(kd); | |
4739c68a | 277 | |
278 | /* --- Copy over attributes --- */ | |
279 | ||
280 | for (key_mkattriter(&i, k->p); key_nextattr(&i, &n, &v); ) | |
281 | key_putattr(k->kf, k->k, n, v); | |
282 | ||
283 | /* --- Done --- */ | |
284 | ||
f49fc4a0 | 285 | dstr_destroy(&t); |
252c122d | 286 | return (1); |
287 | } | |
288 | ||
289 | /* --- @getmp@ --- * | |
290 | * | |
291 | * Arguments: @key_data *k@ = pointer to key data block | |
292 | * @const char *tag@ = tag string to use | |
293 | * | |
294 | * Returns: Pointer to multiprecision integer key item. | |
295 | * | |
296 | * Use: Fetches an MP key component. | |
297 | */ | |
298 | ||
299 | static mp *getmp(key_data *k, const char *tag) | |
300 | { | |
301 | k = key_structfind(k, tag); | |
302 | if (!k) | |
303 | die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag); | |
304 | if ((k->e & KF_ENCMASK) != KENC_MP) | |
9b1663a5 | 305 | die(EXIT_FAILURE, "subkey `%s' has an incompatible type", tag); |
252c122d | 306 | return (k->u.m); |
307 | } | |
308 | ||
052b36d0 | 309 | /* --- @keyrand@ --- * |
310 | * | |
311 | * Arguments: @key_file *kf@ = pointer to key file | |
312 | * @const char *id@ = pointer to key id (or null) | |
313 | * | |
314 | * Returns: --- | |
315 | * | |
316 | * Use: Keys the random number generator. | |
317 | */ | |
318 | ||
319 | static void keyrand(key_file *kf, const char *id) | |
320 | { | |
321 | key *k; | |
322 | ||
323 | /* --- Find the key --- */ | |
324 | ||
325 | if (id) { | |
326 | if ((k = key_bytag(kf, id)) == 0) | |
327 | die(EXIT_FAILURE, "key `%s' not found", id); | |
328 | } else | |
329 | k = key_bytype(kf, "catacomb-rand"); | |
330 | ||
331 | if (k) { | |
ef13e9a4 | 332 | key_data *kd = k->k, *kkd; |
333 | key_incref(kd); | |
052b36d0 | 334 | |
335 | again: | |
336 | switch (kd->e & KF_ENCMASK) { | |
337 | case KENC_BINARY: | |
338 | break; | |
339 | case KENC_ENCRYPT: { | |
340 | dstr d = DSTR_INIT; | |
341 | key_fulltag(k, &d); | |
ef13e9a4 | 342 | if (key_punlock(&kkd, kd, d.buf)) |
052b36d0 | 343 | die(EXIT_FAILURE, "error unlocking key `%s'", d.buf); |
344 | dstr_destroy(&d); | |
ef13e9a4 | 345 | key_drop(kd); |
346 | kd = kkd; | |
052b36d0 | 347 | } goto again; |
348 | default: { | |
349 | dstr d = DSTR_INIT; | |
350 | key_fulltag(k, &d); | |
351 | die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf); | |
352 | } break; | |
353 | } | |
354 | ||
355 | /* --- Key the generator --- */ | |
356 | ||
357 | rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz); | |
ef13e9a4 | 358 | key_drop(kd); |
052b36d0 | 359 | } |
360 | } | |
361 | ||
252c122d | 362 | /* --- Key generation algorithms --- */ |
363 | ||
364 | static void alg_binary(keyopts *k) | |
365 | { | |
366 | unsigned sz; | |
367 | unsigned m; | |
ef13e9a4 | 368 | key_data *kd; |
252c122d | 369 | octet *p; |
370 | ||
371 | if (!k->bits) | |
372 | k->bits = 128; | |
373 | if (k->p) | |
374 | die(EXIT_FAILURE, "no shared parameters for binary keys"); | |
375 | ||
376 | sz = (k->bits + 7) >> 3; | |
377 | p = sub_alloc(sz); | |
378 | m = (1 << (((k->bits - 1) & 7) + 1)) - 1; | |
34e4f738 | 379 | k->r->ops->fill(k->r, p, sz); |
252c122d | 380 | *p &= m; |
ef13e9a4 | 381 | kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz); |
252c122d | 382 | memset(p, 0, sz); |
ef13e9a4 | 383 | dolock(k, &kd, 0); |
384 | key_setkeydata(k->kf, k->k, kd); | |
385 | key_drop(kd); | |
252c122d | 386 | sub_free(p, sz); |
252c122d | 387 | } |
388 | ||
389 | static void alg_des(keyopts *k) | |
390 | { | |
391 | unsigned sz; | |
392 | octet *p; | |
ef13e9a4 | 393 | key_data *kd; |
252c122d | 394 | int i; |
395 | ||
396 | if (!k->bits) | |
1ba83484 | 397 | k->bits = 168; |
252c122d | 398 | if (k->p) |
399 | die(EXIT_FAILURE, "no shared parameters for DES keys"); | |
400 | if (k->bits % 56 || k->bits > 168) | |
401 | die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long"); | |
402 | ||
403 | sz = k->bits / 7; | |
404 | p = sub_alloc(sz); | |
34e4f738 | 405 | k->r->ops->fill(k->r, p, sz); |
252c122d | 406 | for (i = 0; i < sz; i++) { |
052b36d0 | 407 | octet x = p[i] | 0x01; |
252c122d | 408 | x = x ^ (x >> 4); |
409 | x = x ^ (x >> 2); | |
052b36d0 | 410 | x = x ^ (x >> 1); |
252c122d | 411 | p[i] = (p[i] & 0xfe) | (x & 0x01); |
412 | } | |
ef13e9a4 | 413 | kd = key_newbinary(KCAT_SYMM | KF_BURN, p, sz); |
252c122d | 414 | memset(p, 0, sz); |
ef13e9a4 | 415 | dolock(k, &kd, 0); |
416 | key_setkeydata(k->kf, k->k, kd); | |
417 | key_drop(kd); | |
252c122d | 418 | sub_free(p, sz); |
252c122d | 419 | } |
420 | ||
421 | static void alg_rsa(keyopts *k) | |
422 | { | |
ab0ca95f | 423 | rsa_priv rp; |
ef13e9a4 | 424 | key_data *kd, *kkd; |
252c122d | 425 | |
426 | /* --- Sanity checking --- */ | |
427 | ||
428 | if (k->p) | |
429 | die(EXIT_FAILURE, "no shared parameters for RSA keys"); | |
430 | if (!k->bits) | |
431 | k->bits = 1024; | |
432 | ||
433 | /* --- Generate the RSA parameters --- */ | |
434 | ||
34e4f738 | 435 | if (rsa_gen(&rp, k->bits, k->r, 0, |
252c122d | 436 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
437 | die(EXIT_FAILURE, "RSA key generation failed"); | |
438 | ||
439 | /* --- Run a test encryption --- */ | |
440 | ||
441 | { | |
34e4f738 | 442 | grand *g = fibrand_create(k->r->ops->word(k->r)); |
ab0ca95f | 443 | rsa_pub rpp; |
252c122d | 444 | mp *m = mprand_range(MP_NEW, rp.n, g, 0); |
445 | mp *c; | |
446 | ||
ab0ca95f | 447 | rpp.n = rp.n; |
448 | rpp.e = rp.e; | |
449 | c = rsa_qpubop(&rpp, MP_NEW, m); | |
450 | c = rsa_qprivop(&rp, c, c, g); | |
252c122d | 451 | |
4b536f42 | 452 | if (!MP_EQ(c, m)) |
252c122d | 453 | die(EXIT_FAILURE, "test encryption failed"); |
454 | mp_drop(c); | |
455 | mp_drop(m); | |
456 | g->ops->destroy(g); | |
457 | } | |
458 | ||
459 | /* --- Allrighty then --- */ | |
460 | ||
ef13e9a4 | 461 | kd = key_newstruct(); |
462 | key_structsteal(kd, "n", key_newmp(KCAT_PUB, rp.n)); | |
463 | key_structsteal(kd, "e", key_newmp(KCAT_PUB, rp.e)); | |
464 | ||
465 | kkd = key_newstruct(); | |
466 | key_structsteal(kkd, "d", key_newmp(KCAT_PRIV | KF_BURN, rp.d)); | |
467 | key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, rp.p)); | |
468 | key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, rp.q)); | |
469 | key_structsteal(kkd, "q-inv", key_newmp(KCAT_PRIV | KF_BURN, rp.q_inv)); | |
470 | key_structsteal(kkd, "d-mod-p", key_newmp(KCAT_PRIV | KF_BURN, rp.dp)); | |
471 | key_structsteal(kkd, "d-mod-q", key_newmp(KCAT_PRIV | KF_BURN, rp.dq)); | |
472 | dolock(k, &kkd, "private"); | |
473 | key_structsteal(kd, "private", kkd); | |
474 | key_setkeydata(k->kf, k->k, kd); | |
475 | key_drop(kd); | |
ab0ca95f | 476 | rsa_privfree(&rp); |
252c122d | 477 | } |
478 | ||
479 | static void alg_dsaparam(keyopts *k) | |
480 | { | |
481 | static const char *pl[] = { "q", "p", "g", 0 }; | |
482 | if (!copyparam(k, pl)) { | |
483 | dsa_param dp; | |
484 | octet *p; | |
485 | size_t sz; | |
486 | dstr d = DSTR_INIT; | |
2b4be81b | 487 | codec *c; |
ef13e9a4 | 488 | key_data *kd; |
f4f8e305 | 489 | dsa_seed ds; |
252c122d | 490 | |
491 | /* --- Choose appropriate bit lengths if necessary --- */ | |
492 | ||
493 | if (!k->qbits) | |
494 | k->qbits = 160; | |
495 | if (!k->bits) | |
ef13e9a4 | 496 | k->bits = 1024; |
252c122d | 497 | |
498 | /* --- Allocate a seed block --- */ | |
499 | ||
500 | sz = (k->qbits + 7) >> 3; | |
501 | p = sub_alloc(sz); | |
34e4f738 | 502 | k->r->ops->fill(k->r, p, sz); |
252c122d | 503 | |
504 | /* --- Allocate the parameters --- */ | |
505 | ||
f4f8e305 | 506 | if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, &ds, |
40d5a112 | 507 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
252c122d | 508 | die(EXIT_FAILURE, "DSA parameter generation failed"); |
509 | ||
510 | /* --- Store the parameters --- */ | |
511 | ||
ef13e9a4 | 512 | kd = key_newstruct(); |
513 | key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q)); | |
514 | key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p)); | |
515 | key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g)); | |
252c122d | 516 | mp_drop(dp.q); |
517 | mp_drop(dp.p); | |
518 | mp_drop(dp.g); | |
ef13e9a4 | 519 | key_setkeydata(k->kf, k->k, kd); |
520 | key_drop(kd); | |
252c122d | 521 | |
522 | /* --- Store the seed for future verification --- */ | |
523 | ||
2b4be81b MW |
524 | c = base64_class.encoder(0, "", 0); |
525 | c->ops->code(c, ds.p, ds.sz, &d); c->ops->code(c, 0, 0, &d); | |
526 | c->ops->destroy(c); | |
ef13e9a4 | 527 | DPUTZ(&d); |
252c122d | 528 | key_putattr(k->kf, k->k, "seed", d.buf); |
f4f8e305 | 529 | DRESET(&d); |
530 | dstr_putf(&d, "%u", ds.count); | |
531 | key_putattr(k->kf, k->k, "count", d.buf); | |
532 | xfree(ds.p); | |
252c122d | 533 | sub_free(p, sz); |
534 | dstr_destroy(&d); | |
535 | } | |
536 | } | |
537 | ||
538 | static void alg_dsa(keyopts *k) | |
539 | { | |
540 | mp *q, *p, *g; | |
541 | mp *x, *y; | |
542 | mpmont mm; | |
ef13e9a4 | 543 | key_data *kd, *kkd; |
252c122d | 544 | |
545 | /* --- Get the shared parameters --- */ | |
546 | ||
547 | alg_dsaparam(k); | |
ef13e9a4 | 548 | key_split(&k->k->k); kd = k->k->k; |
252c122d | 549 | q = getmp(kd, "q"); |
550 | p = getmp(kd, "p"); | |
551 | g = getmp(kd, "g"); | |
552 | ||
553 | /* --- Choose a private key --- */ | |
554 | ||
34e4f738 | 555 | x = mprand_range(MP_NEWSEC, q, k->r, 0); |
252c122d | 556 | mpmont_create(&mm, p); |
557 | y = mpmont_exp(&mm, MP_NEW, g, x); | |
558 | ||
559 | /* --- Store everything away --- */ | |
560 | ||
ef13e9a4 | 561 | key_structsteal(kd, "y", key_newmp(KCAT_PUB, y)); |
252c122d | 562 | |
ef13e9a4 | 563 | kkd = key_newstruct(); |
564 | key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x)); | |
565 | dolock(k, &kkd, "private"); | |
566 | key_structsteal(kd, "private", kkd); | |
052b36d0 | 567 | |
568 | mp_drop(x); mp_drop(y); | |
252c122d | 569 | } |
570 | ||
571 | static void alg_dhparam(keyopts *k) | |
572 | { | |
052b36d0 | 573 | static const char *pl[] = { "p", "q", "g", 0 }; |
ef13e9a4 | 574 | key_data *kd; |
252c122d | 575 | if (!copyparam(k, pl)) { |
052b36d0 | 576 | dh_param dp; |
40d5a112 | 577 | int rc; |
252c122d | 578 | |
3563e365 | 579 | if (k->curve) { |
580 | qd_parse qd; | |
3688eb75 | 581 | group *g; |
582 | const char *e; | |
45c0fd36 | 583 | |
3563e365 | 584 | if (strcmp(k->curve, "list") == 0) { |
c65df279 | 585 | unsigned i, w; |
3688eb75 | 586 | LIST("Built-in prime fields", stdout, ptab[i].name, ptab[i].name); |
3563e365 | 587 | exit(0); |
588 | } | |
589 | qd.p = k->curve; | |
590 | if (dh_parse(&qd, &dp)) | |
3688eb75 | 591 | die(EXIT_FAILURE, "error in field spec: %s", qd.e); |
592 | if (!qd_eofp(&qd)) | |
593 | die(EXIT_FAILURE, "junk at end of field spec"); | |
594 | if ((g = group_prime(&dp)) == 0) | |
595 | die(EXIT_FAILURE, "invalid prime field"); | |
596 | if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0) | |
45c0fd36 | 597 | moan("WARNING! group check failed: %s", e); |
3688eb75 | 598 | G_DESTROYGROUP(g); |
3563e365 | 599 | goto done; |
600 | } | |
45c0fd36 | 601 | |
252c122d | 602 | if (!k->bits) |
603 | k->bits = 1024; | |
604 | ||
605 | /* --- Choose a large safe prime number --- */ | |
606 | ||
40d5a112 | 607 | if (k->f & f_limlee) { |
608 | mp **f; | |
609 | size_t nf; | |
610 | if (!k->qbits) | |
611 | k->qbits = 256; | |
612 | rc = dh_limlee(&dp, k->qbits, k->bits, | |
613 | (k->f & f_subgroup) ? DH_SUBGROUP : 0, | |
34e4f738 | 614 | 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0, |
40d5a112 | 615 | (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f); |
616 | if (!rc) { | |
617 | dstr d = DSTR_INIT; | |
618 | size_t i; | |
619 | for (i = 0; i < nf; i++) { | |
620 | if (i) | |
621 | dstr_puts(&d, ", "); | |
622 | mp_writedstr(f[i], &d, 10); | |
623 | mp_drop(f[i]); | |
624 | } | |
625 | key_putattr(k->kf, k->k, "factors", d.buf); | |
626 | dstr_destroy(&d); | |
627 | } | |
4e67e30b MW |
628 | } else if (k->f & f_kcdsa) { |
629 | if (!k->qbits) | |
630 | k->qbits = 256; | |
631 | rc = dh_kcdsagen(&dp, k->qbits, k->bits, 0, | |
632 | 0, k->r, (k->f & f_quiet) ? 0 : pgen_ev, 0); | |
633 | if (!rc) { | |
634 | dstr d = DSTR_INIT; | |
635 | mp *v = MP_NEW; | |
636 | ||
637 | mp_writedstr(dp.q, &d, 10); | |
638 | mp_div(&v, 0, dp.p, dp.q); | |
639 | v = mp_lsr(v, v, 1); | |
640 | dstr_puts(&d, ", "); | |
641 | mp_writedstr(v, &d, 10); | |
642 | mp_drop(v); | |
643 | key_putattr(k->kf, k->k, "factors", d.buf); | |
644 | dstr_destroy(&d); | |
645 | } | |
40d5a112 | 646 | } else |
34e4f738 | 647 | rc = dh_gen(&dp, k->qbits, k->bits, 0, k->r, |
40d5a112 | 648 | (k->f & f_quiet) ? 0 : pgen_ev, 0); |
649 | ||
650 | if (rc) | |
252c122d | 651 | die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed"); |
652 | ||
3563e365 | 653 | done: |
ef13e9a4 | 654 | kd = key_newstruct(); |
655 | key_structsteal(kd, "p", key_newmp(KCAT_SHARE, dp.p)); | |
656 | key_structsteal(kd, "q", key_newmp(KCAT_SHARE, dp.q)); | |
657 | key_structsteal(kd, "g", key_newmp(KCAT_SHARE, dp.g)); | |
052b36d0 | 658 | mp_drop(dp.q); |
659 | mp_drop(dp.p); | |
660 | mp_drop(dp.g); | |
ef13e9a4 | 661 | key_setkeydata(k->kf, k->k, kd); |
662 | key_drop(kd); | |
34e4f738 | 663 | } |
252c122d | 664 | } |
665 | ||
666 | static void alg_dh(keyopts *k) | |
667 | { | |
668 | mp *x, *y; | |
052b36d0 | 669 | mp *p, *q, *g; |
252c122d | 670 | mpmont mm; |
ef13e9a4 | 671 | key_data *kd, *kkd; |
252c122d | 672 | |
673 | /* --- Get the shared parameters --- */ | |
674 | ||
675 | alg_dhparam(k); | |
ef13e9a4 | 676 | key_split(&k->k->k); kd = k->k->k; |
252c122d | 677 | p = getmp(kd, "p"); |
052b36d0 | 678 | q = getmp(kd, "q"); |
252c122d | 679 | g = getmp(kd, "g"); |
680 | ||
681 | /* --- Choose a suitable private key --- * | |
682 | * | |
683 | * Since %$g$% has order %$q$%, choose %$x < q$%. | |
684 | */ | |
685 | ||
34e4f738 | 686 | x = mprand_range(MP_NEWSEC, q, k->r, 0); |
252c122d | 687 | |
688 | /* --- Compute the public key %$y = g^x \bmod p$% --- */ | |
689 | ||
690 | mpmont_create(&mm, p); | |
052b36d0 | 691 | y = mpmont_exp(&mm, MP_NEW, g, x); |
252c122d | 692 | mpmont_destroy(&mm); |
693 | ||
694 | /* --- Store everything away --- */ | |
695 | ||
ef13e9a4 | 696 | key_structsteal(kd, "y", key_newmp(KCAT_PUB, y)); |
252c122d | 697 | |
ef13e9a4 | 698 | kkd = key_newstruct(); |
699 | key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x)); | |
700 | dolock(k, &kkd, "private"); | |
701 | key_structsteal(kd, "private", kkd); | |
052b36d0 | 702 | |
703 | mp_drop(x); mp_drop(y); | |
252c122d | 704 | } |
705 | ||
706 | static void alg_bbs(keyopts *k) | |
707 | { | |
ab0ca95f | 708 | bbs_priv bp; |
ef13e9a4 | 709 | key_data *kd, *kkd; |
252c122d | 710 | |
711 | /* --- Sanity checking --- */ | |
712 | ||
713 | if (k->p) | |
714 | die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys"); | |
715 | if (!k->bits) | |
716 | k->bits = 1024; | |
717 | ||
718 | /* --- Generate the BBS parameters --- */ | |
719 | ||
34e4f738 | 720 | if (bbs_gen(&bp, k->bits, k->r, 0, |
052b36d0 | 721 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
252c122d | 722 | die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed"); |
252c122d | 723 | |
724 | /* --- Allrighty then --- */ | |
725 | ||
ef13e9a4 | 726 | kd = key_newstruct(); |
727 | key_structsteal(kd, "n", key_newmp(KCAT_PUB, bp.n)); | |
252c122d | 728 | |
ef13e9a4 | 729 | kkd = key_newstruct(); |
730 | key_structsteal(kkd, "p", key_newmp(KCAT_PRIV | KF_BURN, bp.p)); | |
731 | key_structsteal(kkd, "q", key_newmp(KCAT_PRIV | KF_BURN, bp.q)); | |
732 | dolock(k, &kkd, "private"); | |
733 | key_structsteal(kd, "private", kkd); | |
734 | key_setkeydata(k->kf, k->k, kd); | |
735 | key_drop(kd); | |
252c122d | 736 | |
ab0ca95f | 737 | bbs_privfree(&bp); |
252c122d | 738 | } |
739 | ||
3688eb75 | 740 | static void alg_binparam(keyopts *k) |
741 | { | |
742 | static const char *pl[] = { "p", "q", "g", 0 }; | |
743 | if (!copyparam(k, pl)) { | |
744 | gbin_param gb; | |
745 | qd_parse qd; | |
746 | group *g; | |
747 | const char *e; | |
ef13e9a4 | 748 | key_data *kd; |
3688eb75 | 749 | |
750 | /* --- Decide on a field --- */ | |
751 | ||
752 | if (!k->bits) k->bits = 128; | |
753 | if (k->curve && strcmp(k->curve, "list") == 0) { | |
754 | unsigned i, w; | |
755 | LIST("Built-in binary fields", stdout, | |
756 | bintab[i].name, bintab[i].name); | |
757 | exit(0); | |
758 | } | |
759 | if (!k->curve) { | |
760 | if (k->bits <= 40) k->curve = "p1363-40"; | |
761 | else if (k->bits <= 56) k->curve = "p1363-56"; | |
762 | else if (k->bits <= 64) k->curve = "p1363-64"; | |
763 | else if (k->bits <= 80) k->curve = "p1363-80"; | |
764 | else if (k->bits <= 112) k->curve = "p1363-112"; | |
765 | else if (k->bits <= 128) k->curve = "p1363-128"; | |
766 | else { | |
767 | die(EXIT_FAILURE, | |
768 | "no built-in binary fields provide %u-bit security", | |
769 | k->bits); | |
770 | } | |
771 | } | |
772 | ||
773 | /* --- Check it --- */ | |
774 | ||
775 | qd.e = 0; | |
776 | qd.p = k->curve; | |
777 | if (dhbin_parse(&qd, &gb)) | |
778 | die(EXIT_FAILURE, "error in field spec: %s", qd.e); | |
779 | if (!qd_eofp(&qd)) | |
780 | die(EXIT_FAILURE, "junk at end of field spec"); | |
781 | if ((g = group_binary(&gb)) == 0) | |
782 | die(EXIT_FAILURE, "invalid binary field"); | |
783 | if (!(k->f & f_quiet) && (e = G_CHECK(g, &rand_global)) != 0) | |
784 | moan("WARNING! group check failed: %s", e); | |
785 | G_DESTROYGROUP(g); | |
786 | ||
787 | /* --- Write out the answer --- */ | |
788 | ||
ef13e9a4 | 789 | kd = key_newstruct(); |
790 | key_structsteal(kd, "p", key_newmp(KCAT_SHARE, gb.p)); | |
791 | key_structsteal(kd, "q", key_newmp(KCAT_SHARE, gb.q)); | |
792 | key_structsteal(kd, "g", key_newmp(KCAT_SHARE, gb.g)); | |
3688eb75 | 793 | mp_drop(gb.q); |
794 | mp_drop(gb.p); | |
795 | mp_drop(gb.g); | |
ef13e9a4 | 796 | key_setkeydata(k->kf, k->k, kd); |
797 | key_drop(kd); | |
3688eb75 | 798 | } |
799 | } | |
800 | ||
801 | static void alg_bin(keyopts *k) | |
802 | { | |
803 | mp *x, *y; | |
804 | mp *p, *q, *g; | |
805 | gfreduce r; | |
ef13e9a4 | 806 | key_data *kd, *kkd; |
3688eb75 | 807 | |
808 | /* --- Get the shared parameters --- */ | |
809 | ||
810 | alg_binparam(k); | |
ef13e9a4 | 811 | key_split(&k->k->k); kd = k->k->k; |
3688eb75 | 812 | p = getmp(kd, "p"); |
813 | q = getmp(kd, "q"); | |
814 | g = getmp(kd, "g"); | |
815 | ||
816 | /* --- Choose a suitable private key --- * | |
817 | * | |
818 | * Since %$g$% has order %$q$%, choose %$x < q$%. | |
819 | */ | |
820 | ||
821 | x = mprand_range(MP_NEWSEC, q, k->r, 0); | |
822 | ||
823 | /* --- Compute the public key %$y = g^x \bmod p$% --- */ | |
824 | ||
825 | gfreduce_create(&r, p); | |
826 | y = gfreduce_exp(&r, MP_NEW, g, x); | |
827 | gfreduce_destroy(&r); | |
828 | ||
829 | /* --- Store everything away --- */ | |
830 | ||
ef13e9a4 | 831 | key_structsteal(kd, "y", key_newmp(KCAT_PUB, y)); |
3688eb75 | 832 | |
ef13e9a4 | 833 | kkd = key_newstruct(); |
834 | key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x)); | |
835 | dolock(k, &kkd, "private"); | |
836 | key_structsteal(kd, "private", kkd); | |
3688eb75 | 837 | |
838 | mp_drop(x); mp_drop(y); | |
839 | } | |
840 | ||
1ba83484 | 841 | static void alg_ecparam(keyopts *k) |
842 | { | |
843 | static const char *pl[] = { "curve", 0 }; | |
844 | if (!copyparam(k, pl)) { | |
845 | ec_info ei; | |
846 | const char *e; | |
ef13e9a4 | 847 | key_data *kd; |
1ba83484 | 848 | |
849 | /* --- Decide on a curve --- */ | |
850 | ||
851 | if (!k->bits) k->bits = 256; | |
3563e365 | 852 | if (k->curve && strcmp(k->curve, "list") == 0) { |
c65df279 | 853 | unsigned i, w; |
854 | LIST("Built-in elliptic curves", stdout, | |
855 | ectab[i].name, ectab[i].name); | |
3563e365 | 856 | exit(0); |
857 | } | |
1ba83484 | 858 | if (!k->curve) { |
859 | if (k->bits <= 56) k->curve = "secp112r1"; | |
860 | else if (k->bits <= 64) k->curve = "secp128r1"; | |
861 | else if (k->bits <= 80) k->curve = "secp160r1"; | |
862 | else if (k->bits <= 96) k->curve = "secp192r1"; | |
863 | else if (k->bits <= 112) k->curve = "secp224r1"; | |
864 | else if (k->bits <= 128) k->curve = "secp256r1"; | |
865 | else if (k->bits <= 192) k->curve = "secp384r1"; | |
866 | else if (k->bits <= 256) k->curve = "secp521r1"; | |
867 | else | |
868 | die(EXIT_FAILURE, "no built-in curves provide %u-bit security", | |
869 | k->bits); | |
870 | } | |
871 | ||
872 | /* --- Check it --- */ | |
873 | ||
874 | if ((e = ec_getinfo(&ei, k->curve)) != 0) | |
875 | die(EXIT_FAILURE, "error in curve spec: %s", e); | |
34e4f738 | 876 | if (!(k->f & f_quiet) && (e = ec_checkinfo(&ei, k->r)) != 0) |
1ba83484 | 877 | moan("WARNING! curve check failed: %s", e); |
878 | ec_freeinfo(&ei); | |
879 | ||
880 | /* --- Write out the answer --- */ | |
881 | ||
ef13e9a4 | 882 | kd = key_newstruct(); |
883 | key_structsteal(kd, "curve", key_newstring(KCAT_SHARE, k->curve)); | |
884 | key_setkeydata(k->kf, k->k, kd); | |
885 | key_drop(kd); | |
1ba83484 | 886 | } |
887 | } | |
888 | ||
889 | static void alg_ec(keyopts *k) | |
890 | { | |
ef13e9a4 | 891 | key_data *kd; |
1ba83484 | 892 | key_data *kkd; |
893 | mp *x = MP_NEW; | |
894 | ec p = EC_INIT; | |
895 | const char *e; | |
896 | ec_info ei; | |
897 | ||
898 | /* --- Get the curve --- */ | |
899 | ||
900 | alg_ecparam(k); | |
ef13e9a4 | 901 | key_split(&k->k->k); kd = k->k->k; |
1ba83484 | 902 | if ((kkd = key_structfind(kd, "curve")) == 0) |
903 | die(EXIT_FAILURE, "unexpected failure looking up subkey `curve')"); | |
904 | if ((kkd->e & KF_ENCMASK) != KENC_STRING) | |
905 | die(EXIT_FAILURE, "subkey `curve' is not a string"); | |
906 | if ((e = ec_getinfo(&ei, kkd->u.p)) != 0) | |
907 | die(EXIT_FAILURE, "error in curve spec: %s", e); | |
908 | ||
909 | /* --- Invent a private exponent and compute the public key --- */ | |
910 | ||
34e4f738 | 911 | x = mprand_range(MP_NEWSEC, ei.r, k->r, 0); |
1ba83484 | 912 | ec_mul(ei.c, &p, &ei.g, x); |
913 | ||
914 | /* --- Store everything away --- */ | |
915 | ||
ef13e9a4 | 916 | key_structsteal(kd, "p", key_newec(KCAT_PUB, &p)); |
917 | ||
918 | kkd = key_newstruct(); | |
919 | key_structsteal(kkd, "x", key_newmp(KCAT_PRIV | KF_BURN, x)); | |
920 | dolock(k, &kkd, "private"); | |
921 | key_structsteal(kd, "private", kkd); | |
1ba83484 | 922 | |
923 | /* --- Done --- */ | |
924 | ||
925 | ec_freeinfo(&ei); | |
926 | mp_drop(x); | |
927 | } | |
928 | ||
252c122d | 929 | /* --- The algorithm tables --- */ |
930 | ||
931 | typedef struct keyalg { | |
932 | const char *name; | |
933 | void (*proc)(keyopts *o); | |
934 | const char *help; | |
935 | } keyalg; | |
936 | ||
937 | static keyalg algtab[] = { | |
938 | { "binary", alg_binary, "Plain binary data" }, | |
939 | { "des", alg_des, "Binary with DES-style parity" }, | |
940 | { "rsa", alg_rsa, "RSA public-key encryption" }, | |
3688eb75 | 941 | { "bbs", alg_bbs, "Blum-Blum-Shub generator" }, |
252c122d | 942 | { "dsa", alg_dsa, "DSA digital signatures" }, |
943 | { "dsa-param", alg_dsaparam, "DSA shared parameters" }, | |
944 | { "dh", alg_dh, "Diffie-Hellman key exchange" }, | |
945 | { "dh-param", alg_dhparam, "Diffie-Hellman parameters" }, | |
3688eb75 | 946 | { "bindh", alg_bin, "DH over a binary field" }, |
947 | { "bindh-param", alg_binparam, "Binary-field DH parameters" }, | |
1ba83484 | 948 | { "ec-param", alg_ecparam, "Elliptic curve parameters" }, |
949 | { "ec", alg_ec, "Elliptic curve crypto" }, | |
252c122d | 950 | { 0, 0 } |
951 | }; | |
d03ab969 | 952 | |
953 | /* --- @cmd_add@ --- */ | |
954 | ||
955 | static int cmd_add(int argc, char *argv[]) | |
956 | { | |
957 | key_file f; | |
d03ab969 | 958 | time_t exp = KEXP_EXPIRE; |
90a88ae3 | 959 | uint32 kid = rand_global.ops->word(&rand_global); |
252c122d | 960 | const char *tag = 0, *ptag = 0; |
d03ab969 | 961 | const char *c = 0; |
252c122d | 962 | keyalg *alg = algtab; |
052b36d0 | 963 | const char *rtag = 0; |
34e4f738 | 964 | const struct seedalg *sa = SEEDALG_DEFAULT; |
965 | keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0, 0 }; | |
966 | const char *seed = 0; | |
967 | k.r = &rand_global; | |
d03ab969 | 968 | |
969 | /* --- Parse options for the subcommand --- */ | |
970 | ||
971 | for (;;) { | |
972 | static struct option opt[] = { | |
252c122d | 973 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, |
d03ab969 | 974 | { "bits", OPTF_ARGREQ, 0, 'b' }, |
252c122d | 975 | { "qbits", OPTF_ARGREQ, 0, 'B' }, |
976 | { "parameters", OPTF_ARGREQ, 0, 'p' }, | |
d03ab969 | 977 | { "expire", OPTF_ARGREQ, 0, 'e' }, |
978 | { "comment", OPTF_ARGREQ, 0, 'c' }, | |
252c122d | 979 | { "tag", OPTF_ARGREQ, 0, 't' }, |
ce70ec47 | 980 | { "rand-id", OPTF_ARGREQ, 0, 'R' }, |
90a88ae3 | 981 | { "key-id", OPTF_ARGREQ, 0, 'I' }, |
1ba83484 | 982 | { "curve", OPTF_ARGREQ, 0, 'C' }, |
34e4f738 | 983 | { "seedalg", OPTF_ARGREQ, 0, 'A' }, |
984 | { "seed", OPTF_ARGREQ, 0, 's' }, | |
985 | { "newseed", OPTF_ARGREQ, 0, 'n' }, | |
252c122d | 986 | { "lock", 0, 0, 'l' }, |
987 | { "quiet", 0, 0, 'q' }, | |
40d5a112 | 988 | { "lim-lee", 0, 0, 'L' }, |
989 | { "subgroup", 0, 0, 'S' }, | |
4e67e30b | 990 | { "kcdsa", 0, 0, 'K' }, |
d03ab969 | 991 | { 0, 0, 0, 0 } |
992 | }; | |
90a88ae3 | 993 | int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:R:I:C:A:s:n:lqrLKS", |
a9fcea0e | 994 | opt, 0, 0, 0); |
d03ab969 | 995 | if (i < 0) |
996 | break; | |
997 | ||
998 | /* --- Handle the various options --- */ | |
999 | ||
1000 | switch (i) { | |
1001 | ||
252c122d | 1002 | /* --- Read an algorithm name --- */ |
1003 | ||
1004 | case 'a': { | |
1005 | keyalg *a; | |
1006 | size_t sz = strlen(optarg); | |
1007 | ||
1008 | if (strcmp(optarg, "list") == 0) { | |
1009 | for (a = algtab; a->name; a++) | |
1010 | printf("%-10s %s\n", a->name, a->help); | |
1011 | return (0); | |
1012 | } | |
1013 | ||
1014 | alg = 0; | |
1015 | for (a = algtab; a->name; a++) { | |
1016 | if (strncmp(optarg, a->name, sz) == 0) { | |
1017 | if (a->name[sz] == 0) { | |
1018 | alg = a; | |
1019 | break; | |
1020 | } else if (alg) | |
1021 | die(EXIT_FAILURE, "ambiguous algorithm name `%s'", optarg); | |
1022 | else | |
1023 | alg = a; | |
1024 | } | |
1025 | } | |
1026 | if (!alg) | |
1027 | die(EXIT_FAILURE, "unknown algorithm name `%s'", optarg); | |
1028 | } break; | |
1029 | ||
d03ab969 | 1030 | /* --- Bits must be nonzero and a multiple of 8 --- */ |
1031 | ||
252c122d | 1032 | case 'b': { |
1033 | char *p; | |
1034 | k.bits = strtoul(optarg, &p, 0); | |
1035 | if (k.bits == 0 || *p != 0) | |
1036 | die(EXIT_FAILURE, "bad bitlength `%s'", optarg); | |
1037 | } break; | |
1038 | ||
1039 | case 'B': { | |
1040 | char *p; | |
1041 | k.qbits = strtoul(optarg, &p, 0); | |
1042 | if (k.qbits == 0 || *p != 0) | |
1043 | die(EXIT_FAILURE, "bad bitlength `%s'", optarg); | |
1044 | } break; | |
1045 | ||
1046 | /* --- Parameter selection --- */ | |
1047 | ||
1048 | case 'p': | |
1049 | ptag = optarg; | |
d03ab969 | 1050 | break; |
1051 | ||
1052 | /* --- Expiry dates get passed to @get_date@ for parsing --- */ | |
1053 | ||
1054 | case 'e': | |
9cea6911 | 1055 | if (strcmp(optarg, "forever") == 0) |
d03ab969 | 1056 | exp = KEXP_FOREVER; |
1057 | else { | |
1058 | exp = get_date(optarg, 0); | |
1059 | if (exp == -1) | |
252c122d | 1060 | die(EXIT_FAILURE, "bad expiry date `%s'", optarg); |
d03ab969 | 1061 | } |
1062 | break; | |
1063 | ||
1064 | /* --- Store comments without interpretation --- */ | |
1065 | ||
1066 | case 'c': | |
252c122d | 1067 | if (key_chkcomment(optarg)) |
1068 | die(EXIT_FAILURE, "bad comment string `%s'", optarg); | |
d03ab969 | 1069 | c = optarg; |
1070 | break; | |
1071 | ||
1ba83484 | 1072 | /* --- Elliptic curve parameters --- */ |
1073 | ||
1074 | case 'C': | |
1ba83484 | 1075 | k.curve = optarg; |
1076 | break; | |
1077 | ||
252c122d | 1078 | /* --- Store tags --- */ |
1079 | ||
1080 | case 't': | |
1081 | if (key_chkident(optarg)) | |
1082 | die(EXIT_FAILURE, "bad tag string `%s'", optarg); | |
1083 | tag = optarg; | |
1084 | break; | |
ce70ec47 | 1085 | case 'r': |
1086 | k.f |= f_retag; | |
1087 | break; | |
252c122d | 1088 | |
34e4f738 | 1089 | /* --- Seeding --- */ |
1090 | ||
1091 | case 'A': { | |
1092 | const struct seedalg *ss; | |
1093 | if (strcmp(optarg, "list") == 0) { | |
1094 | printf("Seed algorithms:\n"); | |
1095 | for (ss = seedtab; ss->p; ss++) | |
1096 | printf(" %s\n", ss->p); | |
1097 | exit(0); | |
1098 | } | |
1099 | if (seed) die(EXIT_FAILURE, "seed already set -- put -A first"); | |
1100 | sa = 0; | |
1101 | for (ss = seedtab; ss->p; ss++) { | |
1102 | if (strcmp(optarg, ss->p) == 0) | |
1103 | sa = ss; | |
1104 | } | |
1105 | if (!sa) | |
1106 | die(EXIT_FAILURE, "seed algorithm `%s' not known", optarg); | |
1107 | } break; | |
1108 | ||
1109 | case 's': { | |
2b4be81b MW |
1110 | codec *c; |
1111 | int rc; | |
34e4f738 | 1112 | dstr d = DSTR_INIT; |
45c0fd36 | 1113 | if (seed) die(EXIT_FAILURE, "seed already set"); |
2b4be81b MW |
1114 | c = base64_class.decoder(CDCF_IGNEQPAD); |
1115 | if ((rc = c->ops->code(c, optarg, strlen(optarg), &d)) != 0 || | |
1116 | (rc = c->ops->code(c, 0, 0, &d)) != 0) | |
1117 | die(EXIT_FAILURE, "invalid seed base64: %s", codec_strerror(rc)); | |
1118 | c->ops->destroy(c); | |
34e4f738 | 1119 | k.r = sa->gen(d.buf, d.len); |
1120 | seed = optarg; | |
1121 | dstr_destroy(&d); | |
1122 | } break; | |
45c0fd36 | 1123 | |
34e4f738 | 1124 | case 'n': { |
2b4be81b | 1125 | codec *c; |
34e4f738 | 1126 | dstr d = DSTR_INIT; |
1127 | char *p; | |
1128 | unsigned n = strtoul(optarg, &p, 0); | |
1129 | if (n == 0 || *p != 0 || n % 8 != 0) | |
1130 | die(EXIT_FAILURE, "bad seed length `%s'", optarg); | |
45c0fd36 | 1131 | if (seed) die(EXIT_FAILURE, "seed already set"); |
34e4f738 | 1132 | n /= 8; |
1133 | p = xmalloc(n); | |
1134 | rand_get(RAND_GLOBAL, p, n); | |
2b4be81b MW |
1135 | c = base64_class.encoder(0, "", 0); |
1136 | c->ops->code(c, p, n, &d); c->ops->code(c, 0, 0, &d); | |
1137 | c->ops->destroy(c); | |
34e4f738 | 1138 | seed = d.buf; |
1139 | k.r = sa->gen(p, n); | |
1140 | } break; | |
90a88ae3 MW |
1141 | |
1142 | /* --- Key id --- */ | |
1143 | ||
1144 | case 'I': { | |
1145 | char *p; | |
1146 | unsigned long id; | |
1147 | ||
1148 | errno = 0; | |
1149 | id = strtoul(optarg, &p, 16); | |
1150 | if (errno || *p || id > MASK32) | |
1151 | die(EXIT_FAILURE, "bad key-id `%s'", optarg); | |
1152 | kid = id; | |
1153 | } break; | |
45c0fd36 | 1154 | |
252c122d | 1155 | /* --- Other flags --- */ |
1156 | ||
ce70ec47 | 1157 | case 'R': |
052b36d0 | 1158 | rtag = optarg; |
1159 | break; | |
252c122d | 1160 | case 'l': |
1161 | k.f |= f_lock; | |
1162 | break; | |
1163 | case 'q': | |
1164 | k.f |= f_quiet; | |
1165 | break; | |
40d5a112 | 1166 | case 'L': |
1167 | k.f |= f_limlee; | |
1168 | break; | |
4e67e30b MW |
1169 | case 'K': |
1170 | k.f |= f_kcdsa; | |
1171 | break; | |
40d5a112 | 1172 | case 'S': |
1173 | k.f |= f_subgroup; | |
1174 | break; | |
252c122d | 1175 | |
d03ab969 | 1176 | /* --- Other things are bogus --- */ |
1177 | ||
1178 | default: | |
252c122d | 1179 | k.f |= f_bogus; |
d03ab969 | 1180 | break; |
1181 | } | |
1182 | } | |
1183 | ||
252c122d | 1184 | /* --- Various sorts of bogosity --- */ |
d03ab969 | 1185 | |
252c122d | 1186 | if ((k.f & f_bogus) || optind + 1 > argc) { |
d03ab969 | 1187 | die(EXIT_FAILURE, |
c65df279 | 1188 | "Usage: add [OPTIONS] TYPE [ATTR...]"); |
d03ab969 | 1189 | } |
252c122d | 1190 | if (key_chkident(argv[optind])) |
1191 | die(EXIT_FAILURE, "bad key type `%s'", argv[optind]); | |
1192 | ||
1193 | /* --- Set up various bits of the state --- */ | |
1194 | ||
d03ab969 | 1195 | if (exp == KEXP_EXPIRE) |
1196 | exp = time(0) + 14 * 24 * 60 * 60; | |
1197 | ||
252c122d | 1198 | /* --- Open the file and create the basic key block --- * |
1199 | * | |
1200 | * Keep on generating keyids until one of them doesn't collide. | |
1201 | */ | |
d03ab969 | 1202 | |
252c122d | 1203 | doopen(&f, KOPEN_WRITE); |
1204 | k.kf = &f; | |
1205 | ||
052b36d0 | 1206 | /* --- Key the generator --- */ |
1207 | ||
1208 | keyrand(&f, rtag); | |
1209 | ||
252c122d | 1210 | for (;;) { |
252c122d | 1211 | int err; |
90a88ae3 | 1212 | if ((err = key_new(&f, kid, argv[optind], exp, &k.k)) == 0) |
252c122d | 1213 | break; |
ef13e9a4 | 1214 | else if (err != KERR_DUPID) |
252c122d | 1215 | die(EXIT_FAILURE, "error adding new key: %s", key_strerror(err)); |
1216 | } | |
d03ab969 | 1217 | |
252c122d | 1218 | /* --- Set various simple attributes --- */ |
d03ab969 | 1219 | |
252c122d | 1220 | if (tag) { |
ce70ec47 | 1221 | int err; |
1222 | key *kk; | |
1223 | if (k.f & f_retag) { | |
694fc3a8 | 1224 | if ((kk = key_bytag(&f, tag)) != 0 && |
1225 | kk->tag && | |
1226 | strcmp(kk->tag, tag) == 0) | |
ce70ec47 | 1227 | key_settag(&f, kk, 0); |
1228 | } | |
1229 | if ((err = key_settag(&f, k.k, tag)) != 0) | |
252c122d | 1230 | die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err)); |
1231 | } | |
d03ab969 | 1232 | |
252c122d | 1233 | if (c) { |
1234 | int err = key_setcomment(&f, k.k, c); | |
1235 | if (err) | |
1236 | die(EXIT_FAILURE, "error setting key comment: %s", key_strerror(err)); | |
1237 | } | |
1238 | ||
1239 | setattr(&f, k.k, argv + optind + 1); | |
34e4f738 | 1240 | if (seed) { |
1241 | key_putattr(&f, k.k, "genseed", seed); | |
1242 | key_putattr(&f, k.k, "seedalg", sa->p); | |
1243 | } | |
252c122d | 1244 | |
1245 | key_fulltag(k.k, &k.tag); | |
1246 | ||
1247 | /* --- Find the parameter key --- */ | |
1248 | ||
1249 | if (ptag) { | |
1250 | if ((k.p = key_bytag(&f, ptag)) == 0) | |
1251 | die(EXIT_FAILURE, "parameter key `%s' not found", ptag); | |
ef13e9a4 | 1252 | if ((k.p->k->e & KF_ENCMASK) != KENC_STRUCT) |
252c122d | 1253 | die(EXIT_FAILURE, "parameter key `%s' is not structured", ptag); |
1254 | } | |
1255 | ||
1256 | /* --- Now generate the actual key data --- */ | |
1257 | ||
1258 | alg->proc(&k); | |
1259 | ||
1260 | /* --- Done --- */ | |
d03ab969 | 1261 | |
ef13e9a4 | 1262 | dstr_destroy(&k.tag); |
d03ab969 | 1263 | doclose(&f); |
1264 | return (0); | |
1265 | } | |
1266 | ||
252c122d | 1267 | /*----- Key listing -------------------------------------------------------*/ |
1268 | ||
1269 | /* --- Listing options --- */ | |
1270 | ||
1271 | typedef struct listopts { | |
1272 | const char *tfmt; /* Date format (@strftime@-style) */ | |
1273 | int v; /* Verbosity level */ | |
1274 | unsigned f; /* Various flags */ | |
1275 | time_t t; /* Time now (for key expiry) */ | |
1276 | key_filter kf; /* Filter for matching keys */ | |
1277 | } listopts; | |
1278 | ||
1279 | /* --- Listing flags --- */ | |
1280 | ||
16efd15b | 1281 | #define f_newline 2u /* Write newline before next entry */ |
1282 | #define f_attr 4u /* Written at least one attribute */ | |
1283 | #define f_utc 8u /* Emit UTC time, not local time */ | |
252c122d | 1284 | |
1285 | /* --- @showkeydata@ --- * | |
1286 | * | |
1287 | * Arguments: @key_data *k@ = pointer to key to write | |
1288 | * @int ind@ = indentation level | |
1289 | * @listopts *o@ = listing options | |
1290 | * @dstr *d@ = tag string for this subkey | |
1291 | * | |
1292 | * Returns: --- | |
1293 | * | |
1294 | * Use: Emits a piece of key data in a human-readable format. | |
1295 | */ | |
1296 | ||
1297 | static void showkeydata(key_data *k, int ind, listopts *o, dstr *d) | |
1298 | { | |
1299 | #define INDENT(i) do { \ | |
1300 | int _i; \ | |
1301 | for (_i = 0; _i < (i); _i++) { \ | |
1302 | putchar(' '); \ | |
1303 | } \ | |
1304 | } while (0) | |
1305 | ||
1306 | switch (k->e & KF_ENCMASK) { | |
1307 | ||
1308 | /* --- Binary key data --- * | |
1309 | * | |
1310 | * Emit as a simple hex dump. | |
1311 | */ | |
1312 | ||
1313 | case KENC_BINARY: { | |
1314 | const octet *p = k->u.k.k; | |
1315 | const octet *l = p + k->u.k.sz; | |
1316 | size_t sz = 0; | |
1317 | ||
1318 | fputs(" {", stdout); | |
1319 | while (p < l) { | |
1320 | if (sz % 16 == 0) { | |
1321 | putchar('\n'); | |
1322 | INDENT(ind + 2); | |
1323 | } else if (sz % 8 == 0) | |
1324 | fputs(" ", stdout); | |
1325 | else | |
1326 | putc(' ', stdout); | |
1327 | printf("%02x", *p++); | |
1328 | sz++; | |
1329 | } | |
1330 | putchar('\n'); | |
1331 | INDENT(ind); | |
1332 | fputs("}\n", stdout); | |
1333 | } break; | |
1334 | ||
1335 | /* --- Encrypted data --- * | |
1336 | * | |
1337 | * If the user is sufficiently keen, ask for a passphrase and decrypt the | |
1338 | * key. Otherwise just say that it's encrypted and move on. | |
1339 | */ | |
1340 | ||
1341 | case KENC_ENCRYPT: | |
1342 | if (o->v <= 3) | |
1343 | fputs(" encrypted\n", stdout); | |
1344 | else { | |
ef13e9a4 | 1345 | key_data *kd; |
1346 | if (key_punlock(&kd, k, d->buf)) | |
252c122d | 1347 | printf(" <failed to unlock %s>\n", d->buf); |
1348 | else { | |
1349 | fputs(" encrypted", stdout); | |
ef13e9a4 | 1350 | showkeydata(kd, ind, o, d); |
1351 | key_drop(kd); | |
252c122d | 1352 | } |
1353 | } | |
1354 | break; | |
1355 | ||
1356 | /* --- Integer keys --- * | |
1357 | * | |
1358 | * Emit as a large integer in decimal. This makes using the key in | |
1359 | * `calc' or whatever easier. | |
1360 | */ | |
1361 | ||
1362 | case KENC_MP: | |
1363 | putchar(' '); | |
1364 | mp_writefile(k->u.m, stdout, 10); | |
1365 | putchar('\n'); | |
1366 | break; | |
1367 | ||
1ba83484 | 1368 | /* --- Strings --- */ |
1369 | ||
1370 | case KENC_STRING: | |
1371 | printf(" `%s'\n", k->u.p); | |
1372 | break; | |
1373 | ||
1374 | /* --- Elliptic curve points --- */ | |
1375 | ||
1376 | case KENC_EC: | |
d1ee65aa | 1377 | if (EC_ATINF(&k->u.e)) |
a9fcea0e | 1378 | fputs(" inf\n", stdout); |
d1ee65aa | 1379 | else { |
1380 | fputs(" 0x", stdout); mp_writefile(k->u.e.x, stdout, 16); | |
1381 | fputs(", 0x", stdout); mp_writefile(k->u.e.y, stdout, 16); | |
1382 | putchar('\n'); | |
1383 | } | |
45c0fd36 | 1384 | break; |
1ba83484 | 1385 | |
252c122d | 1386 | /* --- Structured keys --- * |
1387 | * | |
1388 | * Just iterate over the subkeys. | |
1389 | */ | |
1390 | ||
1391 | case KENC_STRUCT: { | |
ef13e9a4 | 1392 | key_subkeyiter i; |
1393 | const char *tag; | |
252c122d | 1394 | size_t n = d->len; |
1395 | ||
1396 | fputs(" {\n", stdout); | |
ef13e9a4 | 1397 | for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) { |
1398 | if (!key_match(k, &o->kf)) | |
252c122d | 1399 | continue; |
1400 | INDENT(ind + 2); | |
ef13e9a4 | 1401 | printf("%s =", tag); |
252c122d | 1402 | d->len = n; |
ef13e9a4 | 1403 | dstr_putf(d, ".%s", tag); |
1404 | showkeydata(k, ind + 2, o, d); | |
252c122d | 1405 | } |
1406 | INDENT(ind); | |
1407 | fputs("}\n", stdout); | |
45c0fd36 | 1408 | } break; |
252c122d | 1409 | } |
1410 | ||
1411 | #undef INDENT | |
1412 | } | |
1413 | ||
1414 | /* --- @showkey@ --- * | |
1415 | * | |
1416 | * Arguments: @key *k@ = pointer to key to show | |
1417 | * @listopts *o@ = pointer to listing options | |
1418 | * | |
1419 | * Returns: --- | |
1420 | * | |
1421 | * Use: Emits a listing of a particular key. | |
1422 | */ | |
1423 | ||
1424 | static void showkey(key *k, listopts *o) | |
1425 | { | |
1426 | char ebuf[24], dbuf[24]; | |
1427 | struct tm *tm; | |
1428 | ||
1429 | /* --- Skip the key if the filter doesn't match --- */ | |
1430 | ||
ef13e9a4 | 1431 | if (!key_match(k->k, &o->kf)) |
252c122d | 1432 | return; |
1433 | ||
1434 | /* --- Sort out the expiry and deletion times --- */ | |
1435 | ||
1436 | if (KEY_EXPIRED(o->t, k->exp)) | |
1437 | strcpy(ebuf, "expired"); | |
1438 | else if (k->exp == KEXP_FOREVER) | |
1439 | strcpy(ebuf, "forever"); | |
1440 | else { | |
1441 | tm = (o->f & f_utc) ? gmtime(&k->exp) : localtime(&k->exp); | |
1442 | strftime(ebuf, sizeof(ebuf), o->tfmt, tm); | |
1443 | } | |
1444 | ||
1445 | if (KEY_EXPIRED(o->t, k->del)) | |
1446 | strcpy(dbuf, "deleted"); | |
1447 | else if (k->del == KEXP_FOREVER) | |
1448 | strcpy(dbuf, "forever"); | |
1449 | else { | |
1450 | tm = (o->f & f_utc) ? gmtime(&k->del) : localtime(&k->del); | |
1451 | strftime(dbuf, sizeof(dbuf), o->tfmt, tm); | |
1452 | } | |
1453 | ||
1454 | /* --- If in compact format, just display and quit --- */ | |
1455 | ||
1456 | if (!o->v) { | |
1457 | if (!(o->f & f_newline)) { | |
8cd9f01d | 1458 | printf("%8s %-20s %-20s %-10s %s\n", |
252c122d | 1459 | "Id", "Tag", "Type", "Expire", "Delete"); |
1460 | } | |
8cd9f01d | 1461 | printf("%08lx %-20s %-20s %-10s %s\n", |
252c122d | 1462 | (unsigned long)k->id, k->tag ? k->tag : "<none>", |
1463 | k->type, ebuf, dbuf); | |
1464 | o->f |= f_newline; | |
1465 | return; | |
1466 | } | |
1467 | ||
1468 | /* --- Display the standard header --- */ | |
1469 | ||
1470 | if (o->f & f_newline) | |
1471 | fputc('\n', stdout); | |
1472 | printf("keyid: %08lx\n", (unsigned long)k->id); | |
1473 | printf("tag: %s\n", k->tag ? k->tag : "<none>"); | |
1474 | printf("type: %s\n", k->type); | |
1475 | printf("expiry: %s\n", ebuf); | |
1476 | printf("delete: %s\n", dbuf); | |
1477 | printf("comment: %s\n", k->c ? k->c : "<none>"); | |
1478 | ||
1479 | /* --- Display the attributes --- */ | |
1480 | ||
1481 | if (o->v > 1) { | |
1482 | key_attriter i; | |
1483 | const char *av, *an; | |
1484 | ||
1485 | o->f &= ~f_attr; | |
1486 | printf("attributes:"); | |
1487 | for (key_mkattriter(&i, k); key_nextattr(&i, &an, &av); ) { | |
dce3fb0d | 1488 | printf("\n %s = %s", an, av); |
252c122d | 1489 | o->f |= f_attr; |
1490 | } | |
1491 | if (o->f & f_attr) | |
1492 | fputc('\n', stdout); | |
1493 | else | |
1494 | puts(" <none>"); | |
1495 | } | |
1496 | ||
1497 | /* --- If dumping requested, dump the raw key data --- */ | |
1498 | ||
1499 | if (o->v > 2) { | |
1500 | dstr d = DSTR_INIT; | |
1501 | fputs("key:", stdout); | |
1502 | key_fulltag(k, &d); | |
ef13e9a4 | 1503 | showkeydata(k->k, 0, o, &d); |
252c122d | 1504 | dstr_destroy(&d); |
1505 | } | |
45c0fd36 | 1506 | |
252c122d | 1507 | o->f |= f_newline; |
1508 | } | |
1509 | ||
1510 | /* --- @cmd_list@ --- */ | |
1511 | ||
1512 | static int cmd_list(int argc, char *argv[]) | |
1513 | { | |
1514 | key_file f; | |
1515 | key *k; | |
1516 | listopts o = { 0, 0, 0, 0, { 0, 0 } }; | |
1517 | ||
1518 | /* --- Parse subcommand options --- */ | |
1519 | ||
1520 | for (;;) { | |
1521 | static struct option opt[] = { | |
1522 | { "quiet", 0, 0, 'q' }, | |
1523 | { "verbose", 0, 0, 'v' }, | |
1524 | { "utc", 0, 0, 'u' }, | |
1525 | { "filter", OPTF_ARGREQ, 0, 'f' }, | |
1526 | { 0, 0, 0, 0 } | |
1527 | }; | |
1528 | int i = mdwopt(argc, argv, "+uqvf:", opt, 0, 0, 0); | |
1529 | if (i < 0) | |
1530 | break; | |
1531 | ||
1532 | switch (i) { | |
1533 | case 'u': | |
1534 | o.f |= f_utc; | |
1535 | break; | |
1536 | case 'q': | |
1537 | if (o.v) | |
1538 | o.v--; | |
1539 | break; | |
1540 | case 'v': | |
1541 | o.v++; | |
1542 | break; | |
1543 | case 'f': { | |
1544 | char *p; | |
1545 | int e = key_readflags(optarg, &p, &o.kf.f, &o.kf.m); | |
1546 | if (e || *p) | |
1547 | die(EXIT_FAILURE, "bad filter string `%s'", optarg); | |
45c0fd36 | 1548 | } break; |
252c122d | 1549 | default: |
1550 | o.f |= f_bogus; | |
1551 | break; | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | if (o.f & f_bogus) | |
c65df279 | 1556 | die(EXIT_FAILURE, "Usage: list [-uqv] [-f FILTER] [TAG...]"); |
252c122d | 1557 | |
1558 | /* --- Open the key file --- */ | |
1559 | ||
1560 | doopen(&f, KOPEN_READ); | |
1561 | o.t = time(0); | |
1562 | ||
1563 | /* --- Set up the time format --- */ | |
1564 | ||
1565 | if (!o.v) | |
1566 | o.tfmt = "%Y-%m-%d"; | |
1567 | else if (o.f & f_utc) | |
1568 | o.tfmt = "%Y-%m-%d %H:%M:%S UTC"; | |
1569 | else | |
1570 | o.tfmt = "%Y-%m-%d %H:%M:%S %Z"; | |
1571 | ||
1572 | /* --- If specific keys were requested use them, otherwise do all --- * | |
1573 | * | |
1574 | * Some day, this might turn into a wildcard match. | |
1575 | */ | |
1576 | ||
1577 | if (optind < argc) { | |
1578 | do { | |
1579 | if ((k = key_bytag(&f, argv[optind])) != 0) | |
1580 | showkey(k, &o); | |
1581 | else { | |
1582 | moan("key `%s' not found", argv[optind]); | |
1583 | o.f |= f_bogus; | |
1584 | } | |
1585 | optind++; | |
1586 | } while (optind < argc); | |
1587 | } else { | |
1588 | key_iter i; | |
1589 | for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; ) | |
1590 | showkey(k, &o); | |
1591 | } | |
1592 | ||
1593 | /* --- Done --- */ | |
1594 | ||
1595 | doclose(&f); | |
1596 | if (o.f & f_bogus) | |
1597 | return (EXIT_FAILURE); | |
1598 | else | |
1599 | return (0); | |
1600 | } | |
1601 | ||
1602 | /*----- Command implementation --------------------------------------------*/ | |
1603 | ||
d03ab969 | 1604 | /* --- @cmd_expire@ --- */ |
1605 | ||
1606 | static int cmd_expire(int argc, char *argv[]) | |
1607 | { | |
1608 | key_file f; | |
1609 | key *k; | |
d03ab969 | 1610 | int i; |
1611 | int rc = 0; | |
1612 | ||
1613 | if (argc < 2) | |
c65df279 | 1614 | die(EXIT_FAILURE, "Usage: expire TAG..."); |
d03ab969 | 1615 | doopen(&f, KOPEN_WRITE); |
1616 | for (i = 1; i < argc; i++) { | |
252c122d | 1617 | if ((k = key_bytag(&f, argv[i])) != 0) |
d03ab969 | 1618 | key_expire(&f, k); |
1619 | else { | |
252c122d | 1620 | moan("key `%s' not found", argv[i]); |
d03ab969 | 1621 | rc = 1; |
1622 | } | |
1623 | } | |
1624 | doclose(&f); | |
1625 | return (rc); | |
1626 | } | |
1627 | ||
1628 | /* --- @cmd_delete@ --- */ | |
1629 | ||
1630 | static int cmd_delete(int argc, char *argv[]) | |
1631 | { | |
1632 | key_file f; | |
1633 | key *k; | |
d03ab969 | 1634 | int i; |
1635 | int rc = 0; | |
1636 | ||
1637 | if (argc < 2) | |
c65df279 | 1638 | die(EXIT_FAILURE, "Usage: delete TAG..."); |
d03ab969 | 1639 | doopen(&f, KOPEN_WRITE); |
1640 | for (i = 1; i < argc; i++) { | |
252c122d | 1641 | if ((k = key_bytag(&f, argv[i])) != 0) |
d03ab969 | 1642 | key_delete(&f, k); |
1643 | else { | |
252c122d | 1644 | moan("key `%s' not found", argv[i]); |
d03ab969 | 1645 | rc = 1; |
1646 | } | |
1647 | } | |
1648 | doclose(&f); | |
1649 | return (rc); | |
1650 | } | |
1651 | ||
1652 | /* --- @cmd_setattr@ --- */ | |
1653 | ||
1654 | static int cmd_setattr(int argc, char *argv[]) | |
1655 | { | |
1656 | key_file f; | |
1657 | key *k; | |
d03ab969 | 1658 | |
1659 | if (argc < 3) | |
c65df279 | 1660 | die(EXIT_FAILURE, "Usage: setattr TAG ATTR..."); |
d03ab969 | 1661 | doopen(&f, KOPEN_WRITE); |
252c122d | 1662 | if ((k = key_bytag(&f, argv[1])) == 0) |
1663 | die(EXIT_FAILURE, "key `%s' not found", argv[1]); | |
d03ab969 | 1664 | setattr(&f, k, argv + 2); |
1665 | doclose(&f); | |
1666 | return (0); | |
1667 | } | |
1668 | ||
e9be047b | 1669 | /* --- @cmd_getattr@ --- */ |
1670 | ||
1671 | static int cmd_getattr(int argc, char *argv[]) | |
1672 | { | |
1673 | key_file f; | |
1674 | key *k; | |
1675 | dstr d = DSTR_INIT; | |
1676 | const char *p; | |
1677 | ||
1678 | if (argc != 3) | |
1679 | die(EXIT_FAILURE, "Usage: getattr TAG ATTR"); | |
1680 | doopen(&f, KOPEN_READ); | |
1681 | if ((k = key_bytag(&f, argv[1])) == 0) | |
1682 | die(EXIT_FAILURE, "key `%s' not found", argv[1]); | |
1683 | key_fulltag(k, &d); | |
1684 | if ((p = key_getattr(&f, k, argv[2])) == 0) | |
1685 | die(EXIT_FAILURE, "no attribute `%s' for key `%s'", argv[2], d.buf); | |
1686 | puts(p); | |
1687 | dstr_destroy(&d); | |
1688 | doclose(&f); | |
1689 | return (0); | |
1690 | } | |
1691 | ||
252c122d | 1692 | /* --- @cmd_finger@ --- */ |
d03ab969 | 1693 | |
16810bbd MW |
1694 | static const struct fpres { |
1695 | const char *name; | |
1696 | const codec_class *cdc; | |
1697 | unsigned short ival; | |
1698 | const char *sep; | |
1699 | } fprestab[] = { | |
1700 | { "hex", &hex_class, 8, "-:" }, | |
1701 | { "base32", &base32_class, 6, ":" }, | |
1702 | { 0, 0 } | |
1703 | }; | |
1704 | ||
1705 | static void fingerprint(key *k, const struct fpres *fpres, | |
1706 | const gchash *ch, const key_filter *kf) | |
d03ab969 | 1707 | { |
981bf127 | 1708 | ghash *h; |
16810bbd | 1709 | dstr d = DSTR_INIT, dd = DSTR_INIT; |
981bf127 | 1710 | const octet *p; |
1711 | size_t i; | |
16810bbd | 1712 | codec *c; |
d03ab969 | 1713 | |
981bf127 | 1714 | h = GH_INIT(ch); |
1715 | if (key_fingerprint(k, h, kf)) { | |
1716 | p = GH_DONE(h, 0); | |
16810bbd MW |
1717 | c = fpres->cdc->encoder(CDCF_LOWERC | CDCF_NOEQPAD, "", 0); |
1718 | c->ops->code(c, p, ch->hashsz, &dd); c->ops->code(c, 0, 0, &dd); | |
1719 | c->ops->destroy(c); | |
1720 | for (i = 0; i < dd.len; i++) { | |
1721 | if (i && i%fpres->ival == 0) dstr_putc(&d, fpres->sep[0]); | |
1722 | dstr_putc(&d, dd.buf[i]); | |
981bf127 | 1723 | } |
16810bbd MW |
1724 | dstr_putc(&d, ' '); key_fulltag(k, &d); dstr_putc(&d, '\n'); |
1725 | dstr_write(&d, stdout); | |
252c122d | 1726 | } |
16810bbd | 1727 | dstr_destroy(&d); dstr_destroy(&dd); |
981bf127 | 1728 | GH_DESTROY(h); |
252c122d | 1729 | } |
d03ab969 | 1730 | |
16810bbd MW |
1731 | static const struct fpres *lookup_fpres(const char *name) |
1732 | { | |
1733 | const struct fpres *fpres; | |
1734 | for (fpres = fprestab; fpres->name; fpres++) | |
1735 | if (strcmp(fpres->name, name) == 0) return (fpres); | |
1736 | die(EXIT_FAILURE, "unknown presentation syle `%s'", name); | |
1737 | } | |
1738 | ||
252c122d | 1739 | static int cmd_finger(int argc, char *argv[]) |
1740 | { | |
1741 | key_file f; | |
1742 | int rc = 0; | |
16810bbd | 1743 | const struct fpres *fpres = fprestab; |
981bf127 | 1744 | const gchash *ch = &rmd160; |
252c122d | 1745 | key_filter kf = { KF_NONSECRET, KF_NONSECRET }; |
d03ab969 | 1746 | |
1747 | for (;;) { | |
1748 | static struct option opt[] = { | |
252c122d | 1749 | { "filter", OPTF_ARGREQ, 0, 'f' }, |
16810bbd | 1750 | { "presentation", OPTF_ARGREQ, 0, 'p' }, |
981bf127 | 1751 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, |
252c122d | 1752 | { 0, 0, 0, 0 } |
d03ab969 | 1753 | }; |
16810bbd | 1754 | int i = mdwopt(argc, argv, "+f:a:p:", opt, 0, 0, 0); |
d03ab969 | 1755 | if (i < 0) |
1756 | break; | |
d03ab969 | 1757 | switch (i) { |
252c122d | 1758 | case 'f': { |
1759 | char *p; | |
1760 | int err = key_readflags(optarg, &p, &kf.f, &kf.m); | |
1761 | if (err || *p) | |
1762 | die(EXIT_FAILURE, "bad filter string `%s'", optarg); | |
1763 | } break; | |
16810bbd MW |
1764 | case 'p': |
1765 | fpres = lookup_fpres(optarg); | |
1766 | break; | |
981bf127 | 1767 | case 'a': |
1768 | if ((ch = ghash_byname(optarg)) == 0) | |
1769 | die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg); | |
1770 | break; | |
d03ab969 | 1771 | default: |
252c122d | 1772 | rc = 1; |
d03ab969 | 1773 | break; |
1774 | } | |
1775 | } | |
1776 | ||
252c122d | 1777 | argv += optind; argc -= optind; |
16810bbd MW |
1778 | if (rc) { |
1779 | die(EXIT_FAILURE, | |
1780 | "Usage: fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]"); | |
1781 | } | |
d03ab969 | 1782 | |
1783 | doopen(&f, KOPEN_READ); | |
d03ab969 | 1784 | |
252c122d | 1785 | if (argc) { |
1786 | int i; | |
1787 | for (i = 0; i < argc; i++) { | |
1788 | key *k = key_bytag(&f, argv[i]); | |
1789 | if (k) | |
16810bbd | 1790 | fingerprint(k, fpres, ch, &kf); |
252c122d | 1791 | else { |
1792 | rc = 1; | |
1793 | moan("key `%s' not found", argv[i]); | |
1794 | } | |
1795 | } | |
1796 | } else { | |
1797 | key_iter i; | |
1798 | key *k; | |
1799 | for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; ) | |
16810bbd | 1800 | fingerprint(k, fpres, ch, &kf); |
252c122d | 1801 | } |
1802 | return (rc); | |
1803 | } | |
d03ab969 | 1804 | |
58507325 | 1805 | /* --- @cmd_verify@ --- */ |
1806 | ||
58507325 | 1807 | static int cmd_verify(int argc, char *argv[]) |
1808 | { | |
1809 | key_file f; | |
1810 | int rc = 0; | |
1811 | const gchash *ch = &rmd160; | |
1812 | ghash *h; | |
1813 | key *k; | |
58507325 | 1814 | const octet *fpr; |
16810bbd MW |
1815 | dstr d = DSTR_INIT, dd = DSTR_INIT; |
1816 | codec *c; | |
1817 | const char *p; | |
1818 | const struct fpres *fpres = fprestab; | |
58507325 | 1819 | key_filter kf = { KF_NONSECRET, KF_NONSECRET }; |
1820 | ||
1821 | for (;;) { | |
1822 | static struct option opt[] = { | |
1823 | { "filter", OPTF_ARGREQ, 0, 'f' }, | |
16810bbd | 1824 | { "presentation", OPTF_ARGREQ, 0, 'p' }, |
58507325 | 1825 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, |
1826 | { 0, 0, 0, 0 } | |
1827 | }; | |
16810bbd | 1828 | int i = mdwopt(argc, argv, "+f:a:p:", opt, 0, 0, 0); |
58507325 | 1829 | if (i < 0) |
1830 | break; | |
1831 | switch (i) { | |
1832 | case 'f': { | |
1833 | char *p; | |
1834 | int err = key_readflags(optarg, &p, &kf.f, &kf.m); | |
1835 | if (err || *p) | |
1836 | die(EXIT_FAILURE, "bad filter string `%s'", optarg); | |
1837 | } break; | |
16810bbd MW |
1838 | case 'p': |
1839 | fpres = lookup_fpres(optarg); | |
1840 | break; | |
58507325 | 1841 | case 'a': |
1842 | if ((ch = ghash_byname(optarg)) == 0) | |
1843 | die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg); | |
1844 | break; | |
1845 | default: | |
1846 | rc = 1; | |
1847 | break; | |
1848 | } | |
1849 | } | |
1850 | ||
1851 | argv += optind; argc -= optind; | |
16810bbd MW |
1852 | if (rc || argc != 2) { |
1853 | die(EXIT_FAILURE, | |
1854 | "Usage: verify [-a HASHALG] [-p STYLE] [-f FILTER] TAG FINGERPRINT"); | |
1855 | } | |
58507325 | 1856 | |
1857 | doopen(&f, KOPEN_READ); | |
1858 | ||
1859 | if ((k = key_bytag(&f, argv[0])) == 0) | |
1860 | die(EXIT_FAILURE, "key `%s' not found", argv[0]); | |
16810bbd MW |
1861 | for (p = argv[1]; *p; p++) { |
1862 | if (strchr(fpres->sep, *p)) continue; | |
1863 | dstr_putc(&dd, *p); | |
1864 | } | |
1865 | c = fpres->cdc->decoder(CDCF_IGNCASE | CDCF_IGNEQPAD | | |
1866 | CDCF_IGNSPC | CDCF_IGNNEWL); | |
1867 | if ((rc = c->ops->code(c, dd.buf, dd.len, &d)) != 0 || | |
1868 | (rc = c->ops->code(c, 0, 0, &d)) != 0) | |
1869 | die(EXIT_FAILURE, "invalid fingerprint: %s", codec_strerror(rc)); | |
1870 | c->ops->destroy(c); | |
1871 | if (d.len != ch->hashsz) { | |
1872 | die(EXIT_FAILURE, "incorrect fingerprint length (%lu != %lu)", | |
1873 | (unsigned long)d.len, (unsigned long)ch->hashsz); | |
1874 | } | |
58507325 | 1875 | h = GH_INIT(ch); |
1876 | if (!key_fingerprint(k, h, &kf)) | |
1877 | die(EXIT_FAILURE, "key has no fingerprintable components (as filtered)"); | |
1878 | fpr = GH_DONE(h, 0); | |
16810bbd | 1879 | if (memcmp(fpr, d.buf, ch->hashsz) != 0) |
58507325 | 1880 | die(EXIT_FAILURE, "key fingerprint mismatch"); |
16810bbd | 1881 | dstr_destroy(&d); dstr_destroy(&dd); |
e9be047b | 1882 | doclose(&f); |
58507325 | 1883 | return (0); |
1884 | } | |
45c0fd36 | 1885 | |
252c122d | 1886 | /* --- @cmd_comment@ --- */ |
d03ab969 | 1887 | |
252c122d | 1888 | static int cmd_comment(int argc, char *argv[]) |
1889 | { | |
1890 | key_file f; | |
1891 | key *k; | |
1892 | int err; | |
d03ab969 | 1893 | |
252c122d | 1894 | if (argc < 2 || argc > 3) |
c65df279 | 1895 | die(EXIT_FAILURE, "Usage: comment TAG [COMMENT]"); |
252c122d | 1896 | doopen(&f, KOPEN_WRITE); |
1897 | if ((k = key_bytag(&f, argv[1])) == 0) | |
1898 | die(EXIT_FAILURE, "key `%s' not found", argv[1]); | |
1899 | if ((err = key_setcomment(&f, k, argv[2])) != 0) | |
1900 | die(EXIT_FAILURE, "bad comment `%s': %s", argv[2], key_strerror(err)); | |
1901 | doclose(&f); | |
1902 | return (0); | |
1903 | } | |
d03ab969 | 1904 | |
252c122d | 1905 | /* --- @cmd_tag@ --- */ |
d03ab969 | 1906 | |
252c122d | 1907 | static int cmd_tag(int argc, char *argv[]) |
1908 | { | |
1909 | key_file f; | |
1910 | key *k; | |
1911 | int err; | |
ce70ec47 | 1912 | unsigned flags = 0; |
1913 | int rc = 0; | |
d03ab969 | 1914 | |
ce70ec47 | 1915 | for (;;) { |
1916 | static struct option opt[] = { | |
1917 | { "retag", 0, 0, 'r' }, | |
1918 | { 0, 0, 0, 0 } | |
1919 | }; | |
1920 | int i = mdwopt(argc, argv, "+r", opt, 0, 0, 0); | |
1921 | if (i < 0) | |
1922 | break; | |
1923 | switch (i) { | |
1924 | case 'r': | |
1925 | flags |= f_retag; | |
1926 | break; | |
1927 | default: | |
1928 | rc = 1; | |
1929 | break; | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | argv += optind; argc -= optind; | |
1934 | if (argc < 1 || argc > 2 || rc) | |
c65df279 | 1935 | die(EXIT_FAILURE, "Usage: tag [-r] TAG [NEW-TAG]"); |
252c122d | 1936 | doopen(&f, KOPEN_WRITE); |
ce70ec47 | 1937 | if (flags & f_retag) { |
1938 | if ((k = key_bytag(&f, argv[1])) != 0 && strcmp(k->tag, argv[1]) == 0) | |
1939 | key_settag(&f, k, 0); | |
1940 | } | |
1941 | if ((k = key_bytag(&f, argv[0])) == 0) | |
1942 | die(EXIT_FAILURE, "key `%s' not found", argv[0]); | |
1943 | if ((err = key_settag(&f, k, argv[1])) != 0) | |
1944 | die(EXIT_FAILURE, "bad tag `%s': %s", argv[1], key_strerror(err)); | |
252c122d | 1945 | doclose(&f); |
1946 | return (0); | |
1947 | } | |
d03ab969 | 1948 | |
252c122d | 1949 | /* --- @cmd_lock@ --- */ |
d03ab969 | 1950 | |
252c122d | 1951 | static int cmd_lock(int argc, char *argv[]) |
1952 | { | |
1953 | key_file f; | |
1954 | key *k; | |
ef13e9a4 | 1955 | key_data **kd; |
252c122d | 1956 | dstr d = DSTR_INIT; |
d03ab969 | 1957 | |
252c122d | 1958 | if (argc != 2) |
c65df279 | 1959 | die(EXIT_FAILURE, "Usage: lock QTAG"); |
252c122d | 1960 | doopen(&f, KOPEN_WRITE); |
1961 | if (key_qtag(&f, argv[1], &d, &k, &kd)) | |
1962 | die(EXIT_FAILURE, "key `%s' not found", argv[1]); | |
ef13e9a4 | 1963 | if ((*kd)->e == KENC_ENCRYPT && key_punlock(kd, 0, d.buf)) |
252c122d | 1964 | die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf); |
ef13e9a4 | 1965 | if (key_plock(kd, 0, d.buf)) |
252c122d | 1966 | die(EXIT_FAILURE, "failed to lock key `%s'", d.buf); |
1967 | f.f |= KF_MODIFIED; | |
1968 | doclose(&f); | |
1969 | return (0); | |
1970 | } | |
d03ab969 | 1971 | |
252c122d | 1972 | /* --- @cmd_unlock@ --- */ |
d03ab969 | 1973 | |
252c122d | 1974 | static int cmd_unlock(int argc, char *argv[]) |
1975 | { | |
1976 | key_file f; | |
1977 | key *k; | |
ef13e9a4 | 1978 | key_data **kd; |
252c122d | 1979 | dstr d = DSTR_INIT; |
d03ab969 | 1980 | |
252c122d | 1981 | if (argc != 2) |
c65df279 | 1982 | die(EXIT_FAILURE, "Usage: unlock QTAG"); |
252c122d | 1983 | doopen(&f, KOPEN_WRITE); |
1984 | if (key_qtag(&f, argv[1], &d, &k, &kd)) | |
1985 | die(EXIT_FAILURE, "key `%s' not found", argv[1]); | |
ef13e9a4 | 1986 | if ((*kd)->e != KENC_ENCRYPT) |
252c122d | 1987 | die(EXIT_FAILURE, "key `%s' is not encrypted", d.buf); |
ef13e9a4 | 1988 | if (key_punlock(kd, 0, d.buf)) |
252c122d | 1989 | die(EXIT_FAILURE, "couldn't unlock key `%s'", d.buf); |
1990 | f.f |= KF_MODIFIED; | |
d03ab969 | 1991 | doclose(&f); |
1992 | return (0); | |
1993 | } | |
1994 | ||
1995 | /* --- @cmd_extract@ --- */ | |
1996 | ||
1997 | static int cmd_extract(int argc, char *argv[]) | |
1998 | { | |
1999 | key_file f; | |
2000 | key *k; | |
d03ab969 | 2001 | int i; |
2002 | int rc = 0; | |
252c122d | 2003 | key_filter kf = { 0, 0 }; |
694fc3a8 | 2004 | dstr d = DSTR_INIT; |
2005 | const char *outfile = 0; | |
d03ab969 | 2006 | FILE *fp; |
2007 | ||
252c122d | 2008 | for (;;) { |
2009 | static struct option opt[] = { | |
2010 | { "filter", OPTF_ARGREQ, 0, 'f' }, | |
2011 | { 0, 0, 0, 0 } | |
2012 | }; | |
2013 | int i = mdwopt(argc, argv, "f:", opt, 0, 0, 0); | |
2014 | if (i < 0) | |
2015 | break; | |
2016 | switch (i) { | |
2017 | case 'f': { | |
2018 | char *p; | |
2019 | int err = key_readflags(optarg, &p, &kf.f, &kf.m); | |
2020 | if (err || *p) | |
2021 | die(EXIT_FAILURE, "bad filter string `%s'", optarg); | |
2022 | } break; | |
2023 | default: | |
2024 | rc = 1; | |
2025 | break; | |
2026 | } | |
2027 | } | |
2028 | ||
2029 | argv += optind; argc -= optind; | |
2030 | if (rc || argc < 1) | |
c65df279 | 2031 | die(EXIT_FAILURE, "Usage: extract [-f FILTER] FILE [TAG...]"); |
252c122d | 2032 | if (strcmp(*argv, "-") == 0) |
d03ab969 | 2033 | fp = stdout; |
694fc3a8 | 2034 | else { |
2035 | outfile = *argv; | |
2036 | dstr_putf(&d, "%s.new", outfile); | |
2037 | if (!(fp = fopen(d.buf, "w"))) { | |
2038 | die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", | |
2039 | d.buf, strerror(errno)); | |
2040 | } | |
d03ab969 | 2041 | } |
2042 | ||
252c122d | 2043 | doopen(&f, KOPEN_READ); |
2044 | if (argc < 2) { | |
2045 | key_iter i; | |
2046 | key *k; | |
2047 | for (key_mkiter(&i, &f); (k = key_next(&i)) != 0; ) | |
2048 | key_extract(&f, k, fp, &kf); | |
45c0fd36 | 2049 | } else { |
252c122d | 2050 | for (i = 1; i < argc; i++) { |
2051 | if ((k = key_bytag(&f, argv[i])) != 0) | |
2052 | key_extract(&f, k, fp, &kf); | |
2053 | else { | |
2054 | moan("key `%s' not found", argv[i]); | |
2055 | rc = 1; | |
2056 | } | |
d03ab969 | 2057 | } |
2058 | } | |
694fc3a8 | 2059 | if (fclose(fp) || (outfile && rename(d.buf, outfile))) |
252c122d | 2060 | die(EXIT_FAILURE, "error writing file: %s", strerror(errno)); |
694fc3a8 | 2061 | dstr_destroy(&d); |
d03ab969 | 2062 | doclose(&f); |
2063 | return (rc); | |
2064 | } | |
2065 | ||
2066 | /* --- @cmd_tidy@ --- */ | |
2067 | ||
2068 | static int cmd_tidy(int argc, char *argv[]) | |
2069 | { | |
2070 | key_file f; | |
2071 | if (argc != 1) | |
c65df279 | 2072 | die(EXIT_FAILURE, "Usage: tidy"); |
d03ab969 | 2073 | doopen(&f, KOPEN_WRITE); |
2074 | f.f |= KF_MODIFIED; /* Nasty hack */ | |
2075 | doclose(&f); | |
2076 | return (0); | |
2077 | } | |
2078 | ||
2079 | /* --- @cmd_merge@ --- */ | |
2080 | ||
2081 | static int cmd_merge(int argc, char *argv[]) | |
2082 | { | |
2083 | key_file f; | |
2084 | FILE *fp; | |
2085 | ||
2086 | if (argc != 2) | |
c65df279 | 2087 | die(EXIT_FAILURE, "Usage: merge FILE"); |
d03ab969 | 2088 | if (strcmp(argv[1], "-") == 0) |
2089 | fp = stdin; | |
2090 | else if (!(fp = fopen(argv[1], "r"))) { | |
d3296500 | 2091 | die(EXIT_FAILURE, "couldn't open `%s' for reading: %s", |
d03ab969 | 2092 | argv[1], strerror(errno)); |
2093 | } | |
2094 | ||
2095 | doopen(&f, KOPEN_WRITE); | |
252c122d | 2096 | key_merge(&f, argv[1], fp, key_moan, 0); |
d03ab969 | 2097 | doclose(&f); |
2098 | return (0); | |
2099 | } | |
2100 | ||
c65df279 | 2101 | /* --- @cmd_show@ --- */ |
2102 | ||
2103 | #define LISTS(LI) \ | |
2104 | LI("Lists", list, \ | |
2105 | listtab[i].name, listtab[i].name) \ | |
2106 | LI("Hash functions", hash, \ | |
2107 | ghashtab[i], ghashtab[i]->name) \ | |
2108 | LI("Elliptic curves", ec, \ | |
2109 | ectab[i].name, ectab[i].name) \ | |
3688eb75 | 2110 | LI("Prime Diffie-Hellman groups", dh, \ |
c65df279 | 2111 | ptab[i].name, ptab[i].name) \ |
3688eb75 | 2112 | LI("Binary Diffie-Hellman groups", bindh, \ |
2113 | bintab[i].name, bintab[i].name) \ | |
c65df279 | 2114 | LI("Key-generation algorithms", keygen, \ |
2115 | algtab[i].name, algtab[i].name) \ | |
2116 | LI("Random seeding algorithms", seed, \ | |
16810bbd MW |
2117 | seedtab[i].p, seedtab[i].p) \ |
2118 | LI("Fingerprint presentation styles", fpres, \ | |
2119 | fprestab[i].name, fprestab[i].name) | |
c65df279 | 2120 | |
2121 | MAKELISTTAB(listtab, LISTS) | |
2122 | ||
2123 | static int cmd_show(int argc, char *argv[]) | |
2124 | { | |
2125 | return (displaylists(listtab, argv + 1)); | |
2126 | } | |
2127 | ||
d03ab969 | 2128 | /*----- Main command table ------------------------------------------------*/ |
2129 | ||
c65df279 | 2130 | static int cmd_help(int argc, char *argv[]); |
2131 | ||
2132 | static cmd cmds[] = { | |
2133 | { "help", cmd_help, "help [COMMAND...]" }, | |
2134 | { "show", cmd_show, "show [ITEM...]" }, | |
2135 | { "list", cmd_list, "list [-uqv] [-f FILTER] [TAG...]", "\ | |
2136 | Options:\n\ | |
2137 | \n\ | |
2138 | -u, --utc Display expiry times etc. in UTC, not local time.\n\ | |
2139 | -q, --quiet Show less information.\n\ | |
2140 | -v, --verbose Show more information.\n\ | |
2141 | " }, | |
16810bbd MW |
2142 | { "fingerprint", cmd_finger, |
2143 | "fingerprint [-a HASHALG] [-p STYLE] [-f FILTER] [TAG...]", "\ | |
c65df279 | 2144 | Options:\n\ |
2145 | \n\ | |
2146 | -f, --filter=FILT Only hash key components matching FILT.\n\ | |
16810bbd | 2147 | -p, --presentation=STYLE Use STYLE for presenting fingerprints.\n\ |
c65df279 | 2148 | -a, --algorithm=HASH Use the named HASH algorithm.\n\ |
2149 | ($ show hash for list.)\n\ | |
2150 | " }, | |
16810bbd MW |
2151 | { "verify", cmd_verify, |
2152 | "verify [-a HASH] [-p STYLE] [-f FILTER] TAG FINGERPRINT", "\ | |
58507325 | 2153 | Options:\n\ |
2154 | \n\ | |
2155 | -f, --filter=FILT Only hash key components matching FILT.\n\ | |
16810bbd | 2156 | -p, --presentation=STYLE Expect FINGERPRINT in the given STYLE.\n\ |
58507325 | 2157 | -a, --algorithm=HASH Use the named HASH algorithm.\n\ |
2158 | ($ show hash for list.)\n\ | |
2159 | " }, | |
c65df279 | 2160 | { "extract", cmd_extract, "extract [-f FILTER] FILE [TAG...]", "\ |
2161 | Options:\n\ | |
2162 | \n\ | |
2163 | -f, --filter=FILT Only extract key components matching FILT.\n\ | |
2164 | " }, | |
2165 | { "merge", cmd_merge, "merge FILE" }, | |
2166 | { "expire", cmd_expire, "expire TAG..." }, | |
2167 | { "delete", cmd_delete, "delete TAG..." }, | |
2168 | { "setattr", cmd_setattr, "setattr TAG ATTR..." }, | |
e9be047b | 2169 | { "getattr", cmd_getattr, "getattr TAG ATTR" }, |
c65df279 | 2170 | { "comment", cmd_comment, "comment TAG [COMMENT]" }, |
2171 | { "lock", cmd_lock, "lock QTAG" }, | |
2172 | { "unlock", cmd_unlock, "unlock QTAG" }, | |
2173 | { "tag", cmd_tag, "tag [-r] TAG [NEW-TAG]", "\ | |
2174 | Options:\n\ | |
2175 | \n\ | |
2176 | -r, --retag Untag any key currently called new-tag.\n\ | |
2177 | " }, | |
2178 | { "tidy", cmd_tidy, "tidy" }, | |
d03ab969 | 2179 | { "add", cmd_add, |
c65df279 | 2180 | "add [-OPTIONS] TYPE [ATTR...]\n\ |
4e67e30b | 2181 | Options: [-lqrLKS] [-a ALG] [-bB BITS] [-p PARAM] [-R TAG]\n\ |
45c0fd36 MW |
2182 | [-A SEEDALG] [-s SEED] [-n BITS] [-I KEYID]\n\ |
2183 | [-e EXPIRE] [-t TAG] [-c COMMENT]", "\ | |
ce70ec47 | 2184 | Options:\n\ |
2185 | \n\ | |
2186 | -a, --algorithm=ALG Generate keys suitable for ALG.\n\ | |
c65df279 | 2187 | ($ show keygen for list.)\n\ |
ce70ec47 | 2188 | -b, --bits=N Generate an N-bit key.\n\ |
2189 | -B, --qbits=N Use an N-bit subgroup or factors.\n\ | |
2190 | -p, --parameters=TAG Get group parameters from TAG.\n\ | |
c65df279 | 2191 | -C, --curve=NAME Use elliptic curve or DH group NAME.\n\ |
2192 | ($ show ec or $ show dh for list.)\n\ | |
2193 | -A, --seedalg=ALG Use pseudorandom generator ALG to generate key.\n\ | |
2194 | ($ show seed for list.)\n\ | |
2195 | -s, --seed=BASE64 Use Base64-encoded string BASE64 as seed.\n\ | |
2196 | -n, --newseed=COUNT Generate new COUNT-bit seed.\n\ | |
ce70ec47 | 2197 | -e, --expire=TIME Make the key expire after TIME.\n\ |
2198 | -c, --comment=STRING Attach the command STRING to the key.\n\ | |
2199 | -t, --tag=TAG Tag the key with the name TAG.\n\ | |
2200 | -r, --retag Untag any key currently with that tag.\n\ | |
2201 | -R, --rand-id=TAG Use key named TAG for the random number generator.\n\ | |
90a88ae3 | 2202 | -I, --key-id=ID Force the key-id for the new key.\n\ |
ce70ec47 | 2203 | -l, --lock Lock the generated key with a passphrase.\n\ |
2204 | -q, --quiet Don't give progress indicators while working.\n\ | |
2205 | -L, --lim-lee Generate Lim-Lee primes for Diffie-Hellman groups.\n\ | |
4e67e30b | 2206 | -K, --kcdsa Generate KCDSA-style Lim-Lee primes for DH groups.\n\ |
ce70ec47 | 2207 | -S, --subgroup Use a prime-order subgroup for Diffie-Hellman.\n\ |
2208 | " }, | |
d03ab969 | 2209 | { 0, 0, 0 } |
2210 | }; | |
2211 | ||
c65df279 | 2212 | static int cmd_help(int argc, char *argv[]) |
ce70ec47 | 2213 | { |
c65df279 | 2214 | sc_help(cmds, stdout, argv + 1); |
2215 | return (0); | |
ce70ec47 | 2216 | } |
2217 | ||
c65df279 | 2218 | /*----- Main code ---------------------------------------------------------*/ |
2219 | ||
d03ab969 | 2220 | /* --- Helpful GNUy functions --- */ |
2221 | ||
c65df279 | 2222 | static void usage(FILE *fp) |
d03ab969 | 2223 | { |
c65df279 | 2224 | pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n"); |
d03ab969 | 2225 | } |
2226 | ||
2227 | void version(FILE *fp) | |
2228 | { | |
052b36d0 | 2229 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
d03ab969 | 2230 | } |
2231 | ||
c65df279 | 2232 | void help_global(FILE *fp) |
d03ab969 | 2233 | { |
c65df279 | 2234 | usage(fp); |
2235 | fputs("\n\ | |
2236 | Performs various simple key management operations.\n\ | |
2237 | \n\ | |
2238 | Global command line options:\n\ | |
d03ab969 | 2239 | \n\ |
c65df279 | 2240 | -h, --help [COMMAND...] Display this help text (or help for COMMANDs).\n\ |
d03ab969 | 2241 | -v, --version Display version number.\n\ |
2242 | -u, --usage Display short usage summary.\n\ | |
2243 | \n\ | |
c65df279 | 2244 | -k, --keyring=FILE Read and write keys in FILE.\n", |
2245 | fp); | |
d03ab969 | 2246 | } |
2247 | ||
2248 | /* --- @main@ --- * | |
2249 | * | |
2250 | * Arguments: @int argc@ = number of command line arguments | |
2251 | * @char *argv[]@ = array of command line arguments | |
2252 | * | |
2253 | * Returns: Nonzero on failure. | |
2254 | * | |
2255 | * Use: Main program. Performs simple key management functions. | |
2256 | */ | |
2257 | ||
2258 | int main(int argc, char *argv[]) | |
2259 | { | |
2260 | unsigned f = 0; | |
2261 | ||
16efd15b | 2262 | #define f_bogus 1u |
d03ab969 | 2263 | |
2264 | /* --- Initialization --- */ | |
2265 | ||
2266 | ego(argv[0]); | |
2267 | sub_init(); | |
2268 | ||
2269 | /* --- Parse command line options --- */ | |
2270 | ||
2271 | for (;;) { | |
2272 | static struct option opt[] = { | |
2273 | ||
2274 | /* --- Standard GNUy help options --- */ | |
2275 | ||
2276 | { "help", 0, 0, 'h' }, | |
2277 | { "version", 0, 0, 'v' }, | |
2278 | { "usage", 0, 0, 'u' }, | |
2279 | ||
2280 | /* --- Real live useful options --- */ | |
2281 | ||
2282 | { "keyring", OPTF_ARGREQ, 0, 'k' }, | |
2283 | ||
2284 | /* --- Magic terminator --- */ | |
2285 | ||
2286 | { 0, 0, 0, 0 } | |
2287 | }; | |
99dde2f4 | 2288 | int i = mdwopt(argc, argv, "+hvu k:", opt, 0, 0, 0); |
d03ab969 | 2289 | |
2290 | if (i < 0) | |
2291 | break; | |
2292 | switch (i) { | |
2293 | ||
2294 | /* --- GNU help options --- */ | |
ce70ec47 | 2295 | |
d03ab969 | 2296 | case 'h': |
c65df279 | 2297 | sc_help(cmds, stdout, argv + optind); |
d03ab969 | 2298 | exit(0); |
2299 | case 'v': | |
2300 | version(stdout); | |
2301 | exit(0); | |
2302 | case 'u': | |
2303 | usage(stdout); | |
2304 | exit(0); | |
2305 | ||
2306 | /* --- Real useful options --- */ | |
2307 | ||
2308 | case 'k': | |
2309 | keyfile = optarg; | |
2310 | break; | |
2311 | ||
2312 | /* --- Bogosity --- */ | |
2313 | ||
2314 | default: | |
2315 | f |= f_bogus; | |
2316 | break; | |
2317 | } | |
2318 | } | |
2319 | ||
2320 | /* --- Complain about excessive bogons --- */ | |
2321 | ||
2322 | if (f & f_bogus || optind == argc) { | |
2323 | usage(stderr); | |
2324 | exit(1); | |
2325 | } | |
2326 | ||
052b36d0 | 2327 | /* --- Initialize the Catacomb random number generator --- */ |
2328 | ||
052b36d0 | 2329 | rand_noisesrc(RAND_GLOBAL, &noise_source); |
9219a5d6 | 2330 | rand_seed(RAND_GLOBAL, 160); |
052b36d0 | 2331 | |
d03ab969 | 2332 | /* --- Dispatch to appropriate command handler --- */ |
2333 | ||
2334 | argc -= optind; | |
2335 | argv += optind; | |
2336 | optind = 0; | |
c65df279 | 2337 | return (findcmd(cmds, argv[0])->cmd(argc, argv)); |
d03ab969 | 2338 | } |
2339 | ||
2340 | /*----- That's all, folks -------------------------------------------------*/ |