chiark / gitweb /
debian: Drop CDBS in favour of plain Debhelper.
[tripe] / server / keymgmt.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Key loading and storing
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE 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 General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
52c03a2a 31/*----- Key groups --------------------------------------------------------*/
32
799e58b9
MW
33/* The key-loading functions here must fill in the kdata slot @g@ and
34 * either @kpriv@ or @kpub@ as appropriate. The caller will take care of
35 * determining @kpub@ given a private key, and of ensuring that @kpriv@ is
36 * null for a public key.
37 */
38
52c03a2a 39typedef struct kgops {
40 const char *ty;
799e58b9
MW
41 int (*loadpriv)(key_data *, kdata *, dstr *, dstr *);
42 int (*loadpub)(key_data *, kdata *, dstr *, dstr *);
52c03a2a 43} kgops;
44
4155aec4
MW
45/* --- @KLOAD@ --- *
46 *
47 * Arguments: @ty@, @TY@ = key type name (lower- and upper-case)
48 * @which@, @WHICH@ = `pub' or `priv' (and upper-case)
49 * @setgroup@ = code to initialize @kd->g@
50 * @setpriv@ = code to initialize @kd->kpriv@
51 * @setpub@ = code to initialize @kd->kpub@
52 *
53 * Use: Generates the body of one of the (rather tedious) key loading
54 * functions. See the description of @KEYTYPES@ below for the
55 * details.
56 */
52c03a2a 57
4155aec4
MW
58#define KLOAD(ty, TY, which, WHICH, setgroup, setpriv, setpub) \
59static int kg##ty##_##which(key_data *d, kdata *kd, dstr *t, dstr *e) \
60{ \
61 key_packstruct kps[TY##_##WHICH##FETCHSZ]; \
62 key_packdef *kp; \
63 ty##_##which p; \
64 int rc; \
65 \
66 /* --- Initialize things we've not set up yet --- */ \
67 \
68 kd->g = 0; kd->kpub = 0; \
69 \
70 /* --- Unpack the key --- */ \
71 \
72 kp = key_fetchinit(ty##_##which##fetch, kps, &p); \
73 if ((rc = key_unpack(kp, d, t)) != 0) { \
74 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END); \
75 goto fail; \
76 } \
77 \
78 /* --- Extract the pieces of the key --- */ \
79 \
80 setgroup; \
81 setpriv; \
82 kd->kpub = G_CREATE(kd->g); \
83 setpub; \
84 \
85 /* --- We win --- */ \
86 \
87 rc = 0; \
88 goto done; \
89 \
90fail: \
91 if (kd->kpub) G_DESTROY(kd->g, kd->kpub); \
92 if (kd->g) G_DESTROYGROUP(kd->g); \
93 rc = -1; \
94 \
95done: \
96 key_fetchdone(kp); \
97 return (rc); \
52c03a2a 98}
99
4155aec4
MW
100/* --- @KEYTYPES@ --- *
101 *
102 * A list of the various key types, and how to unpack them. Each entry in
103 * the list has the form
104 *
105 * _(ty, TY, setgroup, setpriv, setpub)
106 *
107 * The @ty@ and @TY@ are lower- and upper-case versions of the key type name,
108 * and there should be @key_fetchdef@s called @ty_{priv,pub}fetch@.
109 *
110 * The @setgroup@, @setpriv@ and @setpub@ items are code fragments which are
111 * passed to @KLOAD@ to build appropriate key-loading methods. By the time
112 * these code fragments are run, the key has been unpacked from the incoming
113 * key data using @ty_whichfetch@ into a @ty_which@ structure named @p@.
114 * They can report errors by writing an appropriate token sequence to @e@ and
115 * jumping to @fail@.
116 */
52c03a2a 117
4155aec4
MW
118#define KEYTYPES(_) \
119 \
120 /* --- Diffie-Hellman --- */ \
121 \
122 _(dh, DH, \
123 { kd->g = group_prime(&p.dp); }, \
124 { kd->kpriv = MP_COPY(p.x); }, \
125 { if (G_FROMINT(kd->g, kd->kpub, p.y)) { \
126 a_format(e, "bad-public-vector", A_END); \
127 goto fail; \
128 } \
129 }) \
130 \
131 /* --- Elliptic curves --- */ \
132 \
133 _(ec, EC, \
134 { ec_info ei; const char *err; \
135 if ((err = ec_getinfo(&ei, p.cstr)) != 0) { \
136 a_format(e, "decode-failed", "%s", err, A_END); \
137 goto fail; \
138 } \
139 kd->g = group_ec(&ei); \
140 }, \
141 { kd->kpriv = MP_COPY(p.x); }, \
142 { if (G_FROMEC(kd->g, kd->kpub, &p.p)) { \
143 a_format(e, "bad-public-vector", A_END); \
144 goto fail; \
145 } \
146 })
147
148#define KEYTYPE_DEF(ty, TY, setgroup, setpriv, setpub) \
149 KLOAD(ty, TY, priv, PRIV, setgroup, setpriv, \
150 { G_EXP(kd->g, kd->kpub, kd->g->g, kd->kpriv); }) \
151 KLOAD(ty, TY, pub, PUB, setgroup, { }, setpub) \
152 static const kgops kg##ty##_ops = { #ty, kg##ty##_priv, kg##ty##_pub };
153KEYTYPES(KEYTYPE_DEF)
52c03a2a 154
155/* --- Table of supported key types --- */
156
4155aec4
MW
157static const kgops *kgtab[] = {
158#define KEYTYPE_ENTRY(ty, TY, setgroup, setpriv, setpub) &kg##ty##_ops,
159 KEYTYPES(KEYTYPE_ENTRY)
160#undef KEYTYPE_ENTRY
161 0
162};
52c03a2a 163
b5c45da1 164/*----- Algswitch stuff ---------------------------------------------------*/
165
166/* --- @algs_get@ --- *
167 *
168 * Arguments: @algswitch *a@ = where to put the algorithms
4d36660a
MW
169 * @dstr *e@ = where to write errror tokens
170 * @key_file *kf@ = key file
b5c45da1 171 * @key *k@ = key to inspect
172 *
4d36660a 173 * Returns: Zero if OK; nonzero on error.
b5c45da1 174 *
175 * Use: Extracts an algorithm choice from a key.
176 */
177
4d36660a 178static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
b5c45da1 179{
180 const char *p;
4d36660a 181 char *q, *qq;
b5c45da1 182 dstr d = DSTR_INIT;
4d36660a 183 int rc = -1;
b5c45da1 184
4d36660a 185 /* --- Symmetric encryption for bulk data --- */
b5c45da1 186
4d36660a
MW
187 if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc";
188 if ((a->c = gcipher_byname(p)) == 0) {
189 a_format(e, "unknown-cipher", "%s", p, A_END);
190 goto done;
191 }
192
193 /* --- Hash function --- */
194
195 if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
196 if ((a->h = ghash_byname(p)) == 0) {
197 a_format(e, "unknown-hash", "%s", p, A_END);
198 goto done;
199 }
b5c45da1 200
4d36660a 201 /* --- Symmetric encryption for key derivation --- */
b5c45da1 202
74ee77cb 203 if ((p = key_getattr(kf, k, "mgf")) == 0) {
b5c45da1 204 dstr_reset(&d);
74ee77cb 205 dstr_putf(&d, "%s-mgf", a->h->name);
b5c45da1 206 p = d.buf;
207 }
4d36660a
MW
208 if ((a->mgf = gcipher_byname(p)) == 0) {
209 a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
210 goto done;
211 }
212
213 /* --- Message authentication for bulk data --- */
b5c45da1 214
215 if ((p = key_getattr(kf, k, "mac")) != 0) {
216 dstr_reset(&d);
217 dstr_puts(&d, p);
218 if ((q = strchr(d.buf, '/')) != 0)
219 *q++ = 0;
4d36660a
MW
220 if ((a->m = gmac_byname(d.buf)) == 0) {
221 a_format(e, "unknown-mac", "%s", d.buf, A_END);
222 goto done;
223 }
b5c45da1 224 if (!q)
225 a->tagsz = a->m->hashsz;
226 else {
4d36660a
MW
227 unsigned long n = strtoul(q, &qq, 0);
228 if (*qq) {
229 a_format(e, "bad-tag-length-string", "%s", q, A_END);
230 goto done;
231 }
232 if (n%8 || n/8 > a->m->hashsz) {
233 a_format(e, "bad-tag-length", "%lu", n, A_END);
234 goto done;
235 }
b5c45da1 236 a->tagsz = n/8;
237 }
238 } else {
239 dstr_reset(&d);
240 dstr_putf(&d, "%s-hmac", a->h->name);
4d36660a
MW
241 if ((a->m = gmac_byname(d.buf)) == 0) {
242 a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
243 goto done;
244 }
b5c45da1 245 a->tagsz = a->h->hashsz/2;
246 }
247
4d36660a 248 rc = 0;
b5c45da1 249done:
250 dstr_destroy(&d);
4d36660a 251 return (rc);
b5c45da1 252}
253
254/* --- @algs_check@ --- *
255 *
256 * Arguments: @algswitch *a@ = a choice of algorithms
4d36660a 257 * @dstr *e@ = where to write error tokens
b5c45da1 258 * @const group *g@ = the group we're working in
259 *
4d36660a 260 * Returns: Zero if OK; nonzero on error.
b5c45da1 261 *
262 * Use: Checks an algorithm choice for sensibleness. This also
263 * derives some useful information from the choices, and you
264 * must call this before committing the algorithm selection
265 * for use by @keyset@ functions.
266 */
267
4d36660a 268static int algs_check(algswitch *a, dstr *e, const group *g)
b5c45da1 269{
270 /* --- Derive the key sizes --- *
271 *
272 * Must ensure that we have non-empty keys. This isn't ideal, but it
383a9d71
MW
273 * provides a handy sanity check. Also must be based on a 64- or 128-bit
274 * block cipher or we can't do the data expiry properly.
b5c45da1 275 */
276
277 a->hashsz = a->h->hashsz;
4d36660a
MW
278 if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) {
279 a_format(e, "cipher", "%s", a->c->name,
280 "no-key-size", "%lu", (unsigned long)a->hashsz,
281 A_END);
282 return (-1);
283 }
284 if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) {
285 a_format(e, "mac", "%s", a->m->name,
286 "no-key-size", "%lu", (unsigned long)a->hashsz,
287 A_END);
288 return (-1);
289 }
b5c45da1 290
383a9d71
MW
291 /* --- Derive the data limit --- */
292
293 if (a->c->blksz < 16) a->expsz = MEG(64);
294 else a->expsz = MEG(2048);
295
b5c45da1 296 /* --- Ensure the MGF accepts hashes as keys --- */
297
4d36660a
MW
298 if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
299 a_format(e, "mgf", "%s", a->mgf->name,
300 "restrictive-key-schedule",
301 A_END);
302 return (-1);
303 }
b5c45da1 304
305 /* --- All ship-shape and Bristol-fashion --- */
306
307 return (0);
308}
309
799e58b9 310/* --- @km_samealgsp@ --- *
b5c45da1 311 *
799e58b9 312 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
b5c45da1 313 *
799e58b9 314 * Returns: Nonzero if their two algorithm selections are the same.
b5c45da1 315 *
316 * Use: Checks sameness of algorithm selections: used to ensure that
317 * peers are using sensible algorithms.
318 */
319
799e58b9 320int km_samealgsp(const kdata *kdx, const kdata *kdy)
b5c45da1 321{
799e58b9
MW
322 const algswitch *a = &kdx->algs, *aa = &kdy->algs;
323
324 return (group_samep(kdx->g, kdy->g) && a->c == aa->c &&
325 a->mgf == aa->mgf && a->h == aa->h &&
b5c45da1 326 a->m == aa->m && a->tagsz == aa->tagsz);
327}
328
799e58b9
MW
329/*----- Key data and key nodes --------------------------------------------*/
330
331typedef struct keyhalf {
332 const char *kind;
333 int (*load)(const kgops *, key_data *, kdata *, dstr *, dstr *);
334 const char *kr;
335 key_file *kf;
336 fwatch w;
337 sym_table tab;
338} keyhalf;
339
340/* --- @kh_loadpub@, @kh_loadpriv@ --- *
341 *
342 * Arguments: @const kgops *ko@ = key-group operations for key type
343 * @key_data *d@ = key data object as stored in keyring
344 * @kdata *kd@ = our key-data object to fill in
345 * @dstr *t@ = the key tag name
346 * @dstr *e@ = a string to write error tokens to
347 *
348 * Returns: Zero on success, @-1@ on error.
349 *
350 * Use: These functions handle the main difference between public and
351 * private key halves. They are responsible for setting @g@,
352 * @kpriv@ and @kpub@ appropriately in all keys, handling the
353 * mismatch between the largely half-indifferent calling code
354 * and the group-specific loading functions.
355 *
356 * The function @kh_loadpriv@ is also responsible for checking
357 * the group for goodness. We don't bother checking public
358 * keys, because each public key we actually end up using must
359 * share a group with a private key which we'll already have
360 * checked.
361 */
362
363static int kh_loadpub(const kgops *ko, key_data *d, kdata *kd,
364 dstr *t, dstr *e)
365{
366 int rc;
367
368 if ((rc = ko->loadpub(d, kd, t, e)) != 0)
369 goto fail_0;
370 if (group_check(kd->g, kd->kpub)) {
371 a_format(e, "bad-public-group-element");
372 goto fail_1;
373 }
374 kd->kpriv = 0;
375 return (0);
376
377fail_1:
378 G_DESTROY(kd->g, kd->kpub);
379 G_DESTROYGROUP(kd->g);
380fail_0:
381 return (-1);
382}
383
384static int kh_loadpriv(const kgops *ko, key_data *d, kdata *kd,
385 dstr *t, dstr *e)
386{
387 int rc;
388 const char *err;
389
390 if ((rc = ko->loadpriv(d, kd, t, e)) != 0)
391 goto fail_0;
392 if ((err = G_CHECK(kd->g, &rand_global)) != 0) {
393 a_format(e, "bad-group", "%s", err, A_END);
394 goto fail_1;
395 }
799e58b9
MW
396 return (0);
397
398fail_1:
399 mp_drop(kd->kpriv);
4155aec4 400 G_DESTROY(kd->g, kd->kpub);
799e58b9
MW
401 G_DESTROYGROUP(kd->g);
402fail_0:
403 return (-1);
404}
405
406static struct keyhalf
407 priv = { "private", kh_loadpriv },
408 pub = { "public", kh_loadpub };
410c8acf 409
410/* --- @keymoan@ --- *
411 *
56814747 412 * Arguments: @const char *file@ = name of the file
e04c2d50
MW
413 * @int line@ = line number in file
414 * @const char *msg@ = error message
4d36660a 415 * @void *p@ = argument pointer (indicates which keyring)
410c8acf 416 *
e04c2d50 417 * Returns: ---
410c8acf 418 *
e04c2d50 419 * Use: Reports an error message about loading a key file.
410c8acf 420 */
421
422static void keymoan(const char *file, int line, const char *msg, void *p)
f43df819 423{
799e58b9 424 keyhalf *kh = p;
4d36660a
MW
425
426 if (!line) {
799e58b9 427 a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file,
4d36660a
MW
428 "io-error", "?ERRNO", A_END);
429 } else {
799e58b9 430 a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file, "line", "%d", line,
4d36660a
MW
431 "%s", msg, A_END);
432 }
f43df819 433}
410c8acf 434
799e58b9 435/* --- @kh_reopen@ --- *
fc5f4823 436 *
799e58b9 437 * Arguments: @keyhalf *kh@ = pointer to keyhalf structure
fc5f4823 438 *
799e58b9 439 * Returns: Zero on success, @-1@ on error.
fc5f4823 440 *
799e58b9
MW
441 * Use: Reopens the key file for the appropriate key half. If this
442 * fails, everything is left as it was; if it succeeds, then the
443 * old file is closed (if it was non-null) and the new one put
444 * in its place.
fc5f4823
MW
445 */
446
799e58b9 447static int kh_reopen(keyhalf *kh)
fc5f4823 448{
799e58b9 449 key_file *kf = CREATE(key_file);
fc5f4823 450
799e58b9
MW
451 if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) {
452 a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
7f81b1b4 453 "io-error", "?ERRNO", A_END);
799e58b9
MW
454 DESTROY(kf);
455 return (-1);
456 } else {
457 if (kh->kf) {
458 key_close(kh->kf);
459 DESTROY(kh->kf);
460 }
461 kh->kf = kf;
462 return (0);
463 }
464}
fc5f4823 465
799e58b9
MW
466/* --- @kh_init@ --- *
467 *
468 * Arguments: @keyhalf *kh@ = pointer to keyhalf structure to set up
469 * @const char *kr@ = name of the keyring file
470 *
471 * Returns: ---
472 *
473 * Use: Initialize a keyhalf structure, maintaining the private or
474 * public keys. Intended to be called during initialization:
475 * exits if there's some kind of problem.
476 */
fc5f4823 477
799e58b9
MW
478static void kh_init(keyhalf *kh, const char *kr)
479{
480 kh->kr = kr;
481 fwatch_init(&kh->w, kr);
482 sym_create(&kh->tab);
483 kh->kf = 0;
484
485 if (kh_reopen(kh))
486 die(EXIT_FAILURE, "failed to load %s keyring `%s'", kh->kind, kr);
fc5f4823
MW
487}
488
799e58b9 489/* --- @kh_load@ --- *
410c8acf 490 *
799e58b9
MW
491 * Arguments: @keyhalf *kh@ = pointer to keyhalf
492 * @const char *tag@ = key tag to be loaded
493 * @int complainp@ = whether to complain about missing keys
410c8acf 494 *
799e58b9
MW
495 * Returns: Pointer to a @kdata@ structure if successful, or null on
496 * failure.
410c8acf 497 *
799e58b9
MW
498 * Use: Attempts to load a key from the current key file. This
499 * function always reads data from the file: it's used when
500 * there's a cache miss from @kh_find@, and when refreshing the
501 * known keys in @kh_refresh@. The returned kdata has a
502 * reference count of exactly 1, and has no home knode.
410c8acf 503 */
504
799e58b9 505static kdata *kh_load(keyhalf *kh, const char *tag, int complainp)
410c8acf 506{
52c03a2a 507 dstr t = DSTR_INIT;
4d36660a 508 dstr e = DSTR_INIT;
799e58b9
MW
509 key *k;
510 key_data **d;
511 kdata *kd;
512 const char *ty;
513 const kgops **ko;
52c03a2a 514
799e58b9 515 /* --- Find the key and grab its tag --- */
52c03a2a 516
799e58b9
MW
517 if (key_qtag(kh->kf, tag, &t, &k, &d)) {
518 if (complainp) {
519 a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
520 "key-not-found", "%s", tag, A_END);
521 }
522 goto fail_0;
52c03a2a 523 }
524
799e58b9
MW
525 /* --- Find the key's group type and the appropriate operations --- *
526 *
527 * There are several places to look for the key type. The most obvious is
528 * the `kx-group' key attribute. But there's also the key type itself, for
529 * compatibility reasons.
530 */
52c03a2a 531
799e58b9
MW
532 ty = key_getattr(kh->kf, k, "kx-group");
533 if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
534 if (!ty) ty = "dh";
52c03a2a 535
799e58b9
MW
536 for (ko = kgtab; *ko; ko++)
537 if (strcmp((*ko)->ty, ty) == 0) goto foundko;
538 a_warn("KEYMGMT", "%s-keyring", kh->kind,
539 "%s", kh->kr, "key", "%s", t.buf,
540 "unknown-group-type", "%s", ty, A_END);
541 goto fail_0;
542
543foundko:
544 kd = CREATE(kdata);
545 if (kh->load(*ko, *d, kd, &t, &e)) {
546 a_warn("KEYMGMT", "%s-keyring", kh->kind,
547 "%s", kh->kr, "key" "%s", t.buf,
4d36660a 548 "*%s", e.buf, A_END);
799e58b9 549 goto fail_1;
52c03a2a 550 }
551
799e58b9
MW
552 if (algs_get(&kd->algs, &e, kh->kf, k) ||
553 (kd->kpriv && algs_check(&kd->algs, &e, kd->g))) {
554 a_warn("KEYMGMT", "%s-keyring", kh->kind,
555 "%s", kh->kr, "key", "%s", t.buf,
4d36660a 556 "*%s", e.buf, A_END);
799e58b9 557 goto fail_2;
410c8acf 558 }
52c03a2a 559
799e58b9
MW
560 kd->tag = xstrdup(t.buf);
561 kd->indexsz = mp_octets(kd->g->r);
562 kd->ref = 1;
563 kd->kn = 0;
564 kd->t_exp = k->exp;
52c03a2a 565
566 IF_TRACING(T_KEYMGMT, {
799e58b9 567 trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf);
52c03a2a 568 IF_TRACING(T_CRYPTO, {
799e58b9
MW
569 trace(T_CRYPTO, "crypto: r = %s", mpstr(kd->g->r));
570 trace(T_CRYPTO, "crypto: h = %s", mpstr(kd->g->h));
571 if (kd->kpriv)
572 trace(T_CRYPTO, "crypto: x = %s", mpstr(kd->kpriv));
573 trace(T_CRYPTO, "crypto: cipher = %s", kd->algs.c->name);
574 trace(T_CRYPTO, "crypto: mgf = %s", kd->algs.mgf->name);
575 trace(T_CRYPTO, "crypto: hash = %s", kd->algs.h->name);
b5c45da1 576 trace(T_CRYPTO, "crypto: mac = %s/%lu",
799e58b9 577 kd->algs.m->name, (unsigned long)kd->algs.tagsz * 8);
52c03a2a 578 })
579 })
580
799e58b9 581 goto done;
52c03a2a 582
799e58b9
MW
583fail_2:
584 if (kd->kpriv) mp_drop(kd->kpriv);
585 G_DESTROY(kd->g, kd->kpub);
586 G_DESTROYGROUP(kd->g);
587fail_1:
588 DESTROY(kd);
589fail_0:
590 kd = 0;
591done:
52c03a2a 592 dstr_destroy(&t);
4d36660a 593 dstr_destroy(&e);
799e58b9 594 return (kd);
410c8acf 595}
596
799e58b9 597/* --- @kh_find@ --- *
410c8acf 598 *
799e58b9
MW
599 * Arguments: @keyhalf *kh@ = pointer to the keyhalf
600 * @const char *tag@ = key to be obtained
601 * @int complainp@ = whether to complain about missing keys
410c8acf 602 *
799e58b9 603 * Returns: A pointer to the kdata, or null on error.
410c8acf 604 *
799e58b9
MW
605 * Use: Obtains kdata, maybe from the cache. This won't update a
606 * stale cache entry, though @kh_refresh@ ought to have done
607 * that already. The returned kdata object may be shared with
608 * other users. (One of this function's responsibilities, over
609 * @kh_load@, is to set the home knode of a freshly loaded
610 * kdata.)
410c8acf 611 */
612
799e58b9 613static kdata *kh_find(keyhalf *kh, const char *tag, int complainp)
410c8acf 614{
799e58b9
MW
615 knode *kn;
616 kdata *kd;
617 unsigned f;
410c8acf 618
799e58b9
MW
619 kn = sym_find(&kh->tab, tag, -1, sizeof(knode), &f);
620
621 if (f) {
622 if (kn->f & KNF_BROKEN) {
623 T( if (complainp)
624 trace(T_KEYMGMT, "keymgmt: key `%s' marked as broken", tag); )
625 return (0);
626 }
627
628 kd = kn->kd;
629 if (kd) kd->ref++;
630 T( trace(T_KEYMGMT, "keymgmt: %scache hit for key `%s'",
631 kd ? "" : "negative ", tag); )
632 return (kd);
633 } else {
634 kd = kh_load(kh, tag, complainp);
635 kn->kd = kd;
636 kn->kh = kh;
637 kn->f = 0;
638 if (!kd)
639 kn->f |= KNF_BROKEN;
640 else {
641 kd->kn = kn;
642 kd->ref++;
643 }
644 return (kd);
410c8acf 645 }
410c8acf 646}
647
799e58b9 648/* --- @kh_refresh@ --- *
410c8acf 649 *
799e58b9 650 * Arguments: @keyhalf *kh@ = pointer to the keyhalf
410c8acf 651 *
799e58b9
MW
652 * Returns: Zero if nothing needs to be done; nonzero if peers should
653 * refresh their keys.
410c8acf 654 *
799e58b9
MW
655 * Use: Refreshes cached keys from files.
656 *
657 * Each active knode is examined to see if a new key is
658 * available: the return value is nonzero if any new keys are.
659 * A key is considered new if its algorithms, public key, or
660 * expiry time are/is different.
661 *
662 * Stub knodes (with no kdata attached) are removed, so that a
663 * later retry can succeed if the file has been fixed. (This
664 * doesn't count as a change, since no peers should be relying
665 * on a nonexistent key.)
410c8acf 666 */
667
799e58b9 668static int kh_refresh(keyhalf *kh)
410c8acf 669{
799e58b9
MW
670 knode *kn;
671 kdata *kd;
672 sym_iter i;
673 int changep = 0;
674
675 if (!fwatch_update(&kh->w, kh->kr) || kh_reopen(kh))
676 return (0);
677
678 T( trace(T_KEYMGMT, "keymgmt: rescan %s keyring `%s'", kh->kind, kh->kr); )
679 for (sym_mkiter(&i, &kh->tab); (kn = sym_next(&i)) != 0; ) {
680 if (!kn->kd) {
681 T( trace(T_KEYMGMT, "keymgmt: discard stub entry for key `%s'",
682 SYM_NAME(kn)); )
683 sym_remove(&kh->tab, kn);
684 continue;
685 }
686 if ((kd = kh_load(kh, SYM_NAME(kn), 1)) == 0) {
687 if (!(kn->f & KNF_BROKEN)) {
688 T( trace(T_KEYMGMT, "keymgmt: failed to load new key `%s': "
689 "marking it as broken",
690 SYM_NAME(kn)); )
691 kn->f |= KNF_BROKEN;
692 }
693 continue;
694 }
695 kn->f &= ~KNF_BROKEN;
696 if (kd->t_exp == kn->kd->t_exp &&
697 km_samealgsp(kd, kn->kd) &&
698 G_EQ(kd->g, kd->kpub, kn->kd->kpub)) {
699 T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); )
700 continue;
701 }
702 T( trace(T_KEYMGMT, "keymgmt: loaded new version of key `%s'",
703 SYM_NAME(kn)); )
704 km_unref(kn->kd);
705 kd->kn = kn;
706 kn->kd = kd;
707 changep = 1;
708 }
410c8acf 709
799e58b9
MW
710 return (changep);
711}
410c8acf 712
799e58b9 713/*----- Main code ---------------------------------------------------------*/
410c8acf 714
799e58b9
MW
715const char *tag_priv;
716kdata *master;
410c8acf 717
410c8acf 718/* --- @km_init@ --- *
719 *
799e58b9
MW
720 * Arguments: @const char *privkr@ = private keyring file
721 * @const char *pubkr@ = public keyring file
722 * @const char *ptag@ = default private-key tag
410c8acf 723 *
724 * Returns: ---
725 *
799e58b9
MW
726 * Use: Initializes the key-management machinery, loading the
727 * keyrings and so on.
410c8acf 728 */
729
799e58b9 730void km_init(const char *privkr, const char *pubkr, const char *ptag)
410c8acf 731{
b5c45da1 732 const gchash *const *hh;
410c8acf 733
b5c45da1 734 for (hh = ghashtab; *hh; hh++) {
735 if ((*hh)->hashsz > MAXHASHSZ) {
736 die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
737 (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
738 }
739 }
740
799e58b9
MW
741 kh_init(&priv, privkr);
742 kh_init(&pub, pubkr);
743
744 tag_priv = ptag;
745 if ((master = km_findpriv(ptag)) == 0) exit(EXIT_FAILURE);
410c8acf 746}
747
799e58b9 748/* --- @km_reload@ --- *
410c8acf 749 *
799e58b9 750 * Arguments: ---
410c8acf 751 *
799e58b9 752 * Returns: Zero if OK, nonzero to force reloading of keys.
410c8acf 753 *
799e58b9 754 * Use: Checks the keyrings to see if they need reloading.
410c8acf 755 */
756
799e58b9 757int km_reload(void)
410c8acf 758{
799e58b9
MW
759 int changep = 0;
760 kdata *kd;
761
762 if (kh_refresh(&priv)) {
763 changep = 1;
764 kd = master->kn->kd;
35c8b547 765 if (kd != master) {
799e58b9
MW
766 km_unref(master);
767 km_ref(kd);
768 master = kd;
769 }
770 }
771 if (kh_refresh(&pub))
772 changep = 1;
773 return (changep);
774}
52c03a2a 775
799e58b9
MW
776/* --- @km_findpub@, @km_findpriv@ --- *
777 *
778 * Arguments: @const char *tag@ = key tag to load
779 *
780 * Returns: Pointer to the kdata object if successful, or null on error.
781 *
782 * Use: Fetches a public or private key from the keyring.
783 */
52c03a2a 784
799e58b9 785kdata *km_findpub(const char *tag) { return (kh_find(&pub, tag, 1)); }
410c8acf 786
799e58b9
MW
787kdata *km_findpriv(const char *tag)
788{
789 kdata *kd;
52c03a2a 790
799e58b9
MW
791 /* Unpleasantness for the sake of compatibility. */
792 if (!tag && (kd = kh_find(&priv, "tripe", 0)) != 0) return (kd);
793 else return (kh_find(&priv, tag ? tag : "tripe-dh", 1));
794}
52c03a2a 795
799e58b9
MW
796/* --- @km_tag@ --- *
797 *
798 * Arguments: @kdata *kd@ - pointer to the kdata object
799 *
800 * Returns: A pointer to the short tag by which the kdata was loaded.
801 */
52c03a2a 802
799e58b9 803const char *km_tag(kdata *kd) { return (SYM_NAME(kd->kn)); }
52c03a2a 804
799e58b9
MW
805/* --- @km_ref@ --- *
806 *
807 * Arguments: @kdata *kd@ = pointer to the kdata object
808 *
809 * Returns: ---
810 *
811 * Use: Claim a new reference to a kdata object.
812 */
52c03a2a 813
799e58b9 814void km_ref(kdata *kd) { kd->ref++; }
52c03a2a 815
799e58b9
MW
816/* --- @km_unref@ --- *
817 *
818 * Arguments: @kdata *kd@ = pointer to the kdata object
819 *
820 * Returns: ---
821 *
822 * Use: Releases a reference to a kdata object.
823 */
52c03a2a 824
799e58b9
MW
825void km_unref(kdata *kd)
826{
827 if (--kd->ref) return;
828 if (kd->kpriv) mp_drop(kd->kpriv);
829 G_DESTROY(kd->g, kd->kpub);
830 xfree(kd->tag);
831 G_DESTROYGROUP(kd->g);
832}
833
410c8acf 834/*----- That's all, folks -------------------------------------------------*/