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

Encrypt the email body to all recipients. More...

+ Collaboration diagram for smime_build_smime_entity():

Functions

struct Bodysmime_gpgme_build_smime_entity (struct Body *a, char *keylist)
 Implements CryptModuleSpecs::smime_build_smime_entity() -.
 
struct Bodysmime_class_build_smime_entity (struct Body *a, char *certlist)
 Implements CryptModuleSpecs::smime_build_smime_entity() -.
 

Detailed Description

Encrypt the email body to all recipients.

Parameters
aBody of email
certlistList of key fingerprints (space separated)
Return values
ptrNew S/MIME encrypted Body
NULLError

Function Documentation

◆ smime_gpgme_build_smime_entity()

struct Body * smime_gpgme_build_smime_entity ( struct Body a,
char *  keylist 
)

Implements CryptModuleSpecs::smime_build_smime_entity() -.

Definition at line 1083 of file crypt_gpgme.c.

1084{
1085 /* OpenSSL converts line endings to crlf when encrypting. Some clients
1086 * depend on this for signed+encrypted messages: they do not convert line
1087 * endings between decrypting and checking the signature. */
1088 gpgme_data_t plaintext = body_to_data_object(a, true);
1089 if (!plaintext)
1090 return NULL;
1091
1092 char *outfile = encrypt_gpgme_object(plaintext, keylist, true, false, NULL);
1093 gpgme_data_release(plaintext);
1094 if (!outfile)
1095 return NULL;
1096
1097 struct Body *t = mutt_body_new();
1099 t->subtype = mutt_str_dup("pkcs7-mime");
1100 mutt_param_set(&t->parameter, "name", "smime.p7m");
1101 mutt_param_set(&t->parameter, "smime-type", "enveloped-data");
1102 t->encoding = ENC_BASE64; /* The output of OpenSSL SHOULD be binary */
1103 t->use_disp = true;
1105 t->d_filename = mutt_str_dup("smime.p7m");
1106 t->filename = outfile;
1107 t->unlink = true; /* delete after sending the message */
1108 t->parts = 0;
1109 t->next = 0;
1110
1111 return t;
1112}
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_BASE64
Base-64 encoded text.
Definition: mime.h:52
@ TYPE_APPLICATION
Type: 'application/*'.
Definition: mime.h:33
@ DISP_ATTACH
Content is attached.
Definition: mime.h:63
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:

◆ smime_class_build_smime_entity()

struct Body * smime_class_build_smime_entity ( struct Body a,
char *  certlist 
)

Implements CryptModuleSpecs::smime_build_smime_entity() -.

Definition at line 1384 of file smime.c.

1385{
1386 char buf[1024], certfile[PATH_MAX];
1387 char *cert_end = NULL;
1388 FILE *fp_smime_in = NULL, *fp_smime_err = NULL, *fp_out = NULL, *fp_tmp = NULL;
1389 struct Body *t = NULL;
1390 int err = 0, empty, off;
1391 pid_t pid;
1392
1393 struct Buffer *tempfile = buf_pool_get();
1394 struct Buffer *smime_infile = buf_pool_get();
1395
1396 buf_mktemp(tempfile);
1397 fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1398 if (!fp_out)
1399 {
1400 mutt_perror("%s", buf_string(tempfile));
1401 goto cleanup;
1402 }
1403
1404 fp_smime_err = mutt_file_mkstemp();
1405 if (!fp_smime_err)
1406 {
1407 mutt_perror(_("Can't create temporary file"));
1408 goto cleanup;
1409 }
1410
1411 buf_mktemp(smime_infile);
1412 fp_tmp = mutt_file_fopen(buf_string(smime_infile), "w+");
1413 if (!fp_tmp)
1414 {
1415 mutt_perror("%s", buf_string(smime_infile));
1416 goto cleanup;
1417 }
1418
1419 *certfile = '\0';
1420 for (char *cert_start = certlist; cert_start; cert_start = cert_end)
1421 {
1422 cert_end = strchr(cert_start, ' ');
1423 if (cert_end)
1424 *cert_end = '\0';
1425 if (*cert_start)
1426 {
1427 off = mutt_str_len(certfile);
1428 const char *const c_smime_certificates = cs_subset_path(NeoMutt->sub, "smime_certificates");
1429 snprintf(certfile + off, sizeof(certfile) - off, "%s%s/%s",
1430 (off != 0) ? " " : "", NONULL(c_smime_certificates), cert_start);
1431 }
1432 if (cert_end)
1433 *cert_end++ = ' ';
1434 }
1435
1436 /* write a MIME entity */
1437 mutt_write_mime_header(a, fp_tmp, NeoMutt->sub);
1438 fputc('\n', fp_tmp);
1439 mutt_write_mime_body(a, fp_tmp, NeoMutt->sub);
1440 mutt_file_fclose(&fp_tmp);
1441
1442 pid = smime_invoke_encrypt(&fp_smime_in, NULL, NULL, -1, fileno(fp_out),
1443 fileno(fp_smime_err), buf_string(smime_infile), certfile);
1444 if (pid == -1)
1445 {
1446 mutt_file_unlink(buf_string(smime_infile));
1447 goto cleanup;
1448 }
1449
1450 mutt_file_fclose(&fp_smime_in);
1451
1452 filter_wait(pid);
1453 mutt_file_unlink(buf_string(smime_infile));
1454
1455 fflush(fp_out);
1456 rewind(fp_out);
1457 empty = (fgetc(fp_out) == EOF);
1458 mutt_file_fclose(&fp_out);
1459
1460 fflush(fp_smime_err);
1461 rewind(fp_smime_err);
1462 while (fgets(buf, sizeof(buf) - 1, fp_smime_err))
1463 {
1464 err = 1;
1465 fputs(buf, stdout);
1466 }
1467 mutt_file_fclose(&fp_smime_err);
1468
1469 /* pause if there is any error output from SMIME */
1470 if (err)
1472
1473 if (empty)
1474 {
1475 /* fatal error while trying to encrypt message */
1476 if (err == 0)
1477 mutt_any_key_to_continue(_("No output from OpenSSL..."));
1478 mutt_file_unlink(buf_string(tempfile));
1479 goto cleanup;
1480 }
1481
1482 t = mutt_body_new();
1484 t->subtype = mutt_str_dup("pkcs7-mime");
1485 mutt_param_set(&t->parameter, "name", "smime.p7m");
1486 mutt_param_set(&t->parameter, "smime-type", "enveloped-data");
1487 t->encoding = ENC_BASE64; /* The output of OpenSSL SHOULD be binary */
1488 t->use_disp = true;
1490 t->d_filename = mutt_str_dup("smime.p7m");
1491 t->filename = buf_strdup(tempfile);
1492 t->unlink = true; /* delete after sending the message */
1493 t->parts = NULL;
1494 t->next = NULL;
1495
1496cleanup:
1497 if (fp_out)
1498 {
1499 mutt_file_fclose(&fp_out);
1500 mutt_file_unlink(buf_string(tempfile));
1501 }
1502 mutt_file_fclose(&fp_smime_err);
1503 if (fp_tmp)
1504 {
1505 mutt_file_fclose(&fp_tmp);
1506 mutt_file_unlink(buf_string(smime_infile));
1507 }
1508 buf_pool_release(&tempfile);
1509 buf_pool_release(&smime_infile);
1510
1511 return t;
1512}
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
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
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
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:196
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:218
#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
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
#define PATH_MAX
Definition: mutt.h:41
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
static pid_t smime_invoke_encrypt(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, const char *uids)
Use SMIME to encrypt a file.
Definition: smime.c:1342
#define NONULL(x)
Definition: string2.h:37
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: