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