/* -*-c-*-
- *
- * $Id$
*
* Key loading and storing
*
#include "tripe.h"
-/*----- Global variables --------------------------------------------------*/
-
-group *gg;
-mp *kpriv;
-ge *kpub;
-algswitch algs;
-size_t indexsz;
-
-/*----- Static variables --------------------------------------------------*/
-
-static key_file *kf_pub;
-static const char *kr_priv, *kr_pub, *tag_priv;
-static fwatch w_priv, w_pub;
-
-/*----- Key groups --------------------------------------------------------*/
-
-typedef struct kgops {
- const char *ty;
- const char *(*loadpriv)(key_data *, group **, mp **, dstr *);
- const char *(*loadpub)(key_data *, group **, ge **, dstr *);
-} kgops;
-
-/* --- Diffie-Hellman --- */
-
-static const char *kgdh_priv(key_data *kd, group **g, mp **x, dstr *t)
-{
- key_packstruct kps[DH_PRIVFETCHSZ];
- key_packdef *kp;
- dh_priv dp;
- const char *e;
- int rc;
-
- kp = key_fetchinit(dh_privfetch, kps, &dp);
- if ((rc = key_unpack(kp, kd, t)) != 0) {
- e = key_strerror(rc);
- goto done;
- }
- *g = group_prime(&dp.dp);
- *x = MP_COPY(dp.x);
- e = 0;
-done:
- key_fetchdone(kp);
- return (e);
-}
-
-static const char *kgdh_pub(key_data *kd, group **g, ge **p, dstr *t)
-{
- key_packstruct kps[DH_PUBFETCHSZ];
- key_packdef *kp;
- dh_pub dp;
- const char *e;
- int rc;
-
- kp = key_fetchinit(dh_pubfetch, kps, &dp);
- if ((rc = key_unpack(kp, kd, t)) != 0) {
- e = key_strerror(rc);
- goto done;
- }
- *g = group_prime(&dp.dp);
- *p = G_CREATE(*g);
- if (G_FROMINT(*g, *p, dp.y)) {
- e = "bad public value";
- goto done;
- }
- e = 0;
-done:
- key_fetchdone(kp);
- return (e);
-}
-
-static const kgops kgdh_ops = { "tripe-dh", kgdh_priv, kgdh_pub };
-
-/* --- Elliptic curve --- */
-
-static const char *kgec_priv(key_data *kd, group **g, mp **x, dstr *t)
-{
- key_packstruct kps[EC_PRIVFETCHSZ];
- key_packdef *kp;
- ec_priv ep;
- ec_info ei;
- const char *e;
- int rc;
-
- kp = key_fetchinit(ec_privfetch, kps, &ep);
- if ((rc = key_unpack(kp, kd, t)) != 0) {
- e = key_strerror(rc);
- goto done;
- }
- if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
- goto done;
- *g = group_ec(&ei);
- *x = MP_COPY(ep.x);
- e = 0;
-done:
- key_fetchdone(kp);
- return (e);
-}
-
-static const char *kgec_pub(key_data *kd, group **g, ge **p, dstr *t)
-{
- key_packstruct kps[EC_PUBFETCHSZ];
- key_packdef *kp;
- ec_pub ep;
- ec_info ei;
- const char *e;
- int rc;
-
- kp = key_fetchinit(ec_pubfetch, kps, &ep);
- if ((rc = key_unpack(kp, kd, t)) != 0) {
- e = key_strerror(rc);
- goto done;
- }
- if ((e = ec_getinfo(&ei, ep.cstr)) != 0)
- goto done;
- *g = group_ec(&ei);
- *p = G_CREATE(*g);
- if (G_FROMEC(*g, *p, &ep.p)) {
- e = "bad public point";
- goto done;
- }
- e = 0;
-done:
- key_fetchdone(kp);
- return (e);
-}
-
-static const kgops kgec_ops = { "tripe-ec", kgec_priv, kgec_pub };
-
-/* --- Table of supported key types --- */
-
-static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 };
-
/*----- Algswitch stuff ---------------------------------------------------*/
/* --- @algs_get@ --- *
*
* Arguments: @algswitch *a@ = where to put the algorithms
- * @key_file *kf@ = key file (for some stupid reason)
+ * @dstr *e@ = where to write error tokens
+ * @key_file *kf@ = key file
* @key *k@ = key to inspect
*
- * Returns: Null if OK, or an error message.
+ * Returns: Zero if OK; nonzero on error.
*
* Use: Extracts an algorithm choice from a key.
*/
-static const char *algs_get(algswitch *a, key_file *kf, key *k)
+static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k)
{
const char *p;
- char *q;
- dstr d = DSTR_INIT;
- const char *e;
+ const bulkops *bops;
+ dstr d = DSTR_INIT, dd = DSTR_INIT;
+ int rc = -1;
-#define FAIL(msg) do { e = msg; goto done; } while (0)
+ /* --- Hash function --- */
- if ((p = key_getattr(kf, k, "cipher")) == 0)
- p = "blowfish-cbc";
- if ((a->c = gcipher_byname(p)) == 0)
- FAIL("unknown-cipher");
+ if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160";
+ if ((a->h = ghash_byname(p)) == 0) {
+ a_format(e, "unknown-hash", "%s", p, A_END);
+ goto done;
+ }
- if ((p = key_getattr(kf, k, "hash")) == 0)
- p = "rmd160";
- if ((a->h = ghash_byname(p)) == 0)
- FAIL("unknown-hash");
+ /* --- Symmetric encryption for key derivation --- */
if ((p = key_getattr(kf, k, "mgf")) == 0) {
dstr_reset(&d);
dstr_putf(&d, "%s-mgf", a->h->name);
p = d.buf;
}
- if ((a->mgf = gcipher_byname(p)) == 0)
- FAIL("unknown-mgf-cipher");
+ if ((a->mgf = gcipher_byname(p)) == 0) {
+ a_format(e, "unknown-mgf-cipher", "%s", p, A_END);
+ goto done;
+ }
- if ((p = key_getattr(kf, k, "mac")) != 0) {
- dstr_reset(&d);
- dstr_puts(&d, p);
- if ((q = strchr(d.buf, '/')) != 0)
- *q++ = 0;
- if ((a->m = gmac_byname(d.buf)) == 0)
- FAIL("unknown-mac");
- if (!q)
- a->tagsz = a->m->hashsz;
- else {
- unsigned long n = strtoul(q, &q, 0);
- if (*q) FAIL("bad-tag-length-string");
- if (n%8 || n > ~(size_t)0) FAIL("bad-tag-length");
- a->tagsz = n/8;
- }
- } else {
- dstr_reset(&d);
- dstr_putf(&d, "%s-hmac", a->h->name);
- if ((a->m = gmac_byname(d.buf)) == 0)
- FAIL("no-hmac-for-hash");
- a->tagsz = a->h->hashsz/2;
+ /* --- Bulk crypto transform --- */
+
+ if ((p = key_getattr(kf, k, "bulk")) == 0) p = "v0";
+ for (bops = bulktab; bops->name && strcmp(p, bops->name) != 0; bops++);
+ if (!bops->name) {
+ a_format(e, "unknown-bulk-transform", "%s", p, A_END);
+ goto done;
}
+ if ((a->bulk = bops->getalgs(a, e, kf, k)) == 0) goto done;
+ a->bulk->ops = bops;
- e = 0;
+ /* --- All done --- */
+
+ rc = 0;
done:
dstr_destroy(&d);
- return (e);
+ dstr_destroy(&dd);
+ return (rc);
}
/* --- @algs_check@ --- *
*
* Arguments: @algswitch *a@ = a choice of algorithms
- * @const group *g@ = the group we're working in
+ * @dstr *e@ = where to write error tokens
+ * @const dhgrp *grp@ = the group we're working in
*
- * Returns: Null if OK, or an error message.
+ * Returns: Zero if OK; nonzero on error.
*
* Use: Checks an algorithm choice for sensibleness. This also
* derives some useful information from the choices, and you
* for use by @keyset@ functions.
*/
-static const char *algs_check(algswitch *a, const group *g)
+static int algs_check(algswitch *a, dstr *e, const dhgrp *grp)
{
- /* --- Derive the key sizes --- *
- *
- * Must ensure that we have non-empty keys. This isn't ideal, but it
- * provides a handy sanity check.
- */
-
a->hashsz = a->h->hashsz;
- if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0)
- return ("no key size found for cipher");
- if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0)
- return ("no key size found for MAC");
-
- /* --- Ensure that the tag size is sane --- */
-
- if (a->tagsz > a->m->hashsz) return ("tag length too large");
-
- /* --- Ensure the MGF accepts hashes as keys --- */
- if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz)
- return ("MGF not suitable -- restrictive key schedule");
+ if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) {
+ a_format(e, "mgf", "%s", a->mgf->name,
+ "restrictive-key-schedule",
+ A_END);
+ return (-1);
+ }
- /* --- All ship-shape and Bristol-fashion --- */
+ if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1);
return (0);
}
-/* --- @algs_samep@ --- *
+/* --- @km_samealgsp@ --- *
*
- * Arguments: @const algswitch *a, *aa@ = two algorithm selections
+ * Arguments: @const kdata *kdx, *kdy@ = two key data objects
*
- * Returns: Nonzero if the two selections are the same.
+ * Returns: Nonzero if their two algorithm selections are the same.
*
* Use: Checks sameness of algorithm selections: used to ensure that
* peers are using sensible algorithms.
*/
-static int algs_samep(const algswitch *a, const algswitch *aa)
+int km_samealgsp(const kdata *kdx, const kdata *kdy)
{
- return (a->c == aa->c && a->mgf == aa->mgf && a->h == aa->h &&
- a->m == aa->m && a->tagsz == aa->tagsz);
+ const algswitch *a = &kdx->algs, *aa = &kdy->algs;
+
+ return (kdx->grp->ops == kdy->grp->ops &&
+ kdx->grp->ops->samegrpp(kdx->grp, kdy->grp) &&
+ a->mgf == aa->mgf && a->h == aa->h &&
+ a->bulk->ops == aa->bulk->ops &&
+ a->bulk->ops->samealgsp(a->bulk, aa->bulk));
}
-/*----- Main code ---------------------------------------------------------*/
+/*----- Key data and key nodes --------------------------------------------*/
+
+typedef struct keyhalf {
+ const char *kind;
+ int (*load)(key_file *, key *, key_data *,
+ const dhops *, kdata *, dstr *, dstr *);
+ const char *kr;
+ key_file *kf;
+ fwatch w;
+ sym_table tab;
+} keyhalf;
+
+/* --- @kh_loadpub@, @kh_loadpriv@ --- *
+ *
+ * Arguments: @const dhops *dh@ = Diffie--Hellman operations for key type
+ * @key_file *kf@ = key file from which the key was loaded
+ * @key *k@ = the key object we're loading
+ * @key_data *d@ = the key data to load
+ * @kdata *kd@ = our key-data object to fill in
+ * @dstr *t@ = the key tag name
+ * @dstr *e@ = a string to write error tokens to
+ *
+ * Returns: Zero on success, @-1@ on error.
+ *
+ * Use: These functions handle the main difference between public and
+ * private key halves. They are responsible for setting @grp@,
+ * @k@ and @K@ appropriately in all keys, handling the mismatch
+ * between the largely half-indifferent calling code and the
+ * group-specific loading functions.
+ *
+ * The function @kh_loadpriv@ is also responsible for checking
+ * the group for goodness. We don't bother checking public
+ * keys, because each public key we actually end up using must
+ * share a group with a private key which we'll already have
+ * checked.
+ */
+
+static int kh_loadpub(key_file *kf, key *k, key_data *d,
+ const dhops *dh, kdata *kd, dstr *t, dstr *e)
+{
+ int rc;
+
+ if ((rc = dh->ldpub(kf, k, d, kd, t, e)) != 0)
+ goto fail_0;
+ kd->grp->ops = dh;
+ if (kd->grp->ops->checkge(kd->grp, kd->K)) {
+ a_format(e, "bad-public-group-element", A_END);
+ goto fail_1;
+ }
+ return (0);
+
+fail_1:
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
+fail_0:
+ return (-1);
+}
+
+static int kh_loadpriv(key_file *kf, key *k, key_data *d,
+ const dhops *dh, kdata *kd, dstr *t, dstr *e)
+{
+ int rc;
+ const char *err;
+ dhge *K;
+ int ok;
+
+ if ((rc = dh->ldpriv(kf, k, d, kd, t, e)) != 0)
+ goto fail_0;
+ kd->grp->ops = dh;
+ if ((err = kd->grp->ops->checkgrp(kd->grp)) != 0) {
+ a_format(e, "bad-group", "%s", err, A_END);
+ goto fail_1;
+ }
+ K = kd->grp->ops->mul(kd->grp, kd->k, 0);
+ ok = kd->grp->ops->eq(kd->grp, kd->K, K);
+ kd->grp->ops->freege(kd->grp, K);
+ if (!ok) {
+ a_format(e, "incorrect-public-key", A_END);
+ goto fail_1;
+ }
+ return (0);
+
+fail_1:
+ kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
+fail_0:
+ return (-1);
+}
+
+static struct keyhalf
+ priv = { "private", kh_loadpriv },
+ pub = { "public", kh_loadpub };
/* --- @keymoan@ --- *
*
* Arguments: @const char *file@ = name of the file
* @int line@ = line number in file
* @const char *msg@ = error message
- * @void *p@ = argument pointer
+ * @void *p@ = argument pointer (indicates which keyring)
*
* Returns: ---
*
static void keymoan(const char *file, int line, const char *msg, void *p)
{
- a_warn("KEYMGMT",
- "key-file-error",
- "%s:%i", file, line,
- "%s", msg,
- A_END);
+ keyhalf *kh = p;
+
+ if (!line) {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file,
+ "io-error", "?ERRNO", A_END);
+ } else {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", file, "line", "%d", line,
+ "%s", msg, A_END);
+ }
}
-/* --- @loadpriv@ --- *
+/* --- @kh_reopen@ --- *
*
- * Arguments: @dstr *d@ = string to write errors in
+ * Arguments: @keyhalf *kh@ = pointer to keyhalf structure
*
- * Returns: Zero if OK, nonzero on error.
+ * Returns: Zero on success, @-1@ on error.
*
- * Use: Loads the private key from its keyfile.
+ * Use: Reopens the key file for the appropriate key half. If this
+ * fails, everything is left as it was; if it succeeds, then the
+ * old file is closed (if it was non-null) and the new one put
+ * in its place.
*/
-static int loadpriv(dstr *d)
+static int kh_reopen(keyhalf *kh)
{
- key_file kf;
- key *k;
- key_data **kd;
- dstr t = DSTR_INIT;
- group *g = 0;
- mp *x = 0;
- int rc = -1;
- const kgops **ko;
- const char *e;
- algswitch a;
-
- /* --- Open the private key file --- */
-
- if (key_open(&kf, kr_priv, KOPEN_READ, keymoan, 0)) {
- dstr_putf(d, "error reading private keyring `%s': %s",
- kr_priv, strerror(errno));
- goto done_0;
- }
-
- /* --- Find the private key --- */
+ key_file *kf = CREATE(key_file);
- if (key_qtag(&kf, tag_priv, &t, &k, &kd)) {
- dstr_putf(d, "private key `%s' not found in keyring `%s'",
- tag_priv, kr_priv);
- goto done_1;
+ if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
+ "io-error", "?ERRNO", A_END);
+ DESTROY(kf);
+ return (-1);
+ } else {
+ if (kh->kf) {
+ key_close(kh->kf);
+ DESTROY(kh->kf);
+ }
+ kh->kf = kf;
+ return (0);
}
+}
- /* --- Look up the key type in the table --- */
-
- for (ko = kgtab; *ko; ko++) {
- if (strcmp((*ko)->ty, k->type) == 0)
- goto tymatch;
- }
- dstr_putf(d, "private key `%s' has unknown type `%s'", t.buf, k->type);
- goto done_1;
-tymatch:;
+/* --- @kh_init@ --- *
+ *
+ * Arguments: @keyhalf *kh@ = pointer to keyhalf structure to set up
+ * @const char *kr@ = name of the keyring file
+ *
+ * Returns: ---
+ *
+ * Use: Initialize a keyhalf structure, maintaining the private or
+ * public keys. Intended to be called during initialization:
+ * exits if there's some kind of problem.
+ */
- /* --- Load the key --- */
+static void kh_init(keyhalf *kh, const char *kr)
+{
+ kh->kr = kr;
+ fwatch_init(&kh->w, kr);
+ sym_create(&kh->tab);
+ kh->kf = 0;
- if ((e = (*ko)->loadpriv(*kd, &g, &x, &t)) != 0) {
- dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
- goto done_1;
- }
+ if (kh_reopen(kh))
+ die(EXIT_FAILURE, "failed to load %s keyring `%s'", kh->kind, kr);
+}
- /* --- Check that the key is sensible --- */
+/* --- @kh_load@ --- *
+ *
+ * Arguments: @keyhalf *kh@ = pointer to keyhalf
+ * @const char *tag@ = key tag to be loaded
+ * @int complainp@ = whether to complain about missing keys
+ *
+ * Returns: Pointer to a @kdata@ structure if successful, or null on
+ * failure.
+ *
+ * Use: Attempts to load a key from the current key file. This
+ * function always reads data from the file: it's used when
+ * there's a cache miss from @kh_find@, and when refreshing the
+ * known keys in @kh_refresh@. The returned kdata has a
+ * reference count of exactly 1, and has no home knode.
+ */
- if ((e = G_CHECK(g, &rand_global)) != 0) {
- dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
- goto done_1;
- }
+static kdata *kh_load(keyhalf *kh, const char *tag, int complainp)
+{
+ dstr t = DSTR_INIT;
+ dstr e = DSTR_INIT;
+ key *k;
+ key_data **d;
+ kdata *kd;
+ const char *ty;
+ const dhops *dh;
+ T( const dhgrp *g; )
- /* --- Collect the algorithms --- */
+ /* --- Find the key and grab its tag --- */
- if ((e = algs_get(&a, &kf, k)) != 0 ||
- (e = algs_check(&a, g)) != 0) {
- dstr_putf(d, "bad symmetric algorithm selection in private key `%s': %s",
- t.buf, e);
- goto done_1;
+ if (key_qtag(kh->kf, tag, &t, &k, &d)) {
+ if (complainp) {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr,
+ "key-not-found", "%s", tag, A_END);
+ }
+ goto fail_0;
}
- /* --- Good, we're happy --- *
+ /* --- Find the key's group type and the appropriate operations --- *
*
- * Dodginess! We change the group over here, but don't free any old group
- * elements. This assumes that the new group is basically the same as the
- * old one, and will happily adopt the existing elements. If it isn't,
- * then we lose badly. Check this, then.
+ * There are several places to look for the key type. The most obvious is
+ * the `kx-group' key attribute. But there's also the key type itself, for
+ * compatibility reasons.
*/
- if (gg) {
- if (!group_samep(g, gg)) {
- dstr_putf(d, "private key `%s' has different group", t.buf);
- goto done_1;
- }
- G_DESTROYGROUP(gg);
+ ty = key_getattr(kh->kf, k, "kx-group");
+ if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6;
+ if (!ty) ty = "dh";
+
+ for (dh = dhtab; dh->name; dh++)
+ if (strcmp(dh->name, ty) == 0) goto founddh;
+ a_warn("KEYMGMT", "%s-keyring", kh->kind,
+ "%s", kh->kr, "key", "%s", t.buf,
+ "unknown-group-type", "%s", ty, A_END);
+ goto fail_0;
+
+founddh:
+ kd = CREATE(kdata);
+ if (kh->load(kh->kf, k, *d, dh, kd, &t, &e)) {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind,
+ "%s", kh->kr, "key", "%s", t.buf,
+ "*%s", e.buf, A_END);
+ goto fail_1;
}
- if (kpriv)
- mp_drop(kpriv);
- if (kpub)
- G_DESTROY(g, kpub);
- kpub = G_CREATE(g);
- G_EXP(g, kpub, g->g, x);
- indexsz = mp_octets(g->r);
+ if (algs_get(&kd->algs, &e, kh->kf, k) ||
+ (kd->k && algs_check(&kd->algs, &e, kd->grp))) {
+ a_warn("KEYMGMT", "%s-keyring", kh->kind,
+ "%s", kh->kr, "key", "%s", t.buf,
+ "*%s", e.buf, A_END);
+ goto fail_2;
+ }
- /* --- Dump out the group --- */
+ kd->tag = xstrdup(t.buf);
+ kd->ref = 1;
+ kd->kn = 0;
+ kd->t_exp = k->exp;
IF_TRACING(T_KEYMGMT, {
- trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", t.buf);
+ trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf);
IF_TRACING(T_CRYPTO, {
- trace(T_CRYPTO, "crypto: r = %s", mpstr(g->r));
- trace(T_CRYPTO, "crypto: h = %s", mpstr(g->h));
- trace(T_CRYPTO, "crypto: x = %s", mpstr(x));
- trace(T_CRYPTO, "crypto: cipher = %s", a.c->name);
- trace(T_CRYPTO, "crypto: mgf = %s", a.mgf->name);
- trace(T_CRYPTO, "crypto: hash = %s", a.h->name);
- trace(T_CRYPTO, "crypto: mac = %s/%lu",
- a.m->name, (unsigned long)a.tagsz * 8);
+ g = kd->grp;
+ g->ops->tracegrp(g);
+ if (kd->k)
+ trace(T_CRYPTO, "crypto: k = %s", g->ops->scstr(g, kd->k));
+ trace(T_CRYPTO, "crypto: K = %s", g->ops->gestr(g, kd->K));
+ kd->algs.bulk->ops->tracealgs(kd->algs.bulk);
})
})
- /* --- Success! --- */
-
- gg = g; g = 0;
- algs = a;
- kpriv = x; x = 0;
- rc = 0;
-
- /* --- Tidy up --- */
+ goto done;
-done_1:
- key_close(&kf);
-done_0:
+fail_2:
+ if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
+fail_1:
+ DESTROY(kd);
+fail_0:
+ kd = 0;
+done:
dstr_destroy(&t);
- if (x) mp_drop(x);
- if (g) G_DESTROYGROUP(g);
- return (rc);
+ dstr_destroy(&e);
+ return (kd);
}
-/* --- @loadpub@ --- *
+/* --- @kh_find@ --- *
*
- * Arguments: @dstr *d@ = string to write errors to
+ * Arguments: @keyhalf *kh@ = pointer to the keyhalf
+ * @const char *tag@ = key to be obtained
+ * @int complainp@ = whether to complain about missing keys
*
- * Returns: Zero if OK, nonzero on error.
+ * Returns: A pointer to the kdata, or null on error.
*
- * Use: Reloads the public keyring.
+ * Use: Obtains kdata, maybe from the cache. This won't update a
+ * stale cache entry, though @kh_refresh@ ought to have done
+ * that already. The returned kdata object may be shared with
+ * other users. (One of this function's responsibilities, over
+ * @kh_load@, is to set the home knode of a freshly loaded
+ * kdata.)
*/
-static int loadpub(dstr *d)
+static kdata *kh_find(keyhalf *kh, const char *tag, int complainp)
{
- key_file *kf = CREATE(key_file);
+ knode *kn;
+ kdata *kd;
+ unsigned f;
- if (key_open(kf, kr_pub, KOPEN_READ, keymoan, 0)) {
- dstr_putf(d, "error reading public keyring `%s': %s",
- kr_pub, strerror(errno));
- DESTROY(kf);
- return (-1);
+ kn = sym_find(&kh->tab, tag, -1, sizeof(knode), &f);
+
+ if (f) {
+ if (kn->f & KNF_BROKEN) {
+ T( if (complainp)
+ trace(T_KEYMGMT, "keymgmt: key `%s' marked as broken", tag); )
+ return (0);
+ }
+
+ kd = kn->kd;
+ if (kd) kd->ref++;
+ T( trace(T_KEYMGMT, "keymgmt: %scache hit for key `%s'",
+ kd ? "" : "negative ", tag); )
+ return (kd);
+ } else {
+ kd = kh_load(kh, tag, complainp);
+ kn->kd = kd;
+ kn->kh = kh;
+ kn->f = 0;
+ if (!kd)
+ kn->f |= KNF_BROKEN;
+ else {
+ kd->kn = kn;
+ kd->ref++;
+ }
+ return (kd);
}
- kf_pub = kf;
- T( trace(T_KEYMGMT, "keymgmt: loaded public keyring `%s'", kr_pub); )
- return (0);
}
-/* --- @km_reload@ --- *
+/* --- @kh_refresh@ --- *
*
- * Arguments: ---
+ * Arguments: @keyhalf *kh@ = pointer to the keyhalf
*
- * Returns: Zero if OK, nonzero to force reloading of keys.
+ * Returns: Zero if nothing needs to be done; nonzero if peers should
+ * refresh their keys.
*
- * Use: Checks the keyrings to see if they need reloading.
+ * Use: Refreshes cached keys from files.
+ *
+ * Each active knode is examined to see if a new key is
+ * available: the return value is nonzero if any new keys are.
+ * A key is considered new if its algorithms, public key, or
+ * expiry time are/is different.
+ *
+ * Stub knodes (with no kdata attached) are removed, so that a
+ * later retry can succeed if the file has been fixed. (This
+ * doesn't count as a change, since no peers should be relying
+ * on a nonexistent key.)
*/
-int km_reload(void)
+static int kh_refresh(keyhalf *kh)
{
- dstr d = DSTR_INIT;
- key_file *kf;
- int reload = 0;
-
- /* --- Check the private key first --- */
-
- if (fwatch_update(&w_priv, kr_priv)) {
- T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
- DRESET(&d);
- if (loadpriv(&d))
- a_warn("KEYMGMT", "bad-private-key", "%s", d.buf, A_END);
- else
- reload = 1;
- }
-
- /* --- Now check the public keys --- */
-
- if (fwatch_update(&w_pub, kr_pub)) {
- T( trace(T_KEYMGMT, "keymgmt: public keyring updated: reloading..."); )
- kf = kf_pub;
- DRESET(&d);
- if (loadpub(&d))
- a_warn("KEYMGMT", "bad-public-keyring", "%s", d.buf, A_END);
- else {
- reload = 1;
- key_close(kf);
- DESTROY(kf);
+ knode *kn;
+ kdata *kd;
+ sym_iter i;
+ int changep = 0;
+
+ if (!fwatch_update(&kh->w, kh->kr) || kh_reopen(kh))
+ return (0);
+
+ T( trace(T_KEYMGMT, "keymgmt: rescan %s keyring `%s'", kh->kind, kh->kr); )
+ for (sym_mkiter(&i, &kh->tab); (kn = sym_next(&i)) != 0; ) {
+ if (!kn->kd) {
+ T( trace(T_KEYMGMT, "keymgmt: discard stub entry for key `%s'",
+ SYM_NAME(kn)); )
+ sym_remove(&kh->tab, kn);
+ continue;
+ }
+ if ((kd = kh_load(kh, SYM_NAME(kn), 1)) == 0) {
+ if (!(kn->f & KNF_BROKEN)) {
+ T( trace(T_KEYMGMT, "keymgmt: failed to load new key `%s': "
+ "marking it as broken",
+ SYM_NAME(kn)); )
+ kn->f |= KNF_BROKEN;
+ }
+ continue;
+ }
+ kn->f &= ~KNF_BROKEN;
+ if (kd->t_exp == kn->kd->t_exp &&
+ km_samealgsp(kd, kn->kd) &&
+ kd->grp->ops->eq(kd->grp, kd->K, kn->kd->K)) {
+ T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); )
+ continue;
}
+ T( trace(T_KEYMGMT, "keymgmt: loaded new version of key `%s'",
+ SYM_NAME(kn)); )
+ km_unref(kn->kd);
+ kd->kn = kn;
+ kn->kd = kd;
+ changep = 1;
}
- /* --- Done --- */
-
- return (reload);
+ return (changep);
}
+/*----- Main code ---------------------------------------------------------*/
+
+const char *tag_priv;
+kdata *master;
+
/* --- @km_init@ --- *
*
- * Arguments: @const char *priv@ = private keyring file
- * @const char *pub@ = public keyring file
- * @const char *tag@ = tag to load
+ * Arguments: @const char *privkr@ = private keyring file
+ * @const char *pubkr@ = public keyring file
+ * @const char *ptag@ = default private-key tag
*
* Returns: ---
*
- * Use: Initializes, and loads the private key.
+ * Use: Initializes the key-management machinery, loading the
+ * keyrings and so on.
*/
-void km_init(const char *priv, const char *pub, const char *tag)
+void km_init(const char *privkr, const char *pubkr, const char *ptag)
{
- dstr d = DSTR_INIT;
const gchash *const *hh;
- kr_priv = priv;
- kr_pub = pub;
- tag_priv = tag;
- fwatch_init(&w_priv, kr_priv);
- fwatch_init(&w_pub, kr_pub);
-
for (hh = ghashtab; *hh; hh++) {
if ((*hh)->hashsz > MAXHASHSZ) {
die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d",
}
}
- DRESET(&d);
- if (loadpriv(&d))
- die(EXIT_FAILURE, "%s", d.buf);
- if (loadpub(&d))
- die(EXIT_FAILURE, "%s", d.buf);
+ kh_init(&priv, privkr);
+ kh_init(&pub, pubkr);
+
+ tag_priv = ptag;
+ if ((master = km_findpriv(ptag)) == 0) exit(EXIT_FAILURE);
}
-/* --- @km_getpubkey@ --- *
+/* --- @km_reload@ --- *
*
- * Arguments: @const char *tag@ = public key tag to load
- * @ge *kpub@ = where to put the public key
- * @time_t *t_exp@ = where to put the expiry time
+ * Arguments: ---
*
- * Returns: Zero if OK, nonzero if it failed.
+ * Returns: Zero if OK, nonzero to force reloading of keys.
*
- * Use: Fetches a public key from the keyring.
+ * Use: Checks the keyrings to see if they need reloading.
*/
-int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
+int km_reload(void)
{
- key *k;
- key_data **kd;
- dstr t = DSTR_INIT;
- const kgops **ko;
- const char *e;
- group *g = 0;
- ge *p = 0;
- algswitch a;
- int rc = -1;
-
- /* --- Find the key --- */
-
- if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
- a_warn("KEYMGMT", "public-key", "%s", tag, "not-found", A_END);
- goto done;
- }
-
- /* --- Look up the key type in the table --- */
-
- for (ko = kgtab; *ko; ko++) {
- if (strcmp((*ko)->ty, k->type) == 0)
- goto tymatch;
- }
- a_warn("KEYMGMT",
- "public-key", "%s", t.buf,
- "unknown-type", "%s", k->type,
- A_END);
- goto done;
-tymatch:;
-
- /* --- Load the key --- */
-
- if ((e = (*ko)->loadpub(*kd, &g, &p, &t)) != 0) {
- a_warn("KEYMGMT", "public-key", "%s", t.buf, "bad", "%s", e, A_END);
- goto done;
- }
-
- /* --- Ensure that the group is correct --- *
- *
- * Dodginess! We assume that if this works, our global group is willing to
- * adopt this public element. Probably reasonable.
- */
-
- if (!group_samep(gg, g)) {
- a_warn("KEYMGMT", "public-key", "%s", t.buf, "incorrect-group", A_END);
- goto done;
+ int changep = 0;
+ kdata *kd;
+
+ if (kh_refresh(&priv)) {
+ changep = 1;
+ kd = master->kn->kd;
+ if (kd != master) {
+ km_unref(master);
+ km_ref(kd);
+ master = kd;
+ }
}
+ if (kh_refresh(&pub))
+ changep = 1;
+ return (changep);
+}
- /* --- Check the public group element --- */
+/* --- @km_findpub@, @km_findpriv@ --- *
+ *
+ * Arguments: @const char *tag@ = key tag to load
+ *
+ * Returns: Pointer to the kdata object if successful, or null on error.
+ *
+ * Use: Fetches a public or private key from the keyring.
+ */
- if (group_check(gg, p)) {
- a_warn("KEYMGMT",
- "public-key", "%s", t.buf,
- "bad-public-group-element",
- A_END);
- goto done;
- }
+kdata *km_findpub(const char *tag) { return (kh_find(&pub, tag, 1)); }
- /* --- Check the algorithms --- */
+kdata *km_findpriv(const char *tag)
+{
+ kdata *kd;
- if ((e = algs_get(&a, kf_pub, k)) != 0) {
- a_warn("KEYMGMT",
- "public-key", "%s", t.buf,
- "bad-algorithm-selection", e,
- A_END);
- goto done;
- }
- if (!algs_samep(&a, &algs)) {
- a_warn("KEYMGMT",
- "public-key", "%s", t.buf,
- "algorithm-mismatch",
- A_END);
- goto done;
- }
+ /* Unpleasantness for the sake of compatibility. */
+ if (!tag && (kd = kh_find(&priv, "tripe", 0)) != 0) return (kd);
+ else return (kh_find(&priv, tag ? tag : "tripe-dh", 1));
+}
- /* --- Dump the public key --- */
+/* --- @km_tag@ --- *
+ *
+ * Arguments: @kdata *kd@ - pointer to the kdata object
+ *
+ * Returns: A pointer to the short tag by which the kdata was loaded.
+ */
- IF_TRACING(T_KEYMGMT, {
- trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
- trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
- })
+const char *km_tag(kdata *kd) { return (SYM_NAME(kd->kn)); }
- /* --- OK, accept the public key --- */
+/* --- @km_ref@ --- *
+ *
+ * Arguments: @kdata *kd@ = pointer to the kdata object
+ *
+ * Returns: ---
+ *
+ * Use: Claim a new reference to a kdata object.
+ */
- *t_exp = k->exp;
- G_COPY(gg, kpub, p);
- rc = 0;
+void km_ref(kdata *kd) { kd->ref++; }
- /* --- Tidy up --- */
+/* --- @km_unref@ --- *
+ *
+ * Arguments: @kdata *kd@ = pointer to the kdata object
+ *
+ * Returns: ---
+ *
+ * Use: Releases a reference to a kdata object.
+ */
-done:
- if (p) G_DESTROY(g, p);
- if (g) G_DESTROYGROUP(g);
- dstr_destroy(&t);
- return (rc);
+void km_unref(kdata *kd)
+{
+ if (--kd->ref) return;
+ if (kd->k) kd->grp->ops->freesc(kd->grp, kd->k);
+ kd->grp->ops->freege(kd->grp, kd->K);
+ kd->grp->ops->freegrp(kd->grp);
+ xfree(kd->tag);
+ DESTROY(kd);
}
/*----- That's all, folks -------------------------------------------------*/