chiark / gitweb /
server/keymgmt.c (kh_reopen): Promote the happy path.
[tripe] / server / keymgmt.c
1 /* -*-c-*-
2  *
3  * Key loading and storing
4  *
5  * (c) 2001 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
12  * TrIPE is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with TrIPE.  If not, see <https://www.gnu.org/licenses/>.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Algswitch stuff ---------------------------------------------------*/
31
32 /* --- @algs_get@ --- *
33  *
34  * Arguments:   @algswitch *a@ = where to put the algorithms
35  *              @dstr *e@ = where to write error tokens
36  *              @key_file *kf@ = key file
37  *              @key *k@ = key to inspect
38  *
39  * Returns:     Zero if OK; nonzero on error.
40  *
41  * Use:         Extracts an algorithm choice from a key.
42  */
43
44 static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
45 {
46   const char *p;
47   const bulkops *bops;
48   dstr d = DSTR_INIT, dd = DSTR_INIT;
49   int rc = -1;
50
51   /* --- Hash function --- */
52
53   if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
54   if ((a->h = ghash_byname(p)) == 0) {
55     a_format(e, "unknown-hash", "%s", p, A_END);
56     goto done;
57   }
58
59   /* --- Symmetric encryption for key derivation --- */
60
61   if ((p = key_getattr(kf, k, "mgf")) == 0) {
62     dstr_reset(&d);
63     dstr_putf(&d, "%s-mgf", a->h->name);
64     p = d.buf;
65   }
66   if ((a->mgf = gcipher_byname(p)) == 0) {
67     a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
68     goto done;
69   }
70
71   /* --- Bulk crypto transform --- */
72
73   if ((p = key_getattr(kf, k, "bulk")) == 0) p = "v0";
74   for (bops = bulktab; bops->name && strcmp(p, bops->name) != 0; bops++);
75   if (!bops->name) {
76     a_format(e, "unknown-bulk-transform", "%s", p, A_END);
77     goto done;
78   }
79   if ((a->bulk = bops->getalgs(a, e, kf, k)) == 0) goto done;
80   a->bulk->ops = bops;
81
82   /* --- All done --- */
83
84   rc = 0;
85 done:
86   dstr_destroy(&d);
87   dstr_destroy(&dd);
88   return (rc);
89 }
90
91 /* --- @algs_check@ --- *
92  *
93  * Arguments:   @algswitch *a@ = a choice of algorithms
94  *              @dstr *e@ = where to write error tokens
95  *              @const dhgrp *grp@ = the group we're working in
96  *
97  * Returns:     Zero if OK; nonzero on error.
98  *
99  * Use:         Checks an algorithm choice for sensibleness.  This also
100  *              derives some useful information from the choices, and you
101  *              must call this before committing the algorithm selection
102  *              for use by @keyset@ functions.
103  */
104
105 static int algs_check(algswitch *a, dstr *e, const dhgrp *grp)
106 {
107   a->hashsz = a->h->hashsz;
108
109   if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
110     a_format(e, "mgf", "%s", a->mgf->name,
111              "restrictive-key-schedule",
112              A_END);
113     return (-1);
114   }
115
116   if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1);
117
118   return (0);
119 }
120
121 /* --- @km_samealgsp@ --- *
122  *
123  * Arguments:   @const kdata *kdx, *kdy@ = two key data objects
124  *
125  * Returns:     Nonzero if their two algorithm selections are the same.
126  *
127  * Use:         Checks sameness of algorithm selections: used to ensure that
128  *              peers are using sensible algorithms.
129  */
130
131 int km_samealgsp(const kdata *kdx, const kdata *kdy)
132 {
133   const algswitch *a = &kdx->algs, *aa = &kdy->algs;
134
135   return (kdx->grp->ops == kdy->grp->ops &&
136           kdx->grp->ops->samegrpp(kdx->grp, kdy->grp) &&
137           a->mgf == aa->mgf && a->h == aa->h &&
138           a->bulk->ops == aa->bulk->ops &&
139           a->bulk->ops->samealgsp(a->bulk, aa->bulk));
140 }
141
142 /*----- Key data and key nodes --------------------------------------------*/
143
144 typedef struct keyhalf {
145   const char *kind;
146   int (*load)(key_file *, key *, key_data *,
147               const dhops *, kdata *, dstr *, dstr *);
148   const char *kr;
149   key_file *kf;
150   fwatch w;
151   sym_table tab;
152 } keyhalf;
153
154 /* --- @kh_loadpub@, @kh_loadpriv@ --- *
155  *
156  * Arguments:   @const dhops *dh@ = Diffie--Hellman operations for key type
157  *              @key_file *kf@ = key file from which the key was loaded
158  *              @key *k@ = the key object we're loading
159  *              @key_data *d@ = the key data to load
160  *              @kdata *kd@ = our key-data object to fill in
161  *              @dstr *t@ = the key tag name
162  *              @dstr *e@ = a string to write error tokens to
163  *
164  * Returns:     Zero on success, @-1@ on error.
165  *
166  * Use:         These functions handle the main difference between public and
167  *              private key halves.  They are responsible for setting @grp@,
168  *              @k@ and @K@ appropriately in all keys, handling the mismatch
169  *              between the largely half-indifferent calling code and the
170  *              group-specific loading functions.
171  *
172  *              The function @kh_loadpriv@ is also responsible for checking
173  *              the group for goodness.  We don't bother checking public
174  *              keys, because each public key we actually end up using must
175  *              share a group with a private key which we'll already have
176  *              checked.
177  */
178
179 static int kh_loadpub(key_file *kf, key *k, key_data *d,
180                       const dhops *dh, kdata *kd, dstr *t, dstr *e)
181 {
182   int rc;
183
184   if ((rc = dh->ldpub(kf, k, d, kd, t, e)) != 0)
185     goto fail_0;
186   kd->grp->ops = dh;
187   if (kd->grp->ops->checkge(kd->grp, kd->K)) {
188     a_format(e, "bad-public-group-element", A_END);
189     goto fail_1;
190   }
191   return (0);
192
193 fail_1:
194   kd->grp->ops->freege(kd->grp, kd->K);
195   kd->grp->ops->freegrp(kd->grp);
196 fail_0:
197   return (-1);
198 }
199
200 static int kh_loadpriv(key_file *kf, key *k, key_data *d,
201                        const dhops *dh, kdata *kd, dstr *t, dstr *e)
202 {
203   int rc;
204   const char *err;
205   dhge *K;
206   int ok;
207
208   if ((rc = dh->ldpriv(kf, k, d, kd, t, e)) != 0)
209     goto fail_0;
210   kd->grp->ops = dh;
211   if ((err = kd->grp->ops->checkgrp(kd->grp)) != 0) {
212     a_format(e, "bad-group", "%s", err, A_END);
213     goto fail_1;
214   }
215   K = kd->grp->ops->mul(kd->grp, kd->k, 0);
216   ok = kd->grp->ops->eq(kd->grp, kd->K, K);
217   kd->grp->ops->freege(kd->grp, K);
218   if (!ok) {
219     a_format(e, "incorrect-public-key", A_END);
220     goto fail_1;
221   }
222   return (0);
223
224 fail_1:
225   kd->grp->ops->freesc(kd->grp, kd->k);
226   kd->grp->ops->freege(kd->grp, kd->K);
227   kd->grp->ops->freegrp(kd->grp);
228 fail_0:
229   return (-1);
230 }
231
232 static struct keyhalf
233   priv = { "private", kh_loadpriv },
234   pub = { "public", kh_loadpub };
235
236 /* --- @keymoan@ --- *
237  *
238  * Arguments:   @const char *file@ = name of the file
239  *              @int line@ = line number in file
240  *              @const char *msg@ = error message
241  *              @void *p@ = argument pointer (indicates which keyring)
242  *
243  * Returns:     ---
244  *
245  * Use:         Reports an error message about loading a key file.
246  */
247
248 static void keymoan(const char *file, int line, const char *msg, void *p)
249 {
250   keyhalf *kh = p;
251
252   if (!line) {
253     a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file,
254            "io-error", "?ERRNO", A_END);
255   } else {
256     a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file, "line", "%d", line,
257            "%s", msg, A_END);
258   }
259 }
260
261 /* --- @kh_reopen@ --- *
262  *
263  * Arguments:   @keyhalf *kh@ = pointer to keyhalf structure
264  *
265  * Returns:     Zero on success, @-1@ on error.
266  *
267  * Use:         Reopens the key file for the appropriate key half.  If this
268  *              fails, everything is left as it was; if it succeeds, then the
269  *              old file is closed (if it was non-null) and the new one put
270  *              in its place.
271  */
272
273 static int kh_reopen(keyhalf *kh)
274 {
275   key_file *kf = CREATE(key_file);
276
277   if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) {
278     a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
279            "io-error", "?ERRNO", A_END);
280     DESTROY(kf);
281     return (-1);
282   }
283   if (kh->kf) {
284     key_close(kh->kf);
285     DESTROY(kh->kf);
286   }
287   kh->kf = kf;
288   return (0);
289 }
290
291 /* --- @kh_init@ --- *
292  *
293  * Arguments:   @keyhalf *kh@ = pointer to keyhalf structure to set up
294  *              @const char *kr@ = name of the keyring file
295  *
296  * Returns:     ---
297  *
298  * Use:         Initialize a keyhalf structure, maintaining the private or
299  *              public keys.  Intended to be called during initialization:
300  *              exits if there's some kind of problem.
301  */
302
303 static void kh_init(keyhalf *kh, const char *kr)
304 {
305   kh->kr = kr;
306   fwatch_init(&kh->w, kr);
307   sym_create(&kh->tab);
308   kh->kf = 0;
309
310   if (kh_reopen(kh))
311     die(EXIT_FAILURE, "failed to load %s keyring `%s'", kh->kind, kr);
312 }
313
314 /* --- @kh_load@ --- *
315  *
316  * Arguments:   @keyhalf *kh@ = pointer to keyhalf
317  *              @const char *tag@ = key tag to be loaded
318  *              @int complainp@ = whether to complain about missing keys
319  *
320  * Returns:     Pointer to a @kdata@ structure if successful, or null on
321  *              failure.
322  *
323  * Use:         Attempts to load a key from the current key file.  This
324  *              function always reads data from the file: it's used when
325  *              there's a cache miss from @kh_find@, and when refreshing the
326  *              known keys in @kh_refresh@.  The returned kdata has a
327  *              reference count of exactly 1, and has no home knode.
328  */
329
330 static kdata *kh_load(keyhalf *kh, const char *tag, int complainp)
331 {
332   dstr t = DSTR_INIT;
333   dstr e = DSTR_INIT;
334   key *k;
335   key_data **d;
336   kdata *kd;
337   const char *ty;
338   const dhops *dh;
339   T( const dhgrp *g; )
340
341   /* --- Find the key and grab its tag --- */
342
343   if (key_qtag(kh->kf, tag, &t, &k, &d)) {
344     if (complainp) {
345       a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
346              "key-not-found", "%s", tag, A_END);
347     }
348     goto fail_0;
349   }
350
351   /* --- Find the key's group type and the appropriate operations --- *
352    *
353    * There are several places to look for the key type.  The most obvious is
354    * the `kx-group' key attribute.  But there's also the key type itself, for
355    * compatibility reasons.
356    */
357
358   ty = key_getattr(kh->kf, k, "kx-group");
359   if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
360   if (!ty) ty = "dh";
361
362   for (dh = dhtab; dh->name; dh++)
363     if (strcmp(dh->name, ty) == 0) goto founddh;
364   a_warn("KEYMGMT", "%s-keyring", kh->kind,
365          "%s", kh->kr, "key", "%s", t.buf,
366          "unknown-group-type", "%s", ty, A_END);
367   goto fail_0;
368
369 founddh:
370   kd = CREATE(kdata);
371   if (kh->load(kh->kf, k, *d, dh, kd, &t, &e)) {
372     a_warn("KEYMGMT", "%s-keyring", kh->kind,
373            "%s", kh->kr, "key", "%s", t.buf,
374            "*%s", e.buf, A_END);
375     goto fail_1;
376   }
377
378   if (algs_get(&kd->algs, &e, kh->kf, k) ||
379       algs_check(&kd->algs, &e, kd->grp)) {
380     a_warn("KEYMGMT", "%s-keyring", kh->kind,
381            "%s", kh->kr, "key", "%s", t.buf,
382            "*%s", e.buf, A_END);
383     goto fail_2;
384   }
385
386   kd->tag = xstrdup(t.buf);
387   kd->ref = 1;
388   kd->kn = 0;
389   kd->id = k->id;
390   kd->t_exp = k->exp;
391
392   IF_TRACING(T_KEYMGMT, {
393     trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf);
394     IF_TRACING(T_CRYPTO, {
395       g = kd->grp;
396       g->ops->tracegrp(g);
397       if (kd->k)
398         trace(T_CRYPTO, "crypto: k = %s", g->ops->scstr(g, kd->k));
399       trace(T_CRYPTO, "crypto: K = %s", g->ops->gestr(g, kd->K));
400       kd->algs.bulk->ops->tracealgs(kd->algs.bulk);
401     })
402   })
403
404   goto done;
405
406 fail_2:
407   if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
408   kd->grp->ops->freege(kd->grp, kd->K);
409   kd->grp->ops->freegrp(kd->grp);
410 fail_1:
411   DESTROY(kd);
412 fail_0:
413   kd = 0;
414 done:
415   dstr_destroy(&t);
416   dstr_destroy(&e);
417   return (kd);
418 }
419
420 /* --- @kh_find@ --- *
421  *
422  * Arguments:   @keyhalf *kh@ = pointer to the keyhalf
423  *              @const char *tag@ = key to be obtained
424  *              @int complainp@ = whether to complain about missing keys
425  *
426  * Returns:     A pointer to the kdata, or null on error.
427  *
428  * Use:         Obtains kdata, maybe from the cache.  This won't update a
429  *              stale cache entry, though @kh_refresh@ ought to have done
430  *              that already.  The returned kdata object may be shared with
431  *              other users.  (One of this function's responsibilities, over
432  *              @kh_load@, is to set the home knode of a freshly loaded
433  *              kdata.)
434  */
435
436 static kdata *kh_find(keyhalf *kh, const char *tag, int complainp)
437 {
438   knode *kn;
439   kdata *kd;
440   unsigned f;
441
442   kn = sym_find(&kh->tab, tag, -1, sizeof(knode), &f);
443
444   if (f) {
445     if (kn->f & KNF_BROKEN) {
446       T( if (complainp)
447            trace(T_KEYMGMT, "keymgmt: key `%s' marked as broken", tag); )
448       return (0);
449     }
450
451     kd = kn->kd;
452     if (kd) kd->ref++;
453     T( trace(T_KEYMGMT, "keymgmt: %scache hit for key `%s'",
454              kd ? "" : "negative ", tag); )
455     return (kd);
456   } else {
457     kd = kh_load(kh, tag, complainp);
458     kn->kd = kd;
459     kn->kh = kh;
460     kn->f = 0;
461     if (!kd)
462       kn->f |= KNF_BROKEN;
463     else {
464       kd->kn = kn;
465       kd->ref++;
466     }
467     return (kd);
468   }
469 }
470
471 /* --- @kh_refresh@ --- *
472  *
473  * Arguments:   @keyhalf *kh@ = pointer to the keyhalf
474  *
475  * Returns:     Zero if nothing needs to be done; nonzero if peers should
476  *              refresh their keys.
477  *
478  * Use:         Refreshes cached keys from files.
479  *
480  *              Each active knode is examined to see if a new key is
481  *              available: the return value is nonzero if any new keys are.
482  *              A key is considered new if its algorithms, public key, or
483  *              expiry time are/is different.
484  *
485  *              Stub knodes (with no kdata attached) are removed, so that a
486  *              later retry can succeed if the file has been fixed.  (This
487  *              doesn't count as a change, since no peers should be relying
488  *              on a nonexistent key.)
489  */
490
491 static int kh_refresh(keyhalf *kh)
492 {
493   knode *kn;
494   kdata *kd;
495   sym_iter i;
496   int changep = 0;
497
498   if (!fwatch_update(&kh->w, kh->kr) || kh_reopen(kh))
499     return (0);
500
501   T( trace(T_KEYMGMT, "keymgmt: rescan %s keyring `%s'", kh->kind, kh->kr); )
502   for (sym_mkiter(&i, &kh->tab); (kn = sym_next(&i)) != 0; ) {
503     if (!kn->kd) {
504       T( trace(T_KEYMGMT, "keymgmt: discard stub entry for key `%s'",
505                SYM_NAME(kn)); )
506       sym_remove(&kh->tab, kn);
507       continue;
508     }
509     if ((kd = kh_load(kh, SYM_NAME(kn), 1)) == 0) {
510       if (!(kn->f & KNF_BROKEN)) {
511         T( trace(T_KEYMGMT, "keymgmt: failed to load new key `%s': "
512                  "marking it as broken",
513                  SYM_NAME(kn)); )
514         kn->f |= KNF_BROKEN;
515       }
516       continue;
517     }
518     kn->f &= ~KNF_BROKEN;
519     if (kd->t_exp == kn->kd->t_exp &&
520         km_samealgsp(kd, kn->kd) &&
521         kd->grp->ops->eq(kd->grp, kd->K, kn->kd->K)) {
522       T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); )
523       continue;
524     }
525     T( trace(T_KEYMGMT, "keymgmt: loaded new version of key `%s'",
526              SYM_NAME(kn)); )
527     km_unref(kn->kd);
528     kd->kn = kn;
529     kn->kd = kd;
530     changep = 1;
531   }
532
533   return (changep);
534 }
535
536 /*----- Main code ---------------------------------------------------------*/
537
538 const char *tag_priv;
539 kdata *master;
540
541 /* --- @km_init@ --- *
542  *
543  * Arguments:   @const char *privkr@ = private keyring file
544  *              @const char *pubkr@ = public keyring file
545  *              @const char *ptag@ = default private-key tag
546  *
547  * Returns:     ---
548  *
549  * Use:         Initializes the key-management machinery, loading the
550  *              keyrings and so on.
551  */
552
553 void km_init(const char *privkr, const char *pubkr, const char *ptag)
554 {
555   const gchash *const *hh;
556
557   for (hh = ghashtab; *hh; hh++) {
558     if ((*hh)->hashsz > MAXHASHSZ) {
559       die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
560           (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
561     }
562   }
563
564   kh_init(&priv, privkr);
565   kh_init(&pub, pubkr);
566
567   tag_priv = ptag;
568   if ((master = km_findpriv(ptag)) == 0) exit(EXIT_FAILURE);
569 }
570
571 /* --- @km_reload@ --- *
572  *
573  * Arguments:   ---
574  *
575  * Returns:     Zero if OK, nonzero to force reloading of keys.
576  *
577  * Use:         Checks the keyrings to see if they need reloading.
578  */
579
580 int km_reload(void)
581 {
582   int changep = 0;
583   kdata *kd;
584
585   if (kh_refresh(&priv)) {
586     changep = 1;
587     kd = master->kn->kd;
588     if (kd != master) {
589       km_unref(master);
590       km_ref(kd);
591       master = kd;
592     }
593   }
594   if (kh_refresh(&pub))
595     changep = 1;
596   return (changep);
597 }
598
599 /* --- @km_findpub@, @km_findpriv@ --- *
600  *
601  * Arguments:   @const char *tag@ = key tag to load
602  *
603  * Returns:     Pointer to the kdata object if successful, or null on error.
604  *
605  * Use:         Fetches a public or private key from the keyring.
606  */
607
608 kdata *km_findpub(const char *tag) { return (kh_find(&pub, tag, 1)); }
609
610 kdata *km_findpriv(const char *tag)
611 {
612   kdata *kd;
613
614   /* Unpleasantness for the sake of compatibility. */
615   if (!tag && (kd = kh_find(&priv, "tripe", 0)) != 0) return (kd);
616   else return (kh_find(&priv, tag ? tag : "tripe-dh", 1));
617 }
618
619 /* --- @km_findpubbyid@, @km_findprivbyid@ --- *
620  *
621  * Arguments:   @uint32 id@ = key id to load
622  *
623  * Returns:     Pointer to the kdata object if successful, or null on error.
624  *
625  * Use:         Fetches a public or private key from the keyring given its
626  *              numeric id.
627  */
628
629 static kdata *findbyid(keyhalf *kh, uint32 id)
630 {
631   key *k;
632   kdata *kd;
633
634   k = key_byid(kh->kf, id); if (!k) goto notfound;
635   kd = kh_find(kh, k->tag, 1); if (!kd) goto notfound;
636   if (kd->id != id) { km_unref(kd); goto notfound; }
637   return (kd);
638
639 notfound:
640   a_warn("KX", "%s-keyring", kh->kind, "%s", kh->kr,
641          "unknown-key-id", "0x%08lx", (unsigned long)id,
642          A_END);
643   return (0);
644 }
645
646 kdata *km_findpubbyid(uint32 id) { return (findbyid(&pub, id)); }
647
648 kdata *km_findprivbyid(uint32 id)
649 {
650   if (id == master->id) { km_ref(master); return (master); }
651   else return findbyid(&priv, id);
652 }
653
654 /* --- @km_tag@ --- *
655  *
656  * Arguments:   @kdata *kd@ - pointer to the kdata object
657  *
658  * Returns:     A pointer to the short tag by which the kdata was loaded.
659  */
660
661 const char *km_tag(kdata *kd) { return (SYM_NAME(kd->kn)); }
662
663 /* --- @km_ref@ --- *
664  *
665  * Arguments:   @kdata *kd@ = pointer to the kdata object
666  *
667  * Returns:     ---
668  *
669  * Use:         Claim a new reference to a kdata object.
670  */
671
672 void km_ref(kdata *kd) { kd->ref++; }
673
674 /* --- @km_unref@ --- *
675  *
676  * Arguments:   @kdata *kd@ = pointer to the kdata object
677  *
678  * Returns:     ---
679  *
680  * Use:         Releases a reference to a kdata object.
681  */
682
683 void km_unref(kdata *kd)
684 {
685   if (--kd->ref) return;
686   if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
687   kd->grp->ops->freege(kd->grp, kd->K);
688   kd->grp->ops->freegrp(kd->grp);
689   xfree(kd->tag);
690   DESTROY(kd);
691 }
692
693 /*----- That's all, folks -------------------------------------------------*/