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, see <https://www.gnu.org/licenses/>.
26 #include "decodekey.h"
31 #include "keystructs.h"
37 * Convert a Public Key algorithm to its single character representation.
39 char pkalgo2char(uint8_t algo)
44 case OPENPGP_PKALGO_DSA:
47 case OPENPGP_PKALGO_ECDSA:
48 case OPENPGP_PKALGO_EDDSA:
51 case OPENPGP_PKALGO_EC:
54 case OPENPGP_PKALGO_ELGAMAL_SIGN:
57 case OPENPGP_PKALGO_ELGAMAL_ENC:
60 case OPENPGP_PKALGO_RSA:
63 case OPENPGP_PKALGO_RSA_ENC:
66 case OPENPGP_PKALGO_RSA_SIGN:
78 * Given a public key/subkey packet return the key length.
80 unsigned int keylength(struct openpgp_packet *keydata)
84 switch (keydata->data[0]) {
87 length = (keydata->data[8] << 8) +
91 switch (keydata->data[5]) {
92 case OPENPGP_PKALGO_EC:
93 case OPENPGP_PKALGO_ECDSA:
94 case OPENPGP_PKALGO_EDDSA:
95 /* Elliptic curve key size is based on OID */
96 /* Curve25519 / 1.3.6.1.4.1.3029.1.5.1 */
97 if ((keydata->data[6] == 10) &&
98 (keydata->data[7] == 0x2B) &&
99 (keydata->data[8] == 0x06) &&
100 (keydata->data[9] == 0x01) &&
101 (keydata->data[10] == 0x04) &&
102 (keydata->data[11] == 0x01) &&
103 (keydata->data[12] == 0x97) &&
104 (keydata->data[13] == 0x55) &&
105 (keydata->data[14] == 0x01) &&
106 (keydata->data[15] == 0x05) &&
107 (keydata->data[16] == 0x01)) {
109 /* Ed25519 / 1.3.6.1.4.1.11591.15.1 */
110 } else if ((keydata->data[6] == 9) &&
111 (keydata->data[7] == 0x2B) &&
112 (keydata->data[8] == 0x06) &&
113 (keydata->data[9] == 0x01) &&
114 (keydata->data[10] == 0x04) &&
115 (keydata->data[11] == 0x01) &&
116 (keydata->data[12] == 0xDA) &&
117 (keydata->data[13] == 0x47) &&
118 (keydata->data[14] == 0x0F) &&
119 (keydata->data[15] == 0x01)) {
121 /* nistp256 / 1.2.840.10045.3.1.7 */
122 } else if ((keydata->data[6] == 8) &&
123 (keydata->data[7] == 0x2A) &&
124 (keydata->data[8] == 0x86) &&
125 (keydata->data[9] == 0x48) &&
126 (keydata->data[10] == 0xCE) &&
127 (keydata->data[11] == 0x3D) &&
128 (keydata->data[12] == 0x03) &&
129 (keydata->data[13] == 0x01) &&
130 (keydata->data[14] == 0x07)) {
132 /* nistp384 / 1.3.132.0.34 */
133 } else if ((keydata->data[6] == 5) &&
134 (keydata->data[7] == 0x2B) &&
135 (keydata->data[8] == 0x81) &&
136 (keydata->data[9] == 0x04) &&
137 (keydata->data[10] == 0x00) &&
138 (keydata->data[11] == 0x22)) {
140 /* nistp521 / 1.3.132.0.35 */
141 } else if ((keydata->data[6] == 5) &&
142 (keydata->data[7] == 0x2B) &&
143 (keydata->data[8] == 0x81) &&
144 (keydata->data[9] == 0x04) &&
145 (keydata->data[10] == 0x00) &&
146 (keydata->data[11] == 0x23)) {
148 /* brainpoolP256r1 / 1.3.36.3.3.2.8.1.1.7 */
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] == 0x07)) {
160 /* brainpoolP384r1 / 1.3.36.3.3.2.8.1.1.11 */
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] == 0x0B)) {
172 /* brainpoolP512r1 / 1.3.36.3.3.2.8.1.1.13 */
173 } else if ((keydata->data[6] == 9) &&
174 (keydata->data[7] == 0x2B) &&
175 (keydata->data[8] == 0x24) &&
176 (keydata->data[9] == 0x03) &&
177 (keydata->data[10] == 0x03) &&
178 (keydata->data[11] == 0x02) &&
179 (keydata->data[12] == 0x08) &&
180 (keydata->data[13] == 0x01) &&
181 (keydata->data[14] == 0x01) &&
182 (keydata->data[15] == 0x0D)) {
184 /* secp256k1 / 1.3.132.0.10 */
185 } else if ((keydata->data[6] == 5) &&
186 (keydata->data[7] == 0x2B) &&
187 (keydata->data[8] == 0x81) &&
188 (keydata->data[9] == 0x04) &&
189 (keydata->data[10] == 0x00) &&
190 (keydata->data[11] == 0x0A)) {
193 logthing(LOGTHING_ERROR,
194 "Unknown elliptic curve size");
199 length = (keydata->data[6] << 8) +
204 logthing(LOGTHING_ERROR, "Unknown key version: %d",
212 int list_sigs(struct onak_dbctx *dbctx,
213 struct openpgp_packet_list *sigs, bool html)
219 while (sigs != NULL) {
220 sigid = sig_keyid(sigs->packet);
221 uid = dbctx->keyid2uid(dbctx, sigid);
222 if (sigs->packet->data[0] == 4 &&
223 sigs->packet->data[1] == 0x30) {
224 /* It's a Type 4 sig revocation */
229 if (html && uid != NULL) {
230 printf("%s <a href=\"lookup?op=get&"
231 "search=0x%016" PRIX64 "\">%08" PRIX64
233 "<a href=\"lookup?op=vindex&search=0x%016"
234 PRIX64 "\">%s</a>\n",
240 } else if (html && uid == NULL) {
241 printf("%s %08" PRIX64 " "
242 "[User id not found]\n",
246 printf("%s %08" PRIX64
250 (uid != NULL) ? uid :
251 "[User id not found]");
263 int list_uids(struct onak_dbctx *dbctx,
264 uint64_t keyid, struct openpgp_signedpacket_list *uids,
265 bool verbose, bool html)
270 while (uids != NULL) {
271 if (uids->packet->tag == OPENPGP_PACKET_UID) {
272 snprintf(buf, 1023, "%.*s",
273 (int) uids->packet->length,
276 (html) ? txt2html(buf) : buf);
277 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
280 printf("<img src=\"lookup?op=photo&search="
281 "0x%016" PRIX64 "&idx=%d\" alt=\""
287 printf("[photo id]\n");
291 list_sigs(dbctx, uids->sigs, html);
299 int list_subkeys(struct onak_dbctx *dbctx,
300 struct openpgp_signedpacket_list *subkeys, bool verbose,
303 struct tm *created = NULL;
304 time_t created_time = 0;
309 while (subkeys != NULL) {
310 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
312 created_time = (subkeys->packet->data[1] << 24) +
313 (subkeys->packet->data[2] << 16) +
314 (subkeys->packet->data[3] << 8) +
315 subkeys->packet->data[4];
316 created = gmtime(&created_time);
318 switch (subkeys->packet->data[0]) {
321 type = subkeys->packet->data[7];
324 type = subkeys->packet->data[5];
327 logthing(LOGTHING_ERROR,
328 "Unknown key type: %d",
329 subkeys->packet->data[0]);
331 length = keylength(subkeys->packet);
333 if (get_packetid(subkeys->packet,
334 &keyid) != ONAK_E_OK) {
335 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
337 printf("sub %5d%c/%08X %04d/%02d/%02d\n",
340 (uint32_t) (keyid & 0xFFFFFFFF),
341 created->tm_year + 1900,
347 list_sigs(dbctx, subkeys->sigs, html);
349 subkeys = subkeys->next;
355 void display_fingerprint(struct openpgp_publickey *key)
358 struct openpgp_fingerprint fingerprint;
360 get_fingerprint(key->publickey, &fingerprint);
361 printf(" Key fingerprint =");
362 for (i = 0; i < fingerprint.length; i++) {
363 if ((fingerprint.length == 16) ||
367 if (fingerprint.length == 20 &&
368 (i * 2) == fingerprint.length) {
369 /* Extra space in the middle of a SHA1 fingerprint */
372 printf("%02X", fingerprint.fp[i]);
379 void display_skshash(struct openpgp_publickey *key, bool html)
384 get_skshash(key, &hash);
385 printf(" Key hash = ");
387 printf("<a href=\"lookup?op=hget&search=");
388 for (i = 0; i < sizeof(hash.hash); i++) {
389 printf("%02X", hash.hash[i]);
393 for (i = 0; i < sizeof(hash.hash); i++) {
394 printf("%02X", hash.hash[i]);
405 * key_index - List a set of OpenPGP keys.
406 * @keys: The keys to display.
407 * @verbose: Should we list sigs as well?
408 * @fingerprint: List the fingerprint?
409 * @html: Should the output be tailored for HTML?
411 * This function takes a list of OpenPGP public keys and displays an index
412 * of them. Useful for debugging or the keyserver Index function.
414 int key_index(struct onak_dbctx *dbctx,
415 struct openpgp_publickey *keys, bool verbose, bool fingerprint,
416 bool skshash, bool html)
418 struct openpgp_signedpacket_list *curuid = NULL;
419 struct tm *created = NULL;
420 time_t created_time = 0;
429 puts("Type bits/keyID Date User ID");
430 while (keys != NULL) {
431 created_time = (keys->publickey->data[1] << 24) +
432 (keys->publickey->data[2] << 16) +
433 (keys->publickey->data[3] << 8) +
434 keys->publickey->data[4];
435 created = gmtime(&created_time);
437 switch (keys->publickey->data[0]) {
440 type = keys->publickey->data[7];
443 type = keys->publickey->data[5];
446 logthing(LOGTHING_ERROR, "Unknown key type: %d",
447 keys->publickey->data[0]);
449 length = keylength(keys->publickey);
451 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
452 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
456 printf("pub %5d%c/<a href=\"lookup?op=get&"
457 "search=0x%016" PRIX64 "\">%08" PRIX64
458 "</a> %04d/%02d/%02d ",
463 created->tm_year + 1900,
467 printf("pub %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
471 created->tm_year + 1900,
477 if (curuid != NULL &&
478 curuid->packet->tag == OPENPGP_PACKET_UID) {
479 snprintf(buf, 1023, "%.*s",
480 (int) curuid->packet->length,
481 curuid->packet->data);
483 printf("<a href=\"lookup?op=vindex&"
484 "search=0x%016" PRIX64 "\">",
488 (html) ? txt2html(buf) : buf,
489 (html) ? "</a>" : "",
490 (keys->revoked) ? " *** REVOKED ***" : "");
492 display_skshash(keys, html);
495 display_fingerprint(keys);
498 list_sigs(dbctx, curuid->sigs, html);
500 curuid = curuid->next;
503 (keys->revoked) ? "*** REVOKED ***": "");
505 display_fingerprint(keys);
509 list_uids(dbctx, keyid, curuid, verbose, html);
511 list_subkeys(dbctx, keys->subkeys, verbose, html);
525 * mrkey_index - List a set of OpenPGP keys in the MRHKP format.
526 * @keys: The keys to display.
528 * This function takes a list of OpenPGP public keys and displays a
529 * machine readable list of them.
531 int mrkey_index(struct openpgp_publickey *keys)
533 struct openpgp_signedpacket_list *curuid = NULL;
534 time_t created_time = 0;
540 struct openpgp_fingerprint fingerprint;
542 while (keys != NULL) {
543 created_time = (keys->publickey->data[1] << 24) +
544 (keys->publickey->data[2] << 16) +
545 (keys->publickey->data[3] << 8) +
546 keys->publickey->data[4];
550 switch (keys->publickey->data[0]) {
553 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
554 logthing(LOGTHING_ERROR, "Couldn't get keyid");
556 printf("%016" PRIX64, keyid);
557 type = keys->publickey->data[7];
560 (void) get_fingerprint(keys->publickey, &fingerprint);
562 for (i = 0; i < fingerprint.length; i++) {
563 printf("%02X", fingerprint.fp[i]);
566 type = keys->publickey->data[5];
569 logthing(LOGTHING_ERROR, "Unknown key type: %d",
570 keys->publickey->data[0]);
572 length = keylength(keys->publickey);
574 printf(":%d:%d:%ld::%s\n",
578 (keys->revoked) ? "r" : "");
580 for (curuid = keys->uids; curuid != NULL;
581 curuid = curuid->next) {
583 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
585 for (i = 0; i < (int) curuid->packet->length;
587 c = curuid->packet->data[i];
591 } else if (c == ':' || c > 127) {