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