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