]> git.sommitrealweird.co.uk Git - onak.git/blob - keyindex.c
2c4e24c1dd020c32a934e862a2e2b7002e427a55
[onak.git] / keyindex.c
1 /*
2  * keyindex.c - Routines to list an OpenPGP key.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <assert.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "getcgi.h"
17 #include "hash.h"
18 #include "keydb.h"
19 #include "keyid.h"
20 #include "keyindex.h"
21 #include "keystructs.h"
22 #include "ll.h"
23 #include "stats.h"
24
25 int parse_subpackets(unsigned char *data, bool html)
26 {
27         int offset = 0;
28         int length = 0;
29         int packetlen = 0;
30         char *uid;
31
32         assert(data != NULL);
33
34         length = (data[0] << 8) + data[1] + 2;
35
36         offset = 2;
37         while (offset < length) {
38                 packetlen = data[offset++];
39                 if (packetlen > 191 && packetlen < 255) {
40                         packetlen = ((packetlen - 192) << 8) +
41                                         data[offset++] + 192;
42                 } else if (packetlen == 255) {
43                         packetlen = data[offset++];
44                         packetlen <<= 8;
45                         packetlen = data[offset++];
46                         packetlen <<= 8;
47                         packetlen = data[offset++];
48                         packetlen <<= 8;
49                         packetlen = data[offset++];
50                 }
51                 switch (data[offset]) {
52                 case 2:
53                         /*
54                          * Signature creation time. Might want to output this?
55                          */
56                         break;
57                 case 16:
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\">"
68                                         "%s</a>\n",
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],
77
78                                         data[offset+packetlen - 4],
79                                         data[offset+packetlen - 3],
80                                         data[offset+packetlen - 2],
81                                         data[offset+packetlen - 1],
82                                         txt2html(uid));
83                         } else if (html && uid == NULL) {
84                                 printf("sig        "
85                                         "%02X%02X%02X%02X             "
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]);
91                         } else {
92                                 printf("sig        %02X%02X%02X%02X"
93                                                 "             %s\n",
94                                         data[offset+packetlen - 4],
95                                         data[offset+packetlen - 3],
96                                         data[offset+packetlen - 2],
97                                         data[offset+packetlen - 1],
98                                         (uid != NULL) ? uid :
99                                         "[User id not found]");
100                         }
101                         break;
102                 default:
103                         /*
104                          * We don't care about unrecognized packets unless bit
105                          * 7 is set in which case we prefer an error than
106                          * ignoring it.
107                          */
108                         assert(!(data[offset] & 0x80));
109                 }
110                 offset += packetlen;
111         }
112
113         return length;
114 }
115
116 int list_sigs(struct openpgp_packet_list *sigs, bool html)
117 {
118         int length = 0;
119         char *uid;
120
121         while (sigs != NULL) {
122                 switch (sigs->packet->data[0]) {
123                 case 2:
124                 case 3:
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\">"
135                                         "%s</a>\n",
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],
144
145                                         sigs->packet->data[11],
146                                         sigs->packet->data[12],
147                                         sigs->packet->data[13],
148                                         sigs->packet->data[14],
149                                         txt2html(uid));
150                         } else if (html && uid == NULL) {
151                                 printf("sig        %02X%02X%02X%02X"
152                                         "             "
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]);
158                         } else {
159                                 printf("sig        %02X%02X%02X%02X"
160                                                 "             %s\n",
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]");
167                         }
168                         break;
169                 case 4:
170                         length = parse_subpackets(&sigs->packet->data[4], html);
171                         parse_subpackets(&sigs->packet->data[length + 4], html);
172                         break;
173                 default:
174                         printf("sig        [Unknown packet version %d]",
175                                         sigs->packet->data[0]);
176                 }
177                 sigs = sigs->next;
178         }
179
180         return 0;
181 }
182
183 int list_uids(struct openpgp_signedpacket_list *uids, bool verbose, bool html)
184 {
185         char buf[1024];
186
187         while (uids != NULL) {
188                 if (uids->packet->tag == 13) {
189                         snprintf(buf, 1023, "%.*s",
190                                 (int) uids->packet->length,
191                                 uids->packet->data);
192                         printf("uid                            %s\n",
193                                 (html) ? txt2html(buf) : buf);
194                 } else if (uids->packet->tag == 17) {
195                         printf("uid                            "
196                                 "[photo id]\n");
197                 }
198                 if (verbose) {
199                         list_sigs(uids->sigs, html);
200                 }
201                 uids = uids->next;
202         }
203
204         return 0;
205 }
206
207 /**
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?
213  *
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.
216  */
217 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
218                         bool html)
219 {
220         struct openpgp_signedpacket_list        *curuid = NULL;
221         struct tm                               *created = NULL;
222         time_t                                   created_time = 0;
223         int                                      type = 0;
224         int                                      length = 0;
225         char                                     buf[1024];
226
227         if (html) {
228                 puts("<pre>");
229         }
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);
237
238                 switch (keys->publickey->data[0]) {
239                 case 2:
240                 case 3:
241                         type = keys->publickey->data[7];
242                         length = (keys->publickey->data[8] << 8) +
243                                         keys->publickey->data[9];
244                         break;
245                 case 4:
246                         type = keys->publickey->data[5];
247                         length = (keys->publickey->data[6] << 8) +
248                                         keys->publickey->data[7];
249                         break;
250                 default:
251                         fprintf(stderr, "Unknown key type: %d\n",
252                                 keys->publickey->data[0]);
253                 }
254                 
255                 printf("pub  %4d%c/%08X %04d/%02d/%02d ",
256                         length,
257                         (type == 1) ? 'R' : ((type == 17) ? 'D' : '?'),
258                         (uint32_t) (get_keyid(keys) & 0xFFFFFFFF),
259                         created->tm_year + 1900,
260                         created->tm_mon + 1,
261                         created->tm_mday);
262
263                 curuid = keys->uids;
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);
269                         if (verbose) {
270
271                                 list_sigs(curuid->sigs, html);
272                         }
273                         curuid = curuid->next;
274                 } else {
275                         putchar('\n');
276                 }
277
278                 list_uids(curuid, verbose, html);
279
280                 //TODO: List subkeys.
281
282                 keys = keys->next;
283         }
284
285         if (html) {
286                 puts("</pre>");
287         }
288
289         return 0;
290 }
291
292
293 int get_subpackets_keyid(unsigned char *data, uint64_t *keyid)
294 {
295         int offset = 0;
296         int length = 0;
297         int packetlen = 0;
298
299         assert(data != NULL);
300
301         length = (data[0] << 8) + data[1] + 2;
302
303         offset = 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++];
311                         packetlen <<= 8;
312                         packetlen = data[offset++];
313                         packetlen <<= 8;
314                         packetlen = data[offset++];
315                         packetlen <<= 8;
316                         packetlen = data[offset++];
317                 }
318                 switch (data[offset]) {
319                 case 2:
320                         /*
321                          * Signature creation time. Might want to output this?
322                          */
323                         break;
324                 case 16:
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;
330                         break;
331                 default:
332                         /*
333                          * We don't care about unrecognized packets unless bit
334                          * 7 is set in which case we prefer an error than
335                          * ignoring it.
336                          */
337                         assert(!(data[offset] & 0x80));
338                 }
339                 offset += packetlen;
340         }
341
342         return length;
343 }
344
345
346 /**
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.
350  *
351  *      Returns a linked list of stats_key elements containing the sigs on the
352  *      supplied OpenPGP packet list.
353  */
354 struct ll *keysigs(struct ll *curll,
355                 struct openpgp_packet_list *sigs)
356 {
357         int length = 0;
358         uint64_t keyid = 0;
359         
360         while (sigs != NULL) {
361                 keyid = 0;
362                 switch (sigs->packet->data[0]) {
363                 case 2:
364                 case 3:
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];
369                         keyid &= 0xFFFFFFFF;
370                         break;
371                 case 4:
372                         length = get_subpackets_keyid(&sigs->packet->data[4],
373                                         &keyid);
374                         get_subpackets_keyid(&sigs->packet->data[length + 4],
375                                         &keyid);
376                         /*
377                          * Don't bother to look at the unsigned packets.
378                          */
379                         break;
380                 default:
381                         break;
382                 }
383                 sigs = sigs->next;
384                 curll = lladd(curll, createandaddtohash(keyid));
385         }
386
387         return curll;
388 }