]> git.sommitrealweird.co.uk Git - onak.git/blob - keyindex.c
6aab5d3c73da891faeebe6d3262d3420a464fead
[onak.git] / keyindex.c
1 /*
2  * keyindex.c - Routines to list an OpenPGP key.
3  *
4  * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
5  */
6
7 #include <inttypes.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13
14 #include "decodekey.h"
15 #include "getcgi.h"
16 #include "hash.h"
17 #include "keydb.h"
18 #include "keyid.h"
19 #include "keyindex.h"
20 #include "keystructs.h"
21 #include "log.h"
22 #include "onak-conf.h"
23 #include "openpgp.h"
24
25 int list_sigs(struct openpgp_packet_list *sigs, bool html)
26 {
27         char *uid = NULL;
28         uint64_t sigid = 0;
29         char *sig = NULL;
30
31         while (sigs != NULL) {
32                 sigid = sig_keyid(sigs->packet);
33                 uid = config.dbbackend->keyid2uid(sigid);
34                 if (sigs->packet->data[0] == 4 &&
35                                 sigs->packet->data[1] == 0x30) {
36                         /* It's a Type 4 sig revocation */
37                         sig = "rev";
38                 } else {
39                         sig = "sig";
40                 }
41                 if (html && uid != NULL) {
42                         printf("%s         <a href=\"lookup?op=get&"
43                                 "search=%016" PRIX64 "\">%08" PRIX64
44                                 "</a>             "
45                                 "<a href=\"lookup?op=vindex&search=0x%016"
46                                 PRIX64 "\">%s</a>\n",
47                                 sig,
48                                 sigid,
49                                 sigid & 0xFFFFFFFF,
50                                 sigid,
51                                 txt2html(uid));
52                 } else if (html && uid == NULL) {
53                         printf("%s         %08" PRIX64 "             "
54                                 "[User id not found]\n",
55                                 sig,
56                                 sigid & 0xFFFFFFFF);
57                 } else {
58                         printf("%s         %08" PRIX64
59                                 "             %s\n",
60                                 sig,
61                                 sigid & 0xFFFFFFFF,
62                                 (uid != NULL) ? uid :
63                                 "[User id not found]");
64                 }
65                 if (uid != NULL) {
66                         free(uid);
67                         uid = NULL;
68                 }
69                 sigs = sigs->next;
70         }
71
72         return 0;
73 }
74
75 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
76                 bool verbose, bool html)
77 {
78         char buf[1024];
79         int  imgindx = 0;
80
81         while (uids != NULL) {
82                 if (uids->packet->tag == OPENPGP_PACKET_UID) {
83                         snprintf(buf, 1023, "%.*s",
84                                 (int) uids->packet->length,
85                                 uids->packet->data);
86                         printf("                                %s\n",
87                                 (html) ? txt2html(buf) : buf);
88                 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
89                         printf("                                ");
90                         if (html) {
91                                 printf("<img src=\"lookup?op=photo&search="
92                                         "0x%016" PRIX64 "&idx=%d\" alt=\""
93                                         "[photo id]\">\n",
94                                         keyid,
95                                         imgindx);
96                                 imgindx++;
97                         } else {
98                                 printf("[photo id]\n");
99                         }
100                 }
101                 if (verbose) {
102                         list_sigs(uids->sigs, html);
103                 }
104                 uids = uids->next;
105         }
106
107         return 0;
108 }
109
110 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
111                 bool html)
112 {
113         struct tm       *created = NULL;
114         time_t          created_time = 0;
115         int             type = 0;
116         int             length = 0;
117
118         while (subkeys != NULL) {
119                 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
120
121                         created_time = (subkeys->packet->data[1] << 24) +
122                                         (subkeys->packet->data[2] << 16) +
123                                         (subkeys->packet->data[3] << 8) +
124                                         subkeys->packet->data[4];
125                         created = gmtime(&created_time);
126
127                         switch (subkeys->packet->data[0]) {
128                         case 2:
129                         case 3:
130                                 type = subkeys->packet->data[7];
131                                 length = (subkeys->packet->data[8] << 8) +
132                                         subkeys->packet->data[9];
133                                 break;
134                         case 4:
135                                 type = subkeys->packet->data[5];
136                                 length = (subkeys->packet->data[6] << 8) +
137                                         subkeys->packet->data[7];
138                                 break;
139                         default:
140                                 logthing(LOGTHING_ERROR,
141                                         "Unknown key type: %d",
142                                         subkeys->packet->data[0]);
143                         }
144                 
145                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
146                                 length,
147                                 (type == OPENPGP_PKALGO_RSA) ? 'R' :
148                                 ((type == OPENPGP_PKALGO_ELGAMAL) ? 'g' :
149                                 ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
150                                 (uint32_t) (get_packetid(subkeys->packet) &
151                                             0xFFFFFFFF),
152                                 created->tm_year + 1900,
153                                 created->tm_mon + 1,
154                                 created->tm_mday);
155
156                 }
157                 if (verbose) {
158                         list_sigs(subkeys->sigs, html);
159                 }
160                 subkeys = subkeys->next;
161         }
162
163         return 0;
164 }
165
166 void display_fingerprint(struct openpgp_publickey *key)
167 {
168         int             i = 0;
169         size_t          length = 0;
170         unsigned char   fp[20];
171
172         get_fingerprint(key->publickey, fp, &length);
173         printf("      Key fingerprint =");
174         for (i = 0; i < length; i++) {
175                 if ((length == 16) ||
176                         (i % 2 == 0)) {
177                         printf(" ");
178                 }
179                 printf("%02X", fp[i]);
180                 if ((i * 2) == length) {
181                         printf(" ");
182                 }
183         }
184         printf("\n");
185
186         return;
187 }
188
189 void display_skshash(struct openpgp_publickey *key, bool html)
190 {
191         int             i = 0;
192         struct skshash  hash;
193
194         get_skshash(key, &hash);
195         printf("      Key hash = ");
196         if (html) {
197                 printf("<a href=\"lookup?op=hget&search=");
198                 for (i = 0; i < sizeof(hash.hash); i++) {
199                         printf("%02X", hash.hash[i]);
200                 }
201                 printf("\">");
202         }
203         for (i = 0; i < sizeof(hash.hash); i++) {
204                 printf("%02X", hash.hash[i]);
205         }
206         if (html) {
207                 printf("</a>");
208         }
209         printf("\n");
210
211         return;
212 }
213
214 /**
215  *      key_index - List a set of OpenPGP keys.
216  *      @keys: The keys to display.
217  *      @verbose: Should we list sigs as well?
218  *      @fingerprint: List the fingerprint?
219  *      @html: Should the output be tailored for HTML?
220  *
221  *      This function takes a list of OpenPGP public keys and displays an index
222  *      of them. Useful for debugging or the keyserver Index function.
223  */
224 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
225                         bool skshash, bool html)
226 {
227         struct openpgp_signedpacket_list        *curuid = NULL;
228         struct tm                               *created = NULL;
229         time_t                                   created_time = 0;
230         int                                      type = 0;
231         char                                     typech;
232         int                                      length = 0;
233         char                                     buf[1024];
234         uint64_t                                 keyid;
235
236         if (html) {
237                 puts("<pre>");
238         }
239         puts("Type   bits/keyID    Date       User ID");
240         while (keys != NULL) {
241                 created_time = (keys->publickey->data[1] << 24) +
242                                         (keys->publickey->data[2] << 16) +
243                                         (keys->publickey->data[3] << 8) +
244                                         keys->publickey->data[4];
245                 created = gmtime(&created_time);
246
247                 switch (keys->publickey->data[0]) {
248                 case 2:
249                 case 3:
250                         type = keys->publickey->data[7];
251                         length = (keys->publickey->data[8] << 8) +
252                                         keys->publickey->data[9];
253                         break;
254                 case 4:
255                         type = keys->publickey->data[5];
256                         length = (keys->publickey->data[6] << 8) +
257                                         keys->publickey->data[7];
258                         break;
259                 default:
260                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
261                                 keys->publickey->data[0]);
262                 }
263                 
264                 keyid = get_keyid(keys);
265
266                 switch (type) {
267                 case OPENPGP_PKALGO_RSA:
268                         typech = 'R';
269                         break;
270                 case OPENPGP_PKALGO_ELGAMAL:
271                         typech = 'g';
272                         break;
273                 case OPENPGP_PKALGO_DSA:
274                         typech = 'D';
275                         break;
276                 case OPENPGP_PKALGO_ELGAMAL_SIGN:
277                         typech = 'G';
278                         break;
279                 default:
280                         typech = '?';
281                         break;
282                 }
283
284                 if (html) {
285                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
286                                 "search=%016" PRIX64 "\">%08" PRIX64
287                                 "</a> %04d/%02d/%02d ",
288                                 length,
289                                 typech,
290                                 keyid,
291                                 keyid & 0xFFFFFFFF,
292                                 created->tm_year + 1900,
293                                 created->tm_mon + 1,
294                                 created->tm_mday);
295                 } else {
296                         printf("pub  %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
297                                 length,
298                                 typech,
299                                 keyid & 0xFFFFFFFF,
300                                 created->tm_year + 1900,
301                                 created->tm_mon + 1,
302                                 created->tm_mday);
303                 }
304
305                 curuid = keys->uids;
306                 if (curuid != NULL &&
307                                 curuid->packet->tag == OPENPGP_PACKET_UID) {
308                         snprintf(buf, 1023, "%.*s",
309                                 (int) curuid->packet->length,
310                                 curuid->packet->data);
311                         if (html) {
312                                 printf("<a href=\"lookup?op=vindex&"
313                                         "search=0x%016" PRIX64 "\">",
314                                         keyid);
315                         }
316                         printf("%s%s%s\n", 
317                                 (html) ? txt2html(buf) : buf,
318                                 (html) ? "</a>" : "",
319                                 (keys->revoked) ? " *** REVOKED ***" : "");
320                         if (skshash) {
321                                 display_skshash(keys, html);
322                         }
323                         if (fingerprint) {
324                                 display_fingerprint(keys);
325                         }
326                         if (verbose) {
327                                 list_sigs(curuid->sigs, html);
328                         }
329                         curuid = curuid->next;
330                 } else {
331                         printf("%s\n", 
332                                 (keys->revoked) ? "*** REVOKED ***": "");
333                         if (fingerprint) {
334                                 display_fingerprint(keys);
335                         }
336                 }
337
338                 list_uids(keyid, curuid, verbose, html);
339                 if (verbose) {
340                         list_subkeys(keys->subkeys, verbose, html);
341                 }
342
343                 keys = keys->next;
344         }
345
346         if (html) {
347                 puts("</pre>");
348         }
349
350         return 0;
351 }
352
353 /**
354  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
355  *      @keys: The keys to display.
356  *
357  *      This function takes a list of OpenPGP public keys and displays a
358  *      machine readable list of them.
359  */
360 int mrkey_index(struct openpgp_publickey *keys)
361 {
362         struct openpgp_signedpacket_list        *curuid = NULL;
363         time_t                                   created_time = 0;
364         int                                      type = 0;
365         int                                      length = 0;
366         int                                      i = 0;
367         size_t                                   fplength = 0;
368         unsigned char                            fp[20];
369         int                                      c;
370
371         while (keys != NULL) {
372                 created_time = (keys->publickey->data[1] << 24) +
373                                         (keys->publickey->data[2] << 16) +
374                                         (keys->publickey->data[3] << 8) +
375                                         keys->publickey->data[4];
376
377                 printf("pub:");
378
379                 switch (keys->publickey->data[0]) {
380                 case 2:
381                 case 3:
382                         printf("%016" PRIX64, get_keyid(keys));
383                         type = keys->publickey->data[7];
384                         length = (keys->publickey->data[8] << 8) +
385                                         keys->publickey->data[9];
386                         break;
387                 case 4:
388                         (void) get_fingerprint(keys->publickey, fp, &fplength);
389
390                         for (i = 0; i < fplength; i++) {
391                                 printf("%02X", fp[i]);
392                         }
393
394                         type = keys->publickey->data[5];
395                         length = (keys->publickey->data[6] << 8) +
396                                         keys->publickey->data[7];
397                         break;
398                 default:
399                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
400                                 keys->publickey->data[0]);
401                 }
402
403                 printf(":%d:%d:%ld::%s\n",
404                         type,
405                         length,
406                         created_time,
407                         (keys->revoked) ? "r" : "");
408         
409                 for (curuid = keys->uids; curuid != NULL;
410                          curuid = curuid->next) {
411                 
412                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
413                                 printf("uid:");
414                                 for (i = 0; i < (int) curuid->packet->length;
415                                                 i++) {
416                                         c = curuid->packet->data[i];
417                                         if (c == '%') {
418                                                 putchar('%');
419                                                 putchar(c);
420                                         } else if (c == ':' || c > 127) {
421                                                 printf("%%%X", c);
422                                         } else {
423                                                 putchar(c);
424                                         }
425                                 }
426                                 printf("\n");
427                         }
428                 }
429                 keys = keys->next;
430         }
431         return 0;
432 }