3 * Key loading and storing
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Global variables --------------------------------------------------*/
39 /*----- Static variables --------------------------------------------------*/
41 static key_file *kf_pub;
42 static const char *kr_priv, *kr_pub, *tag_priv;
43 static fwatch w_priv, w_pub;
45 /*----- Key groups --------------------------------------------------------*/
47 typedef struct kgops {
49 int (*loadpriv)(key_data *, group **, mp **, dstr *, dstr *);
50 int (*loadpub)(key_data *, group **, ge **, dstr *, dstr *);
53 /* --- Diffie-Hellman --- */
55 static int kgdh_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
57 key_packstruct kps[DH_PRIVFETCHSZ];
62 kp = key_fetchinit(dh_privfetch, kps, &dp);
63 if ((rc = key_unpack(kp, kd, t)) != 0) {
64 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
67 *g = group_prime(&dp.dp);
78 static int kgdh_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
80 key_packstruct kps[DH_PUBFETCHSZ];
85 kp = key_fetchinit(dh_pubfetch, kps, &dp);
86 if ((rc = key_unpack(kp, kd, t)) != 0) {
87 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
90 *g = group_prime(&dp.dp);
92 if (G_FROMINT(*g, *p, dp.y)) {
93 a_format(e, "bad-public-vector", A_END);
108 static const kgops kgdh_ops = { "dh", kgdh_priv, kgdh_pub };
110 /* --- Elliptic curve --- */
112 static int kgec_priv(key_data *kd, group **g, mp **x, dstr *t, dstr *e)
114 key_packstruct kps[EC_PRIVFETCHSZ];
121 kp = key_fetchinit(ec_privfetch, kps, &ep);
122 if ((rc = key_unpack(kp, kd, t)) != 0) {
123 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
126 if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
127 a_format(e, "decode-failed", "%s", err, A_END);
141 static int kgec_pub(key_data *kd, group **g, ge **p, dstr *t, dstr *e)
143 key_packstruct kps[EC_PUBFETCHSZ];
150 kp = key_fetchinit(ec_pubfetch, kps, &ep);
151 if ((rc = key_unpack(kp, kd, t)) != 0) {
152 a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END);
155 if ((err = ec_getinfo(&ei, ep.cstr)) != 0) {
156 a_format(e, "decode-failed", "%s", err, A_END);
161 if (G_FROMEC(*g, *p, &ep.p)) {
162 a_format(e, "bad-public-vector", A_END);
177 static const kgops kgec_ops = { "ec", kgec_priv, kgec_pub };
179 /* --- Table of supported key types --- */
181 static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
183 /*----- Algswitch stuff ---------------------------------------------------*/
185 /* --- @algs_get@ --- *
187 * Arguments: @algswitch *a@ = where to put the algorithms
188 * @dstr *e@ = where to write errror tokens
189 * @key_file *kf@ = key file
190 * @key *k@ = key to inspect
192 * Returns: Zero if OK; nonzero on error.
194 * Use: Extracts an algorithm choice from a key.
197 static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
204 /* --- Symmetric encryption for bulk data --- */
206 if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc";
207 if ((a->c = gcipher_byname(p)) == 0) {
208 a_format(e, "unknown-cipher", "%s", p, A_END);
212 /* --- Hash function --- */
214 if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
215 if ((a->h = ghash_byname(p)) == 0) {
216 a_format(e, "unknown-hash", "%s", p, A_END);
220 /* --- Symmetric encryption for key derivation --- */
222 if ((p = key_getattr(kf, k, "mgf")) == 0) {
224 dstr_putf(&d, "%s-mgf", a->h->name);
227 if ((a->mgf = gcipher_byname(p)) == 0) {
228 a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
232 /* --- Message authentication for bulk data --- */
234 if ((p = key_getattr(kf, k, "mac")) != 0) {
237 if ((q = strchr(d.buf, '/')) != 0)
239 if ((a->m = gmac_byname(d.buf)) == 0) {
240 a_format(e, "unknown-mac", "%s", d.buf, A_END);
244 a->tagsz = a->m->hashsz;
246 unsigned long n = strtoul(q, &qq, 0);
248 a_format(e, "bad-tag-length-string", "%s", q, A_END);
251 if (n%8 || n/8 > a->m->hashsz) {
252 a_format(e, "bad-tag-length", "%lu", n, A_END);
259 dstr_putf(&d, "%s-hmac", a->h->name);
260 if ((a->m = gmac_byname(d.buf)) == 0) {
261 a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END);
264 a->tagsz = a->h->hashsz/2;
273 /* --- @algs_check@ --- *
275 * Arguments: @algswitch *a@ = a choice of algorithms
276 * @dstr *e@ = where to write error tokens
277 * @const group *g@ = the group we're working in
279 * Returns: Zero if OK; nonzero on error.
281 * Use: Checks an algorithm choice for sensibleness. This also
282 * derives some useful information from the choices, and you
283 * must call this before committing the algorithm selection
284 * for use by @keyset@ functions.
287 static int algs_check(algswitch *a, dstr *e, const group *g)
289 /* --- Derive the key sizes --- *
291 * Must ensure that we have non-empty keys. This isn't ideal, but it
292 * provides a handy sanity check. Also must be based on a 64- or 128-bit
293 * block cipher or we can't do the data expiry properly.
296 a->hashsz = a->h->hashsz;
297 if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) {
298 a_format(e, "cipher", "%s", a->c->name,
299 "no-key-size", "%lu", (unsigned long)a->hashsz,
303 if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) {
304 a_format(e, "mac", "%s", a->m->name,
305 "no-key-size", "%lu", (unsigned long)a->hashsz,
310 /* --- Derive the data limit --- */
312 if (a->c->blksz < 16) a->expsz = MEG(64);
313 else a->expsz = MEG(2048);
315 /* --- Ensure the MGF accepts hashes as keys --- */
317 if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
318 a_format(e, "mgf", "%s", a->mgf->name,
319 "restrictive-key-schedule",
324 /* --- All ship-shape and Bristol-fashion --- */
329 /* --- @algs_samep@ --- *
331 * Arguments: @const algswitch *a, *aa@ = two algorithm selections
333 * Returns: Nonzero if the two selections are the same.
335 * Use: Checks sameness of algorithm selections: used to ensure that
336 * peers are using sensible algorithms.
339 static int algs_samep(const algswitch *a, const algswitch *aa)
341 return (a->c == aa->c && a->mgf == aa->mgf && a->h == aa->h &&
342 a->m == aa->m && a->tagsz == aa->tagsz);
345 /*----- Main code ---------------------------------------------------------*/
347 /* --- @keymoan@ --- *
349 * Arguments: @const char *file@ = name of the file
350 * @int line@ = line number in file
351 * @const char *msg@ = error message
352 * @void *p@ = argument pointer (indicates which keyring)
356 * Use: Reports an error message about loading a key file.
359 static void keymoan(const char *file, int line, const char *msg, void *p)
361 const char *kind = p;
364 a_warn("KEYMGMT", "%s-keyring", kind, "%s", file,
365 "io-error", "?ERRNO", A_END);
367 a_warn("KEYMGMT", "%s-keyring", kind, "%s", file, "line", "%d", line,
374 * Arguments: @key_file *kf@ = pointer to key file
375 * @key *k@ = pointer to key
376 * @const char **tyr@ = where to put the type string
378 * Returns: Pointer to indicated key-group options, or null.
380 * Use: Looks up a key's group indicator and tries to find a matching
384 static const kgops *keykg(key_file *kf, key *k, const char **tyr)
389 /* --- Look up the key type in the table --- *
391 * There are several places to look for this. The most obvious is the
392 * `kx-group' key attribute. But there's also the key type itself.
395 ty = key_getattr(kf, k, "kx-group");
396 if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
400 for (ko = kgtab; *ko; ko++) {
401 if (strcmp((*ko)->ty, ty) == 0)
407 /* --- @loadpriv@ --- *
409 * Arguments: @dstr *d@ = string to write errors in
411 * Returns: Zero if OK, nonzero on error.
413 * Use: Loads the private key from its keyfile.
416 static int loadpriv(void)
427 const char *err, *tag, *ty;
430 /* --- Open the private key file --- */
432 if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, "private"))
435 /* --- Find the private key --- */
438 key_qtag(&kf, tag = tag_priv, &t, &k, &kd) :
439 key_qtag(&kf, tag = "tripe", &t, &k, &kd) &&
440 key_qtag(&kf, tag = "tripe-dh", &t, &k, &kd)) {
441 a_warn("KEYMGMT", "private-keyring", "%s", kr_priv,
442 "key-not-found", "%s", tag, A_END);
446 /* --- Look up the key type in the table --- */
448 if ((ko = keykg(&kf, k, &ty)) == 0) {
449 a_warn("KEYMGMT", "private-keyring",
450 "%s", kr_priv, "key", "%s", t.buf,
451 "unknown-group-type", "%s", ty, A_END);
455 /* --- Load the key --- */
457 if (ko->loadpriv(*kd, &g, &x, &t, &e)) {
458 a_warn("KEYMGMT", "private-keyring",
459 "%s", kr_priv, "key", "%s", t.buf,
460 "*%s", e.buf, A_END);
464 /* --- Check that the key is sensible --- */
466 if ((err = G_CHECK(g, &rand_global)) != 0) {
467 a_warn("KEYMGMT", "private-keyring",
468 "%s", kr_priv, "key", "%s", t.buf,
469 "bad-group", "%s", err, A_END);
473 /* --- Collect the algorithms --- */
475 if (algs_get(&a, &e, &kf, k) ||
476 algs_check(&a, &e, g)) {
477 a_warn("KEYMGMT", "private-keyring",
478 "%s", kr_priv, "key", "%s", t.buf,
479 "*%s", e.buf, A_END);
483 /* --- Good, we're happy --- *
485 * Dodginess! We change the group over here, but don't free any old group
486 * elements. This assumes that the new group is basically the same as the
487 * old one, and will happily adopt the existing elements. If it isn't,
488 * then we lose badly. Check this, then.
492 if (!group_samep(g, gg)) {
493 a_warn("KEYMGMT", "private-keyring",
494 "%s", kr_priv, "key", "%s", t.buf,
495 "changed-group", A_END);
506 G_EXP(g, kpub, g->g, x);
507 indexsz = mp_octets(g->r);
509 /* --- Dump out the group --- */
511 IF_TRACING(T_KEYMGMT, {
512 trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", t.buf);
513 IF_TRACING(T_CRYPTO, {
514 trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
515 trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
516 trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
517 trace(T_CRYPTO, "crypto: cipher = %s", a.c->name);
518 trace(T_CRYPTO, "crypto: mgf = %s", a.mgf->name);
519 trace(T_CRYPTO, "crypto: hash = %s", a.h->name);
520 trace(T_CRYPTO, "crypto: mac = %s/%lu",
521 a.m->name, (unsigned long)a.tagsz * 8);
525 /* --- Success! --- */
532 /* --- Tidy up --- */
540 if (g) G_DESTROYGROUP(g);
544 /* --- @loadpub@ --- *
546 * Arguments: @dstr *d@ = string to write errors to
548 * Returns: Zero if OK, nonzero on error.
550 * Use: Reloads the public keyring.
553 static int loadpub(void)
555 key_file *kf = CREATE(key_file);
557 if (key_open(kf, kr_pub, KOPEN_READ, keymoan, "public")) {
562 T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
566 /* --- @km_reload@ --- *
570 * Returns: Zero if OK, nonzero to force reloading of keys.
572 * Use: Checks the keyrings to see if they need reloading.
580 /* --- Check the private key first --- */
582 if (fwatch_update(&w_priv, kr_priv)) {
583 T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
588 /* --- Now check the public keys --- */
590 if (fwatch_update(&w_pub, kr_pub)) {
591 T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
605 /* --- @km_init@ --- *
607 * Arguments: @const char *priv@ = private keyring file
608 * @const char *pub@ = public keyring file
609 * @const char *tag@ = tag to load
613 * Use: Initializes, and loads the private key.
616 void km_init(const char *priv, const char *pub, const char *tag)
618 const gchash *const *hh;
623 fwatch_init(&w_priv, kr_priv);
624 fwatch_init(&w_pub, kr_pub);
626 for (hh = ghashtab; *hh; hh++) {
627 if ((*hh)->hashsz > MAXHASHSZ) {
628 die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
629 (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ);
633 if (loadpriv() || loadpub())
637 /* --- @km_getpubkey@ --- *
639 * Arguments: @const char *tag@ = public key tag to load
640 * @ge *kpub@ = where to put the public key
641 * @time_t *t_exp@ = where to put the expiry time
643 * Returns: Zero if OK, nonzero if it failed.
645 * Use: Fetches a public key from the keyring.
648 int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
661 /* --- Find the key --- */
663 if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
664 a_warn("KEYMGMT", "public-keyring", "%s", kr_pub,
665 "key-not-found", "%s", tag, A_END);
669 /* --- Look up the key type in the table --- */
671 if ((ko = keykg(kf_pub, k, &ty)) == 0) {
672 a_warn("KEYMGMT", "public-keyring",
673 "%s", kr_pub, "key", "%s", t.buf,
674 "unknown-group-type", "%s", ty, A_END);
678 /* --- Load the key --- */
680 if (ko->loadpub(*kd, &g, &p, &t, &e)) {
681 a_warn("KEYMGMT", "public-keyring",
682 "%s", kr_pub, "key", "%s", t.buf,
683 "*%s", e.buf, A_END);
687 /* --- Ensure that the group is correct --- *
689 * Dodginess! We assume that if this works, our global group is willing to
690 * adopt this public element. Probably reasonable.
693 if (!group_samep(gg, g)) {
694 a_warn("KEYMGMT", "public-keyring",
695 "%s", kr_pub, "key", "%s", t.buf,
696 "*%s", e.buf, A_END);
700 /* --- Check the public group element --- */
702 if (group_check(gg, p)) {
703 a_warn("KEYMGMT", "public-keyring",
704 "%s", kr_pub, "key", "%s", t.buf,
705 "bad-public-group-element",
710 /* --- Check the algorithms --- */
712 if (algs_get(&a, &e, kf_pub, k)) {
713 a_warn("KEYMGMT", "public-keyring",
714 "%s", kr_pub, "key", "%s", t.buf,
715 "*%s", e.buf, A_END);
718 if (!algs_samep(&a, &algs)) {
719 a_warn("KEYMGMT", "public-keyring",
720 "%s", kr_pub, "key", "%s", t.buf,
721 "algorithm-mismatch", A_END);
725 /* --- Dump the public key --- */
727 IF_TRACING(T_KEYMGMT, {
728 trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
729 trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
732 /* --- OK, accept the public key --- */
738 /* --- Tidy up --- */
741 if (p) G_DESTROY(g, p);
742 if (g) G_DESTROYGROUP(g);
748 /*----- That's all, folks -------------------------------------------------*/