NeoMutt  2022-04-29-81-g9c5a59
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 1399 of file crypt_gpgme.c.

1401 {
1402  if (sign)
1404  gpgme_data_t plaintext = body_to_data_object(a, false);
1405  if (!plaintext)
1406  return NULL;
1407 
1408  char *outfile = encrypt_gpgme_object(plaintext, keylist, false, sign, from);
1409  gpgme_data_release(plaintext);
1410  if (!outfile)
1411  return NULL;
1412 
1413  struct Body *t = mutt_body_new();
1414  t->type = TYPE_MULTIPART;
1415  t->subtype = mutt_str_dup("encrypted");
1416  t->encoding = ENC_7BIT;
1417  t->use_disp = false;
1418  t->disposition = DISP_INLINE;
1419 
1421  mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1422 
1423  t->parts = mutt_body_new();
1424  t->parts->type = TYPE_APPLICATION;
1425  t->parts->subtype = mutt_str_dup("pgp-encrypted");
1426  t->parts->encoding = ENC_7BIT;
1427 
1428  t->parts->next = mutt_body_new();
1430  t->parts->next->subtype = mutt_str_dup("octet-stream");
1431  t->parts->next->encoding = ENC_7BIT;
1432  t->parts->next->filename = outfile;
1433  t->parts->next->use_disp = true;
1435  t->parts->next->unlink = true; /* delete after sending the message */
1436  t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime
1437  can save */
1438 
1439  return t;
1440 }
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:676
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:1119
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 1566 of file pgp.c.

1568 {
1569  char buf[1024];
1570  FILE *fp_pgp_in = NULL, *fp_tmp = NULL;
1571  struct Body *t = NULL;
1572  int err = 0;
1573  bool empty = false;
1574  pid_t pid;
1575  struct Buffer *tempfile = mutt_buffer_pool_get();
1576  struct Buffer *pgpinfile = mutt_buffer_pool_get();
1577 
1578  mutt_buffer_mktemp(tempfile);
1579  FILE *fp_out = mutt_file_fopen(mutt_buffer_string(tempfile), "w+");
1580  if (!fp_out)
1581  {
1582  mutt_perror(mutt_buffer_string(tempfile));
1583  goto cleanup;
1584  }
1585 
1586  FILE *fp_pgp_err = mutt_file_mkstemp();
1587  if (!fp_pgp_err)
1588  {
1589  mutt_perror(_("Can't create temporary file"));
1590  unlink(mutt_buffer_string(tempfile));
1591  mutt_file_fclose(&fp_out);
1592  goto cleanup;
1593  }
1594 
1595  mutt_buffer_mktemp(pgpinfile);
1596  fp_tmp = mutt_file_fopen(mutt_buffer_string(pgpinfile), "w");
1597  if (!fp_tmp)
1598  {
1599  mutt_perror(mutt_buffer_string(pgpinfile));
1600  unlink(mutt_buffer_string(tempfile));
1601  mutt_file_fclose(&fp_out);
1602  mutt_file_fclose(&fp_pgp_err);
1603  goto cleanup;
1604  }
1605 
1606  if (sign)
1608 
1609  mutt_write_mime_header(a, fp_tmp, NeoMutt->sub);
1610  fputc('\n', fp_tmp);
1611  mutt_write_mime_body(a, fp_tmp, NeoMutt->sub);
1612  mutt_file_fclose(&fp_tmp);
1613 
1614  pid = pgp_invoke_encrypt(&fp_pgp_in, NULL, NULL, -1, fileno(fp_out), fileno(fp_pgp_err),
1615  mutt_buffer_string(pgpinfile), keylist, sign);
1616  if (pid == -1)
1617  {
1618  mutt_file_fclose(&fp_out);
1619  mutt_file_fclose(&fp_pgp_err);
1620  unlink(mutt_buffer_string(pgpinfile));
1621  goto cleanup;
1622  }
1623 
1624  if (sign)
1625  {
1626  if (!pgp_use_gpg_agent())
1627  fputs(PgpPass, fp_pgp_in);
1628  fputc('\n', fp_pgp_in);
1629  }
1630  mutt_file_fclose(&fp_pgp_in);
1631 
1632  const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1633  if (filter_wait(pid) && c_pgp_check_exit)
1634  empty = true;
1635 
1636  unlink(mutt_buffer_string(pgpinfile));
1637 
1638  fflush(fp_out);
1639  rewind(fp_out);
1640  if (!empty)
1641  empty = (fgetc(fp_out) == EOF);
1642  mutt_file_fclose(&fp_out);
1643 
1644  fflush(fp_pgp_err);
1645  rewind(fp_pgp_err);
1646  while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1647  {
1648  err = 1;
1649  fputs(buf, stdout);
1650  }
1651  mutt_file_fclose(&fp_pgp_err);
1652 
1653  /* pause if there is any error output from PGP */
1654  if (err)
1656 
1657  if (empty)
1658  {
1659  /* fatal error while trying to encrypt message */
1660  if (sign)
1661  pgp_class_void_passphrase(); /* just in case */
1662  unlink(mutt_buffer_string(tempfile));
1663  goto cleanup;
1664  }
1665 
1666  t = mutt_body_new();
1667  t->type = TYPE_MULTIPART;
1668  t->subtype = mutt_str_dup("encrypted");
1669  t->encoding = ENC_7BIT;
1670  t->use_disp = false;
1671  t->disposition = DISP_INLINE;
1672 
1674  mutt_param_set(&t->parameter, "protocol", "application/pgp-encrypted");
1675 
1676  t->parts = mutt_body_new();
1677  t->parts->type = TYPE_APPLICATION;
1678  t->parts->subtype = mutt_str_dup("pgp-encrypted");
1679  t->parts->encoding = ENC_7BIT;
1680 
1681  t->parts->next = mutt_body_new();
1683  t->parts->next->subtype = mutt_str_dup("octet-stream");
1684  t->parts->next->encoding = ENC_7BIT;
1685  t->parts->next->filename = mutt_buffer_strdup(tempfile);
1686  t->parts->next->use_disp = true;
1688  t->parts->next->unlink = true; /* delete after sending the message */
1689  t->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime can save */
1690 
1691 cleanup:
1692  mutt_buffer_pool_release(&tempfile);
1693  mutt_buffer_pool_release(&pgpinfile);
1694  return t;
1695 }
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:432
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:498
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:618
#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:74
#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
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
#define _(a)
Definition: message.h:28
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
char PgpPass[1024]
Definition: pgp.c:68
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: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: