use serde::Serialize;
use crate::{DecodeOutcome, Error, KNOWN_COMPATIBLE_NAMESPACES};
#[derive(Clone, Debug, Serialize)]
pub struct Limits {
pub max_input_bytes: u64,
pub max_documents: usize,
pub max_base64_chars: usize,
pub max_decoded_document_bytes: u64,
pub max_total_decoded_bytes: u64,
pub max_zip_members: usize,
pub max_zip_expanded_bytes: u64,
pub max_zip_compression_ratio: u64,
pub max_xml_depth: usize,
pub max_xml_nodes: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_input_bytes: 64 * 1024 * 1024,
max_documents: 256,
max_base64_chars: 96 * 1024 * 1024,
max_decoded_document_bytes: 64 * 1024 * 1024,
max_total_decoded_bytes: 256 * 1024 * 1024,
max_zip_members: 16,
max_zip_expanded_bytes: 64 * 1024 * 1024,
max_zip_compression_ratio: 100,
max_xml_depth: 128,
max_xml_nodes: 1_000_000,
}
}
}
#[derive(Clone, Debug)]
pub struct ParseOptions {
pub limits: Limits,
pub allowed_namespaces: Vec<String>,
}
impl Default for ParseOptions {
fn default() -> Self {
Self {
limits: Limits::default(),
allowed_namespaces: KNOWN_COMPATIBLE_NAMESPACES
.iter()
.map(|namespace| (*namespace).to_owned())
.collect(),
}
}
}
impl ParseOptions {
pub fn with_limits(limits: Limits) -> Self {
Self {
limits,
..Self::default()
}
}
pub(crate) fn allows(&self, namespace: &str) -> bool {
self.allowed_namespaces
.iter()
.any(|allowed| allowed == namespace)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum StructuralWarningCode {
DanglingObjref,
DocumentWithoutProfile,
SourceSizeMissing,
CreationDateMissing,
SignatureInventoryTruncated,
}
impl StructuralWarningCode {
pub const fn as_str(self) -> &'static str {
match self {
Self::DanglingObjref => "dangling_objref",
Self::DocumentWithoutProfile => "document_without_profile",
Self::SourceSizeMissing => "source_size_missing",
Self::CreationDateMissing => "creation_date_missing",
Self::SignatureInventoryTruncated => "signature_inventory_truncated",
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct StructuralWarning {
pub code: StructuralWarningCode,
pub message: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct MimeType {
pub media_type: String,
pub subtype: String,
pub extension: Option<String>,
pub charset: Option<String>,
}
impl MimeType {
pub fn essence(&self) -> String {
format!("{}/{}", self.media_type, self.subtype)
}
}
#[derive(Clone, Debug, Serialize)]
pub struct Document {
pub index: usize,
pub title: String,
pub creation_date: String,
pub mime_type: MimeType,
pub source_size: Option<u64>,
pub object_ref: String,
pub transforms: Vec<String>,
pub nested_dossier: bool,
#[serde(skip)]
pub(crate) payload: String,
}
pub const MAX_INVENTORIED_SIGNATURES: usize = 64;
pub const MAX_INVENTORIED_TIMESTAMPS: usize = 64;
pub const MAX_INVENTORIED_REFERENCE_URIS: usize = 16;
pub const MAX_INVENTORIED_XADES_PROPERTIES: usize = 32;
pub const MAX_INVENTORIED_DIGEST_METHODS: usize = 16;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SignaturePlacement {
Document,
Dossier,
NestedInSignature,
Other,
}
impl SignaturePlacement {
pub const fn as_str(self) -> &'static str {
match self {
Self::Document => "document",
Self::Dossier => "dossier",
Self::NestedInSignature => "nested_in_signature",
Self::Other => "other",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TimestampPlacement {
Dossier,
Document,
Other,
}
impl TimestampPlacement {
pub const fn as_str(self) -> &'static str {
match self {
Self::Dossier => "dossier",
Self::Document => "document",
Self::Other => "other",
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub struct SignatureEvidence {
pub certificates: usize,
pub crls: usize,
pub ocsp_responses: usize,
pub signature_timestamps: usize,
pub archive_timestamps: usize,
}
#[derive(Clone, Debug, Serialize)]
pub struct SignatureSummary {
pub id: Option<String>,
pub placement: SignaturePlacement,
pub document_index: Option<usize>,
pub parent_signature_id: Option<String>,
pub canonicalization_method: Option<String>,
pub signature_method: Option<String>,
pub digest_methods: Vec<String>,
pub reference_count: usize,
pub reference_uris: Vec<String>,
pub xades_namespace: Option<String>,
pub xades_properties: Vec<String>,
pub evidence: SignatureEvidence,
pub claimed_signing_time: Option<String>,
pub key_info_certificates: usize,
}
#[derive(Clone, Debug, Serialize)]
pub struct TimestampSummary {
pub placement: TimestampPlacement,
pub document_index: Option<usize>,
pub include_count: usize,
pub has_token: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct Dossier {
pub title: String,
pub category: Option<String>,
pub creation_date: Option<String>,
pub namespace: String,
pub xml_encoding: String,
pub documents: Vec<Document>,
pub signatures_present: usize,
pub timestamps_present: usize,
pub signatures: Vec<SignatureSummary>,
pub timestamps: Vec<TimestampSummary>,
pub warnings: Vec<StructuralWarning>,
}
impl Dossier {
pub fn decode_document(&self, index: usize, limits: &Limits) -> Result<DecodeOutcome, Error> {
crate::decode::decode_document(
self.documents.get(index).ok_or_else(|| {
Error::new(
crate::ErrorCode::InvalidAttribute,
"document index is out of range",
)
})?,
limits,
)
}
}