1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
|
/* keylist.c - print keys
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
* 2008 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#ifdef HAVE_DOSISH_SYSTEM
#include <fcntl.h> /* for setmode() */
#endif
#include "gpg.h"
#include "options.h"
#include "packet.h"
#include "status.h"
#include "keydb.h"
#include "photoid.h"
#include "util.h"
#include "ttyio.h"
#include "trustdb.h"
#include "main.h"
#include "i18n.h"
#include "status.h"
static void list_all(int);
static void list_one( strlist_t names, int secret);
static void locate_one (strlist_t names);
static void print_card_serialno (PKT_secret_key *sk);
struct sig_stats
{
int inv_sigs;
int no_key;
int oth_err;
};
/* The stream used to write attribute packets to. */
static FILE *attrib_fp = NULL;
/****************
* List the keys
* If list is NULL, all available keys are listed
*/
void
public_key_list( strlist_t list, int locate_mode )
{
if (opt.with_colons)
{
byte trust_model,marginals,completes,cert_depth;
ulong created,nextcheck;
read_trust_options(&trust_model,&created,&nextcheck,
&marginals,&completes,&cert_depth);
printf("tru:");
if(nextcheck && nextcheck <= make_timestamp())
printf("o");
if(trust_model!=opt.trust_model)
printf("t");
if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
{
if(marginals!=opt.marginals_needed)
printf("m");
if(completes!=opt.completes_needed)
printf("c");
if(cert_depth!=opt.max_cert_depth)
printf("d");
}
printf(":%d:%lu:%lu",trust_model,created,nextcheck);
/* Only show marginals, completes, and cert_depth in the classic
or PGP trust models since they are not meaningful
otherwise. */
if(trust_model==TM_PGP || trust_model==TM_CLASSIC)
printf(":%d:%d:%d",marginals,completes,cert_depth);
printf("\n");
}
/* We need to do the stale check right here because it might need to
update the keyring while we already have the keyring open. This
is very bad for W32 because of a sharing violation. For real OSes
it might lead to false results if we are later listing a keyring
which is associated with the inode of a deleted file. */
check_trustdb_stale ();
if (locate_mode)
locate_one (list);
else if (!list)
list_all (0);
else
list_one (list, 0);
}
void
secret_key_list( strlist_t list )
{
check_trustdb_stale ();
if( !list )
list_all(1);
else /* List by user id */
list_one( list, 1 );
}
void
print_seckey_info (PKT_secret_key *sk)
{
u32 keyid[2];
char *p;
keyid_from_sk (sk, keyid);
p=get_user_id_native(keyid);
tty_printf ("\nsec %4u%c/%s %s %s\n",
nbits_from_sk (sk),
pubkey_letter (sk->pubkey_algo),
keystr(keyid), datestr_from_sk (sk), p);
xfree (p);
}
/* Print information about the public key. With FP passed as NULL,
the tty output interface is used, otherwise output is directted to
the given stream. */
void
print_pubkey_info (FILE *fp, PKT_public_key *pk)
{
u32 keyid[2];
char *p;
keyid_from_pk (pk, keyid);
/* If the pk was chosen by a particular user ID, that is the one to
print. */
if(pk->user_id)
p=utf8_to_native(pk->user_id->name,pk->user_id->len,0);
else
p=get_user_id_native(keyid);
if (fp)
fprintf (fp, "pub %4u%c/%s %s %s\n",
nbits_from_pk (pk),
pubkey_letter (pk->pubkey_algo),
keystr(keyid), datestr_from_pk (pk), p);
else
tty_printf ("\npub %4u%c/%s %s %s\n",
nbits_from_pk (pk), pubkey_letter (pk->pubkey_algo),
keystr(keyid), datestr_from_pk (pk), p);
xfree (p);
}
/* Print basic information of a secret key including the card serial
number information. */
void
print_card_key_info (FILE *fp, KBNODE keyblock)
{
KBNODE node;
int i;
for (node = keyblock; node; node = node->next )
{
if (node->pkt->pkttype == PKT_SECRET_KEY
|| (node->pkt->pkttype == PKT_SECRET_SUBKEY) )
{
PKT_secret_key *sk = node->pkt->pkt.secret_key;
tty_fprintf (fp, "%s%c %4u%c/%s ",
node->pkt->pkttype == PKT_SECRET_KEY? "sec":"ssb",
(sk->protect.s2k.mode==1001)?'#':
(sk->protect.s2k.mode==1002)?'>':' ',
nbits_from_sk (sk),
pubkey_letter (sk->pubkey_algo),
keystr_from_sk(sk));
tty_fprintf (fp, _("created: %s"), datestr_from_sk (sk));
tty_fprintf (fp, " ");
tty_fprintf (fp, _("expires: %s"), expirestr_from_sk (sk));
if (sk->is_protected && sk->protect.s2k.mode == 1002)
{
tty_fprintf (fp, "\n ");
tty_fprintf (fp, _("card-no: "));
if (sk->protect.ivlen == 16
&& !memcmp (sk->protect.iv, "\xD2\x76\x00\x01\x24\x01", 6))
{
/* This is an OpenPGP card. */
for (i=8; i < 14; i++)
{
if (i == 10)
tty_fprintf (fp, " ");
tty_fprintf (fp, "%02X", sk->protect.iv[i]);
}
}
else
{ /* Something is wrong: Print all. */
for (i=0; i < sk->protect.ivlen; i++)
tty_fprintf (fp, "%02X", sk->protect.iv[i]);
}
}
tty_fprintf (fp, "\n");
}
}
}
/* Flags = 0x01 hashed 0x02 critical */
static void
status_one_subpacket(sigsubpkttype_t type,size_t len,int flags,const byte *buf)
{
char status[40];
/* Don't print these. */
if(len>256)
return;
sprintf(status,"%d %u %u ",type,flags,(unsigned int)len);
write_status_text_and_buffer(STATUS_SIG_SUBPACKET,status,buf,len,0);
}
/*
mode=0 for stdout.
mode=1 for log_info + status messages
mode=2 for status messages only
*/
void
show_policy_url(PKT_signature *sig,int indent,int mode)
{
const byte *p;
size_t len;
int seq=0,crit;
FILE *fp=mode?log_get_stream():stdout;
while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,&len,&seq,&crit)))
{
if(mode!=2)
{
int i;
const char *str;
for(i=0;i<indent;i++)
putchar(' ');
if(crit)
str=_("Critical signature policy: ");
else
str=_("Signature policy: ");
if(mode)
log_info("%s",str);
else
printf("%s",str);
print_utf8_string(fp,p,len);
fprintf(fp,"\n");
}
if(mode)
write_status_buffer ( STATUS_POLICY_URL, p, len, 0 );
}
}
/*
mode=0 for stdout.
mode=1 for log_info + status messages
mode=2 for status messages only
*/
/* TODO: use this */
void
show_keyserver_url(PKT_signature *sig,int indent,int mode)
{
const byte *p;
size_t len;
int seq=0,crit;
FILE *fp=mode?log_get_stream():stdout;
while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_PREF_KS,&len,&seq,&crit)))
{
if(mode!=2)
{
int i;
const char *str;
for(i=0;i<indent;i++)
putchar(' ');
if(crit)
str=_("Critical preferred keyserver: ");
else
str=_("Preferred keyserver: ");
if(mode)
log_info("%s",str);
else
printf("%s",str);
print_utf8_string(fp,p,len);
fprintf(fp,"\n");
}
if(mode)
status_one_subpacket(SIGSUBPKT_PREF_KS,len,(crit?0x02:0)|0x01,p);
}
}
/*
mode=0 for stdout.
mode=1 for log_info + status messages
mode=2 for status messages only
which bits:
1 == standard notations
2 == user notations
*/
void
show_notation(PKT_signature *sig,int indent,int mode,int which)
{
FILE *fp=mode?log_get_stream():stdout;
struct notation *nd,*notations;
if(which==0)
which=3;
notations=sig_to_notation(sig);
/* There may be multiple notations in the same sig. */
for(nd=notations;nd;nd=nd->next)
{
if(mode!=2)
{
int has_at=!!strchr(nd->name,'@');
if((which&1 && !has_at) || (which&2 && has_at))
{
int i;
const char *str;
for(i=0;i<indent;i++)
putchar(' ');
if(nd->flags.critical)
str=_("Critical signature notation: ");
else
str=_("Signature notation: ");
if(mode)
log_info("%s",str);
else
printf("%s",str);
/* This is all UTF8 */
print_utf8_string(fp,nd->name,strlen(nd->name));
fprintf(fp,"=");
print_utf8_string(fp,nd->value,strlen(nd->value));
fprintf(fp,"\n");
}
}
if(mode)
{
write_status_buffer(STATUS_NOTATION_NAME,
nd->name,strlen(nd->name),0);
write_status_buffer(STATUS_NOTATION_DATA,
nd->value,strlen(nd->value),50);
}
}
free_notation(notations);
}
static void
print_signature_stats(struct sig_stats *s)
{
if( s->inv_sigs == 1 )
tty_printf(_("1 bad signature\n") );
else if( s->inv_sigs )
tty_printf(_("%d bad signatures\n"), s->inv_sigs );
if( s->no_key == 1 )
tty_printf(_("1 signature not checked due to a missing key\n") );
else if( s->no_key )
tty_printf(_("%d signatures not checked due to missing keys\n"),s->no_key);
if( s->oth_err == 1 )
tty_printf(_("1 signature not checked due to an error\n") );
else if( s->oth_err )
tty_printf(_("%d signatures not checked due to errors\n"), s->oth_err );
}
static void
list_all( int secret )
{
KEYDB_HANDLE hd;
KBNODE keyblock = NULL;
int rc=0;
const char *lastresname, *resname;
struct sig_stats stats;
memset(&stats,0,sizeof(stats));
hd = keydb_new (secret);
if (!hd)
rc = G10ERR_GENERAL;
else
rc = keydb_search_first (hd);
if( rc ) {
if( rc != -1 )
log_error("keydb_search_first failed: %s\n", g10_errstr(rc) );
goto leave;
}
lastresname = NULL;
do {
rc = keydb_get_keyblock (hd, &keyblock);
if (rc) {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
goto leave;
}
if(!opt.with_colons)
{
resname = keydb_get_resource_name (hd);
if (lastresname != resname )
{
int i;
printf("%s\n", resname );
for(i=strlen(resname); i; i-- )
putchar('-');
putchar('\n');
lastresname = resname;
}
}
merge_keys_and_selfsig( keyblock );
list_keyblock( keyblock, secret, opt.fingerprint,
opt.check_sigs?&stats:NULL);
release_kbnode( keyblock );
keyblock = NULL;
} while (!(rc = keydb_search_next (hd)));
if( rc && rc != -1 )
log_error ("keydb_search_next failed: %s\n", g10_errstr(rc));
if(opt.check_sigs && !opt.with_colons)
print_signature_stats(&stats);
leave:
release_kbnode (keyblock);
keydb_release (hd);
}
static void
list_one( strlist_t names, int secret )
{
int rc = 0;
KBNODE keyblock = NULL;
GETKEY_CTX ctx;
const char *resname;
const char *keyring_str = _("Keyring");
int i;
struct sig_stats stats;
memset(&stats,0,sizeof(stats));
/* fixme: using the bynames function has the disadvantage that we
* don't know wether one of the names given was not found. OTOH,
* this function has the advantage to list the names in the
* sequence as defined by the keyDB and does not duplicate
* outputs. A solution could be do test whether all given have
* been listed (this needs a way to use the keyDB search
* functions) or to have the search function return indicators for
* found names. Yet another way is to use the keydb search
* facilities directly. */
if( secret ) {
rc = get_seckey_bynames( &ctx, NULL, names, &keyblock );
if( rc ) {
log_error("error reading key: %s\n", g10_errstr(rc) );
get_seckey_end( ctx );
return;
}
do {
if ((opt.list_options&LIST_SHOW_KEYRING) && !opt.with_colons) {
resname = keydb_get_resource_name (get_ctx_handle(ctx));
printf("%s: %s\n", keyring_str, resname);
for(i = strlen(resname) + strlen(keyring_str) + 2; i; i-- )
putchar('-');
putchar('\n');
}
list_keyblock( keyblock, 1, opt.fingerprint, NULL );
release_kbnode( keyblock );
} while( !get_seckey_next( ctx, NULL, &keyblock ) );
get_seckey_end( ctx );
}
else {
rc = get_pubkey_bynames( &ctx, NULL, names, &keyblock );
if( rc ) {
log_error("error reading key: %s\n", g10_errstr(rc) );
get_pubkey_end( ctx );
return;
}
do {
if ((opt.list_options&LIST_SHOW_KEYRING) && !opt.with_colons) {
resname = keydb_get_resource_name (get_ctx_handle(ctx));
printf("%s: %s\n", keyring_str, resname);
for(i = strlen(resname) + strlen(keyring_str) + 2; i; i-- )
putchar('-');
putchar('\n');
}
list_keyblock( keyblock, 0, opt.fingerprint,
opt.check_sigs?&stats:NULL );
release_kbnode( keyblock );
} while( !get_pubkey_next( ctx, NULL, &keyblock ) );
get_pubkey_end( ctx );
}
if(opt.check_sigs && !opt.with_colons)
print_signature_stats(&stats);
}
static void
locate_one (strlist_t names)
{
int rc = 0;
strlist_t sl;
GETKEY_CTX ctx = NULL;
KBNODE keyblock = NULL;
struct sig_stats stats;
memset (&stats,0,sizeof(stats));
for (sl=names; sl; sl = sl->next)
{
rc = get_pubkey_byname (&ctx, NULL, sl->d, &keyblock, NULL, 1, 0);
if (rc)
{
if (gpg_err_code (rc) != GPG_ERR_NO_PUBKEY)
log_error ("error reading key: %s\n", g10_errstr(rc) );
}
else
{
do
{
list_keyblock (keyblock, 0, opt.fingerprint,
opt.check_sigs? &stats : NULL );
release_kbnode (keyblock);
}
while ( ctx && !get_pubkey_next (ctx, NULL, &keyblock));
get_pubkey_end (ctx);
ctx = NULL;
}
}
if (opt.check_sigs && !opt.with_colons)
print_signature_stats (&stats);
}
static void
print_key_data( PKT_public_key *pk )
{
int n = pk ? pubkey_get_npkey( pk->pubkey_algo ) : 0;
int i;
for(i=0; i < n; i++ ) {
printf("pkd:%d:%u:", i, mpi_get_nbits( pk->pkey[i] ) );
mpi_print(stdout, pk->pkey[i], 1 );
putchar(':');
putchar('\n');
}
}
static void
print_capabilities (PKT_public_key *pk, PKT_secret_key *sk, KBNODE keyblock)
{
if(pk || (sk && sk->protect.s2k.mode!=1001))
{
unsigned int use = pk? pk->pubkey_usage : sk->pubkey_usage;
int c_printed = 0;
if ( use & PUBKEY_USAGE_ENC )
putchar ('e');
if ( use & PUBKEY_USAGE_SIG )
{
putchar ('s');
if( pk? pk->is_primary : sk->is_primary )
{
putchar ('c');
/* The PUBKEY_USAGE_CERT flag was introduced later and
we used to always print 'c' for a primary key. To
avoid any regression here we better track whether we
printed 'c' already. */
c_printed = 1;
}
}
if ( (use & PUBKEY_USAGE_CERT) && !c_printed )
putchar ('c');
if ( (use & PUBKEY_USAGE_AUTH) )
putchar ('a');
}
if ( keyblock ) { /* figure out the usable capabilities */
KBNODE k;
int enc=0, sign=0, cert=0, auth=0, disabled=0;
for (k=keyblock; k; k = k->next ) {
if ( k->pkt->pkttype == PKT_PUBLIC_KEY
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
pk = k->pkt->pkt.public_key;
if(pk->is_primary)
disabled=pk_is_disabled(pk);
if ( pk->is_valid && !pk->is_revoked && !pk->has_expired ) {
if ( pk->pubkey_usage & PUBKEY_USAGE_ENC )
enc = 1;
if ( pk->pubkey_usage & PUBKEY_USAGE_SIG )
{
sign = 1;
if(pk->is_primary)
cert = 1;
}
if ( pk->pubkey_usage & PUBKEY_USAGE_CERT )
cert = 1;
if ( (pk->pubkey_usage & PUBKEY_USAGE_AUTH) )
auth = 1;
}
}
else if ( k->pkt->pkttype == PKT_SECRET_KEY
|| k->pkt->pkttype == PKT_SECRET_SUBKEY ) {
sk = k->pkt->pkt.secret_key;
if ( sk->is_valid && !sk->is_revoked && !sk->has_expired
&& sk->protect.s2k.mode!=1001 ) {
if ( sk->pubkey_usage & PUBKEY_USAGE_ENC )
enc = 1;
if ( sk->pubkey_usage & PUBKEY_USAGE_SIG )
{
sign = 1;
if(sk->is_primary)
cert = 1;
}
if ( (sk->pubkey_usage & PUBKEY_USAGE_CERT) )
cert = 1;
if ( (sk->pubkey_usage & PUBKEY_USAGE_AUTH) )
auth = 1;
}
}
}
if (enc)
putchar ('E');
if (sign)
putchar ('S');
if (cert)
putchar ('C');
if (auth)
putchar ('A');
if (disabled)
putchar ('D');
}
putchar(':');
}
/* Flags = 0x01 hashed 0x02 critical */
static void
print_one_subpacket(sigsubpkttype_t type,size_t len,int flags,const byte *buf)
{
size_t i;
printf("spk:%d:%u:%u:",type,flags,(unsigned int)len);
for(i=0;i<len;i++)
{
/* printable ascii other than : and % */
if(buf[i]>=32 && buf[i]<=126 && buf[i]!=':' && buf[i]!='%')
printf("%c",buf[i]);
else
printf("%%%02X",buf[i]);
}
printf("\n");
}
void
print_subpackets_colon(PKT_signature *sig)
{
byte *i;
assert(opt.show_subpackets);
for(i=opt.show_subpackets;*i;i++)
{
const byte *p;
size_t len;
int seq,crit;
seq=0;
while((p=enum_sig_subpkt(sig->hashed,*i,&len,&seq,&crit)))
print_one_subpacket(*i,len,0x01|(crit?0x02:0),p);
seq=0;
while((p=enum_sig_subpkt(sig->unhashed,*i,&len,&seq,&crit)))
print_one_subpacket(*i,len,0x00|(crit?0x02:0),p);
}
}
void
dump_attribs(const PKT_user_id *uid,PKT_public_key *pk,PKT_secret_key *sk)
{
int i;
if(!attrib_fp)
return;
for(i=0;i<uid->numattribs;i++)
{
if(is_status_enabled())
{
byte array[MAX_FINGERPRINT_LEN], *p;
char buf[(MAX_FINGERPRINT_LEN*2)+90];
size_t j,n;
if(pk)
fingerprint_from_pk( pk, array, &n );
else if(sk)
fingerprint_from_sk( sk, array, &n );
else
BUG();
p = array;
for(j=0; j < n ; j++, p++ )
sprintf(buf+2*j, "%02X", *p );
sprintf(buf+strlen(buf)," %lu %u %u %u %lu %lu %u",
(ulong)uid->attribs[i].len,uid->attribs[i].type,i+1,
uid->numattribs,(ulong)uid->created,(ulong)uid->expiredate,
((uid->is_primary?0x01:0)|
(uid->is_revoked?0x02:0)|
(uid->is_expired?0x04:0)));
write_status_text(STATUS_ATTRIBUTE,buf);
}
fwrite(uid->attribs[i].data,uid->attribs[i].len,1,attrib_fp);
fflush (attrib_fp);
}
}
static void
list_keyblock_print ( KBNODE keyblock, int secret, int fpr, void *opaque )
{
int rc = 0;
KBNODE kbctx;
KBNODE node;
PKT_public_key *pk;
PKT_secret_key *sk;
struct sig_stats *stats=opaque;
int skip_sigs=0;
/* get the keyid from the keyblock */
node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
if( !node ) {
log_error("Oops; key lost!\n");
dump_kbnode( keyblock );
return;
}
if( secret )
{
pk = NULL;
sk = node->pkt->pkt.secret_key;
printf("sec%c %4u%c/%s %s",(sk->protect.s2k.mode==1001)?'#':
(sk->protect.s2k.mode==1002)?'>':' ',
nbits_from_sk( sk ),pubkey_letter( sk->pubkey_algo ),
keystr_from_sk(sk),datestr_from_sk( sk ));
if(sk->has_expired)
{
printf(" [");
printf(_("expired: %s"),expirestr_from_sk(sk));
printf("]");
}
else if(sk->expiredate )
{
printf(" [");
printf(_("expires: %s"),expirestr_from_sk(sk));
printf("]");
}
printf("\n");
}
else
{
pk = node->pkt->pkt.public_key;
sk = NULL;
check_trustdb_stale();
printf("pub %4u%c/%s %s",
nbits_from_pk(pk),pubkey_letter(pk->pubkey_algo),
keystr_from_pk(pk),datestr_from_pk( pk ));
/* We didn't include this before in the key listing, but there
is room in the new format, so why not? */
if(pk->is_revoked)
{
printf(" [");
printf(_("revoked: %s"),revokestr_from_pk(pk));
printf("]");
}
else if(pk->has_expired)
{
printf(" [");
printf(_("expired: %s"),expirestr_from_pk(pk));
printf("]");
}
else if(pk->expiredate)
{
printf(" [");
printf(_("expires: %s"),expirestr_from_pk(pk));
printf("]");
}
#if 0
/* I need to think about this some more. It's easy enough to
include, but it looks sort of confusing in the
listing... */
if(opt.list_options&LIST_SHOW_VALIDITY)
{
int validity=get_validity(pk,NULL);
printf(" [%s]",trust_value_to_string(validity));
}
#endif
printf("\n");
}
if( fpr )
print_fingerprint( pk, sk, 0 );
print_card_serialno (sk);
if( opt.with_key_data )
print_key_data( pk );
for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
if( node->pkt->pkttype == PKT_USER_ID && !opt.fast_list_mode ) {
PKT_user_id *uid=node->pkt->pkt.user_id;
if(pk && (uid->is_expired || uid->is_revoked)
&& !(opt.list_options&LIST_SHOW_UNUSABLE_UIDS))
{
skip_sigs=1;
continue;
}
else
skip_sigs=0;
if(attrib_fp && uid->attrib_data!=NULL)
dump_attribs(uid,pk,sk);
if((uid->is_revoked || uid->is_expired)
|| ((opt.list_options&LIST_SHOW_UID_VALIDITY) && pk))
{
const char *validity;
int indent;
validity=uid_trust_string_fixed(pk,uid);
indent=(keystrlen()+9)-atoi(uid_trust_string_fixed(NULL,NULL));
if(indent<0 || indent>40)
indent=0;
printf("uid%*s%s ",indent,"",validity);
}
else
printf("uid%*s", (int)keystrlen()+10,"");
print_utf8_string( stdout, uid->name, uid->len );
putchar('\n');
if((opt.list_options&LIST_SHOW_PHOTOS) && uid->attribs!=NULL)
show_photos(uid->attribs,uid->numattribs,pk,sk,uid);
}
else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
{
PKT_public_key *pk2 = node->pkt->pkt.public_key;
if((pk2->is_revoked || pk2->has_expired)
&& !(opt.list_options&LIST_SHOW_UNUSABLE_SUBKEYS))
{
skip_sigs=1;
continue;
}
else
skip_sigs=0;
printf("sub %4u%c/%s %s",
nbits_from_pk( pk2 ),pubkey_letter( pk2->pubkey_algo ),
keystr_from_pk(pk2),datestr_from_pk(pk2));
if( pk2->is_revoked )
{
printf(" [");
printf(_("revoked: %s"),revokestr_from_pk(pk2));
printf("]");
}
else if( pk2->has_expired )
{
printf(" [");
printf(_("expired: %s"),expirestr_from_pk(pk2));
printf("]");
}
else if( pk2->expiredate )
{
printf(" [");
printf(_("expires: %s"),expirestr_from_pk(pk2));
printf("]");
}
putchar('\n');
if( fpr > 1 )
print_fingerprint( pk2, NULL, 0 );
if( opt.with_key_data )
print_key_data( pk2 );
}
else if( node->pkt->pkttype == PKT_SECRET_SUBKEY )
{
PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
printf("ssb%c %4u%c/%s %s",
(sk2->protect.s2k.mode==1001)?'#':
(sk2->protect.s2k.mode==1002)?'>':' ',
nbits_from_sk( sk2 ),pubkey_letter( sk2->pubkey_algo ),
keystr_from_sk(sk2),datestr_from_sk( sk2 ) );
if( sk2->expiredate )
{
printf(" [");
printf(_("expires: %s"),expirestr_from_sk(sk2));
printf("]");
}
putchar('\n');
if( fpr > 1 )
{
print_fingerprint( NULL, sk2, 0 );
print_card_serialno (sk2);
}
}
else if( opt.list_sigs
&& node->pkt->pkttype == PKT_SIGNATURE
&& !skip_sigs ) {
PKT_signature *sig = node->pkt->pkt.signature;
int sigrc;
char *sigstr;
if( stats ) {
/*fflush(stdout);*/
rc = check_key_signature( keyblock, node, NULL );
switch( gpg_err_code (rc) ) {
case 0: sigrc = '!'; break;
case GPG_ERR_BAD_SIGNATURE:
stats->inv_sigs++; sigrc = '-'; break;
case GPG_ERR_NO_PUBKEY:
case GPG_ERR_UNUSABLE_PUBKEY: stats->no_key++; continue;
default: stats->oth_err++; sigrc = '%'; break;
}
/* TODO: Make sure a cached sig record here still has
the pk that issued it. See also
keyedit.c:print_and_check_one_sig */
}
else {
rc = 0;
sigrc = ' ';
}
if( sig->sig_class == 0x20 || sig->sig_class == 0x28
|| sig->sig_class == 0x30 )
sigstr = "rev";
else if( (sig->sig_class&~3) == 0x10 )
sigstr = "sig";
else if( sig->sig_class == 0x18 )
sigstr = "sig";
else if( sig->sig_class == 0x1F )
sigstr = "sig";
else {
printf("sig "
"[unexpected signature class 0x%02x]\n",sig->sig_class );
continue;
}
fputs( sigstr, stdout );
printf("%c%c %c%c%c%c%c%c %s %s",
sigrc,(sig->sig_class-0x10>0 &&
sig->sig_class-0x10<4)?'0'+sig->sig_class-0x10:' ',
sig->flags.exportable?' ':'L',
sig->flags.revocable?' ':'R',
sig->flags.policy_url?'P':' ',
sig->flags.notation?'N':' ',
sig->flags.expired?'X':' ',
(sig->trust_depth>9)?'T':
(sig->trust_depth>0)?'0'+sig->trust_depth:' ',
keystr(sig->keyid),datestr_from_sig(sig));
if(opt.list_options&LIST_SHOW_SIG_EXPIRE)
printf(" %s", expirestr_from_sig(sig));
printf(" ");
if( sigrc == '%' )
printf("[%s] ", g10_errstr(rc) );
else if( sigrc == '?' )
;
else if ( !opt.fast_list_mode ) {
size_t n;
char *p = get_user_id( sig->keyid, &n );
print_utf8_string( stdout, p, n );
xfree(p);
}
putchar('\n');
if(sig->flags.policy_url
&& (opt.list_options&LIST_SHOW_POLICY_URLS))
show_policy_url(sig,3,0);
if(sig->flags.notation && (opt.list_options&LIST_SHOW_NOTATIONS))
show_notation(sig,3,0,
((opt.list_options&LIST_SHOW_STD_NOTATIONS)?1:0)+
((opt.list_options&LIST_SHOW_USER_NOTATIONS)?2:0));
if(sig->flags.pref_ks
&& (opt.list_options&LIST_SHOW_KEYSERVER_URLS))
show_keyserver_url(sig,3,0);
/* fixme: check or list other sigs here */
}
}
putchar('\n');
}
void
print_revokers(PKT_public_key *pk)
{
/* print the revoker record */
if( !pk->revkey && pk->numrevkeys )
BUG();
else
{
int i,j;
for (i=0; i < pk->numrevkeys; i++)
{
byte *p;
printf ("rvk:::%d::::::", pk->revkey[i].algid);
p = pk->revkey[i].fpr;
for (j=0; j < 20; j++, p++ )
printf ("%02X", *p);
printf (":%02x%s:\n", pk->revkey[i].class,
(pk->revkey[i].class&0x40)?"s":"");
}
}
}
static void
list_keyblock_colon( KBNODE keyblock, int secret, int fpr )
{
int rc = 0;
KBNODE kbctx;
KBNODE node;
PKT_public_key *pk;
PKT_secret_key *sk;
u32 keyid[2];
int trustletter = 0;
int ulti_hack = 0;
int i;
/* get the keyid from the keyblock */
node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
if ( !node )
{
log_error("Oops; key lost!\n");
dump_kbnode( keyblock );
return;
}
if ( secret )
{
pk = NULL;
sk = node->pkt->pkt.secret_key;
keyid_from_sk ( sk, keyid );
printf ("sec::%u:%d:%08lX%08lX:%s:%s:::",
nbits_from_sk( sk ),
sk->pubkey_algo,
(ulong)keyid[0],(ulong)keyid[1],
colon_datestr_from_sk( sk ),
colon_strtime (sk->expiredate)
/* fixme: add LID here */ );
}
else
{
pk = node->pkt->pkt.public_key;
sk = NULL;
keyid_from_pk( pk, keyid );
fputs( "pub:", stdout );
if ( !pk->is_valid )
putchar ('i');
else if ( pk->is_revoked )
putchar ('r');
else if ( pk->has_expired )
putchar ('e');
else if ( opt.fast_list_mode || opt.no_expensive_trust_checks )
;
else
{
trustletter = get_validity_info ( pk, NULL );
if ( trustletter == 'u' )
ulti_hack = 1;
putchar(trustletter);
}
printf (":%u:%d:%08lX%08lX:%s:%s::",
nbits_from_pk( pk ),
pk->pubkey_algo,
(ulong)keyid[0],(ulong)keyid[1],
colon_datestr_from_pk( pk ),
colon_strtime (pk->expiredate) );
if ( !opt.fast_list_mode && !opt.no_expensive_trust_checks )
putchar( get_ownertrust_info(pk) );
putchar(':');
}
putchar (':');
putchar (':');
print_capabilities (pk, sk, keyblock);
if (secret)
{
putchar (':'); /* End of field 13. */
putchar (':'); /* End of field 14. */
if (sk->protect.s2k.mode == 1001)
putchar ('#'); /* Key is just a stub. */
else if (sk->protect.s2k.mode == 1002)
{
/* Key is stored on an external token (card) or handled by
the gpg-agent. Print the serial number of that token
here. */
for (i=0; i < sk->protect.ivlen; i++)
printf ("%02X", sk->protect.iv[i]);
}
putchar (':'); /* End of field 15. */
}
putchar('\n');
if (pk)
print_revokers (pk);
if (fpr)
print_fingerprint (pk, sk, 0);
if (opt.with_key_data)
print_key_data (pk);
for ( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; )
{
if ( node->pkt->pkttype == PKT_USER_ID && !opt.fast_list_mode )
{
char *str;
PKT_user_id *uid=node->pkt->pkt.user_id;
if (attrib_fp && node->pkt->pkt.user_id->attrib_data != NULL)
dump_attribs (node->pkt->pkt.user_id,pk,sk);
/*
* Fixme: We need a is_valid flag here too
*/
str = uid->attrib_data? "uat":"uid";
/* If we're listing a secret key, leave out the validity
values for now. This is handled better in 1.9. */
if (sk)
printf ("%s:::::",str);
else if ( uid->is_revoked )
printf ("%s:r::::",str);
else if ( uid->is_expired )
printf ("%s:e::::",str);
else if ( opt.no_expensive_trust_checks )
printf ("%s:::::",str);
else
{
int uid_validity;
if ( pk && !ulti_hack )
uid_validity=get_validity_info (pk, uid);
else
uid_validity = 'u';
printf ("%s:%c::::",str,uid_validity);
}
printf ("%s:", colon_strtime (uid->created));
printf ("%s:", colon_strtime (uid->expiredate));
namehash_from_uid (uid);
for (i=0; i < 20; i++ )
printf ("%02X",uid->namehash[i]);
printf ("::");
if (uid->attrib_data)
printf ("%u %lu",uid->numattribs,uid->attrib_len);
else
print_string (stdout,uid->name,uid->len, ':' );
putchar (':');
putchar ('\n');
}
else if ( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
{
u32 keyid2[2];
PKT_public_key *pk2 = node->pkt->pkt.public_key;
keyid_from_pk ( pk2, keyid2 );
fputs ("sub:", stdout );
if ( !pk2->is_valid )
putchar ('i');
else if ( pk2->is_revoked )
putchar ('r');
else if ( pk2->has_expired )
putchar ('e');
else if ( opt.fast_list_mode || opt.no_expensive_trust_checks )
;
else
{
/* TRUSTLETTER should always be defined here. */
if (trustletter)
printf ("%c", trustletter );
}
printf(":%u:%d:%08lX%08lX:%s:%s:::::",
nbits_from_pk( pk2 ),
pk2->pubkey_algo,
(ulong)keyid2[0],(ulong)keyid2[1],
colon_datestr_from_pk( pk2 ),
colon_strtime (pk2->expiredate)
/* fixme: add LID and ownertrust here */
);
print_capabilities (pk2, NULL, NULL);
putchar ('\n');
if ( fpr > 1 )
print_fingerprint ( pk2, NULL, 0 );
if ( opt.with_key_data )
print_key_data( pk2 );
}
else if( node->pkt->pkttype == PKT_SECRET_SUBKEY )
{
u32 keyid2[2];
PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
keyid_from_sk ( sk2, keyid2 );
printf ("ssb::%u:%d:%08lX%08lX:%s:%s:::::",
nbits_from_sk( sk2 ),
sk2->pubkey_algo,
(ulong)keyid2[0],(ulong)keyid2[1],
colon_datestr_from_sk( sk2 ),
colon_strtime (sk2->expiredate)
/* fixme: add LID */ );
print_capabilities (NULL, sk2, NULL);
putchar(':'); /* End of field 13. */
putchar(':'); /* End of field 14. */
if (sk2->protect.s2k.mode == 1001)
putchar ('#'); /* Key is just a stub. */
else if (sk2->protect.s2k.mode == 1002)
{
/* Key is stored on an external token (card) or handled by
the gpg-agent. Print the serial number of that token
here. */
for (i=0; i < sk2->protect.ivlen; i++)
printf ("%02X", sk2->protect.iv[i]);
}
putchar(':'); /* End of field 15. */
putchar ('\n');
if ( fpr > 1 )
print_fingerprint ( NULL, sk2, 0 );
}
else if ( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE )
{
PKT_signature *sig = node->pkt->pkt.signature;
int sigrc,fprokay=0;
char *sigstr;
size_t fplen;
byte fparray[MAX_FINGERPRINT_LEN];
if ( sig->sig_class == 0x20 || sig->sig_class == 0x28
|| sig->sig_class == 0x30 )
sigstr = "rev";
else if ( (sig->sig_class&~3) == 0x10 )
sigstr = "sig";
else if ( sig->sig_class == 0x18 )
sigstr = "sig";
else if ( sig->sig_class == 0x1F )
sigstr = "sig";
else
{
printf ("sig::::::::::%02x%c:\n",
sig->sig_class, sig->flags.exportable?'x':'l');
continue;
}
if ( opt.check_sigs )
{
PKT_public_key *signer_pk=NULL;
fflush (stdout);
if (opt.no_sig_cache)
signer_pk = xmalloc_clear (sizeof(PKT_public_key));
rc = check_key_signature2 ( keyblock, node, NULL, signer_pk,
NULL, NULL, NULL );
switch ( gpg_err_code (rc) )
{
case 0: sigrc = '!'; break;
case GPG_ERR_BAD_SIGNATURE: sigrc = '-'; break;
case GPG_ERR_NO_PUBKEY:
case GPG_ERR_UNUSABLE_PUBKEY: sigrc = '?'; break;
default: sigrc = '%'; break;
}
if (opt.no_sig_cache)
{
if (!rc)
{
fingerprint_from_pk (signer_pk, fparray, &fplen);
fprokay = 1;
}
free_public_key(signer_pk);
}
}
else
{
rc = 0;
sigrc = ' ';
}
fputs ( sigstr, stdout );
putchar (':');
if ( sigrc != ' ' )
putchar (sigrc);
printf ("::%d:%08lX%08lX:%s:%s:", sig->pubkey_algo,
(ulong)sig->keyid[0], (ulong)sig->keyid[1],
colon_datestr_from_sig(sig),
colon_expirestr_from_sig(sig));
if (sig->trust_depth || sig->trust_value)
printf("%d %d",sig->trust_depth,sig->trust_value);
printf (":");
if (sig->trust_regexp)
print_string (stdout,sig->trust_regexp,
strlen(sig->trust_regexp),':');
printf(":");
if ( sigrc == '%' )
printf("[%s] ", g10_errstr(rc) );
else if ( sigrc == '?' )
;
else if ( !opt.fast_list_mode )
{
size_t n;
char *p = get_user_id( sig->keyid, &n );
print_string( stdout, p, n, ':' );
xfree(p);
}
printf (":%02x%c:", sig->sig_class,sig->flags.exportable?'x':'l');
if (opt.no_sig_cache && opt.check_sigs && fprokay)
{
putchar (':');
for (i=0; i < fplen ; i++ )
printf ("%02X", fparray[i] );
putchar (':');
}
printf ("\n");
if (opt.show_subpackets)
print_subpackets_colon (sig);
/* fixme: check or list other sigs here */
}
}
}
/*
* Reorder the keyblock so that the primary user ID (and not attribute
* packet) comes first. Fixme: Replace this by a generic sort
* function. */
static void
do_reorder_keyblock (KBNODE keyblock,int attr)
{
KBNODE primary = NULL, primary0 = NULL, primary2 = NULL;
KBNODE last, node;
for (node=keyblock; node; primary0=node, node = node->next) {
if( node->pkt->pkttype == PKT_USER_ID &&
((attr && node->pkt->pkt.user_id->attrib_data) ||
(!attr && !node->pkt->pkt.user_id->attrib_data)) &&
node->pkt->pkt.user_id->is_primary ) {
primary = primary2 = node;
for (node=node->next; node; primary2=node, node = node->next ) {
if( node->pkt->pkttype == PKT_USER_ID
|| node->pkt->pkttype == PKT_PUBLIC_SUBKEY
|| node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
break;
}
}
break;
}
}
if ( !primary )
return; /* no primary key flag found (should not happen) */
for (last=NULL, node=keyblock; node; last = node, node = node->next) {
if( node->pkt->pkttype == PKT_USER_ID )
break;
}
assert (node);
assert (last); /* the user ID is never the first packet */
assert (primary0); /* ditto (this is the node before primary) */
if ( node == primary )
return; /* already the first one */
last->next = primary;
primary0->next = primary2->next;
primary2->next = node;
}
void
reorder_keyblock (KBNODE keyblock)
{
do_reorder_keyblock(keyblock,1);
do_reorder_keyblock(keyblock,0);
}
void
list_keyblock( KBNODE keyblock, int secret, int fpr, void *opaque )
{
reorder_keyblock (keyblock);
if (opt.with_colons)
list_keyblock_colon (keyblock, secret, fpr );
else
list_keyblock_print (keyblock, secret, fpr, opaque );
}
/*
* standard function to print the finperprint.
* mode 0: as used in key listings, opt.with_colons is honored
* 1: print using log_info ()
* 2: direct use of tty
* 3: direct use of tty but only primary key.
* modes 1 and 2 will try and print both subkey and primary key fingerprints
*/
void
print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode )
{
byte array[MAX_FINGERPRINT_LEN], *p;
size_t i, n;
FILE *fp;
const char *text;
int primary=0;
if(sk)
{
if(sk->main_keyid[0]==sk->keyid[0] && sk->main_keyid[1]==sk->keyid[1])
primary=1;
}
else
{
if(pk->main_keyid[0]==pk->keyid[0] && pk->main_keyid[1]==pk->keyid[1])
primary=1;
}
/* Just to be safe */
if(mode&0x80 && !primary)
{
log_error("primary key is not really primary!\n");
return;
}
mode&=~0x80;
if(!primary && (mode==1 || mode==2))
{
if(sk)
{
PKT_secret_key *primary_sk=xmalloc_clear(sizeof(*primary_sk));
get_seckey(primary_sk,sk->main_keyid);
print_fingerprint(NULL,primary_sk,mode|0x80);
free_secret_key(primary_sk);
}
else
{
PKT_public_key *primary_pk=xmalloc_clear(sizeof(*primary_pk));
get_pubkey(primary_pk,pk->main_keyid);
print_fingerprint(primary_pk,NULL,mode|0x80);
free_public_key(primary_pk);
}
}
if (mode == 1) {
fp = log_get_stream ();
if(primary)
text = _("Primary key fingerprint:");
else
text = _(" Subkey fingerprint:");
}
else if (mode == 2) {
fp = NULL; /* use tty */
if(primary)
/* TRANSLATORS: this should fit into 24 bytes to that the
* fingerprint data is properly aligned with the user ID */
text = _(" Primary key fingerprint:");
else
text = _(" Subkey fingerprint:");
}
else if (mode == 3) {
fp = NULL; /* use tty */
text = _(" Key fingerprint =");
}
else {
fp = stdout;
text = _(" Key fingerprint =");
}
if (sk)
fingerprint_from_sk (sk, array, &n);
else
fingerprint_from_pk (pk, array, &n);
p = array;
if (opt.with_colons && !mode) {
fprintf (fp, "fpr:::::::::");
for (i=0; i < n ; i++, p++ )
fprintf (fp, "%02X", *p );
putc(':', fp);
}
else {
if (fp)
fputs (text, fp);
else
tty_printf ("%s", text);
if (n == 20) {
for (i=0; i < n ; i++, i++, p += 2 ) {
if (fp) {
if (i == 10 )
putc(' ', fp);
fprintf (fp, " %02X%02X", *p, p[1] );
}
else {
if (i == 10 )
tty_printf (" ");
tty_printf (" %02X%02X", *p, p[1]);
}
}
}
else {
for (i=0; i < n ; i++, p++ ) {
if (fp) {
if (i && !(i%8) )
putc (' ', fp);
fprintf (fp, " %02X", *p );
}
else {
if (i && !(i%8) )
tty_printf (" ");
tty_printf (" %02X", *p );
}
}
}
}
if (fp)
putc ('\n', fp);
else
tty_printf ("\n");
}
/* Print the serial number of an OpenPGP card if available. */
static void
print_card_serialno (PKT_secret_key *sk)
{
int i;
if (!sk)
return;
if (!sk->is_protected || sk->protect.s2k.mode != 1002)
return; /* Not a card. */
if (opt.with_colons)
return; /* Handled elsewhere. */
fputs (_(" Card serial no. ="), stdout);
putchar (' ');
if (sk->protect.ivlen == 16
&& !memcmp (sk->protect.iv, "\xD2\x76\x00\x01\x24\x01", 6) )
{ /* This is an OpenPGP card. Just print the relevant part. */
for (i=8; i < 14; i++)
{
if (i == 10)
putchar (' ');
printf ("%02X", sk->protect.iv[i]);
}
}
else
{ /* Something is wrong: Print all. */
for (i=0; i < sk->protect.ivlen; i++)
printf ("%02X", sk->protect.iv[i]);
}
putchar ('\n');
}
void
set_attrib_fd (int fd)
{
static int last_fd=-1;
if ( fd != -1 && last_fd == fd )
return;
if ( attrib_fp && attrib_fp != stdout && attrib_fp != stderr
&& attrib_fp != log_get_stream () )
fclose (attrib_fp);
attrib_fp = NULL;
if ( fd == -1 )
return;
#ifdef HAVE_DOSISH_SYSTEM
setmode (fd, O_BINARY);
#endif
if( fd == 1 )
attrib_fp = stdout;
else if( fd == 2 )
attrib_fp = stderr;
else
attrib_fp = fdopen (fd, "wb");
if (!attrib_fp)
{
log_fatal("can't open fd %d for attribute output: %s\n",
fd, strerror(errno));
}
last_fd = fd;
}
|