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.
64 * @revoked: Is the key revoked?
66 * This function gets the list of signatures on a key. Used for key
67 * indexing and doing stats bits. If revoked is non-NULL then if the key
68 * is revoked it's set to true.
70 struct ll *getkeysigs(uint64_t keyid, bool *revoked)
72 struct ll *sigs = NULL;
73 struct openpgp_signedpacket_list *uids = NULL;
74 struct openpgp_publickey *publickey = NULL;
76 fetch_key(keyid, &publickey, false);
78 if (publickey != NULL) {
79 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
80 sigs = keysigs(sigs, uids->sigs);
82 if (revoked != NULL) {
83 *revoked = (publickey->revocations != NULL);
85 free_publickey(publickey);
93 * cached_getkeysigs - Gets the signatures on a key.
94 * @keyid: The key we want the signatures for.
96 * This function gets the signatures on a key. It's the same as the
97 * getkeysigs function above except we use the hash module to cache the
98 * data so if we need it again it's already loaded.
100 struct ll *cached_getkeysigs(uint64_t keyid)
102 struct stats_key *key = NULL;
103 struct stats_key *signedkey = NULL;
104 struct ll *cursig = NULL;
105 bool revoked = false;
111 key = createandaddtohash(keyid);
113 if (key->gotsigs == false) {
114 key->sigs = getkeysigs(key->keyid, &revoked);
115 key->revoked = revoked;
116 for (cursig = key->sigs; cursig != NULL;
117 cursig = cursig->next) {
118 signedkey = (struct stats_key *) cursig->object;
119 signedkey->signs = lladd(signedkey->signs, key);
127 #ifdef NEED_GETFULLKEYID
129 * getfullkeyid - Maps a 32bit key id to a 64bit one.
130 * @keyid: The 32bit keyid.
132 * This function maps a 32bit key id to the full 64bit one. It returns the
133 * full keyid. If the key isn't found a keyid of 0 is returned.
135 uint64_t getfullkeyid(uint64_t keyid)
137 struct openpgp_publickey *publickey = NULL;
139 if (keyid < 0x100000000LL) {
140 fetch_key(keyid, &publickey, false);
141 if (publickey != NULL) {
142 keyid = get_keyid(publickey);
143 free_publickey(publickey);