2 * decodekey.c - Routines to further decode an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
8 * $Id: decodekey.c,v 1.6 2004/05/27 03:24:01 noodles Exp $
18 #include "decodekey.h"
21 #include "keystructs.h"
26 * parse_subpackets - Parse the subpackets of a Type 4 signature.
27 * @data: The subpacket data.
28 * @keyid: A pointer to where we should return the keyid.
30 * This function parses the subkey data of a Type 4 signature and fills
31 * in the supplied variables. It also returns the length of the data
34 int parse_subpackets(unsigned char *data, uint64_t *keyid)
42 length = (data[0] << 8) + data[1] + 2;
45 while (offset < length) {
46 packetlen = data[offset++];
47 if (packetlen > 191 && packetlen < 255) {
48 packetlen = ((packetlen - 192) << 8) +
50 } else if (packetlen == 255) {
51 packetlen = data[offset++];
53 packetlen = data[offset++];
55 packetlen = data[offset++];
57 packetlen = data[offset++];
59 switch (data[offset] & 0x7F) {
62 * Signature creation time. Might want to output this?
67 * Signature expiration time. Might want to output this?
71 *keyid = data[offset+packetlen - 8];
73 *keyid += data[offset+packetlen - 7];
75 *keyid += data[offset+packetlen - 6];
77 *keyid += data[offset+packetlen - 5];
79 *keyid += data[offset+packetlen - 4];
81 *keyid += data[offset+packetlen - 3];
83 *keyid += data[offset+packetlen - 2];
85 *keyid += data[offset+packetlen - 1];
95 * Key server preferences. Including no-modify.
105 * We don't care about unrecognized packets unless bit
106 * 7 is set in which case we log a major error.
108 if (data[offset] & 0x80) {
109 logthing(LOGTHING_CRITICAL,
110 "Critical subpacket type not parsed: 0x%X",
122 * keysigs - Return the sigs on a given OpenPGP signature list.
123 * @curll: The current linked list. Can be NULL to create a new list.
124 * @sigs: The signature list we want the sigs on.
126 * Returns a linked list of stats_key elements containing the sigs on the
127 * supplied OpenPGP packet list.
129 struct ll *keysigs(struct ll *curll,
130 struct openpgp_packet_list *sigs)
134 while (sigs != NULL) {
135 keyid = sig_keyid(sigs->packet);
137 curll = lladd(curll, createandaddtohash(keyid));
144 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
145 * @packet: The signature packet.
147 * Returns the keyid for the supplied signature packet.
149 uint64_t sig_keyid(struct openpgp_packet *packet)
154 if (packet != NULL) {
156 switch (packet->data[0]) {
159 keyid = packet->data[7];
161 keyid += packet->data[8];
163 keyid += packet->data[9];
165 keyid += packet->data[10];
167 keyid += packet->data[11];
169 keyid += packet->data[12];
171 keyid += packet->data[13];
173 keyid += packet->data[14];
176 length = parse_subpackets(&packet->data[4],
178 parse_subpackets(&packet->data[length + 4],
181 * Don't bother to look at the unsigned packets.
193 * TODO: Abstract out; all our linked lists should be generic and then we can
196 int spsize(struct openpgp_signedpacket_list *list)
199 struct openpgp_signedpacket_list *cur;
201 for (cur = list; cur != NULL; cur = cur->next, size++) ;
207 * keyuids - Takes a key and returns an array of its UIDs
208 * @key: The key to get the uids of.
209 * @primary: A pointer to store the primary UID in.
211 * keyuids takes a public key structure and builds an array of the UIDs
212 * on the key. It also attempts to work out the primary UID and returns a
213 * separate pointer to that particular element of the array.
215 char **keyuids(struct openpgp_publickey *key, char **primary)
217 struct openpgp_signedpacket_list *curuid = NULL;
222 if (primary != NULL) {
226 if (key != NULL && key->uids != NULL) {
227 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
230 while (curuid != NULL) {
232 if (curuid->packet->tag == 13) {
233 snprintf(buf, 1023, "%.*s",
234 (int) curuid->packet->length,
235 curuid->packet->data);
236 uids[count++] = strdup(buf);
238 curuid = curuid -> next;
243 * TODO: Parse subpackets for real primary ID (v4 keys)
245 if (primary != NULL) {