]> the.earth.li Git - onak.git/blobdiff - keydb_keyd.c
Change SHA_DIGEST_LENGTH to SHA1_DIGEST_SIZE to match nettle
[onak.git] / keydb_keyd.c
index 2a9faafa40cc72c9a21d0fcb698819e94a6c5345..7995e38a8e757ddb7a1d685432ae54d7b432aa0e 100644 (file)
@@ -1,9 +1,20 @@
 /*
  * keydb_keyd.c - Routines to talk to keyd backend.
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002-2004,2011 Jonathan McDowell <noodles@earth.li>
  *
- * Copyright 2004 Project Purple
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include <errno.h>
@@ -176,12 +187,13 @@ static void keyd_endtrans(void)
  *
  *      TODO: What about keyid collisions? Should we use fingerprint instead?
  */
-static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int keyd_fetch_key_id(uint64_t keyid,
+               struct openpgp_publickey **publickey,
                bool intrans)
 {
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GET;
+       uint32_t                    cmd = KEYD_CMD_GET_ID;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
@@ -214,7 +226,57 @@ static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                        keybuf.size = 0;
                }
        }
-       
+
+       return (count > 0) ? 1 : 0;
+}
+
+static int keyd_fetch_key_fp(uint8_t *fp, size_t fpsize,
+               struct openpgp_publickey **publickey,
+               bool intrans)
+{
+       struct buffer_ctx           keybuf;
+       struct openpgp_packet_list *packets = NULL;
+       uint32_t                    cmd = KEYD_CMD_GET_FP;
+       ssize_t                     bytes = 0;
+       ssize_t                     count = 0;
+       uint8_t                     size;
+
+       if (fpsize > MAX_FINGERPRINT_LEN) {
+               return 0;
+       }
+
+       write(keyd_fd, &cmd, sizeof(cmd));
+       read(keyd_fd, &cmd, sizeof(cmd));
+       if (cmd == KEYD_REPLY_OK) {
+               size = fpsize;
+               write(keyd_fd, &size, sizeof(size));
+               write(keyd_fd, fp, size);
+               keybuf.offset = 0;
+               read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
+               if (keybuf.size > 0) {
+                       keybuf.buffer = malloc(keybuf.size);
+                       bytes = count = 0;
+                       logthing(LOGTHING_TRACE,
+                                       "Getting %d bytes of key data.",
+                                       keybuf.size);
+                       while (bytes >= 0 && count < keybuf.size) {
+                               bytes = read(keyd_fd, &keybuf.buffer[count],
+                                               keybuf.size - count);
+                               logthing(LOGTHING_TRACE,
+                                               "Read %d bytes.", bytes);
+                               count += bytes;
+                       }
+                       read_openpgp_stream(buffer_fetchchar, &keybuf,
+                                       &packets, 0);
+                       parse_keys(packets, publickey);
+                       free_packet_list(packets);
+                       packets = NULL;
+                       free(keybuf.buffer);
+                       keybuf.buffer = NULL;
+                       keybuf.size = 0;
+               }
+       }
+
        return (count > 0) ? 1 : 0;
 }
 
@@ -263,7 +325,10 @@ static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans,
        uint32_t                    cmd = KEYD_CMD_STORE;
        uint64_t                    keyid;
 
-       keyid = get_keyid(publickey);
+       if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
+               logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
+               return 0;
+       }
        
        if (update) {
                keyd_delete_key(keyid, false);
@@ -311,7 +376,7 @@ static int keyd_fetch_key_text(const char *search,
 {
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GETTEXT;
+       uint32_t                    cmd = KEYD_CMD_GET_TEXT;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
@@ -352,6 +417,49 @@ static int keyd_fetch_key_text(const char *search,
        return 0;
 }
 
+static int keyd_fetch_key_skshash(const struct skshash *hash,
+               struct openpgp_publickey **publickey)
+{
+       struct buffer_ctx           keybuf;
+       struct openpgp_packet_list *packets = NULL;
+       uint32_t                    cmd = KEYD_CMD_GET_SKSHASH;
+       ssize_t                     bytes = 0;
+       ssize_t                     count = 0;
+
+       write(keyd_fd, &cmd, sizeof(cmd));
+       read(keyd_fd, &cmd, sizeof(cmd));
+       if (cmd == KEYD_REPLY_OK) {
+               write(keyd_fd, hash->hash, sizeof(hash->hash));
+               keybuf.offset = 0;
+               read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
+               if (keybuf.size > 0) {
+                       keybuf.buffer = malloc(keybuf.size);
+                       bytes = count = 0;
+                       logthing(LOGTHING_TRACE,
+                                       "Getting %d bytes of key data.",
+                                       keybuf.size);
+                       while (bytes >= 0 && count < keybuf.size) {
+                               bytes = read(keyd_fd, &keybuf.buffer[count],
+                                               keybuf.size - count);
+                               logthing(LOGTHING_TRACE,
+                                               "Read %d bytes.", bytes);
+                               count += bytes;
+                       }
+                       read_openpgp_stream(buffer_fetchchar, &keybuf,
+                                       &packets, 0);
+                       parse_keys(packets, publickey);
+                       free_packet_list(packets);
+                       packets = NULL;
+                       free(keybuf.buffer);
+                       keybuf.buffer = NULL;
+                       keybuf.size = 0;
+               }
+       }
+       
+       return (count > 0) ? 1 : 0;
+}
+
+
 /**
  *     getfullkeyid - Maps a 32bit key id to a 64bit one.
  *     @keyid: The 32bit keyid.
@@ -452,8 +560,10 @@ struct dbfuncs keydb_keyd_funcs = {
        .cleanupdb              = keyd_cleanupdb,
        .starttrans             = keyd_starttrans,
        .endtrans               = keyd_endtrans,
-       .fetch_key              = keyd_fetch_key,
+       .fetch_key_id           = keyd_fetch_key_id,
+       .fetch_key_fp           = keyd_fetch_key_fp,
        .fetch_key_text         = keyd_fetch_key_text,
+       .fetch_key_skshash      = keyd_fetch_key_skshash,
        .store_key              = keyd_store_key,
        .update_keys            = generic_update_keys,
        .delete_key             = keyd_delete_key,