From e0dd4d10f385cd19da389ec66622eea8aa66ae59 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Tue, 24 Jul 2012 21:44:51 -0700 Subject: [PATCH 1/1] Start pulling non-library material out of core source files As part of moving towards a libonak start pulling things that are related to the onak keyserver out of the core PGP related source files. Start with logthing, our logging framework, instead moving towards an onak_status_t enum to allow up to bubble up errors to the caller. --- armor.c | 6 +----- decodekey.c | 4 ++-- keyd.c | 4 +++- keydb.c | 8 ++++--- keydb_db4.c | 2 +- keydb_dynamic.c | 8 ++++--- keydb_file.c | 4 +++- keydb_fs.c | 3 ++- keydb_keyd.c | 2 +- keyid.c | 55 ++++++++++++++++++++++++------------------------- keyid.h | 14 ++++++++----- keyindex.c | 21 ++++++++++++++----- lookup.c | 2 +- mem.c | 9 ++------ merge.c | 11 ++++++++-- onak.c | 3 ++- parsekey.c | 54 +++++++++++++++++++++--------------------------- parsekey.h | 5 +++-- photoid.c | 15 ++++++-------- photoid.h | 4 ++-- stripkey.c | 3 ++- 21 files changed, 126 insertions(+), 111 deletions(-) diff --git a/armor.c b/armor.c index 230510a..9b61b1a 100644 --- a/armor.c +++ b/armor.c @@ -22,7 +22,6 @@ #include "armor.h" #include "keystructs.h" -#include "log.h" #include "onak-conf.h" #include "parsekey.h" #include "version.h" @@ -56,7 +55,7 @@ static unsigned char encode64(unsigned char c) { } else if (c == 63) { c = '/'; } else { - log_assert(c < 64); + c = '?'; } return c; @@ -165,7 +164,6 @@ static int armor_putchar_int(void *ctx, unsigned char c) unsigned char t; int i; - log_assert(ctx != NULL); state = (struct armor_context *) ctx; switch (state->curoctet++) { @@ -210,7 +208,6 @@ static int armor_putchar(void *ctx, size_t count, void *c) { int i; - log_assert(c != NULL); for (i = 0; i < count; i++) { armor_putchar_int(ctx, ((char *) c)[i]); @@ -269,7 +266,6 @@ static int dearmor_getchar(void *ctx, unsigned char *c) unsigned char tmpc; int i; - log_assert(ctx != NULL); state = (struct dearmor_context *) ctx; *c = 0; diff --git a/decodekey.c b/decodekey.c index 5b2092e..b4f7ceb 100644 --- a/decodekey.c +++ b/decodekey.c @@ -315,13 +315,13 @@ uint64_t *keysubkeys(struct openpgp_publickey *key) struct openpgp_signedpacket_list *cursubkey = NULL; uint64_t *subkeys = NULL; int count = 0; - + if (key != NULL && key->subkeys != NULL) { subkeys = malloc((spsize(key->subkeys) + 1) * sizeof (uint64_t)); cursubkey = key->subkeys; while (cursubkey != NULL) { - subkeys[count++] = get_packetid(cursubkey->packet); + get_packetid(cursubkey->packet, &subkeys[count++]); cursubkey = cursubkey -> next; } subkeys[count] = 0; diff --git a/keyd.c b/keyd.c index 5911ce9..e43115d 100644 --- a/keyd.c +++ b/keyd.c @@ -78,15 +78,17 @@ void iteratefunc(void *ctx, struct openpgp_publickey *key) struct buffer_ctx storebuf; int ret = 0; int *fd = (int *) ctx; + uint64_t keyid; if (key != NULL) { storebuf.offset = 0; storebuf.size = 8192; storebuf.buffer = malloc(8192); + get_keyid(key, &keyid); logthing(LOGTHING_TRACE, "Iterating over 0x%016" PRIX64 ".", - get_keyid(key)); + keyid); flatten_publickey(key, &packets, diff --git a/keydb.c b/keydb.c index 00bb3ff..c257eb5 100644 --- a/keydb.c +++ b/keydb.c @@ -162,7 +162,7 @@ uint64_t generic_getfullkeyid(uint64_t keyid) if (keyid < 0x100000000LL) { config.dbbackend->fetch_key(keyid, &publickey, false); if (publickey != NULL) { - keyid = get_keyid(publickey); + get_keyid(publickey, &keyid); free_publickey(publickey); publickey = NULL; } else { @@ -193,13 +193,15 @@ int generic_update_keys(struct openpgp_publickey **keys, bool sendsync) struct openpgp_publickey *prev = NULL; int newkeys = 0; bool intrans; + uint64_t keyid; for (curkey = *keys; curkey != NULL; curkey = curkey->next) { intrans = config.dbbackend->starttrans(); + get_keyid(curkey, &keyid); logthing(LOGTHING_INFO, "Fetching key 0x%" PRIX64 ", result: %d", - get_keyid(curkey), - config.dbbackend->fetch_key(get_keyid(curkey), &oldkey, + keyid, + config.dbbackend->fetch_key(keyid, &oldkey, intrans)); /* diff --git a/keydb_db4.c b/keydb_db4.c index 802903c..d582eaa 100644 --- a/keydb_db4.c +++ b/keydb_db4.c @@ -1089,7 +1089,7 @@ static int db4_store_key(struct openpgp_publickey *publickey, bool intrans, bool deadlock = false; struct skshash hash; - keyid = get_keyid(publickey); + get_keyid(publickey, &keyid); if (!intrans) { db4_starttrans(); diff --git a/keydb_dynamic.c b/keydb_dynamic.c index 10bec16..6537386 100644 --- a/keydb_dynamic.c +++ b/keydb_dynamic.c @@ -393,7 +393,7 @@ static uint64_t dynamic_getfullkeyid(uint64_t keyid) if (keyid < 0x100000000LL) { dynamic_fetch_key(keyid, &publickey, false); if (publickey != NULL) { - keyid = get_keyid(publickey); + get_keyid(publickey, &keyid); free_publickey(publickey); publickey = NULL; } else { @@ -422,6 +422,7 @@ static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync) struct openpgp_publickey *prev = NULL; int newkeys = 0; bool intrans; + uint64_t keyid; if (loaded_backend == NULL) { load_backend(); @@ -435,10 +436,11 @@ static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync) for (curkey = *keys; curkey != NULL; curkey = curkey->next) { intrans = dynamic_starttrans(); + get_keyid(curkey, &keyid); logthing(LOGTHING_INFO, "Fetching key 0x%" PRIX64 ", result: %d", - get_keyid(curkey), - dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans)); + keyid, + dynamic_fetch_key(keyid, &oldkey, intrans)); /* * If we already have the key stored in the DB then merge it diff --git a/keydb_file.c b/keydb_file.c index e1d0b52..65e71f5 100644 --- a/keydb_file.c +++ b/keydb_file.c @@ -128,9 +128,11 @@ static int file_store_key(struct openpgp_publickey *publickey, bool intrans, struct openpgp_publickey *next = NULL; char keyfile[1024]; int fd = -1; + uint64_t keyid; + get_keyid(publickey, &keyid); snprintf(keyfile, 1023, "%s/0x%" PRIX64, config.db_dir, - get_keyid(publickey) & 0xFFFFFFFF); + keyid & 0xFFFFFFFF); fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK); if (fd > -1) { diff --git a/keydb_fs.c b/keydb_fs.c index ea09c7c..eacf1c6 100644 --- a/keydb_fs.c +++ b/keydb_fs.c @@ -329,13 +329,14 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, struct openpgp_packet_list *packets = NULL; struct openpgp_packet_list *list_end = NULL; struct openpgp_publickey *next = NULL; - uint64_t keyid = get_keyid(publickey); + uint64_t keyid; struct ll *wordlist = NULL, *wl = NULL; struct skshash hash; uint64_t *subkeyids = NULL; uint32_t hashid; int i = 0; + get_keyid(publickey, &keyid); if (!intrans) fs_starttrans(); diff --git a/keydb_keyd.c b/keydb_keyd.c index 0f501fe..26f2f27 100644 --- a/keydb_keyd.c +++ b/keydb_keyd.c @@ -274,7 +274,7 @@ static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans, uint32_t cmd = KEYD_CMD_STORE; uint64_t keyid; - keyid = get_keyid(publickey); + get_keyid(publickey, &keyid); if (update) { keyd_delete_key(keyid, false); diff --git a/keyid.c b/keyid.c index c75f0a1..d114852 100644 --- a/keyid.c +++ b/keyid.c @@ -24,7 +24,7 @@ #include "config.h" #include "keyid.h" #include "keystructs.h" -#include "log.h" +#include "onak.h" #include "parsekey.h" #include "mem.h" #include "merge.h" @@ -42,22 +42,22 @@ * get_keyid - Given a public key returns the keyid. * @publickey: The key to calculate the id for. */ -uint64_t get_keyid(struct openpgp_publickey *publickey) +onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid) { - return (get_packetid(publickey->publickey)); + return (get_packetid(publickey->publickey, keyid)); } /** * get_fingerprint - Given a public key returns the fingerprint. * @publickey: The key to calculate the id for. - * @fingerprint: The fingerprint (must be at least 20 bytes of space). + * @fingerprint: The fingerprint (must be at least 20 bytes of space). * @len: The length of the returned fingerprint. * * This function returns the fingerprint for a given public key. As Type 3 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates * which we've returned. */ -unsigned char *get_fingerprint(struct openpgp_packet *packet, +onak_status_t get_fingerprint(struct openpgp_packet *packet, unsigned char *fingerprint, size_t *len) { @@ -66,8 +66,10 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet, unsigned char c; size_t modlen, explen; - log_assert(fingerprint != NULL); - log_assert(len != NULL); + if (fingerprint == NULL) + return ONAK_E_INVALID_PARAM; + if (len == NULL) + return ONAK_E_INVALID_PARAM; *len = 0; @@ -111,11 +113,10 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet, break; default: - logthing(LOGTHING_ERROR, "Unknown key type: %d", - packet->data[0]); + return ONAK_E_UNKNOWN_VER; } - return fingerprint; + return ONAK_E_OK; } @@ -123,15 +124,15 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet, * get_packetid - Given a PGP packet returns the keyid. * @packet: The packet to calculate the id for. */ -uint64_t get_packetid(struct openpgp_packet *packet) +onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid) { - uint64_t keyid = 0; int offset = 0; int i = 0; size_t length = 0; unsigned char buff[20]; - log_assert(packet != NULL); + if (packet == NULL) + return ONAK_E_INVALID_PARAM; switch (packet->data[0]) { case 2: @@ -145,38 +146,34 @@ uint64_t get_packetid(struct openpgp_packet *packet) packet->data[9]; offset = ((offset + 7) / 8) + 2; - for (keyid = 0, i = 0; i < 8; i++) { - keyid <<= 8; - keyid += packet->data[offset++]; + for (*keyid = 0, i = 0; i < 8; i++) { + *keyid <<= 8; + *keyid += packet->data[offset++]; } /* - * Check for an RSA key; if not then log but accept anyway. + * Check for an RSA key; if not return an error. * 1 == RSA * 2 == RSA Encrypt-Only * 3 == RSA Sign-Only */ if (packet->data[7] < 1 || packet->data[7] > 3) { - logthing(LOGTHING_NOTICE, - "Type 2 or 3 key, but not RSA: %llx (type %d)", - keyid, - packet->data[7]); + return ONAK_E_INVALID_PKT; } break; case 4: get_fingerprint(packet, buff, &length); - for (keyid = 0, i = 12; i < 20; i++) { - keyid <<= 8; - keyid += buff[i]; + for (*keyid = 0, i = 12; i < 20; i++) { + *keyid <<= 8; + *keyid += buff[i]; } break; default: - logthing(LOGTHING_ERROR, "Unknown key type: %d", - packet->data[0]); + return ONAK_E_UNKNOWN_VER; } - return keyid; + return ONAK_E_OK; } static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list @@ -200,7 +197,7 @@ static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list return sorted; } -void get_skshash(struct openpgp_publickey *key, struct skshash *hash) +onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash) { struct openpgp_packet_list *packets = NULL, *list_end = NULL; struct openpgp_packet_list *curpacket; @@ -233,6 +230,8 @@ void get_skshash(struct openpgp_publickey *key, struct skshash *hash) md5_digest(&md5_context, 16, (uint8_t *) &hash->hash); free_packet_list(packets); + + return ONAK_E_OK; } uint8_t hexdigit(char c) diff --git a/keyid.h b/keyid.h index d6e48e6..a0ae4ff 100644 --- a/keyid.h +++ b/keyid.h @@ -23,36 +23,39 @@ #include #include "keystructs.h" +#include "onak.h" /** * get_keyid - Given a public key returns the keyid. * @publickey: The key to calculate the id for. + * @keeyid: The returned keyid * * This function returns the key id for a given public key. */ -uint64_t get_keyid(struct openpgp_publickey *publickey); +onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid); /** * get_fingerprint - Given a public key returns the fingerprint. * @publickey: The key to calculate the id for. - * @fingerprint: The fingerprint (must be at least 20 bytes of space). + * @fingerprint: The fingerprint (must be at least 20 bytes of space). * @len: The length of the returned fingerprint. * * This function returns the fingerprint for a given public key. As Type 3 * fingerprints are 16 bytes and Type 4 are 20 the len field indicates * which we've returned. */ -unsigned char *get_fingerprint(struct openpgp_packet *packet, +onak_status_t get_fingerprint(struct openpgp_packet *packet, unsigned char *fingerprint, size_t *len); /** * get_packetid - Given a PGP packet returns the keyid. * @packet: The packet to calculate the id for. + * @keyid: The returned keyid * * This function returns the key id for a given PGP packet. */ -uint64_t get_packetid(struct openpgp_packet *packet); +onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid); /** * get_skshash - Given a public key returns the SKS hash for it. @@ -64,7 +67,8 @@ uint64_t get_packetid(struct openpgp_packet *packet); * make up the key. The caller should allocate the memory for the * hash. */ -void get_skshash(struct openpgp_publickey *publickey, struct skshash *hash); +onak_status_t get_skshash(struct openpgp_publickey *publickey, + struct skshash *hash); /** * parse_skshash - Parse a string into an SKS hash structure. diff --git a/keyindex.c b/keyindex.c index 8de8b23..46e778c 100644 --- a/keyindex.c +++ b/keyindex.c @@ -32,6 +32,7 @@ #include "keyindex.h" #include "keystructs.h" #include "log.h" +#include "onak.h" #include "onak-conf.h" #include "openpgp.h" @@ -127,6 +128,7 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose, time_t created_time = 0; int type = 0; int length = 0; + uint64_t keyid = 0; while (subkeys != NULL) { if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) { @@ -154,14 +156,17 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose, "Unknown key type: %d", subkeys->packet->data[0]); } - + + if (get_packetid(subkeys->packet, + &keyid) != ONAK_E_OK) { + logthing(LOGTHING_ERROR, "Couldn't get keyid."); + } printf("sub %5d%c/%08X %04d/%02d/%02d\n", length, (type == OPENPGP_PKALGO_RSA) ? 'R' : ((type == OPENPGP_PKALGO_ELGAMAL_ENC) ? 'g' : ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')), - (uint32_t) (get_packetid(subkeys->packet) & - 0xFFFFFFFF), + (uint32_t) (keyid & 0xFFFFFFFF), created->tm_year + 1900, created->tm_mon + 1, created->tm_mday); @@ -275,7 +280,9 @@ int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint, keys->publickey->data[0]); } - keyid = get_keyid(keys); + if (get_keyid(keys, &keyid) != ONAK_E_OK) { + logthing(LOGTHING_ERROR, "Couldn't get keyid."); + } switch (type) { case OPENPGP_PKALGO_RSA: @@ -381,6 +388,7 @@ int mrkey_index(struct openpgp_publickey *keys) size_t fplength = 0; unsigned char fp[20]; int c; + uint64_t keyid; while (keys != NULL) { created_time = (keys->publickey->data[1] << 24) + @@ -393,7 +401,10 @@ int mrkey_index(struct openpgp_publickey *keys) switch (keys->publickey->data[0]) { case 2: case 3: - printf("%016" PRIX64, get_keyid(keys)); + if (get_keyid(keys, &keyid) != ONAK_E_OK) { + logthing(LOGTHING_ERROR, "Couldn't get keyid"); + } + printf("%016" PRIX64, keyid); type = keys->publickey->data[7]; length = (keys->publickey->data[8] << 8) + keys->publickey->data[9]; diff --git a/lookup.c b/lookup.c index 273afc2..9af11d9 100644 --- a/lookup.c +++ b/lookup.c @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) size_t length = 0; if (getphoto(publickey, indx, &photo, - &length)) { + &length) == ONAK_E_OK) { fwrite(photo, 1, length, diff --git a/mem.c b/mem.c index 2f13cf5..2b45158 100644 --- a/mem.c +++ b/mem.c @@ -23,7 +23,6 @@ #include "keystructs.h" #include "ll.h" -#include "log.h" #include "mem.h" /** @@ -38,7 +37,8 @@ struct openpgp_packet *packet_dup(struct openpgp_packet *packet) { struct openpgp_packet *newpacket = NULL; - log_assert(packet != NULL); + if (packet == NULL) + return NULL; newpacket = malloc(sizeof (struct openpgp_packet)); if (newpacket != NULL) { @@ -70,9 +70,6 @@ void packet_list_add(struct openpgp_packet_list **list, struct openpgp_packet_list **list_end, struct openpgp_packet_list *packet_list) { - log_assert(list != NULL); - log_assert(list_end != NULL); - for (; packet_list != NULL; packet_list = packet_list->next) { ADD_PACKET_TO_LIST((*list_end), packet_dup(packet_list->packet)); @@ -92,8 +89,6 @@ void packet_list_add(struct openpgp_packet_list **list, * including the data part. */ void free_packet(struct openpgp_packet *packet) { - log_assert(packet != NULL); - if (packet->data != NULL) { free(packet->data); packet->data = NULL; diff --git a/merge.c b/merge.c index 9dcbf4d..353a844 100644 --- a/merge.c +++ b/merge.c @@ -327,13 +327,20 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b) struct openpgp_packet_list *curpacket = NULL; struct openpgp_packet_list *lastpacket = NULL; struct openpgp_packet_list *nextpacket = NULL; + uint64_t keya, keyb; if (a == NULL || b == NULL) { /* * Do nothing. */ - rc = 1; - } else if (get_keyid(a) != get_keyid(b)) { + return 1; + } + + if (get_keyid(a, &keya) != ONAK_E_OK) { + return 1; + } else if (get_keyid(b, &keyb) != ONAK_E_OK) { + return 1; + } else if (keya != keyb) { /* * Key IDs are different. */ diff --git a/onak.c b/onak.c index fb14df0..6baea3d 100644 --- a/onak.c +++ b/onak.c @@ -330,7 +330,8 @@ int main(int argc, char *argv[]) unsigned char *photo = NULL; size_t length = 0; - if (getphoto(keys, 0, &photo, &length)) { + if (getphoto(keys, 0, &photo, + &length) == ONAK_E_OK) { fwrite(photo, 1, length, diff --git a/parsekey.c b/parsekey.c index 517b029..aedbd14 100644 --- a/parsekey.c +++ b/parsekey.c @@ -25,8 +25,8 @@ #include "keyid.h" #include "keystructs.h" #include "ll.h" -#include "log.h" #include "mem.h" +#include "onak.h" #include "openpgp.h" #include "parsekey.h" @@ -62,7 +62,8 @@ int parse_keys(struct openpgp_packet_list *packets, * It's a signature packet. Add it to either the public * key, to the current UID or the current subkey. */ - log_assert(curkey != NULL); + if (curkey == NULL) + return ONAK_E_INVALID_PARAM; if (curkey->subkeys != NULL) { ADD_PACKET_TO_LIST_END(curkey->last_subkey, sig, @@ -117,8 +118,10 @@ int parse_keys(struct openpgp_packet_list *packets, /* * It's a UID packet (or a photo id, which is similar). */ - log_assert(curkey != NULL); - log_assert(curkey->subkeys == NULL); + if (curkey == NULL) + return ONAK_E_INVALID_PARAM; + if (curkey->subkeys != NULL) + return ONAK_E_INVALID_PARAM; ADD_PACKET_TO_LIST_END(curkey, uid, packet_dup(packets->packet)); @@ -127,7 +130,8 @@ int parse_keys(struct openpgp_packet_list *packets, /* * It's a subkey packet. */ - log_assert(curkey != NULL); + if (curkey == NULL) + return ONAK_E_INVALID_PARAM; ADD_PACKET_TO_LIST_END(curkey, subkey, packet_dup(packets->packet)); @@ -142,9 +146,8 @@ int parse_keys(struct openpgp_packet_list *packets, */ break; default: - logthing(LOGTHING_ERROR, - "Unsupported packet type: %d", - packets->packet->tag); + /* Unsupported packet. Do what? Ignore for now. */ + break; } packets = packets->next; } @@ -181,7 +184,7 @@ int debug_packet(struct openpgp_packet *packet) * packet stream and reads the packets into a linked list of packets * ready for parsing as a public key or whatever. */ -int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, +onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, void *c), void *ctx, struct openpgp_packet_list **packets, @@ -189,11 +192,13 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, { unsigned char curchar = 0; struct openpgp_packet_list *curpacket = NULL; - int rc = 0; + onak_status_t rc = ONAK_E_OK; int keys = 0; bool inpacket = false; - log_assert(packets != NULL); + if (packets == NULL) + return ONAK_E_INVALID_PARAM; + curpacket = *packets; if (curpacket != NULL) { while (curpacket->next != NULL) { @@ -240,9 +245,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, curpacket->packet->length += 192; } else if (curpacket->packet->length > 223 && curpacket->packet->length < 255) { - logthing(LOGTHING_NOTICE, - "Partial length;" - " not supported."); + return ONAK_E_UNSUPPORTED_FEATURE; } else if (curpacket->packet->length == 255) { /* * 5 byte length; ie 255 followed by 3 @@ -288,11 +291,9 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, curpacket->packet->length += curchar; break; case 3: - logthing(LOGTHING_ERROR, - "Unsupported length type 3."); + rc = ONAK_E_UNSUPPORTED_FEATURE; curpacket->packet->length = 0; curpacket->packet->data = NULL; - rc = -1; break; } } @@ -306,10 +307,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, malloc(curpacket->packet->length * sizeof(unsigned char)); if (curpacket->packet->data == NULL) { - logthing(LOGTHING_ERROR, - "Can't allocate memory for " - "packet!"); - rc = -1; + rc = ONAK_E_NOMEM; } else { rc = getchar_func(ctx, curpacket->packet->length, @@ -318,9 +316,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, } inpacket = false; } else { - logthing(LOGTHING_ERROR, "Unexpected character: 0x%X", - curchar); - rc = 1; + rc = ONAK_E_INVALID_PKT; } } @@ -336,7 +332,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, * This function uses putchar_func to write characters to an OpenPGP * packet stream from a linked list of packets. */ -int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, +onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, struct openpgp_packet_list *packets) @@ -364,8 +360,6 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, putchar_func(ctx, 1, &curchar); } else if (packets->packet->length > 8382 && packets->packet->length < 0xFFFFFFFF) { - logthing(LOGTHING_DEBUG, - "Writing 5 byte length"); curchar = 255; putchar_func(ctx, 1, &curchar); @@ -385,8 +379,7 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, curchar &= 0xFF; putchar_func(ctx, 1, &curchar); } else { - logthing(LOGTHING_ERROR, - "Unsupported new format length."); + return ONAK_E_UNSUPPORTED_FEATURE; } } else { curchar |= (packets->packet->tag << 2); @@ -419,7 +412,8 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, packets->packet->data); packets = packets->next; } - return 0; + + return ONAK_E_OK; } /** diff --git a/parsekey.h b/parsekey.h index e9bea41..bc208d2 100644 --- a/parsekey.h +++ b/parsekey.h @@ -21,6 +21,7 @@ #define __PARSEKEY_H__ #include "keystructs.h" +#include "onak.h" /** * parse_keys - Process a stream of packets for public keys + sigs. @@ -59,7 +60,7 @@ int debug_packet(struct openpgp_packet *packet); * then only the public key component of the last key will be returned, * none of the other packets of the key will be read. */ -int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, +onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, void *c), void *ctx, struct openpgp_packet_list **packets, @@ -74,7 +75,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, * This function uses putchar_func to write characters to an OpenPGP * packet stream from a linked list of packets. */ -int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, +onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, struct openpgp_packet_list *packets); diff --git a/photoid.c b/photoid.c index febc3f5..2501f4a 100644 --- a/photoid.c +++ b/photoid.c @@ -25,7 +25,7 @@ #include "keyid.h" #include "keyindex.h" #include "keystructs.h" -#include "log.h" +#include "onak.h" #include "photoid.h" /** @@ -40,16 +40,15 @@ * photo id NULL is returned. The returned data pointer refers to the key * data supplied rather than a copy of it. */ -int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo, - size_t *length) +onak_status_t getphoto(struct openpgp_publickey *key, int index, + unsigned char **photo, size_t *length) { struct openpgp_signedpacket_list *curuid = NULL; int i = 0; int j = 0; - log_assert(key != NULL); - log_assert(photo != NULL); - log_assert(length != NULL); + if (key == NULL || photo == NULL || length == NULL) + return ONAK_E_INVALID_PARAM; *photo = NULL; @@ -76,8 +75,6 @@ int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo, *length <<= 8; *length += curuid->packet->data[j++]; } - logthing(LOGTHING_DEBUG, "Got photo, size %d", - *length); j++; *length -= 17; *photo = &(curuid->packet->data[j+16]); @@ -88,5 +85,5 @@ int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo, curuid = curuid->next; } - return (*photo != NULL); + return *photo == NULL ? ONAK_E_NOT_FOUND : ONAK_E_OK; } diff --git a/photoid.h b/photoid.h index aaafe34..0e0373b 100644 --- a/photoid.h +++ b/photoid.h @@ -34,7 +34,7 @@ * photo id NULL is returned. The returned data pointer refers to the key * data supplied rather than a copy of it. */ -int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo, - size_t *length); +onak_status_t getphoto(struct openpgp_publickey *key, int index, + unsigned char **photo, size_t *length); #endif /* __PHOTOID_H__ */ diff --git a/stripkey.c b/stripkey.c index 13aeb1d..e0fb5e3 100644 --- a/stripkey.c +++ b/stripkey.c @@ -63,7 +63,8 @@ int main(int argc, char** argv) { cleankeys( keys ); /* Iterate over the keys... */ for( key = keys; key; key = key->next ) { - uint64_t keyid = get_keyid( key ); + uint64_t keyid; + get_keyid( key, &keyid ); for( uid = key->uids; uid; uid = uid->next ) { REPEATTHISUID: for( sig = uid->sigs, prevsig = NULL; -- 2.39.5