From: Jonathan McDowell Date: Sun, 1 May 2011 00:49:57 +0000 (-0700) Subject: Define OpenPGP constants and use them rather than magic numbers X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/0211c730256c9bbaf10a569c88f7df0ef882311e Define OpenPGP constants and use them rather than magic numbers Take the set of OpenPGP constants from RFC4880 that we're using and produce some #defines, to try and make the code a bit clearer. --- diff --git a/decodekey.c b/decodekey.c index 606b091..7dd7992 100644 --- a/decodekey.c +++ b/decodekey.c @@ -16,6 +16,7 @@ #include "keystructs.h" #include "ll.h" #include "log.h" +#include "openpgp.h" /* * parse_subpackets - Parse the subpackets of a Type 4 signature. @@ -54,7 +55,7 @@ int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation) packetlen = data[offset++]; } switch (data[offset] & 0x7F) { - case 2: + case OPENPGP_SIGSUB_CREATION: /* * Signature creation time. */ @@ -68,17 +69,17 @@ int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation) *creation = data[offset + packetlen - 1]; } break; - case 3: + case OPENPGP_SIGSUB_EXPIRY: /* * Signature expiration time. Might want to output this? */ break; - case 6: + case OPENPGP_SIGSUB_REGEX: /* * Regular expression for UIDs this sig is over. */ break; - case 16: + case OPENPGP_SIGSUB_ISSUER: if (keyid != NULL) { *keyid = data[offset+packetlen - 8]; *keyid <<= 8; @@ -97,23 +98,23 @@ int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation) *keyid += data[offset+packetlen - 1]; } break; - case 20: + case OPENPGP_SIGSUB_NOTATION: /* * Annotation data. */ break; - case 23: + case OPENPGP_SIGSUB_KEYSERVER: /* * Key server preferences. Including no-modify. */ break; - case 25: + case OPENPGP_SIGSUB_PRIMARYUID: /* * Primary UID. */ break; - case 26: + case OPENPGP_SIGSUB_POLICYURI: /* * Policy URI. */ @@ -276,7 +277,7 @@ char **keyuids(struct openpgp_publickey *key, char **primary) curuid = key->uids; while (curuid != NULL) { buf[0] = 0; - if (curuid->packet->tag == 13) { + if (curuid->packet->tag == OPENPGP_PACKET_UID) { snprintf(buf, 1023, "%.*s", (int) curuid->packet->length, curuid->packet->data); diff --git a/keydb.c b/keydb.c index 4dc9332..2abfa02 100644 --- a/keydb.c +++ b/keydb.c @@ -23,6 +23,7 @@ #include "keystructs.h" #include "mem.h" #include "merge.h" +#include "openpgp.h" #include "parsekey.h" #include "sendsync.h" @@ -42,7 +43,7 @@ char *generic_keyid2uid(uint64_t keyid) publickey != NULL) { curuid = publickey->uids; while (curuid != NULL && buf[0] == 0) { - if (curuid->packet->tag == 13) { + if (curuid->packet->tag == OPENPGP_PACKET_UID) { snprintf(buf, 1023, "%.*s", (int) curuid->packet->length, curuid->packet->data); diff --git a/keydb_dynamic.c b/keydb_dynamic.c index 5c2d3d5..ae2b805 100644 --- a/keydb_dynamic.c +++ b/keydb_dynamic.c @@ -19,6 +19,7 @@ #include "mem.h" #include "merge.h" #include "onak-conf.h" +#include "openpgp.h" #include "parsekey.h" #include "sendsync.h" @@ -255,7 +256,7 @@ static char *dynamic_keyid2uid(uint64_t keyid) if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) { curuid = publickey->uids; while (curuid != NULL && buf[0] == 0) { - if (curuid->packet->tag == 13) { + if (curuid->packet->tag == OPENPGP_PACKET_UID) { snprintf(buf, 1023, "%.*s", (int) curuid->packet->length, curuid->packet->data); diff --git a/keyindex.c b/keyindex.c index 869befd..6aab5d3 100644 --- a/keyindex.c +++ b/keyindex.c @@ -20,6 +20,7 @@ #include "keystructs.h" #include "log.h" #include "onak-conf.h" +#include "openpgp.h" int list_sigs(struct openpgp_packet_list *sigs, bool html) { @@ -78,13 +79,13 @@ int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids, int imgindx = 0; while (uids != NULL) { - if (uids->packet->tag == 13) { + if (uids->packet->tag == OPENPGP_PACKET_UID) { snprintf(buf, 1023, "%.*s", (int) uids->packet->length, uids->packet->data); printf(" %s\n", (html) ? txt2html(buf) : buf); - } else if (uids->packet->tag == 17) { + } else if (uids->packet->tag == OPENPGP_PACKET_UAT) { printf(" "); if (html) { printf("packet->tag == 14) { + if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) { created_time = (subkeys->packet->data[1] << 24) + (subkeys->packet->data[2] << 16) + @@ -143,8 +144,9 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose, printf("sub %5d%c/%08X %04d/%02d/%02d\n", length, - (type == 1) ? 'R' : ((type == 16) ? 'g' : - ((type == 17) ? 'D' : '?')), + (type == OPENPGP_PKALGO_RSA) ? 'R' : + ((type == OPENPGP_PKALGO_ELGAMAL) ? 'g' : + ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')), (uint32_t) (get_packetid(subkeys->packet) & 0xFFFFFFFF), created->tm_year + 1900, @@ -262,16 +264,16 @@ int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint, keyid = get_keyid(keys); switch (type) { - case 1: + case OPENPGP_PKALGO_RSA: typech = 'R'; break; - case 16: + case OPENPGP_PKALGO_ELGAMAL: typech = 'g'; break; - case 17: + case OPENPGP_PKALGO_DSA: typech = 'D'; break; - case 20: + case OPENPGP_PKALGO_ELGAMAL_SIGN: typech = 'G'; break; default: @@ -301,7 +303,8 @@ int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint, } curuid = keys->uids; - if (curuid != NULL && curuid->packet->tag == 13) { + if (curuid != NULL && + curuid->packet->tag == OPENPGP_PACKET_UID) { snprintf(buf, 1023, "%.*s", (int) curuid->packet->length, curuid->packet->data); @@ -406,7 +409,7 @@ int mrkey_index(struct openpgp_publickey *keys) for (curuid = keys->uids; curuid != NULL; curuid = curuid->next) { - if (curuid->packet->tag == 13) { + if (curuid->packet->tag == OPENPGP_PACKET_UID) { printf("uid:"); for (i = 0; i < (int) curuid->packet->length; i++) { diff --git a/openpgp.h b/openpgp.h new file mode 100644 index 0000000..7ccf133 --- /dev/null +++ b/openpgp.h @@ -0,0 +1,45 @@ +/* + * openpgp.h - Defines directly related to OpenPGP RFC 4880 + * + * Copyright 2011 Jonathan McDowell + */ + +#ifndef __OPENPGP_H__ +#define __OPENPGP_H__ + +#define OPENPGP_PKALGO_RSA 1 +#define OPENPGP_PKALGO_ELGAMAL 16 +#define OPENPGP_PKALGO_DSA 17 +#define OPENPGP_PKALGO_ELGAMAL_SIGN 20 + +#define OPENPGP_HASH_MD5 1 +#define OPENPGP_HASH_SHA1 2 +#define OPENPGP_HASH_RIPEMD160 3 +#define OPENPGP_HASH_SHA256 8 +#define OPENPGP_HASH_SHA384 9 +#define OPENPGP_HASH_SHA512 10 +#define OPENPGP_HASH_SHA224 11 + +#define OPENPGP_PACKET_SIGNATURE 2 +#define OPENPGP_PACKET_PUBLICKEY 6 +#define OPENPGP_PACKET_TRUST 12 +#define OPENPGP_PACKET_UID 13 +#define OPENPGP_PACKET_PUBLICSUBKEY 14 +#define OPENPGP_PACKET_UAT 17 + +#define OPENPGP_SIGTYPE_BINARY 0x00 +#define OPENPGP_SIGTYPE_TEXT 0x01 +#define OPENPGP_SIGTYPE_KEY_REV 0x20 +#define OPENPGP_SIGTYPE_SUBKEY_REV 0x28 +#define OPENPGP_SIGTYPE_CERT_REV 0x30 + +#define OPENPGP_SIGSUB_CREATION 2 +#define OPENPGP_SIGSUB_EXPIRY 3 +#define OPENPGP_SIGSUB_REGEX 6 +#define OPENPGP_SIGSUB_ISSUER 16 +#define OPENPGP_SIGSUB_NOTATION 20 +#define OPENPGP_SIGSUB_KEYSERVER 23 +#define OPENPGP_SIGSUB_PRIMARYUID 25 +#define OPENPGP_SIGSUB_POLICYURI 26 + +#endif /* __OPENPGP_H__ */ diff --git a/parsekey.c b/parsekey.c index 2d95106..349d5c8 100644 --- a/parsekey.c +++ b/parsekey.c @@ -16,6 +16,7 @@ #include "ll.h" #include "log.h" #include "mem.h" +#include "openpgp.h" #include "parsekey.h" /** @@ -45,7 +46,7 @@ int parse_keys(struct openpgp_packet_list *packets, while (packets != NULL) { switch (packets->packet->tag) { - case 2: + case OPENPGP_PACKET_SIGNATURE: /* * It's a signature packet. Add it to either the public * key, to the current UID or the current subkey. @@ -68,13 +69,15 @@ int parse_keys(struct openpgp_packet_list *packets, * if it's a revocation. */ if (packets->packet->data[0] == 3 && - packets->packet->data[2] == 0x20) { + packets->packet->data[2] == + OPENPGP_SIGTYPE_KEY_REV) { /* * Type 3 key, 0x20 == revocation */ curkey->revoked = true; } else if (packets->packet->data[0] == 4 && - packets->packet->data[1] == 0x20) { + packets->packet->data[1] == + OPENPGP_SIGTYPE_KEY_REV) { /* * Type 4 key, 0x20 == revocation */ @@ -82,7 +85,7 @@ int parse_keys(struct openpgp_packet_list *packets, } } break; - case 6: + case OPENPGP_PACKET_PUBLICKEY: /* * It's a public key packet, so start a new key in our * list. @@ -98,8 +101,8 @@ int parse_keys(struct openpgp_packet_list *packets, curkey->publickey = packet_dup(packets->packet); count++; break; - case 13: - case 17: + case OPENPGP_PACKET_UID: + case OPENPGP_PACKET_UAT: /* * It's a UID packet (or a photo id, which is similar). */ @@ -109,7 +112,7 @@ int parse_keys(struct openpgp_packet_list *packets, uid, packet_dup(packets->packet)); break; - case 14: + case OPENPGP_PACKET_PUBLICSUBKEY: /* * It's a subkey packet. */ @@ -118,7 +121,7 @@ int parse_keys(struct openpgp_packet_list *packets, subkey, packet_dup(packets->packet)); break; - case 12: + case OPENPGP_PACKET_TRUST: case 61: /* * One of: @@ -284,7 +287,8 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, } if (rc == 0) { - if (curpacket->packet->tag == 6) { + if (curpacket->packet->tag == + OPENPGP_PACKET_PUBLICKEY) { keys++; } curpacket->packet->data = diff --git a/splitkeys.c b/splitkeys.c index 73a94cd..86a8363 100644 --- a/splitkeys.c +++ b/splitkeys.c @@ -17,6 +17,7 @@ #include "log.h" #include "mem.h" #include "onak-conf.h" +#include "openpgp.h" #include "parsekey.h" int main(int argc, char *argv[]) @@ -52,7 +53,8 @@ int main(int argc, char *argv[]) tmp = list_end; list_end = list_end->next; if (list_end->next == NULL && - list_end->packet->tag == 6) { + list_end->packet->tag == + OPENPGP_PACKET_PUBLICKEY) { tmp->next = NULL; } }