/* -*-c-*-
*
- * $Id: keymgmt.c,v 1.3 2001/06/22 19:40:36 mdw Exp $
+ * $Id: keymgmt.c,v 1.4 2004/04/03 12:35:13 mdw Exp $
*
* Key loading and storing
*
/*----- Revision history --------------------------------------------------*
*
* $Log: keymgmt.c,v $
+ * Revision 1.4 2004/04/03 12:35:13 mdw
+ * Support elliptic curve key exchange.
+ *
* Revision 1.3 2001/06/22 19:40:36 mdw
* Support expiry of other peers' public keys.
*
/*----- Global variables --------------------------------------------------*/
-mpmont mg;
-dh_priv kpriv;
+group *gg;
+mp *kpriv;
/*----- Static variables --------------------------------------------------*/
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 };
+
/*----- Main code ---------------------------------------------------------*/
/* --- @keymoan@ --- *
*/
static void keymoan(const char *file, int line, const char *msg, void *p)
-{
- a_warn("%s:%i: error: %s", file, line, msg);
-}
+ { a_warn("%s:%i: error: %s", file, line, msg); }
/* --- @loadpriv@ --- *
*
* Arguments: @dstr *d@ = string to write errors in
- * @dh_priv *dh@ = where to store the key
*
* Returns: Zero if OK, nonzero on error.
*
* Use: Loads the private key from its keyfile.
*/
-static int loadpriv(dstr *d, dh_priv *dh)
+static int loadpriv(dstr *d)
{
key_file kf;
- key_packstruct kps[DH_PRIVFETCHSZ];
- key_packdef *kp;
- dh_priv mydh;
- int rc = 0;
- int e;
+ key *k;
+ key_data *kd;
+ dstr t = DSTR_INIT;
+ group *g = 0;
+ mp *x = 0;
+ int rc = -1;
+ const kgops **ko;
+ const char *e;
+
+ /* --- 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));
- rc = -1;
- } else {
- T( trace(T_KEYMGMT, "keymgmt: loaded private keyring `%s'", kr_priv); )
- kp = key_fetchinit(dh_privfetch, kps, &mydh);
- if ((e = key_fetchbyname(kp, &kf, tag_priv)) != 0) {
- dstr_putf(d, "error loading private key `%s': %s",
- tag_priv, key_strerror(e));
- rc = -1;
- } else {
- dh->dp.p = MP_COPY(mydh.dp.p);
- dh->dp.q = MP_COPY(mydh.dp.q);
- dh->dp.g = MP_COPY(mydh.dp.g);
- dh->x = MP_COPY(mydh.x);
- dh->y = MP_COPY(mydh.y);
- IF_TRACING(T_KEYMGMT, {
- trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", tag_priv);
- IF_TRACING(T_CRYPTO, {
- trace(T_CRYPTO, "crypto: p = %s", mpstr(kpriv.dp.p));
- trace(T_CRYPTO, "crypto: q = %s", mpstr(kpriv.dp.q));
- trace(T_CRYPTO, "crypto: g = %s", mpstr(kpriv.dp.g));
- trace(T_CRYPTO, "crypto: x = %s", mpstr(kpriv.x));
- trace(T_CRYPTO, "crypto: g^x = %s", mpstr(kpriv.y));
- })
- })
+ goto done_0;
+ }
+
+ /* --- Find the private key --- */
+
+ 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;
+ }
+
+ /* --- 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:;
+
+ /* --- Load the key --- */
+
+ if ((e = (*ko)->loadpriv(kd, &g, &x, &t)) != 0) {
+ dstr_putf(d, "error reading private key `%s': %s", t.buf, e);
+ goto done_1;
+ }
+
+ /* --- Check that the key is sensible --- */
+
+ if ((e = G_CHECK(g, &rand_global)) != 0) {
+ dstr_putf(d, "bad group in private key `%s': %s", t.buf, e);
+ goto done_1;
+ }
+
+ /* --- Good, we're happy --- *
+ *
+ * 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.
+ */
+
+ if (gg) {
+ if (!group_samep(g, gg)) {
+ dstr_putf(d, "private key `%s' has different group", t.buf);
+ goto done_1;
}
- key_fetchdone(kp);
- key_close(&kf);
+ G_DESTROYGROUP(gg);
}
+ if (kpriv)
+ mp_drop(kpriv);
+
+ /* --- Dump out the group --- */
+
+ IF_TRACING(T_KEYMGMT, {
+ trace(T_KEYMGMT, "keymgmt: extracted private key `%s'", 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));
+ })
+ })
+
+ /* --- Success! --- */
+
+ gg = g; g = 0;
+ kpriv = x; x = 0;
+ rc = 0;
+
+ /* --- Tidy up --- */
+
+done_1:
+ key_close(&kf);
+done_0:
+ dstr_destroy(&t);
+ if (x) mp_drop(x);
+ if (g) G_DESTROYGROUP(g);
return (rc);
}
int km_interval(void)
{
dstr d = DSTR_INIT;
- dh_priv dh;
key_file *kf;
int reload = 0;
if (fwatch_update(&w_priv, kr_priv)) {
T( trace(T_KEYMGMT, "keymgmt: private keyring updated: reloading..."); )
DRESET(&d);
- if (loadpriv(&d, &dh))
+ if (loadpriv(&d))
a_warn("%s -- ignoring changes", d.buf);
- else {
+ else
reload = 1;
- mpmont_destroy(&mg);
- dh_privfree(&kpriv);
- kpriv = dh;
- mpmont_create(&mg, kpriv.dp.p);
- }
}
/* --- Now check the public keys --- */
fwatch_init(&w_pub, kr_pub);
DRESET(&d);
- if (loadpriv(&d, &kpriv))
+ if (loadpriv(&d))
die(EXIT_FAILURE, "%s", d.buf);
- mpmont_create(&mg, kpriv.dp.p);
if (loadpub(&d))
die(EXIT_FAILURE, "%s", d.buf);
}
/* --- @km_getpubkey@ --- *
*
* Arguments: @const char *tag@ = public key tag to load
- * @dh_pub *kpub@ = where to put the public key
+ * @ge *kpub@ = where to put the public key
* @time_t *t_exp@ = where to put the expiry time
*
* Returns: Zero if OK, nonzero if it failed.
* Use: Fetches a public key from the keyring.
*/
-int km_getpubkey(const char *tag, dh_pub *kpub, time_t *t_exp)
+int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp)
{
- key_packstruct kps[DH_PUBFETCHSZ];
- key_packdef *kp;
key *k;
- dh_pub dp;
- int e;
+ key_data *kd;
+ dstr t = DSTR_INIT;
+ const kgops **ko;
+ const char *e;
+ group *g = 0;
+ ge *p = 0;
+ int rc = -1;
+
+ /* --- Find the key --- */
+
+ if (key_qtag(kf_pub, tag, &t, &k, &kd)) {
+ a_warn("private key `%s' not found in keyring `%s'", tag_priv, kr_priv);
+ goto done;
+ }
- kp = key_fetchinit(dh_pubfetch, kps, &dp);
- if ((k = key_bytag(kf_pub, tag)) == 0)
- e = KERR_NOTFOUND;
- else
- e = key_fetch(kp, k);
- key_fetchdone(kp);
- if (e) {
- a_warn("error loading public key `%s': %s", tag, key_strerror(e));
- return (-1);
+ /* --- Look up the key type in the table --- */
+
+ for (ko = kgtab; *ko; ko++) {
+ if (strcmp((*ko)->ty, k->type) == 0)
+ goto tymatch;
+ }
+ a_warn("public key `%s' has unknown type `%s'", t.buf, k->type);
+ goto done;
+tymatch:;
+
+ /* --- Load the key --- */
+
+ if ((e = (*ko)->loadpub(kd, &g, &p, &t)) != 0) {
+ a_warn("error reading public key `%s': %s", t.buf, e);
+ 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("public key `%s' has incorrect group", t.buf);
+ goto done;
+ }
+
+ /* --- Check the public group element --- */
+
+ if (group_check(gg, p)) {
+ a_warn("public key `%s' has bad public group element", t.buf);
+ goto done;
}
+
+ /* --- Dump the public key --- */
+
IF_TRACING(T_KEYMGMT, {
- trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", tag);
- IF_TRACING(T_CRYPTO, {
- trace(T_CRYPTO, "crypto: p = %s", mpstr(dp.dp.p));
- trace(T_CRYPTO, "crypto: q = %s", mpstr(dp.dp.q));
- trace(T_CRYPTO, "crypto: g = %s", mpstr(dp.dp.g));
- trace(T_CRYPTO, "crypto: g^x = %s", mpstr(dp.y));
- })
+ trace(T_KEYMGMT, "keymgmt: extracted public key `%s'", t.buf);
+ trace(T_CRYPTO, "crypto: p = %s", gestr(gg, p));
})
- if (!mp_eq(dp.dp.p, kpriv.dp.p) ||
- !mp_eq(dp.dp.q, kpriv.dp.q) ||
- !mp_eq(dp.dp.g, kpriv.dp.g)) {
- a_warn("public key `%s' has different group from private key", tag);
- return (-1);
- }
- kpub->dp.p = MP_COPY(dp.dp.p);
- kpub->dp.q = MP_COPY(dp.dp.q);
- kpub->dp.g = MP_COPY(dp.dp.g);
- kpub->y = MP_COPY(dp.y);
+
+ /* --- OK, accept the public key --- */
+
*t_exp = k->exp;
- return (0);
+ G_COPY(gg, kpub, p);
+ rc = 0;
+
+ /* --- Tidy up --- */
+
+done:
+ if (p) G_DESTROY(g, p);
+ if (g) G_DESTROYGROUP(g);
+ dstr_destroy(&t);
+ return (rc);
}
/*----- That's all, folks -------------------------------------------------*/