2 * keydb.c - Routines for DB access that just use store/fetch.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
10 * The routines in this file are meant to be used as an initial step when
11 * adding a new db access module. They provide various functions required
12 * of the db access module using only the store and fetch functions. As
13 * they need to parse the actual OpenPGP data to work they are a lot
14 * slower than custom functions however.
19 #include "decodekey.h"
23 #include "keystructs.h"
29 * keyid2uid - Takes a keyid and returns the primary UID for it.
30 * @keyid: The keyid to lookup.
32 char *keyid2uid(uint64_t keyid)
34 struct openpgp_publickey *publickey = NULL;
35 struct openpgp_signedpacket_list *curuid = NULL;
39 if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
40 curuid = publickey->uids;
41 while (curuid != NULL && buf[0] == 0) {
42 if (curuid->packet->tag == 13) {
43 snprintf(buf, 1023, "%.*s",
44 (int) curuid->packet->length,
45 curuid->packet->data);
47 curuid = curuid -> next;
49 free_publickey(publickey);
60 #ifdef NEED_GETKEYSIGS
62 * getkeysigs - Gets a linked list of the signatures on a key.
63 * @keyid: The keyid to get the sigs for.
65 * This function gets the list of signatures on a key. Used for key
66 * indexing and doing stats bits.
68 struct ll *getkeysigs(uint64_t keyid)
70 struct ll *sigs = NULL;
71 struct openpgp_signedpacket_list *uids = NULL;
72 struct openpgp_publickey *publickey = NULL;
74 fetch_key(keyid, &publickey, false);
76 if (publickey != NULL) {
77 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
78 sigs = keysigs(sigs, uids->sigs);
80 free_publickey(publickey);
88 * cached_getkeysigs - Gets the signatures on a key.
89 * @keyid: The key we want the signatures for.
91 * This function gets the signatures on a key. It's the same as the
92 * getkeysigs function above except we use the hash module to cache the
93 * data so if we need it again it's already loaded.
95 struct ll *cached_getkeysigs(uint64_t keyid)
97 struct stats_key *key = NULL;
98 struct stats_key *signedkey = NULL;
99 struct ll *cursig = NULL;
105 key = createandaddtohash(keyid);
107 if (key->gotsigs == false) {
108 key->sigs = getkeysigs(key->keyid);
109 for (cursig = key->sigs; cursig != NULL;
110 cursig = cursig->next) {
111 signedkey = (struct stats_key *) cursig->object;
112 signedkey->signs = lladd(signedkey->signs, key);
120 #ifdef NEED_GETFULLKEYID
122 * getfullkeyid - Maps a 32bit key id to a 64bit one.
123 * @keyid: The 32bit keyid.
125 * This function maps a 32bit key id to the full 64bit one. It returns the
126 * full keyid. If the key isn't found a keyid of 0 is returned.
128 uint64_t getfullkeyid(uint64_t keyid)
130 struct openpgp_publickey *publickey = NULL;
132 if (keyid < 0x100000000LL) {
133 fetch_key(keyid, &publickey, false);
134 if (publickey != NULL) {
135 keyid = get_keyid(publickey);
136 free_publickey(publickey);