cscvs to tla changeset 52
[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
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(struct openpgp_signedpacket_list *uids, bool verbose, bool html)
64 {
65         char buf[1024];
66
67         while (uids != NULL) {
68                 if (uids->packet->tag == 13) {
69                         snprintf(buf, 1023, "%.*s",
70                                 (int) uids->packet->length,
71                                 uids->packet->data);
72                         printf("uid                             %s\n",
73                                 (html) ? txt2html(buf) : buf);
74                 } else if (uids->packet->tag == 17) {
75                         printf("uid                             "
76                                 "[photo id]\n");
77                 }
78                 if (verbose) {
79                         list_sigs(uids->sigs, html);
80                 }
81                 uids = uids->next;
82         }
83
84         return 0;
85 }
86
87 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
88                 bool html)
89 {
90         struct tm       *created = NULL;
91         time_t          created_time = 0;
92         int             type = 0;
93         int             length = 0;
94
95         while (subkeys != NULL) {
96                 if (subkeys->packet->tag == 14) {
97
98                         created_time = (subkeys->packet->data[1] << 24) +
99                                         (subkeys->packet->data[2] << 16) +
100                                         (subkeys->packet->data[3] << 8) +
101                                         subkeys->packet->data[4];
102                         created = gmtime(&created_time);
103
104                         switch (subkeys->packet->data[0]) {
105                         case 2:
106                         case 3:
107                                 type = subkeys->packet->data[7];
108                                 length = (subkeys->packet->data[8] << 8) +
109                                         subkeys->packet->data[9];
110                                 break;
111                         case 4:
112                                 type = subkeys->packet->data[5];
113                                 length = (subkeys->packet->data[6] << 8) +
114                                         subkeys->packet->data[7];
115                                 break;
116                         default:
117                                 fprintf(stderr, "Unknown key type: %d\n",
118                                         subkeys->packet->data[0]);
119                         }
120                 
121                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
122                                 length,
123                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
124                                         ((type == 17) ? 'D' : '?')),
125                                 (uint32_t) (get_packetid(subkeys->packet) &
126                                             0xFFFFFFFF),
127                                 created->tm_year + 1900,
128                                 created->tm_mon + 1,
129                                 created->tm_mday);
130
131                 }
132                 if (verbose) {
133                         list_sigs(subkeys->sigs, html);
134                 }
135                 subkeys = subkeys->next;
136         }
137
138         return 0;
139 }
140
141 void display_fingerprint(struct openpgp_publickey *key)
142 {
143         int             i = 0;
144         size_t          length = 0;
145         unsigned char   fp[20];
146
147         get_fingerprint(key->publickey, fp, &length);
148         printf("      Key fingerprint =");
149         for (i = 0; i < length; i++) {
150                 if ((length == 16) ||
151                         (i % 2 == 0)) {
152                         printf(" ");
153                 }
154                 printf("%02X", fp[i]);
155                 if ((i * 2) == length) {
156                         printf(" ");
157                 }
158         }
159         printf("\n");
160
161         return;
162 }
163
164 /**
165  *      key_index - List a set of OpenPGP keys.
166  *      @keys: The keys to display.
167  *      @verbose: Should we list sigs as well?
168  *      @fingerprint: List the fingerprint?
169  *      @html: Should the output be tailored for HTML?
170  *
171  *      This function takes a list of OpenPGP public keys and displays an index
172  *      of them. Useful for debugging or the keyserver Index function.
173  */
174 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
175                         bool html)
176 {
177         struct openpgp_signedpacket_list        *curuid = NULL;
178         struct tm                               *created = NULL;
179         time_t                                   created_time = 0;
180         int                                      type = 0;
181         int                                      length = 0;
182         char                                     buf[1024];
183
184         if (html) {
185                 puts("<pre>");
186         }
187         puts("Type   bits/keyID    Date       User ID");
188         while (keys != NULL) {
189                 created_time = (keys->publickey->data[1] << 24) +
190                                         (keys->publickey->data[2] << 16) +
191                                         (keys->publickey->data[3] << 8) +
192                                         keys->publickey->data[4];
193                 created = gmtime(&created_time);
194
195                 switch (keys->publickey->data[0]) {
196                 case 2:
197                 case 3:
198                         type = keys->publickey->data[7];
199                         length = (keys->publickey->data[8] << 8) +
200                                         keys->publickey->data[9];
201                         break;
202                 case 4:
203                         type = keys->publickey->data[5];
204                         length = (keys->publickey->data[6] << 8) +
205                                         keys->publickey->data[7];
206                         break;
207                 default:
208                         fprintf(stderr, "Unknown key type: %d\n",
209                                 keys->publickey->data[0]);
210                 }
211                 
212                 printf("pub  %5d%c/%08X %04d/%02d/%02d ",
213                         length,
214                         (type == 1) ? 'R' : ((type == 16) ? 'g' : 
215                                 ((type == 17) ? 'D' : '?')),
216                         (uint32_t) (get_keyid(keys) & 0xFFFFFFFF),
217                         created->tm_year + 1900,
218                         created->tm_mon + 1,
219                         created->tm_mday);
220
221                 curuid = keys->uids;
222                 if (curuid != NULL && curuid->packet->tag == 13) {
223                         snprintf(buf, 1023, "%.*s",
224                                 (int) curuid->packet->length,
225                                 curuid->packet->data);
226                         printf("%s\n", (html) ? txt2html(buf) : buf);
227                         if (fingerprint) {
228                                 display_fingerprint(keys);
229                         }
230                         if (verbose) {
231                                 list_sigs(curuid->sigs, html);
232                         }
233                         curuid = curuid->next;
234                 } else {
235                         putchar('\n');
236                         if (fingerprint) {
237                                 display_fingerprint(keys);
238                         }
239                 }
240
241                 list_uids(curuid, verbose, html);
242                 list_subkeys(keys->subkeys, verbose, html);
243
244                 keys = keys->next;
245         }
246
247         if (html) {
248                 puts("</pre>");
249         }
250
251         return 0;
252 }