]> the.earth.li Git - onak.git/commitdiff
Switch keysubkeys to returning an array of fingerprints instead of IDs
authorJonathan McDowell <noodles@earth.li>
Sun, 10 Nov 2013 04:14:26 +0000 (20:14 -0800)
committerJonathan McDowell <noodles@earth.li>
Sun, 10 Nov 2013 04:14:26 +0000 (20:14 -0800)
No functional change at present because the users only currently use the
64 bit key ID, so add a helper function fingerprint2keyid as well (which
only works for v4 keys, but v3 keys can't have subkeys).

decodekey.c
decodekey.h
keydb_db4.c
keydb_fs.c
keyid.c
keyid.h

index aeb7c623c516a959a277bf0b7177792fb5e472b8..705b827d43ef7a7e69197a9a4dd9af537e81d542 100644 (file)
@@ -310,21 +310,21 @@ char **keyuids(struct openpgp_publickey *key, char **primary)
  *     keysubkeys takes a public key structure and returns an array of the
  *     subkey keyids for that key.
  */
-uint64_t *keysubkeys(struct openpgp_publickey *key)
+struct openpgp_fingerprint *keysubkeys(struct openpgp_publickey *key)
 {
        struct openpgp_signedpacket_list *cursubkey = NULL;
-       uint64_t                         *subkeys = NULL;
+       struct openpgp_fingerprint       *subkeys = NULL;
        int                               count = 0;
 
        if (key != NULL && key->subkeys != NULL) {
                subkeys = malloc((spsize(key->subkeys) + 1) *
-                               sizeof (uint64_t));
+                               sizeof (struct openpgp_fingerprint));
                cursubkey = key->subkeys;
                while (cursubkey != NULL) {
-                       get_packetid(cursubkey->packet, &subkeys[count++]);
+                       get_fingerprint(cursubkey->packet, &subkeys[count++]);
                        cursubkey = cursubkey -> next;
                }
-               subkeys[count] = 0;
+               subkeys[count].length = 0;
        }
 
        return subkeys;
index 8cd5480027fa39ba21ab2aa6bf9ce79e65391c21..8d6306aeb2ac144086fb960a0122a23dcd87b05e 100644 (file)
@@ -68,13 +68,13 @@ uint64_t sig_keyid(struct openpgp_packet *packet);
 char **keyuids(struct openpgp_publickey *key, char **primary);
 
 /**
- *     keysubkeys - Takes a key and returns an array of its subkey keyids.
+ *     keysubkeys - Takes a key & returns an array of its subkey fingerprints
  *     @key: The key to get the subkeys of.
  *
  *     keysubkeys takes a public key structure and returns an array of the
- *     subkey keyids for that key.
+ *     subkey fingerprints for that key.
  */
-uint64_t *keysubkeys(struct openpgp_publickey *key);
+struct openpgp_fingerprint *keysubkeys(struct openpgp_publickey *key);
 
 /**
  *     parse_subpackets - Parse the subpackets of a Type 4 signature.
index 6a21fab73678a811a286eeff35a6d3d7e97f2351..2ffb256c4c350ea4b77e941ba4b162b3e77e9f87 100644 (file)
@@ -587,7 +587,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
        DBT key, data;
        DBC *cursor = NULL;
        uint32_t   shortkeyid = 0;
-       uint64_t  *subkeyids = NULL;
+       struct openpgp_fingerprint *subkeyids = NULL;
        int ret = 0;
        int i;
        char **uids = NULL;
@@ -597,6 +597,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
        struct ll *curword  = NULL;
        bool deadlock = false;
        struct skshash hash;
+       uint64_t subkeyid;
 
        if (!intrans) {
                db4_starttrans(dbctx);
@@ -758,10 +759,11 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
 
                subkeyids = keysubkeys(publickey);
                i = 0;
-               while (subkeyids != NULL && subkeyids[i] != 0) {
+               while (subkeyids != NULL && subkeyids[i].length != 0) {
+                       subkeyid = fingerprint2keyid(&subkeyids[i]);
                        memset(&key, 0, sizeof(key));
-                       key.data = &subkeyids[i];
-                       key.size = sizeof(subkeyids[i]);
+                       key.data = &subkeyid;
+                       key.size = sizeof(subkeyid);
                        privctx->subkeydb->del(privctx->subkeydb,
                                        privctx->txn, &key, 0);
                        if (ret != 0) {
@@ -775,7 +777,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                                }
                        }
 
-                       shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
+                       shortkeyid = subkeyid & 0xFFFFFFFF;
 
                        memset(&key, 0, sizeof(key));
                        memset(&data, 0, sizeof(data));
@@ -803,6 +805,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                                        deadlock = true;
                                }
                        }
+                       i++;
                }
                if (subkeyids != NULL) {
                        free(subkeyids);
@@ -856,7 +859,7 @@ static int db4_store_key(struct onak_dbctx *dbctx,
        DBT        data;
        uint64_t   keyid = 0;
        uint32_t   shortkeyid = 0;
-       uint64_t  *subkeyids = NULL;
+       struct openpgp_fingerprint *subkeyids = NULL;
        char     **uids = NULL;
        char      *primary = NULL;
        unsigned char worddb_data[12];
@@ -864,6 +867,7 @@ static int db4_store_key(struct onak_dbctx *dbctx,
        struct ll *curword  = NULL;
        bool       deadlock = false;
        struct skshash hash;
+       uint64_t subkeyid;
 
        if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
                logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
@@ -1030,12 +1034,13 @@ static int db4_store_key(struct onak_dbctx *dbctx,
        if (!deadlock) {
                subkeyids = keysubkeys(publickey);
                i = 0;
-               while (subkeyids != NULL && subkeyids[i] != 0) {
+               while (subkeyids != NULL && subkeyids[i].length != 0) {
+                       subkeyid = fingerprint2keyid(&subkeyids[i]);
                        /* Store the subkey ID -> main key ID mapping */
                        memset(&key, 0, sizeof(key));
                        memset(&data, 0, sizeof(data));
-                       key.data = &subkeyids[i];
-                       key.size = sizeof(subkeyids[i]);
+                       key.data = &subkeyid;
+                       key.size = sizeof(subkeyid);
                        data.data = &keyid;
                        data.size = sizeof(keyid);
 
@@ -1054,7 +1059,7 @@ static int db4_store_key(struct onak_dbctx *dbctx,
                        }
 
                        /* Store the short subkey ID -> main key ID mapping */
-                       shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
+                       shortkeyid = subkeyid & 0xFFFFFFFF;
 
                        memset(&key, 0, sizeof(key));
                        memset(&data, 0, sizeof(data));
@@ -1076,6 +1081,7 @@ static int db4_store_key(struct onak_dbctx *dbctx,
                                        deadlock = true;
                                }
                        }
+                       i++;
                }
                if (subkeyids != NULL) {
                        free(subkeyids);
index 67bfc62f58c87890ddffd5a3758661cadb7ed223..26eb99e70ea98d70dd7a76becd6fca719078d016 100644 (file)
@@ -300,7 +300,7 @@ static int fs_store_key(struct onak_dbctx *dbctx,
        uint64_t keyid;
        struct ll *wordlist = NULL, *wl = NULL;
        struct skshash hash;
-       uint64_t *subkeyids = NULL;
+       struct openpgp_fingerprint *subkeyids = NULL;
        uint32_t hashid;
        int i = 0;
 
@@ -348,12 +348,14 @@ static int fs_store_key(struct onak_dbctx *dbctx,
                
                subkeyids = keysubkeys(publickey);
                i = 0;
-               while (subkeyids != NULL && subkeyids[i] != 0) {
-                       prove_path_to(subkeyids[i], "subkeys");
+               while (subkeyids != NULL && subkeyids[i].length != 0) {
+                       keyid = fingerprint2keyid(&subkeyids[i]);
 
-                       subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
+                       prove_path_to(keyid, "subkeys");
+
+                       subkeydir(wbuffer, sizeof(wbuffer), keyid);
                        mkdir(wbuffer, 0777);
-                       subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i]);
+                       subkeypath(wbuffer, sizeof(wbuffer), keyid);
                        link(buffer, wbuffer);
 
                        i++;
@@ -388,7 +390,8 @@ static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
        struct openpgp_publickey *pk = NULL;
        struct skshash hash;
        struct ll *wordlist = NULL, *wl = NULL;
-       uint64_t *subkeyids = NULL;
+       struct openpgp_fingerprint *subkeyids = NULL;
+       uint64_t subkeyid;
        int i = 0;
 
        if ((keyid >> 32) == 0)
@@ -418,10 +421,11 @@ static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
 
                subkeyids = keysubkeys(pk);
                i = 0;
-               while (subkeyids != NULL && subkeyids[i] != 0) {
-                       prove_path_to(subkeyids[i], "subkeys");
+               while (subkeyids != NULL && subkeyids[i].length != 0) {
+                       subkeyid = fingerprint2keyid(&subkeyids[i]);
+                       prove_path_to(subkeyid, "subkeys");
 
-                       subkeypath(buffer, sizeof(buffer), subkeyids[i]);
+                       subkeypath(buffer, sizeof(buffer), subkeyid);
                        unlink(buffer);
 
                        i++;
diff --git a/keyid.c b/keyid.c
index 6a9b5580c34fa1c6a9df2100f565fd117ce382dc..546d65dba175336210818a89d2944982da3e3a71 100644 (file)
--- a/keyid.c
+++ b/keyid.c
 #include "sha1.h"
 #endif
 
+uint64_t fingerprint2keyid(struct openpgp_fingerprint *fingerprint)
+{
+       uint64_t keyid;
+       int i;
+
+       keyid = 0;
+       for (keyid = 0, i = 12; i < 20; i++) {
+               keyid <<= 8;
+               keyid += fingerprint->fp[i];
+       }
+
+       return keyid;
+}
+
 
 /**
  *     get_keyid - Given a public key returns the keyid.
@@ -159,11 +173,9 @@ onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
                        ripemd160_digest(&ripemd160_context,
                                RIPEMD160_DIGEST_SIZE,
                                fingerprint.fp);
+                       fingerprint.length = RIPEMD160_DIGEST_SIZE;
 
-                       for (*keyid = 0, i = 12; i < 20; i++) {
-                               *keyid <<= 8;
-                               *keyid += fingerprint.fp[i];
-                       }
+                       *keyid = fingerprint2keyid(&fingerprint);
 
                        return ONAK_E_OK;
                }
@@ -194,11 +206,8 @@ onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
                break;
        case 4:
                get_fingerprint(packet, &fingerprint);
-               
-               for (*keyid = 0, i = 12; i < 20; i++) {
-                       *keyid <<= 8;
-                       *keyid += fingerprint.fp[i];
-               }
+
+               *keyid = fingerprint2keyid(&fingerprint);
 
                break;
        default:
diff --git a/keyid.h b/keyid.h
index 47f7d163beabbe8f2d45d3c1ecedc7e8884dd937..642bd59079b9cdda7a8252f976151c363422f736 100644 (file)
--- a/keyid.h
+++ b/keyid.h
 #include "keystructs.h"
 #include "onak.h"
 
+/**
+ *     fingerprint2keyid - convert a fingerprint to a keyid
+ *     @fingerprint: The fingerprint structure to convert
+ *     @returns: 64 bit keyid
+ *
+ *     This function returns the key id for a given fingerprint. Currently
+ *     only works for v4 fingerprints.
+ */
+uint64_t fingerprint2keyid(struct openpgp_fingerprint *fingerprint);
+
 /**
  *     get_keyid - Given a public key returns the keyid.
  *     @publickey: The key to calculate the id for.