+/**
+ * fetch_key_fp - Given a fingerprint fetch the key from storage.
+ */
+static int db4_fetch_key_fp(struct onak_dbctx *dbctx,
+ struct openpgp_fingerprint *fingerprint,
+ struct openpgp_publickey **publickey,
+ bool intrans)
+{
+ struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
+ struct openpgp_packet_list *packets = NULL;
+ DBT key, data;
+ int ret = 0;
+ int numkeys = 0;
+ struct buffer_ctx fetchbuf;
+ struct openpgp_fingerprint subfp;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ data.size = 0;
+ data.data = NULL;
+
+ key.size = fingerprint->length;
+ key.data = fingerprint->fp;
+
+ if (!intrans) {
+ db4_starttrans(dbctx);
+ }
+
+ ret = keydb_fp(privctx, fingerprint)->get(keydb_fp(privctx,
+ fingerprint),
+ privctx->txn,
+ &key,
+ &data,
+ 0); /* flags*/
+
+ if (ret == DB_NOTFOUND) {
+ /* If we didn't find the key ID see if it's a subkey ID */
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+ data.data = subfp.fp;
+ data.ulen = MAX_FINGERPRINT_LEN;
+ data.flags = DB_DBT_USERMEM;
+ key.data = fingerprint->fp;
+ key.size = fingerprint->length;
+
+ ret = privctx->subkeydb->get(privctx->subkeydb,
+ privctx->txn,
+ &key,
+ &data,
+ 0); /* flags*/
+
+ if (ret == 0) {
+ /* We got a subkey match; retrieve the actual key */
+ memset(&key, 0, sizeof(key));
+ key.size = subfp.length = data.size;
+ key.data = subfp.fp;
+
+ memset(&data, 0, sizeof(data));
+ data.size = 0;
+ data.data = NULL;
+
+ ret = keydb_fp(privctx, &subfp)->get(
+ keydb_fp(privctx, &subfp),
+ privctx->txn,
+ &key,
+ &data,
+ 0); /* flags*/
+ }
+ }
+
+ if (ret == 0) {
+ fetchbuf.buffer = data.data;
+ fetchbuf.offset = 0;
+ fetchbuf.size = data.size;
+ read_openpgp_stream(buffer_fetchchar, &fetchbuf,
+ &packets, 0);
+ parse_keys(packets, publickey);
+ free_packet_list(packets);
+ packets = NULL;
+ numkeys++;
+ } else if (ret != DB_NOTFOUND) {
+ logthing(LOGTHING_ERROR,
+ "Problem retrieving key: %s",
+ db_strerror(ret));
+ }
+
+ if (!intrans) {
+ db4_endtrans(dbctx);
+ }
+
+ return (numkeys);
+}
+