NeoMutt
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 *a, char *keylist, bool sign, const struct AddressList *from)
 Implements CryptModuleSpecs::pgp_encrypt_message() -.
 
struct Bodypgp_class_encrypt_message (struct Body *a, char *keylist, bool sign, const struct AddressList *from)
 Implements CryptModuleSpecs::pgp_encrypt_message() -.
 

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 1037 of file crypt_gpgme.c.

1039{
1040 if (sign)
1042 gpgme_data_t plaintext = body_to_data_object(a, false);
1043 if (!plaintext)
1044 return NULL;
1045
1046 char *outfile = encrypt_gpgme_object(plaintext, keylist, false, sign, from);
1047 gpgme_data_release(plaintext);
1048 if (!outfile)
1049 return NULL;
1050
1051 struct Body *t = mutt_body_new();
1052 t->type = TYPE_MULTIPART;
1053 t->subtype = mutt_str_dup("encrypted");
1054 t->encoding = ENC_7BIT;
1055 t->use_disp = false;
1057
1059 mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1060
1061 t->parts = mutt_body_new();
1063 t->parts->subtype = mutt_str_dup("pgp-encrypted");
1064 t->parts->encoding = ENC_7BIT;
1065
1066 t->parts->next = mutt_body_new();
1068 t->parts->next->subtype = mutt_str_dup("octet-stream");
1069 t->parts->next->encoding = ENC_7BIT;
1070 t->parts->next->filename = outfile;
1071 t->parts->next->use_disp = true;
1073 t->parts->next->unlink = true; /* delete after sending the message */
1074 t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime
1075 can save */
1076
1077 return t;
1078}
void crypt_convert_to_7bit(struct Body *a)
Convert an email to 7bit encoding.
Definition: crypt.c:798
static gpgme_data_t body_to_data_object(struct Body *a, bool convert)
Create GPGME object from the mail body.
Definition: crypt_gpgme.c:418
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:775
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:251
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 1592 of file pgp.c.

1594{
1595 char buf[1024] = { 0 };
1596 FILE *fp_pgp_in = NULL, *fp_tmp = NULL;
1597 struct Body *t = NULL;
1598 int err = 0;
1599 bool empty = false;
1600 pid_t pid;
1601 struct Buffer *tempfile = buf_pool_get();
1602 struct Buffer *pgpinfile = buf_pool_get();
1603
1604 buf_mktemp(tempfile);
1605 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1606 if (!fp_out)
1607 {
1608 mutt_perror("%s", buf_string(tempfile));
1609 goto cleanup;
1610 }
1611
1612 FILE *fp_pgp_err = mutt_file_mkstemp();
1613 if (!fp_pgp_err)
1614 {
1615 mutt_perror(_("Can't create temporary file"));
1616 unlink(buf_string(tempfile));
1617 mutt_file_fclose(&fp_out);
1618 goto cleanup;
1619 }
1620
1621 buf_mktemp(pgpinfile);
1622 fp_tmp = mutt_file_fopen(buf_string(pgpinfile), "w");
1623 if (!fp_tmp)
1624 {
1625 mutt_perror("%s", buf_string(pgpinfile));
1626 unlink(buf_string(tempfile));
1627 mutt_file_fclose(&fp_out);
1628 mutt_file_fclose(&fp_pgp_err);
1629 goto cleanup;
1630 }
1631
1632 if (sign)
1634
1635 mutt_write_mime_header(a, fp_tmp, NeoMutt->sub);
1636 fputc('\n', fp_tmp);
1637 mutt_write_mime_body(a, fp_tmp, NeoMutt->sub);
1638 mutt_file_fclose(&fp_tmp);
1639
1640 pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out),
1641 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, sign);
1642 if (pid == -1)
1643 {
1644 mutt_file_fclose(&fp_out);
1645 mutt_file_fclose(&fp_pgp_err);
1646 unlink(buf_string(pgpinfile));
1647 goto cleanup;
1648 }
1649
1650 if (sign)
1651 {
1652 if (!pgp_use_gpg_agent())
1653 fputs(PgpPass, fp_pgp_in);
1654 fputc('\n', fp_pgp_in);
1655 }
1656 mutt_file_fclose(&fp_pgp_in);
1657
1658 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1659 if (filter_wait(pid) && c_pgp_check_exit)
1660 empty = true;
1661
1662 unlink(buf_string(pgpinfile));
1663
1664 fflush(fp_out);
1665 rewind(fp_out);
1666 if (!empty)
1667 empty = (fgetc(fp_out) == EOF);
1668 mutt_file_fclose(&fp_out);
1669
1670 fflush(fp_pgp_err);
1671 rewind(fp_pgp_err);
1672 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1673 {
1674 err = 1;
1675 fputs(buf, stdout);
1676 }
1677 mutt_file_fclose(&fp_pgp_err);
1678
1679 /* pause if there is any error output from PGP */
1680 if (err)
1682
1683 if (empty)
1684 {
1685 /* fatal error while trying to encrypt message */
1686 if (sign)
1687 pgp_class_void_passphrase(); /* just in case */
1688 unlink(buf_string(tempfile));
1689 goto cleanup;
1690 }
1691
1692 t = mutt_body_new();
1693 t->type = TYPE_MULTIPART;
1694 t->subtype = mutt_str_dup("encrypted");
1695 t->encoding = ENC_7BIT;
1696 t->use_disp = false;
1698
1700 mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1701
1702 t->parts = mutt_body_new();
1704 t->parts->subtype = mutt_str_dup("pgp-encrypted");
1705 t->parts->encoding = ENC_7BIT;
1706
1707 t->parts->next = mutt_body_new();
1709 t->parts->next->subtype = mutt_str_dup("octet-stream");
1710 t->parts->next->encoding = ENC_7BIT;
1711 t->parts->next->filename = buf_strdup(tempfile);
1712 t->parts->next->use_disp = true;
1714 t->parts->next->unlink = true; /* delete after sending the message */
1715 t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1716
1717cleanup:
1718 buf_pool_release(&tempfile);
1719 buf_pool_release(&pgpinfile);
1720 return t;
1721}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:188
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:218
void pgp_class_void_passphrase(void)
Implements CryptModuleSpecs::void_passphrase() -.
Definition: pgp.c:77
#define mutt_perror(...)
Definition: logging2.h:93
int mutt_write_mime_header(struct Body *a, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition: header.c:758
#define _(a)
Definition: message.h:28
static char PgpPass[1024]
Cached PGP Passphrase.
Definition: pgp.c:70
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition: pgp.c:128
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:361
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
int mutt_write_mime_body(struct Body *a, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition: body.c:301
String manipulation buffer.
Definition: buffer.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
#define buf_mktemp(buf)
Definition: tmp.h:33
#define mutt_file_mkstemp()
Definition: tmp.h:36
+ Here is the call graph for this function: