NeoMutt  2022-04-29-323-g5fcc6c
Teaching an old dog new tricks
DOXYGEN
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 *a)
 Convert an email to 7bit encoding. More...
 
void crypt_current_time (struct State *s, const char *app_name)
 Print the current time. More...
 
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. More...
 
bool crypt_is_numerical_keyid (const char *s)
 Is this a numerical keyid. More...
 
int crypt_write_signed (struct Body *a, struct State *s, const char *tempfile)
 Write the message body/part. More...
 

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 a)

Convert an email to 7bit encoding.

Parameters
aBody of email to convert

Definition at line 793 of file crypt.c.

794{
795 if (!WithCrypto)
796 return;
797
798 const bool c_pgp_strict_enc = cs_subset_bool(NeoMutt->sub, "pgp_strict_enc");
799 while (a)
800 {
801 if (a->type == TYPE_MULTIPART)
802 {
803 if (a->encoding != ENC_7BIT)
804 {
805 a->encoding = ENC_7BIT;
807 }
808 else if (((WithCrypto & APPLICATION_PGP) != 0) && c_pgp_strict_enc)
810 }
811 else if ((a->type == TYPE_MESSAGE) && !mutt_istr_equal(a->subtype, "delivery-status"))
812 {
813 if (a->encoding != ENC_7BIT)
815 }
816 else if (a->encoding == ENC_8BIT)
818 else if (a->encoding == ENC_BINARY)
819 a->encoding = ENC_BASE64;
820 else if (a->content && (a->encoding != ENC_BASE64) &&
821 (a->content->from || (a->content->space && c_pgp_strict_enc)))
822 {
824 }
825 a = a->next;
826 }
827}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
void crypt_convert_to_7bit(struct Body *a)
Convert an email to 7bit encoding.
Definition: crypt.c:793
@ 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:819
#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 *a, FILE *fp, struct ConfigSubset *sub)
Convert an email's MIME parts to 7-bit.
Definition: sendlib.c:254
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
struct Content * content
Detailed info about the content of the attachment.
Definition: body.h:69
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
bool space
Whitespace at the end of lines?
Definition: content.h:42
bool from
Has a line beginning with "From "?
Definition: content.h:44
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:
+ Here is the caller graph for this function:

◆ crypt_current_time()

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

Print the current time.

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

print the current time to avoid spoofing of the signature output

Definition at line 70 of file crypt.c.

71{
72 char p[256], tmp[256];
73
74 if (!WithCrypto)
75 return;
76
77 const bool c_crypt_timestamp = cs_subset_bool(NeoMutt->sub, "crypt_timestamp");
78 if (c_crypt_timestamp)
79 {
80 mutt_date_localtime_format(p, sizeof(p), _(" (current time: %c)"), MUTT_DATE_NOW);
81 }
82 else
83 *p = '\0';
84
85 snprintf(tmp, sizeof(tmp), _("[-- %s output follows%s --]\n"), NONULL(app_name), p);
86 state_attach_puts(s, tmp);
87}
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:692
#define MUTT_DATE_NOW
Constant representing the 'current time', see: mutt_date_gmtime(), mutt_date_localtime()
Definition: date.h:39
#define _(a)
Definition: message.h:28
void state_attach_puts(struct State *s, const char *t)
Write a string to the state.
Definition: state.c:100
#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 1261 of file crypt.c.

1263{
1264 const char *ps = NULL, *pl = NULL, *phint = NULL;
1265 char *pfcopy = NULL, *s1 = NULL, *s2 = NULL;
1266 char c;
1267 int isid;
1268 size_t hexdigits;
1269
1270 /* User input may be partial name, fingerprint or short or long key ID,
1271 * independent of `$pgp_long_ids`.
1272 * Fingerprint without spaces is 40 hex digits (SHA-1) or 32 hex digits (MD5).
1273 * Strip leading "0x" for key ID detection and prepare pl and ps to indicate
1274 * if an ID was found and to simplify logic in the key loop's inner
1275 * condition of the caller. */
1276
1277 char *pf = mutt_str_skip_whitespace(p);
1278 if (mutt_istr_startswith(pf, "0x"))
1279 pf += 2;
1280
1281 /* Check if a fingerprint is given, must be hex digits only, blanks
1282 * separating groups of 4 hex digits are allowed. Also pre-check for ID. */
1283 isid = 2; /* unknown */
1284 hexdigits = 0;
1285 s1 = pf;
1286 do
1287 {
1288 c = *(s1++);
1289 if ((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) ||
1290 (('a' <= c) && (c <= 'f')))
1291 {
1292 hexdigits++;
1293 if (isid == 2)
1294 isid = 1; /* it is an ID so far */
1295 }
1296 else if (c)
1297 {
1298 isid = 0; /* not an ID */
1299 if ((c == ' ') && ((hexdigits % 4) == 0))
1300 ; /* skip blank before or after 4 hex digits */
1301 else
1302 break; /* any other character or position */
1303 }
1304 } while (c);
1305
1306 /* If at end of input, check for correct fingerprint length and copy if. */
1307 pfcopy = (!c && ((hexdigits == 40) || (hexdigits == 32)) ? mutt_str_dup(pf) : NULL);
1308
1309 if (pfcopy)
1310 {
1311 /* Use pfcopy to strip all spaces from fingerprint and as hint. */
1312 s1 = pfcopy;
1313 s2 = pfcopy;
1314 do
1315 {
1316 *(s1++) = *(s2 = mutt_str_skip_whitespace(s2));
1317 } while (*(s2++));
1318
1319 phint = pfcopy;
1320 ps = NULL;
1321 pl = NULL;
1322 }
1323 else
1324 {
1325 phint = p;
1326 ps = NULL;
1327 pl = NULL;
1328 if (isid == 1)
1329 {
1330 if (mutt_str_len(pf) == 16)
1331 pl = pf; /* long key ID */
1332 else if (mutt_str_len(pf) == 8)
1333 ps = pf; /* short key ID */
1334 }
1335 }
1336
1337 *pphint = phint;
1338 *ppl = pl;
1339 *pps = ps;
1340 return pfcopy;
1341}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
char * mutt_str_skip_whitespace(const char *p)
Find the first non-whitespace character in a string.
Definition: string.c:622
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:567
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:239
+ 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 1350 of file crypt.c.

1351{
1352 /* or should we require the "0x"? */
1353 if (mutt_strn_equal(s, "0x", 2))
1354 s += 2;
1355 if (strlen(s) % 8)
1356 return false;
1357 while (*s)
1358 if (!strchr("0123456789ABCDEFabcdef", *s++))
1359 return false;
1360
1361 return true;
1362}
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:496
+ 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 a,
struct State s,
const char *  tempfile 
)

Write the message body/part.

Parameters
aBody to write
sState to use
tempfileFile to write to
Return values
0Success
-1Error

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

Definition at line 745 of file crypt.c.

746{
747 if (!WithCrypto)
748 return -1;
749
750 FILE *fp = mutt_file_fopen(tempfile, "w");
751 if (!fp)
752 {
753 mutt_perror(tempfile);
754 return -1;
755 }
756
757 if (!mutt_file_seek(s->fp_in, a->hdr_offset, SEEK_SET))
758 {
759 mutt_file_fclose(&fp);
760 return -1;
761 }
762 size_t bytes = a->length + a->offset - a->hdr_offset;
763 bool hadcr = false;
764 while (bytes > 0)
765 {
766 const int c = fgetc(s->fp_in);
767 if (c == EOF)
768 break;
769
770 bytes--;
771
772 if (c == '\r')
773 hadcr = true;
774 else
775 {
776 if ((c == '\n') && !hadcr)
777 fputc('\r', fp);
778
779 hadcr = false;
780 }
781
782 fputc(c, fp);
783 }
784 mutt_file_fclose(&fp);
785
786 return 0;
787}
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:151
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:706
#define mutt_perror(...)
Definition: logging.h:88
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:80
FILE * fp_in
File to read from.
Definition: state.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function: