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.4 2003/09/28 21:07:50 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];
89 * Key server preferences. Including no-modify.
99 * We don't care about unrecognized packets unless bit
100 * 7 is set in which case we log a major error.
102 if (data[offset] & 0x80) {
103 logthing(LOGTHING_CRITICAL,
104 "Critical subpacket type not parsed: 0x%X",
116 * keysigs - Return the sigs on a given OpenPGP signature list.
117 * @curll: The current linked list. Can be NULL to create a new list.
118 * @sigs: The signature list we want the sigs on.
120 * Returns a linked list of stats_key elements containing the sigs on the
121 * supplied OpenPGP packet list.
123 struct ll *keysigs(struct ll *curll,
124 struct openpgp_packet_list *sigs)
128 while (sigs != NULL) {
129 keyid = sig_keyid(sigs->packet);
131 curll = lladd(curll, createandaddtohash(keyid));
138 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
139 * @packet: The signature packet.
141 * Returns the keyid for the supplied signature packet.
143 uint64_t sig_keyid(struct openpgp_packet *packet)
148 if (packet != NULL) {
150 switch (packet->data[0]) {
153 keyid = packet->data[7];
155 keyid += packet->data[8];
157 keyid += packet->data[9];
159 keyid += packet->data[10];
161 keyid += packet->data[11];
163 keyid += packet->data[12];
165 keyid += packet->data[13];
167 keyid += packet->data[14];
170 length = parse_subpackets(&packet->data[4],
172 parse_subpackets(&packet->data[length + 4],
175 * Don't bother to look at the unsigned packets.
187 * TODO: Abstract out; all our linked lists should be generic and then we can
190 int spsize(struct openpgp_signedpacket_list *list)
193 struct openpgp_signedpacket_list *cur;
195 for (cur = list; cur != NULL; cur = cur->next, size++) ;
201 * keyuids - Takes a key and returns an array of its UIDs
202 * @key: The key to get the uids of.
203 * @primary: A pointer to store the primary UID in.
205 * keyuids takes a public key structure and builds an array of the UIDs
206 * on the key. It also attempts to work out the primary UID and returns a
207 * separate pointer to that particular element of the array.
209 char **keyuids(struct openpgp_publickey *key, char **primary)
211 struct openpgp_signedpacket_list *curuid = NULL;
217 if (key != NULL && key->uids != NULL) {
218 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
221 while (curuid != NULL) {
223 if (curuid->packet->tag == 13) {
224 snprintf(buf, 1023, "%.*s",
225 (int) curuid->packet->length,
226 curuid->packet->data);
227 uids[count++] = strdup(buf);
229 curuid = curuid -> next;
234 * TODO: Parse subpackets for real primary ID (v4 keys)
236 if (primary != NULL) {