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"
23 * parse_subpackets - Parse the subpackets of a Type 4 signature.
24 * @data: The subpacket data.
25 * @keyid: A pointer to where we should return the keyid.
27 * This function parses the subkey data of a Type 4 signature and fills
28 * in the supplied variables. It also returns the length of the data
31 int parse_subpackets(unsigned char *data, uint64_t *keyid)
39 length = (data[0] << 8) + data[1] + 2;
42 while (offset < length) {
43 packetlen = data[offset++];
44 if (packetlen > 191 && packetlen < 255) {
45 packetlen = ((packetlen - 192) << 8) +
47 } else if (packetlen == 255) {
48 packetlen = data[offset++];
50 packetlen = data[offset++];
52 packetlen = data[offset++];
54 packetlen = data[offset++];
56 switch (data[offset]) {
59 * Signature creation time. Might want to output this?
64 * Signature expiration time. Might want to output this?
68 *keyid = data[offset+packetlen - 8];
70 *keyid += data[offset+packetlen - 7];
72 *keyid += data[offset+packetlen - 6];
74 *keyid += data[offset+packetlen - 5];
76 *keyid += data[offset+packetlen - 4];
78 *keyid += data[offset+packetlen - 3];
80 *keyid += data[offset+packetlen - 2];
82 *keyid += data[offset+packetlen - 1];
86 * Key server preferences. Including no-modify.
96 * We don't care about unrecognized packets unless bit
97 * 7 is set in which case we prefer an error than
100 assert(!(data[offset] & 0x80));
109 * keysigs - Return the sigs on a given OpenPGP signature list.
110 * @curll: The current linked list. Can be NULL to create a new list.
111 * @sigs: The signature list we want the sigs on.
113 * Returns a linked list of stats_key elements containing the sigs on the
114 * supplied OpenPGP packet list.
116 struct ll *keysigs(struct ll *curll,
117 struct openpgp_packet_list *sigs)
121 while (sigs != NULL) {
122 keyid = sig_keyid(sigs->packet);
124 curll = lladd(curll, createandaddtohash(keyid));
131 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
132 * @packet: The signature packet.
134 * Returns the keyid for the supplied signature packet.
136 uint64_t sig_keyid(struct openpgp_packet *packet)
141 if (packet != NULL) {
143 switch (packet->data[0]) {
146 keyid = packet->data[7];
148 keyid += packet->data[8];
150 keyid += packet->data[9];
152 keyid += packet->data[10];
154 keyid += packet->data[11];
156 keyid += packet->data[12];
158 keyid += packet->data[13];
160 keyid += packet->data[14];
163 length = parse_subpackets(&packet->data[4],
165 parse_subpackets(&packet->data[length + 4],
168 * Don't bother to look at the unsigned packets.
180 * TODO: Abstract out; all our linked lists should be generic and then we can
183 int spsize(struct openpgp_signedpacket_list *list)
186 struct openpgp_signedpacket_list *cur;
188 for (cur = list; cur != NULL; cur = cur->next, size++) ;
194 * keyuids - Takes a key and returns an array of its UIDs
195 * @key: The key to get the uids of.
196 * @primary: A pointer to store the primary UID in.
198 * keyuids takes a public key structure and builds an array of the UIDs
199 * on the key. It also attempts to work out the primary UID and returns a
200 * separate pointer to that particular element of the array.
202 char **keyuids(struct openpgp_publickey *key, char **primary)
204 struct openpgp_signedpacket_list *curuid = NULL;
209 if (key != NULL && key->uids != NULL) {
210 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
213 while (curuid != NULL) {
215 if (curuid->packet->tag == 13) {
216 snprintf(buf, 1023, "%.*s",
217 (int) curuid->packet->length,
218 curuid->packet->data);
219 uids[count++] = strdup(buf);
221 curuid = curuid -> next;
226 * TODO: Parse subpackets for real primary ID (v4 keys)
228 if (primary != NULL) {