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