2 * keyindex.c - Routines to list an OpenPGP key.
4 * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
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.
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
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.
27 #include "decodekey.h"
33 #include "keystructs.h"
36 #include "onak-conf.h"
39 int list_sigs(struct openpgp_packet_list *sigs, bool html)
45 while (sigs != NULL) {
46 sigid = sig_keyid(sigs->packet);
47 uid = config.dbbackend->keyid2uid(sigid);
48 if (sigs->packet->data[0] == 4 &&
49 sigs->packet->data[1] == 0x30) {
50 /* It's a Type 4 sig revocation */
55 if (html && uid != NULL) {
56 printf("%s <a href=\"lookup?op=get&"
57 "search=0x%016" PRIX64 "\">%08" PRIX64
59 "<a href=\"lookup?op=vindex&search=0x%016"
66 } else if (html && uid == NULL) {
67 printf("%s %08" PRIX64 " "
68 "[User id not found]\n",
72 printf("%s %08" PRIX64
77 "[User id not found]");
89 int list_uids(uint64_t keyid, struct openpgp_signedpacket_list *uids,
90 bool verbose, bool html)
95 while (uids != NULL) {
96 if (uids->packet->tag == OPENPGP_PACKET_UID) {
97 snprintf(buf, 1023, "%.*s",
98 (int) uids->packet->length,
101 (html) ? txt2html(buf) : buf);
102 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
105 printf("<img src=\"lookup?op=photo&search="
106 "0x%016" PRIX64 "&idx=%d\" alt=\""
112 printf("[photo id]\n");
116 list_sigs(uids->sigs, html);
124 int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
127 struct tm *created = NULL;
128 time_t created_time = 0;
133 while (subkeys != NULL) {
134 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
136 created_time = (subkeys->packet->data[1] << 24) +
137 (subkeys->packet->data[2] << 16) +
138 (subkeys->packet->data[3] << 8) +
139 subkeys->packet->data[4];
140 created = gmtime(&created_time);
142 switch (subkeys->packet->data[0]) {
145 type = subkeys->packet->data[7];
146 length = (subkeys->packet->data[8] << 8) +
147 subkeys->packet->data[9];
150 type = subkeys->packet->data[5];
151 length = (subkeys->packet->data[6] << 8) +
152 subkeys->packet->data[7];
155 logthing(LOGTHING_ERROR,
156 "Unknown key type: %d",
157 subkeys->packet->data[0]);
160 if (get_packetid(subkeys->packet,
161 &keyid) != ONAK_E_OK) {
162 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
164 printf("sub %5d%c/%08X %04d/%02d/%02d\n",
166 (type == OPENPGP_PKALGO_RSA) ? 'R' :
167 ((type == OPENPGP_PKALGO_ELGAMAL_ENC) ? 'g' :
168 ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
169 (uint32_t) (keyid & 0xFFFFFFFF),
170 created->tm_year + 1900,
176 list_sigs(subkeys->sigs, html);
178 subkeys = subkeys->next;
184 void display_fingerprint(struct openpgp_publickey *key)
188 unsigned char fp[20];
190 get_fingerprint(key->publickey, fp, &length);
191 printf(" Key fingerprint =");
192 for (i = 0; i < length; i++) {
193 if ((length == 16) ||
197 if (length == 20 && (i * 2) == length) {
198 /* Extra space in the middle of a SHA1 fingerprint */
201 printf("%02X", fp[i]);
208 void display_skshash(struct openpgp_publickey *key, bool html)
213 get_skshash(key, &hash);
214 printf(" Key hash = ");
216 printf("<a href=\"lookup?op=hget&search=");
217 for (i = 0; i < sizeof(hash.hash); i++) {
218 printf("%02X", hash.hash[i]);
222 for (i = 0; i < sizeof(hash.hash); i++) {
223 printf("%02X", hash.hash[i]);
234 * key_index - List a set of OpenPGP keys.
235 * @keys: The keys to display.
236 * @verbose: Should we list sigs as well?
237 * @fingerprint: List the fingerprint?
238 * @html: Should the output be tailored for HTML?
240 * This function takes a list of OpenPGP public keys and displays an index
241 * of them. Useful for debugging or the keyserver Index function.
243 int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
244 bool skshash, bool html)
246 struct openpgp_signedpacket_list *curuid = NULL;
247 struct tm *created = NULL;
248 time_t created_time = 0;
258 puts("Type bits/keyID Date User ID");
259 while (keys != NULL) {
260 created_time = (keys->publickey->data[1] << 24) +
261 (keys->publickey->data[2] << 16) +
262 (keys->publickey->data[3] << 8) +
263 keys->publickey->data[4];
264 created = gmtime(&created_time);
266 switch (keys->publickey->data[0]) {
269 type = keys->publickey->data[7];
270 length = (keys->publickey->data[8] << 8) +
271 keys->publickey->data[9];
274 type = keys->publickey->data[5];
275 length = (keys->publickey->data[6] << 8) +
276 keys->publickey->data[7];
279 logthing(LOGTHING_ERROR, "Unknown key type: %d",
280 keys->publickey->data[0]);
283 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
284 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
288 case OPENPGP_PKALGO_RSA:
291 case OPENPGP_PKALGO_ELGAMAL_ENC:
294 case OPENPGP_PKALGO_DSA:
297 case OPENPGP_PKALGO_ELGAMAL_SIGN:
306 printf("pub %5d%c/<a href=\"lookup?op=get&"
307 "search=0x%016" PRIX64 "\">%08" PRIX64
308 "</a> %04d/%02d/%02d ",
313 created->tm_year + 1900,
317 printf("pub %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
321 created->tm_year + 1900,
327 if (curuid != NULL &&
328 curuid->packet->tag == OPENPGP_PACKET_UID) {
329 snprintf(buf, 1023, "%.*s",
330 (int) curuid->packet->length,
331 curuid->packet->data);
333 printf("<a href=\"lookup?op=vindex&"
334 "search=0x%016" PRIX64 "\">",
338 (html) ? txt2html(buf) : buf,
339 (html) ? "</a>" : "",
340 (keys->revoked) ? " *** REVOKED ***" : "");
342 display_skshash(keys, html);
345 display_fingerprint(keys);
348 list_sigs(curuid->sigs, html);
350 curuid = curuid->next;
353 (keys->revoked) ? "*** REVOKED ***": "");
355 display_fingerprint(keys);
359 list_uids(keyid, curuid, verbose, html);
361 list_subkeys(keys->subkeys, verbose, html);
375 * mrkey_index - List a set of OpenPGP keys in the MRHKP format.
376 * @keys: The keys to display.
378 * This function takes a list of OpenPGP public keys and displays a
379 * machine readable list of them.
381 int mrkey_index(struct openpgp_publickey *keys)
383 struct openpgp_signedpacket_list *curuid = NULL;
384 time_t created_time = 0;
389 unsigned char fp[20];
393 while (keys != NULL) {
394 created_time = (keys->publickey->data[1] << 24) +
395 (keys->publickey->data[2] << 16) +
396 (keys->publickey->data[3] << 8) +
397 keys->publickey->data[4];
401 switch (keys->publickey->data[0]) {
404 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
405 logthing(LOGTHING_ERROR, "Couldn't get keyid");
407 printf("%016" PRIX64, keyid);
408 type = keys->publickey->data[7];
409 length = (keys->publickey->data[8] << 8) +
410 keys->publickey->data[9];
413 (void) get_fingerprint(keys->publickey, fp, &fplength);
415 for (i = 0; i < fplength; i++) {
416 printf("%02X", fp[i]);
419 type = keys->publickey->data[5];
420 length = (keys->publickey->data[6] << 8) +
421 keys->publickey->data[7];
424 logthing(LOGTHING_ERROR, "Unknown key type: %d",
425 keys->publickey->data[0]);
428 printf(":%d:%d:%ld::%s\n",
432 (keys->revoked) ? "r" : "");
434 for (curuid = keys->uids; curuid != NULL;
435 curuid = curuid->next) {
437 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
439 for (i = 0; i < (int) curuid->packet->length;
441 c = curuid->packet->data[i];
445 } else if (c == ':' || c > 127) {