use revoker::RevokeError;
use revoker::Revokable;
#[derive(Debug, Hash, PartialEq, Eq)]
pub enum ValidationError {
SignatureInvalid,
ParentInvalid,
Expired,
Revoked,
Other,
}
impl From<RevokeError> for ValidationError {
fn from(r: RevokeError) -> ValidationError {
match r {
RevokeError::Revoked => ValidationError::Revoked,
_ => ValidationError::Other,
}
}
}
pub trait Validatable {
fn self_validate<T: Validator>(&self, validator: &T) -> Result<(), ValidationError>;
}
pub trait Validator {
fn is_valid<V: Validatable + Revokable>(&self, cert: &V) -> Result<(), ValidationError>;
fn is_signature_valid(&self, data: &[u8], signature: &[u8]) -> bool;
}
#[test]
fn test_validator() {
use ed25519;
use chrono::Timelike;
use chrono::UTC;
use chrono::duration::Duration;
use meta::Meta;
use certificate::Certificate;
use root_validator::RootValidator;
use revoker::NoRevoker;
let (mpk, msk) = ed25519::generate_keypair();
let cv = RootValidator::new(&mpk, NoRevoker);
let meta = Meta::new_empty();
let expires = UTC::now()
.checked_add(Duration::days(90))
.expect("Failed to add 90 days to expiration date.")
.with_nanosecond(0)
.unwrap();
let mut cert = Certificate::generate_random(meta.clone(), expires.clone());
cert.sign_with_master(&msk);
assert_eq!(cv.is_valid(&cert).is_ok(), true);
let cert_invalid = Certificate::generate_random(meta.clone(), expires.clone());
assert_eq!(cv.is_valid(&cert_invalid).is_ok(), false);
}
#[test]
fn test_meta_can_sign() {
use ed25519;
use chrono::Timelike;
use chrono::UTC;
use chrono::duration::Duration;
use meta::Meta;
use certificate::Certificate;
use root_validator::RootValidator;
use revoker::NoRevoker;
let (mpk, msk) = ed25519::generate_keypair();
let cv = RootValidator::new(&mpk, NoRevoker);
let mut meta = Meta::new_empty();
let expires = UTC::now()
.checked_add(Duration::days(90))
.expect("Failed to add 90 days to expiration date.")
.with_nanosecond(0)
.unwrap();
{
let mut cert = Certificate::generate_random(meta.clone(), expires.clone());
cert.sign_with_master(&msk);
assert_eq!(cv.is_valid(&cert).is_ok(), true);
let mut cert_child = Certificate::generate_random(meta.clone(), expires.clone());
cert.sign_certificate(&mut cert_child).expect("Failed to sign certificate");
assert_eq!(cv.is_valid(&cert_child).is_ok(), false);
}
{
meta.set("use-for", "[\"edcert.sign\"]");
let mut cert = Certificate::generate_random(meta.clone(), expires.clone());
cert.sign_with_master(&msk);
assert_eq!(cv.is_valid(&cert).is_ok(), true);
let mut cert_child = Certificate::generate_random(meta.clone(), expires.clone());
cert.sign_certificate(&mut cert_child).expect("Failed to sign certificate");
assert_eq!(cv.is_valid(&cert_child).is_ok(), true);
}
}