1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2019 Daniel Kahn Gillmor
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Authors: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
26 _notmuch_crypto_payload_has_legacy_display (GMimeObject *payload)
28 GMimeMultipart *mpayload;
29 const char *protected_header_parameter;
32 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (payload),
33 "multipart", "mixed"))
35 protected_header_parameter = g_mime_object_get_content_type_parameter (payload,
37 if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
39 if (! GMIME_IS_MULTIPART (payload))
41 mpayload = GMIME_MULTIPART (payload);
44 if (g_mime_multipart_get_count (mpayload) != 2)
46 first = g_mime_multipart_get_part (mpayload, 0);
47 /* Early implementations that generated "Legacy Display" parts used
48 * Content-Type: text/rfc822-headers, but text/plain is more widely
49 * rendered, so it is now the standard choice. We accept either as a
50 * Legacy Display part. */
51 if (! (g_mime_content_type_is_type (g_mime_object_get_content_type (first),
53 g_mime_content_type_is_type (g_mime_object_get_content_type (first),
54 "text", "rfc822-headers")))
56 protected_header_parameter = g_mime_object_get_content_type_parameter (first,
58 if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
60 if (! GMIME_IS_TEXT_PART (first))
67 _notmuch_repair_crypto_payload_skip_legacy_display (GMimeObject *payload)
69 if (_notmuch_crypto_payload_has_legacy_display (payload)) {
70 return g_mime_multipart_get_part (GMIME_MULTIPART (payload), 1);
77 * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.1 */
79 _notmuch_is_mixed_up_mangled (GMimeObject *part)
81 GMimeMultipart *mpart = NULL;
82 GMimeObject *parts[3] = { NULL, NULL, NULL };
83 GMimeContentType *type = NULL;
84 char *prelude_string = NULL;
85 bool prelude_is_empty;
89 type = g_mime_object_get_content_type (part);
92 if (! g_mime_content_type_is_type (type, "multipart", "mixed"))
94 if (! GMIME_IS_MULTIPART (part)) /* probably impossible */
96 mpart = GMIME_MULTIPART (part);
99 if (g_mime_multipart_get_count (mpart) != 3)
101 parts[0] = g_mime_multipart_get_part (mpart, 0);
102 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[0]),
105 if (! GMIME_IS_TEXT_PART (parts[0]))
107 parts[1] = g_mime_multipart_get_part (mpart, 1);
108 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[1]),
109 "application", "pgp-encrypted"))
111 parts[2] = g_mime_multipart_get_part (mpart, 2);
112 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[2]),
113 "application", "octet-stream"))
116 /* Is parts[0] length 0? */
117 prelude_string = g_mime_text_part_get_text (GMIME_TEXT_PART (parts[0]));
118 prelude_is_empty = (prelude_string[0] == '\0');
119 g_free (prelude_string);
120 if (! prelude_is_empty)
123 /* FIXME: after decoding and stripping whitespace, is parts[1]
124 * subpart just "Version: 1" ? */
126 /* FIXME: can we determine that parts[2] subpart is *only* PGP
127 * encrypted data? I tried g_mime_part_get_openpgp_data () but
128 * found https://github.com/jstedfast/gmime/issues/60 */
135 * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.2 */
137 _notmuch_repair_mixed_up_mangled (GMimeObject *part)
139 GMimeMultipart *mpart = NULL, *mpart_ret = NULL;
140 GMimeObject *ret = NULL;
142 if (! _notmuch_is_mixed_up_mangled (part))
144 mpart = GMIME_MULTIPART (part);
145 ret = GMIME_OBJECT (g_mime_multipart_encrypted_new ());
148 mpart_ret = GMIME_MULTIPART (ret);
149 if (mpart_ret == NULL) {
150 g_object_unref (ret);
153 g_mime_object_set_content_type_parameter (ret, "protocol", "application/pgp-encrypted");
155 g_mime_multipart_insert (mpart_ret, 0, g_mime_multipart_get_part (mpart, 1));
156 g_mime_multipart_insert (mpart_ret, 1, g_mime_multipart_get_part (mpart, 2));