NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
decrypt_mime()

Decrypt an encrypted MIME part. More...

+ Collaboration diagram for decrypt_mime():

Functions

int pgp_gpgme_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
 Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
 
int smime_gpgme_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
 Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
 
int pgp_class_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
 Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
 
int smime_class_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
 Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
 

Detailed Description

Decrypt an encrypted MIME part.

Parameters
[in]fp_inFile containing the encrypted part
[out]fp_outFile containing the decrypted part
[in]bBody of the email
[out]b_decBody containing the decrypted part
Return values
0Success
-1Failure

Function Documentation

◆ pgp_gpgme_decrypt_mime()

int pgp_gpgme_decrypt_mime ( FILE *  fp_in,
FILE **  fp_out,
struct Body b,
struct Body **  b_dec 
)

Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1896 of file crypt_gpgme.c.

1897{
1898 struct State state = { 0 };
1899 struct Body *first_part = b;
1900 int is_signed = 0;
1901 bool need_decode = false;
1902 LOFF_T saved_offset = 0;
1903 size_t saved_length = 0;
1904 FILE *fp_decoded = NULL;
1905 int rc = 0;
1906
1907 first_part->goodsig = false;
1908 first_part->warnsig = false;
1909
1911 {
1912 b = b->parts->next;
1913 /* Some clients improperly encode the octetstream part. */
1914 if (b->encoding != ENC_7BIT)
1915 need_decode = true;
1916 }
1918 {
1919 b = b->parts->next->next;
1920 need_decode = true;
1921 }
1922 else
1923 {
1924 return -1;
1925 }
1926
1927 state.fp_in = fp_in;
1928
1929 if (need_decode)
1930 {
1931 saved_offset = b->offset;
1932 saved_length = b->length;
1933
1934 fp_decoded = mutt_file_mkstemp();
1935 if (!fp_decoded)
1936 {
1937 mutt_perror(_("Can't create temporary file"));
1938 return -1;
1939 }
1940
1941 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1942 {
1943 rc = -1;
1944 goto bail;
1945 }
1946 state.fp_out = fp_decoded;
1947
1948 mutt_decode_attachment(b, &state);
1949
1950 fflush(fp_decoded);
1951 b->length = ftello(fp_decoded);
1952 b->offset = 0;
1953 rewind(fp_decoded);
1954 state.fp_in = fp_decoded;
1955 state.fp_out = 0;
1956 }
1957
1958 *fp_out = mutt_file_mkstemp();
1959 if (!*fp_out)
1960 {
1961 mutt_perror(_("Can't create temporary file"));
1962 rc = -1;
1963 goto bail;
1964 }
1965
1966 *b_dec = decrypt_part(b, &state, *fp_out, false, &is_signed);
1967 if (*b_dec)
1968 {
1969 rewind(*fp_out);
1970 if (is_signed > 0)
1971 first_part->goodsig = true;
1972 }
1973 else
1974 {
1975 rc = -1;
1976 mutt_file_fclose(fp_out);
1977 }
1978
1979bail:
1980 if (need_decode)
1981 {
1982 b->length = saved_length;
1983 b->offset = saved_offset;
1984 mutt_file_fclose(&fp_decoded);
1985 }
1986
1987 return rc;
1988}
int mutt_is_valid_multipart_pgp_encrypted(struct Body *b)
Is this a valid multi-part encrypted message?
Definition: crypt.c:467
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition: crypt.c:504
static struct Body * decrypt_part(struct Body *b, struct State *state, FILE *fp_out, bool is_smime, int *r_is_signed)
Decrypt a PGP or SMIME message.
Definition: crypt_gpgme.c:1732
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_perror(...)
Definition: logging2.h:93
void mutt_decode_attachment(const struct Body *b, struct State *state)
Decode an email's attachment.
Definition: handler.c:1906
@ ENC_7BIT
7-bit text
Definition: mime.h:49
#define _(a)
Definition: message.h:28
The body of an email.
Definition: body.h:36
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:73
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
struct Body * next
next attachment in the list
Definition: body.h:72
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition: body.h:41
bool goodsig
Good cryptographic signature.
Definition: body.h:45
bool warnsig
Maybe good signature.
Definition: body.h:48
Keep track when processing files.
Definition: state.h:48
FILE * fp_out
File to write to.
Definition: state.h:50
FILE * fp_in
File to read from.
Definition: state.h:49
#define mutt_file_mkstemp()
Definition: tmp.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smime_gpgme_decrypt_mime()

int smime_gpgme_decrypt_mime ( FILE *  fp_in,
FILE **  fp_out,
struct Body b,
struct Body **  b_dec 
)

Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1993 of file crypt_gpgme.c.

1994{
1995 struct State state = { 0 };
1996 int is_signed;
1997 LOFF_T saved_b_offset;
1998 size_t saved_b_length;
1999
2001 return -1;
2002
2003 if (b->parts)
2004 return -1;
2005
2006 /* Decode the body - we need to pass binary CMS to the
2007 * backend. The backend allows for Base64 encoded data but it does
2008 * not allow for QP which I have seen in some messages. So better
2009 * do it here. */
2010 saved_b_offset = b->offset;
2011 saved_b_length = b->length;
2012 state.fp_in = fp_in;
2013 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
2014 {
2015 return -1;
2016 }
2017 FILE *fp_tmp = mutt_file_mkstemp();
2018 if (!fp_tmp)
2019 {
2020 mutt_perror(_("Can't create temporary file"));
2021 return -1;
2022 }
2023
2024 state.fp_out = fp_tmp;
2025 mutt_decode_attachment(b, &state);
2026 fflush(fp_tmp);
2027 b->length = ftello(state.fp_out);
2028 b->offset = 0;
2029 rewind(fp_tmp);
2030
2031 memset(&state, 0, sizeof(state));
2032 state.fp_in = fp_tmp;
2033 state.fp_out = 0;
2035 if (!*fp_out)
2036 {
2037 mutt_perror(_("Can't create temporary file"));
2038 return -1;
2039 }
2040
2041 *b_dec = decrypt_part(b, &state, *fp_out, true, &is_signed);
2042 if (*b_dec)
2043 (*b_dec)->goodsig = is_signed > 0;
2044 b->length = saved_b_length;
2045 b->offset = saved_b_offset;
2046 mutt_file_fclose(&fp_tmp);
2047 rewind(*fp_out);
2048 if (*b_dec && !is_signed && !(*b_dec)->parts && mutt_is_application_smime(*b_dec))
2049 {
2050 /* Assume that this is a opaque signed s/mime message. This is an ugly way
2051 * of doing it but we have anyway a problem with arbitrary encoded S/MIME
2052 * messages: Only the outer part may be encrypted. The entire mime parsing
2053 * should be revamped, probably by keeping the temporary files so that we
2054 * don't need to decrypt them all the time. Inner parts of an encrypted
2055 * part can then point into this file and there won't ever be a need to
2056 * decrypt again. This needs a partial rewrite of the MIME engine. */
2057 struct Body *bb = *b_dec;
2058
2059 saved_b_offset = bb->offset;
2060 saved_b_length = bb->length;
2061 memset(&state, 0, sizeof(state));
2062 state.fp_in = *fp_out;
2063 if (!mutt_file_seek(state.fp_in, bb->offset, SEEK_SET))
2064 {
2065 return -1;
2066 }
2067 FILE *fp_tmp2 = mutt_file_mkstemp();
2068 if (!fp_tmp2)
2069 {
2070 mutt_perror(_("Can't create temporary file"));
2071 return -1;
2072 }
2073
2074 state.fp_out = fp_tmp2;
2075 mutt_decode_attachment(bb, &state);
2076 fflush(fp_tmp2);
2077 bb->length = ftello(state.fp_out);
2078 bb->offset = 0;
2079 rewind(fp_tmp2);
2080 mutt_file_fclose(fp_out);
2081
2082 memset(&state, 0, sizeof(state));
2083 state.fp_in = fp_tmp2;
2084 state.fp_out = 0;
2085 *fp_out = mutt_file_mkstemp();
2086 if (!*fp_out)
2087 {
2088 mutt_perror(_("Can't create temporary file"));
2089 return -1;
2090 }
2091
2092 struct Body *b_tmp = decrypt_part(bb, &state, *fp_out, true, &is_signed);
2093 if (b_tmp)
2094 b_tmp->goodsig = is_signed > 0;
2095 bb->length = saved_b_length;
2096 bb->offset = saved_b_offset;
2097 mutt_file_fclose(&fp_tmp2);
2098 rewind(*fp_out);
2099 mutt_body_free(b_dec);
2100 *b_dec = b_tmp;
2101 }
2102 return *b_dec ? 0 : -1;
2103}
SecurityFlags mutt_is_application_smime(struct Body *b)
Does the message use S/MIME?
Definition: crypt.c:609
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:58
#define SEC_NO_FLAGS
No flags are set.
Definition: lib.h:83
+ Here is the call graph for this function:

◆ pgp_class_decrypt_mime()

int pgp_class_decrypt_mime ( FILE *  fp_in,
FILE **  fp_out,
struct Body b,
struct Body **  b_dec 
)

Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1162 of file pgp.c.

1163{
1164 struct State state = { 0 };
1165 struct Body *p = b;
1166 bool need_decode = false;
1167 LOFF_T saved_offset = 0;
1168 size_t saved_length = 0;
1169 FILE *fp_decoded = NULL;
1170 int rc = 0;
1171
1173 {
1174 b = b->parts->next;
1175 /* Some clients improperly encode the octetstream part. */
1176 if (b->encoding != ENC_7BIT)
1177 need_decode = true;
1178 }
1180 {
1181 b = b->parts->next->next;
1182 need_decode = true;
1183 }
1184 else
1185 {
1186 return -1;
1187 }
1188
1189 state.fp_in = fp_in;
1190
1191 if (need_decode)
1192 {
1193 saved_offset = b->offset;
1194 saved_length = b->length;
1195
1196 fp_decoded = mutt_file_mkstemp();
1197 if (!fp_decoded)
1198 {
1199 mutt_perror(_("Can't create temporary file"));
1200 return -1;
1201 }
1202
1203 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1204 {
1205 rc = -1;
1206 goto bail;
1207 }
1208 state.fp_out = fp_decoded;
1209
1210 mutt_decode_attachment(b, &state);
1211
1212 fflush(fp_decoded);
1213 b->length = ftello(fp_decoded);
1214 b->offset = 0;
1215 rewind(fp_decoded);
1216 state.fp_in = fp_decoded;
1217 state.fp_out = 0;
1218 }
1219
1220 *fp_out = mutt_file_mkstemp();
1221 if (!*fp_out)
1222 {
1223 mutt_perror(_("Can't create temporary file"));
1224 rc = -1;
1225 goto bail;
1226 }
1227
1228 *b_dec = pgp_decrypt_part(b, &state, *fp_out, p);
1229 if (!*b_dec)
1230 rc = -1;
1231 rewind(*fp_out);
1232
1233bail:
1234 if (need_decode)
1235 {
1236 b->length = saved_length;
1237 b->offset = saved_offset;
1238 mutt_file_fclose(&fp_decoded);
1239 }
1240
1241 return rc;
1242}
static struct Body * pgp_decrypt_part(struct Body *a, struct State *state, FILE *fp_out, struct Body *p)
Decrypt part of a PGP message.
Definition: pgp.c:1026
+ Here is the call graph for this function:

◆ smime_class_decrypt_mime()

int smime_class_decrypt_mime ( FILE *  fp_in,
FILE **  fp_out,
struct Body b,
struct Body **  b_dec 
)

Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1917 of file smime.c.

1918{
1919 struct State state = { 0 };
1920 LOFF_T tmpoffset = b->offset;
1921 size_t tmplength = b->length;
1922 int rc = -1;
1923
1925 return -1;
1926
1927 if (b->parts)
1928 return -1;
1929
1930 state.fp_in = fp_in;
1931 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1932 {
1933 return -1;
1934 }
1935
1936 FILE *fp_tmp = mutt_file_mkstemp();
1937 if (!fp_tmp)
1938 {
1939 mutt_perror(_("Can't create temporary file"));
1940 return -1;
1941 }
1942
1943 state.fp_out = fp_tmp;
1944 mutt_decode_attachment(b, &state);
1945 fflush(fp_tmp);
1946 b->length = ftello(state.fp_out);
1947 b->offset = 0;
1948 rewind(fp_tmp);
1949 state.fp_in = fp_tmp;
1950 state.fp_out = 0;
1951
1953 if (!*fp_out)
1954 {
1955 mutt_perror(_("Can't create temporary file"));
1956 goto bail;
1957 }
1958
1959 *b_dec = smime_handle_entity(b, &state, *fp_out);
1960 if (!*b_dec)
1961 goto bail;
1962
1963 (*b_dec)->goodsig = b->goodsig;
1964 (*b_dec)->badsig = b->badsig;
1965 rc = 0;
1966
1967bail:
1968 b->length = tmplength;
1969 b->offset = tmpoffset;
1970 mutt_file_fclose(&fp_tmp);
1971 if (*fp_out)
1972 rewind(*fp_out);
1973
1974 return rc;
1975}
static struct Body * smime_handle_entity(struct Body *b, struct State *state, FILE *fp_out_file)
Handle type application/pkcs7-mime.
Definition: smime.c:1679
bool badsig
Bad cryptographic signature (needed to check encrypted s/mime-signatures)
Definition: body.h:43
+ Here is the call graph for this function: