2 * decodekey.c - Routines to further decode an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
16 #include "decodekey.h"
19 #include "keystructs.h"
24 * parse_subpackets - Parse the subpackets of a Type 4 signature.
25 * @data: The subpacket data.
26 * @keyid: A pointer to where we should return the keyid.
28 * This function parses the subkey data of a Type 4 signature and fills
29 * in the supplied variables. It also returns the length of the data
32 int parse_subpackets(unsigned char *data, uint64_t *keyid)
40 length = (data[0] << 8) + data[1] + 2;
43 while (offset < length) {
44 packetlen = data[offset++];
45 if (packetlen > 191 && packetlen < 255) {
46 packetlen = ((packetlen - 192) << 8) +
48 } else if (packetlen == 255) {
49 packetlen = data[offset++];
51 packetlen = data[offset++];
53 packetlen = data[offset++];
55 packetlen = data[offset++];
57 switch (data[offset] & 0x7F) {
60 * Signature creation time. Might want to output this?
65 * Signature expiration time. Might want to output this?
69 *keyid = data[offset+packetlen - 8];
71 *keyid += data[offset+packetlen - 7];
73 *keyid += data[offset+packetlen - 6];
75 *keyid += data[offset+packetlen - 5];
77 *keyid += data[offset+packetlen - 4];
79 *keyid += data[offset+packetlen - 3];
81 *keyid += data[offset+packetlen - 2];
83 *keyid += data[offset+packetlen - 1];
93 * Key server preferences. Including no-modify.
103 * We don't care about unrecognized packets unless bit
104 * 7 is set in which case we log a major error.
106 if (data[offset] & 0x80) {
107 logthing(LOGTHING_CRITICAL,
108 "Critical subpacket type not parsed: 0x%X",
120 * keysigs - Return the sigs on a given OpenPGP signature list.
121 * @curll: The current linked list. Can be NULL to create a new list.
122 * @sigs: The signature list we want the sigs on.
124 * Returns a linked list of stats_key elements containing the sigs on the
125 * supplied OpenPGP packet list.
127 struct ll *keysigs(struct ll *curll,
128 struct openpgp_packet_list *sigs)
132 while (sigs != NULL) {
133 keyid = sig_keyid(sigs->packet);
135 curll = lladd(curll, createandaddtohash(keyid));
142 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
143 * @packet: The signature packet.
145 * Returns the keyid for the supplied signature packet.
147 uint64_t sig_keyid(struct openpgp_packet *packet)
152 if (packet != NULL) {
154 switch (packet->data[0]) {
157 keyid = packet->data[7];
159 keyid += packet->data[8];
161 keyid += packet->data[9];
163 keyid += packet->data[10];
165 keyid += packet->data[11];
167 keyid += packet->data[12];
169 keyid += packet->data[13];
171 keyid += packet->data[14];
174 length = parse_subpackets(&packet->data[4],
176 parse_subpackets(&packet->data[length + 4],
179 * Don't bother to look at the unsigned packets.
191 * TODO: Abstract out; all our linked lists should be generic and then we can
194 int spsize(struct openpgp_signedpacket_list *list)
197 struct openpgp_signedpacket_list *cur;
199 for (cur = list; cur != NULL; cur = cur->next, size++) ;
205 * keyuids - Takes a key and returns an array of its UIDs
206 * @key: The key to get the uids of.
207 * @primary: A pointer to store the primary UID in.
209 * keyuids takes a public key structure and builds an array of the UIDs
210 * on the key. It also attempts to work out the primary UID and returns a
211 * separate pointer to that particular element of the array.
213 char **keyuids(struct openpgp_publickey *key, char **primary)
215 struct openpgp_signedpacket_list *curuid = NULL;
220 if (primary != NULL) {
224 if (key != NULL && key->uids != NULL) {
225 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
228 while (curuid != NULL) {
230 if (curuid->packet->tag == 13) {
231 snprintf(buf, 1023, "%.*s",
232 (int) curuid->packet->length,
233 curuid->packet->data);
234 uids[count++] = strdup(buf);
236 curuid = curuid -> next;
241 * TODO: Parse subpackets for real primary ID (v4 keys)
243 if (primary != NULL) {
252 * keysubkeys - Takes a key and returns an array of its subkey keyids.
253 * @key: The key to get the subkeys of.
255 * keysubkeys takes a public key structure and returns an array of the
256 * subkey keyids for that key.
258 uint64_t *keysubkeys(struct openpgp_publickey *key)
260 struct openpgp_signedpacket_list *cursubkey = NULL;
261 uint64_t *subkeys = NULL;
264 if (key != NULL && key->subkeys != NULL) {
265 subkeys = malloc((spsize(key->subkeys) + 1) *
267 cursubkey = key->subkeys;
268 while (cursubkey != NULL) {
269 subkeys[count++] = get_packetid(cursubkey->packet);
270 cursubkey = cursubkey -> next;