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