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