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