]> git.sommitrealweird.co.uk Git - onak.git/blob - keyindex.c
cscvs to tla changeset 79
[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.10 2003/06/04 22:11:40 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\n", (html) ? txt2html(buf) : buf);
231                         if (fingerprint) {
232                                 display_fingerprint(keys);
233                         }
234                         if (verbose) {
235                                 list_sigs(curuid->sigs, html);
236                         }
237                         curuid = curuid->next;
238                 } else {
239                         putchar('\n');
240                         if (fingerprint) {
241                                 display_fingerprint(keys);
242                         }
243                 }
244
245                 list_uids(curuid, verbose, html);
246                 list_subkeys(keys->subkeys, verbose, html);
247
248                 keys = keys->next;
249         }
250
251         if (html) {
252                 puts("</pre>");
253         }
254
255         return 0;
256 }
257
258 /**
259  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
260  *      @keys: The keys to display.
261  *
262  *      This function takes a list of OpenPGP public keys and displays a
263  *      machine readable list of them.
264  */
265 int mrkey_index(struct openpgp_publickey *keys)
266 {
267         struct openpgp_signedpacket_list        *curuid = NULL;
268         time_t                                   created_time = 0;
269         int                                      type = 0;
270         int                                      length = 0;
271         int                                      i = 0;
272         size_t                                   fplength = 0;
273         unsigned char                            fp[20];
274
275
276
277         while (keys != NULL) {
278                 created_time = (keys->publickey->data[1] << 24) +
279                                         (keys->publickey->data[2] << 16) +
280                                         (keys->publickey->data[3] << 8) +
281                                         keys->publickey->data[4];
282
283                 printf("pub:");
284
285                 switch (keys->publickey->data[0]) {
286                 case 2:
287                 case 3:
288                         printf("%016llX", get_keyid(keys));
289                         type = keys->publickey->data[7];
290                         length = (keys->publickey->data[8] << 8) +
291                                         keys->publickey->data[9];
292                         break;
293                 case 4:
294                         get_fingerprint(keys->publickey, fp, &fplength);
295
296                         for (i = 0; i < fplength; i++) {
297                                 printf("%02X", fp[i]);
298                         }
299
300                         type = keys->publickey->data[5];
301                         length = (keys->publickey->data[6] << 8) +
302                                         keys->publickey->data[7];
303                         break;
304                 default:
305                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
306                                 keys->publickey->data[0]);
307                 }
308
309                 printf(":%d:%d:%d::\n",
310                         type,
311                         length,
312                         created_time);
313         
314                 for (curuid = keys->uids; curuid != NULL;
315                          curuid = curuid->next) {
316                 
317                         if (curuid->packet->tag == 13) {
318                                 printf("uid:%.*s\n",
319                                         (int) curuid->packet->length,
320                                         curuid->packet->data);
321                         }
322                 }
323                 keys = keys->next;
324         }
325         return 0;
326 }