NeoMutt  2025-12-11-1040-g5187ba
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_encrypt_message()

PGP encrypt an email. More...

Collaboration diagram for pgp_encrypt_message():

Functions

struct Bodypgp_gpgme_encrypt_message (struct Body *b, char *keylist, bool sign, const struct AddressList *from)
 PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.
struct Bodypgp_class_encrypt_message (struct Body *b, char *keylist, bool sign, const struct AddressList *from)
 PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.

Detailed Description

PGP encrypt an email.

Parameters
bBody of email to encrypt
keylistList of keys, or fingerprints (space separated)
signIf true, sign the message too
fromFrom line, to choose the key to sign
Return values
ptrEncrypted Body
NULLError

Encrypt the mail body to all the given keys.

Function Documentation

◆ pgp_gpgme_encrypt_message()

struct Body * pgp_gpgme_encrypt_message ( struct Body * b,
char * keylist,
bool sign,
const struct AddressList * from )

PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.

Definition at line 1054 of file crypt_gpgme.c.

1056{
1057 if (sign)
1059 gpgme_data_t plaintext = body_to_data_object(b, false);
1060 if (!plaintext)
1061 return NULL;
1062
1063 char *outfile = encrypt_gpgme_object(plaintext, keylist, false, sign, from);
1064 gpgme_data_release(plaintext);
1065 if (!outfile)
1066 return NULL;
1067
1068 struct Body *b_enc = mutt_body_new();
1069 b_enc->type = TYPE_MULTIPART;
1070 b_enc->subtype = mutt_str_dup("encrypted");
1071 b_enc->encoding = ENC_7BIT;
1072 b_enc->use_disp = false;
1073 b_enc->disposition = DISP_INLINE;
1074
1076 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-encrypted");
1077
1078 b_enc->parts = mutt_body_new();
1079 b_enc->parts->type = TYPE_APPLICATION;
1080 b_enc->parts->subtype = mutt_str_dup("pgp-encrypted");
1081 b_enc->parts->encoding = ENC_7BIT;
1082
1083 b_enc->parts->next = mutt_body_new();
1084 b_enc->parts->next->type = TYPE_APPLICATION;
1085 b_enc->parts->next->subtype = mutt_str_dup("octet-stream");
1086 b_enc->parts->next->encoding = ENC_7BIT;
1087 b_enc->parts->next->filename = outfile;
1088 b_enc->parts->next->use_disp = true;
1089 b_enc->parts->next->disposition = DISP_ATTACH;
1090 b_enc->parts->next->unlink = true; /* delete after sending the message */
1091 b_enc->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime
1092 can save */
1093
1094 return b_enc;
1095}
void crypt_convert_to_7bit(struct Body *b)
Convert an email to 7bit encoding.
Definition crypt.c:815
static gpgme_data_t body_to_data_object(struct Body *b, bool convert)
Create GPGME object from the mail body.
static char * encrypt_gpgme_object(gpgme_data_t plaintext, char *keylist, bool use_smime, bool combined_signed, const struct AddressList *from)
Encrypt the GPGPME data object.
struct Body * mutt_body_new(void)
Create a new Body.
Definition body.c:44
@ ENC_7BIT
7-bit text
Definition mime.h:49
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition mime.h:37
@ TYPE_APPLICATION
Type: 'application/*'.
Definition mime.h:33
@ DISP_ATTACH
Content is attached.
Definition mime.h:63
@ DISP_INLINE
Content is inline.
Definition mime.h:62
void mutt_generate_boundary(struct ParameterList *pl)
Create a unique boundary id for a MIME part.
Definition multipart.c:93
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition parameter.c:111
The body of an email.
Definition body.h:36
char * d_filename
filename to be used for the content-disposition header If NULL, filename is used instead.
Definition body.h:56
struct Body * parts
parts of a multipart or message/rfc822
Definition body.h:73
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition body.h:68
struct ParameterList parameter
Parameters of the content-type.
Definition body.h:63
bool use_disp
Content-Disposition uses filename= ?
Definition body.h:47
unsigned int disposition
content-disposition, ContentDisposition
Definition body.h:42
struct Body * next
next attachment in the list
Definition body.h:72
char * subtype
content-type subtype
Definition body.h:61
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition body.h:41
unsigned int type
content-type primary type, ContentType
Definition body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition body.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pgp_class_encrypt_message()

struct Body * pgp_class_encrypt_message ( struct Body * b,
char * keylist,
bool sign,
const struct AddressList * from )

PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.

Warning
"b" is no longer freed in this routine, you need to free it later. This is necessary for $fcc_attach.

Definition at line 1616 of file pgp.c.

1618{
1620 char buf[1024] = { 0 };
1621 FILE *fp_pgp_in = NULL;
1622 FILE *fp_tmp = NULL;
1623 struct Body *b_enc = NULL;
1624 bool err = false;
1625 bool empty = false;
1626 pid_t pid;
1627 struct Buffer *tempfile = buf_pool_get();
1628 struct Buffer *pgpinfile = buf_pool_get();
1629
1630 buf_mktemp(tempfile);
1631 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1632 if (!fp_out)
1633 {
1634 mutt_perror("%s", buf_string(tempfile));
1635 goto cleanup;
1636 }
1637
1638 FILE *fp_pgp_err = mutt_file_mkstemp();
1639 if (!fp_pgp_err)
1640 {
1641 mutt_perror(_("Can't create temporary file"));
1642 unlink(buf_string(tempfile));
1643 mutt_file_fclose(&fp_out);
1644 goto cleanup;
1645 }
1646
1647 buf_mktemp(pgpinfile);
1648 fp_tmp = mutt_file_fopen(buf_string(pgpinfile), "w");
1649 if (!fp_tmp)
1650 {
1651 mutt_perror("%s", buf_string(pgpinfile));
1652 unlink(buf_string(tempfile));
1653 mutt_file_fclose(&fp_out);
1654 mutt_file_fclose(&fp_pgp_err);
1655 goto cleanup;
1656 }
1657
1658 if (sign)
1660
1661 mutt_write_mime_header(b, fp_tmp, NeoMutt->sub);
1662 fputc('\n', fp_tmp);
1663 mutt_write_mime_body(b, fp_tmp, NeoMutt->sub);
1664 mutt_file_fclose(&fp_tmp);
1665
1666 pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out),
1667 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, sign);
1668 if (pid == -1)
1669 {
1670 mutt_file_fclose(&fp_out);
1671 mutt_file_fclose(&fp_pgp_err);
1672 unlink(buf_string(pgpinfile));
1673 goto cleanup;
1674 }
1675
1676 if (sign)
1677 {
1678 if (!pgp_use_gpg_agent())
1679 fputs(mod_data->pgp_pass, fp_pgp_in);
1680 fputc('\n', fp_pgp_in);
1681 }
1682 mutt_file_fclose(&fp_pgp_in);
1683
1684 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1685 if (filter_wait(pid) && c_pgp_check_exit)
1686 empty = true;
1687
1688 unlink(buf_string(pgpinfile));
1689
1690 fflush(fp_out);
1691 fseek(fp_out, 0, SEEK_SET);
1692 clearerr(fp_out);
1693 if (!empty)
1694 empty = (fgetc(fp_out) == EOF);
1695 mutt_file_fclose(&fp_out);
1696
1697 fflush(fp_pgp_err);
1698 fseek(fp_pgp_err, 0, SEEK_SET);
1699 clearerr(fp_pgp_err);
1700 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1701 {
1702 err = true;
1703 fputs(buf, stdout);
1704 }
1705 mutt_file_fclose(&fp_pgp_err);
1706
1707 /* pause if there is any error output from PGP */
1708 if (err)
1710
1711 if (empty)
1712 {
1713 /* fatal error while trying to encrypt message */
1714 if (sign)
1715 pgp_class_void_passphrase(); /* just in case */
1716 unlink(buf_string(tempfile));
1717 goto cleanup;
1718 }
1719
1720 b_enc = mutt_body_new();
1721 b_enc->type = TYPE_MULTIPART;
1722 b_enc->subtype = mutt_str_dup("encrypted");
1723 b_enc->encoding = ENC_7BIT;
1724 b_enc->use_disp = false;
1725 b_enc->disposition = DISP_INLINE;
1726
1728 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-encrypted");
1729
1730 b_enc->parts = mutt_body_new();
1731 b_enc->parts->type = TYPE_APPLICATION;
1732 b_enc->parts->subtype = mutt_str_dup("pgp-encrypted");
1733 b_enc->parts->encoding = ENC_7BIT;
1734
1735 b_enc->parts->next = mutt_body_new();
1736 b_enc->parts->next->type = TYPE_APPLICATION;
1737 b_enc->parts->next->subtype = mutt_str_dup("octet-stream");
1738 b_enc->parts->next->encoding = ENC_7BIT;
1739 b_enc->parts->next->filename = buf_strdup(tempfile);
1740 b_enc->parts->next->use_disp = true;
1741 b_enc->parts->next->disposition = DISP_ATTACH;
1742 b_enc->parts->next->unlink = true; /* delete after sending the message */
1743 b_enc->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1744
1745cleanup:
1746 buf_pool_release(&tempfile);
1747 buf_pool_release(&pgpinfile);
1748 return b_enc;
1749}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:577
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
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition curs_lib.c:174
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
void pgp_class_void_passphrase(void)
Forget the cached passphrase - Implements CryptModuleSpecs::void_passphrase() -.
Definition pgp.c:71
#define mutt_perror(...)
Definition logging2.h:95
@ MODULE_ID_NCRYPT
ModuleNcrypt, Ncrypt
Definition module_api.h:82
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:231
#define _(a)
Definition message.h:28
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition pgp.c:124
pid_t pgp_invoke_encrypt(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, const char *uids, bool sign)
Use PGP to encrypt a file.
Definition pgpinvoke.c:229
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
int mutt_write_mime_body(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition body.c:304
int mutt_write_mime_header(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition header.c:763
String manipulation buffer.
Definition buffer.h:36
Ncrypt private Module data.
Definition module_data.h:39
char pgp_pass[1024]
Cached PGP Passphrase.
Definition module_data.h:51
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
#define buf_mktemp(buf)
Definition tmp.h:33
#define mutt_file_mkstemp()
Definition tmp.h:36
Here is the call graph for this function: