Update sig lib, forward more error context, better blind key rotation support
This commit is contained in:
+48
-13
@@ -8,6 +8,27 @@ use uuid::Uuid;
|
||||
|
||||
const REFETCH_DURATION: Duration = Duration::from_secs(60 * 30);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MaybeCached<T> {
|
||||
Cached(T),
|
||||
Fetched(T),
|
||||
}
|
||||
|
||||
impl<T> MaybeCached<T> {
|
||||
pub fn is_cached(&self) -> bool {
|
||||
match self {
|
||||
MaybeCached::Cached(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> T {
|
||||
match self {
|
||||
MaybeCached::Cached(t) | MaybeCached::Fetched(t) => t,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ActorCache {
|
||||
db: Db,
|
||||
@@ -32,19 +53,11 @@ impl ActorCache {
|
||||
self.following.read().await.contains(id)
|
||||
}
|
||||
|
||||
pub async fn get(&self, id: &XsdAnyUri, requests: &Requests) -> Result<Actor, MyError> {
|
||||
if let Some(actor) = self.cache.read().await.get(id) {
|
||||
return Ok(actor.clone());
|
||||
}
|
||||
|
||||
if let Some(actor) = self.lookup(id).await? {
|
||||
self.cache
|
||||
.write()
|
||||
.await
|
||||
.insert(id.clone(), actor.clone(), REFETCH_DURATION);
|
||||
return Ok(actor);
|
||||
}
|
||||
|
||||
pub async fn get_no_cache(
|
||||
&self,
|
||||
id: &XsdAnyUri,
|
||||
requests: &Requests,
|
||||
) -> Result<Actor, MyError> {
|
||||
let accepted_actor = requests.fetch::<AcceptedActors>(id.as_str()).await?;
|
||||
|
||||
let input_host = id.as_url().host();
|
||||
@@ -85,6 +98,28 @@ impl ActorCache {
|
||||
Ok(actor)
|
||||
}
|
||||
|
||||
pub async fn get(
|
||||
&self,
|
||||
id: &XsdAnyUri,
|
||||
requests: &Requests,
|
||||
) -> Result<MaybeCached<Actor>, MyError> {
|
||||
if let Some(actor) = self.cache.read().await.get(id) {
|
||||
return Ok(MaybeCached::Cached(actor.clone()));
|
||||
}
|
||||
|
||||
if let Some(actor) = self.lookup(id).await? {
|
||||
self.cache
|
||||
.write()
|
||||
.await
|
||||
.insert(id.clone(), actor.clone(), REFETCH_DURATION);
|
||||
return Ok(MaybeCached::Cached(actor));
|
||||
}
|
||||
|
||||
self.get_no_cache(id, requests)
|
||||
.await
|
||||
.map(MaybeCached::Fetched)
|
||||
}
|
||||
|
||||
pub async fn follower(&self, actor: &Actor) -> Result<(), MyError> {
|
||||
self.save(actor.clone()).await
|
||||
}
|
||||
|
||||
+2
-2
@@ -80,8 +80,8 @@ pub enum MyError {
|
||||
#[error("Timed out while waiting on db pool, {0:?}")]
|
||||
DbTimeout(TimeoutType),
|
||||
|
||||
#[error("Invalid algorithm provided to verifier")]
|
||||
Algorithm,
|
||||
#[error("Invalid algorithm provided to verifier, {0}")]
|
||||
Algorithm(String),
|
||||
|
||||
#[error("Object has already been relayed")]
|
||||
Duplicate,
|
||||
|
||||
+48
-25
@@ -2,7 +2,7 @@ use crate::{data::ActorCache, error::MyError, requests::Requests};
|
||||
use activitystreams::primitives::XsdAnyUri;
|
||||
use actix_web::web;
|
||||
use http_signature_normalization_actix::{prelude::*, verify::DeprecatedAlgorithm};
|
||||
use log::{error, warn};
|
||||
use log::error;
|
||||
use rsa::{hash::Hashes, padding::PaddingScheme, PublicKey, RSAPublicKey};
|
||||
use rsa_pem::KeyExt;
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -22,37 +22,64 @@ impl MyVerify {
|
||||
let mut uri: XsdAnyUri = key_id.parse()?;
|
||||
uri.as_url_mut().set_fragment(None);
|
||||
let actor = self.1.get(&uri, &self.0).await?;
|
||||
|
||||
let public_key = RSAPublicKey::from_pem_pkcs8(&actor.public_key)?;
|
||||
let was_cached = actor.is_cached();
|
||||
let actor = actor.into_inner();
|
||||
|
||||
match algorithm {
|
||||
Some(Algorithm::Hs2019) => (),
|
||||
Some(Algorithm::Deprecated(DeprecatedAlgorithm::RsaSha256)) => (),
|
||||
other => {
|
||||
warn!("Invalid algorithm supplied for signature, {:?}", other);
|
||||
return Err(MyError::Algorithm);
|
||||
Some(other) => {
|
||||
return Err(MyError::Algorithm(other.to_string()));
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
|
||||
web::block(move || {
|
||||
let decoded = base64::decode(signature)?;
|
||||
let hashed = Sha256::digest(signing_string.as_bytes());
|
||||
let res = do_verify(&actor.public_key, signature.clone(), signing_string.clone()).await;
|
||||
|
||||
public_key.verify(
|
||||
PaddingScheme::PKCS1v15,
|
||||
Some(&Hashes::SHA2_256),
|
||||
&hashed,
|
||||
&decoded,
|
||||
)?;
|
||||
if let Err(e) = res {
|
||||
if !was_cached {
|
||||
return Err(e);
|
||||
}
|
||||
} else {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
Ok(()) as Result<(), MyError>
|
||||
})
|
||||
.await?;
|
||||
// Previously we verified the sig from an actor's local cache
|
||||
//
|
||||
// Now we make sure we fetch an updated actor
|
||||
let actor = self.1.get_no_cache(&uri, &self.0).await?;
|
||||
|
||||
do_verify(&actor.public_key, signature, signing_string).await?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
async fn do_verify(
|
||||
public_key: &str,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Result<(), MyError> {
|
||||
let public_key = RSAPublicKey::from_pem_pkcs8(public_key)?;
|
||||
|
||||
web::block(move || {
|
||||
let decoded = base64::decode(signature)?;
|
||||
let hashed = Sha256::digest(signing_string.as_bytes());
|
||||
|
||||
public_key.verify(
|
||||
PaddingScheme::PKCS1v15,
|
||||
Some(&Hashes::SHA2_256),
|
||||
&hashed,
|
||||
&decoded,
|
||||
)?;
|
||||
|
||||
Ok(()) as Result<(), MyError>
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl SignatureVerify for MyVerify {
|
||||
type Error = MyError;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<bool, Self::Error>>>>;
|
||||
@@ -60,14 +87,10 @@ impl SignatureVerify for MyVerify {
|
||||
fn signature_verify(
|
||||
&mut self,
|
||||
algorithm: Option<Algorithm>,
|
||||
key_id: &str,
|
||||
signature: &str,
|
||||
signing_string: &str,
|
||||
key_id: String,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Self::Future {
|
||||
let key_id = key_id.to_owned();
|
||||
let signature = signature.to_owned();
|
||||
let signing_string = signing_string.to_owned();
|
||||
|
||||
let this = self.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ pub async fn route(
|
||||
) -> Result<HttpResponse, MyError> {
|
||||
let input = input.into_inner();
|
||||
|
||||
let actor = actors.get(&input.actor, &client).await?;
|
||||
let actor = actors.get(&input.actor, &client).await?.into_inner();
|
||||
|
||||
let (is_blocked, is_whitelisted, is_listener) = join!(
|
||||
state.is_blocked(&actor.id),
|
||||
|
||||
Reference in New Issue
Block a user