]> the.earth.li Git - onak.git/blob - keyindex.c
Add support for displaying EC/ECDSA key types
[onak.git] / keyindex.c
1 /*
2  * keyindex.c - Routines to list an OpenPGP key.
3  *
4  * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <inttypes.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "decodekey.h"
28 #include "getcgi.h"
29 #include "hash.h"
30 #include "keydb.h"
31 #include "keyid.h"
32 #include "keyindex.h"
33 #include "keystructs.h"
34 #include "log.h"
35 #include "onak.h"
36 #include "onak-conf.h"
37 #include "openpgp.h"
38
39 /*
40  * Convert a Public Key algorithm to its single character representation.
41  */
42 char pkalgo2char(uint8_t algo)
43 {
44         char typech;
45
46         switch (algo) {
47         case OPENPGP_PKALGO_DSA:
48                 typech = 'D';
49                 break;
50         case OPENPGP_PKALGO_ECDSA:
51                 typech = 'E';
52                 break;
53         case OPENPGP_PKALGO_EC:
54                 typech = 'e';
55                 break;
56         case OPENPGP_PKALGO_ELGAMAL_SIGN:
57                 typech = 'G';
58                 break;
59         case OPENPGP_PKALGO_ELGAMAL_ENC:
60                 typech = 'g';
61                 break;
62         case OPENPGP_PKALGO_RSA:
63                 typech = 'R';
64                 break;
65         case OPENPGP_PKALGO_RSA_ENC:
66                 typech = 'r';
67                 break;
68         case OPENPGP_PKALGO_RSA_SIGN:
69                 typech = 's';
70                 break;
71         default:
72                 typech = '?';
73                 break;
74         }
75
76         return typech;
77 }
78
79 int list_sigs(struct openpgp_packet_list *sigs, bool html)
80 {
81         char *uid = NULL;
82         uint64_t sigid = 0;
83         char *sig = NULL;
84
85         while (sigs != NULL) {
86                 sigid = sig_keyid(sigs->packet);
87                 uid = config.dbbackend->keyid2uid(sigid);
88                 if (sigs->packet->data[0] == 4 &&
89                                 sigs->packet->data[1] == 0x30) {
90                         /* It's a Type 4 sig revocation */
91                         sig = "rev";
92                 } else {
93                         sig = "sig";
94                 }
95                 if (html && uid != NULL) {
96                         printf("%s         <a href=\"lookup?op=get&"
97                                 "search=0x%016" PRIX64 "\">%08" PRIX64
98                                 "</a>             "
99                                 "<a href=\"lookup?op=vindex&search=0x%016"
100                                 PRIX64 "\">%s</a>\n",
101                                 sig,
102                                 sigid,
103                                 sigid & 0xFFFFFFFF,
104                                 sigid,
105                                 txt2html(uid));
106                 } else if (html && uid == NULL) {
107                         printf("%s         %08" PRIX64 "             "
108                                 "[User id not found]\n",
109                                 sig,
110                                 sigid & 0xFFFFFFFF);
111                 } else {
112                         printf("%s         %08" PRIX64
113                                 "             %s\n",
114                                 sig,
115                                 sigid & 0xFFFFFFFF,
116                                 (uid != NULL) ? uid :
117                                 "[User id not found]");
118                 }
119                 if (uid != NULL) {
120                         free(uid);
121                         uid = NULL;
122                 }
123                 sigs = sigs->next;
124         }
125
126         return 0;
127 }
128
129 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
130                 bool verbose, bool html)
131 {
132         char buf[1024];
133         int  imgindx = 0;
134
135         while (uids != NULL) {
136                 if (uids->packet->tag == OPENPGP_PACKET_UID) {
137                         snprintf(buf, 1023, "%.*s",
138                                 (int) uids->packet->length,
139                                 uids->packet->data);
140                         printf("                                %s\n",
141                                 (html) ? txt2html(buf) : buf);
142                 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
143                         printf("                                ");
144                         if (html) {
145                                 printf("<img src=\"lookup?op=photo&search="
146                                         "0x%016" PRIX64 "&idx=%d\" alt=\""
147                                         "[photo id]\">\n",
148                                         keyid,
149                                         imgindx);
150                                 imgindx++;
151                         } else {
152                                 printf("[photo id]\n");
153                         }
154                 }
155                 if (verbose) {
156                         list_sigs(uids->sigs, html);
157                 }
158                 uids = uids->next;
159         }
160
161         return 0;
162 }
163
164 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
165                 bool html)
166 {
167         struct tm       *created = NULL;
168         time_t          created_time = 0;
169         int             type = 0;
170         int             length = 0;
171         uint64_t        keyid = 0;
172
173         while (subkeys != NULL) {
174                 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
175
176                         created_time = (subkeys->packet->data[1] << 24) +
177                                         (subkeys->packet->data[2] << 16) +
178                                         (subkeys->packet->data[3] << 8) +
179                                         subkeys->packet->data[4];
180                         created = gmtime(&created_time);
181
182                         switch (subkeys->packet->data[0]) {
183                         case 2:
184                         case 3:
185                                 type = subkeys->packet->data[7];
186                                 length = (subkeys->packet->data[8] << 8) +
187                                         subkeys->packet->data[9];
188                                 break;
189                         case 4:
190                                 type = subkeys->packet->data[5];
191                                 length = (subkeys->packet->data[6] << 8) +
192                                         subkeys->packet->data[7];
193                                 break;
194                         default:
195                                 logthing(LOGTHING_ERROR,
196                                         "Unknown key type: %d",
197                                         subkeys->packet->data[0]);
198                         }
199
200                         if (get_packetid(subkeys->packet,
201                                         &keyid) != ONAK_E_OK) {
202                                 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
203                         }
204                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
205                                 length,
206                                 pkalgo2char(type),
207                                 (uint32_t) (keyid & 0xFFFFFFFF),
208                                 created->tm_year + 1900,
209                                 created->tm_mon + 1,
210                                 created->tm_mday);
211
212                 }
213                 if (verbose) {
214                         list_sigs(subkeys->sigs, html);
215                 }
216                 subkeys = subkeys->next;
217         }
218
219         return 0;
220 }
221
222 void display_fingerprint(struct openpgp_publickey *key)
223 {
224         int             i = 0;
225         size_t          length = 0;
226         unsigned char   fp[20];
227
228         get_fingerprint(key->publickey, fp, &length);
229         printf("      Key fingerprint =");
230         for (i = 0; i < length; i++) {
231                 if ((length == 16) ||
232                         (i % 2 == 0)) {
233                         printf(" ");
234                 }
235                 if (length == 20 && (i * 2) == length) {
236                         /* Extra space in the middle of a SHA1 fingerprint */
237                         printf(" ");
238                 }
239                 printf("%02X", fp[i]);
240         }
241         printf("\n");
242
243         return;
244 }
245
246 void display_skshash(struct openpgp_publickey *key, bool html)
247 {
248         int             i = 0;
249         struct skshash  hash;
250
251         get_skshash(key, &hash);
252         printf("      Key hash = ");
253         if (html) {
254                 printf("<a href=\"lookup?op=hget&search=");
255                 for (i = 0; i < sizeof(hash.hash); i++) {
256                         printf("%02X", hash.hash[i]);
257                 }
258                 printf("\">");
259         }
260         for (i = 0; i < sizeof(hash.hash); i++) {
261                 printf("%02X", hash.hash[i]);
262         }
263         if (html) {
264                 printf("</a>");
265         }
266         printf("\n");
267
268         return;
269 }
270
271 /**
272  *      key_index - List a set of OpenPGP keys.
273  *      @keys: The keys to display.
274  *      @verbose: Should we list sigs as well?
275  *      @fingerprint: List the fingerprint?
276  *      @html: Should the output be tailored for HTML?
277  *
278  *      This function takes a list of OpenPGP public keys and displays an index
279  *      of them. Useful for debugging or the keyserver Index function.
280  */
281 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
282                         bool skshash, bool html)
283 {
284         struct openpgp_signedpacket_list        *curuid = NULL;
285         struct tm                               *created = NULL;
286         time_t                                   created_time = 0;
287         int                                      type = 0;
288         int                                      length = 0;
289         char                                     buf[1024];
290         uint64_t                                 keyid;
291
292         if (html) {
293                 puts("<pre>");
294         }
295         puts("Type   bits/keyID    Date       User ID");
296         while (keys != NULL) {
297                 created_time = (keys->publickey->data[1] << 24) +
298                                         (keys->publickey->data[2] << 16) +
299                                         (keys->publickey->data[3] << 8) +
300                                         keys->publickey->data[4];
301                 created = gmtime(&created_time);
302
303                 switch (keys->publickey->data[0]) {
304                 case 2:
305                 case 3:
306                         type = keys->publickey->data[7];
307                         length = (keys->publickey->data[8] << 8) +
308                                         keys->publickey->data[9];
309                         break;
310                 case 4:
311                         type = keys->publickey->data[5];
312                         length = (keys->publickey->data[6] << 8) +
313                                         keys->publickey->data[7];
314                         break;
315                 default:
316                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
317                                 keys->publickey->data[0]);
318                 }
319                 
320                 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
321                         logthing(LOGTHING_ERROR, "Couldn't get keyid.");
322                 }
323
324                 if (html) {
325                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
326                                 "search=0x%016" PRIX64 "\">%08" PRIX64
327                                 "</a> %04d/%02d/%02d ",
328                                 length,
329                                 pkalgo2char(type),
330                                 keyid,
331                                 keyid & 0xFFFFFFFF,
332                                 created->tm_year + 1900,
333                                 created->tm_mon + 1,
334                                 created->tm_mday);
335                 } else {
336                         printf("pub  %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
337                                 length,
338                                 pkalgo2char(type),
339                                 keyid & 0xFFFFFFFF,
340                                 created->tm_year + 1900,
341                                 created->tm_mon + 1,
342                                 created->tm_mday);
343                 }
344
345                 curuid = keys->uids;
346                 if (curuid != NULL &&
347                                 curuid->packet->tag == OPENPGP_PACKET_UID) {
348                         snprintf(buf, 1023, "%.*s",
349                                 (int) curuid->packet->length,
350                                 curuid->packet->data);
351                         if (html) {
352                                 printf("<a href=\"lookup?op=vindex&"
353                                         "search=0x%016" PRIX64 "\">",
354                                         keyid);
355                         }
356                         printf("%s%s%s\n", 
357                                 (html) ? txt2html(buf) : buf,
358                                 (html) ? "</a>" : "",
359                                 (keys->revoked) ? " *** REVOKED ***" : "");
360                         if (skshash) {
361                                 display_skshash(keys, html);
362                         }
363                         if (fingerprint) {
364                                 display_fingerprint(keys);
365                         }
366                         if (verbose) {
367                                 list_sigs(curuid->sigs, html);
368                         }
369                         curuid = curuid->next;
370                 } else {
371                         printf("%s\n", 
372                                 (keys->revoked) ? "*** REVOKED ***": "");
373                         if (fingerprint) {
374                                 display_fingerprint(keys);
375                         }
376                 }
377
378                 list_uids(keyid, curuid, verbose, html);
379                 if (verbose) {
380                         list_subkeys(keys->subkeys, verbose, html);
381                 }
382
383                 keys = keys->next;
384         }
385
386         if (html) {
387                 puts("</pre>");
388         }
389
390         return 0;
391 }
392
393 /**
394  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
395  *      @keys: The keys to display.
396  *
397  *      This function takes a list of OpenPGP public keys and displays a
398  *      machine readable list of them.
399  */
400 int mrkey_index(struct openpgp_publickey *keys)
401 {
402         struct openpgp_signedpacket_list        *curuid = NULL;
403         time_t                                   created_time = 0;
404         int                                      type = 0;
405         int                                      length = 0;
406         int                                      i = 0;
407         size_t                                   fplength = 0;
408         unsigned char                            fp[20];
409         int                                      c;
410         uint64_t                                 keyid;
411
412         while (keys != NULL) {
413                 created_time = (keys->publickey->data[1] << 24) +
414                                         (keys->publickey->data[2] << 16) +
415                                         (keys->publickey->data[3] << 8) +
416                                         keys->publickey->data[4];
417
418                 printf("pub:");
419
420                 switch (keys->publickey->data[0]) {
421                 case 2:
422                 case 3:
423                         if (get_keyid(keys, &keyid) != ONAK_E_OK) {
424                                 logthing(LOGTHING_ERROR, "Couldn't get keyid");
425                         }
426                         printf("%016" PRIX64, keyid);
427                         type = keys->publickey->data[7];
428                         length = (keys->publickey->data[8] << 8) +
429                                         keys->publickey->data[9];
430                         break;
431                 case 4:
432                         (void) get_fingerprint(keys->publickey, fp, &fplength);
433
434                         for (i = 0; i < fplength; i++) {
435                                 printf("%02X", fp[i]);
436                         }
437
438                         type = keys->publickey->data[5];
439                         length = (keys->publickey->data[6] << 8) +
440                                         keys->publickey->data[7];
441                         break;
442                 default:
443                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
444                                 keys->publickey->data[0]);
445                 }
446
447                 printf(":%d:%d:%ld::%s\n",
448                         type,
449                         length,
450                         created_time,
451                         (keys->revoked) ? "r" : "");
452         
453                 for (curuid = keys->uids; curuid != NULL;
454                          curuid = curuid->next) {
455                 
456                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
457                                 printf("uid:");
458                                 for (i = 0; i < (int) curuid->packet->length;
459                                                 i++) {
460                                         c = curuid->packet->data[i];
461                                         if (c == '%') {
462                                                 putchar('%');
463                                                 putchar(c);
464                                         } else if (c == ':' || c > 127) {
465                                                 printf("%%%X", c);
466                                         } else {
467                                                 putchar(c);
468                                         }
469                                 }
470                                 printf("\n");
471                         }
472                 }
473                 keys = keys->next;
474         }
475         return 0;
476 }