From da6eeedf9406f360729e887b8159aa33f3b439de Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Thu, 7 Feb 2013 18:05:24 +0100 Subject: [PATCH 1/3] Ignore stray OpenPGP ASCII armor encryption headers .. that aren't located at the start of the buffer or line. This makes makes it less likely that unencrypted messages are shown as encrypted and trigger decryption errors when being displayed. RFC 4880 6.2. allows trailing but no leading white-space. --- src/plugins/pgpinline/pgpinline.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c index 17523a1..a1ab276 100644 --- a/src/plugins/pgpinline/pgpinline.c +++ b/src/plugins/pgpinline/pgpinline.c @@ -173,6 +173,22 @@ static gchar *get_part_as_string(MimeInfo *mimeinfo) return textdata; } +static gchar *pgpinline_locate_armor_header(gchar *textdata, const gchar *armor_header) +{ + gchar *pos; + + pos = strstr(textdata, armor_header); + /* + * It's only a valid armor header if it's at the + * beginning of the buffer or a new line. + */ + if (pos != NULL && (pos == textdata || *(pos-1) == '\n')) + { + return pos; + } + return NULL; +} + static gboolean pgpinline_is_signed(MimeInfo *mimeinfo) { PrivacyDataPGP *data = NULL; @@ -364,7 +380,7 @@ static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo) if (!textdata) return FALSE; - if (!strstr(textdata, enc_indicator)) { + if (!pgpinline_locate_armor_header(textdata, enc_indicator)) { g_free(textdata); return FALSE; } @@ -456,7 +472,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo) } /* Store any part before encrypted text */ - pos = strstr(textdata, begin_indicator); + pos = pgpinline_locate_armor_header(textdata, begin_indicator); if (pos != NULL && (pos - textdata) > 0) { if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) { FILE_OP_ERROR(fname, "fwrite"); @@ -491,7 +507,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo) goto FILE_ERROR; } if (pos != NULL) { - pos = strstr(pos, end_indicator); + pos = pgpinline_locate_armor_header(pos, end_indicator); if (pos != NULL && *pos != '\0') { pos += strlen(end_indicator); if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) { -- 1.8.1.3 From 995ee02f515b1145e9227c38e5eeb8cbf7d25322 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Fri, 8 Feb 2013 09:19:30 +0100 Subject: [PATCH 2/3] Slightly simplify pgpinline_is_signed() by using gpinline_locate_armor_header() --- src/plugins/pgpinline/pgpinline.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c index a1ab276..df1d651 100644 --- a/src/plugins/pgpinline/pgpinline.c +++ b/src/plugins/pgpinline/pgpinline.c @@ -222,13 +222,9 @@ static gboolean pgpinline_is_signed(MimeInfo *mimeinfo) textdata = get_part_as_string(mimeinfo); if (!textdata) return FALSE; - - if ((sigpos = strstr(textdata, sig_indicator)) == NULL) { - g_free(textdata); - return FALSE; - } - if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) { + sigpos = pgpinline_locate_armor_header(textdata, sig_indicator); + if (sigpos == NULL) { g_free(textdata); return FALSE; } -- 1.8.1.3 From 8fdf2888e2723568d378bc3159451e14af74e717 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Fri, 8 Feb 2013 09:26:12 +0100 Subject: [PATCH 3/3] Move a couple of armor headers from the stack to the data section --- src/plugins/pgpinline/pgpinline.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c index df1d651..a388d3b 100644 --- a/src/plugins/pgpinline/pgpinline.c +++ b/src/plugins/pgpinline/pgpinline.c @@ -192,7 +192,7 @@ static gchar *pgpinline_locate_armor_header(gchar *textdata, const gchar *armor_ static gboolean pgpinline_is_signed(MimeInfo *mimeinfo) { PrivacyDataPGP *data = NULL; - const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----"; + static const gchar sig_indicator[] = "-----BEGIN PGP SIGNED MESSAGE-----"; gchar *textdata, *sigpos; cm_return_val_if_fail(mimeinfo != NULL, FALSE); @@ -351,7 +351,7 @@ static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo) static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo) { - const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----"; + static const gchar enc_indicator[] = "-----BEGIN PGP MESSAGE-----"; gchar *textdata; cm_return_val_if_fail(mimeinfo != NULL, FALSE); @@ -400,8 +400,8 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo) gpgme_ctx_t ctx; gchar *chars; size_t len; - const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----"; - const gchar *end_indicator = "-----END PGP MESSAGE-----"; + static const gchar begin_indicator[] = "-----BEGIN PGP MESSAGE-----"; + static const gchar end_indicator[] = "-----END PGP MESSAGE-----"; gchar *pos; if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR) -- 1.8.1.3