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