checkable: Add a simple type for self-signed objects.
This commit is contained in:
parent
4ec97aa613
commit
63adba1744
|
@ -7,4 +7,7 @@ license = "MIT OR Apache-2.0"
|
|||
publish = false
|
||||
|
||||
[dependencies]
|
||||
signature = "*"
|
||||
thiserror = "*"
|
||||
|
||||
tor-llcrypto = { path="../tor-llcrypto", version = "*" }
|
|
@ -17,6 +17,7 @@
|
|||
use std::time;
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod signed;
|
||||
pub mod timed;
|
||||
|
||||
/// An error that can occur when checking whether a Timebound object is
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
//! Convenience implementation of a SelfSigned object.
|
||||
|
||||
use tor_llcrypto::pk::ValidatableSignature;
|
||||
|
||||
/// A SignatureGated object is a self-signed object that's well-signed
|
||||
/// when one or more ValidatableSignature objects are correct.
|
||||
pub struct SignatureGated<T> {
|
||||
obj: T,
|
||||
signatures: Vec<Box<dyn ValidatableSignature>>,
|
||||
}
|
||||
|
||||
impl<T> SignatureGated<T> {
|
||||
/// Return a new SignatureGated object that will be treated as
|
||||
/// correct if every one if the given set of signatures is valid.
|
||||
pub fn new(obj: T, signatures: Vec<Box<dyn ValidatableSignature>>) -> Self {
|
||||
SignatureGated { obj, signatures }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> super::SelfSigned<T> for SignatureGated<T> {
|
||||
type Error = signature::Error;
|
||||
fn dangerously_assume_wellsigned(self) -> T {
|
||||
self.obj
|
||||
}
|
||||
fn is_well_signed(&self) -> Result<(), Self::Error> {
|
||||
if self.signatures.iter().all(|b| b.is_valid()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(signature::Error::new())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue