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:
53 case OPENPGP_PKALGO_EC:
56 case OPENPGP_PKALGO_ELGAMAL_SIGN:
59 case OPENPGP_PKALGO_ELGAMAL_ENC:
62 case OPENPGP_PKALGO_RSA:
65 case OPENPGP_PKALGO_RSA_ENC:
68 case OPENPGP_PKALGO_RSA_SIGN:
80 * Given a public key/subkey packet return the key length.
82 unsigned int keylength(struct openpgp_packet *keydata)
86 switch (keydata->data[0]) {
89 length = (keydata->data[8] << 8) +
93 switch (keydata->data[5]) {
94 case OPENPGP_PKALGO_EC:
95 case OPENPGP_PKALGO_ECDSA:
96 /* Elliptic curve key size is based on OID */
97 /* Ed25519 / 1.3.6.1.4.1.11591.15.1 */
98 if ((keydata->data[6] == 9) &&
99 (keydata->data[7] == 0x2B) &&
100 (keydata->data[8] == 0x06) &&
101 (keydata->data[9] == 0x01) &&
102 (keydata->data[10] == 0x04) &&
103 (keydata->data[11] == 0x01) &&
104 (keydata->data[12] == 0xDA) &&
105 (keydata->data[13] == 0x47) &&
106 (keydata->data[14] == 0x0F) &&
107 (keydata->data[15] == 0x01)) {
109 /* nistp256 / 1.2.840.10045.3.1.7 */
110 } else if ((keydata->data[6] == 8) &&
111 (keydata->data[7] == 0x2A) &&
112 (keydata->data[8] == 0x86) &&
113 (keydata->data[9] == 0x48) &&
114 (keydata->data[10] == 0xCE) &&
115 (keydata->data[11] == 0x3D) &&
116 (keydata->data[12] == 0x03) &&
117 (keydata->data[13] == 0x01) &&
118 (keydata->data[14] == 0x07)) {
120 /* nistp384 / 1.3.132.0.34 */
121 } else if ((keydata->data[6] == 5) &&
122 (keydata->data[7] == 0x2B) &&
123 (keydata->data[8] == 0x81) &&
124 (keydata->data[9] == 0x04) &&
125 (keydata->data[10] == 0x00) &&
126 (keydata->data[11] == 0x22)) {
128 /* nistp521 / 1.3.132.0.35 */
129 } else if ((keydata->data[6] == 5) &&
130 (keydata->data[7] == 0x2B) &&
131 (keydata->data[8] == 0x81) &&
132 (keydata->data[9] == 0x04) &&
133 (keydata->data[10] == 0x00) &&
134 (keydata->data[11] == 0x23)) {
136 /* brainpoolP256r1 / 1.3.36.3.3.2.8.1.1.7 */
137 } else if ((keydata->data[6] == 9) &&
138 (keydata->data[7] == 0x2B) &&
139 (keydata->data[8] == 0x24) &&
140 (keydata->data[9] == 0x03) &&
141 (keydata->data[10] == 0x03) &&
142 (keydata->data[11] == 0x02) &&
143 (keydata->data[12] == 0x08) &&
144 (keydata->data[13] == 0x01) &&
145 (keydata->data[14] == 0x01) &&
146 (keydata->data[15] == 0x07)) {
148 /* brainpoolP384r1 / 1.3.36.3.3.2.8.1.1.11 */
149 } else if ((keydata->data[6] == 9) &&
150 (keydata->data[7] == 0x2B) &&
151 (keydata->data[8] == 0x24) &&
152 (keydata->data[9] == 0x03) &&
153 (keydata->data[10] == 0x03) &&
154 (keydata->data[11] == 0x02) &&
155 (keydata->data[12] == 0x08) &&
156 (keydata->data[13] == 0x01) &&
157 (keydata->data[14] == 0x01) &&
158 (keydata->data[15] == 0x0B)) {
160 /* brainpoolP512r1 / 1.3.36.3.3.2.8.1.1.13 */
161 } else if ((keydata->data[6] == 9) &&
162 (keydata->data[7] == 0x2B) &&
163 (keydata->data[8] == 0x24) &&
164 (keydata->data[9] == 0x03) &&
165 (keydata->data[10] == 0x03) &&
166 (keydata->data[11] == 0x02) &&
167 (keydata->data[12] == 0x08) &&
168 (keydata->data[13] == 0x01) &&
169 (keydata->data[14] == 0x01) &&
170 (keydata->data[15] == 0x0D)) {
172 /* secp256k1 / 1.3.132.0.10 */
173 } else if ((keydata->data[6] == 5) &&
174 (keydata->data[7] == 0x2B) &&
175 (keydata->data[8] == 0x81) &&
176 (keydata->data[9] == 0x04) &&
177 (keydata->data[10] == 0x00) &&
178 (keydata->data[11] == 0x0A)) {
181 logthing(LOGTHING_ERROR,
182 "Unknown elliptic curve size");
187 length = (keydata->data[6] << 8) +
192 logthing(LOGTHING_ERROR, "Unknown key version: %d",
200 int list_sigs(struct onak_dbctx *dbctx,
201 struct openpgp_packet_list *sigs, bool html)
207 while (sigs != NULL) {
208 sigid = sig_keyid(sigs->packet);
209 uid = dbctx->keyid2uid(dbctx, sigid);
210 if (sigs->packet->data[0] == 4 &&
211 sigs->packet->data[1] == 0x30) {
212 /* It's a Type 4 sig revocation */
217 if (html && uid != NULL) {
218 printf("%s <a href=\"lookup?op=get&"
219 "search=0x%016" PRIX64 "\">%08" PRIX64
221 "<a href=\"lookup?op=vindex&search=0x%016"
222 PRIX64 "\">%s</a>\n",
228 } else if (html && uid == NULL) {
229 printf("%s %08" PRIX64 " "
230 "[User id not found]\n",
234 printf("%s %08" PRIX64
238 (uid != NULL) ? uid :
239 "[User id not found]");
251 int list_uids(struct onak_dbctx *dbctx,
252 uint64_t keyid, struct openpgp_signedpacket_list *uids,
253 bool verbose, bool html)
258 while (uids != NULL) {
259 if (uids->packet->tag == OPENPGP_PACKET_UID) {
260 snprintf(buf, 1023, "%.*s",
261 (int) uids->packet->length,
264 (html) ? txt2html(buf) : buf);
265 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
268 printf("<img src=\"lookup?op=photo&search="
269 "0x%016" PRIX64 "&idx=%d\" alt=\""
275 printf("[photo id]\n");
279 list_sigs(dbctx, uids->sigs, html);
287 int list_subkeys(struct onak_dbctx *dbctx,
288 struct openpgp_signedpacket_list *subkeys, bool verbose,
291 struct tm *created = NULL;
292 time_t created_time = 0;
297 while (subkeys != NULL) {
298 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
300 created_time = (subkeys->packet->data[1] << 24) +
301 (subkeys->packet->data[2] << 16) +
302 (subkeys->packet->data[3] << 8) +
303 subkeys->packet->data[4];
304 created = gmtime(&created_time);
306 switch (subkeys->packet->data[0]) {
309 type = subkeys->packet->data[7];
312 type = subkeys->packet->data[5];
315 logthing(LOGTHING_ERROR,
316 "Unknown key type: %d",
317 subkeys->packet->data[0]);
319 length = keylength(subkeys->packet);
321 if (get_packetid(subkeys->packet,
322 &keyid) != ONAK_E_OK) {
323 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
325 printf("sub %5d%c/%08X %04d/%02d/%02d\n",
328 (uint32_t) (keyid & 0xFFFFFFFF),
329 created->tm_year + 1900,
335 list_sigs(dbctx, subkeys->sigs, html);
337 subkeys = subkeys->next;
343 void display_fingerprint(struct openpgp_publickey *key)
346 struct openpgp_fingerprint fingerprint;
348 get_fingerprint(key->publickey, &fingerprint);
349 printf(" Key fingerprint =");
350 for (i = 0; i < fingerprint.length; i++) {
351 if ((fingerprint.length == 16) ||
355 if (fingerprint.length == 20 &&
356 (i * 2) == fingerprint.length) {
357 /* Extra space in the middle of a SHA1 fingerprint */
360 printf("%02X", fingerprint.fp[i]);
367 void display_skshash(struct openpgp_publickey *key, bool html)
372 get_skshash(key, &hash);
373 printf(" Key hash = ");
375 printf("<a href=\"lookup?op=hget&search=");
376 for (i = 0; i < sizeof(hash.hash); i++) {
377 printf("%02X", hash.hash[i]);
381 for (i = 0; i < sizeof(hash.hash); i++) {
382 printf("%02X", hash.hash[i]);
393 * key_index - List a set of OpenPGP keys.
394 * @keys: The keys to display.
395 * @verbose: Should we list sigs as well?
396 * @fingerprint: List the fingerprint?
397 * @html: Should the output be tailored for HTML?
399 * This function takes a list of OpenPGP public keys and displays an index
400 * of them. Useful for debugging or the keyserver Index function.
402 int key_index(struct onak_dbctx *dbctx,
403 struct openpgp_publickey *keys, bool verbose, bool fingerprint,
404 bool skshash, bool html)
406 struct openpgp_signedpacket_list *curuid = NULL;
407 struct tm *created = NULL;
408 time_t created_time = 0;
417 puts("Type bits/keyID Date User ID");
418 while (keys != NULL) {
419 created_time = (keys->publickey->data[1] << 24) +
420 (keys->publickey->data[2] << 16) +
421 (keys->publickey->data[3] << 8) +
422 keys->publickey->data[4];
423 created = gmtime(&created_time);
425 switch (keys->publickey->data[0]) {
428 type = keys->publickey->data[7];
431 type = keys->publickey->data[5];
434 logthing(LOGTHING_ERROR, "Unknown key type: %d",
435 keys->publickey->data[0]);
437 length = keylength(keys->publickey);
439 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
440 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
444 printf("pub %5d%c/<a href=\"lookup?op=get&"
445 "search=0x%016" PRIX64 "\">%08" PRIX64
446 "</a> %04d/%02d/%02d ",
451 created->tm_year + 1900,
455 printf("pub %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
459 created->tm_year + 1900,
465 if (curuid != NULL &&
466 curuid->packet->tag == OPENPGP_PACKET_UID) {
467 snprintf(buf, 1023, "%.*s",
468 (int) curuid->packet->length,
469 curuid->packet->data);
471 printf("<a href=\"lookup?op=vindex&"
472 "search=0x%016" PRIX64 "\">",
476 (html) ? txt2html(buf) : buf,
477 (html) ? "</a>" : "",
478 (keys->revoked) ? " *** REVOKED ***" : "");
480 display_skshash(keys, html);
483 display_fingerprint(keys);
486 list_sigs(dbctx, curuid->sigs, html);
488 curuid = curuid->next;
491 (keys->revoked) ? "*** REVOKED ***": "");
493 display_fingerprint(keys);
497 list_uids(dbctx, keyid, curuid, verbose, html);
499 list_subkeys(dbctx, keys->subkeys, verbose, html);
513 * mrkey_index - List a set of OpenPGP keys in the MRHKP format.
514 * @keys: The keys to display.
516 * This function takes a list of OpenPGP public keys and displays a
517 * machine readable list of them.
519 int mrkey_index(struct openpgp_publickey *keys)
521 struct openpgp_signedpacket_list *curuid = NULL;
522 time_t created_time = 0;
528 struct openpgp_fingerprint fingerprint;
530 while (keys != NULL) {
531 created_time = (keys->publickey->data[1] << 24) +
532 (keys->publickey->data[2] << 16) +
533 (keys->publickey->data[3] << 8) +
534 keys->publickey->data[4];
538 switch (keys->publickey->data[0]) {
541 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
542 logthing(LOGTHING_ERROR, "Couldn't get keyid");
544 printf("%016" PRIX64, keyid);
545 type = keys->publickey->data[7];
548 (void) get_fingerprint(keys->publickey, &fingerprint);
550 for (i = 0; i < fingerprint.length; i++) {
551 printf("%02X", fingerprint.fp[i]);
554 type = keys->publickey->data[5];
557 logthing(LOGTHING_ERROR, "Unknown key type: %d",
558 keys->publickey->data[0]);
560 length = keylength(keys->publickey);
562 printf(":%d:%d:%ld::%s\n",
566 (keys->revoked) ? "r" : "");
568 for (curuid = keys->uids; curuid != NULL;
569 curuid = curuid->next) {
571 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
573 for (i = 0; i < (int) curuid->packet->length;
575 c = curuid->packet->data[i];
579 } else if (c == ':' || c > 127) {