2 * keydb_hkp.c - Routines to store and fetch keys from another keyserver.
4 * Copyright 2013 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/>.
24 #include <curl/curl.h>
26 #include "build-config.h"
29 #include "charfuncs.h"
31 #include "keystructs.h"
34 #include "onak-conf.h"
37 struct onak_hkp_dbctx {
38 struct onak_db_config *config; /* Our DB config info */
43 static int hkp_parse_url(struct onak_hkp_dbctx *privctx, const char *url)
45 char proto[6], host[256];
50 proto[0] = host[0] = 0;
53 matched = sscanf(url, "%5[a-z]://%256[a-zA-Z0-9.-]:%u", proto, host,
57 sscanf(url, "%256[a-zA-Z0-9.-]:%u", host, &port);
61 logthing(LOGTHING_CRITICAL, "Couldn't parse HKP host: %s",
67 if (proto[0] == 0 || !strcmp(proto, "hkp")) {
71 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
72 "http://%s:%u/pks", host, port);
73 } else if (!strcmp(proto, "hkps")) {
77 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
78 "https://%s:%u/pks", host, port);
79 } else if (strcmp(proto, "http") && strcmp(proto, "https")) {
80 logthing(LOGTHING_CRITICAL, "Unknown HKP protocol: %s",
84 } else if (port == 0) {
85 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
86 "%s://%s/pks", proto, host);
88 snprintf(privctx->hkpbase, sizeof(privctx->hkpbase),
89 "%s://%s:%u/pks", proto, host, port);
97 * Receive data from a CURL request and process it into a buffer context.
99 static size_t hkp_curl_recv_data(void *buffer, size_t size, size_t nmemb,
102 buffer_putchar(ctx, nmemb * size, buffer);
104 return (nmemb * size);
107 static int hkp_fetch_key_url(struct onak_dbctx *dbctx,
109 struct openpgp_publickey **publickey,
110 __unused bool intrans)
112 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
113 struct openpgp_packet_list *packets = NULL;
115 struct buffer_ctx buf;
120 buf.buffer = malloc(8192);
122 curl_easy_setopt(privctx->curl, CURLOPT_URL, url);
123 curl_easy_setopt(privctx->curl, CURLOPT_WRITEFUNCTION,
125 curl_easy_setopt(privctx->curl, CURLOPT_WRITEDATA, &buf);
126 res = curl_easy_perform(privctx->curl);
130 dearmor_openpgp_stream(buffer_fetchchar, &buf, &packets);
131 count = parse_keys(packets, publickey);
132 free_packet_list(packets);
135 logthing(LOGTHING_ERROR, "Couldn't find key: %s (%d)",
136 curl_easy_strerror(res), res);
140 buf.offset = buf.size = 0;
147 * hkp_fetch_key_fp - Given a fingerprint fetch the key from HKP server.
149 static int hkp_fetch_key_fp(struct onak_dbctx *dbctx,
150 struct openpgp_fingerprint *fingerprint,
151 struct openpgp_publickey **publickey,
152 __unused bool intrans)
154 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
158 if (fingerprint->length > MAX_FINGERPRINT_LEN) {
162 ofs = snprintf(keyurl, sizeof(keyurl),
163 "%s/lookup?op=get&search=0x", privctx->hkpbase);
165 if ((ofs + fingerprint->length * 2 + 1)> sizeof(keyurl)) {
169 for (i = 0; i < fingerprint->length; i++) {
170 ofs += sprintf(&keyurl[ofs], "%02X", fingerprint->fp[i]);
173 return (hkp_fetch_key_url(dbctx, keyurl, publickey, intrans));
177 * hkp_fetch_key_id - Given a keyid fetch the key from HKP server.
179 static int hkp_fetch_key_id(struct onak_dbctx *dbctx,
181 struct openpgp_publickey **publickey,
184 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
187 snprintf(keyurl, sizeof(keyurl),
188 "%s/lookup?op=get&search=0x%08" PRIX64,
189 privctx->hkpbase, keyid);
191 return (hkp_fetch_key_url(dbctx, keyurl, publickey, intrans));
195 * fetch_key_text - Tries to find the keys that contain the supplied text.
196 * @search: The text to search for.
197 * @publickey: A pointer to a structure to return the key in.
199 * This function searches for the supplied text and returns the keys that
202 * TODO: Write for flat file access. Some sort of grep?
204 static int hkp_fetch_key_text(struct onak_dbctx *dbctx,
206 struct openpgp_publickey **publickey)
208 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
211 snprintf(keyurl, sizeof(keyurl),
212 "%s/lookup?op=get&search=%s",
213 privctx->hkpbase, search);
215 return (hkp_fetch_key_url(dbctx, keyurl, publickey, false));
219 * store_key - Takes a key and stores it.
220 * @publickey: A pointer to the public key to store.
221 * @intrans: If we're already in a transaction.
222 * @update: If true the key exists and should be updated.
225 static int hkp_store_key(struct onak_dbctx *dbctx,
226 struct openpgp_publickey *publickey,
227 __unused bool intrans,
228 __unused bool update)
230 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
231 struct openpgp_packet_list *packets = NULL;
232 struct openpgp_packet_list *list_end = NULL;
235 struct buffer_ctx buf;
240 buf.buffer = malloc(8192);
241 buf.offset = snprintf(buf.buffer, buf.size, "keytextz");
243 flatten_publickey(publickey, &packets, &list_end);
244 armor_openpgp_stream(buffer_putchar, &buf, packets);
245 addform = curl_easy_escape(privctx->curl, buf.buffer, buf.offset);
248 snprintf(keyurl, sizeof(keyurl), "%s/add", privctx->hkpbase);
250 curl_easy_setopt(privctx->curl, CURLOPT_URL, keyurl);
251 curl_easy_setopt(privctx->curl, CURLOPT_POSTFIELDS, addform);
252 curl_easy_setopt(privctx->curl, CURLOPT_WRITEFUNCTION,
255 curl_easy_setopt(privctx->curl, CURLOPT_WRITEDATA, &buf);
256 res = curl_easy_perform(privctx->curl);
259 logthing(LOGTHING_ERROR, "Couldn't send key: %s (%d)",
260 curl_easy_strerror(res), res);
265 /* TODO: buf has any response text we might want to parse. */
267 buf.offset = buf.size = 0;
270 return (res == 0) ? 1 : 0;
274 * delete_key - Given a keyid delete the key from storage.
275 * @fp: The fingerprint of the key to delete.
276 * @intrans: If we're already in a transaction.
280 static int hkp_delete_key(__unused struct onak_dbctx *dbctx,
281 __unused struct openpgp_fingerprint *fp, __unused bool intrans)
287 * iterate_keys - call a function once for each key in the db.
288 * @iterfunc: The function to call.
289 * @ctx: A context pointer
291 * Not applicable for HKP backend.
293 static int hkp_iterate_keys(__unused struct onak_dbctx *dbctx,
294 __unused void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
301 * starttrans - Start a transaction.
303 * This is just a no-op for HKP access.
305 static bool hkp_starttrans(__unused struct onak_dbctx *dbctx)
311 * endtrans - End a transaction.
313 * This is just a no-op for HKP access.
315 static void hkp_endtrans(__unused struct onak_dbctx *dbctx)
321 * Include the basic keydb routines.
323 #define NEED_KEYID2UID 1
324 #define NEED_GETKEYSIGS 1
325 #define NEED_UPDATEKEYS 1
330 * cleanupdb - De-initialize the key database.
332 * We cleanup CURL here.
334 static void hkp_cleanupdb(struct onak_dbctx *dbctx)
336 struct onak_hkp_dbctx *privctx = (struct onak_hkp_dbctx *) dbctx->priv;
339 curl_easy_cleanup(privctx->curl);
340 privctx->curl = NULL;
342 curl_global_cleanup();
348 * initdb - Initialize the key database.
350 * We initialize CURL here.
352 struct onak_dbctx *keydb_hkp_init(struct onak_db_config *dbcfg,
353 __unused bool readonly)
355 struct onak_dbctx *dbctx;
356 struct onak_hkp_dbctx *privctx;
357 curl_version_info_data *curl_info;
359 dbctx = malloc(sizeof(struct onak_dbctx));
364 dbctx->config = dbcfg;
365 dbctx->priv = privctx = malloc(sizeof(*privctx));
366 dbctx->cleanupdb = hkp_cleanupdb;
367 dbctx->starttrans = hkp_starttrans;
368 dbctx->endtrans = hkp_endtrans;
369 dbctx->fetch_key = generic_fetch_key;
370 dbctx->fetch_key_fp = hkp_fetch_key_fp;
371 dbctx->fetch_key_id = hkp_fetch_key_id;
372 dbctx->fetch_key_text = hkp_fetch_key_text;
373 dbctx->store_key = hkp_store_key;
374 dbctx->update_keys = generic_update_keys;
375 dbctx->delete_key = hkp_delete_key;
376 dbctx->getkeysigs = generic_getkeysigs;
377 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
378 dbctx->keyid2uid = generic_keyid2uid;
379 dbctx->iterate_keys = hkp_iterate_keys;
381 if (!hkp_parse_url(privctx, dbcfg->location)) {
384 logthing(LOGTHING_INFO, "Using %s as HKP forwarding URL.",
386 curl_global_init(CURL_GLOBAL_DEFAULT);
387 privctx->curl = curl_easy_init();
388 if (privctx->curl == NULL) {
389 logthing(LOGTHING_CRITICAL, "Could not initialize CURL.");
390 hkp_cleanupdb(dbctx);
394 curl_easy_setopt(privctx->curl, CURLOPT_USERAGENT,
395 "onak/" ONAK_VERSION);
397 if (strncmp(privctx->hkpbase, "https://", 8) == 0) {
398 curl_info = curl_version_info(CURLVERSION_NOW);
399 if (! (curl_info->features & CURL_VERSION_SSL)) {
400 logthing(LOGTHING_CRITICAL,
401 "CURL lacks SSL support; cannot use HKP url: %s",
403 hkp_cleanupdb(dbctx);