NeoMutt  2024-11-14-34-g5aaf0d
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 *b, char *keylist)
 Encrypt the email body to all recipients - Implements CryptModuleSpecs::smime_build_smime_entity() -.
 
struct Bodysmime_class_build_smime_entity (struct Body *b, char *certlist)
 Encrypt the email body to all recipients - Implements CryptModuleSpecs::smime_build_smime_entity() -.
 

Detailed Description

Encrypt the email body to all recipients.

Parameters
bBody 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 b,
char *  keylist 
)

Encrypt the email body to all recipients - Implements CryptModuleSpecs::smime_build_smime_entity() -.

Definition at line 1089 of file crypt_gpgme.c.

1090{
1091 /* OpenSSL converts line endings to crlf when encrypting. Some clients
1092 * depend on this for signed+encrypted messages: they do not convert line
1093 * endings between decrypting and checking the signature. */
1094 gpgme_data_t plaintext = body_to_data_object(b, true);
1095 if (!plaintext)
1096 return NULL;
1097
1098 char *outfile = encrypt_gpgme_object(plaintext, keylist, true, false, NULL);
1099 gpgme_data_release(plaintext);
1100 if (!outfile)
1101 return NULL;
1102
1103 struct Body *b_enc = mutt_body_new();
1104 b_enc->type = TYPE_APPLICATION;
1105 b_enc->subtype = mutt_str_dup("pkcs7-mime");
1106 mutt_param_set(&b_enc->parameter, "name", "smime.p7m");
1107 mutt_param_set(&b_enc->parameter, "smime-type", "enveloped-data");
1108 b_enc->encoding = ENC_BASE64; /* The output of OpenSSL SHOULD be binary */
1109 b_enc->use_disp = true;
1110 b_enc->disposition = DISP_ATTACH;
1111 b_enc->d_filename = mutt_str_dup("smime.p7m");
1112 b_enc->filename = outfile;
1113 b_enc->unlink = true; /* delete after sending the message */
1114 b_enc->parts = 0;
1115 b_enc->next = 0;
1116
1117 return b_enc;
1118}
static gpgme_data_t body_to_data_object(struct Body *b, bool convert)
Create GPGME object from the mail body.
Definition: crypt_gpgme.c:419
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:781
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:44
@ 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:253
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:111
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:73
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:68
struct ParameterList parameter
Parameters of the content-type.
Definition: body.h:63
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:72
char * subtype
content-type subtype
Definition: body.h:61
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:59
+ Here is the call graph for this function:

◆ smime_class_build_smime_entity()

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

Encrypt the email body to all recipients - Implements CryptModuleSpecs::smime_build_smime_entity() -.

Definition at line 1334 of file smime.c.

1335{
1336 char buf[1024] = { 0 };
1337 char certfile[PATH_MAX] = { 0 };
1338 char *cert_end = NULL;
1339 FILE *fp_smime_in = NULL, *fp_smime_err = NULL, *fp_out = NULL, *fp_tmp = NULL;
1340 struct Body *b_enc = NULL;
1341 bool err = false;
1342 int empty, off;
1343 pid_t pid;
1344
1345 struct Buffer *tempfile = buf_pool_get();
1346 struct Buffer *smime_infile = buf_pool_get();
1347
1348 buf_mktemp(tempfile);
1349 fp_out = mutt_file_fopen(buf_string(tempfile), "w+");
1350 if (!fp_out)
1351 {
1352 mutt_perror("%s", buf_string(tempfile));
1353 goto cleanup;
1354 }
1355
1356 fp_smime_err = mutt_file_mkstemp();
1357 if (!fp_smime_err)
1358 {
1359 mutt_perror(_("Can't create temporary file"));
1360 goto cleanup;
1361 }
1362
1363 buf_mktemp(smime_infile);
1364 fp_tmp = mutt_file_fopen(buf_string(smime_infile), "w+");
1365 if (!fp_tmp)
1366 {
1367 mutt_perror("%s", buf_string(smime_infile));
1368 goto cleanup;
1369 }
1370
1371 *certfile = '\0';
1372 for (char *cert_start = certlist; cert_start; cert_start = cert_end)
1373 {
1374 cert_end = strchr(cert_start, ' ');
1375 if (cert_end)
1376 *cert_end = '\0';
1377 if (*cert_start)
1378 {
1379 off = mutt_str_len(certfile);
1380 const char *const c_smime_certificates = cs_subset_path(NeoMutt->sub, "smime_certificates");
1381 snprintf(certfile + off, sizeof(certfile) - off, "%s%s/%s",
1382 (off != 0) ? " " : "", NONULL(c_smime_certificates), cert_start);
1383 }
1384 if (cert_end)
1385 *cert_end++ = ' ';
1386 }
1387
1388 /* write a MIME entity */
1389 mutt_write_mime_header(b, fp_tmp, NeoMutt->sub);
1390 fputc('\n', fp_tmp);
1391 mutt_write_mime_body(b, fp_tmp, NeoMutt->sub);
1392 mutt_file_fclose(&fp_tmp);
1393
1394 pid = smime_invoke_encrypt(&fp_smime_in, NULL, NULL, -1, fileno(fp_out),
1395 fileno(fp_smime_err), buf_string(smime_infile), certfile);
1396 if (pid == -1)
1397 {
1398 mutt_file_unlink(buf_string(smime_infile));
1399 goto cleanup;
1400 }
1401
1402 mutt_file_fclose(&fp_smime_in);
1403
1404 filter_wait(pid);
1405 mutt_file_unlink(buf_string(smime_infile));
1406
1407 fflush(fp_out);
1408 rewind(fp_out);
1409 empty = (fgetc(fp_out) == EOF);
1410 mutt_file_fclose(&fp_out);
1411
1412 fflush(fp_smime_err);
1413 rewind(fp_smime_err);
1414 while (fgets(buf, sizeof(buf) - 1, fp_smime_err))
1415 {
1416 err = true;
1417 fputs(buf, stdout);
1418 }
1419 mutt_file_fclose(&fp_smime_err);
1420
1421 /* pause if there is any error output from SMIME */
1422 if (err)
1424
1425 if (empty)
1426 {
1427 /* fatal error while trying to encrypt message */
1428 if (!err)
1429 mutt_any_key_to_continue(_("No output from OpenSSL..."));
1430 mutt_file_unlink(buf_string(tempfile));
1431 goto cleanup;
1432 }
1433
1434 b_enc = mutt_body_new();
1435 b_enc->type = TYPE_APPLICATION;
1436 b_enc->subtype = mutt_str_dup("pkcs7-mime");
1437 mutt_param_set(&b_enc->parameter, "name", "smime.p7m");
1438 mutt_param_set(&b_enc->parameter, "smime-type", "enveloped-data");
1439 b_enc->encoding = ENC_BASE64; /* The output of OpenSSL SHOULD be binary */
1440 b_enc->use_disp = true;
1441 b_enc->disposition = DISP_ATTACH;
1442 b_enc->d_filename = mutt_str_dup("smime.p7m");
1443 b_enc->filename = buf_strdup(tempfile);
1444 b_enc->unlink = true; /* delete after sending the message */
1445 b_enc->parts = NULL;
1446 b_enc->next = NULL;
1447
1448cleanup:
1449 if (fp_out)
1450 {
1451 mutt_file_fclose(&fp_out);
1452 mutt_file_unlink(buf_string(tempfile));
1453 }
1454 mutt_file_fclose(&fp_smime_err);
1455 if (fp_tmp)
1456 {
1457 mutt_file_fclose(&fp_tmp);
1458 mutt_file_unlink(buf_string(smime_infile));
1459 }
1460 buf_pool_release(&tempfile);
1461 buf_pool_release(&smime_infile);
1462
1463 return b_enc;
1464}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:168
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:173
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:221
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define mutt_perror(...)
Definition: logging2.h:93
int mutt_write_mime_header(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition: header.c:756
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
#define _(a)
Definition: message.h:28
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
#define PATH_MAX
Definition: mutt.h:42
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 *b, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition: body.c:300
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:1292
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
#define buf_mktemp(buf)
Definition: tmp.h:33
#define mutt_file_mkstemp()
Definition: tmp.h:36
+ Here is the call graph for this function: