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;
30 GMimeTextPart *legacy_display;
31 char *legacy_display_header_text = NULL;
32 GMimeStream *stream = NULL;
33 GMimeParser *parser = NULL;
34 GMimeObject *legacy_header_object = NULL, *first;
35 GMimeHeaderList *legacy_display_headers = NULL, *protected_headers = NULL;
38 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (payload),
39 "multipart", "mixed"))
41 protected_header_parameter = g_mime_object_get_content_type_parameter (payload, "protected-headers");
42 if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
44 if (! GMIME_IS_MULTIPART (payload))
46 mpayload = GMIME_MULTIPART (payload);
49 if (g_mime_multipart_get_count (mpayload) != 2)
51 first = g_mime_multipart_get_part (mpayload, 0);
52 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (first),
53 "text", "rfc822-headers"))
55 protected_header_parameter = g_mime_object_get_content_type_parameter (first, "protected-headers");
56 if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
58 if (! GMIME_IS_TEXT_PART (first))
61 /* ensure that the headers in the first part all match the values
62 * found in the payload's own protected headers! if they don't,
63 * we should not treat this as a valid "legacy-display" part.
65 * Crafting a GMimeHeaderList object from the content of the
66 * text/rfc822-headers part is pretty clumsy; we should probably
67 * push something into GMime that makes this a one-shot
69 if ((protected_headers = g_mime_object_get_header_list (payload), protected_headers) &&
70 (legacy_display = GMIME_TEXT_PART (first), legacy_display) &&
71 (legacy_display_header_text = g_mime_text_part_get_text (legacy_display), legacy_display_header_text) &&
72 (stream = g_mime_stream_mem_new_with_buffer (legacy_display_header_text, strlen (legacy_display_header_text)), stream) &&
73 (g_mime_stream_write (stream, "\r\n\r\n", 4) == 4) &&
74 (g_mime_stream_seek (stream, 0, GMIME_STREAM_SEEK_SET) == 0) &&
75 (parser = g_mime_parser_new_with_stream (stream), parser) &&
76 (legacy_header_object = g_mime_parser_construct_part (parser, NULL), legacy_header_object) &&
77 (legacy_display_headers = g_mime_object_get_header_list (legacy_header_object), legacy_display_headers)) {
78 /* walk through legacy_display_headers, comparing them against
79 * their values in the protected_headers: */
81 for (int i = 0; i < g_mime_header_list_get_count (legacy_display_headers); i++) {
82 GMimeHeader *dh = g_mime_header_list_get_header_at (legacy_display_headers, i);
87 GMimeHeader *ph = g_mime_header_list_get_header (protected_headers, g_mime_header_get_name (dh));
92 const char *dhv = g_mime_header_get_value (dh);
93 const char *phv = g_mime_header_get_value (ph);
94 if (dhv == NULL || phv == NULL || strcmp (dhv, phv)) {
102 if (legacy_display_header_text)
103 g_free (legacy_display_header_text);
105 g_object_unref (stream);
107 g_object_unref (parser);
108 if (legacy_header_object)
109 g_object_unref (legacy_header_object);
115 _notmuch_repair_crypto_payload_skip_legacy_display (GMimeObject *payload)
117 if (_notmuch_crypto_payload_has_legacy_display (payload)) {
118 return g_mime_multipart_get_part (GMIME_MULTIPART (payload), 1);
125 * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.1 */
127 _notmuch_is_mixed_up_mangled (GMimeObject *part)
129 GMimeMultipart *mpart = NULL;
130 GMimeObject *parts[3] = {NULL, NULL, NULL};
131 GMimeContentType *type = NULL;
132 char *prelude_string = NULL;
133 bool prelude_is_empty;
137 type = g_mime_object_get_content_type (part);
140 if (! g_mime_content_type_is_type (type, "multipart", "mixed"))
142 if (! GMIME_IS_MULTIPART (part)) /* probably impossible */
144 mpart = GMIME_MULTIPART (part);
147 if (g_mime_multipart_get_count (mpart) != 3)
149 parts[0] = g_mime_multipart_get_part (mpart, 0);
150 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[0]),
153 if (! GMIME_IS_TEXT_PART (parts[0]))
155 parts[1] = g_mime_multipart_get_part (mpart, 1);
156 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[1]),
157 "application", "pgp-encrypted"))
159 parts[2] = g_mime_multipart_get_part (mpart, 2);
160 if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[2]),
161 "application", "octet-stream"))
164 /* Is parts[0] length 0? */
165 prelude_string = g_mime_text_part_get_text (GMIME_TEXT_PART (parts[0]));
166 prelude_is_empty = (prelude_string[0] == '\0');
167 g_free (prelude_string);
168 if (! prelude_is_empty)
171 /* FIXME: after decoding and stripping whitespace, is parts[1]
172 * subpart just "Version: 1" ? */
174 /* FIXME: can we determine that parts[2] subpart is *only* PGP
175 * encrypted data? I tried g_mime_part_get_openpgp_data () but
176 * found https://github.com/jstedfast/gmime/issues/60 */
183 * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.2 */
185 _notmuch_repair_mixed_up_mangled (GMimeObject *part)
187 GMimeMultipart *mpart = NULL, *mpart_ret = NULL;
188 GMimeObject *ret = NULL;
190 if (! _notmuch_is_mixed_up_mangled (part))
192 mpart = GMIME_MULTIPART (part);
193 ret = GMIME_OBJECT (g_mime_multipart_encrypted_new ());
196 mpart_ret = GMIME_MULTIPART (ret);
197 if (mpart_ret == NULL) {
198 g_object_unref (ret);
201 g_mime_object_set_content_type_parameter (ret, "protocol", "application/pgp-encrypted");
203 g_mime_multipart_insert (mpart_ret, 0, g_mime_multipart_get_part (mpart, 1));
204 g_mime_multipart_insert (mpart_ret, 1, g_mime_multipart_get_part (mpart, 2));