2 * decodekey.c - Routines to further decode an OpenPGP key.
4 * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
13 #include "decodekey.h"
16 #include "keystructs.h"
21 * parse_subpackets - Parse the subpackets of a Type 4 signature.
22 * @data: The subpacket data.
23 * @keyid: A pointer to where we should return the keyid.
24 * @creationtime: A pointer to where we should return the creation time.
26 * This function parses the subkey data of a Type 4 signature and fills
27 * in the supplied variables. It also returns the length of the data
28 * processed. If the value of any piece of data is not desired a NULL
29 * can be passed instead of a pointer to a storage area for that value.
31 int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation)
37 log_assert(data != NULL);
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] & 0x7F) {
59 * Signature creation time.
61 if (creation != NULL) {
62 *creation = data[offset + packetlen - 4];
64 *creation = data[offset + packetlen - 3];
66 *creation = data[offset + packetlen - 2];
68 *creation = data[offset + packetlen - 1];
73 * Signature expiration time. Might want to output this?
78 * Regular expression for UIDs this sig is over.
83 *keyid = data[offset+packetlen - 8];
85 *keyid += data[offset+packetlen - 7];
87 *keyid += data[offset+packetlen - 6];
89 *keyid += data[offset+packetlen - 5];
91 *keyid += data[offset+packetlen - 4];
93 *keyid += data[offset+packetlen - 3];
95 *keyid += data[offset+packetlen - 2];
97 *keyid += data[offset+packetlen - 1];
108 * Key server preferences. Including no-modify.
123 * We don't care about unrecognized packets unless bit
124 * 7 is set in which case we log a major error.
126 if (data[offset] & 0x80) {
127 logthing(LOGTHING_CRITICAL,
128 "Critical subpacket type not parsed: 0x%X",
140 * keysigs - Return the sigs on a given OpenPGP signature list.
141 * @curll: The current linked list. Can be NULL to create a new list.
142 * @sigs: The signature list we want the sigs on.
144 * Returns a linked list of stats_key elements containing the sigs on the
145 * supplied OpenPGP packet list.
147 struct ll *keysigs(struct ll *curll,
148 struct openpgp_packet_list *sigs)
152 while (sigs != NULL) {
153 keyid = sig_keyid(sigs->packet);
155 curll = lladd(curll, createandaddtohash(keyid));
162 * sig_info - Get info on a given OpenPGP signature packet
163 * @packet: The signature packet
164 * @keyid: A pointer for where to return the signature keyid
165 * @creation: A pointer for where to return the signature creation time
167 * Gets any info about a signature packet; parses the subpackets for a v4
168 * key or pulls the data directly from v2/3. NULL can be passed for any
169 * values which aren't cared about.
171 void sig_info(struct openpgp_packet *packet, uint64_t *keyid, time_t *creation)
175 if (packet != NULL) {
176 switch (packet->data[0]) {
180 *keyid = packet->data[7];
182 *keyid += packet->data[8];
184 *keyid += packet->data[9];
186 *keyid += packet->data[10];
188 *keyid += packet->data[11];
190 *keyid += packet->data[12];
192 *keyid += packet->data[13];
194 *keyid += packet->data[14];
196 if (creation != NULL) {
197 *creation = packet->data[3];
199 *creation = packet->data[4];
201 *creation = packet->data[5];
203 *creation = packet->data[6];
207 length = parse_subpackets(&packet->data[4],
209 parse_subpackets(&packet->data[length + 4],
212 * Don't bother to look at the unsigned packets.
224 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
225 * @packet: The signature packet.
227 * Returns the keyid for the supplied signature packet.
229 uint64_t sig_keyid(struct openpgp_packet *packet)
233 sig_info(packet, &keyid, NULL);
240 * TODO: Abstract out; all our linked lists should be generic and then we can
243 int spsize(struct openpgp_signedpacket_list *list)
246 struct openpgp_signedpacket_list *cur;
248 for (cur = list; cur != NULL; cur = cur->next, size++) ;
254 * keyuids - Takes a key and returns an array of its UIDs
255 * @key: The key to get the uids of.
256 * @primary: A pointer to store the primary UID in.
258 * keyuids takes a public key structure and builds an array of the UIDs
259 * on the key. It also attempts to work out the primary UID and returns a
260 * separate pointer to that particular element of the array.
262 char **keyuids(struct openpgp_publickey *key, char **primary)
264 struct openpgp_signedpacket_list *curuid = NULL;
269 if (primary != NULL) {
273 if (key != NULL && key->uids != NULL) {
274 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
277 while (curuid != NULL) {
279 if (curuid->packet->tag == 13) {
280 snprintf(buf, 1023, "%.*s",
281 (int) curuid->packet->length,
282 curuid->packet->data);
283 uids[count++] = strdup(buf);
285 curuid = curuid -> next;
290 * TODO: Parse subpackets for real primary ID (v4 keys)
292 if (primary != NULL) {
301 * keysubkeys - Takes a key and returns an array of its subkey keyids.
302 * @key: The key to get the subkeys of.
304 * keysubkeys takes a public key structure and returns an array of the
305 * subkey keyids for that key.
307 uint64_t *keysubkeys(struct openpgp_publickey *key)
309 struct openpgp_signedpacket_list *cursubkey = NULL;
310 uint64_t *subkeys = NULL;
313 if (key != NULL && key->subkeys != NULL) {
314 subkeys = malloc((spsize(key->subkeys) + 1) *
316 cursubkey = key->subkeys;
317 while (cursubkey != NULL) {
318 subkeys[count++] = get_packetid(cursubkey->packet);
319 cursubkey = cursubkey -> next;