X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/799e58b96ef4948629d5e1b5401f537d74082ab7..b50ba1bda8722c66c40005f43707ebc63515732b:/server/keymgmt.c diff --git a/server/keymgmt.c b/server/keymgmt.c index 69ea74a5..a72164e5 100644 --- a/server/keymgmt.c +++ b/server/keymgmt.c @@ -9,175 +9,30 @@ * * This file is part of Trivial IP Encryption (TrIPE). * - * TrIPE is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * TrIPE is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your + * option) any later version. * - * TrIPE is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * TrIPE is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License - * along with TrIPE; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * along with TrIPE. If not, see . */ /*----- Header files ------------------------------------------------------*/ #include "tripe.h" -/*----- Key groups --------------------------------------------------------*/ - -/* The key-loading functions here must fill in the kdata slot @g@ and - * either @kpriv@ or @kpub@ as appropriate. The caller will take care of - * determining @kpub@ given a private key, and of ensuring that @kpriv@ is - * null for a public key. - */ - -typedef struct kgops { - const char *ty; - int (*loadpriv)(key_data *, kdata *, dstr *, dstr *); - int (*loadpub)(key_data *, kdata *, dstr *, dstr *); -} kgops; - -/* --- Diffie-Hellman --- */ - -static int kgdh_priv(key_data *d, kdata *kd, dstr *t, dstr *e) -{ - key_packstruct kps[DH_PRIVFETCHSZ]; - key_packdef *kp; - dh_priv dp; - int rc; - - kp = key_fetchinit(dh_privfetch, kps, &dp); - if ((rc = key_unpack(kp, d, t)) != 0) { - a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END); - goto fail_0; - } - kd->g = group_prime(&dp.dp); - kd->kpriv = MP_COPY(dp.x); - rc = 0; - goto done; -fail_0: - rc = -1; -done: - key_fetchdone(kp); - return (rc); -} - -static int kgdh_pub(key_data *d, kdata *kd, dstr *t, dstr *e) -{ - key_packstruct kps[DH_PUBFETCHSZ]; - key_packdef *kp; - dh_pub dp; - int rc; - - kp = key_fetchinit(dh_pubfetch, kps, &dp); - if ((rc = key_unpack(kp, d, t)) != 0) { - a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END); - goto fail_0; - } - kd->g = group_prime(&dp.dp); - kd->kpub = G_CREATE(kd->g); - if (G_FROMINT(kd->g, kd->kpub, dp.y)) { - a_format(e, "bad-public-vector", A_END); - goto fail_1; - } - rc = 0; - goto done; -fail_1: - G_DESTROY(kd->g, kd->kpub); - G_DESTROYGROUP(kd->g); -fail_0: - rc = -1; -done: - key_fetchdone(kp); - return (rc); -} - -static const kgops kgdh_ops = { "dh", kgdh_priv, kgdh_pub }; - -/* --- Elliptic curve --- */ - -static int kgec_priv(key_data *d, kdata *kd, dstr *t, dstr *e) -{ - key_packstruct kps[EC_PRIVFETCHSZ]; - key_packdef *kp; - ec_priv ep; - ec_info ei; - const char *err; - int rc; - - kp = key_fetchinit(ec_privfetch, kps, &ep); - if ((rc = key_unpack(kp, d, t)) != 0) { - a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END); - goto fail_0; - } - if ((err = ec_getinfo(&ei, ep.cstr)) != 0) { - a_format(e, "decode-failed", "%s", err, A_END); - goto fail_0; - } - kd->g = group_ec(&ei); - kd->kpriv = MP_COPY(ep.x); - rc = 0; - goto done; -fail_0: - rc = -1; -done: - key_fetchdone(kp); - return (rc); -} - -static int kgec_pub(key_data *d, kdata *kd, dstr *t, dstr *e) -{ - key_packstruct kps[EC_PUBFETCHSZ]; - key_packdef *kp; - ec_pub ep; - ec_info ei; - const char *err; - int rc; - - kp = key_fetchinit(ec_pubfetch, kps, &ep); - if ((rc = key_unpack(kp, d, t)) != 0) { - a_format(e, "unpack-failed", "%s", key_strerror(rc), A_END); - goto fail_0; - } - if ((err = ec_getinfo(&ei, ep.cstr)) != 0) { - a_format(e, "decode-failed", "%s", err, A_END); - goto fail_0; - } - kd->g = group_ec(&ei); - kd->kpub = G_CREATE(kd->g); - if (G_FROMEC(kd->g, kd->kpub, &ep.p)) { - a_format(e, "bad-public-vector", A_END); - goto fail_1; - } - rc = 0; - goto done; -fail_1: - G_DESTROY(kd->g, kd->kpub); - G_DESTROYGROUP(kd->g); -fail_0: - rc = -1; -done: - key_fetchdone(kp); - return (rc); -} - -static const kgops kgec_ops = { "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 - * @dstr *e@ = where to write errror tokens + * @dstr *e@ = where to write error tokens * @key_file *kf@ = key file * @key *k@ = key to inspect * @@ -189,18 +44,10 @@ static const kgops *kgtab[] = { &kgdh_ops, &kgec_ops, 0 }; static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k) { const char *p; - char *q, *qq; - dstr d = DSTR_INIT; + const bulkops *bops; + dstr d = DSTR_INIT, dd = DSTR_INIT; int rc = -1; - /* --- Symmetric encryption for bulk data --- */ - - if ((p = key_getattr(kf, k, "cipher")) == 0) p = "blowfish-cbc"; - if ((a->c = gcipher_byname(p)) == 0) { - a_format(e, "unknown-cipher", "%s", p, A_END); - goto done; - } - /* --- Hash function --- */ if ((p = key_getattr(kf, k, "hash")) == 0) p = "rmd160"; @@ -221,44 +68,23 @@ static int algs_get(algswitch *a, dstr *e, key_file *kf, key *k) goto done; } - /* --- Message authentication for bulk data --- */ + /* --- Bulk crypto transform --- */ - 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) { - a_format(e, "unknown-mac", "%s", d.buf, A_END); - goto done; - } - if (!q) - a->tagsz = a->m->hashsz; - else { - unsigned long n = strtoul(q, &qq, 0); - if (*qq) { - a_format(e, "bad-tag-length-string", "%s", q, A_END); - goto done; - } - if (n%8 || n/8 > a->m->hashsz) { - a_format(e, "bad-tag-length", "%lu", n, A_END); - goto done; - } - a->tagsz = n/8; - } - } else { - dstr_reset(&d); - dstr_putf(&d, "%s-hmac", a->h->name); - if ((a->m = gmac_byname(d.buf)) == 0) { - a_format(e, "no-hmac-for-hash", "%s", a->h->name, A_END); - goto done; - } - a->tagsz = a->h->hashsz/2; + 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; + + /* --- All done --- */ rc = 0; done: dstr_destroy(&d); + dstr_destroy(&dd); return (rc); } @@ -266,7 +92,7 @@ done: * * Arguments: @algswitch *a@ = a choice of algorithms * @dstr *e@ = where to write error tokens - * @const group *g@ = the group we're working in + * @const dhgrp *grp@ = the group we're working in * * Returns: Zero if OK; nonzero on error. * @@ -276,35 +102,9 @@ done: * for use by @keyset@ functions. */ -static int algs_check(algswitch *a, dstr *e, 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. Also must be based on a 64- or 128-bit - * block cipher or we can't do the data expiry properly. - */ - a->hashsz = a->h->hashsz; - if ((a->cksz = keysz(a->hashsz, a->c->keysz)) == 0) { - a_format(e, "cipher", "%s", a->c->name, - "no-key-size", "%lu", (unsigned long)a->hashsz, - A_END); - return (-1); - } - if ((a->mksz = keysz(a->hashsz, a->m->keysz)) == 0) { - a_format(e, "mac", "%s", a->m->name, - "no-key-size", "%lu", (unsigned long)a->hashsz, - A_END); - return (-1); - } - - /* --- Derive the data limit --- */ - - if (a->c->blksz < 16) a->expsz = MEG(64); - else a->expsz = MEG(2048); - - /* --- Ensure the MGF accepts hashes as keys --- */ if (keysz(a->hashsz, a->mgf->keysz) != a->hashsz) { a_format(e, "mgf", "%s", a->mgf->name, @@ -313,7 +113,7 @@ static int algs_check(algswitch *a, dstr *e, const group *g) return (-1); } - /* --- All ship-shape and Bristol-fashion --- */ + if (a->bulk->ops->checkalgs(a->bulk, a, e)) return (-1); return (0); } @@ -332,16 +132,19 @@ int km_samealgsp(const kdata *kdx, const kdata *kdy) { const algswitch *a = &kdx->algs, *aa = &kdy->algs; - return (group_samep(kdx->g, kdy->g) && a->c == aa->c && + return (kdx->grp->ops == kdy->grp->ops && + kdx->grp->ops->samegrpp(kdx->grp, kdy->grp) && a->mgf == aa->mgf && a->h == aa->h && - a->m == aa->m && a->tagsz == aa->tagsz); + a->bulk->ops == aa->bulk->ops && + a->bulk->ops->samealgsp(a->bulk, aa->bulk)); } /*----- Key data and key nodes --------------------------------------------*/ typedef struct keyhalf { const char *kind; - int (*load)(const kgops *, key_data *, kdata *, dstr *, dstr *); + int (*load)(key_file *, key *, key_data *, + const dhops *, kdata *, dstr *, dstr *); const char *kr; key_file *kf; fwatch w; @@ -350,8 +153,10 @@ typedef struct keyhalf { /* --- @kh_loadpub@, @kh_loadpriv@ --- * * - * Arguments: @const kgops *ko@ = key-group operations for key type - * @key_data *d@ = key data object as stored in keyring + * 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 @@ -359,10 +164,10 @@ typedef struct keyhalf { * 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 @g@, - * @kpriv@ and @kpub@ appropriately in all keys, handling the - * mismatch between the largely half-indifferent calling code - * and the group-specific loading functions. + * 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 @@ -371,46 +176,55 @@ typedef struct keyhalf { * checked. */ -static int kh_loadpub(const kgops *ko, key_data *d, kdata *kd, - dstr *t, dstr *e) +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 = ko->loadpub(d, kd, t, e)) != 0) + if ((rc = dh->ldpub(kf, k, d, kd, t, e)) != 0) goto fail_0; - if (group_check(kd->g, kd->kpub)) { - a_format(e, "bad-public-group-element"); + 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; } - kd->kpriv = 0; return (0); fail_1: - G_DESTROY(kd->g, kd->kpub); - G_DESTROYGROUP(kd->g); + kd->grp->ops->freege(kd->grp, kd->K); + kd->grp->ops->freegrp(kd->grp); fail_0: return (-1); } -static int kh_loadpriv(const kgops *ko, key_data *d, kdata *kd, - dstr *t, dstr *e) +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 = ko->loadpriv(d, kd, t, e)) != 0) + if ((rc = dh->ldpriv(kf, k, d, kd, t, e)) != 0) goto fail_0; - if ((err = G_CHECK(kd->g, &rand_global)) != 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; } - kd->kpub = G_CREATE(kd->g); - G_EXP(kd->g, kd->kpub, kd->g->g, kd->kpriv); + 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: - mp_drop(kd->kpriv); - G_DESTROYGROUP(kd->g); + 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); } @@ -461,18 +275,15 @@ static int kh_reopen(keyhalf *kh) key_file *kf = CREATE(key_file); if (key_open(kf, kh->kr, KOPEN_READ, keymoan, kh)) { - a_warn("KEYMGMT", "%s-keyring", kh->kind, "%s", kh->kr, - "read-error", "?ERRNO", A_END); DESTROY(kf); return (-1); - } else { - if (kh->kf) { - key_close(kh->kf); - DESTROY(kh->kf); - } - kh->kf = kf; - return (0); } + if (kh->kf) { + key_close(kh->kf); + DESTROY(kh->kf); + } + kh->kf = kf; + return (0); } /* --- @kh_init@ --- * @@ -492,10 +303,7 @@ 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 (kh_reopen(kh)) - die(EXIT_FAILURE, "failed to load %s keyring `%s'", kh->kind, kr); + if (kh_reopen(kh)) exit(EXIT_FAILURE); } /* --- @kh_load@ --- * @@ -522,7 +330,8 @@ static kdata *kh_load(keyhalf *kh, const char *tag, int complainp) key_data **d; kdata *kd; const char *ty; - const kgops **ko; + const dhops *dh; + T( const dhgrp *g; ) /* --- Find the key and grab its tag --- */ @@ -545,24 +354,24 @@ static kdata *kh_load(keyhalf *kh, const char *tag, int complainp) if (!ty && strncmp(k->type, "tripe-", 6) == 0) ty = k->type + 6; if (!ty) ty = "dh"; - for (ko = kgtab; *ko; ko++) - if (strcmp((*ko)->ty, ty) == 0) goto foundko; + 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; -foundko: +founddh: kd = CREATE(kdata); - if (kh->load(*ko, *d, kd, &t, &e)) { + 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", kh->kr, "key", "%s", t.buf, "*%s", e.buf, A_END); goto fail_1; } if (algs_get(&kd->algs, &e, kh->kf, k) || - (kd->kpriv && algs_check(&kd->algs, &e, kd->g))) { + 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); @@ -570,32 +379,29 @@ foundko: } kd->tag = xstrdup(t.buf); - kd->indexsz = mp_octets(kd->g->r); kd->ref = 1; kd->kn = 0; + kd->id = k->id; kd->t_exp = k->exp; IF_TRACING(T_KEYMGMT, { trace(T_KEYMGMT, "keymgmt: loaded %s key `%s'", kh->kind, t.buf); IF_TRACING(T_CRYPTO, { - trace(T_CRYPTO, "crypto: r = %s", mpstr(kd->g->r)); - trace(T_CRYPTO, "crypto: h = %s", mpstr(kd->g->h)); - if (kd->kpriv) - trace(T_CRYPTO, "crypto: x = %s", mpstr(kd->kpriv)); - trace(T_CRYPTO, "crypto: cipher = %s", kd->algs.c->name); - trace(T_CRYPTO, "crypto: mgf = %s", kd->algs.mgf->name); - trace(T_CRYPTO, "crypto: hash = %s", kd->algs.h->name); - trace(T_CRYPTO, "crypto: mac = %s/%lu", - kd->algs.m->name, (unsigned long)kd->algs.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); }) }) goto done; fail_2: - if (kd->kpriv) mp_drop(kd->kpriv); - G_DESTROY(kd->g, kd->kpub); - G_DESTROYGROUP(kd->g); + 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: @@ -707,7 +513,7 @@ static int kh_refresh(keyhalf *kh) kn->f &= ~KNF_BROKEN; if (kd->t_exp == kn->kd->t_exp && km_samealgsp(kd, kn->kd) && - G_EQ(kd->g, kd->kpub, kn->kd->kpub)) { + kd->grp->ops->eq(kd->grp, kd->K, kn->kd->K)) { T( trace(T_KEYMGMT, "keymgmt: key `%s' unchanged", SYM_NAME(kn)); ) continue; } @@ -722,50 +528,32 @@ static int kh_refresh(keyhalf *kh) return (changep); } -/*----- Main code ---------------------------------------------------------*/ - -const char *tag_priv; -kdata *master; - -group *gg; -mp *kpriv; -ge *kpub; -algswitch algs; -size_t indexsz; - -/* --- @update_compat@ --- * +/* --- @kh_clear@ --- * * - * Arguments: @kdata *kd@ = proposed new master key + * Arguments: @keyhalf *kh@ = pointer to keyhalf structure * - * Returns: Zero on success, nonzero to refuse the replacement. + * Returns: --- * - * Use: Updates the exported private key variables for compatibility. + * Use: Clears out the keyhalf's keyring and flushes the cache. */ -static int update_compat(kdata *kd) +static void kh_clear(keyhalf *kh) { - if (gg) { - if (!group_samep(kd->g, gg)) { - a_warn("KEYMGMT", "private-keyring", - "%s", kd->kn->kh->kr, "key", "%s", kd->tag, - "changed-group", A_END); - return (-1); - } - G_DESTROYGROUP(gg); - } - gg = kd->g; + sym_iter i; + knode *kn; - if (kpriv) mp_drop(kpriv); - kpriv = MP_COPY(kd->kpriv); - if (kpub) G_DESTROY(gg, kpub); - kpub = G_CREATE(gg); - G_COPY(gg, kpub, kd->kpub); + if (!kh->kf) return; + for (sym_mkiter(&i, &kh->tab); (kn = sym_next(&i)) != 0; ) + if (kn->kd) km_unref(kn->kd); + sym_destroy(&kh->tab); + key_close(kh->kf); + kh->kf = 0; +} - algs = kd->algs; - indexsz = kd->indexsz; +/*----- Main code ---------------------------------------------------------*/ - return (0); -} +const char *tag_priv; +kdata *master; /* --- @km_init@ --- * * @@ -785,8 +573,10 @@ void km_init(const char *privkr, const char *pubkr, const char *ptag) for (hh = ghashtab; *hh; hh++) { if ((*hh)->hashsz > MAXHASHSZ) { - die(EXIT_FAILURE, "INTERNAL ERROR: %s hash length %lu > MAXHASHSZ %d", - (*hh)->name, (unsigned long)(*hh)->hashsz, MAXHASHSZ); + a_warn("ABORT", "hash-size-too-large", "hash", + "%s", (*hh)->name, "size", "%lu", (*hh)->hashsz, + "limit", "%d", MAXHASHSZ, A_END); + abort(); } } @@ -795,8 +585,6 @@ void km_init(const char *privkr, const char *pubkr, const char *ptag) tag_priv = ptag; if ((master = km_findpriv(ptag)) == 0) exit(EXIT_FAILURE); - - if (update_compat(master)) exit(EXIT_FAILURE); } /* --- @km_reload@ --- * @@ -816,7 +604,7 @@ int km_reload(void) if (kh_refresh(&priv)) { changep = 1; kd = master->kn->kd; - if (kd != master && !update_compat(kd)) { + if (kd != master) { km_unref(master); km_ref(kd); master = kd; @@ -827,6 +615,25 @@ int km_reload(void) return (changep); } +/* --- @km_clear@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Forget the currently loaded keyrings. The @master@ key will + * be cleared, but other keys already loaded will continue to + * exist until their reference count drops to zero. Call + * @km_init@ to make everything work again. + */ + +void km_clear(void) +{ + kh_clear(&priv); + kh_clear(&pub); + if (master) { km_unref(master); master = 0; } +} + /* --- @km_findpub@, @km_findpriv@ --- * * * Arguments: @const char *tag@ = key tag to load @@ -847,6 +654,41 @@ kdata *km_findpriv(const char *tag) else return (kh_find(&priv, tag ? tag : "tripe-dh", 1)); } +/* --- @km_findpubbyid@, @km_findprivbyid@ --- * + * + * Arguments: @uint32 id@ = key id to load + * + * Returns: Pointer to the kdata object if successful, or null on error. + * + * Use: Fetches a public or private key from the keyring given its + * numeric id. + */ + +static kdata *findbyid(keyhalf *kh, uint32 id) +{ + key *k; + kdata *kd; + + k = key_byid(kh->kf, id); if (!k) goto notfound; + kd = kh_find(kh, k->tag, 1); if (!kd) goto notfound; + if (kd->id != id) { km_unref(kd); goto notfound; } + return (kd); + +notfound: + a_warn("KX", "%s-keyring", kh->kind, "%s", kh->kr, + "unknown-key-id", "0x%08lx", (unsigned long)id, + A_END); + return (0); +} + +kdata *km_findpubbyid(uint32 id) { return (findbyid(&pub, id)); } + +kdata *km_findprivbyid(uint32 id) +{ + if (id == master->id) { km_ref(master); return (master); } + else return findbyid(&priv, id); +} + /* --- @km_tag@ --- * * * Arguments: @kdata *kd@ - pointer to the kdata object @@ -879,44 +721,11 @@ void km_ref(kdata *kd) { kd->ref++; } void km_unref(kdata *kd) { if (--kd->ref) return; - if (kd->kpriv) mp_drop(kd->kpriv); - G_DESTROY(kd->g, kd->kpub); + 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); - G_DESTROYGROUP(kd->g); -} - -/* --- @km_getpubkey@ --- * - * - * 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 - * - * Returns: Zero if OK, nonzero if it failed. - * - * Use: Fetches a public key from the keyring. (Temporary - * compatibility hack.) - */ - -int km_getpubkey(const char *tag, ge *kpub, time_t *t_exp) -{ - kdata *kd; - int rc = -1; - - if ((kd = km_findpub(tag)) == 0) - goto done_0; - if (!km_samealgsp(kd, master)) { - a_warn("KEYMGMT", "public-keyring", - "%s", kd->kn->kh->kr, "key", "%s", kd->tag, - "algorithm-mismatch", A_END); - goto done_1; - } - G_COPY(gg, kpub, kd->kpub); - *t_exp = kd->t_exp; - rc = 0; -done_1: - km_unref(kd); -done_0: - return (rc); + DESTROY(kd); } /*----- That's all, folks -------------------------------------------------*/