From: Mark Wooding Date: Sun, 3 Sep 2017 23:17:40 +0000 (+0100) Subject: server/keymgmt.c: Track and find keys by their 32-bit IDs. X-Git-Tag: 1.5.0~47 X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/commitdiff_plain/fb6a9f13a40d1b9e797b4fe858a06cfdbcc1109b server/keymgmt.c: Track and find keys by their 32-bit IDs. --- diff --git a/server/keymgmt.c b/server/keymgmt.c index 51a13d70..7386408f 100644 --- a/server/keymgmt.c +++ b/server/keymgmt.c @@ -387,6 +387,7 @@ founddh: kd->tag = xstrdup(t.buf); kd->ref = 1; kd->kn = 0; + kd->id = k->id; kd->t_exp = k->exp; IF_TRACING(T_KEYMGMT, { @@ -616,6 +617,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 diff --git a/server/tripe-admin.5.in b/server/tripe-admin.5.in index 5836eeae..a737e778 100644 --- a/server/tripe-admin.5.in +++ b/server/tripe-admin.5.in @@ -1384,6 +1384,11 @@ A key named .I tag couldn't be found in the keyring. .SP +.BI "KEYMGMT " which "-keyring " file " unknown-key-id 0x" keyid +A key with the given +.I keyid +(in hex) was requested but not found. +.SP .BI "KEYMGMT " which "-keyring " file " line " line " " message The contents of the keyring file are invalid. There may well be a bug in the diff --git a/server/tripe.h b/server/tripe.h index 940e3d61..d3dce08b 100644 --- a/server/tripe.h +++ b/server/tripe.h @@ -402,6 +402,7 @@ struct algswitch { struct kdata { unsigned ref; /* Reference counter */ struct knode *kn; /* Pointer to cache entry */ + uint32 id; /* The underlying key's id */ char *tag; /* Full tag name of the key */ dhgrp *grp; /* The group we work in */ dhsc *k; /* The private key (or null) */ @@ -827,6 +828,19 @@ extern int km_reload(void); extern kdata *km_findpub(const char */*tag*/); extern kdata *km_findpriv(const char */*tag*/); +/* --- @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. + */ + +extern kdata *km_findpubbyid(uint32 /*id*/); +extern kdata *km_findprivbyid(uint32 /*id*/); + /* --- @km_samealgsp@ --- * * * Arguments: @const kdata *kdx, *kdy@ = two key data objects