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