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