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