]> the.earth.li Git - onak.git/commitdiff
Define OpenPGP constants and use them rather than magic numbers
authorJonathan McDowell <noodles@earth.li>
Sun, 1 May 2011 00:49:57 +0000 (17:49 -0700)
committerJonathan McDowell <noodles@earth.li>
Sun, 1 May 2011 00:49:57 +0000 (17:49 -0700)
  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.

decodekey.c
keydb.c
keydb_dynamic.c
keyindex.c
openpgp.h [new file with mode: 0644]
parsekey.c
splitkeys.c

index 606b091f941237ec173cedf64b10b0c6aa1eb29f..7dd79927533d855bfbfb68ba6e309e0fb545d078 100644 (file)
@@ -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 4dc9332ad3690bbee67c1dfa6b7f39ada55f7910..2abfa02bae51ae8a9d8d7b3f587844b66c4b29ca 100644 (file)
--- 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);
index 5c2d3d58f5484e0941026aad01769037789532bc..ae2b8056870dba7a2a27c75c37ac5193fd275490 100644 (file)
@@ -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);
index 869befd0138090c0a2a246c2d3e9b8e15cf3bf49..6aab5d3c73da891faeebe6d3262d3420a464fead 100644 (file)
@@ -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("<img src=\"lookup?op=photo&search="
@@ -115,7 +116,7 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
        int             length = 0;
 
        while (subkeys != NULL) {
-               if (subkeys->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 (file)
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 <noodles@earth.li>
+ */
+
+#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__ */
index 2d9510697cbe8d893f9ae6c8017f6b419ba8b476..349d5c862903e6fecad1f3b00c804d6d0620e835 100644 (file)
@@ -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 =
index 73a94cddc42a4f396e9e835aeab6bb45748fb07b..86a83632a3baf786fac8b9cf2373cae03de041c0 100644 (file)
@@ -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;
                                }
                        }