From 23f086c85c5d2db35e9ce76cf0bbf72200b4dc42 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Sat, 9 Nov 2013 20:14:26 -0800 Subject: [PATCH] Switch keysubkeys to returning an array of fingerprints instead of IDs 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 | 10 +++++----- decodekey.h | 6 +++--- keydb_db4.c | 26 ++++++++++++++++---------- keydb_fs.c | 22 +++++++++++++--------- keyid.c | 27 ++++++++++++++++++--------- keyid.h | 10 ++++++++++ 6 files changed, 65 insertions(+), 36 deletions(-) diff --git a/decodekey.c b/decodekey.c index aeb7c62..705b827 100644 --- a/decodekey.c +++ b/decodekey.c @@ -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; diff --git a/decodekey.h b/decodekey.h index 8cd5480..8d6306a 100644 --- a/decodekey.h +++ b/decodekey.h @@ -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. diff --git a/keydb_db4.c b/keydb_db4.c index 6a21fab..2ffb256 100644 --- a/keydb_db4.c +++ b/keydb_db4.c @@ -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); diff --git a/keydb_fs.c b/keydb_fs.c index 67bfc62..26eb99e 100644 --- a/keydb_fs.c +++ b/keydb_fs.c @@ -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 6a9b558..546d65d 100644 --- a/keyid.c +++ b/keyid.c @@ -38,6 +38,20 @@ #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 47f7d16..642bd59 100644 --- a/keyid.h +++ b/keyid.h @@ -25,6 +25,16 @@ #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. -- 2.39.5