From c08eff5da06ae6df459d1e45133c2113557927fe Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Thu, 26 Sep 2019 12:36:49 +0200 Subject: net: Bail out if the received keyid does not match. - Arguably, the user wanted to fetch a key with a certain ID. If the server returns something different, we throw an error. That error contains both the expected keyid as well as the TPK from the server, in case the consumer wants to inspect the problem or make use of the key regardless. --- net/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/net/src/lib.rs b/net/src/lib.rs index 8a37ccc5..277761a8 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -160,6 +160,7 @@ impl KeyServer { /// Retrieves the key with the given `keyid`. pub fn get(&mut self, keyid: &KeyID) -> Box + 'static> { + let keyid_want = keyid.clone(); let uri = self.uri.join( &format!("pks/lookup?op=get&options=mr&search=0x{}", keyid.to_hex())); @@ -180,7 +181,22 @@ impl KeyServer { c, armor::ReaderMode::Tolerant( Some(armor::Kind::PublicKey))); - future::done(TPK::from_reader(r)) + match TPK::from_reader(r) { + Ok(tpk) => { + if tpk.keys_all().any(|(_, _, key)| { + key.fingerprint().to_keyid() + == keyid_want + }) { + future::done(Ok(tpk)) + } else { + future::err(Error::MismatchedKeyID( + keyid_want, tpk).into()) + } + }, + Err(e) => { + future::err(e.into()) + } + } }, StatusCode::NOT_FOUND => future::err(Error::NotFound.into()), @@ -283,6 +299,9 @@ pub enum Error { /// A requested key was not found. #[fail(display = "Key not found")] NotFound, + /// Mismatched key ID + #[fail(display = "Mismatched key ID, expected {}", _0)] + MismatchedKeyID(KeyID, TPK), /// A given keyserver URI was malformed. #[fail(display = "Malformed URI; expected hkp: or hkps:")] MalformedUri, -- cgit v1.2.3