Update Debian Vcs-* fields to point to git repository
[onak.git] / decodekey.c
index fac501ea918e842e36b81ecef07eedd01d0bd99c..b4f7ceb46781d45f376385c4cea0ac42affffb63 100644 (file)
@@ -1,14 +1,22 @@
 /*
  * decodekey.c - Routines to further decode an OpenPGP key.
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
  *
- * Copyright 2002 Project Purple
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
  *
- * $Id: decodekey.c,v 1.4 2003/09/28 21:07:50 noodles Exp $
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include <assert.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "keystructs.h"
 #include "ll.h"
 #include "log.h"
+#include "openpgp.h"
 
 /*
  *     parse_subpackets - Parse the subpackets of a Type 4 signature.
  *     @data: The subpacket data.
- *      @keyid: A pointer to where we should return the keyid.
+ *     @keyid: A pointer to where we should return the keyid.
+ *     @creationtime: A pointer to where we should return the creation time.
  *
  *     This function parses the subkey data of a Type 4 signature and fills
  *     in the supplied variables. It also returns the length of the data
- *     processed.
+ *     processed. If the value of any piece of data is not desired a NULL
+ *     can be passed instead of a pointer to a storage area for that value.
  */
-int parse_subpackets(unsigned char *data, uint64_t *keyid)
+int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation)
 {
        int offset = 0;
        int length = 0;
        int packetlen = 0;
 
-       assert(data != NULL);
+       log_assert(data != NULL);
 
        length = (data[0] << 8) + data[1] + 2;
 
@@ -57,41 +68,60 @@ int parse_subpackets(unsigned char *data, uint64_t *keyid)
                        packetlen = data[offset++];
                }
                switch (data[offset] & 0x7F) {
-               case 2:
+               case OPENPGP_SIGSUB_CREATION:
                        /*
-                        * Signature creation time. Might want to output this?
+                        * Signature creation time.
                         */
+                       if (creation != NULL) {
+                               *creation = data[offset + packetlen - 4];
+                               *creation <<= 8;
+                               *creation = data[offset + packetlen - 3];
+                               *creation <<= 8;
+                               *creation = data[offset + packetlen - 2];
+                               *creation <<= 8;
+                               *creation = data[offset + packetlen - 1];
+                       }
                        break;
-               case 3:
                        /*
                         * Signature expiration time. Might want to output this?
                         */
                        break;
-               case 16:
-                       *keyid = data[offset+packetlen - 8];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 7];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 6];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 5];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 4];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 3];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 2];
-                       *keyid <<= 8;
-                       *keyid += data[offset+packetlen - 1];
-                       break;
-               case 23:
-                       /*
-                        * Key server preferences. Including no-modify.
-                        */
+               case OPENPGP_SIGSUB_ISSUER:
+                       if (keyid != NULL) {
+                               *keyid = data[offset+packetlen - 8];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 7];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 6];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 5];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 4];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 3];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 2];
+                               *keyid <<= 8;
+                               *keyid += data[offset+packetlen - 1];
+                       }
                        break;
-               case 25:
+               case OPENPGP_SIGSUB_EXPIRY:
+               case OPENPGP_SIGSUB_EXPORTABLE:
+               case OPENPGP_SIGSUB_TRUSTSIG:
+               case OPENPGP_SIGSUB_REGEX:
+               case OPENPGP_SIGSUB_KEYEXPIRY:
+               case OPENPGP_SIGSUB_PREFSYM:
+               case OPENPGP_SIGSUB_NOTATION:
+               case OPENPGP_SIGSUB_PREFHASH:
+               case OPENPGP_SIGSUB_PREFCOMPRESS:
+               case OPENPGP_SIGSUB_KEYSERVER:
+               case OPENPGP_SIGSUB_PRIMARYUID:
+               case OPENPGP_SIGSUB_POLICYURI:
+               case OPENPGP_SIGSUB_KEYFLAGS:
                        /*
-                        * Primary UID.
+                        * Various subpacket types we know about, but don't
+                        * currently handle. Some are candidates for being
+                        * supported if we add signature checking support.
                         */
                        break;
                default:
@@ -135,42 +165,55 @@ struct ll *keysigs(struct ll *curll,
 }
 
 /**
- *     sig_keyid - Return the keyid for a given OpenPGP signature packet.
- *     @packet: The signature packet.
+ *     sig_info - Get info on a given OpenPGP signature packet
+ *     @packet: The signature packet
+ *     @keyid: A pointer for where to return the signature keyid
+ *     @creation: A pointer for where to return the signature creation time
  *
- *     Returns the keyid for the supplied signature packet.
+ *     Gets any info about a signature packet; parses the subpackets for a v4
+ *     key or pulls the data directly from v2/3. NULL can be passed for any
+ *     values which aren't cared about.
  */
-uint64_t sig_keyid(struct openpgp_packet *packet)
+void sig_info(struct openpgp_packet *packet, uint64_t *keyid, time_t *creation)
 {
        int length = 0;
-       uint64_t keyid = 0;
        
        if (packet != NULL) {
-               keyid = 0;
                switch (packet->data[0]) {
                case 2:
                case 3:
-                       keyid = packet->data[7];
-                       keyid <<= 8;
-                       keyid += packet->data[8];
-                       keyid <<= 8;
-                       keyid += packet->data[9];
-                       keyid <<= 8;
-                       keyid += packet->data[10];
-                       keyid <<= 8;
-                       keyid += packet->data[11];
-                       keyid <<= 8;
-                       keyid += packet->data[12];
-                       keyid <<= 8;
-                       keyid += packet->data[13];
-                       keyid <<= 8;
-                       keyid += packet->data[14];
+                       if (keyid != NULL) {
+                               *keyid = packet->data[7];
+                               *keyid <<= 8;
+                               *keyid += packet->data[8];
+                               *keyid <<= 8;
+                               *keyid += packet->data[9];
+                               *keyid <<= 8;
+                               *keyid += packet->data[10];
+                               *keyid <<= 8;
+                               *keyid += packet->data[11];
+                               *keyid <<= 8;
+                               *keyid += packet->data[12];
+                               *keyid <<= 8;
+                               *keyid += packet->data[13];
+                               *keyid <<= 8;
+                               *keyid += packet->data[14];
+                       }
+                       if (creation != NULL) {
+                               *creation = packet->data[3];
+                               *creation <<= 8;
+                               *creation = packet->data[4];
+                               *creation <<= 8;
+                               *creation = packet->data[5];
+                               *creation <<= 8;
+                               *creation = packet->data[6];
+                       }
                        break;
                case 4:
                        length = parse_subpackets(&packet->data[4],
-                                       &keyid);
+                                       keyid, creation);
                        parse_subpackets(&packet->data[length + 4],
-                                       &keyid);
+                                       keyid, creation);
                        /*
                         * Don't bother to look at the unsigned packets.
                         */
@@ -180,9 +223,25 @@ uint64_t sig_keyid(struct openpgp_packet *packet)
                }
        }
 
+       return;
+}
+
+/**
+ *     sig_keyid - Return the keyid for a given OpenPGP signature packet.
+ *     @packet: The signature packet.
+ *
+ *     Returns the keyid for the supplied signature packet.
+ */
+uint64_t sig_keyid(struct openpgp_packet *packet)
+{
+       uint64_t keyid = 0;
+
+       sig_info(packet, &keyid, NULL);
+
        return keyid;
 }
 
+
 /*
  * TODO: Abstract out; all our linked lists should be generic and then we can
  * llsize them.
@@ -212,15 +271,18 @@ char **keyuids(struct openpgp_publickey *key, char **primary)
        char buf[1024];
        char **uids = NULL;
        int count = 0;
+        
+        if (primary != NULL) {
+               *primary = NULL;
+       }
 
-       *primary = NULL;
        if (key != NULL && key->uids != NULL) {
                uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
        
                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);
@@ -240,3 +302,30 @@ char **keyuids(struct openpgp_publickey *key, char **primary)
 
        return uids;
 }
+
+/**
+ *     keysubkeys - Takes a key and returns an array of its subkey keyids.
+ *     @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.
+ */
+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) {
+                       get_packetid(cursubkey->packet, &subkeys[count++]);
+                       cursubkey = cursubkey -> next;
+               }
+               subkeys[count] = 0;
+       }
+
+       return subkeys;
+}