From b86c277c8badcb9c35c8f6ea25b183ea6d0ce1bc Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Fri, 15 Jan 2021 02:08:42 -0500 Subject: [PATCH] Add some better PGP header stripping code --- src/main.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8bd3256..d9b3b49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -172,12 +172,27 @@ fn gpg_encrypt(to: Jid, body: &str) -> Result { let output = output.stdout; - if output.len() < (28+26+10) { // 10 is just a... fudge factor + // strip off headers per https://xmpp.org/extensions/xep-0027.html + // header spec: https://tools.ietf.org/html/rfc4880#section-6.2 + + // find index of leading blank line (2 newlines in a row) + let start = first_index_of(0, &output, &[10, 10])? + 2; + + if output.len() <= start { bail!("length {} returned by gpg too short to be valid", output.len()); } - let start = 28; // length of -----BEGIN PGP MESSAGE----- is 28 - let end = output.len() - 26; // length of -----END PGP MESSAGE----- is 26 + // find first newline+dash after the start + let end = first_index_of(start, &output, &[10, 45])?; Ok(String::from_utf8((&output[start..end]).to_vec())?) } + +fn first_index_of(start_index: usize, haystack: &[u8], needle: &[u8]) -> Result { + for i in start_index..haystack.len()-needle.len()+1 { + if haystack[i..i+needle.len()] == needle[..] { + return Ok(i); + } + } + Err(anyhow!("not found")) +}