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
|
/**
* @file
* Gpgme functions
*
* @authors
* Copyright (C) 2022-2023 Richard Russon <rich@flatcap.org>
* Copyright (C) 2024 Alejandro Colomar <alx@kernel.org>
*
* @copyright
* This program 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 2 of the License, or (at your option) any later
* version.
*
* This program 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/>.
*/
/**
* @page crypt_gpgme_functions Gpgme functions
*
* Gpgme functions
*/
#include "config.h"
#include <ctype.h>
#include <gpg-error.h>
#include <gpgme.h>
#include <langinfo.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "gpgme_functions.h"
#include "lib.h"
#include "menu/lib.h"
#include "pager/lib.h"
#include "question/lib.h"
#include "crypt_gpgme.h"
#include "globals.h"
#include "mutt_logging.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/// Number of padding spaces needed after each of the strings in #KeyInfoPrompts after translation
int KeyInfoPadding[KIP_MAX] = { 0 };
/// Names of header fields used in the pgp key display, e.g. Name:, Fingerprint:
static const char *const KeyInfoPrompts[] = {
/* L10N: The following are the headers for the "verify key" output from the
GPGME key selection menu (bound to "c" in the key selection menu).
They will be automatically aligned. */
N_("Name: "), N_("aka: "), N_("Valid From: "), N_("Valid To: "),
N_("Key Type: "), N_("Key Usage: "), N_("Fingerprint: "), N_("Serial-No: "),
N_("Issued By: "), N_("Subkey: ")
};
/**
* struct DnArray - An X500 Distinguished Name
*/
struct DnArray
{
char *key; ///< Key
char *value; ///< Value
};
/**
* print_utf8 - Write a UTF-8 string to a file
* @param fp File to write to
* @param buf Buffer to read from
* @param len Length to read
*
* Convert the character set.
*/
static void print_utf8(FILE *fp, const char *buf, size_t len)
{
char *tstr = MUTT_MEM_MALLOC(len + 1, char);
memcpy(tstr, buf, len);
tstr[len] = 0;
/* fromcode "utf-8" is sure, so we don't want
* charset-hook corrections: flags must be 0. */
mutt_ch_convert_string(&tstr, "utf-8", cc_charset(), MUTT_ICONV_NO_FLAGS);
fputs(tstr, fp);
FREE(&tstr);
}
/**
* print_dn_part - Print the X.500 Distinguished Name
* @param fp File to write to
* @param dn Distinguished Name
* @param key Key string
* @retval true Any DN keys match the given key string
* @retval false Otherwise
*
* Print the X.500 Distinguished Name part KEY from the array of parts DN to FP.
*/
static bool print_dn_part(FILE *fp, struct DnArray *dn, const char *key)
{
bool any = false;
for (; dn->key; dn++)
{
if (mutt_str_equal(dn->key, key))
{
if (any)
fputs(" + ", fp);
print_utf8(fp, dn->value, strlen(dn->value));
any = true;
}
}
return any;
}
/**
* print_dn_parts - Print all parts of a DN in a standard sequence
* @param fp File to write to
* @param dn Array of Distinguished Names
*/
static void print_dn_parts(FILE *fp, struct DnArray *dn)
{
static const char *const stdpart[] = {
"CN", "OU", "O", "STREET", "L", "ST", "C", NULL,
};
bool any = false;
bool any2 = false;
for (int i = 0; stdpart[i]; i++)
{
if (any)
fputs(", ", fp);
any = print_dn_part(fp, dn, stdpart[i]);
}
/* now print the rest without any specific ordering */
for (; dn->key; dn++)
{
int i;
for (i = 0; stdpart[i]; i++)
{
if (mutt_str_equal(dn->key, stdpart[i]))
break;
}
if (!stdpart[i])
{
if (any)
fputs(", ", fp);
if (!any2)
fputs("(", fp);
any = print_dn_part(fp, dn, dn->key);
any2 = true;
}
}
if (any2)
fputs(")", fp);
}
/**
* parse_dn_part - Parse an RDN
* @param array Array for results
* @param str String to parse
* @retval ptr First character after Distinguished Name
*
* This is a helper to parse_dn()
*/
static const char *parse_dn_part(struct DnArray *array, const char *str)
{
const char *s = NULL, *s1 = NULL;
int n;
char *p = NULL;
/* parse attribute type */
for (s = str + 1; (s[0] != '\0') && (s[0] != '='); s++)
; // do nothing
if (s[0] == '\0')
return NULL; /* error */
n = s - str;
if (n == 0)
return NULL; /* empty key */
array->key = MUTT_MEM_MALLOC(n + 1, char);
p = array->key;
memcpy(p, str, n); /* fixme: trim trailing spaces */
p[n] = 0;
str = s + 1;
if (*str == '#')
{ /* hexstring */
str++;
for (s = str; isxdigit(*s); s++)
s++;
n = s - str;
if ((n == 0) || (n & 1))
return NULL; /* empty or odd number of digits */
n /= 2;
p = MUTT_MEM_MALLOC(n + 1, char);
array->value = (char *) p;
for (s1 = str; n > 0; s1 += 2, n--)
sscanf(s1, "%2hhx", (unsigned char *) p++);
*p = '\0';
}
else
{ /* regular v3 quoted string */
for (n = 0, s = str; *s; s++)
{
if (*s == '\\')
{ /* pair */
s++;
if ((*s == ',') || (*s == '=') || (*s == '+') || (*s == '<') || (*s == '>') ||
(*s == '#') || (*s == ';') || (*s == '\\') || (*s == '\"') || (*s == ' '))
{
n++;
}
else if (isxdigit(s[0]) && isxdigit(s[1]))
{
s++;
n++;
}
else
{
return NULL; /* invalid escape sequence */
}
}
else if (*s == '\"')
{
return NULL; /* invalid encoding */
}
else if ((*s == ',') || (*s == '=') || (*s == '+') || (*s == '<') ||
(*s == '>') || (*s == '#') || (*s == ';'))
{
break;
}
else
{
n++;
}
}
p = MUTT_MEM_MALLOC(n + 1, char);
array->value = (char *) p;
for (s = str; n > 0; s++, n--)
{
if (*s == '\\')
{
s++;
if (isxdigit(*s))
{
sscanf(s, "%2hhx", (unsigned char *) p++);
s++;
}
else
{
*p++ = *s;
}
}
else
{
*p++ = *s;
}
}
*p = '\0';
}
return s;
}
/**
* parse_dn - Parse a DN and return an array-ized one
* @param str String to parse
* @retval ptr Array of Distinguished Names
*
* This is not a validating parser and it does not support any old-stylish
* syntax; GPGME is expected to return only rfc2253 compatible strings.
*/
static struct DnArray *parse_dn(const char *str)
{
struct DnArray *array = NULL;
size_t arrayidx, arraysize;
arraysize = 7; /* C,ST,L,O,OU,CN,email */
array = MUTT_MEM_MALLOC(arraysize + 1, struct DnArray);
arrayidx = 0;
while (*str)
{
while (str[0] == ' ')
str++;
if (str[0] == '\0')
break; /* ready */
if (arrayidx >= arraysize)
{
/* neomutt lacks a real mutt_mem_realloc - so we need to copy */
arraysize += 5;
struct DnArray *a2 = MUTT_MEM_MALLOC(arraysize + 1, struct DnArray);
for (int i = 0; i < arrayidx; i++)
{
a2[i].key = array[i].key;
a2[i].value = array[i].value;
}
FREE(&array);
array = a2;
}
array[arrayidx].key = NULL;
array[arrayidx].value = NULL;
str = parse_dn_part(array + arrayidx, str);
arrayidx++;
if (!str)
goto failure;
while (str[0] == ' ')
str++;
if ((str[0] != '\0') && (str[0] != ',') && (str[0] != ';') && (str[0] != '+'))
goto failure; /* invalid delimiter */
if (str[0] != '\0')
str++;
}
array[arrayidx].key = NULL;
array[arrayidx].value = NULL;
return array;
failure:
for (int i = 0; i < arrayidx; i++)
{
FREE(&array[i].key);
FREE(&array[i].value);
}
FREE(&array);
return NULL;
}
/**
* parse_and_print_user_id - Print a nice representation of the userid
* @param fp File to write to
* @param userid String returned by GPGME key functions (utf-8 encoded)
*
* Make sure it is displayed in a proper way, which does mean to reorder some
* parts for S/MIME's DNs.
*/
static void parse_and_print_user_id(FILE *fp, const char *userid)
{
const char *s = NULL;
if (*userid == '<')
{
s = strchr(userid + 1, '>');
if (s)
print_utf8(fp, userid + 1, s - userid - 1);
}
else if (*userid == '(')
{
fputs(_("[Can't display this user ID (unknown encoding)]"), fp);
}
else if (!isalnum(userid[0]))
{
fputs(_("[Can't display this user ID (invalid encoding)]"), fp);
}
else
{
struct DnArray *dn = parse_dn(userid);
if (dn)
{
print_dn_parts(fp, dn);
for (int i = 0; dn[i].key; i++)
{
FREE(&dn[i].key);
FREE(&dn[i].value);
}
FREE(&dn);
}
else
{
fputs(_("[Can't display this user ID (invalid DN)]"), fp);
}
}
}
/**
* print_key_info - Verbose information about a key or certificate to a file
* @param key Key to use
* @param fp File to write to
*/
static void print_key_info(gpgme_key_t key, FILE *fp)
{
int idx;
const char *s = NULL, *s2 = NULL;
time_t tt = 0;
char shortbuf[128] = { 0 };
unsigned long aval = 0;
const char *delim = NULL;
gpgme_user_id_t uid = NULL;
static int max_header_width = 0;
if (max_header_width == 0)
{
for (int i = 0; i < KIP_MAX; i++)
{
KeyInfoPadding[i] = mutt_str_len(_(KeyInfoPrompts[i]));
const int width = mutt_strwidth(_(KeyInfoPrompts[i]));
if (max_header_width < width)
max_header_width = width;
KeyInfoPadding[i] -= width;
}
for (int i = 0; i < KIP_MAX; i++)
KeyInfoPadding[i] += max_header_width;
}
bool is_pgp = (key->protocol == GPGME_PROTOCOL_OpenPGP);
for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
{
if (uid->revoked)
continue;
s = uid->uid;
/* L10N: DOTFILL */
if (idx == 0)
fprintf(fp, "%*s", KeyInfoPadding[KIP_NAME], _(KeyInfoPrompts[KIP_NAME]));
else
fprintf(fp, "%*s", KeyInfoPadding[KIP_AKA], _(KeyInfoPrompts[KIP_AKA]));
if (uid->invalid)
{
/* L10N: comes after the Name or aka if the key is invalid */
fputs(_("[Invalid]"), fp);
putc(' ', fp);
}
if (is_pgp)
print_utf8(fp, s, strlen(s));
else
parse_and_print_user_id(fp, s);
putc('\n', fp);
}
if (key->subkeys && (key->subkeys->timestamp > 0))
{
tt = key->subkeys->timestamp;
mutt_date_localtime_format(shortbuf, sizeof(shortbuf), nl_langinfo(D_T_FMT), tt);
fprintf(fp, "%*s%s\n", KeyInfoPadding[KIP_VALID_FROM],
_(KeyInfoPrompts[KIP_VALID_FROM]), shortbuf);
}
if (key->subkeys && (key->subkeys->expires > 0))
{
tt = key->subkeys->expires;
mutt_date_localtime_format(shortbuf, sizeof(shortbuf), nl_langinfo(D_T_FMT), tt);
fprintf(fp, "%*s%s\n", KeyInfoPadding[KIP_VALID_TO],
_(KeyInfoPrompts[KIP_VALID_TO]), shortbuf);
}
if (key->subkeys)
s = gpgme_pubkey_algo_name(key->subkeys->pubkey_algo);
else
s = "?";
s2 = is_pgp ? "PGP" : "X.509";
if (key->subkeys)
aval = key->subkeys->length;
fprintf(fp, "%*s", KeyInfoPadding[KIP_KEY_TYPE], _(KeyInfoPrompts[KIP_KEY_TYPE]));
/* L10N: This is printed after "Key Type: " and looks like this: PGP, 2048 bit RSA */
fprintf(fp, ngettext("%s, %lu bit %s\n", "%s, %lu bit %s\n", aval), s2, aval, s);
fprintf(fp, "%*s", KeyInfoPadding[KIP_KEY_USAGE], _(KeyInfoPrompts[KIP_KEY_USAGE]));
delim = "";
if (key_check_cap(key, KEY_CAP_CAN_ENCRYPT))
{
/* L10N: value in Key Usage: field */
fprintf(fp, "%s%s", delim, _("encryption"));
delim = _(", ");
}
if (key_check_cap(key, KEY_CAP_CAN_SIGN))
{
/* L10N: value in Key Usage: field */
fprintf(fp, "%s%s", delim, _("signing"));
delim = _(", ");
}
if (key_check_cap(key, KEY_CAP_CAN_CERTIFY))
{
/* L10N: value in Key Usage: field */
fprintf(fp, "%s%s", delim, _("certification"));
}
putc('\n', fp);
if (key->subkeys)
{
s = key->subkeys->fpr;
fprintf(fp, "%*s", KeyInfoPadding[KIP_FINGERPRINT], _(KeyInfoPrompts[KIP_FINGERPRINT]));
if (is_pgp && (strlen(s) == 40))
{
for (int i = 0; (s[0] != '\0') && (s[1] != '\0') && (s[2] != '\0') &&
(s[3] != '\0') && (s[4] != '\0');
s += 4, i++)
{
putc(*s, fp);
putc(s[1], fp);
putc(s[2], fp);
putc(s[3], fp);
putc(' ', fp);
if (i == 4)
putc(' ', fp);
}
}
else
{
for (int i = 0; (s[0] != '\0') && (s[1] != '\0') && (s[2] != '\0'); s += 2, i++)
{
putc(*s, fp);
putc(s[1], fp);
putc(is_pgp ? ' ' : ':', fp);
if (is_pgp && (i == 7))
putc(' ', fp);
}
}
fprintf(fp, "%s\n", s);
}
if (key->issuer_serial)
{
s = key->issuer_serial;
fprintf(fp, "%*s0x%s\n", KeyInfoPadding[KIP_SERIAL_NO],
_(KeyInfoPrompts[KIP_SERIAL_NO]), s);
}
if (key->issuer_name)
{
s = key->issuer_name;
fprintf(fp, "%*s", KeyInfoPadding[KIP_ISSUED_BY], _(KeyInfoPrompts[KIP_ISSUED_BY]));
parse_and_print_user_id(fp, s);
putc('\n', fp);
}
/* For PGP we list all subkeys. */
if (is_pgp)
{
gpgme_subkey_t subkey = NULL;
for (idx = 1, subkey = key->subkeys; subkey; idx++, subkey = subkey->next)
{
s = subkey->keyid;
putc('\n', fp);
if (strlen(s) == 16)
s += 8; /* display only the short keyID */
fprintf(fp, "%*s0x%s", KeyInfoPadding[KIP_SUBKEY], _(KeyInfoPrompts[KIP_SUBKEY]), s);
if (subkey->revoked)
{
putc(' ', fp);
/* L10N: describes a subkey */
fputs(_("[Revoked]"), fp);
}
if (subkey->invalid)
{
putc(' ', fp);
/* L10N: describes a subkey */
fputs(_("[Invalid]"), fp);
}
if (subkey->expired)
{
putc(' ', fp);
/* L10N: describes a subkey */
fputs(_("[Expired]"), fp);
}
if (subkey->disabled)
{
putc(' ', fp);
/* L10N: describes a subkey */
fputs(_("[Disabled]"), fp);
}
putc('\n', fp);
if (subkey->timestamp > 0)
{
tt = subkey->timestamp;
mutt_date_localtime_format(shortbuf, sizeof(shortbuf), nl_langinfo(D_T_FMT), tt);
fprintf(fp, "%*s%s\n", KeyInfoPadding[KIP_VALID_FROM],
_(KeyInfoPrompts[KIP_VALID_FROM]), shortbuf);
}
if (subkey->expires > 0)
{
tt = subkey->expires;
mutt_date_localtime_format(shortbuf, sizeof(shortbuf), nl_langinfo(D_T_FMT), tt);
fprintf(fp, "%*s%s\n", KeyInfoPadding[KIP_VALID_TO],
_(KeyInfoPrompts[KIP_VALID_TO]), shortbuf);
}
s = gpgme_pubkey_algo_name(subkey->pubkey_algo);
aval = subkey->length;
fprintf(fp, "%*s", KeyInfoPadding[KIP_KEY_TYPE], _(KeyInfoPrompts[KIP_KEY_TYPE]));
/* L10N: This is printed after "Key Type: " and looks like this: PGP, 2048 bit RSA */
fprintf(fp, ngettext("%s, %lu bit %s\n", "%s, %lu bit %s\n", aval), "PGP", aval, s);
fprintf(fp, "%*s", KeyInfoPadding[KIP_KEY_USAGE], _(KeyInfoPrompts[KIP_KEY_USAGE]));
delim = "";
if (subkey->can_encrypt)
{
fprintf(fp, "%s%s", delim, _("encryption"));
delim = _(", ");
}
if (subkey->can_sign)
{
fprintf(fp, "%s%s", delim, _("signing"));
delim = _(", ");
}
if (subkey->can_certify)
{
fprintf(fp, "%s%s", delim, _("certification"));
}
putc('\n', fp);
}
}
}
/**
* verify_key - Show detailed information about the selected key
* @param key Key to show
*/
static void verify_key(struct CryptKeyInfo *key)
{
const char *s = NULL;
gpgme_ctx_t listctx = NULL;
gpgme_error_t err = GPG_ERR_NO_ERROR;
gpgme_key_t k = NULL;
int maxdepth = 100;
struct Buffer *tempfile = buf_pool_get();
buf_mktemp(tempfile);
FILE *fp = mutt_file_fopen(buf_string(tempfile), "w");
if (!fp)
{
mutt_perror(_("Can't create temporary file"));
goto cleanup;
}
mutt_message(_("Collecting data..."));
print_key_info(key->kobj, fp);
listctx = create_gpgme_context(key->flags & KEYFLAG_ISX509);
k = key->kobj;
gpgme_key_ref(k);
while ((s = k->chain_id) && k->subkeys && !mutt_str_equal(s, k->subkeys->fpr))
{
putc('\n', fp);
err = gpgme_op_keylist_start(listctx, s, 0);
gpgme_key_unref(k);
k = NULL;
if (err == GPG_ERR_NO_ERROR)
err = gpgme_op_keylist_next(listctx, &k);
if (err != GPG_ERR_NO_ERROR)
{
fprintf(fp, _("Error finding issuer key: %s\n"), gpgme_strerror(err));
goto leave;
}
gpgme_op_keylist_end(listctx);
print_key_info(k, fp);
if (!--maxdepth)
{
putc('\n', fp);
fputs(_("Error: certification chain too long - stopping here\n"), fp);
break;
}
}
leave:
gpgme_key_unref(k);
gpgme_release(listctx);
mutt_file_fclose(&fp);
mutt_clear_error();
char title[1024] = { 0 };
snprintf(title, sizeof(title), _("Key ID: 0x%s"), crypt_keyid(key));
struct PagerData pdata = { 0 };
struct PagerView pview = { &pdata };
pdata.fname = buf_string(tempfile);
pview.banner = title;
pview.flags = MUTT_PAGER_NO_FLAGS;
pview.mode = PAGER_MODE_OTHER;
mutt_do_pager(&pview, NULL);
cleanup:
buf_pool_release(&tempfile);
}
/**
* crypt_key_is_valid - Is the key valid
* @param k Key to test
* @retval true Key is valid
*/
static bool crypt_key_is_valid(struct CryptKeyInfo *k)
{
if (k->flags & KEYFLAG_CANTUSE)
return false;
return true;
}
/**
* crypt_keys_are_valid - Are all these keys valid?
* @param keys Set of keys to test
* @retval true All keys are valid
*/
bool crypt_keys_are_valid(struct CryptKeyInfo *keys)
{
for (struct CryptKeyInfo *k = keys; k != NULL; k = k->next)
{
if (!crypt_key_is_valid(k))
return false;
}
return true;
}
// -----------------------------------------------------------------------------
/**
* op_exit - Exit this menu - Implements ::gpgme_function_t - @ingroup gpgme_function_api
*/
static int op_exit(struct GpgmeData *gd, int op)
{
gd->done = true;
return FR_SUCCESS;
}
/**
* op_generic_select_entry - Select the current entry - Implements ::gpgme_function_t - @ingroup gpgme_function_api
*/
static int op_generic_select_entry(struct GpgmeData *gd, int op)
{
const int index = menu_get_index(gd->menu);
struct CryptKeyInfo **pkey = ARRAY_GET(gd->key_table, index);
if (!pkey)
return FR_ERROR;
struct CryptKeyInfo *cur_key = *pkey;
/* FIXME make error reporting more verbose - this should be
* easy because GPGME provides more information */
if (OptPgpCheckTrust)
{
if (!crypt_key_is_valid(cur_key))
{
mutt_error(_("This key can't be used: expired/disabled/revoked"));
return FR_ERROR;
}
}
if (OptPgpCheckTrust && (!crypt_id_is_valid(cur_key) || !crypt_id_is_strong(cur_key)))
{
const char *warn_s = NULL;
char buf2[1024] = { 0 };
if (cur_key->flags & KEYFLAG_CANTUSE)
{
warn_s = _("ID is expired/disabled/revoked. Do you really want to use the key?");
}
else
{
warn_s = "??";
switch (cur_key->validity)
{
case GPGME_VALIDITY_NEVER:
warn_s = _("ID is not valid. Do you really want to use the key?");
break;
case GPGME_VALIDITY_MARGINAL:
warn_s = _("ID is only marginally valid. Do you really want to use the key?");
break;
case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE:
break;
case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED:
warn_s = _("ID has undefined validity. Do you really want to use the key?");
break;
}
}
snprintf(buf2, sizeof(buf2), "%s", warn_s);
if (query_yesorno(buf2, MUTT_NO) != MUTT_YES)
{
mutt_clear_error();
return FR_NO_ACTION;
}
}
gd->key = crypt_copy_key(cur_key);
gd->done = true;
return FR_SUCCESS;
}
/**
* op_verify_key - Verify a PGP public key - Implements ::gpgme_function_t - @ingroup gpgme_function_api
*/
static int op_verify_key(struct GpgmeData *gd, int op)
{
const int index = menu_get_index(gd->menu);
struct CryptKeyInfo **pkey = ARRAY_GET(gd->key_table, index);
if (!pkey)
return FR_ERROR;
verify_key(*pkey);
menu_queue_redraw(gd->menu, MENU_REDRAW_FULL);
return FR_SUCCESS;
}
/**
* op_view_id - View the key's user id - Implements ::gpgme_function_t - @ingroup gpgme_function_api
*/
static int op_view_id(struct GpgmeData *gd, int op)
{
const int index = menu_get_index(gd->menu);
struct CryptKeyInfo **pkey = ARRAY_GET(gd->key_table, index);
if (!pkey)
return FR_ERROR;
mutt_message("%s", (*pkey)->uid);
return FR_SUCCESS;
}
// -----------------------------------------------------------------------------
/**
* GpgmeFunctions - All the NeoMutt functions that the Gpgme supports
*/
static const struct GpgmeFunction GpgmeFunctions[] = {
// clang-format off
{ OP_EXIT, op_exit },
{ OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
{ OP_VERIFY_KEY, op_verify_key },
{ OP_VIEW_ID, op_view_id },
{ 0, NULL },
// clang-format on
};
/**
* gpgme_function_dispatcher - Perform a Gpgme function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
*/
int gpgme_function_dispatcher(struct MuttWindow *win, int op)
{
// The Dispatcher may be called on any Window in the Dialog
struct MuttWindow *dlg = dialog_find(win);
if (!dlg || !dlg->wdata)
return FR_ERROR;
struct Menu *menu = dlg->wdata;
struct GpgmeData *gd = menu->mdata;
int rc = FR_UNKNOWN;
for (size_t i = 0; GpgmeFunctions[i].op != OP_NULL; i++)
{
const struct GpgmeFunction *fn = &GpgmeFunctions[i];
if (fn->op == op)
{
rc = fn->function(gd, op);
break;
}
}
if (rc == FR_UNKNOWN) // Not our function
return rc;
const char *result = dispatcher_get_retval_name(rc);
mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
return rc;
}
|