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