2  * keyindex.c - Routines to list an OpenPGP key.
 
   4  * Jonathan McDowell <noodles@earth.li>
 
   6  * Copyright 2002 Project Purple
 
   8  * $Id: keyindex.c,v 1.15 2004/05/27 01:25:37 noodles Exp $
 
  19 #include "decodekey.h"
 
  25 #include "keystructs.h"
 
  28 int list_sigs(struct openpgp_packet_list *sigs, bool html)
 
  33         while (sigs != NULL) {
 
  34                 sigid = sig_keyid(sigs->packet);
 
  35                 uid = keyid2uid(sigid);
 
  36                 if (html && uid != NULL) {
 
  37                         printf("sig         <a href=\"lookup?op=get&"
 
  38                                 "search=%08llX\">%08llX</a>             "
 
  39                                 "<a href=\"lookup?op=vindex&search=0x%08llX\">"
 
  45                 } else if (html && uid == NULL) {
 
  47                                 "[User id not found]\n",
 
  54                                 "[User id not found]");
 
  66 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
 
  67                 bool verbose, bool html)
 
  72         while (uids != NULL) {
 
  73                 if (uids->packet->tag == 13) {
 
  74                         snprintf(buf, 1023, "%.*s",
 
  75                                 (int) uids->packet->length,
 
  78                                 (html) ? txt2html(buf) : buf);
 
  79                 } else if (uids->packet->tag == 17) {
 
  82                                 printf("<img src=\"lookup?op=photo&search=0x%llX&idx=%d\" alt=\"[photo id]\">\n",
 
  87                                 printf("[photo id]\n");
 
  91                         list_sigs(uids->sigs, html);
 
  99 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
 
 102         struct tm       *created = NULL;
 
 103         time_t          created_time = 0;
 
 107         while (subkeys != NULL) {
 
 108                 if (subkeys->packet->tag == 14) {
 
 110                         created_time = (subkeys->packet->data[1] << 24) +
 
 111                                         (subkeys->packet->data[2] << 16) +
 
 112                                         (subkeys->packet->data[3] << 8) +
 
 113                                         subkeys->packet->data[4];
 
 114                         created = gmtime(&created_time);
 
 116                         switch (subkeys->packet->data[0]) {
 
 119                                 type = subkeys->packet->data[7];
 
 120                                 length = (subkeys->packet->data[8] << 8) +
 
 121                                         subkeys->packet->data[9];
 
 124                                 type = subkeys->packet->data[5];
 
 125                                 length = (subkeys->packet->data[6] << 8) +
 
 126                                         subkeys->packet->data[7];
 
 129                                 logthing(LOGTHING_ERROR,
 
 130                                         "Unknown key type: %d",
 
 131                                         subkeys->packet->data[0]);
 
 134                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
 
 136                                 (type == 1) ? 'R' : ((type == 16) ? 'g' : 
 
 137                                         ((type == 17) ? 'D' : '?')),
 
 138                                 (uint32_t) (get_packetid(subkeys->packet) &
 
 140                                 created->tm_year + 1900,
 
 146                         list_sigs(subkeys->sigs, html);
 
 148                 subkeys = subkeys->next;
 
 154 void display_fingerprint(struct openpgp_publickey *key)
 
 158         unsigned char   fp[20];
 
 160         get_fingerprint(key->publickey, fp, &length);
 
 161         printf("      Key fingerprint =");
 
 162         for (i = 0; i < length; i++) {
 
 163                 if ((length == 16) ||
 
 167                 printf("%02X", fp[i]);
 
 168                 if ((i * 2) == length) {
 
 178  *      key_index - List a set of OpenPGP keys.
 
 179  *      @keys: The keys to display.
 
 180  *      @verbose: Should we list sigs as well?
 
 181  *      @fingerprint: List the fingerprint?
 
 182  *      @html: Should the output be tailored for HTML?
 
 184  *      This function takes a list of OpenPGP public keys and displays an index
 
 185  *      of them. Useful for debugging or the keyserver Index function.
 
 187 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
 
 190         struct openpgp_signedpacket_list        *curuid = NULL;
 
 191         struct tm                               *created = NULL;
 
 192         time_t                                   created_time = 0;
 
 201         puts("Type   bits/keyID    Date       User ID");
 
 202         while (keys != NULL) {
 
 203                 created_time = (keys->publickey->data[1] << 24) +
 
 204                                         (keys->publickey->data[2] << 16) +
 
 205                                         (keys->publickey->data[3] << 8) +
 
 206                                         keys->publickey->data[4];
 
 207                 created = gmtime(&created_time);
 
 209                 switch (keys->publickey->data[0]) {
 
 212                         type = keys->publickey->data[7];
 
 213                         length = (keys->publickey->data[8] << 8) +
 
 214                                         keys->publickey->data[9];
 
 217                         type = keys->publickey->data[5];
 
 218                         length = (keys->publickey->data[6] << 8) +
 
 219                                         keys->publickey->data[7];
 
 222                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
 
 223                                 keys->publickey->data[0]);
 
 226                 keyid = (get_keyid(keys) & 0xFFFFFFFF),
 
 227                 printf("pub  %5d%c/%08X %04d/%02d/%02d ",
 
 229                         (type == 1) ? 'R' : ((type == 16) ? 'g' : 
 
 230                                 ((type == 17) ? 'D' : '?')),
 
 232                         created->tm_year + 1900,
 
 237                 if (curuid != NULL && curuid->packet->tag == 13) {
 
 238                         snprintf(buf, 1023, "%.*s",
 
 239                                 (int) curuid->packet->length,
 
 240                                 curuid->packet->data);
 
 242                                 (html) ? txt2html(buf) : buf,
 
 243                                 (keys->revocations == NULL) ? "" :
 
 246                                 display_fingerprint(keys);
 
 249                                 list_sigs(curuid->sigs, html);
 
 251                         curuid = curuid->next;
 
 254                                 (keys->revocations == NULL) ? "" :
 
 257                                 display_fingerprint(keys);
 
 261                 list_uids(keyid, curuid, verbose, html);
 
 263                         list_subkeys(keys->subkeys, verbose, html);
 
 277  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
 
 278  *      @keys: The keys to display.
 
 280  *      This function takes a list of OpenPGP public keys and displays a
 
 281  *      machine readable list of them.
 
 283 int mrkey_index(struct openpgp_publickey *keys)
 
 285         struct openpgp_signedpacket_list        *curuid = NULL;
 
 286         time_t                                   created_time = 0;
 
 291         unsigned char                            fp[20];
 
 293         while (keys != NULL) {
 
 294                 created_time = (keys->publickey->data[1] << 24) +
 
 295                                         (keys->publickey->data[2] << 16) +
 
 296                                         (keys->publickey->data[3] << 8) +
 
 297                                         keys->publickey->data[4];
 
 301                 switch (keys->publickey->data[0]) {
 
 304                         printf("%016llX", get_keyid(keys));
 
 305                         type = keys->publickey->data[7];
 
 306                         length = (keys->publickey->data[8] << 8) +
 
 307                                         keys->publickey->data[9];
 
 310                         (void) get_fingerprint(keys->publickey, fp, &fplength);
 
 312                         for (i = 0; i < fplength; i++) {
 
 313                                 printf("%02X", fp[i]);
 
 316                         type = keys->publickey->data[5];
 
 317                         length = (keys->publickey->data[6] << 8) +
 
 318                                         keys->publickey->data[7];
 
 321                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
 
 322                                 keys->publickey->data[0]);
 
 325                 printf(":%d:%d:%ld::%s\n",
 
 329                         (keys->revocations == NULL) ? "" : "r");
 
 331                 for (curuid = keys->uids; curuid != NULL;
 
 332                          curuid = curuid->next) {
 
 334                         if (curuid->packet->tag == 13) {
 
 336                                         (int) curuid->packet->length,
 
 337                                         curuid->packet->data);