]> the.earth.li Git - onak.git/blob - keyindex.c
Add support for displaying ECDH/ECDSA key sizes
[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 /*
80  * Given a public key/subkey packet return the key length.
81  */
82 unsigned int keylength(struct openpgp_packet *keydata)
83 {
84         unsigned int length;
85
86         switch (keydata->data[0]) {
87         case 2:
88         case 3:
89                 length = (keydata->data[8] << 8) +
90                                 keydata->data[9];
91                 break;
92         case 4:
93                 switch (keydata->data[5]) {
94                 case OPENPGP_PKALGO_EC:
95                 case OPENPGP_PKALGO_ECDSA:
96                         /* Elliptic curve key size is based on OID */
97                         if ((keydata->data[6] == 8) &&
98                                         (keydata->data[7] == 0x2A) &&
99                                         (keydata->data[8] == 0x86) &&
100                                         (keydata->data[9] == 0x48) &&
101                                         (keydata->data[10] == 0xCE) &&
102                                         (keydata->data[11] == 0x3D) &&
103                                         (keydata->data[12] == 0x03) &&
104                                         (keydata->data[13] == 0x01) &&
105                                         (keydata->data[14] == 0x07)) {
106                                 length = 256;
107                         } else if ((keydata->data[6] == 5) &&
108                                         (keydata->data[7] == 0x2B) &&
109                                         (keydata->data[8] == 0x81) &&
110                                         (keydata->data[9] == 0x04) &&
111                                         (keydata->data[10] == 0x00) &&
112                                         (keydata->data[11] == 0x22)) {
113                                 length = 384;
114                         } else if ((keydata->data[6] == 5) &&
115                                         (keydata->data[7] == 0x2B) &&
116                                         (keydata->data[8] == 0x81) &&
117                                         (keydata->data[9] == 0x04) &&
118                                         (keydata->data[10] == 0x00) &&
119                                         (keydata->data[11] == 0x23)) {
120                                 length = 521;
121                         } else {
122                                 logthing(LOGTHING_ERROR,
123                                         "Unknown elliptic curve size");
124                                 length = 0;
125                         }
126                         break;
127                 default:
128                         length = (keydata->data[6] << 8) +
129                                 keydata->data[7];
130                 }
131                 break;
132         default:
133                 logthing(LOGTHING_ERROR, "Unknown key version: %d",
134                         keydata->data[0]);
135                 length = 0;
136         }
137
138         return length;
139 }
140
141 int list_sigs(struct openpgp_packet_list *sigs, bool html)
142 {
143         char *uid = NULL;
144         uint64_t sigid = 0;
145         char *sig = NULL;
146
147         while (sigs != NULL) {
148                 sigid = sig_keyid(sigs->packet);
149                 uid = config.dbbackend->keyid2uid(sigid);
150                 if (sigs->packet->data[0] == 4 &&
151                                 sigs->packet->data[1] == 0x30) {
152                         /* It's a Type 4 sig revocation */
153                         sig = "rev";
154                 } else {
155                         sig = "sig";
156                 }
157                 if (html && uid != NULL) {
158                         printf("%s         <a href=\"lookup?op=get&"
159                                 "search=0x%016" PRIX64 "\">%08" PRIX64
160                                 "</a>             "
161                                 "<a href=\"lookup?op=vindex&search=0x%016"
162                                 PRIX64 "\">%s</a>\n",
163                                 sig,
164                                 sigid,
165                                 sigid & 0xFFFFFFFF,
166                                 sigid,
167                                 txt2html(uid));
168                 } else if (html && uid == NULL) {
169                         printf("%s         %08" PRIX64 "             "
170                                 "[User id not found]\n",
171                                 sig,
172                                 sigid & 0xFFFFFFFF);
173                 } else {
174                         printf("%s         %08" PRIX64
175                                 "             %s\n",
176                                 sig,
177                                 sigid & 0xFFFFFFFF,
178                                 (uid != NULL) ? uid :
179                                 "[User id not found]");
180                 }
181                 if (uid != NULL) {
182                         free(uid);
183                         uid = NULL;
184                 }
185                 sigs = sigs->next;
186         }
187
188         return 0;
189 }
190
191 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
192                 bool verbose, bool html)
193 {
194         char buf[1024];
195         int  imgindx = 0;
196
197         while (uids != NULL) {
198                 if (uids->packet->tag == OPENPGP_PACKET_UID) {
199                         snprintf(buf, 1023, "%.*s",
200                                 (int) uids->packet->length,
201                                 uids->packet->data);
202                         printf("                                %s\n",
203                                 (html) ? txt2html(buf) : buf);
204                 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
205                         printf("                                ");
206                         if (html) {
207                                 printf("<img src=\"lookup?op=photo&search="
208                                         "0x%016" PRIX64 "&idx=%d\" alt=\""
209                                         "[photo id]\">\n",
210                                         keyid,
211                                         imgindx);
212                                 imgindx++;
213                         } else {
214                                 printf("[photo id]\n");
215                         }
216                 }
217                 if (verbose) {
218                         list_sigs(uids->sigs, html);
219                 }
220                 uids = uids->next;
221         }
222
223         return 0;
224 }
225
226 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
227                 bool html)
228 {
229         struct tm       *created = NULL;
230         time_t          created_time = 0;
231         int             type = 0;
232         int             length = 0;
233         uint64_t        keyid = 0;
234
235         while (subkeys != NULL) {
236                 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
237
238                         created_time = (subkeys->packet->data[1] << 24) +
239                                         (subkeys->packet->data[2] << 16) +
240                                         (subkeys->packet->data[3] << 8) +
241                                         subkeys->packet->data[4];
242                         created = gmtime(&created_time);
243
244                         switch (subkeys->packet->data[0]) {
245                         case 2:
246                         case 3:
247                                 type = subkeys->packet->data[7];
248                                 break;
249                         case 4:
250                                 type = subkeys->packet->data[5];
251                                 break;
252                         default:
253                                 logthing(LOGTHING_ERROR,
254                                         "Unknown key type: %d",
255                                         subkeys->packet->data[0]);
256                         }
257                         length = keylength(subkeys->packet);
258
259                         if (get_packetid(subkeys->packet,
260                                         &keyid) != ONAK_E_OK) {
261                                 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
262                         }
263                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
264                                 length,
265                                 pkalgo2char(type),
266                                 (uint32_t) (keyid & 0xFFFFFFFF),
267                                 created->tm_year + 1900,
268                                 created->tm_mon + 1,
269                                 created->tm_mday);
270
271                 }
272                 if (verbose) {
273                         list_sigs(subkeys->sigs, html);
274                 }
275                 subkeys = subkeys->next;
276         }
277
278         return 0;
279 }
280
281 void display_fingerprint(struct openpgp_publickey *key)
282 {
283         int             i = 0;
284         size_t          length = 0;
285         unsigned char   fp[20];
286
287         get_fingerprint(key->publickey, fp, &length);
288         printf("      Key fingerprint =");
289         for (i = 0; i < length; i++) {
290                 if ((length == 16) ||
291                         (i % 2 == 0)) {
292                         printf(" ");
293                 }
294                 if (length == 20 && (i * 2) == length) {
295                         /* Extra space in the middle of a SHA1 fingerprint */
296                         printf(" ");
297                 }
298                 printf("%02X", fp[i]);
299         }
300         printf("\n");
301
302         return;
303 }
304
305 void display_skshash(struct openpgp_publickey *key, bool html)
306 {
307         int             i = 0;
308         struct skshash  hash;
309
310         get_skshash(key, &hash);
311         printf("      Key hash = ");
312         if (html) {
313                 printf("<a href=\"lookup?op=hget&search=");
314                 for (i = 0; i < sizeof(hash.hash); i++) {
315                         printf("%02X", hash.hash[i]);
316                 }
317                 printf("\">");
318         }
319         for (i = 0; i < sizeof(hash.hash); i++) {
320                 printf("%02X", hash.hash[i]);
321         }
322         if (html) {
323                 printf("</a>");
324         }
325         printf("\n");
326
327         return;
328 }
329
330 /**
331  *      key_index - List a set of OpenPGP keys.
332  *      @keys: The keys to display.
333  *      @verbose: Should we list sigs as well?
334  *      @fingerprint: List the fingerprint?
335  *      @html: Should the output be tailored for HTML?
336  *
337  *      This function takes a list of OpenPGP public keys and displays an index
338  *      of them. Useful for debugging or the keyserver Index function.
339  */
340 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
341                         bool skshash, bool html)
342 {
343         struct openpgp_signedpacket_list        *curuid = NULL;
344         struct tm                               *created = NULL;
345         time_t                                   created_time = 0;
346         int                                      type = 0;
347         int                                      length = 0;
348         char                                     buf[1024];
349         uint64_t                                 keyid;
350
351         if (html) {
352                 puts("<pre>");
353         }
354         puts("Type   bits/keyID    Date       User ID");
355         while (keys != NULL) {
356                 created_time = (keys->publickey->data[1] << 24) +
357                                         (keys->publickey->data[2] << 16) +
358                                         (keys->publickey->data[3] << 8) +
359                                         keys->publickey->data[4];
360                 created = gmtime(&created_time);
361
362                 switch (keys->publickey->data[0]) {
363                 case 2:
364                 case 3:
365                         type = keys->publickey->data[7];
366                         break;
367                 case 4:
368                         type = keys->publickey->data[5];
369                         break;
370                 default:
371                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
372                                 keys->publickey->data[0]);
373                 }
374                 length = keylength(keys->publickey);
375
376                 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
377                         logthing(LOGTHING_ERROR, "Couldn't get keyid.");
378                 }
379
380                 if (html) {
381                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
382                                 "search=0x%016" PRIX64 "\">%08" PRIX64
383                                 "</a> %04d/%02d/%02d ",
384                                 length,
385                                 pkalgo2char(type),
386                                 keyid,
387                                 keyid & 0xFFFFFFFF,
388                                 created->tm_year + 1900,
389                                 created->tm_mon + 1,
390                                 created->tm_mday);
391                 } else {
392                         printf("pub  %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
393                                 length,
394                                 pkalgo2char(type),
395                                 keyid & 0xFFFFFFFF,
396                                 created->tm_year + 1900,
397                                 created->tm_mon + 1,
398                                 created->tm_mday);
399                 }
400
401                 curuid = keys->uids;
402                 if (curuid != NULL &&
403                                 curuid->packet->tag == OPENPGP_PACKET_UID) {
404                         snprintf(buf, 1023, "%.*s",
405                                 (int) curuid->packet->length,
406                                 curuid->packet->data);
407                         if (html) {
408                                 printf("<a href=\"lookup?op=vindex&"
409                                         "search=0x%016" PRIX64 "\">",
410                                         keyid);
411                         }
412                         printf("%s%s%s\n", 
413                                 (html) ? txt2html(buf) : buf,
414                                 (html) ? "</a>" : "",
415                                 (keys->revoked) ? " *** REVOKED ***" : "");
416                         if (skshash) {
417                                 display_skshash(keys, html);
418                         }
419                         if (fingerprint) {
420                                 display_fingerprint(keys);
421                         }
422                         if (verbose) {
423                                 list_sigs(curuid->sigs, html);
424                         }
425                         curuid = curuid->next;
426                 } else {
427                         printf("%s\n", 
428                                 (keys->revoked) ? "*** REVOKED ***": "");
429                         if (fingerprint) {
430                                 display_fingerprint(keys);
431                         }
432                 }
433
434                 list_uids(keyid, curuid, verbose, html);
435                 if (verbose) {
436                         list_subkeys(keys->subkeys, verbose, html);
437                 }
438
439                 keys = keys->next;
440         }
441
442         if (html) {
443                 puts("</pre>");
444         }
445
446         return 0;
447 }
448
449 /**
450  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
451  *      @keys: The keys to display.
452  *
453  *      This function takes a list of OpenPGP public keys and displays a
454  *      machine readable list of them.
455  */
456 int mrkey_index(struct openpgp_publickey *keys)
457 {
458         struct openpgp_signedpacket_list        *curuid = NULL;
459         time_t                                   created_time = 0;
460         int                                      type = 0;
461         int                                      length = 0;
462         int                                      i = 0;
463         size_t                                   fplength = 0;
464         unsigned char                            fp[20];
465         int                                      c;
466         uint64_t                                 keyid;
467
468         while (keys != NULL) {
469                 created_time = (keys->publickey->data[1] << 24) +
470                                         (keys->publickey->data[2] << 16) +
471                                         (keys->publickey->data[3] << 8) +
472                                         keys->publickey->data[4];
473
474                 printf("pub:");
475
476                 switch (keys->publickey->data[0]) {
477                 case 2:
478                 case 3:
479                         if (get_keyid(keys, &keyid) != ONAK_E_OK) {
480                                 logthing(LOGTHING_ERROR, "Couldn't get keyid");
481                         }
482                         printf("%016" PRIX64, keyid);
483                         type = keys->publickey->data[7];
484                         break;
485                 case 4:
486                         (void) get_fingerprint(keys->publickey, fp, &fplength);
487
488                         for (i = 0; i < fplength; i++) {
489                                 printf("%02X", fp[i]);
490                         }
491
492                         type = keys->publickey->data[5];
493                         break;
494                 default:
495                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
496                                 keys->publickey->data[0]);
497                 }
498                 length = keylength(keys->publickey);
499
500                 printf(":%d:%d:%ld::%s\n",
501                         type,
502                         length,
503                         created_time,
504                         (keys->revoked) ? "r" : "");
505         
506                 for (curuid = keys->uids; curuid != NULL;
507                          curuid = curuid->next) {
508                 
509                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
510                                 printf("uid:");
511                                 for (i = 0; i < (int) curuid->packet->length;
512                                                 i++) {
513                                         c = curuid->packet->data[i];
514                                         if (c == '%') {
515                                                 putchar('%');
516                                                 putchar(c);
517                                         } else if (c == ':' || c > 127) {
518                                                 printf("%%%X", c);
519                                         } else {
520                                                 putchar(c);
521                                         }
522                                 }
523                                 printf("\n");
524                         }
525                 }
526                 keys = keys->next;
527         }
528         return 0;
529 }