]> the.earth.li Git - onak.git/blobdiff - keydb_hkp.c
Extend database backends to support fetching by key fingerprint
[onak.git] / keydb_hkp.c
index ac1edace24617127c6cd6375c7241f493044e025..3fa15935196b4222d86075d86573942822435acd 100644 (file)
@@ -147,35 +147,20 @@ static size_t hkp_curl_recv_data(void *buffer, size_t size, size_t nmemb,
        return (nmemb * size);
 }
 
-/**
- *     fetch_key - Given a keyid fetch the key from storage.
- *     @keyid: The keyid to fetch.
- *     @publickey: A pointer to a structure to return the key in.
- *     @intrans: If we're already in a transaction.
- *
- *     We use the hex representation of the keyid as the filename to fetch the
- *     key from. The key is stored in the file as a binary OpenPGP stream of
- *     packets, so we can just use read_openpgp_stream() to read the packets
- *     in and then parse_keys() to parse the packets into a publickey
- *     structure.
- */
-static int hkp_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int hkp_fetch_key_url(char *url,
+               struct openpgp_publickey **publickey,
                bool intrans)
 {
        struct openpgp_packet_list *packets = NULL;
-       char keyurl[1024];
        CURLcode res;
        struct buffer_ctx buf;
+       int count = 0;
 
        buf.offset = 0;
        buf.size = 8192;
        buf.buffer = malloc(8192);
 
-       snprintf(keyurl, sizeof(keyurl),
-                       "%s/lookup?op=get&search=0x%08" PRIX64,
-                       hkpbase, keyid);
-
-       curl_easy_setopt(curl, CURLOPT_URL, keyurl);
+       curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                        hkp_curl_recv_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
@@ -184,7 +169,7 @@ static int hkp_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        if (res == 0) {
                buf.offset = 0;
                dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
-               parse_keys(packets, publickey);
+               count = parse_keys(packets, publickey);
                free_packet_list(packets);
                packets = NULL;
        } else {
@@ -196,7 +181,51 @@ static int hkp_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        buf.offset = buf.size = 0;
        buf.buffer = NULL;
 
-       return (res == 0) ? 1 : 0;
+       return count;
+}
+
+/**
+ *     hkp_fetch_key_id - Given a keyid fetch the key from HKP server.
+ */
+static int hkp_fetch_key_id(uint64_t keyid,
+               struct openpgp_publickey **publickey,
+               bool intrans)
+{
+       char keyurl[1024];
+
+       snprintf(keyurl, sizeof(keyurl),
+                       "%s/lookup?op=get&search=0x%08" PRIX64,
+                       hkpbase, keyid);
+
+       return (hkp_fetch_key_url(keyurl, publickey, intrans));
+}
+
+/**
+ *     hkp_fetch_key_fp - Given a fingerprint fetch the key from HKP server.
+ */
+static int hkp_fetch_key_fp(uint8_t *fp, size_t fpsize,
+               struct openpgp_publickey **publickey,
+               bool intrans)
+{
+       char keyurl[1024];
+       int i, ofs;
+
+       if (fpsize > MAX_FINGERPRINT_LEN) {
+               return 0;
+       }
+
+       ofs = snprintf(keyurl, sizeof(keyurl),
+                       "%s/lookup?op=get&search=0x", hkpbase);
+
+       if ((ofs + fpsize * 2 + 1)> sizeof(keyurl)) {
+               return 0;
+       }
+
+       for (i = 0; i < fpsize; i++) {
+               ofs += sprintf(&keyurl[ofs], "%02X", fp[i]);
+       }
+
+       return (hkp_fetch_key_url(keyurl, publickey, intrans));
 }
 
 /**
@@ -212,42 +241,13 @@ static int hkp_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 static int hkp_fetch_key_text(const char *search,
                struct openpgp_publickey **publickey)
 {
-       struct openpgp_packet_list *packets = NULL;
        char keyurl[1024];
-       CURLcode res;
-       struct buffer_ctx buf;
-       int count = 0;
-
-       buf.offset = 0;
-       buf.size = 8192;
-       buf.buffer = malloc(8192);
 
        snprintf(keyurl, sizeof(keyurl),
                        "%s/lookup?op=get&search=%s",
                        hkpbase, search);
 
-       curl_easy_setopt(curl, CURLOPT_URL, keyurl);
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
-                       hkp_curl_recv_data);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
-       res = curl_easy_perform(curl);
-
-       if (res == 0) {
-               buf.offset = 0;
-               dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
-               count = parse_keys(packets, publickey);
-               free_packet_list(packets);
-               packets = NULL;
-       } else {
-               logthing(LOGTHING_ERROR, "Couldn't find key: %s (%d)",
-                       curl_easy_strerror(res), res);
-       }
-
-       free(buf.buffer);
-       buf.offset = buf.size = 0;
-       buf.buffer = NULL;
-
-       return count;
+       return (hkp_fetch_key_url(keyurl, publickey, false));
 }
 
 /**
@@ -361,7 +361,8 @@ struct dbfuncs keydb_hkp_funcs = {
        .cleanupdb              = hkp_cleanupdb,
        .starttrans             = hkp_starttrans,
        .endtrans               = hkp_endtrans,
-       .fetch_key              = hkp_fetch_key,
+       .fetch_key_id           = hkp_fetch_key_id,
+       .fetch_key_fp           = hkp_fetch_key_fp,
        .fetch_key_text         = hkp_fetch_key_text,
        .store_key              = hkp_store_key,
        .update_keys            = generic_update_keys,