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.2 2003/06/04 20:57:07 noodles Exp $
18 #include "decodekey.h"
21 #include "keystructs.h"
25 * parse_subpackets - Parse the subpackets of a Type 4 signature.
26 * @data: The subpacket data.
27 * @keyid: A pointer to where we should return the keyid.
29 * This function parses the subkey data of a Type 4 signature and fills
30 * in the supplied variables. It also returns the length of the data
33 int parse_subpackets(unsigned char *data, uint64_t *keyid)
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]) {
61 * Signature creation time. Might want to output this?
66 * Signature expiration time. Might want to output this?
70 *keyid = data[offset+packetlen - 8];
72 *keyid += data[offset+packetlen - 7];
74 *keyid += data[offset+packetlen - 6];
76 *keyid += data[offset+packetlen - 5];
78 *keyid += data[offset+packetlen - 4];
80 *keyid += data[offset+packetlen - 3];
82 *keyid += data[offset+packetlen - 2];
84 *keyid += data[offset+packetlen - 1];
88 * Key server preferences. Including no-modify.
98 * We don't care about unrecognized packets unless bit
99 * 7 is set in which case we prefer an error than
102 assert(!(data[offset] & 0x80));
111 * keysigs - Return the sigs on a given OpenPGP signature list.
112 * @curll: The current linked list. Can be NULL to create a new list.
113 * @sigs: The signature list we want the sigs on.
115 * Returns a linked list of stats_key elements containing the sigs on the
116 * supplied OpenPGP packet list.
118 struct ll *keysigs(struct ll *curll,
119 struct openpgp_packet_list *sigs)
123 while (sigs != NULL) {
124 keyid = sig_keyid(sigs->packet);
126 curll = lladd(curll, createandaddtohash(keyid));
133 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
134 * @packet: The signature packet.
136 * Returns the keyid for the supplied signature packet.
138 uint64_t sig_keyid(struct openpgp_packet *packet)
143 if (packet != NULL) {
145 switch (packet->data[0]) {
148 keyid = packet->data[7];
150 keyid += packet->data[8];
152 keyid += packet->data[9];
154 keyid += packet->data[10];
156 keyid += packet->data[11];
158 keyid += packet->data[12];
160 keyid += packet->data[13];
162 keyid += packet->data[14];
165 length = parse_subpackets(&packet->data[4],
167 parse_subpackets(&packet->data[length + 4],
170 * Don't bother to look at the unsigned packets.
182 * TODO: Abstract out; all our linked lists should be generic and then we can
185 int spsize(struct openpgp_signedpacket_list *list)
188 struct openpgp_signedpacket_list *cur;
190 for (cur = list; cur != NULL; cur = cur->next, size++) ;
196 * keyuids - Takes a key and returns an array of its UIDs
197 * @key: The key to get the uids of.
198 * @primary: A pointer to store the primary UID in.
200 * keyuids takes a public key structure and builds an array of the UIDs
201 * on the key. It also attempts to work out the primary UID and returns a
202 * separate pointer to that particular element of the array.
204 char **keyuids(struct openpgp_publickey *key, char **primary)
206 struct openpgp_signedpacket_list *curuid = NULL;
211 if (key != NULL && key->uids != NULL) {
212 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
215 while (curuid != NULL) {
217 if (curuid->packet->tag == 13) {
218 snprintf(buf, 1023, "%.*s",
219 (int) curuid->packet->length,
220 curuid->packet->data);
221 uids[count++] = strdup(buf);
223 curuid = curuid -> next;
228 * TODO: Parse subpackets for real primary ID (v4 keys)
230 if (primary != NULL) {