NeoMutt  2025-12-11-1009-ga75d9e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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)
 Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
char * smime_gpgme_find_keys (const struct AddressList *addrlist, bool oppenc_mode)
 Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
char * pgp_class_find_keys (const struct AddressList *addrlist, bool oppenc_mode)
 Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
char * smime_class_find_keys (const struct AddressList *al, bool oppenc_mode)
 Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.

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 )

Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.

Definition at line 3689 of file crypt_gpgme.c.

3690{
3691 return find_keys(addrlist, APPLICATION_PGP, oppenc_mode);
3692}
static char * find_keys(const struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
Find keys of the recipients of the message.
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:106
Here is the call graph for this function:

◆ smime_gpgme_find_keys()

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

Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.

Definition at line 3697 of file crypt_gpgme.c.

3698{
3699 return find_keys(addrlist, APPLICATION_SMIME, oppenc_mode);
3700}
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition lib.h:107
Here is the call graph for this function:

◆ pgp_class_find_keys()

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

Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.

Definition at line 1472 of file pgp.c.

1473{
1474 struct ListHead crypt_hook_list = STAILQ_HEAD_INITIALIZER(crypt_hook_list);
1475 struct ListNode *crypt_hook = NULL;
1476 const char *keyid = NULL;
1477 char *keylist = NULL;
1478 size_t keylist_size = 0;
1479 size_t keylist_used = 0;
1480 struct Address *p = NULL;
1481 struct PgpKeyInfo *k_info = NULL;
1482 const char *fqdn = mutt_fqdn(true, NeoMutt->sub);
1483 char buf[1024] = { 0 };
1484 bool key_selected;
1485 struct AddressList hookal = TAILQ_HEAD_INITIALIZER(hookal);
1486
1487 struct Address *a = NULL;
1488 const bool c_crypt_confirm_hook = cs_subset_bool(NeoMutt->sub, "crypt_confirm_hook");
1489 /* Iterate through each recipient address to find an encryption key */
1490 TAILQ_FOREACH(a, addrlist, entries)
1491 {
1492 key_selected = false;
1493 /* Check for crypt-hook overrides for this recipient */
1494 mutt_crypt_hook(&crypt_hook_list, a);
1495 crypt_hook = STAILQ_FIRST(&crypt_hook_list);
1496 do
1497 {
1498 p = a;
1499 k_info = NULL;
1500
1501 /* If a crypt-hook provides a key ID, confirm with the user unless
1502 * in opportunistic encryption mode */
1503 if (crypt_hook)
1504 {
1505 keyid = crypt_hook->data;
1506 enum QuadOption ans = MUTT_YES;
1507 if (!oppenc_mode && c_crypt_confirm_hook && isatty(STDIN_FILENO))
1508 {
1509 snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyid,
1510 buf_string(p->mailbox));
1511 ans = query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "crypt_confirm_hook");
1512 }
1513 if (ans == MUTT_YES)
1514 {
1515 if (crypt_is_numerical_keyid(keyid))
1516 {
1517 if (mutt_strn_equal(keyid, "0x", 2))
1518 keyid += 2;
1519 goto bypass_selection; /* you don't see this. */
1520 }
1521
1522 /* check for e-mail address */
1523 mutt_addrlist_clear(&hookal);
1524 if (strchr(keyid, '@') && (mutt_addrlist_parse(&hookal, keyid) != 0))
1525 {
1526 mutt_addrlist_qualify(&hookal, fqdn);
1527 p = TAILQ_FIRST(&hookal);
1528 }
1529 else if (!oppenc_mode)
1530 {
1532 }
1533 }
1534 else if (ans == MUTT_NO)
1535 {
1536 if (key_selected || STAILQ_NEXT(crypt_hook, entries))
1537 {
1538 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1539 continue;
1540 }
1541 }
1542 else if (ans == MUTT_ABORT)
1543 {
1544 FREE(&keylist);
1545 mutt_addrlist_clear(&hookal);
1546 mutt_list_free(&crypt_hook_list);
1547 return NULL;
1548 }
1549 }
1550
1551 /* If no key found yet, try looking up by address in the keyring */
1552 if (!k_info)
1553 {
1555 k_info = pgp_getkeybyaddr(p, KEYFLAG_CANENCRYPT, PGP_PUBRING, oppenc_mode);
1556 }
1557
1558 /* Last resort: prompt the user to enter a key ID interactively */
1559 if (!k_info && !oppenc_mode && isatty(STDIN_FILENO))
1560 {
1561 snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), buf_string(p->mailbox));
1563 }
1564
1565 if (!k_info)
1566 {
1567 FREE(&keylist);
1568 mutt_addrlist_clear(&hookal);
1569 mutt_list_free(&crypt_hook_list);
1570 return NULL;
1571 }
1572
1573 keyid = pgp_fpr_or_lkeyid(k_info);
1574
1575 bypass_selection:
1576 /* Append the selected key ID to the space-separated keylist string */
1577 keylist_size += mutt_str_len(keyid) + 4;
1578 MUTT_MEM_REALLOC(&keylist, keylist_size, char);
1579 sprintf(keylist + keylist_used, "%s0x%s", keylist_used ? " " : "", keyid);
1580 keylist_used = mutt_str_len(keylist);
1581
1582 key_selected = true;
1583
1584 pgp_key_free(&k_info);
1585 mutt_addrlist_clear(&hookal);
1586
1587 if (crypt_hook)
1588 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
1589
1590 } while (crypt_hook);
1591
1592 mutt_list_free(&crypt_hook_list);
1593 }
1594 return keylist;
1595}
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition address.c:687
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1475
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition address.c:481
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
bool crypt_is_numerical_keyid(const char *s)
Is this a numerical keyid.
Definition crypt.c:1489
void pgp_class_invoke_getkeys(struct Address *addr)
Run a command to download a PGP key - Implements CryptModuleSpecs::pgp_invoke_getkeys() -.
Definition pgpinvoke.c:315
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition exec.c:319
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
#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:429
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
@ KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition lib.h:148
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition pgp.c:231
struct PgpKeyInfo * pgp_ask_for_key(char *tag, const char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition pgpkey.c:202
struct PgpKeyInfo * pgp_getkeybyaddr(struct Address *a, KeyFlags abilities, enum PgpRing keyring, bool oppenc_mode)
Find a PGP key by address.
Definition pgpkey.c:382
struct PgpKeyInfo * pgp_getkeybystr(const char *cp, KeyFlags abilities, enum PgpRing keyring)
Find a PGP key by string.
Definition pgpkey.c:523
@ PGP_PUBRING
Public keys.
Definition pgpkey.h:40
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition pgplib.c:204
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 query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition question.c:357
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define STAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define STAILQ_FIRST(head)
Definition queue.h:388
#define TAILQ_FIRST(head)
Definition queue.h:780
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition sendlib.c:717
An email address.
Definition address.h:35
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Information about a PGP key.
Definition pgplib.h:49
Here is the call graph for this function:

◆ smime_class_find_keys()

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

Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.

Definition at line 656 of file smime.c.

657{
658 struct SmimeKey *key = NULL;
659 char *keyid = NULL;
660 char *keylist = NULL;
661 size_t keylist_size = 0;
662 size_t keylist_used = 0;
663
664 struct Address *a = NULL;
665 TAILQ_FOREACH(a, al, entries)
666 {
667 key = smime_get_key_by_addr(buf_string(a->mailbox), KEYFLAG_CANENCRYPT, true, oppenc_mode);
668 if (!key && !oppenc_mode && isatty(STDIN_FILENO))
669 {
670 struct Buffer *prompt = buf_pool_get();
671 buf_printf(prompt, _("Enter keyID for %s: "), buf_string(a->mailbox));
672 key = smime_ask_for_key(buf_string(prompt), KEYFLAG_CANENCRYPT, true);
673 buf_pool_release(&prompt);
674 }
675 if (!key)
676 {
677 if (!oppenc_mode)
678 mutt_message(_("No (valid) certificate found for %s"), buf_string(a->mailbox));
679 FREE(&keylist);
680 return NULL;
681 }
682
683 keyid = key->hash;
684 keylist_size += mutt_str_len(keyid) + 2;
685 MUTT_MEM_REALLOC(&keylist, keylist_size, char);
686 sprintf(keylist + keylist_used, "%s%s", keylist_used ? " " : "", keyid);
687 keylist_used = mutt_str_len(keylist);
688
689 smime_key_free(&key);
690 }
691 return keylist;
692}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
#define mutt_message(...)
Definition logging2.h:93
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
static struct SmimeKey * smime_get_key_by_addr(const char *mailbox, KeyFlags abilities, bool only_public_key, bool oppenc_mode)
Find an SIME key by address.
Definition smime.c:408
static void smime_key_free(struct SmimeKey **keylist)
Free a list of SMIME keys.
Definition smime.c:91
static struct SmimeKey * smime_ask_for_key(const char *prompt, KeyFlags abilities, bool only_public_key)
Ask the user to select a key.
Definition smime.c:537
String manipulation buffer.
Definition buffer.h:36
An SIME key.
Definition smime.h:43
char * hash
Key hash.
Definition smime.h:45
Here is the call graph for this function: