chiark / gitweb /
Expunge revision histories in files.
[tripe] / keymgmt.c
1 /* -*-c-*-
2  *
3  * $Id: keymgmt.c,v 1.5 2004/04/08 01:36:17 mdw Exp $
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
29 /*----- Header files ------------------------------------------------------*/
30
31 #include "tripe.h"
32
33 /*----- Global variables --------------------------------------------------*/
34
35 group *gg;
36 mp *kpriv;
37
38 /*----- Static variables --------------------------------------------------*/
39
40 static key_file *kf_pub;
41 static const char *kr_priv, *kr_pub, *tag_priv;
42 static fwatch w_priv, w_pub;
43
44 /*----- Key groups --------------------------------------------------------*/
45
46 typedef struct kgops {
47   const char *ty;
48   const char *(*loadpriv)(key_data *, group **, mp **, dstr *);
49   const char *(*loadpub)(key_data *, group **, ge **, dstr *);
50 } kgops;
51
52 /* --- Diffie-Hellman --- */
53
54 static const char *kgdh_priv(key_data *kd, group **g, mp **x, dstr *t)
55 {
56   key_packstruct kps[DH_PRIVFETCHSZ];
57   key_packdef *kp;
58   dh_priv dp;
59   const char *e;
60   int rc;
61
62   kp = key_fetchinit(dh_privfetch, kps, &dp);
63   if ((rc = key_unpack(kp, kd, t)) != 0) {
64     e = key_strerror(rc);
65     goto done;
66   }
67   *g = group_prime(&dp.dp);
68   *x = MP_COPY(dp.x);
69   e = 0;
70 done:
71   key_fetchdone(kp);
72   return (e);
73 }
74
75 static const char *kgdh_pub(key_data *kd, group **g, ge **p, dstr *t)
76 {
77   key_packstruct kps[DH_PUBFETCHSZ];
78   key_packdef *kp;
79   dh_pub dp;
80   const char *e;
81   int rc;
82
83   kp = key_fetchinit(dh_pubfetch, kps, &dp);
84   if ((rc = key_unpack(kp, kd, t)) != 0) {
85     e = key_strerror(rc);
86     goto done;
87   }
88   *g = group_prime(&dp.dp);
89   *p = G_CREATE(*g);
90   if (G_FROMINT(*g, *p, dp.y)) {
91     e = "bad public value";
92     goto done;
93   }
94   e = 0;
95 done:
96   key_fetchdone(kp);
97   return (e);
98 }
99
100 static const kgops kgdh_ops = { "tripe-dh", kgdh_priv, kgdh_pub };
101
102 /* --- Elliptic curve --- */
103
104 static const char *kgec_priv(key_data *kd, group **g, mp **x, dstr *t)
105 {
106   key_packstruct kps[EC_PRIVFETCHSZ];
107   key_packdef *kp;
108   ec_priv ep;
109   ec_info ei;
110   const char *e;
111   int rc;
112
113   kp = key_fetchinit(ec_privfetch, kps, &ep);
114   if ((rc = key_unpack(kp, kd, t)) != 0) {
115     e = key_strerror(rc);
116     goto done;
117   }
118   if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
119     goto done;
120   *g = group_ec(&ei);
121   *x = MP_COPY(ep.x);
122   e = 0;
123 done:
124   key_fetchdone(kp);
125   return (e);
126 }
127
128 static const char *kgec_pub(key_data *kd, group **g, ge **p, dstr *t)
129 {
130   key_packstruct kps[EC_PUBFETCHSZ];
131   key_packdef *kp;
132   ec_pub ep;
133   ec_info ei;
134   const char *e;
135   int rc;
136
137   kp = key_fetchinit(ec_pubfetch, kps, &ep);
138   if ((rc = key_unpack(kp, kd, t)) != 0) {
139     e = key_strerror(rc);
140     goto done;
141   }
142   if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
143     goto done;
144   *g = group_ec(&ei);
145   *p = G_CREATE(*g);
146   if (G_FROMEC(*g, *p, &ep.p)) {
147     e = "bad public point";
148     goto done;
149   }
150   e = 0;
151 done:
152   key_fetchdone(kp);
153   return (e);
154 }
155
156 static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
157
158 /* --- Table of supported key types --- */
159
160 static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
161
162 /*----- Main code ---------------------------------------------------------*/
163
164 /* --- @keymoan@ --- *
165  *
166  * Arguments:   @const char *file@ = name of the file
167  *              @int line@ = line number in file
168  *              @const char *msg@ = error message
169  *              @void *p@ = argument pointer
170  *
171  * Returns:     ---
172  *
173  * Use:         Reports an error message about loading a key file.
174  */
175
176 static void keymoan(const char *file, int line, const char *msg, void *p)
177   { a_warn("%s:%i: error: %s", file, line, msg); }
178
179 /* --- @loadpriv@ --- *
180  *
181  * Arguments:   @dstr *d@ = string to write errors in
182  *
183  * Returns:     Zero if OK, nonzero on error.
184  *
185  * Use:         Loads the private key from its keyfile.
186  */
187
188 static int loadpriv(dstr *d)
189 {
190   key_file kf;
191   key *k;
192   key_data *kd;
193   dstr t = DSTR_INIT;
194   group *g = 0;
195   mp *x = 0;
196   int rc = -1;
197   const kgops **ko;
198   const char *e;
199
200   /* --- Open the private key file --- */
201
202   if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, 0)) {
203     dstr_putf(d, "error reading private keyring `%s': %s",
204               kr_priv, strerror(errno));
205     goto done_0;
206   }
207
208   /* --- Find the private key --- */
209
210   if (key_qtag(&kf, tag_priv, &t, &k, &kd)) {
211     dstr_putf(d, "private key `%s' not found in keyring `%s'",
212               tag_priv, kr_priv);
213     goto done_1;
214   }
215
216   /* --- Look up the key type in the table --- */
217
218   for (ko = kgtab; *ko; ko++) {
219     if (strcmp((*ko)->ty, k->type) == 0)
220       goto tymatch;
221   }
222   dstr_putf(d, "private key `%s' has unknown type `%s'", t.buf, k->type);
223   goto done_1;
224 tymatch:;
225
226   /* --- Load the key --- */
227
228   if ((e = (*ko)->loadpriv(kd, &g, &x, &t)) != 0) {
229     dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
230     goto done_1;
231   }
232
233   /* --- Check that the key is sensible --- */
234
235   if ((e = G_CHECK(g, &rand_global)) != 0) {
236     dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
237     goto done_1;
238   }
239
240   /* --- Good, we're happy --- *
241    *
242    * Dodginess!  We change the group over here, but don't free any old group
243    * elements.  This assumes that the new group is basically the same as the
244    * old one, and will happily adopt the existing elements.  If it isn't,
245    * then we lose badly.  Check this, then.
246    */
247
248   if (gg) {
249     if (!group_samep(g, gg)) {
250       dstr_putf(d, "private key `%s' has different group", t.buf);
251       goto done_1;
252     }
253     G_DESTROYGROUP(gg);
254   }
255   if (kpriv)
256     mp_drop(kpriv);
257
258   /* --- Dump out the group --- */
259
260   IF_TRACING(T_KEYMGMT, {
261     trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", t.buf);
262     IF_TRACING(T_CRYPTO, {
263       trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
264       trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
265       trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
266     })
267   })
268
269   /* --- Success! --- */
270
271   gg = g; g = 0;
272   kpriv = x; x = 0;
273   rc = 0;
274
275   /* --- Tidy up --- */
276
277 done_1:
278   key_close(&kf);
279 done_0:
280   dstr_destroy(&t);
281   if (x) mp_drop(x);
282   if (g) G_DESTROYGROUP(g);
283   return (rc);
284 }
285
286 /* --- @loadpub@ --- *
287  *
288  * Arguments:   @dstr *d@ = string to write errors to
289  *
290  * Returns:     Zero if OK, nonzero on error.
291  *
292  * Use:         Reloads the public keyring.
293  */
294
295 static int loadpub(dstr *d)
296 {
297   key_file *kf = CREATE(key_file);
298
299   if (key_open(kf, kr_pub, KOPEN_READ, keymoan, 0)) {
300     dstr_putf(d, "error reading public keyring `%s': %s",
301               kr_pub, strerror(errno));
302     DESTROY(kf);
303     return (-1);
304   }
305   kf_pub = kf;
306   T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
307   return (0);
308 }
309
310 /* --- @km_interval@ --- *
311  *
312  * Arguments:   ---
313  *
314  * Returns:     Zero if OK, nonzero to force reloading of keys.
315  *
316  * Use:         Called on the interval timer to perform various useful jobs.
317  */
318
319 int km_interval(void)
320 {
321   dstr d = DSTR_INIT;
322   key_file *kf;
323   int reload = 0;
324
325   /* --- Check the private key first --- */
326
327   if (fwatch_update(&w_priv, kr_priv)) {
328     T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
329     DRESET(&d);
330     if (loadpriv(&d))
331       a_warn("%s -- ignoring changes", d.buf);
332     else
333       reload = 1;
334   }
335
336   /* --- Now check the public keys --- */
337
338   if (fwatch_update(&w_pub, kr_pub)) {
339     T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
340     kf = kf_pub;
341     DRESET(&d);
342     if (loadpub(&d))
343       a_warn("%s -- ignoring changes", d.buf);
344     else {
345       reload = 1;
346       key_close(kf);
347       DESTROY(kf);
348     }
349   }
350
351   /* --- Done --- */
352
353   return (reload);
354 }
355
356 /* --- @km_init@ --- *
357  *
358  * Arguments:   @const char *priv@ = private keyring file
359  *              @const char *pub@ = public keyring file
360  *              @const char *tag@ = tag to load
361  *
362  * Returns:     ---
363  *
364  * Use:         Initializes, and loads the private key.
365  */
366
367 void km_init(const char *priv, const char *pub, const char *tag)
368 {
369   dstr d = DSTR_INIT;
370
371   kr_priv = priv;
372   kr_pub = pub;
373   tag_priv = tag;
374   fwatch_init(&w_priv, kr_priv);
375   fwatch_init(&w_pub, kr_pub);
376
377   DRESET(&d);
378   if (loadpriv(&d))
379     die(EXIT_FAILURE, "%s", d.buf);
380   if (loadpub(&d))
381     die(EXIT_FAILURE, "%s", d.buf);
382 }
383
384 /* --- @km_getpubkey@ --- *
385  *
386  * Arguments:   @const char *tag@ = public key tag to load
387  *              @ge *kpub@ = where to put the public key
388  *              @time_t *t_exp@ = where to put the expiry time
389  *
390  * Returns:     Zero if OK, nonzero if it failed.
391  *
392  * Use:         Fetches a public key from the keyring.
393  */
394
395 int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
396 {
397   key *k;
398   key_data *kd;
399   dstr t = DSTR_INIT;
400   const kgops **ko;
401   const char *e;
402   group *g = 0;
403   ge *p = 0;
404   int rc = -1;
405
406   /* --- Find the key --- */
407
408   if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
409     a_warn("private key `%s' not found in keyring `%s'", tag_priv, kr_priv);
410     goto done;
411   }
412
413   /* --- Look up the key type in the table --- */
414
415   for (ko = kgtab; *ko; ko++) {
416     if (strcmp((*ko)->ty, k->type) == 0)
417       goto tymatch;
418   }
419   a_warn("public key `%s' has unknown type `%s'", t.buf, k->type);
420   goto done;
421 tymatch:;
422
423   /* --- Load the key --- */
424
425   if ((e = (*ko)->loadpub(kd, &g, &p, &t)) != 0) {
426     a_warn("error reading public key `%s': %s", t.buf, e);
427     goto done;
428   }
429
430   /* --- Ensure that the group is correct --- *
431    *
432    * Dodginess!  We assume that if this works, our global group is willing to
433    * adopt this public element.  Probably reasonable.
434    */
435
436   if (!group_samep(gg, g)) {
437     a_warn("public key `%s' has incorrect group", t.buf);
438     goto done;
439   }
440
441   /* --- Check the public group element --- */
442
443   if (group_check(gg, p)) {
444     a_warn("public key `%s' has bad public group element", t.buf);
445     goto done;
446   }
447
448   /* --- Dump the public key --- */
449
450   IF_TRACING(T_KEYMGMT, {
451     trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
452     trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
453   })
454
455   /* --- OK, accept the public key --- */
456
457   *t_exp = k->exp;
458   G_COPY(gg, kpub, p);
459   rc = 0;
460
461   /* --- Tidy up --- */
462
463 done:
464   if (p) G_DESTROY(g, p);
465   if (g) G_DESTROYGROUP(g);
466   dstr_destroy(&t);
467   return (rc);
468 }
469
470 /*----- That's all, folks -------------------------------------------------*/