NeoMutt  2024-10-02-7-gd3e66a
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
crypt.h File Reference

Signing/encryption multiplexor. More...

#include <stdbool.h>
+ Include dependency graph for crypt.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void crypt_convert_to_7bit (struct Body *b)
 Convert an email to 7bit encoding.
 
void crypt_current_time (struct State *state, const char *app_name)
 Print the current time.
 
const char * crypt_get_fingerprint_or_id (const char *p, const char **pphint, const char **ppl, const char **pps)
 Get the fingerprint or long key ID.
 
bool crypt_is_numerical_keyid (const char *s)
 Is this a numerical keyid.
 
int crypt_write_signed (struct Body *b, struct State *state, const char *tempfile)
 Write the message body/part.
 

Detailed Description

Signing/encryption multiplexor.

Authors
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file crypt.h.

Function Documentation

◆ crypt_convert_to_7bit()

void crypt_convert_to_7bit ( struct Body b)

Convert an email to 7bit encoding.

Parameters
bBody of email to convert

Definition at line 809 of file crypt.c.

810{
811 if (!WithCrypto)
812 return;
813
814 const bool c_pgp_strict_enc = cs_subset_bool(NeoMutt->sub, "pgp_strict_enc");
815 while (b)
816 {
817 if (b->type == TYPE_MULTIPART)
818 {
819 if (b->encoding != ENC_7BIT)
820 {
821 b->encoding = ENC_7BIT;
823 }
824 else if (((WithCrypto & APPLICATION_PGP) != 0) && c_pgp_strict_enc)
825 {
827 }
828 }
829 else if ((b->type == TYPE_MESSAGE) && !mutt_istr_equal(b->subtype, "delivery-status"))
830 {
831 if (b->encoding != ENC_7BIT)
833 }
834 else if (b->encoding == ENC_8BIT)
835 {
837 }
838 else if (b->encoding == ENC_BINARY)
839 {
840 b->encoding = ENC_BASE64;
841 }
842 else if (b->content && (b->encoding != ENC_BASE64) &&
843 (b->content->from || (b->content->space && c_pgp_strict_enc)))
844 {
846 }
847 b = b->next;
848 }
849}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
void crypt_convert_to_7bit(struct Body *b)
Convert an email to 7bit encoding.
Definition: crypt.c:809
@ ENC_7BIT
7-bit text
Definition: mime.h:49
@ ENC_BINARY
Binary.
Definition: mime.h:53
@ ENC_BASE64
Base-64 encoded text.
Definition: mime.h:52
@ ENC_8BIT
8-bit text
Definition: mime.h:50
@ ENC_QUOTED_PRINTABLE
Quoted-printable text.
Definition: mime.h:51
@ TYPE_MESSAGE
Type: 'message/*'.
Definition: mime.h:35
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define WithCrypto
Definition: lib.h:116
void mutt_message_to_7bit(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Convert an email's MIME parts to 7-bit.
Definition: sendlib.c:259
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:73
struct Content * content
Detailed info about the content of the attachment.
Definition: body.h:70
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
bool space
Whitespace at the end of lines?
Definition: content.h:43
bool from
Has a line beginning with "From "?
Definition: content.h:45
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_current_time()

void crypt_current_time ( struct State state,
const char *  app_name 
)

Print the current time.

Parameters
stateState to use
app_nameApp name, e.g. "PGP"

print the current time to avoid spoofing of the signature output

Definition at line 65 of file crypt.c.

66{
67 char p[256] = { 0 };
68 char tmp[512] = { 0 };
69
70 if (!WithCrypto)
71 return;
72
73 const bool c_crypt_timestamp = cs_subset_bool(NeoMutt->sub, "crypt_timestamp");
74 if (c_crypt_timestamp)
75 {
76 mutt_date_localtime_format(p, sizeof(p), _(" (current time: %c)"), mutt_date_now());
77 }
78 else
79 {
80 *p = '\0';
81 }
82
83 snprintf(tmp, sizeof(tmp), _("[-- %s output follows%s --]\n"), NONULL(app_name), p);
84 state_attach_puts(state, tmp);
85}
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:951
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:456
#define _(a)
Definition: message.h:28
void state_attach_puts(struct State *state, const char *t)
Write a string to the state.
Definition: state.c:103
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_get_fingerprint_or_id()

const char * crypt_get_fingerprint_or_id ( const char *  p,
const char **  pphint,
const char **  ppl,
const char **  pps 
)

Get the fingerprint or long key ID.

Parameters
[in]pString to examine
[out]pphintStart of string to be passed to pgp_add_string_to_hints() or crypt_add_string_to_hints()
[out]pplStart of long key ID if detected, else NULL
[out]ppsStart of short key ID if detected, else NULL
Return values
ptrCopy of fingerprint, if any, stripped of all spaces. Must be FREE'd by caller
NULLOtherwise

Obtain pointers to fingerprint or short or long key ID, if any.

Upon return, at most one of return, *ppl and *pps pointers is non-NULL, indicating the longest fingerprint or ID found, if any.

Definition at line 1380 of file crypt.c.

1382{
1383 const char *ps = NULL, *pl = NULL, *phint = NULL;
1384 char *pfcopy = NULL, *s1 = NULL, *s2 = NULL;
1385 char c;
1386 int isid;
1387 size_t hexdigits;
1388
1389 /* User input may be partial name, fingerprint or short or long key ID,
1390 * independent of `$pgp_long_ids`.
1391 * Fingerprint without spaces is 40 hex digits (SHA-1) or 32 hex digits (MD5).
1392 * Strip leading "0x" for key ID detection and prepare pl and ps to indicate
1393 * if an ID was found and to simplify logic in the key loop's inner
1394 * condition of the caller. */
1395
1396 char *pf = mutt_str_skip_whitespace(p);
1397 if (mutt_istr_startswith(pf, "0x"))
1398 pf += 2;
1399
1400 /* Check if a fingerprint is given, must be hex digits only, blanks
1401 * separating groups of 4 hex digits are allowed. Also pre-check for ID. */
1402 isid = 2; /* unknown */
1403 hexdigits = 0;
1404 s1 = pf;
1405 do
1406 {
1407 c = *(s1++);
1408 if ((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) ||
1409 (('a' <= c) && (c <= 'f')))
1410 {
1411 hexdigits++;
1412 if (isid == 2)
1413 isid = 1; /* it is an ID so far */
1414 }
1415 else if (c)
1416 {
1417 isid = 0; /* not an ID */
1418 if ((c == ' ') && ((hexdigits % 4) == 0))
1419 ; /* skip blank before or after 4 hex digits */
1420 else
1421 break; /* any other character or position */
1422 }
1423 } while (c);
1424
1425 /* If at end of input, check for correct fingerprint length and copy if. */
1426 pfcopy = (!c && ((hexdigits == 40) || (hexdigits == 32)) ? mutt_str_dup(pf) : NULL);
1427
1428 if (pfcopy)
1429 {
1430 /* Use pfcopy to strip all spaces from fingerprint and as hint. */
1431 s1 = pfcopy;
1432 s2 = pfcopy;
1433 do
1434 {
1435 *(s1++) = *(s2 = mutt_str_skip_whitespace(s2));
1436 } while (*(s2++));
1437
1438 phint = pfcopy;
1439 ps = NULL;
1440 pl = NULL;
1441 }
1442 else
1443 {
1444 phint = p;
1445 ps = NULL;
1446 pl = NULL;
1447 if (isid == 1)
1448 {
1449 if (mutt_str_len(pf) == 16)
1450 pl = pf; /* long key ID */
1451 else if (mutt_str_len(pf) == 8)
1452 ps = pf; /* short key ID */
1453 }
1454 }
1455
1456 *pphint = phint;
1457 *ppl = pl;
1458 *pps = ps;
1459 return pfcopy;
1460}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
char * mutt_str_skip_whitespace(const char *p)
Find the first non-whitespace character in a string.
Definition: string.c:551
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_is_numerical_keyid()

bool crypt_is_numerical_keyid ( const char *  s)

Is this a numerical keyid.

Parameters
sKey to test
Return values
trueKeyid is numeric

Check if a crypt-hook value is a key id.

Definition at line 1469 of file crypt.c.

1470{
1471 /* or should we require the "0x"? */
1472 if (mutt_strn_equal(s, "0x", 2))
1473 s += 2;
1474 if (strlen(s) % 8)
1475 return false;
1476 while (*s)
1477 if (!strchr("0123456789ABCDEFabcdef", *s++))
1478 return false;
1479
1480 return true;
1481}
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:425
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_write_signed()

int crypt_write_signed ( struct Body b,
struct State state,
const char *  tempfile 
)

Write the message body/part.

Parameters
bBody to write
stateState to use
tempfileFile to write to
Return values
0Success
-1Error

Body/part A described by state state to the given TEMPFILE.

Definition at line 759 of file crypt.c.

760{
761 if (!WithCrypto)
762 return -1;
763
764 FILE *fp = mutt_file_fopen(tempfile, "w");
765 if (!fp)
766 {
767 mutt_perror("%s", tempfile);
768 return -1;
769 }
770
771 if (!mutt_file_seek(state->fp_in, b->hdr_offset, SEEK_SET))
772 {
773 mutt_file_fclose(&fp);
774 return -1;
775 }
776 size_t bytes = b->length + b->offset - b->hdr_offset;
777 bool hadcr = false;
778 while (bytes > 0)
779 {
780 const int c = fgetc(state->fp_in);
781 if (c == EOF)
782 break;
783
784 bytes--;
785
786 if (c == '\r')
787 {
788 hadcr = true;
789 }
790 else
791 {
792 if ((c == '\n') && !hadcr)
793 fputc('\r', fp);
794
795 hadcr = false;
796 }
797
798 fputc(c, fp);
799 }
800 mutt_file_fclose(&fp);
801
802 return 0;
803}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:778
#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
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition: body.h:81
FILE * fp_in
File to read from.
Definition: state.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function: