2 * keyindex.c - Routines to list an OpenPGP key.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
21 #include "keystructs.h"
25 int parse_subpackets(unsigned char *data, bool html)
34 length = (data[0] << 8) + data[1] + 2;
37 while (offset < length) {
38 packetlen = data[offset++];
39 if (packetlen > 191 && packetlen < 255) {
40 packetlen = ((packetlen - 192) << 8) +
42 } else if (packetlen == 255) {
43 packetlen = data[offset++];
45 packetlen = data[offset++];
47 packetlen = data[offset++];
49 packetlen = data[offset++];
51 switch (data[offset]) {
54 * Signature creation time. Might want to output this?
58 uid = keyid2uid((data[offset+packetlen - 4] << 24) +
59 (data[offset+packetlen - 3] << 16) +
60 (data[offset+packetlen - 2] << 8) +
61 data[offset+packetlen - 1]);
62 if (html && uid != NULL) {
63 printf("sig <a href=\"lookup?op=get&"
64 "search=%02X%02X%02X%02X\">"
65 "%02X%02X%02X%02X</a> "
66 "<a href=\"lookup?op=vindex&"
67 "search=0x%02X%02X%02X%02X\">"
69 data[offset+packetlen - 4],
70 data[offset+packetlen - 3],
71 data[offset+packetlen - 2],
72 data[offset+packetlen - 1],
73 data[offset+packetlen - 4],
74 data[offset+packetlen - 3],
75 data[offset+packetlen - 2],
76 data[offset+packetlen - 1],
78 data[offset+packetlen - 4],
79 data[offset+packetlen - 3],
80 data[offset+packetlen - 2],
81 data[offset+packetlen - 1],
83 } else if (html && uid == NULL) {
86 "[User id not found]\n",
87 data[offset+packetlen - 4],
88 data[offset+packetlen - 3],
89 data[offset+packetlen - 2],
90 data[offset+packetlen - 1]);
92 printf("sig %02X%02X%02X%02X"
94 data[offset+packetlen - 4],
95 data[offset+packetlen - 3],
96 data[offset+packetlen - 2],
97 data[offset+packetlen - 1],
99 "[User id not found]");
104 * We don't care about unrecognized packets unless bit
105 * 7 is set in which case we prefer an error than
108 assert(!(data[offset] & 0x80));
116 int list_sigs(struct openpgp_packet_list *sigs, bool html)
121 while (sigs != NULL) {
122 switch (sigs->packet->data[0]) {
125 uid = keyid2uid((sigs->packet->data[11] << 24) +
126 (sigs->packet->data[12] << 16) +
127 (sigs->packet->data[13] << 8) +
128 sigs->packet->data[14]);
129 if (html && uid != NULL) {
130 printf("sig <a href=\"lookup?op=get&"
131 "search=%02X%02X%02X%02X\">"
132 "%02X%02X%02X%02X</a> "
133 "<a href=\"lookup?op=vindex&"
134 "search=0x%02X%02X%02X%02X\">"
136 sigs->packet->data[11],
137 sigs->packet->data[12],
138 sigs->packet->data[13],
139 sigs->packet->data[14],
140 sigs->packet->data[11],
141 sigs->packet->data[12],
142 sigs->packet->data[13],
143 sigs->packet->data[14],
145 sigs->packet->data[11],
146 sigs->packet->data[12],
147 sigs->packet->data[13],
148 sigs->packet->data[14],
150 } else if (html && uid == NULL) {
151 printf("sig %02X%02X%02X%02X"
153 "[User id not found]\n",
154 sigs->packet->data[11],
155 sigs->packet->data[12],
156 sigs->packet->data[13],
157 sigs->packet->data[14]);
159 printf("sig %02X%02X%02X%02X"
161 sigs->packet->data[11],
162 sigs->packet->data[12],
163 sigs->packet->data[13],
164 sigs->packet->data[14],
165 (uid != NULL) ? uid :
166 "[User id not found]");
170 length = parse_subpackets(&sigs->packet->data[4], html);
171 parse_subpackets(&sigs->packet->data[length + 4], html);
174 printf("sig [Unknown packet version %d]",
175 sigs->packet->data[0]);
183 int list_uids(struct openpgp_signedpacket_list *uids, bool verbose, bool html)
187 while (uids != NULL) {
188 if (uids->packet->tag == 13) {
189 snprintf(buf, 1023, "%.*s",
190 (int) uids->packet->length,
193 (html) ? txt2html(buf) : buf);
194 } else if (uids->packet->tag == 17) {
199 list_sigs(uids->sigs, html);
208 * key_index - List a set of OpenPGP keys.
209 * @keys: The keys to display.
210 * @verbose: Should we list sigs as well?
211 * @fingerprint: List the fingerprint?
212 * @html: Should the output be tailored for HTML?
214 * This function takes a list of OpenPGP public keys and displays an index
215 * of them. Useful for debugging or the keyserver Index function.
217 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
220 struct openpgp_signedpacket_list *curuid = NULL;
221 struct tm *created = NULL;
222 time_t created_time = 0;
230 puts("Type bits/keyID Date User ID");
231 while (keys != NULL) {
232 created_time = (keys->publickey->data[1] << 24) +
233 (keys->publickey->data[2] << 16) +
234 (keys->publickey->data[3] << 8) +
235 keys->publickey->data[4];
236 created = gmtime(&created_time);
238 switch (keys->publickey->data[0]) {
241 type = keys->publickey->data[7];
242 length = (keys->publickey->data[8] << 8) +
243 keys->publickey->data[9];
246 type = keys->publickey->data[5];
247 length = (keys->publickey->data[6] << 8) +
248 keys->publickey->data[7];
251 fprintf(stderr, "Unknown key type: %d\n",
252 keys->publickey->data[0]);
255 printf("pub %4d%c/%08X %04d/%02d/%02d ",
257 (type == 1) ? 'R' : ((type == 17) ? 'D' : '?'),
258 (uint32_t) (get_keyid(keys) & 0xFFFFFFFF),
259 created->tm_year + 1900,
264 if (curuid != NULL && curuid->packet->tag == 13) {
265 snprintf(buf, 1023, "%.*s",
266 (int) curuid->packet->length,
267 curuid->packet->data);
268 printf("%s\n", (html) ? txt2html(buf) : buf);
271 list_sigs(curuid->sigs, html);
273 curuid = curuid->next;
278 list_uids(curuid, verbose, html);
280 //TODO: List subkeys.
293 int get_subpackets_keyid(unsigned char *data, uint64_t *keyid)
299 assert(data != NULL);
301 length = (data[0] << 8) + data[1] + 2;
304 while (offset < length) {
305 packetlen = data[offset++];
306 if (packetlen > 191 && packetlen < 255) {
307 packetlen = ((packetlen - 192) << 8) +
308 data[offset++] + 192;
309 } else if (packetlen == 255) {
310 packetlen = data[offset++];
312 packetlen = data[offset++];
314 packetlen = data[offset++];
316 packetlen = data[offset++];
318 switch (data[offset]) {
321 * Signature creation time. Might want to output this?
325 *keyid = (data[offset+packetlen - 4] << 24) +
326 (data[offset+packetlen - 3] << 16) +
327 (data[offset+packetlen - 2] << 8) +
328 data[offset+packetlen - 1];
329 *keyid &= 0xFFFFFFFF;
333 * We don't care about unrecognized packets unless bit
334 * 7 is set in which case we prefer an error than
337 assert(!(data[offset] & 0x80));
347 * keysigs - Return the sigs on a given OpenPGP signature list.
348 * @curll: The current linked list. Can be NULL to create a new list.
349 * @sigs: The signature list we want the sigs on.
351 * Returns a linked list of stats_key elements containing the sigs on the
352 * supplied OpenPGP packet list.
354 struct ll *keysigs(struct ll *curll,
355 struct openpgp_packet_list *sigs)
360 while (sigs != NULL) {
362 switch (sigs->packet->data[0]) {
365 keyid = sigs->packet->data[11] << 24;
366 keyid += (sigs->packet->data[12] << 16);
367 keyid += (sigs->packet->data[13] << 8);
368 keyid += sigs->packet->data[14];
372 length = get_subpackets_keyid(&sigs->packet->data[4],
374 get_subpackets_keyid(&sigs->packet->data[length + 4],
377 * Don't bother to look at the unsigned packets.
384 curll = lladd(curll, createandaddtohash(keyid));