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"
40 * Convert a Public Key algorithm to its single character representation.
42 char pkalgo2char(uint8_t algo)
47 case OPENPGP_PKALGO_DSA:
50 case OPENPGP_PKALGO_ECDSA:
51 case OPENPGP_PKALGO_EDDSA:
54 case OPENPGP_PKALGO_EC:
57 case OPENPGP_PKALGO_ELGAMAL_SIGN:
60 case OPENPGP_PKALGO_ELGAMAL_ENC:
63 case OPENPGP_PKALGO_RSA:
66 case OPENPGP_PKALGO_RSA_ENC:
69 case OPENPGP_PKALGO_RSA_SIGN:
81 * Given a public key/subkey packet return the key length.
83 unsigned int keylength(struct openpgp_packet *keydata)
87 switch (keydata->data[0]) {
90 length = (keydata->data[8] << 8) +
94 switch (keydata->data[5]) {
95 case OPENPGP_PKALGO_EC:
96 case OPENPGP_PKALGO_ECDSA:
97 case OPENPGP_PKALGO_EDDSA:
98 /* Elliptic curve key size is based on OID */
99 /* Ed25519 / 1.3.6.1.4.1.11591.15.1 */
100 if ((keydata->data[6] == 9) &&
101 (keydata->data[7] == 0x2B) &&
102 (keydata->data[8] == 0x06) &&
103 (keydata->data[9] == 0x01) &&
104 (keydata->data[10] == 0x04) &&
105 (keydata->data[11] == 0x01) &&
106 (keydata->data[12] == 0xDA) &&
107 (keydata->data[13] == 0x47) &&
108 (keydata->data[14] == 0x0F) &&
109 (keydata->data[15] == 0x01)) {
111 /* nistp256 / 1.2.840.10045.3.1.7 */
112 } else if ((keydata->data[6] == 8) &&
113 (keydata->data[7] == 0x2A) &&
114 (keydata->data[8] == 0x86) &&
115 (keydata->data[9] == 0x48) &&
116 (keydata->data[10] == 0xCE) &&
117 (keydata->data[11] == 0x3D) &&
118 (keydata->data[12] == 0x03) &&
119 (keydata->data[13] == 0x01) &&
120 (keydata->data[14] == 0x07)) {
122 /* nistp384 / 1.3.132.0.34 */
123 } else if ((keydata->data[6] == 5) &&
124 (keydata->data[7] == 0x2B) &&
125 (keydata->data[8] == 0x81) &&
126 (keydata->data[9] == 0x04) &&
127 (keydata->data[10] == 0x00) &&
128 (keydata->data[11] == 0x22)) {
130 /* nistp521 / 1.3.132.0.35 */
131 } else if ((keydata->data[6] == 5) &&
132 (keydata->data[7] == 0x2B) &&
133 (keydata->data[8] == 0x81) &&
134 (keydata->data[9] == 0x04) &&
135 (keydata->data[10] == 0x00) &&
136 (keydata->data[11] == 0x23)) {
138 /* brainpoolP256r1 / 1.3.36.3.3.2.8.1.1.7 */
139 } else if ((keydata->data[6] == 9) &&
140 (keydata->data[7] == 0x2B) &&
141 (keydata->data[8] == 0x24) &&
142 (keydata->data[9] == 0x03) &&
143 (keydata->data[10] == 0x03) &&
144 (keydata->data[11] == 0x02) &&
145 (keydata->data[12] == 0x08) &&
146 (keydata->data[13] == 0x01) &&
147 (keydata->data[14] == 0x01) &&
148 (keydata->data[15] == 0x07)) {
150 /* brainpoolP384r1 / 1.3.36.3.3.2.8.1.1.11 */
151 } else if ((keydata->data[6] == 9) &&
152 (keydata->data[7] == 0x2B) &&
153 (keydata->data[8] == 0x24) &&
154 (keydata->data[9] == 0x03) &&
155 (keydata->data[10] == 0x03) &&
156 (keydata->data[11] == 0x02) &&
157 (keydata->data[12] == 0x08) &&
158 (keydata->data[13] == 0x01) &&
159 (keydata->data[14] == 0x01) &&
160 (keydata->data[15] == 0x0B)) {
162 /* brainpoolP512r1 / 1.3.36.3.3.2.8.1.1.13 */
163 } else if ((keydata->data[6] == 9) &&
164 (keydata->data[7] == 0x2B) &&
165 (keydata->data[8] == 0x24) &&
166 (keydata->data[9] == 0x03) &&
167 (keydata->data[10] == 0x03) &&
168 (keydata->data[11] == 0x02) &&
169 (keydata->data[12] == 0x08) &&
170 (keydata->data[13] == 0x01) &&
171 (keydata->data[14] == 0x01) &&
172 (keydata->data[15] == 0x0D)) {
174 /* secp256k1 / 1.3.132.0.10 */
175 } else if ((keydata->data[6] == 5) &&
176 (keydata->data[7] == 0x2B) &&
177 (keydata->data[8] == 0x81) &&
178 (keydata->data[9] == 0x04) &&
179 (keydata->data[10] == 0x00) &&
180 (keydata->data[11] == 0x0A)) {
183 logthing(LOGTHING_ERROR,
184 "Unknown elliptic curve size");
189 length = (keydata->data[6] << 8) +
194 logthing(LOGTHING_ERROR, "Unknown key version: %d",
202 int list_sigs(struct onak_dbctx *dbctx,
203 struct openpgp_packet_list *sigs, bool html)
209 while (sigs != NULL) {
210 sigid = sig_keyid(sigs->packet);
211 uid = dbctx->keyid2uid(dbctx, sigid);
212 if (sigs->packet->data[0] == 4 &&
213 sigs->packet->data[1] == 0x30) {
214 /* It's a Type 4 sig revocation */
219 if (html && uid != NULL) {
220 printf("%s <a href=\"lookup?op=get&"
221 "search=0x%016" PRIX64 "\">%08" PRIX64
223 "<a href=\"lookup?op=vindex&search=0x%016"
224 PRIX64 "\">%s</a>\n",
230 } else if (html && uid == NULL) {
231 printf("%s %08" PRIX64 " "
232 "[User id not found]\n",
236 printf("%s %08" PRIX64
240 (uid != NULL) ? uid :
241 "[User id not found]");
253 int list_uids(struct onak_dbctx *dbctx,
254 uint64_t keyid, struct openpgp_signedpacket_list *uids,
255 bool verbose, bool html)
260 while (uids != NULL) {
261 if (uids->packet->tag == OPENPGP_PACKET_UID) {
262 snprintf(buf, 1023, "%.*s",
263 (int) uids->packet->length,
266 (html) ? txt2html(buf) : buf);
267 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
270 printf("<img src=\"lookup?op=photo&search="
271 "0x%016" PRIX64 "&idx=%d\" alt=\""
277 printf("[photo id]\n");
281 list_sigs(dbctx, uids->sigs, html);
289 int list_subkeys(struct onak_dbctx *dbctx,
290 struct openpgp_signedpacket_list *subkeys, bool verbose,
293 struct tm *created = NULL;
294 time_t created_time = 0;
299 while (subkeys != NULL) {
300 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
302 created_time = (subkeys->packet->data[1] << 24) +
303 (subkeys->packet->data[2] << 16) +
304 (subkeys->packet->data[3] << 8) +
305 subkeys->packet->data[4];
306 created = gmtime(&created_time);
308 switch (subkeys->packet->data[0]) {
311 type = subkeys->packet->data[7];
314 type = subkeys->packet->data[5];
317 logthing(LOGTHING_ERROR,
318 "Unknown key type: %d",
319 subkeys->packet->data[0]);
321 length = keylength(subkeys->packet);
323 if (get_packetid(subkeys->packet,
324 &keyid) != ONAK_E_OK) {
325 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
327 printf("sub %5d%c/%08X %04d/%02d/%02d\n",
330 (uint32_t) (keyid & 0xFFFFFFFF),
331 created->tm_year + 1900,
337 list_sigs(dbctx, subkeys->sigs, html);
339 subkeys = subkeys->next;
345 void display_fingerprint(struct openpgp_publickey *key)
348 struct openpgp_fingerprint fingerprint;
350 get_fingerprint(key->publickey, &fingerprint);
351 printf(" Key fingerprint =");
352 for (i = 0; i < fingerprint.length; i++) {
353 if ((fingerprint.length == 16) ||
357 if (fingerprint.length == 20 &&
358 (i * 2) == fingerprint.length) {
359 /* Extra space in the middle of a SHA1 fingerprint */
362 printf("%02X", fingerprint.fp[i]);
369 void display_skshash(struct openpgp_publickey *key, bool html)
374 get_skshash(key, &hash);
375 printf(" Key hash = ");
377 printf("<a href=\"lookup?op=hget&search=");
378 for (i = 0; i < sizeof(hash.hash); i++) {
379 printf("%02X", hash.hash[i]);
383 for (i = 0; i < sizeof(hash.hash); i++) {
384 printf("%02X", hash.hash[i]);
395 * key_index - List a set of OpenPGP keys.
396 * @keys: The keys to display.
397 * @verbose: Should we list sigs as well?
398 * @fingerprint: List the fingerprint?
399 * @html: Should the output be tailored for HTML?
401 * This function takes a list of OpenPGP public keys and displays an index
402 * of them. Useful for debugging or the keyserver Index function.
404 int key_index(struct onak_dbctx *dbctx,
405 struct openpgp_publickey *keys, bool verbose, bool fingerprint,
406 bool skshash, bool html)
408 struct openpgp_signedpacket_list *curuid = NULL;
409 struct tm *created = NULL;
410 time_t created_time = 0;
419 puts("Type bits/keyID Date User ID");
420 while (keys != NULL) {
421 created_time = (keys->publickey->data[1] << 24) +
422 (keys->publickey->data[2] << 16) +
423 (keys->publickey->data[3] << 8) +
424 keys->publickey->data[4];
425 created = gmtime(&created_time);
427 switch (keys->publickey->data[0]) {
430 type = keys->publickey->data[7];
433 type = keys->publickey->data[5];
436 logthing(LOGTHING_ERROR, "Unknown key type: %d",
437 keys->publickey->data[0]);
439 length = keylength(keys->publickey);
441 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
442 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
446 printf("pub %5d%c/<a href=\"lookup?op=get&"
447 "search=0x%016" PRIX64 "\">%08" PRIX64
448 "</a> %04d/%02d/%02d ",
453 created->tm_year + 1900,
457 printf("pub %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
461 created->tm_year + 1900,
467 if (curuid != NULL &&
468 curuid->packet->tag == OPENPGP_PACKET_UID) {
469 snprintf(buf, 1023, "%.*s",
470 (int) curuid->packet->length,
471 curuid->packet->data);
473 printf("<a href=\"lookup?op=vindex&"
474 "search=0x%016" PRIX64 "\">",
478 (html) ? txt2html(buf) : buf,
479 (html) ? "</a>" : "",
480 (keys->revoked) ? " *** REVOKED ***" : "");
482 display_skshash(keys, html);
485 display_fingerprint(keys);
488 list_sigs(dbctx, curuid->sigs, html);
490 curuid = curuid->next;
493 (keys->revoked) ? "*** REVOKED ***": "");
495 display_fingerprint(keys);
499 list_uids(dbctx, keyid, curuid, verbose, html);
501 list_subkeys(dbctx, keys->subkeys, verbose, html);
515 * mrkey_index - List a set of OpenPGP keys in the MRHKP format.
516 * @keys: The keys to display.
518 * This function takes a list of OpenPGP public keys and displays a
519 * machine readable list of them.
521 int mrkey_index(struct openpgp_publickey *keys)
523 struct openpgp_signedpacket_list *curuid = NULL;
524 time_t created_time = 0;
530 struct openpgp_fingerprint fingerprint;
532 while (keys != NULL) {
533 created_time = (keys->publickey->data[1] << 24) +
534 (keys->publickey->data[2] << 16) +
535 (keys->publickey->data[3] << 8) +
536 keys->publickey->data[4];
540 switch (keys->publickey->data[0]) {
543 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
544 logthing(LOGTHING_ERROR, "Couldn't get keyid");
546 printf("%016" PRIX64, keyid);
547 type = keys->publickey->data[7];
550 (void) get_fingerprint(keys->publickey, &fingerprint);
552 for (i = 0; i < fingerprint.length; i++) {
553 printf("%02X", fingerprint.fp[i]);
556 type = keys->publickey->data[5];
559 logthing(LOGTHING_ERROR, "Unknown key type: %d",
560 keys->publickey->data[0]);
562 length = keylength(keys->publickey);
564 printf(":%d:%d:%ld::%s\n",
568 (keys->revoked) ? "r" : "");
570 for (curuid = keys->uids; curuid != NULL;
571 curuid = curuid->next) {
573 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
575 for (i = 0; i < (int) curuid->packet->length;
577 c = curuid->packet->data[i];
581 } else if (c == ':' || c > 127) {