3 * $Id: keymgmt.c,v 1.5 2004/04/08 01:36:17 mdw Exp $
5 * Key loading and storing
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
38 /*----- Static variables --------------------------------------------------*/
40 static key_file *kf_pub;
41 static const char *kr_priv, *kr_pub, *tag_priv;
42 static fwatch w_priv, w_pub;
44 /*----- Key groups --------------------------------------------------------*/
46 typedef struct kgops {
48 const char *(*loadpriv)(key_data *, group **, mp **, dstr *);
49 const char *(*loadpub)(key_data *, group **, ge **, dstr *);
52 /* --- Diffie-Hellman --- */
54 static const char *kgdh_priv(key_data *kd, group **g, mp **x, dstr *t)
56 key_packstruct kps[DH_PRIVFETCHSZ];
62 kp = key_fetchinit(dh_privfetch, kps, &dp);
63 if ((rc = key_unpack(kp, kd, t)) != 0) {
67 *g = group_prime(&dp.dp);
75 static const char *kgdh_pub(key_data *kd, group **g, ge **p, dstr *t)
77 key_packstruct kps[DH_PUBFETCHSZ];
83 kp = key_fetchinit(dh_pubfetch, kps, &dp);
84 if ((rc = key_unpack(kp, kd, t)) != 0) {
88 *g = group_prime(&dp.dp);
90 if (G_FROMINT(*g, *p, dp.y)) {
91 e = "bad public value";
100 static const kgops kgdh_ops = { "tripe-dh", kgdh_priv, kgdh_pub };
102 /* --- Elliptic curve --- */
104 static const char *kgec_priv(key_data *kd, group **g, mp **x, dstr *t)
106 key_packstruct kps[EC_PRIVFETCHSZ];
113 kp = key_fetchinit(ec_privfetch, kps, &ep);
114 if ((rc = key_unpack(kp, kd, t)) != 0) {
115 e = key_strerror(rc);
118 if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
128 static const char *kgec_pub(key_data *kd, group **g, ge **p, dstr *t)
130 key_packstruct kps[EC_PUBFETCHSZ];
137 kp = key_fetchinit(ec_pubfetch, kps, &ep);
138 if ((rc = key_unpack(kp, kd, t)) != 0) {
139 e = key_strerror(rc);
142 if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
146 if (G_FROMEC(*g, *p, &ep.p)) {
147 e = "bad public point";
156 static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
158 /* --- Table of supported key types --- */
160 static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
162 /*----- Main code ---------------------------------------------------------*/
164 /* --- @keymoan@ --- *
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
173 * Use: Reports an error message about loading a key file.
176 static void keymoan(const char *file, int line, const char *msg, void *p)
177 { a_warn("%s:%i: error: %s", file, line, msg); }
179 /* --- @loadpriv@ --- *
181 * Arguments: @dstr *d@ = string to write errors in
183 * Returns: Zero if OK, nonzero on error.
185 * Use: Loads the private key from its keyfile.
188 static int loadpriv(dstr *d)
200 /* --- Open the private key file --- */
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));
208 /* --- Find the private key --- */
210 if (key_qtag(&kf, tag_priv, &t, &k, &kd)) {
211 dstr_putf(d, "private key `%s' not found in keyring `%s'",
216 /* --- Look up the key type in the table --- */
218 for (ko = kgtab; *ko; ko++) {
219 if (strcmp((*ko)->ty, k->type) == 0)
222 dstr_putf(d, "private key `%s' has unknown type `%s'", t.buf, k->type);
226 /* --- Load the key --- */
228 if ((e = (*ko)->loadpriv(kd, &g, &x, &t)) != 0) {
229 dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
233 /* --- Check that the key is sensible --- */
235 if ((e = G_CHECK(g, &rand_global)) != 0) {
236 dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
240 /* --- Good, we're happy --- *
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.
249 if (!group_samep(g, gg)) {
250 dstr_putf(d, "private key `%s' has different group", t.buf);
258 /* --- Dump out the group --- */
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));
269 /* --- Success! --- */
275 /* --- Tidy up --- */
282 if (g) G_DESTROYGROUP(g);
286 /* --- @loadpub@ --- *
288 * Arguments: @dstr *d@ = string to write errors to
290 * Returns: Zero if OK, nonzero on error.
292 * Use: Reloads the public keyring.
295 static int loadpub(dstr *d)
297 key_file *kf = CREATE(key_file);
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));
306 T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
310 /* --- @km_interval@ --- *
314 * Returns: Zero if OK, nonzero to force reloading of keys.
316 * Use: Called on the interval timer to perform various useful jobs.
319 int km_interval(void)
325 /* --- Check the private key first --- */
327 if (fwatch_update(&w_priv, kr_priv)) {
328 T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
331 a_warn("%s -- ignoring changes", d.buf);
336 /* --- Now check the public keys --- */
338 if (fwatch_update(&w_pub, kr_pub)) {
339 T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
343 a_warn("%s -- ignoring changes", d.buf);
356 /* --- @km_init@ --- *
358 * Arguments: @const char *priv@ = private keyring file
359 * @const char *pub@ = public keyring file
360 * @const char *tag@ = tag to load
364 * Use: Initializes, and loads the private key.
367 void km_init(const char *priv, const char *pub, const char *tag)
374 fwatch_init(&w_priv, kr_priv);
375 fwatch_init(&w_pub, kr_pub);
379 die(EXIT_FAILURE, "%s", d.buf);
381 die(EXIT_FAILURE, "%s", d.buf);
384 /* --- @km_getpubkey@ --- *
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
390 * Returns: Zero if OK, nonzero if it failed.
392 * Use: Fetches a public key from the keyring.
395 int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
406 /* --- Find the key --- */
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);
413 /* --- Look up the key type in the table --- */
415 for (ko = kgtab; *ko; ko++) {
416 if (strcmp((*ko)->ty, k->type) == 0)
419 a_warn("public key `%s' has unknown type `%s'", t.buf, k->type);
423 /* --- Load the key --- */
425 if ((e = (*ko)->loadpub(kd, &g, &p, &t)) != 0) {
426 a_warn("error reading public key `%s': %s", t.buf, e);
430 /* --- Ensure that the group is correct --- *
432 * Dodginess! We assume that if this works, our global group is willing to
433 * adopt this public element. Probably reasonable.
436 if (!group_samep(gg, g)) {
437 a_warn("public key `%s' has incorrect group", t.buf);
441 /* --- Check the public group element --- */
443 if (group_check(gg, p)) {
444 a_warn("public key `%s' has bad public group element", t.buf);
448 /* --- Dump the public key --- */
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));
455 /* --- OK, accept the public key --- */
461 /* --- Tidy up --- */
464 if (p) G_DESTROY(g, p);
465 if (g) G_DESTROYGROUP(g);
470 /*----- That's all, folks -------------------------------------------------*/