NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
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 **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 
int smime_gpgme_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 
int pgp_class_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 
int smime_class_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 

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]curBody 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 **  cur 
)

Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1853 of file crypt_gpgme.c.

1854{
1855 struct State state = { 0 };
1856 struct Body *first_part = b;
1857 int is_signed = 0;
1858 bool need_decode = false;
1859 LOFF_T saved_offset = 0;
1860 size_t saved_length = 0;
1861 FILE *fp_decoded = NULL;
1862 int rc = 0;
1863
1864 first_part->goodsig = false;
1865 first_part->warnsig = false;
1866
1868 {
1869 b = b->parts->next;
1870 /* Some clients improperly encode the octetstream part. */
1871 if (b->encoding != ENC_7BIT)
1872 need_decode = true;
1873 }
1875 {
1876 b = b->parts->next->next;
1877 need_decode = true;
1878 }
1879 else
1880 {
1881 return -1;
1882 }
1883
1884 state.fp_in = fp_in;
1885
1886 if (need_decode)
1887 {
1888 saved_offset = b->offset;
1889 saved_length = b->length;
1890
1891 fp_decoded = mutt_file_mkstemp();
1892 if (!fp_decoded)
1893 {
1894 mutt_perror(_("Can't create temporary file"));
1895 return -1;
1896 }
1897
1898 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1899 {
1900 rc = -1;
1901 goto bail;
1902 }
1903 state.fp_out = fp_decoded;
1904
1905 mutt_decode_attachment(b, &state);
1906
1907 fflush(fp_decoded);
1908 b->length = ftello(fp_decoded);
1909 b->offset = 0;
1910 rewind(fp_decoded);
1911 state.fp_in = fp_decoded;
1912 state.fp_out = 0;
1913 }
1914
1915 *fp_out = mutt_file_mkstemp();
1916 if (!*fp_out)
1917 {
1918 mutt_perror(_("Can't create temporary file"));
1919 rc = -1;
1920 goto bail;
1921 }
1922
1923 *cur = decrypt_part(b, &state, *fp_out, false, &is_signed);
1924 if (*cur)
1925 {
1926 rewind(*fp_out);
1927 if (is_signed > 0)
1928 first_part->goodsig = true;
1929 }
1930 else
1931 {
1932 rc = -1;
1933 mutt_file_fclose(fp_out);
1934 }
1935
1936bail:
1937 if (need_decode)
1938 {
1939 b->length = saved_length;
1940 b->offset = saved_offset;
1941 mutt_file_fclose(&fp_decoded);
1942 }
1943
1944 return rc;
1945}
int mutt_is_valid_multipart_pgp_encrypted(struct Body *b)
Is this a valid multi-part encrypted message?
Definition: crypt.c:457
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition: crypt.c:494
static struct Body * decrypt_part(struct Body *a, struct State *state, FILE *fp_out, bool is_smime, int *r_is_signed)
Decrypt a PGP or SMIME message.
Definition: crypt_gpgme.c:1693
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:708
#define mutt_perror(...)
Definition: logging2.h:91
void mutt_decode_attachment(struct Body *b, struct State *state)
Decode an email's attachment.
Definition: handler.c:1892
@ 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:72
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:71
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:47
FILE * fp_out
File to write to.
Definition: state.h:49
FILE * fp_in
File to read from.
Definition: state.h:48
#define mutt_file_mkstemp()
Definition: tmp.h:40
+ 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 **  cur 
)

Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1950 of file crypt_gpgme.c.

1951{
1952 struct State state = { 0 };
1953 int is_signed;
1954 LOFF_T saved_b_offset;
1955 size_t saved_b_length;
1956
1958 return -1;
1959
1960 if (b->parts)
1961 return -1;
1962
1963 /* Decode the body - we need to pass binary CMS to the
1964 * backend. The backend allows for Base64 encoded data but it does
1965 * not allow for QP which I have seen in some messages. So better
1966 * do it here. */
1967 saved_b_offset = b->offset;
1968 saved_b_length = b->length;
1969 state.fp_in = fp_in;
1970 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1971 {
1972 return -1;
1973 }
1974 FILE *fp_tmp = mutt_file_mkstemp();
1975 if (!fp_tmp)
1976 {
1977 mutt_perror(_("Can't create temporary file"));
1978 return -1;
1979 }
1980
1981 state.fp_out = fp_tmp;
1982 mutt_decode_attachment(b, &state);
1983 fflush(fp_tmp);
1984 b->length = ftello(state.fp_out);
1985 b->offset = 0;
1986 rewind(fp_tmp);
1987
1988 memset(&state, 0, sizeof(state));
1989 state.fp_in = fp_tmp;
1990 state.fp_out = 0;
1992 if (!*fp_out)
1993 {
1994 mutt_perror(_("Can't create temporary file"));
1995 return -1;
1996 }
1997
1998 *cur = decrypt_part(b, &state, *fp_out, true, &is_signed);
1999 if (*cur)
2000 (*cur)->goodsig = is_signed > 0;
2001 b->length = saved_b_length;
2002 b->offset = saved_b_offset;
2003 mutt_file_fclose(&fp_tmp);
2004 rewind(*fp_out);
2005 if (*cur && !is_signed && !(*cur)->parts && mutt_is_application_smime(*cur))
2006 {
2007 /* Assume that this is a opaque signed s/mime message. This is an ugly way
2008 * of doing it but we have anyway a problem with arbitrary encoded S/MIME
2009 * messages: Only the outer part may be encrypted. The entire mime parsing
2010 * should be revamped, probably by keeping the temporary files so that we
2011 * don't need to decrypt them all the time. Inner parts of an encrypted
2012 * part can then point into this file and there won't ever be a need to
2013 * decrypt again. This needs a partial rewrite of the MIME engine. */
2014 struct Body *bb = *cur;
2015
2016 saved_b_offset = bb->offset;
2017 saved_b_length = bb->length;
2018 memset(&state, 0, sizeof(state));
2019 state.fp_in = *fp_out;
2020 if (!mutt_file_seek(state.fp_in, bb->offset, SEEK_SET))
2021 {
2022 return -1;
2023 }
2024 FILE *fp_tmp2 = mutt_file_mkstemp();
2025 if (!fp_tmp2)
2026 {
2027 mutt_perror(_("Can't create temporary file"));
2028 return -1;
2029 }
2030
2031 state.fp_out = fp_tmp2;
2032 mutt_decode_attachment(bb, &state);
2033 fflush(fp_tmp2);
2034 bb->length = ftello(state.fp_out);
2035 bb->offset = 0;
2036 rewind(fp_tmp2);
2037 mutt_file_fclose(fp_out);
2038
2039 memset(&state, 0, sizeof(state));
2040 state.fp_in = fp_tmp2;
2041 state.fp_out = 0;
2042 *fp_out = mutt_file_mkstemp();
2043 if (!*fp_out)
2044 {
2045 mutt_perror(_("Can't create temporary file"));
2046 return -1;
2047 }
2048
2049 struct Body *b_tmp = decrypt_part(bb, &state, *fp_out, true, &is_signed);
2050 if (b_tmp)
2051 b_tmp->goodsig = is_signed > 0;
2052 bb->length = saved_b_length;
2053 bb->offset = saved_b_offset;
2054 mutt_file_fclose(&fp_tmp2);
2055 rewind(*fp_out);
2056 mutt_body_free(cur);
2057 *cur = b_tmp;
2058 }
2059 return *cur ? 0 : -1;
2060}
SecurityFlags mutt_is_application_smime(struct Body *b)
Does the message use S/MIME?
Definition: crypt.c:599
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
#define SEC_NO_FLAGS
No flags are set.
Definition: lib.h:77
+ 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 **  cur 
)

Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 1164 of file pgp.c.

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

Implements CryptModuleSpecs::decrypt_mime() -.

Definition at line 2108 of file smime.c.

2109{
2110 struct State state = { 0 };
2111 LOFF_T tmpoffset = b->offset;
2112 size_t tmplength = b->length;
2113 int rc = -1;
2114
2116 return -1;
2117
2118 if (b->parts)
2119 return -1;
2120
2121 state.fp_in = fp_in;
2122 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
2123 {
2124 return -1;
2125 }
2126
2127 FILE *fp_tmp = mutt_file_mkstemp();
2128 if (!fp_tmp)
2129 {
2130 mutt_perror(_("Can't create temporary file"));
2131 return -1;
2132 }
2133
2134 state.fp_out = fp_tmp;
2135 mutt_decode_attachment(b, &state);
2136 fflush(fp_tmp);
2137 b->length = ftello(state.fp_out);
2138 b->offset = 0;
2139 rewind(fp_tmp);
2140 state.fp_in = fp_tmp;
2141 state.fp_out = 0;
2142
2144 if (!*fp_out)
2145 {
2146 mutt_perror(_("Can't create temporary file"));
2147 goto bail;
2148 }
2149
2150 *cur = smime_handle_entity(b, &state, *fp_out);
2151 if (!*cur)
2152 goto bail;
2153
2154 (*cur)->goodsig = b->goodsig;
2155 (*cur)->badsig = b->badsig;
2156 rc = 0;
2157
2158bail:
2159 b->length = tmplength;
2160 b->offset = tmpoffset;
2161 mutt_file_fclose(&fp_tmp);
2162 if (*fp_out)
2163 rewind(*fp_out);
2164
2165 return rc;
2166}
static struct Body * smime_handle_entity(struct Body *m, struct State *state, FILE *fp_out_file)
Handle type application/pkcs7-mime.
Definition: smime.c:1870
bool badsig
Bad cryptographic signature (needed to check encrypted s/mime-signatures)
Definition: body.h:43
+ Here is the call graph for this function: