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.
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)
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. 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];
92 * Key server preferences. Including no-modify.
102 * We don't care about unrecognized packets unless bit
103 * 7 is set in which case we log a major error.
105 if (data[offset] & 0x80) {
106 logthing(LOGTHING_CRITICAL,
107 "Critical subpacket type not parsed: 0x%X",
119 * keysigs - Return the sigs on a given OpenPGP signature list.
120 * @curll: The current linked list. Can be NULL to create a new list.
121 * @sigs: The signature list we want the sigs on.
123 * Returns a linked list of stats_key elements containing the sigs on the
124 * supplied OpenPGP packet list.
126 struct ll *keysigs(struct ll *curll,
127 struct openpgp_packet_list *sigs)
131 while (sigs != NULL) {
132 keyid = sig_keyid(sigs->packet);
134 curll = lladd(curll, createandaddtohash(keyid));
141 * sig_keyid - Return the keyid for a given OpenPGP signature packet.
142 * @packet: The signature packet.
144 * Returns the keyid for the supplied signature packet.
146 uint64_t sig_keyid(struct openpgp_packet *packet)
151 if (packet != NULL) {
153 switch (packet->data[0]) {
156 keyid = packet->data[7];
158 keyid += packet->data[8];
160 keyid += packet->data[9];
162 keyid += packet->data[10];
164 keyid += packet->data[11];
166 keyid += packet->data[12];
168 keyid += packet->data[13];
170 keyid += packet->data[14];
173 length = parse_subpackets(&packet->data[4],
175 parse_subpackets(&packet->data[length + 4],
178 * Don't bother to look at the unsigned packets.
190 * TODO: Abstract out; all our linked lists should be generic and then we can
193 int spsize(struct openpgp_signedpacket_list *list)
196 struct openpgp_signedpacket_list *cur;
198 for (cur = list; cur != NULL; cur = cur->next, size++) ;
204 * keyuids - Takes a key and returns an array of its UIDs
205 * @key: The key to get the uids of.
206 * @primary: A pointer to store the primary UID in.
208 * keyuids takes a public key structure and builds an array of the UIDs
209 * on the key. It also attempts to work out the primary UID and returns a
210 * separate pointer to that particular element of the array.
212 char **keyuids(struct openpgp_publickey *key, char **primary)
214 struct openpgp_signedpacket_list *curuid = NULL;
219 if (primary != NULL) {
223 if (key != NULL && key->uids != NULL) {
224 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
227 while (curuid != NULL) {
229 if (curuid->packet->tag == 13) {
230 snprintf(buf, 1023, "%.*s",
231 (int) curuid->packet->length,
232 curuid->packet->data);
233 uids[count++] = strdup(buf);
235 curuid = curuid -> next;
240 * TODO: Parse subpackets for real primary ID (v4 keys)
242 if (primary != NULL) {
251 * keysubkeys - Takes a key and returns an array of its subkey keyids.
252 * @key: The key to get the subkeys of.
254 * keysubkeys takes a public key structure and returns an array of the
255 * subkey keyids for that key.
257 uint64_t *keysubkeys(struct openpgp_publickey *key)
259 struct openpgp_signedpacket_list *cursubkey = NULL;
260 uint64_t *subkeys = NULL;
263 if (key != NULL && key->subkeys != NULL) {
264 subkeys = malloc((spsize(key->subkeys) + 1) *
266 cursubkey = key->subkeys;
267 while (cursubkey != NULL) {
268 subkeys[count++] = get_packetid(cursubkey->packet);
269 cursubkey = cursubkey -> next;