NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
pgp_encrypt_message()

PGP encrypt an email. More...

+ Collaboration diagram for pgp_encrypt_message():

Functions

struct Bodypgp_gpgme_encrypt_message (struct Body *a, char *keylist, bool sign, const struct AddressList *from)
 Implements CryptModuleSpecs::pgp_encrypt_message() -. More...
 
struct Bodypgp_class_encrypt_message (struct Body *a, char *keylist, bool sign, const struct AddressList *from)
 Implements CryptModuleSpecs::pgp_encrypt_message() -. More...
 

Detailed Description

PGP encrypt an email.

Parameters
aBody 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 a,
char *  keylist,
bool  sign,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::pgp_encrypt_message() -.

Definition at line 1035 of file crypt_gpgme.c.

1037{
1038 if (sign)
1040 gpgme_data_t plaintext = body_to_data_object(a, false);
1041 if (!plaintext)
1042 return NULL;
1043
1044 char *outfile = encrypt_gpgme_object(plaintext, keylist, false, sign, from);
1045 gpgme_data_release(plaintext);
1046 if (!outfile)
1047 return NULL;
1048
1049 struct Body *t = mutt_body_new();
1050 t->type = TYPE_MULTIPART;
1051 t->subtype = mutt_str_dup("encrypted");
1052 t->encoding = ENC_7BIT;
1053 t->use_disp = false;
1055
1057 mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1058
1059 t->parts = mutt_body_new();
1061 t->parts->subtype = mutt_str_dup("pgp-encrypted");
1062 t->parts->encoding = ENC_7BIT;
1063
1064 t->parts->next = mutt_body_new();
1066 t->parts->next->subtype = mutt_str_dup("octet-stream");
1067 t->parts->next->encoding = ENC_7BIT;
1068 t->parts->next->filename = outfile;
1069 t->parts->next->use_disp = true;
1071 t->parts->next->unlink = true; /* delete after sending the message */
1072 t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime
1073 can save */
1074
1075 return t;
1076}
void crypt_convert_to_7bit(struct Body *a)
Convert an email to 7bit encoding.
Definition: crypt.c:795
static gpgme_data_t body_to_data_object(struct Body *a, bool convert)
Create GPGME object from the mail body.
Definition: crypt_gpgme.c:420
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.
Definition: crypt_gpgme.c:773
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:43
@ 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:86
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:110
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:72
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:67
struct ParameterList parameter
Parameters of the content-type.
Definition: body.h:62
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:71
char * subtype
content-type subtype
Definition: body.h:60
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:58
+ 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 a,
char *  keylist,
bool  sign,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::pgp_encrypt_message() -.

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

Definition at line 1569 of file pgp.c.

1571{
1572 char buf[1024] = { 0 };
1573 FILE *fp_pgp_in = NULL, *fp_tmp = NULL;
1574 struct Body *t = NULL;
1575 int err = 0;
1576 bool empty = false;
1577 pid_t pid;
1578 struct Buffer *tempfile = mutt_buffer_pool_get();
1579 struct Buffer *pgpinfile = mutt_buffer_pool_get();
1580
1581 mutt_buffer_mktemp(tempfile);
1582 FILE *fp_out = mutt_file_fopen(mutt_buffer_string(tempfile), "w+");
1583 if (!fp_out)
1584 {
1586 goto cleanup;
1587 }
1588
1589 FILE *fp_pgp_err = mutt_file_mkstemp();
1590 if (!fp_pgp_err)
1591 {
1592 mutt_perror(_("Can't create temporary file"));
1593 unlink(mutt_buffer_string(tempfile));
1594 mutt_file_fclose(&fp_out);
1595 goto cleanup;
1596 }
1597
1598 mutt_buffer_mktemp(pgpinfile);
1599 fp_tmp = mutt_file_fopen(mutt_buffer_string(pgpinfile), "w");
1600 if (!fp_tmp)
1601 {
1602 mutt_perror(mutt_buffer_string(pgpinfile));
1603 unlink(mutt_buffer_string(tempfile));
1604 mutt_file_fclose(&fp_out);
1605 mutt_file_fclose(&fp_pgp_err);
1606 goto cleanup;
1607 }
1608
1609 if (sign)
1611
1612 mutt_write_mime_header(a, fp_tmp, NeoMutt->sub);
1613 fputc('\n', fp_tmp);
1614 mutt_write_mime_body(a, fp_tmp, NeoMutt->sub);
1615 mutt_file_fclose(&fp_tmp);
1616
1617 pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out), fileno(fp_pgp_err),
1618 mutt_buffer_string(pgpinfile), keylist, sign);
1619 if (pid == -1)
1620 {
1621 mutt_file_fclose(&fp_out);
1622 mutt_file_fclose(&fp_pgp_err);
1623 unlink(mutt_buffer_string(pgpinfile));
1624 goto cleanup;
1625 }
1626
1627 if (sign)
1628 {
1629 if (!pgp_use_gpg_agent())
1630 fputs(PgpPass, fp_pgp_in);
1631 fputc('\n', fp_pgp_in);
1632 }
1633 mutt_file_fclose(&fp_pgp_in);
1634
1635 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1636 if (filter_wait(pid) && c_pgp_check_exit)
1637 empty = true;
1638
1639 unlink(mutt_buffer_string(pgpinfile));
1640
1641 fflush(fp_out);
1642 rewind(fp_out);
1643 if (!empty)
1644 empty = (fgetc(fp_out) == EOF);
1645 mutt_file_fclose(&fp_out);
1646
1647 fflush(fp_pgp_err);
1648 rewind(fp_pgp_err);
1649 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1650 {
1651 err = 1;
1652 fputs(buf, stdout);
1653 }
1654 mutt_file_fclose(&fp_pgp_err);
1655
1656 /* pause if there is any error output from PGP */
1657 if (err)
1659
1660 if (empty)
1661 {
1662 /* fatal error while trying to encrypt message */
1663 if (sign)
1664 pgp_class_void_passphrase(); /* just in case */
1665 unlink(mutt_buffer_string(tempfile));
1666 goto cleanup;
1667 }
1668
1669 t = mutt_body_new();
1670 t->type = TYPE_MULTIPART;
1671 t->subtype = mutt_str_dup("encrypted");
1672 t->encoding = ENC_7BIT;
1673 t->use_disp = false;
1675
1677 mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1678
1679 t->parts = mutt_body_new();
1681 t->parts->subtype = mutt_str_dup("pgp-encrypted");
1682 t->parts->encoding = ENC_7BIT;
1683
1684 t->parts->next = mutt_body_new();
1686 t->parts->next->subtype = mutt_str_dup("octet-stream");
1687 t->parts->next->encoding = ENC_7BIT;
1688 t->parts->next->filename = mutt_buffer_strdup(tempfile);
1689 t->parts->next->use_disp = true;
1691 t->parts->next->unlink = true; /* delete after sending the message */
1692 t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1693
1694cleanup:
1695 mutt_buffer_pool_release(&tempfile);
1696 mutt_buffer_pool_release(&pgpinfile);
1697 return t;
1698}
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:447
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:387
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:618
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
#define mutt_file_mkstemp()
Definition: file.h:112
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
void pgp_class_void_passphrase(void)
Implements CryptModuleSpecs::void_passphrase() -.
Definition: pgp.c:75
#define mutt_perror(...)
Definition: logging.h:88
int mutt_write_mime_header(struct Body *a, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition: header.c:760
#define _(a)
Definition: message.h:28
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
char PgpPass[1024]
Definition: pgp.c:69
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition: pgp.c:127
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:350
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
int mutt_write_mime_body(struct Body *a, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition: body.c:314
String manipulation buffer.
Definition: buffer.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function: