Trait x509_parser::validate::Validator
source · [−]pub trait Validator<'a> {
type Item;
fn validate<L: Logger>(&self, item: &'a Self::Item, l: &mut L) -> bool;
fn chain<V2>(self, v2: V2) -> ChainValidator<'a, Self, V2, Self::Item>
where
Self: Sized,
V2: Validator<'a, Item = Self::Item>,
{ ... }
}
Available on crate feature
validate
only.Expand description
Trait for build item validators (for ex. validate X.509 structure)
See X509StructureValidator
for a default implementation, validating the
DER structure of a X.509 Certificate.
See implementors of the Logger
trait for methods to collect or handle warnings and errors.
Examples
Collecting warnings and errors to Vec
:
use x509_parser::certificate::X509Certificate;
use x509_parser::validate::*;
#[cfg(feature = "validate")]
fn validate_certificate(x509: &X509Certificate<'_>) -> Result<(), &'static str> {
let mut logger = VecLogger::default();
println!(" Subject: {}", x509.subject());
// validate and print warnings and errors to stderr
let ok = X509StructureValidator.validate(&x509, &mut logger);
print!("Structure validation status: ");
if ok {
println!("Ok");
} else {
println!("FAIL");
}
for warning in logger.warnings() {
eprintln!(" [W] {}", warning);
}
for error in logger.errors() {
eprintln!(" [E] {}", error);
}
println!();
if !logger.errors().is_empty() {
return Err("validation failed");
}
Ok(())
}