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