NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
find_keys()

Find the keyids of the recipients of a message. More...

+ Collaboration diagram for find_keys():

Functions

char * pgp_gpgme_find_keys (const struct AddressList *addrlist, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 
char * smime_gpgme_find_keys (const struct AddressList *addrlist, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 
char * pgp_class_find_keys (const struct AddressList *addrlist, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 
char * smime_class_find_keys (const struct AddressList *al, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 

Detailed Description

Find the keyids of the recipients of a message.

Parameters
addrlistAddress List
oppenc_modeIf true, use opportunistic encryption
Return values
ptrSpace-separated string of keys
NULLAt least one of the keys can't be found

If oppenc_mode is true, only keys that can be determined without prompting will be used.

Function Documentation

◆ pgp_gpgme_find_keys()

char* pgp_gpgme_find_keys ( const struct AddressList *  addrlist,
bool  oppenc_mode 
)

Implements CryptModuleSpecs::find_keys() -.

Definition at line 3941 of file crypt_gpgme.c.

3942 {
3943  return find_keys(addrlist, APPLICATION_PGP, oppenc_mode);
3944 }
static char * find_keys(const struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
Find keys of the recipients of the message.
Definition: crypt_gpgme.c:3818
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
+ Here is the call graph for this function:

◆ smime_gpgme_find_keys()

char* smime_gpgme_find_keys ( const struct AddressList *  addrlist,
bool  oppenc_mode 
)

Implements CryptModuleSpecs::find_keys() -.

Definition at line 3949 of file crypt_gpgme.c.

3950 {
3951  return find_keys(addrlist, APPLICATION_SMIME, oppenc_mode);
3952 }
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:91
+ Here is the call graph for this function:

◆ pgp_class_find_keys()

char* pgp_class_find_keys ( const struct AddressList *  addrlist,
bool  oppenc_mode 
)

Implements CryptModuleSpecs::find_keys() -.

Definition at line 1446 of file pgp.c.

1447 {
1448  struct ListHead crypt_hook_list = STAILQ_HEAD_INITIALIZER(crypt_hook_list);
1449  struct ListNode *crypt_hook = NULL;
1450  const char *keyid = NULL;
1451  char *keylist = NULL;
1452  size_t keylist_size = 0;
1453  size_t keylist_used = 0;
1454  struct Address *p = NULL;
1455  struct PgpKeyInfo *k_info = NULL;
1456  const char *fqdn = mutt_fqdn(true, NeoMutt->sub);
1457  char buf[1024];
1458  bool key_selected;
1459  struct AddressList hookal = TAILQ_HEAD_INITIALIZER(hookal);
1460 
1461  struct Address *a = NULL;
1462  TAILQ_FOREACH(a, addrlist, entries)
1463  {
1464  key_selected = false;
1465  mutt_crypt_hook(&crypt_hook_list, a);
1466  crypt_hook = STAILQ_FIRST(&crypt_hook_list);
1467  do
1468  {
1469  p = a;
1470  k_info = NULL;
1471 
1472  if (crypt_hook)
1473  {
1474  keyid = crypt_hook->data;
1475  enum QuadOption ans = MUTT_YES;
1476  const bool c_crypt_confirm_hook = cs_subset_bool(NeoMutt->sub, "crypt_confirm_hook");
1477  if (!oppenc_mode && c_crypt_confirm_hook)
1478  {
1479  snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyid, p->mailbox);
1480  ans = mutt_yesorno(buf, MUTT_YES);
1481  }
1482  if (ans == MUTT_YES)
1483  {
1484  if (crypt_is_numerical_keyid(keyid))
1485  {
1486  if (mutt_strn_equal(keyid, "0x", 2))
1487  keyid += 2;
1488  goto bypass_selection; /* you don't see this. */
1489  }
1490 
1491  /* check for e-mail address */
1492  mutt_addrlist_clear(&hookal);
1493  if (strchr(keyid, '@') && (mutt_addrlist_parse(&hookal, keyid) != 0))
1494  {
1495  mutt_addrlist_qualify(&hookal, fqdn);
1496  p = TAILQ_FIRST(&hookal);
1497  }
1498  else if (!oppenc_mode)
1499  {
1501  }
1502  }
1503  else if (ans == MUTT_NO)
1504  {
1505  if (key_selected || STAILQ_NEXT(crypt_hook, entries))
1506  {
1507  crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1508  continue;
1509  }
1510  }
1511  else if (ans == MUTT_ABORT)
1512  {
1513  FREE(&keylist);
1514  mutt_addrlist_clear(&hookal);
1515  mutt_list_free(&crypt_hook_list);
1516  return NULL;
1517  }
1518  }
1519 
1520  if (!k_info)
1521  {
1523  k_info = pgp_getkeybyaddr(p, KEYFLAG_CANENCRYPT, PGP_PUBRING, oppenc_mode);
1524  }
1525 
1526  if (!k_info && !oppenc_mode)
1527  {
1528  snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), p->mailbox);
1530  }
1531 
1532  if (!k_info)
1533  {
1534  FREE(&keylist);
1535  mutt_addrlist_clear(&hookal);
1536  mutt_list_free(&crypt_hook_list);
1537  return NULL;
1538  }
1539 
1540  keyid = pgp_fpr_or_lkeyid(k_info);
1541 
1542  bypass_selection:
1543  keylist_size += mutt_str_len(keyid) + 4;
1544  mutt_mem_realloc(&keylist, keylist_size);
1545  sprintf(keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", keyid);
1546  keylist_used = mutt_str_len(keylist);
1547 
1548  key_selected = true;
1549 
1550  pgp_key_free(&k_info);
1551  mutt_addrlist_clear(&hookal);
1552 
1553  if (crypt_hook)
1554  crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1555 
1556  } while (crypt_hook);
1557 
1558  mutt_list_free(&crypt_hook_list);
1559  }
1560  return keylist;
1561 }
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition: address.c:650
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1470
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:458
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
bool crypt_is_numerical_keyid(const char *s)
Is this a numerical keyid.
Definition: crypt.c:1352
void pgp_class_invoke_getkeys(struct Address *addr)
Implements CryptModuleSpecs::pgp_invoke_getkeys() -.
Definition: pgpinvoke.c:435
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition: hook.c:828
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:43
#define _(a)
Definition: message.h:28
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:473
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:544
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:234
struct PgpKeyInfo * pgp_getkeybystr(const char *cp, KeyFlags abilities, enum PgpRing keyring)
Find a PGP key by string.
Definition: pgpkey.c:491
struct PgpKeyInfo * pgp_getkeybyaddr(struct Address *a, KeyFlags abilities, enum PgpRing keyring, bool oppenc_mode)
Find a PGP key by address.
Definition: pgpkey.c:355
struct PgpKeyInfo * pgp_ask_for_key(char *tag, char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition: pgpkey.c:180
@ PGP_PUBRING
Public keys.
Definition: pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:199
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition: quad.h:37
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:194
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:698
An email address.
Definition: address.h:36
char * mailbox
Mailbox and host address.
Definition: address.h:38
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Information about a PGP key.
Definition: pgplib.h:47
+ Here is the call graph for this function:

◆ smime_class_find_keys()

char* smime_class_find_keys ( const struct AddressList *  al,
bool  oppenc_mode 
)

Implements CryptModuleSpecs::find_keys() -.

Definition at line 833 of file smime.c.

834 {
835  struct SmimeKey *key = NULL;
836  char *keyid = NULL, *keylist = NULL;
837  size_t keylist_size = 0;
838  size_t keylist_used = 0;
839 
840  struct Address *a = NULL;
841  TAILQ_FOREACH(a, al, entries)
842  {
843  key = smime_get_key_by_addr(a->mailbox, KEYFLAG_CANENCRYPT, true, oppenc_mode);
844  if (!key && !oppenc_mode)
845  {
846  char buf[1024];
847  snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), a->mailbox);
848  key = smime_ask_for_key(buf, KEYFLAG_CANENCRYPT, true);
849  }
850  if (!key)
851  {
852  if (!oppenc_mode)
853  mutt_message(_("No (valid) certificate found for %s"), a->mailbox);
854  FREE(&keylist);
855  return NULL;
856  }
857 
858  keyid = key->hash;
859  keylist_size += mutt_str_len(keyid) + 2;
860  mutt_mem_realloc(&keylist, keylist_size);
861  sprintf(keylist + keylist_used, "%s%s", keylist_used ? " " : "", keyid);
862  keylist_used = mutt_str_len(keylist);
863 
864  smime_key_free(&key);
865  }
866  return keylist;
867 }
#define mutt_message(...)
Definition: logging.h:86
static struct SmimeKey * smime_get_key_by_addr(char *mailbox, KeyFlags abilities, bool only_public_key, bool oppenc_mode)
Find an SIME key by address.
Definition: smime.c:593
static struct SmimeKey * smime_ask_for_key(char *prompt, KeyFlags abilities, bool only_public_key)
Ask the user to select a key.
Definition: smime.c:720
static void smime_key_free(struct SmimeKey **keylist)
Free a list of SMIME keys.
Definition: smime.c:109
An SIME key.
Definition: smime.h:44
char * hash
Definition: smime.h:46
+ Here is the call graph for this function: