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"
25 int list_sigs(struct openpgp_packet_list *sigs, bool html)
31 while (sigs != NULL) {
32 sigid = sig_keyid(sigs->packet);
33 uid = config.dbbackend->keyid2uid(sigid);
34 if (sigs->packet->data[0] == 4 &&
35 sigs->packet->data[1] == 0x30) {
36 /* It's a Type 4 sig revocation */
41 if (html && uid != NULL) {
42 printf("%s <a href=\"lookup?op=get&"
43 "search=%016" PRIX64 "\">%08" PRIX64
45 "<a href=\"lookup?op=vindex&search=0x%016"
52 } else if (html && uid == NULL) {
53 printf("%s %08" PRIX64 " "
54 "[User id not found]\n",
58 printf("%s %08" PRIX64
63 "[User id not found]");
75 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
76 bool verbose, bool html)
81 while (uids != NULL) {
82 if (uids->packet->tag == OPENPGP_PACKET_UID) {
83 snprintf(buf, 1023, "%.*s",
84 (int) uids->packet->length,
87 (html) ? txt2html(buf) : buf);
88 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
91 printf("<img src=\"lookup?op=photo&search="
92 "0x%016" PRIX64 "&idx=%d\" alt=\""
98 printf("[photo id]\n");
102 list_sigs(uids->sigs, html);
110 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
113 struct tm *created = NULL;
114 time_t created_time = 0;
118 while (subkeys != NULL) {
119 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
121 created_time = (subkeys->packet->data[1] << 24) +
122 (subkeys->packet->data[2] << 16) +
123 (subkeys->packet->data[3] << 8) +
124 subkeys->packet->data[4];
125 created = gmtime(&created_time);
127 switch (subkeys->packet->data[0]) {
130 type = subkeys->packet->data[7];
131 length = (subkeys->packet->data[8] << 8) +
132 subkeys->packet->data[9];
135 type = subkeys->packet->data[5];
136 length = (subkeys->packet->data[6] << 8) +
137 subkeys->packet->data[7];
140 logthing(LOGTHING_ERROR,
141 "Unknown key type: %d",
142 subkeys->packet->data[0]);
145 printf("sub %5d%c/%08X %04d/%02d/%02d\n",
147 (type == OPENPGP_PKALGO_RSA) ? 'R' :
148 ((type == OPENPGP_PKALGO_ELGAMAL) ? 'g' :
149 ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
150 (uint32_t) (get_packetid(subkeys->packet) &
152 created->tm_year + 1900,
158 list_sigs(subkeys->sigs, html);
160 subkeys = subkeys->next;
166 void display_fingerprint(struct openpgp_publickey *key)
170 unsigned char fp[20];
172 get_fingerprint(key->publickey, fp, &length);
173 printf(" Key fingerprint =");
174 for (i = 0; i < length; i++) {
175 if ((length == 16) ||
179 printf("%02X", fp[i]);
180 if ((i * 2) == length) {
189 void display_skshash(struct openpgp_publickey *key, bool html)
194 get_skshash(key, &hash);
195 printf(" Key hash = ");
197 printf("<a href=\"lookup?op=hget&search=");
198 for (i = 0; i < sizeof(hash.hash); i++) {
199 printf("%02X", hash.hash[i]);
203 for (i = 0; i < sizeof(hash.hash); i++) {
204 printf("%02X", hash.hash[i]);
215 * key_index - List a set of OpenPGP keys.
216 * @keys: The keys to display.
217 * @verbose: Should we list sigs as well?
218 * @fingerprint: List the fingerprint?
219 * @html: Should the output be tailored for HTML?
221 * This function takes a list of OpenPGP public keys and displays an index
222 * of them. Useful for debugging or the keyserver Index function.
224 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
225 bool skshash, bool html)
227 struct openpgp_signedpacket_list *curuid = NULL;
228 struct tm *created = NULL;
229 time_t created_time = 0;
239 puts("Type bits/keyID Date User ID");
240 while (keys != NULL) {
241 created_time = (keys->publickey->data[1] << 24) +
242 (keys->publickey->data[2] << 16) +
243 (keys->publickey->data[3] << 8) +
244 keys->publickey->data[4];
245 created = gmtime(&created_time);
247 switch (keys->publickey->data[0]) {
250 type = keys->publickey->data[7];
251 length = (keys->publickey->data[8] << 8) +
252 keys->publickey->data[9];
255 type = keys->publickey->data[5];
256 length = (keys->publickey->data[6] << 8) +
257 keys->publickey->data[7];
260 logthing(LOGTHING_ERROR, "Unknown key type: %d",
261 keys->publickey->data[0]);
264 keyid = get_keyid(keys);
267 case OPENPGP_PKALGO_RSA:
270 case OPENPGP_PKALGO_ELGAMAL:
273 case OPENPGP_PKALGO_DSA:
276 case OPENPGP_PKALGO_ELGAMAL_SIGN:
285 printf("pub %5d%c/<a href=\"lookup?op=get&"
286 "search=%016" PRIX64 "\">%08" PRIX64
287 "</a> %04d/%02d/%02d ",
292 created->tm_year + 1900,
296 printf("pub %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
300 created->tm_year + 1900,
306 if (curuid != NULL &&
307 curuid->packet->tag == OPENPGP_PACKET_UID) {
308 snprintf(buf, 1023, "%.*s",
309 (int) curuid->packet->length,
310 curuid->packet->data);
312 printf("<a href=\"lookup?op=vindex&"
313 "search=0x%016" PRIX64 "\">",
317 (html) ? txt2html(buf) : buf,
318 (html) ? "</a>" : "",
319 (keys->revoked) ? " *** REVOKED ***" : "");
321 display_skshash(keys, html);
324 display_fingerprint(keys);
327 list_sigs(curuid->sigs, html);
329 curuid = curuid->next;
332 (keys->revoked) ? "*** REVOKED ***": "");
334 display_fingerprint(keys);
338 list_uids(keyid, curuid, verbose, html);
340 list_subkeys(keys->subkeys, verbose, html);
354 * mrkey_index - List a set of OpenPGP keys in the MRHKP format.
355 * @keys: The keys to display.
357 * This function takes a list of OpenPGP public keys and displays a
358 * machine readable list of them.
360 int mrkey_index(struct openpgp_publickey *keys)
362 struct openpgp_signedpacket_list *curuid = NULL;
363 time_t created_time = 0;
368 unsigned char fp[20];
371 while (keys != NULL) {
372 created_time = (keys->publickey->data[1] << 24) +
373 (keys->publickey->data[2] << 16) +
374 (keys->publickey->data[3] << 8) +
375 keys->publickey->data[4];
379 switch (keys->publickey->data[0]) {
382 printf("%016" PRIX64, get_keyid(keys));
383 type = keys->publickey->data[7];
384 length = (keys->publickey->data[8] << 8) +
385 keys->publickey->data[9];
388 (void) get_fingerprint(keys->publickey, fp, &fplength);
390 for (i = 0; i < fplength; i++) {
391 printf("%02X", fp[i]);
394 type = keys->publickey->data[5];
395 length = (keys->publickey->data[6] << 8) +
396 keys->publickey->data[7];
399 logthing(LOGTHING_ERROR, "Unknown key type: %d",
400 keys->publickey->data[0]);
403 printf(":%d:%d:%ld::%s\n",
407 (keys->revoked) ? "r" : "");
409 for (curuid = keys->uids; curuid != NULL;
410 curuid = curuid->next) {
412 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
414 for (i = 0; i < (int) curuid->packet->length;
416 c = curuid->packet->data[i];
420 } else if (c == ':' || c > 127) {