chiark / gitweb /
admin, resolve ops bugfix: initialize tag in resop early.
[tripe] / keymgmt.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
3cdc3f3a 3 * $Id$
410c8acf 4 *
5 * Key loading and storing
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * TrIPE 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 General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
410c8acf 29/*----- Header files ------------------------------------------------------*/
30
31#include "tripe.h"
32
33/*----- Global variables --------------------------------------------------*/
34
52c03a2a 35group *gg;
36mp *kpriv;
b5c45da1 37algswitch algs;
410c8acf 38
39/*----- Static variables --------------------------------------------------*/
40
41static key_file *kf_pub;
42static const char *kr_priv, *kr_pub, *tag_priv;
43static fwatch w_priv, w_pub;
44
52c03a2a 45/*----- Key groups --------------------------------------------------------*/
46
47typedef struct kgops {
48 const char *ty;
49 const char *(*loadpriv)(key_data *, group **, mp **, dstr *);
50 const char *(*loadpub)(key_data *, group **, ge **, dstr *);
51} kgops;
52
53/* --- Diffie-Hellman --- */
54
55static const char *kgdh_priv(key_data *kd, group **g, mp **x, dstr *t)
56{
57 key_packstruct kps[DH_PRIVFETCHSZ];
58 key_packdef *kp;
59 dh_priv dp;
60 const char *e;
61 int rc;
62
63 kp = key_fetchinit(dh_privfetch, kps, &dp);
64 if ((rc = key_unpack(kp, kd, t)) != 0) {
65 e = key_strerror(rc);
66 goto done;
67 }
68 *g = group_prime(&dp.dp);
69 *x = MP_COPY(dp.x);
70 e = 0;
71done:
72 key_fetchdone(kp);
73 return (e);
74}
75
76static const char *kgdh_pub(key_data *kd, group **g, ge **p, dstr *t)
77{
78 key_packstruct kps[DH_PUBFETCHSZ];
79 key_packdef *kp;
80 dh_pub dp;
81 const char *e;
82 int rc;
83
84 kp = key_fetchinit(dh_pubfetch, kps, &dp);
85 if ((rc = key_unpack(kp, kd, t)) != 0) {
86 e = key_strerror(rc);
87 goto done;
88 }
89 *g = group_prime(&dp.dp);
90 *p = G_CREATE(*g);
91 if (G_FROMINT(*g, *p, dp.y)) {
92 e = "bad public value";
93 goto done;
94 }
95 e = 0;
96done:
97 key_fetchdone(kp);
98 return (e);
99}
100
101static const kgops kgdh_ops = { "tripe-dh", kgdh_priv, kgdh_pub };
102
103/* --- Elliptic curve --- */
104
105static const char *kgec_priv(key_data *kd, group **g, mp **x, dstr *t)
106{
107 key_packstruct kps[EC_PRIVFETCHSZ];
108 key_packdef *kp;
109 ec_priv ep;
110 ec_info ei;
111 const char *e;
112 int rc;
113
114 kp = key_fetchinit(ec_privfetch, kps, &ep);
115 if ((rc = key_unpack(kp, kd, t)) != 0) {
116 e = key_strerror(rc);
117 goto done;
118 }
119 if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
120 goto done;
121 *g = group_ec(&ei);
122 *x = MP_COPY(ep.x);
123 e = 0;
124done:
125 key_fetchdone(kp);
126 return (e);
127}
128
129static const char *kgec_pub(key_data *kd, group **g, ge **p, dstr *t)
130{
131 key_packstruct kps[EC_PUBFETCHSZ];
132 key_packdef *kp;
133 ec_pub ep;
134 ec_info ei;
135 const char *e;
136 int rc;
137
138 kp = key_fetchinit(ec_pubfetch, kps, &ep);
139 if ((rc = key_unpack(kp, kd, t)) != 0) {
140 e = key_strerror(rc);
141 goto done;
142 }
143 if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
144 goto done;
145 *g = group_ec(&ei);
146 *p = G_CREATE(*g);
147 if (G_FROMEC(*g, *p, &ep.p)) {
148 e = "bad public point";
149 goto done;
150 }
151 e = 0;
152done:
153 key_fetchdone(kp);
154 return (e);
155}
156
157static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
158
159/* --- Table of supported key types --- */
160
161static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
162
b5c45da1 163/*----- Algswitch stuff ---------------------------------------------------*/
164
165/* --- @algs_get@ --- *
166 *
167 * Arguments: @algswitch *a@ = where to put the algorithms
168 * @key_file *kf@ = key file (for some stupid reason)
169 * @key *k@ = key to inspect
170 *
171 * Returns: Null if OK, or an error message.
172 *
173 * Use: Extracts an algorithm choice from a key.
174 */
175
176static const char *algs_get(algswitch *a, key_file *kf, key *k)
177{
178 const char *p;
179 char *q;
180 dstr d = DSTR_INIT;
181 const char *e;
182
183#define FAIL(msg) do { e = msg; goto done; } while (0)
184
185 if ((p = key_getattr(kf, k, "cipher")) == 0)
186 p = "blowfish-cbc";
187 if ((a->c = gcipher_byname(p)) == 0)
3cdc3f3a 188 FAIL("unknown-cipher");
b5c45da1 189
190 if ((p = key_getattr(kf, k, "hash")) == 0)
191 p = "rmd160";
192 if ((a->h = ghash_byname(p)) == 0)
3cdc3f3a 193 FAIL("unknown-hash");
b5c45da1 194
74ee77cb 195 if ((p = key_getattr(kf, k, "mgf")) == 0) {
b5c45da1 196 dstr_reset(&d);
74ee77cb 197 dstr_putf(&d, "%s-mgf", a->h->name);
b5c45da1 198 p = d.buf;
199 }
200 if ((a->mgf = gcipher_byname(p)) == 0)
3cdc3f3a 201 FAIL("unknown-mgf-cipher");
b5c45da1 202
203 if ((p = key_getattr(kf, k, "mac")) != 0) {
204 dstr_reset(&d);
205 dstr_puts(&d, p);
206 if ((q = strchr(d.buf, '/')) != 0)
207 *q++ = 0;
208 if ((a->m = gmac_byname(d.buf)) == 0)
3cdc3f3a 209 FAIL("unknown-mac");
b5c45da1 210 if (!q)
211 a->tagsz = a->m->hashsz;
212 else {
213 unsigned long n = strtoul(q, &q, 0);
3cdc3f3a 214 if (*q) FAIL("bad-tag-length-string");
215 if (n%8 || n > ~(size_t)0) FAIL("bad-tag-length");
b5c45da1 216 a->tagsz = n/8;
217 }
218 } else {
219 dstr_reset(&d);
220 dstr_putf(&d, "%s-hmac", a->h->name);
221 if ((a->m = gmac_byname(d.buf)) == 0)
3cdc3f3a 222 FAIL("no-hmac-for-hash");
b5c45da1 223 a->tagsz = a->h->hashsz/2;
224 }
225
226 e = 0;
227done:
228 dstr_destroy(&d);
229 return (e);
230}
231
232/* --- @algs_check@ --- *
233 *
234 * Arguments: @algswitch *a@ = a choice of algorithms
235 * @const group *g@ = the group we're working in
236 *
237 * Returns: Null if OK, or an error message.
238 *
239 * Use: Checks an algorithm choice for sensibleness. This also
240 * derives some useful information from the choices, and you
241 * must call this before committing the algorithm selection
242 * for use by @keyset@ functions.
243 */
244
245static const char *algs_check(algswitch *a, const group *g)
246{
247 /* --- Derive the key sizes --- *
248 *
249 * Must ensure that we have non-empty keys. This isn't ideal, but it
250 * provides a handy sanity check.
251 */
252
253 a->hashsz = a->h->hashsz;
254 if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0)
255 return ("no key size found for cipher");
256 if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0)
257 return ("no key size found for MAC");
258
259 /* --- Ensure that the tag size is sane --- */
260
261 if (a->tagsz > a->m->hashsz) return ("tag length too large");
262
263 /* --- Ensure the MGF accepts hashes as keys --- */
264
265 if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz)
266 return ("MGF not suitable -- restrictive key schedule");
267
268 /* --- All ship-shape and Bristol-fashion --- */
269
270 return (0);
271}
272
273/* --- @algs_samep@ --- *
274 *
275 * Arguments: @const algswitch *a, *aa@ = two algorithm selections
276 *
277 * Returns: Nonzero if the two selections are the same.
278 *
279 * Use: Checks sameness of algorithm selections: used to ensure that
280 * peers are using sensible algorithms.
281 */
282
283static int algs_samep(const algswitch *a, const algswitch *aa)
284{
285 return (a->c == aa->c && a->mgf == aa->mgf && a->h == aa->h &&
286 a->m == aa->m && a->tagsz == aa->tagsz);
287}
288
410c8acf 289/*----- Main code ---------------------------------------------------------*/
290
291/* --- @keymoan@ --- *
292 *
56814747 293 * Arguments: @const char *file@ = name of the file
410c8acf 294 * @int line@ = line number in file
295 * @const char *msg@ = error message
296 * @void *p@ = argument pointer
297 *
298 * Returns: ---
299 *
300 * Use: Reports an error message about loading a key file.
301 */
302
303static void keymoan(const char *file, int line, const char *msg, void *p)
f43df819
MW
304{
305 a_warn("KEYMGMT",
306 "key-file-error",
307 "%s:%i", file, line,
308 "%s", msg,
309 A_END);
310}
410c8acf 311
312/* --- @loadpriv@ --- *
313 *
314 * Arguments: @dstr *d@ = string to write errors in
410c8acf 315 *
316 * Returns: Zero if OK, nonzero on error.
317 *
318 * Use: Loads the private key from its keyfile.
319 */
320
52c03a2a 321static int loadpriv(dstr *d)
410c8acf 322{
323 key_file kf;
52c03a2a 324 key *k;
d59d4fe9 325 key_data **kd;
52c03a2a 326 dstr t = DSTR_INIT;
327 group *g = 0;
328 mp *x = 0;
329 int rc = -1;
330 const kgops **ko;
331 const char *e;
b5c45da1 332 algswitch a;
52c03a2a 333
334 /* --- Open the private key file --- */
410c8acf 335
336 if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, 0)) {
337 dstr_putf(d, "error reading private keyring `%s': %s",
338 kr_priv, strerror(errno));
52c03a2a 339 goto done_0;
340 }
341
342 /* --- Find the private key --- */
343
344 if (key_qtag(&kf, tag_priv, &t, &k, &kd)) {
345 dstr_putf(d, "private key `%s' not found in keyring `%s'",
346 tag_priv, kr_priv);
347 goto done_1;
348 }
349
350 /* --- Look up the key type in the table --- */
351
352 for (ko = kgtab; *ko; ko++) {
353 if (strcmp((*ko)->ty, k->type) == 0)
354 goto tymatch;
355 }
356 dstr_putf(d, "private key `%s' has unknown type `%s'", t.buf, k->type);
357 goto done_1;
358tymatch:;
359
360 /* --- Load the key --- */
361
d59d4fe9 362 if ((e = (*ko)->loadpriv(*kd, &g, &x, &t)) != 0) {
52c03a2a 363 dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
364 goto done_1;
365 }
366
367 /* --- Check that the key is sensible --- */
368
369 if ((e = G_CHECK(g, &rand_global)) != 0) {
370 dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
371 goto done_1;
372 }
373
b5c45da1 374 /* --- Collect the algorithms --- */
375
376 if ((e = algs_get(&a, &kf, k)) != 0 ||
377 (e = algs_check(&a, g)) != 0) {
378 dstr_putf(d, "bad symmetric algorithm selection in private key `%s': %s",
379 t.buf, e);
380 goto done_1;
381 }
382
52c03a2a 383 /* --- Good, we're happy --- *
384 *
385 * Dodginess! We change the group over here, but don't free any old group
386 * elements. This assumes that the new group is basically the same as the
387 * old one, and will happily adopt the existing elements. If it isn't,
388 * then we lose badly. Check this, then.
389 */
390
391 if (gg) {
392 if (!group_samep(g, gg)) {
393 dstr_putf(d, "private key `%s' has different group", t.buf);
394 goto done_1;
410c8acf 395 }
52c03a2a 396 G_DESTROYGROUP(gg);
410c8acf 397 }
52c03a2a 398 if (kpriv)
399 mp_drop(kpriv);
400
401 /* --- Dump out the group --- */
402
403 IF_TRACING(T_KEYMGMT, {
404 trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", t.buf);
405 IF_TRACING(T_CRYPTO, {
406 trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
407 trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
408 trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
b5c45da1 409 trace(T_CRYPTO, "crypto: cipher = %s", a.c->name);
410 trace(T_CRYPTO, "crypto: mgf = %s", a.mgf->name);
411 trace(T_CRYPTO, "crypto: hash = %s", a.h->name);
412 trace(T_CRYPTO, "crypto: mac = %s/%lu",
413 a.m->name, (unsigned long)a.tagsz * 8);
52c03a2a 414 })
415 })
416
417 /* --- Success! --- */
418
419 gg = g; g = 0;
b5c45da1 420 algs = a;
52c03a2a 421 kpriv = x; x = 0;
422 rc = 0;
423
424 /* --- Tidy up --- */
425
426done_1:
427 key_close(&kf);
428done_0:
429 dstr_destroy(&t);
430 if (x) mp_drop(x);
431 if (g) G_DESTROYGROUP(g);
410c8acf 432 return (rc);
433}
434
435/* --- @loadpub@ --- *
436 *
437 * Arguments: @dstr *d@ = string to write errors to
438 *
439 * Returns: Zero if OK, nonzero on error.
440 *
441 * Use: Reloads the public keyring.
442 */
443
444static int loadpub(dstr *d)
445{
446 key_file *kf = CREATE(key_file);
447
448 if (key_open(kf, kr_pub, KOPEN_READ, keymoan, 0)) {
449 dstr_putf(d, "error reading public keyring `%s': %s",
450 kr_pub, strerror(errno));
451 DESTROY(kf);
452 return (-1);
453 }
454 kf_pub = kf;
455 T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
456 return (0);
457}
458
de014da6 459/* --- @km_reload@ --- *
410c8acf 460 *
461 * Arguments: ---
462 *
463 * Returns: Zero if OK, nonzero to force reloading of keys.
464 *
de014da6 465 * Use: Checks the keyrings to see if they need reloading.
410c8acf 466 */
467
de014da6 468int km_reload(void)
410c8acf 469{
470 dstr d = DSTR_INIT;
410c8acf 471 key_file *kf;
472 int reload = 0;
473
474 /* --- Check the private key first --- */
475
476 if (fwatch_update(&w_priv, kr_priv)) {
477 T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
478 DRESET(&d);
52c03a2a 479 if (loadpriv(&d))
f43df819 480 a_warn("KEYMGMT", "bad-private-key", "%s", d.buf, A_END);
52c03a2a 481 else
410c8acf 482 reload = 1;
410c8acf 483 }
484
485 /* --- Now check the public keys --- */
486
487 if (fwatch_update(&w_pub, kr_pub)) {
488 T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
489 kf = kf_pub;
490 DRESET(&d);
491 if (loadpub(&d))
f43df819 492 a_warn("KEYMGMT", "bad-public-keyring", "%s", d.buf, A_END);
410c8acf 493 else {
494 reload = 1;
495 key_close(kf);
496 DESTROY(kf);
497 }
498 }
499
500 /* --- Done --- */
501
502 return (reload);
503}
504
505/* --- @km_init@ --- *
506 *
507 * Arguments: @const char *priv@ = private keyring file
508 * @const char *pub@ = public keyring file
509 * @const char *tag@ = tag to load
510 *
511 * Returns: ---
512 *
513 * Use: Initializes, and loads the private key.
514 */
515
516void km_init(const char *priv, const char *pub, const char *tag)
517{
518 dstr d = DSTR_INIT;
b5c45da1 519 const gchash *const *hh;
410c8acf 520
521 kr_priv = priv;
522 kr_pub = pub;
523 tag_priv = tag;
524 fwatch_init(&w_priv, kr_priv);
525 fwatch_init(&w_pub, kr_pub);
526
b5c45da1 527 for (hh = ghashtab; *hh; hh++) {
528 if ((*hh)->hashsz > MAXHASHSZ) {
529 die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
530 (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
531 }
532 }
533
410c8acf 534 DRESET(&d);
52c03a2a 535 if (loadpriv(&d))
410c8acf 536 die(EXIT_FAILURE, "%s", d.buf);
410c8acf 537 if (loadpub(&d))
538 die(EXIT_FAILURE, "%s", d.buf);
539}
540
541/* --- @km_getpubkey@ --- *
542 *
543 * Arguments: @const char *tag@ = public key tag to load
52c03a2a 544 * @ge *kpub@ = where to put the public key
00e64b67 545 * @time_t *t_exp@ = where to put the expiry time
410c8acf 546 *
547 * Returns: Zero if OK, nonzero if it failed.
548 *
549 * Use: Fetches a public key from the keyring.
550 */
551
52c03a2a 552int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
410c8acf 553{
00e64b67 554 key *k;
d59d4fe9 555 key_data **kd;
52c03a2a 556 dstr t = DSTR_INIT;
557 const kgops **ko;
558 const char *e;
559 group *g = 0;
560 ge *p = 0;
b5c45da1 561 algswitch a;
52c03a2a 562 int rc = -1;
563
564 /* --- Find the key --- */
565
566 if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
f43df819 567 a_warn("KEYMGMT", "public-key", "%s", tag, "not-found", A_END);
52c03a2a 568 goto done;
569 }
410c8acf 570
52c03a2a 571 /* --- Look up the key type in the table --- */
572
573 for (ko = kgtab; *ko; ko++) {
574 if (strcmp((*ko)->ty, k->type) == 0)
575 goto tymatch;
576 }
f43df819
MW
577 a_warn("KEYMGMT",
578 "public-key", "%s", t.buf,
579 "unknown-type", "%s", k->type,
580 A_END);
52c03a2a 581 goto done;
582tymatch:;
583
584 /* --- Load the key --- */
585
d59d4fe9 586 if ((e = (*ko)->loadpub(*kd, &g, &p, &t)) != 0) {
f43df819 587 a_warn("KEYMGMT", "public-key", "%s", t.buf, "bad", "%s", e, A_END);
52c03a2a 588 goto done;
589 }
590
591 /* --- Ensure that the group is correct --- *
592 *
593 * Dodginess! We assume that if this works, our global group is willing to
594 * adopt this public element. Probably reasonable.
595 */
596
597 if (!group_samep(gg, g)) {
f43df819 598 a_warn("KEYMGMT", "public-key", "%s", t.buf, "incorrect-group", A_END);
52c03a2a 599 goto done;
600 }
601
602 /* --- Check the public group element --- */
603
604 if (group_check(gg, p)) {
f43df819
MW
605 a_warn("KEYMGMT",
606 "public-key", "%s", t.buf,
607 "bad-public-group-element",
608 A_END);
52c03a2a 609 goto done;
410c8acf 610 }
52c03a2a 611
b5c45da1 612 /* --- Check the algorithms --- */
613
614 if ((e = algs_get(&a, kf_pub, k)) != 0) {
f43df819
MW
615 a_warn("KEYMGMT",
616 "public-key", "%s", t.buf,
617 "bad-algorithm-selection", e,
618 A_END);
b5c45da1 619 goto done;
620 }
3cdc3f3a 621 if (!algs_samep(&a, &algs)) {
f43df819
MW
622 a_warn("KEYMGMT",
623 "public-key", "%s", t.buf,
624 "algorithm-mismatch",
625 A_END);
b5c45da1 626 goto done;
627 }
628
52c03a2a 629 /* --- Dump the public key --- */
630
410c8acf 631 IF_TRACING(T_KEYMGMT, {
52c03a2a 632 trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
633 trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
410c8acf 634 })
52c03a2a 635
636 /* --- OK, accept the public key --- */
637
00e64b67 638 *t_exp = k->exp;
52c03a2a 639 G_COPY(gg, kpub, p);
640 rc = 0;
641
642 /* --- Tidy up --- */
643
644done:
645 if (p) G_DESTROY(g, p);
646 if (g) G_DESTROYGROUP(g);
647 dstr_destroy(&t);
648 return (rc);
410c8acf 649}
650
651/*----- That's all, folks -------------------------------------------------*/