NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sign_message()

Cryptographically sign the Body of a message. More...

+ Collaboration diagram for sign_message():

Functions

struct Bodypgp_gpgme_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -.
 
struct Bodysmime_gpgme_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -.
 
struct Bodypgp_class_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -.
 
struct Bodysmime_class_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -.
 

Detailed Description

Cryptographically sign the Body of a message.

Parameters
aBody of the message
fromFrom line
Return values
ptrNew encrypted Body
NULLError

Function Documentation

◆ pgp_gpgme_sign_message()

struct Body * pgp_gpgme_sign_message ( struct Body a,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::sign_message() -.

Definition at line 1021 of file crypt_gpgme.c.

1022{
1023 return sign_message(a, from, false);
1024}
static struct Body * sign_message(struct Body *a, const struct AddressList *from, bool use_smime)
Sign a message.
Definition: crypt_gpgme.c:900
+ Here is the call graph for this function:

◆ smime_gpgme_sign_message()

struct Body * smime_gpgme_sign_message ( struct Body a,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::sign_message() -.

Definition at line 1029 of file crypt_gpgme.c.

1030{
1031 return sign_message(a, from, true);
1032}
+ Here is the call graph for this function:

◆ pgp_class_sign_message()

struct Body * pgp_class_sign_message ( struct Body a,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::sign_message() -.

Definition at line 1337 of file pgp.c.

1338{
1339 struct Body *t = NULL, *rv = NULL;
1340 char buf[1024] = { 0 };
1341 FILE *fp_pgp_in = NULL, *fp_pgp_out = NULL, *fp_pgp_err = NULL, *fp_signed = NULL;
1342 bool err = false;
1343 bool empty = true;
1344 pid_t pid;
1345 struct Buffer *sigfile = buf_pool_get();
1346 struct Buffer *signedfile = buf_pool_get();
1347
1348 crypt_convert_to_7bit(a); /* Signed data _must_ be in 7-bit format. */
1349
1350 buf_mktemp(sigfile);
1351 FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
1352 if (!fp_sig)
1353 {
1354 goto cleanup;
1355 }
1356
1357 buf_mktemp(signedfile);
1358 fp_signed = mutt_file_fopen(buf_string(signedfile), "w");
1359 if (!fp_signed)
1360 {
1361 mutt_perror("%s", buf_string(signedfile));
1362 mutt_file_fclose(&fp_sig);
1363 unlink(buf_string(sigfile));
1364 goto cleanup;
1365 }
1366
1367 mutt_write_mime_header(a, fp_signed, NeoMutt->sub);
1368 fputc('\n', fp_signed);
1369 mutt_write_mime_body(a, fp_signed, NeoMutt->sub);
1370 mutt_file_fclose(&fp_signed);
1371
1372 pid = pgp_invoke_sign(&fp_pgp_in, &fp_pgp_out, &fp_pgp_err, -1, -1, -1,
1373 buf_string(signedfile));
1374 if (pid == -1)
1375 {
1376 mutt_perror(_("Can't open PGP subprocess"));
1377 mutt_file_fclose(&fp_sig);
1378 unlink(buf_string(sigfile));
1379 unlink(buf_string(signedfile));
1380 goto cleanup;
1381 }
1382
1383 if (!pgp_use_gpg_agent())
1384 fputs(PgpPass, fp_pgp_in);
1385 fputc('\n', fp_pgp_in);
1386 mutt_file_fclose(&fp_pgp_in);
1387
1388 /* Read back the PGP signature. Also, change MESSAGE=>SIGNATURE as
1389 * recommended for future releases of PGP. */
1390 while (fgets(buf, sizeof(buf) - 1, fp_pgp_out))
1391 {
1392 if (mutt_str_equal("-----BEGIN PGP MESSAGE-----\n", buf))
1393 fputs("-----BEGIN PGP SIGNATURE-----\n", fp_sig);
1394 else if (mutt_str_equal("-----END PGP MESSAGE-----\n", buf))
1395 fputs("-----END PGP SIGNATURE-----\n", fp_sig);
1396 else
1397 fputs(buf, fp_sig);
1398 empty = false; /* got some output, so we're ok */
1399 }
1400
1401 /* check for errors from PGP */
1402 err = false;
1403 while (fgets(buf, sizeof(buf) - 1, fp_pgp_err))
1404 {
1405 err = true;
1406 fputs(buf, stdout);
1407 }
1408
1409 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1410 if (filter_wait(pid) && c_pgp_check_exit)
1411 empty = true;
1412
1413 mutt_file_fclose(&fp_pgp_err);
1414 mutt_file_fclose(&fp_pgp_out);
1415 unlink(buf_string(signedfile));
1416
1417 if (mutt_file_fclose(&fp_sig) != 0)
1418 {
1419 mutt_perror("fclose");
1420 unlink(buf_string(sigfile));
1421 goto cleanup;
1422 }
1423
1424 if (err)
1426 if (empty)
1427 {
1428 unlink(buf_string(sigfile));
1429 /* most likely error is a bad passphrase, so automatically forget it */
1431 goto cleanup; /* fatal error while signing */
1432 }
1433
1434 t = mutt_body_new();
1435 t->type = TYPE_MULTIPART;
1436 t->subtype = mutt_str_dup("signed");
1437 t->encoding = ENC_7BIT;
1438 t->use_disp = false;
1440 rv = t;
1441
1443 mutt_param_set(&t->parameter, "protocol", "application/pgp-signature");
1444 mutt_param_set(&t->parameter, "micalg", pgp_micalg(buf_string(sigfile)));
1445
1446 t->parts = a;
1447
1448 t->parts->next = mutt_body_new();
1449 t = t->parts->next;
1451 t->subtype = mutt_str_dup("pgp-signature");
1452 t->filename = buf_strdup(sigfile);
1453 t->use_disp = false;
1455 t->encoding = ENC_7BIT;
1456 t->unlink = true; /* ok to remove this file after sending. */
1457 mutt_param_set(&t->parameter, "name", "signature.asc");
1458
1459cleanup:
1460 buf_pool_release(&sigfile);
1461 buf_pool_release(&signedfile);
1462 return rv;
1463}
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
void crypt_convert_to_7bit(struct Body *a)
Convert an email to 7bit encoding.
Definition: crypt.c:798
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:188
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:43
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
@ 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_INLINE
Content is inline.
Definition: mime.h:62
@ DISP_NONE
No preferred disposition.
Definition: mime.h:65
void mutt_generate_boundary(struct ParameterList *pl)
Create a unique boundary id for a MIME part.
Definition: multipart.c:86
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:110
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_sign(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)
Use PGP to sign a file.
Definition: pgpinvoke.c:336
const char * pgp_micalg(const char *fname)
Find the hash algorithm of a file.
Definition: pgpmicalg.c:227
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
The body of an email.
Definition: body.h:36
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
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
+ Here is the call graph for this function:

◆ smime_class_sign_message()

struct Body * smime_class_sign_message ( struct Body a,
const struct AddressList *  from 
)

Implements CryptModuleSpecs::sign_message() -.

Definition at line 1547 of file smime.c.

1548{
1549 struct Body *t = NULL;
1550 struct Body *rc = NULL;
1551 char buf[1024] = { 0 };
1552 struct Buffer *filetosign = NULL, *signedfile = NULL;
1553 FILE *fp_smime_in = NULL, *fp_smime_out = NULL, *fp_smime_err = NULL, *fp_sign = NULL;
1554 int err = 0;
1555 int empty = 0;
1556 pid_t pid;
1557 const char *intermediates = NULL;
1558
1559 const char *const c_smime_sign_as = cs_subset_string(NeoMutt->sub, "smime_sign_as");
1560 const char *const c_smime_default_key = cs_subset_string(NeoMutt->sub, "smime_default_key");
1561 const char *signas = c_smime_sign_as ? c_smime_sign_as : c_smime_default_key;
1562 if (!signas || (*signas == '\0'))
1563 {
1564 mutt_error(_("Can't sign: No key specified. Use Sign As."));
1565 return NULL;
1566 }
1567
1568 crypt_convert_to_7bit(a); /* Signed data _must_ be in 7-bit format. */
1569
1570 filetosign = buf_pool_get();
1571 signedfile = buf_pool_get();
1572
1573 buf_mktemp(filetosign);
1574 fp_sign = mutt_file_fopen(buf_string(filetosign), "w+");
1575 if (!fp_sign)
1576 {
1577 mutt_perror("%s", buf_string(filetosign));
1578 goto cleanup;
1579 }
1580
1581 buf_mktemp(signedfile);
1582 fp_smime_out = mutt_file_fopen(buf_string(signedfile), "w+");
1583 if (!fp_smime_out)
1584 {
1585 mutt_perror("%s", buf_string(signedfile));
1586 goto cleanup;
1587 }
1588
1589 mutt_write_mime_header(a, fp_sign, NeoMutt->sub);
1590 fputc('\n', fp_sign);
1591 mutt_write_mime_body(a, fp_sign, NeoMutt->sub);
1592 mutt_file_fclose(&fp_sign);
1593
1594 const char *const c_smime_keys = cs_subset_path(NeoMutt->sub, "smime_keys");
1595 const char *const c_smime_certificates = cs_subset_path(NeoMutt->sub, "smime_certificates");
1596 buf_printf(&SmimeKeyToUse, "%s/%s", NONULL(c_smime_keys), signas);
1597 buf_printf(&SmimeCertToUse, "%s/%s", NONULL(c_smime_certificates), signas);
1598
1599 struct SmimeKey *signas_key = smime_get_key_by_hash(signas, 1);
1600 if (!signas_key || mutt_str_equal("?", signas_key->issuer))
1601 intermediates = signas; /* so openssl won't complain in any case */
1602 else
1603 intermediates = signas_key->issuer;
1604
1605 buf_printf(&SmimeIntermediateToUse, "%s/%s", NONULL(c_smime_certificates), intermediates);
1606
1607 smime_key_free(&signas_key);
1608
1609 pid = smime_invoke_sign(&fp_smime_in, NULL, &fp_smime_err, -1,
1610 fileno(fp_smime_out), -1, buf_string(filetosign));
1611 if (pid == -1)
1612 {
1613 mutt_perror(_("Can't open OpenSSL subprocess"));
1614 mutt_file_unlink(buf_string(filetosign));
1615 goto cleanup;
1616 }
1617 fputs(SmimePass, fp_smime_in);
1618 fputc('\n', fp_smime_in);
1619 mutt_file_fclose(&fp_smime_in);
1620
1621 filter_wait(pid);
1622
1623 /* check for errors from OpenSSL */
1624 err = 0;
1625 fflush(fp_smime_err);
1626 rewind(fp_smime_err);
1627 while (fgets(buf, sizeof(buf) - 1, fp_smime_err))
1628 {
1629 err = 1;
1630 fputs(buf, stdout);
1631 }
1632 mutt_file_fclose(&fp_smime_err);
1633
1634 fflush(fp_smime_out);
1635 rewind(fp_smime_out);
1636 empty = (fgetc(fp_smime_out) == EOF);
1637 mutt_file_fclose(&fp_smime_out);
1638
1639 mutt_file_unlink(buf_string(filetosign));
1640
1641 if (err)
1643
1644 if (empty)
1645 {
1646 mutt_any_key_to_continue(_("No output from OpenSSL..."));
1647 mutt_file_unlink(buf_string(signedfile));
1648 goto cleanup; /* fatal error while signing */
1649 }
1650
1651 t = mutt_body_new();
1652 t->type = TYPE_MULTIPART;
1653 t->subtype = mutt_str_dup("signed");
1654 t->encoding = ENC_7BIT;
1655 t->use_disp = false;
1657
1659
1660 const char *const c_smime_sign_digest_alg = cs_subset_string(NeoMutt->sub, "smime_sign_digest_alg");
1661 char *micalg = openssl_md_to_smime_micalg(c_smime_sign_digest_alg);
1662 mutt_param_set(&t->parameter, "micalg", micalg);
1663 FREE(&micalg);
1664
1665 mutt_param_set(&t->parameter, "protocol", "application/pkcs7-signature");
1666
1667 t->parts = a;
1668 rc = t;
1669
1670 t->parts->next = mutt_body_new();
1671 t = t->parts->next;
1673 t->subtype = mutt_str_dup("pkcs7-signature");
1674 t->filename = buf_strdup(signedfile);
1675 t->d_filename = mutt_str_dup("smime.p7s");
1676 t->use_disp = true;
1678 t->encoding = ENC_BASE64;
1679 t->unlink = true; /* ok to remove this file after sending. */
1680
1681cleanup:
1682 if (fp_sign)
1683 {
1684 mutt_file_fclose(&fp_sign);
1685 mutt_file_unlink(buf_string(filetosign));
1686 }
1687 if (fp_smime_out)
1688 {
1689 mutt_file_fclose(&fp_smime_out);
1690 mutt_file_unlink(buf_string(signedfile));
1691 }
1692 buf_pool_release(&filetosign);
1693 buf_pool_release(&signedfile);
1694 return rc;
1695}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:196
#define mutt_error(...)
Definition: logging2.h:92
#define FREE(x)
Definition: memory.h:45
@ ENC_BASE64
Base-64 encoded text.
Definition: mime.h:52
@ DISP_ATTACH
Content is attached.
Definition: mime.h:63
static struct Buffer SmimeIntermediateToUse
Smime intermediate certificate to use.
Definition: smime.c:90
static struct SmimeKey * smime_get_key_by_hash(const char *hash, bool only_public_key)
Find a key by its hash.
Definition: smime.c:589
static pid_t smime_invoke_sign(FILE **fp_smime_in, FILE **fp_smime_out, FILE **fp_smime_err, int fp_smime_infd, int fp_smime_outfd, int fp_smime_errfd, const char *fname)
Use SMIME to sign a file.
Definition: smime.c:1369
static char SmimePass[256]
Cached Smime Passphrase.
Definition: smime.c:81
static struct Buffer SmimeKeyToUse
Smime key to use.
Definition: smime.c:86
static struct Buffer SmimeCertToUse
Smime certificate to use.
Definition: smime.c:88
static void smime_key_free(struct SmimeKey **keylist)
Free a list of SMIME keys.
Definition: smime.c:116
static char * openssl_md_to_smime_micalg(const char *md)
Change the algorithm names.
Definition: smime.c:1526
#define NONULL(x)
Definition: string2.h:37
char * d_filename
filename to be used for the content-disposition header If NULL, filename is used instead.
Definition: body.h:56
An SIME key.
Definition: smime.h:44
char * issuer
Definition: smime.h:48
+ Here is the call graph for this function: