d03ab969 |
1 | /* -*-c-*- |
2 | * |
4b536f42 |
3 | * $Id: keyutil.c,v 1.10 2000/10/08 12:02:21 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 $ |
4b536f42 |
33 | * Revision 1.10 2000/10/08 12:02:21 mdw |
34 | * Use @MP_EQ@ instead of @MP_CMP@. |
35 | * |
dce3fb0d |
36 | * Revision 1.9 2000/08/15 21:40:49 mdw |
37 | * Minor formatting change in listing attributes. |
38 | * |
40d5a112 |
39 | * Revision 1.8 2000/07/29 09:59:13 mdw |
40 | * Support Lim-Lee primes in Diffie-Hellman parameter generation. |
41 | * |
ab0ca95f |
42 | * Revision 1.7 2000/07/01 11:18:51 mdw |
43 | * Use new interfaces for key manipulation. |
44 | * |
9219a5d6 |
45 | * Revision 1.6 2000/06/17 11:28:22 mdw |
46 | * Use secure memory interface from MP library. `rand_getgood' is |
47 | * deprecated. |
48 | * |
052b36d0 |
49 | * Revision 1.5 2000/02/12 18:21:03 mdw |
50 | * Overhaul of key management (again). |
51 | * |
252c122d |
52 | * Revision 1.4 1999/12/22 15:48:10 mdw |
53 | * Track new key-management changes. Support new key generation |
54 | * algorithms. |
55 | * |
ce1f2631 |
56 | * Revision 1.3 1999/11/02 15:23:24 mdw |
57 | * Fix newlines in keyring list. |
58 | * |
cc321283 |
59 | * Revision 1.2 1999/10/15 21:05:28 mdw |
60 | * In `key list', show timezone for local times, and support `-u' option |
61 | * for UTC output. |
62 | * |
d03ab969 |
63 | * Revision 1.1 1999/09/03 08:41:12 mdw |
64 | * Initial import. |
65 | * |
66 | */ |
67 | |
68 | /*----- Header files ------------------------------------------------------*/ |
69 | |
70 | #include "config.h" |
71 | |
72 | #include <errno.h> |
73 | #include <stdio.h> |
74 | #include <stdlib.h> |
75 | #include <string.h> |
76 | #include <time.h> |
77 | |
252c122d |
78 | #include <mLib/base64.h> |
d03ab969 |
79 | #include <mLib/mdwopt.h> |
80 | #include <mLib/quis.h> |
81 | #include <mLib/report.h> |
82 | #include <mLib/sub.h> |
83 | |
84 | #include <noise.h> |
85 | #include <rand.h> |
86 | |
252c122d |
87 | #include "bbs.h" |
052b36d0 |
88 | #include "dh.h" |
252c122d |
89 | #include "dsa.h" |
90 | #include "fibrand.h" |
d03ab969 |
91 | #include "getdate.h" |
92 | #include "key.h" |
252c122d |
93 | #include "mp.h" |
94 | #include "mpmont.h" |
95 | #include "mprand.h" |
96 | #include "mptext.h" |
97 | #include "pgen.h" |
98 | #include "rsa.h" |
d03ab969 |
99 | |
100 | /*----- Handy global state ------------------------------------------------*/ |
101 | |
102 | static const char *keyfile = "keyring"; |
103 | |
104 | /*----- Useful shared functions -------------------------------------------*/ |
105 | |
106 | /* --- @doopen@ --- * |
107 | * |
108 | * Arguments: @key_file *f@ = pointer to key file block |
109 | * @unsigned how@ = method to open file with |
110 | * |
111 | * Returns: --- |
112 | * |
113 | * Use: Opens a key file and handles errors by panicking |
114 | * appropriately. |
115 | */ |
116 | |
117 | static void doopen(key_file *f, unsigned how) |
118 | { |
252c122d |
119 | if (key_open(f, keyfile, how, key_moan, 0)) |
052b36d0 |
120 | die(1, "couldn't open keyring `%s': %s", keyfile, strerror(errno)); |
d03ab969 |
121 | } |
122 | |
123 | /* --- @doclose@ --- * |
124 | * |
125 | * Arguments: @key_file *f@ = pointer to key file block |
126 | * |
127 | * Returns: --- |
128 | * |
129 | * Use: Closes a key file and handles errors by panicking |
130 | * appropriately. |
131 | */ |
132 | |
133 | static void doclose(key_file *f) |
134 | { |
135 | switch (key_close(f)) { |
136 | case KWRITE_FAIL: |
137 | die(EXIT_FAILURE, "couldn't write file `%s': %s", |
138 | keyfile, strerror(errno)); |
139 | case KWRITE_BROKEN: |
140 | die(EXIT_FAILURE, "keyring file `%s' broken: %s (repair manually)", |
141 | keyfile, strerror(errno)); |
142 | } |
143 | } |
144 | |
145 | /* --- @setattr@ --- * |
146 | * |
147 | * Arguments: @key_file *f@ = pointer to key file block |
148 | * @key *k@ = pointer to key block |
149 | * @char *v[]@ = array of assignments (overwritten!) |
150 | * |
151 | * Returns: --- |
152 | * |
153 | * Use: Applies the attribute assignments to the key. |
154 | */ |
155 | |
156 | static void setattr(key_file *f, key *k, char *v[]) |
157 | { |
158 | while (*v) { |
252c122d |
159 | int err; |
d03ab969 |
160 | char *p = *v; |
161 | size_t eq = strcspn(p, "="); |
162 | if (p[eq] == 0) |
163 | moan("invalid assignment: `%s'", p); |
164 | p[eq] = 0; |
165 | p += eq + 1; |
252c122d |
166 | if ((err = key_putattr(f, k, *v, *p ? p : 0)) != 0) |
167 | die(EXIT_FAILURE, "couldn't set attributes: %s", key_strerror(err)); |
d03ab969 |
168 | v++; |
169 | } |
170 | } |
171 | |
252c122d |
172 | /*----- Key generation ----------------------------------------------------*/ |
173 | |
174 | /* --- Key generation parameters --- */ |
175 | |
176 | typedef struct keyopts { |
177 | key_file *kf; /* Pointer to key file */ |
178 | key *k; /* Pointer to the actual key */ |
179 | dstr tag; /* Full tag name for the key */ |
180 | unsigned f; /* Flags for the new key */ |
181 | unsigned bits, qbits; /* Bit length for the new key */ |
182 | key *p; /* Parameters key-data */ |
183 | } keyopts; |
184 | |
185 | enum { |
186 | f_bogus = 1, /* Error in parsing */ |
187 | f_lock = 2, /* Passphrase-lock private key */ |
40d5a112 |
188 | f_quiet = 4, /* Don't show a progress indicator */ |
189 | f_limlee = 8, /* Generate Lim-Lee primes */ |
190 | f_subgroup = 16 /* Generate a subgroup */ |
252c122d |
191 | }; |
192 | |
193 | /* --- @dolock@ --- * |
194 | * |
195 | * Arguments: @keyopts *k@ = key generation options |
196 | * @key_data *kd@ = pointer to key data to lock |
197 | * @const char *t@ = tag suffix or null |
198 | * |
199 | * Returns: --- |
200 | * |
201 | * Use: Does passphrase locking on new keys. |
202 | */ |
203 | |
204 | static void dolock(keyopts *k, key_data *kd, const char *t) |
205 | { |
206 | if (!(k->f & f_lock)) |
207 | return; |
208 | if (t) |
209 | dstr_putf(&k->tag, ".%s", t); |
210 | if (key_plock(k->tag.buf, kd, kd)) |
211 | die(EXIT_FAILURE, "couldn't lock key"); |
212 | } |
213 | |
214 | /* --- @mpkey@ --- * |
215 | * |
216 | * Arguments: @key_data *kd@ = pointer to parent key block |
217 | * @const char *tag@ = pointer to tag string |
218 | * @mp *m@ = integer to store |
219 | * @unsigned f@ = flags to set |
220 | * |
221 | * Returns: --- |
222 | * |
223 | * Use: Sets a multiprecision integer subkey. |
224 | */ |
225 | |
226 | static void mpkey(key_data *kd, const char *tag, mp *m, unsigned f) |
227 | { |
228 | key_data *kkd = key_structcreate(kd, tag); |
229 | key_mp(kkd, m); |
230 | kkd->e |= f; |
231 | } |
232 | |
233 | /* --- @copyparam@ --- * |
234 | * |
235 | * Arguments: @keyopts *k@ = pointer to key options |
236 | * @const char **pp@ = checklist of parameters |
237 | * |
238 | * Returns: Nonzero if parameters copied; zero if you have to generate |
239 | * them. |
240 | * |
241 | * Use: Copies parameters from a source key to the current one. |
242 | */ |
243 | |
244 | static int copyparam(keyopts *k, const char **pp) |
245 | { |
246 | key_filter kf; |
247 | |
248 | /* --- Quick check if no parameters supplied --- */ |
249 | |
250 | if (!k->p) |
251 | return (0); |
252 | |
253 | /* --- Run through the checklist --- */ |
254 | |
255 | while (*pp) { |
256 | key_data *kd = key_structfind(&k->p->k, *pp); |
257 | if (!kd) |
258 | die(EXIT_FAILURE, "bad parameter key: parameter `%s' not found", *pp); |
259 | if ((kd->e & KF_CATMASK) != KCAT_SHARE) |
260 | die(EXIT_FAILURE, "bad parameter key: subkey `%s' is not shared", *pp); |
261 | pp++; |
262 | } |
263 | |
264 | /* --- Copy over the parameters --- */ |
265 | |
266 | kf.f = KCAT_SHARE; |
267 | kf.m = KF_CATMASK; |
268 | if (!key_copy(&k->k->k, &k->p->k, &kf)) |
269 | die(EXIT_FAILURE, "unexpected failure while copying parameters"); |
270 | return (1); |
271 | } |
272 | |
273 | /* --- @getmp@ --- * |
274 | * |
275 | * Arguments: @key_data *k@ = pointer to key data block |
276 | * @const char *tag@ = tag string to use |
277 | * |
278 | * Returns: Pointer to multiprecision integer key item. |
279 | * |
280 | * Use: Fetches an MP key component. |
281 | */ |
282 | |
283 | static mp *getmp(key_data *k, const char *tag) |
284 | { |
285 | k = key_structfind(k, tag); |
286 | if (!k) |
287 | die(EXIT_FAILURE, "unexpected failure looking up subkey `%s'", tag); |
288 | if ((k->e & KF_ENCMASK) != KENC_MP) |
289 | die(EXIT_FAILURE, "subkey `%s' has an incompatible type"); |
290 | return (k->u.m); |
291 | } |
292 | |
052b36d0 |
293 | /* --- @keyrand@ --- * |
294 | * |
295 | * Arguments: @key_file *kf@ = pointer to key file |
296 | * @const char *id@ = pointer to key id (or null) |
297 | * |
298 | * Returns: --- |
299 | * |
300 | * Use: Keys the random number generator. |
301 | */ |
302 | |
303 | static void keyrand(key_file *kf, const char *id) |
304 | { |
305 | key *k; |
306 | |
307 | /* --- Find the key --- */ |
308 | |
309 | if (id) { |
310 | if ((k = key_bytag(kf, id)) == 0) |
311 | die(EXIT_FAILURE, "key `%s' not found", id); |
312 | } else |
313 | k = key_bytype(kf, "catacomb-rand"); |
314 | |
315 | if (k) { |
316 | key_data *kd = &k->k, kkd; |
317 | |
318 | again: |
319 | switch (kd->e & KF_ENCMASK) { |
320 | case KENC_BINARY: |
321 | break; |
322 | case KENC_ENCRYPT: { |
323 | dstr d = DSTR_INIT; |
324 | key_fulltag(k, &d); |
325 | if (key_punlock(d.buf, kd, &kkd)) |
326 | die(EXIT_FAILURE, "error unlocking key `%s'", d.buf); |
327 | dstr_destroy(&d); |
328 | kd = &kkd; |
329 | } goto again; |
330 | default: { |
331 | dstr d = DSTR_INIT; |
332 | key_fulltag(k, &d); |
333 | die(EXIT_FAILURE, "bad encoding type for key `%s'", d.buf); |
334 | } break; |
335 | } |
336 | |
337 | /* --- Key the generator --- */ |
338 | |
339 | rand_key(RAND_GLOBAL, kd->u.k.k, kd->u.k.sz); |
340 | if (kd == &kkd) |
341 | key_destroy(&kkd); |
342 | } |
343 | } |
344 | |
252c122d |
345 | /* --- Key generation algorithms --- */ |
346 | |
347 | static void alg_binary(keyopts *k) |
348 | { |
349 | unsigned sz; |
350 | unsigned m; |
351 | octet *p; |
352 | |
353 | if (!k->bits) |
354 | k->bits = 128; |
355 | if (k->p) |
356 | die(EXIT_FAILURE, "no shared parameters for binary keys"); |
357 | |
358 | sz = (k->bits + 7) >> 3; |
359 | p = sub_alloc(sz); |
360 | m = (1 << (((k->bits - 1) & 7) + 1)) - 1; |
9219a5d6 |
361 | rand_get(RAND_GLOBAL, p, sz); |
252c122d |
362 | *p &= m; |
363 | key_binary(&k->k->k, p, sz); |
364 | k->k->k.e |= KCAT_SYMM | KF_BURN; |
365 | memset(p, 0, sz); |
366 | sub_free(p, sz); |
367 | dolock(k, &k->k->k, 0); |
368 | } |
369 | |
370 | static void alg_des(keyopts *k) |
371 | { |
372 | unsigned sz; |
373 | octet *p; |
374 | int i; |
375 | |
376 | if (!k->bits) |
377 | k->bits = 112; |
378 | if (k->p) |
379 | die(EXIT_FAILURE, "no shared parameters for DES keys"); |
380 | if (k->bits % 56 || k->bits > 168) |
381 | die(EXIT_FAILURE, "DES keys must be 56, 112 or 168 bits long"); |
382 | |
383 | sz = k->bits / 7; |
384 | p = sub_alloc(sz); |
9219a5d6 |
385 | rand_get(RAND_GLOBAL, p, sz); /* Too much work done here! */ |
252c122d |
386 | for (i = 0; i < sz; i++) { |
052b36d0 |
387 | octet x = p[i] | 0x01; |
252c122d |
388 | x = x ^ (x >> 4); |
389 | x = x ^ (x >> 2); |
052b36d0 |
390 | x = x ^ (x >> 1); |
252c122d |
391 | p[i] = (p[i] & 0xfe) | (x & 0x01); |
392 | } |
393 | key_binary(&k->k->k, p, sz); |
394 | k->k->k.e |= KCAT_SYMM | KF_BURN; |
395 | memset(p, 0, sz); |
396 | sub_free(p, sz); |
397 | dolock(k, &k->k->k, 0); |
398 | } |
399 | |
400 | static void alg_rsa(keyopts *k) |
401 | { |
ab0ca95f |
402 | rsa_priv rp; |
252c122d |
403 | key_data *kd; |
404 | |
405 | /* --- Sanity checking --- */ |
406 | |
407 | if (k->p) |
408 | die(EXIT_FAILURE, "no shared parameters for RSA keys"); |
409 | if (!k->bits) |
410 | k->bits = 1024; |
411 | |
412 | /* --- Generate the RSA parameters --- */ |
413 | |
414 | if (rsa_gen(&rp, k->bits, &rand_global, 0, |
415 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
416 | die(EXIT_FAILURE, "RSA key generation failed"); |
417 | |
418 | /* --- Run a test encryption --- */ |
419 | |
420 | { |
421 | grand *g = fibrand_create(rand_global.ops->word(&rand_global)); |
ab0ca95f |
422 | rsa_pub rpp; |
252c122d |
423 | mp *m = mprand_range(MP_NEW, rp.n, g, 0); |
424 | mp *c; |
425 | |
ab0ca95f |
426 | rpp.n = rp.n; |
427 | rpp.e = rp.e; |
428 | c = rsa_qpubop(&rpp, MP_NEW, m); |
429 | c = rsa_qprivop(&rp, c, c, g); |
252c122d |
430 | |
4b536f42 |
431 | if (!MP_EQ(c, m)) |
252c122d |
432 | die(EXIT_FAILURE, "test encryption failed"); |
433 | mp_drop(c); |
434 | mp_drop(m); |
435 | g->ops->destroy(g); |
436 | } |
437 | |
438 | /* --- Allrighty then --- */ |
439 | |
440 | kd = &k->k->k; |
441 | key_structure(kd); |
442 | mpkey(kd, "n", rp.n, KCAT_PUB); |
443 | mpkey(kd, "e", rp.e, KCAT_PUB); |
444 | |
445 | kd = key_structcreate(kd, "private"); |
446 | key_structure(kd); |
447 | mpkey(kd, "d", rp.d, KCAT_PRIV | KF_BURN); |
448 | mpkey(kd, "p", rp.p, KCAT_PRIV | KF_BURN); |
449 | mpkey(kd, "q", rp.q, KCAT_PRIV | KF_BURN); |
450 | mpkey(kd, "q-inv", rp.q_inv, KCAT_PRIV | KF_BURN); |
451 | mpkey(kd, "d-mod-p", rp.dp, KCAT_PRIV | KF_BURN); |
452 | mpkey(kd, "d-mod-q", rp.dq, KCAT_PRIV | KF_BURN); |
453 | dolock(k, kd, "private"); |
454 | |
ab0ca95f |
455 | rsa_privfree(&rp); |
252c122d |
456 | } |
457 | |
458 | static void alg_dsaparam(keyopts *k) |
459 | { |
460 | static const char *pl[] = { "q", "p", "g", 0 }; |
461 | if (!copyparam(k, pl)) { |
462 | dsa_param dp; |
463 | octet *p; |
464 | size_t sz; |
465 | dstr d = DSTR_INIT; |
466 | base64_ctx c; |
467 | key_data *kd = &k->k->k; |
468 | |
469 | /* --- Choose appropriate bit lengths if necessary --- */ |
470 | |
471 | if (!k->qbits) |
472 | k->qbits = 160; |
473 | if (!k->bits) |
474 | k->bits = 768; |
475 | |
476 | /* --- Allocate a seed block --- */ |
477 | |
478 | sz = (k->qbits + 7) >> 3; |
479 | p = sub_alloc(sz); |
9219a5d6 |
480 | rand_get(RAND_GLOBAL, p, sz); |
252c122d |
481 | |
482 | /* --- Allocate the parameters --- */ |
483 | |
40d5a112 |
484 | if (dsa_gen(&dp, k->qbits, k->bits, 0, p, sz, |
485 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
252c122d |
486 | die(EXIT_FAILURE, "DSA parameter generation failed"); |
487 | |
488 | /* --- Store the parameters --- */ |
489 | |
490 | key_structure(kd); |
491 | mpkey(kd, "q", dp.q, KCAT_SHARE); |
492 | mpkey(kd, "p", dp.p, KCAT_SHARE); |
493 | mpkey(kd, "g", dp.g, KCAT_SHARE); |
494 | mp_drop(dp.q); |
495 | mp_drop(dp.p); |
496 | mp_drop(dp.g); |
497 | |
498 | /* --- Store the seed for future verification --- */ |
499 | |
500 | base64_init(&c); |
501 | c.maxline = 0; |
502 | c.indent = ""; |
503 | base64_encode(&c, p, sz, &d); |
504 | base64_encode(&c, 0, 0, &d); |
505 | key_putattr(k->kf, k->k, "seed", d.buf); |
506 | sub_free(p, sz); |
507 | dstr_destroy(&d); |
508 | } |
509 | } |
510 | |
511 | static void alg_dsa(keyopts *k) |
512 | { |
513 | mp *q, *p, *g; |
514 | mp *x, *y; |
515 | mpmont mm; |
516 | key_data *kd = &k->k->k; |
517 | |
518 | /* --- Get the shared parameters --- */ |
519 | |
520 | alg_dsaparam(k); |
521 | q = getmp(kd, "q"); |
522 | p = getmp(kd, "p"); |
523 | g = getmp(kd, "g"); |
524 | |
525 | /* --- Choose a private key --- */ |
526 | |
9219a5d6 |
527 | x = mprand_range(MP_NEWSEC, q, &rand_global, 0); |
252c122d |
528 | mpmont_create(&mm, p); |
529 | y = mpmont_exp(&mm, MP_NEW, g, x); |
530 | |
531 | /* --- Store everything away --- */ |
532 | |
533 | mpkey(kd, "y", y, KCAT_PUB); |
534 | |
535 | kd = key_structcreate(kd, "private"); |
536 | key_structure(kd); |
537 | mpkey(kd, "x", x, KCAT_PRIV | KF_BURN); |
538 | dolock(k, kd, "private"); |
052b36d0 |
539 | |
540 | mp_drop(x); mp_drop(y); |
252c122d |
541 | } |
542 | |
543 | static void alg_dhparam(keyopts *k) |
544 | { |
052b36d0 |
545 | static const char *pl[] = { "p", "q", "g", 0 }; |
252c122d |
546 | if (!copyparam(k, pl)) { |
052b36d0 |
547 | dh_param dp; |
252c122d |
548 | key_data *kd = &k->k->k; |
40d5a112 |
549 | int rc; |
252c122d |
550 | |
551 | if (!k->bits) |
552 | k->bits = 1024; |
553 | |
554 | /* --- Choose a large safe prime number --- */ |
555 | |
40d5a112 |
556 | if (k->f & f_limlee) { |
557 | mp **f; |
558 | size_t nf; |
559 | if (!k->qbits) |
560 | k->qbits = 256; |
561 | rc = dh_limlee(&dp, k->qbits, k->bits, |
562 | (k->f & f_subgroup) ? DH_SUBGROUP : 0, |
563 | 0, &rand_global, (k->f & f_quiet) ? 0 : pgen_ev, 0, |
564 | (k->f & f_quiet) ? 0 : pgen_evspin, 0, &nf, &f); |
565 | if (!rc) { |
566 | dstr d = DSTR_INIT; |
567 | size_t i; |
568 | for (i = 0; i < nf; i++) { |
569 | if (i) |
570 | dstr_puts(&d, ", "); |
571 | mp_writedstr(f[i], &d, 10); |
572 | mp_drop(f[i]); |
573 | } |
574 | key_putattr(k->kf, k->k, "factors", d.buf); |
575 | dstr_destroy(&d); |
576 | } |
577 | } else |
578 | rc = dh_gen(&dp, k->qbits, k->bits, 0, &rand_global, |
579 | (k->f & f_quiet) ? 0 : pgen_ev, 0); |
580 | |
581 | if (rc) |
252c122d |
582 | die(EXIT_FAILURE, "Diffie-Hellman parameter generation failed"); |
583 | |
584 | key_structure(kd); |
052b36d0 |
585 | mpkey(kd, "p", dp.p, KCAT_SHARE); |
586 | mpkey(kd, "q", dp.q, KCAT_SHARE); |
587 | mpkey(kd, "g", dp.g, KCAT_SHARE); |
588 | mp_drop(dp.q); |
589 | mp_drop(dp.p); |
590 | mp_drop(dp.g); |
252c122d |
591 | } |
592 | } |
593 | |
594 | static void alg_dh(keyopts *k) |
595 | { |
596 | mp *x, *y; |
052b36d0 |
597 | mp *p, *q, *g; |
252c122d |
598 | mpmont mm; |
599 | key_data *kd = &k->k->k; |
600 | |
601 | /* --- Get the shared parameters --- */ |
602 | |
603 | alg_dhparam(k); |
604 | p = getmp(kd, "p"); |
052b36d0 |
605 | q = getmp(kd, "q"); |
252c122d |
606 | g = getmp(kd, "g"); |
607 | |
608 | /* --- Choose a suitable private key --- * |
609 | * |
610 | * Since %$g$% has order %$q$%, choose %$x < q$%. |
611 | */ |
612 | |
9219a5d6 |
613 | x = mprand_range(MP_NEWSEC, q, &rand_global, 0); |
252c122d |
614 | |
615 | /* --- Compute the public key %$y = g^x \bmod p$% --- */ |
616 | |
617 | mpmont_create(&mm, p); |
052b36d0 |
618 | y = mpmont_exp(&mm, MP_NEW, g, x); |
252c122d |
619 | mpmont_destroy(&mm); |
620 | |
621 | /* --- Store everything away --- */ |
622 | |
623 | mpkey(kd, "y", y, KCAT_PUB); |
624 | |
625 | kd = key_structcreate(kd, "private"); |
626 | key_structure(kd); |
627 | mpkey(kd, "x", x, KCAT_PRIV | KF_BURN); |
628 | dolock(k, kd, "private"); |
052b36d0 |
629 | |
630 | mp_drop(x); mp_drop(y); |
252c122d |
631 | } |
632 | |
633 | static void alg_bbs(keyopts *k) |
634 | { |
ab0ca95f |
635 | bbs_priv bp; |
252c122d |
636 | key_data *kd; |
252c122d |
637 | |
638 | /* --- Sanity checking --- */ |
639 | |
640 | if (k->p) |
641 | die(EXIT_FAILURE, "no shared parameters for Blum-Blum-Shub keys"); |
642 | if (!k->bits) |
643 | k->bits = 1024; |
644 | |
645 | /* --- Generate the BBS parameters --- */ |
646 | |
052b36d0 |
647 | if (bbs_gen(&bp, k->bits, &rand_global, 0, |
648 | (k->f & f_quiet) ? 0 : pgen_ev, 0)) |
252c122d |
649 | die(EXIT_FAILURE, "Blum-Blum-Shub key generation failed"); |
252c122d |
650 | |
651 | /* --- Allrighty then --- */ |
652 | |
653 | kd = &k->k->k; |
654 | key_structure(kd); |
655 | mpkey(kd, "n", bp.n, KCAT_PUB); |
656 | |
657 | kd = key_structcreate(kd, "private"); |
658 | key_structure(kd); |
659 | mpkey(kd, "p", bp.p, KCAT_PRIV | KF_BURN); |
660 | mpkey(kd, "q", bp.q, KCAT_PRIV | KF_BURN); |
661 | dolock(k, kd, "private"); |
662 | |
ab0ca95f |
663 | bbs_privfree(&bp); |
252c122d |
664 | } |
665 | |
666 | /* --- The algorithm tables --- */ |
667 | |
668 | typedef struct keyalg { |
669 | const char *name; |
670 | void (*proc)(keyopts *o); |
671 | const char *help; |
672 | } keyalg; |
673 | |
674 | static keyalg algtab[] = { |
675 | { "binary", alg_binary, "Plain binary data" }, |
676 | { "des", alg_des, "Binary with DES-style parity" }, |
677 | { "rsa", alg_rsa, "RSA public-key encryption" }, |
678 | { "dsa", alg_dsa, "DSA digital signatures" }, |
679 | { "dsa-param", alg_dsaparam, "DSA shared parameters" }, |
680 | { "dh", alg_dh, "Diffie-Hellman key exchange" }, |
681 | { "dh-param", alg_dhparam, "Diffie-Hellman parameters" }, |
682 | { "bbs", alg_bbs, "Blum-Blum-Shub generator" }, |
683 | { 0, 0 } |
684 | }; |
d03ab969 |
685 | |
686 | /* --- @cmd_add@ --- */ |
687 | |
688 | static int cmd_add(int argc, char *argv[]) |
689 | { |
690 | key_file f; |
d03ab969 |
691 | time_t exp = KEXP_EXPIRE; |
252c122d |
692 | const char *tag = 0, *ptag = 0; |
d03ab969 |
693 | const char *c = 0; |
252c122d |
694 | keyalg *alg = algtab; |
052b36d0 |
695 | const char *rtag = 0; |
252c122d |
696 | keyopts k = { 0, 0, DSTR_INIT, 0, 0, 0, 0 }; |
d03ab969 |
697 | |
698 | /* --- Parse options for the subcommand --- */ |
699 | |
700 | for (;;) { |
701 | static struct option opt[] = { |
252c122d |
702 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, |
d03ab969 |
703 | { "bits", OPTF_ARGREQ, 0, 'b' }, |
252c122d |
704 | { "qbits", OPTF_ARGREQ, 0, 'B' }, |
705 | { "parameters", OPTF_ARGREQ, 0, 'p' }, |
d03ab969 |
706 | { "expire", OPTF_ARGREQ, 0, 'e' }, |
707 | { "comment", OPTF_ARGREQ, 0, 'c' }, |
252c122d |
708 | { "tag", OPTF_ARGREQ, 0, 't' }, |
052b36d0 |
709 | { "rand-id", OPTF_ARGREQ, 0, 'r' }, |
252c122d |
710 | { "lock", 0, 0, 'l' }, |
711 | { "quiet", 0, 0, 'q' }, |
40d5a112 |
712 | { "lim-lee", 0, 0, 'L' }, |
713 | { "subgroup", 0, 0, 'S' }, |
d03ab969 |
714 | { 0, 0, 0, 0 } |
715 | }; |
40d5a112 |
716 | int i = mdwopt(argc, argv, "+a:b:B:p:e:c:t:r:lqLS", opt, 0, 0, 0); |
d03ab969 |
717 | if (i < 0) |
718 | break; |
719 | |
720 | /* --- Handle the various options --- */ |
721 | |
722 | switch (i) { |
723 | |
252c122d |
724 | /* --- Read an algorithm name --- */ |
725 | |
726 | case 'a': { |
727 | keyalg *a; |
728 | size_t sz = strlen(optarg); |
729 | |
730 | if (strcmp(optarg, "list") == 0) { |
731 | for (a = algtab; a->name; a++) |
732 | printf("%-10s %s\n", a->name, a->help); |
733 | return (0); |
734 | } |
735 | |
736 | alg = 0; |
737 | for (a = algtab; a->name; a++) { |
738 | if (strncmp(optarg, a->name, sz) == 0) { |
739 | if (a->name[sz] == 0) { |
740 | alg = a; |
741 | break; |
742 | } else if (alg) |
743 | die(EXIT_FAILURE, "ambiguous algorithm name `%s'", optarg); |
744 | else |
745 | alg = a; |
746 | } |
747 | } |
748 | if (!alg) |
749 | die(EXIT_FAILURE, "unknown algorithm name `%s'", optarg); |
750 | } break; |
751 | |
d03ab969 |
752 | /* --- Bits must be nonzero and a multiple of 8 --- */ |
753 | |
252c122d |
754 | case 'b': { |
755 | char *p; |
756 | k.bits = strtoul(optarg, &p, 0); |
757 | if (k.bits == 0 || *p != 0) |
758 | die(EXIT_FAILURE, "bad bitlength `%s'", optarg); |
759 | } break; |
760 | |
761 | case 'B': { |
762 | char *p; |
763 | k.qbits = strtoul(optarg, &p, 0); |
764 | if (k.qbits == 0 || *p != 0) |
765 | die(EXIT_FAILURE, "bad bitlength `%s'", optarg); |
766 | } break; |
767 | |
768 | /* --- Parameter selection --- */ |
769 | |
770 | case 'p': |
771 | ptag = optarg; |
d03ab969 |
772 | break; |
773 | |
774 | /* --- Expiry dates get passed to @get_date@ for parsing --- */ |
775 | |
776 | case 'e': |
252c122d |
777 | if (strncmp(optarg, "forever", strlen(optarg)) == 0) |
d03ab969 |
778 | exp = KEXP_FOREVER; |
779 | else { |
780 | exp = get_date(optarg, 0); |
781 | if (exp == -1) |
252c122d |
782 | die(EXIT_FAILURE, "bad expiry date `%s'", optarg); |
d03ab969 |
783 | } |
784 | break; |
785 | |
786 | /* --- Store comments without interpretation --- */ |
787 | |
788 | case 'c': |
252c122d |
789 | if (key_chkcomment(optarg)) |
790 | die(EXIT_FAILURE, "bad comment string `%s'", optarg); |
d03ab969 |
791 | c = optarg; |
792 | break; |
793 | |
252c122d |
794 | /* --- Store tags --- */ |
795 | |
796 | case 't': |
797 | if (key_chkident(optarg)) |
798 | die(EXIT_FAILURE, "bad tag string `%s'", optarg); |
799 | tag = optarg; |
800 | break; |
801 | |
802 | /* --- Other flags --- */ |
803 | |
052b36d0 |
804 | case 'r': |
805 | rtag = optarg; |
806 | break; |
252c122d |
807 | case 'l': |
808 | k.f |= f_lock; |
809 | break; |
810 | case 'q': |
811 | k.f |= f_quiet; |
812 | break; |
40d5a112 |
813 | case 'L': |
814 | k.f |= f_limlee; |
815 | break; |
816 | case 'S': |
817 | k.f |= f_subgroup; |
818 | break; |
252c122d |
819 | |
d03ab969 |
820 | /* --- Other things are bogus --- */ |
821 | |
822 | default: |
252c122d |
823 | k.f |= f_bogus; |
d03ab969 |
824 | break; |
825 | } |
826 | } |
827 | |
252c122d |
828 | /* --- Various sorts of bogosity --- */ |
d03ab969 |
829 | |
252c122d |
830 | if ((k.f & f_bogus) || optind + 1 > argc) { |
d03ab969 |
831 | die(EXIT_FAILURE, |
252c122d |
832 | "Usage: add [options] type [attr...]"); |
d03ab969 |
833 | } |
252c122d |
834 | if (key_chkident(argv[optind])) |
835 | die(EXIT_FAILURE, "bad key type `%s'", argv[optind]); |
836 | |
837 | /* --- Set up various bits of the state --- */ |
838 | |
d03ab969 |
839 | if (exp == KEXP_EXPIRE) |
840 | exp = time(0) + 14 * 24 * 60 * 60; |
841 | |
252c122d |
842 | /* --- Open the file and create the basic key block --- * |
843 | * |
844 | * Keep on generating keyids until one of them doesn't collide. |
845 | */ |
d03ab969 |
846 | |
252c122d |
847 | doopen(&f, KOPEN_WRITE); |
848 | k.kf = &f; |
849 | |
052b36d0 |
850 | /* --- Key the generator --- */ |
851 | |
852 | keyrand(&f, rtag); |
853 | |
252c122d |
854 | for (;;) { |
855 | uint32 id = rand_global.ops->word(&rand_global); |
856 | int err; |
857 | k.k = key_new(&f, id, argv[optind], exp, &err); |
858 | if (k.k) |
859 | break; |
860 | if (err != KERR_DUPID) |
861 | die(EXIT_FAILURE, "error adding new key: %s", key_strerror(err)); |
862 | } |
d03ab969 |
863 | |
252c122d |
864 | /* --- Set various simple attributes --- */ |
d03ab969 |
865 | |
252c122d |
866 | if (tag) { |
867 | int err = key_settag(&f, k.k, tag); |
868 | if (err) |
869 | die(EXIT_FAILURE, "error setting key tag: %s", key_strerror(err)); |
870 | } |
d03ab969 |
871 | |
252c122d |
872 | if (c) { |
873 | int err = key_setcomment(&f, k.k, c); |
874 | if (err) |
875 | die(EXIT_FAILURE, "error setting key comment: %s", key_strerror(err)); |
876 | } |
877 | |
878 | setattr(&f, k.k, argv + optind + 1); |
879 | |
880 | key_fulltag(k.k, &k.tag); |
881 | |
882 | /* --- Find the parameter key --- */ |
883 | |
884 | if (ptag) { |
885 | if ((k.p = key_bytag(&f, ptag)) == 0) |
886 | die(EXIT_FAILURE, "parameter key `%s' not found", ptag); |
887 | if ((k.p->k.e & KF_ENCMASK) != KENC_STRUCT) |
888 | die(EXIT_FAILURE, "parameter key `%s' is not structured", ptag); |
889 | } |
890 | |
891 | /* --- Now generate the actual key data --- */ |
892 | |
893 | alg->proc(&k); |
894 | |
895 | /* --- Done --- */ |
d03ab969 |
896 | |
d03ab969 |
897 | doclose(&f); |
898 | return (0); |
899 | } |
900 | |
252c122d |
901 | /*----- Key listing -------------------------------------------------------*/ |
902 | |
903 | /* --- Listing options --- */ |
904 | |
905 | typedef struct listopts { |
906 | const char *tfmt; /* Date format (@strftime@-style) */ |
907 | int v; /* Verbosity level */ |
908 | unsigned f; /* Various flags */ |
909 | time_t t; /* Time now (for key expiry) */ |
910 | key_filter kf; /* Filter for matching keys */ |
911 | } listopts; |
912 | |
913 | /* --- Listing flags --- */ |
914 | |
915 | enum { |
916 | f_newline = 2, /* Write newline before next entry */ |
917 | f_attr = 4, /* Written at least one attribute */ |
918 | f_utc = 8 /* Emit UTC time, not local time */ |
919 | }; |
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 | |
1630 | enum { |
1631 | f_bogus = 1 |
1632 | }; |
1633 | |
1634 | /* --- Initialization --- */ |
1635 | |
1636 | ego(argv[0]); |
1637 | sub_init(); |
1638 | |
1639 | /* --- Parse command line options --- */ |
1640 | |
1641 | for (;;) { |
1642 | static struct option opt[] = { |
1643 | |
1644 | /* --- Standard GNUy help options --- */ |
1645 | |
1646 | { "help", 0, 0, 'h' }, |
1647 | { "version", 0, 0, 'v' }, |
1648 | { "usage", 0, 0, 'u' }, |
1649 | |
1650 | /* --- Real live useful options --- */ |
1651 | |
1652 | { "keyring", OPTF_ARGREQ, 0, 'k' }, |
052b36d0 |
1653 | { "id", OPTF_ARGREQ, 0, 'i' }, |
1654 | { "type", OPTF_ARGREQ, 0, 't' }, |
d03ab969 |
1655 | |
1656 | /* --- Magic terminator --- */ |
1657 | |
1658 | { 0, 0, 0, 0 } |
1659 | }; |
052b36d0 |
1660 | int i = mdwopt(argc, argv, "+hvu k:i:t:", opt, 0, 0, 0); |
d03ab969 |
1661 | |
1662 | if (i < 0) |
1663 | break; |
1664 | switch (i) { |
1665 | |
1666 | /* --- GNU help options --- */ |
1667 | case 'h': |
1668 | help(stdout); |
1669 | exit(0); |
1670 | case 'v': |
1671 | version(stdout); |
1672 | exit(0); |
1673 | case 'u': |
1674 | usage(stdout); |
1675 | exit(0); |
1676 | |
1677 | /* --- Real useful options --- */ |
1678 | |
1679 | case 'k': |
1680 | keyfile = optarg; |
1681 | break; |
1682 | |
1683 | /* --- Bogosity --- */ |
1684 | |
1685 | default: |
1686 | f |= f_bogus; |
1687 | break; |
1688 | } |
1689 | } |
1690 | |
1691 | /* --- Complain about excessive bogons --- */ |
1692 | |
1693 | if (f & f_bogus || optind == argc) { |
1694 | usage(stderr); |
1695 | exit(1); |
1696 | } |
1697 | |
052b36d0 |
1698 | /* --- Initialize the Catacomb random number generator --- */ |
1699 | |
052b36d0 |
1700 | rand_noisesrc(RAND_GLOBAL, &noise_source); |
9219a5d6 |
1701 | rand_seed(RAND_GLOBAL, 160); |
052b36d0 |
1702 | |
d03ab969 |
1703 | /* --- Dispatch to appropriate command handler --- */ |
1704 | |
1705 | argc -= optind; |
1706 | argv += optind; |
1707 | optind = 0; |
1708 | |
1709 | { |
1710 | cmd *c, *chosen = 0; |
1711 | size_t sz = strlen(argv[0]); |
1712 | |
1713 | for (c = cmds; c->name; c++) { |
1714 | if (strncmp(argv[0], c->name, sz) == 0) { |
1715 | if (c->name[sz] == 0) { |
1716 | chosen = c; |
1717 | break; |
1718 | } else if (chosen) |
1719 | die(EXIT_FAILURE, "ambiguous command name `%s'", argv[0]); |
1720 | else |
1721 | chosen = c; |
1722 | } |
1723 | } |
1724 | if (!chosen) |
1725 | die(EXIT_FAILURE, "unknown command name `%s'", argv[0]); |
1726 | return (chosen->cmd(argc, argv)); |
1727 | } |
1728 | } |
1729 | |
1730 | /*----- That's all, folks -------------------------------------------------*/ |