2  * keyindex.c - Routines to list an OpenPGP key.
 
   4  * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
 
  14 #include "decodekey.h"
 
  20 #include "keystructs.h"
 
  22 #include "onak-conf.h"
 
  24 int list_sigs(struct openpgp_packet_list *sigs, bool html)
 
  30         while (sigs != NULL) {
 
  31                 sigid = sig_keyid(sigs->packet);
 
  32                 uid = config.dbbackend->keyid2uid(sigid);
 
  33                 if (sigs->packet->data[0] == 4 &&
 
  34                                 sigs->packet->data[1] == 0x30) {
 
  35                         /* It's a Type 4 sig revocation */
 
  40                 if (html && uid != NULL) {
 
  41                         printf("%s         <a href=\"lookup?op=get&"
 
  42                                 "search=%016" PRIX64 "\">%08" PRIX64
 
  44                                 "<a href=\"lookup?op=vindex&search=0x%016"
 
  51                 } else if (html && uid == NULL) {
 
  52                         printf("%s         %08" PRIX64 "             "
 
  53                                 "[User id not found]\n",
 
  57                         printf("%s         %08" PRIX64
 
  62                                 "[User id not found]");
 
  74 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
 
  75                 bool verbose, bool html)
 
  80         while (uids != NULL) {
 
  81                 if (uids->packet->tag == 13) {
 
  82                         snprintf(buf, 1023, "%.*s",
 
  83                                 (int) uids->packet->length,
 
  86                                 (html) ? txt2html(buf) : buf);
 
  87                 } else if (uids->packet->tag == 17) {
 
  90                                 printf("<img src=\"lookup?op=photo&search="
 
  91                                         "0x%016" PRIX64 "&idx=%d\" alt=\""
 
  97                                 printf("[photo id]\n");
 
 101                         list_sigs(uids->sigs, html);
 
 109 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
 
 112         struct tm       *created = NULL;
 
 113         time_t          created_time = 0;
 
 117         while (subkeys != NULL) {
 
 118                 if (subkeys->packet->tag == 14) {
 
 120                         created_time = (subkeys->packet->data[1] << 24) +
 
 121                                         (subkeys->packet->data[2] << 16) +
 
 122                                         (subkeys->packet->data[3] << 8) +
 
 123                                         subkeys->packet->data[4];
 
 124                         created = gmtime(&created_time);
 
 126                         switch (subkeys->packet->data[0]) {
 
 129                                 type = subkeys->packet->data[7];
 
 130                                 length = (subkeys->packet->data[8] << 8) +
 
 131                                         subkeys->packet->data[9];
 
 134                                 type = subkeys->packet->data[5];
 
 135                                 length = (subkeys->packet->data[6] << 8) +
 
 136                                         subkeys->packet->data[7];
 
 139                                 logthing(LOGTHING_ERROR,
 
 140                                         "Unknown key type: %d",
 
 141                                         subkeys->packet->data[0]);
 
 144                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
 
 146                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
 
 147                                         ((type == 17) ? 'D' : '?')),
 
 148                                 (uint32_t) (get_packetid(subkeys->packet) &
 
 150                                 created->tm_year + 1900,
 
 156                         list_sigs(subkeys->sigs, html);
 
 158                 subkeys = subkeys->next;
 
 164 void display_fingerprint(struct openpgp_publickey *key)
 
 168         unsigned char   fp[20];
 
 170         get_fingerprint(key->publickey, fp, &length);
 
 171         printf("      Key fingerprint =");
 
 172         for (i = 0; i < length; i++) {
 
 173                 if ((length == 16) ||
 
 177                 printf("%02X", fp[i]);
 
 178                 if ((i * 2) == length) {
 
 187 void display_skshash(struct openpgp_publickey *key, bool html)
 
 192         get_skshash(key, &hash);
 
 193         printf("      Key hash = ");
 
 195                 printf("<a href=\"lookup?op=hget&search=");
 
 196                 for (i = 0; i < sizeof(hash.hash); i++) {
 
 197                         printf("%02X", hash.hash[i]);
 
 201         for (i = 0; i < sizeof(hash.hash); i++) {
 
 202                 printf("%02X", hash.hash[i]);
 
 213  *      key_index - List a set of OpenPGP keys.
 
 214  *      @keys: The keys to display.
 
 215  *      @verbose: Should we list sigs as well?
 
 216  *      @fingerprint: List the fingerprint?
 
 217  *      @html: Should the output be tailored for HTML?
 
 219  *      This function takes a list of OpenPGP public keys and displays an index
 
 220  *      of them. Useful for debugging or the keyserver Index function.
 
 222 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
 
 223                         bool skshash, bool html)
 
 225         struct openpgp_signedpacket_list        *curuid = NULL;
 
 226         struct tm                               *created = NULL;
 
 227         time_t                                   created_time = 0;
 
 237         puts("Type   bits/keyID    Date       User ID");
 
 238         while (keys != NULL) {
 
 239                 created_time = (keys->publickey->data[1] << 24) +
 
 240                                         (keys->publickey->data[2] << 16) +
 
 241                                         (keys->publickey->data[3] << 8) +
 
 242                                         keys->publickey->data[4];
 
 243                 created = gmtime(&created_time);
 
 245                 switch (keys->publickey->data[0]) {
 
 248                         type = keys->publickey->data[7];
 
 249                         length = (keys->publickey->data[8] << 8) +
 
 250                                         keys->publickey->data[9];
 
 253                         type = keys->publickey->data[5];
 
 254                         length = (keys->publickey->data[6] << 8) +
 
 255                                         keys->publickey->data[7];
 
 258                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
 
 259                                 keys->publickey->data[0]);
 
 262                 keyid = get_keyid(keys);
 
 283                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
 
 284                                 "search=%016" PRIX64 "\">%08" PRIX64
 
 285                                 "</a> %04d/%02d/%02d ",
 
 290                                 created->tm_year + 1900,
 
 294                         printf("pub  %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
 
 298                                 created->tm_year + 1900,
 
 304                 if (curuid != NULL && curuid->packet->tag == 13) {
 
 305                         snprintf(buf, 1023, "%.*s",
 
 306                                 (int) curuid->packet->length,
 
 307                                 curuid->packet->data);
 
 309                                 printf("<a href=\"lookup?op=vindex&"
 
 310                                         "search=0x%016" PRIX64 "\">",
 
 314                                 (html) ? txt2html(buf) : buf,
 
 315                                 (html) ? "</a>" : "",
 
 316                                 (keys->revoked) ? " *** REVOKED ***" : "");
 
 318                                 display_skshash(keys, html);
 
 321                                 display_fingerprint(keys);
 
 324                                 list_sigs(curuid->sigs, html);
 
 326                         curuid = curuid->next;
 
 329                                 (keys->revoked) ? "*** REVOKED ***": "");
 
 331                                 display_fingerprint(keys);
 
 335                 list_uids(keyid, curuid, verbose, html);
 
 337                         list_subkeys(keys->subkeys, verbose, html);
 
 351  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
 
 352  *      @keys: The keys to display.
 
 354  *      This function takes a list of OpenPGP public keys and displays a
 
 355  *      machine readable list of them.
 
 357 int mrkey_index(struct openpgp_publickey *keys)
 
 359         struct openpgp_signedpacket_list        *curuid = NULL;
 
 360         time_t                                   created_time = 0;
 
 365         unsigned char                            fp[20];
 
 368         while (keys != NULL) {
 
 369                 created_time = (keys->publickey->data[1] << 24) +
 
 370                                         (keys->publickey->data[2] << 16) +
 
 371                                         (keys->publickey->data[3] << 8) +
 
 372                                         keys->publickey->data[4];
 
 376                 switch (keys->publickey->data[0]) {
 
 379                         printf("%016" PRIX64, get_keyid(keys));
 
 380                         type = keys->publickey->data[7];
 
 381                         length = (keys->publickey->data[8] << 8) +
 
 382                                         keys->publickey->data[9];
 
 385                         (void) get_fingerprint(keys->publickey, fp, &fplength);
 
 387                         for (i = 0; i < fplength; i++) {
 
 388                                 printf("%02X", fp[i]);
 
 391                         type = keys->publickey->data[5];
 
 392                         length = (keys->publickey->data[6] << 8) +
 
 393                                         keys->publickey->data[7];
 
 396                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
 
 397                                 keys->publickey->data[0]);
 
 400                 printf(":%d:%d:%ld::%s\n",
 
 404                         (keys->revoked) ? "r" : "");
 
 406                 for (curuid = keys->uids; curuid != NULL;
 
 407                          curuid = curuid->next) {
 
 409                         if (curuid->packet->tag == 13) {
 
 411                                 for (i = 0; i < (int) curuid->packet->length;
 
 413                                         c = curuid->packet->data[i];
 
 417                                         } else if (c == ':' || c > 127) {