Add some better PGP header stripping code
Some checks failed
moparisthebest/sendxmpp-rs/pipeline/head There was a failure building this commit

This commit is contained in:
Travis Burtrum 2021-01-15 02:08:42 -05:00
parent 209a0ae2a3
commit b86c277c8b

View File

@ -172,12 +172,27 @@ fn gpg_encrypt(to: Jid, body: &str) -> Result<String> {
let output = output.stdout; 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()); bail!("length {} returned by gpg too short to be valid", output.len());
} }
let start = 28; // length of -----BEGIN PGP MESSAGE----- is 28 // find first newline+dash after the start
let end = output.len() - 26; // length of -----END PGP MESSAGE----- is 26 let end = first_index_of(start, &output, &[10, 45])?;
Ok(String::from_utf8((&output[start..end]).to_vec())?) Ok(String::from_utf8((&output[start..end]).to_vec())?)
} }
fn first_index_of(start_index: usize, haystack: &[u8], needle: &[u8]) -> Result<usize> {
for i in start_index..haystack.len()-needle.len()+1 {
if haystack[i..i+needle.len()] == needle[..] {
return Ok(i);
}
}
Err(anyhow!("not found"))
}