Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
2 | * | |
410c8acf | 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 | 39 | typedef 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) \ |
59 | static 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 | \ | |
90 | fail: \ | |
91 | if (kd->kpub) G_DESTROY(kd->g, kd->kpub); \ | |
92 | if (kd->g) G_DESTROYGROUP(kd->g); \ | |
93 | rc = -1; \ | |
94 | \ | |
95 | done: \ | |
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 }; | |
153 | KEYTYPES(KEYTYPE_DEF) | |
52c03a2a | 154 | |
155 | /* --- Table of supported key types --- */ | |
156 | ||
4155aec4 MW |
157 | static 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 | |
4998da61 | 169 | * @dstr *e@ = where to write error tokens |
4d36660a | 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 | 178 | static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k) |
b5c45da1 | 179 | { |
180 | const char *p; | |
c70a7c5c | 181 | const bulkops *bops; |
b87bffcb | 182 | dstr d = DSTR_INIT, dd = DSTR_INIT; |
4d36660a | 183 | int rc = -1; |
b5c45da1 | 184 | |
4d36660a MW |
185 | /* --- Hash function --- */ |
186 | ||
187 | if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160"; | |
188 | if ((a->h = ghash_byname(p)) == 0) { | |
189 | a_format(e, "unknown-hash", "%s", p, A_END); | |
190 | goto done; | |
191 | } | |
b5c45da1 | 192 | |
4d36660a | 193 | /* --- Symmetric encryption for key derivation --- */ |
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 | } | |
4d36660a MW |
200 | if ((a->mgf = gcipher_byname(p)) == 0) { |
201 | a_format(e, "unknown-mgf-cipher", "%s", p, A_END); | |
202 | goto done; | |
203 | } | |
204 | ||
a93aacce | 205 | /* --- Bulk crypto transform --- */ |
b5c45da1 | 206 | |
a93aacce | 207 | if ((p = key_getattr(kf, k, "bulk")) == 0) p = "v0"; |
c70a7c5c MW |
208 | for (bops = bulktab; bops->name && strcmp(p, bops->name) != 0; bops++); |
209 | if (!bops->name) { | |
a93aacce MW |
210 | a_format(e, "unknown-bulk-transform", "%s", p, A_END); |
211 | goto done; | |
212 | } | |
c70a7c5c MW |
213 | if ((a->bulk = bops->getalgs(a, e, kf, k)) == 0) goto done; |
214 | a->bulk->ops = bops; | |
b5c45da1 | 215 | |
a93aacce MW |
216 | /* --- All done --- */ |
217 | ||
4d36660a | 218 | rc = 0; |
b5c45da1 | 219 | done: |
220 | dstr_destroy(&d); | |
b87bffcb | 221 | dstr_destroy(&dd); |
4d36660a | 222 | return (rc); |
b5c45da1 | 223 | } |
224 | ||
225 | /* --- @algs_check@ --- * | |
226 | * | |
227 | * Arguments: @algswitch *a@ = a choice of algorithms | |
4d36660a | 228 | * @dstr *e@ = where to write error tokens |
b5c45da1 | 229 | * @const group *g@ = the group we're working in |
230 | * | |
4d36660a | 231 | * Returns: Zero if OK; nonzero on error. |
b5c45da1 | 232 | * |
233 | * Use: Checks an algorithm choice for sensibleness. This also | |
234 | * derives some useful information from the choices, and you | |
235 | * must call this before committing the algorithm selection | |
236 | * for use by @keyset@ functions. | |
237 | */ | |
238 | ||
4d36660a | 239 | static int algs_check(algswitch *a, dstr *e, const group *g) |
b5c45da1 | 240 | { |
b5c45da1 | 241 | a->hashsz = a->h->hashsz; |
b5c45da1 | 242 | |
4d36660a MW |
243 | if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) { |
244 | a_format(e, "mgf", "%s", a->mgf->name, | |
245 | "restrictive-key-schedule", | |
246 | A_END); | |
247 | return (-1); | |
248 | } | |
b5c45da1 | 249 | |
c70a7c5c | 250 | if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1); |
b5c45da1 | 251 | |
252 | return (0); | |
253 | } | |
254 | ||
799e58b9 | 255 | /* --- @km_samealgsp@ --- * |
b5c45da1 | 256 | * |
799e58b9 | 257 | * Arguments: @const kdata *kdx, *kdy@ = two key data objects |
b5c45da1 | 258 | * |
799e58b9 | 259 | * Returns: Nonzero if their two algorithm selections are the same. |
b5c45da1 | 260 | * |
261 | * Use: Checks sameness of algorithm selections: used to ensure that | |
262 | * peers are using sensible algorithms. | |
263 | */ | |
264 | ||
799e58b9 | 265 | int km_samealgsp(const kdata *kdx, const kdata *kdy) |
b5c45da1 | 266 | { |
799e58b9 MW |
267 | const algswitch *a = &kdx->algs, *aa = &kdy->algs; |
268 | ||
b87bffcb | 269 | return (group_samep(kdx->g, kdy->g) && |
799e58b9 | 270 | a->mgf == aa->mgf && a->h == aa->h && |
c70a7c5c MW |
271 | a->bulk->ops == aa->bulk->ops && |
272 | a->bulk->ops->samealgsp(a->bulk, aa->bulk)); | |
b5c45da1 | 273 | } |
274 | ||
799e58b9 MW |
275 | /*----- Key data and key nodes --------------------------------------------*/ |
276 | ||
277 | typedef struct keyhalf { | |
278 | const char *kind; | |
279 | int (*load)(const kgops *, key_data *, kdata *, dstr *, dstr *); | |
280 | const char *kr; | |
281 | key_file *kf; | |
282 | fwatch w; | |
283 | sym_table tab; | |
284 | } keyhalf; | |
285 | ||
286 | /* --- @kh_loadpub@, @kh_loadpriv@ --- * | |
287 | * | |
288 | * Arguments: @const kgops *ko@ = key-group operations for key type | |
289 | * @key_data *d@ = key data object as stored in keyring | |
290 | * @kdata *kd@ = our key-data object to fill in | |
291 | * @dstr *t@ = the key tag name | |
292 | * @dstr *e@ = a string to write error tokens to | |
293 | * | |
294 | * Returns: Zero on success, @-1@ on error. | |
295 | * | |
296 | * Use: These functions handle the main difference between public and | |
297 | * private key halves. They are responsible for setting @g@, | |
298 | * @kpriv@ and @kpub@ appropriately in all keys, handling the | |
299 | * mismatch between the largely half-indifferent calling code | |
300 | * and the group-specific loading functions. | |
301 | * | |
302 | * The function @kh_loadpriv@ is also responsible for checking | |
303 | * the group for goodness. We don't bother checking public | |
304 | * keys, because each public key we actually end up using must | |
305 | * share a group with a private key which we'll already have | |
306 | * checked. | |
307 | */ | |
308 | ||
309 | static int kh_loadpub(const kgops *ko, key_data *d, kdata *kd, | |
310 | dstr *t, dstr *e) | |
311 | { | |
312 | int rc; | |
313 | ||
314 | if ((rc = ko->loadpub(d, kd, t, e)) != 0) | |
315 | goto fail_0; | |
316 | if (group_check(kd->g, kd->kpub)) { | |
73174919 | 317 | a_format(e, "bad-public-group-element", A_END); |
799e58b9 MW |
318 | goto fail_1; |
319 | } | |
320 | kd->kpriv = 0; | |
321 | return (0); | |
322 | ||
323 | fail_1: | |
324 | G_DESTROY(kd->g, kd->kpub); | |
325 | G_DESTROYGROUP(kd->g); | |
326 | fail_0: | |
327 | return (-1); | |
328 | } | |
329 | ||
330 | static int kh_loadpriv(const kgops *ko, key_data *d, kdata *kd, | |
331 | dstr *t, dstr *e) | |
332 | { | |
333 | int rc; | |
334 | const char *err; | |
335 | ||
336 | if ((rc = ko->loadpriv(d, kd, t, e)) != 0) | |
337 | goto fail_0; | |
338 | if ((err = G_CHECK(kd->g, &rand_global)) != 0) { | |
339 | a_format(e, "bad-group", "%s", err, A_END); | |
340 | goto fail_1; | |
341 | } | |
799e58b9 MW |
342 | return (0); |
343 | ||
344 | fail_1: | |
345 | mp_drop(kd->kpriv); | |
4155aec4 | 346 | G_DESTROY(kd->g, kd->kpub); |
799e58b9 MW |
347 | G_DESTROYGROUP(kd->g); |
348 | fail_0: | |
349 | return (-1); | |
350 | } | |
351 | ||
352 | static struct keyhalf | |
353 | priv = { "private", kh_loadpriv }, | |
354 | pub = { "public", kh_loadpub }; | |
410c8acf | 355 | |
356 | /* --- @keymoan@ --- * | |
357 | * | |
56814747 | 358 | * Arguments: @const char *file@ = name of the file |
e04c2d50 MW |
359 | * @int line@ = line number in file |
360 | * @const char *msg@ = error message | |
4d36660a | 361 | * @void *p@ = argument pointer (indicates which keyring) |
410c8acf | 362 | * |
e04c2d50 | 363 | * Returns: --- |
410c8acf | 364 | * |
e04c2d50 | 365 | * Use: Reports an error message about loading a key file. |
410c8acf | 366 | */ |
367 | ||
368 | static void keymoan(const char *file, int line, const char *msg, void *p) | |
f43df819 | 369 | { |
799e58b9 | 370 | keyhalf *kh = p; |
4d36660a MW |
371 | |
372 | if (!line) { | |
799e58b9 | 373 | a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file, |
4d36660a MW |
374 | "io-error", "?ERRNO", A_END); |
375 | } else { | |
799e58b9 | 376 | a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file, "line", "%d", line, |
4d36660a MW |
377 | "%s", msg, A_END); |
378 | } | |
f43df819 | 379 | } |
410c8acf | 380 | |
799e58b9 | 381 | /* --- @kh_reopen@ --- * |
fc5f4823 | 382 | * |
799e58b9 | 383 | * Arguments: @keyhalf *kh@ = pointer to keyhalf structure |
fc5f4823 | 384 | * |
799e58b9 | 385 | * Returns: Zero on success, @-1@ on error. |
fc5f4823 | 386 | * |
799e58b9 MW |
387 | * Use: Reopens the key file for the appropriate key half. If this |
388 | * fails, everything is left as it was; if it succeeds, then the | |
389 | * old file is closed (if it was non-null) and the new one put | |
390 | * in its place. | |
fc5f4823 MW |
391 | */ |
392 | ||
799e58b9 | 393 | static int kh_reopen(keyhalf *kh) |
fc5f4823 | 394 | { |
799e58b9 | 395 | key_file *kf = CREATE(key_file); |
fc5f4823 | 396 | |
799e58b9 MW |
397 | if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) { |
398 | a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr, | |
7f81b1b4 | 399 | "io-error", "?ERRNO", A_END); |
799e58b9 MW |
400 | DESTROY(kf); |
401 | return (-1); | |
402 | } else { | |
403 | if (kh->kf) { | |
404 | key_close(kh->kf); | |
405 | DESTROY(kh->kf); | |
406 | } | |
407 | kh->kf = kf; | |
408 | return (0); | |
409 | } | |
410 | } | |
fc5f4823 | 411 | |
799e58b9 MW |
412 | /* --- @kh_init@ --- * |
413 | * | |
414 | * Arguments: @keyhalf *kh@ = pointer to keyhalf structure to set up | |
415 | * @const char *kr@ = name of the keyring file | |
416 | * | |
417 | * Returns: --- | |
418 | * | |
419 | * Use: Initialize a keyhalf structure, maintaining the private or | |
420 | * public keys. Intended to be called during initialization: | |
421 | * exits if there's some kind of problem. | |
422 | */ | |
fc5f4823 | 423 | |
799e58b9 MW |
424 | static void kh_init(keyhalf *kh, const char *kr) |
425 | { | |
426 | kh->kr = kr; | |
427 | fwatch_init(&kh->w, kr); | |
428 | sym_create(&kh->tab); | |
429 | kh->kf = 0; | |
430 | ||
431 | if (kh_reopen(kh)) | |
432 | die(EXIT_FAILURE, "failed to load %s keyring `%s'", kh->kind, kr); | |
fc5f4823 MW |
433 | } |
434 | ||
799e58b9 | 435 | /* --- @kh_load@ --- * |
410c8acf | 436 | * |
799e58b9 MW |
437 | * Arguments: @keyhalf *kh@ = pointer to keyhalf |
438 | * @const char *tag@ = key tag to be loaded | |
439 | * @int complainp@ = whether to complain about missing keys | |
410c8acf | 440 | * |
799e58b9 MW |
441 | * Returns: Pointer to a @kdata@ structure if successful, or null on |
442 | * failure. | |
410c8acf | 443 | * |
799e58b9 MW |
444 | * Use: Attempts to load a key from the current key file. This |
445 | * function always reads data from the file: it's used when | |
446 | * there's a cache miss from @kh_find@, and when refreshing the | |
447 | * known keys in @kh_refresh@. The returned kdata has a | |
448 | * reference count of exactly 1, and has no home knode. | |
410c8acf | 449 | */ |
450 | ||
799e58b9 | 451 | static kdata *kh_load(keyhalf *kh, const char *tag, int complainp) |
410c8acf | 452 | { |
52c03a2a | 453 | dstr t = DSTR_INIT; |
4d36660a | 454 | dstr e = DSTR_INIT; |
799e58b9 MW |
455 | key *k; |
456 | key_data **d; | |
457 | kdata *kd; | |
458 | const char *ty; | |
459 | const kgops **ko; | |
52c03a2a | 460 | |
799e58b9 | 461 | /* --- Find the key and grab its tag --- */ |
52c03a2a | 462 | |
799e58b9 MW |
463 | if (key_qtag(kh->kf, tag, &t, &k, &d)) { |
464 | if (complainp) { | |
465 | a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr, | |
466 | "key-not-found", "%s", tag, A_END); | |
467 | } | |
468 | goto fail_0; | |
52c03a2a | 469 | } |
470 | ||
799e58b9 MW |
471 | /* --- Find the key's group type and the appropriate operations --- * |
472 | * | |
473 | * There are several places to look for the key type. The most obvious is | |
474 | * the `kx-group' key attribute. But there's also the key type itself, for | |
475 | * compatibility reasons. | |
476 | */ | |
52c03a2a | 477 | |
799e58b9 MW |
478 | ty = key_getattr(kh->kf, k, "kx-group"); |
479 | if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6; | |
480 | if (!ty) ty = "dh"; | |
52c03a2a | 481 | |
799e58b9 MW |
482 | for (ko = kgtab; *ko; ko++) |
483 | if (strcmp((*ko)->ty, ty) == 0) goto foundko; | |
484 | a_warn("KEYMGMT", "%s-keyring", kh->kind, | |
485 | "%s", kh->kr, "key", "%s", t.buf, | |
486 | "unknown-group-type", "%s", ty, A_END); | |
487 | goto fail_0; | |
488 | ||
489 | foundko: | |
490 | kd = CREATE(kdata); | |
491 | if (kh->load(*ko, *d, kd, &t, &e)) { | |
492 | a_warn("KEYMGMT", "%s-keyring", kh->kind, | |
01d12d8f | 493 | "%s", kh->kr, "key", "%s", t.buf, |
4d36660a | 494 | "*%s", e.buf, A_END); |
799e58b9 | 495 | goto fail_1; |
52c03a2a | 496 | } |
497 | ||
799e58b9 MW |
498 | if (algs_get(&kd->algs, &e, kh->kf, k) || |
499 | (kd->kpriv && algs_check(&kd->algs, &e, kd->g))) { | |
500 | a_warn("KEYMGMT", "%s-keyring", kh->kind, | |
501 | "%s", kh->kr, "key", "%s", t.buf, | |
4d36660a | 502 | "*%s", e.buf, A_END); |
799e58b9 | 503 | goto fail_2; |
410c8acf | 504 | } |
52c03a2a | 505 | |
799e58b9 MW |
506 | kd->tag = xstrdup(t.buf); |
507 | kd->indexsz = mp_octets(kd->g->r); | |
508 | kd->ref = 1; | |
509 | kd->kn = 0; | |
510 | kd->t_exp = k->exp; | |
52c03a2a | 511 | |
512 | IF_TRACING(T_KEYMGMT, { | |
799e58b9 | 513 | trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf); |
52c03a2a | 514 | IF_TRACING(T_CRYPTO, { |
799e58b9 MW |
515 | trace(T_CRYPTO, "crypto: r = %s", mpstr(kd->g->r)); |
516 | trace(T_CRYPTO, "crypto: h = %s", mpstr(kd->g->h)); | |
517 | if (kd->kpriv) | |
518 | trace(T_CRYPTO, "crypto: x = %s", mpstr(kd->kpriv)); | |
c70a7c5c | 519 | kd->algs.bulk->ops->tracealgs(kd->algs.bulk); |
52c03a2a | 520 | }) |
521 | }) | |
522 | ||
799e58b9 | 523 | goto done; |
52c03a2a | 524 | |
799e58b9 MW |
525 | fail_2: |
526 | if (kd->kpriv) mp_drop(kd->kpriv); | |
527 | G_DESTROY(kd->g, kd->kpub); | |
528 | G_DESTROYGROUP(kd->g); | |
529 | fail_1: | |
530 | DESTROY(kd); | |
531 | fail_0: | |
532 | kd = 0; | |
533 | done: | |
52c03a2a | 534 | dstr_destroy(&t); |
4d36660a | 535 | dstr_destroy(&e); |
799e58b9 | 536 | return (kd); |
410c8acf | 537 | } |
538 | ||
799e58b9 | 539 | /* --- @kh_find@ --- * |
410c8acf | 540 | * |
799e58b9 MW |
541 | * Arguments: @keyhalf *kh@ = pointer to the keyhalf |
542 | * @const char *tag@ = key to be obtained | |
543 | * @int complainp@ = whether to complain about missing keys | |
410c8acf | 544 | * |
799e58b9 | 545 | * Returns: A pointer to the kdata, or null on error. |
410c8acf | 546 | * |
799e58b9 MW |
547 | * Use: Obtains kdata, maybe from the cache. This won't update a |
548 | * stale cache entry, though @kh_refresh@ ought to have done | |
549 | * that already. The returned kdata object may be shared with | |
550 | * other users. (One of this function's responsibilities, over | |
551 | * @kh_load@, is to set the home knode of a freshly loaded | |
552 | * kdata.) | |
410c8acf | 553 | */ |
554 | ||
799e58b9 | 555 | static kdata *kh_find(keyhalf *kh, const char *tag, int complainp) |
410c8acf | 556 | { |
799e58b9 MW |
557 | knode *kn; |
558 | kdata *kd; | |
559 | unsigned f; | |
410c8acf | 560 | |
799e58b9 MW |
561 | kn = sym_find(&kh->tab, tag, -1, sizeof(knode), &f); |
562 | ||
563 | if (f) { | |
564 | if (kn->f & KNF_BROKEN) { | |
565 | T( if (complainp) | |
566 | trace(T_KEYMGMT, "keymgmt: key `%s' marked as broken", tag); ) | |
567 | return (0); | |
568 | } | |
569 | ||
570 | kd = kn->kd; | |
571 | if (kd) kd->ref++; | |
572 | T( trace(T_KEYMGMT, "keymgmt: %scache hit for key `%s'", | |
573 | kd ? "" : "negative ", tag); ) | |
574 | return (kd); | |
575 | } else { | |
576 | kd = kh_load(kh, tag, complainp); | |
577 | kn->kd = kd; | |
578 | kn->kh = kh; | |
579 | kn->f = 0; | |
580 | if (!kd) | |
581 | kn->f |= KNF_BROKEN; | |
582 | else { | |
583 | kd->kn = kn; | |
584 | kd->ref++; | |
585 | } | |
586 | return (kd); | |
410c8acf | 587 | } |
410c8acf | 588 | } |
589 | ||
799e58b9 | 590 | /* --- @kh_refresh@ --- * |
410c8acf | 591 | * |
799e58b9 | 592 | * Arguments: @keyhalf *kh@ = pointer to the keyhalf |
410c8acf | 593 | * |
799e58b9 MW |
594 | * Returns: Zero if nothing needs to be done; nonzero if peers should |
595 | * refresh their keys. | |
410c8acf | 596 | * |
799e58b9 MW |
597 | * Use: Refreshes cached keys from files. |
598 | * | |
599 | * Each active knode is examined to see if a new key is | |
600 | * available: the return value is nonzero if any new keys are. | |
601 | * A key is considered new if its algorithms, public key, or | |
602 | * expiry time are/is different. | |
603 | * | |
604 | * Stub knodes (with no kdata attached) are removed, so that a | |
605 | * later retry can succeed if the file has been fixed. (This | |
606 | * doesn't count as a change, since no peers should be relying | |
607 | * on a nonexistent key.) | |
410c8acf | 608 | */ |
609 | ||
799e58b9 | 610 | static int kh_refresh(keyhalf *kh) |
410c8acf | 611 | { |
799e58b9 MW |
612 | knode *kn; |
613 | kdata *kd; | |
614 | sym_iter i; | |
615 | int changep = 0; | |
616 | ||
617 | if (!fwatch_update(&kh->w, kh->kr) || kh_reopen(kh)) | |
618 | return (0); | |
619 | ||
620 | T( trace(T_KEYMGMT, "keymgmt: rescan %s keyring `%s'", kh->kind, kh->kr); ) | |
621 | for (sym_mkiter(&i, &kh->tab); (kn = sym_next(&i)) != 0; ) { | |
622 | if (!kn->kd) { | |
623 | T( trace(T_KEYMGMT, "keymgmt: discard stub entry for key `%s'", | |
624 | SYM_NAME(kn)); ) | |
625 | sym_remove(&kh->tab, kn); | |
626 | continue; | |
627 | } | |
628 | if ((kd = kh_load(kh, SYM_NAME(kn), 1)) == 0) { | |
629 | if (!(kn->f & KNF_BROKEN)) { | |
630 | T( trace(T_KEYMGMT, "keymgmt: failed to load new key `%s': " | |
631 | "marking it as broken", | |
632 | SYM_NAME(kn)); ) | |
633 | kn->f |= KNF_BROKEN; | |
634 | } | |
635 | continue; | |
636 | } | |
637 | kn->f &= ~KNF_BROKEN; | |
638 | if (kd->t_exp == kn->kd->t_exp && | |
639 | km_samealgsp(kd, kn->kd) && | |
640 | G_EQ(kd->g, kd->kpub, kn->kd->kpub)) { | |
641 | T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); ) | |
642 | continue; | |
643 | } | |
644 | T( trace(T_KEYMGMT, "keymgmt: loaded new version of key `%s'", | |
645 | SYM_NAME(kn)); ) | |
646 | km_unref(kn->kd); | |
647 | kd->kn = kn; | |
648 | kn->kd = kd; | |
649 | changep = 1; | |
650 | } | |
410c8acf | 651 | |
799e58b9 MW |
652 | return (changep); |
653 | } | |
410c8acf | 654 | |
799e58b9 | 655 | /*----- Main code ---------------------------------------------------------*/ |
410c8acf | 656 | |
799e58b9 MW |
657 | const char *tag_priv; |
658 | kdata *master; | |
410c8acf | 659 | |
410c8acf | 660 | /* --- @km_init@ --- * |
661 | * | |
799e58b9 MW |
662 | * Arguments: @const char *privkr@ = private keyring file |
663 | * @const char *pubkr@ = public keyring file | |
664 | * @const char *ptag@ = default private-key tag | |
410c8acf | 665 | * |
666 | * Returns: --- | |
667 | * | |
799e58b9 MW |
668 | * Use: Initializes the key-management machinery, loading the |
669 | * keyrings and so on. | |
410c8acf | 670 | */ |
671 | ||
799e58b9 | 672 | void km_init(const char *privkr, const char *pubkr, const char *ptag) |
410c8acf | 673 | { |
b5c45da1 | 674 | const gchash *const *hh; |
410c8acf | 675 | |
b5c45da1 | 676 | for (hh = ghashtab; *hh; hh++) { |
677 | if ((*hh)->hashsz > MAXHASHSZ) { | |
678 | die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d", | |
679 | (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ); | |
680 | } | |
681 | } | |
682 | ||
799e58b9 MW |
683 | kh_init(&priv, privkr); |
684 | kh_init(&pub, pubkr); | |
685 | ||
686 | tag_priv = ptag; | |
687 | if ((master = km_findpriv(ptag)) == 0) exit(EXIT_FAILURE); | |
410c8acf | 688 | } |
689 | ||
799e58b9 | 690 | /* --- @km_reload@ --- * |
410c8acf | 691 | * |
799e58b9 | 692 | * Arguments: --- |
410c8acf | 693 | * |
799e58b9 | 694 | * Returns: Zero if OK, nonzero to force reloading of keys. |
410c8acf | 695 | * |
799e58b9 | 696 | * Use: Checks the keyrings to see if they need reloading. |
410c8acf | 697 | */ |
698 | ||
799e58b9 | 699 | int km_reload(void) |
410c8acf | 700 | { |
799e58b9 MW |
701 | int changep = 0; |
702 | kdata *kd; | |
703 | ||
704 | if (kh_refresh(&priv)) { | |
705 | changep = 1; | |
706 | kd = master->kn->kd; | |
35c8b547 | 707 | if (kd != master) { |
799e58b9 MW |
708 | km_unref(master); |
709 | km_ref(kd); | |
710 | master = kd; | |
711 | } | |
712 | } | |
713 | if (kh_refresh(&pub)) | |
714 | changep = 1; | |
715 | return (changep); | |
716 | } | |
52c03a2a | 717 | |
799e58b9 MW |
718 | /* --- @km_findpub@, @km_findpriv@ --- * |
719 | * | |
720 | * Arguments: @const char *tag@ = key tag to load | |
721 | * | |
722 | * Returns: Pointer to the kdata object if successful, or null on error. | |
723 | * | |
724 | * Use: Fetches a public or private key from the keyring. | |
725 | */ | |
52c03a2a | 726 | |
799e58b9 | 727 | kdata *km_findpub(const char *tag) { return (kh_find(&pub, tag, 1)); } |
410c8acf | 728 | |
799e58b9 MW |
729 | kdata *km_findpriv(const char *tag) |
730 | { | |
731 | kdata *kd; | |
52c03a2a | 732 | |
799e58b9 MW |
733 | /* Unpleasantness for the sake of compatibility. */ |
734 | if (!tag && (kd = kh_find(&priv, "tripe", 0)) != 0) return (kd); | |
735 | else return (kh_find(&priv, tag ? tag : "tripe-dh", 1)); | |
736 | } | |
52c03a2a | 737 | |
799e58b9 MW |
738 | /* --- @km_tag@ --- * |
739 | * | |
740 | * Arguments: @kdata *kd@ - pointer to the kdata object | |
741 | * | |
742 | * Returns: A pointer to the short tag by which the kdata was loaded. | |
743 | */ | |
52c03a2a | 744 | |
799e58b9 | 745 | const char *km_tag(kdata *kd) { return (SYM_NAME(kd->kn)); } |
52c03a2a | 746 | |
799e58b9 MW |
747 | /* --- @km_ref@ --- * |
748 | * | |
749 | * Arguments: @kdata *kd@ = pointer to the kdata object | |
750 | * | |
751 | * Returns: --- | |
752 | * | |
753 | * Use: Claim a new reference to a kdata object. | |
754 | */ | |
52c03a2a | 755 | |
799e58b9 | 756 | void km_ref(kdata *kd) { kd->ref++; } |
52c03a2a | 757 | |
799e58b9 MW |
758 | /* --- @km_unref@ --- * |
759 | * | |
760 | * Arguments: @kdata *kd@ = pointer to the kdata object | |
761 | * | |
762 | * Returns: --- | |
763 | * | |
764 | * Use: Releases a reference to a kdata object. | |
765 | */ | |
52c03a2a | 766 | |
799e58b9 MW |
767 | void km_unref(kdata *kd) |
768 | { | |
769 | if (--kd->ref) return; | |
770 | if (kd->kpriv) mp_drop(kd->kpriv); | |
771 | G_DESTROY(kd->g, kd->kpub); | |
772 | xfree(kd->tag); | |
773 | G_DESTROYGROUP(kd->g); | |
5290b9d5 | 774 | DESTROY(kd); |
799e58b9 MW |
775 | } |
776 | ||
410c8acf | 777 | /*----- That's all, folks -------------------------------------------------*/ |