2 * decodekey.c - Routines to further decode an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
15 #include "decodekey.h"
18 #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.
26 * @creationtime: A pointer to where we should return the creation time.
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
30 * processed. If the value of any piece of data is not desired a NULL
31 * can be passed instead of a pointer to a storage area for that value.
33 int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation)
39 log_assert(data != NULL);
41 length = (data[0] << 8) + data[1] + 2;
44 while (offset < length) {
45 packetlen = data[offset++];
46 if (packetlen > 191 && packetlen < 255) {
47 packetlen = ((packetlen - 192) << 8) +
49 } else if (packetlen == 255) {
50 packetlen = data[offset++];
52 packetlen = data[offset++];
54 packetlen = data[offset++];
56 packetlen = data[offset++];
58 switch (data[offset] & 0x7F) {
61 * Signature creation time.
63 if (creation != NULL) {
64 *creation = data[offset + packetlen - 4];
66 *creation = data[offset + packetlen - 3];
68 *creation = data[offset + packetlen - 2];
70 *creation = data[offset + packetlen - 1];
75 * Signature expiration time. Might want to output this?
80 *keyid = data[offset+packetlen - 8];
82 *keyid += data[offset+packetlen - 7];
84 *keyid += data[offset+packetlen - 6];
86 *keyid += data[offset+packetlen - 5];
88 *keyid += data[offset+packetlen - 4];
90 *keyid += data[offset+packetlen - 3];
92 *keyid += data[offset+packetlen - 2];
94 *keyid += data[offset+packetlen - 1];
105 * Key server preferences. Including no-modify.
115 * We don't care about unrecognized packets unless bit
116 * 7 is set in which case we log a major error.
118 if (data[offset] & 0x80) {
119 logthing(LOGTHING_CRITICAL,
120 "Critical subpacket type not parsed: 0x%X",
132 * keysigs - Return the sigs on a given OpenPGP signature list.
133 * @curll: The current linked list. Can be NULL to create a new list.
134 * @sigs: The signature list we want the sigs on.
136 * Returns a linked list of stats_key elements containing the sigs on the
137 * supplied OpenPGP packet list.
139 struct ll *keysigs(struct ll *curll,
140 struct openpgp_packet_list *sigs)
144 while (sigs != NULL) {
145 keyid = sig_keyid(sigs->packet);
147 curll = lladd(curll, createandaddtohash(keyid));
154 * sig_info - Get info on a given OpenPGP signature packet
155 * @packet: The signature packet
156 * @keyid: A pointer for where to return the signature keyid
157 * @creation: A pointer for where to return the signature creation time
159 * Gets any info about a signature packet; parses the subpackets for a v4
160 * key or pulls the data directly from v2/3. NULL can be passed for any
161 * values which aren't cared about.
163 void sig_info(struct openpgp_packet *packet, uint64_t *keyid, time_t *creation)
167 if (packet != NULL) {
168 switch (packet->data[0]) {
172 *keyid = packet->data[7];
174 *keyid += packet->data[8];
176 *keyid += packet->data[9];
178 *keyid += packet->data[10];
180 *keyid += packet->data[11];
182 *keyid += packet->data[12];
184 *keyid += packet->data[13];
186 *keyid += packet->data[14];
188 if (creation != NULL) {
189 *creation = packet->data[3];
191 *creation = packet->data[4];
193 *creation = packet->data[5];
195 *creation = packet->data[6];
199 length = parse_subpackets(&packet->data[4],
201 parse_subpackets(&packet->data[length + 4],
204 * Don't bother to look at the unsigned packets.
216 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
217 * @packet: The signature packet.
219 * Returns the keyid for the supplied signature packet.
221 uint64_t sig_keyid(struct openpgp_packet *packet)
225 sig_info(packet, &keyid, NULL);
232 * TODO: Abstract out; all our linked lists should be generic and then we can
235 int spsize(struct openpgp_signedpacket_list *list)
238 struct openpgp_signedpacket_list *cur;
240 for (cur = list; cur != NULL; cur = cur->next, size++) ;
246 * keyuids - Takes a key and returns an array of its UIDs
247 * @key: The key to get the uids of.
248 * @primary: A pointer to store the primary UID in.
250 * keyuids takes a public key structure and builds an array of the UIDs
251 * on the key. It also attempts to work out the primary UID and returns a
252 * separate pointer to that particular element of the array.
254 char **keyuids(struct openpgp_publickey *key, char **primary)
256 struct openpgp_signedpacket_list *curuid = NULL;
261 if (primary != NULL) {
265 if (key != NULL && key->uids != NULL) {
266 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
269 while (curuid != NULL) {
271 if (curuid->packet->tag == 13) {
272 snprintf(buf, 1023, "%.*s",
273 (int) curuid->packet->length,
274 curuid->packet->data);
275 uids[count++] = strdup(buf);
277 curuid = curuid -> next;
282 * TODO: Parse subpackets for real primary ID (v4 keys)
284 if (primary != NULL) {
293 * keysubkeys - Takes a key and returns an array of its subkey keyids.
294 * @key: The key to get the subkeys of.
296 * keysubkeys takes a public key structure and returns an array of the
297 * subkey keyids for that key.
299 uint64_t *keysubkeys(struct openpgp_publickey *key)
301 struct openpgp_signedpacket_list *cursubkey = NULL;
302 uint64_t *subkeys = NULL;
305 if (key != NULL && key->subkeys != NULL) {
306 subkeys = malloc((spsize(key->subkeys) + 1) *
308 cursubkey = key->subkeys;
309 while (cursubkey != NULL) {
310 subkeys[count++] = get_packetid(cursubkey->packet);
311 cursubkey = cursubkey -> next;