]> git.sommitrealweird.co.uk Git - onak.git/blob - keyindex.c
cscvs to tla changeset 4
[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(
59                                 ((uint64_t) data[offset+packetlen - 8] << 56) +
60                                 ((uint64_t) data[offset+packetlen - 7] << 48) +
61                                 ((uint64_t) data[offset+packetlen - 6] << 40) +
62                                 ((uint64_t) data[offset+packetlen - 5] << 32) +
63                                 ((uint64_t) data[offset+packetlen - 4] << 24) +
64                                 ((uint64_t) data[offset+packetlen - 3] << 16) +
65                                 ((uint64_t) data[offset+packetlen - 2] << 8) +
66                                 data[offset+packetlen - 1]);
67                         if (html && uid != NULL) {
68                                 printf("sig         <a href=\"lookup?op=get&"
69                                         "search=%02X%02X%02X%02X\">"
70                                         "%02X%02X%02X%02X</a>             "
71                                         "<a href=\"lookup?op=vindex&"
72                                         "search=0x%02X%02X%02X%02X\">"
73                                         "%s</a>\n",
74                                         data[offset+packetlen - 4],
75                                         data[offset+packetlen - 3],
76                                         data[offset+packetlen - 2],
77                                         data[offset+packetlen - 1],
78                                         data[offset+packetlen - 4],
79                                         data[offset+packetlen - 3],
80                                         data[offset+packetlen - 2],
81                                         data[offset+packetlen - 1],
82
83                                         data[offset+packetlen - 4],
84                                         data[offset+packetlen - 3],
85                                         data[offset+packetlen - 2],
86                                         data[offset+packetlen - 1],
87                                         txt2html(uid));
88                         } else if (html && uid == NULL) {
89                                 printf("sig         "
90                                         "%02X%02X%02X%02X             "
91                                         "[User id not found]\n",
92                                         data[offset+packetlen - 4],
93                                         data[offset+packetlen - 3],
94                                         data[offset+packetlen - 2],
95                                         data[offset+packetlen - 1]);
96                         } else {
97                                 printf("sig         %02X%02X%02X%02X"
98                                                 "             %s\n",
99                                         data[offset+packetlen - 4],
100                                         data[offset+packetlen - 3],
101                                         data[offset+packetlen - 2],
102                                         data[offset+packetlen - 1],
103                                         (uid != NULL) ? uid :
104                                         "[User id not found]");
105                         }
106                         break;
107                 default:
108                         /*
109                          * We don't care about unrecognized packets unless bit
110                          * 7 is set in which case we prefer an error than
111                          * ignoring it.
112                          */
113                         assert(!(data[offset] & 0x80));
114                 }
115                 offset += packetlen;
116         }
117
118         return length;
119 }
120
121 int list_sigs(struct openpgp_packet_list *sigs, bool html)
122 {
123         int length = 0;
124         char *uid;
125
126         while (sigs != NULL) {
127                 switch (sigs->packet->data[0]) {
128                 case 2:
129                 case 3:
130                         uid = keyid2uid(
131                                 ((uint64_t) sigs->packet->data[7] << 56) +
132                                 ((uint64_t) sigs->packet->data[8] << 48) +
133                                 ((uint64_t) sigs->packet->data[9] << 40) +
134                                 ((uint64_t) sigs->packet->data[10] << 32) +
135                                 ((uint64_t) sigs->packet->data[11] << 24) +
136                                 ((uint64_t) sigs->packet->data[12] << 16) +
137                                 ((uint64_t) sigs->packet->data[13] << 8) +
138                                 sigs->packet->data[14]);
139                         if (html && uid != NULL) {
140                                 printf("sig         <a href=\"lookup?op=get&"
141                                         "search=%02X%02X%02X%02X\">"
142                                         "%02X%02X%02X%02X</a>             "
143                                         "<a href=\"lookup?op=vindex&"
144                                         "search=0x%02X%02X%02X%02X\">"
145                                         "%s</a>\n",
146                                         sigs->packet->data[11],
147                                         sigs->packet->data[12],
148                                         sigs->packet->data[13],
149                                         sigs->packet->data[14],
150                                         sigs->packet->data[11],
151                                         sigs->packet->data[12],
152                                         sigs->packet->data[13],
153                                         sigs->packet->data[14],
154
155                                         sigs->packet->data[11],
156                                         sigs->packet->data[12],
157                                         sigs->packet->data[13],
158                                         sigs->packet->data[14],
159                                         txt2html(uid));
160                         } else if (html && uid == NULL) {
161                                 printf("sig         %02X%02X%02X%02X"
162                                         "             "
163                                         "[User id not found]\n",
164                                         sigs->packet->data[11],
165                                         sigs->packet->data[12],
166                                         sigs->packet->data[13],
167                                         sigs->packet->data[14]);
168                         } else {
169                                 printf("sig         %02X%02X%02X%02X"
170                                                 "             %s\n",
171                                         sigs->packet->data[11],
172                                         sigs->packet->data[12],
173                                         sigs->packet->data[13],
174                                         sigs->packet->data[14],
175                                         (uid != NULL) ? uid :
176                                         "[User id not found]");
177                         }
178                         break;
179                 case 4:
180                         length = parse_subpackets(&sigs->packet->data[4], html);
181                         parse_subpackets(&sigs->packet->data[length + 4], html);
182                         break;
183                 default:
184                         printf("sig        [Unknown packet version %d]",
185                                         sigs->packet->data[0]);
186                 }
187                 sigs = sigs->next;
188         }
189
190         return 0;
191 }
192
193 int list_uids(struct openpgp_signedpacket_list *uids, bool verbose, bool html)
194 {
195         char buf[1024];
196
197         while (uids != NULL) {
198                 if (uids->packet->tag == 13) {
199                         snprintf(buf, 1023, "%.*s",
200                                 (int) uids->packet->length,
201                                 uids->packet->data);
202                         printf("uid                             %s\n",
203                                 (html) ? txt2html(buf) : buf);
204                 } else if (uids->packet->tag == 17) {
205                         printf("uid                             "
206                                 "[photo id]\n");
207                 }
208                 if (verbose) {
209                         list_sigs(uids->sigs, html);
210                 }
211                 uids = uids->next;
212         }
213
214         return 0;
215 }
216
217 /**
218  *      key_index - List a set of OpenPGP keys.
219  *      @keys: The keys to display.
220  *      @verbose: Should we list sigs as well?
221  *      @fingerprint: List the fingerprint?
222  *      @html: Should the output be tailored for HTML?
223  *
224  *      This function takes a list of OpenPGP public keys and displays an index
225  *      of them. Useful for debugging or the keyserver Index function.
226  */
227 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
228                         bool html)
229 {
230         struct openpgp_signedpacket_list        *curuid = NULL;
231         struct tm                               *created = NULL;
232         time_t                                   created_time = 0;
233         int                                      type = 0;
234         int                                      length = 0;
235         char                                     buf[1024];
236
237         if (html) {
238                 puts("<pre>");
239         }
240         puts("Type   bits/keyID    Date       User ID");
241         while (keys != NULL) {
242                 created_time = (keys->publickey->data[1] << 24) +
243                                         (keys->publickey->data[2] << 16) +
244                                         (keys->publickey->data[3] << 8) +
245                                         keys->publickey->data[4];
246                 created = gmtime(&created_time);
247
248                 switch (keys->publickey->data[0]) {
249                 case 2:
250                 case 3:
251                         type = keys->publickey->data[7];
252                         length = (keys->publickey->data[8] << 8) +
253                                         keys->publickey->data[9];
254                         break;
255                 case 4:
256                         type = keys->publickey->data[5];
257                         length = (keys->publickey->data[6] << 8) +
258                                         keys->publickey->data[7];
259                         break;
260                 default:
261                         fprintf(stderr, "Unknown key type: %d\n",
262                                 keys->publickey->data[0]);
263                 }
264                 
265                 printf("pub  %5d%c/%08X %04d/%02d/%02d ",
266                         length,
267                         (type == 1) ? 'R' : ((type == 17) ? 'D' : '?'),
268                         (uint32_t) (get_keyid(keys) & 0xFFFFFFFF),
269                         created->tm_year + 1900,
270                         created->tm_mon + 1,
271                         created->tm_mday);
272
273                 curuid = keys->uids;
274                 if (curuid != NULL && curuid->packet->tag == 13) {
275                         snprintf(buf, 1023, "%.*s",
276                                 (int) curuid->packet->length,
277                                 curuid->packet->data);
278                         printf("%s\n", (html) ? txt2html(buf) : buf);
279                         if (verbose) {
280                                 list_sigs(curuid->sigs, html);
281                         }
282                         curuid = curuid->next;
283                 } else {
284                         putchar('\n');
285                 }
286
287                 list_uids(curuid, verbose, html);
288
289                 //TODO: List subkeys.
290
291                 keys = keys->next;
292         }
293
294         if (html) {
295                 puts("</pre>");
296         }
297
298         return 0;
299 }
300
301
302 int get_subpackets_keyid(unsigned char *data, uint64_t *keyid)
303 {
304         int offset = 0;
305         int length = 0;
306         int packetlen = 0;
307
308         assert(data != NULL);
309
310         length = (data[0] << 8) + data[1] + 2;
311
312         offset = 2;
313         while (offset < length) {
314                 packetlen = data[offset++];
315                 if (packetlen > 191 && packetlen < 255) {
316                         packetlen = ((packetlen - 192) << 8) +
317                                         data[offset++] + 192;
318                 } else if (packetlen == 255) {
319                         packetlen = data[offset++];
320                         packetlen <<= 8;
321                         packetlen = data[offset++];
322                         packetlen <<= 8;
323                         packetlen = data[offset++];
324                         packetlen <<= 8;
325                         packetlen = data[offset++];
326                 }
327                 switch (data[offset]) {
328                 case 2:
329                         /*
330                          * Signature creation time. Might want to output this?
331                          */
332                         break;
333                 case 0x83:
334                         /*
335                          * Signature expiration time. Might want to output this?
336                          */
337                         break;
338                 case 16:
339                         *keyid = data[offset+packetlen - 8];
340                         *keyid <<= 8;
341                         *keyid += data[offset+packetlen - 7];
342                         *keyid <<= 8;
343                         *keyid += data[offset+packetlen - 6];
344                         *keyid <<= 8;
345                         *keyid += data[offset+packetlen - 5];
346                         *keyid <<= 8;
347                         *keyid += data[offset+packetlen - 4];
348                         *keyid <<= 8;
349                         *keyid += data[offset+packetlen - 3];
350                         *keyid <<= 8;
351                         *keyid += data[offset+packetlen - 2];
352                         *keyid <<= 8;
353                         *keyid += data[offset+packetlen - 1];
354                         break;
355                 default:
356                         /*
357                          * We don't care about unrecognized packets unless bit
358                          * 7 is set in which case we prefer an error than
359                          * ignoring it.
360                          */
361                         assert(!(data[offset] & 0x80));
362                 }
363                 offset += packetlen;
364         }
365
366         return length;
367 }
368
369
370 /**
371  *      keysigs - Return the sigs on a given OpenPGP signature list.
372  *      @curll: The current linked list. Can be NULL to create a new list.
373  *      @sigs: The signature list we want the sigs on.
374  *
375  *      Returns a linked list of stats_key elements containing the sigs on the
376  *      supplied OpenPGP packet list.
377  */
378 struct ll *keysigs(struct ll *curll,
379                 struct openpgp_packet_list *sigs)
380 {
381         int length = 0;
382         uint64_t keyid = 0;
383         
384         while (sigs != NULL) {
385                 keyid = 0;
386                 switch (sigs->packet->data[0]) {
387                 case 2:
388                 case 3:
389                         keyid = sigs->packet->data[7];
390                         keyid <<= 8;
391                         keyid += sigs->packet->data[8];
392                         keyid <<= 8;
393                         keyid += sigs->packet->data[9];
394                         keyid <<= 8;
395                         keyid += sigs->packet->data[10];
396                         keyid <<= 8;
397                         keyid += sigs->packet->data[11];
398                         keyid <<= 8;
399                         keyid += sigs->packet->data[12];
400                         keyid <<= 8;
401                         keyid += sigs->packet->data[13];
402                         keyid <<= 8;
403                         keyid += sigs->packet->data[14];
404                         break;
405                 case 4:
406                         length = get_subpackets_keyid(&sigs->packet->data[4],
407                                         &keyid);
408                         get_subpackets_keyid(&sigs->packet->data[length + 4],
409                                         &keyid);
410                         /*
411                          * Don't bother to look at the unsigned packets.
412                          */
413                         break;
414                 default:
415                         break;
416                 }
417                 sigs = sigs->next;
418                 curll = lladd(curll, createandaddtohash(keyid));
419         }
420
421         return curll;
422 }
423
424 /*
425  * TODO: Abstract out; all our linked lists should be generic and then we can
426  * llsize them.
427  */
428 int spsize(struct openpgp_signedpacket_list *list)
429 {
430         int size = 0;
431         struct openpgp_signedpacket_list *cur;
432
433         for (cur = list; cur != NULL; cur = cur->next, size++) ;
434
435         return size;
436 }
437
438 /**
439  *      keyuids - Takes a key and returns an array of its UIDs
440  *      @key: The key to get the uids of.
441  *      @primary: A pointer to store the primary UID in.
442  *
443  *      keyuids takes a public key structure and builds an array of the UIDs 
444  *      on the key. It also attempts to work out the primary UID and returns a
445  *      separate pointer to that particular element of the array.
446  */
447 char **keyuids(struct openpgp_publickey *key, char **primary)
448 {
449         struct openpgp_signedpacket_list *curuid = NULL;
450         char buf[1024];
451         char **uids = NULL;
452         int count = 0;
453
454         if (key != NULL && key->uids != NULL) {
455                 uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
456         
457                 curuid = key->uids;
458                 while (curuid != NULL) {
459                         buf[0] = 0;
460                         if (curuid->packet->tag == 13) {
461                                 snprintf(buf, 1023, "%.*s",
462                                                 (int) curuid->packet->length,
463                                                 curuid->packet->data);
464                                 uids[count++] = strdup(buf);
465                         }
466                         curuid = curuid -> next;
467                 }
468                 uids[count] = NULL;
469         }
470         /*
471          * TODO: Parse subpackets for real primary ID (v4 keys)
472          */
473         if (primary != NULL) {
474                 *primary = uids[0];
475         }
476
477         return uids;
478 }