use tor_checkable::TimeRangeBound;
use tor_error::internal;
use tor_llcrypto::pk::rsa::RsaIdentity;
use tor_netdoc::{
doc::{
authcert::{AuthCert, AuthCertKeyIds},
microdesc::Microdesc,
netstatus::{
ConsensusFlavor, ConsensusVerifiabilityError, ConsensusVerifyFailed, Lifetime, md,
plain,
},
routerdesc::{ExtraInfoDigests, RouterDesc, RouterDescUnverified},
},
parse2::{NetdocParseable, NetdocParseableUnverified, SignaturesData},
};
use crate::database::{Sha1, Sha256};
pub(crate) trait FlavoredConsensusBody: Clone {
fn lifetime(&self) -> &Lifetime;
fn doc_digests(&self) -> impl Iterator<Item = (Option<Sha1>, Option<Sha256>)>;
}
pub(crate) trait FlavoredConsensusSignatures: Clone {
fn signatories(&self) -> Vec<AuthCertKeyIds>;
}
pub(crate) trait FlavoredConsensusUnverified:
NetdocParseableUnverified<Body: FlavoredConsensusBody, Signatures: FlavoredConsensusSignatures>
+ NetdocParseable
+ Clone
{
fn flavor() -> ConsensusFlavor;
fn can_verify(
&self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<(), ConsensusVerifiabilityError>;
fn verify(
self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<TimeRangeBound<Self::Body>, ConsensusVerifyFailed>;
fn sigs(&self) -> &Self::Signatures {
&self.inspect_unverified().1.sigs
}
}
impl FlavoredConsensusBody for plain::NetworkStatus {
fn lifetime(&self) -> &Lifetime {
&self.preamble.lifetime
}
fn doc_digests(&self) -> impl Iterator<Item = (Option<Sha1>, Option<Sha256>)> {
self.routers
.iter()
.map(|r| (Some(Sha1::from(*r.doc_digest())), None))
}
}
impl FlavoredConsensusBody for md::NetworkStatus {
fn lifetime(&self) -> &Lifetime {
&self.preamble.lifetime
}
fn doc_digests(&self) -> impl Iterator<Item = (Option<Sha1>, Option<Sha256>)> {
self.routers
.iter()
.map(|r| (None, Some(Sha256::from(*r.doc_digest()))))
}
}
impl FlavoredConsensusSignatures for plain::NetworkStatusSignatures {
fn signatories(&self) -> Vec<AuthCertKeyIds> {
self.directory_signature
.iter()
.map(|sig| sig.key_ids)
.collect()
}
}
impl FlavoredConsensusSignatures for md::NetworkStatusSignatures {
fn signatories(&self) -> Vec<AuthCertKeyIds> {
self.directory_signature
.iter()
.map(|sig| sig.key_ids)
.collect()
}
}
impl FlavoredConsensusUnverified for plain::NetworkStatusUnverified {
fn flavor() -> ConsensusFlavor {
ConsensusFlavor::Plain
}
fn can_verify(
&self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<(), ConsensusVerifiabilityError> {
self.can_verify(trusted_authorities, certs_already)
}
fn verify(
self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<TimeRangeBound<Self::Body>, ConsensusVerifyFailed> {
self.verify(trusted_authorities, certs_already)
}
}
impl FlavoredConsensusUnverified for md::NetworkStatusUnverified {
fn flavor() -> ConsensusFlavor {
ConsensusFlavor::Microdesc
}
fn can_verify(
&self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<(), ConsensusVerifiabilityError> {
self.can_verify(trusted_authorities, certs_already)
}
fn verify(
self,
trusted_authorities: &[RsaIdentity],
certs_already: &[AuthCert],
) -> Result<TimeRangeBound<Self::Body>, ConsensusVerifyFailed> {
self.verify(trusted_authorities, certs_already)
}
}
pub(crate) trait FlavoredDescriptor: Clone {
fn flavor() -> ConsensusFlavor;
fn id_rsa(&self) -> Option<RsaIdentity>;
fn extra_info_sha1(&self) -> Option<ExtraInfoDigests>;
fn sha1(&self) -> Result<Sha1, tor_error::Bug>;
fn sha256(&self) -> Result<Sha256, tor_error::Bug>;
}
pub(crate) type FlavoredDescriptorPlain = (RouterDesc, SignaturesData<RouterDescUnverified>);
impl FlavoredDescriptor for FlavoredDescriptorPlain {
fn flavor() -> ConsensusFlavor {
ConsensusFlavor::Plain
}
fn id_rsa(&self) -> Option<RsaIdentity> {
Some(self.0.signing_key.to_rsa_identity())
}
fn extra_info_sha1(&self) -> Option<ExtraInfoDigests> {
self.0.extra_info_digest.clone()
}
fn sha1(&self) -> Result<Sha1, tor_error::Bug> {
Ok(Sha1(self.1.hashes.sha1.ok_or_else(|| {
internal!("router-signature should be present")
})?))
}
fn sha256(&self) -> Result<Sha256, tor_error::Bug> {
Ok(Sha256(self.1.hashes.sha256.ok_or_else(|| {
internal!("router-sig-ed25519 should be present")
})?))
}
}
pub(crate) type FlavoredDescriptorMd = (Microdesc, Sha1, Sha256);
impl FlavoredDescriptor for FlavoredDescriptorMd {
fn flavor() -> ConsensusFlavor {
ConsensusFlavor::Microdesc
}
fn id_rsa(&self) -> Option<RsaIdentity> {
None
}
fn extra_info_sha1(&self) -> Option<ExtraInfoDigests> {
None
}
fn sha1(&self) -> Result<Sha1, tor_error::Bug> {
Ok(self.1)
}
fn sha256(&self) -> Result<Sha256, tor_error::Bug> {
Ok(self.2)
}
}