2 * keydb.c - Routines for DB access that just use store/fetch.
4 * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
20 * The routines in this file are meant to be used as an initial step when
21 * adding a new db access module. They provide various functions required
22 * of the db access module using only the store and fetch functions. As
23 * they need to parse the actual OpenPGP data to work they are a lot
24 * slower than custom functions however.
30 #include "decodekey.h"
34 #include "keystructs.h"
44 * keyid2uid - Takes a keyid and returns the primary UID for it.
45 * @keyid: The keyid to lookup.
47 char *generic_keyid2uid(struct onak_dbctx *dbctx, uint64_t keyid)
49 struct openpgp_publickey *publickey = NULL;
50 struct openpgp_signedpacket_list *curuid = NULL;
54 if (dbctx->fetch_key_id(dbctx, keyid, &publickey, false) &&
56 curuid = publickey->uids;
57 while (curuid != NULL && buf[0] == 0) {
58 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
59 snprintf(buf, 1023, "%.*s",
60 (int) curuid->packet->length,
61 curuid->packet->data);
63 curuid = curuid -> next;
65 free_publickey(publickey);
76 #ifdef NEED_GETKEYSIGS
78 * getkeysigs - Gets a linked list of the signatures on a key.
79 * @keyid: The keyid to get the sigs for.
80 * @revoked: Is the key revoked?
82 * This function gets the list of signatures on a key. Used for key
83 * indexing and doing stats bits. If revoked is non-NULL then if the key
84 * is revoked it's set to true.
86 struct ll *generic_getkeysigs(struct onak_dbctx *dbctx,
87 uint64_t keyid, bool *revoked)
89 struct ll *sigs = NULL;
90 struct openpgp_signedpacket_list *uids = NULL;
91 struct openpgp_packet_list *cursig;
92 struct openpgp_publickey *publickey = NULL;
94 dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
96 if (publickey != NULL) {
97 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
98 for (cursig = uids->sigs; cursig != NULL;
99 cursig = cursig->next) {
101 createandaddtohash(sig_keyid(
105 if (revoked != NULL) {
106 *revoked = publickey->revoked;
108 free_publickey(publickey);
116 * cached_getkeysigs - Gets the signatures on a key.
117 * @keyid: The key we want the signatures for.
119 * This function gets the signatures on a key. It's the same as the
120 * getkeysigs function above except we use the hash module to cache the
121 * data so if we need it again it's already loaded.
123 struct ll *generic_cached_getkeysigs(struct onak_dbctx *dbctx, uint64_t keyid)
125 struct stats_key *key = NULL;
126 struct stats_key *signedkey = NULL;
127 struct ll *cursig = NULL;
128 struct ll *sigs = NULL;
129 bool revoked = false;
135 key = findinhash(keyid);
137 if (key == NULL || key->gotsigs == false) {
138 sigs = dbctx->getkeysigs(dbctx, keyid, &revoked);
143 key = createandaddtohash(keyid);
146 key->revoked = revoked;
147 for (cursig = key->sigs; cursig != NULL;
148 cursig = cursig->next) {
149 signedkey = (struct stats_key *) cursig->object;
150 signedkey->signs = lladd(signedkey->signs, key);
158 #ifdef NEED_GETFULLKEYID
160 * getfullkeyid - Maps a 32bit key id to a 64bit one.
161 * @keyid: The 32bit keyid.
163 * This function maps a 32bit key id to the full 64bit one. It returns the
164 * full keyid. If the key isn't found a keyid of 0 is returned.
166 uint64_t generic_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
168 struct openpgp_publickey *publickey = NULL;
170 if (keyid < 0x100000000LL) {
171 dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
172 if (publickey != NULL) {
173 get_keyid(publickey, &keyid);
174 free_publickey(publickey);
185 #ifdef NEED_UPDATEKEYS
187 * update_keys - Takes a list of public keys and updates them in the DB.
188 * @keys: The keys to update in the DB.
189 * @sendsync: Should we send a sync mail to our peers.
191 * Takes a list of keys and adds them to the database, merging them with
192 * the key in the database if it's already present there. The key list is
193 * update to contain the minimum set of updates required to get from what
194 * we had before to what we have now (ie the set of data that was added to
195 * the DB). Returns the number of entirely new keys added.
197 int generic_update_keys(struct onak_dbctx *dbctx,
198 struct openpgp_publickey **keys, bool sendsync)
200 struct openpgp_publickey *curkey = NULL;
201 struct openpgp_publickey *oldkey = NULL;
202 struct openpgp_publickey *prev = NULL;
203 struct openpgp_fingerprint fp;
207 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
208 intrans = dbctx->starttrans(dbctx);
209 get_fingerprint(curkey->publickey, &fp);
210 logthing(LOGTHING_INFO,
211 "Fetching key, result: %d",
212 dbctx->fetch_key_fp(dbctx, &fp, &oldkey,
216 * If we already have the key stored in the DB then merge it
217 * with the new one that's been supplied. Otherwise the key
218 * we've just got is the one that goes in the DB and also the
219 * one that we send out.
221 if (oldkey != NULL) {
222 merge_keys(oldkey, curkey);
223 if (curkey->sigs == NULL &&
224 curkey->uids == NULL &&
225 curkey->subkeys == NULL) {
227 *keys = curkey->next;
229 prev->next = curkey->next;
231 free_publickey(curkey);
236 logthing(LOGTHING_INFO,
237 "Merged key; storing updated key.");
238 dbctx->store_key(dbctx, oldkey, intrans,
241 free_publickey(oldkey);
244 logthing(LOGTHING_INFO,
245 "Storing completely new key.");
246 dbctx->store_key(dbctx, curkey, intrans, false);
249 dbctx->endtrans(dbctx);
252 if (sendsync && keys != NULL) {
258 #endif /* NEED_UPDATEKEYS */
261 static int generic_fetch_key_fp(struct onak_dbctx *dbctx,
262 struct openpgp_fingerprint *fingerprint,
263 struct openpgp_publickey **publickey, bool intrans)
268 if (fingerprint->length > MAX_FINGERPRINT_LEN) {
273 * We assume if the backend is using this function it's not storing
274 * anything bigger than the 64 bit key ID and just truncate the
275 * fingerprint to get that value. v4 keys want the lowest 64 bits, v5
276 * keys need the top 64 bits. This doesn't work for v3 keys,
277 * but there's no way to map from v3 fingerprint to v3 key ID so
278 * if the backend can't do it we're going to fail anyway.
281 if (fingerprint->length == 20) {
283 for (i = (fingerprint->length - 8); i < fingerprint->length;
285 keyid = (keyid << 8) + fingerprint->fp[i];
289 for (i = 0; i < 8; i++) {
290 keyid = (keyid << 8) + fingerprint->fp[i];
294 return dbctx->fetch_key_id(dbctx, keyid, publickey, intrans);