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